*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval '(run-hooks 'gnus-load-hook))
29
30 (require 'mail-utils)
31 (require 'timezone)
32 (require 'nnheader)
33
34 (eval-when-compile (require 'cl))
35
36 ;; Site dependent variables.  These variables should be defined in
37 ;; paths.el.
38
39 (defvar gnus-default-nntp-server nil
40   "Specify a default NNTP server.
41 This variable should be defined in paths.el, and should never be set
42 by the user.
43 If you want to change servers, you should use `gnus-select-method'.
44 See the documentation to that variable.")
45
46 (defvar gnus-backup-default-subscribed-newsgroups
47   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
48   "Default default new newsgroups the first time Gnus is run.
49 Should be set in paths.el, and shouldn't be touched by the user.")
50
51 (defvar gnus-local-domain nil
52   "Local domain name without a host name.
53 The DOMAINNAME environment variable is used instead if it is defined.
54 If the `system-name' function returns the full Internet name, there is
55 no need to set this variable.")
56
57 (defvar gnus-local-organization nil
58   "String with a description of what organization (if any) the user belongs to.
59 The ORGANIZATION environment variable is used instead if it is defined.
60 If this variable contains a function, this function will be called
61 with the current newsgroup name as the argument.  The function should
62 return a string.
63
64 In any case, if the string (either in the variable, in the environment
65 variable, or returned by the function) is a file name, the contents of
66 this file will be used as the organization.")
67
68 (defvar gnus-use-generic-from nil
69   "If nil, the full host name will be the system name prepended to the domain name.
70 If this is a string, the full host name will be this string.
71 If this is non-nil, non-string, the domain name will be used as the
72 full host name.")
73
74 (defvar gnus-use-generic-path nil
75   "If nil, use the NNTP server name in the Path header.
76 If stringp, use this; if non-nil, use no host name (user name only).")
77
78 ;; Customization variables
79
80 ;; Don't touch this variable.
81 (defvar gnus-nntp-service "nntp"
82   "*NNTP service name (\"nntp\" or 119).
83 This is an obsolete variable, which is scarcely used.  If you use an
84 nntp server for your newsgroup and want to change the port number
85 used to 899, you would say something along these lines:
86
87  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
88
89 (defvar gnus-nntpserver-file "/etc/nntpserver"
90   "*A file with only the name of the nntp server in it.")
91
92 ;; This function is used to check both the environment variable
93 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
94 ;; an nntp server name default.
95 (defun gnus-getenv-nntpserver ()
96   (or (getenv "NNTPSERVER")
97       (and (file-readable-p gnus-nntpserver-file)
98            (save-excursion
99              (set-buffer (get-buffer-create " *gnus nntp*"))
100              (buffer-disable-undo (current-buffer))
101              (insert-file-contents gnus-nntpserver-file)
102              (let ((name (buffer-string)))
103                (prog1
104                    (if (string-match "^[ \t\n]*$" name)
105                        nil
106                      name)
107                  (kill-buffer (current-buffer))))))))
108
109 (defvar gnus-select-method
110   (nconc
111    (list 'nntp (or (condition-case ()
112                        (gnus-getenv-nntpserver)
113                      (error nil))
114                    (if (and gnus-default-nntp-server
115                             (not (string= gnus-default-nntp-server "")))
116                        gnus-default-nntp-server)
117                    (system-name)))
118    (if (or (null gnus-nntp-service)
119            (equal gnus-nntp-service "nntp"))
120        nil
121      (list gnus-nntp-service)))
122   "*Default method for selecting a newsgroup.
123 This variable should be a list, where the first element is how the
124 news is to be fetched, the second is the address.
125
126 For instance, if you want to get your news via NNTP from
127 \"flab.flab.edu\", you could say:
128
129 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
130
131 If you want to use your local spool, say:
132
133 (setq gnus-select-method (list 'nnspool (system-name)))
134
135 If you use this variable, you must set `gnus-nntp-server' to nil.
136
137 There is a lot more to know about select methods and virtual servers -
138 see the manual for details.")
139
140 (defvar gnus-message-archive-method 
141   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
142              (nnfolder-active-file "~/Mail/archive/active")
143              (nnfolder-get-new-mail nil)
144              (nnfolder-inhibit-expiry t))
145   "*Method used for archiving messages you've sent.
146 This should be a mail method.")
147
148 (defvar gnus-refer-article-method nil
149   "*Preferred method for fetching an article by Message-ID.
150 If you are reading news from the local spool (with nnspool), fetching
151 articles by Message-ID is painfully slow.  By setting this method to an
152 nntp method, you might get acceptable results.
153
154 The value of this variable must be a valid select method as discussed
155 in the documentation of `gnus-select-method'.")
156
157 (defvar gnus-secondary-select-methods nil
158   "*A list of secondary methods that will be used for reading news.
159 This is a list where each element is a complete select method (see
160 `gnus-select-method').
161
162 If, for instance, you want to read your mail with the nnml backend,
163 you could set this variable:
164
165 (setq gnus-secondary-select-methods '((nnml \"\")))")
166
167 (defvar gnus-secondary-servers nil
168   "*List of NNTP servers that the user can choose between interactively.
169 To make Gnus query you for a server, you have to give `gnus' a
170 non-numeric prefix - `C-u M-x gnus', in short.")
171
172 (defvar gnus-nntp-server nil
173   "*The name of the host running the NNTP server.
174 This variable is semi-obsolete.  Use the `gnus-select-method'
175 variable instead.")
176
177 (defvar gnus-startup-file "~/.newsrc"
178   "*Your `.newsrc' file.
179 `.newsrc-SERVER' will be used instead if that exists.")
180
181 (defvar gnus-init-file "~/.gnus"
182   "*Your Gnus elisp startup file.
183 If a file with the .el or .elc suffixes exist, it will be read
184 instead.")
185
186 (defvar gnus-group-faq-directory
187   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
188     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
189     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
190     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
191     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
192     "/ftp@ftp.sunet.se:/pub/usenet/"
193     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
194     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
195     "/ftp@ftp.hk.super.net:/mirror/faqs/")
196   "*Directory where the group FAQs are stored.
197 This will most commonly be on a remote machine, and the file will be
198 fetched by ange-ftp.
199
200 This variable can also be a list of directories.  In that case, the
201 first element in the list will be used by default, and the others will
202 be used as backup sites.
203
204 Note that Gnus uses an aol machine as the default directory.  If this
205 feels fundamentally unclean, just think of it as a way to finally get
206 something of value back from them.
207
208 If the default site is too slow, try one of these:
209
210    North America: mirrors.aol.com                /pub/rtfm/usenet
211                   ftp.seas.gwu.edu               /pub/rtfm
212                   rtfm.mit.edu                   /pub/usenet/news.answers
213    Europe:        ftp.uni-paderborn.de           /pub/FAQ
214                   src.doc.ic.ac.uk               /usenet/news-FAQS
215                   ftp.sunet.se                   /pub/usenet
216    Asia:          nctuccca.edu.tw                /USENET/FAQ
217                   hwarang.postech.ac.kr          /pub/usenet/news.answers
218                   ftp.hk.super.net               /mirror/faqs")
219
220 (defvar gnus-group-archive-directory
221   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
222   "*The address of the (ding) archives.")
223
224 (defvar gnus-group-recent-archive-directory
225   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
226   "*The address of the most recent (ding) articles.")
227
228 (defvar gnus-default-subscribed-newsgroups nil
229   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
230 It should be a list of strings.
231 If it is `t', Gnus will not do anything special the first time it is
232 started; it'll just use the normal newsgroups subscription methods.")
233
234 (defvar gnus-use-cross-reference t
235   "*Non-nil means that cross referenced articles will be marked as read.
236 If nil, ignore cross references.  If t, mark articles as read in
237 subscribed newsgroups.  If neither t nor nil, mark as read in all
238 newsgroups.")
239
240 (defvar gnus-single-article-buffer t
241   "*If non-nil, display all articles in the same buffer.
242 If nil, each group will get its own article buffer.")
243
244 (defvar gnus-use-dribble-file t
245   "*Non-nil means that Gnus will use a dribble file to store user updates.
246 If Emacs should crash without saving the .newsrc files, complete
247 information can be restored from the dribble file.")
248
249 (defvar gnus-dribble-directory nil
250   "*The directory where dribble files will be saved.
251 If this variable is nil, the directory where the .newsrc files are
252 saved will be used.")
253
254 (defvar gnus-asynchronous nil
255   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
256
257 (defvar gnus-kill-summary-on-exit t
258   "*If non-nil, kill the summary buffer when you exit from it.
259 If nil, the summary will become a \"*Dead Summary*\" buffer, and
260 it will be killed sometime later.")
261
262 (defvar gnus-large-newsgroup 200
263   "*The number of articles which indicates a large newsgroup.
264 If the number of articles in a newsgroup is greater than this value,
265 confirmation is required for selecting the newsgroup.")
266
267 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
268 (defvar gnus-no-groups-message "No news is horrible news"
269   "*Message displayed by Gnus when no groups are available.")
270
271 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
272   "*Non-nil means that the default name of a file to save articles in is the group name.
273 If it's nil, the directory form of the group name is used instead.
274
275 If this variable is a list, and the list contains the element
276 `not-score', long file names will not be used for score files; if it
277 contains the element `not-save', long file names will not be used for
278 saving; and if it contains the element `not-kill', long file names
279 will not be used for kill files.")
280
281 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
282   "*Name of the directory articles will be saved in (default \"~/News\").
283 Initialized from the SAVEDIR environment variable.")
284
285 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
286   "*Name of the directory where kill files will be stored (default \"~/News\").
287 Initialized from the SAVEDIR environment variable.")
288
289 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
290   "*A function to save articles in your favorite format.
291 The function must be interactively callable (in other words, it must
292 be an Emacs command).
293
294 Gnus provides the following functions:
295
296 * gnus-summary-save-in-rmail (Rmail format)
297 * gnus-summary-save-in-mail (Unix mail format)
298 * gnus-summary-save-in-folder (MH folder)
299 * gnus-summary-save-in-file (article format).
300 * gnus-summary-save-in-vm (use VM's folder format).")
301
302 (defvar gnus-prompt-before-saving 'always
303   "*This variable says how much prompting is to be done when saving articles.
304 If it is nil, no prompting will be done, and the articles will be
305 saved to the default files.  If this variable is `always', each and
306 every article that is saved will be preceded by a prompt, even when
307 saving large batches of articles.  If this variable is neither nil not
308 `always', there the user will be prompted once for a file name for
309 each invocation of the saving commands.")
310
311 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
312   "*A function generating a file name to save articles in Rmail format.
313 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
314
315 (defvar gnus-mail-save-name (function gnus-plain-save-name)
316   "*A function generating a file name to save articles in Unix mail format.
317 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
318
319 (defvar gnus-folder-save-name (function gnus-folder-save-name)
320   "*A function generating a file name to save articles in MH folder.
321 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
322
323 (defvar gnus-file-save-name (function gnus-numeric-save-name)
324   "*A function generating a file name to save articles in article format.
325 The function is called with NEWSGROUP, HEADERS, and optional
326 LAST-FILE.")
327
328 (defvar gnus-split-methods
329   '((gnus-article-archive-name))
330   "*Variable used to suggest where articles are to be saved.
331 For instance, if you would like to save articles related to Gnus in
332 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
333 you could set this variable to something like:
334
335  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
336    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
337
338 This variable is an alist where the where the key is the match and the
339 value is a list of possible files to save in if the match is non-nil.
340
341 If the match is a string, it is used as a regexp match on the
342 article.  If the match is a symbol, that symbol will be funcalled
343 from the buffer of the article to be saved with the newsgroup as the
344 parameter.  If it is a list, it will be evaled in the same buffer.
345
346 If this form or function returns a string, this string will be used as
347 a possible file name; and if it returns a non-nil list, that list will
348 be used as possible file names.")
349
350 (defvar gnus-move-split-methods nil
351   "*Variable used to suggest where articles are to be moved to.
352 It uses the same syntax as the `gnus-split-methods' variable.")
353
354 (defvar gnus-save-score nil
355   "*If non-nil, save group scoring info.")
356
357 (defvar gnus-use-adaptive-scoring nil
358   "*If non-nil, use some adaptive scoring scheme.")
359
360 (defvar gnus-use-cache nil
361   "*If nil, Gnus will ignore the article cache.
362 If `passive', it will allow entering (and reading) articles
363 explicitly entered into the cache.  If anything else, use the
364 cache to the full extent of the law.")
365
366 (defvar gnus-use-trees nil
367   "*If non-nil, display a thread tree buffer.")
368
369 (defvar gnus-use-grouplens nil
370   "*If non-nil, use GroupLens ratings.")
371
372 (defvar gnus-keep-backlog nil
373   "*If non-nil, Gnus will keep read articles for later re-retrieval.
374 If it is a number N, then Gnus will only keep the last N articles
375 read.  If it is neither nil nor a number, Gnus will keep all read
376 articles.  This is not a good idea.")
377
378 (defvar gnus-use-nocem nil
379   "*If non-nil, Gnus will read NoCeM cancel messages.")
380
381 (defvar gnus-use-demon nil
382   "If non-nil, Gnus might use some demons.")
383
384 (defvar gnus-use-scoring t
385   "*If non-nil, enable scoring.")
386
387 (defvar gnus-use-picons nil
388   "*If non-nil, display picons.")
389
390 (defvar gnus-fetch-old-headers nil
391   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
392 If an unread article in the group refers to an older, already read (or
393 just marked as read) article, the old article will not normally be
394 displayed in the Summary buffer.  If this variable is non-nil, Gnus
395 will attempt to grab the headers to the old articles, and thereby
396 build complete threads.  If it has the value `some', only enough
397 headers to connect otherwise loose threads will be displayed.
398 This variable can also be a number.  In that case, no more than that
399 number of old headers will be fetched.
400
401 The server has to support NOV for any of this to work.")
402
403 ;see gnus-cus.el
404 ;(defvar gnus-visual t
405 ;  "*If non-nil, will do various highlighting.
406 ;If nil, no mouse highlights (or any other highlights) will be
407 ;performed.  This might speed up Gnus some when generating large group
408 ;and summary buffers.")
409
410 (defvar gnus-novice-user t
411   "*Non-nil means that you are a usenet novice.
412 If non-nil, verbose messages may be displayed and confirmations may be
413 required.")
414
415 (defvar gnus-expert-user nil
416   "*Non-nil means that you will never be asked for confirmation about anything.
417 And that means *anything*.")
418
419 (defvar gnus-verbose 7
420   "*Integer that says how verbose Gnus should be.
421 The higher the number, the more messages Gnus will flash to say what
422 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
423 display most important messages; and at ten, Gnus will keep on
424 jabbering all the time.")
425
426 (defvar gnus-keep-same-level nil
427   "*Non-nil means that the next newsgroup after the current will be on the same level.
428 When you type, for instance, `n' after reading the last article in the
429 current newsgroup, you will go to the next newsgroup.  If this variable
430 is nil, the next newsgroup will be the next from the group
431 buffer.
432 If this variable is non-nil, Gnus will either put you in the
433 next newsgroup with the same level, or, if no such newsgroup is
434 available, the next newsgroup with the lowest possible level higher
435 than the current level.
436 If this variable is `best', Gnus will make the next newsgroup the one
437 with the best level.")
438
439 (defvar gnus-summary-make-false-root 'adopt
440   "*nil means that Gnus won't gather loose threads.
441 If the root of a thread has expired or been read in a previous
442 session, the information necessary to build a complete thread has been
443 lost.  Instead of having many small sub-threads from this original thread
444 scattered all over the summary buffer, Gnus can gather them.
445
446 If non-nil, Gnus will try to gather all loose sub-threads from an
447 original thread into one large thread.
448
449 If this variable is non-nil, it should be one of `none', `adopt',
450 `dummy' or `empty'.
451
452 If this variable is `none', Gnus will not make a false root, but just
453 present the sub-threads after another.
454 If this variable is `dummy', Gnus will create a dummy root that will
455 have all the sub-threads as children.
456 If this variable is `adopt', Gnus will make one of the \"children\"
457 the parent and mark all the step-children as such.
458 If this variable is `empty', the \"children\" are printed with empty
459 subject fields.  (Or rather, they will be printed with a string
460 given by the `gnus-summary-same-subject' variable.)")
461
462 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
463   "*A regexp to match subjects to be excluded from loose thread gathering.
464 As loose thread gathering is done on subjects only, that means that
465 there can be many false gatherings performed.  By rooting out certain
466 common subjects, gathering might become saner.")
467
468 (defvar gnus-summary-gather-subject-limit nil
469   "*Maximum length of subject comparisons when gathering loose threads.
470 Use nil to compare full subjects.  Setting this variable to a low
471 number will help gather threads that have been corrupted by
472 newsreaders chopping off subject lines, but it might also mean that
473 unrelated articles that have subject that happen to begin with the
474 same few characters will be incorrectly gathered.
475
476 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
477 comparing subjects.")
478
479 (defvar gnus-simplify-ignored-prefixes nil
480   "*Regexp, matches for which are removed from subject lines when simplifying.")
481
482 (defvar gnus-build-sparse-threads nil
483   "*If non-nil, fill in the gaps in threads.
484 If `some', only fill in the gaps that are needed to tie loose threads
485 together.  If `more', fill in all leaf nodes that Gnus can find.  If
486 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
487
488 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
489   "Function used for gathering loose threads.
490 There are two pre-defined functions: `gnus-gather-threads-by-subject',
491 which only takes Subjects into consideration; and
492 `gnus-gather-threads-by-references', which compared the References
493 headers of the articles to find matches.")
494
495 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
496 (defvar gnus-summary-same-subject ""
497   "*String indicating that the current article has the same subject as the previous.
498 This variable will only be used if the value of
499 `gnus-summary-make-false-root' is `empty'.")
500
501 (defvar gnus-summary-goto-unread t
502   "*If non-nil, marking commands will go to the next unread article.
503 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
504 whether it is read or not.")
505
506 (defvar gnus-group-goto-unread t
507   "*If non-nil, movement commands will go to the next unread and subscribed group.")
508
509 (defvar gnus-goto-next-group-when-activating t
510   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
511
512 (defvar gnus-check-new-newsgroups t
513   "*Non-nil means that Gnus will add new newsgroups at startup.
514 If this variable is `ask-server', Gnus will ask the server for new
515 groups since the last time it checked.  This means that the killed list
516 is no longer necessary, so you could set `gnus-save-killed-list' to
517 nil.
518
519 A variant is to have this variable be a list of select methods.  Gnus
520 will then use the `ask-server' method on all these select methods to
521 query for new groups from all those servers.
522
523 Eg.
524   (setq gnus-check-new-newsgroups
525         '((nntp \"some.server\") (nntp \"other.server\")))
526
527 If this variable is nil, then you have to tell Gnus explicitly to
528 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
529
530 (defvar gnus-check-bogus-newsgroups nil
531   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
532 If this variable is nil, then you have to tell Gnus explicitly to
533 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
534
535 (defvar gnus-read-active-file t
536   "*Non-nil means that Gnus will read the entire active file at startup.
537 If this variable is nil, Gnus will only know about the groups in your
538 `.newsrc' file.
539
540 If this variable is `some', Gnus will try to only read the relevant
541 parts of the active file from the server.  Not all servers support
542 this, and it might be quite slow with other servers, but this should
543 generally be faster than both the t and nil value.
544
545 If you set this variable to nil or `some', you probably still want to
546 be told about new newsgroups that arrive.  To do that, set
547 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
548 properly with all servers.")
549
550 (defvar gnus-level-subscribed 5
551   "*Groups with levels less than or equal to this variable are subscribed.")
552
553 (defvar gnus-level-unsubscribed 7
554   "*Groups with levels less than or equal to this variable are unsubscribed.
555 Groups with levels less than `gnus-level-subscribed', which should be
556 less than this variable, are subscribed.")
557
558 (defvar gnus-level-zombie 8
559   "*Groups with this level are zombie groups.")
560
561 (defvar gnus-level-killed 9
562   "*Groups with this level are killed.")
563
564 (defvar gnus-level-default-subscribed 3
565   "*New subscribed groups will be subscribed at this level.")
566
567 (defvar gnus-level-default-unsubscribed 6
568   "*New unsubscribed groups will be unsubscribed at this level.")
569
570 (defvar gnus-activate-level (1+ gnus-level-subscribed)
571   "*Groups higher than this level won't be activated on startup.
572 Setting this variable to something log might save lots of time when
573 you have many groups that you aren't interested in.")
574
575 (defvar gnus-activate-foreign-newsgroups 4
576   "*If nil, Gnus will not check foreign newsgroups at startup.
577 If it is non-nil, it should be a number between one and nine.  Foreign
578 newsgroups that have a level lower or equal to this number will be
579 activated on startup.  For instance, if you want to active all
580 subscribed newsgroups, but not the rest, you'd set this variable to
581 `gnus-level-subscribed'.
582
583 If you subscribe to lots of newsgroups from different servers, startup
584 might take a while.  By setting this variable to nil, you'll save time,
585 but you won't be told how many unread articles there are in the
586 groups.")
587
588 (defvar gnus-save-newsrc-file t
589   "*Non-nil means that Gnus will save the `.newsrc' file.
590 Gnus always saves its own startup file, which is called
591 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
592 be readily understood by other newsreaders.  If you don't plan on
593 using other newsreaders, set this variable to nil to save some time on
594 exit.")
595
596 (defvar gnus-save-killed-list t
597   "*If non-nil, save the list of killed groups to the startup file.
598 If you set this variable to nil, you'll save both time (when starting
599 and quitting) and space (both memory and disk), but it will also mean
600 that Gnus has no record of which groups are new and which are old, so
601 the automatic new newsgroups subscription methods become meaningless.
602
603 You should always set `gnus-check-new-newsgroups' to `ask-server' or
604 nil if you set this variable to nil.")
605
606 (defvar gnus-interactive-catchup t
607   "*If non-nil, require your confirmation when catching up a group.")
608
609 (defvar gnus-interactive-post t
610   "*If non-nil, group name will be asked for when posting.")
611
612 (defvar gnus-interactive-exit t
613   "*If non-nil, require your confirmation when exiting Gnus.")
614
615 (defvar gnus-kill-killed t
616   "*If non-nil, Gnus will apply kill files to already killed articles.
617 If it is nil, Gnus will never apply kill files to articles that have
618 already been through the scoring process, which might very well save lots
619 of time.")
620
621 (defvar gnus-extract-address-components 'gnus-extract-address-components
622   "*Function for extracting address components from a From header.
623 Two pre-defined function exist: `gnus-extract-address-components',
624 which is the default, quite fast, and too simplistic solution, and
625 `mail-extract-address-components', which works much better, but is
626 slower.")
627
628 (defvar gnus-summary-default-score 0
629   "*Default article score level.
630 If this variable is nil, scoring will be disabled.")
631
632 (defvar gnus-summary-zcore-fuzz 0
633   "*Fuzziness factor for the zcore in the summary buffer.
634 Articles with scores closer than this to `gnus-summary-default-score'
635 will not be marked.")
636
637 (defvar gnus-simplify-subject-fuzzy-regexp nil
638   "*Strings to be removed when doing fuzzy matches.
639 This can either be a regular expression or list of regular expressions
640 that will be removed from subject strings if fuzzy subject
641 simplification is selected.")
642
643 (defvar gnus-permanently-visible-groups nil
644   "*Regexp to match groups that should always be listed in the group buffer.
645 This means that they will still be listed when there are no unread
646 articles in the groups.")
647
648 (defvar gnus-list-groups-with-ticked-articles t
649   "*If non-nil, list groups that have only ticked articles.
650 If nil, only list groups that have unread articles.")
651
652 (defvar gnus-group-default-list-level gnus-level-subscribed
653   "*Default listing level.
654 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
655
656 (defvar gnus-group-use-permanent-levels nil
657   "*If non-nil, once you set a level, Gnus will use this level.")
658
659 (defvar gnus-group-list-inactive-groups t
660   "*If non-nil, inactive groups will be listed.")
661
662 (defvar gnus-show-mime nil
663   "*If non-nil, do mime processing of articles.
664 The articles will simply be fed to the function given by
665 `gnus-show-mime-method'.")
666
667 (defvar gnus-strict-mime t
668   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
669
670 (defvar gnus-show-mime-method 'metamail-buffer
671   "*Function to process a MIME message.
672 The function is called from the article buffer.")
673
674 (defvar gnus-decode-encoded-word-method (lambda ())
675   "*Function to decode a MIME encoded-words.
676 The function is called from the article buffer.")
677
678 (defvar gnus-show-threads t
679   "*If non-nil, display threads in summary mode.")
680
681 (defvar gnus-thread-hide-subtree nil
682   "*If non-nil, hide all threads initially.
683 If threads are hidden, you have to run the command
684 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
685 to expose hidden threads.")
686
687 (defvar gnus-thread-hide-killed t
688   "*If non-nil, hide killed threads automatically.")
689
690 (defvar gnus-thread-ignore-subject nil
691   "*If non-nil, ignore subjects and do all threading based on the Reference header.
692 If nil, which is the default, articles that have different subjects
693 from their parents will start separate threads.")
694
695 (defvar gnus-thread-operation-ignore-subject t
696   "*If non-nil, subjects will be ignored when doing thread commands.
697 This affects commands like `gnus-summary-kill-thread' and
698 `gnus-summary-lower-thread'.
699
700 If this variable is nil, articles in the same thread with different
701 subjects will not be included in the operation in question.  If this
702 variable is `fuzzy', only articles that have subjects that are fuzzily
703 equal will be included.")
704
705 (defvar gnus-thread-indent-level 4
706   "*Number that says how much each sub-thread should be indented.")
707
708 (defvar gnus-ignored-newsgroups
709   (purecopy (mapconcat 'identity
710                        '("^to\\."       ; not "real" groups
711                          "^[0-9. \t]+ " ; all digits in name
712                          "[][\"#'()]"   ; bogus characters
713                          )
714                        "\\|"))
715   "*A regexp to match uninteresting newsgroups in the active file.
716 Any lines in the active file matching this regular expression are
717 removed from the newsgroup list before anything else is done to it,
718 thus making them effectively non-existent.")
719
720 (defvar gnus-ignored-headers
721   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
722   "*All headers that match this regexp will be hidden.
723 This variable can also be a list of regexps of headers to be ignored.
724 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
725
726 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
727   "*All headers that do not match this regexp will be hidden.
728 This variable can also be a list of regexp of headers to remain visible.
729 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
730
731 (defvar gnus-sorted-header-list
732   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
733     "^Cc:" "^Date:" "^Organization:")
734   "*This variable is a list of regular expressions.
735 If it is non-nil, headers that match the regular expressions will
736 be placed first in the article buffer in the sequence specified by
737 this list.")
738
739 (defvar gnus-boring-article-headers
740   '(empty followup-to reply-to)
741   "*Headers that are only to be displayed if they have interesting data.
742 Possible values in this list are `empty', `newsgroups', `followup-to',
743 `reply-to', and `date'.")
744
745 (defvar gnus-show-all-headers nil
746   "*If non-nil, don't hide any headers.")
747
748 (defvar gnus-save-all-headers t
749   "*If non-nil, don't remove any headers before saving.")
750
751 (defvar gnus-saved-headers gnus-visible-headers
752   "*Headers to keep if `gnus-save-all-headers' is nil.
753 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
754 If that variable is nil, however, all headers that match this regexp
755 will be kept while the rest will be deleted before saving.")
756
757 (defvar gnus-inhibit-startup-message nil
758   "*If non-nil, the startup message will not be displayed.")
759
760 (defvar gnus-signature-separator "^-- *$"
761   "Regexp matching signature separator.")
762
763 (defvar gnus-signature-limit nil
764   "Provide a limit to what is considered a signature.
765 If it is a number, no signature may not be longer (in characters) than
766 that number.  If it is a function, the function will be called without
767 any parameters, and if it returns nil, there is no signature in the
768 buffer.  If it is a string, it will be used as a regexp.  If it
769 matches, the text in question is not a signature.")
770
771 (defvar gnus-auto-extend-newsgroup t
772   "*If non-nil, extend newsgroup forward and backward when requested.")
773
774 (defvar gnus-auto-select-first t
775   "*If nil, don't select the first unread article when entering a group.
776 If this variable is `best', select the highest-scored unread article
777 in the group.  If neither nil nor `best', select the first unread
778 article.
779
780 If you want to prevent automatic selection of the first unread article
781 in some newsgroups, set the variable to nil in
782 `gnus-select-group-hook'.")
783
784 (defvar gnus-auto-select-next t
785   "*If non-nil, offer to go to the next group from the end of the previous.
786 If the value is t and the next newsgroup is empty, Gnus will exit
787 summary mode and go back to group mode.  If the value is neither nil
788 nor t, Gnus will select the following unread newsgroup.  In
789 particular, if the value is the symbol `quietly', the next unread
790 newsgroup will be selected without any confirmation, and if it is
791 `almost-quietly', the next group will be selected without any
792 confirmation if you are located on the last article in the group.
793 Finally, if this variable is `slightly-quietly', the `Z n' command
794 will go to the next group without confirmation.")
795
796 (defvar gnus-auto-select-same nil
797   "*If non-nil, select the next article with the same subject.")
798
799 (defvar gnus-summary-check-current nil
800   "*If non-nil, consider the current article when moving.
801 The \"unread\" movement commands will stay on the same line if the
802 current article is unread.")
803
804 (defvar gnus-auto-center-summary t
805   "*If non-nil, always center the current summary buffer.
806 In particular, if `vertical' do only vertical recentering.  If non-nil
807 and non-`vertical', do both horizontal and vertical recentering.")
808
809 (defvar gnus-break-pages t
810   "*If non-nil, do page breaking on articles.
811 The page delimiter is specified by the `gnus-page-delimiter'
812 variable.")
813
814 (defvar gnus-page-delimiter "^\^L"
815   "*Regexp describing what to use as article page delimiters.
816 The default value is \"^\^L\", which is a form linefeed at the
817 beginning of a line.")
818
819 (defvar gnus-use-full-window t
820   "*If non-nil, use the entire Emacs screen.")
821
822 (defvar gnus-window-configuration nil
823   "Obsolete variable.  See `gnus-buffer-configuration'.")
824
825 (defvar gnus-window-min-width 2
826   "*Minimum width of Gnus buffers.")
827
828 (defvar gnus-window-min-height 1
829   "*Minimum height of Gnus buffers.")
830
831 (defvar gnus-buffer-configuration
832   '((group
833      (vertical 1.0
834                (group 1.0 point)
835                (if gnus-carpal '(group-carpal 4))))
836     (summary
837      (vertical 1.0
838                (summary 1.0 point)
839                (if gnus-carpal '(summary-carpal 4))))
840     (article
841      (cond 
842       (gnus-use-picons
843        '(frame 1.0
844                (vertical 1.0
845                          (summary 0.25 point)
846                          (if gnus-carpal '(summary-carpal 4))
847                          (article 1.0))
848                (vertical ((height . 5) (width . 15)
849                           (user-position . t)
850                           (left . -1) (top . 1))
851                          (picons 1.0))))
852       (gnus-use-trees
853        '(vertical 1.0
854                   (summary 0.25 point)
855                   (tree 0.25)
856                   (article 1.0)))
857       (t
858        '(vertical 1.0
859                  (summary 0.25 point)
860                  (if gnus-carpal '(summary-carpal 4))
861                  (if gnus-use-trees '(tree 0.25))
862                  (article 1.0)))))
863     (server
864      (vertical 1.0
865                (server 1.0 point)
866                (if gnus-carpal '(server-carpal 2))))
867     (browse
868      (vertical 1.0
869                (browse 1.0 point)
870                (if gnus-carpal '(browse-carpal 2))))
871     (group-mail
872      (vertical 1.0
873                (mail 1.0 point)))
874     (summary-mail
875      (vertical 1.0
876                (mail 1.0 point)))
877     (summary-reply
878      (vertical 1.0
879                (article-copy 0.5)
880                (mail 1.0 point)))
881     (pick
882      (vertical 1.0
883                (article 1.0 point)))
884     (info
885      (vertical 1.0
886                (info 1.0 point)))
887     (summary-faq
888      (vertical 1.0
889                (summary 0.25)
890                (faq 1.0 point)))
891     (edit-group
892      (vertical 1.0
893                (group 0.5)
894                (edit-group 1.0 point)))
895     (edit-server
896      (vertical 1.0
897                (server 0.5)
898                (edit-server 1.0 point)))
899     (edit-score
900      (vertical 1.0
901                (summary 0.25)
902                (edit-score 1.0 point)))
903     (post
904      (vertical 1.0
905                (post 1.0 point)))
906     (reply
907      (vertical 1.0
908                (article-copy 0.5)
909                (mail 1.0 point)))
910     (mail-forward
911      (vertical 1.0
912                (mail 1.0 point)))
913     (post-forward
914      (vertical 1.0
915                (post 1.0 point)))
916     (reply-yank
917      (vertical 1.0
918                (mail 1.0 point)))
919     (mail-bounce
920      (vertical 1.0
921                (article 0.5)
922                (mail 1.0 point)))
923     (draft
924      (vertical 1.0
925                (draft 1.0 point)))
926     (pipe
927      (vertical 1.0
928                (summary 0.25 point)
929                (if gnus-carpal '(summary-carpal 4))
930                ("*Shell Command Output*" 1.0)))
931     (followup
932      (vertical 1.0
933                (article-copy 0.5)
934                (post 1.0 point)))
935     (followup-yank
936      (vertical 1.0
937                (post 1.0 point))))
938   "Window configuration for all possible Gnus buffers.
939 This variable is a list of lists.  Each of these lists has a NAME and
940 a RULE.  The NAMEs are commonsense names like `group', which names a
941 rule used when displaying the group buffer; `summary', which names a
942 rule for what happens when you enter a group and do not display an
943 article buffer; and so on.  See the value of this variable for a
944 complete list of NAMEs.
945
946 Each RULE is a list of vectors.  The first element in this vector is
947 the name of the buffer to be displayed; the second element is the
948 percentage of the screen this buffer is to occupy (a number in the
949 0.0-0.99 range); the optional third element is `point', which should
950 be present to denote which buffer point is to go to after making this
951 buffer configuration.")
952
953 (defvar gnus-window-to-buffer
954   '((group . gnus-group-buffer)
955     (summary . gnus-summary-buffer)
956     (article . gnus-article-buffer)
957     (server . gnus-server-buffer)
958     (browse . "*Gnus Browse Server*")
959     (edit-group . gnus-group-edit-buffer)
960     (edit-server . gnus-server-edit-buffer)
961     (group-carpal . gnus-carpal-group-buffer)
962     (summary-carpal . gnus-carpal-summary-buffer)
963     (server-carpal . gnus-carpal-server-buffer)
964     (browse-carpal . gnus-carpal-browse-buffer)
965     (edit-score . gnus-score-edit-buffer)
966     (mail . gnus-mail-buffer)
967     (post . gnus-post-news-buffer)
968     (faq . gnus-faq-buffer)
969     (picons . "*Picons*")
970     (tree . gnus-tree-buffer)
971     (info . gnus-info-buffer)
972     (article-copy . gnus-article-copy)
973     (draft . gnus-draft-buffer))
974   "Mapping from short symbols to buffer names or buffer variables.")
975
976 (defvar gnus-carpal nil
977   "*If non-nil, display clickable icons.")
978
979 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
980   "*Function called with a group name when new group is detected.
981 A few pre-made functions are supplied: `gnus-subscribe-randomly'
982 inserts new groups at the beginning of the list of groups;
983 `gnus-subscribe-alphabetically' inserts new groups in strict
984 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
985 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
986 for your decision; `gnus-subscribe-killed' kills all new groups.")
987
988 ;; Suggested by a bug report by Hallvard B Furuseth.
989 ;; <h.b.furuseth@usit.uio.no>.
990 (defvar gnus-subscribe-options-newsgroup-method
991   (function gnus-subscribe-alphabetically)
992   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
993 If, for instance, you want to subscribe to all newsgroups in the
994 \"no\" and \"alt\" hierarchies, you'd put the following in your
995 .newsrc file:
996
997 options -n no.all alt.all
998
999 Gnus will the subscribe all new newsgroups in these hierarchies with
1000 the subscription method in this variable.")
1001
1002 (defvar gnus-subscribe-hierarchical-interactive nil
1003   "*If non-nil, Gnus will offer to subscribe hierarchically.
1004 When a new hierarchy appears, Gnus will ask the user:
1005
1006 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1007
1008 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1009 subscribe to all newsgroups in the hierarchy and `s' will skip this
1010 hierarchy in its entirety.")
1011
1012 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1013   "*Function used for sorting the group buffer.
1014 This function will be called with group info entries as the arguments
1015 for the groups to be sorted.  Pre-made functions include
1016 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1017 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1018 `gnus-group-sort-by-rank'.
1019
1020 This variable can also be a list of sorting functions.  In that case,
1021 the most significant sort function should be the last function in the
1022 list.")
1023
1024 ;; Mark variables suggested by Thomas Michanek
1025 ;; <Thomas.Michanek@telelogic.se>.
1026 (defvar gnus-unread-mark ? 
1027   "*Mark used for unread articles.")
1028 (defvar gnus-ticked-mark ?!
1029   "*Mark used for ticked articles.")
1030 (defvar gnus-dormant-mark ??
1031   "*Mark used for dormant articles.")
1032 (defvar gnus-del-mark ?r
1033   "*Mark used for del'd articles.")
1034 (defvar gnus-read-mark ?R
1035   "*Mark used for read articles.")
1036 (defvar gnus-expirable-mark ?E
1037   "*Mark used for expirable articles.")
1038 (defvar gnus-killed-mark ?K
1039   "*Mark used for killed articles.")
1040 (defvar gnus-souped-mark ?F
1041   "*Mark used for killed articles.")
1042 (defvar gnus-kill-file-mark ?X
1043   "*Mark used for articles killed by kill files.")
1044 (defvar gnus-low-score-mark ?Y
1045   "*Mark used for articles with a low score.")
1046 (defvar gnus-catchup-mark ?C
1047   "*Mark used for articles that are caught up.")
1048 (defvar gnus-replied-mark ?A
1049   "*Mark used for articles that have been replied to.")
1050 (defvar gnus-cached-mark ?*
1051   "*Mark used for articles that are in the cache.")
1052 (defvar gnus-saved-mark ?S
1053   "*Mark used for articles that have been saved to.")
1054 (defvar gnus-process-mark ?#
1055   "*Process mark.")
1056 (defvar gnus-ancient-mark ?O
1057   "*Mark used for ancient articles.")
1058 (defvar gnus-sparse-mark ?Q
1059   "*Mark used for sparsely reffed articles.")
1060 (defvar gnus-canceled-mark ?G
1061   "*Mark used for canceled articles.")
1062 (defvar gnus-score-over-mark ?+
1063   "*Score mark used for articles with high scores.")
1064 (defvar gnus-score-below-mark ?-
1065   "*Score mark used for articles with low scores.")
1066 (defvar gnus-empty-thread-mark ? 
1067   "*There is no thread under the article.")
1068 (defvar gnus-not-empty-thread-mark ?=
1069   "*There is a thread under the article.")
1070
1071 (defvar gnus-view-pseudo-asynchronously nil
1072   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1073
1074 (defvar gnus-view-pseudos nil
1075   "*If `automatic', pseudo-articles will be viewed automatically.
1076 If `not-confirm', pseudos will be viewed automatically, and the user
1077 will not be asked to confirm the command.")
1078
1079 (defvar gnus-view-pseudos-separately t
1080   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1081 If nil, all files that use the same viewing command will be given as a
1082 list of parameters to that command.")
1083
1084 (defvar gnus-insert-pseudo-articles t
1085   "*If non-nil, insert pseudo-articles when decoding articles.")
1086
1087 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1088   "*Format of group lines.
1089 It works along the same lines as a normal formatting string,
1090 with some simple extensions.
1091
1092 %M    Only marked articles (character, \"*\" or \" \")
1093 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1094 %L    Level of subscribedness (integer)
1095 %N    Number of unread articles (integer)
1096 %I    Number of dormant articles (integer)
1097 %i    Number of ticked and dormant (integer)
1098 %T    Number of ticked articles (integer)
1099 %R    Number of read articles (integer)
1100 %t    Total number of articles (integer)
1101 %y    Number of unread, unticked articles (integer)
1102 %G    Group name (string)
1103 %g    Qualified group name (string)
1104 %D    Group description (string)
1105 %s    Select method (string)
1106 %o    Moderated group (char, \"m\")
1107 %p    Process mark (char)
1108 %O    Moderated group (string, \"(m)\" or \"\")
1109 %P    Topic indentation (string)
1110 %l    Whether there are GroupLens predictions for this group (string)
1111 %n    Select from where (string)
1112 %z    A string that look like `<%s:%n>' if a foreign select method is used
1113 %u    User defined specifier.  The next character in the format string should
1114       be a letter.  Gnus will call the function gnus-user-format-function-X,
1115       where X is the letter following %u.  The function will be passed the
1116       current header as argument.  The function should return a string, which
1117       will be inserted into the buffer just like information from any other
1118       group specifier.
1119
1120 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1121 the mouse point move inside the area.  There can only be one such area.
1122
1123 Note that this format specification is not always respected.  For
1124 reasons of efficiency, when listing killed groups, this specification
1125 is ignored altogether.  If the spec is changed considerably, your
1126 output may end up looking strange when listing both alive and killed
1127 groups.
1128
1129 If you use %o or %O, reading the active file will be slower and quite
1130 a bit of extra memory will be used. %D will also worsen performance.
1131 Also note that if you change the format specification to include any
1132 of these specs, you must probably re-start Gnus to see them go into
1133 effect.")
1134
1135 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1136   "*The format specification of the lines in the summary buffer.
1137
1138 It works along the same lines as a normal formatting string,
1139 with some simple extensions.
1140
1141 %N   Article number, left padded with spaces (string)
1142 %S   Subject (string)
1143 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1144 %n   Name of the poster (string)
1145 %a   Extracted name of the poster (string)
1146 %A   Extracted address of the poster (string)
1147 %F   Contents of the From: header (string)
1148 %x   Contents of the Xref: header (string)
1149 %D   Date of the article (string)
1150 %d   Date of the article (string) in DD-MMM format
1151 %M   Message-id of the article (string)
1152 %r   References of the article (string)
1153 %c   Number of characters in the article (integer)
1154 %L   Number of lines in the article (integer)
1155 %I   Indentation based on thread level (a string of spaces)
1156 %T   A string with two possible values: 80 spaces if the article
1157      is on thread level two or larger and 0 spaces on level one
1158 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1159 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1160 %[   Opening bracket (character, \"[\" or \"<\")
1161 %]   Closing bracket (character, \"]\" or \">\")
1162 %>   Spaces of length thread-level (string)
1163 %<   Spaces of length (- 20 thread-level) (string)
1164 %i   Article score (number)
1165 %z   Article zcore (character)
1166 %t   Number of articles under the current thread (number).
1167 %e   Whether the thread is empty or not (character).
1168 %l   GroupLens score (number)
1169 %u   User defined specifier.  The next character in the format string should
1170      be a letter.  Gnus will call the function gnus-user-format-function-X,
1171      where X is the letter following %u.  The function will be passed the
1172      current header as argument.  The function should return a string, which
1173      will be inserted into the summary just like information from any other
1174      summary specifier.
1175
1176 Text between %( and %) will be highlighted with `gnus-mouse-face'
1177 when the mouse point is placed inside the area.  There can only be one
1178 such area.
1179
1180 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1181 with care.  For reasons of efficiency, Gnus will compute what column
1182 these characters will end up in, and \"hard-code\" that.  This means that
1183 it is illegal to have these specs after a variable-length spec.  Well,
1184 you might not be arrested, but your summary buffer will look strange,
1185 which is bad enough.
1186
1187 The smart choice is to have these specs as for to the left as
1188 possible.
1189
1190 This restriction may disappear in later versions of Gnus.")
1191
1192 (defvar gnus-summary-dummy-line-format
1193   "*  %(:                          :%) %S\n"
1194   "*The format specification for the dummy roots in the summary buffer.
1195 It works along the same lines as a normal formatting string,
1196 with some simple extensions.
1197
1198 %S  The subject")
1199
1200 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1201   "*The format specification for the summary mode line.
1202 It works along the same lines as a normal formatting string,
1203 with some simple extensions:
1204
1205 %G  Group name
1206 %p  Unprefixed group name
1207 %A  Current article number
1208 %V  Gnus version
1209 %U  Number of unread articles in the group
1210 %e  Number of unselected articles in the group
1211 %Z  A string with unread/unselected article counts
1212 %g  Shortish group name
1213 %S  Subject of the current article
1214 %u  User-defined spec
1215 %s  Current score file name
1216 %d  Number of dormant articles
1217 %r  Number of articles that have been marked as read in this session
1218 %E  Number of articles expunged by the score files")
1219
1220 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1221   "*The format specification for the article mode line.
1222 See `gnus-summary-mode-line-format' for a closer description.")
1223
1224 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1225   "*The format specification for the group mode line.
1226 It works along the same lines as a normal formatting string,
1227 with some simple extensions:
1228
1229 %S   The native news server.
1230 %M   The native select method.")
1231
1232 (defvar gnus-valid-select-methods
1233   '(("nntp" post address prompt-address)
1234     ("nnspool" post address)
1235     ("nnvirtual" post-mail virtual prompt-address)
1236     ("nnmbox" mail respool address)
1237     ("nnml" mail respool address)
1238     ("nnmh" mail respool address)
1239     ("nndir" post-mail prompt-address address)
1240     ("nneething" none address prompt-address)
1241     ("nndoc" none address prompt-address)
1242     ("nnbabyl" mail address respool)
1243     ("nnkiboze" post address virtual)
1244     ("nnsoup" post-mail address)
1245     ("nndraft" post-mail)
1246     ("nnfolder" mail respool address))
1247   "An alist of valid select methods.
1248 The first element of each list lists should be a string with the name
1249 of the select method.  The other elements may be be the category of
1250 this method (ie. `post', `mail', `none' or whatever) or other
1251 properties that this method has (like being respoolable).
1252 If you implement a new select method, all you should have to change is
1253 this variable.  I think.")
1254
1255 (defvar gnus-updated-mode-lines '(group article summary tree)
1256   "*List of buffers that should update their mode lines.
1257 The list may contain the symbols `group', `article' and `summary'.  If
1258 the corresponding symbol is present, Gnus will keep that mode line
1259 updated with information that may be pertinent.
1260 If this variable is nil, screen refresh may be quicker.")
1261
1262 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1263 (defvar gnus-mode-non-string-length nil
1264   "*Max length of mode-line non-string contents.
1265 If this is nil, Gnus will take space as is needed, leaving the rest
1266 of the modeline intact.")
1267
1268 ;see gnus-cus.el
1269 ;(defvar gnus-mouse-face 'highlight
1270 ;  "*Face used for mouse highlighting in Gnus.
1271 ;No mouse highlights will be done if `gnus-visual' is nil.")
1272
1273 (defvar gnus-summary-mark-below nil
1274   "*Mark all articles with a score below this variable as read.
1275 This variable is local to each summary buffer and usually set by the
1276 score file.")
1277
1278 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1279   "*List of functions used for sorting articles in the summary buffer.
1280 This variable is only used when not using a threaded display.")
1281
1282 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1283   "*List of functions used for sorting threads in the summary buffer.
1284 By default, threads are sorted by article number.
1285
1286 Each function takes two threads and return non-nil if the first thread
1287 should be sorted before the other.  If you use more than one function,
1288 the primary sort function should be the last.  You should probably
1289 always include `gnus-thread-sort-by-number' in the list of sorting
1290 functions -- preferably first.
1291
1292 Ready-mady functions include `gnus-thread-sort-by-number',
1293 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1294 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1295 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1296
1297 (defvar gnus-thread-score-function '+
1298   "*Function used for calculating the total score of a thread.
1299
1300 The function is called with the scores of the article and each
1301 subthread and should then return the score of the thread.
1302
1303 Some functions you can use are `+', `max', or `min'.")
1304
1305 (defvar gnus-summary-expunge-below nil
1306   "All articles that have a score less than this variable will be expunged.")
1307
1308 (defvar gnus-thread-expunge-below nil
1309   "All threads that have a total score less than this variable will be expunged.
1310 See `gnus-thread-score-function' for en explanation of what a
1311 \"thread score\" is.")
1312
1313 (defvar gnus-auto-subscribed-groups
1314   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1315   "*All new groups that match this regexp will be subscribed automatically.
1316 Note that this variable only deals with new groups.  It has no effect
1317 whatsoever on old groups.")
1318
1319 (defvar gnus-options-subscribe nil
1320   "*All new groups matching this regexp will be subscribed unconditionally.
1321 Note that this variable deals only with new newsgroups.  This variable
1322 does not affect old newsgroups.")
1323
1324 (defvar gnus-options-not-subscribe nil
1325   "*All new groups matching this regexp will be ignored.
1326 Note that this variable deals only with new newsgroups.  This variable
1327 does not affect old (already subscribed) newsgroups.")
1328
1329 (defvar gnus-auto-expirable-newsgroups nil
1330   "*Groups in which to automatically mark read articles as expirable.
1331 If non-nil, this should be a regexp that should match all groups in
1332 which to perform auto-expiry.  This only makes sense for mail groups.")
1333
1334 (defvar gnus-total-expirable-newsgroups nil
1335   "*Groups in which to perform expiry of all read articles.
1336 Use with extreme caution.  All groups that match this regexp will be
1337 expiring - which means that all read articles will be deleted after
1338 (say) one week.  (This only goes for mail groups and the like, of
1339 course.)")
1340
1341 (defvar gnus-group-uncollapsed-levels 1
1342   "Number of group name elements to leave alone when making a short group name.")
1343
1344 (defvar gnus-hidden-properties '(invisible t intangible t)
1345   "Property list to use for hiding text.")
1346
1347 (defvar gnus-modtime-botch nil
1348   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1349 due to the bogus appearance that .newsrc was modified on disc.")
1350
1351 ;; Hooks.
1352
1353 (defvar gnus-group-mode-hook nil
1354   "*A hook for Gnus group mode.")
1355
1356 (defvar gnus-summary-mode-hook nil
1357   "*A hook for Gnus summary mode.
1358 This hook is run before any variables are set in the summary buffer.")
1359
1360 (defvar gnus-article-mode-hook nil
1361   "*A hook for Gnus article mode.")
1362
1363 (defvar gnus-summary-prepare-exit-hook nil
1364   "*A hook called when preparing to exit from the summary buffer.
1365 It calls `gnus-summary-expire-articles' by default.")
1366 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1367
1368 (defvar gnus-summary-exit-hook nil
1369   "*A hook called on exit from the summary buffer.")
1370
1371 (defvar gnus-group-catchup-group-hook nil
1372   "*A hook run when catching up a group from the group buffer.")
1373
1374 (defvar gnus-open-server-hook nil
1375   "*A hook called just before opening connection to the news server.")
1376
1377 (defvar gnus-load-hook nil
1378   "*A hook run while Gnus is loaded.")
1379
1380 (defvar gnus-startup-hook nil
1381   "*A hook called at startup.
1382 This hook is called after Gnus is connected to the NNTP server.")
1383
1384 (defvar gnus-get-new-news-hook nil
1385   "*A hook run just before Gnus checks for new news.")
1386
1387 (defvar gnus-after-getting-new-news-hook nil
1388   "*A hook run after Gnus checks for new news.")
1389
1390 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1391   "*A function that is called to generate the group buffer.
1392 The function is called with three arguments: The first is a number;
1393 all group with a level less or equal to that number should be listed,
1394 if the second is non-nil, empty groups should also be displayed.  If
1395 the third is non-nil, it is a number.  No groups with a level lower
1396 than this number should be displayed.
1397
1398 The only current function implemented is `gnus-group-prepare-flat'.")
1399
1400 (defvar gnus-group-prepare-hook nil
1401   "*A hook called after the group buffer has been generated.
1402 If you want to modify the group buffer, you can use this hook.")
1403
1404 (defvar gnus-summary-prepare-hook nil
1405   "*A hook called after the summary buffer has been generated.
1406 If you want to modify the summary buffer, you can use this hook.")
1407
1408 (defvar gnus-summary-generate-hook nil
1409   "*A hook run just before generating the summary buffer.
1410 This hook is commonly used to customize threading variables and the
1411 like.")
1412
1413 (defvar gnus-article-prepare-hook nil
1414   "*A hook called after an article has been prepared in the article buffer.
1415 If you want to run a special decoding program like nkf, use this hook.")
1416
1417 ;(defvar gnus-article-display-hook nil
1418 ;  "*A hook called after the article is displayed in the article buffer.
1419 ;The hook is designed to change the contents of the article
1420 ;buffer.  Typical functions that this hook may contain are
1421 ;`gnus-article-hide-headers' (hide selected headers),
1422 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1423 ;`gnus-article-hide-signature' (hide signature) and
1424 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1425 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1426 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1427 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1428
1429 (defvar gnus-article-x-face-command
1430   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1431   "String or function to be executed to display an X-Face header.
1432 If it is a string, the command will be executed in a sub-shell
1433 asynchronously.  The compressed face will be piped to this command.")
1434
1435 (defvar gnus-article-x-face-too-ugly nil
1436   "Regexp matching posters whose face shouldn't be shown automatically.")
1437
1438 (defvar gnus-select-group-hook nil
1439   "*A hook called when a newsgroup is selected.
1440
1441 If you'd like to simplify subjects like the
1442 `gnus-summary-next-same-subject' command does, you can use the
1443 following hook:
1444
1445  (setq gnus-select-group-hook
1446       (list
1447         (lambda ()
1448           (mapcar (lambda (header)
1449                      (mail-header-set-subject
1450                       header
1451                       (gnus-simplify-subject
1452                        (mail-header-subject header) 're-only)))
1453                   gnus-newsgroup-headers))))")
1454
1455 (defvar gnus-select-article-hook nil
1456   "*A hook called when an article is selected.")
1457
1458 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1459   "*A hook called to apply kill files to a group.
1460 This hook is intended to apply a kill file to the selected newsgroup.
1461 The function `gnus-apply-kill-file' is called by default.
1462
1463 Since a general kill file is too heavy to use only for a few
1464 newsgroups, I recommend you to use a lighter hook function.  For
1465 example, if you'd like to apply a kill file to articles which contains
1466 a string `rmgroup' in subject in newsgroup `control', you can use the
1467 following hook:
1468
1469  (setq gnus-apply-kill-hook
1470       (list
1471         (lambda ()
1472           (cond ((string-match \"control\" gnus-newsgroup-name)
1473                  (gnus-kill \"Subject\" \"rmgroup\")
1474                  (gnus-expunge \"X\"))))))")
1475
1476 (defvar gnus-visual-mark-article-hook
1477   (list 'gnus-highlight-selected-summary)
1478   "*Hook run after selecting an article in the summary buffer.
1479 It is meant to be used for highlighting the article in some way.  It
1480 is not run if `gnus-visual' is nil.")
1481
1482 (defvar gnus-parse-headers-hook nil
1483   "*A hook called before parsing the headers.")
1484
1485 (defvar gnus-exit-group-hook nil
1486   "*A hook called when exiting (not quitting) summary mode.")
1487
1488 (defvar gnus-suspend-gnus-hook nil
1489   "*A hook called when suspending (not exiting) Gnus.")
1490
1491 (defvar gnus-exit-gnus-hook nil
1492   "*A hook called when exiting Gnus.")
1493
1494 (defvar gnus-after-exiting-gnus-hook nil
1495   "*A hook called after exiting Gnus.")
1496
1497 (defvar gnus-save-newsrc-hook nil
1498   "*A hook called before saving any of the newsrc files.")
1499
1500 (defvar gnus-save-quick-newsrc-hook nil
1501   "*A hook called just before saving the quick newsrc file.
1502 Can be used to turn version control on or off.")
1503
1504 (defvar gnus-save-standard-newsrc-hook nil
1505   "*A hook called just before saving the standard newsrc file.
1506 Can be used to turn version control on or off.")
1507
1508 (defvar gnus-summary-update-hook
1509   (list 'gnus-summary-highlight-line)
1510   "*A hook called when a summary line is changed.
1511 The hook will not be called if `gnus-visual' is nil.
1512
1513 The default function `gnus-summary-highlight-line' will
1514 highlight the line according to the `gnus-summary-highlight'
1515 variable.")
1516
1517 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1518   "*A hook called when a group line is changed.
1519 The hook will not be called if `gnus-visual' is nil.
1520
1521 The default function `gnus-group-highlight-line' will
1522 highlight the line according to the `gnus-group-highlight'
1523 variable.")
1524
1525 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1526   "*A hook called when an article is selected for the first time.
1527 The hook is intended to mark an article as read (or unread)
1528 automatically when it is selected.")
1529
1530 (defvar gnus-group-change-level-function nil
1531   "Function run when a group level is changed.
1532 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1533
1534 ;; Remove any hilit infestation.
1535 (add-hook 'gnus-startup-hook
1536           (lambda ()
1537             (remove-hook 'gnus-summary-prepare-hook
1538                          'hilit-rehighlight-buffer-quietly)
1539             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1540             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1541             (remove-hook 'gnus-article-prepare-hook
1542                          'hilit-rehighlight-buffer-quietly)))
1543
1544 \f
1545 ;; Internal variables
1546
1547 (defvar gnus-server-alist nil
1548   "List of available servers.")
1549
1550 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1551
1552 (defvar gnus-goto-missing-group-function nil)
1553
1554 (defvar gnus-override-subscribe-method nil)
1555
1556 (defvar gnus-group-goto-next-group-function nil
1557   "Function to override finding the next group after listing groups.")
1558
1559 (defconst gnus-article-mark-lists
1560   '((marked . tick) (replied . reply)
1561     (expirable . expire) (killed . killed)
1562     (bookmarks . bookmark) (dormant . dormant)
1563     (scored . score) (saved . save)
1564     (cached . cache)
1565     ))
1566
1567 ;; Avoid highlighting in kill files.
1568 (defvar gnus-summary-inhibit-highlight nil)
1569 (defvar gnus-newsgroup-selected-overlay nil)
1570
1571 (defvar gnus-inhibit-hiding nil)
1572 (defvar gnus-group-indentation "")
1573 (defvar gnus-inhibit-limiting nil)
1574 (defvar gnus-created-frames nil)
1575
1576 (defvar gnus-article-mode-map nil)
1577 (defvar gnus-dribble-buffer nil)
1578 (defvar gnus-headers-retrieved-by nil)
1579 (defvar gnus-article-reply nil)
1580 (defvar gnus-override-method nil)
1581 (defvar gnus-article-check-size nil)
1582
1583 (defvar gnus-current-score-file nil)
1584 (defvar gnus-newsgroup-adaptive-score-file nil)
1585 (defvar gnus-scores-exclude-files nil)
1586
1587 (defvar gnus-opened-servers nil)
1588
1589 (defvar gnus-current-move-group nil)
1590
1591 (defvar gnus-newsgroup-dependencies nil)
1592 (defvar gnus-newsgroup-async nil)
1593 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1594
1595 (defvar gnus-newsgroup-adaptive nil)
1596
1597 (defvar gnus-summary-display-table nil)
1598 (defvar gnus-summary-display-article-function nil)
1599
1600 (defvar gnus-summary-highlight-line-function nil
1601   "Function called after highlighting a summary line.")
1602
1603 (defvar gnus-group-line-format-alist
1604   `((?M gnus-tmp-marked-mark ?c)
1605     (?S gnus-tmp-subscribed ?c)
1606     (?L gnus-tmp-level ?d)
1607     (?N (cond ((eq number t) "*" )
1608               ((numberp number) 
1609                (int-to-string
1610                 (+ number
1611                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1612                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1613               (t number)) ?s)
1614     (?R gnus-tmp-number-of-read ?s)
1615     (?t gnus-tmp-number-total ?d)
1616     (?y gnus-tmp-number-of-unread ?s)
1617     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1618     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1619     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1620            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1621     (?g gnus-tmp-group ?s)
1622     (?G gnus-tmp-qualified-group ?s)
1623     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1624     (?D gnus-tmp-newsgroup-description ?s)
1625     (?o gnus-tmp-moderated ?c)
1626     (?O gnus-tmp-moderated-string ?s)
1627     (?p gnus-tmp-process-marked ?c)
1628     (?s gnus-tmp-news-server ?s)
1629     (?n gnus-tmp-news-method ?s)
1630     (?P gnus-group-indentation ?s)
1631     (?l gnus-tmp-grouplens ?s)
1632     (?z gnus-tmp-news-method-string ?s)
1633     (?u gnus-tmp-user-defined ?s)))
1634
1635 (defvar gnus-summary-line-format-alist
1636   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1637     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1638     (?s gnus-tmp-subject-or-nil ?s)
1639     (?n gnus-tmp-name ?s)
1640     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1641         ?s)
1642     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1643             gnus-tmp-from) ?s)
1644     (?F gnus-tmp-from ?s)
1645     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1646     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1647     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1648     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1649     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1650     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1651     (?L gnus-tmp-lines ?d)
1652     (?I gnus-tmp-indentation ?s)
1653     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1654     (?R gnus-tmp-replied ?c)
1655     (?\[ gnus-tmp-opening-bracket ?c)
1656     (?\] gnus-tmp-closing-bracket ?c)
1657     (?\> (make-string gnus-tmp-level ? ) ?s)
1658     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1659     (?i gnus-tmp-score ?d)
1660     (?z gnus-tmp-score-char ?c)
1661     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1662     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1663     (?U gnus-tmp-unread ?c)
1664     (?t (gnus-summary-number-of-articles-in-thread
1665          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1666         ?d)
1667     (?e (gnus-summary-number-of-articles-in-thread
1668          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1669         ?c)
1670     (?u gnus-tmp-user-defined ?s))
1671   "An alist of format specifications that can appear in summary lines,
1672 and what variables they correspond with, along with the type of the
1673 variable (string, integer, character, etc).")
1674
1675 (defvar gnus-summary-dummy-line-format-alist
1676   `((?S gnus-tmp-subject ?s)
1677     (?N gnus-tmp-number ?d)
1678     (?u gnus-tmp-user-defined ?s)))
1679
1680 (defvar gnus-summary-mode-line-format-alist
1681   `((?G gnus-tmp-group-name ?s)
1682     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1683     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1684     (?A gnus-tmp-article-number ?d)
1685     (?Z gnus-tmp-unread-and-unselected ?s)
1686     (?V gnus-version ?s)
1687     (?U gnus-tmp-unread ?d)
1688     (?S gnus-tmp-subject ?s)
1689     (?e gnus-tmp-unselected ?d)
1690     (?u gnus-tmp-user-defined ?s)
1691     (?d (length gnus-newsgroup-dormant) ?d)
1692     (?t (length gnus-newsgroup-marked) ?d)
1693     (?r (length gnus-newsgroup-reads) ?d)
1694     (?E gnus-newsgroup-expunged-tally ?d)
1695     (?s (gnus-current-score-file-nondirectory) ?s)))
1696
1697 (defvar gnus-article-mode-line-format-alist
1698   gnus-summary-mode-line-format-alist)
1699
1700 (defvar gnus-group-mode-line-format-alist
1701   `((?S gnus-tmp-news-server ?s)
1702     (?M gnus-tmp-news-method ?s)
1703     (?u gnus-tmp-user-defined ?s)))
1704
1705 (defvar gnus-have-read-active-file nil)
1706
1707 (defconst gnus-maintainer
1708   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1709   "The mail address of the Gnus maintainers.")
1710
1711 (defconst gnus-version "September Gnus v0.48"
1712   "Version number for this version of Gnus.")
1713
1714 (defvar gnus-info-nodes
1715   '((gnus-group-mode            "(gnus)The Group Buffer")
1716     (gnus-summary-mode          "(gnus)The Summary Buffer")
1717     (gnus-article-mode          "(gnus)The Article Buffer"))
1718   "Assoc list of major modes and related Info nodes.")
1719
1720 (defvar gnus-group-buffer "*Group*")
1721 (defvar gnus-summary-buffer "*Summary*")
1722 (defvar gnus-article-buffer "*Article*")
1723 (defvar gnus-server-buffer "*Server*")
1724
1725 (defvar gnus-work-buffer " *gnus work*")
1726
1727 (defvar gnus-original-article-buffer " *Original Article*")
1728 (defvar gnus-original-article nil)
1729
1730 (defvar gnus-buffer-list nil
1731   "Gnus buffers that should be killed on exit.")
1732
1733 (defvar gnus-slave nil
1734   "Whether this Gnus is a slave or not.")
1735
1736 (defvar gnus-variable-list
1737   '(gnus-newsrc-options gnus-newsrc-options-n
1738     gnus-newsrc-last-checked-date
1739     gnus-newsrc-alist gnus-server-alist
1740     gnus-killed-list gnus-zombie-list
1741     gnus-topic-topology gnus-topic-alist
1742     gnus-format-specs)
1743   "Gnus variables saved in the quick startup file.")
1744
1745 (defvar gnus-newsrc-options nil
1746   "Options line in the .newsrc file.")
1747
1748 (defvar gnus-newsrc-options-n nil
1749   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1750
1751 (defvar gnus-newsrc-last-checked-date nil
1752   "Date Gnus last asked server for new newsgroups.")
1753
1754 (defvar gnus-topic-topology nil
1755   "The complete topic hierarchy.")
1756
1757 (defvar gnus-topic-alist nil
1758   "The complete topic-group alist.")
1759
1760 (defvar gnus-newsrc-alist nil
1761   "Assoc list of read articles.
1762 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1763
1764 (defvar gnus-newsrc-hashtb nil
1765   "Hashtable of gnus-newsrc-alist.")
1766
1767 (defvar gnus-killed-list nil
1768   "List of killed newsgroups.")
1769
1770 (defvar gnus-killed-hashtb nil
1771   "Hash table equivalent of gnus-killed-list.")
1772
1773 (defvar gnus-zombie-list nil
1774   "List of almost dead newsgroups.")
1775
1776 (defvar gnus-description-hashtb nil
1777   "Descriptions of newsgroups.")
1778
1779 (defvar gnus-list-of-killed-groups nil
1780   "List of newsgroups that have recently been killed by the user.")
1781
1782 (defvar gnus-active-hashtb nil
1783   "Hashtable of active articles.")
1784
1785 (defvar gnus-moderated-list nil
1786   "List of moderated newsgroups.")
1787
1788 (defvar gnus-group-marked nil)
1789
1790 (defvar gnus-current-startup-file nil
1791   "Startup file for the current host.")
1792
1793 (defvar gnus-last-search-regexp nil
1794   "Default regexp for article search command.")
1795
1796 (defvar gnus-last-shell-command nil
1797   "Default shell command on article.")
1798
1799 (defvar gnus-current-select-method nil
1800   "The current method for selecting a newsgroup.")
1801
1802 (defvar gnus-group-list-mode nil)
1803
1804 (defvar gnus-article-internal-prepare-hook nil)
1805
1806 (defvar gnus-newsgroup-name nil)
1807 (defvar gnus-newsgroup-begin nil)
1808 (defvar gnus-newsgroup-end nil)
1809 (defvar gnus-newsgroup-last-rmail nil)
1810 (defvar gnus-newsgroup-last-mail nil)
1811 (defvar gnus-newsgroup-last-folder nil)
1812 (defvar gnus-newsgroup-last-file nil)
1813 (defvar gnus-newsgroup-auto-expire nil)
1814 (defvar gnus-newsgroup-active nil)
1815
1816 (defvar gnus-newsgroup-data nil)
1817 (defvar gnus-newsgroup-data-reverse nil)
1818 (defvar gnus-newsgroup-limit nil)
1819 (defvar gnus-newsgroup-limits nil)
1820
1821 (defvar gnus-newsgroup-unreads nil
1822   "List of unread articles in the current newsgroup.")
1823
1824 (defvar gnus-newsgroup-unselected nil
1825   "List of unselected unread articles in the current newsgroup.")
1826
1827 (defvar gnus-newsgroup-reads nil
1828   "Alist of read articles and article marks in the current newsgroup.")
1829
1830 (defvar gnus-newsgroup-expunged-tally nil)
1831
1832 (defvar gnus-newsgroup-marked nil
1833   "List of ticked articles in the current newsgroup (a subset of unread art).")
1834
1835 (defvar gnus-newsgroup-killed nil
1836   "List of ranges of articles that have been through the scoring process.")
1837
1838 (defvar gnus-newsgroup-cached nil
1839   "List of articles that come from the article cache.")
1840
1841 (defvar gnus-newsgroup-saved nil
1842   "List of articles that have been saved.")
1843
1844 (defvar gnus-newsgroup-kill-headers nil)
1845
1846 (defvar gnus-newsgroup-replied nil
1847   "List of articles that have been replied to in the current newsgroup.")
1848
1849 (defvar gnus-newsgroup-expirable nil
1850   "List of articles in the current newsgroup that can be expired.")
1851
1852 (defvar gnus-newsgroup-processable nil
1853   "List of articles in the current newsgroup that can be processed.")
1854
1855 (defvar gnus-newsgroup-bookmarks nil
1856   "List of articles in the current newsgroup that have bookmarks.")
1857
1858 (defvar gnus-newsgroup-dormant nil
1859   "List of dormant articles in the current newsgroup.")
1860
1861 (defvar gnus-newsgroup-scored nil
1862   "List of scored articles in the current newsgroup.")
1863
1864 (defvar gnus-newsgroup-headers nil
1865   "List of article headers in the current newsgroup.")
1866
1867 (defvar gnus-newsgroup-threads nil)
1868
1869 (defvar gnus-newsgroup-prepared nil
1870   "Whether the current group has been prepared properly.")
1871
1872 (defvar gnus-newsgroup-ancient nil
1873   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1874
1875 (defvar gnus-newsgroup-sparse nil)
1876
1877 (defvar gnus-current-article nil)
1878 (defvar gnus-article-current nil)
1879 (defvar gnus-current-headers nil)
1880 (defvar gnus-have-all-headers nil)
1881 (defvar gnus-last-article nil)
1882 (defvar gnus-newsgroup-history nil)
1883 (defvar gnus-current-kill-article nil)
1884
1885 ;; Save window configuration.
1886 (defvar gnus-prev-winconf nil)
1887
1888 (defvar gnus-summary-mark-positions nil)
1889 (defvar gnus-group-mark-positions nil)
1890
1891 (defvar gnus-reffed-article-number nil)
1892
1893 ;;; Let the byte-compiler know that we know about this variable.
1894 (defvar rmail-default-rmail-file)
1895
1896 (defvar gnus-cache-removable-articles nil)
1897
1898 (defvar gnus-dead-summary nil)
1899
1900 (defconst gnus-summary-local-variables
1901   '(gnus-newsgroup-name
1902     gnus-newsgroup-begin gnus-newsgroup-end
1903     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1904     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1905     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1906     gnus-newsgroup-unselected gnus-newsgroup-marked
1907     gnus-newsgroup-reads gnus-newsgroup-saved
1908     gnus-newsgroup-replied gnus-newsgroup-expirable
1909     gnus-newsgroup-processable gnus-newsgroup-killed
1910     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1911     gnus-newsgroup-headers gnus-newsgroup-threads
1912     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1913     gnus-current-article gnus-current-headers gnus-have-all-headers
1914     gnus-last-article gnus-article-internal-prepare-hook
1915     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1916     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1917     gnus-newsgroup-async 
1918     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1919     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1920     gnus-newsgroup-history gnus-newsgroup-ancient
1921     gnus-newsgroup-sparse
1922     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1923     gnus-newsgroup-adaptive-score-file
1924     (gnus-newsgroup-expunged-tally . 0)
1925     gnus-cache-removable-articles gnus-newsgroup-cached
1926     gnus-newsgroup-data gnus-newsgroup-data-reverse
1927     gnus-newsgroup-limit gnus-newsgroup-limits)
1928   "Variables that are buffer-local to the summary buffers.")
1929
1930 (defconst gnus-bug-message
1931   "Sending a bug report to the Gnus Towers.
1932 ========================================
1933
1934 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1935 be sent to the Gnus Bug Exterminators.
1936
1937 At the bottom of the buffer you'll see lots of variable settings.
1938 Please do not delete those.  They will tell the Bug People what your
1939 environment is, so that it will be easier to locate the bugs.
1940
1941 If you have found a bug that makes Emacs go \"beep\", set
1942 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1943 and include the backtrace in your bug report.
1944
1945 Please describe the bug in annoying, painstaking detail.
1946
1947 Thank you for your help in stamping out bugs.
1948 ")
1949
1950 ;;; End of variables.
1951
1952 ;; Define some autoload functions Gnus might use.
1953 (eval-and-compile
1954
1955   ;; This little mapcar goes through the list below and marks the
1956   ;; symbols in question as autoloaded functions.
1957   (mapcar
1958    (lambda (package)
1959      (let ((interactive (nth 1 (memq ':interactive package))))
1960        (mapcar
1961         (lambda (function)
1962           (let (keymap)
1963             (when (consp function)
1964               (setq keymap (car (memq 'keymap function)))
1965               (setq function (car function)))
1966             (autoload function (car package) nil interactive keymap)))
1967         (if (eq (nth 1 package) ':interactive)
1968             (cdddr package)
1969           (cdr package)))))
1970    '(("metamail" metamail-buffer)
1971      ("info" Info-goto-node)
1972      ("hexl" hexl-hex-string-to-integer)
1973      ("pp" pp pp-to-string pp-eval-expression)
1974      ("mail-extr" mail-extract-address-components)
1975      ("nnmail" nnmail-split-fancy nnmail-article-group)
1976      ("nnvirtual" nnvirtual-catchup-group)
1977      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1978       timezone-make-sortable-date timezone-make-time-string)
1979      ("sendmail" mail-position-on-field mail-setup)
1980      ("rmailout" rmail-output)
1981      ("rnewspost" news-mail-other-window news-reply-yank-original
1982       news-caesar-buffer-body)
1983      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1984       rmail-show-message)
1985      ("gnus-soup" :interactive t
1986       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1987       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1988      ("nnsoup" nnsoup-pack-replies)
1989      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1990       gnus-Folder-save-name gnus-folder-save-name)
1991      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1992      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1993       gnus-server-make-menu-bar gnus-article-make-menu-bar
1994       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1995       gnus-summary-highlight-line gnus-carpal-setup-buffer
1996       gnus-group-highlight-line
1997       gnus-article-add-button gnus-insert-next-page-button
1998       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
1999      ("gnus-vis" :interactive t
2000       gnus-article-push-button gnus-article-press-button
2001       gnus-article-highlight gnus-article-highlight-some
2002       gnus-article-highlight-headers gnus-article-highlight-signature
2003       gnus-article-add-buttons gnus-article-add-buttons-to-head
2004       gnus-article-next-button gnus-article-prev-button)
2005      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2006       gnus-demon-add-disconnection gnus-demon-add-handler
2007       gnus-demon-remove-handler)
2008      ("gnus-demon" :interactive t
2009       gnus-demon-init gnus-demon-cancel)
2010      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2011       gnus-tree-open gnus-tree-close)
2012      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2013       gnus-nocem-unwanted-article-p)
2014      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2015      ("gnus-srvr" gnus-browse-foreign-server)
2016      ("gnus-cite" :interactive t
2017       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2018       gnus-article-hide-citation gnus-article-fill-cited-article)
2019      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2020       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2021       gnus-execute gnus-expunge)
2022      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2023       gnus-cache-possibly-remove-articles gnus-cache-request-article
2024       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2025       gnus-cache-enter-remove-article gnus-cached-article-p
2026       gnus-cache-open gnus-cache-close)
2027      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2028       gnus-cache-remove-article)
2029      ("gnus-score" :interactive t
2030       gnus-summary-increase-score gnus-summary-lower-score
2031       gnus-score-flush-cache gnus-score-close
2032       gnus-score-raise-same-subject-and-select
2033       gnus-score-raise-same-subject gnus-score-default
2034       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2035       gnus-score-lower-same-subject gnus-score-lower-thread
2036       gnus-possibly-score-headers)
2037      ("gnus-score"
2038       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2039       gnus-current-score-file-nondirectory gnus-score-adaptive
2040       gnus-score-find-trace gnus-score-file-name)
2041      ("gnus-edit" :interactive t gnus-score-customize)
2042      ("gnus-topic" :interactive t gnus-topic-mode)
2043      ("gnus-topic" gnus-topic-remove-group)
2044      ("gnus-salt" :interactive t gnus-pick-mode)
2045      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2046      ("gnus-uu" :interactive t
2047       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2048       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2049       gnus-uu-mark-by-regexp gnus-uu-mark-all
2050       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2051       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2052       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2053       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2054       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2055       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2056       gnus-uu-decode-binhex-view)
2057      ("gnus-msg" (gnus-summary-send-map keymap)
2058       gnus-mail-yank-original gnus-mail-send-and-exit
2059       gnus-sendmail-setup-mail gnus-article-mail
2060       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2061      ("gnus-msg" :interactive t
2062       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2063       gnus-summary-followup gnus-summary-followup-with-original
2064       gnus-summary-followup-and-reply
2065       gnus-summary-followup-and-reply-with-original
2066       gnus-summary-cancel-article gnus-summary-supersede-article
2067       gnus-post-news gnus-inews-news gnus-cancel-news
2068       gnus-summary-reply gnus-summary-reply-with-original
2069       gnus-summary-mail-forward gnus-summary-mail-other-window
2070       gnus-bug)
2071      ("gnus-picon" :interactive t gnus-article-display-picons
2072       gnus-group-display-picons gnus-picons-article-display-x-face)
2073      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2074       gnus-grouplens-mode)
2075      ("gnus-vm" gnus-vm-mail-setup)
2076      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2077       gnus-summary-save-article-vm gnus-yank-article))))
2078
2079 \f
2080
2081 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2082 ;; If you want the cursor to go somewhere else, set these two
2083 ;; functions in some startup hook to whatever you want.
2084 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2085 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2086
2087 ;;; Various macros and substs.
2088
2089 (defun gnus-header-from (header)
2090   (mail-header-from header))
2091
2092 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2093   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2094   `(let ((GnusStartBufferWindow (selected-window)))
2095      (unwind-protect
2096          (progn
2097            (pop-to-buffer ,buffer)
2098            ,@forms)
2099        (select-window GnusStartBufferWindow))))
2100
2101 (defmacro gnus-gethash (string hashtable)
2102   "Get hash value of STRING in HASHTABLE."
2103   `(symbol-value (intern-soft ,string ,hashtable)))
2104
2105 (defmacro gnus-sethash (string value hashtable)
2106   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2107   `(set (intern ,string ,hashtable) ,value))
2108
2109 (defmacro gnus-intern-safe (string hashtable)
2110   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2111   `(let ((symbol (intern ,string ,hashtable)))
2112      (or (boundp symbol)
2113          (set symbol nil))
2114      symbol))
2115
2116 (defmacro gnus-group-unread (group)
2117   "Get the currently computed number of unread articles in GROUP."
2118   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2119
2120 (defmacro gnus-group-entry (group)
2121   "Get the newsrc entry for GROUP."
2122   `(gnus-gethash ,group gnus-newsrc-hashtb))
2123
2124 (defmacro gnus-active (group)
2125   "Get active info on GROUP."
2126   `(gnus-gethash ,group gnus-active-hashtb))
2127
2128 (defmacro gnus-set-active (group active)
2129   "Set GROUP's active info."
2130   `(gnus-sethash ,group ,active gnus-active-hashtb))
2131
2132 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2133 ;;   function `substring' might cut on a middle of multi-octet
2134 ;;   character.
2135 (defun gnus-truncate-string (str width)
2136   (substring str 0 width))
2137
2138 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2139 ;; to limit the length of a string.  This function is necessary since
2140 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2141 (defsubst gnus-limit-string (str width)
2142   (if (> (length str) width)
2143       (substring str 0 width)
2144     str))
2145
2146 (defsubst gnus-simplify-subject-re (subject)
2147   "Remove \"Re:\" from subject lines."
2148   (if (string-match "^[Rr][Ee]: *" subject)
2149       (substring subject (match-end 0))
2150     subject))
2151
2152 (defsubst gnus-goto-char (point)
2153   (and point (goto-char point)))
2154
2155 (defmacro gnus-buffer-exists-p (buffer)
2156   `(and ,buffer
2157         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2158                  ,buffer)))
2159
2160 (defmacro gnus-kill-buffer (buffer)
2161   `(let ((buf ,buffer))
2162      (if (gnus-buffer-exists-p buf)
2163          (kill-buffer buf))))
2164
2165 (defsubst gnus-point-at-bol ()
2166   "Return point at the beginning of the line."
2167   (let ((p (point)))
2168     (beginning-of-line)
2169     (prog1
2170         (point)
2171       (goto-char p))))
2172
2173 (defsubst gnus-point-at-eol ()
2174   "Return point at the end of the line."
2175   (let ((p (point)))
2176     (end-of-line)
2177     (prog1
2178         (point)
2179       (goto-char p))))
2180
2181 (defun gnus-alive-p ()
2182   "Say whether Gnus is running or not."
2183   (and gnus-group-buffer
2184        (get-buffer gnus-group-buffer)))
2185
2186 ;; Delete the current line (and the next N lines.);
2187 (defmacro gnus-delete-line (&optional n)
2188   `(delete-region (progn (beginning-of-line) (point))
2189                   (progn (forward-line ,(or n 1)) (point))))
2190
2191 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2192 (defvar gnus-init-inhibit nil)
2193 (defun gnus-read-init-file (&optional inhibit-next)
2194   (if gnus-init-inhibit
2195       (setq gnus-init-inhibit nil)
2196     (setq gnus-init-inhibit inhibit-next)
2197     (and gnus-init-file
2198          (or (and (file-exists-p gnus-init-file)
2199                   ;; Don't try to load a directory.
2200                   (not (file-directory-p gnus-init-file)))
2201              (file-exists-p (concat gnus-init-file ".el"))
2202              (file-exists-p (concat gnus-init-file ".elc")))
2203          (condition-case var
2204              (load gnus-init-file nil t)
2205            (error
2206             (error "Error in %s: %s" gnus-init-file var))))))
2207
2208 ;; Info access macros.
2209
2210 (defmacro gnus-info-group (info)
2211   `(nth 0 ,info))
2212 (defmacro gnus-info-rank (info)
2213   `(nth 1 ,info))
2214 (defmacro gnus-info-read (info)
2215   `(nth 2 ,info))
2216 (defmacro gnus-info-marks (info)
2217   `(nth 3 ,info))
2218 (defmacro gnus-info-method (info)
2219   `(nth 4 ,info))
2220 (defmacro gnus-info-params (info)
2221   `(nth 5 ,info))
2222
2223 (defmacro gnus-info-level (info)
2224   `(let ((rank (gnus-info-rank ,info)))
2225      (if (consp rank)
2226          (car rank)
2227        rank)))
2228 (defmacro gnus-info-score (info)
2229   `(let ((rank (gnus-info-rank ,info)))
2230      (or (and (consp rank) (cdr rank)) 0)))
2231
2232 (defmacro gnus-info-set-group (info group)
2233   `(setcar ,info ,group))
2234 (defmacro gnus-info-set-rank (info rank)
2235   `(setcar (nthcdr 1 ,info) ,rank))
2236 (defmacro gnus-info-set-read (info read)
2237   `(setcar (nthcdr 2 ,info) ,read))
2238 (defmacro gnus-info-set-marks (info marks)
2239   `(setcar (nthcdr 3 ,info) ,marks))
2240 (defmacro gnus-info-set-method (info method)
2241   `(setcar (nthcdr 4 ,info) ,method))
2242 (defmacro gnus-info-set-params (info params)
2243   `(setcar (nthcdr 5 ,info) ,params))
2244
2245 (defmacro gnus-info-set-level (info level)
2246   `(let ((rank (cdr ,info)))
2247      (if (consp (car rank))
2248          (setcar (car rank) ,level)
2249        (setcar rank ,level))))
2250 (defmacro gnus-info-set-score (info score)
2251   `(let ((rank (cdr ,info)))
2252      (if (consp (car rank))
2253          (setcdr (car rank) ,score)
2254        (setcar rank (cons (car rank) ,score)))))
2255
2256 (defmacro gnus-get-info (group)
2257   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2258
2259 (defun gnus-byte-code (func)
2260   "Return a form that can be `eval'ed based on FUNC."
2261   (let ((fval (symbol-function func)))
2262     (if (byte-code-function-p fval)
2263         (let ((flist (append fval nil)))
2264           (setcar flist 'byte-code)
2265           flist)
2266       (cons 'progn (cddr fval)))))
2267
2268 ;;; Load the compatability functions.
2269
2270 (require 'gnus-cus)
2271 (require 'gnus-ems)
2272
2273 \f
2274
2275 ;; Format specs.  The chunks below are the machine-generated forms
2276 ;; that are to be evaled as the result of the default format strings.
2277 ;; We write them in here to get them byte-compiled.  That way the
2278 ;; default actions will be quite fast, while still retaining the full
2279 ;; flexibility of the user-defined format specs.
2280
2281 ;; First we have lots of dummy defvars to let the compiler know these
2282 ;; are really dynamic variables.
2283
2284 (defvar gnus-tmp-unread)
2285 (defvar gnus-tmp-replied)
2286 (defvar gnus-tmp-score-char)
2287 (defvar gnus-tmp-indentation)
2288 (defvar gnus-tmp-opening-bracket)
2289 (defvar gnus-tmp-lines)
2290 (defvar gnus-tmp-name)
2291 (defvar gnus-tmp-closing-bracket)
2292 (defvar gnus-tmp-subject-or-nil)
2293 (defvar gnus-tmp-subject)
2294 (defvar gnus-tmp-marked)
2295 (defvar gnus-tmp-marked-mark)
2296 (defvar gnus-tmp-subscribed)
2297 (defvar gnus-tmp-process-marked)
2298 (defvar gnus-tmp-number-of-unread)
2299 (defvar gnus-tmp-group-name)
2300 (defvar gnus-tmp-group)
2301 (defvar gnus-tmp-article-number)
2302 (defvar gnus-tmp-unread-and-unselected)
2303 (defvar gnus-tmp-news-method)
2304 (defvar gnus-tmp-news-server)
2305 (defvar gnus-tmp-article-number)
2306 (defvar gnus-mouse-face)
2307 (defvar gnus-mouse-face-prop)
2308
2309 (defun gnus-summary-line-format-spec ()
2310   (insert gnus-tmp-unread gnus-tmp-replied
2311           gnus-tmp-score-char gnus-tmp-indentation)
2312   (put-text-property
2313    (point)
2314    (progn
2315      (insert
2316       gnus-tmp-opening-bracket
2317       (format "%4d: %-20s"
2318               gnus-tmp-lines
2319               (if (> (length gnus-tmp-name) 20)
2320                   (substring gnus-tmp-name 0 20)
2321                 gnus-tmp-name))
2322       gnus-tmp-closing-bracket)
2323      (point))
2324    gnus-mouse-face-prop gnus-mouse-face)
2325   (insert " " gnus-tmp-subject-or-nil "\n"))
2326
2327 (defvar gnus-summary-line-format-spec
2328   (gnus-byte-code 'gnus-summary-line-format-spec))
2329
2330 (defun gnus-summary-dummy-line-format-spec ()
2331   (insert "*  ")
2332   (put-text-property
2333    (point)
2334    (progn
2335      (insert ":                          :")
2336      (point))
2337    gnus-mouse-face-prop gnus-mouse-face)
2338   (insert " " gnus-tmp-subject "\n"))
2339
2340 (defvar gnus-summary-dummy-line-format-spec
2341   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2342
2343 (defun gnus-group-line-format-spec ()
2344   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2345           gnus-tmp-process-marked
2346           gnus-group-indentation
2347           (format "%5s: " gnus-tmp-number-of-unread))
2348   (put-text-property
2349    (point)
2350    (progn
2351      (insert gnus-tmp-group "\n")
2352      (1- (point)))
2353    gnus-mouse-face-prop gnus-mouse-face))
2354 (defvar gnus-group-line-format-spec
2355   (gnus-byte-code 'gnus-group-line-format-spec))
2356
2357 (defvar gnus-format-specs
2358   `((version . ,emacs-version)
2359     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2360     (summary-dummy ,gnus-summary-dummy-line-format
2361                    ,gnus-summary-dummy-line-format-spec)
2362     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2363
2364 (defvar gnus-article-mode-line-format-spec nil)
2365 (defvar gnus-summary-mode-line-format-spec nil)
2366 (defvar gnus-group-mode-line-format-spec nil)
2367
2368 ;;; Phew.  All that gruft is over, fortunately.
2369
2370 \f
2371 ;;;
2372 ;;; Gnus Utility Functions
2373 ;;;
2374
2375 (defun gnus-extract-address-components (from)
2376   (let (name address)
2377     ;; First find the address - the thing with the @ in it.  This may
2378     ;; not be accurate in mail addresses, but does the trick most of
2379     ;; the time in news messages.
2380     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2381         (setq address (substring from (match-beginning 0) (match-end 0))))
2382     ;; Then we check whether the "name <address>" format is used.
2383     (and address
2384          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2385          ;; Linear white space is not required.
2386          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2387          (and (setq name (substring from 0 (match-beginning 0)))
2388               ;; Strip any quotes from the name.
2389               (string-match "\".*\"" name)
2390               (setq name (substring name 1 (1- (match-end 0))))))
2391     ;; If not, then "address (name)" is used.
2392     (or name
2393         (and (string-match "(.+)" from)
2394              (setq name (substring from (1+ (match-beginning 0))
2395                                    (1- (match-end 0)))))
2396         (and (string-match "()" from)
2397              (setq name address))
2398         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2399         ;; XOVER might not support folded From headers.
2400         (and (string-match "(.*" from)
2401              (setq name (substring from (1+ (match-beginning 0))
2402                                    (match-end 0)))))
2403     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2404     (list (or name from) (or address from))))
2405
2406 (defun gnus-fetch-field (field)
2407   "Return the value of the header FIELD of current article."
2408   (save-excursion
2409     (save-restriction
2410       (let ((case-fold-search t))
2411         (nnheader-narrow-to-headers)
2412         (mail-fetch-field field)))))
2413
2414 (defun gnus-goto-colon ()
2415   (beginning-of-line)
2416   (search-forward ":" (gnus-point-at-eol) t))
2417
2418 ;;;###autoload
2419 (defun gnus-update-format (var)
2420   "Update the format specification near point."
2421   (interactive
2422    (list
2423     (save-excursion
2424       (eval-defun nil)
2425       ;; Find the end of the current word.
2426       (re-search-forward "[ \t\n]" nil t)
2427       ;; Search backward.
2428       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2429         (match-string 1)))))
2430   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2431                               (match-string 1 var))))
2432          (entry (assq type gnus-format-specs))
2433          value spec)
2434     (when entry
2435       (setq gnus-format-specs (delq entry gnus-format-specs)))
2436     (set
2437      (intern (format "%s-spec" var))
2438      (gnus-parse-format (setq value (symbol-value (intern var)))
2439                         (symbol-value (intern (format "%s-alist" var)))
2440                         (not (string-match "mode" var))))
2441     (setq spec (symbol-value (intern (format "%s-spec" var))))
2442     (push (list type value spec) gnus-format-specs)
2443
2444     (pop-to-buffer "*Gnus Format*")
2445     (erase-buffer)
2446     (lisp-interaction-mode)
2447     (insert (pp-to-string spec))))
2448
2449 (defun gnus-update-format-specifications (&optional force)
2450   "Update all (necessary) format specifications."
2451   ;; Make the indentation array.
2452   (gnus-make-thread-indent-array)
2453
2454   ;; See whether all the stored info needs to be flushed.
2455   (when (or force
2456             (not (equal emacs-version
2457                         (cdr (assq 'version gnus-format-specs)))))
2458     (setq gnus-format-specs nil))
2459
2460   ;; Go through all the formats and see whether they need updating.
2461   (let ((types '(summary summary-dummy group
2462                          summary-mode group-mode article-mode))
2463         new-format entry type val)
2464     (while (setq type (pop types))
2465       ;; Jump to the proper buffer to find out the value of
2466       ;; the variable, if possible.  (It may be buffer-local.)
2467       (save-excursion
2468         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2469               val)
2470           (when (and (boundp buffer)
2471                      (setq val (symbol-value buffer))
2472                      (get-buffer val)
2473                      (buffer-name (get-buffer val)))
2474             (set-buffer (get-buffer val)))
2475           (setq new-format (symbol-value
2476                             (intern (format "gnus-%s-line-format" type))))))
2477       (setq entry (cdr (assq type gnus-format-specs)))
2478       (if (and entry
2479                (equal (car entry) new-format))
2480           ;; Use the old format.
2481           (set (intern (format "gnus-%s-line-format-spec" type))
2482                (cadr entry))
2483         ;; This is a new format.
2484         (setq val
2485               (if (not (stringp new-format))
2486                   ;; This is a function call or something.
2487                   new-format
2488                 ;; This is a "real" format.
2489                 (gnus-parse-format
2490                  new-format
2491                  (symbol-value
2492                   (intern (format "gnus-%s-line-format-alist"
2493                                   (if (eq type 'article-mode)
2494                                       'summary-mode type))))
2495                  (not (string-match "mode$" (symbol-name type))))))
2496         ;; Enter the new format spec into the list.
2497         (if entry
2498             (progn
2499               (setcar (cdr entry) val)
2500               (setcar entry new-format))
2501           (push (list type new-format val) gnus-format-specs))
2502         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2503
2504   (gnus-update-group-mark-positions)
2505   (gnus-update-summary-mark-positions)
2506
2507   ;; See whether we need to read the description file.
2508   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2509            (not gnus-description-hashtb)
2510            gnus-read-active-file)
2511       (gnus-read-all-descriptions-files)))
2512
2513 (defun gnus-update-summary-mark-positions ()
2514   "Compute where the summary marks are to go."
2515   (save-excursion
2516     (let ((gnus-replied-mark 129)
2517           (gnus-score-below-mark 130)
2518           (gnus-score-over-mark 130)
2519           (thread nil)
2520           (gnus-visual nil)
2521           pos)
2522       (gnus-set-work-buffer)
2523       (gnus-summary-insert-line
2524        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2525       (goto-char (point-min))
2526       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2527                                          (- (point) 2)))))
2528       (goto-char (point-min))
2529       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2530             pos)
2531       (goto-char (point-min))
2532       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2533             pos)
2534       (setq gnus-summary-mark-positions pos))))
2535
2536 (defun gnus-update-group-mark-positions ()
2537   (save-excursion
2538     (let ((gnus-process-mark 128)
2539           (gnus-group-marked '("dummy.group")))
2540       (gnus-set-active "dummy.group" '(0 . 0))
2541       (gnus-set-work-buffer)
2542       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2543       (goto-char (point-min))
2544       (setq gnus-group-mark-positions
2545             (list (cons 'process (and (search-forward "\200" nil t)
2546                                       (- (point) 2))))))))
2547
2548 (defvar gnus-mouse-face-0 'highlight)
2549 (defvar gnus-mouse-face-1 'highlight)
2550 (defvar gnus-mouse-face-2 'highlight)
2551 (defvar gnus-mouse-face-3 'highlight)
2552 (defvar gnus-mouse-face-4 'highlight)
2553
2554 (defun gnus-mouse-face-function (form type)
2555   `(put-text-property
2556     (point) (progn ,@form (point))
2557     gnus-mouse-face-prop
2558     ,(if (equal type 0)
2559          'gnus-mouse-face
2560        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2561
2562 (defvar gnus-face-0 'bold)
2563 (defvar gnus-face-1 'italic)
2564 (defvar gnus-face-2 'bold-italic)
2565 (defvar gnus-face-3 'bold)
2566 (defvar gnus-face-4 'bold)
2567
2568 (defun gnus-face-face-function (form type)
2569   `(put-text-property
2570     (point) (progn ,@form (point))
2571     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2572
2573 (defun gnus-max-width-function (el max-width)
2574   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2575   (if (symbolp el)
2576       `(if (> (length ,el) ,max-width)
2577            (substring ,el 0 ,max-width)
2578          ,el)
2579     `(let ((val (eval ,el)))
2580        (if (numberp val)
2581            (setq val (int-to-string val)))
2582        (if (> (length val) ,max-width)
2583            (substring val 0 ,max-width)
2584          val))))
2585
2586 (defun gnus-parse-format (format spec-alist &optional insert)
2587   ;; This function parses the FORMAT string with the help of the
2588   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2589   ;; string.  If the FORMAT string contains the specifiers %( and %)
2590   ;; the text between them will have the mouse-face text property.
2591   (if (string-match
2592        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2593        format)
2594       (gnus-parse-complex-format format spec-alist)
2595     ;; This is a simple format.
2596     (gnus-parse-simple-format format spec-alist insert)))
2597
2598 (defun gnus-parse-complex-format (format spec-alist)
2599   (save-excursion
2600     (gnus-set-work-buffer)
2601     (insert format)
2602     (goto-char (point-min))
2603     (while (re-search-forward "\"" nil t)
2604       (replace-match "\\\"" nil t))
2605     (goto-char (point-min))
2606     (insert "(\"")
2607     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2608       (let ((number (if (match-beginning 1)
2609                         (match-string 1) "0"))
2610             (delim (aref (match-string 2) 0)))
2611         (if (or (= delim ?\() (= delim ?\{))
2612             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2613                                    " " number " \""))
2614           (replace-match "\")\""))))
2615     (goto-char (point-max))
2616     (insert "\")")
2617     (goto-char (point-min))
2618     (let ((form (read (current-buffer))))
2619       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2620
2621 (defun gnus-complex-form-to-spec (form spec-alist)
2622   (delq nil
2623         (mapcar
2624          (lambda (sform)
2625            (if (stringp sform)
2626                (gnus-parse-simple-format sform spec-alist t)
2627              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2628                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2629                       (nth 1 sform))))
2630          form)))
2631
2632 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2633   ;; This function parses the FORMAT string with the help of the
2634   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2635   ;; string.
2636   (let ((max-width 0)
2637         spec flist fstring newspec elem beg result dontinsert)
2638     (save-excursion
2639       (gnus-set-work-buffer)
2640       (insert format)
2641       (goto-char (point-min))
2642       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2643                                 nil t)
2644         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2645               (setq newspec "%"
2646                     beg (1+ (match-beginning 0)))
2647           ;; First check if there are any specs that look anything like
2648           ;; "%12,12A", ie. with a "max width specification".  These have
2649           ;; to be treated specially.
2650           (if (setq beg (match-beginning 1))
2651               (setq max-width
2652                     (string-to-int
2653                      (buffer-substring
2654                       (1+ (match-beginning 1)) (match-end 1))))
2655             (setq max-width 0)
2656             (setq beg (match-beginning 2)))
2657           ;; Find the specification from `spec-alist'.
2658           (unless (setq elem (cdr (assq spec spec-alist)))
2659             (setq elem '("*" ?s)))
2660           ;; Treat user defined format specifiers specially.
2661           (when (eq (car elem) 'gnus-tmp-user-defined)
2662             (setq elem
2663                   (list
2664                    (list (intern (concat "gnus-user-format-function-"
2665                                          (match-string 3)))
2666                          'gnus-tmp-header) ?s))
2667             (delete-region (match-beginning 3) (match-end 3)))
2668           (if (not (zerop max-width))
2669               (let ((el (car elem)))
2670                 (cond ((= (cadr elem) ?c)
2671                        (setq el (list 'char-to-string el)))
2672                       ((= (cadr elem) ?d)
2673                        (setq el (list 'int-to-string el))))
2674                 (setq flist (cons (gnus-max-width-function el max-width)
2675                                   flist))
2676                 (setq newspec ?s))
2677             (progn
2678               (setq flist (cons (car elem) flist))
2679               (setq newspec (cadr elem)))))
2680         ;; Remove the old specification (and possibly a ",12" string).
2681         (delete-region beg (match-end 2))
2682         ;; Insert the new specification.
2683         (goto-char beg)
2684         (insert newspec))
2685       (setq fstring (buffer-substring 1 (point-max))))
2686     ;; Do some postprocessing to increase efficiency.
2687     (setq
2688      result
2689      (cond
2690       ;; Emptyness.
2691       ((string= fstring "")
2692        nil)
2693       ;; Not a format string.
2694       ((not (string-match "%" fstring))
2695        (list fstring))
2696       ;; A format string with just a single string spec.
2697       ((string= fstring "%s")
2698        (list (car flist)))
2699       ;; A single character.
2700       ((string= fstring "%c")
2701        (list (car flist)))
2702       ;; A single number.
2703       ((string= fstring "%d")
2704        (setq dontinsert)
2705        (if insert
2706            (list `(princ ,(car flist)))
2707          (list `(int-to-string ,(car flist)))))
2708       ;; Just lots of chars and strings.
2709       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2710        (nreverse flist))
2711       ;; A single string spec at the beginning of the spec.
2712       ((string-match "\\`%[sc][^%]+\\'" fstring)
2713        (list (car flist) (substring fstring 2)))
2714       ;; A single string spec in the middle of the spec.
2715       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2716        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2717       ;; A single string spec in the end of the spec.
2718       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2719        (list (match-string 1 fstring) (car flist)))
2720       ;; A more complex spec.
2721       (t
2722        (list (cons 'format (cons fstring (nreverse flist)))))))
2723
2724     (if insert
2725         (when result
2726           (if dontinsert
2727               result
2728             (cons 'insert result)))
2729       (cond ((stringp result)
2730              result)
2731             ((consp result)
2732              (cons 'concat result))
2733             (t "")))))
2734
2735 (defun gnus-eval-format (format &optional alist props)
2736   "Eval the format variable FORMAT, using ALIST.
2737 If PROPS, insert the result."
2738   (let ((form (gnus-parse-format format alist props)))
2739     (if props
2740         (add-text-properties (point) (progn (eval form) (point)) props)
2741       (eval form))))
2742
2743 (defun gnus-remove-text-with-property (prop)
2744   "Delete all text in the current buffer with text property PROP."
2745   (save-excursion
2746     (goto-char (point-min))
2747     (while (not (eobp))
2748       (while (get-text-property (point) prop)
2749         (delete-char 1))
2750       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2751
2752 (defun gnus-set-work-buffer ()
2753   (if (get-buffer gnus-work-buffer)
2754       (progn
2755         (set-buffer gnus-work-buffer)
2756         (erase-buffer))
2757     (set-buffer (get-buffer-create gnus-work-buffer))
2758     (kill-all-local-variables)
2759     (buffer-disable-undo (current-buffer))
2760     (gnus-add-current-to-buffer-list)))
2761
2762 ;; Article file names when saving.
2763
2764 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2765   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2766 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2767 Otherwise, it is like ~/News/news/group/num."
2768   (let ((default
2769           (expand-file-name
2770            (concat (if (gnus-use-long-file-name 'not-save)
2771                        (gnus-capitalize-newsgroup newsgroup)
2772                      (gnus-newsgroup-directory-form newsgroup))
2773                    "/" (int-to-string (mail-header-number headers)))
2774            (or gnus-article-save-directory "~/News"))))
2775     (if (and last-file
2776              (string-equal (file-name-directory default)
2777                            (file-name-directory last-file))
2778              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2779         default
2780       (or last-file default))))
2781
2782 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2783   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2784 If variable `gnus-use-long-file-name' is non-nil, it is
2785 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2786   (let ((default
2787           (expand-file-name
2788            (concat (if (gnus-use-long-file-name 'not-save)
2789                        newsgroup
2790                      (gnus-newsgroup-directory-form newsgroup))
2791                    "/" (int-to-string (mail-header-number headers)))
2792            (or gnus-article-save-directory "~/News"))))
2793     (if (and last-file
2794              (string-equal (file-name-directory default)
2795                            (file-name-directory last-file))
2796              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2797         default
2798       (or last-file default))))
2799
2800 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2801   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2802 If variable `gnus-use-long-file-name' is non-nil, it is
2803 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2804   (or last-file
2805       (expand-file-name
2806        (if (gnus-use-long-file-name 'not-save)
2807            (gnus-capitalize-newsgroup newsgroup)
2808          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2809        (or gnus-article-save-directory "~/News"))))
2810
2811 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2812   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2813 If variable `gnus-use-long-file-name' is non-nil, it is
2814 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2815   (or last-file
2816       (expand-file-name
2817        (if (gnus-use-long-file-name 'not-save)
2818            newsgroup
2819          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2820        (or gnus-article-save-directory "~/News"))))
2821
2822 ;; For subscribing new newsgroup
2823
2824 (defun gnus-subscribe-hierarchical-interactive (groups)
2825   (let ((groups (sort groups 'string<))
2826         prefixes prefix start ans group starts)
2827     (while groups
2828       (setq prefixes (list "^"))
2829       (while (and groups prefixes)
2830         (while (not (string-match (car prefixes) (car groups)))
2831           (setq prefixes (cdr prefixes)))
2832         (setq prefix (car prefixes))
2833         (setq start (1- (length prefix)))
2834         (if (and (string-match "[^\\.]\\." (car groups) start)
2835                  (cdr groups)
2836                  (setq prefix
2837                        (concat "^" (substring (car groups) 0 (match-end 0))))
2838                  (string-match prefix (cadr groups)))
2839             (progn
2840               (setq prefixes (cons prefix prefixes))
2841               (message "Descend hierarchy %s? ([y]nsq): "
2842                        (substring prefix 1 (1- (length prefix))))
2843               (setq ans (read-char))
2844               (cond ((= ans ?n)
2845                      (while (and groups
2846                                  (string-match prefix
2847                                                (setq group (car groups))))
2848                        (setq gnus-killed-list
2849                              (cons group gnus-killed-list))
2850                        (gnus-sethash group group gnus-killed-hashtb)
2851                        (setq groups (cdr groups)))
2852                      (setq starts (cdr starts)))
2853                     ((= ans ?s)
2854                      (while (and groups
2855                                  (string-match prefix
2856                                                (setq group (car groups))))
2857                        (gnus-sethash group group gnus-killed-hashtb)
2858                        (gnus-subscribe-alphabetically (car groups))
2859                        (setq groups (cdr groups)))
2860                      (setq starts (cdr starts)))
2861                     ((= ans ?q)
2862                      (while groups
2863                        (setq group (car groups))
2864                        (setq gnus-killed-list (cons group gnus-killed-list))
2865                        (gnus-sethash group group gnus-killed-hashtb)
2866                        (setq groups (cdr groups))))
2867                     (t nil)))
2868           (message "Subscribe %s? ([n]yq)" (car groups))
2869           (setq ans (read-char))
2870           (setq group (car groups))
2871           (cond ((= ans ?y)
2872                  (gnus-subscribe-alphabetically (car groups))
2873                  (gnus-sethash group group gnus-killed-hashtb))
2874                 ((= ans ?q)
2875                  (while groups
2876                    (setq group (car groups))
2877                    (setq gnus-killed-list (cons group gnus-killed-list))
2878                    (gnus-sethash group group gnus-killed-hashtb)
2879                    (setq groups (cdr groups))))
2880                 (t
2881                  (setq gnus-killed-list (cons group gnus-killed-list))
2882                  (gnus-sethash group group gnus-killed-hashtb)))
2883           (setq groups (cdr groups)))))))
2884
2885 (defun gnus-subscribe-randomly (newsgroup)
2886   "Subscribe new NEWSGROUP by making it the first newsgroup."
2887   (gnus-subscribe-newsgroup newsgroup))
2888
2889 (defun gnus-subscribe-alphabetically (newgroup)
2890   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2891   (let ((groups (cdr gnus-newsrc-alist))
2892         before)
2893     (while (and (not before) groups)
2894       (if (string< newgroup (caar groups))
2895           (setq before (caar groups))
2896         (setq groups (cdr groups))))
2897     (gnus-subscribe-newsgroup newgroup before)))
2898
2899 (defun gnus-subscribe-hierarchically (newgroup)
2900   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2901   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2902   (save-excursion
2903     (set-buffer (find-file-noselect gnus-current-startup-file))
2904     (let ((groupkey newgroup)
2905           before)
2906       (while (and (not before) groupkey)
2907         (goto-char (point-min))
2908         (let ((groupkey-re
2909                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2910           (while (and (re-search-forward groupkey-re nil t)
2911                       (progn
2912                         (setq before (match-string 1))
2913                         (string< before newgroup)))))
2914         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2915         (setq groupkey
2916               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2917                   (substring groupkey (match-beginning 1) (match-end 1)))))
2918       (gnus-subscribe-newsgroup newgroup before))))
2919
2920 (defun gnus-subscribe-interactively (group)
2921   "Subscribe the new GROUP interactively.
2922 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2923 it is killed."
2924   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2925       (gnus-subscribe-hierarchically group)
2926     (push group gnus-killed-list)))
2927
2928 (defun gnus-subscribe-zombies (group)
2929   "Make the new GROUP into a zombie group."
2930   (push group gnus-zombie-list))
2931
2932 (defun gnus-subscribe-killed (group)
2933   "Make the new GROUP a killed group."
2934   (push group gnus-killed-list))
2935
2936 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2937   "Subscribe new NEWSGROUP.
2938 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2939 the first newsgroup."
2940   ;; We subscribe the group by changing its level to `subscribed'.
2941   (gnus-group-change-level
2942    newsgroup gnus-level-default-subscribed
2943    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2944   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2945
2946 ;; For directories
2947
2948 (defun gnus-newsgroup-directory-form (newsgroup)
2949   "Make hierarchical directory name from NEWSGROUP name."
2950   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2951         (len (length newsgroup))
2952         idx)
2953     ;; If this is a foreign group, we don't want to translate the
2954     ;; entire name.
2955     (if (setq idx (string-match ":" newsgroup))
2956         (aset newsgroup idx ?/)
2957       (setq idx 0))
2958     ;; Replace all occurrences of `.' with `/'.
2959     (while (< idx len)
2960       (if (= (aref newsgroup idx) ?.)
2961           (aset newsgroup idx ?/))
2962       (setq idx (1+ idx)))
2963     newsgroup))
2964
2965 (defun gnus-newsgroup-savable-name (group)
2966   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2967   ;; with dots.
2968   (nnheader-replace-chars-in-string group ?/ ?.))
2969
2970 (defun gnus-make-directory (dir)
2971   "Make DIRECTORY recursively."
2972   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2973   ;; of the many mysteries of the universe.
2974   (let* ((dir (expand-file-name dir default-directory))
2975          dirs err)
2976     (if (string-match "/$" dir)
2977         (setq dir (substring dir 0 (match-beginning 0))))
2978     ;; First go down the path until we find a directory that exists.
2979     (while (not (file-exists-p dir))
2980       (setq dirs (cons dir dirs))
2981       (string-match "/[^/]+$" dir)
2982       (setq dir (substring dir 0 (match-beginning 0))))
2983     ;; Then create all the subdirs.
2984     (while (and dirs (not err))
2985       (condition-case ()
2986           (make-directory (car dirs))
2987         (error (setq err t)))
2988       (setq dirs (cdr dirs)))
2989     ;; We return whether we were successful or not.
2990     (not dirs)))
2991
2992 (defun gnus-capitalize-newsgroup (newsgroup)
2993   "Capitalize NEWSGROUP name."
2994   (and (not (zerop (length newsgroup)))
2995        (concat (char-to-string (upcase (aref newsgroup 0)))
2996                (substring newsgroup 1))))
2997
2998 ;; Various... things.
2999
3000 (defun gnus-simplify-subject (subject &optional re-only)
3001   "Remove `Re:' and words in parentheses.
3002 If RE-ONLY is non-nil, strip leading `Re:'s only."
3003   (let ((case-fold-search t))           ;Ignore case.
3004     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3005     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3006       (setq subject (substring subject (match-end 0))))
3007     ;; Remove uninteresting prefixes.
3008     (if (and (not re-only)
3009              gnus-simplify-ignored-prefixes
3010              (string-match gnus-simplify-ignored-prefixes subject))
3011         (setq subject (substring subject (match-end 0))))
3012     ;; Remove words in parentheses from end.
3013     (unless re-only
3014       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3015         (setq subject (substring subject 0 (match-beginning 0)))))
3016     ;; Return subject string.
3017     subject))
3018
3019 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3020 ;; all whitespace.
3021 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3022 (defun gnus-simplify-buffer-fuzzy ()
3023   (goto-char (point-min))
3024   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3025   (goto-char (match-beginning 0))
3026   (while (or
3027           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3028           (looking-at "^[[].*:[ \t].*[]]$"))
3029     (goto-char (point-min))
3030     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3031                               nil t)
3032       (replace-match "" t t))
3033     (goto-char (point-min))
3034     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3035       (goto-char (match-end 0))
3036       (delete-char -1)
3037       (delete-region
3038        (progn (goto-char (match-beginning 0)))
3039        (re-search-forward ":"))))
3040   (goto-char (point-min))
3041   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3042     (replace-match "" t t))
3043   (goto-char (point-min))
3044   (while (re-search-forward "[ \t]+" nil t)
3045     (replace-match " " t t))
3046   (goto-char (point-min))
3047   (while (re-search-forward "[ \t]+$" nil t)
3048     (replace-match "" t t))
3049   (goto-char (point-min))
3050   (while (re-search-forward "^[ \t]+" nil t)
3051     (replace-match "" t t))
3052   (goto-char (point-min))
3053   (if gnus-simplify-subject-fuzzy-regexp
3054       (if (listp gnus-simplify-subject-fuzzy-regexp)
3055           (let ((list gnus-simplify-subject-fuzzy-regexp))
3056             (while list
3057               (goto-char (point-min))
3058               (while (re-search-forward (car list) nil t)
3059                 (replace-match "" t t))
3060               (setq list (cdr list))))
3061         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3062           (replace-match "" t t)))))
3063
3064 (defun gnus-simplify-subject-fuzzy (subject)
3065   "Siplify a subject string fuzzily."
3066   (save-excursion
3067     (gnus-set-work-buffer)
3068     (let ((case-fold-search t))
3069       (insert subject)
3070       (inline (gnus-simplify-buffer-fuzzy))
3071       (buffer-string))))
3072
3073 ;; Add the current buffer to the list of buffers to be killed on exit.
3074 (defun gnus-add-current-to-buffer-list ()
3075   (or (memq (current-buffer) gnus-buffer-list)
3076       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3077
3078 (defun gnus-string> (s1 s2)
3079   (not (or (string< s1 s2)
3080            (string= s1 s2))))
3081
3082 ;;; General various misc type functions.
3083
3084 (defun gnus-clear-system ()
3085   "Clear all variables and buffers."
3086   ;; Clear Gnus variables.
3087   (let ((variables gnus-variable-list))
3088     (while variables
3089       (set (car variables) nil)
3090       (setq variables (cdr variables))))
3091   ;; Clear other internal variables.
3092   (setq gnus-list-of-killed-groups nil
3093         gnus-have-read-active-file nil
3094         gnus-newsrc-alist nil
3095         gnus-newsrc-hashtb nil
3096         gnus-killed-list nil
3097         gnus-zombie-list nil
3098         gnus-killed-hashtb nil
3099         gnus-active-hashtb nil
3100         gnus-moderated-list nil
3101         gnus-description-hashtb nil
3102         gnus-newsgroup-headers nil
3103         gnus-newsgroup-name nil
3104         gnus-server-alist nil
3105         gnus-opened-servers nil
3106         gnus-current-select-method nil)
3107   ;; Reset any score variables.
3108   (when gnus-use-scoring 
3109     (gnus-score-close))
3110   ;; Kill the startup file.
3111   (and gnus-current-startup-file
3112        (get-file-buffer gnus-current-startup-file)
3113        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3114   ;; Save any cache buffers.
3115   (when gnus-use-cache 
3116     (gnus-cache-save-buffers))
3117   ;; Clear the dribble buffer.
3118   (gnus-dribble-clear)
3119   ;; Close down NoCeM.
3120   (when gnus-use-nocem 
3121     (gnus-nocem-close))
3122   ;; Shut down the demons.
3123   (when gnus-use-demon
3124     (gnus-demon-cancel))
3125   ;; Kill global KILL file buffer.
3126   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3127     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3128   (gnus-kill-buffer nntp-server-buffer)
3129   ;; Backlog.
3130   (when gnus-keep-backlog
3131     (gnus-backlog-shutdown))
3132   ;; Kill Gnus buffers.
3133   (while gnus-buffer-list
3134     (gnus-kill-buffer (pop gnus-buffer-list)))
3135   ;; Remove Gnus frames.
3136   (while gnus-created-frames
3137     (when (frame-live-p (car gnus-created-frames))
3138       ;; We slap a condition-case around this `delete-frame' to ensure 
3139       ;; agains errors if we try do delete the single frame that's left.
3140       (condition-case ()
3141           (delete-frame (car gnus-created-frames))
3142         (error nil)))
3143     (pop gnus-created-frames)))
3144
3145 (defun gnus-windows-old-to-new (setting)
3146   ;; First we take care of the really, really old Gnus 3 actions.
3147   (when (symbolp setting)
3148     (setq setting
3149           ;; Take care of ooold GNUS 3.x values.
3150           (cond ((eq setting 'SelectArticle) 'article)
3151                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3152                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3153                 (t setting))))
3154   (if (or (listp setting)
3155           (not (and gnus-window-configuration
3156                     (memq setting '(group summary article)))))
3157       setting
3158     (let* ((setting (if (eq setting 'group)
3159                         (if (assq 'newsgroup gnus-window-configuration)
3160                             'newsgroup
3161                           'newsgroups) setting))
3162            (elem (cadr (assq setting gnus-window-configuration)))
3163            (total (apply '+ elem))
3164            (types '(group summary article))
3165            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3166            (i 0)
3167            perc
3168            out)
3169       (while (< i 3)
3170         (or (not (numberp (nth i elem)))
3171             (zerop (nth i elem))
3172             (progn
3173               (setq perc  (/ (float (nth 0 elem)) total))
3174               (setq out (cons (if (eq pbuf (nth i types))
3175                                   (vector (nth i types) perc 'point)
3176                                 (vector (nth i types) perc))
3177                               out))))
3178         (setq i (1+ i)))
3179       (list (nreverse out)))))
3180
3181 ;;;###autoload
3182 (defun gnus-add-configuration (conf)
3183   "Add the window configuration CONF to `gnus-buffer-configuration'."
3184   (setq gnus-buffer-configuration
3185         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3186                          gnus-buffer-configuration))))
3187
3188 (defvar gnus-frame-list nil)
3189
3190 (defun gnus-configure-frame (split &optional window)
3191   "Split WINDOW according to SPLIT."
3192   (unless window
3193     (setq window (get-buffer-window (current-buffer))))
3194   (select-window window)
3195   ;; This might be an old-stylee buffer config.
3196   (when (vectorp split)
3197     (setq split (append split nil)))
3198   (when (or (consp (car split))
3199             (vectorp (car split)))
3200     (push 1.0 split)
3201     (push 'vertical split))
3202   ;; The SPLIT might be something that is to be evaled to
3203   ;; return a new SPLIT.
3204   (while (and (not (assq (car split) gnus-window-to-buffer))
3205               (gnus-functionp (car split)))
3206     (setq split (eval split)))
3207   (let* ((type (car split))
3208          (subs (cddr split))
3209          (len (if (eq type 'horizontal) (window-width) (window-height)))
3210          (total 0)
3211          (window-min-width (or gnus-window-min-width window-min-width))
3212          (window-min-height (or gnus-window-min-height window-min-height))
3213          s result new-win rest comp-subs size sub)
3214     (cond
3215      ;; Nothing to do here.
3216      ((null split))
3217      ;; Don't switch buffers.
3218      ((null type)
3219       (and (memq 'point split) window))
3220      ;; This is a buffer to be selected.
3221      ((not (memq type '(frame horizontal vertical)))
3222       (let ((buffer (cond ((stringp type) type)
3223                           (t (cdr (assq type gnus-window-to-buffer)))))
3224             buf)
3225         (unless buffer
3226           (error "Illegal buffer type: %s" type))
3227         (unless (setq buf (get-buffer (if (symbolp buffer)
3228                                           (symbol-value buffer) buffer)))
3229           (setq buf (get-buffer-create (if (symbolp buffer)
3230                                            (symbol-value buffer) buffer))))
3231         (switch-to-buffer buf)
3232         ;; We return the window if it has the `point' spec.
3233         (and (memq 'point split) window)))
3234      ;; This is a frame split.
3235      ((eq type 'frame)
3236       (unless gnus-frame-list
3237         (setq gnus-frame-list (list (window-frame
3238                                      (get-buffer-window (current-buffer))))))
3239       (let ((i 0)
3240             params frame fresult)
3241         (while (< i (length subs))
3242           ;; Frame parameter is gotten from the sub-split.
3243           (setq params (cadr (elt subs i)))
3244           ;; It should be a list.
3245           (unless (listp params)
3246             (setq params nil))
3247           ;; Create a new frame?
3248           (unless (setq frame (elt gnus-frame-list i))
3249             (nconc gnus-frame-list (list (setq frame (make-frame params)))))
3250           (push frame gnus-created-frames)
3251           ;; Is the old frame still alive?
3252           (unless (frame-live-p frame)
3253             (setcar (nthcdr i gnus-frame-list)
3254                     (setq frame (make-frame params))))
3255           ;; Select the frame in question and do more splits there.
3256           (select-frame frame)
3257           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3258           (incf i))
3259         ;; Select the frame that has the selected buffer.
3260         (when fresult
3261           (select-frame (window-frame fresult)))))
3262      ;; This is a normal split.
3263      (t
3264       (when (> (length subs) 0)
3265         ;; First we have to compute the sizes of all new windows.
3266         (while subs
3267           (setq sub (append (pop subs) nil))
3268           (while (and (not (assq (car sub) gnus-window-to-buffer))
3269                       (gnus-functionp (car sub)))
3270             (setq sub (eval sub)))
3271           (when sub
3272             (push sub comp-subs)
3273             (setq size (cadar comp-subs))
3274             (cond ((equal size 1.0)
3275                    (setq rest (car comp-subs))
3276                    (setq s 0))
3277                   ((floatp size)
3278                    (setq s (floor (* size len))))
3279                   ((integerp size)
3280                    (setq s size))
3281                   (t
3282                    (error "Illegal size: %s" size)))
3283             ;; Try to make sure that we are inside the safe limits.
3284             (cond ((zerop s))
3285                   ((eq type 'horizontal)
3286                    (setq s (max s window-min-width)))
3287                   ((eq type 'vertical)
3288                    (setq s (max s window-min-height))))
3289             (setcar (cdar comp-subs) s)
3290             (incf total s)))
3291         ;; Take care of the "1.0" spec.
3292         (if rest
3293             (setcar (cdr rest) (- len total))
3294           (error "No 1.0 specs in %s" split))
3295         ;; The we do the actual splitting in a nice recursive
3296         ;; fashion.
3297         (setq comp-subs (nreverse comp-subs))
3298         (while comp-subs
3299           (if (null (cdr comp-subs))
3300               (setq new-win window)
3301             (setq new-win
3302                   (split-window window (cadar comp-subs)
3303                                 (eq type 'horizontal))))
3304           (setq result (or (gnus-configure-frame
3305                             (car comp-subs) window) result))
3306           (select-window new-win)
3307           (setq window new-win)
3308           (setq comp-subs (cdr comp-subs))))
3309       ;; Return the proper window, if any.
3310       (when result
3311         (select-window result))))))
3312
3313 (defvar gnus-frame-split-p nil)
3314
3315 (defun gnus-configure-windows (setting &optional force)
3316   (setq setting (gnus-windows-old-to-new setting))
3317   (let ((split (if (symbolp setting)
3318                    (cadr (assq setting gnus-buffer-configuration))
3319                  setting))
3320         (in-buf (current-buffer))
3321         rule val w height hor ohor heights sub jump-buffer
3322         rel total to-buf all-visible)
3323
3324     (setq gnus-frame-split-p nil)
3325
3326     (unless split
3327       (error "No such setting: %s" setting))
3328
3329     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3330              (not force))
3331         ;; All the windows mentioned are already visible, so we just
3332         ;; put point in the assigned buffer, and do not touch the
3333         ;; winconf.
3334         (select-window all-visible)
3335
3336       ;; Either remove all windows or just remove all Gnus windows.
3337       (let ((frame (selected-frame)))
3338         (unwind-protect
3339             (if gnus-use-full-window
3340                 ;; We want to remove all other windows.
3341                 (if (not gnus-frame-split-p)
3342                     ;; This is not a `frame' split, so we ignore the
3343                     ;; other frames.  
3344                     (delete-other-windows)
3345                   ;; This is a `frame' split, so we delete all windows
3346                   ;; on all frames.
3347                   (mapcar 
3348                    (lambda (frame)
3349                      (unless (eq (cdr (assq 'minibuffer
3350                                             (frame-parameters frame)))
3351                                  'only)
3352                        (select-frame frame)
3353                        (delete-other-windows)))
3354                    (frame-list)))
3355               ;; Just remove some windows.
3356               (gnus-remove-some-windows)
3357               (switch-to-buffer nntp-server-buffer))
3358           (select-frame frame)))
3359
3360       (switch-to-buffer nntp-server-buffer)
3361       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3362
3363 (defun gnus-all-windows-visible-p (split)
3364   (when (vectorp split)
3365     (setq split (append split nil)))
3366   (when (or (consp (car split))
3367             (vectorp (car split)))
3368     (push 1.0 split)
3369     (push 'vertical split))
3370   ;; The SPLIT might be something that is to be evaled to
3371   ;; return a new SPLIT.
3372   (while (and (not (assq (car split) gnus-window-to-buffer))
3373               (gnus-functionp (car split)))
3374     (setq split (eval split)))
3375   (let* ((type (elt split 0)))
3376     (cond
3377      ((null split)
3378       t)
3379      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3380       (let ((buffer (cond ((stringp type) type)
3381                           (t (cdr (assq type gnus-window-to-buffer)))))
3382             win buf)
3383         (unless buffer
3384           (error "Illegal buffer type: %s" type))
3385         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3386                                       buffer)))
3387           (setq win (get-buffer-window buf t)))
3388         (when win
3389           (if (memq 'point split)
3390               win
3391             t))))
3392      (t
3393       (when (eq type 'frame)
3394         (setq gnus-frame-split-p t))
3395       (let ((n (mapcar 'gnus-all-windows-visible-p
3396                        (cddr split)))
3397             (win t))
3398         (while n
3399           (cond ((windowp (car n))
3400                  (setq win (car n)))
3401                 ((null (car n))
3402                  (setq win nil)))
3403           (setq n (cdr n)))
3404         win)))))
3405
3406 (defun gnus-window-top-edge (&optional window)
3407   (nth 1 (window-edges window)))
3408
3409 (defun gnus-remove-some-windows ()
3410   (let ((buffers gnus-window-to-buffer)
3411         buf bufs lowest-buf lowest)
3412     (save-excursion
3413       ;; Remove windows on all known Gnus buffers.
3414       (while buffers
3415         (setq buf (cdar buffers))
3416         (if (symbolp buf)
3417             (setq buf (and (boundp buf) (symbol-value buf))))
3418         (and buf
3419              (get-buffer-window buf)
3420              (progn
3421                (setq bufs (cons buf bufs))
3422                (pop-to-buffer buf)
3423                (if (or (not lowest)
3424                        (< (gnus-window-top-edge) lowest))
3425                    (progn
3426                      (setq lowest (gnus-window-top-edge))
3427                      (setq lowest-buf buf)))))
3428         (setq buffers (cdr buffers)))
3429       ;; Remove windows on *all* summary buffers.
3430       (let (wins)
3431         (walk-windows
3432          (lambda (win)
3433            (let ((buf (window-buffer win)))
3434              (if (string-match  "^\\*Summary" (buffer-name buf))
3435                  (progn
3436                    (setq bufs (cons buf bufs))
3437                    (pop-to-buffer buf)
3438                    (if (or (not lowest)
3439                            (< (gnus-window-top-edge) lowest))
3440                        (progn
3441                          (setq lowest-buf buf)
3442                          (setq lowest (gnus-window-top-edge))))))))))
3443       (and lowest-buf
3444            (progn
3445              (pop-to-buffer lowest-buf)
3446              (switch-to-buffer nntp-server-buffer)))
3447       (while bufs
3448         (and (not (eq (car bufs) lowest-buf))
3449              (delete-windows-on (car bufs)))
3450         (setq bufs (cdr bufs))))))
3451
3452 (defun gnus-version ()
3453   "Version numbers of this version of Gnus."
3454   (interactive)
3455   (let ((methods gnus-valid-select-methods)
3456         (mess gnus-version)
3457         meth)
3458     ;; Go through all the legal select methods and add their version
3459     ;; numbers to the total version string.  Only the backends that are
3460     ;; currently in use will have their message numbers taken into
3461     ;; consideration.
3462     (while methods
3463       (setq meth (intern (concat (caar methods) "-version")))
3464       (and (boundp meth)
3465            (stringp (symbol-value meth))
3466            (setq mess (concat mess "; " (symbol-value meth))))
3467       (setq methods (cdr methods)))
3468     (gnus-message 2 mess)))
3469
3470 (defun gnus-info-find-node ()
3471   "Find Info documentation of Gnus."
3472   (interactive)
3473   ;; Enlarge info window if needed.
3474   (let ((mode major-mode)
3475         gnus-info-buffer)
3476     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3477     (setq gnus-info-buffer (current-buffer))
3478     (gnus-configure-windows 'info)))
3479
3480 (defun gnus-days-between (date1 date2)
3481   ;; Return the number of days between date1 and date2.
3482   (- (gnus-day-number date1) (gnus-day-number date2)))
3483
3484 (defun gnus-day-number (date)
3485   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3486                      (timezone-parse-date date))))
3487     (timezone-absolute-from-gregorian
3488      (nth 1 dat) (nth 2 dat) (car dat))))
3489
3490 (defun gnus-encode-date (date)
3491   "Convert DATE to internal time."
3492   (let* ((parse (timezone-parse-date date))
3493          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3494          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3495     (encode-time (caddr time) (cadr time) (car time)
3496                  (caddr date) (cadr date) (car date) (nth 4 date))))
3497
3498 (defun gnus-time-minus (t1 t2)
3499   "Subtract two internal times."
3500   (let ((borrow (< (cadr t1) (cadr t2))))
3501     (list (- (car t1) (car t2) (if borrow 1 0))
3502           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3503
3504 (defun gnus-file-newer-than (file date)
3505   (let ((fdate (nth 5 (file-attributes file))))
3506     (or (> (car fdate) (car date))
3507         (and (= (car fdate) (car date))
3508              (> (nth 1 fdate) (nth 1 date))))))
3509
3510 (defmacro gnus-local-set-keys (&rest plist)
3511   "Set the keys in PLIST in the current keymap."
3512   `(gnus-define-keys-1 (current-local-map) ',plist))
3513
3514 (defmacro gnus-define-keys (keymap &rest plist)
3515   "Define all keys in PLIST in KEYMAP."
3516   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3517
3518 (put 'gnus-define-keys 'lisp-indent-function 1)
3519 (put 'gnus-define-keys 'lisp-indent-hook 1)
3520 (put 'gnus-define-keymap 'lisp-indent-function 1)
3521 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3522
3523 (defmacro gnus-define-keymap (keymap &rest plist)
3524   "Define all keys in PLIST in KEYMAP."
3525   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3526
3527 (defun gnus-define-keys-1 (keymap plist)
3528   (when (null keymap)
3529     (error "Can't set keys in a null keymap"))
3530   (cond ((symbolp keymap)
3531          (setq keymap (symbol-value keymap)))
3532         ((keymapp keymap))
3533         ((listp keymap)
3534          (set (car keymap) nil)
3535          (define-prefix-command (car keymap))
3536          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3537          (setq keymap (symbol-value (car keymap)))))
3538   (let (key)
3539     (while plist
3540       (when (symbolp (setq key (pop plist)))
3541         (setq key (symbol-value key)))
3542       (define-key keymap key (pop plist)))))
3543
3544 (defun gnus-group-read-only-p (&optional group)
3545   "Check whether GROUP supports editing or not.
3546 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3547 that that variable is buffer-local to the summary buffers."
3548   (let ((group (or group gnus-newsgroup-name)))
3549     (not (gnus-check-backend-function 'request-replace-article group))))
3550
3551 (defun gnus-group-total-expirable-p (group)
3552   "Check whether GROUP is total-expirable or not."
3553   (let ((params (gnus-info-params (gnus-get-info group))))
3554     (or (memq 'total-expire params)
3555         (cdr (assq 'total-expire params)) ; (total-expire . t)
3556         (and gnus-total-expirable-newsgroups ; Check var.
3557              (string-match gnus-total-expirable-newsgroups group)))))
3558
3559 (defun gnus-group-auto-expirable-p (group)
3560   "Check whether GROUP is total-expirable or not."
3561   (let ((params (gnus-info-params (gnus-get-info group))))
3562     (or (memq 'auto-expire params)
3563         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3564         (and gnus-auto-expirable-newsgroups ; Check var.
3565              (string-match gnus-auto-expirable-newsgroups group)))))
3566
3567 (defun gnus-virtual-group-p (group)
3568   "Say whether GROUP is virtual or not."
3569   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3570                         gnus-valid-select-methods)))
3571
3572 (defsubst gnus-simplify-subject-fully (subject)
3573   "Simplify a subject string according to the user's wishes."
3574   (cond
3575    ((null gnus-summary-gather-subject-limit)
3576     (gnus-simplify-subject-re subject))
3577    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3578     (gnus-simplify-subject-fuzzy subject))
3579    ((numberp gnus-summary-gather-subject-limit)
3580     (gnus-limit-string (gnus-simplify-subject-re subject)
3581                        gnus-summary-gather-subject-limit))
3582    (t
3583     subject)))
3584
3585 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3586   "Check whether two subjects are equal.  If optional argument
3587 simple-first is t, first argument is already simplified."
3588   (cond
3589    ((null simple-first)
3590     (equal (gnus-simplify-subject-fully s1)
3591            (gnus-simplify-subject-fully s2)))
3592    (t
3593     (equal s1
3594            (gnus-simplify-subject-fully s2)))))
3595
3596 ;; Returns a list of writable groups.
3597 (defun gnus-writable-groups ()
3598   (let ((alist gnus-newsrc-alist)
3599         groups group)
3600     (while (setq group (car (pop alist)))
3601       (unless (gnus-group-read-only-p group)
3602         (push group groups)))
3603     (nreverse groups)))
3604
3605 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3606 ;; the echo area.
3607 (defun gnus-y-or-n-p (prompt)
3608   (prog1
3609       (y-or-n-p prompt)
3610     (message "")))
3611
3612 (defun gnus-yes-or-no-p (prompt)
3613   (prog1
3614       (yes-or-no-p prompt)
3615     (message "")))
3616
3617 ;; Check whether to use long file names.
3618 (defun gnus-use-long-file-name (symbol)
3619   ;; The variable has to be set...
3620   (and gnus-use-long-file-name
3621        ;; If it isn't a list, then we return t.
3622        (or (not (listp gnus-use-long-file-name))
3623            ;; If it is a list, and the list contains `symbol', we
3624            ;; return nil.
3625            (not (memq symbol gnus-use-long-file-name)))))
3626
3627 ;; I suspect there's a better way, but I haven't taken the time to do
3628 ;; it yet. -erik selberg@cs.washington.edu
3629 (defun gnus-dd-mmm (messy-date)
3630   "Return a string like DD-MMM from a big messy string"
3631   (let ((datevec (timezone-parse-date messy-date)))
3632     (format "%2s-%s"
3633             (or (aref datevec 2) "??")
3634             (capitalize
3635              (or (car
3636                   (nth (1- (string-to-number (aref datevec 1)))
3637                        timezone-months-assoc))
3638                  "???")))))
3639
3640 ;; Make a hash table (default and minimum size is 255).
3641 ;; Optional argument HASHSIZE specifies the table size.
3642 (defun gnus-make-hashtable (&optional hashsize)
3643   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3644
3645 ;; Make a number that is suitable for hashing; bigger than MIN and one
3646 ;; less than 2^x.
3647 (defun gnus-create-hash-size (min)
3648   (let ((i 1))
3649     (while (< i min)
3650       (setq i (* 2 i)))
3651     (1- i)))
3652
3653 ;; Show message if message has a lower level than `gnus-verbose'.
3654 ;; Guideline for numbers:
3655 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3656 ;; for things that take a long time, 7 - not very important messages
3657 ;; on stuff, 9 - messages inside loops.
3658 (defun gnus-message (level &rest args)
3659   (if (<= level gnus-verbose)
3660       (apply 'message args)
3661     ;; We have to do this format thingy here even if the result isn't
3662     ;; shown - the return value has to be the same as the return value
3663     ;; from `message'.
3664     (apply 'format args)))
3665
3666 (defun gnus-functionp (form)
3667   "Return non-nil if FORM is funcallable."
3668   (or (and (symbolp form) (fboundp form))
3669       (and (listp form) (eq (car form) 'lambda))))
3670
3671 ;; Generate a unique new group name.
3672 (defun gnus-generate-new-group-name (leaf)
3673   (let ((name leaf)
3674         (num 0))
3675     (while (gnus-gethash name gnus-newsrc-hashtb)
3676       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3677     name))
3678
3679 ;; Find out whether the gnus-visual TYPE is wanted.
3680 (defun gnus-visual-p (&optional type class)
3681   (and gnus-visual                      ; Has to be non-nil, at least.
3682        (if (not type)                   ; We don't care about type.
3683            gnus-visual
3684          (if (listp gnus-visual)        ; It's a list, so we check it.
3685              (or (memq type gnus-visual)
3686                  (memq class gnus-visual))
3687            t))))
3688
3689 (defun gnus-parent-id (references)
3690   "Return the last Message-ID in REFERENCES."
3691   (when (and references
3692              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3693     (substring references (match-beginning 1) (match-end 1))))
3694
3695 (defun gnus-split-references (references)
3696   "Return a list of Message-IDs in REFERENCES."
3697   (let ((beg 0)
3698         ids)
3699     (while (string-match "<[^>]+>" references beg)
3700       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3701             ids))
3702     (nreverse ids)))
3703
3704 (defun gnus-ephemeral-group-p (group)
3705   "Say whether GROUP is ephemeral or not."
3706   (gnus-group-get-parameter group 'quit-config))
3707
3708 (defun gnus-group-quit-config (group)
3709   "Return the quit-config of GROUP."
3710   (gnus-group-get-parameter group 'quit-config))
3711
3712 (defun gnus-simplify-mode-line ()
3713   "Make mode lines a bit simpler."
3714   (setq mode-line-modified "-- ")
3715   (when (listp mode-line-format)
3716     (make-local-variable 'mode-line-format)
3717     (setq mode-line-format (copy-sequence mode-line-format))
3718     (when (equal (nth 3 mode-line-format) "   ")
3719       (setcar (nthcdr 3 mode-line-format) " "))))
3720
3721 ;;; List and range functions
3722
3723 (defun gnus-last-element (list)
3724   "Return last element of LIST."
3725   (while (cdr list)
3726     (setq list (cdr list)))
3727   (car list))
3728
3729 (defun gnus-copy-sequence (list)
3730   "Do a complete, total copy of a list."
3731   (if (and (consp list) (not (consp (cdr list))))
3732       (cons (car list) (cdr list))
3733     (mapcar (lambda (elem) (if (consp elem)
3734                                (if (consp (cdr elem))
3735                                    (gnus-copy-sequence elem)
3736                                  (cons (car elem) (cdr elem)))
3737                              elem))
3738             list)))
3739
3740 (defun gnus-set-difference (list1 list2)
3741   "Return a list of elements of LIST1 that do not appear in LIST2."
3742   (let ((list1 (copy-sequence list1)))
3743     (while list2
3744       (setq list1 (delq (car list2) list1))
3745       (setq list2 (cdr list2)))
3746     list1))
3747
3748 (defun gnus-sorted-complement (list1 list2)
3749   "Return a list of elements of LIST1 that do not appear in LIST2.
3750 Both lists have to be sorted over <."
3751   (let (out)
3752     (if (or (null list1) (null list2))
3753         (or list1 list2)
3754       (while (and list1 list2)
3755         (cond ((= (car list1) (car list2))
3756                (setq list1 (cdr list1)
3757                      list2 (cdr list2)))
3758               ((< (car list1) (car list2))
3759                (setq out (cons (car list1) out))
3760                (setq list1 (cdr list1)))
3761               (t
3762                (setq out (cons (car list2) out))
3763                (setq list2 (cdr list2)))))
3764       (nconc (nreverse out) (or list1 list2)))))
3765
3766 (defun gnus-intersection (list1 list2)
3767   (let ((result nil))
3768     (while list2
3769       (if (memq (car list2) list1)
3770           (setq result (cons (car list2) result)))
3771       (setq list2 (cdr list2)))
3772     result))
3773
3774 (defun gnus-sorted-intersection (list1 list2)
3775   ;; LIST1 and LIST2 have to be sorted over <.
3776   (let (out)
3777     (while (and list1 list2)
3778       (cond ((= (car list1) (car list2))
3779              (setq out (cons (car list1) out)
3780                    list1 (cdr list1)
3781                    list2 (cdr list2)))
3782             ((< (car list1) (car list2))
3783              (setq list1 (cdr list1)))
3784             (t
3785              (setq list2 (cdr list2)))))
3786     (nreverse out)))
3787
3788 (defun gnus-set-sorted-intersection (list1 list2)
3789   ;; LIST1 and LIST2 have to be sorted over <.
3790   ;; This function modifies LIST1.
3791   (let* ((top (cons nil list1))
3792          (prev top))
3793     (while (and list1 list2)
3794       (cond ((= (car list1) (car list2))
3795              (setq prev list1
3796                    list1 (cdr list1)
3797                    list2 (cdr list2)))
3798             ((< (car list1) (car list2))
3799              (setcdr prev (cdr list1))
3800              (setq list1 (cdr list1)))
3801             (t
3802              (setq list2 (cdr list2)))))
3803     (setcdr prev nil)
3804     (cdr top)))
3805
3806 (defun gnus-compress-sequence (numbers &optional always-list)
3807   "Convert list of numbers to a list of ranges or a single range.
3808 If ALWAYS-LIST is non-nil, this function will always release a list of
3809 ranges."
3810   (let* ((first (car numbers))
3811          (last (car numbers))
3812          result)
3813     (if (null numbers)
3814         nil
3815       (if (not (listp (cdr numbers)))
3816           numbers
3817         (while numbers
3818           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3819                 ((= (1+ last) (car numbers)) ;Still in sequence
3820                  (setq last (car numbers)))
3821                 (t                      ;End of one sequence
3822                  (setq result
3823                        (cons (if (= first last) first
3824                                (cons first last)) result))
3825                  (setq first (car numbers))
3826                  (setq last  (car numbers))))
3827           (setq numbers (cdr numbers)))
3828         (if (and (not always-list) (null result))
3829             (if (= first last) (list first) (cons first last))
3830           (nreverse (cons (if (= first last) first (cons first last))
3831                           result)))))))
3832
3833 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3834 (defun gnus-uncompress-range (ranges)
3835   "Expand a list of ranges into a list of numbers.
3836 RANGES is either a single range on the form `(num . num)' or a list of
3837 these ranges."
3838   (let (first last result)
3839     (cond
3840      ((null ranges)
3841       nil)
3842      ((not (listp (cdr ranges)))
3843       (setq first (car ranges))
3844       (setq last (cdr ranges))
3845       (while (<= first last)
3846         (setq result (cons first result))
3847         (setq first (1+ first)))
3848       (nreverse result))
3849      (t
3850       (while ranges
3851         (if (atom (car ranges))
3852             (if (numberp (car ranges))
3853                 (setq result (cons (car ranges) result)))
3854           (setq first (caar ranges))
3855           (setq last  (cdar ranges))
3856           (while (<= first last)
3857             (setq result (cons first result))
3858             (setq first (1+ first))))
3859         (setq ranges (cdr ranges)))
3860       (nreverse result)))))
3861
3862 (defun gnus-add-to-range (ranges list)
3863   "Return a list of ranges that has all articles from both RANGES and LIST.
3864 Note: LIST has to be sorted over `<'."
3865   (if (not ranges)
3866       (gnus-compress-sequence list t)
3867     (setq list (copy-sequence list))
3868     (or (listp (cdr ranges))
3869         (setq ranges (list ranges)))
3870     (let ((out ranges)
3871           ilist lowest highest temp)
3872       (while (and ranges list)
3873         (setq ilist list)
3874         (setq lowest (or (and (atom (car ranges)) (car ranges))
3875                          (caar ranges)))
3876         (while (and list (cdr list) (< (cadr list) lowest))
3877           (setq list (cdr list)))
3878         (if (< (car ilist) lowest)
3879             (progn
3880               (setq temp list)
3881               (setq list (cdr list))
3882               (setcdr temp nil)
3883               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3884         (setq highest (or (and (atom (car ranges)) (car ranges))
3885                           (cdar ranges)))
3886         (while (and list (<= (car list) highest))
3887           (setq list (cdr list)))
3888         (setq ranges (cdr ranges)))
3889       (if list
3890           (setq out (nconc (gnus-compress-sequence list t) out)))
3891       (setq out (sort out (lambda (r1 r2)
3892                             (< (or (and (atom r1) r1) (car r1))
3893                                (or (and (atom r2) r2) (car r2))))))
3894       (setq ranges out)
3895       (while ranges
3896         (if (atom (car ranges))
3897             (if (cdr ranges)
3898                 (if (atom (cadr ranges))
3899                     (if (= (1+ (car ranges)) (cadr ranges))
3900                         (progn
3901                           (setcar ranges (cons (car ranges)
3902                                                (cadr ranges)))
3903                           (setcdr ranges (cddr ranges))))
3904                   (if (= (1+ (car ranges)) (caadr ranges))
3905                       (progn
3906                         (setcar (cadr ranges) (car ranges))
3907                         (setcar ranges (cadr ranges))
3908                         (setcdr ranges (cddr ranges))))))
3909           (if (cdr ranges)
3910               (if (atom (cadr ranges))
3911                   (if (= (1+ (cdar ranges)) (cadr ranges))
3912                       (progn
3913                         (setcdr (car ranges) (cadr ranges))
3914                         (setcdr ranges (cddr ranges))))
3915                 (if (= (1+ (cdar ranges)) (caadr ranges))
3916                     (progn
3917                       (setcdr (car ranges) (cdadr ranges))
3918                       (setcdr ranges (cddr ranges)))))))
3919         (setq ranges (cdr ranges)))
3920       out)))
3921
3922 (defun gnus-remove-from-range (ranges list)
3923   "Return a list of ranges that has all articles from LIST removed from RANGES.
3924 Note: LIST has to be sorted over `<'."
3925   ;; !!! This function shouldn't look like this, but I've got a headache.
3926   (gnus-compress-sequence
3927    (gnus-sorted-complement
3928     (gnus-uncompress-range ranges) list)))
3929
3930 (defun gnus-member-of-range (number ranges)
3931   (if (not (listp (cdr ranges)))
3932       (and (>= number (car ranges))
3933            (<= number (cdr ranges)))
3934     (let ((not-stop t))
3935       (while (and ranges
3936                   (if (numberp (car ranges))
3937                       (>= number (car ranges))
3938                     (>= number (caar ranges)))
3939                   not-stop)
3940         (if (if (numberp (car ranges))
3941                 (= number (car ranges))
3942               (and (>= number (caar ranges))
3943                    (<= number (cdar ranges))))
3944             (setq not-stop nil))
3945         (setq ranges (cdr ranges)))
3946       (not not-stop))))
3947
3948 (defun gnus-range-length (range)
3949   "Return the length RANGE would have if uncompressed."
3950   (length (gnus-uncompress-range range)))
3951
3952 (defun gnus-sublist-p (list sublist)
3953   "Test whether all elements in SUBLIST are members of LIST."
3954   (let ((sublistp t))
3955     (while sublist
3956       (unless (memq (pop sublist) list)
3957         (setq sublistp nil
3958               sublist nil)))
3959     sublistp))
3960
3961 \f
3962 ;;;
3963 ;;; Gnus group mode
3964 ;;;
3965
3966 (defvar gnus-group-mode-map nil)
3967 (put 'gnus-group-mode 'mode-class 'special)
3968
3969 (unless gnus-group-mode-map
3970   (setq gnus-group-mode-map (make-keymap))
3971   (suppress-keymap gnus-group-mode-map)
3972
3973   (gnus-define-keys gnus-group-mode-map
3974     " " gnus-group-read-group
3975     "=" gnus-group-select-group
3976     "\M- " gnus-group-unhidden-select-group
3977     "\r" gnus-group-select-group
3978     "\M-\r" gnus-group-quick-select-group
3979     "j" gnus-group-jump-to-group
3980     "n" gnus-group-next-unread-group
3981     "p" gnus-group-prev-unread-group
3982     "\177" gnus-group-prev-unread-group
3983     [delete] gnus-group-prev-unread-group
3984     "N" gnus-group-next-group
3985     "P" gnus-group-prev-group
3986     "\M-n" gnus-group-next-unread-group-same-level
3987     "\M-p" gnus-group-prev-unread-group-same-level
3988     "," gnus-group-best-unread-group
3989     "." gnus-group-first-unread-group
3990     "u" gnus-group-unsubscribe-current-group
3991     "U" gnus-group-unsubscribe-group
3992     "c" gnus-group-catchup-current
3993     "C" gnus-group-catchup-current-all
3994     "l" gnus-group-list-groups
3995     "L" gnus-group-list-all-groups
3996     "m" gnus-group-mail
3997     "g" gnus-group-get-new-news
3998     "\M-g" gnus-group-get-new-news-this-group
3999     "R" gnus-group-restart
4000     "r" gnus-group-read-init-file
4001     "B" gnus-group-browse-foreign-server
4002     "b" gnus-group-check-bogus-groups
4003     "F" gnus-find-new-newsgroups
4004     "\C-c\C-d" gnus-group-describe-group
4005     "\M-d" gnus-group-describe-all-groups
4006     "\C-c\C-a" gnus-group-apropos
4007     "\C-c\M-\C-a" gnus-group-description-apropos
4008     "a" gnus-group-post-news
4009     "\ek" gnus-group-edit-local-kill
4010     "\eK" gnus-group-edit-global-kill
4011     "\C-k" gnus-group-kill-group
4012     "\C-y" gnus-group-yank-group
4013     "\C-w" gnus-group-kill-region
4014     "\C-x\C-t" gnus-group-transpose-groups
4015     "\C-c\C-l" gnus-group-list-killed
4016     "\C-c\C-x" gnus-group-expire-articles
4017     "\C-c\M-\C-x" gnus-group-expire-all-groups
4018     "V" gnus-version
4019     "s" gnus-group-save-newsrc
4020     "z" gnus-group-suspend
4021     "Z" gnus-group-clear-dribble
4022     "q" gnus-group-exit
4023     "Q" gnus-group-quit
4024     "?" gnus-group-describe-briefly
4025     "\C-c\C-i" gnus-info-find-node
4026     "\M-e" gnus-group-edit-group-method
4027     "^" gnus-group-enter-server-mode
4028     gnus-mouse-2 gnus-mouse-pick-group
4029     "<" beginning-of-buffer
4030     ">" end-of-buffer
4031     "\C-c\C-b" gnus-bug
4032     "\C-c\C-s" gnus-group-sort-groups
4033     "t" gnus-topic-mode
4034     "\C-c\M-g" gnus-activate-all-groups
4035     "\M-&" gnus-group-universal-argument
4036     "#" gnus-group-mark-group
4037     "\M-#" gnus-group-unmark-group)
4038
4039   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4040     "m" gnus-group-mark-group
4041     "u" gnus-group-unmark-group
4042     "w" gnus-group-mark-region
4043     "m" gnus-group-mark-buffer
4044     "r" gnus-group-mark-regexp
4045     "U" gnus-group-unmark-all-groups)
4046
4047   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4048     "d" gnus-group-make-directory-group
4049     "h" gnus-group-make-help-group
4050     "a" gnus-group-make-archive-group
4051     "k" gnus-group-make-kiboze-group
4052     "m" gnus-group-make-group
4053     "E" gnus-group-edit-group
4054     "e" gnus-group-edit-group-method
4055     "p" gnus-group-edit-group-parameters
4056     "v" gnus-group-add-to-virtual
4057     "V" gnus-group-make-empty-virtual
4058     "D" gnus-group-enter-directory
4059     "f" gnus-group-make-doc-group
4060     "r" gnus-group-rename-group
4061     "\177" gnus-group-delete-group
4062     [delete] gnus-group-delete-group)
4063
4064    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4065      "b" gnus-group-brew-soup
4066      "w" gnus-soup-save-areas
4067      "s" gnus-soup-send-replies
4068      "p" gnus-soup-pack-packet
4069      "r" nnsoup-pack-replies)
4070
4071    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4072      "s" gnus-group-sort-groups
4073      "a" gnus-group-sort-groups-by-alphabet
4074      "u" gnus-group-sort-groups-by-unread
4075      "l" gnus-group-sort-groups-by-level
4076      "v" gnus-group-sort-groups-by-score
4077      "r" gnus-group-sort-groups-by-rank
4078      "m" gnus-group-sort-groups-by-method)
4079
4080    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4081      "k" gnus-group-list-killed
4082      "z" gnus-group-list-zombies
4083      "s" gnus-group-list-groups
4084      "u" gnus-group-list-all-groups
4085      "A" gnus-group-list-active
4086      "a" gnus-group-apropos
4087      "d" gnus-group-description-apropos
4088      "m" gnus-group-list-matching
4089      "M" gnus-group-list-all-matching
4090      "l" gnus-group-list-level)
4091
4092    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4093      "f" gnus-score-flush-cache)
4094
4095    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4096      "f" gnus-group-fetch-faq)
4097
4098    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4099      "l" gnus-group-set-current-level
4100      "t" gnus-group-unsubscribe-current-group
4101      "s" gnus-group-unsubscribe-group
4102      "k" gnus-group-kill-group
4103      "y" gnus-group-yank-group
4104      "w" gnus-group-kill-region
4105      "\C-k" gnus-group-kill-level
4106      "z" gnus-group-kill-all-zombies))
4107
4108 (defun gnus-group-mode ()
4109   "Major mode for reading news.
4110
4111 All normal editing commands are switched off.
4112 \\<gnus-group-mode-map>
4113 The group buffer lists (some of) the groups available.  For instance,
4114 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4115 lists all zombie groups.
4116
4117 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4118 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4119
4120 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4121
4122 The following commands are available:
4123
4124 \\{gnus-group-mode-map}"
4125   (interactive)
4126   (when (and menu-bar-mode
4127              (gnus-visual-p 'group-menu 'menu))
4128     (gnus-group-make-menu-bar))
4129   (kill-all-local-variables)
4130   (gnus-simplify-mode-line)
4131   (setq major-mode 'gnus-group-mode)
4132   (setq mode-name "Group")
4133   (gnus-group-set-mode-line)
4134   (setq mode-line-process nil)
4135   (use-local-map gnus-group-mode-map)
4136   (buffer-disable-undo (current-buffer))
4137   (setq truncate-lines t)
4138   (setq buffer-read-only t)
4139   (run-hooks 'gnus-group-mode-hook))
4140
4141 (defun gnus-mouse-pick-group (e)
4142   "Enter the group under the mouse pointer."
4143   (interactive "e")
4144   (mouse-set-point e)
4145   (gnus-group-read-group nil))
4146
4147 ;; Look at LEVEL and find out what the level is really supposed to be.
4148 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4149 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4150 (defun gnus-group-default-level (&optional level number-or-nil)
4151   (cond
4152    (gnus-group-use-permanent-levels
4153     (setq gnus-group-default-list-level
4154           (or level gnus-group-default-list-level))
4155     (or gnus-group-default-list-level gnus-level-subscribed))
4156    (number-or-nil
4157     level)
4158    (t
4159     (or level gnus-group-default-list-level gnus-level-subscribed))))
4160
4161 ;;;###autoload
4162 (defun gnus-slave-no-server (&optional arg)
4163   "Read network news as a slave, without connecting to local server"
4164   (interactive "P")
4165   (gnus-no-server arg t))
4166
4167 ;;;###autoload
4168 (defun gnus-no-server (&optional arg slave)
4169   "Read network news.
4170 If ARG is a positive number, Gnus will use that as the
4171 startup level.  If ARG is nil, Gnus will be started at level 2.
4172 If ARG is non-nil and not a positive number, Gnus will
4173 prompt the user for the name of an NNTP server to use.
4174 As opposed to `gnus', this command will not connect to the local server."
4175   (interactive "P")
4176   (make-local-variable 'gnus-group-use-permanent-levels)
4177   (setq gnus-group-use-permanent-levels t)
4178   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4179
4180 ;;;###autoload
4181 (defun gnus-slave (&optional arg)
4182   "Read news as a slave."
4183   (interactive "P")
4184   (gnus arg nil 'slave))
4185
4186 ;;;###autoload
4187 (defun gnus-other-frame (&optional arg)
4188   "Pop up a frame to read news."
4189   (interactive "P")
4190   (if (get-buffer gnus-group-buffer)
4191       (let ((pop-up-frames t))
4192         (gnus arg))
4193     (select-frame (make-frame))
4194     (gnus arg)))
4195
4196 ;;;###autoload
4197 (defun gnus (&optional arg dont-connect slave)
4198   "Read network news.
4199 If ARG is non-nil and a positive number, Gnus will use that as the
4200 startup level.  If ARG is non-nil and not a positive number, Gnus will
4201 prompt the user for the name of an NNTP server to use."
4202   (interactive "P")
4203
4204   (if (get-buffer gnus-group-buffer)
4205       (progn
4206         (switch-to-buffer gnus-group-buffer)
4207         (gnus-group-get-new-news))
4208
4209     (gnus-clear-system)
4210     (nnheader-init-server-buffer)
4211     (gnus-read-init-file)
4212     (setq gnus-slave slave)
4213
4214     (gnus-group-setup-buffer)
4215     (let ((buffer-read-only nil))
4216       (erase-buffer)
4217       (if (not gnus-inhibit-startup-message)
4218           (progn
4219             (gnus-group-startup-message)
4220             (sit-for 0))))
4221
4222     (let ((level (and (numberp arg) (> arg 0) arg))
4223           did-connect)
4224       (unwind-protect
4225           (progn
4226             (or dont-connect
4227                 (setq did-connect
4228                       (gnus-start-news-server (and arg (not level))))))
4229         (if (and (not dont-connect)
4230                  (not did-connect))
4231             (gnus-group-quit)
4232           (run-hooks 'gnus-startup-hook)
4233           ;; NNTP server is successfully open.
4234
4235           ;; Find the current startup file name.
4236           (setq gnus-current-startup-file
4237                 (gnus-make-newsrc-file gnus-startup-file))
4238
4239           ;; Read the dribble file.
4240           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4241
4242           ;; Allow using GroupLens predictions.
4243           (when gnus-use-grouplens
4244             (bbb-login)
4245             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4246
4247           (gnus-summary-make-display-table)
4248           ;; Do the actual startup.
4249           (gnus-setup-news nil level)
4250           ;; Generate the group buffer.
4251           (gnus-group-list-groups level)
4252           (gnus-group-first-unread-group)
4253           (gnus-configure-windows 'group)
4254           (gnus-group-set-mode-line))))))
4255
4256 (defun gnus-unload ()
4257   "Unload all Gnus features."
4258   (interactive)
4259   (or (boundp 'load-history)
4260       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4261   (let ((history load-history)
4262         feature)
4263     (while history
4264       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4265            (setq feature (cdr (assq 'provide (car history))))
4266            (unload-feature feature 'force))
4267       (setq history (cdr history)))))
4268
4269 (defun gnus-compile ()
4270   "Byte-compile the user-defined format specs."
4271   (interactive)
4272   (let ((entries gnus-format-specs)
4273         entry gnus-tmp-func)
4274     (save-excursion
4275       (gnus-message 7 "Compiling format specs...")
4276
4277       (while entries
4278         (setq entry (pop entries))
4279         (if (eq (car entry) 'version)
4280             (setq gnus-format-specs (delq entry gnus-format-specs))
4281           (when (and (listp (caddr entry))
4282                      (not (eq 'byte-code (caaddr entry))))
4283             (fset 'gnus-tmp-func
4284                   `(lambda () ,(caddr entry)))
4285             (byte-compile 'gnus-tmp-func)
4286             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4287
4288       (push (cons 'version emacs-version) gnus-format-specs)
4289
4290       (gnus-message 7 "Compiling user specs...done"))))
4291
4292 (defun gnus-indent-rigidly (start end arg)
4293   "Indent rigidly using only spaces and no tabs."
4294   (save-excursion
4295     (save-restriction
4296       (narrow-to-region start end)
4297       (indent-rigidly start end arg)
4298       (goto-char (point-min))
4299       (while (search-forward "\t" nil t)
4300         (replace-match "        " t t)))))
4301
4302 (defun gnus-group-startup-message (&optional x y)
4303   "Insert startup message in current buffer."
4304   ;; Insert the message.
4305   (erase-buffer)
4306   (insert
4307    (format "              %s
4308           _    ___ _             _
4309           _ ___ __ ___  __    _ ___
4310           __   _     ___    __  ___
4311               _           ___     _
4312              _  _ __             _
4313              ___   __            _
4314                    __           _
4315                     _      _   _
4316                    _      _    _
4317                       _  _    _
4318                   __  ___
4319                  _   _ _     _
4320                 _   _
4321               _    _
4322              _    _
4323             _
4324           __
4325
4326 "
4327            ""))
4328   ;; And then hack it.
4329   (gnus-indent-rigidly (point-min) (point-max)
4330                        (/ (max (- (window-width) (or x 46)) 0) 2))
4331   (goto-char (point-min))
4332   (forward-line 1)
4333   (let* ((pheight (count-lines (point-min) (point-max)))
4334          (wheight (window-height))
4335          (rest (- wheight pheight)))
4336     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4337   ;; Fontify some.
4338   (goto-char (point-min))
4339   (and (search-forward "Praxis" nil t)
4340        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4341   (goto-char (point-min))
4342   (let* ((mode-string (gnus-group-set-mode-line)))
4343     (setq mode-line-buffer-identification
4344           (list (concat gnus-version (substring (car mode-string) 4))))
4345     (set-buffer-modified-p t)))
4346
4347 (defun gnus-group-setup-buffer ()
4348   (or (get-buffer gnus-group-buffer)
4349       (progn
4350         (switch-to-buffer gnus-group-buffer)
4351         (gnus-add-current-to-buffer-list)
4352         (gnus-group-mode)
4353         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4354
4355 (defun gnus-group-list-groups (&optional level unread lowest)
4356   "List newsgroups with level LEVEL or lower that have unread articles.
4357 Default is all subscribed groups.
4358 If argument UNREAD is non-nil, groups with no unread articles are also
4359 listed."
4360   (interactive (list (if current-prefix-arg
4361                          (prefix-numeric-value current-prefix-arg)
4362                        (or
4363                         (gnus-group-default-level nil t)
4364                         gnus-group-default-list-level
4365                         gnus-level-subscribed))))
4366   (or level
4367       (setq level (car gnus-group-list-mode)
4368             unread (cdr gnus-group-list-mode)))
4369   (setq level (gnus-group-default-level level))
4370   (gnus-group-setup-buffer)             ;May call from out of group buffer
4371   (gnus-update-format-specifications)
4372   (let ((case-fold-search nil)
4373         (props (text-properties-at (gnus-point-at-bol)))
4374         (group (gnus-group-group-name)))
4375     (funcall gnus-group-prepare-function level unread lowest)
4376     (if (zerop (buffer-size))
4377         (gnus-message 5 gnus-no-groups-message)
4378       (goto-char (point-max))
4379       (when (or (not gnus-group-goto-next-group-function)
4380                 (not (funcall gnus-group-goto-next-group-function 
4381                               group props)))
4382         (if (not group)
4383             ;; Go to the first group with unread articles.
4384             (gnus-group-search-forward t)
4385           ;; Find the right group to put point on.  If the current group
4386           ;; has disappeared in the new listing, try to find the next
4387           ;; one.        If no next one can be found, just leave point at the
4388           ;; first newsgroup in the buffer.
4389           (if (not (gnus-goto-char
4390                     (text-property-any
4391                      (point-min) (point-max)
4392                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4393               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4394                 (while (and newsrc
4395                             (not (gnus-goto-char
4396                                   (text-property-any
4397                                    (point-min) (point-max) 'gnus-group
4398                                    (gnus-intern-safe
4399                                     (caar newsrc) gnus-active-hashtb)))))
4400                   (setq newsrc (cdr newsrc)))
4401                 (or newsrc (progn (goto-char (point-max))
4402                                   (forward-line -1)))))))
4403       ;; Adjust cursor point.
4404       (gnus-group-position-point))))
4405
4406 (defun gnus-group-list-level (level &optional all)
4407   "List groups on LEVEL.
4408 If ALL (the prefix), also list groups that have no unread articles."
4409   (interactive "nList groups on level: \nP")
4410   (gnus-group-list-groups level all level))
4411
4412 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4413   "List all newsgroups with unread articles of level LEVEL or lower.
4414 If ALL is non-nil, list groups that have no unread articles.
4415 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4416 If REGEXP, only list groups matching REGEXP."
4417   (set-buffer gnus-group-buffer)
4418   (let ((buffer-read-only nil)
4419         (newsrc (cdr gnus-newsrc-alist))
4420         (lowest (or lowest 1))
4421         info clevel unread group params)
4422     (erase-buffer)
4423     (if (< lowest gnus-level-zombie)
4424         ;; List living groups.
4425         (while newsrc
4426           (setq info (car newsrc)
4427                 group (gnus-info-group info)
4428                 params (gnus-info-params info)
4429                 newsrc (cdr newsrc)
4430                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4431           (and unread                   ; This group might be bogus
4432                (or (not regexp)
4433                    (string-match regexp group))
4434                (<= (setq clevel (gnus-info-level info)) level)
4435                (>= clevel lowest)
4436                (or all                  ; We list all groups?
4437                    (and gnus-group-list-inactive-groups
4438                         (eq unread t))  ; We list unactivated groups
4439                    (> unread 0)         ; We list groups with unread articles
4440                    (and gnus-list-groups-with-ticked-articles
4441                         (cdr (assq 'tick (gnus-info-marks info))))
4442                                         ; And groups with tickeds
4443                    ;; Check for permanent visibility.
4444                    (and gnus-permanently-visible-groups
4445                         (string-match gnus-permanently-visible-groups
4446                                       group))
4447                    (memq 'visible params)
4448                    (cdr (assq 'visible params)))
4449                (gnus-group-insert-group-line
4450                 group (gnus-info-level info)
4451                 (gnus-info-marks info) unread (gnus-info-method info)))))
4452
4453     ;; List dead groups.
4454     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4455          (gnus-group-prepare-flat-list-dead
4456           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4457           gnus-level-zombie ?Z
4458           regexp))
4459     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4460          (gnus-group-prepare-flat-list-dead
4461           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4462           gnus-level-killed ?K regexp))
4463
4464     (gnus-group-set-mode-line)
4465     (setq gnus-group-list-mode (cons level all))
4466     (run-hooks 'gnus-group-prepare-hook)))
4467
4468 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4469   ;; List zombies and killed lists somewhat faster, which was
4470   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4471   ;; this by ignoring the group format specification altogether.
4472   (let (group beg)
4473     (if regexp
4474         ;; This loop is used when listing groups that match some
4475         ;; regexp.
4476         (while groups
4477           (setq group (pop groups))
4478           (when (string-match regexp group)
4479             (add-text-properties
4480              (point) (prog1 (1+ (point))
4481                        (insert " " mark "     *: " group "\n"))
4482              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4483                    'gnus-unread t
4484                    'gnus-level level))))
4485       ;; This loop is used when listing all groups.
4486       (while groups
4487         (add-text-properties
4488          (point) (prog1 (1+ (point))
4489                    (insert " " mark "     *: "
4490                            (setq group (pop groups)) "\n"))
4491          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4492                'gnus-unread t
4493                'gnus-level level))))))
4494
4495 (defmacro gnus-group-real-name (group)
4496   "Find the real name of a foreign newsgroup."
4497   `(let ((gname ,group))
4498      (if (string-match ":[^:]+$" gname)
4499          (substring gname (1+ (match-beginning 0)))
4500        gname)))
4501
4502 (defsubst gnus-server-add-address (method)
4503   (let ((method-name (symbol-name (car method))))
4504     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4505              (not (assq (intern (concat method-name "-address")) method)))
4506         (append method (list (list (intern (concat method-name "-address"))
4507                                    (nth 1 method))))
4508       method)))
4509
4510 (defsubst gnus-server-get-method (group method)
4511   ;; Input either a server name, and extended server name, or a
4512   ;; select method, and return a select method.
4513   (cond ((stringp method)
4514          (gnus-server-to-method method))
4515         ((and (stringp (car method)) group)
4516          (gnus-server-extend-method group method))
4517         (t
4518          (gnus-server-add-address method))))
4519
4520 (defun gnus-server-to-method (server)
4521   "Map virtual server names to select methods."
4522   (or 
4523    ;; Perhaps this is the native server?
4524    (and (equal server "native") gnus-select-method)
4525    ;; It should be in the server alist.
4526    (cdr (assoc server gnus-server-alist))
4527    ;; If not, we look through all the opened server
4528    ;; to see whether we can find it there.
4529    (let ((opened gnus-opened-servers))
4530      (while (and opened
4531                  (not (equal server (format "%s:%s" (caaar opened)
4532                                             (cadaar opened)))))
4533        (pop opened))
4534      (caar opened))))
4535
4536 (defmacro gnus-server-equal (ss1 ss2)
4537   "Say whether two servers are equal."
4538   `(let ((s1 ,ss1)
4539          (s2 ,ss2))
4540      (or (equal s1 s2)
4541          (and (= (length s1) (length s2))
4542               (progn
4543                 (while (and s1 (member (car s1) s2))
4544                   (setq s1 (cdr s1)))
4545                 (null s1))))))
4546
4547 (defun gnus-group-prefixed-name (group method)
4548   "Return the whole name from GROUP and METHOD."
4549   (and (stringp method) (setq method (gnus-server-to-method method)))
4550   (concat (format "%s" (car method))
4551           (if (and
4552                (or (assoc (format "%s" (car method)) 
4553                           (gnus-methods-using 'address))
4554                    (equal method gnus-message-archive-method))
4555                (not (string= (nth 1 method) "")))
4556               (concat "+" (nth 1 method)))
4557           ":" group))
4558
4559 (defun gnus-group-real-prefix (group)
4560   "Return the prefix of the current group name."
4561   (if (string-match "^[^:]+:" group)
4562       (substring group 0 (match-end 0))
4563     ""))
4564
4565 (defun gnus-group-method (group)
4566   "Return the server or method used for selecting GROUP."
4567   (let ((prefix (gnus-group-real-prefix group)))
4568     (if (equal prefix "")
4569         gnus-select-method
4570       (let ((servers gnus-opened-servers)
4571             (server "")
4572             backend possible found)
4573         (if (string-match "^[^\\+]+\\+" prefix)
4574             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4575                   server (substring prefix (match-end 0) (1- (length prefix))))
4576           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4577         (while servers
4578           (when (eq (caaar servers) backend)
4579             (setq possible (caar servers))
4580             (when (equal (cadaar servers) server)
4581               (setq found (caar servers))))
4582           (pop servers))
4583         (or (car (rassoc found gnus-server-alist))
4584             found
4585             (car (rassoc possible gnus-server-alist))
4586             possible
4587             (list backend server))))))
4588
4589 (defsubst gnus-secondary-method-p (method)
4590   "Return whether METHOD is a secondary select method."
4591   (let ((methods gnus-secondary-select-methods)
4592         (gmethod (gnus-server-get-method nil method)))
4593     (while (and methods
4594                 (not (equal (gnus-server-get-method nil (car methods))
4595                             gmethod)))
4596       (setq methods (cdr methods)))
4597     methods))
4598
4599 (defun gnus-group-foreign-p (group)
4600   "Say whether a group is foreign or not."
4601   (and (not (gnus-group-native-p group))
4602        (not (gnus-group-secondary-p group))))
4603
4604 (defun gnus-group-native-p (group)
4605   "Say whether the group is native or not."
4606   (not (string-match ":" group)))
4607
4608 (defun gnus-group-secondary-p (group)
4609   "Say whether the group is secondary or not."
4610   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4611
4612 (defun gnus-group-get-parameter (group &optional symbol)
4613   "Returns the group parameters for GROUP.
4614 If SYMBOL, return the value of that symbol in the group parameters."
4615   (let ((params (gnus-info-params (gnus-get-info group))))
4616     (if symbol
4617         (gnus-group-parameter-value params symbol)
4618       params)))
4619
4620 (defun gnus-group-parameter-value (params symbol)
4621   "Return the value of SYMBOL in group PARAMS."
4622   (or (car (memq symbol params))        ; It's either a simple symbol
4623       (cdr (assq symbol params))))      ; or a cons.
4624
4625 (defun gnus-group-add-parameter (group param)
4626   "Add parameter PARAM to GROUP."
4627   (let ((info (gnus-get-info group)))
4628     (if (not info)
4629         () ; This is a dead group.  We just ignore it.
4630       ;; Cons the new param to the old one and update.
4631       (gnus-group-set-info (cons param (gnus-info-params info))
4632                            group 'params))))
4633
4634 (defun gnus-group-set-parameter (group name value)
4635   "Set parameter NAME to VALUE in GROUP."
4636   (let ((info (gnus-get-info group)))
4637     (if (not info)
4638         () ; This is a dead group.  We just ignore it.
4639       (let ((old-params (gnus-info-params info))
4640             (new-params (list (cons name value))))
4641         (while old-params
4642           (if (or (not (listp (car old-params)))
4643                   (not (eq (caar old-params) name)))
4644               (setq new-params (append new-params (list (car old-params)))))
4645           (setq old-params (cdr old-params)))
4646         (gnus-group-set-info new-params group 'params)))))
4647
4648 (defun gnus-group-add-score (group &optional score)
4649   "Add SCORE to the GROUP score.
4650 If SCORE is nil, add 1 to the score of GROUP."
4651   (let ((info (gnus-get-info group)))
4652     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4653
4654 (defun gnus-summary-bubble-group ()
4655   "Increase the score of the current group.
4656 This is a handy function to add to `gnus-summary-exit-hook' to
4657 increase the score of each group you read."
4658   (gnus-group-add-score gnus-newsgroup-name))
4659
4660 (defun gnus-group-set-info (info &optional method-only-group part)
4661   (let* ((entry (gnus-gethash
4662                  (or method-only-group (gnus-info-group info))
4663                  gnus-newsrc-hashtb))
4664          (part-info info)
4665          (info (if method-only-group (nth 2 entry) info)))
4666     (when method-only-group
4667       (unless entry
4668         (error "Trying to change non-existent group %s" method-only-group))
4669       ;; We have received parts of the actual group info - either the
4670       ;; select method or the group parameters.  We first check
4671       ;; whether we have to extend the info, and if so, do that.
4672       (let ((len (length info))
4673             (total (if (eq part 'method) 5 6)))
4674         (when (< len total)
4675           (setcdr (nthcdr (1- len) info)
4676                   (make-list (- total len) nil)))
4677         ;; Then we enter the new info.
4678         (setcar (nthcdr (1- total) info) part-info)))
4679     (unless entry
4680       ;; This is a new group, so we just create it.
4681       (save-excursion
4682         (set-buffer gnus-group-buffer)
4683         (if (gnus-info-method info)
4684             ;; It's a foreign group...
4685             (gnus-group-make-group
4686              (gnus-group-real-name (gnus-info-group info))
4687              (prin1-to-string (car (gnus-info-method info)))
4688              (nth 1 (gnus-info-method info)))
4689           ;; It's a native group.
4690           (gnus-group-make-group (gnus-info-group info)))
4691         (gnus-message 6 "Note: New group created")
4692         (setq entry
4693               (gnus-gethash (gnus-group-prefixed-name
4694                              (gnus-group-real-name (gnus-info-group info))
4695                              (or (gnus-info-method info) gnus-select-method))
4696                             gnus-newsrc-hashtb))))
4697     ;; Whether it was a new group or not, we now have the entry, so we
4698     ;; can do the update.
4699     (if entry
4700         (progn
4701           (setcar (nthcdr 2 entry) info)
4702           (when (and (not (eq (car entry) t))
4703                      (gnus-active (gnus-info-group info)))
4704             (let ((marked (gnus-info-marks info)))
4705               (setcar entry (length (gnus-list-of-unread-articles
4706                                      (car info)))))))
4707       (error "No such group: %s" (gnus-info-group info)))))
4708
4709 (defun gnus-group-set-method-info (group select-method)
4710   (gnus-group-set-info select-method group 'method))
4711
4712 (defun gnus-group-set-params-info (group params)
4713   (gnus-group-set-info params group 'params))
4714
4715 (defun gnus-group-update-group-line ()
4716   "Update the current line in the group buffer."
4717   (let* ((buffer-read-only nil)
4718          (group (gnus-group-group-name))
4719          (gnus-group-indentation (gnus-group-group-indentation))
4720          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4721     (and entry
4722          (not (gnus-ephemeral-group-p group))
4723          (gnus-dribble-enter
4724           (concat "(gnus-group-set-info '"
4725                   (prin1-to-string (nth 2 entry)) ")")))
4726     (gnus-delete-line)
4727     (gnus-group-insert-group-line-info group)
4728     (forward-line -1)
4729     (gnus-group-position-point)))
4730
4731 (defun gnus-group-insert-group-line-info (group)
4732   "Insert GROUP on the current line."
4733   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4734         active info)
4735     (if entry
4736         (progn
4737           ;; (Un)subscribed group.
4738           (setq info (nth 2 entry))
4739           (gnus-group-insert-group-line
4740            group (gnus-info-level info) (gnus-info-marks info)
4741            (or (car entry) t) (gnus-info-method info)))
4742       ;; This group is dead.
4743       (gnus-group-insert-group-line
4744        group
4745        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4746        nil
4747        (if (setq active (gnus-active group))
4748            (- (1+ (cdr active)) (car active)) 0)
4749        nil))))
4750
4751 (defun gnus-group-insert-group-line
4752   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4753                   gnus-tmp-method)
4754   "Insert a group line in the group buffer."
4755   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4756          (gnus-tmp-number-total
4757           (if gnus-tmp-active
4758               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4759             0))
4760          (gnus-tmp-number-of-unread
4761           (if (numberp number) (int-to-string (max 0 number))
4762             "*"))
4763          (gnus-tmp-number-of-read
4764           (if (numberp number)
4765               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4766             "*"))
4767          (gnus-tmp-subscribed
4768           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4769                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4770                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4771                 (t ?K)))
4772          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4773          (gnus-tmp-newsgroup-description
4774           (if gnus-description-hashtb
4775               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4776             ""))
4777          (gnus-tmp-moderated
4778           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4779          (gnus-tmp-moderated-string
4780           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4781          (gnus-tmp-method
4782           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4783          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4784          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4785          (gnus-tmp-news-method-string
4786           (if gnus-tmp-method
4787               (format "(%s:%s)" (car gnus-tmp-method)
4788                       (cadr gnus-tmp-method)) ""))
4789          (gnus-tmp-marked-mark
4790           (if (and (numberp number)
4791                    (zerop number)
4792                    (cdr (assq 'tick gnus-tmp-marked)))
4793               ?* ? ))
4794          (gnus-tmp-process-marked
4795           (if (member gnus-tmp-group gnus-group-marked)
4796               gnus-process-mark ? ))
4797          (gnus-tmp-grouplens
4798           (or (and gnus-use-grouplens
4799                    (bbb-grouplens-group-p gnus-tmp-group))
4800               ""))
4801          (buffer-read-only nil)
4802          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4803     (beginning-of-line)
4804     (add-text-properties
4805      (point)
4806      (prog1 (1+ (point))
4807        ;; Insert the text.
4808        (eval gnus-group-line-format-spec))
4809      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4810        gnus-unread ,(if (numberp number)
4811                         (string-to-int gnus-tmp-number-of-unread)
4812                       t)
4813        gnus-marked ,gnus-tmp-marked-mark
4814        gnus-indentation ,gnus-group-indentation
4815        gnus-level ,gnus-tmp-level))
4816     (when (gnus-visual-p 'group-highlight 'highlight)
4817       (forward-line -1)
4818       (run-hooks 'gnus-group-update-hook)
4819       (forward-line))
4820     ;; Allow XEmacs to remove front-sticky text properties.
4821     (gnus-group-remove-excess-properties)))
4822
4823 (defun gnus-group-update-group (group &optional visible-only)
4824   "Update all lines where GROUP appear.
4825 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4826 already."
4827   (save-excursion
4828     (set-buffer gnus-group-buffer)
4829     ;; The buffer may be narrowed.
4830     (save-restriction
4831       (widen)
4832       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4833             (loc (point-min))
4834             found buffer-read-only visible)
4835         ;; Enter the current status into the dribble buffer.
4836         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4837           (if (and entry (not (gnus-ephemeral-group-p group)))
4838               (gnus-dribble-enter
4839                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4840                        ")"))))
4841         ;; Find all group instances.  If topics are in use, each group
4842         ;; may be listed in more than once.
4843         (while (setq loc (text-property-any
4844                           loc (point-max) 'gnus-group ident))
4845           (setq found t)
4846           (goto-char loc)
4847           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4848             (gnus-delete-line)
4849             (gnus-group-insert-group-line-info group))
4850           (setq loc (1+ loc)))
4851         (unless (or found visible-only)
4852           ;; No such line in the buffer, find out where it's supposed to
4853           ;; go, and insert it there (or at the end of the buffer).
4854           (if gnus-goto-missing-group-function
4855               (funcall gnus-goto-missing-group-function group)
4856             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4857               (while (and entry (car entry)
4858                           (not
4859                            (gnus-goto-char
4860                             (text-property-any
4861                              (point-min) (point-max)
4862                              'gnus-group (gnus-intern-safe
4863                                           (caar entry) gnus-active-hashtb)))))
4864                 (setq entry (cdr entry)))
4865               (or entry (goto-char (point-max)))))
4866           ;; Finally insert the line.
4867           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4868             (gnus-group-insert-group-line-info group)))
4869         (gnus-group-set-mode-line)))))
4870
4871 (defun gnus-group-set-mode-line ()
4872   (when (memq 'group gnus-updated-mode-lines)
4873     (let* ((gformat (or gnus-group-mode-line-format-spec
4874                         (setq gnus-group-mode-line-format-spec
4875                               (gnus-parse-format
4876                                gnus-group-mode-line-format
4877                                gnus-group-mode-line-format-alist))))
4878            (gnus-tmp-news-server (cadr gnus-select-method))
4879            (gnus-tmp-news-method (car gnus-select-method))
4880            (max-len 60)
4881            gnus-tmp-header                      ;Dummy binding for user-defined formats
4882            ;; Get the resulting string.
4883            (mode-string (eval gformat)))
4884       ;; If the line is too long, we chop it off.
4885       (when (> (length mode-string) max-len)
4886         (setq mode-string (substring mode-string 0 (- max-len 4))))
4887       (prog1
4888           (setq mode-line-buffer-identification (list mode-string))
4889         (set-buffer-modified-p t)))))
4890
4891 (defun gnus-group-group-name ()
4892   "Get the name of the newsgroup on the current line."
4893   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4894     (and group (symbol-name group))))
4895
4896 (defun gnus-group-group-level ()
4897   "Get the level of the newsgroup on the current line."
4898   (get-text-property (gnus-point-at-bol) 'gnus-level))
4899
4900 (defun gnus-group-group-indentation ()
4901   "Get the indentation of the newsgroup on the current line."
4902   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4903
4904 (defun gnus-group-group-unread ()
4905   "Get the number of unread articles of the newsgroup on the current line."
4906   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4907
4908 (defun gnus-group-search-forward (&optional backward all level first-too)
4909   "Find the next newsgroup with unread articles.
4910 If BACKWARD is non-nil, find the previous newsgroup instead.
4911 If ALL is non-nil, just find any newsgroup.
4912 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4913 group exists.
4914 If FIRST-TOO, the current line is also eligible as a target."
4915   (let ((way (if backward -1 1))
4916         (low gnus-level-killed)
4917         (beg (point))
4918         pos found lev)
4919     (if (and backward (progn (beginning-of-line)) (bobp))
4920         nil
4921       (or first-too (forward-line way))
4922       (while (and
4923               (not (eobp))
4924               (not (setq
4925                     found
4926                     (and (or all
4927                              (and
4928                               (let ((unread
4929                                      (get-text-property (point) 'gnus-unread)))
4930                                 (and (numberp unread) (> unread 0)))
4931                               (setq lev (get-text-property (point)
4932                                                            'gnus-level))
4933                               (<= lev gnus-level-subscribed)))
4934                          (or (not level)
4935                              (and (setq lev (get-text-property (point)
4936                                                                'gnus-level))
4937                                   (or (= lev level)
4938                                       (and (< lev low)
4939                                            (< level lev)
4940                                            (progn
4941                                              (setq low lev)
4942                                              (setq pos (point))
4943                                              nil))))))))
4944               (zerop (forward-line way)))))
4945     (if found
4946         (progn (gnus-group-position-point) t)
4947       (goto-char (or pos beg))
4948       (and pos t))))
4949
4950 ;;; Gnus group mode commands
4951
4952 ;; Group marking.
4953
4954 (defun gnus-group-mark-group (n &optional unmark no-advance)
4955   "Mark the current group."
4956   (interactive "p")
4957   (let ((buffer-read-only nil)
4958         group)
4959     (while
4960         (and (> n 0)
4961              (setq group (gnus-group-group-name))
4962              (progn
4963                (beginning-of-line)
4964                (forward-char
4965                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4966                (delete-char 1)
4967                (if unmark
4968                    (progn
4969                      (insert " ")
4970                      (setq gnus-group-marked (delete group gnus-group-marked)))
4971                  (insert "#")
4972                  (setq gnus-group-marked
4973                        (cons group (delete group gnus-group-marked))))
4974                t)
4975              (or no-advance (zerop (gnus-group-next-group 1))))
4976       (setq n (1- n)))
4977     (gnus-summary-position-point)
4978     n))
4979
4980 (defun gnus-group-unmark-group (n)
4981   "Remove the mark from the current group."
4982   (interactive "p")
4983   (gnus-group-mark-group n 'unmark)
4984   (gnus-group-position-point))
4985
4986 (defun gnus-group-unmark-all-groups ()
4987   "Unmark all groups."
4988   (let ((groups gnus-group-marked))
4989     (save-excursion
4990       (while groups
4991         (gnus-group-remove-mark (pop groups)))))
4992   (gnus-group-position-point))
4993
4994 (defun gnus-group-mark-region (unmark beg end)
4995   "Mark all groups between point and mark.
4996 If UNMARK, remove the mark instead."
4997   (interactive "P\nr")
4998   (let ((num (count-lines beg end)))
4999     (save-excursion
5000       (goto-char beg)
5001       (- num (gnus-group-mark-group num unmark)))))
5002
5003 (defun gnus-group-mark-buffer (&optional unmark)
5004   "Mark all groups in the buffer.
5005 If UNMARK, remove the mark instead."
5006   (interactive "P")
5007   (gnus-group-mark-region unmark (point-min) (point-max)))
5008
5009 (defun gnus-group-mark-regexp (regexp)
5010   "Mark all groups that match some regexp."
5011   (interactive "sMark (regexp): ")
5012   (let ((alist (cdr gnus-newsrc-alist))
5013         group)
5014     (while alist
5015       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5016         (gnus-group-set-mark group))))
5017   (gnus-group-position-point))
5018
5019 (defun gnus-group-remove-mark (group)
5020   "Remove the process mark from GROUP and move point there.
5021 Return nil if the group isn't displayed."
5022   (if (gnus-group-goto-group group)
5023       (save-excursion
5024         (gnus-group-mark-group 1 'unmark t)
5025         t)
5026     (setq gnus-group-marked
5027           (delete group gnus-group-marked))
5028     nil))
5029
5030 (defun gnus-group-set-mark (group)
5031   "Set the process mark on GROUP."
5032   (if (gnus-group-goto-group group)
5033       (save-excursion
5034         (gnus-group-mark-group 1 nil t))
5035     (setq gnus-group-marked
5036           (cons group (delete group gnus-group-marked)))))
5037
5038 (defun gnus-group-universal-argument (arg &optional groups func)
5039   "Perform any command on all groups accoring to the process/prefix convention."
5040   (interactive "P")
5041   (let ((groups (or groups (gnus-group-process-prefix arg)))
5042         group func)
5043     (if (eq (setq func (or func
5044                            (key-binding
5045                             (read-key-sequence
5046                              (substitute-command-keys
5047                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5048             'undefined)
5049         (progn
5050           (message "Undefined key")
5051           (ding))
5052       (while groups
5053         (gnus-group-remove-mark (setq group (pop groups)))
5054         (command-execute func))))
5055   (gnus-group-position-point))
5056
5057 (defun gnus-group-process-prefix (n)
5058   "Return a list of groups to work on.
5059 Take into consideration N (the prefix) and the list of marked groups."
5060   (cond
5061    (n
5062     (setq n (prefix-numeric-value n))
5063     ;; There is a prefix, so we return a list of the N next
5064     ;; groups.
5065     (let ((way (if (< n 0) -1 1))
5066           (n (abs n))
5067           group groups)
5068       (save-excursion
5069         (while (and (> n 0)
5070                     (setq group (gnus-group-group-name)))
5071           (setq groups (cons group groups))
5072           (setq n (1- n))
5073           (gnus-group-next-group way)))
5074       (nreverse groups)))
5075    ((and (boundp 'transient-mark-mode)
5076          transient-mark-mode
5077          mark-active)
5078     ;; Work on the region between point and mark.
5079     (let ((max (max (point) (mark)))
5080           groups)
5081       (save-excursion
5082         (goto-char (min (point) (mark)))
5083         (while
5084             (and
5085              (push (gnus-group-group-name) groups)
5086              (zerop (gnus-group-next-group 1))
5087              (< (point) max)))
5088         (nreverse groups))))
5089    (gnus-group-marked
5090     ;; No prefix, but a list of marked articles.
5091     (reverse gnus-group-marked))
5092    (t
5093     ;; Neither marked articles or a prefix, so we return the
5094     ;; current group.
5095     (let ((group (gnus-group-group-name)))
5096       (and group (list group))))))
5097
5098 ;; Selecting groups.
5099
5100 (defun gnus-group-read-group (&optional all no-article group)
5101   "Read news in this newsgroup.
5102 If the prefix argument ALL is non-nil, already read articles become
5103 readable.  IF ALL is a number, fetch this number of articles.  If the
5104 optional argument NO-ARTICLE is non-nil, no article will be
5105 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5106 group."
5107   (interactive "P")
5108   (let ((group (or group (gnus-group-group-name)))
5109         number active marked entry)
5110     (or group (error "No group on current line"))
5111     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5112                                             group gnus-newsrc-hashtb)))))
5113     ;; This group might be a dead group.  In that case we have to get
5114     ;; the number of unread articles from `gnus-active-hashtb'.
5115     (setq number
5116           (cond ((numberp all) all)
5117                 (entry (car entry))
5118                 ((setq active (gnus-active group))
5119                  (- (1+ (cdr active)) (car active)))))
5120     (gnus-summary-read-group
5121      group (or all (and (numberp number)
5122                         (zerop (+ number (length (cdr (assq 'tick marked)))
5123                                   (length (cdr (assq 'dormant marked)))))))
5124      no-article)))
5125
5126 (defun gnus-group-select-group (&optional all)
5127   "Select this newsgroup.
5128 No article is selected automatically.
5129 If ALL is non-nil, already read articles become readable.
5130 If ALL is a number, fetch this number of articles."
5131   (interactive "P")
5132   (gnus-group-read-group all t))
5133
5134 (defun gnus-group-quick-select-group (&optional all)
5135   "Select the current group \"quickly\".
5136 This means that no highlighting or scoring will be performed."
5137   (interactive "P")
5138   (let (gnus-visual
5139         gnus-score-find-score-files-function
5140         gnus-apply-kill-hook
5141         gnus-summary-expunge-below)
5142     (gnus-group-read-group all t)))
5143
5144 (defun gnus-group-visible-select-group (&optional all)
5145   "Select the current group without hiding any articles."
5146   (interactive "P")
5147   (let ((gnus-inhibit-limiting t))
5148     (gnus-group-read-group all t)))
5149
5150 ;;;###autoload
5151 (defun gnus-fetch-group (group)
5152   "Start Gnus if necessary and enter GROUP.
5153 Returns whether the fetching was successful or not."
5154   (interactive "sGroup name: ")
5155   (or (get-buffer gnus-group-buffer)
5156       (gnus))
5157   (gnus-group-read-group nil nil group))
5158
5159 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5160 ;; if selection was successful.
5161 (defun gnus-group-read-ephemeral-group
5162   (group method &optional activate quit-config)
5163   (let ((group (if (gnus-group-foreign-p group) group
5164                  (gnus-group-prefixed-name group method))))
5165     (gnus-sethash
5166      group
5167      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5168                      ((quit-config . ,(if quit-config quit-config
5169                                         (cons (current-buffer) 'summary))))))
5170      gnus-newsrc-hashtb)
5171     (set-buffer gnus-group-buffer)
5172     (or (gnus-check-server method)
5173         (error "Unable to contact server: %s" (gnus-status-message method)))
5174     (if activate (or (gnus-request-group group)
5175                      (error "Couldn't request group")))
5176     (condition-case ()
5177         (gnus-group-read-group t t group)
5178       (error nil)
5179       (quit nil))))
5180
5181 (defun gnus-group-jump-to-group (group)
5182   "Jump to newsgroup GROUP."
5183   (interactive
5184    (list (completing-read
5185           "Group: " gnus-active-hashtb nil
5186           (memq gnus-select-method gnus-have-read-active-file))))
5187
5188   (if (equal group "")
5189       (error "Empty group name"))
5190
5191   (let ((b (text-property-any
5192             (point-min) (point-max)
5193             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5194     (unless (gnus-ephemeral-group-p group)
5195       (if b
5196           ;; Either go to the line in the group buffer...
5197           (goto-char b)
5198         ;; ... or insert the line.
5199         (or
5200          (gnus-active group)
5201          (gnus-activate-group group)
5202          (error "%s error: %s" group (gnus-status-message group)))
5203
5204         (gnus-group-update-group group)
5205         (goto-char (text-property-any
5206                     (point-min) (point-max)
5207                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5208     ;; Adjust cursor point.
5209     (gnus-group-position-point)))
5210
5211 (defun gnus-group-goto-group (group)
5212   "Goto to newsgroup GROUP."
5213   (when group
5214     (let ((b (text-property-any (point-min) (point-max)
5215                                 'gnus-group (gnus-intern-safe
5216                                              group gnus-active-hashtb))))
5217       (and b (goto-char b)))))
5218
5219 (defun gnus-group-next-group (n)
5220   "Go to next N'th newsgroup.
5221 If N is negative, search backward instead.
5222 Returns the difference between N and the number of skips actually
5223 done."
5224   (interactive "p")
5225   (gnus-group-next-unread-group n t))
5226
5227 (defun gnus-group-next-unread-group (n &optional all level)
5228   "Go to next N'th unread newsgroup.
5229 If N is negative, search backward instead.
5230 If ALL is non-nil, choose any newsgroup, unread or not.
5231 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5232 such group can be found, the next group with a level higher than
5233 LEVEL.
5234 Returns the difference between N and the number of skips actually
5235 made."
5236   (interactive "p")
5237   (let ((backward (< n 0))
5238         (n (abs n)))
5239     (while (and (> n 0)
5240                 (gnus-group-search-forward
5241                  backward (or (not gnus-group-goto-unread) all) level))
5242       (setq n (1- n)))
5243     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5244                                (if level " on this level or higher" "")))
5245     n))
5246
5247 (defun gnus-group-prev-group (n)
5248   "Go to previous N'th newsgroup.
5249 Returns the difference between N and the number of skips actually
5250 done."
5251   (interactive "p")
5252   (gnus-group-next-unread-group (- n) t))
5253
5254 (defun gnus-group-prev-unread-group (n)
5255   "Go to previous N'th unread newsgroup.
5256 Returns the difference between N and the number of skips actually
5257 done."
5258   (interactive "p")
5259   (gnus-group-next-unread-group (- n)))
5260
5261 (defun gnus-group-next-unread-group-same-level (n)
5262   "Go to next N'th unread newsgroup on the same level.
5263 If N is negative, search backward instead.
5264 Returns the difference between N and the number of skips actually
5265 done."
5266   (interactive "p")
5267   (gnus-group-next-unread-group n t (gnus-group-group-level))
5268   (gnus-group-position-point))
5269
5270 (defun gnus-group-prev-unread-group-same-level (n)
5271   "Go to next N'th unread newsgroup on the same level.
5272 Returns the difference between N and the number of skips actually
5273 done."
5274   (interactive "p")
5275   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5276   (gnus-group-position-point))
5277
5278 (defun gnus-group-best-unread-group (&optional exclude-group)
5279   "Go to the group with the highest level.
5280 If EXCLUDE-GROUP, do not go to that group."
5281   (interactive)
5282   (goto-char (point-min))
5283   (let ((best 100000)
5284         unread best-point)
5285     (while (setq unread (get-text-property (point) 'gnus-unread))
5286       (if (and (numberp unread) (> unread 0))
5287           (progn
5288             (if (and (< (get-text-property (point) 'gnus-level) best)
5289                      (or (not exclude-group)
5290                          (not (equal exclude-group (gnus-group-group-name)))))
5291                 (progn
5292                   (setq best (get-text-property (point) 'gnus-level))
5293                   (setq best-point (point))))))
5294       (forward-line 1))
5295     (if best-point (goto-char best-point))
5296     (gnus-summary-position-point)
5297     (and best-point (gnus-group-group-name))))
5298
5299 (defun gnus-group-first-unread-group ()
5300   "Go to the first group with unread articles."
5301   (interactive)
5302   (prog1
5303       (let ((opoint (point))
5304             unread)
5305         (goto-char (point-min))
5306         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5307                 (and (numberp unread)   ; Not a topic.
5308                      (not (zerop unread))) ; Has unread articles.
5309                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5310             (point)                     ; Success.
5311           (goto-char opoint)
5312           nil))                         ; Not success.
5313     (gnus-group-position-point)))
5314
5315 (defun gnus-group-enter-server-mode ()
5316   "Jump to the server buffer."
5317   (interactive)
5318   (gnus-enter-server-buffer))
5319
5320 (defun gnus-group-make-group (name &optional method address)
5321   "Add a new newsgroup.
5322 The user will be prompted for a NAME, for a select METHOD, and an
5323 ADDRESS."
5324   (interactive
5325    (cons
5326     (read-string "Group name: ")
5327     (let ((method
5328            (completing-read
5329             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5330             nil t)))
5331       (if (assoc method gnus-valid-select-methods)
5332           (list method
5333                 (if (memq 'prompt-address
5334                           (assoc method gnus-valid-select-methods))
5335                     (read-string "Address: ")
5336                   ""))
5337         (list method "")))))
5338
5339   (save-excursion
5340     (set-buffer gnus-group-buffer)
5341     (let* ((meth (and method (if address (list (intern method) address)
5342                                method)))
5343            (nname (if method (gnus-group-prefixed-name name meth) name))
5344            info)
5345       (and (gnus-gethash nname gnus-newsrc-hashtb)
5346            (error "Group %s already exists" nname))
5347       (gnus-group-change-level
5348        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5349        gnus-level-default-subscribed gnus-level-killed
5350        (and (gnus-group-group-name)
5351             (gnus-gethash (gnus-group-group-name)
5352                           gnus-newsrc-hashtb))
5353        t)
5354       (gnus-set-active nname (cons 1 0))
5355       (or (gnus-ephemeral-group-p name)
5356           (gnus-dribble-enter
5357            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5358       (gnus-group-insert-group-line-info nname)
5359
5360       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5361         (require (car meth)))
5362       (gnus-check-server meth)
5363       (and (gnus-check-backend-function 'request-create-group nname)
5364            (gnus-request-create-group nname))
5365       t)))
5366
5367 (defun gnus-group-delete-group (group &optional force)
5368   "Delete the current group.
5369 If FORCE (the prefix) is non-nil, all the articles in the group will
5370 be deleted.  This is \"deleted\" as in \"removed forever from the face
5371 of the Earth\".  There is no undo."
5372   (interactive
5373    (list (gnus-group-group-name)
5374          current-prefix-arg))
5375   (or group (error "No group to rename"))
5376   (or (gnus-check-backend-function 'request-delete-group group)
5377       (error "This backend does not support group deletion"))
5378   (prog1
5379       (if (not (gnus-yes-or-no-p
5380                 (format
5381                  "Do you really want to delete %s%s? "
5382                  group (if force " and all its contents" ""))))
5383           () ; Whew!
5384         (gnus-message 6 "Deleting group %s..." group)
5385         (if (not (gnus-request-delete-group group force))
5386             (progn
5387               (gnus-message 3 "Couldn't delete group %s" group)
5388               (ding))
5389           (gnus-message 6 "Deleting group %s...done" group)
5390           (gnus-group-goto-group group)
5391           (gnus-group-kill-group 1 t)
5392           t))
5393     (gnus-group-position-point)))
5394
5395 (defun gnus-group-rename-group (group new-name)
5396   (interactive
5397    (list
5398     (gnus-group-group-name)
5399     (progn
5400       (or (gnus-check-backend-function
5401            'request-rename-group (gnus-group-group-name))
5402           (error "This backend does not support renaming groups"))
5403       (read-string "New group name: "))))
5404
5405   (or (gnus-check-backend-function 'request-rename-group group)
5406       (error "This backend does not support renaming groups"))
5407
5408   (or group (error "No group to rename"))
5409   (and (string-match "^[ \t]*$" new-name)
5410        (error "Not a valid group name"))
5411
5412   ;; We find the proper prefixed name.
5413   (setq new-name
5414         (gnus-group-prefixed-name
5415          (gnus-group-real-name new-name)
5416          (gnus-info-method (gnus-get-info group))))
5417
5418   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5419   (prog1
5420       (if (not (gnus-request-rename-group group new-name))
5421           (progn
5422             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5423             (ding))
5424         ;; We rename the group internally by killing it...
5425         (gnus-group-goto-group group)
5426         (gnus-group-kill-group)
5427         ;; ... changing its name ...
5428         (setcar (cdar gnus-list-of-killed-groups) new-name)
5429         ;; ... and then yanking it.  Magic!
5430         (gnus-group-yank-group)
5431         (gnus-set-active new-name (gnus-active group))
5432         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5433         new-name)
5434     (gnus-group-position-point)))
5435
5436 (defun gnus-group-edit-group (group &optional part)
5437   "Edit the group on the current line."
5438   (interactive (list (gnus-group-group-name)))
5439   (let* ((part (or part 'info))
5440          (done-func `(lambda ()
5441                        "Exit editing mode and update the information."
5442                        (interactive)
5443                        (gnus-group-edit-group-done ',part ,group)))
5444          (winconf (current-window-configuration))
5445          info)
5446     (or group (error "No group on current line"))
5447     (or (setq info (gnus-get-info group))
5448         (error "Killed group; can't be edited"))
5449     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5450     (gnus-configure-windows 'edit-group)
5451     (gnus-add-current-to-buffer-list)
5452     (emacs-lisp-mode)
5453     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5454     (use-local-map (copy-keymap emacs-lisp-mode-map))
5455     (local-set-key "\C-c\C-c" done-func)
5456     (make-local-variable 'gnus-prev-winconf)
5457     (setq gnus-prev-winconf winconf)
5458     (erase-buffer)
5459     (insert
5460      (cond
5461       ((eq part 'method)
5462        ";; Type `C-c C-c' after editing the select method.\n\n")
5463       ((eq part 'params)
5464        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5465       ((eq part 'info)
5466        ";; Type `C-c C-c' after editing the group info.\n\n")))
5467     (insert
5468      (pp-to-string
5469       (cond ((eq part 'method)
5470              (or (gnus-info-method info) "native"))
5471             ((eq part 'params)
5472              (gnus-info-params info))
5473             (t info)))
5474      "\n")))
5475
5476 (defun gnus-group-edit-group-method (group)
5477   "Edit the select method of GROUP."
5478   (interactive (list (gnus-group-group-name)))
5479   (gnus-group-edit-group group 'method))
5480
5481 (defun gnus-group-edit-group-parameters (group)
5482   "Edit the group parameters of GROUP."
5483   (interactive (list (gnus-group-group-name)))
5484   (gnus-group-edit-group group 'params))
5485
5486 (defun gnus-group-edit-group-done (part group)
5487   "Get info from buffer, update variables and jump to the group buffer."
5488   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5489   (goto-char (point-min))
5490   (let* ((form (read (current-buffer)))
5491          (winconf gnus-prev-winconf)
5492          (method (cond ((eq part 'info) (nth 4 form))
5493                        ((eq part 'method) form)
5494                        (t nil)))
5495          (info (cond ((eq part 'info) form)
5496                      ((eq part 'method) (gnus-get-info group))
5497                      (t nil)))
5498          (new-group (if info
5499                       (if (or (not method)
5500                               (gnus-server-equal
5501                                gnus-select-method method))
5502                           (gnus-group-real-name (car info))
5503                         (gnus-group-prefixed-name
5504                          (gnus-group-real-name (car info)) method))
5505                       nil)))
5506     (when (and new-group
5507                (not (equal new-group group)))
5508       (when (gnus-group-goto-group group)
5509         (gnus-group-kill-group 1))
5510       (gnus-activate-group new-group))
5511     ;; Set the info.
5512     (if (and info new-group)
5513         (progn
5514           (setq info (gnus-copy-sequence info))
5515           (setcar info new-group)
5516           (setcar (cddddr info) method)
5517           (gnus-group-set-info info))
5518       (gnus-group-set-info form (or new-group group) part))
5519     (kill-buffer (current-buffer))
5520     (and winconf (set-window-configuration winconf))
5521     (set-buffer gnus-group-buffer)
5522     (gnus-group-update-group (or new-group group))
5523     (gnus-group-position-point)))
5524
5525 (defun gnus-group-make-help-group ()
5526   "Create the Gnus documentation group."
5527   (interactive)
5528   (let ((path load-path)
5529         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5530         file dir)
5531     (and (gnus-gethash name gnus-newsrc-hashtb)
5532          (error "Documentation group already exists"))
5533     (while path
5534       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5535             file nil)
5536       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5537                 (file-exists-p
5538                  (setq file (concat (file-name-directory
5539                                      (directory-file-name dir))
5540                                     "etc/gnus-tut.txt"))))
5541         (setq path nil)))
5542     (if (not file)
5543         (message "Couldn't find doc group")
5544       (gnus-group-make-group
5545        (gnus-group-real-name name)
5546        (list 'nndoc name
5547              (list 'nndoc-address file)
5548              (list 'nndoc-article-type 'mbox)))))
5549   (gnus-group-position-point))
5550
5551 (defun gnus-group-make-doc-group (file type)
5552   "Create a group that uses a single file as the source."
5553   (interactive
5554    (list (read-file-name "File name: ")
5555          (and current-prefix-arg 'ask)))
5556   (when (eq type 'ask)
5557     (let ((err "")
5558           char found)
5559       (while (not found)
5560         (message
5561          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5562          err)
5563         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5564                           ((= char ?b) 'babyl)
5565                           ((= char ?d) 'digest)
5566                           ((= char ?f) 'forward)
5567                           ((= char ?a) 'mmfd)
5568                           (t (setq err (format "%c unknown. " char))
5569                              nil))))
5570       (setq type found)))
5571   (let* ((file (expand-file-name file))
5572          (name (gnus-generate-new-group-name
5573                 (gnus-group-prefixed-name
5574                  (file-name-nondirectory file) '(nndoc "")))))
5575     (gnus-group-make-group
5576      (gnus-group-real-name name)
5577      (list 'nndoc name
5578            (list 'nndoc-address file)
5579            (list 'nndoc-article-type (or type 'guess))))
5580     (forward-line -1)
5581     (gnus-group-position-point)))
5582
5583 (defun gnus-group-make-archive-group (&optional all)
5584   "Create the (ding) Gnus archive group of the most recent articles.
5585 Given a prefix, create a full group."
5586   (interactive "P")
5587   (let ((group (gnus-group-prefixed-name
5588                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5589     (and (gnus-gethash group gnus-newsrc-hashtb)
5590          (error "Archive group already exists"))
5591     (gnus-group-make-group
5592      (gnus-group-real-name group)
5593      (list 'nndir (if all "hpc" "edu")
5594            (list 'nndir-directory
5595                  (if all gnus-group-archive-directory
5596                    gnus-group-recent-archive-directory)))))
5597   (forward-line -1)
5598   (gnus-group-position-point))
5599
5600 (defun gnus-group-make-directory-group (dir)
5601   "Create an nndir group.
5602 The user will be prompted for a directory.  The contents of this
5603 directory will be used as a newsgroup.  The directory should contain
5604 mail messages or news articles in files that have numeric names."
5605   (interactive
5606    (list (read-file-name "Create group from directory: ")))
5607   (or (file-exists-p dir) (error "No such directory"))
5608   (or (file-directory-p dir) (error "Not a directory"))
5609   (let ((ext "")
5610         (i 0)
5611         group)
5612     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5613       (setq group
5614             (gnus-group-prefixed-name
5615              (concat (file-name-as-directory (directory-file-name dir))
5616                      ext)
5617              '(nndir "")))
5618       (setq ext (format "<%d>" (setq i (1+ i)))))
5619     (gnus-group-make-group
5620      (gnus-group-real-name group)
5621      (list 'nndir group (list 'nndir-directory dir))))
5622   (forward-line -1)
5623   (gnus-group-position-point))
5624
5625 (defun gnus-group-make-kiboze-group (group address scores)
5626   "Create an nnkiboze group.
5627 The user will be prompted for a name, a regexp to match groups, and
5628 score file entries for articles to include in the group."
5629   (interactive
5630    (list
5631     (read-string "nnkiboze group name: ")
5632     (read-string "Source groups (regexp): ")
5633     (let ((headers (mapcar (lambda (group) (list group))
5634                            '("subject" "from" "number" "date" "message-id"
5635                              "references" "chars" "lines" "xref"
5636                              "followup" "all" "body" "head")))
5637           scores header regexp regexps)
5638       (while (not (equal "" (setq header (completing-read
5639                                           "Match on header: " headers nil t))))
5640         (setq regexps nil)
5641         (while (not (equal "" (setq regexp (read-string
5642                                             (format "Match on %s (string): "
5643                                                     header)))))
5644           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5645         (setq scores (cons (cons header regexps) scores)))
5646       scores)))
5647   (gnus-group-make-group group "nnkiboze" address)
5648   (save-excursion
5649     (gnus-set-work-buffer)
5650     (let (emacs-lisp-mode-hook)
5651       (pp scores (current-buffer)))
5652     (write-region (point-min) (point-max)
5653                   (gnus-score-file-name (concat "nnkiboze:" group))))
5654   (forward-line -1)
5655   (gnus-group-position-point))
5656
5657 (defun gnus-group-add-to-virtual (n vgroup)
5658   "Add the current group to a virtual group."
5659   (interactive
5660    (list current-prefix-arg
5661          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5662                           "nnvirtual:")))
5663   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5664       (error "%s is not an nnvirtual group" vgroup))
5665   (let* ((groups (gnus-group-process-prefix n))
5666          (method (gnus-info-method (gnus-get-info vgroup))))
5667     (setcar (cdr method)
5668             (concat
5669              (nth 1 method) "\\|"
5670              (mapconcat
5671               (lambda (s)
5672                 (gnus-group-remove-mark s)
5673                 (concat "\\(^" (regexp-quote s) "$\\)"))
5674               groups "\\|"))))
5675   (gnus-group-position-point))
5676
5677 (defun gnus-group-make-empty-virtual (group)
5678   "Create a new, fresh, empty virtual group."
5679   (interactive "sCreate new, empty virtual group: ")
5680   (let* ((method (list 'nnvirtual "^$"))
5681          (pgroup (gnus-group-prefixed-name group method)))
5682     ;; Check whether it exists already.
5683     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5684          (error "Group %s already exists." pgroup))
5685     ;; Subscribe the new group after the group on the current line.
5686     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5687     (gnus-group-update-group pgroup)
5688     (forward-line -1)
5689     (gnus-group-position-point)))
5690
5691 (defun gnus-group-enter-directory (dir)
5692   "Enter an ephemeral nneething group."
5693   (interactive "DDirectory to read: ")
5694   (let* ((method (list 'nneething dir))
5695          (leaf (gnus-group-prefixed-name
5696                 (file-name-nondirectory (directory-file-name dir))
5697                 method))
5698          (name (gnus-generate-new-group-name leaf)))
5699     (let ((nneething-read-only t))
5700       (or (gnus-group-read-ephemeral-group
5701            name method t
5702            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5703                                       'summary 'group)))
5704           (error "Couldn't enter %s" dir)))))
5705
5706 ;; Group sorting commands
5707 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5708
5709 (defun gnus-group-sort-groups (func &optional reverse)
5710   "Sort the group buffer according to FUNC.
5711 If REVERSE, reverse the sorting order."
5712   (interactive (list gnus-group-sort-function
5713                      current-prefix-arg))
5714   (let ((func (cond 
5715                ((not (listp func)) func)
5716                ((null func) func)
5717                ((= 1 (length func)) (car func))
5718                (t `(lambda (t1 t2)
5719                      ,(gnus-make-sort-function 
5720                        (reverse func)))))))
5721     ;; We peel off the dummy group from the alist.
5722     (when func
5723       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5724         (pop gnus-newsrc-alist))
5725       ;; Do the sorting.
5726       (setq gnus-newsrc-alist
5727             (sort gnus-newsrc-alist func))
5728       (when reverse
5729         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5730       ;; Regenerate the hash table.
5731       (gnus-make-hashtable-from-newsrc-alist)
5732       (gnus-group-list-groups))))
5733
5734 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5735   "Sort the group buffer alphabetically by group name.
5736 If REVERSE, sort in reverse order."
5737   (interactive "P")
5738   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5739
5740 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5741   "Sort the group buffer by number of unread articles.
5742 If REVERSE, sort in reverse order."
5743   (interactive "P")
5744   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5745
5746 (defun gnus-group-sort-groups-by-level (&optional reverse)
5747   "Sort the group buffer by group level.
5748 If REVERSE, sort in reverse order."
5749   (interactive "P")
5750   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5751
5752 (defun gnus-group-sort-groups-by-score (&optional reverse)
5753   "Sort the group buffer by group score.
5754 If REVERSE, sort in reverse order."
5755   (interactive "P")
5756   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5757
5758 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5759   "Sort the group buffer by group rank.
5760 If REVERSE, sort in reverse order."
5761   (interactive "P")
5762   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5763
5764 (defun gnus-group-sort-groups-by-method (&optional reverse)
5765   "Sort the group buffer alphabetically by backend name.
5766 If REVERSE, sort in reverse order."
5767   (interactive "P")
5768   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5769
5770 (defun gnus-group-sort-by-alphabet (info1 info2)
5771   "Sort alphabetically."
5772   (string< (gnus-info-group info1) (gnus-info-group info2)))
5773
5774 (defun gnus-group-sort-by-unread (info1 info2)
5775   "Sort by number of unread articles."
5776   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5777         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5778     (< (or (and (numberp n1) n1) 0)
5779        (or (and (numberp n2) n2) 0))))
5780
5781 (defun gnus-group-sort-by-level (info1 info2)
5782   "Sort by level."
5783   (< (gnus-info-level info1) (gnus-info-level info2)))
5784
5785 (defun gnus-group-sort-by-method (info1 info2)
5786   "Sort alphabetically by backend name."
5787   (string< (symbol-name (car (gnus-find-method-for-group
5788                               (gnus-info-group info1) info1)))
5789            (symbol-name (car (gnus-find-method-for-group
5790                               (gnus-info-group info2) info2)))))
5791
5792 (defun gnus-group-sort-by-score (info1 info2)
5793   "Sort by group score."
5794   (< (gnus-info-score info1) (gnus-info-score info2)))
5795
5796 (defun gnus-group-sort-by-rank (info1 info2)
5797   "Sort by level and score."
5798   (let ((level1 (gnus-info-level info1))
5799         (level2 (gnus-info-level info2)))
5800     (or (< level1 level2)
5801         (and (= level1 level2)
5802              (< (gnus-info-score info1) (gnus-info-score info2))))))
5803
5804 ;; Group catching up.
5805
5806 (defun gnus-group-catchup-current (&optional n all)
5807   "Mark all articles not marked as unread in current newsgroup as read.
5808 If prefix argument N is numeric, the ARG next newsgroups will be
5809 caught up.  If ALL is non-nil, marked articles will also be marked as
5810 read.  Cross references (Xref: header) of articles are ignored.
5811 The difference between N and actual number of newsgroups that were
5812 caught up is returned."
5813   (interactive "P")
5814   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5815                gnus-expert-user
5816                (gnus-y-or-n-p
5817                 (if all
5818                     "Do you really want to mark all articles as read? "
5819                   "Mark all unread articles as read? "))))
5820       n
5821     (let ((groups (gnus-group-process-prefix n))
5822           (ret 0))
5823       (while groups
5824         ;; Virtual groups have to be given special treatment.
5825         (let ((method (gnus-find-method-for-group (car groups))))
5826           (if (eq 'nnvirtual (car method))
5827               (nnvirtual-catchup-group
5828                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5829         (gnus-group-remove-mark (car groups))
5830         (if (prog1
5831                 (gnus-group-goto-group (car groups))
5832               (gnus-group-catchup (car groups) all))
5833             (gnus-group-update-group-line)
5834           (setq ret (1+ ret)))
5835         (setq groups (cdr groups)))
5836       (gnus-group-next-unread-group 1)
5837       ret)))
5838
5839 (defun gnus-group-catchup-current-all (&optional n)
5840   "Mark all articles in current newsgroup as read.
5841 Cross references (Xref: header) of articles are ignored."
5842   (interactive "P")
5843   (gnus-group-catchup-current n 'all))
5844
5845 (defun gnus-group-catchup (group &optional all)
5846   "Mark all articles in GROUP as read.
5847 If ALL is non-nil, all articles are marked as read.
5848 The return value is the number of articles that were marked as read,
5849 or nil if no action could be taken."
5850   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5851          (num (car entry)))
5852     ;; Do the updating only if the newsgroup isn't killed.
5853     (if (not (numberp (car entry)))
5854         (gnus-message 1 "Can't catch up; non-active group")
5855       ;; Do auto-expirable marks if that's required.
5856       (when (gnus-group-auto-expirable-p group)
5857         (gnus-add-marked-articles
5858          group 'expire (gnus-list-of-unread-articles group))
5859         (when all
5860           (let ((marks (nth 3 (nth 2 entry))))
5861             (gnus-add-marked-articles
5862              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5863             (gnus-add-marked-articles
5864              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5865       (when entry
5866         (gnus-update-read-articles group nil)
5867         ;; Also nix out the lists of marks and dormants.
5868         (when all
5869           (gnus-add-marked-articles group 'tick nil nil 'force)
5870           (gnus-add-marked-articles group 'dormant nil nil 'force))
5871         (run-hooks 'gnus-group-catchup-group-hook)
5872         num))))
5873
5874 (defun gnus-group-expire-articles (&optional n)
5875   "Expire all expirable articles in the current newsgroup."
5876   (interactive "P")
5877   (let ((groups (gnus-group-process-prefix n))
5878         group)
5879     (unless groups
5880       (error "No groups to expire"))
5881     (while (setq group (pop groups))
5882       (gnus-group-remove-mark group)
5883       (when (gnus-check-backend-function 'request-expire-articles group)
5884         (gnus-message 6 "Expiring articles in %s..." group)
5885         (let* ((info (gnus-get-info group))
5886                (expirable (if (gnus-group-total-expirable-p group)
5887                               (cons nil (gnus-list-of-read-articles group))
5888                             (assq 'expire (gnus-info-marks info))))
5889                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5890           (when expirable
5891             (setcdr
5892              expirable
5893              (gnus-compress-sequence
5894               (if expiry-wait
5895                   ;; We set the expiry variables to the groupp
5896                   ;; parameter. 
5897                   (let ((nnmail-expiry-wait-function nil)
5898                         (nnmail-expiry-wait expiry-wait))
5899                     (gnus-request-expire-articles
5900                      (gnus-uncompress-sequence (cdr expirable)) group))
5901                 ;; Just expire using the normal expiry values.
5902                 (gnus-request-expire-articles
5903                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5904           (gnus-message 6 "Expiring articles in %s...done" group)))
5905       (gnus-group-position-point))))
5906
5907 (defun gnus-group-expire-all-groups ()
5908   "Expire all expirable articles in all newsgroups."
5909   (interactive)
5910   (save-excursion
5911     (gnus-message 5 "Expiring...")
5912     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5913                                      (cdr gnus-newsrc-alist))))
5914       (gnus-group-expire-articles nil)))
5915   (gnus-group-position-point)
5916   (gnus-message 5 "Expiring...done"))
5917
5918 (defun gnus-group-set-current-level (n level)
5919   "Set the level of the next N groups to LEVEL."
5920   (interactive
5921    (list
5922     current-prefix-arg
5923     (if (not (gnus-group-group-name))
5924         (error "No group on the current line")
5925       (string-to-int
5926        (let ((s (read-string
5927                  (format "Level (default %s): " (gnus-group-group-level)))))
5928          (if (string-match "^\\s-*$" s)
5929              (int-to-string (gnus-group-group-level))
5930            s))))))
5931   (or (and (>= level 1) (<= level gnus-level-killed))
5932       (error "Illegal level: %d" level))
5933   (let ((groups (gnus-group-process-prefix n))
5934         group)
5935     (while (setq group (pop groups))
5936       (gnus-group-remove-mark group)
5937       (gnus-message 6 "Changed level of %s from %d to %d"
5938                     group (or (gnus-group-group-level) gnus-level-killed)
5939                     level)
5940       (gnus-group-change-level
5941        group level (or (gnus-group-group-level) gnus-level-killed))
5942       (gnus-group-update-group-line)))
5943   (gnus-group-position-point))
5944
5945 (defun gnus-group-unsubscribe-current-group (&optional n)
5946   "Toggle subscription of the current group.
5947 If given numerical prefix, toggle the N next groups."
5948   (interactive "P")
5949   (let ((groups (gnus-group-process-prefix n))
5950         group)
5951     (while groups
5952       (setq group (car groups)
5953             groups (cdr groups))
5954       (gnus-group-remove-mark group)
5955       (gnus-group-unsubscribe-group
5956        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5957                  gnus-level-default-unsubscribed
5958                gnus-level-default-subscribed) t)
5959       (gnus-group-update-group-line))
5960     (gnus-group-next-group 1)))
5961
5962 (defun gnus-group-unsubscribe-group (group &optional level silent)
5963   "Toggle subscription to GROUP.
5964 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5965 group line."
5966   (interactive
5967    (list (completing-read
5968           "Group: " gnus-active-hashtb nil
5969           (memq gnus-select-method gnus-have-read-active-file))))
5970   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5971     (cond
5972      ((string-match "^[ \t]$" group)
5973       (error "Empty group name"))
5974      (newsrc
5975       ;; Toggle subscription flag.
5976       (gnus-group-change-level
5977        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
5978                                       gnus-level-subscribed)
5979                                   (1+ gnus-level-subscribed)
5980                                 gnus-level-default-subscribed)))
5981       (unless silent
5982         (gnus-group-update-group group)))
5983      ((and (stringp group)
5984            (or (not (memq gnus-select-method gnus-have-read-active-file))
5985                (gnus-active group)))
5986       ;; Add new newsgroup.
5987       (gnus-group-change-level
5988        group
5989        (if level level gnus-level-default-subscribed)
5990        (or (and (member group gnus-zombie-list)
5991                 gnus-level-zombie)
5992            gnus-level-killed)
5993        (and (gnus-group-group-name)
5994             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
5995       (unless silent
5996         (gnus-group-update-group group)))
5997      (t (error "No such newsgroup: %s" group)))
5998     (gnus-group-position-point)))
5999
6000 (defun gnus-group-transpose-groups (n)
6001   "Move the current newsgroup up N places.
6002 If given a negative prefix, move down instead.  The difference between
6003 N and the number of steps taken is returned."
6004   (interactive "p")
6005   (or (gnus-group-group-name)
6006       (error "No group on current line"))
6007   (gnus-group-kill-group 1)
6008   (prog1
6009       (forward-line (- n))
6010     (gnus-group-yank-group)
6011     (gnus-group-position-point)))
6012
6013 (defun gnus-group-kill-all-zombies ()
6014   "Kill all zombie newsgroups."
6015   (interactive)
6016   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6017   (setq gnus-zombie-list nil)
6018   (gnus-group-list-groups))
6019
6020 (defun gnus-group-kill-region (begin end)
6021   "Kill newsgroups in current region (excluding current point).
6022 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6023   (interactive "r")
6024   (let ((lines
6025          ;; Count lines.
6026          (save-excursion
6027            (count-lines
6028             (progn
6029               (goto-char begin)
6030               (beginning-of-line)
6031               (point))
6032             (progn
6033               (goto-char end)
6034               (beginning-of-line)
6035               (point))))))
6036     (goto-char begin)
6037     (beginning-of-line)                 ;Important when LINES < 1
6038     (gnus-group-kill-group lines)))
6039
6040 (defun gnus-group-kill-group (&optional n discard)
6041   "Kill the next N groups.
6042 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6043 However, only groups that were alive can be yanked; already killed
6044 groups or zombie groups can't be yanked.
6045 The return value is the name of the group that was killed, or a list
6046 of groups killed."
6047   (interactive "P")
6048   (let ((buffer-read-only nil)
6049         (groups (gnus-group-process-prefix n))
6050         group entry level out)
6051     (if (< (length groups) 10)
6052         ;; This is faster when there are few groups.
6053         (while groups
6054           (push (setq group (pop groups)) out)
6055           (gnus-group-remove-mark group)
6056           (setq level (gnus-group-group-level))
6057           (gnus-delete-line)
6058           (if (and (not discard)
6059                    (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6060               (setq gnus-list-of-killed-groups
6061                     (cons (cons (car entry) (nth 2 entry))
6062                           gnus-list-of-killed-groups)))
6063           (gnus-group-change-level
6064            (if entry entry group) gnus-level-killed (if entry nil level)))
6065       ;; If there are lots and lots of groups to be killed, we use
6066       ;; this thing instead.
6067       (let (entry)
6068         (setq groups (nreverse groups))
6069         (while groups
6070           (gnus-group-remove-mark (setq group (pop groups)))
6071           (gnus-delete-line)
6072           (cond
6073            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6074             (push (cons (car entry) (nth 2 entry))
6075                   gnus-list-of-killed-groups)
6076             (setcdr (cdr entry) (cdddr entry)))
6077            ((member group gnus-zombie-list)
6078             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6079         (gnus-make-hashtable-from-newsrc-alist)))
6080
6081     (gnus-group-position-point)
6082     (if (< (length out) 2) (car out) (nreverse out))))
6083
6084 (defun gnus-group-yank-group (&optional arg)
6085   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6086 inserting it before the current newsgroup.  The numeric ARG specifies
6087 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6088 is returned, or (if several groups are yanked) a list of yanked groups
6089 is returned."
6090   (interactive "p")
6091   (setq arg (or arg 1))
6092   (let (info group prev out)
6093     (while (>= (decf arg) 0)
6094       (if (not (setq info (pop gnus-list-of-killed-groups)))
6095           (error "No more newsgroups to yank"))
6096       (push (setq group (nth 1 info)) out)
6097       ;; Find which newsgroup to insert this one before - search
6098       ;; backward until something suitable is found.  If there are no
6099       ;; other newsgroups in this buffer, just make this newsgroup the
6100       ;; first newsgroup.
6101       (setq prev (gnus-group-group-name))
6102       (gnus-group-change-level
6103        info (nth 2 info) gnus-level-killed
6104        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6105        t)
6106       (gnus-group-insert-group-line-info group))
6107     (forward-line -1)
6108     (gnus-group-position-point)
6109     (if (< (length out) 2) (car out) (nreverse out))))
6110
6111 (defun gnus-group-kill-level (level)
6112   "Kill all groups that is on a certain LEVEL."
6113   (interactive "nKill all groups on level: ")
6114   (cond
6115    ((= level gnus-level-zombie)
6116     (setq gnus-killed-list
6117           (nconc gnus-zombie-list gnus-killed-list))
6118     (setq gnus-zombie-list nil))
6119    ((and (< level gnus-level-zombie)
6120          (> level 0)
6121          (or gnus-expert-user
6122              (gnus-yes-or-no-p
6123               (format
6124                "Do you really want to kill all groups on level %d? "
6125                level))))
6126     (let* ((prev gnus-newsrc-alist)
6127            (alist (cdr prev)))
6128       (while alist
6129         (if (= (gnus-info-level level) level)
6130             (setcdr prev (cdr alist))
6131           (setq prev alist))
6132         (setq alist (cdr alist)))
6133       (gnus-make-hashtable-from-newsrc-alist)
6134       (gnus-group-list-groups)))
6135    (t
6136     (error "Can't kill; illegal level: %d" level))))
6137
6138 (defun gnus-group-list-all-groups (&optional arg)
6139   "List all newsgroups with level ARG or lower.
6140 Default is gnus-level-unsubscribed, which lists all subscribed and most
6141 unsubscribed groups."
6142   (interactive "P")
6143   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6144
6145 ;; Redefine this to list ALL killed groups if prefix arg used.
6146 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6147 (defun gnus-group-list-killed (&optional arg)
6148   "List all killed newsgroups in the group buffer.
6149 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6150 entail asking the server for the groups."
6151   (interactive "P")
6152   ;; Find all possible killed newsgroups if arg.
6153   (when arg
6154     ;; First make sure active file has been read.
6155     (unless gnus-have-read-active-file
6156       (let ((gnus-read-active-file t))
6157         (gnus-read-active-file)))
6158     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6159     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6160     (mapatoms
6161      (lambda (sym)
6162        (let ((groups 0)
6163              (group (symbol-name sym)))
6164          (if (or (null group)
6165                  (gnus-gethash group gnus-killed-hashtb)
6166                  (gnus-gethash group gnus-newsrc-hashtb))
6167              ()
6168            (let ((do-sub (gnus-matches-options-n group)))
6169              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6170                  ()
6171                (setq groups (1+ groups))
6172                (setq gnus-killed-list
6173                      (cons group gnus-killed-list))
6174                (gnus-sethash group group gnus-killed-hashtb))))))
6175      gnus-active-hashtb))
6176   (if (not gnus-killed-list)
6177       (gnus-message 6 "No killed groups")
6178     (let (gnus-group-list-mode)
6179       (funcall gnus-group-prepare-function
6180                gnus-level-killed t gnus-level-killed))
6181     (goto-char (point-min)))
6182   (gnus-group-position-point))
6183
6184 (defun gnus-group-list-zombies ()
6185   "List all zombie newsgroups in the group buffer."
6186   (interactive)
6187   (if (not gnus-zombie-list)
6188       (gnus-message 6 "No zombie groups")
6189     (let (gnus-group-list-mode)
6190       (funcall gnus-group-prepare-function
6191                gnus-level-zombie t gnus-level-zombie))
6192     (goto-char (point-min)))
6193   (gnus-group-position-point))
6194
6195 (defun gnus-group-list-active ()
6196   "List all groups that are available from the server(s)."
6197   (interactive)
6198   ;; First we make sure that we have really read the active file.
6199   (unless gnus-have-read-active-file
6200     (let ((gnus-read-active-file t))
6201       (gnus-read-active-file)))
6202   ;; Find all groups and sort them.
6203   (let ((groups
6204          (sort
6205           (let (list)
6206             (mapatoms
6207              (lambda (sym)
6208                (and (symbol-value sym)
6209                     (setq list (cons (symbol-name sym) list))))
6210              gnus-active-hashtb)
6211             list)
6212           'string<))
6213         (buffer-read-only nil))
6214     (erase-buffer)
6215     (while groups
6216       (gnus-group-insert-group-line-info (car groups))
6217       (setq groups (cdr groups)))
6218     (goto-char (point-min))))
6219
6220 (defun gnus-activate-all-groups (level)
6221   "Activate absolutely all groups."
6222   (interactive (list 7))
6223   (let ((gnus-activate-level level)
6224         (gnus-activate-foreign-newsgroups level))
6225     (gnus-group-get-new-news)))
6226
6227 (defun gnus-group-get-new-news (&optional arg)
6228   "Get newly arrived articles.
6229 If ARG is a number, it specifies which levels you are interested in
6230 re-scanning.  If ARG is non-nil and not a number, this will force
6231 \"hard\" re-reading of the active files from all servers."
6232   (interactive "P")
6233   (run-hooks 'gnus-get-new-news-hook)
6234   ;; We might read in new NoCeM messages here.
6235   (when (and gnus-use-nocem 
6236              (null arg))
6237     (gnus-nocem-scan-groups))
6238   ;; If ARG is not a number, then we read the active file.
6239   (when (and arg (not (numberp arg)))
6240     (let ((gnus-read-active-file t))
6241       (gnus-read-active-file))
6242     (setq arg nil))
6243
6244   (setq arg (gnus-group-default-level arg t))
6245   (if (and gnus-read-active-file (not arg))
6246       (progn
6247         (gnus-read-active-file)
6248         (gnus-get-unread-articles arg))
6249     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6250       (gnus-get-unread-articles arg)))
6251   (run-hooks 'gnus-after-getting-new-news-hook)
6252   (gnus-group-list-groups))
6253
6254 (defun gnus-group-get-new-news-this-group (&optional n)
6255   "Check for newly arrived news in the current group (and the N-1 next groups).
6256 The difference between N and the number of newsgroup checked is returned.
6257 If N is negative, this group and the N-1 previous groups will be checked."
6258   (interactive "P")
6259   (let* ((groups (gnus-group-process-prefix n))
6260          (ret (if (numberp n) (- n (length groups)) 0))
6261          group)
6262     (while groups
6263       (setq group (car groups)
6264             groups (cdr groups))
6265       (gnus-group-remove-mark group)
6266       (unless (gnus-get-new-news-in-group group)
6267         (ding)
6268         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6269     (when gnus-goto-next-group-when-activating
6270       (gnus-group-next-unread-group 1 t))
6271     (gnus-summary-position-point)
6272     ret))
6273
6274 (defun gnus-get-new-news-in-group (group)
6275   (when (and group (gnus-activate-group group 'scan))
6276     (gnus-get-unread-articles-in-group
6277      (gnus-get-info group) (gnus-active group) t)
6278     (when (gnus-group-goto-group group)
6279       (gnus-group-update-group-line))
6280     t))
6281
6282 (defun gnus-group-fetch-faq (group &optional faq-dir)
6283   "Fetch the FAQ for the current group."
6284   (interactive
6285    (list
6286     (gnus-group-real-name (gnus-group-group-name))
6287     (cond (current-prefix-arg
6288            (completing-read
6289             "Faq dir: " (and (listp gnus-group-faq-directory)
6290                              gnus-group-faq-directory))))))
6291   (or faq-dir
6292       (setq faq-dir (if (listp gnus-group-faq-directory)
6293                         (car gnus-group-faq-directory)
6294                       gnus-group-faq-directory)))
6295   (or group (error "No group name given"))
6296   (let ((file (concat (file-name-as-directory faq-dir)
6297                       (gnus-group-real-name group))))
6298     (if (not (file-exists-p file))
6299         (error "No such file: %s" file)
6300       (find-file file))))
6301
6302 (defun gnus-group-describe-group (force &optional group)
6303   "Display a description of the current newsgroup."
6304   (interactive (list current-prefix-arg (gnus-group-group-name)))
6305   (and force (setq gnus-description-hashtb nil))
6306   (let ((method (gnus-find-method-for-group group))
6307         desc)
6308     (or group (error "No group name given"))
6309     (and (or (and gnus-description-hashtb
6310                   ;; We check whether this group's method has been
6311                   ;; queried for a description file.
6312                   (gnus-gethash
6313                    (gnus-group-prefixed-name "" method)
6314                    gnus-description-hashtb))
6315              (setq desc (gnus-group-get-description group))
6316              (gnus-read-descriptions-file method))
6317          (message
6318           (or desc (gnus-gethash group gnus-description-hashtb)
6319               "No description available")))))
6320
6321 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6322 (defun gnus-group-describe-all-groups (&optional force)
6323   "Pop up a buffer with descriptions of all newsgroups."
6324   (interactive "P")
6325   (and force (setq gnus-description-hashtb nil))
6326   (if (not (or gnus-description-hashtb
6327                (gnus-read-all-descriptions-files)))
6328       (error "Couldn't request descriptions file"))
6329   (let ((buffer-read-only nil)
6330         b)
6331     (erase-buffer)
6332     (mapatoms
6333      (lambda (group)
6334        (setq b (point))
6335        (insert (format "      *: %-20s %s\n" (symbol-name group)
6336                        (symbol-value group)))
6337        (add-text-properties
6338         b (1+ b) (list 'gnus-group group
6339                        'gnus-unread t 'gnus-marked nil
6340                        'gnus-level (1+ gnus-level-subscribed))))
6341      gnus-description-hashtb)
6342     (goto-char (point-min))
6343     (gnus-group-position-point)))
6344
6345 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6346 (defun gnus-group-apropos (regexp &optional search-description)
6347   "List all newsgroups that have names that match a regexp."
6348   (interactive "sGnus apropos (regexp): ")
6349   (let ((prev "")
6350         (obuf (current-buffer))
6351         groups des)
6352     ;; Go through all newsgroups that are known to Gnus.
6353     (mapatoms
6354      (lambda (group)
6355        (and (symbol-name group)
6356             (string-match regexp (symbol-name group))
6357             (setq groups (cons (symbol-name group) groups))))
6358      gnus-active-hashtb)
6359     ;; Go through all descriptions that are known to Gnus.
6360     (if search-description
6361         (mapatoms
6362          (lambda (group)
6363            (and (string-match regexp (symbol-value group))
6364                 (gnus-active (symbol-name group))
6365                 (setq groups (cons (symbol-name group) groups))))
6366          gnus-description-hashtb))
6367     (if (not groups)
6368         (gnus-message 3 "No groups matched \"%s\"." regexp)
6369       ;; Print out all the groups.
6370       (save-excursion
6371         (pop-to-buffer "*Gnus Help*")
6372         (buffer-disable-undo (current-buffer))
6373         (erase-buffer)
6374         (setq groups (sort groups 'string<))
6375         (while groups
6376           ;; Groups may be entered twice into the list of groups.
6377           (if (not (string= (car groups) prev))
6378               (progn
6379                 (insert (setq prev (car groups)) "\n")
6380                 (if (and gnus-description-hashtb
6381                          (setq des (gnus-gethash (car groups)
6382                                                  gnus-description-hashtb)))
6383                     (insert "  " des "\n"))))
6384           (setq groups (cdr groups)))
6385         (goto-char (point-min))))
6386     (pop-to-buffer obuf)))
6387
6388 (defun gnus-group-description-apropos (regexp)
6389   "List all newsgroups that have names or descriptions that match a regexp."
6390   (interactive "sGnus description apropos (regexp): ")
6391   (if (not (or gnus-description-hashtb
6392                (gnus-read-all-descriptions-files)))
6393       (error "Couldn't request descriptions file"))
6394   (gnus-group-apropos regexp t))
6395
6396 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6397 (defun gnus-group-list-matching (level regexp &optional all lowest)
6398   "List all groups with unread articles that match REGEXP.
6399 If the prefix LEVEL is non-nil, it should be a number that says which
6400 level to cut off listing groups.
6401 If ALL, also list groups with no unread articles.
6402 If LOWEST, don't list groups with level lower than LOWEST."
6403   (interactive "P\nsList newsgroups matching: ")
6404   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6405                            all (or lowest 1) regexp)
6406   (goto-char (point-min))
6407   (gnus-group-position-point))
6408
6409 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6410   "List all groups that match REGEXP.
6411 If the prefix LEVEL is non-nil, it should be a number that says which
6412 level to cut off listing groups.
6413 If LOWEST, don't list groups with level lower than LOWEST."
6414   (interactive "P\nsList newsgroups matching: ")
6415   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6416
6417 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6418 (defun gnus-group-save-newsrc (&optional force)
6419   "Save the Gnus startup files.
6420 If FORCE, force saving whether it is necessary or not."
6421   (interactive "P")
6422   (gnus-save-newsrc-file force))
6423
6424 (defun gnus-group-restart (&optional arg)
6425   "Force Gnus to read the .newsrc file."
6426   (interactive "P")
6427   (gnus-save-newsrc-file)
6428   (gnus-setup-news 'force)
6429   (gnus-group-list-groups arg))
6430
6431 (defun gnus-group-read-init-file ()
6432   "Read the Gnus elisp init file."
6433   (interactive)
6434   (gnus-read-init-file))
6435
6436 (defun gnus-group-check-bogus-groups (&optional silent)
6437   "Check bogus newsgroups.
6438 If given a prefix, don't ask for confirmation before removing a bogus
6439 group."
6440   (interactive "P")
6441   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6442   (gnus-group-list-groups))
6443
6444 (defun gnus-group-edit-global-kill (&optional article group)
6445   "Edit the global kill file.
6446 If GROUP, edit that local kill file instead."
6447   (interactive "P")
6448   (setq gnus-current-kill-article article)
6449   (gnus-kill-file-edit-file group)
6450   (gnus-message
6451    6
6452    (substitute-command-keys
6453     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6454             (if group "local" "global")))))
6455
6456 (defun gnus-group-edit-local-kill (article group)
6457   "Edit a local kill file."
6458   (interactive (list nil (gnus-group-group-name)))
6459   (gnus-group-edit-global-kill article group))
6460
6461 (defun gnus-group-force-update ()
6462   "Update `.newsrc' file."
6463   (interactive)
6464   (gnus-save-newsrc-file))
6465
6466 (defun gnus-group-suspend ()
6467   "Suspend the current Gnus session.
6468 In fact, cleanup buffers except for group mode buffer.
6469 The hook gnus-suspend-gnus-hook is called before actually suspending."
6470   (interactive)
6471   (run-hooks 'gnus-suspend-gnus-hook)
6472   ;; Kill Gnus buffers except for group mode buffer.
6473   (let ((group-buf (get-buffer gnus-group-buffer)))
6474     ;; Do this on a separate list in case the user does a ^G before we finish
6475     (let ((gnus-buffer-list
6476            (delq group-buf (delq gnus-dribble-buffer
6477                                  (append gnus-buffer-list nil)))))
6478       (while gnus-buffer-list
6479         (gnus-kill-buffer (car gnus-buffer-list))
6480         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6481     (if group-buf
6482         (progn
6483           (setq gnus-buffer-list (list group-buf))
6484           (bury-buffer group-buf)
6485           (delete-windows-on group-buf t)))))
6486
6487 (defun gnus-group-clear-dribble ()
6488   "Clear all information from the dribble buffer."
6489   (interactive)
6490   (gnus-dribble-clear)
6491   (gnus-message 7 "Cleared dribble buffer"))
6492
6493 (defun gnus-group-exit ()
6494   "Quit reading news after updating .newsrc.eld and .newsrc.
6495 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6496   (interactive)
6497   (when 
6498       (or noninteractive                ;For gnus-batch-kill
6499           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6500           (not gnus-interactive-exit)   ;Without confirmation
6501           gnus-expert-user
6502           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6503     (run-hooks 'gnus-exit-gnus-hook)
6504     ;; Close down GroupLens.
6505     (when gnus-use-grouplens
6506       (bbb-logout))
6507     ;; Offer to save data from non-quitted summary buffers.
6508     (gnus-offer-save-summaries)
6509     ;; Save the newsrc file(s).
6510     (gnus-save-newsrc-file)
6511     ;; Kill-em-all.
6512     (gnus-close-backends)
6513     ;; Shut down the cache.
6514     (when gnus-use-cache
6515       (gnus-cache-close))
6516     ;; Reset everything.
6517     (gnus-clear-system)
6518     ;; Allow the user to do things after cleaning up.
6519     (run-hooks 'gnus-after-exiting-gnus-hook)))
6520
6521 (defun gnus-close-backends ()
6522   ;; Send a close request to all backends that support such a request.
6523   (let ((methods gnus-valid-select-methods)
6524         func)
6525     (while methods
6526       (if (fboundp (setq func (intern (concat (caar methods)
6527                                               "-request-close"))))
6528           (funcall func))
6529       (setq methods (cdr methods)))))
6530
6531 (defun gnus-group-quit ()
6532   "Quit reading news without updating .newsrc.eld or .newsrc.
6533 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6534   (interactive)
6535   (when (or noninteractive              ;For gnus-batch-kill
6536             (zerop (buffer-size))
6537             (not (gnus-server-opened gnus-select-method))
6538             gnus-expert-user
6539             (not gnus-current-startup-file)
6540             (gnus-yes-or-no-p
6541              (format "Quit reading news without saving %s? "
6542                      (file-name-nondirectory gnus-current-startup-file))))
6543     (run-hooks 'gnus-exit-gnus-hook)
6544     (if gnus-use-full-window
6545         (delete-other-windows)
6546       (gnus-remove-some-windows))
6547     (gnus-dribble-save)
6548     (gnus-close-backends)
6549     ;; Shut down the cache.
6550     (when gnus-use-cache
6551       (gnus-cache-close))
6552     (gnus-clear-system)
6553     ;; Allow the user to do things after cleaning up.
6554     (run-hooks 'gnus-after-exiting-gnus-hook)))
6555
6556 (defun gnus-offer-save-summaries ()
6557   "Offer to save all active summary buffers."
6558   (save-excursion
6559     (let ((buflist (buffer-list))
6560           buffers bufname)
6561       ;; Go through all buffers and find all summaries.
6562       (while buflist
6563         (and (setq bufname (buffer-name (car buflist)))
6564              (string-match "Summary" bufname)
6565              (save-excursion
6566                (set-buffer bufname)
6567                ;; We check that this is, indeed, a summary buffer.
6568                (and (eq major-mode 'gnus-summary-mode)
6569                     ;; Also make sure this isn't bogus.
6570                     gnus-newsgroup-prepared))
6571              (push bufname buffers))
6572         (setq buflist (cdr buflist)))
6573       ;; Go through all these summary buffers and offer to save them.
6574       (when buffers
6575         (map-y-or-n-p
6576          "Update summary buffer %s? "
6577          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6578          buffers)))))
6579
6580 (defun gnus-group-describe-briefly ()
6581   "Give a one line description of the group mode commands."
6582   (interactive)
6583   (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")))
6584
6585 (defun gnus-group-browse-foreign-server (method)
6586   "Browse a foreign news server.
6587 If called interactively, this function will ask for a select method
6588  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6589 If not, METHOD should be a list where the first element is the method
6590 and the second element is the address."
6591   (interactive
6592    (list (let ((how (completing-read
6593                      "Which backend: "
6594                      (append gnus-valid-select-methods gnus-server-alist)
6595                      nil t (cons "nntp" 0))))
6596            ;; We either got a backend name or a virtual server name.
6597            ;; If the first, we also need an address.
6598            (if (assoc how gnus-valid-select-methods)
6599                (list (intern how)
6600                      ;; Suggested by mapjph@bath.ac.uk.
6601                      (completing-read
6602                       "Address: "
6603                       (mapcar (lambda (server) (list server))
6604                               gnus-secondary-servers)))
6605              ;; We got a server name, so we find the method.
6606              (gnus-server-to-method how)))))
6607   (gnus-browse-foreign-server method))
6608
6609 \f
6610 ;;;
6611 ;;; Gnus summary mode
6612 ;;;
6613
6614 (defvar gnus-summary-mode-map nil)
6615
6616 (put 'gnus-summary-mode 'mode-class 'special)
6617
6618 (unless gnus-summary-mode-map
6619   (setq gnus-summary-mode-map (make-keymap))
6620   (suppress-keymap gnus-summary-mode-map)
6621
6622   ;; Non-orthogonal keys
6623
6624   (gnus-define-keys gnus-summary-mode-map
6625     " " gnus-summary-next-page
6626     "\177" gnus-summary-prev-page
6627     [delete] gnus-summary-prev-page
6628     "\r" gnus-summary-scroll-up
6629     "n" gnus-summary-next-unread-article
6630     "p" gnus-summary-prev-unread-article
6631     "N" gnus-summary-next-article
6632     "P" gnus-summary-prev-article
6633     "\M-\C-n" gnus-summary-next-same-subject
6634     "\M-\C-p" gnus-summary-prev-same-subject
6635     "\M-n" gnus-summary-next-unread-subject
6636     "\M-p" gnus-summary-prev-unread-subject
6637     "." gnus-summary-first-unread-article
6638     "," gnus-summary-best-unread-article
6639     "\M-s" gnus-summary-search-article-forward
6640     "\M-r" gnus-summary-search-article-backward
6641     "<" gnus-summary-beginning-of-article
6642     ">" gnus-summary-end-of-article
6643     "j" gnus-summary-goto-article
6644     "^" gnus-summary-refer-parent-article
6645     "\M-^" gnus-summary-refer-article
6646     "u" gnus-summary-tick-article-forward
6647     "!" gnus-summary-tick-article-forward
6648     "U" gnus-summary-tick-article-backward
6649     "d" gnus-summary-mark-as-read-forward
6650     "D" gnus-summary-mark-as-read-backward
6651     "E" gnus-summary-mark-as-expirable
6652     "\M-u" gnus-summary-clear-mark-forward
6653     "\M-U" gnus-summary-clear-mark-backward
6654     "k" gnus-summary-kill-same-subject-and-select
6655     "\C-k" gnus-summary-kill-same-subject
6656     "\M-\C-k" gnus-summary-kill-thread
6657     "\M-\C-l" gnus-summary-lower-thread
6658     "e" gnus-summary-edit-article
6659     "#" gnus-summary-mark-as-processable
6660     "\M-#" gnus-summary-unmark-as-processable
6661     "\M-\C-t" gnus-summary-toggle-threads
6662     "\M-\C-s" gnus-summary-show-thread
6663     "\M-\C-h" gnus-summary-hide-thread
6664     "\M-\C-f" gnus-summary-next-thread
6665     "\M-\C-b" gnus-summary-prev-thread
6666     "\M-\C-u" gnus-summary-up-thread
6667     "\M-\C-d" gnus-summary-down-thread
6668     "&" gnus-summary-execute-command
6669     "c" gnus-summary-catchup-and-exit
6670     "\C-w" gnus-summary-mark-region-as-read
6671     "\C-t" gnus-summary-toggle-truncation
6672     "?" gnus-summary-mark-as-dormant
6673     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6674     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6675     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6676     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6677     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6678     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6679     "=" gnus-summary-expand-window
6680     "\C-x\C-s" gnus-summary-reselect-current-group
6681     "\M-g" gnus-summary-rescan-group
6682     "w" gnus-summary-stop-page-breaking
6683     "\C-c\C-r" gnus-summary-caesar-message
6684     "\M-t" gnus-summary-toggle-mime
6685     "f" gnus-summary-followup
6686     "F" gnus-summary-followup-with-original
6687     "C" gnus-summary-cancel-article
6688     "r" gnus-summary-reply
6689     "R" gnus-summary-reply-with-original
6690     "\C-c\C-f" gnus-summary-mail-forward
6691     "o" gnus-summary-save-article
6692     "\C-o" gnus-summary-save-article-mail
6693     "|" gnus-summary-pipe-output
6694     "\M-k" gnus-summary-edit-local-kill
6695     "\M-K" gnus-summary-edit-global-kill
6696     "V" gnus-version
6697     "\C-c\C-d" gnus-summary-describe-group
6698     "q" gnus-summary-exit
6699     "Q" gnus-summary-exit-no-update
6700     "\C-c\C-i" gnus-info-find-node
6701     gnus-mouse-2 gnus-mouse-pick-article
6702     "m" gnus-summary-mail-other-window
6703     "a" gnus-summary-post-news
6704     "x" gnus-summary-limit-to-unread
6705     "s" gnus-summary-isearch-article
6706     "t" gnus-article-hide-headers
6707     "g" gnus-summary-show-article
6708     "l" gnus-summary-goto-last-article
6709     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6710     "\C-d" gnus-summary-enter-digest-group
6711     "\C-c\C-b" gnus-bug
6712     "*" gnus-cache-enter-article
6713     "\M-*" gnus-cache-remove-article
6714     "\M-&" gnus-summary-universal-argument
6715     "\C-l" gnus-recenter
6716     "I" gnus-summary-increase-score
6717     "L" gnus-summary-lower-score
6718
6719     "V" gnus-summary-score-map
6720     "X" gnus-uu-extract-map
6721     "S" gnus-summary-send-map)
6722
6723   ;; Sort of orthogonal keymap
6724   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6725     "t" gnus-summary-tick-article-forward
6726     "!" gnus-summary-tick-article-forward
6727     "d" gnus-summary-mark-as-read-forward
6728     "r" gnus-summary-mark-as-read-forward
6729     "c" gnus-summary-clear-mark-forward
6730     " " gnus-summary-clear-mark-forward
6731     "e" gnus-summary-mark-as-expirable
6732     "x" gnus-summary-mark-as-expirable
6733     "?" gnus-summary-mark-as-dormant
6734     "b" gnus-summary-set-bookmark
6735     "B" gnus-summary-remove-bookmark
6736     "#" gnus-summary-mark-as-processable
6737     "\M-#" gnus-summary-unmark-as-processable
6738     "S" gnus-summary-limit-include-expunged
6739     "C" gnus-summary-catchup
6740     "H" gnus-summary-catchup-to-here
6741     "\C-c" gnus-summary-catchup-all
6742     "k" gnus-summary-kill-same-subject-and-select
6743     "K" gnus-summary-kill-same-subject
6744     "P" gnus-uu-mark-map)
6745
6746   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6747     "c" gnus-summary-clear-above
6748     "u" gnus-summary-tick-above
6749     "m" gnus-summary-mark-above
6750     "k" gnus-summary-kill-below)
6751
6752   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6753     "/" gnus-summary-limit-to-subject
6754     "n" gnus-summary-limit-to-articles
6755     "w" gnus-summary-pop-limit
6756     "s" gnus-summary-limit-to-subject
6757     "a" gnus-summary-limit-to-author
6758     "u" gnus-summary-limit-to-unread
6759     "m" gnus-summary-limit-to-marks
6760     "v" gnus-summary-limit-to-score
6761     "D" gnus-summary-limit-include-dormant
6762     "d" gnus-summary-limit-exclude-dormant
6763     ;;  "t" gnus-summary-limit-exclude-thread
6764     "E" gnus-summary-limit-include-expunged
6765     "c" gnus-summary-limit-exclude-childless-dormant
6766     "C" gnus-summary-limit-mark-excluded-as-read)
6767
6768   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6769     "n" gnus-summary-next-unread-article
6770     "p" gnus-summary-prev-unread-article
6771     "N" gnus-summary-next-article
6772     "P" gnus-summary-prev-article
6773     "\C-n" gnus-summary-next-same-subject
6774     "\C-p" gnus-summary-prev-same-subject
6775     "\M-n" gnus-summary-next-unread-subject
6776     "\M-p" gnus-summary-prev-unread-subject
6777     "f" gnus-summary-first-unread-article
6778     "b" gnus-summary-best-unread-article
6779     "g" gnus-summary-goto-subject
6780     "l" gnus-summary-goto-last-article
6781     "p" gnus-summary-pop-article)
6782
6783   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6784     "k" gnus-summary-kill-thread
6785     "l" gnus-summary-lower-thread
6786     "i" gnus-summary-raise-thread
6787     "T" gnus-summary-toggle-threads
6788     "t" gnus-summary-rethread-current
6789     "^" gnus-summary-reparent-thread
6790     "s" gnus-summary-show-thread
6791     "S" gnus-summary-show-all-threads
6792     "h" gnus-summary-hide-thread
6793     "H" gnus-summary-hide-all-threads
6794     "n" gnus-summary-next-thread
6795     "p" gnus-summary-prev-thread
6796     "u" gnus-summary-up-thread
6797     "o" gnus-summary-top-thread
6798     "d" gnus-summary-down-thread
6799     "#" gnus-uu-mark-thread
6800     "\M-#" gnus-uu-unmark-thread)
6801
6802   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6803     "c" gnus-summary-catchup-and-exit
6804     "C" gnus-summary-catchup-all-and-exit
6805     "E" gnus-summary-exit-no-update
6806     "Q" gnus-summary-exit
6807     "Z" gnus-summary-exit
6808     "n" gnus-summary-catchup-and-goto-next-group
6809     "R" gnus-summary-reselect-current-group
6810     "G" gnus-summary-rescan-group
6811     "N" gnus-summary-next-group
6812     "P" gnus-summary-prev-group)
6813
6814   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6815     " " gnus-summary-next-page
6816     "n" gnus-summary-next-page
6817     "\177" gnus-summary-prev-page
6818     [delete] gnus-summary-prev-page
6819     "p" gnus-summary-prev-page
6820     "\r" gnus-summary-scroll-up
6821     "<" gnus-summary-beginning-of-article
6822     ">" gnus-summary-end-of-article
6823     "b" gnus-summary-beginning-of-article
6824     "e" gnus-summary-end-of-article
6825     "^" gnus-summary-refer-parent-article
6826     "r" gnus-summary-refer-parent-article
6827     "R" gnus-summary-refer-references
6828     "g" gnus-summary-show-article
6829     "s" gnus-summary-isearch-article)
6830
6831   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6832     "b" gnus-article-add-buttons
6833     "B" gnus-article-add-buttons-to-head
6834     "o" gnus-article-treat-overstrike
6835     ;;  "w" gnus-article-word-wrap
6836     "w" gnus-article-fill-cited-article
6837     "c" gnus-article-remove-cr
6838     "L" gnus-article-remove-trailing-blank-lines
6839     "q" gnus-article-de-quoted-unreadable
6840     "f" gnus-article-display-x-face
6841     "l" gnus-summary-stop-page-breaking
6842     "r" gnus-summary-caesar-message
6843     "t" gnus-article-hide-headers
6844     "v" gnus-summary-verbose-headers
6845     "m" gnus-summary-toggle-mime)
6846
6847   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6848     "a" gnus-article-hide
6849     "h" gnus-article-hide-headers
6850     "b" gnus-article-hide-boring-headers
6851     "s" gnus-article-hide-signature
6852     "c" gnus-article-hide-citation
6853     "p" gnus-article-hide-pgp
6854     "\C-c" gnus-article-hide-citation-maybe)
6855
6856   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6857     "a" gnus-article-highlight
6858     "h" gnus-article-highlight-headers
6859     "c" gnus-article-highlight-citation
6860     "s" gnus-article-highlight-signature)
6861
6862   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6863     "z" gnus-article-date-ut
6864     "u" gnus-article-date-ut
6865     "l" gnus-article-date-local
6866     "e" gnus-article-date-lapsed
6867     "o" gnus-article-date-original)
6868
6869   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6870     "v" gnus-version
6871     "f" gnus-summary-fetch-faq
6872     "d" gnus-summary-describe-group
6873     "h" gnus-summary-describe-briefly
6874     "i" gnus-info-find-node)
6875
6876   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6877     "e" gnus-summary-expire-articles
6878     "\M-\C-e" gnus-summary-expire-articles-now
6879     "\177" gnus-summary-delete-article
6880     [delete] gnus-summary-delete-article
6881     "m" gnus-summary-move-article
6882     "r" gnus-summary-respool-article
6883     "w" gnus-summary-edit-article
6884     "c" gnus-summary-copy-article
6885     "B" gnus-summary-crosspost-article
6886     "q" gnus-summary-respool-query
6887     "i" gnus-summary-import-article)
6888
6889   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6890     "o" gnus-summary-save-article
6891     "m" gnus-summary-save-article-mail
6892     "r" gnus-summary-save-article-rmail
6893     "f" gnus-summary-save-article-file
6894     "b" gnus-summary-save-article-body-file
6895     "h" gnus-summary-save-article-folder
6896     "v" gnus-summary-save-article-vm
6897     "p" gnus-summary-pipe-output
6898     "s" gnus-soup-add-article)
6899   )
6900
6901 \f
6902
6903 (defun gnus-summary-mode (&optional group)
6904   "Major mode for reading articles.
6905
6906 All normal editing commands are switched off.
6907 \\<gnus-summary-mode-map>
6908 Each line in this buffer represents one article.  To read an
6909 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6910 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6911 respectively.
6912
6913 You can also post articles and send mail from this buffer.  To
6914 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6915 of an article, type `\\[gnus-summary-reply]'.
6916
6917 There are approx. one gazillion commands you can execute in this
6918 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6919
6920 The following commands are available:
6921
6922 \\{gnus-summary-mode-map}"
6923   (interactive)
6924   (when (and menu-bar-mode
6925              (gnus-visual-p 'summary-menu 'menu))
6926     (gnus-summary-make-menu-bar))
6927   (kill-all-local-variables)
6928   (let ((locals gnus-summary-local-variables))
6929     (while locals
6930       (if (consp (car locals))
6931           (progn
6932             (make-local-variable (caar locals))
6933             (set (caar locals) (eval (cdar locals))))
6934         (make-local-variable (car locals))
6935         (set (car locals) nil))
6936       (setq locals (cdr locals))))
6937   (gnus-make-thread-indent-array)
6938   (gnus-simplify-mode-line)
6939   (setq major-mode 'gnus-summary-mode)
6940   (setq mode-name "Summary")
6941   (make-local-variable 'minor-mode-alist)
6942   (use-local-map gnus-summary-mode-map)
6943   (buffer-disable-undo (current-buffer))
6944   (setq buffer-read-only t)             ;Disable modification
6945   (setq truncate-lines t)
6946   (setq selective-display t)
6947   (setq selective-display-ellipses t)   ;Display `...'
6948   (setq buffer-display-table gnus-summary-display-table)
6949   (setq gnus-newsgroup-name group)
6950   (run-hooks 'gnus-summary-mode-hook))
6951
6952 (defun gnus-summary-make-display-table ()
6953   ;; Change the display table.  Odd characters have a tendency to mess
6954   ;; up nicely formatted displays - we make all possible glyphs
6955   ;; display only a single character.
6956
6957   ;; We start from the standard display table, if any.
6958   (setq gnus-summary-display-table
6959         (or (copy-sequence standard-display-table)
6960             (make-display-table)))
6961   ;; Nix out all the control chars...
6962   (let ((i 32))
6963     (while (>= (setq i (1- i)) 0)
6964       (aset gnus-summary-display-table i [??])))
6965   ;; ... but not newline and cr, of course. (cr is necessary for the
6966   ;; selective display).
6967   (aset gnus-summary-display-table ?\n nil)
6968   (aset gnus-summary-display-table ?\r nil)
6969   ;; We nix out any glyphs over 126 that are not set already.
6970   (let ((i 256))
6971     (while (>= (setq i (1- i)) 127)
6972       ;; Only modify if the entry is nil.
6973       (or (aref gnus-summary-display-table i)
6974           (aset gnus-summary-display-table i [??])))))
6975
6976 (defun gnus-summary-clear-local-variables ()
6977   (let ((locals gnus-summary-local-variables))
6978     (while locals
6979       (if (consp (car locals))
6980           (and (vectorp (caar locals))
6981                (set (caar locals) nil))
6982         (and (vectorp (car locals))
6983              (set (car locals) nil)))
6984       (setq locals (cdr locals)))))
6985
6986 ;; Summary data functions.
6987
6988 (defmacro gnus-data-number (data)
6989   `(car ,data))
6990
6991 (defmacro gnus-data-set-number (data number)
6992   `(setcar ,data ,number))
6993
6994 (defmacro gnus-data-mark (data)
6995   `(nth 1 ,data))
6996
6997 (defmacro gnus-data-set-mark (data mark)
6998   `(setcar (nthcdr 1 ,data) ,mark))
6999
7000 (defmacro gnus-data-pos (data)
7001   `(nth 2 ,data))
7002
7003 (defmacro gnus-data-set-pos (data pos)
7004   `(setcar (nthcdr 2 ,data) ,pos))
7005
7006 (defmacro gnus-data-header (data)
7007   `(nth 3 ,data))
7008
7009 (defmacro gnus-data-level (data)
7010   `(nth 4 ,data))
7011
7012 (defmacro gnus-data-unread-p (data)
7013   `(= (nth 1 ,data) gnus-unread-mark))
7014
7015 (defmacro gnus-data-pseudo-p (data)
7016   `(consp (nth 3 ,data)))
7017
7018 (defmacro gnus-data-find (number)
7019   `(assq ,number gnus-newsgroup-data))
7020
7021 (defmacro gnus-data-find-list (number &optional data)
7022   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7023      (memq (assq ,number bdata)
7024            bdata)))
7025
7026 (defmacro gnus-data-make (number mark pos header level)
7027   `(list ,number ,mark ,pos ,header ,level))
7028
7029 (defun gnus-data-enter (after-article number mark pos header level offset)
7030   (let ((data (gnus-data-find-list after-article)))
7031     (or data (error "No such article: %d" after-article))
7032     (setcdr data (cons (gnus-data-make number mark pos header level)
7033                        (cdr data)))
7034     (setq gnus-newsgroup-data-reverse nil)
7035     (gnus-data-update-list (cddr data) offset)))
7036
7037 (defun gnus-data-enter-list (after-article list &optional offset)
7038   (when list
7039     (let ((data (and after-article (gnus-data-find-list after-article)))
7040           (ilist list))
7041       (or data (not after-article) (error "No such article: %d" after-article))
7042       ;; Find the last element in the list to be spliced into the main
7043       ;; list.
7044       (while (cdr list)
7045         (setq list (cdr list)))
7046       (if (not data)
7047           (progn
7048             (setcdr list gnus-newsgroup-data)
7049             (setq gnus-newsgroup-data ilist)
7050             (and offset (gnus-data-update-list (cdr list) offset)))
7051         (setcdr list (cdr data))
7052         (setcdr data ilist)
7053         (and offset (gnus-data-update-list (cdr data) offset)))
7054       (setq gnus-newsgroup-data-reverse nil))))
7055
7056 (defun gnus-data-remove (article &optional offset)
7057   (let ((data gnus-newsgroup-data))
7058     (if (= (gnus-data-number (car data)) article)
7059         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7060               gnus-newsgroup-data-reverse nil)
7061       (while (cdr data)
7062         (and (= (gnus-data-number (cadr data)) article)
7063              (progn
7064                (setcdr data (cddr data))
7065                (and offset (gnus-data-update-list (cdr data) offset))
7066                (setq data nil
7067                      gnus-newsgroup-data-reverse nil)))
7068         (setq data (cdr data))))))
7069
7070 (defmacro gnus-data-list (backward)
7071   `(if ,backward
7072        (or gnus-newsgroup-data-reverse
7073            (setq gnus-newsgroup-data-reverse
7074                  (reverse gnus-newsgroup-data)))
7075      gnus-newsgroup-data))
7076
7077 (defun gnus-data-update-list (data offset)
7078   "Add OFFSET to the POS of all data entries in DATA."
7079   (while data
7080     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7081     (setq data (cdr data))))
7082
7083 (defun gnus-data-compute-positions ()
7084   "Compute the positions of all articles."
7085   (let ((data gnus-newsgroup-data)
7086         pos)
7087     (while data
7088       (when (setq pos (text-property-any
7089                        (point-min) (point-max)
7090                        'gnus-number (gnus-data-number (car data))))
7091         (gnus-data-set-pos (car data) (+ pos 3)))
7092       (setq data (cdr data)))))
7093
7094 (defun gnus-summary-article-pseudo-p (article)
7095   "Say whether this article is a pseudo article or not."
7096   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7097
7098 (defun gnus-article-parent-p (number)
7099   "Say whether this article is a parent or not."
7100   (let ((data (gnus-data-find-list number)))
7101     (and (cdr data)                     ; There has to be an article after...
7102          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7103             (gnus-data-level (nth 1 data))))))
7104
7105 (defun gnus-article-children (number)
7106   "Return a list of all children to NUMBER."
7107   (let* ((data (gnus-data-find-list number))
7108          (level (gnus-data-level (car data)))
7109          children)
7110     (setq data (cdr data))
7111     (while (and data            
7112                 (= (gnus-data-level (car data)) (1+ level)))
7113       (push (gnus-data-number (car data)) children)
7114       (setq data (cdr data)))
7115     children))
7116
7117 (defmacro gnus-summary-skip-intangible ()
7118   "If the current article is intangible, then jump to a different article."
7119   '(let ((to (get-text-property (point) 'gnus-intangible)))
7120     (and to (gnus-summary-goto-subject to))))
7121
7122 (defmacro gnus-summary-article-intangible-p ()
7123   "Say whether this article is intangible or not."
7124   '(get-text-property (point) 'gnus-intangible))
7125
7126 ;; Some summary mode macros.
7127
7128 (defmacro gnus-summary-article-number ()
7129   "The article number of the article on the current line.
7130 If there isn's an article number here, then we return the current
7131 article number."
7132   '(progn
7133      (gnus-summary-skip-intangible)
7134      (or (get-text-property (point) 'gnus-number)
7135          (gnus-summary-last-subject))))
7136
7137 (defmacro gnus-summary-article-header (&optional number)
7138   `(gnus-data-header (gnus-data-find
7139                       ,(or number '(gnus-summary-article-number)))))
7140
7141 (defmacro gnus-summary-thread-level (&optional number)
7142   `(if (and (eq gnus-summary-make-false-root 'dummy)
7143             (get-text-property (point) 'gnus-intangible))
7144        0
7145      (gnus-data-level (gnus-data-find
7146                        ,(or number '(gnus-summary-article-number))))))
7147
7148 (defmacro gnus-summary-article-mark (&optional number)
7149   `(gnus-data-mark (gnus-data-find
7150                     ,(or number '(gnus-summary-article-number)))))
7151
7152 (defmacro gnus-summary-article-pos (&optional number)
7153   `(gnus-data-pos (gnus-data-find
7154                    ,(or number '(gnus-summary-article-number)))))
7155
7156 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7157 (defmacro gnus-summary-article-subject (&optional number)
7158   "Return current subject string or nil if nothing."
7159   `(let ((headers
7160           ,(if number
7161                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7162              '(gnus-data-header (assq (gnus-summary-article-number)
7163                                       gnus-newsgroup-data)))))
7164      (and headers
7165           (vectorp headers)
7166           (mail-header-subject headers))))
7167
7168 (defmacro gnus-summary-article-score (&optional number)
7169   "Return current article score."
7170   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7171                   gnus-newsgroup-scored))
7172        gnus-summary-default-score 0))
7173
7174 (defun gnus-summary-article-children (&optional number)
7175   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7176          (level (gnus-data-level (car data)))
7177          l children)
7178     (while (and (setq data (cdr data))
7179                 (> (setq l (gnus-data-level (car data))) level))
7180       (and (= (1+ level) l)
7181            (setq children (cons (gnus-data-number (car data))
7182                                 children))))
7183     (nreverse children)))
7184
7185 (defun gnus-summary-article-parent (&optional number)
7186   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7187                                     (gnus-data-list t)))
7188          (level (gnus-data-level (car data)))
7189          l)
7190     (if (zerop level)
7191         () ; This is a root.
7192       ;; We search until we find an article with a level less than
7193       ;; this one.  That function has to be the parent.
7194       (while (and (setq data (cdr data))
7195                   (not (< (gnus-data-level (car data)) level))))
7196       (and data (gnus-data-number (car data))))))
7197
7198 (defun gnus-unread-mark-p (mark)
7199   "Say whether MARK is the unread mark."
7200   (= mark gnus-unread-mark))
7201
7202 (defun gnus-read-mark-p (mark)
7203   "Say whether MARK is one of the marks that mark as read.
7204 This is all marks except unread, ticked, dormant, and expirable."
7205   (not (or (= mark gnus-unread-mark)
7206            (= mark gnus-ticked-mark)
7207            (= mark gnus-dormant-mark)
7208            (= mark gnus-expirable-mark))))
7209
7210 ;; Various summary mode internalish functions.
7211
7212 (defun gnus-mouse-pick-article (e)
7213   (interactive "e")
7214   (mouse-set-point e)
7215   (gnus-summary-next-page nil t))
7216
7217 (defun gnus-summary-setup-buffer (group)
7218   "Initialize summary buffer."
7219   (let ((buffer (concat "*Summary " group "*")))
7220     (if (get-buffer buffer)
7221         (progn
7222           (set-buffer buffer)
7223           (setq gnus-summary-buffer (current-buffer))
7224           (not gnus-newsgroup-prepared))
7225       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7226       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7227       (gnus-add-current-to-buffer-list)
7228       (gnus-summary-mode group)
7229       (when gnus-carpal
7230         (gnus-carpal-setup-buffer 'summary))
7231       (unless gnus-single-article-buffer
7232         (make-local-variable 'gnus-article-buffer)
7233         (make-local-variable 'gnus-original-article-buffer))
7234       (setq gnus-newsgroup-name group)
7235       t)))
7236
7237 (defun gnus-set-global-variables ()
7238   ;; Set the global equivalents of the summary buffer-local variables
7239   ;; to the latest values they had.  These reflect the summary buffer
7240   ;; that was in action when the last article was fetched.
7241   (when (eq major-mode 'gnus-summary-mode)
7242     (setq gnus-summary-buffer (current-buffer))
7243     (let ((name gnus-newsgroup-name)
7244           (marked gnus-newsgroup-marked)
7245           (unread gnus-newsgroup-unreads)
7246           (headers gnus-current-headers)
7247           (data gnus-newsgroup-data)
7248           (article-buffer gnus-article-buffer)
7249           (original gnus-original-article-buffer)
7250           (score-file gnus-current-score-file))
7251       (save-excursion
7252         (set-buffer gnus-group-buffer)
7253         (setq gnus-newsgroup-name name)
7254         (setq gnus-newsgroup-marked marked)
7255         (setq gnus-newsgroup-unreads unread)
7256         (setq gnus-current-headers headers)
7257         (setq gnus-newsgroup-data data)
7258         (setq gnus-article-buffer article-buffer)
7259         (setq gnus-original-article-buffer original)
7260         (setq gnus-current-score-file score-file)))))
7261
7262 (defun gnus-summary-last-article-p (&optional article)
7263   "Return whether ARTICLE is the last article in the buffer."
7264   (if (not (setq article (or article (gnus-summary-article-number))))
7265       t ; All non-existant numbers are the last article. :-)
7266     (cdr (gnus-data-find-list article))))
7267
7268 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7269   "Insert a dummy root in the summary buffer."
7270   (beginning-of-line)
7271   (add-text-properties
7272    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7273    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7274
7275 (defvar gnus-thread-indent-array nil)
7276 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7277 (defun gnus-make-thread-indent-array ()
7278   (let ((n 200))
7279     (if (and gnus-thread-indent-array
7280              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7281         nil
7282       (setq gnus-thread-indent-array (make-vector 201 "")
7283             gnus-thread-indent-array-level gnus-thread-indent-level)
7284       (while (>= n 0)
7285         (aset gnus-thread-indent-array n
7286               (make-string (* n gnus-thread-indent-level) ? ))
7287         (setq n (1- n))))))
7288
7289 (defun gnus-summary-insert-line
7290   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7291                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7292                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7293   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7294          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7295          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7296          (gnus-tmp-score-char
7297           (if (or (null gnus-summary-default-score)
7298                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7299                       gnus-summary-zcore-fuzz)) ? 
7300             (if (< gnus-tmp-score gnus-summary-default-score)
7301                 gnus-score-below-mark gnus-score-over-mark)))
7302          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7303                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7304                                   gnus-cached-mark)
7305                                  (gnus-tmp-replied gnus-replied-mark)
7306                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7307                                   gnus-saved-mark)
7308                                  (t gnus-unread-mark)))
7309          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7310          (gnus-tmp-name
7311           (cond
7312            ((string-match "(.+)" gnus-tmp-from)
7313             (substring gnus-tmp-from
7314                        (1+ (match-beginning 0)) (1- (match-end 0))))
7315            ((string-match "<[^>]+> *$" gnus-tmp-from)
7316             (let ((beg (match-beginning 0)))
7317               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7318                        (substring gnus-tmp-from (1+ (match-beginning 0))
7319                                   (1- (match-end 0))))
7320                   (substring gnus-tmp-from 0 beg))))
7321            (t gnus-tmp-from)))
7322          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7323          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7324          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7325          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7326          (buffer-read-only nil))
7327     (when (string= gnus-tmp-name "")
7328       (setq gnus-tmp-name gnus-tmp-from))
7329     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7330     (put-text-property
7331      (point)
7332      (progn (eval gnus-summary-line-format-spec) (point))
7333      'gnus-number gnus-tmp-number)
7334     (when (gnus-visual-p 'summary-highlight 'highlight)
7335       (forward-line -1)
7336       (run-hooks 'gnus-summary-update-hook)
7337       (forward-line 1))))
7338
7339 (defun gnus-summary-update-line (&optional dont-update)
7340   ;; Update summary line after change.
7341   (when (and gnus-summary-default-score
7342              (not gnus-summary-inhibit-highlight))
7343     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7344            (article (gnus-summary-article-number))
7345            (score (gnus-summary-article-score article)))
7346       (unless dont-update
7347         (if (and gnus-summary-mark-below
7348                  (< (gnus-summary-article-score)
7349                     gnus-summary-mark-below))
7350             ;; This article has a low score, so we mark it as read.
7351             (when (memq article gnus-newsgroup-unreads)
7352               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7353           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7354             ;; This article was previously marked as read on account
7355             ;; of a low score, but now it has risen, so we mark it as
7356             ;; unread.
7357             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7358         (gnus-summary-update-mark
7359          (if (or (null gnus-summary-default-score)
7360                  (<= (abs (- score gnus-summary-default-score))
7361                      gnus-summary-zcore-fuzz)) ? 
7362            (if (< score gnus-summary-default-score)
7363                gnus-score-below-mark gnus-score-over-mark)) 'score))
7364       ;; Do visual highlighting.
7365       (when (gnus-visual-p 'summary-highlight 'highlight)
7366         (run-hooks 'gnus-summary-update-hook)))))
7367
7368 (defvar gnus-tmp-new-adopts nil)
7369
7370 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7371   ;; Sum up all elements (and sub-elements) in a list.
7372   (let* ((number
7373           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7374           (cond
7375            ((and (consp thread) (cdr thread))
7376             (apply
7377              '+ 1 (mapcar
7378                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7379            ((null thread)
7380             1)
7381            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7382             1)
7383            (t 1))))
7384     (when (and level (zerop level) gnus-tmp-new-adopts)
7385       (incf number
7386             (apply '+ (mapcar
7387                        'gnus-summary-number-of-articles-in-thread
7388                        gnus-tmp-new-adopts))))
7389     (if char
7390         (if (> number 1) gnus-not-empty-thread-mark
7391           gnus-empty-thread-mark)
7392       number)))
7393
7394 (defun gnus-summary-set-local-parameters (group)
7395  "Go through the local params of GROUP and set all variable specs in that list."
7396   (let ((params (gnus-info-params (gnus-get-info group)))
7397         elem)
7398     (while params
7399       (setq elem (car params)
7400             params (cdr params))
7401       (and (consp elem)                 ; Has to be a cons.
7402            (consp (cdr elem))           ; The cdr has to be a list.
7403            (symbolp (car elem))         ; Has to be a symbol in there.
7404            (not (memq (car elem) 
7405                       '(quit-config to-address to-list to-group)))
7406            (progn                       ; So we set it.
7407              (make-local-variable (car elem))
7408              (set (car elem) (eval (nth 1 elem))))))))
7409
7410 (defun gnus-summary-read-group
7411   (group &optional show-all no-article kill-buffer no-display)
7412   "Start reading news in newsgroup GROUP.
7413 If SHOW-ALL is non-nil, already read articles are also listed.
7414 If NO-ARTICLE is non-nil, no article is selected initially.
7415 If NO-DISPLAY, don't generate a summary buffer."
7416   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7417   (let* ((new-group (gnus-summary-setup-buffer group))
7418          (quit-config (gnus-group-quit-config group))
7419          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7420     (cond
7421      ;; This summary buffer exists already, so we just select it.
7422      ((not new-group)
7423       (gnus-set-global-variables)
7424       (when kill-buffer
7425         (gnus-kill-or-deaden-summary kill-buffer))
7426       (gnus-configure-windows 'summary 'force)
7427       (gnus-set-mode-line 'summary)
7428       (gnus-summary-position-point)
7429       (message "")
7430       t)
7431      ;; We couldn't select this group.
7432      ((null did-select)
7433       (when (and (eq major-mode 'gnus-summary-mode)
7434                  (not (equal (current-buffer) kill-buffer)))
7435         (kill-buffer (current-buffer))
7436         (if (not quit-config)
7437             (progn
7438               (set-buffer gnus-group-buffer)
7439               (gnus-group-jump-to-group group)
7440               (gnus-group-next-unread-group 1))
7441           (if (not (buffer-name (car quit-config)))
7442               (gnus-configure-windows 'group 'force)
7443             (set-buffer (car quit-config))
7444             (and (eq major-mode 'gnus-summary-mode)
7445                  (gnus-set-global-variables))
7446             (gnus-configure-windows (cdr quit-config)))))
7447       (gnus-message 3 "Can't select group")
7448       nil)
7449      ;; The user did a `C-g' while prompting for number of articles,
7450      ;; so we exit this group.
7451      ((eq did-select 'quit)
7452       (and (eq major-mode 'gnus-summary-mode)
7453            (not (equal (current-buffer) kill-buffer))
7454            (kill-buffer (current-buffer)))
7455       (when kill-buffer
7456         (gnus-kill-or-deaden-summary kill-buffer))
7457       (if (not quit-config)
7458           (progn
7459             (set-buffer gnus-group-buffer)
7460             (gnus-group-jump-to-group group)
7461             (gnus-group-next-unread-group 1)
7462             (gnus-configure-windows 'group 'force))
7463         (if (not (buffer-name (car quit-config)))
7464             (gnus-configure-windows 'group 'force)
7465           (set-buffer (car quit-config))
7466           (and (eq major-mode 'gnus-summary-mode)
7467                (gnus-set-global-variables))
7468           (gnus-configure-windows (cdr quit-config))))
7469       ;; Finally signal the quit.
7470       (signal 'quit nil))
7471      ;; The group was successfully selected.
7472      (t
7473       (gnus-set-global-variables)
7474       ;; Save the active value in effect when the group was entered.
7475       (setq gnus-newsgroup-active
7476             (gnus-copy-sequence
7477              (gnus-active gnus-newsgroup-name)))
7478       ;; You can change the summary buffer in some way with this hook.
7479       (run-hooks 'gnus-select-group-hook)
7480       ;; Set any local variables in the group parameters.
7481       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7482       (gnus-update-format-specifications)
7483       ;; Do score processing.
7484       (when gnus-use-scoring
7485         (gnus-possibly-score-headers))
7486       ;; Check whether to fill in the gaps in the threads.
7487       (when gnus-build-sparse-threads
7488         (gnus-build-sparse-threads))
7489       ;; Find the initial limit.
7490       (if show-all
7491           (let ((gnus-newsgroup-dormant nil))
7492             (gnus-summary-initial-limit show-all))
7493         (gnus-summary-initial-limit show-all))
7494       ;; Generate the summary buffer.
7495       (unless no-display
7496         (gnus-summary-prepare))
7497       (when gnus-use-trees
7498         (gnus-tree-open group)
7499         (setq gnus-summary-highlight-line-function
7500               'gnus-tree-highlight-article))
7501       ;; If the summary buffer is empty, but there are some low-scored
7502       ;; articles or some excluded dormants, we include these in the
7503       ;; buffer.
7504       (when (and (zerop (buffer-size))
7505                  (not no-display))
7506         (cond (gnus-newsgroup-dormant
7507                (gnus-summary-limit-include-dormant))
7508               ((and gnus-newsgroup-scored show-all)
7509                (gnus-summary-limit-include-expunged))))
7510       ;; Function `gnus-apply-kill-file' must be called in this hook.
7511       (run-hooks 'gnus-apply-kill-hook)
7512       (if (and (zerop (buffer-size))
7513                (not no-display))
7514           (progn
7515             ;; This newsgroup is empty.
7516             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7517             (gnus-message 6 "No unread news")
7518             (when kill-buffer
7519               (gnus-kill-or-deaden-summary kill-buffer))
7520             ;; Return nil from this function.
7521             nil)
7522         ;; Hide conversation thread subtrees.  We cannot do this in
7523         ;; gnus-summary-prepare-hook since kill processing may not
7524         ;; work with hidden articles.
7525         (and gnus-show-threads
7526              gnus-thread-hide-subtree
7527              (gnus-summary-hide-all-threads))
7528         ;; Show first unread article if requested.
7529         (if (and (not no-article)
7530                  (not no-display)
7531                  gnus-newsgroup-unreads
7532                  gnus-auto-select-first)
7533             (if (eq gnus-auto-select-first 'best)
7534                 (gnus-summary-best-unread-article)
7535               (gnus-summary-first-unread-article))
7536           ;; Don't select any articles, just move point to the first
7537           ;; article in the group.
7538           (goto-char (point-min))
7539           (gnus-summary-position-point)
7540           (gnus-set-mode-line 'summary)
7541           (gnus-configure-windows 'summary 'force))
7542         ;; If we are in async mode, we send some info to the backend.
7543         (when gnus-newsgroup-async
7544           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7545         (when kill-buffer
7546           (gnus-kill-or-deaden-summary kill-buffer))
7547         (when (get-buffer-window gnus-group-buffer t)
7548           ;; Gotta use windows, because recenter does wierd stuff if
7549           ;; the current buffer ain't the displayed window.
7550           (let ((owin (selected-window)))
7551             (select-window (get-buffer-window gnus-group-buffer t))
7552             (when (gnus-group-goto-group group)
7553               (recenter))
7554             (select-window owin))))
7555       ;; Mark this buffer as "prepared".
7556       (setq gnus-newsgroup-prepared t)
7557       t))))
7558
7559 (defun gnus-summary-prepare ()
7560   "Generate the summary buffer."
7561   (let ((buffer-read-only nil))
7562     (erase-buffer)
7563     (setq gnus-newsgroup-data nil
7564           gnus-newsgroup-data-reverse nil)
7565     (run-hooks 'gnus-summary-generate-hook)
7566     ;; Generate the buffer, either with threads or without.
7567     (when gnus-newsgroup-headers
7568       (gnus-summary-prepare-threads
7569        (if gnus-show-threads
7570            (gnus-sort-gathered-threads
7571             (funcall gnus-summary-thread-gathering-function
7572                      (gnus-sort-threads
7573                       (gnus-cut-threads (gnus-make-threads)))))
7574          ;; Unthreaded display.
7575          (gnus-sort-articles gnus-newsgroup-headers))))
7576     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7577     ;; Call hooks for modifying summary buffer.
7578     (goto-char (point-min))
7579     (run-hooks 'gnus-summary-prepare-hook)))
7580
7581 (defun gnus-gather-threads-by-subject (threads)
7582   "Gather threads by looking at Subject headers."
7583   (if (not gnus-summary-make-false-root)
7584       threads
7585     (let ((hashtb (gnus-make-hashtable 1023))
7586           (prev threads)
7587           (result threads)
7588           subject hthread whole-subject)
7589       (while threads
7590         (setq whole-subject (mail-header-subject (caar threads)))
7591         (if (and gnus-summary-gather-exclude-subject
7592                  (string-match gnus-summary-gather-exclude-subject
7593                                whole-subject))
7594             () ; We don't want to do anything with this article.
7595           ;; We simplify the subject before looking it up in the
7596           ;; hash table.
7597           (setq subject
7598                 (cond
7599                  ;; Truncate the subject.
7600                  ((numberp gnus-summary-gather-subject-limit)
7601                   (setq subject (gnus-simplify-subject-re whole-subject))
7602                   (if (> (length subject) gnus-summary-gather-subject-limit)
7603                       (substring subject 0 gnus-summary-gather-subject-limit)
7604                     subject))
7605                  ;; Fuzzily simplify it.
7606                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7607                   (gnus-simplify-subject-fuzzy whole-subject))
7608                  ;; Just remove the leading "Re:".
7609                  (t
7610                   (gnus-simplify-subject-re whole-subject))))
7611
7612           (if (setq hthread (gnus-gethash subject hashtb))
7613               (progn
7614                 ;; We enter a dummy root into the thread, if we
7615                 ;; haven't done that already.
7616                 (unless (stringp (caar hthread))
7617                   (setcar hthread (list whole-subject (car hthread))))
7618                 ;; We add this new gathered thread to this gathered
7619                 ;; thread.
7620                 (setcdr (car hthread)
7621                         (nconc (cdar hthread) (list (car threads))))
7622                 ;; Remove it from the list of threads.
7623                 (setcdr prev (cdr threads))
7624                 (setq threads prev))
7625             ;; Enter this thread into the hash table.
7626             (gnus-sethash subject threads hashtb)))
7627         (setq prev threads)
7628         (setq threads (cdr threads)))
7629       result)))
7630
7631 (defun gnus-summary-gather-threads-by-references (threads)
7632   "Gather threads by looking at References headers."
7633   (let ((idhashtb (gnus-make-hashtable 1023))
7634         (thhashtb (gnus-make-hashtable 1023))
7635         (prev threads)
7636         (result threads)
7637         ids references id gthread gid entered)
7638     (while threads
7639       (when (setq references (mail-header-references (caar threads)))
7640         (setq id (mail-header-id (caar threads)))
7641         (setq ids (gnus-split-references references))
7642         (setq entered nil)
7643         (while ids
7644           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7645               (progn
7646                 (gnus-sethash (car ids) id idhashtb)
7647                 (gnus-sethash id threads thhashtb))
7648             (setq gthread (gnus-gethash gid thhashtb))
7649             (unless entered
7650               ;; We enter a dummy root into the thread, if we
7651               ;; haven't done that already.
7652               (unless (stringp (caar gthread))
7653                 (setcar gthread (list (mail-header-subject (caar gthread))
7654                                       (car gthread))))
7655               ;; We add this new gathered thread to this gathered
7656               ;; thread.
7657               (setcdr (car gthread)
7658                       (nconc (cdar gthread) (list (car threads)))))
7659             ;; Add it into the thread hash table.
7660             (gnus-sethash id gthread thhashtb)
7661             (setq entered t)
7662             ;; Remove it from the list of threads.
7663             (setcdr prev (cdr threads))
7664             (setq threads prev))
7665           (setq ids (cdr ids))))
7666       (setq prev threads)
7667       (setq threads (cdr threads)))
7668     result))
7669
7670 (defun gnus-sort-gathered-threads (threads)
7671   "Sort subtreads inside each gathered thread by article number."
7672   (let ((result threads))
7673     (while threads
7674       (when (stringp (caar threads))
7675         (setcdr (car threads)
7676                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7677       (setq threads (cdr threads)))
7678     result))
7679
7680 (defun gnus-make-threads ()
7681   "Go through the dependency hashtb and find the roots.  Return all threads."
7682   (let (threads)
7683     (mapatoms
7684      (lambda (refs)
7685        (unless (car (symbol-value refs))
7686          ;; These threads do not refer back to any other articles,
7687          ;; so they're roots.
7688          (setq threads (append (cdr (symbol-value refs)) threads))))
7689      gnus-newsgroup-dependencies)
7690     threads))
7691
7692 (defun gnus-build-sparse-threads ()
7693   (let ((headers gnus-newsgroup-headers)
7694         (deps gnus-newsgroup-dependencies)
7695         header references generation relations 
7696         cthread subject child end pthread relation)
7697     ;; First we create an alist of generations/relations, where 
7698     ;; generations is how much we trust the ralation, and the relation
7699     ;; is parent/child.
7700     (gnus-message 7 "Making sparse threads...")
7701     (save-excursion
7702       (nnheader-set-temp-buffer " *gnus sparse threads*")
7703       (while (setq header (pop headers))
7704         (when (and (setq references (mail-header-references header))
7705                    (not (string= references "")))
7706           (insert references)
7707           (setq child (downcase (mail-header-id header))
7708                 subject (mail-header-subject header))
7709           (setq generation 0)
7710           (while (search-backward ">" nil t)
7711             (setq end (1+ (point)))
7712             (when (search-backward "<" nil t)
7713               (push (list (incf generation) 
7714                           child (setq child (downcase
7715                                              (buffer-substring (point) end)))
7716                           subject)
7717                     relations)))
7718           (push (list (1+ generation) child nil subject) relations)
7719           (erase-buffer)))
7720       (kill-buffer (current-buffer)))
7721     ;; Sort over trustworthiness.
7722     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7723     (while (setq relation (pop relations))
7724       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7725                 (unless (car (symbol-value cthread))
7726                   ;; Make this article the parent of these threads.
7727                   (setcar (symbol-value cthread)
7728                           (vector gnus-reffed-article-number 
7729                                   (cadddr relation) 
7730                                   "" ""
7731                                   (cadr relation) 
7732                                   (or (caddr relation) "") 0 0 "")))
7733               (set cthread (list (vector gnus-reffed-article-number
7734                                          (cadddr relation) 
7735                                          "" "" (cadr relation) 
7736                                          (or (caddr relation) "") 0 0 ""))))
7737         (push gnus-reffed-article-number gnus-newsgroup-limit)
7738         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7739         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7740               gnus-newsgroup-reads)
7741         (decf gnus-reffed-article-number)
7742         ;; Make this new thread the child of its parent.
7743         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7744             (setcdr (symbol-value pthread)
7745                     (nconc (cdr (symbol-value pthread))
7746                            (list (symbol-value cthread))))
7747           (set pthread (list nil (symbol-value cthread))))))
7748     (gnus-message 7 "Making sparse threads...done")))
7749
7750 (defun gnus-build-old-threads ()
7751   ;; Look at all the articles that refer back to old articles, and
7752   ;; fetch the headers for the articles that aren't there.  This will
7753   ;; build complete threads - if the roots haven't been expired by the
7754   ;; server, that is.
7755   (let (id heads)
7756     (mapatoms
7757      (lambda (refs)
7758        (when (not (car (symbol-value refs)))
7759          (setq heads (cdr (symbol-value refs)))
7760          (while heads
7761            (if (memq (mail-header-number (caar heads))
7762                      gnus-newsgroup-dormant)
7763                (setq heads (cdr heads))
7764              (setq id (symbol-name refs))
7765              (while (and (setq id (gnus-build-get-header id))
7766                          (not (car (gnus-gethash
7767                                     id gnus-newsgroup-dependencies)))))
7768              (setq heads nil)))))
7769      gnus-newsgroup-dependencies)))
7770
7771 (defun gnus-build-get-header (id)
7772   ;; Look through the buffer of NOV lines and find the header to
7773   ;; ID.  Enter this line into the dependencies hash table, and return
7774   ;; the id of the parent article (if any).
7775   (let ((deps gnus-newsgroup-dependencies)
7776         found header)
7777     (prog1
7778         (save-excursion
7779           (set-buffer nntp-server-buffer)
7780           (goto-char (point-min))
7781           (while (and (not found) (search-forward id nil t))
7782             (beginning-of-line)
7783             (setq found (looking-at
7784                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7785                                  (regexp-quote id))))
7786             (or found (beginning-of-line 2)))
7787           (when found
7788             (let (ref)
7789               (beginning-of-line)
7790               (and
7791                (setq header (gnus-nov-parse-line
7792                              (read (current-buffer)) deps))
7793                (gnus-parent-id (mail-header-references header))))))
7794       (when header
7795         (let ((number (mail-header-number header)))
7796           (push number gnus-newsgroup-limit)
7797           (push header gnus-newsgroup-headers)
7798           (if (memq number gnus-newsgroup-unselected)
7799               (progn
7800                 (push number gnus-newsgroup-unreads)
7801                 (setq gnus-newsgroup-unselected
7802                       (delq number gnus-newsgroup-unselected)))
7803             (push number gnus-newsgroup-ancient)))))))
7804
7805 (defun gnus-summary-update-article (article &optional header)
7806   "Update ARTICLE in the summary buffer."
7807   (let ((id (mail-header-id (gnus-summary-article-header article)))
7808         (data (gnus-data-find article)))
7809     (setcar (gnus-id-to-thread id) nil)
7810     (gnus-summary-insert-subject id)
7811     ;; Set the (possibly) new article number in the data structure.
7812     (gnus-data-set-number data (gnus-id-to-article id))))
7813
7814 (defun gnus-rebuild-thread (id)
7815   "Rebuild the thread containing ID."
7816   (let ((dep gnus-newsgroup-dependencies)
7817         (buffer-read-only nil)
7818         current headers refs thread art data)
7819     (if (not gnus-show-threads)
7820         (setq thread (list (car (gnus-id-to-thread id))))
7821       ;; Get the thread this article is part of.
7822       (setq thread (gnus-remove-thread id)))
7823     (setq current (save-excursion
7824                     (and (zerop (forward-line -1))
7825                          (gnus-summary-article-number))))
7826     ;; If this is a gathered thread, we have to go some re-gathering.
7827     (when (stringp (car thread))
7828       (let ((subject (car thread))
7829             roots thr)
7830         (setq thread (cdr thread))
7831         (while thread
7832           (unless (memq (setq thr (gnus-id-to-thread
7833                                       (gnus-root-id
7834                                        (mail-header-id (caar thread)))))
7835                         roots)
7836             (push thr roots))
7837           (setq thread (cdr thread)))
7838         ;; We now have all (unique) roots.
7839         (if (= (length roots) 1)
7840             ;; All the loose roots are now one solid root.
7841             (setq thread (car roots))
7842           (setq thread (cons subject (gnus-sort-threads roots))))))
7843     (let ((beg (point))
7844           threads)
7845       ;; We then insert this thread into the summary buffer.
7846       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7847         (gnus-summary-prepare-threads (list thread))
7848         (setq data (nreverse gnus-newsgroup-data))
7849         (setq threads gnus-newsgroup-threads))
7850       ;; We splice the new data into the data structure.
7851       (gnus-data-enter-list current data)
7852       (gnus-data-compute-positions)
7853       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7854
7855 (defun gnus-id-to-thread (id)
7856   "Return the (sub-)thread where ID appears."
7857   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7858
7859 (defun gnus-id-to-article (id)
7860   "Return the article number of ID."
7861   (let ((thread (gnus-id-to-thread id)))
7862     (when thread
7863       (mail-header-number (car thread)))))
7864
7865 (defun gnus-id-to-header (id)
7866   "Return the article headers of ID."
7867   (car (gnus-id-to-thread id)))
7868
7869 (defun gnus-article-displayed-root-p (article)
7870   "Say whether ARTICLE is a root(ish) article."
7871   (let ((level (gnus-summary-thread-level article))
7872         particle)
7873     (cond 
7874      ((null level) nil)
7875      ((zerop level) t)
7876      ((and (= 1 level)
7877            (null (setq particle (gnus-id-to-article
7878                                  (gnus-parent-id 
7879                                   (mail-header-references 
7880                                    (gnus-summary-article-header article))))))
7881            (null (gnus-summary-thread-level particle)))))))
7882
7883 (defun gnus-root-id (id)
7884   "Return the id of the root of the thread where ID appears."
7885   (let (last-id prev)
7886     (while (and id (setq prev (car (gnus-gethash
7887                                     (downcase id)
7888                                     gnus-newsgroup-dependencies))))
7889       (setq last-id id
7890             id (gnus-parent-id (mail-header-references prev))))
7891     last-id))
7892
7893 (defun gnus-remove-thread (id &optional dont-remove)
7894   "Remove the thread that has ID in it."
7895   (let ((dep gnus-newsgroup-dependencies)
7896         headers thread prev last-id)
7897     ;; First go up in this thread until we find the root.
7898     (setq last-id (gnus-root-id id))
7899     (setq headers (list (car (gnus-id-to-thread last-id))
7900                         (caadr (gnus-id-to-thread last-id))))
7901     ;; We have now found the real root of this thread.  It might have
7902     ;; been gathered into some loose thread, so we have to search
7903     ;; through the threads to find the thread we wanted.
7904     (let ((threads gnus-newsgroup-threads)
7905           sub)
7906       (while threads
7907         (setq sub (car threads))
7908         (if (stringp (car sub))
7909             ;; This is a gathered threads, so we look at the roots
7910             ;; below it to find whether this article in in this
7911             ;; gathered root.
7912             (progn
7913               (setq sub (cdr sub))
7914               (while sub
7915                 (when (member (caar sub) headers)
7916                   (setq thread (car threads)
7917                         threads nil
7918                         sub nil))
7919                 (setq sub (cdr sub))))
7920           ;; It's an ordinary thread, so we check it.
7921           (when (eq (car sub) (car headers))
7922             (setq thread sub
7923                   threads nil)))
7924         (setq threads (cdr threads)))
7925       ;; If this article is in no thread, then it's a root.
7926       (if thread
7927           (unless dont-remove
7928             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7929         (setq thread (gnus-gethash (downcase last-id) dep)))
7930       (when thread
7931         (prog1
7932             thread ; We return this thread.
7933           (unless dont-remove
7934             (if (stringp (car thread))
7935                 (progn
7936                   ;; If we use dummy roots, then we have to remove the
7937                   ;; dummy root as well.
7938                   (when (eq gnus-summary-make-false-root 'dummy)
7939                     ;; Uhm.
7940                     )
7941                   (setq thread (cdr thread))
7942                   (while thread
7943                     (gnus-remove-thread-1 (car thread))
7944                     (setq thread (cdr thread))))
7945               (gnus-remove-thread-1 thread))))))))
7946
7947 (defun gnus-remove-thread-1 (thread)
7948   "Remove the thread THREAD recursively."
7949   (let ((number (mail-header-number (car thread)))
7950         pos)
7951     (when (setq pos (text-property-any
7952                      (point-min) (point-max) 'gnus-number number))
7953       (goto-char pos)
7954       (gnus-delete-line)
7955       (gnus-data-remove number))
7956     (setq thread (cdr thread))
7957     (while thread
7958       (gnus-remove-thread-1 (car thread))
7959       (setq thread (cdr thread)))))
7960
7961 (defun gnus-sort-threads (threads)
7962   "Sort THREADS."
7963   (if (not gnus-thread-sort-functions)
7964       threads
7965     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7966                     (car gnus-thread-sort-functions)
7967                   `(lambda (t1 t2)
7968                      ,(gnus-make-sort-function 
7969                        (reverse gnus-thread-sort-functions))))))
7970       (gnus-message 7 "Sorting threads...")
7971       (prog1
7972           (sort threads func)
7973         (gnus-message 7 "Sorting threads...done")))))
7974
7975 (defun gnus-sort-articles (articles)
7976   "Sort ARTICLES."
7977   (when gnus-article-sort-functions
7978     (let ((func (if (= 1 (length gnus-article-sort-functions))
7979                     (car gnus-article-sort-functions)
7980                   `(lambda (t1 t2)
7981                      ,(gnus-make-sort-function 
7982                        (reverse gnus-article-sort-functions))))))
7983       (gnus-message 7 "Sorting articles...")
7984       (prog1
7985           (setq gnus-newsgroup-headers (sort articles func))
7986         (gnus-message 7 "Sorting articles...done")))))
7987
7988 (defun gnus-make-sort-function (funs)
7989   "Return a composite sort condition based on the functions in FUNC."
7990   (if (cdr funs)
7991       `(or (,(car funs) t1 t2)
7992            (and (not (,(car funs) t2 t1))
7993                 ,(gnus-make-sort-function (cdr funs))))
7994     `(,(car funs) t1 t2)))
7995                  
7996 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
7997 (defmacro gnus-thread-header (thread)
7998   ;; Return header of first article in THREAD.
7999   ;; Note that THREAD must never, ever be anything else than a variable -
8000   ;; using some other form will lead to serious barfage.
8001   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8002   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8003   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8004         (vector thread) 2))
8005
8006 (defsubst gnus-article-sort-by-number (h1 h2)
8007   "Sort articles by article number."
8008   (< (mail-header-number h1)
8009      (mail-header-number h2)))
8010
8011 (defun gnus-thread-sort-by-number (h1 h2)
8012   "Sort threads by root article number."
8013   (gnus-article-sort-by-number
8014    (gnus-thread-header h1) (gnus-thread-header h2)))
8015
8016 (defsubst gnus-article-sort-by-author (h1 h2)
8017   "Sort articles by root author."
8018   (string-lessp
8019    (let ((extract (funcall
8020                    gnus-extract-address-components
8021                    (mail-header-from h1))))
8022      (or (car extract) (cdr extract)))
8023    (let ((extract (funcall
8024                    gnus-extract-address-components
8025                    (mail-header-from h2))))
8026      (or (car extract) (cdr extract)))))
8027
8028 (defun gnus-thread-sort-by-author (h1 h2)
8029   "Sort threads by root author."
8030   (gnus-article-sort-by-author
8031    (gnus-thread-header h1)  (gnus-thread-header h2)))
8032
8033 (defsubst gnus-article-sort-by-subject (h1 h2)
8034   "Sort articles by root subject."
8035   (string-lessp
8036    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8037    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8038
8039 (defun gnus-thread-sort-by-subject (h1 h2)
8040   "Sort threads by root subject."
8041   (gnus-article-sort-by-subject
8042    (gnus-thread-header h1) (gnus-thread-header h2)))
8043
8044 (defsubst gnus-article-sort-by-date (h1 h2)
8045   "Sort articles by root article date."
8046   (string-lessp
8047    (gnus-sortable-date (mail-header-date h1))
8048    (gnus-sortable-date (mail-header-date h2))))
8049
8050 (defun gnus-thread-sort-by-date (h1 h2)
8051   "Sort threads by root article date."
8052   (gnus-article-sort-by-date
8053    (gnus-thread-header h1) (gnus-thread-header h2)))
8054
8055 (defsubst gnus-article-sort-by-score (h1 h2)
8056   "Sort articles by root article score.
8057 Unscored articles will be counted as having a score of zero."
8058   (> (or (cdr (assq (mail-header-number h1)
8059                     gnus-newsgroup-scored))
8060          gnus-summary-default-score 0)
8061      (or (cdr (assq (mail-header-number h2)
8062                     gnus-newsgroup-scored))
8063          gnus-summary-default-score 0)))
8064
8065 (defun gnus-thread-sort-by-score (h1 h2)
8066   "Sort threads by root article score."
8067   (gnus-article-sort-by-score
8068    (gnus-thread-header h1) (gnus-thread-header h2)))
8069
8070 (defun gnus-thread-sort-by-total-score (h1 h2)
8071   "Sort threads by the sum of all scores in the thread.
8072 Unscored articles will be counted as having a score of zero."
8073   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8074
8075 (defun gnus-thread-total-score (thread)
8076   ;;  This function find the total score of THREAD.
8077   (if (consp thread)
8078       (if (stringp (car thread))
8079           (apply gnus-thread-score-function 0
8080                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8081         (gnus-thread-total-score-1 thread))
8082     (gnus-thread-total-score-1 (list thread))))
8083
8084 (defun gnus-thread-total-score-1 (root)
8085   ;; This function find the total score of the thread below ROOT.
8086   (setq root (car root))
8087   (apply gnus-thread-score-function
8088          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8089              gnus-summary-default-score 0)
8090          (mapcar 'gnus-thread-total-score
8091                  (cdr (gnus-gethash (downcase (mail-header-id root))
8092                                     gnus-newsgroup-dependencies)))))
8093
8094 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8095 (defvar gnus-tmp-prev-subject nil)
8096 (defvar gnus-tmp-false-parent nil)
8097 (defvar gnus-tmp-root-expunged nil)
8098 (defvar gnus-tmp-dummy-line nil)
8099
8100 (defun gnus-summary-prepare-threads (threads)
8101   "Prepare summary buffer from THREADS and indentation LEVEL.
8102 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8103 or a straight list of headers."
8104   (gnus-message 7 "Generating summary...")
8105
8106   (setq gnus-newsgroup-threads threads)
8107   (beginning-of-line)
8108
8109   (let ((gnus-tmp-level 0)
8110         (default-score (or gnus-summary-default-score 0))
8111         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8112         thread number subject stack state gnus-tmp-gathered beg-match
8113         new-roots gnus-tmp-new-adopts thread-end
8114         gnus-tmp-header gnus-tmp-unread
8115         gnus-tmp-replied gnus-tmp-subject-or-nil
8116         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8117         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8118         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8119
8120     (setq gnus-tmp-prev-subject nil)
8121
8122     (if (vectorp (car threads))
8123         ;; If this is a straight (sic) list of headers, then a
8124         ;; threaded summary display isn't required, so we just create
8125         ;; an unthreaded one.
8126         (gnus-summary-prepare-unthreaded threads)
8127
8128       ;; Do the threaded display.
8129
8130       (while (or threads stack gnus-tmp-new-adopts new-roots)
8131
8132         (if (and (= gnus-tmp-level 0)
8133                  (not (setq gnus-tmp-dummy-line nil))
8134                  (or (not stack)
8135                      (= (caar stack) 0))
8136                  (not gnus-tmp-false-parent)
8137                  (or gnus-tmp-new-adopts new-roots))
8138             (if gnus-tmp-new-adopts
8139                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8140                       thread (list (car gnus-tmp-new-adopts))
8141                       gnus-tmp-header (caar thread)
8142                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8143               (if new-roots
8144                   (setq thread (list (car new-roots))
8145                         gnus-tmp-header (caar thread)
8146                         new-roots (cdr new-roots))))
8147
8148           (if threads
8149               ;; If there are some threads, we do them before the
8150               ;; threads on the stack.
8151               (setq thread threads
8152                     gnus-tmp-header (caar thread))
8153             ;; There were no current threads, so we pop something off
8154             ;; the stack.
8155             (setq state (car stack)
8156                   gnus-tmp-level (car state)
8157                   thread (cdr state)
8158                   stack (cdr stack)
8159                   gnus-tmp-header (caar thread))))
8160
8161         (setq gnus-tmp-false-parent nil)
8162         (setq gnus-tmp-root-expunged nil)
8163         (setq thread-end nil)
8164
8165         (if (stringp gnus-tmp-header)
8166             ;; The header is a dummy root.
8167             (cond
8168              ((eq gnus-summary-make-false-root 'adopt)
8169               ;; We let the first article adopt the rest.
8170               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8171                                                (cddar thread)))
8172               (setq gnus-tmp-gathered
8173                     (nconc (mapcar
8174                             (lambda (h) (mail-header-number (car h)))
8175                             (cddar thread))
8176                            gnus-tmp-gathered))
8177               (setq thread (cons (list (caar thread)
8178                                        (cadar thread))
8179                                  (cdr thread)))
8180               (setq gnus-tmp-level -1
8181                     gnus-tmp-false-parent t))
8182              ((eq gnus-summary-make-false-root 'empty)
8183               ;; We print adopted articles with empty subject fields.
8184               (setq gnus-tmp-gathered
8185                     (nconc (mapcar
8186                             (lambda (h) (mail-header-number (car h)))
8187                             (cddar thread))
8188                            gnus-tmp-gathered))
8189               (setq gnus-tmp-level -1))
8190              ((eq gnus-summary-make-false-root 'dummy)
8191               ;; We remember that we probably want to output a dummy
8192               ;; root.
8193               (setq gnus-tmp-dummy-line gnus-tmp-header)
8194               (setq gnus-tmp-prev-subject gnus-tmp-header))
8195              (t
8196               ;; We do not make a root for the gathered
8197               ;; sub-threads at all.
8198               (setq gnus-tmp-level -1)))
8199
8200           (setq number (mail-header-number gnus-tmp-header)
8201                 subject (mail-header-subject gnus-tmp-header))
8202
8203           (cond
8204            ;; If the thread has changed subject, we might want to make
8205            ;; this subthread into a root.
8206            ((and (null gnus-thread-ignore-subject)
8207                  (not (zerop gnus-tmp-level))
8208                  gnus-tmp-prev-subject
8209                  (not (inline
8210                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8211             (setq new-roots (nconc new-roots (list (car thread)))
8212                   thread-end t
8213                   gnus-tmp-header nil))
8214            ;; If the article lies outside the current limit,
8215            ;; then we do not display it.
8216            ((and (not (memq number gnus-newsgroup-limit))
8217                  (not gnus-tmp-dummy-line))
8218             (setq gnus-tmp-gathered
8219                   (nconc (mapcar
8220                           (lambda (h) (mail-header-number (car h)))
8221                           (cdar thread))
8222                          gnus-tmp-gathered))
8223             (setq gnus-tmp-new-adopts (if (cdar thread)
8224                                           (append gnus-tmp-new-adopts
8225                                                   (cdar thread))
8226                                         gnus-tmp-new-adopts)
8227                   thread-end t
8228                   gnus-tmp-header nil)
8229             (when (zerop gnus-tmp-level)
8230               (setq gnus-tmp-root-expunged t)))
8231            ;; Perhaps this article is to be marked as read?
8232            ((and gnus-summary-mark-below
8233                  (< (or (cdr (assq number gnus-newsgroup-scored))
8234                         default-score)
8235                     gnus-summary-mark-below)
8236                  ;; Don't touch sparse articles.
8237                  (not (memq number gnus-newsgroup-sparse)))
8238             (setq gnus-newsgroup-unreads
8239                   (delq number gnus-newsgroup-unreads))
8240             (if gnus-newsgroup-auto-expire
8241                 (push number gnus-newsgroup-expirable)
8242               (push (cons number gnus-low-score-mark)
8243                     gnus-newsgroup-reads))))
8244
8245           (when gnus-tmp-header
8246             ;; We may have an old dummy line to output before this
8247             ;; article.
8248             (when gnus-tmp-dummy-line
8249               (gnus-summary-insert-dummy-line
8250                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8251               (setq gnus-tmp-dummy-line nil))
8252
8253             ;; Compute the mark.
8254             (setq
8255              gnus-tmp-unread
8256              (cond
8257               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8258               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8259               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8260               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8261               (t (or (cdr (assq number gnus-newsgroup-reads))
8262                      gnus-ancient-mark))))
8263
8264             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8265                                   gnus-tmp-header gnus-tmp-level)
8266                   gnus-newsgroup-data)
8267
8268             ;; Actually insert the line.
8269             (setq
8270              gnus-tmp-subject-or-nil
8271              (cond
8272               ((and gnus-thread-ignore-subject
8273                     gnus-tmp-prev-subject
8274                     (not (inline (gnus-subject-equal
8275                                   gnus-tmp-prev-subject subject))))
8276                subject)
8277               ((zerop gnus-tmp-level)
8278                (if (and (eq gnus-summary-make-false-root 'empty)
8279                         (memq number gnus-tmp-gathered)
8280                         gnus-tmp-prev-subject
8281                         (inline (gnus-subject-equal
8282                                  gnus-tmp-prev-subject subject)))
8283                    gnus-summary-same-subject
8284                  subject))
8285               (t gnus-summary-same-subject)))
8286             (if (and (eq gnus-summary-make-false-root 'adopt)
8287                      (= gnus-tmp-level 1)
8288                      (memq number gnus-tmp-gathered))
8289                 (setq gnus-tmp-opening-bracket ?\<
8290                       gnus-tmp-closing-bracket ?\>)
8291               (setq gnus-tmp-opening-bracket ?\[
8292                     gnus-tmp-closing-bracket ?\]))
8293             (setq
8294              gnus-tmp-indentation
8295              (aref gnus-thread-indent-array gnus-tmp-level)
8296              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8297              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8298                                 gnus-summary-default-score 0)
8299              gnus-tmp-score-char
8300              (if (or (null gnus-summary-default-score)
8301                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8302                          gnus-summary-zcore-fuzz)) ? 
8303                (if (< gnus-tmp-score gnus-summary-default-score)
8304                    gnus-score-below-mark gnus-score-over-mark))
8305              gnus-tmp-replied
8306              (cond ((memq number gnus-newsgroup-processable)
8307                     gnus-process-mark)
8308                    ((memq number gnus-newsgroup-cached)
8309                     gnus-cached-mark)
8310                    ((memq number gnus-newsgroup-replied)
8311                     gnus-replied-mark)
8312                    (t gnus-unread-mark))
8313              gnus-tmp-from (mail-header-from gnus-tmp-header)
8314              gnus-tmp-name
8315              (cond
8316               ((string-match "(.+)" gnus-tmp-from)
8317                (substring gnus-tmp-from
8318                           (1+ (match-beginning 0)) (1- (match-end 0))))
8319               ((string-match "<[^>]+> *$" gnus-tmp-from)
8320                (setq beg-match (match-beginning 0))
8321                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8322                         (substring gnus-tmp-from (1+ (match-beginning 0))
8323                                    (1- (match-end 0))))
8324                    (substring gnus-tmp-from 0 beg-match)))
8325               (t gnus-tmp-from)))
8326             (when (string= gnus-tmp-name "")
8327               (setq gnus-tmp-name gnus-tmp-from))
8328             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8329             (put-text-property
8330              (point)
8331              (progn (eval gnus-summary-line-format-spec) (point))
8332              'gnus-number number)
8333             (when gnus-visual-p
8334               (forward-line -1)
8335               (run-hooks 'gnus-summary-update-hook)
8336               (forward-line 1))
8337
8338             (setq gnus-tmp-prev-subject subject)))
8339
8340         (when (nth 1 thread)
8341           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8342         (incf gnus-tmp-level)
8343         (setq threads (if thread-end nil (cdar thread)))
8344         (unless threads
8345           (setq gnus-tmp-level 0)))))
8346   (gnus-message 7 "Generating summary...done"))
8347
8348 (defun gnus-summary-prepare-unthreaded (headers)
8349   "Generate an unthreaded summary buffer based on HEADERS."
8350   (let (header number mark)
8351
8352     (while headers
8353       (setq header (car headers)
8354             headers (cdr headers)
8355             number (mail-header-number header))
8356
8357       ;; We may have to root out some bad articles...
8358       (when (memq number gnus-newsgroup-limit)
8359         (when (and gnus-summary-mark-below
8360                    (< (or (cdr (assq number gnus-newsgroup-scored))
8361                           gnus-summary-default-score 0)
8362                       gnus-summary-mark-below))
8363           (setq gnus-newsgroup-unreads
8364                 (delq number gnus-newsgroup-unreads))
8365           (if gnus-newsgroup-auto-expire
8366               (push number gnus-newsgroup-expirable)
8367             (push (cons number gnus-low-score-mark)
8368                   gnus-newsgroup-reads)))
8369
8370         (setq mark
8371               (cond
8372                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8373                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8374                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8375                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8376                (t (or (cdr (assq number gnus-newsgroup-reads))
8377                       gnus-ancient-mark))))
8378         (setq gnus-newsgroup-data
8379               (cons (gnus-data-make number mark (1+ (point)) header 0)
8380                     gnus-newsgroup-data))
8381         (gnus-summary-insert-line
8382          header 0 nil mark (memq number gnus-newsgroup-replied)
8383          (memq number gnus-newsgroup-expirable)
8384          (mail-header-subject header) nil
8385          (cdr (assq number gnus-newsgroup-scored))
8386          (memq number gnus-newsgroup-processable))))))
8387
8388 (defun gnus-select-newsgroup (group &optional read-all)
8389   "Select newsgroup GROUP.
8390 If READ-ALL is non-nil, all articles in the group are selected."
8391   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8392          (info (nth 2 entry))
8393          articles fetched-articles cached)
8394
8395     (or (gnus-check-server
8396          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8397         (error "Couldn't open server"))
8398
8399     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8400         (gnus-activate-group group) ; Or we can activate it...
8401         (progn ; Or we bug out.
8402           (kill-buffer (current-buffer))
8403           (error "Couldn't request group %s: %s"
8404                  group (gnus-status-message group))))
8405
8406     (setq gnus-newsgroup-name group)
8407     (setq gnus-newsgroup-unselected nil)
8408     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8409
8410     (and gnus-asynchronous
8411          (gnus-check-backend-function
8412           'request-asynchronous gnus-newsgroup-name)
8413          (setq gnus-newsgroup-async
8414                (gnus-request-asynchronous gnus-newsgroup-name)))
8415
8416     ;; Adjust and set lists of article marks.
8417     (when info
8418       (gnus-adjust-marked-articles info))
8419
8420     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8421     (when (gnus-virtual-group-p group)
8422       (setq cached gnus-newsgroup-cached))
8423
8424     (setq gnus-newsgroup-unreads
8425           (gnus-set-difference
8426            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8427            gnus-newsgroup-dormant))
8428
8429     (setq gnus-newsgroup-processable nil)
8430
8431     (setq articles (gnus-articles-to-read group read-all))
8432
8433     (cond
8434      ((null articles)
8435       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8436       'quit)
8437      ((eq articles 0) nil)
8438      (t
8439       ;; Init the dependencies hash table.
8440       (setq gnus-newsgroup-dependencies
8441             (gnus-make-hashtable (length articles)))
8442       ;; Retrieve the headers and read them in.
8443       (gnus-message 5 "Fetching headers...")
8444       (setq gnus-newsgroup-headers
8445             (if (eq 'nov
8446                     (setq gnus-headers-retrieved-by
8447                           (gnus-retrieve-headers
8448                            articles gnus-newsgroup-name
8449                            ;; We might want to fetch old headers, but
8450                            ;; not if there is only 1 article.
8451                            (and gnus-fetch-old-headers
8452                                 (or (and
8453                                      (not (eq gnus-fetch-old-headers 'some))
8454                                      (not (numberp gnus-fetch-old-headers)))
8455                                     (> (length articles) 1))))))
8456                 (gnus-get-newsgroup-headers-xover articles)
8457               (gnus-get-newsgroup-headers)))
8458       (gnus-message 5 "Fetching headers...done")
8459
8460       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8461       (when cached
8462         (setq gnus-newsgroup-cached cached))
8463
8464       ;; Set the initial limit.
8465       (setq gnus-newsgroup-limit (copy-sequence articles))
8466       ;; Remove canceled articles from the list of unread articles.
8467       (setq gnus-newsgroup-unreads
8468             (gnus-set-sorted-intersection
8469              gnus-newsgroup-unreads
8470              (setq fetched-articles
8471                    (mapcar (lambda (headers) (mail-header-number headers))
8472                            gnus-newsgroup-headers))))
8473       ;; Removed marked articles that do not exist.
8474       (gnus-update-missing-marks
8475        (gnus-sorted-complement fetched-articles articles))
8476       ;; We might want to build some more threads first.
8477       (and gnus-fetch-old-headers
8478            (eq gnus-headers-retrieved-by 'nov)
8479            (gnus-build-old-threads))
8480       ;; Check whether auto-expire is to be done in this group.
8481       (setq gnus-newsgroup-auto-expire
8482             (gnus-group-auto-expirable-p group))
8483       ;; First and last article in this newsgroup.
8484       (and gnus-newsgroup-headers
8485            (setq gnus-newsgroup-begin
8486                  (mail-header-number (car gnus-newsgroup-headers)))
8487            (setq gnus-newsgroup-end
8488                  (mail-header-number
8489                   (gnus-last-element gnus-newsgroup-headers))))
8490       (setq gnus-reffed-article-number -1)
8491       ;; GROUP is successfully selected.
8492       (or gnus-newsgroup-headers t)))))
8493
8494 (defun gnus-articles-to-read (group read-all)
8495   ;; Find out what articles the user wants to read.
8496   (let* ((articles
8497           ;; Select all articles if `read-all' is non-nil, or if there
8498           ;; are no unread articles.
8499           (if (or read-all
8500                   (and (zerop (length gnus-newsgroup-marked))
8501                        (zerop (length gnus-newsgroup-unreads))))
8502               (gnus-uncompress-range (gnus-active group))
8503             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8504                           (copy-sequence gnus-newsgroup-unreads))
8505                   '<)))
8506          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8507          (scored (length scored-list))
8508          (number (length articles))
8509          (marked (+ (length gnus-newsgroup-marked)
8510                     (length gnus-newsgroup-dormant)))
8511          (select
8512           (cond
8513            ((numberp read-all)
8514             read-all)
8515            (t
8516             (condition-case ()
8517                 (cond
8518                  ((and (or (<= scored marked) (= scored number))
8519                        (numberp gnus-large-newsgroup)
8520                        (> number gnus-large-newsgroup))
8521                   (let ((input
8522                          (read-string
8523                           (format
8524                            "How many articles from %s (default %d): "
8525                            gnus-newsgroup-name number))))
8526                     (if (string-match "^[ \t]*$" input) number input)))
8527                  ((and (> scored marked) (< scored number))
8528                   (let ((input
8529                          (read-string
8530                           (format "%s %s (%d scored, %d total): "
8531                                   "How many articles from"
8532                                   group scored number))))
8533                     (if (string-match "^[ \t]*$" input)
8534                         number input)))
8535                  (t number))
8536               (quit nil))))))
8537     (setq select (if (stringp select) (string-to-number select) select))
8538     (if (or (null select) (zerop select))
8539         select
8540       (if (and (not (zerop scored)) (<= (abs select) scored))
8541           (progn
8542             (setq articles (sort scored-list '<))
8543             (setq number (length articles)))
8544         (setq articles (copy-sequence articles)))
8545
8546       (if (< (abs select) number)
8547           (if (< select 0)
8548               ;; Select the N oldest articles.
8549               (setcdr (nthcdr (1- (abs select)) articles) nil)
8550             ;; Select the N most recent articles.
8551             (setq articles (nthcdr (- number select) articles))))
8552       (setq gnus-newsgroup-unselected
8553             (gnus-sorted-intersection
8554              gnus-newsgroup-unreads
8555              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8556       articles)))
8557
8558 (defun gnus-killed-articles (killed articles)
8559   (let (out)
8560     (while articles
8561       (if (inline (gnus-member-of-range (car articles) killed))
8562           (setq out (cons (car articles) out)))
8563       (setq articles (cdr articles)))
8564     out))
8565
8566 (defun gnus-uncompress-marks (marks)
8567   "Uncompress the mark ranges in MARKS."
8568   (let ((uncompressed '(score bookmark))
8569         out)
8570     (while marks
8571       (if (memq (caar marks) uncompressed)
8572           (push (car marks) out)
8573         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8574       (setq marks (cdr marks)))
8575     out))
8576
8577 (defun gnus-adjust-marked-articles (info)
8578   "Set all article lists and remove all marks that are no longer legal."
8579   (let* ((marked-lists (gnus-info-marks info))
8580          (active (gnus-active (gnus-info-group info)))
8581          (min (car active))
8582          (max (cdr active))
8583          (types gnus-article-mark-lists)
8584          (uncompressed '(score bookmark))
8585          marks var articles article mark)
8586
8587     (while marked-lists
8588       (setq marks (pop marked-lists))
8589       (set (setq var (intern (format "gnus-newsgroup-%s"
8590                                      (car (rassq (setq mark (car marks))
8591                                                  types)))))
8592            (if (memq (car marks) uncompressed) (cdr marks)
8593              (gnus-uncompress-range (cdr marks))))
8594
8595       (setq articles (symbol-value var))
8596
8597       ;; All articles have to be subsets of the active articles.
8598       (cond
8599        ;; Adjust "simple" lists.
8600        ((memq mark '(tick dormant expirable reply killed save))
8601         (while articles
8602           (when (or (< (setq article (pop articles)) min) (> article max))
8603             (set var (delq article (symbol-value var))))))
8604        ;; Adjust assocs.
8605        ((memq mark '(score bookmark))
8606         (while articles
8607           (when (or (< (car (setq article (pop articles))) min)
8608                     (> (car article) max))
8609             (set var (delq article (symbol-value var))))))))))
8610
8611 (defun gnus-update-missing-marks (missing)
8612   "Go through the list of MISSING articles and remove them mark lists."
8613   (when missing
8614     (let ((types gnus-article-mark-lists)
8615           var m)
8616       ;; Go through all types.
8617       (while types
8618         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8619         (when (symbol-value var)
8620           ;; This list has articles.  So we delete all missing articles
8621           ;; from it.
8622           (setq m missing)
8623           (while m
8624             (set var (delq (pop m) (symbol-value var)))))))))
8625
8626 (defun gnus-update-marks ()
8627   "Enter the various lists of marked articles into the newsgroup info list."
8628   (let ((types gnus-article-mark-lists)
8629         (info (gnus-get-info gnus-newsgroup-name))
8630         (uncompressed '(score bookmark killed))
8631         var type list newmarked symbol)
8632     (when info
8633       ;; Add all marks lists that are non-nil to the list of marks lists.
8634       (while types
8635         (setq type (pop types))
8636         (when (setq list (symbol-value
8637                           (setq symbol
8638                                 (intern (format "gnus-newsgroup-%s"
8639                                                 (car type))))))
8640           (push (cons (cdr type)
8641                       (if (memq (cdr type) uncompressed) list
8642                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8643                 newmarked)))
8644
8645       ;; Enter these new marks into the info of the group.
8646       (if (nthcdr 3 info)
8647           (setcar (nthcdr 3 info) newmarked)
8648         ;; Add the marks lists to the end of the info.
8649         (when newmarked
8650           (setcdr (nthcdr 2 info) (list newmarked))))
8651
8652       ;; Cut off the end of the info if there's nothing else there.
8653       (let ((i 5))
8654         (while (and (> i 2)
8655                     (not (nth i info)))
8656           (when (nthcdr (decf i) info)
8657             (setcdr (nthcdr i info) nil)))))))
8658
8659 (defun gnus-add-marked-articles (group type articles &optional info force)
8660   ;; Add ARTICLES of TYPE to the info of GROUP.
8661   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8662   ;; add, but replace marked articles of TYPE with ARTICLES.
8663   (let ((info (or info (gnus-get-info group)))
8664         (uncompressed '(score bookmark killed))
8665         marked m)
8666     (or (not info)
8667         (and (not (setq marked (nthcdr 3 info)))
8668              (or (null articles)
8669                  (setcdr (nthcdr 2 info)
8670                          (list (list (cons type (gnus-compress-sequence
8671                                                  articles t)))))))
8672         (and (not (setq m (assq type (car marked))))
8673              (or (null articles)
8674                  (setcar marked
8675                          (cons (cons type (gnus-compress-sequence articles t) )
8676                                (car marked)))))
8677         (if force
8678             (if (null articles)
8679                 (setcar (nthcdr 3 info)
8680                         (delq (assq type (car marked)) (car marked)))
8681               (setcdr m (gnus-compress-sequence articles t)))
8682           (setcdr m (gnus-compress-sequence
8683                      (sort (nconc (gnus-uncompress-range m)
8684                                   (copy-sequence articles)) '<) t))))))
8685
8686 (defun gnus-set-mode-line (where)
8687   "This function sets the mode line of the article or summary buffers.
8688 If WHERE is `summary', the summary mode line format will be used."
8689   ;; Is this mode line one we keep updated?
8690   (when (memq where gnus-updated-mode-lines)
8691     (let (mode-string)
8692       (save-excursion
8693         ;; We evaluate this in the summary buffer since these
8694         ;; variables are buffer-local to that buffer.
8695         (set-buffer gnus-summary-buffer)
8696         ;; We bind all these variables that are used in the `eval' form
8697         ;; below.
8698         (let* ((mformat (symbol-value
8699                          (intern
8700                           (format "gnus-%s-mode-line-format-spec" where))))
8701                (gnus-tmp-group-name gnus-newsgroup-name)
8702                (gnus-tmp-article-number (or gnus-current-article 0))
8703                (gnus-tmp-unread gnus-newsgroup-unreads)
8704                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8705                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8706                (gnus-tmp-unread-and-unselected
8707                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8708                             (zerop gnus-tmp-unselected)) "")
8709                       ((zerop gnus-tmp-unselected)
8710                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8711                       (t (format "{%d(+%d) more}"
8712                                  gnus-tmp-unread-and-unticked
8713                                  gnus-tmp-unselected))))
8714                (gnus-tmp-subject
8715                 (if (and gnus-current-headers
8716                          (vectorp gnus-current-headers))
8717                     (mail-header-subject gnus-current-headers) ""))
8718                max-len
8719                gnus-tmp-header);; passed as argument to any user-format-funcs
8720           (setq mode-string (eval mformat))
8721           (setq max-len (max 4 (if gnus-mode-non-string-length
8722                                    (- (frame-width)
8723                                       gnus-mode-non-string-length)
8724                                  (length mode-string))))
8725           ;; We might have to chop a bit of the string off...
8726           (when (> (length mode-string) max-len)
8727             (setq mode-string
8728                   (concat (gnus-truncate-string mode-string (- max-len 3))
8729                           "...")))
8730           ;; Pad the mode string a bit.
8731           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8732       ;; Update the mode line.
8733       (setq mode-line-buffer-identification (list mode-string))
8734       (set-buffer-modified-p t))))
8735
8736 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8737   "Go through the HEADERS list and add all Xrefs to a hash table.
8738 The resulting hash table is returned, or nil if no Xrefs were found."
8739   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8740          (virtual (gnus-virtual-group-p from-newsgroup))
8741          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8742          (xref-hashtb (make-vector 63 0))
8743          start group entry number xrefs header)
8744     (while headers
8745       (setq header (pop headers))
8746       (when (and (setq xrefs (mail-header-xref header))
8747                  (not (memq (setq number (mail-header-number header))
8748                             unreads)))
8749         (setq start 0)
8750         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8751           (setq start (match-end 0))
8752           (setq group (concat prefix (substring xrefs (match-beginning 1)
8753                                                 (match-end 1))))
8754           (setq number
8755                 (string-to-int (substring xrefs (match-beginning 2)
8756                                           (match-end 2))))
8757           (if (setq entry (gnus-gethash group xref-hashtb))
8758               (setcdr entry (cons number (cdr entry)))
8759             (gnus-sethash group (cons number nil) xref-hashtb)))))
8760     (and start xref-hashtb)))
8761
8762 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8763   "Look through all the headers and mark the Xrefs as read."
8764   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8765         name entry info xref-hashtb idlist method nth4)
8766     (save-excursion
8767       (set-buffer gnus-group-buffer)
8768       (when (setq xref-hashtb
8769                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8770         (mapatoms
8771          (lambda (group)
8772            (unless (string= from-newsgroup (setq name (symbol-name group)))
8773              (setq idlist (symbol-value group))
8774              ;; Dead groups are not updated.
8775              (and (prog1
8776                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8777                             info (nth 2 entry))
8778                     (if (stringp (setq nth4 (gnus-info-method info)))
8779                         (setq nth4 (gnus-server-to-method nth4))))
8780                   ;; Only do the xrefs if the group has the same
8781                   ;; select method as the group we have just read.
8782                   (or (gnus-methods-equal-p
8783                        nth4 (gnus-find-method-for-group from-newsgroup))
8784                       virtual
8785                       (equal nth4 (setq method (gnus-find-method-for-group
8786                                                 from-newsgroup)))
8787                       (and (equal (car nth4) (car method))
8788                            (equal (nth 1 nth4) (nth 1 method))))
8789                   gnus-use-cross-reference
8790                   (or (not (eq gnus-use-cross-reference t))
8791                       virtual
8792                       ;; Only do cross-references on subscribed
8793                       ;; groups, if that is what is wanted.
8794                       (<= (gnus-info-level info) gnus-level-subscribed))
8795                   (gnus-group-make-articles-read name idlist))))
8796          xref-hashtb)))))
8797
8798 (defun gnus-group-make-articles-read (group articles)
8799   (let* ((num 0)
8800          (entry (gnus-gethash group gnus-newsrc-hashtb))
8801          (info (nth 2 entry))
8802          (active (gnus-active group))
8803          range)
8804     ;; First peel off all illegal article numbers.
8805     (if active
8806         (let ((ids articles)
8807               id first)
8808           (while ids
8809             (setq id (car ids))
8810             (if (and first (> id (cdr active)))
8811                 (progn
8812                   ;; We'll end up in this situation in one particular
8813                   ;; obscure situation.  If you re-scan a group and get
8814                   ;; a new article that is cross-posted to a different
8815                   ;; group that has not been re-scanned, you might get
8816                   ;; crossposted article that has a higher number than
8817                   ;; Gnus believes possible.  So we re-activate this
8818                   ;; group as well.  This might mean doing the
8819                   ;; crossposting thingy will *increase* the number
8820                   ;; of articles in some groups.  Tsk, tsk.
8821                   (setq active (or (gnus-activate-group group) active))))
8822             (if (or (> id (cdr active))
8823                     (< id (car active)))
8824                 (setq articles (delq id articles)))
8825             (setq ids (cdr ids)))))
8826     ;; If the read list is nil, we init it.
8827     (and active
8828          (null (gnus-info-read info))
8829          (> (car active) 1)
8830          (gnus-info-set-read info (cons 1 (1- (car active)))))
8831     ;; Then we add the read articles to the range.
8832     (gnus-info-set-read
8833      info
8834      (setq range
8835            (gnus-add-to-range
8836             (gnus-info-read info) (setq articles (sort articles '<)))))
8837     ;; Then we have to re-compute how many unread
8838     ;; articles there are in this group.
8839     (if active
8840         (progn
8841           (cond
8842            ((not range)
8843             (setq num (- (1+ (cdr active)) (car active))))
8844            ((not (listp (cdr range)))
8845             (setq num (- (cdr active) (- (1+ (cdr range))
8846                                          (car range)))))
8847            (t
8848             (while range
8849               (if (numberp (car range))
8850                   (setq num (1+ num))
8851                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8852               (setq range (cdr range)))
8853             (setq num (- (cdr active) num))))
8854           ;; Update the number of unread articles.
8855           (setcar entry num)
8856           ;; Update the group buffer.
8857           (gnus-group-update-group group t)))))
8858
8859 (defun gnus-methods-equal-p (m1 m2)
8860   (let ((m1 (or m1 gnus-select-method))
8861         (m2 (or m2 gnus-select-method)))
8862     (or (equal m1 m2)
8863         (and (eq (car m1) (car m2))
8864              (or (not (memq 'address (assoc (symbol-name (car m1))
8865                                             gnus-valid-select-methods)))
8866                  (equal (nth 1 m1) (nth 1 m2)))))))
8867
8868 (defsubst gnus-header-value ()
8869   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8870
8871 (defvar gnus-newsgroup-none-id 0)
8872
8873 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8874   (let ((cur nntp-server-buffer)
8875         (dependencies
8876          (or dependencies
8877              (save-excursion (set-buffer gnus-summary-buffer)
8878                              gnus-newsgroup-dependencies)))
8879         headers id id-dep ref-dep end ref)
8880     (save-excursion
8881       (set-buffer nntp-server-buffer)
8882       (let ((case-fold-search t)
8883             in-reply-to header number p lines)
8884         (goto-char (point-min))
8885         ;; Search to the beginning of the next header.  Error messages
8886         ;; do not begin with 2 or 3.
8887         (while (re-search-forward "^[23][0-9]+ " nil t)
8888           (setq id nil
8889                 ref nil)
8890           ;; This implementation of this function, with nine
8891           ;; search-forwards instead of the one re-search-forward and
8892           ;; a case (which basically was the old function) is actually
8893           ;; about twice as fast, even though it looks messier.  You
8894           ;; can't have everything, I guess.  Speed and elegance
8895           ;; doesn't always go hand in hand.
8896           (setq
8897            header
8898            (vector
8899             ;; Number.
8900             (prog1
8901                 (read cur)
8902               (end-of-line)
8903               (setq p (point))
8904               (narrow-to-region (point)
8905                                 (or (and (search-forward "\n.\n" nil t)
8906                                          (- (point) 2))
8907                                     (point))))
8908             ;; Subject.
8909             (progn
8910               (goto-char p)
8911               (if (search-forward "\nsubject: " nil t)
8912                   (gnus-header-value) "(none)"))
8913             ;; From.
8914             (progn
8915               (goto-char p)
8916               (if (search-forward "\nfrom: " nil t)
8917                   (gnus-header-value) "(nobody)"))
8918             ;; Date.
8919             (progn
8920               (goto-char p)
8921               (if (search-forward "\ndate: " nil t)
8922                   (gnus-header-value) ""))
8923             ;; Message-ID.
8924             (progn
8925               (goto-char p)
8926               (if (search-forward "\nmessage-id: " nil t)
8927                   (setq id (gnus-header-value))
8928                 ;; If there was no message-id, we just fake one to make
8929                 ;; subsequent routines simpler.
8930                 (setq id (concat "none+"
8931                                  (int-to-string
8932                                   (setq gnus-newsgroup-none-id
8933                                         (1+ gnus-newsgroup-none-id)))))))
8934             ;; References.
8935             (progn
8936               (goto-char p)
8937               (if (search-forward "\nreferences: " nil t)
8938                   (prog1
8939                       (gnus-header-value)
8940                     (setq end (match-end 0))
8941                     (save-excursion
8942                       (setq ref
8943                             (downcase
8944                              (buffer-substring
8945                               (progn
8946                                 (end-of-line)
8947                                 (search-backward ">" end t)
8948                                 (1+ (point)))
8949                               (progn
8950                                 (search-backward "<" end t)
8951                                 (point)))))))
8952                 ;; Get the references from the in-reply-to header if there
8953                 ;; were no references and the in-reply-to header looks
8954                 ;; promising.
8955                 (if (and (search-forward "\nin-reply-to: " nil t)
8956                          (setq in-reply-to (gnus-header-value))
8957                          (string-match "<[^>]+>" in-reply-to))
8958                     (prog1
8959                         (setq ref (substring in-reply-to (match-beginning 0)
8960                                              (match-end 0)))
8961                       (setq ref (downcase ref))))
8962                 (setq ref "")))
8963             ;; Chars.
8964             0
8965             ;; Lines.
8966             (progn
8967               (goto-char p)
8968               (if (search-forward "\nlines: " nil t)
8969                   (if (numberp (setq lines (read cur)))
8970                       lines 0)
8971                 0))
8972             ;; Xref.
8973             (progn
8974               (goto-char p)
8975               (and (search-forward "\nxref: " nil t)
8976                    (gnus-header-value)))))
8977           ;; We do the threading while we read the headers.  The
8978           ;; message-id and the last reference are both entered into
8979           ;; the same hash table.  Some tippy-toeing around has to be
8980           ;; done in case an article has arrived before the article
8981           ;; which it refers to.
8982           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
8983               (if (and (car (symbol-value id-dep))
8984                        (not force-new))
8985                   ;; An article with this Message-ID has already
8986                   ;; been seen, so we ignore this one, except we add
8987                   ;; any additional Xrefs (in case the two articles
8988                   ;; came from different servers).
8989                   (progn
8990                     (mail-header-set-xref
8991                      (car (symbol-value id-dep))
8992                      (concat (or (mail-header-xref
8993                                   (car (symbol-value id-dep))) "")
8994                              (or (mail-header-xref header) "")))
8995                     (setq header nil))
8996                 (setcar (symbol-value id-dep) header))
8997             (set id-dep (list header)))
8998           (when header
8999             (if (boundp (setq ref-dep (intern ref dependencies)))
9000                 (setcdr (symbol-value ref-dep)
9001                         (nconc (cdr (symbol-value ref-dep))
9002                                (list (symbol-value id-dep))))
9003               (set ref-dep (list nil (symbol-value id-dep))))
9004             (setq headers (cons header headers)))
9005           (goto-char (point-max))
9006           (widen))
9007         (nreverse headers)))))
9008
9009 ;; The following macros and functions were written by Felix Lee
9010 ;; <flee@cse.psu.edu>.
9011
9012 (defmacro gnus-nov-read-integer ()
9013   '(prog1
9014        (if (= (following-char) ?\t)
9015            0
9016          (let ((num (condition-case nil (read buffer) (error nil))))
9017            (if (numberp num) num 0)))
9018      (or (eobp) (forward-char 1))))
9019
9020 (defmacro gnus-nov-skip-field ()
9021   '(search-forward "\t" eol 'move))
9022
9023 (defmacro gnus-nov-field ()
9024   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9025
9026 ;; Goes through the xover lines and returns a list of vectors
9027 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9028   "Parse the news overview data in the server buffer, and return a
9029 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9030   ;; Get the Xref when the users reads the articles since most/some
9031   ;; NNTP servers do not include Xrefs when using XOVER.
9032   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9033   (let ((cur nntp-server-buffer)
9034         (dependencies gnus-newsgroup-dependencies)
9035         number headers header)
9036     (save-excursion
9037       (set-buffer nntp-server-buffer)
9038       ;; Allow the user to mangle the headers before parsing them.
9039       (run-hooks 'gnus-parse-headers-hook)
9040       ;; Allow the user to mangle the headers before parsing them.
9041       (run-hooks 'gnus-parse-headers-hook)
9042       (goto-char (point-min))
9043       (while (and sequence (not (eobp)))
9044         (setq number (read cur))
9045         (while (and sequence (< (car sequence) number))
9046           (setq sequence (cdr sequence)))
9047         (and sequence
9048              (eq number (car sequence))
9049              (progn
9050                (setq sequence (cdr sequence))
9051                (if (setq header
9052                          (inline (gnus-nov-parse-line
9053                                   number dependencies force-new)))
9054                    (setq headers (cons header headers)))))
9055         (forward-line 1))
9056       (setq headers (nreverse headers)))
9057     headers))
9058
9059 ;; This function has to be called with point after the article number
9060 ;; on the beginning of the line.
9061 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9062   (let ((none 0)
9063         (eol (gnus-point-at-eol))
9064         (buffer (current-buffer))
9065         header ref id id-dep ref-dep)
9066
9067     ;; overview: [num subject from date id refs chars lines misc]
9068     (narrow-to-region (point) eol)
9069     (or (eobp) (forward-char))
9070
9071     (condition-case nil
9072         (setq header
9073               (vector
9074                number                   ; number
9075                (gnus-nov-field)         ; subject
9076                (gnus-nov-field)         ; from
9077                (gnus-nov-field)         ; date
9078                (setq id (or (gnus-nov-field)
9079                             (concat "none+"
9080                                     (int-to-string
9081                                      (setq none (1+ none)))))) ; id
9082                (progn
9083                  (save-excursion
9084                    (let ((beg (point)))
9085                      (search-forward "\t" eol)
9086                      (if (search-backward ">" beg t)
9087                          (setq ref
9088                                (downcase
9089                                 (buffer-substring
9090                                  (1+ (point))
9091                                  (progn
9092                                    (search-backward "<" beg t)
9093                                    (point)))))
9094                        (setq ref nil))))
9095                  (gnus-nov-field))      ; refs
9096                (gnus-nov-read-integer)  ; chars
9097                (gnus-nov-read-integer)  ; lines
9098                (if (= (following-char) ?\n)
9099                    nil
9100                  (gnus-nov-field))      ; misc
9101                ))
9102       (error (progn
9103                (ding)
9104                (gnus-message 4 "Strange nov line")
9105                (setq header nil)
9106                (goto-char eol))))
9107
9108     (widen)
9109
9110     ;; We build the thread tree.
9111     (when header
9112       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9113           (if (and (car (symbol-value id-dep))
9114                    (not force-new))
9115               ;; An article with this Message-ID has already been seen,
9116               ;; so we ignore this one, except we add any additional
9117               ;; Xrefs (in case the two articles came from different
9118               ;; servers.
9119               (progn
9120                 (mail-header-set-xref
9121                  (car (symbol-value id-dep))
9122                  (concat (or (mail-header-xref
9123                               (car (symbol-value id-dep))) "")
9124                          (or (mail-header-xref header) "")))
9125                 (setq header nil))
9126             (setcar (symbol-value id-dep) header))
9127         (set id-dep (list header))))
9128     (if header
9129         (progn
9130           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9131               (setcdr (symbol-value ref-dep)
9132                       (nconc (cdr (symbol-value ref-dep))
9133                              (list (symbol-value id-dep))))
9134             (set ref-dep (list nil (symbol-value id-dep))))))
9135     header))
9136
9137 (defun gnus-article-get-xrefs ()
9138   "Fill in the Xref value in `gnus-current-headers', if necessary.
9139 This is meant to be called in `gnus-article-internal-prepare-hook'."
9140   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9141                                  gnus-current-headers)))
9142     (or (not gnus-use-cross-reference)
9143         (not headers)
9144         (and (mail-header-xref headers)
9145              (not (string= (mail-header-xref headers) "")))
9146         (let ((case-fold-search t)
9147               xref)
9148           (save-restriction
9149             (nnheader-narrow-to-headers)
9150             (goto-char (point-min))
9151             (if (or (and (eq (downcase (following-char)) ?x)
9152                          (looking-at "Xref:"))
9153                     (search-forward "\nXref:" nil t))
9154                 (progn
9155                   (goto-char (1+ (match-end 0)))
9156                   (setq xref (buffer-substring (point)
9157                                                (progn (end-of-line) (point))))
9158                   (mail-header-set-xref headers xref))))))))
9159
9160 (defun gnus-summary-insert-subject (id)
9161   "Find article ID and insert the summary line for that article."
9162   (let ((header (gnus-read-header id))
9163         (number (and (numberp id) id)))
9164     (when header
9165       ;; Rebuild the thread that this article is part of and go to the
9166       ;; article we have fetched.
9167       (gnus-rebuild-thread (mail-header-id header))
9168       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9169     (when (and (numberp number)
9170                (> number 0))
9171       ;; We have to update the boundaries even if we can't fetch the
9172       ;; article if ID is a number -- so that the next `P' or `N'
9173       ;; command will fetch the previous (or next) article even
9174       ;; if the one we tried to fetch this time has been canceled.
9175       (and (> number gnus-newsgroup-end)
9176            (setq gnus-newsgroup-end number))
9177       (and (< number gnus-newsgroup-begin)
9178            (setq gnus-newsgroup-begin number))
9179       (setq gnus-newsgroup-unselected
9180             (delq number gnus-newsgroup-unselected)))
9181     ;; Report back a success?
9182     (and header number)))
9183
9184 (defun gnus-summary-work-articles (n)
9185   "Return a list of articles to be worked upon.  The prefix argument,
9186 the list of process marked articles, and the current article will be
9187 taken into consideration."
9188   (cond
9189    ((and n (numberp n))
9190     ;; A numerical prefix has been given.
9191     (let ((backward (< n 0))
9192           (n (abs n))
9193           articles article)
9194       (save-excursion
9195         (while
9196             (and (> n 0)
9197                  (push (setq article (gnus-summary-article-number))
9198                        articles)
9199                  (if backward
9200                      (gnus-summary-find-prev nil article)
9201                    (gnus-summary-find-next nil article)))
9202           (decf n)))
9203       (nreverse articles)))
9204    ((and (boundp 'transient-mark-mode)
9205          transient-mark-mode
9206          mark-active)
9207     ;; Work on the region between point and mark.
9208     (let ((max (max (point) (mark)))
9209           articles article)
9210       (save-excursion
9211         (goto-char (min (point) (mark)))
9212         (while
9213             (and
9214              (push (setq article (gnus-summary-article-number)) articles)
9215              (gnus-summary-find-next nil article)
9216              (< (point) max)))
9217         (nreverse articles))))
9218    (gnus-newsgroup-processable
9219     ;; There are process-marked articles present.
9220     (reverse gnus-newsgroup-processable))
9221    (t
9222     ;; Just return the current article.
9223     (list (gnus-summary-article-number)))))
9224
9225 (defun gnus-summary-search-group (&optional backward use-level)
9226   "Search for next unread newsgroup.
9227 If optional argument BACKWARD is non-nil, search backward instead."
9228   (save-excursion
9229     (set-buffer gnus-group-buffer)
9230     (if (gnus-group-search-forward
9231          backward nil (if use-level (gnus-group-group-level) nil))
9232         (gnus-group-group-name))))
9233
9234 (defun gnus-summary-best-group (&optional exclude-group)
9235   "Find the name of the best unread group.
9236 If EXCLUDE-GROUP, do not go to this group."
9237   (save-excursion
9238     (set-buffer gnus-group-buffer)
9239     (save-excursion
9240       (gnus-group-best-unread-group exclude-group))))
9241
9242 (defun gnus-summary-find-next (&optional unread article backward)
9243   (if backward (gnus-summary-find-prev)
9244     (let* ((article (or article (gnus-summary-article-number)))
9245            (arts (gnus-data-find-list article))
9246            result)
9247       (when (or (not gnus-summary-check-current)
9248                 (not unread)
9249                 (not (gnus-data-unread-p (car arts))))
9250         (setq arts (cdr arts)))
9251       (when (setq result
9252                   (if unread
9253                       (progn
9254                         (while arts
9255                           (when (gnus-data-unread-p (car arts))
9256                             (setq result (car arts)
9257                                   arts nil))
9258                           (setq arts (cdr arts)))
9259                         result)
9260                     (car arts)))
9261         (goto-char (gnus-data-pos result))
9262         (gnus-data-number result)))))
9263
9264 (defun gnus-summary-find-prev (&optional unread article)
9265   (let* ((article (or article (gnus-summary-article-number)))
9266          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9267          result)
9268     (when (or (not gnus-summary-check-current)
9269               (not unread)
9270               (not (gnus-data-unread-p (car arts))))
9271       (setq arts (cdr arts)))
9272     (if (setq result
9273               (if unread
9274                   (progn
9275                     (while arts
9276                       (and (gnus-data-unread-p (car arts))
9277                            (setq result (car arts)
9278                                  arts nil))
9279                       (setq arts (cdr arts)))
9280                     result)
9281                 (car arts)))
9282         (progn
9283           (goto-char (gnus-data-pos result))
9284           (gnus-data-number result)))))
9285
9286 (defun gnus-summary-find-subject (subject &optional unread backward article)
9287   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9288          (article (or article (gnus-summary-article-number)))
9289          (articles (gnus-data-list backward))
9290          (arts (gnus-data-find-list article articles))
9291          result)
9292     (when (or (not gnus-summary-check-current)
9293               (not unread)
9294               (not (gnus-data-unread-p (car arts))))
9295       (setq arts (cdr arts)))
9296     (while arts
9297       (and (or (not unread)
9298                (gnus-data-unread-p (car arts)))
9299            (vectorp (gnus-data-header (car arts)))
9300            (gnus-subject-equal
9301             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9302            (setq result (car arts)
9303                  arts nil))
9304       (setq arts (cdr arts)))
9305     (and result
9306          (goto-char (gnus-data-pos result))
9307          (gnus-data-number result))))
9308
9309 (defun gnus-summary-search-forward (&optional unread subject backward)
9310   "Search forward for an article.
9311 If UNREAD, look for unread articles.  If SUBJECT, look for
9312 articles with that subject.  If BACKWARD, search backward instead."
9313   (cond (subject (gnus-summary-find-subject subject unread backward))
9314         (backward (gnus-summary-find-prev unread))
9315         (t (gnus-summary-find-next unread))))
9316
9317 (defun gnus-recenter (&optional n)
9318   "Center point in window and redisplay frame.
9319 Also do horizontal recentering."
9320   (interactive "P")
9321   (when (and gnus-auto-center-summary
9322              (not (eq gnus-auto-center-summary 'vertical)))
9323     (gnus-horizontal-recenter))
9324   (recenter n))
9325
9326 (defun gnus-summary-recenter ()
9327   "Center point in the summary window.
9328 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9329 displayed, no centering will be performed."
9330   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9331   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9332   (let* ((top (cond ((< (window-height) 4) 0)
9333                     ((< (window-height) 7) 1)
9334                     (t 2)))
9335          (height (1- (window-height)))
9336          (bottom (save-excursion (goto-char (point-max))
9337                                  (forward-line (- height))
9338                                  (point)))
9339          (window (get-buffer-window (current-buffer))))
9340     ;; The user has to want it.
9341     (when gnus-auto-center-summary
9342       (when (get-buffer-window gnus-article-buffer)
9343        ;; Only do recentering when the article buffer is displayed,
9344        ;; Set the window start to either `bottom', which is the biggest
9345        ;; possible valid number, or the second line from the top,
9346        ;; whichever is the least.
9347        (set-window-start
9348         window (min bottom (save-excursion 
9349                              (forward-line (- top)) (point)))))
9350       ;; Do horizontal recentering while we're at it.
9351       (when (and (get-buffer-window (current-buffer) t)
9352                  (not (eq gnus-auto-center-summary 'vertical)))
9353         (let ((selected (selected-window)))
9354           (select-window (get-buffer-window (current-buffer) t))
9355           (gnus-summary-position-point)
9356           (gnus-horizontal-recenter)
9357           (select-window selected))))))
9358
9359 (defun gnus-horizontal-recenter ()
9360   "Recenter the current buffer horizontally."
9361   (if (< (current-column) (/ (window-width) 2))
9362       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9363     (let* ((orig (point))
9364            (end (window-end (get-buffer-window (current-buffer) t)))
9365            (max 0))
9366       ;; Find the longest line currently displayed in the window.
9367       (goto-char (window-start))
9368       (while (and (not (eobp)) 
9369                   (< (point) end))
9370         (end-of-line)
9371         (setq max (max max (current-column)))
9372         (forward-line 1))
9373       (goto-char orig)
9374       ;; Scroll horizontally to center (sort of) the point.
9375       (if (> max (window-width))
9376           (set-window-hscroll 
9377            (get-buffer-window (current-buffer) t)
9378            (min (- (current-column) (/ (window-width) 3))
9379                 (+ 2 (- max (window-width)))))
9380         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9381       max)))
9382
9383 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9384 (defun gnus-short-group-name (group &optional levels)
9385   "Collapse GROUP name LEVELS."
9386   (let* ((name "") 
9387          (foreign "")
9388          (depth 0) 
9389          (skip 1)
9390          (levels (or levels
9391                      (progn
9392                        (while (string-match "\\." group skip)
9393                          (setq skip (match-end 0)
9394                                depth (+ depth 1)))
9395                        depth))))
9396     (if (string-match ":" group)
9397         (setq foreign (substring group 0 (match-end 0))
9398               group (substring group (match-end 0))))
9399     (while group
9400       (if (and (string-match "\\." group)
9401                (> levels (- gnus-group-uncollapsed-levels 1)))
9402           (setq name (concat name (substring group 0 1))
9403                 group (substring group (match-end 0))
9404                 levels (- levels 1)
9405                 name (concat name "."))
9406         (setq name (concat foreign name group)
9407               group nil)))
9408     name))
9409
9410 (defun gnus-summary-jump-to-group (newsgroup)
9411   "Move point to NEWSGROUP in group mode buffer."
9412   ;; Keep update point of group mode buffer if visible.
9413   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9414       (save-window-excursion
9415         ;; Take care of tree window mode.
9416         (if (get-buffer-window gnus-group-buffer)
9417             (pop-to-buffer gnus-group-buffer))
9418         (gnus-group-jump-to-group newsgroup))
9419     (save-excursion
9420       ;; Take care of tree window mode.
9421       (if (get-buffer-window gnus-group-buffer)
9422           (pop-to-buffer gnus-group-buffer)
9423         (set-buffer gnus-group-buffer))
9424       (gnus-group-jump-to-group newsgroup))))
9425
9426 ;; This function returns a list of article numbers based on the
9427 ;; difference between the ranges of read articles in this group and
9428 ;; the range of active articles.
9429 (defun gnus-list-of-unread-articles (group)
9430   (let* ((read (gnus-info-read (gnus-get-info group)))
9431          (active (gnus-active group))
9432          (last (cdr active))
9433          first nlast unread)
9434     ;; If none are read, then all are unread.
9435     (if (not read)
9436         (setq first (car active))
9437       ;; If the range of read articles is a single range, then the
9438       ;; first unread article is the article after the last read
9439       ;; article.  Sounds logical, doesn't it?
9440       (if (not (listp (cdr read)))
9441           (setq first (1+ (cdr read)))
9442         ;; `read' is a list of ranges.
9443         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9444                                 (caar read))) 1)
9445             (setq first 1))
9446         (while read
9447           (if first
9448               (while (< first nlast)
9449                 (setq unread (cons first unread))
9450                 (setq first (1+ first))))
9451           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9452           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9453           (setq read (cdr read)))))
9454     ;; And add the last unread articles.
9455     (while (<= first last)
9456       (setq unread (cons first unread))
9457       (setq first (1+ first)))
9458     ;; Return the list of unread articles.
9459     (nreverse unread)))
9460
9461 (defun gnus-list-of-read-articles (group)
9462   "Return a list of unread, unticked and non-dormant articles."
9463   (let* ((info (gnus-get-info group))
9464          (marked (gnus-info-marks info))
9465          (active (gnus-active group)))
9466     (and info active
9467          (gnus-set-difference
9468           (gnus-sorted-complement
9469            (gnus-uncompress-range active)
9470            (gnus-list-of-unread-articles group))
9471           (append
9472            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9473            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9474
9475 ;; Various summary commands
9476
9477 (defun gnus-summary-universal-argument (arg)
9478   "Perform any operation on all articles that are process/prefixed."
9479   (interactive "P")
9480   (gnus-set-global-variables)
9481   (let ((articles (gnus-summary-work-articles arg))
9482         func article)
9483     (if (eq
9484          (setq
9485           func
9486           (key-binding
9487            (read-key-sequence
9488             (substitute-command-keys
9489              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9490              ))))
9491          'undefined)
9492         (progn
9493           (message "Undefined key")
9494           (ding))
9495       (save-excursion
9496         (while articles
9497           (gnus-summary-goto-subject (setq article (pop articles)))
9498           (command-execute func)
9499           (gnus-summary-remove-process-mark article)))))
9500   (gnus-summary-position-point))
9501
9502 (defun gnus-summary-toggle-truncation (&optional arg)
9503   "Toggle truncation of summary lines.
9504 With arg, turn line truncation on iff arg is positive."
9505   (interactive "P")
9506   (setq truncate-lines
9507         (if (null arg) (not truncate-lines)
9508           (> (prefix-numeric-value arg) 0)))
9509   (redraw-display))
9510
9511 (defun gnus-summary-reselect-current-group (&optional all rescan)
9512   "Exit and then reselect the current newsgroup.
9513 The prefix argument ALL means to select all articles."
9514   (interactive "P")
9515   (gnus-set-global-variables)
9516   (let ((current-subject (gnus-summary-article-number))
9517         (group gnus-newsgroup-name))
9518     (setq gnus-newsgroup-begin nil)
9519     (gnus-summary-exit)
9520     ;; We have to adjust the point of group mode buffer because the
9521     ;; current point was moved to the next unread newsgroup by
9522     ;; exiting.
9523     (gnus-summary-jump-to-group group)
9524     (when rescan
9525       (save-excursion
9526         (gnus-group-get-new-news-this-group 1)))
9527     (gnus-group-read-group all t)
9528     (gnus-summary-goto-subject current-subject)))
9529
9530 (defun gnus-summary-rescan-group (&optional all)
9531   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9532   (interactive "P")
9533   (gnus-summary-reselect-current-group all t))
9534
9535 (defun gnus-summary-update-info ()
9536   (let* ((group gnus-newsgroup-name))
9537     (when gnus-newsgroup-kill-headers
9538       (setq gnus-newsgroup-killed
9539             (gnus-compress-sequence
9540              (nconc
9541               (gnus-set-sorted-intersection
9542                (gnus-uncompress-range gnus-newsgroup-killed)
9543                (setq gnus-newsgroup-unselected
9544                      (sort gnus-newsgroup-unselected '<)))
9545               (setq gnus-newsgroup-unreads
9546                     (sort gnus-newsgroup-unreads '<))) t)))
9547     (unless (listp (cdr gnus-newsgroup-killed))
9548       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9549     (let ((headers gnus-newsgroup-headers))
9550       (gnus-close-group group)
9551       (run-hooks 'gnus-exit-group-hook)
9552       (unless gnus-save-score
9553         (setq gnus-newsgroup-scored nil))
9554       ;; Set the new ranges of read articles.
9555       (gnus-update-read-articles
9556        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9557       ;; Set the current article marks.
9558       (gnus-update-marks)
9559       ;; Do the cross-ref thing.
9560       (when gnus-use-cross-reference
9561         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9562       ;; Do adaptive scoring, and possibly save score files.
9563       (when gnus-newsgroup-adaptive
9564         (gnus-score-adaptive))
9565       (when gnus-use-scoring
9566         (gnus-score-save))
9567       ;; Do not switch windows but change the buffer to work.
9568       (set-buffer gnus-group-buffer)
9569       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9570           (gnus-group-update-group group)))))
9571
9572 (defun gnus-summary-exit (&optional temporary)
9573   "Exit reading current newsgroup, and then return to group selection mode.
9574 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9575   (interactive)
9576   (gnus-set-global-variables)
9577   (gnus-kill-save-kill-buffer)
9578   (let* ((group gnus-newsgroup-name)
9579          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9580          (mode major-mode)
9581          (buf (current-buffer)))
9582     (run-hooks 'gnus-summary-prepare-exit-hook)
9583     ;; If we have several article buffers, we kill them at exit.
9584     (unless gnus-single-article-buffer
9585       (gnus-kill-buffer gnus-article-buffer)
9586       (gnus-kill-buffer gnus-original-article-buffer))
9587     (when gnus-use-cache
9588       (gnus-cache-possibly-remove-articles)
9589       (gnus-cache-save-buffers))
9590     (when gnus-use-trees
9591       (gnus-tree-close group))
9592     ;; Make all changes in this group permanent.
9593     (unless quit-config
9594       (gnus-summary-update-info))
9595     ;; Make sure where I was, and go to next newsgroup.
9596     (set-buffer gnus-group-buffer)
9597     (unless quit-config
9598       (gnus-group-jump-to-group group)
9599       (gnus-group-next-unread-group 1))
9600     (run-hooks 'gnus-summary-exit-hook)
9601     (if temporary
9602         nil                             ;Nothing to do.
9603       ;; If we have several article buffers, we kill them at exit.
9604       (unless gnus-single-article-buffer
9605         (gnus-kill-buffer gnus-article-buffer)
9606         (gnus-kill-buffer gnus-original-article-buffer))
9607       (set-buffer buf)
9608       (if (not gnus-kill-summary-on-exit)
9609           (gnus-deaden-summary)
9610         ;; We set all buffer-local variables to nil.  It is unclear why
9611         ;; this is needed, but if we don't, buffer-local variables are
9612         ;; not garbage-collected, it seems.  This would the lead to en
9613         ;; ever-growing Emacs.
9614         (gnus-summary-clear-local-variables)
9615         (when (get-buffer gnus-article-buffer)
9616           (bury-buffer gnus-article-buffer))
9617         ;; We clear the global counterparts of the buffer-local
9618         ;; variables as well, just to be on the safe side.
9619         (gnus-configure-windows 'group 'force)
9620         (gnus-summary-clear-local-variables)
9621         ;; Return to group mode buffer.
9622         (if (eq mode 'gnus-summary-mode)
9623             (gnus-kill-buffer buf)))
9624       (setq gnus-current-select-method gnus-select-method)
9625       (pop-to-buffer gnus-group-buffer)
9626       ;; Clear the current group name.
9627       (if (not quit-config)
9628           (progn
9629             (gnus-group-jump-to-group group)
9630             (gnus-group-next-unread-group 1)
9631             (gnus-configure-windows 'group 'force))
9632         (if (not (buffer-name (car quit-config)))
9633             (gnus-configure-windows 'group 'force)
9634           (set-buffer (car quit-config))
9635           (and (eq major-mode 'gnus-summary-mode)
9636                (gnus-set-global-variables))
9637           (gnus-configure-windows (cdr quit-config))))
9638       (unless quit-config
9639         (setq gnus-newsgroup-name nil)))))
9640
9641 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9642 (defun gnus-summary-exit-no-update (&optional no-questions)
9643   "Quit reading current newsgroup without updating read article info."
9644   (interactive)
9645   (gnus-set-global-variables)
9646   (let* ((group gnus-newsgroup-name)
9647          (quit-config (gnus-group-quit-config group)))
9648     (when (or no-questions
9649               gnus-expert-user
9650               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9651       ;; If we have several article buffers, we kill them at exit.
9652       (unless gnus-single-article-buffer
9653         (gnus-kill-buffer gnus-article-buffer)
9654         (gnus-kill-buffer gnus-original-article-buffer))
9655       (if (not gnus-kill-summary-on-exit)
9656           (gnus-deaden-summary)
9657         (gnus-close-group group)
9658         (gnus-summary-clear-local-variables)
9659         (set-buffer gnus-group-buffer)
9660         (gnus-summary-clear-local-variables)
9661         (when (get-buffer gnus-summary-buffer)
9662           (kill-buffer gnus-summary-buffer)))
9663       (when gnus-use-trees
9664         (gnus-tree-close group))
9665       (when (get-buffer gnus-article-buffer)
9666         (bury-buffer gnus-article-buffer))
9667       ;; Return to the group buffer.
9668       (gnus-configure-windows 'group 'force)
9669       ;; Clear the current group name.
9670       (setq gnus-newsgroup-name nil)
9671       (when (equal (gnus-group-group-name) group)
9672         (gnus-group-next-unread-group 1))
9673       (when quit-config
9674         (if (not (buffer-name (car quit-config)))
9675             (gnus-configure-windows 'group 'force)
9676           (set-buffer (car quit-config))
9677           (when (eq major-mode 'gnus-summary-mode)
9678             (gnus-set-global-variables))
9679           (gnus-configure-windows (cdr quit-config)))))))
9680
9681 ;;; Dead summaries.
9682
9683 (defvar gnus-dead-summary-mode-map nil)
9684
9685 (if gnus-dead-summary-mode-map
9686     nil
9687   (setq gnus-dead-summary-mode-map (make-keymap))
9688   (suppress-keymap gnus-dead-summary-mode-map)
9689   (substitute-key-definition
9690    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9691   (let ((keys '("\C-d" "\r" "\177")))
9692     (while keys
9693       (define-key gnus-dead-summary-mode-map
9694         (pop keys) 'gnus-summary-wake-up-the-dead))))
9695
9696 (defvar gnus-dead-summary-mode nil
9697   "Minor mode for Gnus summary buffers.")
9698
9699 (defun gnus-dead-summary-mode (&optional arg)
9700   "Minor mode for Gnus summary buffers."
9701   (interactive "P")
9702   (when (eq major-mode 'gnus-summary-mode)
9703     (make-local-variable 'gnus-dead-summary-mode)
9704     (setq gnus-dead-summary-mode
9705           (if (null arg) (not gnus-dead-summary-mode)
9706             (> (prefix-numeric-value arg) 0)))
9707     (when gnus-dead-summary-mode
9708       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9709         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9710       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9711         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9712               minor-mode-map-alist)))))
9713
9714 (defun gnus-deaden-summary ()
9715   "Make the current summary buffer into a dead summary buffer."
9716   ;; Kill any previous dead summary buffer.
9717   (when (and gnus-dead-summary
9718              (buffer-name gnus-dead-summary))
9719     (save-excursion
9720       (set-buffer gnus-dead-summary)
9721       (when gnus-dead-summary-mode
9722         (kill-buffer (current-buffer)))))
9723   ;; Make this the current dead summary.
9724   (setq gnus-dead-summary (current-buffer))
9725   (gnus-dead-summary-mode 1)
9726   (let ((name (buffer-name)))
9727     (when (string-match "Summary" name)
9728       (rename-buffer
9729        (concat (substring name 0 (match-beginning 0)) "Dead "
9730                (substring name (match-beginning 0))) t))))
9731
9732 (defun gnus-kill-or-deaden-summary (buffer)
9733   "Kill or deaden the summary BUFFER."
9734   (cond (gnus-kill-summary-on-exit
9735          (when (and gnus-use-trees
9736                     (and (get-buffer buffer)
9737                          (buffer-name (get-buffer buffer))))
9738            (save-excursion
9739              (set-buffer (get-buffer buffer))
9740              (gnus-tree-close gnus-newsgroup-name)))
9741          (gnus-kill-buffer buffer))
9742         ((and (get-buffer buffer)
9743               (buffer-name (get-buffer buffer)))
9744          (save-excursion
9745            (set-buffer buffer)
9746            (gnus-deaden-summary)))))
9747
9748 (defun gnus-summary-wake-up-the-dead (&rest args)
9749   "Wake up the dead summary buffer."
9750   (interactive)
9751   (gnus-dead-summary-mode -1)
9752   (let ((name (buffer-name)))
9753     (when (string-match "Dead " name)
9754       (rename-buffer
9755        (concat (substring name 0 (match-beginning 0))
9756                (substring name (match-end 0))) t)))
9757   (gnus-message 3 "This dead summary is now alive again"))
9758
9759 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9760 (defun gnus-summary-fetch-faq (&optional faq-dir)
9761   "Fetch the FAQ for the current group.
9762 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9763 in."
9764   (interactive
9765    (list
9766     (if current-prefix-arg
9767         (completing-read
9768          "Faq dir: " (and (listp gnus-group-faq-directory)
9769                           gnus-group-faq-directory)))))
9770   (let (gnus-faq-buffer)
9771     (and (setq gnus-faq-buffer
9772                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9773          (gnus-configure-windows 'summary-faq))))
9774
9775 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9776 (defun gnus-summary-describe-group (&optional force)
9777   "Describe the current newsgroup."
9778   (interactive "P")
9779   (gnus-group-describe-group force gnus-newsgroup-name))
9780
9781 (defun gnus-summary-describe-briefly ()
9782   "Describe summary mode commands briefly."
9783   (interactive)
9784   (gnus-message 6
9785                 (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")))
9786
9787 ;; Walking around group mode buffer from summary mode.
9788
9789 (defun gnus-summary-next-group (&optional no-article target-group backward)
9790   "Exit current newsgroup and then select next unread newsgroup.
9791 If prefix argument NO-ARTICLE is non-nil, no article is selected
9792 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9793 previous group instead."
9794   (interactive "P")
9795   (gnus-set-global-variables)
9796   (let ((current-group gnus-newsgroup-name)
9797         (current-buffer (current-buffer))
9798         entered)
9799     ;; First we semi-exit this group to update Xrefs and all variables.
9800     ;; We can't do a real exit, because the window conf must remain
9801     ;; the same in case the user is prompted for info, and we don't
9802     ;; want the window conf to change before that...
9803     (gnus-summary-exit t)
9804     (while (not entered)
9805       ;; Then we find what group we are supposed to enter.
9806       (set-buffer gnus-group-buffer)
9807       (gnus-group-jump-to-group current-group)
9808       (setq target-group
9809             (or target-group
9810                 (if (eq gnus-keep-same-level 'best)
9811                     (gnus-summary-best-group gnus-newsgroup-name)
9812                   (gnus-summary-search-group backward gnus-keep-same-level))))
9813       (if (not target-group)
9814           ;; There are no further groups, so we return to the group
9815           ;; buffer.
9816           (progn
9817             (gnus-message 5 "Returning to the group buffer")
9818             (setq entered t)
9819             (set-buffer current-buffer)
9820             (gnus-summary-exit))
9821         ;; We try to enter the target group.
9822         (gnus-group-jump-to-group target-group)
9823         (let ((unreads (gnus-group-group-unread)))
9824           (if (and (or (eq t unreads)
9825                        (and unreads (not (zerop unreads))))
9826                    (gnus-summary-read-group
9827                     target-group nil no-article current-buffer))
9828               (setq entered t)
9829             (setq current-group target-group
9830                   target-group nil)))))))
9831
9832 (defun gnus-summary-prev-group (&optional no-article)
9833   "Exit current newsgroup and then select previous unread newsgroup.
9834 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9835   (interactive "P")
9836   (gnus-summary-next-group no-article nil t))
9837
9838 ;; Walking around summary lines.
9839
9840 (defun gnus-summary-first-subject (&optional unread)
9841   "Go to the first unread subject.
9842 If UNREAD is non-nil, go to the first unread article.
9843 Returns the article selected or nil if there are no unread articles."
9844   (interactive "P")
9845   (prog1
9846       (cond
9847        ;; Empty summary.
9848        ((null gnus-newsgroup-data)
9849         (gnus-message 3 "No articles in the group")
9850         nil)
9851        ;; Pick the first article.
9852        ((not unread)
9853         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9854         (gnus-data-number (car gnus-newsgroup-data)))
9855        ;; No unread articles.
9856        ((null gnus-newsgroup-unreads)
9857         (gnus-message 3 "No more unread articles")
9858         nil)
9859        ;; Find the first unread article.
9860        (t
9861         (let ((data gnus-newsgroup-data))
9862           (while (and data
9863                       (not (gnus-data-unread-p (car data))))
9864             (setq data (cdr data)))
9865           (if data
9866               (progn
9867                 (goto-char (gnus-data-pos (car data)))
9868                 (gnus-data-number (car data)))))))
9869     (gnus-summary-position-point)))
9870
9871 (defun gnus-summary-next-subject (n &optional unread dont-display)
9872   "Go to next N'th summary line.
9873 If N is negative, go to the previous N'th subject line.
9874 If UNREAD is non-nil, only unread articles are selected.
9875 The difference between N and the actual number of steps taken is
9876 returned."
9877   (interactive "p")
9878   (let ((backward (< n 0))
9879         (n (abs n)))
9880     (while (and (> n 0)
9881                 (if backward
9882                     (gnus-summary-find-prev unread)
9883                   (gnus-summary-find-next unread)))
9884       (setq n (1- n)))
9885     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9886                                (if unread " unread" "")))
9887     (or dont-display
9888         (progn
9889           (gnus-summary-recenter)
9890           (gnus-summary-position-point)))
9891     n))
9892
9893 (defun gnus-summary-next-unread-subject (n)
9894   "Go to next N'th unread summary line."
9895   (interactive "p")
9896   (gnus-summary-next-subject n t))
9897
9898 (defun gnus-summary-prev-subject (n &optional unread)
9899   "Go to previous N'th summary line.
9900 If optional argument UNREAD is non-nil, only unread article is selected."
9901   (interactive "p")
9902   (gnus-summary-next-subject (- n) unread))
9903
9904 (defun gnus-summary-prev-unread-subject (n)
9905   "Go to previous N'th unread summary line."
9906   (interactive "p")
9907   (gnus-summary-next-subject (- n) t))
9908
9909 (defun gnus-summary-goto-subject (article &optional force silent)
9910   "Go the subject line of ARTICLE.
9911 If FORCE, also allow jumping to articles not currently shown."
9912   (let ((b (point))
9913         (data (gnus-data-find article)))
9914     ;; We read in the article if we have to.
9915     (and (not data)
9916          force
9917          (gnus-summary-insert-subject article)
9918          (setq data (gnus-data-find article)))
9919     (goto-char b)
9920     (if (not data)
9921         (progn
9922           (unless silent
9923             (gnus-message 3 "Can't find article %d" article))
9924           nil)
9925       (goto-char (gnus-data-pos data))
9926       article)))
9927
9928 ;; Walking around summary lines with displaying articles.
9929
9930 (defun gnus-summary-expand-window (&optional arg)
9931   "Make the summary buffer take up the entire Emacs frame.
9932 Given a prefix, will force an `article' buffer configuration."
9933   (interactive "P")
9934   (gnus-set-global-variables)
9935   (if arg
9936       (gnus-configure-windows 'article 'force)
9937     (gnus-configure-windows 'summary 'force)))
9938
9939 (defun gnus-summary-display-article (article &optional all-header)
9940   "Display ARTICLE in article buffer."
9941   (gnus-set-global-variables)
9942   (if (null article)
9943       nil
9944     (prog1
9945         (if gnus-summary-display-article-function
9946             (funcall gnus-summary-display-article-function article all-header)
9947           (gnus-article-prepare article all-header))
9948       (run-hooks 'gnus-select-article-hook)
9949       (gnus-summary-recenter)
9950       (gnus-summary-goto-subject article)
9951       (when gnus-use-trees
9952         (gnus-possibly-generate-tree article)
9953         (gnus-highlight-selected-tree article))
9954       ;; Successfully display article.
9955       (gnus-article-set-window-start
9956        (cdr (assq article gnus-newsgroup-bookmarks)))
9957       t)))
9958
9959 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9960   "Select the current article.
9961 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9962 non-nil, the article will be re-fetched even if it already present in
9963 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9964 be displayed."
9965   (let ((article (or article (gnus-summary-article-number)))
9966         (all-headers (not (not all-headers))) ;Must be T or NIL.
9967         gnus-summary-display-article-function
9968         did)
9969     (and (not pseudo)
9970          (gnus-summary-article-pseudo-p article)
9971          (error "This is a pseudo-article."))
9972     (prog1
9973         (save-excursion
9974           (set-buffer gnus-summary-buffer)
9975           (if (or (and gnus-single-article-buffer
9976                        (or (null gnus-current-article)
9977                            (null gnus-article-current)
9978                            (null (get-buffer gnus-article-buffer))
9979                            (not (eq article (cdr gnus-article-current)))
9980                            (not (equal (car gnus-article-current)
9981                                        gnus-newsgroup-name))))
9982                   (and (not gnus-single-article-buffer)
9983                        (null gnus-current-article))
9984                   force)
9985               ;; The requested article is different from the current article.
9986               (prog1
9987                   (gnus-summary-display-article article all-headers)
9988                 (setq did article))
9989             (if (or all-headers gnus-show-all-headers)
9990                 (gnus-article-show-all-headers))
9991             'old))
9992       (if did
9993           (gnus-article-set-window-start
9994            (cdr (assq article gnus-newsgroup-bookmarks)))))))
9995
9996 (defun gnus-summary-set-current-mark (&optional current-mark)
9997   "Obsolete function."
9998   nil)
9999
10000 (defun gnus-summary-next-article (&optional unread subject backward push)
10001   "Select the next article.
10002 If UNREAD, only unread articles are selected.
10003 If SUBJECT, only articles with SUBJECT are selected.
10004 If BACKWARD, the previous article is selected instead of the next."
10005   (interactive "P")
10006   (gnus-set-global-variables)
10007   (let (header)
10008     (cond
10009      ;; Is there such an article?
10010      ((and (gnus-summary-search-forward unread subject backward)
10011            (or (gnus-summary-display-article (gnus-summary-article-number))
10012                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10013       (gnus-summary-position-point))
10014      ;; If not, we try the first unread, if that is wanted.
10015      ((and subject
10016            gnus-auto-select-same
10017            (or (gnus-summary-first-unread-article)
10018                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10019       (gnus-summary-position-point)
10020       (gnus-message 6 "Wrapped"))
10021      ;; Try to get next/previous article not displayed in this group.
10022      ((and gnus-auto-extend-newsgroup
10023            (not unread) (not subject))
10024       (gnus-summary-goto-article
10025        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10026        nil t))
10027      ;; Go to next/previous group.
10028      (t
10029       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10030           (gnus-summary-jump-to-group gnus-newsgroup-name))
10031       (let ((cmd last-command-char)
10032             (group
10033              (if (eq gnus-keep-same-level 'best)
10034                  (gnus-summary-best-group gnus-newsgroup-name)
10035                (gnus-summary-search-group backward gnus-keep-same-level))))
10036         ;; For some reason, the group window gets selected.  We change
10037         ;; it back.
10038         (select-window (get-buffer-window (current-buffer)))
10039         ;; Select next unread newsgroup automagically.
10040         (cond
10041          ((not gnus-auto-select-next)
10042           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10043          ((or (eq gnus-auto-select-next 'quietly)
10044               (and (eq gnus-auto-select-next 'slightly-quietly)
10045                    push)
10046               (and (eq gnus-auto-select-next 'almost-quietly)
10047                    (gnus-summary-last-article-p)))
10048           ;; Select quietly.
10049           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10050               (gnus-summary-exit)
10051             (gnus-message 7 "No more%s articles (%s)..."
10052                           (if unread " unread" "")
10053                           (if group (concat "selecting " group)
10054                             "exiting"))
10055             (gnus-summary-next-group nil group backward)))
10056          (t
10057           (gnus-summary-walk-group-buffer
10058            gnus-newsgroup-name cmd unread backward))))))))
10059
10060 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10061   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10062                       (?\C-p (gnus-group-prev-unread-group 1))))
10063         keve key group ended)
10064     (save-excursion
10065       (set-buffer gnus-group-buffer)
10066       (gnus-summary-jump-to-group from-group)
10067       (setq group
10068             (if (eq gnus-keep-same-level 'best)
10069                 (gnus-summary-best-group gnus-newsgroup-name)
10070               (gnus-summary-search-group backward gnus-keep-same-level))))
10071     (while (not ended)
10072       (gnus-message
10073        5 "No more%s articles%s" (if unread " unread" "")
10074        (if (and group
10075                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10076            (format " (Type %s for %s [%s])"
10077                    (single-key-description cmd) group
10078                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10079          (format " (Type %s to exit %s)"
10080                  (single-key-description cmd)
10081                  gnus-newsgroup-name)))
10082       ;; Confirm auto selection.
10083       (setq key (car (setq keve (gnus-read-event-char))))
10084       (setq ended t)
10085       (cond
10086        ((assq key keystrokes)
10087         (let ((obuf (current-buffer)))
10088           (switch-to-buffer gnus-group-buffer)
10089           (and group
10090                (gnus-group-jump-to-group group))
10091           (eval (cadr (assq key keystrokes)))
10092           (setq group (gnus-group-group-name))
10093           (switch-to-buffer obuf))
10094         (setq ended nil))
10095        ((equal key cmd)
10096         (if (or (not group)
10097                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10098             (gnus-summary-exit)
10099           (gnus-summary-next-group nil group backward)))
10100        (t
10101         (push (cdr keve) unread-command-events))))))
10102
10103 (defun gnus-read-event-char ()
10104   "Get the next event."
10105   (let ((event (read-event)))
10106     (cons (and (numberp event) event) event)))
10107
10108 (defun gnus-summary-next-unread-article ()
10109   "Select unread article after current one."
10110   (interactive)
10111   (gnus-summary-next-article t (and gnus-auto-select-same
10112                                     (gnus-summary-article-subject))))
10113
10114 (defun gnus-summary-prev-article (&optional unread subject)
10115   "Select the article after the current one.
10116 If UNREAD is non-nil, only unread articles are selected."
10117   (interactive "P")
10118   (gnus-summary-next-article unread subject t))
10119
10120 (defun gnus-summary-prev-unread-article ()
10121   "Select unred article before current one."
10122   (interactive)
10123   (gnus-summary-prev-article t (and gnus-auto-select-same
10124                                     (gnus-summary-article-subject))))
10125
10126 (defun gnus-summary-next-page (&optional lines circular)
10127   "Show next page of the selected article.
10128 If at the end of the current article, select the next article.
10129 LINES says how many lines should be scrolled up.
10130
10131 If CIRCULAR is non-nil, go to the start of the article instead of
10132 selecting the next article when reaching the end of the current
10133 article."
10134   (interactive "P")
10135   (setq gnus-summary-buffer (current-buffer))
10136   (gnus-set-global-variables)
10137   (let ((article (gnus-summary-article-number))
10138         (endp nil))
10139     (gnus-configure-windows 'article)
10140     (if (or (null gnus-current-article)
10141             (null gnus-article-current)
10142             (/= article (cdr gnus-article-current))
10143             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10144         ;; Selected subject is different from current article's.
10145         (gnus-summary-display-article article)
10146       (gnus-eval-in-buffer-window
10147        gnus-article-buffer
10148        (setq endp (gnus-article-next-page lines)))
10149       (if endp
10150           (cond (circular
10151                  (gnus-summary-beginning-of-article))
10152                 (lines
10153                  (gnus-message 3 "End of message"))
10154                 ((null lines)
10155                  (if (and (eq gnus-summary-goto-unread 'never)
10156                           (not (eq article gnus-newsgroup-end)))
10157                      (gnus-summary-next-article)
10158                    (gnus-summary-next-unread-article))))))
10159     (gnus-summary-recenter)
10160     (gnus-summary-position-point)))
10161
10162 (defun gnus-summary-prev-page (&optional lines)
10163   "Show previous page of selected article.
10164 Argument LINES specifies lines to be scrolled down."
10165   (interactive "P")
10166   (gnus-set-global-variables)
10167   (let ((article (gnus-summary-article-number)))
10168     (gnus-configure-windows 'article)
10169     (if (or (null gnus-current-article)
10170             (null gnus-article-current)
10171             (/= article (cdr gnus-article-current))
10172             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10173         ;; Selected subject is different from current article's.
10174         (gnus-summary-display-article article)
10175       (gnus-summary-recenter)
10176       (gnus-eval-in-buffer-window gnus-article-buffer
10177                                   (gnus-article-prev-page lines))))
10178   (gnus-summary-position-point))
10179
10180 (defun gnus-summary-scroll-up (lines)
10181   "Scroll up (or down) one line current article.
10182 Argument LINES specifies lines to be scrolled up (or down if negative)."
10183   (interactive "p")
10184   (gnus-set-global-variables)
10185   (gnus-configure-windows 'article)
10186   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10187     (gnus-eval-in-buffer-window
10188      gnus-article-buffer
10189      (cond ((> lines 0)
10190             (if (gnus-article-next-page lines)
10191                 (gnus-message 3 "End of message")))
10192            ((< lines 0)
10193             (gnus-article-prev-page (- lines))))))
10194   (gnus-summary-recenter)
10195   (gnus-summary-position-point))
10196
10197 (defun gnus-summary-next-same-subject ()
10198   "Select next article which has the same subject as current one."
10199   (interactive)
10200   (gnus-set-global-variables)
10201   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10202
10203 (defun gnus-summary-prev-same-subject ()
10204   "Select previous article which has the same subject as current one."
10205   (interactive)
10206   (gnus-set-global-variables)
10207   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10208
10209 (defun gnus-summary-next-unread-same-subject ()
10210   "Select next unread article which has the same subject as current one."
10211   (interactive)
10212   (gnus-set-global-variables)
10213   (gnus-summary-next-article t (gnus-summary-article-subject)))
10214
10215 (defun gnus-summary-prev-unread-same-subject ()
10216   "Select previous unread article which has the same subject as current one."
10217   (interactive)
10218   (gnus-set-global-variables)
10219   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10220
10221 (defun gnus-summary-first-unread-article ()
10222   "Select the first unread article.
10223 Return nil if there are no unread articles."
10224   (interactive)
10225   (gnus-set-global-variables)
10226   (prog1
10227       (if (gnus-summary-first-subject t)
10228           (progn
10229             (gnus-summary-show-thread)
10230             (gnus-summary-first-subject t)
10231             (gnus-summary-display-article (gnus-summary-article-number))))
10232     (gnus-summary-position-point)))
10233
10234 (defun gnus-summary-best-unread-article ()
10235   "Select the unread article with the highest score."
10236   (interactive)
10237   (gnus-set-global-variables)
10238   (let ((best -1000000)
10239         (data gnus-newsgroup-data)
10240         article score)
10241     (while data
10242       (and (gnus-data-unread-p (car data))
10243            (> (setq score
10244                     (gnus-summary-article-score (gnus-data-number (car data))))
10245               best)
10246            (setq best score
10247                  article (gnus-data-number (car data))))
10248       (setq data (cdr data)))
10249     (if article
10250         (gnus-summary-goto-article article)
10251       (error "No unread articles"))
10252     (gnus-summary-position-point)))
10253
10254 (defun gnus-summary-last-subject ()
10255   "Go to the last displayed subject line in the group."
10256   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10257     (when article
10258       (gnus-summary-goto-subject article))))
10259
10260 (defun gnus-summary-goto-article (article &optional all-headers force)
10261   "Fetch ARTICLE and display it if it exists.
10262 If ALL-HEADERS is non-nil, no header lines are hidden."
10263   (interactive
10264    (list
10265     (string-to-int
10266      (completing-read
10267       "Article number: "
10268       (mapcar (lambda (number) (list (int-to-string number)))
10269               gnus-newsgroup-limit)))
10270     current-prefix-arg
10271     t))
10272   (prog1
10273       (if (gnus-summary-goto-subject article force)
10274           (gnus-summary-display-article article all-headers)
10275         (gnus-message 4 "Couldn't go to article %s" article) nil)
10276     (gnus-summary-position-point)))
10277
10278 (defun gnus-summary-goto-last-article ()
10279   "Go to the previously read article."
10280   (interactive)
10281   (prog1
10282       (and gnus-last-article
10283            (gnus-summary-goto-article gnus-last-article))
10284     (gnus-summary-position-point)))
10285
10286 (defun gnus-summary-pop-article (number)
10287   "Pop one article off the history and go to the previous.
10288 NUMBER articles will be popped off."
10289   (interactive "p")
10290   (let (to)
10291     (setq gnus-newsgroup-history
10292           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10293     (if to
10294         (gnus-summary-goto-article (car to))
10295       (error "Article history empty")))
10296   (gnus-summary-position-point))
10297
10298 ;; Summary commands and functions for limiting the summary buffer.
10299
10300 (defun gnus-summary-limit-to-articles (n)
10301   "Limit the summary buffer to the next N articles.
10302 If not given a prefix, use the process marked articles instead."
10303   (interactive "P")
10304   (gnus-set-global-variables)
10305   (prog1
10306       (let ((articles (gnus-summary-work-articles n)))
10307         (setq gnus-newsgroup-processable nil)
10308         (gnus-summary-limit articles))
10309     (gnus-summary-position-point)))
10310
10311 (defun gnus-summary-pop-limit (&optional total)
10312   "Restore the previous limit.
10313 If given a prefix, remove all limits."
10314   (interactive "P")
10315   (gnus-set-global-variables)
10316   (when total 
10317     (setq gnus-newsgroup-limits
10318           (list (mapcar (lambda (h) (mail-header-number h))
10319                         gnus-newsgroup-headers))))
10320   (unless gnus-newsgroup-limits
10321     (error "No limit to pop"))
10322   (prog1
10323       (gnus-summary-limit nil 'pop)
10324     (gnus-summary-position-point)))
10325
10326 (defun gnus-summary-limit-to-subject (subject &optional header)
10327   "Limit the summary buffer to articles that have subjects that match a regexp."
10328   (interactive "sRegexp: ")
10329   (unless header
10330     (setq header "subject"))
10331   (when (not (equal "" subject))
10332     (prog1
10333         (let ((articles (gnus-summary-find-matching
10334                          (or header "subject") subject 'all)))
10335           (or articles (error "Found no matches for \"%s\"" subject))
10336           (gnus-summary-limit articles))
10337       (gnus-summary-position-point))))
10338
10339 (defun gnus-summary-limit-to-author (from)
10340   "Limit the summary buffer to articles that have authors that match a regexp."
10341   (interactive "sRegexp: ")
10342   (gnus-summary-limit-to-subject from "from"))
10343
10344 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10345 (make-obsolete
10346  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10347
10348 (defun gnus-summary-limit-to-unread (&optional all)
10349   "Limit the summary buffer to articles that are not marked as read.
10350 If ALL is non-nil, limit strictly to unread articles."
10351   (interactive "P")
10352   (if all
10353       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10354     (gnus-summary-limit-to-marks
10355      ;; Concat all the marks that say that an article is read and have
10356      ;; those removed.
10357      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10358            gnus-killed-mark gnus-kill-file-mark
10359            gnus-low-score-mark gnus-expirable-mark
10360            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10361      'reverse)))
10362
10363 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10364 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10365
10366 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10367   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10368 If REVERSE, limit the summary buffer to articles that are not marked
10369 with MARKS.  MARKS can either be a string of marks or a list of marks.
10370 Returns how many articles were removed."
10371   (interactive "sMarks: ")
10372   (gnus-set-global-variables)
10373   (prog1
10374       (let ((data gnus-newsgroup-data)
10375             (marks (if (listp marks) marks
10376                      (append marks nil))) ; Transform to list.
10377             articles)
10378         (while data
10379           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10380                  (memq (gnus-data-mark (car data)) marks))
10381                (setq articles (cons (gnus-data-number (car data)) articles)))
10382           (setq data (cdr data)))
10383         (gnus-summary-limit articles))
10384     (gnus-summary-position-point)))
10385
10386 (defun gnus-summary-limit-to-score (&optional score)
10387   "Limit to articles with score at or above SCORE."
10388   (interactive "P")
10389   (gnus-set-global-variables)
10390   (setq score (if score
10391                   (prefix-numeric-value score)
10392                 (or gnus-summary-default-score 0)))
10393   (let ((data gnus-newsgroup-data)
10394         articles)
10395     (while data
10396       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10397                 score)
10398         (push (gnus-data-number (car data)) articles))
10399       (setq data (cdr data)))
10400     (prog1
10401         (gnus-summary-limit articles)
10402       (gnus-summary-position-point))))
10403
10404 (defun gnus-summary-limit-include-dormant ()
10405   "Display all the hidden articles that are marked as dormant."
10406   (interactive)
10407   (gnus-set-global-variables)
10408   (or gnus-newsgroup-dormant
10409       (error "There are no dormant articles in this group"))
10410   (prog1
10411       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10412     (gnus-summary-position-point)))
10413
10414 (defun gnus-summary-limit-exclude-dormant ()
10415   "Hide all dormant articles."
10416   (interactive)
10417   (gnus-set-global-variables)
10418   (prog1
10419       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10420     (gnus-summary-position-point)))
10421
10422 (defun gnus-summary-limit-exclude-childless-dormant ()
10423   "Hide all dormant articles that have no children."
10424   (interactive)
10425   (gnus-set-global-variables)
10426   (let ((data (gnus-data-list t))
10427         articles d children)
10428     ;; Find all articles that are either not dormant or have
10429     ;; children.
10430     (while (setq d (pop data))
10431       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10432                 (and (setq children 
10433                            (gnus-article-children (gnus-data-number d)))
10434                      (let (found)
10435                        (while children
10436                          (when (memq (car children) articles)
10437                            (setq children nil
10438                                  found t))
10439                          (pop children))
10440                        found)))
10441         (push (gnus-data-number d) articles)))
10442     ;; Do the limiting.
10443     (prog1
10444         (gnus-summary-limit articles)
10445       (gnus-summary-position-point))))
10446
10447 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10448   "Mark all unread excluded articles as read.
10449 If ALL, mark even excluded ticked and dormants as read."
10450   (interactive "P")
10451   (let ((articles (gnus-sorted-complement
10452                    (sort
10453                     (mapcar (lambda (h) (mail-header-number h))
10454                             gnus-newsgroup-headers)
10455                     '<)
10456                    (sort gnus-newsgroup-limit '<)))
10457         article)
10458     (setq gnus-newsgroup-unreads nil)
10459     (if all
10460         (setq gnus-newsgroup-dormant nil
10461               gnus-newsgroup-marked nil
10462               gnus-newsgroup-reads
10463               (nconc
10464                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10465                gnus-newsgroup-reads))
10466       (while (setq article (pop articles))
10467         (unless (or (memq article gnus-newsgroup-dormant)
10468                     (memq article gnus-newsgroup-marked))
10469           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10470
10471 (defun gnus-summary-limit (articles &optional pop)
10472   (if pop
10473       ;; We pop the previous limit off the stack and use that.
10474       (setq articles (car gnus-newsgroup-limits)
10475             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10476     ;; We use the new limit, so we push the old limit on the stack.
10477     (setq gnus-newsgroup-limits
10478           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10479   ;; Set the limit.
10480   (setq gnus-newsgroup-limit articles)
10481   (let ((total (length gnus-newsgroup-data))
10482         (data (gnus-data-find-list (gnus-summary-article-number)))
10483         found)
10484     ;; This will do all the work of generating the new summary buffer
10485     ;; according to the new limit.
10486     (gnus-summary-prepare)
10487     ;; Hide any threads, possibly.
10488     (and gnus-show-threads
10489          gnus-thread-hide-subtree
10490          (gnus-summary-hide-all-threads))
10491     ;; Try to return to the article you were at, or one in the
10492     ;; neighborhood.
10493     (if data
10494         ;; We try to find some article after the current one.
10495         (while data
10496           (and (gnus-summary-goto-subject
10497                 (gnus-data-number (car data)) nil t)
10498                (setq data nil
10499                      found t))
10500           (setq data (cdr data))))
10501     (or found
10502         ;; If there is no data, that means that we were after the last
10503         ;; article.  The same goes when we can't find any articles
10504         ;; after the current one.
10505         (progn
10506           (goto-char (point-max))
10507           (gnus-summary-find-prev)))
10508     ;; We return how many articles were removed from the summary
10509     ;; buffer as a result of the new limit.
10510     (- total (length gnus-newsgroup-data))))
10511
10512 (defsubst gnus-cut-thread (thread)
10513   "Go forwards in the thread until we find an article that we want to display."
10514   (if (eq gnus-fetch-old-headers 'some)
10515       (while (and thread
10516                   (memq (mail-header-number (car thread)) 
10517                         gnus-newsgroup-ancient)
10518                   (<= (length (cdr thread)) 1))
10519         (setq thread (cadr thread)))
10520     (while (and thread
10521                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10522                 (= (length (cdr thread)) 1))
10523       (setq thread (cadr thread))))
10524   thread)
10525
10526 (defun gnus-cut-threads (threads)
10527   "Cut off all uninteresting articles from the beginning of threads."
10528   (when (or (eq gnus-fetch-old-headers 'some)
10529             (eq gnus-build-sparse-threads 'some)
10530             (eq gnus-build-sparse-threads 'more))
10531     (let ((th threads))
10532       (while th
10533         (setcar th (gnus-cut-thread (car th)))
10534         (setq th (cdr th)))))
10535   threads)
10536
10537 (defun gnus-summary-initial-limit (&optional show-if-empty)
10538   "Figure out what the initial limit is supposed to be on group entry.
10539 This entails weeding out unwanted dormants, low-scored articles,
10540 fetch-old-headers verbiage, and so on."
10541   ;; Most groups have nothing to remove.
10542   (if (or gnus-inhibit-limiting
10543           (and (null gnus-newsgroup-dormant)
10544                (not (eq gnus-fetch-old-headers 'some))
10545                (null gnus-summary-expunge-below)
10546                (not (eq gnus-build-sparse-threads 'some))
10547                (not (eq gnus-build-sparse-threads 'more))
10548                (null gnus-thread-expunge-below)
10549                (not gnus-use-nocem)))
10550       () ; Do nothing.
10551     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10552     (setq gnus-newsgroup-limit nil)
10553     (mapatoms
10554      (lambda (node)
10555        (unless (car (symbol-value node))
10556          ;; These threads have no parents -- they are roots.
10557          (let ((nodes (cdr (symbol-value node)))
10558                thread)
10559            (while nodes
10560              (if (and gnus-thread-expunge-below
10561                       (< (gnus-thread-total-score (car nodes))
10562                          gnus-thread-expunge-below))
10563                  (gnus-expunge-thread (pop nodes))
10564                (setq thread (pop nodes))
10565                (gnus-summary-limit-children thread))))))
10566      gnus-newsgroup-dependencies)
10567     ;; If this limitation resulted in an empty group, we might
10568     ;; pop the previous limit and use it instead.
10569     (when (and (not gnus-newsgroup-limit)
10570                show-if-empty)
10571       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10572     gnus-newsgroup-limit))
10573
10574 (defun gnus-summary-limit-children (thread)
10575   "Return 1 if this subthread is visible and 0 if it is not."
10576   ;; First we get the number of visible children to this thread.  This
10577   ;; is done by recursing down the thread using this function, so this
10578   ;; will really go down to a leaf article first, before slowly
10579   ;; working its way up towards the root.
10580   (when thread
10581     (let ((children
10582            (if (cdr thread)
10583                (apply '+ (mapcar 'gnus-summary-limit-children
10584                                  (cdr thread)))
10585              0))
10586           (number (mail-header-number (car thread)))
10587           score)
10588       (if (or
10589            ;; If this article is dormant and has absolutely no visible
10590            ;; children, then this article isn't visible.
10591            (and (memq number gnus-newsgroup-dormant)
10592                 (= children 0))
10593            ;; If this is a "fetch-old-headered" and there is only one
10594            ;; visible child (or less), then we don't want this article.
10595            (and (eq gnus-fetch-old-headers 'some)
10596                 (memq number gnus-newsgroup-ancient)
10597                 (zerop children))
10598            ;; If this is a sparsely inserted article with no children,
10599            ;; we don't want it.
10600            (and (eq gnus-build-sparse-threads 'some)
10601                 (memq number gnus-newsgroup-sparse)
10602                 (zerop children))
10603            ;; If we use expunging, and this article is really
10604            ;; low-scored, then we don't want this article.
10605            (when (and gnus-summary-expunge-below
10606                       (< (setq score
10607                                (or (cdr (assq number gnus-newsgroup-scored))
10608                                    gnus-summary-default-score))
10609                          gnus-summary-expunge-below))
10610              ;; We increase the expunge-tally here, but that has
10611              ;; nothing to do with the limits, really.
10612              (incf gnus-newsgroup-expunged-tally)
10613              ;; We also mark as read here, if that's wanted.
10614              (when (and gnus-summary-mark-below
10615                         (< score gnus-summary-mark-below))
10616                (setq gnus-newsgroup-unreads
10617                      (delq number gnus-newsgroup-unreads))
10618                (if gnus-newsgroup-auto-expire
10619                    (push number gnus-newsgroup-expirable)
10620                  (push (cons number gnus-low-score-mark)
10621                        gnus-newsgroup-reads)))
10622              t)
10623            (and gnus-use-nocem
10624                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10625           ;; Nope, invisible article.
10626           0
10627         ;; Ok, this article is to be visible, so we add it to the limit
10628         ;; and return 1.
10629         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10630         1))))
10631
10632 (defun gnus-expunge-thread (thread)
10633   "Mark all articles in THREAD as read."
10634   (let* ((number (mail-header-number (car thread))))
10635     (incf gnus-newsgroup-expunged-tally)
10636     ;; We also mark as read here, if that's wanted.
10637     (setq gnus-newsgroup-unreads
10638           (delq number gnus-newsgroup-unreads))
10639     (if gnus-newsgroup-auto-expire
10640         (push number gnus-newsgroup-expirable)
10641       (push (cons number gnus-low-score-mark)
10642             gnus-newsgroup-reads)))
10643   ;; Go recursively through all subthreads.
10644   (mapcar 'gnus-expunge-thread (cdr thread)))
10645
10646 ;; Summary article oriented commands
10647
10648 (defun gnus-summary-refer-parent-article (n)
10649   "Refer parent article N times.
10650 The difference between N and the number of articles fetched is returned."
10651   (interactive "p")
10652   (gnus-set-global-variables)
10653   (while
10654       (and
10655        (> n 0)
10656        (let* ((header (gnus-summary-article-header))
10657               (ref
10658                ;; If we try to find the parent of the currently
10659                ;; displayed article, then we take a look at the actual
10660                ;; References header, since this is slightly more
10661                ;; reliable than the References field we got from the
10662                ;; server.
10663                (if (and (eq (mail-header-number header)
10664                             (cdr gnus-article-current))
10665                         (equal gnus-newsgroup-name
10666                                (car gnus-article-current)))
10667                    (save-excursion
10668                      (set-buffer gnus-original-article-buffer)
10669                      (nnheader-narrow-to-headers)
10670                      (prog1
10671                          (mail-fetch-field "references")
10672                        (widen)))
10673                  ;; It's not the current article, so we take a bet on
10674                  ;; the value we got from the server.
10675                  (mail-header-references header))))
10676          (if (setq ref (or ref (mail-header-references header)))
10677              (or (gnus-summary-refer-article (gnus-parent-id ref))
10678                  (gnus-message 1 "Couldn't find parent"))
10679            (gnus-message 1 "No references in article %d"
10680                          (gnus-summary-article-number))
10681            nil)))
10682     (setq n (1- n)))
10683   (gnus-summary-position-point)
10684   n)
10685
10686 (defun gnus-summary-refer-references ()
10687   "Fetch all articles mentioned in the References header.
10688 Return how many articles were fetched."
10689   (interactive)
10690   (gnus-set-global-variables)
10691   (let ((ref (mail-header-references (gnus-summary-article-header)))
10692         (current (gnus-summary-article-number))
10693         (n 0))
10694     ;; For each Message-ID in the References header...
10695     (while (string-match "<[^>]*>" ref)
10696       (incf n)
10697       ;; ... fetch that article.
10698       (gnus-summary-refer-article
10699        (prog1 (match-string 0 ref)
10700          (setq ref (substring ref (match-end 0))))))
10701     (gnus-summary-goto-subject current)
10702     (gnus-summary-position-point)
10703     n))
10704
10705 (defun gnus-summary-refer-article (message-id)
10706   "Fetch an article specified by MESSAGE-ID."
10707   (interactive "sMessage-ID: ")
10708   (when (and (stringp message-id)
10709              (not (zerop (length message-id))))
10710     ;; Construct the correct Message-ID if necessary.
10711     ;; Suggested by tale@pawl.rpi.edu.
10712     (unless (string-match "^<" message-id)
10713       (setq message-id (concat "<" message-id)))
10714     (unless (string-match ">$" message-id)
10715       (setq message-id (concat message-id ">")))
10716     (let ((header (car (gnus-gethash (downcase message-id)
10717                                      gnus-newsgroup-dependencies))))
10718       (if header
10719           ;; The article is present in the buffer, to we just go to it.
10720           (gnus-summary-goto-article (mail-header-number header) nil t)
10721         ;; We fetch the article
10722         (let ((gnus-override-method gnus-refer-article-method)
10723               number)
10724           ;; Start the special refer-article method, if necessary.
10725           (when gnus-refer-article-method
10726             (gnus-check-server gnus-refer-article-method))
10727           ;; Fetch the header, and display the article.
10728           (if (setq number (gnus-summary-insert-subject message-id))
10729               (gnus-summary-select-article nil nil nil number)
10730             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10731
10732 (defun gnus-summary-enter-digest-group (&optional force)
10733   "Enter a digest group based on the current article."
10734   (interactive "P")
10735   (gnus-set-global-variables)
10736   (gnus-summary-select-article)
10737   (let ((name (format "%s-%d"
10738                       (gnus-group-prefixed-name
10739                        gnus-newsgroup-name (list 'nndoc ""))
10740                       gnus-current-article))
10741         (ogroup gnus-newsgroup-name)
10742         (case-fold-search t)
10743         (buf (current-buffer))
10744         dig)
10745     (save-excursion
10746       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10747       (insert-buffer-substring gnus-original-article-buffer)
10748       (narrow-to-region
10749        (goto-char (point-min))
10750        (or (search-forward "\n\n" nil t) (point)))
10751       (goto-char (point-min))
10752       (delete-matching-lines "^\\(Path\\):\\|^From ")
10753       (widen))
10754     (unwind-protect
10755         (if (gnus-group-read-ephemeral-group
10756              name `(nndoc ,name (nndoc-address
10757                                  ,(get-buffer dig))
10758                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10759             ;; Make all postings to this group go to the parent group.
10760             (nconc (gnus-info-params (gnus-get-info name))
10761                    (list (cons 'to-group ogroup)))
10762           ;; Couldn't select this doc group.
10763           (switch-to-buffer buf)
10764           (gnus-set-global-variables)
10765           (gnus-configure-windows 'summary)
10766           (gnus-message 3 "Article couldn't be entered?"))
10767       (kill-buffer dig))))
10768
10769 (defun gnus-summary-isearch-article (&optional regexp-p)
10770   "Do incremental search forward on the current article.
10771 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10772   (interactive "P")
10773   (gnus-set-global-variables)
10774   (gnus-summary-select-article)
10775   (gnus-configure-windows 'article)
10776   (gnus-eval-in-buffer-window
10777    gnus-article-buffer
10778    (goto-char (point-min))
10779    (isearch-forward regexp-p)))
10780
10781 (defun gnus-summary-search-article-forward (regexp &optional backward)
10782   "Search for an article containing REGEXP forward.
10783 If BACKWARD, search backward instead."
10784   (interactive
10785    (list (read-string
10786           (format "Search article %s (regexp%s): "
10787                   (if current-prefix-arg "backward" "forward")
10788                   (if gnus-last-search-regexp
10789                       (concat ", default " gnus-last-search-regexp)
10790                     "")))
10791          current-prefix-arg))
10792   (gnus-set-global-variables)
10793   (if (string-equal regexp "")
10794       (setq regexp (or gnus-last-search-regexp ""))
10795     (setq gnus-last-search-regexp regexp))
10796   (if (gnus-summary-search-article regexp backward)
10797       (gnus-article-set-window-start
10798        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10799     (error "Search failed: \"%s\"" regexp)))
10800
10801 (defun gnus-summary-search-article-backward (regexp)
10802   "Search for an article containing REGEXP backward."
10803   (interactive
10804    (list (read-string
10805           (format "Search article backward (regexp%s): "
10806                   (if gnus-last-search-regexp
10807                       (concat ", default " gnus-last-search-regexp)
10808                     "")))))
10809   (gnus-summary-search-article-forward regexp 'backward))
10810
10811 (defun gnus-summary-search-article (regexp &optional backward)
10812   "Search for an article containing REGEXP.
10813 Optional argument BACKWARD means do search for backward.
10814 gnus-select-article-hook is not called during the search."
10815   (let ((gnus-select-article-hook nil)  ;Disable hook.
10816         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10817         (re-search
10818          (if backward
10819              (function re-search-backward) (function re-search-forward)))
10820         (found nil)
10821         (last nil))
10822     ;; Hidden thread subtrees must be searched for ,too.
10823     (gnus-summary-show-all-threads)
10824     ;; First of all, search current article.
10825     ;; We don't want to read article again from NNTP server nor reset
10826     ;; current point.
10827     (gnus-summary-select-article)
10828     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10829     (setq last gnus-current-article)
10830     (gnus-eval-in-buffer-window
10831      gnus-article-buffer
10832      (save-restriction
10833        (widen)
10834        ;; Begin search from current point.
10835        (setq found (funcall re-search regexp nil t))))
10836     ;; Then search next articles.
10837     (while (and (not found)
10838                 (gnus-summary-display-article
10839                  (if backward (gnus-summary-find-prev)
10840                    (gnus-summary-find-next))))
10841       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10842       (gnus-eval-in-buffer-window
10843        gnus-article-buffer
10844        (save-restriction
10845          (widen)
10846          (goto-char (if backward (point-max) (point-min)))
10847          (setq found (funcall re-search regexp nil t)))))
10848     (message "")
10849     ;; Adjust article pointer.
10850     (or (eq last gnus-current-article)
10851         (setq gnus-last-article last))
10852     ;; Return T if found such article.
10853     found))
10854
10855 (defun gnus-summary-find-matching (header regexp &optional backward unread
10856                                           not-case-fold)
10857   "Return a list of all articles that match REGEXP on HEADER.
10858 The search stars on the current article and goes forwards unless
10859 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10860 If UNREAD is non-nil, only unread articles will
10861 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10862 in the comparisons."
10863   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10864                 (gnus-data-find-list
10865                  (gnus-summary-article-number) (gnus-data-list backward))))
10866         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10867         (case-fold-search (not not-case-fold))
10868         articles d)
10869     (or (fboundp (intern (concat "mail-header-" header)))
10870         (error "%s is not a valid header" header))
10871     (while data
10872       (setq d (car data))
10873       (and (or (not unread)             ; We want all articles...
10874                (gnus-data-unread-p d))  ; Or just unreads.
10875            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10876            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10877            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10878       (setq data (cdr data)))
10879     (nreverse articles)))
10880
10881 (defun gnus-summary-execute-command (header regexp command &optional backward)
10882   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10883 If HEADER is an empty string (or nil), the match is done on the entire
10884 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10885   (interactive
10886    (list (let ((completion-ignore-case t))
10887            (completing-read
10888             "Header name: "
10889             (mapcar (lambda (string) (list string))
10890                     '("Number" "Subject" "From" "Lines" "Date"
10891                       "Message-ID" "Xref" "References" "Body"))
10892             nil 'require-match))
10893          (read-string "Regexp: ")
10894          (read-key-sequence "Command: ")
10895          current-prefix-arg))
10896   (when (equal header "Body")
10897     (setq header ""))
10898   (gnus-set-global-variables)
10899   ;; Hidden thread subtrees must be searched as well.
10900   (gnus-summary-show-all-threads)
10901   ;; We don't want to change current point nor window configuration.
10902   (save-excursion
10903     (save-window-excursion
10904       (gnus-message 6 "Executing %s..." (key-description command))
10905       ;; We'd like to execute COMMAND interactively so as to give arguments.
10906       (gnus-execute header regexp
10907                     `(lambda () (call-interactively ',(key-binding command)))
10908                     backward)
10909       (gnus-message 6 "Executing %s...done" (key-description command)))))
10910
10911 (defun gnus-summary-beginning-of-article ()
10912   "Scroll the article back to the beginning."
10913   (interactive)
10914   (gnus-set-global-variables)
10915   (gnus-summary-select-article)
10916   (gnus-configure-windows 'article)
10917   (gnus-eval-in-buffer-window
10918    gnus-article-buffer
10919    (widen)
10920    (goto-char (point-min))
10921    (and gnus-break-pages (gnus-narrow-to-page))))
10922
10923 (defun gnus-summary-end-of-article ()
10924   "Scroll to the end of the article."
10925   (interactive)
10926   (gnus-set-global-variables)
10927   (gnus-summary-select-article)
10928   (gnus-configure-windows 'article)
10929   (gnus-eval-in-buffer-window
10930    gnus-article-buffer
10931    (widen)
10932    (goto-char (point-max))
10933    (recenter -3)
10934    (and gnus-break-pages (gnus-narrow-to-page))))
10935
10936 (defun gnus-summary-show-article (&optional arg)
10937   "Force re-fetching of the current article.
10938 If ARG (the prefix) is non-nil, show the raw article without any
10939 article massaging functions being run."
10940   (interactive "P")
10941   (gnus-set-global-variables)
10942   (if (not arg)
10943       ;; Select the article the normal way.
10944       (gnus-summary-select-article nil 'force)
10945     ;; Bind the article treatment functions to nil.
10946     (let ((gnus-have-all-headers t)
10947           gnus-article-display-hook
10948           gnus-article-prepare-hook
10949           gnus-visual)
10950       (gnus-summary-select-article nil 'force)))
10951 ;  (gnus-configure-windows 'article)
10952   (gnus-summary-position-point))
10953
10954 (defun gnus-summary-verbose-headers (&optional arg)
10955   "Toggle permanent full header display.
10956 If ARG is a positive number, turn header display on.
10957 If ARG is a negative number, turn header display off."
10958   (interactive "P")
10959   (gnus-set-global-variables)
10960   (gnus-summary-toggle-header arg)
10961   (setq gnus-show-all-headers
10962         (cond ((or (not (numberp arg))
10963                    (zerop arg))
10964                (not gnus-show-all-headers))
10965               ((natnump arg)
10966                t))))
10967
10968 (defun gnus-summary-toggle-header (&optional arg)
10969   "Show the headers if they are hidden, or hide them if they are shown.
10970 If ARG is a positive number, show the entire header.
10971 If ARG is a negative number, hide the unwanted header lines."
10972   (interactive "P")
10973   (gnus-set-global-variables)
10974   (save-excursion
10975     (set-buffer gnus-article-buffer)
10976     (let* ((buffer-read-only nil)
10977            (inhibit-point-motion-hooks t)
10978            (hidden (text-property-any
10979                     (goto-char (point-min)) (search-forward "\n\n")
10980                     'invisible t))
10981            e)
10982       (goto-char (point-min))
10983       (when (search-forward "\n\n" nil t)
10984         (delete-region (point-min) (1- (point))))
10985       (goto-char (point-min))
10986       (save-excursion
10987         (set-buffer gnus-original-article-buffer)
10988         (goto-char (point-min))
10989         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
10990       (insert-buffer-substring gnus-original-article-buffer 1 e)
10991       (let ((gnus-inhibit-hiding t))
10992         (run-hooks 'gnus-article-display-hook))
10993       (if (or (not hidden) (and (numberp arg) (< arg 0)))
10994           (gnus-article-hide-headers)))))
10995
10996 (defun gnus-summary-show-all-headers ()
10997   "Make all header lines visible."
10998   (interactive)
10999   (gnus-set-global-variables)
11000   (gnus-article-show-all-headers))
11001
11002 (defun gnus-summary-toggle-mime (&optional arg)
11003   "Toggle MIME processing.
11004 If ARG is a positive number, turn MIME processing on."
11005   (interactive "P")
11006   (gnus-set-global-variables)
11007   (setq gnus-show-mime
11008         (if (null arg) (not gnus-show-mime)
11009           (> (prefix-numeric-value arg) 0)))
11010   (gnus-summary-select-article t 'force))
11011
11012 (defun gnus-summary-caesar-message (&optional arg)
11013   "Caesar rotate the current article by 13.
11014 The numerical prefix specifies how manu places to rotate each letter
11015 forward."
11016   (interactive "P")
11017   (gnus-set-global-variables)
11018   (gnus-summary-select-article)
11019   (let ((mail-header-separator ""))
11020     (gnus-eval-in-buffer-window
11021      gnus-article-buffer
11022      (save-restriction
11023        (widen)
11024        (let ((start (window-start)))
11025          (news-caesar-buffer-body arg)
11026          (set-window-start (get-buffer-window (current-buffer)) start))))))
11027
11028 (defun gnus-summary-stop-page-breaking ()
11029   "Stop page breaking in the current article."
11030   (interactive)
11031   (gnus-set-global-variables)
11032   (gnus-summary-select-article)
11033   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11034
11035 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11036   "Move the current article to a different newsgroup.
11037 If N is a positive number, move the N next articles.
11038 If N is a negative number, move the N previous articles.
11039 If N is nil and any articles have been marked with the process mark,
11040 move those articles instead.
11041 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11042 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11043 re-spool using this method.
11044
11045 For this function to work, both the current newsgroup and the
11046 newsgroup that you want to move to have to support the `request-move'
11047 and `request-accept' functions."
11048   (interactive "P")
11049   (unless action (setq action 'move))
11050   (gnus-set-global-variables)
11051   ;; Check whether the source group supports the required functions.
11052   (cond ((and (eq action 'move)
11053               (not (gnus-check-backend-function
11054                     'request-move-article gnus-newsgroup-name)))
11055          (error "The current group does not support article moving"))
11056         ((and (eq action 'crosspost)
11057               (not (gnus-check-backend-function
11058                     'request-replace-article gnus-newsgroup-name)))
11059          (error "The current group does not support article editing")))
11060   (let ((articles (gnus-summary-work-articles n))
11061         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11062         (names '((move "move" "Moving")
11063                  (copy "copy" "Copying")
11064                  (crosspost "crosspost" "Crossposting")))
11065         (copy-buf (save-excursion
11066                     (nnheader-set-temp-buffer " *copy article*")))
11067         art-group to-method new-xref article)
11068     (unless (assq action names)
11069       (error "Unknown action %s" action))
11070     ;; Read the newsgroup name.
11071     (when (and (not to-newsgroup)
11072                (not select-method))
11073       (setq to-newsgroup
11074             (gnus-read-move-group-name
11075              (cadr (assq action names))
11076              gnus-current-move-group articles prefix))
11077       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11078     (setq to-method (if select-method (list select-method "")
11079                       (gnus-find-method-for-group to-newsgroup)))
11080     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11081     ;;(error "Can't %s to the same group you're already in" action))
11082     ;; Check the method we are to move this article to...
11083     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11084         (error "%s does not support article copying" (car to-method)))
11085     (or (gnus-check-server to-method)
11086         (error "Can't open server %s" (car to-method)))
11087     (gnus-message 6 "%s to %s: %s..."
11088                   (caddr (assq action names))
11089                   (or select-method to-newsgroup) articles)
11090     (while articles
11091       (setq article (pop articles))
11092       (setq
11093        art-group
11094        (cond
11095         ;; Move the article.
11096         ((eq action 'move)
11097          (gnus-request-move-article
11098           article                       ; Article to move
11099           gnus-newsgroup-name           ; From newsgrouo
11100           (nth 1 (gnus-find-method-for-group
11101                   gnus-newsgroup-name)) ; Server
11102           (list 'gnus-request-accept-article
11103                 (if select-method
11104                     (list 'quote select-method)
11105                   to-newsgroup)
11106                 (not articles))         ; Accept form
11107           (not articles)))              ; Only save nov last time
11108         ;; Copy the article.
11109         ((eq action 'copy)
11110          (save-excursion
11111            (set-buffer copy-buf)
11112            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11113            (gnus-request-accept-article
11114             (if select-method select-method to-newsgroup)
11115             (not articles))))
11116         ;; Crosspost the article.
11117         ((eq action 'crosspost)
11118          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11119            (setq new-xref (concat gnus-newsgroup-name ":" article))
11120            (if (and xref (not (string= xref "")))
11121                (progn
11122                  (when (string-match "^Xref: " xref)
11123                    (setq xref (substring xref (match-end 0))))
11124                  (setq new-xref (concat xref " " new-xref)))
11125              (setq new-xref (concat (system-name) " " new-xref)))
11126            (save-excursion
11127              (set-buffer copy-buf)
11128              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11129              (nnheader-replace-header "xref" new-xref)
11130              (gnus-request-accept-article
11131               (if select-method select-method to-newsgroup)
11132               (not articles)))))))
11133       (if (not art-group)
11134           (gnus-message 1 "Couldn't %s article %s"
11135                         (cadr (assq action names)) article)
11136         (let* ((entry
11137                 (or
11138                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11139                  (gnus-gethash
11140                   (gnus-group-prefixed-name
11141                    (car art-group)
11142                    (if select-method (list select-method "")
11143                      (gnus-find-method-for-group to-newsgroup)))
11144                   gnus-newsrc-hashtb)))
11145                (info (nth 2 entry)))
11146           ;; Update the group that has been moved to.
11147           (when (and info
11148                      (memq action '(move copy)))
11149             (unless (memq article gnus-newsgroup-unreads)
11150               (gnus-info-set-read
11151                info (gnus-add-to-range (gnus-info-read info)
11152                                        (list (cdr art-group)))))
11153
11154             ;; Copy any marks over to the new group.
11155             (let ((marks gnus-article-mark-lists)
11156                   (to-article (cdr art-group)))
11157
11158               ;; See whether the article is to be put in the cache.
11159               (when gnus-use-cache
11160                 (gnus-cache-possibly-enter-article
11161                  (gnus-info-group info) to-article
11162                  (let ((header (copy-sequence
11163                                 (gnus-summary-article-header article))))
11164                    (mail-header-set-number header to-article)
11165                    header)
11166                  (memq article gnus-newsgroup-marked)
11167                  (memq article gnus-newsgroup-dormant)
11168                  (memq article gnus-newsgroup-unreads)))
11169
11170               (while marks
11171                 (when (memq article (symbol-value
11172                                      (intern (format "gnus-newsgroup-%s"
11173                                                      (caar marks)))))
11174                   (gnus-add-marked-articles
11175                    (gnus-info-group info) (cdar marks)
11176                    (list to-article) info))
11177                 (setq marks (cdr marks)))))
11178
11179           ;; Update the Xref header in this article to point to
11180           ;; the new crossposted article we have just created.
11181           (when (eq action 'crosspost)
11182             (save-excursion
11183               (set-buffer copy-buf)
11184               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11185               (nnheader-replace-header
11186                "xref" (concat new-xref " " (gnus-group-prefixed-name
11187                                             (car art-group) to-method)
11188                               ":" (cdr art-group)))
11189               (gnus-request-replace-article
11190                article gnus-newsgroup-name (current-buffer)))))
11191
11192         (gnus-summary-goto-subject article)
11193         (when (eq action 'move)
11194           (gnus-summary-mark-article article gnus-canceled-mark)))
11195       (gnus-summary-remove-process-mark article))
11196     (gnus-kill-buffer copy-buf)
11197     (gnus-summary-position-point)
11198     (gnus-set-mode-line 'summary)))
11199
11200 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11201   "Move the current article to a different newsgroup.
11202 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11203 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11204 re-spool using this method."
11205   (interactive "P")
11206   (gnus-summary-move-article n nil nil 'copy))
11207
11208 (defun gnus-summary-crosspost-article (&optional n)
11209   "Crosspost the current article to some other group."
11210   (interactive "P")
11211   (gnus-summary-move-article n nil nil 'crosspost))
11212
11213 (defun gnus-summary-respool-article (&optional n respool-method)
11214   "Respool the current article.
11215 The article will be squeezed through the mail spooling process again,
11216 which means that it will be put in some mail newsgroup or other
11217 depending on `nnmail-split-methods'.
11218 If N is a positive number, respool the N next articles.
11219 If N is a negative number, respool the N previous articles.
11220 If N is nil and any articles have been marked with the process mark,
11221 respool those articles instead.
11222
11223 Respooling can be done both from mail groups and \"real\" newsgroups.
11224 In the former case, the articles in question will be moved from the
11225 current group into whatever groups they are destined to.  In the
11226 latter case, they will be copied into the relevant groups."
11227   (interactive "P")
11228   (gnus-set-global-variables)
11229   (let ((respool-methods (gnus-methods-using 'respool))
11230         (methname
11231          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11232     (or respool-method
11233         (setq respool-method
11234               (completing-read
11235                "What method do you want to use when respooling? "
11236                respool-methods nil t methname)))
11237     (or (string= respool-method "")
11238         (if (assoc (symbol-name
11239                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11240                    respool-methods)
11241             (gnus-summary-move-article n nil (intern respool-method))
11242           (gnus-summary-copy-article n nil (intern respool-method))))))
11243
11244 (defun gnus-summary-import-article (file)
11245   "Import a random file into a mail newsgroup."
11246   (interactive "fImport file: ")
11247   (gnus-set-global-variables)
11248   (let ((group gnus-newsgroup-name)
11249         (now (current-time))
11250         atts lines)
11251     (or (gnus-check-backend-function 'request-accept-article group)
11252         (error "%s does not support article importing" group))
11253     (or (file-readable-p file)
11254         (not (file-regular-p file))
11255         (error "Can't read %s" file))
11256     (save-excursion
11257       (set-buffer (get-buffer-create " *import file*"))
11258       (buffer-disable-undo (current-buffer))
11259       (erase-buffer)
11260       (insert-file-contents file)
11261       (goto-char (point-min))
11262       (unless (nnheader-article-p)
11263         ;; This doesn't look like an article, so we fudge some headers.
11264         (setq atts (file-attributes file)
11265               lines (count-lines (point-min) (point-max)))
11266         (insert "From: " (read-string "From: ") "\n"
11267                 "Subject: " (read-string "Subject: ") "\n"
11268                 "Date: " (timezone-make-date-arpa-standard
11269                           (current-time-string (nth 5 atts))
11270                           (current-time-zone now)
11271                           (current-time-zone now)) "\n"
11272                 "Message-ID: " (gnus-inews-message-id) "\n"
11273                 "Lines: " (int-to-string lines) "\n"
11274                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11275       (gnus-request-accept-article group t)
11276       (kill-buffer (current-buffer)))))
11277
11278 (defun gnus-summary-expire-articles ()
11279   "Expire all articles that are marked as expirable in the current group."
11280   (interactive)
11281   (gnus-set-global-variables)
11282   (when (gnus-check-backend-function
11283          'request-expire-articles gnus-newsgroup-name)
11284     ;; This backend supports expiry.
11285     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11286            (expirable (if total
11287                           (gnus-list-of-read-articles gnus-newsgroup-name)
11288                         (setq gnus-newsgroup-expirable
11289                               (sort gnus-newsgroup-expirable '<))))
11290            (expiry-wait (gnus-group-get-parameter
11291                          gnus-newsgroup-name 'expiry-wait))
11292            es)
11293       (when expirable
11294         ;; There are expirable articles in this group, so we run them
11295         ;; through the expiry process.
11296         (gnus-message 6 "Expiring articles...")
11297         ;; The list of articles that weren't expired is returned.
11298         (if expiry-wait
11299             (let ((nnmail-expiry-wait-function nil)
11300                   (nnmail-expiry-wait expiry-wait))
11301               (setq es (gnus-request-expire-articles
11302                         expirable gnus-newsgroup-name)))
11303           (setq es (gnus-request-expire-articles
11304                     expirable gnus-newsgroup-name)))
11305         (or total (setq gnus-newsgroup-expirable es))
11306         ;; We go through the old list of expirable, and mark all
11307         ;; really expired articles as nonexistent.
11308         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11309           (let ((gnus-use-cache nil))
11310             (while expirable
11311               (unless (memq (car expirable) es)
11312                 (when (gnus-data-find (car expirable))
11313                   (gnus-summary-mark-article
11314                    (car expirable) gnus-canceled-mark)))
11315               (setq expirable (cdr expirable)))))
11316         (gnus-message 6 "Expiring articles...done")))))
11317
11318 (defun gnus-summary-expire-articles-now ()
11319   "Expunge all expirable articles in the current group.
11320 This means that *all* articles that are marked as expirable will be
11321 deleted forever, right now."
11322   (interactive)
11323   (gnus-set-global-variables)
11324   (or gnus-expert-user
11325       (gnus-y-or-n-p
11326        "Are you really, really, really sure you want to expunge? ")
11327       (error "Phew!"))
11328   (let ((nnmail-expiry-wait 'immediate)
11329         (nnmail-expiry-wait-function nil))
11330     (gnus-summary-expire-articles)))
11331
11332 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11333 (defun gnus-summary-delete-article (&optional n)
11334   "Delete the N next (mail) articles.
11335 This command actually deletes articles.  This is not a marking
11336 command.  The article will disappear forever from your life, never to
11337 return.
11338 If N is negative, delete backwards.
11339 If N is nil and articles have been marked with the process mark,
11340 delete these instead."
11341   (interactive "P")
11342   (gnus-set-global-variables)
11343   (or (gnus-check-backend-function 'request-expire-articles
11344                                    gnus-newsgroup-name)
11345       (error "The current newsgroup does not support article deletion."))
11346   ;; Compute the list of articles to delete.
11347   (let ((articles (gnus-summary-work-articles n))
11348         not-deleted)
11349     (if (and gnus-novice-user
11350              (not (gnus-y-or-n-p
11351                    (format "Do you really want to delete %s forever? "
11352                            (if (> (length articles) 1) "these articles"
11353                              "this article")))))
11354         ()
11355       ;; Delete the articles.
11356       (setq not-deleted (gnus-request-expire-articles
11357                          articles gnus-newsgroup-name 'force))
11358       (while articles
11359         (gnus-summary-remove-process-mark (car articles))
11360         ;; The backend might not have been able to delete the article
11361         ;; after all.
11362         (or (memq (car articles) not-deleted)
11363             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11364         (setq articles (cdr articles))))
11365     (gnus-summary-position-point)
11366     (gnus-set-mode-line 'summary)
11367     not-deleted))
11368
11369 (defun gnus-summary-edit-article (&optional force)
11370   "Enter into a buffer and edit the current article.
11371 This will have permanent effect only in mail groups.
11372 If FORCE is non-nil, allow editing of articles even in read-only
11373 groups."
11374   (interactive "P")
11375   (save-excursion
11376     (set-buffer gnus-summary-buffer)
11377     (gnus-set-global-variables)
11378     (when (and (not force)
11379                (gnus-group-read-only-p))
11380       (error "The current newsgroup does not support article editing."))
11381     (gnus-summary-select-article t nil t)
11382     (gnus-configure-windows 'article)
11383     (select-window (get-buffer-window gnus-article-buffer))
11384     (gnus-message 6 "C-c C-c to end edits")
11385     (setq buffer-read-only nil)
11386     (text-mode)
11387     (use-local-map (copy-keymap (current-local-map)))
11388     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11389     (buffer-enable-undo)
11390     (widen)
11391     (goto-char (point-min))
11392     (search-forward "\n\n" nil t)))
11393
11394 (defun gnus-summary-edit-article-done ()
11395   "Make edits to the current article permanent."
11396   (interactive)
11397   (if (gnus-group-read-only-p)
11398       (progn
11399         (gnus-summary-edit-article-postpone)
11400         (gnus-message
11401          1 "The current newsgroup does not support article editing.")
11402         (ding))
11403     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11404       (erase-buffer)
11405       (insert buf)
11406       (if (not (gnus-request-replace-article
11407                 (cdr gnus-article-current) (car gnus-article-current)
11408                 (current-buffer)))
11409           (error "Couldn't replace article.")
11410         (gnus-article-mode)
11411         (use-local-map gnus-article-mode-map)
11412         (setq buffer-read-only t)
11413         (buffer-disable-undo (current-buffer))
11414         (gnus-configure-windows 'summary)
11415         (gnus-summary-update-article (cdr gnus-article-current)))
11416       (run-hooks 'gnus-article-display-hook)
11417       (and (gnus-visual-p 'summary-highlight 'highlight)
11418            (run-hooks 'gnus-visual-mark-article-hook)))))
11419
11420 (defun gnus-summary-edit-article-postpone ()
11421   "Postpone changes to the current article."
11422   (interactive)
11423   (gnus-article-mode)
11424   (use-local-map gnus-article-mode-map)
11425   (setq buffer-read-only t)
11426   (buffer-disable-undo (current-buffer))
11427   (gnus-configure-windows 'summary)
11428   (and (gnus-visual-p 'summary-highlight 'highlight)
11429        (run-hooks 'gnus-visual-mark-article-hook)))
11430
11431 (defun gnus-summary-respool-query ()
11432   "Query where the respool algorithm would put this article."
11433   (interactive)
11434   (gnus-set-global-variables)
11435   (gnus-summary-select-article)
11436   (save-excursion
11437     (set-buffer gnus-article-buffer)
11438     (save-restriction
11439       (goto-char (point-min))
11440       (search-forward "\n\n")
11441       (narrow-to-region (point-min) (point))
11442       (pp-eval-expression
11443        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11444
11445 ;; Summary score commands.
11446
11447 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11448
11449 (defun gnus-summary-raise-score (n)
11450   "Raise the score of the current article by N."
11451   (interactive "p")
11452   (gnus-set-global-variables)
11453   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11454
11455 (defun gnus-summary-set-score (n)
11456   "Set the score of the current article to N."
11457   (interactive "p")
11458   (gnus-set-global-variables)
11459   (save-excursion
11460     (gnus-summary-show-thread)
11461     (let ((buffer-read-only nil))
11462       ;; Set score.
11463       (gnus-summary-update-mark
11464        (if (= n (or gnus-summary-default-score 0)) ? 
11465          (if (< n (or gnus-summary-default-score 0))
11466              gnus-score-below-mark gnus-score-over-mark)) 'score))
11467     (let* ((article (gnus-summary-article-number))
11468            (score (assq article gnus-newsgroup-scored)))
11469       (if score (setcdr score n)
11470         (setq gnus-newsgroup-scored
11471               (cons (cons article n) gnus-newsgroup-scored))))
11472     (gnus-summary-update-line)))
11473
11474 (defun gnus-summary-current-score ()
11475   "Return the score of the current article."
11476   (interactive)
11477   (gnus-set-global-variables)
11478   (message "%s" (gnus-summary-article-score)))
11479
11480 ;; Summary marking commands.
11481
11482 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11483   "Mark articles which has the same subject as read, and then select the next.
11484 If UNMARK is positive, remove any kind of mark.
11485 If UNMARK is negative, tick articles."
11486   (interactive "P")
11487   (gnus-set-global-variables)
11488   (if unmark
11489       (setq unmark (prefix-numeric-value unmark)))
11490   (let ((count
11491          (gnus-summary-mark-same-subject
11492           (gnus-summary-article-subject) unmark)))
11493     ;; Select next unread article.  If auto-select-same mode, should
11494     ;; select the first unread article.
11495     (gnus-summary-next-article t (and gnus-auto-select-same
11496                                       (gnus-summary-article-subject)))
11497     (gnus-message 7 "%d article%s marked as %s"
11498                   count (if (= count 1) " is" "s are")
11499                   (if unmark "unread" "read"))))
11500
11501 (defun gnus-summary-kill-same-subject (&optional unmark)
11502   "Mark articles which has the same subject as read.
11503 If UNMARK is positive, remove any kind of mark.
11504 If UNMARK is negative, tick articles."
11505   (interactive "P")
11506   (gnus-set-global-variables)
11507   (if unmark
11508       (setq unmark (prefix-numeric-value unmark)))
11509   (let ((count
11510          (gnus-summary-mark-same-subject
11511           (gnus-summary-article-subject) unmark)))
11512     ;; If marked as read, go to next unread subject.
11513     (if (null unmark)
11514         ;; Go to next unread subject.
11515         (gnus-summary-next-subject 1 t))
11516     (gnus-message 7 "%d articles are marked as %s"
11517                   count (if unmark "unread" "read"))))
11518
11519 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11520   "Mark articles with same SUBJECT as read, and return marked number.
11521 If optional argument UNMARK is positive, remove any kinds of marks.
11522 If optional argument UNMARK is negative, mark articles as unread instead."
11523   (let ((count 1))
11524     (save-excursion
11525       (cond
11526        ((null unmark)                   ; Mark as read.
11527         (while (and
11528                 (progn
11529                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11530                   (gnus-summary-show-thread) t)
11531                 (gnus-summary-find-subject subject))
11532           (setq count (1+ count))))
11533        ((> unmark 0)                    ; Tick.
11534         (while (and
11535                 (progn
11536                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11537                   (gnus-summary-show-thread) t)
11538                 (gnus-summary-find-subject subject))
11539           (setq count (1+ count))))
11540        (t                               ; Mark as unread.
11541         (while (and
11542                 (progn
11543                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11544                   (gnus-summary-show-thread) t)
11545                 (gnus-summary-find-subject subject))
11546           (setq count (1+ count)))))
11547       (gnus-set-mode-line 'summary)
11548       ;; Return the number of marked articles.
11549       count)))
11550
11551 (defun gnus-summary-mark-as-processable (n &optional unmark)
11552   "Set the process mark on the next N articles.
11553 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11554 the process mark instead.  The difference between N and the actual
11555 number of articles marked is returned."
11556   (interactive "p")
11557   (gnus-set-global-variables)
11558   (let ((backward (< n 0))
11559         (n (abs n)))
11560     (while (and
11561             (> n 0)
11562             (if unmark
11563                 (gnus-summary-remove-process-mark
11564                  (gnus-summary-article-number))
11565               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11566             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11567       (setq n (1- n)))
11568     (if (/= 0 n) (gnus-message 7 "No more articles"))
11569     (gnus-summary-recenter)
11570     (gnus-summary-position-point)
11571     n))
11572
11573 (defun gnus-summary-unmark-as-processable (n)
11574   "Remove the process mark from the next N articles.
11575 If N is negative, mark backward instead.  The difference between N and
11576 the actual number of articles marked is returned."
11577   (interactive "p")
11578   (gnus-set-global-variables)
11579   (gnus-summary-mark-as-processable n t))
11580
11581 (defun gnus-summary-unmark-all-processable ()
11582   "Remove the process mark from all articles."
11583   (interactive)
11584   (gnus-set-global-variables)
11585   (save-excursion
11586     (while gnus-newsgroup-processable
11587       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11588   (gnus-summary-position-point))
11589
11590 (defun gnus-summary-mark-as-expirable (n)
11591   "Mark N articles forward as expirable.
11592 If N is negative, mark backward instead.  The difference between N and
11593 the actual number of articles marked is returned."
11594   (interactive "p")
11595   (gnus-set-global-variables)
11596   (gnus-summary-mark-forward n gnus-expirable-mark))
11597
11598 (defun gnus-summary-mark-article-as-replied (article)
11599   "Mark ARTICLE replied and update the summary line."
11600   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11601   (let ((buffer-read-only nil))
11602     (when (gnus-summary-goto-subject article)
11603       (gnus-summary-update-secondary-mark article))))
11604
11605 (defun gnus-summary-set-bookmark (article)
11606   "Set a bookmark in current article."
11607   (interactive (list (gnus-summary-article-number)))
11608   (gnus-set-global-variables)
11609   (if (or (not (get-buffer gnus-article-buffer))
11610           (not gnus-current-article)
11611           (not gnus-article-current)
11612           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11613       (error "No current article selected"))
11614   ;; Remove old bookmark, if one exists.
11615   (let ((old (assq article gnus-newsgroup-bookmarks)))
11616     (if old (setq gnus-newsgroup-bookmarks
11617                   (delq old gnus-newsgroup-bookmarks))))
11618   ;; Set the new bookmark, which is on the form
11619   ;; (article-number . line-number-in-body).
11620   (setq gnus-newsgroup-bookmarks
11621         (cons
11622          (cons article
11623                (save-excursion
11624                  (set-buffer gnus-article-buffer)
11625                  (count-lines
11626                   (min (point)
11627                        (save-excursion
11628                          (goto-char (point-min))
11629                          (search-forward "\n\n" nil t)
11630                          (point)))
11631                   (point))))
11632          gnus-newsgroup-bookmarks))
11633   (gnus-message 6 "A bookmark has been added to the current article."))
11634
11635 (defun gnus-summary-remove-bookmark (article)
11636   "Remove the bookmark from the current article."
11637   (interactive (list (gnus-summary-article-number)))
11638   (gnus-set-global-variables)
11639   ;; Remove old bookmark, if one exists.
11640   (let ((old (assq article gnus-newsgroup-bookmarks)))
11641     (if old
11642         (progn
11643           (setq gnus-newsgroup-bookmarks
11644                 (delq old gnus-newsgroup-bookmarks))
11645           (gnus-message 6 "Removed bookmark."))
11646       (gnus-message 6 "No bookmark in current article."))))
11647
11648 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11649 (defun gnus-summary-mark-as-dormant (n)
11650   "Mark N articles forward as dormant.
11651 If N is negative, mark backward instead.  The difference between N and
11652 the actual number of articles marked is returned."
11653   (interactive "p")
11654   (gnus-set-global-variables)
11655   (gnus-summary-mark-forward n gnus-dormant-mark))
11656
11657 (defun gnus-summary-set-process-mark (article)
11658   "Set the process mark on ARTICLE and update the summary line."
11659   (setq gnus-newsgroup-processable
11660         (cons article
11661               (delq article gnus-newsgroup-processable)))
11662   (when (gnus-summary-goto-subject article)
11663     (gnus-summary-show-thread)
11664     (gnus-summary-update-secondary-mark article)))
11665
11666 (defun gnus-summary-remove-process-mark (article)
11667   "Remove the process mark from ARTICLE and update the summary line."
11668   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11669   (when (gnus-summary-goto-subject article)
11670     (gnus-summary-show-thread)
11671     (gnus-summary-update-secondary-mark article)))
11672
11673 (defun gnus-summary-set-saved-mark (article)
11674   "Set the process mark on ARTICLE and update the summary line."
11675   (push article gnus-newsgroup-saved)
11676   (when (gnus-summary-goto-subject article)
11677     (gnus-summary-update-secondary-mark article)))
11678
11679 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11680   "Mark N articles as read forwards.
11681 If N is negative, mark backwards instead.
11682 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11683 marked as unread.
11684 The difference between N and the actual number of articles marked is
11685 returned."
11686   (interactive "p")
11687   (gnus-set-global-variables)
11688   (let ((backward (< n 0))
11689         (gnus-summary-goto-unread
11690          (and gnus-summary-goto-unread
11691               (not (eq gnus-summary-goto-unread 'never))
11692               (not (memq mark (list gnus-unread-mark
11693                                     gnus-ticked-mark gnus-dormant-mark)))))
11694         (n (abs n))
11695         (mark (or mark gnus-del-mark)))
11696     (while (and (> n 0)
11697                 (gnus-summary-mark-article nil mark no-expire)
11698                 (zerop (gnus-summary-next-subject
11699                         (if backward -1 1)
11700                         (and gnus-summary-goto-unread
11701                              (not (eq gnus-summary-goto-unread 'never)))
11702                         t)))
11703       (setq n (1- n)))
11704     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11705     (gnus-summary-recenter)
11706     (gnus-summary-position-point)
11707     (gnus-set-mode-line 'summary)
11708     n))
11709
11710 (defun gnus-summary-mark-article-as-read (mark)
11711   "Mark the current article quickly as read with MARK."
11712   (let ((article (gnus-summary-article-number)))
11713     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11714     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11715     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11716     (setq gnus-newsgroup-reads
11717           (cons (cons article mark) gnus-newsgroup-reads))
11718     ;; Possibly remove from cache, if that is used.
11719     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11720     ;; Allow the backend to change the mark.
11721     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11722     ;; Check for auto-expiry.
11723     (when (and gnus-newsgroup-auto-expire
11724                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11725                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11726                    (= mark gnus-ancient-mark)
11727                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11728       (setq mark gnus-expirable-mark)
11729       (push article gnus-newsgroup-expirable))
11730     ;; Set the mark in the buffer.
11731     (gnus-summary-update-mark mark 'unread)
11732     t))
11733
11734 (defun gnus-summary-mark-article-as-unread (mark)
11735   "Mark the current article quickly as unread with MARK."
11736   (let ((article (gnus-summary-article-number)))
11737     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11738     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11739     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11740     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11741     (cond ((= mark gnus-ticked-mark)
11742            (push article gnus-newsgroup-marked))
11743           ((= mark gnus-dormant-mark)
11744            (push article gnus-newsgroup-dormant))
11745           (t
11746            (push article gnus-newsgroup-unreads)))
11747     (setq gnus-newsgroup-reads
11748           (delq (assq article gnus-newsgroup-reads)
11749                 gnus-newsgroup-reads))
11750
11751     ;; See whether the article is to be put in the cache.
11752     (and gnus-use-cache
11753          (vectorp (gnus-summary-article-header article))
11754          (save-excursion
11755            (gnus-cache-possibly-enter-article
11756             gnus-newsgroup-name article
11757             (gnus-summary-article-header article)
11758             (= mark gnus-ticked-mark)
11759             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11760
11761     ;; Fix the mark.
11762     (gnus-summary-update-mark mark 'unread)
11763     t))
11764
11765 (defun gnus-summary-mark-article (&optional article mark no-expire)
11766   "Mark ARTICLE with MARK.  MARK can be any character.
11767 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11768 `??' (dormant) and `?E' (expirable).
11769 If MARK is nil, then the default character `?D' is used.
11770 If ARTICLE is nil, then the article on the current line will be
11771 marked."
11772   ;; The mark might be a string.
11773   (and (stringp mark)
11774        (setq mark (aref mark 0)))
11775   ;; If no mark is given, then we check auto-expiring.
11776   (and (not no-expire)
11777        gnus-newsgroup-auto-expire
11778        (or (not mark)
11779            (and (numberp mark)
11780                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11781                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11782                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11783        (setq mark gnus-expirable-mark))
11784   (let* ((mark (or mark gnus-del-mark))
11785          (article (or article (gnus-summary-article-number))))
11786     (or article (error "No article on current line"))
11787     (if (or (= mark gnus-unread-mark)
11788             (= mark gnus-ticked-mark)
11789             (= mark gnus-dormant-mark))
11790         (gnus-mark-article-as-unread article mark)
11791       (gnus-mark-article-as-read article mark))
11792
11793     ;; See whether the article is to be put in the cache.
11794     (and gnus-use-cache
11795          (not (= mark gnus-canceled-mark))
11796          (vectorp (gnus-summary-article-header article))
11797          (save-excursion
11798            (gnus-cache-possibly-enter-article
11799             gnus-newsgroup-name article
11800             (gnus-summary-article-header article)
11801             (= mark gnus-ticked-mark)
11802             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11803
11804     (if (gnus-summary-goto-subject article nil t)
11805         (let ((buffer-read-only nil))
11806           (gnus-summary-show-thread)
11807           ;; Fix the mark.
11808           (gnus-summary-update-mark mark 'unread)
11809           t))))
11810
11811 (defun gnus-summary-update-secondary-mark (article)
11812   "Update the secondary (read, process, cache) mark."
11813   (gnus-summary-update-mark
11814    (cond ((memq article gnus-newsgroup-processable)
11815           gnus-process-mark)
11816          ((memq article gnus-newsgroup-cached)
11817           gnus-cached-mark)
11818          ((memq article gnus-newsgroup-replied)
11819           gnus-replied-mark)
11820          ((memq article gnus-newsgroup-saved)
11821           gnus-saved-mark)
11822          (t gnus-unread-mark))
11823    'replied)
11824   (when (gnus-visual-p 'summary-highlight 'highlight)
11825     (run-hooks 'gnus-summary-update-hook))
11826   t)
11827
11828 (defun gnus-summary-update-mark (mark type)
11829   (beginning-of-line)
11830   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11831         (buffer-read-only nil))
11832     (when forward
11833       ;; Go to the right position on the line.
11834       (forward-char forward)
11835       ;; Replace the old mark with the new mark.
11836       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11837       ;; Optionally update the marks by some user rule.
11838       (when (eq type 'unread)
11839         (gnus-data-set-mark
11840          (gnus-data-find (gnus-summary-article-number)) mark)
11841         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11842
11843 (defun gnus-mark-article-as-read (article &optional mark)
11844   "Enter ARTICLE in the pertinent lists and remove it from others."
11845   ;; Make the article expirable.
11846   (let ((mark (or mark gnus-del-mark)))
11847     (if (= mark gnus-expirable-mark)
11848         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11849       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11850     ;; Remove from unread and marked lists.
11851     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11852     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11853     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11854     (push (cons article mark) gnus-newsgroup-reads)
11855     ;; Possibly remove from cache, if that is used.
11856     (when gnus-use-cache
11857       (gnus-cache-enter-remove-article article))))
11858
11859 (defun gnus-mark-article-as-unread (article &optional mark)
11860   "Enter ARTICLE in the pertinent lists and remove it from others."
11861   (let ((mark (or mark gnus-ticked-mark)))
11862     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11863     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11864     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11865     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11866     (cond ((= mark gnus-ticked-mark)
11867            (push article gnus-newsgroup-marked))
11868           ((= mark gnus-dormant-mark)
11869            (push article gnus-newsgroup-dormant))
11870           (t
11871            (push article gnus-newsgroup-unreads)))
11872     (setq gnus-newsgroup-reads
11873           (delq (assq article gnus-newsgroup-reads)
11874                 gnus-newsgroup-reads))))
11875
11876 (defalias 'gnus-summary-mark-as-unread-forward
11877   'gnus-summary-tick-article-forward)
11878 (make-obsolete 'gnus-summary-mark-as-unread-forward
11879                'gnus-summary-tick-article-forward)
11880 (defun gnus-summary-tick-article-forward (n)
11881   "Tick N articles forwards.
11882 If N is negative, tick backwards instead.
11883 The difference between N and the number of articles ticked is returned."
11884   (interactive "p")
11885   (gnus-summary-mark-forward n gnus-ticked-mark))
11886
11887 (defalias 'gnus-summary-mark-as-unread-backward
11888   'gnus-summary-tick-article-backward)
11889 (make-obsolete 'gnus-summary-mark-as-unread-backward
11890                'gnus-summary-tick-article-backward)
11891 (defun gnus-summary-tick-article-backward (n)
11892   "Tick N articles backwards.
11893 The difference between N and the number of articles ticked is returned."
11894   (interactive "p")
11895   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11896
11897 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11898 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11899 (defun gnus-summary-tick-article (&optional article clear-mark)
11900   "Mark current article as unread.
11901 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11902 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11903   (interactive)
11904   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11905                                        gnus-ticked-mark)))
11906
11907 (defun gnus-summary-mark-as-read-forward (n)
11908   "Mark N articles as read forwards.
11909 If N is negative, mark backwards instead.
11910 The difference between N and the actual number of articles marked is
11911 returned."
11912   (interactive "p")
11913   (gnus-summary-mark-forward n gnus-del-mark t))
11914
11915 (defun gnus-summary-mark-as-read-backward (n)
11916   "Mark the N articles as read backwards.
11917 The difference between N and the actual number of articles marked is
11918 returned."
11919   (interactive "p")
11920   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11921
11922 (defun gnus-summary-mark-as-read (&optional article mark)
11923   "Mark current article as read.
11924 ARTICLE specifies the article to be marked as read.
11925 MARK specifies a string to be inserted at the beginning of the line."
11926   (gnus-summary-mark-article article mark))
11927
11928 (defun gnus-summary-clear-mark-forward (n)
11929   "Clear marks from N articles forward.
11930 If N is negative, clear backward instead.
11931 The difference between N and the number of marks cleared is returned."
11932   (interactive "p")
11933   (gnus-summary-mark-forward n gnus-unread-mark))
11934
11935 (defun gnus-summary-clear-mark-backward (n)
11936   "Clear marks from N articles backward.
11937 The difference between N and the number of marks cleared is returned."
11938   (interactive "p")
11939   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11940
11941 (defun gnus-summary-mark-unread-as-read ()
11942   "Intended to be used by `gnus-summary-mark-article-hook'."
11943   (when (memq gnus-current-article gnus-newsgroup-unreads)
11944     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11945
11946 (defun gnus-summary-mark-read-and-unread-as-read ()
11947   "Intended to be used by `gnus-summary-mark-article-hook'."
11948   (let ((mark (gnus-summary-article-mark)))
11949     (when (or (gnus-unread-mark-p mark)
11950               (gnus-read-mark-p mark))
11951       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11952
11953 (defun gnus-summary-mark-region-as-read (point mark all)
11954   "Mark all unread articles between point and mark as read.
11955 If given a prefix, mark all articles between point and mark as read,
11956 even ticked and dormant ones."
11957   (interactive "r\nP")
11958   (save-excursion
11959     (let (article)
11960       (goto-char point)
11961       (beginning-of-line)
11962       (while (and
11963               (< (point) mark)
11964               (progn
11965                 (when (or all
11966                           (memq (setq article (gnus-summary-article-number))
11967                                 gnus-newsgroup-unreads))
11968                   (gnus-summary-mark-article article gnus-del-mark))
11969                 t)
11970               (gnus-summary-find-next))))))
11971
11972 (defun gnus-summary-mark-below (score mark)
11973   "Mark articles with score less than SCORE with MARK."
11974   (interactive "P\ncMark: ")
11975   (gnus-set-global-variables)
11976   (setq score (if score
11977                   (prefix-numeric-value score)
11978                 (or gnus-summary-default-score 0)))
11979   (save-excursion
11980     (set-buffer gnus-summary-buffer)
11981     (goto-char (point-min))
11982     (while 
11983         (progn
11984           (and (< (gnus-summary-article-score) score)
11985                (gnus-summary-mark-article nil mark))
11986           (gnus-summary-find-next)))))
11987
11988 (defun gnus-summary-kill-below (&optional score)
11989   "Mark articles with score below SCORE as read."
11990   (interactive "P")
11991   (gnus-set-global-variables)
11992   (gnus-summary-mark-below score gnus-killed-mark))
11993
11994 (defun gnus-summary-clear-above (&optional score)
11995   "Clear all marks from articles with score above SCORE."
11996   (interactive "P")
11997   (gnus-set-global-variables)
11998   (gnus-summary-mark-above score gnus-unread-mark))
11999
12000 (defun gnus-summary-tick-above (&optional score)
12001   "Tick all articles with score above SCORE."
12002   (interactive "P")
12003   (gnus-set-global-variables)
12004   (gnus-summary-mark-above score gnus-ticked-mark))
12005
12006 (defun gnus-summary-mark-above (score mark)
12007   "Mark articles with score over SCORE with MARK."
12008   (interactive "P\ncMark: ")
12009   (gnus-set-global-variables)
12010   (setq score (if score
12011                   (prefix-numeric-value score)
12012                 (or gnus-summary-default-score 0)))
12013   (save-excursion
12014     (set-buffer gnus-summary-buffer)
12015     (goto-char (point-min))
12016     (while (and (progn
12017                   (if (> (gnus-summary-article-score) score)
12018                       (gnus-summary-mark-article nil mark))
12019                   t)
12020                 (gnus-summary-find-next)))))
12021
12022 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12023 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12024 (defun gnus-summary-limit-include-expunged ()
12025   "Display all the hidden articles that were expunged for low scores."
12026   (interactive)
12027   (gnus-set-global-variables)
12028   (let ((buffer-read-only nil))
12029     (let ((scored gnus-newsgroup-scored)
12030           headers h)
12031       (while scored
12032         (or (gnus-summary-goto-subject (caar scored))
12033             (and (setq h (gnus-summary-article-header (caar scored)))
12034                  (< (cdar scored) gnus-summary-expunge-below)
12035                  (setq headers (cons h headers))))
12036         (setq scored (cdr scored)))
12037       (or headers (error "No expunged articles hidden."))
12038       (goto-char (point-min))
12039       (gnus-summary-prepare-unthreaded (nreverse headers)))
12040     (goto-char (point-min))
12041     (gnus-summary-position-point)))
12042
12043 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12044   "Mark all articles not marked as unread in this newsgroup as read.
12045 If prefix argument ALL is non-nil, all articles are marked as read.
12046 If QUIETLY is non-nil, no questions will be asked.
12047 If TO-HERE is non-nil, it should be a point in the buffer.  All
12048 articles before this point will be marked as read.
12049 The number of articles marked as read is returned."
12050   (interactive "P")
12051   (gnus-set-global-variables)
12052   (prog1
12053       (if (or quietly
12054               (not gnus-interactive-catchup) ;Without confirmation?
12055               gnus-expert-user
12056               (gnus-y-or-n-p
12057                (if all
12058                    "Mark absolutely all articles as read? "
12059                  "Mark all unread articles as read? ")))
12060           (if (and not-mark
12061                    (not gnus-newsgroup-adaptive)
12062                    (not gnus-newsgroup-auto-expire))
12063               (progn
12064                 (when all
12065                   (setq gnus-newsgroup-marked nil
12066                         gnus-newsgroup-dormant nil))
12067                 (setq gnus-newsgroup-unreads nil))
12068             ;; We actually mark all articles as canceled, which we
12069             ;; have to do when using auto-expiry or adaptive scoring.
12070             (gnus-summary-show-all-threads)
12071             (if (gnus-summary-first-subject (not all))
12072                 (while (and
12073                         (if to-here (< (point) to-here) t)
12074                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12075                         (gnus-summary-find-next (not all)))))
12076             (unless to-here
12077               (setq gnus-newsgroup-unreads nil))
12078             (gnus-set-mode-line 'summary)))
12079     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12080       (if (and (not to-here) (eq 'nnvirtual (car method)))
12081           (nnvirtual-catchup-group
12082            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12083     (gnus-summary-position-point)))
12084
12085 (defun gnus-summary-catchup-to-here (&optional all)
12086   "Mark all unticked articles before the current one as read.
12087 If ALL is non-nil, also mark ticked and dormant articles as read."
12088   (interactive "P")
12089   (gnus-set-global-variables)
12090   (save-excursion
12091     (let ((beg (point)))
12092       ;; We check that there are unread articles.
12093       (when (or all (gnus-summary-find-prev))
12094         (gnus-summary-catchup all t beg))))
12095   (gnus-summary-position-point))
12096
12097 (defun gnus-summary-catchup-all (&optional quietly)
12098   "Mark all articles in this newsgroup as read."
12099   (interactive "P")
12100   (gnus-set-global-variables)
12101   (gnus-summary-catchup t quietly))
12102
12103 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12104   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12105 If prefix argument ALL is non-nil, all articles are marked as read."
12106   (interactive "P")
12107   (gnus-set-global-variables)
12108   (gnus-summary-catchup all quietly nil 'fast)
12109   ;; Select next newsgroup or exit.
12110   (if (eq gnus-auto-select-next 'quietly)
12111       (gnus-summary-next-group nil)
12112     (gnus-summary-exit)))
12113
12114 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12115   "Mark all articles in this newsgroup as read, and then exit."
12116   (interactive "P")
12117   (gnus-set-global-variables)
12118   (gnus-summary-catchup-and-exit t quietly))
12119
12120 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12121 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12122   "Mark all articles in this group as read and select the next group.
12123 If given a prefix, mark all articles, unread as well as ticked, as
12124 read."
12125   (interactive "P")
12126   (gnus-set-global-variables)
12127   (save-excursion
12128     (gnus-summary-catchup all))
12129   (gnus-summary-next-article t nil nil t))
12130
12131 ;; Thread-based commands.
12132
12133 (defun gnus-summary-articles-in-thread (&optional article)
12134   "Return a list of all articles in the current thread.
12135 If ARTICLE is non-nil, return all articles in the thread that starts
12136 with that article."
12137   (let* ((article (or article (gnus-summary-article-number)))
12138          (data (gnus-data-find-list article))
12139          (top-level (gnus-data-level (car data)))
12140          (top-subject
12141           (cond ((null gnus-thread-operation-ignore-subject)
12142                  (gnus-simplify-subject-re
12143                   (mail-header-subject (gnus-data-header (car data)))))
12144                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12145                  (gnus-simplify-subject-fuzzy
12146                   (mail-header-subject (gnus-data-header (car data)))))
12147                 (t nil)))
12148          articles)
12149     (if (not data)
12150         ()                              ; This article doesn't exist.
12151       (while data
12152         (and (or (not top-subject)
12153                  (string= top-subject
12154                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12155                               (gnus-simplify-subject-fuzzy
12156                                (mail-header-subject
12157                                 (gnus-data-header (car data))))
12158                             (gnus-simplify-subject-re
12159                              (mail-header-subject
12160                               (gnus-data-header (car data)))))))
12161              (setq articles (cons (gnus-data-number (car data)) articles)))
12162         (if (and (setq data (cdr data))
12163                  (> (gnus-data-level (car data)) top-level))
12164             ()
12165           (setq data nil)))
12166       ;; Return the list of articles.
12167       (nreverse articles))))
12168
12169 (defun gnus-summary-rethread-current ()
12170   "Rethread the thread the current article is part of."
12171   (interactive)
12172   (gnus-set-global-variables)
12173   (let* ((gnus-show-threads t)
12174          (article (gnus-summary-article-number))
12175          (id (mail-header-id (gnus-summary-article-header)))
12176          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12177     (unless id
12178       (error "No article on the current line"))
12179     (gnus-rebuild-thread id)
12180     (gnus-summary-goto-subject article)))
12181
12182 (defun gnus-summary-reparent-thread ()
12183   "Make current article child of the marked (or previous) article.
12184
12185 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12186 is non-nil or the Subject: of both articles are the same."
12187   (interactive)
12188   (or (not (gnus-group-read-only-p))
12189       (error "The current newsgroup does not support article editing."))
12190   (or (<= (length gnus-newsgroup-processable) 1)
12191       (error "No more than one article may be marked."))
12192   (save-window-excursion
12193     (let ((gnus-article-buffer " *reparent*")
12194           (current-article (gnus-summary-article-number))
12195           ; first grab the marked article, otherwise one line up.
12196           (parent-article (if (not (null gnus-newsgroup-processable))
12197                               (car gnus-newsgroup-processable)
12198                             (save-excursion
12199                               (if (eq (forward-line -1) 0)
12200                                   (gnus-summary-article-number)
12201                                 (error "Beginning of summary buffer."))))))
12202       (or (not (eq current-article parent-article))
12203           (error "An article may not be self-referential."))
12204       (let ((message-id (mail-header-id 
12205                          (gnus-summary-article-header parent-article))))
12206         (or (and message-id (not (equal message-id "")))
12207             (error "No message-id in desired parent."))
12208         (gnus-summary-select-article t t nil current-article)
12209         (set-buffer gnus-article-buffer)
12210         (setq buffer-read-only nil)
12211         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12212           (erase-buffer)
12213           (insert buf))
12214         (goto-char (point-min))
12215         (if (search-forward-regexp "^References: " nil t)
12216             (insert message-id " " )
12217           (insert "References: " message-id "\n"))
12218         (or (gnus-request-replace-article current-article
12219                                           (car gnus-article-current)
12220                                           gnus-article-buffer)
12221             (error "Couldn't replace article."))
12222         (set-buffer gnus-summary-buffer)
12223         (gnus-summary-unmark-all-processable)
12224         (gnus-summary-rethread-current)
12225         (message "Article %d is now the child of article %d."
12226                  current-article parent-article)))))
12227
12228 (defun gnus-summary-toggle-threads (&optional arg)
12229   "Toggle showing conversation threads.
12230 If ARG is positive number, turn showing conversation threads on."
12231   (interactive "P")
12232   (gnus-set-global-variables)
12233   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12234     (setq gnus-show-threads
12235           (if (null arg) (not gnus-show-threads)
12236             (> (prefix-numeric-value arg) 0)))
12237     (gnus-summary-prepare)
12238     (gnus-summary-goto-subject current)
12239     (gnus-summary-position-point)))
12240
12241 (defun gnus-summary-show-all-threads ()
12242   "Show all threads."
12243   (interactive)
12244   (gnus-set-global-variables)
12245   (save-excursion
12246     (let ((buffer-read-only nil))
12247       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12248   (gnus-summary-position-point))
12249
12250 (defun gnus-summary-show-thread ()
12251   "Show thread subtrees.
12252 Returns nil if no thread was there to be shown."
12253   (interactive)
12254   (gnus-set-global-variables)
12255   (let ((buffer-read-only nil)
12256         (orig (point))
12257         ;; first goto end then to beg, to have point at beg after let
12258         (end (progn (end-of-line) (point)))
12259         (beg (progn (beginning-of-line) (point))))
12260     (prog1
12261         ;; Any hidden lines here?
12262         (search-forward "\r" end t)
12263       (subst-char-in-region beg end ?\^M ?\n t)
12264       (goto-char orig)
12265       (gnus-summary-position-point))))
12266
12267 (defun gnus-summary-hide-all-threads ()
12268   "Hide all thread subtrees."
12269   (interactive)
12270   (gnus-set-global-variables)
12271   (save-excursion
12272     (goto-char (point-min))
12273     (gnus-summary-hide-thread)
12274     (while (zerop (gnus-summary-next-thread 1 t))
12275       (gnus-summary-hide-thread)))
12276   (gnus-summary-position-point))
12277
12278 (defun gnus-summary-hide-thread ()
12279   "Hide thread subtrees.
12280 Returns nil if no threads were there to be hidden."
12281   (interactive)
12282   (gnus-set-global-variables)
12283   (let ((buffer-read-only nil)
12284         (start (point))
12285         (article (gnus-summary-article-number))
12286         end)
12287     (goto-char start)
12288     ;; Go forward until either the buffer ends or the subthread
12289     ;; ends.
12290     (when (and (not (eobp))
12291                (or (zerop (gnus-summary-next-thread 1 t))
12292                    (goto-char (point-max))))
12293       (setq end (point))
12294       (prog1
12295           (if (and (> (point) start)
12296                    (search-backward "\n" start t))
12297               (progn
12298                 (subst-char-in-region start (point) ?\n ?\^M)
12299                 (gnus-summary-goto-subject article))
12300             (goto-char start)
12301             nil)
12302         ;;(gnus-summary-position-point)
12303         ))))
12304
12305 (defun gnus-summary-go-to-next-thread (&optional previous)
12306   "Go to the same level (or less) next thread.
12307 If PREVIOUS is non-nil, go to previous thread instead.
12308 Return the article number moved to, or nil if moving was impossible."
12309   (let* ((level (gnus-summary-thread-level))
12310          (article (gnus-summary-article-number))
12311          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12312          oart)
12313     (while data
12314       (if (<= (gnus-data-level (car data)) level)
12315           (setq oart (gnus-data-number (car data))
12316                 data nil)
12317         (setq data (cdr data))))
12318     (and oart
12319          (gnus-summary-goto-subject oart))))
12320
12321 (defun gnus-summary-next-thread (n &optional silent)
12322   "Go to the same level next N'th thread.
12323 If N is negative, search backward instead.
12324 Returns the difference between N and the number of skips actually
12325 done.
12326
12327 If SILENT, don't output messages."
12328   (interactive "p")
12329   (gnus-set-global-variables)
12330   (let ((backward (< n 0))
12331         (n (abs n))
12332         old dum)
12333     (while (and (> n 0)
12334                 (setq old (save-excursion (forward-line 1) (point)))
12335                 (gnus-summary-go-to-next-thread backward))
12336       (when (and (eq gnus-summary-make-false-root 'dummy)
12337                  (setq dum (text-property-not-all
12338                             old (point) 'gnus-intangible nil)))
12339         (goto-char dum))
12340       (decf n))
12341     (unless silent 
12342       (gnus-summary-position-point))
12343     (when (and (not silent) (/= 0 n))
12344       (gnus-message 7 "No more threads"))
12345     n))
12346
12347 (defun gnus-summary-prev-thread (n)
12348   "Go to the same level previous N'th thread.
12349 Returns the difference between N and the number of skips actually
12350 done."
12351   (interactive "p")
12352   (gnus-set-global-variables)
12353   (gnus-summary-next-thread (- n)))
12354
12355 (defun gnus-summary-go-down-thread ()
12356   "Go down one level in the current thread."
12357   (let ((children (gnus-summary-article-children)))
12358     (and children
12359          (gnus-summary-goto-subject (car children)))))
12360
12361 (defun gnus-summary-go-up-thread ()
12362   "Go up one level in the current thread."
12363   (let ((parent (gnus-summary-article-parent)))
12364     (and parent
12365          (gnus-summary-goto-subject parent))))
12366
12367 (defun gnus-summary-down-thread (n)
12368   "Go down thread N steps.
12369 If N is negative, go up instead.
12370 Returns the difference between N and how many steps down that were
12371 taken."
12372   (interactive "p")
12373   (gnus-set-global-variables)
12374   (let ((up (< n 0))
12375         (n (abs n)))
12376     (while (and (> n 0)
12377                 (if up (gnus-summary-go-up-thread)
12378                   (gnus-summary-go-down-thread)))
12379       (setq n (1- n)))
12380     (gnus-summary-position-point)
12381     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12382     n))
12383
12384 (defun gnus-summary-up-thread (n)
12385   "Go up thread N steps.
12386 If N is negative, go up instead.
12387 Returns the difference between N and how many steps down that were
12388 taken."
12389   (interactive "p")
12390   (gnus-set-global-variables)
12391   (gnus-summary-down-thread (- n)))
12392
12393 (defun gnus-summary-top-thread ()
12394   "Go to the top of the thread."
12395   (interactive)
12396   (gnus-set-global-variables)
12397   (while (gnus-summary-go-up-thread))
12398   (gnus-summary-article-number))
12399
12400 (defun gnus-summary-kill-thread (&optional unmark)
12401   "Mark articles under current thread as read.
12402 If the prefix argument is positive, remove any kinds of marks.
12403 If the prefix argument is negative, tick articles instead."
12404   (interactive "P")
12405   (gnus-set-global-variables)
12406   (if unmark
12407       (setq unmark (prefix-numeric-value unmark)))
12408   (let ((articles (gnus-summary-articles-in-thread)))
12409     (save-excursion
12410       ;; Expand the thread.
12411       (gnus-summary-show-thread)
12412       ;; Mark all the articles.
12413       (while articles
12414         (gnus-summary-goto-subject (car articles))
12415         (cond ((null unmark)
12416                (gnus-summary-mark-article-as-read gnus-killed-mark))
12417               ((> unmark 0)
12418                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12419               (t
12420                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12421         (setq articles (cdr articles))))
12422     ;; Hide killed subtrees.
12423     (and (null unmark)
12424          gnus-thread-hide-killed
12425          (gnus-summary-hide-thread))
12426     ;; If marked as read, go to next unread subject.
12427     (if (null unmark)
12428         ;; Go to next unread subject.
12429         (gnus-summary-next-subject 1 t)))
12430   (gnus-set-mode-line 'summary))
12431
12432 ;; Summary sorting commands
12433
12434 (defun gnus-summary-sort-by-number (&optional reverse)
12435   "Sort summary buffer by article number.
12436 Argument REVERSE means reverse order."
12437   (interactive "P")
12438   (gnus-summary-sort 'number reverse))
12439
12440 (defun gnus-summary-sort-by-author (&optional reverse)
12441   "Sort summary buffer by author name alphabetically.
12442 If case-fold-search is non-nil, case of letters is ignored.
12443 Argument REVERSE means reverse order."
12444   (interactive "P")
12445   (gnus-summary-sort 'author reverse))
12446
12447 (defun gnus-summary-sort-by-subject (&optional reverse)
12448   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12449 If case-fold-search is non-nil, case of letters is ignored.
12450 Argument REVERSE means reverse order."
12451   (interactive "P")
12452   (gnus-summary-sort 'subject reverse))
12453
12454 (defun gnus-summary-sort-by-date (&optional reverse)
12455   "Sort summary buffer by date.
12456 Argument REVERSE means reverse order."
12457   (interactive "P")
12458   (gnus-summary-sort 'date reverse))
12459
12460 (defun gnus-summary-sort-by-score (&optional reverse)
12461   "Sort summary buffer by score.
12462 Argument REVERSE means reverse order."
12463   (interactive "P")
12464   (gnus-summary-sort 'score reverse))
12465
12466 (defun gnus-summary-sort (predicate reverse)
12467   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12468   (gnus-set-global-variables)
12469   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12470          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12471          (gnus-thread-sort-functions
12472           (list
12473            (if (not reverse)
12474                thread
12475              `(lambda (t1 t2)
12476                 (,thread t2 t1)))))
12477          (gnus-article-sort-functions
12478           (list
12479            (if (not reverse)
12480                article
12481              `(lambda (t1 t2)
12482                 (,article t2 t1)))))
12483          (buffer-read-only)
12484          (gnus-summary-prepare-hook nil))
12485     ;; We do the sorting by regenerating the threads.
12486     (gnus-summary-prepare)
12487     ;; Hide subthreads if needed.
12488     (when (and gnus-show-threads gnus-thread-hide-subtree)
12489       (gnus-summary-hide-all-threads)))
12490   ;; If in async mode, we send some info to the backend.
12491   (when gnus-newsgroup-async
12492     (gnus-request-asynchronous
12493      gnus-newsgroup-name gnus-newsgroup-data)))
12494
12495 (defun gnus-sortable-date (date)
12496   "Make sortable string by string-lessp from DATE.
12497 Timezone package is used."
12498   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12499          (year (aref date 0))
12500          (month (aref date 1))
12501          (day (aref date 2)))
12502     (timezone-make-sortable-date
12503      year month day
12504      (timezone-make-time-string
12505       (aref date 3) (aref date 4) (aref date 5)))))
12506
12507 ;; Summary saving commands.
12508
12509 (defun gnus-summary-save-article (&optional n not-saved)
12510   "Save the current article using the default saver function.
12511 If N is a positive number, save the N next articles.
12512 If N is a negative number, save the N previous articles.
12513 If N is nil and any articles have been marked with the process mark,
12514 save those articles instead.
12515 The variable `gnus-default-article-saver' specifies the saver function."
12516   (interactive "P")
12517   (gnus-set-global-variables)
12518   (let ((articles (gnus-summary-work-articles n))
12519         file header article)
12520     (while articles
12521       (setq header (gnus-summary-article-header
12522                     (setq article (pop articles))))
12523       (if (not (vectorp header))
12524           ;; This is a pseudo-article.
12525           (if (assq 'name header)
12526               (gnus-copy-file (cdr (assq 'name header)))
12527             (gnus-message 1 "Article %d is unsaveable" article))
12528         ;; This is a real article.
12529         (save-window-excursion
12530           (gnus-summary-select-article t nil nil article))
12531         (unless gnus-save-all-headers
12532           ;; Remove headers accoring to `gnus-saved-headers'.
12533           (let ((gnus-visible-headers
12534                  (or gnus-saved-headers gnus-visible-headers)))
12535             (gnus-article-hide-headers nil t)))
12536         ;; Remove any X-Gnus lines.
12537         (save-excursion
12538           (set-buffer gnus-article-buffer)
12539           (save-restriction
12540             (let ((buffer-read-only nil))
12541               (nnheader-narrow-to-headers)
12542               (while (re-search-forward "^X-Gnus" nil t)
12543                 (gnus-delete-line)))))
12544         (save-window-excursion
12545           (if (not gnus-default-article-saver)
12546               (error "No default saver is defined.")
12547             (setq file (funcall
12548                         gnus-default-article-saver
12549                         (cond
12550                          ((not gnus-prompt-before-saving)
12551                           'default)
12552                          ((eq gnus-prompt-before-saving 'always)
12553                           nil)
12554                          (t file))))))
12555         (gnus-summary-remove-process-mark article)
12556         (unless not-saved
12557           (gnus-summary-set-saved-mark article))))
12558     (gnus-summary-position-point)
12559     n))
12560
12561 (defun gnus-summary-pipe-output (&optional arg)
12562   "Pipe the current article to a subprocess.
12563 If N is a positive number, pipe the N next articles.
12564 If N is a negative number, pipe the N previous articles.
12565 If N is nil and any articles have been marked with the process mark,
12566 pipe those articles instead."
12567   (interactive "P")
12568   (gnus-set-global-variables)
12569   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12570     (gnus-summary-save-article arg t))
12571   (gnus-configure-windows 'pipe))
12572
12573 (defun gnus-summary-save-article-mail (&optional arg)
12574   "Append the current article to an mail file.
12575 If N is a positive number, save the N next articles.
12576 If N is a negative number, save the N previous articles.
12577 If N is nil and any articles have been marked with the process mark,
12578 save those articles instead."
12579   (interactive "P")
12580   (gnus-set-global-variables)
12581   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12582     (gnus-summary-save-article arg)))
12583
12584 (defun gnus-summary-save-article-rmail (&optional arg)
12585   "Append the current article to an rmail file.
12586 If N is a positive number, save the N next articles.
12587 If N is a negative number, save the N previous articles.
12588 If N is nil and any articles have been marked with the process mark,
12589 save those articles instead."
12590   (interactive "P")
12591   (gnus-set-global-variables)
12592   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12593     (gnus-summary-save-article arg)))
12594
12595 (defun gnus-summary-save-article-file (&optional arg)
12596   "Append the current article to a file.
12597 If N is a positive number, save the N next articles.
12598 If N is a negative number, save the N previous articles.
12599 If N is nil and any articles have been marked with the process mark,
12600 save those articles instead."
12601   (interactive "P")
12602   (gnus-set-global-variables)
12603   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12604     (gnus-summary-save-article arg)))
12605
12606 (defun gnus-summary-save-article-body-file (&optional arg)
12607   "Append the current article body to a file.
12608 If N is a positive number, save the N next articles.
12609 If N is a negative number, save the N previous articles.
12610 If N is nil and any articles have been marked with the process mark,
12611 save those articles instead."
12612   (interactive "P")
12613   (gnus-set-global-variables)
12614   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12615     (gnus-summary-save-article arg)))
12616
12617 (defun gnus-get-split-value (methods)
12618   "Return a value based on the split METHODS."
12619   (let (split-name method result match)
12620     (when methods
12621       (save-excursion
12622         (set-buffer gnus-original-article-buffer)
12623         (save-restriction
12624           (nnheader-narrow-to-headers)
12625           (while methods
12626             (goto-char (point-min))
12627             (setq method (pop methods))
12628             (setq match (pop method))
12629             (when (cond
12630                    ((stringp match)
12631                     ;; Regular expression.
12632                     (condition-case ()
12633                         (re-search-forward match nil t)
12634                       (error nil)))
12635                    ((gnus-functionp match)
12636                     ;; Function.
12637                     (save-restriction
12638                       (widen)
12639                       (setq result (funcall match gnus-newsgroup-name))))
12640                    ((consp match)
12641                     ;; Form.
12642                     (save-restriction
12643                       (widen)
12644                       (setq result (eval match)))))
12645               (setq split-name (append (cdr methods) split-name))
12646               (cond ((stringp result)
12647                      (push result split-name))
12648                     ((consp result)
12649                      (setq split-name (append result split-name)))))))))
12650     split-name))
12651
12652 (defun gnus-read-move-group-name (prompt default articles prefix)
12653   "Read a group name."
12654   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12655          (prom
12656           (format "Where do you want to %s %s? "
12657                   prompt
12658                   (if (> (length articles) 1)
12659                       (format "these %d articles" (length articles))
12660                     "this article")))
12661          (to-newsgroup
12662           (cond
12663            ((null split-name)
12664             (completing-read
12665              (concat prom
12666                      (if default
12667                          (format "(default %s) " default)
12668                        ""))
12669              gnus-active-hashtb nil nil prefix))
12670            ((= 1 (length split-name))
12671             (completing-read prom gnus-active-hashtb
12672                              nil nil (cons (car split-name) 0)))
12673            (t
12674             (completing-read
12675              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12676
12677     (when to-newsgroup
12678       (if (or (string= to-newsgroup "")
12679               (string= to-newsgroup prefix))
12680           (setq to-newsgroup (or default "")))
12681       (or (gnus-active to-newsgroup)
12682           (gnus-activate-group to-newsgroup)
12683           (error "No such group: %s" to-newsgroup)))
12684     to-newsgroup))
12685
12686 (defun gnus-read-save-file-name (prompt default-name)
12687   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12688          (file
12689           ;; Let the split methods have their say.
12690           (cond
12691            ;; No split name was found.
12692            ((null split-name)
12693             (read-file-name
12694              (concat prompt " (default "
12695                      (file-name-nondirectory default-name) ") ")
12696              (file-name-directory default-name)
12697              default-name))
12698            ;; A single split name was found
12699            ((= 1 (length split-name))
12700             (read-file-name
12701              (concat prompt " (default " (car split-name) ") ")
12702              gnus-article-save-directory
12703              (concat gnus-article-save-directory (car split-name))))
12704            ;; A list of splits was found.
12705            (t
12706             (setq split-name (mapcar (lambda (el) (list el))
12707                                      (nreverse split-name)))
12708             (let ((result (completing-read
12709                            (concat prompt " ") split-name nil nil)))
12710               (concat gnus-article-save-directory
12711                       (if (string= result "")
12712                           (caar split-name)
12713                         result)))))))
12714     ;; If we have read a directory, we append the default file name.
12715     (when (file-directory-p file)
12716       (setq file (concat (file-name-as-directory file)
12717                          (file-name-nondirectory default-name))))
12718     ;; Possibly translate some charaters.
12719     (nnheader-translate-file-chars file)))
12720
12721 (defun gnus-article-archive-name (group)
12722   "Return the first instance of an \"Archive-name\" in the current buffer."
12723   (let ((case-fold-search t))
12724     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12725       (match-string 1))))
12726
12727 (defun gnus-summary-save-in-rmail (&optional filename)
12728   "Append this article to Rmail file.
12729 Optional argument FILENAME specifies file name.
12730 Directory to save to is default to `gnus-article-save-directory' which
12731 is initialized from the SAVEDIR environment variable."
12732   (interactive)
12733   (gnus-set-global-variables)
12734   (let ((default-name
12735           (funcall gnus-rmail-save-name gnus-newsgroup-name
12736                    gnus-current-headers gnus-newsgroup-last-rmail)))
12737     (setq filename
12738           (cond ((eq filename 'default)
12739                  default-name)
12740                 (filename filename)
12741                 (t (gnus-read-save-file-name
12742                     "Save in rmail file:" default-name))))
12743     (gnus-make-directory (file-name-directory filename))
12744     (gnus-eval-in-buffer-window
12745      gnus-original-article-buffer
12746      (save-excursion
12747        (save-restriction
12748          (widen)
12749          (gnus-output-to-rmail filename))))
12750     ;; Remember the directory name to save articles
12751     (setq gnus-newsgroup-last-rmail filename)))
12752
12753 (defun gnus-summary-save-in-mail (&optional filename)
12754   "Append this article to Unix mail file.
12755 Optional argument FILENAME specifies file name.
12756 Directory to save to is default to `gnus-article-save-directory' which
12757 is initialized from the SAVEDIR environment variable."
12758   (interactive)
12759   (gnus-set-global-variables)
12760   (let ((default-name
12761           (funcall gnus-mail-save-name gnus-newsgroup-name
12762                    gnus-current-headers gnus-newsgroup-last-mail)))
12763     (setq filename
12764           (cond ((eq filename 'default)
12765                  default-name)
12766                 (filename filename)
12767                 (t (gnus-read-save-file-name
12768                     "Save in Unix mail file:" default-name))))
12769     (setq filename
12770           (expand-file-name filename
12771                             (and default-name
12772                                  (file-name-directory default-name))))
12773     (gnus-make-directory (file-name-directory filename))
12774     (gnus-eval-in-buffer-window
12775      gnus-original-article-buffer
12776      (save-excursion
12777        (save-restriction
12778          (widen)
12779          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12780              (gnus-output-to-rmail filename)
12781            (let ((mail-use-rfc822 t))
12782              (rmail-output filename 1 t t))))))
12783     ;; Remember the directory name to save articles.
12784     (setq gnus-newsgroup-last-mail filename)))
12785
12786 (defun gnus-summary-save-in-file (&optional filename)
12787   "Append this article to file.
12788 Optional argument FILENAME specifies file name.
12789 Directory to save to is default to `gnus-article-save-directory' which
12790 is initialized from the SAVEDIR environment variable."
12791   (interactive)
12792   (gnus-set-global-variables)
12793   (let ((default-name
12794           (funcall gnus-file-save-name gnus-newsgroup-name
12795                    gnus-current-headers gnus-newsgroup-last-file)))
12796     (setq filename
12797           (cond ((eq filename 'default)
12798                  default-name)
12799                 (filename filename)
12800                 (t (gnus-read-save-file-name
12801                     "Save in file:" default-name))))
12802     (gnus-make-directory (file-name-directory filename))
12803     (gnus-eval-in-buffer-window
12804      gnus-original-article-buffer
12805      (save-excursion
12806        (save-restriction
12807          (widen)
12808          (gnus-output-to-file filename))))
12809     ;; Remember the directory name to save articles.
12810     (setq gnus-newsgroup-last-file filename)))
12811
12812 (defun gnus-summary-save-body-in-file (&optional filename)
12813   "Append this article body to a file.
12814 Optional argument FILENAME specifies file name.
12815 The directory to save in defaults to `gnus-article-save-directory' which
12816 is initialized from the SAVEDIR environment variable."
12817   (interactive)
12818   (gnus-set-global-variables)
12819   (let ((default-name
12820           (funcall gnus-file-save-name gnus-newsgroup-name
12821                    gnus-current-headers gnus-newsgroup-last-file)))
12822     (setq filename
12823           (cond ((eq filename 'default)
12824                  default-name)
12825                 (filename filename)
12826                 (t (gnus-read-save-file-name
12827                     "Save body in file:" default-name))))
12828     (gnus-make-directory (file-name-directory filename))
12829     (gnus-eval-in-buffer-window
12830      gnus-article-buffer
12831      (save-excursion
12832        (save-restriction
12833          (widen)
12834          (goto-char (point-min))
12835          (and (search-forward "\n\n" nil t)
12836               (narrow-to-region (point) (point-max)))
12837          (gnus-output-to-file filename))))
12838     ;; Remember the directory name to save articles.
12839     (setq gnus-newsgroup-last-file filename)))
12840
12841 (defun gnus-summary-save-in-pipe (&optional command)
12842   "Pipe this article to subprocess."
12843   (interactive)
12844   (gnus-set-global-variables)
12845   (setq command
12846         (cond ((eq command 'default)
12847                gnus-last-shell-command)
12848               (command command)
12849               (t (read-string "Shell command on article: "
12850                               gnus-last-shell-command))))
12851   (if (string-equal command "")
12852       (setq command gnus-last-shell-command))
12853   (gnus-eval-in-buffer-window
12854    gnus-article-buffer
12855    (save-restriction
12856      (widen)
12857      (shell-command-on-region (point-min) (point-max) command nil)))
12858   (setq gnus-last-shell-command command))
12859
12860 ;; Summary extract commands
12861
12862 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12863   (let ((buffer-read-only nil)
12864         (article (gnus-summary-article-number))
12865         after-article b e)
12866     (or (gnus-summary-goto-subject article)
12867         (error (format "No such article: %d" article)))
12868     (gnus-summary-position-point)
12869     ;; If all commands are to be bunched up on one line, we collect
12870     ;; them here.
12871     (if gnus-view-pseudos-separately
12872         ()
12873       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12874             files action)
12875         (while ps
12876           (setq action (cdr (assq 'action (car ps))))
12877           (setq files (list (cdr (assq 'name (car ps)))))
12878           (while (and ps (cdr ps)
12879                       (string= (or action "1")
12880                                (or (cdr (assq 'action (cadr ps))) "2")))
12881             (setq files (cons (cdr (assq 'name (cadr ps))) files))
12882             (setcdr ps (cddr ps)))
12883           (if (not files)
12884               ()
12885             (if (not (string-match "%s" action))
12886                 (setq files (cons " " files)))
12887             (setq files (cons " " files))
12888             (and (assq 'execute (car ps))
12889                  (setcdr (assq 'execute (car ps))
12890                          (funcall (if (string-match "%s" action)
12891                                       'format 'concat)
12892                                   action
12893                                   (mapconcat (lambda (f) f) files " ")))))
12894           (setq ps (cdr ps)))))
12895     (if (and gnus-view-pseudos (not not-view))
12896         (while pslist
12897           (and (assq 'execute (car pslist))
12898                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12899                                      (eq gnus-view-pseudos 'not-confirm)))
12900           (setq pslist (cdr pslist)))
12901       (save-excursion
12902         (while pslist
12903           (setq after-article (or (cdr (assq 'article (car pslist)))
12904                                   (gnus-summary-article-number)))
12905           (gnus-summary-goto-subject after-article)
12906           (forward-line 1)
12907           (setq b (point))
12908           (insert "          " (file-name-nondirectory
12909                                 (cdr (assq 'name (car pslist))))
12910                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12911           (setq e (point))
12912           (forward-line -1)             ; back to `b'
12913           (add-text-properties
12914            b e (list 'gnus-number gnus-reffed-article-number
12915                      gnus-mouse-face-prop gnus-mouse-face))
12916           (gnus-data-enter
12917            after-article gnus-reffed-article-number
12918            gnus-unread-mark b (car pslist) 0 (- e b))
12919           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12920           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12921           (setq pslist (cdr pslist)))))))
12922
12923 (defun gnus-pseudos< (p1 p2)
12924   (let ((c1 (cdr (assq 'action p1)))
12925         (c2 (cdr (assq 'action p2))))
12926     (and c1 c2 (string< c1 c2))))
12927
12928 (defun gnus-request-pseudo-article (props)
12929   (cond ((assq 'execute props)
12930          (gnus-execute-command (cdr (assq 'execute props)))))
12931   (let ((gnus-current-article (gnus-summary-article-number)))
12932     (run-hooks 'gnus-mark-article-hook)))
12933
12934 (defun gnus-execute-command (command &optional automatic)
12935   (save-excursion
12936     (gnus-article-setup-buffer)
12937     (set-buffer gnus-article-buffer)
12938     (let ((command (if automatic command (read-string "Command: " command)))
12939           (buffer-read-only nil))
12940       (erase-buffer)
12941       (insert "$ " command "\n\n")
12942       (if gnus-view-pseudo-asynchronously
12943           (start-process "gnus-execute" nil "sh" "-c" command)
12944         (call-process "sh" nil t nil "-c" command)))))
12945
12946 (defun gnus-copy-file (file &optional to)
12947   "Copy FILE to TO."
12948   (interactive
12949    (list (read-file-name "Copy file: " default-directory)
12950          (read-file-name "Copy file to: " default-directory)))
12951   (gnus-set-global-variables)
12952   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12953   (and (file-directory-p to)
12954        (setq to (concat (file-name-as-directory to)
12955                         (file-name-nondirectory file))))
12956   (copy-file file to))
12957
12958 ;; Summary kill commands.
12959
12960 (defun gnus-summary-edit-global-kill (article)
12961   "Edit the \"global\" kill file."
12962   (interactive (list (gnus-summary-article-number)))
12963   (gnus-set-global-variables)
12964   (gnus-group-edit-global-kill article))
12965
12966 (defun gnus-summary-edit-local-kill ()
12967   "Edit a local kill file applied to the current newsgroup."
12968   (interactive)
12969   (gnus-set-global-variables)
12970   (setq gnus-current-headers (gnus-summary-article-header))
12971   (gnus-set-global-variables)
12972   (gnus-group-edit-local-kill
12973    (gnus-summary-article-number) gnus-newsgroup-name))
12974
12975 \f
12976 ;;;
12977 ;;; Gnus article mode
12978 ;;;
12979
12980 (put 'gnus-article-mode 'mode-class 'special)
12981
12982 (if gnus-article-mode-map
12983     nil
12984   (setq gnus-article-mode-map (make-keymap))
12985   (suppress-keymap gnus-article-mode-map)
12986
12987   (gnus-define-keys gnus-article-mode-map
12988     " " gnus-article-goto-next-page
12989     "\177" gnus-article-goto-prev-page
12990     [delete] gnus-article-goto-prev-page
12991     "\C-c^" gnus-article-refer-article
12992     "h" gnus-article-show-summary
12993     "s" gnus-article-show-summary
12994     "\C-c\C-m" gnus-article-mail
12995     "?" gnus-article-describe-briefly
12996     gnus-mouse-2 gnus-article-push-button
12997     "\r" gnus-article-press-button
12998     "\t" gnus-article-next-button
12999     "\M-\t" gnus-article-prev-button
13000     "\C-c\C-b" gnus-bug)
13001
13002   (substitute-key-definition
13003    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13004
13005 (defun gnus-article-mode ()
13006   "Major mode for displaying an article.
13007
13008 All normal editing commands are switched off.
13009
13010 The following commands are available:
13011
13012 \\<gnus-article-mode-map>
13013 \\[gnus-article-next-page]\t Scroll the article one page forwards
13014 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13015 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13016 \\[gnus-article-show-summary]\t Display the summary buffer
13017 \\[gnus-article-mail]\t Send a reply to the address near point
13018 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13019 \\[gnus-info-find-node]\t Go to the Gnus info node"
13020   (interactive)
13021   (when (and menu-bar-mode
13022              (gnus-visual-p 'article-menu 'menu))
13023     (gnus-article-make-menu-bar))
13024   (kill-all-local-variables)
13025   (gnus-simplify-mode-line)
13026   (setq mode-name "Article")
13027   (setq major-mode 'gnus-article-mode)
13028   (make-local-variable 'minor-mode-alist)
13029   (or (assq 'gnus-show-mime minor-mode-alist)
13030       (setq minor-mode-alist
13031             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13032   (use-local-map gnus-article-mode-map)
13033   (make-local-variable 'page-delimiter)
13034   (setq page-delimiter gnus-page-delimiter)
13035   (buffer-disable-undo (current-buffer))
13036   (setq buffer-read-only t)             ;Disable modification
13037   (run-hooks 'gnus-article-mode-hook))
13038
13039 (defun gnus-article-setup-buffer ()
13040   "Initialize the article buffer."
13041   (let* ((name (if gnus-single-article-buffer "*Article*"
13042                  (concat "*Article " gnus-newsgroup-name "*")))
13043          (original
13044           (progn (string-match "\\*Article" name)
13045                  (concat " *Original Article"
13046                          (substring name (match-end 0))))))
13047     (setq gnus-article-buffer name)
13048     (setq gnus-original-article-buffer original)
13049     ;; This might be a variable local to the summary buffer.
13050     (unless gnus-single-article-buffer
13051       (save-excursion
13052         (set-buffer gnus-summary-buffer)
13053         (setq gnus-article-buffer name)
13054         (setq gnus-original-article-buffer original)
13055         (gnus-set-global-variables))
13056       (make-local-variable 'gnus-summary-buffer))
13057     (if (get-buffer name)
13058         (save-excursion
13059           (set-buffer name)
13060           (buffer-disable-undo (current-buffer))
13061           (setq buffer-read-only t)
13062           (gnus-add-current-to-buffer-list)
13063           (or (eq major-mode 'gnus-article-mode)
13064               (gnus-article-mode))
13065           (current-buffer))
13066       (save-excursion
13067         (set-buffer (get-buffer-create name))
13068         (gnus-add-current-to-buffer-list)
13069         (gnus-article-mode)
13070         (current-buffer)))))
13071
13072 ;; Set article window start at LINE, where LINE is the number of lines
13073 ;; from the head of the article.
13074 (defun gnus-article-set-window-start (&optional line)
13075   (set-window-start
13076    (get-buffer-window gnus-article-buffer)
13077    (save-excursion
13078      (set-buffer gnus-article-buffer)
13079      (goto-char (point-min))
13080      (if (not line)
13081          (point-min)
13082        (gnus-message 6 "Moved to bookmark")
13083        (search-forward "\n\n" nil t)
13084        (forward-line line)
13085        (point)))))
13086
13087 (defun gnus-kill-all-overlays ()
13088   "Delete all overlays in the current buffer."
13089   (when (fboundp 'overlay-lists)
13090     (let* ((overlayss (overlay-lists))
13091            (buffer-read-only nil)
13092            (overlays (nconc (car overlayss) (cdr overlayss))))
13093       (while overlays
13094         (delete-overlay (pop overlays))))))
13095
13096 (defun gnus-request-article-this-buffer (article group)
13097   "Get an article and insert it into this buffer."
13098   (prog1
13099       (save-excursion
13100         (erase-buffer)
13101         (gnus-kill-all-overlays)
13102         (setq group (or group gnus-newsgroup-name))
13103
13104         ;; Open server if it has closed.
13105         (gnus-check-server (gnus-find-method-for-group group))
13106
13107         ;; Using `gnus-request-article' directly will insert the article into
13108         ;; `nntp-server-buffer' - so we'll save some time by not having to
13109         ;; copy it from the server buffer into the article buffer.
13110
13111         ;; We only request an article by message-id when we do not have the
13112         ;; headers for it, so we'll have to get those.
13113         (when (stringp article)
13114           (let ((gnus-override-method gnus-refer-article-method))
13115             (gnus-read-header article)))
13116
13117         ;; If the article number is negative, that means that this article
13118         ;; doesn't belong in this newsgroup (possibly), so we find its
13119         ;; message-id and request it by id instead of number.
13120         (when (and (numberp article)
13121                    gnus-summary-buffer
13122                    (buffer-name gnus-summary-buffer))
13123           (save-excursion
13124             (set-buffer gnus-summary-buffer)
13125             (let ((header (gnus-summary-article-header article)))
13126               (if (< article 0)
13127                   (cond 
13128                    ((memq article gnus-newsgroup-sparse)
13129                     ;; This is a sparse gap article.
13130                     (setq article (mail-header-id header)))
13131                    ((vectorp header)
13132                     ;; It's a real article.
13133                     (setq article (mail-header-id header)))
13134                    (t
13135                     ;; It is an extracted pseudo-article.
13136                     (setq article 'pseudo)
13137                     (gnus-request-pseudo-article header))))
13138                 
13139               (let ((method (gnus-find-method-for-group 
13140                              gnus-newsgroup-name)))
13141                 (if (not (eq (car method) 'nneething))
13142                     ()
13143                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13144                                      (mail-header-subject header))))
13145                     (if (file-directory-p dir)
13146                         (progn
13147                           (setq article 'nneething)
13148                           (gnus-group-enter-directory dir)))))))))
13149
13150         (cond
13151          ;; We first check `gnus-original-article-buffer'.
13152          ((and (equal (car gnus-original-article) group)
13153                (eq (cdr gnus-original-article) article)
13154                (get-buffer gnus-original-article-buffer))
13155           (insert-buffer-substring gnus-original-article-buffer)
13156           'article)
13157          ;; Check the backlog.
13158          ((and gnus-keep-backlog
13159                (gnus-backlog-request-article group article (current-buffer)))
13160           'article)
13161          ;; Check the cache.
13162          ((and gnus-use-cache
13163                (numberp article)
13164                (gnus-cache-request-article article group))
13165           'article)
13166          ;; Get the article and put into the article buffer.
13167          ((or (stringp article) (numberp article))
13168           (let ((gnus-override-method
13169                  (and (stringp article) gnus-refer-article-method))
13170                 (buffer-read-only nil))
13171             (erase-buffer)
13172             (gnus-kill-all-overlays)
13173             (if (gnus-request-article article group (current-buffer))
13174                 (progn
13175                   (and gnus-keep-backlog
13176                        (gnus-backlog-enter-article
13177                         group article (current-buffer)))
13178                   'article))))
13179          ;; It was a pseudo.
13180          (t article)))
13181
13182     ;; Take the article from the original article buffer
13183     ;; and place it in the buffer it's supposed to be in.
13184     (setq gnus-original-article (cons group article))
13185     (when (and (get-buffer gnus-article-buffer)
13186                (equal (buffer-name (current-buffer))
13187                       (buffer-name (get-buffer gnus-article-buffer))))
13188       (save-excursion
13189         (if (get-buffer gnus-original-article-buffer)
13190             (set-buffer (get-buffer gnus-original-article-buffer))
13191           (set-buffer (get-buffer-create gnus-original-article-buffer))
13192           (buffer-disable-undo (current-buffer))
13193           (setq major-mode 'gnus-original-article-mode)
13194           (setq buffer-read-only t)
13195           (gnus-add-current-to-buffer-list))
13196         (let (buffer-read-only)
13197           (erase-buffer)
13198           (insert-buffer-substring gnus-article-buffer))))
13199     
13200     ;; Update sparse articles.
13201     (when (memq article gnus-newsgroup-sparse)
13202       (gnus-summary-update-article article))))
13203
13204 (defun gnus-read-header (id)
13205   "Read the headers of article ID and enter them into the Gnus system."
13206   (let ((group gnus-newsgroup-name)
13207         (headers gnus-newsgroup-headers)
13208         header where)
13209     ;; First we check to see whether the header in question is already
13210     ;; fetched.
13211     (if (stringp id)
13212         ;; This is a Message-ID.
13213         (setq header (gnus-id-to-header id))
13214       ;; This is an article number.
13215       (setq header (gnus-summary-article-header id)))
13216     (if header
13217         ;; We have found the header.
13218         header
13219       ;; We have to really fetch the header to this article.
13220       (when (setq where
13221                   (if (gnus-check-backend-function 'request-head group)
13222                       (gnus-request-head id group)
13223                     (gnus-request-article id group)))
13224         (save-excursion
13225           (set-buffer nntp-server-buffer)
13226           (and (search-forward "\n\n" nil t)
13227                (delete-region (1- (point)) (point-max)))
13228           (goto-char (point-max))
13229           (insert ".\n")
13230           (goto-char (point-min))
13231           (insert "211 "
13232                   (int-to-string
13233                    (cond
13234                     ((numberp id)
13235                      id)
13236                     ((cdr where)
13237                      (cdr where))
13238                     (t
13239                      gnus-reffed-article-number)))
13240                   " Article retrieved.\n"))
13241         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13242             () ; Malformed head.
13243           (if (and (stringp id)
13244                    (not (string= (gnus-group-real-name group)
13245                                  (car where))))
13246               ;; If we fetched by Message-ID and the article came
13247               ;; from a different group, we fudge some bogus article
13248               ;; numbers for this article.
13249               (mail-header-set-number header gnus-reffed-article-number))
13250           (decf gnus-reffed-article-number)
13251           (push header gnus-newsgroup-headers)
13252           (setq gnus-current-headers header)
13253           (push (mail-header-number header) gnus-newsgroup-limit)
13254           header)))))
13255
13256 (defun gnus-article-prepare (article &optional all-headers header)
13257   "Prepare ARTICLE in article mode buffer.
13258 ARTICLE should either be an article number or a Message-ID.
13259 If ARTICLE is an id, HEADER should be the article headers.
13260 If ALL-HEADERS is non-nil, no headers are hidden."
13261   (save-excursion
13262     ;; Make sure we start in a summary buffer.
13263     (unless (eq major-mode 'gnus-summary-mode)
13264       (set-buffer gnus-summary-buffer))
13265     (setq gnus-summary-buffer (current-buffer))
13266     ;; Make sure the connection to the server is alive.
13267     (unless (gnus-server-opened
13268              (gnus-find-method-for-group gnus-newsgroup-name))
13269       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13270       (gnus-request-group gnus-newsgroup-name t))
13271     (let* ((article (if header (mail-header-number header) article))
13272            (summary-buffer (current-buffer))
13273            (internal-hook gnus-article-internal-prepare-hook)
13274            (group gnus-newsgroup-name)
13275            result)
13276       (save-excursion
13277         (gnus-article-setup-buffer)
13278         (set-buffer gnus-article-buffer)
13279         ;; Deactivate active regions.
13280         (when (and (boundp 'transient-mark-mode)
13281                    transient-mark-mode)
13282           (setq mark-active nil))
13283         (if (not (setq result (let ((buffer-read-only nil))
13284                                 (gnus-request-article-this-buffer
13285                                  article group))))
13286             ;; There is no such article.
13287             (save-excursion
13288               (when (and (numberp article)
13289                          (not (memq article gnus-newsgroup-sparse)))
13290                 (setq gnus-article-current
13291                       (cons gnus-newsgroup-name article))
13292                 (set-buffer gnus-summary-buffer)
13293                 (setq gnus-current-article article)
13294                 (gnus-summary-mark-article article gnus-canceled-mark))
13295               (unless (memq article gnus-newsgroup-sparse)
13296                 (gnus-message
13297                  1 "No such article (may have expired or been canceled)")
13298                 (ding)
13299                 nil))
13300           (if (or (eq result 'pseudo) (eq result 'nneething))
13301               (progn
13302                 (save-excursion
13303                   (set-buffer summary-buffer)
13304                   (setq gnus-last-article gnus-current-article
13305                         gnus-newsgroup-history (cons gnus-current-article
13306                                                      gnus-newsgroup-history)
13307                         gnus-current-article 0
13308                         gnus-current-headers nil
13309                         gnus-article-current nil)
13310                   (if (eq result 'nneething)
13311                       (gnus-configure-windows 'summary)
13312                     (gnus-configure-windows 'article))
13313                   (gnus-set-global-variables))
13314                 (gnus-set-mode-line 'article))
13315             ;; The result from the `request' was an actual article -
13316             ;; or at least some text that is now displayed in the
13317             ;; article buffer.
13318             (if (and (numberp article)
13319                      (not (eq article gnus-current-article)))
13320                 ;; Seems like a new article has been selected.
13321                 ;; `gnus-current-article' must be an article number.
13322                 (save-excursion
13323                   (set-buffer summary-buffer)
13324                   (setq gnus-last-article gnus-current-article
13325                         gnus-newsgroup-history (cons gnus-current-article
13326                                                      gnus-newsgroup-history)
13327                         gnus-current-article article
13328                         gnus-current-headers
13329                         (gnus-summary-article-header gnus-current-article)
13330                         gnus-article-current
13331                         (cons gnus-newsgroup-name gnus-current-article))
13332                   (unless (vectorp gnus-current-headers)
13333                     (setq gnus-current-headers nil))
13334                   (gnus-summary-show-thread)
13335                   (run-hooks 'gnus-mark-article-hook)
13336                   (gnus-set-mode-line 'summary)
13337                   (and (gnus-visual-p 'article-highlight 'highlight)
13338                        (run-hooks 'gnus-visual-mark-article-hook))
13339                   ;; Set the global newsgroup variables here.
13340                   ;; Suggested by Jim Sisolak
13341                   ;; <sisolak@trans4.neep.wisc.edu>.
13342                   (gnus-set-global-variables)
13343                   (setq gnus-have-all-headers
13344                         (or all-headers gnus-show-all-headers))
13345                   (and gnus-use-cache
13346                        (vectorp (gnus-summary-article-header article))
13347                        (gnus-cache-possibly-enter-article
13348                         group article
13349                         (gnus-summary-article-header article)
13350                         (memq article gnus-newsgroup-marked)
13351                         (memq article gnus-newsgroup-dormant)
13352                         (memq article gnus-newsgroup-unreads)))))
13353             ;; Hooks for getting information from the article.
13354             ;; This hook must be called before being narrowed.
13355             (let (buffer-read-only)
13356               (run-hooks 'internal-hook)
13357               (run-hooks 'gnus-article-prepare-hook)
13358               ;; Decode MIME message.
13359               (if gnus-show-mime
13360                   (if (or (not gnus-strict-mime)
13361                           (gnus-fetch-field "Mime-Version"))
13362                       (funcall gnus-show-mime-method)
13363                     (funcall gnus-decode-encoded-word-method)))
13364               ;; Perform the article display hooks.
13365               (run-hooks 'gnus-article-display-hook))
13366             ;; Do page break.
13367             (goto-char (point-min))
13368             (and gnus-break-pages (gnus-narrow-to-page))
13369             (gnus-set-mode-line 'article)
13370             (gnus-configure-windows 'article)
13371             (goto-char (point-min))
13372             t))))))
13373
13374 (defun gnus-article-show-all-headers ()
13375   "Show all article headers in article mode buffer."
13376   (save-excursion
13377     (gnus-article-setup-buffer)
13378     (set-buffer gnus-article-buffer)
13379     (let ((buffer-read-only nil))
13380       (remove-text-properties (point-min) (point-max)
13381                               gnus-hidden-properties))))
13382
13383 (defun gnus-article-hide-headers-if-wanted ()
13384   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13385 Provided for backwards compatibility."
13386   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13387       gnus-inhibit-hiding
13388       (gnus-article-hide-headers)))
13389
13390 (defun gnus-article-hide-headers (&optional arg delete)
13391   "Toggle whether to hide unwanted headers and possibly sort them as well.
13392 If given a negative prefix, always show; if given a positive prefix,
13393 always hide."
13394   (interactive "P")
13395   (unless (gnus-article-check-hidden-text 'headers arg)
13396     ;; This function might be inhibited.
13397     (unless gnus-inhibit-hiding
13398       (save-excursion
13399         (set-buffer gnus-article-buffer)
13400         (save-restriction
13401           (let ((buffer-read-only nil)
13402                 (ignored (when (not (stringp gnus-visible-headers))
13403                            (cond ((stringp gnus-ignored-headers)
13404                                   gnus-ignored-headers)
13405                                  ((listp gnus-ignored-headers)
13406                                   (mapconcat 'identity gnus-ignored-headers
13407                                              "\\|")))))
13408                 (visible
13409                  (cond ((stringp gnus-visible-headers)
13410                         gnus-visible-headers)
13411                        ((and gnus-visible-headers
13412                              (listp gnus-visible-headers))
13413                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13414                 want-list beg want-l)
13415             ;; First we narrow to just the headers.
13416             (widen)
13417             (goto-char (point-min))
13418             ;; Hide any "From " lines at the beginning of (mail) articles.
13419             (while (looking-at "From ")
13420               (forward-line 1))
13421             (unless (bobp)
13422               (add-text-properties
13423                (point-min) (point)
13424                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13425             ;; Then treat the rest of the header lines.
13426             (narrow-to-region
13427              (point)
13428              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13429             ;; Then we use the two regular expressions
13430             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13431             ;; select which header lines is to remain visible in the
13432             ;; article buffer.
13433             (goto-char (point-min))
13434             (while (re-search-forward "^[^ \t]*:" nil t)
13435               (beginning-of-line)
13436               ;; We add the headers we want to keep to a list and delete
13437               ;; them from the buffer.
13438               (if (or (and visible (looking-at visible))
13439                       (and ignored (not (looking-at ignored))))
13440                   (progn
13441                     (push (buffer-substring
13442                            (setq beg (point))
13443                            (progn
13444                              (forward-line 1)
13445                              ;; Be sure to get multi-line headers...
13446                              (re-search-forward "^[^ \t]*:" nil t)
13447                              (beginning-of-line)
13448                              (point)))
13449                           want-list)
13450                     (delete-region beg (point)))
13451                 (forward-line 1)))
13452             ;; Sort the headers that we want to display.
13453             (setq want-list (sort want-list 'gnus-article-header-less))
13454             (goto-char (point-min))
13455             (while want-list
13456               (insert (pop want-list)))
13457             ;; We make the unwanted headers invisible.
13458             (if delete
13459                 (delete-region (point-min) (point-max))
13460               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13461               (add-text-properties
13462                (point) (point-max)
13463                (nconc (list 'gnus-type 'headers)
13464                       gnus-hidden-properties)))))))))
13465
13466 (defsubst gnus-article-header-rank (header)
13467   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13468   (let ((list gnus-sorted-header-list)
13469         (i 0))
13470     (while list
13471       (when (string-match (car list) header)
13472         (setq list nil))
13473       (setq list (cdr list))
13474       (incf i))
13475     i))
13476
13477 (defun gnus-article-header-less (h1 h2)
13478   "Say whether string H1 is \"less\" than string H2."
13479   (< (gnus-article-header-rank h1)
13480      (gnus-article-header-rank h2)))
13481
13482 (defun gnus-article-hide-boring-headers (&optional arg)
13483   "Toggle hiding of headers that aren't very interesting.
13484 If given a negative prefix, always show; if given a positive prefix,
13485 always hide."
13486   (interactive "P")
13487   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13488     (save-excursion
13489       (set-buffer gnus-article-buffer)
13490       (save-restriction
13491         (let ((buffer-read-only nil)
13492               (list gnus-boring-article-headers)
13493               (inhibit-point-motion-hooks t)
13494               elem)
13495           (nnheader-narrow-to-headers)
13496           (while list
13497             (setq elem (pop list))
13498             (goto-char (point-min))
13499             (cond
13500              ;; Hide empty headers.
13501              ((eq elem 'empty)
13502               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13503                 (forward-line -1)
13504                 (add-text-properties
13505                  (progn (beginning-of-line) (point))
13506                  (progn 
13507                    (end-of-line)
13508                    (if (re-search-forward "^[^ \t]" nil t)
13509                        (match-beginning 0)
13510                      (point-max)))
13511                  (nconc (list 'gnus-type 'boring-headers)
13512                         gnus-hidden-properties))))
13513              ;; Hide boring Newsgroups header.
13514              ((eq elem 'newsgroups)
13515               (when (equal (mail-fetch-field "newsgroups")
13516                            (gnus-group-real-name gnus-newsgroup-name))
13517                 (gnus-article-hide-header "newsgroups")))
13518              ((eq elem 'followup-to)
13519               (when (equal (mail-fetch-field "followup-to")
13520                            (mail-fetch-field "newsgroups"))
13521                 (gnus-article-hide-header "followup-to")))
13522              ((eq elem 'reply-to)
13523               (let ((from (mail-fetch-field "from"))
13524                     (reply-to (mail-fetch-field "reply-to")))
13525                 (when (and
13526                        from reply-to
13527                        (equal 
13528                         (nth 1 (mail-extract-address-components from))
13529                         (nth 1 (mail-extract-address-components reply-to))))
13530                   (gnus-article-hide-header "reply-to"))))
13531              ((eq elem 'date)
13532               (let ((date (mail-fetch-field "date")))
13533                 (when (and date
13534                            (< (gnus-days-between date (current-time-string))
13535                               4))
13536                   (gnus-article-hide-header "date")))))))))))
13537
13538 (defun gnus-article-hide-header (header)
13539   (save-excursion
13540     (goto-char (point-min))
13541     (when (re-search-forward (concat "^" header ":") nil t)
13542       (add-text-properties
13543        (progn (beginning-of-line) (point))
13544        (progn 
13545          (end-of-line)
13546          (if (re-search-forward "^[^ \t]" nil t)
13547              (match-beginning 0)
13548            (point-max)))
13549        (nconc (list 'gnus-type 'boring-headers)
13550               gnus-hidden-properties)))))
13551
13552 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13553 (defun gnus-article-treat-overstrike ()
13554   "Translate overstrikes into bold text."
13555   (interactive)
13556   (save-excursion
13557     (set-buffer gnus-article-buffer)
13558     (let ((buffer-read-only nil))
13559       (while (search-forward "\b" nil t)
13560         (let ((next (following-char))
13561               (previous (char-after (- (point) 2))))
13562           (cond ((eq next previous)
13563                  (put-text-property (- (point) 2) (point) 'invisible t)
13564                  (put-text-property (point) (1+ (point)) 'face 'bold))
13565                 ((eq next ?_)
13566                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13567                  (put-text-property
13568                   (- (point) 2) (1- (point)) 'face 'underline))
13569                 ((eq previous ?_)
13570                  (put-text-property (- (point) 2) (point) 'invisible t)
13571                  (put-text-property
13572                   (point) (1+ (point))  'face 'underline))))))))
13573
13574 (defun gnus-article-word-wrap ()
13575   "Format too long lines."
13576   (interactive)
13577   (save-excursion
13578     (set-buffer gnus-article-buffer)
13579     (let ((buffer-read-only nil)
13580           p)
13581       (widen)
13582       (goto-char (point-min))
13583       (search-forward "\n\n" nil t)
13584       (end-of-line 1)
13585       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13586             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13587             (adaptive-fill-mode t))
13588         (while (not (eobp))
13589           (and (>= (current-column) (min fill-column (window-width)))
13590                (/= (preceding-char) ?:)
13591                (fill-paragraph nil))
13592           (end-of-line 2))))))
13593
13594 (defun gnus-article-remove-cr ()
13595   "Remove carriage returns from an article."
13596   (interactive)
13597   (save-excursion
13598     (set-buffer gnus-article-buffer)
13599     (let ((buffer-read-only nil))
13600       (goto-char (point-min))
13601       (while (search-forward "\r" nil t)
13602         (replace-match "" t t)))))
13603
13604 (defun gnus-article-remove-trailing-blank-lines ()
13605   "Remove all trailing blank lines from the article."
13606   (interactive)
13607   (save-excursion
13608     (set-buffer gnus-article-buffer)
13609     (let ((buffer-read-only nil))
13610       (goto-char (point-max))
13611       (delete-region
13612        (point)
13613        (progn
13614          (while (looking-at "^[ \t]*$")
13615            (forward-line -1))
13616          (forward-line 1)
13617          (point))))))
13618
13619 (defun gnus-article-display-x-face (&optional force)
13620   "Look for an X-Face header and display it if present."
13621   (interactive (list 'force))
13622   (save-excursion
13623     (set-buffer gnus-article-buffer)
13624     ;; Delete the old process, if any.
13625     (when (process-status "gnus-x-face")
13626       (delete-process "gnus-x-face"))
13627     (let ((inhibit-point-motion-hooks t)
13628           (case-fold-search nil)
13629           from)
13630       (save-restriction
13631         (nnheader-narrow-to-headers)
13632         (setq from (mail-fetch-field "from"))
13633         (goto-char (point-min))
13634         (when (and gnus-article-x-face-command
13635                    (or force
13636                        ;; Check whether this face is censored.
13637                        (not gnus-article-x-face-too-ugly)
13638                        (and gnus-article-x-face-too-ugly from
13639                             (not (string-match gnus-article-x-face-too-ugly
13640                                                from))))
13641                    ;; Has to be present.
13642                    (re-search-forward "^X-Face: " nil t))
13643           ;; We now have the area of the buffer where the X-Face is stored.
13644           (let ((beg (point))
13645                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13646             ;; We display the face.
13647             (if (symbolp gnus-article-x-face-command)
13648                 ;; The command is a lisp function, so we call it.
13649                 (if (gnus-functionp gnus-article-x-face-command)
13650                     (funcall gnus-article-x-face-command beg end)
13651                   (error "%s is not a function" gnus-article-x-face-command))
13652               ;; The command is a string, so we interpret the command
13653               ;; as a, well, command, and fork it off.
13654               (let ((process-connection-type nil))
13655                 (process-kill-without-query
13656                  (start-process
13657                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13658                 (process-send-region "gnus-x-face" beg end)
13659                 (process-send-eof "gnus-x-face")))))))))
13660
13661 (defun gnus-headers-decode-quoted-printable ()
13662   "Hack to remove QP encoding from headers."
13663   (let ((case-fold-search t)
13664         (inhibit-point-motion-hooks t)
13665         string)
13666     (goto-char (point-min))
13667     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13668       (setq string (match-string 1))
13669       (narrow-to-region (match-beginning 0) (match-end 0))
13670       (delete-region (point-min) (point-max))
13671       (insert string)
13672       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13673       (subst-char-in-region (point-min) (point-max) ?_ ? )
13674       (widen)
13675       (goto-char (point-min)))))
13676
13677 (defun gnus-article-de-quoted-unreadable (&optional force)
13678   "Do a naive translation of a quoted-printable-encoded article.
13679 This is in no way, shape or form meant as a replacement for real MIME
13680 processing, but is simply a stop-gap measure until MIME support is
13681 written.
13682 If FORCE, decode the article whether it is marked as quoted-printable
13683 or not."
13684   (interactive (list 'force))
13685   (save-excursion
13686     (set-buffer gnus-article-buffer)
13687     (let ((case-fold-search t)
13688           (buffer-read-only nil)
13689           (type (gnus-fetch-field "content-transfer-encoding")))
13690       (when (or force
13691                 (and type (string-match "quoted-printable" type)))
13692         (gnus-headers-decode-quoted-printable)
13693         (goto-char (point-min))
13694         (search-forward "\n\n" nil 'move)
13695         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13696
13697 (defun gnus-mime-decode-quoted-printable (from to)
13698   "Decode Quoted-Printable in the region between FROM and TO."
13699   (goto-char from)
13700   (while (search-forward "=" to t)
13701     (cond ((eq (following-char) ?\n)
13702            (delete-char -1)
13703            (delete-char 1))
13704           ((looking-at "[0-9A-F][0-9A-F]")
13705            (delete-char -1)
13706            (insert (hexl-hex-string-to-integer
13707                     (buffer-substring (point) (+ 2 (point)))))
13708            (delete-char 2))
13709           ((looking-at "=")
13710            (delete-char 1))
13711           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13712
13713 (defun gnus-article-hide-pgp (&optional arg)
13714   "Toggle hiding of any PGP headers and signatures in the current article.
13715 If given a negative prefix, always show; if given a positive prefix,
13716 always hide."
13717   (interactive "P")
13718   (unless (gnus-article-check-hidden-text 'pgp arg)
13719     (save-excursion
13720       (set-buffer gnus-article-buffer)
13721       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13722             buffer-read-only beg end)
13723         (widen)
13724         (goto-char (point-min))
13725         ;; Hide the "header".
13726         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13727              (add-text-properties (match-beginning 0) (match-end 0) props))
13728         (setq beg (point))
13729         ;; Hide the actual signature.
13730         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13731              (setq end (match-beginning 0))
13732              (add-text-properties
13733               (match-beginning 0)
13734               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13735                   (match-end 0)
13736                 ;; Perhaps we shouldn't hide to the end of the buffer
13737                 ;; if there is no end to the signature?
13738                 (point-max))
13739               props))
13740         ;; Hide "- " PGP quotation markers.
13741         (when (and beg end)
13742           (narrow-to-region beg end)
13743           (goto-char (point-min))
13744           (while (re-search-forward "^- " nil t)
13745             (add-text-properties (match-beginning 0) (match-end 0) props))
13746           (widen))))))
13747
13748 (defun gnus-article-hide-signature (&optional arg)
13749   "Hide the signature in the current article.
13750 If given a negative prefix, always show; if given a positive prefix,
13751 always hide."
13752   (interactive "P")
13753   (unless (gnus-article-check-hidden-text 'signature arg)
13754     (save-excursion
13755       (set-buffer gnus-article-buffer)
13756       (save-restriction
13757         (let ((buffer-read-only nil))
13758           (when (gnus-narrow-to-signature)
13759             (add-text-properties
13760              (point-min) (point-max)
13761              (nconc (list 'gnus-type 'signature)
13762                     gnus-hidden-properties))))))))
13763
13764 (defun gnus-narrow-to-signature ()
13765   "Narrow to the signature."
13766   (widen)
13767   (goto-char (point-max))
13768   (when (re-search-backward gnus-signature-separator nil t)
13769     (forward-line 1)
13770     (when (or (null gnus-signature-limit)
13771               (and (numberp gnus-signature-limit)
13772                    (< (- (point-max) (point)) gnus-signature-limit))
13773               (and (gnus-functionp gnus-signature-limit)
13774                    (funcall gnus-signature-limit))
13775               (and (stringp gnus-signature-limit)
13776                    (not (re-search-forward gnus-signature-limit nil t))))
13777       (narrow-to-region (point) (point-max))
13778       t)))
13779
13780 (defun gnus-article-check-hidden-text (type arg)
13781   "Return nil if hiding is necessary."
13782   (save-excursion
13783     (set-buffer gnus-article-buffer)
13784     (let ((hide (gnus-article-hidden-text-p type)))
13785       (cond ((or (and (null arg) (eq hide 'hidden))
13786                  (and arg (< 0 (prefix-numeric-value arg))))
13787              (gnus-article-show-hidden-text type))
13788             ((eq hide 'shown)
13789              (gnus-article-show-hidden-text type t))
13790             (t nil)))))
13791
13792 (defun gnus-article-hidden-text-p (type)
13793   "Say whether the current buffer contains hidden text of type TYPE."
13794   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13795         prop)
13796     (when pos
13797       (if (get-text-property pos 'invisible)
13798           'hidden
13799         'shown))))
13800
13801 (defun gnus-article-hide (&optional arg force)
13802   "Hide all the gruft in the current article.
13803 This means that PGP stuff, signatures, cited text and (some)
13804 headers will be hidden.
13805 If given a prefix, show the hidden text instead."
13806   (interactive (list current-prefix-arg 'force))
13807   (gnus-article-hide-headers arg)
13808   (gnus-article-hide-pgp arg)
13809   (gnus-article-hide-citation-maybe arg force)
13810   (gnus-article-hide-signature arg))
13811
13812 (defun gnus-article-show-hidden-text (type &optional hide)
13813   "Show all hidden text of type TYPE.
13814 If HIDE, hide the text instead."
13815   (save-excursion
13816     (set-buffer gnus-article-buffer)
13817     (let ((buffer-read-only nil)
13818           (inhibit-point-motion-hooks t)
13819           (beg (point-min)))
13820       (while (gnus-goto-char (text-property-any
13821                               beg (point-max) 'gnus-type type))
13822         (setq beg (point))
13823         (forward-char)
13824         (if hide
13825             (add-text-properties beg (point) gnus-hidden-properties)
13826           (remove-text-properties beg (point) gnus-hidden-properties))
13827         (setq beg (point)))
13828       t)))
13829
13830 (defvar gnus-article-time-units
13831   `((year . ,(* 365.25 24 60 60))
13832     (week . ,(* 7 24 60 60))
13833     (day . ,(* 24 60 60))
13834     (hour . ,(* 60 60))
13835     (minute . 60)
13836     (second . 1))
13837   "Mapping from time units to seconds.")
13838
13839 (defun gnus-article-date-ut (&optional type highlight)
13840   "Convert DATE date to universal time in the current article.
13841 If TYPE is `local', convert to local time; if it is `lapsed', output
13842 how much time has lapsed since DATE."
13843   (interactive (list 'ut t))
13844   (let* ((header (or gnus-current-headers
13845                      (gnus-summary-article-header) ""))
13846          (date (and (vectorp header) (mail-header-date header)))
13847          (date-regexp "^Date: \\|^X-Sent: ")
13848          (now (current-time))
13849          (inhibit-point-motion-hooks t))
13850     (when (and date (not (string= date "")))
13851       (save-excursion
13852         (set-buffer gnus-article-buffer)
13853         (save-restriction
13854           (nnheader-narrow-to-headers)
13855           (let ((buffer-read-only nil))
13856             ;; Delete any old Date headers.
13857             (if (zerop (nnheader-remove-header date-regexp t))
13858                 (beginning-of-line)
13859               (goto-char (point-max)))
13860             (insert
13861              (cond
13862               ;; Convert to the local timezone.  We have to slap a
13863               ;; `condition-case' round the calls to the timezone
13864               ;; functions since they aren't particularly resistant to
13865               ;; buggy dates.
13866               ((eq type 'local)
13867                (concat "Date: " (condition-case ()
13868                                     (timezone-make-date-arpa-standard date)
13869                                   (error date))
13870                        "\n"))
13871               ;; Convert to Universal Time.
13872               ((eq type 'ut)
13873                (concat "Date: "
13874                        (condition-case ()
13875                            (timezone-make-date-arpa-standard date nil "UT")
13876                          (error date))
13877                        "\n"))
13878               ;; Get the original date from the article.
13879               ((eq type 'original)
13880                (concat "Date: " date "\n"))
13881               ;; Do an X-Sent lapsed format.
13882               ((eq type 'lapsed)
13883                ;; If the date is seriously mangled, the timezone
13884                ;; functions are liable to bug out, so we condition-case
13885                ;; the entire thing.
13886                (let* ((real-time
13887                        (condition-case ()
13888                            (gnus-time-minus
13889                             (gnus-encode-date
13890                              (timezone-make-date-arpa-standard
13891                               (current-time-string now)
13892                               (current-time-zone now) "UT"))
13893                             (gnus-encode-date
13894                              (timezone-make-date-arpa-standard
13895                               date nil "UT")))
13896                          (error '(0 0))))
13897                       (real-sec (+ (* (float (car real-time)) 65536)
13898                                    (cadr real-time)))
13899                       (sec (abs real-sec))
13900                       num prev)
13901                  (if (zerop sec)
13902                      "X-Sent: Now\n"
13903                    (concat
13904                     "X-Sent: "
13905                     ;; This is a bit convoluted, but basically we go
13906                     ;; through the time units for years, weeks, etc,
13907                     ;; and divide things to see whether that results
13908                     ;; in positive answers.
13909                     (mapconcat
13910                      (lambda (unit)
13911                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13912                            ;; The (remaining) seconds are too few to
13913                            ;; be divided into this time unit.
13914                            ""
13915                          ;; It's big enough, so we output it.
13916                          (setq sec (- sec (* num (cdr unit))))
13917                          (prog1
13918                              (concat (if prev ", " "") (int-to-string
13919                                                         (floor num))
13920                                      " " (symbol-name (car unit))
13921                                      (if (> num 1) "s" ""))
13922                            (setq prev t))))
13923                      gnus-article-time-units "")
13924                     ;; If dates are odd, then it might appear like the
13925                     ;; article was sent in the future.
13926                     (if (> real-sec 0)
13927                         " ago\n"
13928                       " in the future\n")))))
13929               (t
13930                (error "Unknown conversion type: %s" type)))))
13931           ;; Do highlighting.
13932           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13933             (gnus-article-highlight-headers)))))))
13934
13935 (defun gnus-article-date-local (&optional highlight)
13936   "Convert the current article date to the local timezone."
13937   (interactive (list t))
13938   (gnus-article-date-ut 'local highlight))
13939
13940 (defun gnus-article-date-original (&optional highlight)
13941   "Convert the current article date to what it was originally.
13942 This is only useful if you have used some other date conversion
13943 function and want to see what the date was before converting."
13944   (interactive (list t))
13945   (gnus-article-date-ut 'original highlight))
13946
13947 (defun gnus-article-date-lapsed (&optional highlight)
13948   "Convert the current article date to time lapsed since it was sent."
13949   (interactive (list t))
13950   (gnus-article-date-ut 'lapsed highlight))
13951
13952 (defun gnus-article-maybe-highlight ()
13953   "Do some article highlighting if `gnus-visual' is non-nil."
13954   (if (gnus-visual-p 'article-highlight 'highlight)
13955       (gnus-article-highlight-some)))
13956
13957 ;; Article savers.
13958
13959 (defun gnus-output-to-rmail (file-name)
13960   "Append the current article to an Rmail file named FILE-NAME."
13961   (require 'rmail)
13962   ;; Most of these codes are borrowed from rmailout.el.
13963   (setq file-name (expand-file-name file-name))
13964   (setq rmail-default-rmail-file file-name)
13965   (let ((artbuf (current-buffer))
13966         (tmpbuf (get-buffer-create " *Gnus-output*")))
13967     (save-excursion
13968       (or (get-file-buffer file-name)
13969           (file-exists-p file-name)
13970           (if (gnus-yes-or-no-p
13971                (concat "\"" file-name "\" does not exist, create it? "))
13972               (let ((file-buffer (create-file-buffer file-name)))
13973                 (save-excursion
13974                   (set-buffer file-buffer)
13975                   (rmail-insert-rmail-file-header)
13976                   (let ((require-final-newline nil))
13977                     (write-region (point-min) (point-max) file-name t 1)))
13978                 (kill-buffer file-buffer))
13979             (error "Output file does not exist")))
13980       (set-buffer tmpbuf)
13981       (buffer-disable-undo (current-buffer))
13982       (erase-buffer)
13983       (insert-buffer-substring artbuf)
13984       (gnus-convert-article-to-rmail)
13985       ;; Decide whether to append to a file or to an Emacs buffer.
13986       (let ((outbuf (get-file-buffer file-name)))
13987         (if (not outbuf)
13988             (append-to-file (point-min) (point-max) file-name)
13989           ;; File has been visited, in buffer OUTBUF.
13990           (set-buffer outbuf)
13991           (let ((buffer-read-only nil)
13992                 (msg (and (boundp 'rmail-current-message)
13993                           (symbol-value 'rmail-current-message))))
13994             ;; If MSG is non-nil, buffer is in RMAIL mode.
13995             (if msg
13996                 (progn (widen)
13997                        (narrow-to-region (point-max) (point-max))))
13998             (insert-buffer-substring tmpbuf)
13999             (if msg
14000                 (progn
14001                   (goto-char (point-min))
14002                   (widen)
14003                   (search-backward "\^_")
14004                   (narrow-to-region (point) (point-max))
14005                   (goto-char (1+ (point-min)))
14006                   (rmail-count-new-messages t)
14007                   (rmail-show-message msg)))))))
14008     (kill-buffer tmpbuf)))
14009
14010 (defun gnus-output-to-file (file-name)
14011   "Append the current article to a file named FILE-NAME."
14012   (setq file-name (expand-file-name file-name))
14013   (let ((artbuf (current-buffer))
14014         (tmpbuf (get-buffer-create " *Gnus-output*")))
14015     (save-excursion
14016       (set-buffer tmpbuf)
14017       (buffer-disable-undo (current-buffer))
14018       (erase-buffer)
14019       (insert-buffer-substring artbuf)
14020       ;; Append newline at end of the buffer as separator, and then
14021       ;; save it to file.
14022       (goto-char (point-max))
14023       (insert "\n")
14024       (append-to-file (point-min) (point-max) file-name))
14025     (kill-buffer tmpbuf)))
14026
14027 (defun gnus-convert-article-to-rmail ()
14028   "Convert article in current buffer to Rmail message format."
14029   (let ((buffer-read-only nil))
14030     ;; Convert article directly into Babyl format.
14031     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14032     (goto-char (point-min))
14033     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14034     (while (search-forward "\n\^_" nil t) ;single char
14035       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14036     (goto-char (point-max))
14037     (insert "\^_")))
14038
14039 (defun gnus-narrow-to-page (&optional arg)
14040   "Narrow the article buffer to a page.
14041 If given a numerical ARG, move forward ARG pages."
14042   (interactive "P")
14043   (setq arg (if arg (prefix-numeric-value arg) 0))
14044   (save-excursion
14045     (set-buffer gnus-article-buffer)
14046     (goto-char (point-min))
14047     (widen)
14048     (when (gnus-visual-p 'page-marker)
14049       (let ((buffer-read-only nil))
14050         (gnus-remove-text-with-property 'gnus-prev)
14051         (gnus-remove-text-with-property 'gnus-next)))
14052     (when
14053         (cond ((< arg 0)
14054                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14055               ((> arg 0)
14056                (re-search-forward page-delimiter nil 'move arg)))
14057       (goto-char (match-end 0)))
14058     (narrow-to-region
14059      (point)
14060      (if (re-search-forward page-delimiter nil 'move)
14061          (match-beginning 0)
14062        (point)))
14063     (when (and (gnus-visual-p 'page-marker)
14064                (not (= (point-min) 1)))
14065       (save-excursion
14066         (goto-char (point-min))
14067         (gnus-insert-prev-page-button)))
14068     (when (and (gnus-visual-p 'page-marker)
14069                (not (= (1- (point-max)) (buffer-size))))
14070       (save-excursion
14071         (goto-char (point-max))
14072         (gnus-insert-next-page-button)))))
14073
14074 ;; Article mode commands
14075
14076 (defun gnus-article-goto-next-page ()
14077   "Show the next page of the article."
14078   (interactive)
14079   (when (gnus-article-next-page)
14080     (gnus-article-read-summary-keys nil ?n)))
14081
14082 (defun gnus-article-goto-prev-page ()
14083   "Show the next page of the article."
14084   (interactive)
14085   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14086     (gnus-article-prev-page nil)))
14087
14088 (defun gnus-article-next-page (&optional lines)
14089   "Show the next page of the current article.
14090 If end of article, return non-nil.  Otherwise return nil.
14091 Argument LINES specifies lines to be scrolled up."
14092   (interactive "p")
14093   (move-to-window-line -1)
14094   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14095   (if (save-excursion
14096         (end-of-line)
14097         (and (pos-visible-in-window-p)  ;Not continuation line.
14098              (eobp)))
14099       ;; Nothing in this page.
14100       (if (or (not gnus-break-pages)
14101               (save-excursion
14102                 (save-restriction
14103                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14104           t                             ;Nothing more.
14105         (gnus-narrow-to-page 1)         ;Go to next page.
14106         nil)
14107     ;; More in this page.
14108     (condition-case ()
14109         (scroll-up lines)
14110       (end-of-buffer
14111        ;; Long lines may cause an end-of-buffer error.
14112        (goto-char (point-max))))
14113     nil))
14114
14115 (defun gnus-article-prev-page (&optional lines)
14116   "Show previous page of current article.
14117 Argument LINES specifies lines to be scrolled down."
14118   (interactive "p")
14119   (move-to-window-line 0)
14120   (if (and gnus-break-pages
14121            (bobp)
14122            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14123       (progn
14124         (gnus-narrow-to-page -1)        ;Go to previous page.
14125         (goto-char (point-max))
14126         (recenter -1))
14127     (condition-case ()
14128         (scroll-down lines)
14129       (error nil))))
14130
14131 (defun gnus-article-refer-article ()
14132   "Read article specified by message-id around point."
14133   (interactive)
14134   (let ((point (point)))
14135     (search-forward ">" nil t)          ;Move point to end of "<....>".
14136     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14137         (let ((message-id (match-string 1)))
14138           (goto-char point)
14139           (set-buffer gnus-summary-buffer)
14140           (gnus-summary-refer-article message-id))
14141       (goto-char (point))
14142       (error "No references around point"))))
14143
14144 (defun gnus-article-show-summary ()
14145   "Reconfigure windows to show summary buffer."
14146   (interactive)
14147   (gnus-configure-windows 'article)
14148   (gnus-summary-goto-subject gnus-current-article))
14149
14150 (defun gnus-article-describe-briefly ()
14151   "Describe article mode commands briefly."
14152   (interactive)
14153   (gnus-message 6
14154                 (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")))
14155
14156 (defun gnus-article-summary-command ()
14157   "Execute the last keystroke in the summary buffer."
14158   (interactive)
14159   (let ((obuf (current-buffer))
14160         (owin (current-window-configuration))
14161         func)
14162     (switch-to-buffer gnus-summary-buffer 'norecord)
14163     (setq func (lookup-key (current-local-map) (this-command-keys)))
14164     (call-interactively func)
14165     (set-buffer obuf)
14166     (set-window-configuration owin)
14167     (set-window-point (get-buffer-window (current-buffer)) (point))))
14168
14169 (defun gnus-article-summary-command-nosave ()
14170   "Execute the last keystroke in the summary buffer."
14171   (interactive)
14172   (let (func)
14173     (pop-to-buffer gnus-summary-buffer 'norecord)
14174     (setq func (lookup-key (current-local-map) (this-command-keys)))
14175     (call-interactively func)))
14176
14177 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14178   "Read a summary buffer key sequence and execute it from the article buffer."
14179   (interactive "P")
14180   (let ((nosaves
14181          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14182            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14183            "=" "^" "\M-^"))
14184         keys)
14185     (save-excursion
14186       (set-buffer gnus-summary-buffer)
14187       (push (or key last-command-event) unread-command-events)
14188       (setq keys (read-key-sequence nil)))
14189     (message "")
14190
14191     (if (member keys nosaves)
14192         (let (func)
14193           (pop-to-buffer gnus-summary-buffer 'norecord)
14194           (if (setq func (lookup-key (current-local-map) keys))
14195               (call-interactively func)
14196             (ding)))
14197       (let ((obuf (current-buffer))
14198             (owin (current-window-configuration))
14199             (opoint (point))
14200             func in-buffer)
14201         (if not-restore-window
14202             (pop-to-buffer gnus-summary-buffer 'norecord)
14203           (switch-to-buffer gnus-summary-buffer 'norecord))
14204         (setq in-buffer (current-buffer))
14205         (if (setq func (lookup-key (current-local-map) keys))
14206             (call-interactively func)
14207           (ding))
14208         (when (eq in-buffer (current-buffer))
14209           (set-buffer obuf)
14210           (unless not-restore-window
14211             (set-window-configuration owin))
14212           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14213
14214 \f
14215 ;;;
14216 ;;; Kill file handling.
14217 ;;;
14218
14219 ;;;###autoload
14220 (defalias 'gnus-batch-kill 'gnus-batch-score)
14221 ;;;###autoload
14222 (defun gnus-batch-score ()
14223   "Run batched scoring.
14224 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14225 Newsgroups is a list of strings in Bnews format.  If you want to score
14226 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14227 score the alt hierarchy, you'd say \"!alt.all\"."
14228   (interactive)
14229   (let* ((yes-and-no
14230           (gnus-newsrc-parse-options
14231            (apply (function concat)
14232                   (mapcar (lambda (g) (concat g " "))
14233                           command-line-args-left))))
14234          (gnus-expert-user t)
14235          (nnmail-spool-file nil)
14236          (gnus-use-dribble-file nil)
14237          (yes (car yes-and-no))
14238          (no (cdr yes-and-no))
14239          group newsrc entry
14240          ;; Disable verbose message.
14241          gnus-novice-user gnus-large-newsgroup)
14242     ;; Eat all arguments.
14243     (setq command-line-args-left nil)
14244     ;; Start Gnus.
14245     (gnus)
14246     ;; Apply kills to specified newsgroups in command line arguments.
14247     (setq newsrc (cdr gnus-newsrc-alist))
14248     (while newsrc
14249       (setq group (caar newsrc))
14250       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14251       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14252                (and (car entry)
14253                     (or (eq (car entry) t)
14254                         (not (zerop (car entry)))))
14255                (if yes (string-match yes group) t)
14256                (or (null no) (not (string-match no group))))
14257           (progn
14258             (gnus-summary-read-group group nil t nil t)
14259             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14260                  (gnus-summary-exit))))
14261       (setq newsrc (cdr newsrc)))
14262     ;; Exit Emacs.
14263     (switch-to-buffer gnus-group-buffer)
14264     (gnus-group-save-newsrc)))
14265
14266 (defun gnus-apply-kill-file ()
14267   "Apply a kill file to the current newsgroup.
14268 Returns the number of articles marked as read."
14269   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14270           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14271       (gnus-apply-kill-file-internal)
14272     0))
14273
14274 (defun gnus-kill-save-kill-buffer ()
14275   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14276     (when (get-file-buffer file)
14277       (save-excursion
14278         (set-buffer (get-file-buffer file))
14279         (and (buffer-modified-p) (save-buffer))
14280         (kill-buffer (current-buffer))))))
14281
14282 (defvar gnus-kill-file-name "KILL"
14283   "Suffix of the kill files.")
14284
14285 (defun gnus-newsgroup-kill-file (newsgroup)
14286   "Return the name of a kill file name for NEWSGROUP.
14287 If NEWSGROUP is nil, return the global kill file name instead."
14288   (cond 
14289    ;; The global KILL file is placed at top of the directory.
14290    ((or (null newsgroup)
14291         (string-equal newsgroup ""))
14292     (expand-file-name gnus-kill-file-name
14293                       (or gnus-kill-files-directory "~/News")))
14294    ;; Append ".KILL" to newsgroup name.
14295    ((gnus-use-long-file-name 'not-kill)
14296     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14297                               "." gnus-kill-file-name)
14298                       (or gnus-kill-files-directory "~/News")))
14299    ;; Place "KILL" under the hierarchical directory.
14300    (t
14301     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14302                               "/" gnus-kill-file-name)
14303                       (or gnus-kill-files-directory "~/News")))))
14304
14305 \f
14306 ;;;
14307 ;;; Dribble file
14308 ;;;
14309
14310 (defvar gnus-dribble-ignore nil)
14311 (defvar gnus-dribble-eval-file nil)
14312
14313 (defun gnus-dribble-file-name ()
14314   "Return the dribble file for the current .newsrc."
14315   (concat
14316    (if gnus-dribble-directory
14317        (concat (file-name-as-directory gnus-dribble-directory)
14318                (file-name-nondirectory gnus-current-startup-file))
14319      gnus-current-startup-file)
14320    "-dribble"))
14321
14322 (defun gnus-dribble-enter (string)
14323   "Enter STRING into the dribble buffer."
14324   (if (and (not gnus-dribble-ignore)
14325            gnus-dribble-buffer
14326            (buffer-name gnus-dribble-buffer))
14327       (let ((obuf (current-buffer)))
14328         (set-buffer gnus-dribble-buffer)
14329         (insert string "\n")
14330         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14331         (set-buffer obuf))))
14332
14333 (defun gnus-dribble-read-file ()
14334   "Read the dribble file from disk."
14335   (let ((dribble-file (gnus-dribble-file-name)))
14336     (save-excursion
14337       (set-buffer (setq gnus-dribble-buffer
14338                         (get-buffer-create
14339                          (file-name-nondirectory dribble-file))))
14340       (gnus-add-current-to-buffer-list)
14341       (erase-buffer)
14342       (setq buffer-file-name dribble-file)
14343       (auto-save-mode t)
14344       (buffer-disable-undo (current-buffer))
14345       (bury-buffer (current-buffer))
14346       (set-buffer-modified-p nil)
14347       (let ((auto (make-auto-save-file-name))
14348             (gnus-dribble-ignore t))
14349         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14350           ;; Load whichever file is newest -- the auto save file
14351           ;; or the "real" file.
14352           (if (file-newer-than-file-p auto dribble-file)
14353               (insert-file-contents auto)
14354             (insert-file-contents dribble-file))
14355           (unless (zerop (buffer-size))
14356             (set-buffer-modified-p t))
14357           ;; Set the file modes to reflect the .newsrc file modes.
14358           (save-buffer)
14359           (when (file-exists-p gnus-current-startup-file)
14360             (set-file-modes dribble-file
14361                             (file-modes gnus-current-startup-file)))
14362           ;; Possibly eval the file later.
14363           (when (gnus-y-or-n-p
14364                  "Auto-save file exists.  Do you want to read it? ")
14365             (setq gnus-dribble-eval-file t)))))))
14366
14367 (defun gnus-dribble-eval-file ()
14368   (if (not gnus-dribble-eval-file)
14369       ()
14370     (setq gnus-dribble-eval-file nil)
14371     (save-excursion
14372       (let ((gnus-dribble-ignore t))
14373         (set-buffer gnus-dribble-buffer)
14374         (eval-buffer (current-buffer))))))
14375
14376 (defun gnus-dribble-delete-file ()
14377   (if (file-exists-p (gnus-dribble-file-name))
14378       (delete-file (gnus-dribble-file-name)))
14379   (if gnus-dribble-buffer
14380       (save-excursion
14381         (set-buffer gnus-dribble-buffer)
14382         (let ((auto (make-auto-save-file-name)))
14383           (if (file-exists-p auto)
14384               (delete-file auto))
14385           (erase-buffer)
14386           (set-buffer-modified-p nil)))))
14387
14388 (defun gnus-dribble-save ()
14389   (if (and gnus-dribble-buffer
14390            (buffer-name gnus-dribble-buffer))
14391       (save-excursion
14392         (set-buffer gnus-dribble-buffer)
14393         (save-buffer))))
14394
14395 (defun gnus-dribble-clear ()
14396   (save-excursion
14397     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14398         (progn
14399           (set-buffer gnus-dribble-buffer)
14400           (erase-buffer)
14401           (set-buffer-modified-p nil)
14402           (setq buffer-saved-size (buffer-size))))))
14403
14404 \f
14405 ;;;
14406 ;;; Server Communication
14407 ;;;
14408
14409 (defun gnus-start-news-server (&optional confirm)
14410   "Open a method for getting news.
14411 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14412   (let (how)
14413     (if gnus-current-select-method
14414         ;; Stream is already opened.
14415         nil
14416       ;; Open NNTP server.
14417       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14418       (if confirm
14419           (progn
14420             ;; Read server name with completion.
14421             (setq gnus-nntp-server
14422                   (completing-read "NNTP server: "
14423                                    (mapcar (lambda (server) (list server))
14424                                            (cons (list gnus-nntp-server)
14425                                                  gnus-secondary-servers))
14426                                    nil nil gnus-nntp-server))))
14427
14428       (if (and gnus-nntp-server
14429                (stringp gnus-nntp-server)
14430                (not (string= gnus-nntp-server "")))
14431           (setq gnus-select-method
14432                 (cond ((or (string= gnus-nntp-server "")
14433                            (string= gnus-nntp-server "::"))
14434                        (list 'nnspool (system-name)))
14435                       ((string-match "^:" gnus-nntp-server)
14436                        (list 'nnmh gnus-nntp-server
14437                              (list 'nnmh-directory
14438                                    (file-name-as-directory
14439                                     (expand-file-name
14440                                      (concat "~/" (substring
14441                                                    gnus-nntp-server 1)))))
14442                              (list 'nnmh-get-new-mail nil)))
14443                       (t
14444                        (list 'nntp gnus-nntp-server)))))
14445
14446       (setq how (car gnus-select-method))
14447       (cond ((eq how 'nnspool)
14448              (require 'nnspool)
14449              (gnus-message 5 "Looking up local news spool..."))
14450             ((eq how 'nnmh)
14451              (require 'nnmh)
14452              (gnus-message 5 "Looking up mh spool..."))
14453             (t
14454              (require 'nntp)))
14455       (setq gnus-current-select-method gnus-select-method)
14456       (run-hooks 'gnus-open-server-hook)
14457       (or
14458        ;; gnus-open-server-hook might have opened it
14459        (gnus-server-opened gnus-select-method)
14460        (gnus-open-server gnus-select-method)
14461        (gnus-y-or-n-p
14462         (format
14463          "%s (%s) open error: '%s'.     Continue? "
14464          (car gnus-select-method) (cadr gnus-select-method)
14465          (gnus-status-message gnus-select-method)))
14466        (progn
14467          (gnus-message 1 "Couldn't open server on %s"
14468                        (nth 1 gnus-select-method))
14469          (ding)
14470          nil)))))
14471
14472 (defun gnus-check-group (group)
14473   "Try to make sure that the server where GROUP exists is alive."
14474   (let ((method (gnus-find-method-for-group group)))
14475     (or (gnus-server-opened method)
14476         (gnus-open-server method))))
14477
14478 (defun gnus-check-server (&optional method silent)
14479   "Check whether the connection to METHOD is down.
14480 If METHOD is nil, use `gnus-select-method'.
14481 If it is down, start it up (again)."
14482   (let ((method (or method gnus-select-method)))
14483     ;; Transform virtual server names into select methods.
14484     (when (stringp method)
14485       (setq method (gnus-server-to-method method)))
14486     (if (gnus-server-opened method)
14487         ;; The stream is already opened.
14488         t
14489       ;; Open the server.
14490       (unless silent
14491         (gnus-message 5 "Opening %s server%s..." (car method)
14492                       (if (equal (nth 1 method) "") ""
14493                         (format " on %s" (nth 1 method)))))
14494       (run-hooks 'gnus-open-server-hook)
14495       (prog1
14496           (gnus-open-server method)
14497         (unless silent
14498           (message ""))))))
14499
14500 (defun gnus-get-function (method function)
14501   "Return a function symbol based on METHOD and FUNCTION."
14502   ;; Translate server names into methods.
14503   (unless method
14504     (error "Attempted use of a nil select method"))
14505   (when (stringp method)
14506     (setq method (gnus-server-to-method method)))
14507   (let ((func (intern (format "%s-%s" (car method) function))))
14508     ;; If the functions isn't bound, we require the backend in
14509     ;; question.
14510     (unless (fboundp func)
14511       (require (car method))
14512       (unless (fboundp func)
14513         ;; This backend doesn't implement this function.
14514         (error "No such function: %s" func)))
14515     func))
14516
14517 \f
14518 ;;;
14519 ;;; Interface functions to the backends.
14520 ;;;
14521
14522 (defun gnus-open-server (method)
14523   "Open a connection to METHOD."
14524   (let ((elem (assoc method gnus-opened-servers)))
14525     ;; If this method was previously denied, we just return nil.
14526     (if (eq (nth 1 elem) 'denied)
14527         (progn
14528           (gnus-message 1 "Denied server")
14529           nil)
14530       ;; Open the server.
14531       (let ((result
14532              (funcall (gnus-get-function method 'open-server)
14533                       (nth 1 method) (nthcdr 2 method))))
14534         ;; If this hasn't been opened before, we add it to the list.
14535         (unless elem
14536           (setq elem (list method nil)
14537                 gnus-opened-servers (cons elem gnus-opened-servers)))
14538         ;; Set the status of this server.
14539         (setcar (cdr elem) (if result 'ok 'denied))
14540         ;; Return the result from the "open" call.
14541         result))))
14542
14543 (defun gnus-close-server (method)
14544   "Close the connection to METHOD."
14545   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14546
14547 (defun gnus-request-list (method)
14548   "Request the active file from METHOD."
14549   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14550
14551 (defun gnus-request-list-newsgroups (method)
14552   "Request the newsgroups file from METHOD."
14553   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14554
14555 (defun gnus-request-newgroups (date method)
14556   "Request all new groups since DATE from METHOD."
14557   (funcall (gnus-get-function method 'request-newgroups)
14558            date (nth 1 method)))
14559
14560 (defun gnus-server-opened (method)
14561   "Check whether a connection to METHOD has been opened."
14562   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14563
14564 (defun gnus-status-message (method)
14565   "Return the status message from METHOD.
14566 If METHOD is a string, it is interpreted as a group name.   The method
14567 this group uses will be queried."
14568   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14569                   method)))
14570     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14571
14572 (defun gnus-request-group (group &optional dont-check method)
14573   "Request GROUP.  If DONT-CHECK, no information is required."
14574   (let ((method (or method (gnus-find-method-for-group group))))
14575     (funcall (gnus-get-function method 'request-group)
14576              (gnus-group-real-name group) (nth 1 method) dont-check)))
14577
14578 (defun gnus-request-asynchronous (group &optional articles)
14579   "Request that GROUP behave asynchronously.
14580 ARTICLES is the `data' of the group."
14581   (let ((method (gnus-find-method-for-group group)))
14582     (funcall (gnus-get-function method 'request-asynchronous)
14583              (gnus-group-real-name group) (nth 1 method) articles)))
14584
14585 (defun gnus-list-active-group (group)
14586   "Request active information on GROUP."
14587   (let ((method (gnus-find-method-for-group group))
14588         (func 'list-active-group))
14589     (when (gnus-check-backend-function func group)
14590       (funcall (gnus-get-function method func)
14591                (gnus-group-real-name group) (nth 1 method)))))
14592
14593 (defun gnus-request-group-description (group)
14594   "Request a description of GROUP."
14595   (let ((method (gnus-find-method-for-group group))
14596         (func 'request-group-description))
14597     (when (gnus-check-backend-function func group)
14598       (funcall (gnus-get-function method func)
14599                (gnus-group-real-name group) (nth 1 method)))))
14600
14601 (defun gnus-close-group (group)
14602   "Request the GROUP be closed."
14603   (let ((method (gnus-find-method-for-group group)))
14604     (funcall (gnus-get-function method 'close-group)
14605              (gnus-group-real-name group) (nth 1 method))))
14606
14607 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14608   "Request headers for ARTICLES in GROUP.
14609 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14610   (let ((method (gnus-find-method-for-group group)))
14611     (if (and gnus-use-cache (numberp (car articles)))
14612         (gnus-cache-retrieve-headers articles group fetch-old)
14613       (funcall (gnus-get-function method 'retrieve-headers)
14614                articles (gnus-group-real-name group) (nth 1 method)
14615                fetch-old))))
14616
14617 (defun gnus-retrieve-groups (groups method)
14618   "Request active information on GROUPS from METHOD."
14619   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14620
14621 (defun gnus-request-type (group &optional article)
14622   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14623   (let ((method (gnus-find-method-for-group group)))
14624     (if (not (gnus-check-backend-function 'request-type (car method)))
14625         'unknown
14626       (funcall (gnus-get-function method 'request-type)
14627                (gnus-group-real-name group) article))))
14628
14629 (defun gnus-request-update-mark (group article mark)
14630   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14631   (let ((method (gnus-find-method-for-group group)))
14632     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14633         mark
14634       (funcall (gnus-get-function method 'request-update-mark)
14635                (gnus-group-real-name group) article mark))))
14636
14637 (defun gnus-request-article (article group &optional buffer)
14638   "Request the ARTICLE in GROUP.
14639 ARTICLE can either be an article number or an article Message-ID.
14640 If BUFFER, insert the article in that group."
14641   (let ((method (gnus-find-method-for-group group)))
14642     (funcall (gnus-get-function method 'request-article)
14643              article (gnus-group-real-name group) (nth 1 method) buffer)))
14644
14645 (defun gnus-request-head (article group)
14646   "Request the head of ARTICLE in GROUP."
14647   (let ((method (gnus-find-method-for-group group)))
14648     (funcall (gnus-get-function method 'request-head)
14649              article (gnus-group-real-name group) (nth 1 method))))
14650
14651 (defun gnus-request-body (article group)
14652   "Request the body of ARTICLE in GROUP."
14653   (let ((method (gnus-find-method-for-group group)))
14654     (funcall (gnus-get-function method 'request-body)
14655              article (gnus-group-real-name group) (nth 1 method))))
14656
14657 (defun gnus-request-post (method)
14658   "Post the current buffer using METHOD."
14659   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14660
14661 (defun gnus-request-scan (group method)
14662   "Request a SCAN being performed in GROUP from METHOD.
14663 If GROUP is nil, all groups on METHOD are scanned."
14664   (let ((method (if group (gnus-find-method-for-group group) method)))
14665     (funcall (gnus-get-function method 'request-scan)
14666              (and group (gnus-group-real-name group)) (nth 1 method))))
14667
14668 (defsubst gnus-request-update-info (info method)
14669   "Request that METHOD update INFO."
14670   (when (gnus-check-backend-function 'request-update-info (car method))
14671     (funcall (gnus-get-function method 'request-update-info)
14672              (gnus-group-real-name (gnus-info-group info))
14673              info (nth 1 method))))
14674
14675 (defun gnus-request-expire-articles (articles group &optional force)
14676   (let ((method (gnus-find-method-for-group group)))
14677     (funcall (gnus-get-function method 'request-expire-articles)
14678              articles (gnus-group-real-name group) (nth 1 method)
14679              force)))
14680
14681 (defun gnus-request-move-article
14682   (article group server accept-function &optional last)
14683   (let ((method (gnus-find-method-for-group group)))
14684     (funcall (gnus-get-function method 'request-move-article)
14685              article (gnus-group-real-name group)
14686              (nth 1 method) accept-function last)))
14687
14688 (defun gnus-request-accept-article (group &optional last method)
14689   ;; Make sure there's a newline at the end of the article.
14690   (goto-char (point-max))
14691   (unless (bolp)
14692     (insert "\n"))
14693   (let ((func (if (symbolp group) group
14694                 (car (or method (gnus-find-method-for-group group))))))
14695     (funcall (intern (format "%s-request-accept-article" func))
14696              (if (stringp group) (gnus-group-real-name group) group)
14697              last)))
14698
14699 (defun gnus-request-replace-article (article group buffer)
14700   (let ((func (car (gnus-find-method-for-group group))))
14701     (funcall (intern (format "%s-request-replace-article" func))
14702              article (gnus-group-real-name group) buffer)))
14703
14704 (defun gnus-request-associate-buffer (group)
14705   (let ((method (gnus-find-method-for-group group)))
14706     (funcall (gnus-get-function method 'request-associate-buffer)
14707              (gnus-group-real-name group))))
14708
14709 (defun gnus-request-restore-buffer (article group)
14710   "Request a new buffer restored to the state of ARTICLE."
14711   (let ((method (gnus-find-method-for-group group)))
14712     (funcall (gnus-get-function method 'request-restore-buffer)
14713              article (gnus-group-real-name group) (nth 1 method))))
14714
14715 (defun gnus-request-create-group (group &optional method)
14716   (let ((method (or method (gnus-find-method-for-group group))))
14717     (funcall (gnus-get-function method 'request-create-group)
14718              (gnus-group-real-name group) (nth 1 method))))
14719
14720 (defun gnus-request-delete-group (group &optional force)
14721   (let ((method (gnus-find-method-for-group group)))
14722     (funcall (gnus-get-function method 'request-delete-group)
14723              (gnus-group-real-name group) force (nth 1 method))))
14724
14725 (defun gnus-request-rename-group (group new-name)
14726   (let ((method (gnus-find-method-for-group group)))
14727     (funcall (gnus-get-function method 'request-rename-group)
14728              (gnus-group-real-name group)
14729              (gnus-group-real-name new-name) (nth 1 method))))
14730
14731 (defun gnus-member-of-valid (symbol group)
14732   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14733   (memq symbol (assoc
14734                 (symbol-name (car (gnus-find-method-for-group group)))
14735                 gnus-valid-select-methods)))
14736
14737 (defun gnus-method-option-p (method option)
14738   "Return non-nil if select METHOD has OPTION as a parameter."
14739   (memq option (assoc (format "%s" (car method))
14740                       gnus-valid-select-methods)))
14741
14742 (defun gnus-server-extend-method (group method)
14743   ;; This function "extends" a virtual server.  If the server is
14744   ;; "hello", and the select method is ("hello" (my-var "something"))
14745   ;; in the group "alt.alt", this will result in a new virtual server
14746   ;; called "hello+alt.alt".
14747   (let ((entry
14748          (gnus-copy-sequence
14749           (if (equal (car method) "native") gnus-select-method
14750             (cdr (assoc (car method) gnus-server-alist))))))
14751     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14752     (nconc entry (cdr method))))
14753
14754 (defun gnus-find-method-for-group (group &optional info)
14755   "Find the select method that GROUP uses."
14756   (or gnus-override-method
14757       (and (not group)
14758            gnus-select-method)
14759       (let ((info (or info (gnus-get-info group)))
14760             method)
14761         (if (or (not info)
14762                 (not (setq method (gnus-info-method info)))
14763                 (equal method "native"))
14764             gnus-select-method
14765           (setq method
14766                 (cond ((stringp method)
14767                        (gnus-server-to-method method))
14768                       ((stringp (car method))
14769                        (gnus-server-extend-method group method))
14770                       (t
14771                        method)))
14772           (if (equal (cadr method) "")
14773               method
14774             (gnus-server-add-address method))))))
14775
14776 (defun gnus-check-backend-function (func group)
14777   "Check whether GROUP supports function FUNC."
14778   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14779                   group)))
14780     (fboundp (intern (format "%s-%s" method func)))))
14781
14782 (defun gnus-methods-using (feature)
14783   "Find all methods that have FEATURE."
14784   (let ((valids gnus-valid-select-methods)
14785         outs)
14786     (while valids
14787       (if (memq feature (car valids))
14788           (setq outs (cons (car valids) outs)))
14789       (setq valids (cdr valids)))
14790     outs))
14791
14792 \f
14793 ;;;
14794 ;;; Active & Newsrc File Handling
14795 ;;;
14796
14797 (defun gnus-setup-news (&optional rawfile level)
14798   "Setup news information.
14799 If RAWFILE is non-nil, the .newsrc file will also be read.
14800 If LEVEL is non-nil, the news will be set up at level LEVEL."
14801   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14802     ;; Clear some variables to re-initialize news information.
14803     (if init (setq gnus-newsrc-alist nil
14804                    gnus-active-hashtb nil))
14805
14806     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14807     (if init (gnus-read-newsrc-file rawfile))
14808
14809     (when (and (not (assoc "archive" gnus-server-alist))
14810                gnus-message-archive-method)
14811       (push (cons "archive" gnus-message-archive-method)
14812             gnus-server-alist))
14813
14814     ;; If we don't read the complete active file, we fill in the
14815     ;; hashtb here.
14816     (if (or (null gnus-read-active-file)
14817             (eq gnus-read-active-file 'some))
14818         (gnus-update-active-hashtb-from-killed))
14819
14820     ;; Read the active file and create `gnus-active-hashtb'.
14821     ;; If `gnus-read-active-file' is nil, then we just create an empty
14822     ;; hash table.  The partial filling out of the hash table will be
14823     ;; done in `gnus-get-unread-articles'.
14824     (and gnus-read-active-file
14825          (not level)
14826          (gnus-read-active-file))
14827
14828     (or gnus-active-hashtb
14829         (setq gnus-active-hashtb (make-vector 4095 0)))
14830
14831     ;; Initialize the cache.
14832     (when gnus-use-cache
14833       (gnus-cache-open))
14834
14835     ;; Possibly eval the dribble file.
14836     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14837
14838     (gnus-update-format-specifications)
14839
14840     ;; Find new newsgroups and treat them.
14841     (if (and init gnus-check-new-newsgroups (not level)
14842              (gnus-check-server gnus-select-method))
14843         (gnus-find-new-newsgroups))
14844
14845     ;; Find the number of unread articles in each non-dead group.
14846     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14847       (gnus-get-unread-articles level))
14848
14849     (if (and init gnus-check-bogus-newsgroups
14850              gnus-read-active-file (not level)
14851              (gnus-server-opened gnus-select-method))
14852         (gnus-check-bogus-newsgroups))))
14853
14854 (defun gnus-find-new-newsgroups (&optional arg)
14855   "Search for new newsgroups and add them.
14856 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14857 The `-n' option line from .newsrc is respected.
14858 If ARG (the prefix), use the `ask-server' method to query
14859 the server for new groups."
14860   (interactive "P")
14861   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14862                        (null gnus-read-active-file)
14863                        (eq gnus-read-active-file 'some))
14864                    'ask-server gnus-check-new-newsgroups)))
14865     (unless (gnus-check-first-time-used)
14866       (if (or (consp check)
14867               (eq check 'ask-server))
14868           ;; Ask the server for new groups.
14869           (gnus-ask-server-for-new-groups)
14870         ;; Go through the active hashtb and look for new groups.
14871         (let ((groups 0)
14872               group new-newsgroups)
14873           (gnus-message 5 "Looking for new newsgroups...")
14874           (unless gnus-have-read-active-file
14875             (gnus-read-active-file))
14876           (setq gnus-newsrc-last-checked-date (current-time-string))
14877           (unless gnus-killed-hashtb
14878             (gnus-make-hashtable-from-killed))
14879           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14880           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14881           (mapatoms
14882            (lambda (sym)
14883              (if (or (null (setq group (symbol-name sym)))
14884                      (not (boundp sym))
14885                      (null (symbol-value sym))
14886                      (gnus-gethash group gnus-killed-hashtb)
14887                      (gnus-gethash group gnus-newsrc-hashtb))
14888                  ()
14889                (let ((do-sub (gnus-matches-options-n group)))
14890                  (cond
14891                   ((eq do-sub 'subscribe)
14892                    (setq groups (1+ groups))
14893                    (gnus-sethash group group gnus-killed-hashtb)
14894                    (funcall gnus-subscribe-options-newsgroup-method group))
14895                   ((eq do-sub 'ignore)
14896                    nil)
14897                   (t
14898                    (setq groups (1+ groups))
14899                    (gnus-sethash group group gnus-killed-hashtb)
14900                    (if gnus-subscribe-hierarchical-interactive
14901                        (setq new-newsgroups (cons group new-newsgroups))
14902                      (funcall gnus-subscribe-newsgroup-method group)))))))
14903            gnus-active-hashtb)
14904           (when new-newsgroups
14905             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14906           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14907           (if (> groups 0)
14908               (gnus-message 6 "%d new newsgroup%s arrived."
14909                             groups (if (> groups 1) "s have" " has"))
14910             (gnus-message 6 "No new newsgroups.")))))))
14911
14912 (defun gnus-matches-options-n (group)
14913   ;; Returns `subscribe' if the group is to be unconditionally
14914   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14915   ;; no match for the group.
14916
14917   ;; First we check the two user variables.
14918   (cond
14919    ((and gnus-options-subscribe
14920          (string-match gnus-options-subscribe group))
14921     'subscribe)
14922    ((and gnus-auto-subscribed-groups
14923          (string-match gnus-auto-subscribed-groups group))
14924     'subscribe)
14925    ((and gnus-options-not-subscribe
14926          (string-match gnus-options-not-subscribe group))
14927     'ignore)
14928    ;; Then we go through the list that was retrieved from the .newsrc
14929    ;; file.  This list has elements on the form
14930    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14931    ;; is in the reverse order of the options line) is returned.
14932    (t
14933     (let ((regs gnus-newsrc-options-n))
14934       (while (and regs
14935                   (not (string-match (caar regs) group)))
14936         (setq regs (cdr regs)))
14937       (and regs (cdar regs))))))
14938
14939 (defun gnus-ask-server-for-new-groups ()
14940   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14941          (methods (cons gnus-select-method
14942                         (nconc
14943                          (when gnus-message-archive-method
14944                            (list "archive"))
14945                          (append
14946                           (and (consp gnus-check-new-newsgroups)
14947                                gnus-check-new-newsgroups)
14948                           gnus-secondary-select-methods))))
14949          (groups 0)
14950          (new-date (current-time-string))
14951          group new-newsgroups got-new method hashtb
14952          gnus-override-subscribe-method)
14953     ;; Go through both primary and secondary select methods and
14954     ;; request new newsgroups.
14955     (while (setq method (gnus-server-get-method nil (pop methods)))
14956       (setq new-newsgroups nil)
14957       (setq gnus-override-subscribe-method method)
14958       (when (and (gnus-check-server method)
14959                  (gnus-request-newgroups date method))
14960         (save-excursion
14961           (setq got-new t)
14962           (setq hashtb (gnus-make-hashtable 100))
14963           (set-buffer nntp-server-buffer)
14964           ;; Enter all the new groups into a hashtable.
14965           (gnus-active-to-gnus-format method hashtb 'ignore)))
14966       ;; Now all new groups from `method' are in `hashtb'.
14967       (mapatoms
14968        (lambda (group-sym)
14969          (if (or (null (setq group (symbol-name group-sym)))
14970                  (null (symbol-value group-sym))
14971                  (gnus-gethash group gnus-newsrc-hashtb)
14972                  (member group gnus-zombie-list)
14973                  (member group gnus-killed-list))
14974              ;; The group is already known.
14975              ()
14976            ;; Make this group active.
14977            (when (symbol-value group-sym)
14978              (gnus-set-active group (symbol-value group-sym)))
14979            ;; Check whether we want it or not.
14980            (let ((do-sub (gnus-matches-options-n group)))
14981              (cond
14982               ((eq do-sub 'subscribe)
14983                (incf groups)
14984                (gnus-sethash group group gnus-killed-hashtb)
14985                (funcall gnus-subscribe-options-newsgroup-method group))
14986               ((eq do-sub 'ignore)
14987                nil)
14988               (t
14989                (incf groups)
14990                (gnus-sethash group group gnus-killed-hashtb)
14991                (if gnus-subscribe-hierarchical-interactive
14992                    (push group new-newsgroups)
14993                  (funcall gnus-subscribe-newsgroup-method group)))))))
14994        hashtb)
14995       (when new-newsgroups
14996         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
14997     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14998     (when (> groups 0)
14999       (gnus-message 6 "%d new newsgroup%s arrived."
15000                     groups (if (> groups 1) "s have" " has")))
15001     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15002     got-new))
15003
15004 (defun gnus-check-first-time-used ()
15005   (if (or (> (length gnus-newsrc-alist) 1)
15006           (file-exists-p gnus-startup-file)
15007           (file-exists-p (concat gnus-startup-file ".el"))
15008           (file-exists-p (concat gnus-startup-file ".eld")))
15009       nil
15010     (gnus-message 6 "First time user; subscribing you to default groups")
15011     (unless gnus-have-read-active-file
15012       (gnus-read-active-file))
15013     (setq gnus-newsrc-last-checked-date (current-time-string))
15014     (let ((groups gnus-default-subscribed-newsgroups)
15015           group)
15016       (if (eq groups t)
15017           nil
15018         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15019         (mapatoms
15020          (lambda (sym)
15021            (if (null (setq group (symbol-name sym)))
15022                ()
15023              (let ((do-sub (gnus-matches-options-n group)))
15024                (cond
15025                 ((eq do-sub 'subscribe)
15026                  (gnus-sethash group group gnus-killed-hashtb)
15027                  (funcall gnus-subscribe-options-newsgroup-method group))
15028                 ((eq do-sub 'ignore)
15029                  nil)
15030                 (t
15031                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15032          gnus-active-hashtb)
15033         (while groups
15034           (if (gnus-active (car groups))
15035               (gnus-group-change-level
15036                (car groups) gnus-level-default-subscribed gnus-level-killed))
15037           (setq groups (cdr groups)))
15038         (gnus-group-make-help-group)
15039         (and gnus-novice-user
15040              (gnus-message 7 "`A k' to list killed groups"))))))
15041
15042 (defun gnus-subscribe-group (group previous &optional method)
15043   (gnus-group-change-level
15044    (if method
15045        (list t group gnus-level-default-subscribed nil nil method)
15046      group)
15047    gnus-level-default-subscribed gnus-level-killed previous t))
15048
15049 ;; `gnus-group-change-level' is the fundamental function for changing
15050 ;; subscription levels of newsgroups.  This might mean just changing
15051 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15052 ;; again, which subscribes/unsubscribes a group, which is equally
15053 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15054 ;; from 8-9 to 1-7 means that you remove the group from the list of
15055 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15056 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15057 ;; which is trivial.
15058 ;; ENTRY can either be a string (newsgroup name) or a list (if
15059 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15060 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15061 ;; entries.
15062 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15063 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15064 ;; after.
15065 (defun gnus-group-change-level (entry level &optional oldlevel
15066                                       previous fromkilled)
15067   (let (group info active num)
15068     ;; Glean what info we can from the arguments
15069     (if (consp entry)
15070         (if fromkilled (setq group (nth 1 entry))
15071           (setq group (car (nth 2 entry))))
15072       (setq group entry))
15073     (if (and (stringp entry)
15074              oldlevel
15075              (< oldlevel gnus-level-zombie))
15076         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15077     (if (and (not oldlevel)
15078              (consp entry))
15079         (setq oldlevel (cadr (nth 2 entry))))
15080     (if (stringp previous)
15081         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15082
15083     (if (and (>= oldlevel gnus-level-zombie)
15084              (gnus-gethash group gnus-newsrc-hashtb))
15085         ;; We are trying to subscribe a group that is already
15086         ;; subscribed.
15087         ()                              ; Do nothing.
15088
15089       (or (gnus-ephemeral-group-p group)
15090           (gnus-dribble-enter
15091            (format "(gnus-group-change-level %S %S %S %S %S)"
15092                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15093
15094       ;; Then we remove the newgroup from any old structures, if needed.
15095       ;; If the group was killed, we remove it from the killed or zombie
15096       ;; list.  If not, and it is in fact going to be killed, we remove
15097       ;; it from the newsrc hash table and assoc.
15098       (cond
15099        ((>= oldlevel gnus-level-zombie)
15100         (if (= oldlevel gnus-level-zombie)
15101             (setq gnus-zombie-list (delete group gnus-zombie-list))
15102           (setq gnus-killed-list (delete group gnus-killed-list))))
15103        (t
15104         (if (and (>= level gnus-level-zombie)
15105                  entry)
15106             (progn
15107               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15108               (if (nth 3 entry)
15109                   (setcdr (gnus-gethash (car (nth 3 entry))
15110                                         gnus-newsrc-hashtb)
15111                           (cdr entry)))
15112               (setcdr (cdr entry) (cdddr entry))))))
15113
15114       ;; Finally we enter (if needed) the list where it is supposed to
15115       ;; go, and change the subscription level.  If it is to be killed,
15116       ;; we enter it into the killed or zombie list.
15117       (cond 
15118        ((>= level gnus-level-zombie)
15119         ;; Remove from the hash table.
15120         (gnus-sethash group nil gnus-newsrc-hashtb)
15121         ;; We do not enter foreign groups into the list of dead
15122         ;; groups.
15123         (unless (gnus-group-foreign-p group)
15124           (if (= level gnus-level-zombie)
15125               (setq gnus-zombie-list (cons group gnus-zombie-list))
15126             (setq gnus-killed-list (cons group gnus-killed-list)))))
15127        (t
15128         ;; If the list is to be entered into the newsrc assoc, and
15129         ;; it was killed, we have to create an entry in the newsrc
15130         ;; hashtb format and fix the pointers in the newsrc assoc.
15131         (if (< oldlevel gnus-level-zombie)
15132             ;; It was alive, and it is going to stay alive, so we
15133             ;; just change the level and don't change any pointers or
15134             ;; hash table entries.
15135             (setcar (cdaddr entry) level)
15136           (if (listp entry)
15137               (setq info (cdr entry)
15138                     num (car entry))
15139             (setq active (gnus-active group))
15140             (setq num
15141                   (if active (- (1+ (cdr active)) (car active)) t))
15142             ;; Check whether the group is foreign.  If so, the
15143             ;; foreign select method has to be entered into the
15144             ;; info.
15145             (let ((method (or gnus-override-subscribe-method
15146                               (gnus-group-method group))))
15147               (if (eq method gnus-select-method)
15148                   (setq info (list group level nil))
15149                 (setq info (list group level nil nil method)))))
15150           (unless previous
15151             (setq previous
15152                   (let ((p gnus-newsrc-alist))
15153                     (while (cddr p)
15154                       (setq p (cdr p)))
15155                     p)))
15156           (setq entry (cons info (cddr previous)))
15157           (if (cdr previous)
15158               (progn
15159                 (setcdr (cdr previous) entry)
15160                 (gnus-sethash group (cons num (cdr previous))
15161                               gnus-newsrc-hashtb))
15162             (setcdr previous entry)
15163             (gnus-sethash group (cons num previous)
15164                           gnus-newsrc-hashtb))
15165           (when (cdr entry)
15166             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15167       (when gnus-group-change-level-function
15168         (funcall gnus-group-change-level-function group level oldlevel)))))
15169
15170 (defun gnus-kill-newsgroup (newsgroup)
15171   "Obsolete function.  Kills a newsgroup."
15172   (gnus-group-change-level
15173    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15174
15175 (defun gnus-check-bogus-newsgroups (&optional confirm)
15176   "Remove bogus newsgroups.
15177 If CONFIRM is non-nil, the user has to confirm the deletion of every
15178 newsgroup."
15179   (let ((newsrc (cdr gnus-newsrc-alist))
15180         bogus group entry info)
15181     (gnus-message 5 "Checking bogus newsgroups...")
15182     (unless gnus-have-read-active-file
15183       (gnus-read-active-file))
15184     (when (member gnus-select-method gnus-have-read-active-file)
15185       ;; Find all bogus newsgroup that are subscribed.
15186       (while newsrc
15187         (setq info (pop newsrc)
15188               group (gnus-info-group info))
15189         (unless (or (gnus-active group) ; Active
15190                     (gnus-info-method info) ; Foreign
15191                     (and confirm
15192                          (not (gnus-y-or-n-p
15193                                (format "Remove bogus newsgroup: %s " group)))))
15194           ;; Found a bogus newsgroup.
15195           (push group bogus)))
15196       ;; Remove all bogus subscribed groups by first killing them, and
15197       ;; then removing them from the list of killed groups.
15198       (while bogus
15199         (when (setq entry (gnus-gethash (setq group (pop bogus))
15200                                         gnus-newsrc-hashtb))
15201           (gnus-group-change-level entry gnus-level-killed)
15202           (setq gnus-killed-list (delete group gnus-killed-list))))
15203       ;; Then we remove all bogus groups from the list of killed and
15204       ;; zombie groups.  They are are removed without confirmation.
15205       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15206             killed)
15207         (while dead-lists
15208           (setq killed (symbol-value (car dead-lists)))
15209           (while killed
15210             (unless (gnus-active (setq group (pop killed)))
15211               ;; The group is bogus.
15212               ;; !!!Slow as hell.
15213               (set (car dead-lists)
15214                    (delete group (symbol-value (car dead-lists))))))
15215           (setq dead-lists (cdr dead-lists))))
15216       (gnus-message 5 "Checking bogus newsgroups...done"))))
15217
15218 (defun gnus-check-duplicate-killed-groups ()
15219   "Remove duplicates from the list of killed groups."
15220   (interactive)
15221   (let ((killed gnus-killed-list))
15222     (while killed
15223       (gnus-message 9 "%d" (length killed))
15224       (setcdr killed (delete (car killed) (cdr killed)))
15225       (setq killed (cdr killed)))))
15226
15227 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15228 ;; and compute how many unread articles there are in each group.
15229 (defun gnus-get-unread-articles (&optional level)
15230   (let* ((newsrc (cdr gnus-newsrc-alist))
15231          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15232          (foreign-level
15233           (min
15234            (cond ((and gnus-activate-foreign-newsgroups
15235                        (not (numberp gnus-activate-foreign-newsgroups)))
15236                   (1+ gnus-level-subscribed))
15237                  ((numberp gnus-activate-foreign-newsgroups)
15238                   gnus-activate-foreign-newsgroups)
15239                  (t 0))
15240            level))
15241          info group active method)
15242     (gnus-message 5 "Checking new news...")
15243
15244     (while newsrc
15245       (setq active (gnus-active (setq group (gnus-info-group
15246                                              (setq info (pop newsrc))))))
15247
15248       ;; Check newsgroups.  If the user doesn't want to check them, or
15249       ;; they can't be checked (for instance, if the news server can't
15250       ;; be reached) we just set the number of unread articles in this
15251       ;; newsgroup to t.  This means that Gnus thinks that there are
15252       ;; unread articles, but it has no idea how many.
15253       (if (and (setq method (gnus-info-method info))
15254                (not (gnus-server-equal
15255                      gnus-select-method
15256                      (gnus-server-get-method nil method)))
15257                (not (gnus-secondary-method-p method)))
15258           ;; These groups are foreign.  Check the level.
15259           (when (<= (gnus-info-level info) foreign-level)
15260             (setq active (gnus-activate-group group 'scan))
15261             (gnus-close-group group))
15262
15263         ;; These groups are native or secondary.
15264         (when (and (<= (gnus-info-level info) level)
15265                    (not gnus-read-active-file))
15266           (setq active (gnus-activate-group group 'scan))
15267           (gnus-close-group group)))
15268
15269       (if active
15270           (gnus-get-unread-articles-in-group info active t)
15271         ;; The group couldn't be reached, so we nix out the number of
15272         ;; unread articles and stuff.
15273         (gnus-set-active group nil)
15274         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15275
15276     (gnus-message 5 "Checking new news...done")))
15277
15278 ;; Create a hash table out of the newsrc alist.  The `car's of the
15279 ;; alist elements are used as keys.
15280 (defun gnus-make-hashtable-from-newsrc-alist ()
15281   (let ((alist gnus-newsrc-alist)
15282         (ohashtb gnus-newsrc-hashtb)
15283         prev)
15284     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15285     (setq alist
15286           (setq prev (setq gnus-newsrc-alist
15287                            (if (equal (caar gnus-newsrc-alist)
15288                                       "dummy.group")
15289                                gnus-newsrc-alist
15290                              (cons (list "dummy.group" 0 nil) alist)))))
15291     (while alist
15292       (gnus-sethash
15293        (caar alist)
15294        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15295              prev)
15296        gnus-newsrc-hashtb)
15297       (setq prev alist
15298             alist (cdr alist)))))
15299
15300 (defun gnus-make-hashtable-from-killed ()
15301   "Create a hash table from the killed and zombie lists."
15302   (let ((lists '(gnus-killed-list gnus-zombie-list))
15303         list)
15304     (setq gnus-killed-hashtb
15305           (gnus-make-hashtable
15306            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15307     (while lists
15308       (setq list (symbol-value (car lists)))
15309       (setq lists (cdr lists))
15310       (while list
15311         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15312         (setq list (cdr list))))))
15313
15314 (defun gnus-get-unread-articles-in-group (info active &optional update)
15315   (when active
15316     ;; Allow the backend to update the info in the group.
15317     (when update 
15318       (gnus-request-update-info
15319        info (gnus-find-method-for-group (gnus-info-group info))))
15320     (let* ((range (gnus-info-read info))
15321            (num 0)
15322            (marked (gnus-info-marks info)))
15323       ;; If a cache is present, we may have to alter the active info.
15324       (and gnus-use-cache
15325            (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15326       ;; Modify the list of read articles according to what articles
15327       ;; are available; then tally the unread articles and add the
15328       ;; number to the group hash table entry.
15329       (cond
15330        ((zerop (cdr active))
15331         (setq num 0))
15332        ((not range)
15333         (setq num (- (1+ (cdr active)) (car active))))
15334        ((not (listp (cdr range)))
15335         ;; Fix a single (num . num) range according to the
15336         ;; active hash table.
15337         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15338         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15339         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15340         ;; Compute number of unread articles.
15341         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15342        (t
15343         ;; The read list is a list of ranges.  Fix them according to
15344         ;; the active hash table.
15345         ;; First peel off any elements that are below the lower
15346         ;; active limit.
15347         (while (and (cdr range)
15348                     (>= (car active)
15349                         (or (and (atom (cadr range)) (cadr range))
15350                             (caadr range))))
15351           (if (numberp (car range))
15352               (setcar range
15353                       (cons (car range)
15354                             (or (and (numberp (cadr range))
15355                                      (cadr range))
15356                                 (cdadr range))))
15357             (setcdr (car range)
15358                     (or (and (numberp (nth 1 range)) (nth 1 range))
15359                         (cdadr range))))
15360           (setcdr range (cddr range)))
15361         ;; Adjust the first element to be the same as the lower limit.
15362         (if (and (not (atom (car range)))
15363                  (< (cdar range) (car active)))
15364             (setcdr (car range) (1- (car active))))
15365         ;; Then we want to peel off any elements that are higher
15366         ;; than the upper active limit.
15367         (let ((srange range))
15368           ;; Go past all legal elements.
15369           (while (and (cdr srange)
15370                       (<= (or (and (atom (cadr srange))
15371                                    (cadr srange))
15372                               (caadr srange)) (cdr active)))
15373             (setq srange (cdr srange)))
15374           (if (cdr srange)
15375               ;; Nuke all remaining illegal elements.
15376               (setcdr srange nil))
15377
15378           ;; Adjust the final element.
15379           (if (and (not (atom (car srange)))
15380                    (> (cdar srange) (cdr active)))
15381               (setcdr (car srange) (cdr active))))
15382         ;; Compute the number of unread articles.
15383         (while range
15384           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15385                                       (cdar range)))
15386                               (or (and (atom (car range)) (car range))
15387                                   (caar range)))))
15388           (setq range (cdr range)))
15389         (setq num (max 0 (- (cdr active) num)))))
15390       ;; Set the number of unread articles.
15391       (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num)
15392       num)))
15393
15394 (defun gnus-activate-group (group &optional scan)
15395   ;; Check whether a group has been activated or not.
15396   ;; If SCAN, request a scan of that group as well.
15397   (let ((method (gnus-find-method-for-group group))
15398         active)
15399     (and (gnus-check-server method)
15400          ;; We escape all bugs and quit here to make it possible to
15401          ;; continue if a group is so out-there that it reports bugs
15402          ;; and stuff.
15403          (progn
15404            (and scan
15405                 (gnus-check-backend-function 'request-scan (car method))
15406                 (gnus-request-scan group method))
15407            t)
15408          (condition-case ()
15409              (gnus-request-group group)
15410         ;   (error nil)
15411            (quit nil))
15412          (save-excursion
15413            (set-buffer nntp-server-buffer)
15414            (goto-char (point-min))
15415            ;; Parse the result we got from `gnus-request-group'.
15416            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15417                 (progn
15418                   (goto-char (match-beginning 1))
15419                   (gnus-set-active
15420                    group (setq active (cons (read (current-buffer))
15421                                             (read (current-buffer)))))
15422                   ;; Return the new active info.
15423                   active))))))
15424
15425 (defun gnus-update-read-articles (group unread)
15426   "Update the list of read and ticked articles in GROUP using the
15427 UNREAD and TICKED lists.
15428 Note: UNSELECTED has to be sorted over `<'.
15429 Returns whether the updating was successful."
15430   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15431          (entry (gnus-gethash group gnus-newsrc-hashtb))
15432          (info (nth 2 entry))
15433          (marked (gnus-info-marks info))
15434          (prev 1)
15435          (unread (sort (copy-sequence unread) '<))
15436          read)
15437     (if (or (not info) (not active))
15438         ;; There is no info on this group if it was, in fact,
15439         ;; killed.  Gnus stores no information on killed groups, so
15440         ;; there's nothing to be done.
15441         ;; One could store the information somewhere temporarily,
15442         ;; perhaps...  Hmmm...
15443         ()
15444       ;; Remove any negative articles numbers.
15445       (while (and unread (< (car unread) 0))
15446         (setq unread (cdr unread)))
15447       ;; Remove any expired article numbers
15448       (while (and unread (< (car unread) (car active)))
15449         (setq unread (cdr unread)))
15450       ;; Compute the ranges of read articles by looking at the list of
15451       ;; unread articles.
15452       (while unread
15453         (if (/= (car unread) prev)
15454             (setq read (cons (if (= prev (1- (car unread))) prev
15455                                (cons prev (1- (car unread)))) read)))
15456         (setq prev (1+ (car unread)))
15457         (setq unread (cdr unread)))
15458       (when (<= prev (cdr active))
15459         (setq read (cons (cons prev (cdr active)) read)))
15460       ;; Enter this list into the group info.
15461       (gnus-info-set-read
15462        info (if (> (length read) 1) (nreverse read) read))
15463       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15464       (gnus-get-unread-articles-in-group info (gnus-active group))
15465       t)))
15466
15467 (defun gnus-make-articles-unread (group articles)
15468   "Mark ARTICLES in GROUP as unread."
15469   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15470                           (gnus-gethash (gnus-group-real-name group)
15471                                         gnus-newsrc-hashtb))))
15472          (ranges (gnus-info-read info))
15473          news article)
15474     (while articles
15475       (when (gnus-member-of-range
15476              (setq article (pop articles)) ranges)
15477         (setq news (cons article news))))
15478     (when news
15479       (gnus-info-set-read
15480        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15481       (gnus-group-update-group group t))))
15482
15483 ;; Enter all dead groups into the hashtb.
15484 (defun gnus-update-active-hashtb-from-killed ()
15485   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15486         (lists (list gnus-killed-list gnus-zombie-list))
15487         killed)
15488     (while lists
15489       (setq killed (car lists))
15490       (while killed
15491         (gnus-sethash (car killed) nil hashtb)
15492         (setq killed (cdr killed)))
15493       (setq lists (cdr lists)))))
15494
15495 ;; Get the active file(s) from the backend(s).
15496 (defun gnus-read-active-file ()
15497   (gnus-group-set-mode-line)
15498   (let ((methods 
15499          (append
15500           (if (gnus-check-server gnus-select-method)
15501               ;; The native server is available.
15502               (cons gnus-select-method gnus-secondary-select-methods)
15503             ;; The native server is down, so we just do the
15504             ;; secondary ones.
15505             gnus-secondary-select-methods)
15506           ;; Also read from the archive server.
15507           (when gnus-message-archive-method
15508             (list "archive"))))
15509         list-type)
15510     (setq gnus-have-read-active-file nil)
15511     (save-excursion
15512       (set-buffer nntp-server-buffer)
15513       (while methods
15514         (let* ((method (if (stringp (car methods))
15515                            (gnus-server-get-method nil (car methods))
15516                          (car methods)))
15517                (where (nth 1 method))
15518                (mesg (format "Reading active file%s via %s..."
15519                              (if (and where (not (zerop (length where))))
15520                                  (concat " from " where) "")
15521                              (car method))))
15522           (gnus-message 5 mesg)
15523           (when (gnus-check-server method)
15524             ;; Request that the backend scan its incoming messages.
15525             (and (gnus-check-backend-function 'request-scan (car method))
15526                  (gnus-request-scan nil method))
15527             (cond
15528              ((and (eq gnus-read-active-file 'some)
15529                    (gnus-check-backend-function 'retrieve-groups (car method)))
15530               (let ((newsrc (cdr gnus-newsrc-alist))
15531                     (gmethod (if (stringp method)
15532                                  (gnus-server-get-method nil method)
15533                                method))
15534                     groups info)
15535                 (while (setq info (pop newsrc))
15536                   (when (gnus-server-equal
15537                          (gnus-find-method-for-group 
15538                           (gnus-info-group info) info)
15539                          gmethod)
15540                     (push (gnus-group-real-name (gnus-info-group info)) 
15541                           groups)))
15542                 (when groups
15543                   (gnus-check-server method)
15544                   (setq list-type (gnus-retrieve-groups groups method))
15545                   (cond
15546                    ((not list-type)
15547                     (gnus-message
15548                      1 "Cannot read partial active file from %s server."
15549                      (car method))
15550                     (ding)
15551                     (sit-for 2))
15552                    ((eq list-type 'active)
15553                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15554                    (t
15555                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15556              (t
15557               (if (not (gnus-request-list method))
15558                   (progn
15559                     (unless (equal method gnus-message-archive-method)
15560                       (gnus-message 1 "Cannot read active file from %s server."
15561                                     (car method))
15562                       (ding)))
15563                 (gnus-active-to-gnus-format method)
15564                 ;; We mark this active file as read.
15565                 (push method gnus-have-read-active-file)
15566                 (gnus-message 5 "%sdone" mesg))))))
15567         (setq methods (cdr methods))))))
15568
15569 ;; Read an active file and place the results in `gnus-active-hashtb'.
15570 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15571   (unless method
15572     (setq method gnus-select-method))
15573   (let ((cur (current-buffer))
15574         (hashtb (or hashtb
15575                     (if (and gnus-active-hashtb
15576                              (not (equal method gnus-select-method)))
15577                         gnus-active-hashtb
15578                       (setq gnus-active-hashtb
15579                             (if (equal method gnus-select-method)
15580                                 (gnus-make-hashtable
15581                                  (count-lines (point-min) (point-max)))
15582                               (gnus-make-hashtable 4096))))))
15583         (flag-hashtb (gnus-make-hashtable 60)))
15584     ;; Delete unnecessary lines.
15585     (goto-char (point-min))
15586     (while (search-forward "\nto." nil t)
15587       (delete-region (1+ (match-beginning 0))
15588                      (progn (forward-line 1) (point))))
15589     (or (string= gnus-ignored-newsgroups "")
15590         (progn
15591           (goto-char (point-min))
15592           (delete-matching-lines gnus-ignored-newsgroups)))
15593     ;; Make the group names readable as a lisp expression even if they
15594     ;; contain special characters.
15595     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15596     (goto-char (point-max))
15597     (while (re-search-backward "[][';?()#]" nil t)
15598       (insert ?\\))
15599     ;; If these are groups from a foreign select method, we insert the
15600     ;; group prefix in front of the group names.
15601     (and method (not (gnus-server-equal
15602                       (gnus-server-get-method nil method)
15603                       (gnus-server-get-method nil gnus-select-method)))
15604          (let ((prefix (gnus-group-prefixed-name "" method)))
15605            (goto-char (point-min))
15606            (while (and (not (eobp))
15607                        (progn (insert prefix)
15608                               (zerop (forward-line 1)))))))
15609     ;; Store the active file in a hash table.
15610     (goto-char (point-min))
15611     (if (string-match "%[oO]" gnus-group-line-format)
15612         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15613         ;; If we want information on moderated groups, we use this
15614         ;; loop...
15615         (let* ((mod-hashtb (make-vector 7 0))
15616                (m (intern "m" mod-hashtb))
15617                group max min)
15618           (while (not (eobp))
15619             (condition-case nil
15620                 (progn
15621                   (narrow-to-region (point) (gnus-point-at-eol))
15622                   (setq group (let ((obarray hashtb)) (read cur)))
15623                   (if (and (numberp (setq max (read cur)))
15624                            (numberp (setq min (read cur)))
15625                            (progn
15626                              (skip-chars-forward " \t")
15627                              (not
15628                               (or (= (following-char) ?=)
15629                                   (= (following-char) ?x)
15630                                   (= (following-char) ?j)))))
15631                       (set group (cons min max))
15632                     (set group nil))
15633                   ;; Enter moderated groups into a list.
15634                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15635                       (setq gnus-moderated-list
15636                             (cons (symbol-name group) gnus-moderated-list))))
15637               (error
15638                (and group
15639                     (symbolp group)
15640                     (set group nil))))
15641             (widen)
15642             (forward-line 1)))
15643       ;; And if we do not care about moderation, we use this loop,
15644       ;; which is faster.
15645       (let (group max min)
15646         (while (not (eobp))
15647           (condition-case ()
15648               (progn
15649                 (narrow-to-region (point) (gnus-point-at-eol))
15650                 ;; group gets set to a symbol interned in the hash table
15651                 ;; (what a hack!!) - jwz
15652                 (setq group (let ((obarray hashtb)) (read cur)))
15653                 (if (and (numberp (setq max (read cur)))
15654                          (numberp (setq min (read cur)))
15655                          (progn
15656                            (skip-chars-forward " \t")
15657                            (not
15658                             (or (= (following-char) ?=)
15659                                 (= (following-char) ?x)
15660                                 (= (following-char) ?j)))))
15661                     (set group (cons min max))
15662                   (set group nil)))
15663             (error
15664              (progn
15665                (and group
15666                     (symbolp group)
15667                     (set group nil))
15668                (or ignore-errors
15669                    (gnus-message 3 "Warning - illegal active: %s"
15670                                  (buffer-substring
15671                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15672           (widen)
15673           (forward-line 1))))))
15674
15675 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15676   ;; Parse a "groups" active file.
15677   (let ((cur (current-buffer))
15678         (hashtb (or hashtb
15679                     (if (and method gnus-active-hashtb)
15680                         gnus-active-hashtb
15681                       (setq gnus-active-hashtb
15682                             (gnus-make-hashtable
15683                              (count-lines (point-min) (point-max)))))))
15684         (prefix (and method
15685                      (not (gnus-server-equal
15686                            (gnus-server-get-method nil method)
15687                            (gnus-server-get-method nil gnus-select-method)))
15688                      (gnus-group-prefixed-name "" method))))
15689
15690     (goto-char (point-min))
15691     ;; We split this into to separate loops, one with the prefix
15692     ;; and one without to speed the reading up somewhat.
15693     (if prefix
15694         (let (min max opoint group)
15695           (while (not (eobp))
15696             (condition-case ()
15697                 (progn
15698                   (read cur) (read cur)
15699                   (setq min (read cur)
15700                         max (read cur)
15701                         opoint (point))
15702                   (skip-chars-forward " \t")
15703                   (insert prefix)
15704                   (goto-char opoint)
15705                   (set (let ((obarray hashtb)) (read cur))
15706                        (cons min max)))
15707               (error (and group (symbolp group) (set group nil))))
15708             (forward-line 1)))
15709       (let (min max group)
15710         (while (not (eobp))
15711           (condition-case ()
15712               (if (= (following-char) ?2)
15713                   (progn
15714                     (read cur) (read cur)
15715                     (setq min (read cur)
15716                           max (read cur))
15717                     (set (setq group (let ((obarray hashtb)) (read cur)))
15718                          (cons min max))))
15719             (error (and group (symbolp group) (set group nil))))
15720           (forward-line 1))))))
15721
15722 (defun gnus-read-newsrc-file (&optional force)
15723   "Read startup file.
15724 If FORCE is non-nil, the .newsrc file is read."
15725   ;; Reset variables that might be defined in the .newsrc.eld file.
15726   (let ((variables gnus-variable-list))
15727     (while variables
15728       (set (car variables) nil)
15729       (setq variables (cdr variables))))
15730   (let* ((newsrc-file gnus-current-startup-file)
15731          (quick-file (concat newsrc-file ".el")))
15732     (save-excursion
15733       ;; We always load the .newsrc.eld file.  If always contains
15734       ;; much information that can not be gotten from the .newsrc
15735       ;; file (ticked articles, killed groups, foreign methods, etc.)
15736       (gnus-read-newsrc-el-file quick-file)
15737
15738       (if (or force
15739               (and (file-newer-than-file-p newsrc-file quick-file)
15740                    (file-newer-than-file-p newsrc-file
15741                                            (concat quick-file "d")))
15742               (not gnus-newsrc-alist))
15743           ;; We read the .newsrc file.  Note that if there if a
15744           ;; .newsrc.eld file exists, it has already been read, and
15745           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15746           ;; the .newsrc file, Gnus will only use the information it
15747           ;; can find there for changing the data already read -
15748           ;; ie. reading the .newsrc file will not trash the data
15749           ;; already read (except for read articles).
15750           (save-excursion
15751             (gnus-message 5 "Reading %s..." newsrc-file)
15752             (set-buffer (find-file-noselect newsrc-file))
15753             (buffer-disable-undo (current-buffer))
15754             (gnus-newsrc-to-gnus-format)
15755             (kill-buffer (current-buffer))
15756             (gnus-message 5 "Reading %s...done" newsrc-file)))
15757
15758       ;; Read any slave files.
15759       (or gnus-slave
15760           (gnus-master-read-slave-newsrc)))))
15761
15762 (defun gnus-read-newsrc-el-file (file)
15763   (let ((ding-file (concat file "d")))
15764     ;; We always, always read the .eld file.
15765     (gnus-message 5 "Reading %s..." ding-file)
15766     (let (gnus-newsrc-assoc)
15767       (condition-case nil
15768           (load ding-file t t t)
15769         (error
15770          (gnus-message 1 "Error in %s" ding-file)
15771          (ding)))
15772       (when gnus-newsrc-assoc
15773         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15774     (gnus-make-hashtable-from-newsrc-alist)
15775     (when (file-newer-than-file-p file ding-file)
15776       ;; Old format quick file
15777       (gnus-message 5 "Reading %s..." file)
15778       ;; The .el file is newer than the .eld file, so we read that one
15779       ;; as well.
15780       (gnus-read-old-newsrc-el-file file))))
15781
15782 ;; Parse the old-style quick startup file
15783 (defun gnus-read-old-newsrc-el-file (file)
15784   (let (newsrc killed marked group m)
15785     (prog1
15786         (let ((gnus-killed-assoc nil)
15787               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15788           (prog1
15789               (condition-case nil
15790                   (load file t t t)
15791                 (error nil))
15792             (setq newsrc gnus-newsrc-assoc
15793                   killed gnus-killed-assoc
15794                   marked gnus-marked-assoc)))
15795       (setq gnus-newsrc-alist nil)
15796       (while newsrc
15797         (setq group (car newsrc))
15798         (let ((info (gnus-get-info (car group))))
15799           (if info
15800               (progn
15801                 (gnus-info-set-read info (cddr group))
15802                 (gnus-info-set-level
15803                  info (if (nth 1 group) gnus-level-default-subscribed
15804                         gnus-level-default-unsubscribed))
15805                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15806             (setq gnus-newsrc-alist
15807                   (cons
15808                    (setq info
15809                          (list (car group)
15810                                (if (nth 1 group) gnus-level-default-subscribed
15811                                  gnus-level-default-unsubscribed)
15812                                (cddr group)))
15813                    gnus-newsrc-alist)))
15814           (if (setq m (assoc (car group) marked))
15815               (gnus-info-set-marks
15816                info (cons (list (cons 'tick (gnus-compress-sequence
15817                                              (sort (cdr m) '<) t)))
15818                           nil))))
15819         (setq newsrc (cdr newsrc)))
15820       (setq newsrc killed)
15821       (while newsrc
15822         (setcar newsrc (caar newsrc))
15823         (setq newsrc (cdr newsrc)))
15824       (setq gnus-killed-list killed))
15825     ;; The .el file version of this variable does not begin with
15826     ;; "options", while the .eld version does, so we just add it if it
15827     ;; isn't there.
15828     (and
15829      gnus-newsrc-options
15830      (progn
15831        (and (not (string-match "^ *options" gnus-newsrc-options))
15832             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15833        (and (not (string-match "\n$" gnus-newsrc-options))
15834             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15835        ;; Finally, if we read some options lines, we parse them.
15836        (or (string= gnus-newsrc-options "")
15837            (gnus-newsrc-parse-options gnus-newsrc-options))))
15838
15839     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15840     (gnus-make-hashtable-from-newsrc-alist)))
15841
15842 (defun gnus-make-newsrc-file (file)
15843   "Make server dependent file name by catenating FILE and server host name."
15844   (let* ((file (expand-file-name file nil))
15845          (real-file (concat file "-" (nth 1 gnus-select-method))))
15846     (if (or (file-exists-p real-file)
15847             (file-exists-p (concat real-file ".el"))
15848             (file-exists-p (concat real-file ".eld")))
15849         real-file file)))
15850
15851 (defun gnus-newsrc-to-gnus-format ()
15852   (setq gnus-newsrc-options "")
15853   (setq gnus-newsrc-options-n nil)
15854
15855   (or gnus-active-hashtb
15856       (setq gnus-active-hashtb (make-vector 4095 0)))
15857   (let ((buf (current-buffer))
15858         (already-read (> (length gnus-newsrc-alist) 1))
15859         group subscribed options-symbol newsrc Options-symbol
15860         symbol reads num1)
15861     (goto-char (point-min))
15862     ;; We intern the symbol `options' in the active hashtb so that we
15863     ;; can `eq' against it later.
15864     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15865     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15866
15867     (while (not (eobp))
15868       ;; We first read the first word on the line by narrowing and
15869       ;; then reading into `gnus-active-hashtb'.  Most groups will
15870       ;; already exist in that hashtb, so this will save some string
15871       ;; space.
15872       (narrow-to-region
15873        (point)
15874        (progn (skip-chars-forward "^ \t!:\n") (point)))
15875       (goto-char (point-min))
15876       (setq symbol
15877             (and (/= (point-min) (point-max))
15878                  (let ((obarray gnus-active-hashtb)) (read buf))))
15879       (widen)
15880       ;; Now, the symbol we have read is either `options' or a group
15881       ;; name.  If it is an options line, we just add it to a string.
15882       (cond
15883        ((or (eq symbol options-symbol)
15884             (eq symbol Options-symbol))
15885         (setq gnus-newsrc-options
15886               ;; This concating is quite inefficient, but since our
15887               ;; thorough studies show that approx 99.37% of all
15888               ;; .newsrc files only contain a single options line, we
15889               ;; don't give a damn, frankly, my dear.
15890               (concat gnus-newsrc-options
15891                       (buffer-substring
15892                        (gnus-point-at-bol)
15893                        ;; Options may continue on the next line.
15894                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15895                                 (progn (beginning-of-line) (point)))
15896                            (point)))))
15897         (forward-line -1))
15898        (symbol
15899         ;; Group names can be just numbers.  
15900         (when (numberp symbol) 
15901           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15902         (or (boundp symbol) (set symbol nil))
15903         ;; It was a group name.
15904         (setq subscribed (= (following-char) ?:)
15905               group (symbol-name symbol)
15906               reads nil)
15907         (if (eolp)
15908             ;; If the line ends here, this is clearly a buggy line, so
15909             ;; we put point a the beginning of line and let the cond
15910             ;; below do the error handling.
15911             (beginning-of-line)
15912           ;; We skip to the beginning of the ranges.
15913           (skip-chars-forward "!: \t"))
15914         ;; We are now at the beginning of the list of read articles.
15915         ;; We read them range by range.
15916         (while
15917             (cond
15918              ((looking-at "[0-9]+")
15919               ;; We narrow and read a number instead of buffer-substring/
15920               ;; string-to-int because it's faster.  narrow/widen is
15921               ;; faster than save-restriction/narrow, and save-restriction
15922               ;; produces a garbage object.
15923               (setq num1 (progn
15924                            (narrow-to-region (match-beginning 0) (match-end 0))
15925                            (read buf)))
15926               (widen)
15927               ;; If the next character is a dash, then this is a range.
15928               (if (= (following-char) ?-)
15929                   (progn
15930                     ;; We read the upper bound of the range.
15931                     (forward-char 1)
15932                     (if (not (looking-at "[0-9]+"))
15933                         ;; This is a buggy line, by we pretend that
15934                         ;; it's kinda OK.  Perhaps the user should be
15935                         ;; dinged?
15936                         (setq reads (cons num1 reads))
15937                       (setq reads
15938                             (cons
15939                              (cons num1
15940                                    (progn
15941                                      (narrow-to-region (match-beginning 0)
15942                                                        (match-end 0))
15943                                      (read buf)))
15944                              reads))
15945                       (widen)))
15946                 ;; It was just a simple number, so we add it to the
15947                 ;; list of ranges.
15948                 (setq reads (cons num1 reads)))
15949               ;; If the next char in ?\n, then we have reached the end
15950               ;; of the line and return nil.
15951               (/= (following-char) ?\n))
15952              ((= (following-char) ?\n)
15953               ;; End of line, so we end.
15954               nil)
15955              (t
15956               ;; Not numbers and not eol, so this might be a buggy
15957               ;; line...
15958               (or (eobp)
15959                   ;; If it was eob instead of ?\n, we allow it.
15960                   (progn
15961                     ;; The line was buggy.
15962                     (setq group nil)
15963                     (gnus-message 3 "Mangled line: %s"
15964                                   (buffer-substring (gnus-point-at-bol)
15965                                                     (gnus-point-at-eol)))
15966                     (ding)
15967                     (sit-for 1)))
15968               nil))
15969           ;; Skip past ", ".  Spaces are illegal in these ranges, but
15970           ;; we allow them, because it's a common mistake to put a
15971           ;; space after the comma.
15972           (skip-chars-forward ", "))
15973
15974         ;; We have already read .newsrc.eld, so we gently update the
15975         ;; data in the hash table with the information we have just
15976         ;; read.
15977         (when group
15978           (let ((info (gnus-get-info group))
15979                 level)
15980             (if info
15981                 ;; There is an entry for this file in the alist.
15982                 (progn
15983                   (gnus-info-set-read info (nreverse reads))
15984                   ;; We update the level very gently.  In fact, we
15985                   ;; only change it if there's been a status change
15986                   ;; from subscribed to unsubscribed, or vice versa.
15987                   (setq level (gnus-info-level info))
15988                   (cond ((and (<= level gnus-level-subscribed)
15989                               (not subscribed))
15990                          (setq level (if reads
15991                                          gnus-level-default-unsubscribed
15992                                        (1+ gnus-level-default-unsubscribed))))
15993                         ((and (> level gnus-level-subscribed) subscribed)
15994                          (setq level gnus-level-default-subscribed)))
15995                   (gnus-info-set-level info level))
15996               ;; This is a new group.
15997               (setq info (list group
15998                                (if subscribed
15999                                    gnus-level-default-subscribed
16000                                  (if reads
16001                                      (1+ gnus-level-subscribed)
16002                                    gnus-level-default-unsubscribed))
16003                                (nreverse reads))))
16004             (setq newsrc (cons info newsrc))))))
16005       (forward-line 1))
16006
16007     (setq newsrc (nreverse newsrc))
16008
16009     (if (not already-read)
16010         ()
16011       ;; We now have two newsrc lists - `newsrc', which is what we
16012       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16013       ;; what we've read from .newsrc.eld.  We have to merge these
16014       ;; lists.  We do this by "attaching" any (foreign) groups in the
16015       ;; gnus-newsrc-alist to the (native) group that precedes them.
16016       (let ((rc (cdr gnus-newsrc-alist))
16017             (prev gnus-newsrc-alist)
16018             entry mentry)
16019         (while rc
16020           (or (null (nth 4 (car rc)))   ; It's a native group.
16021               (assoc (caar rc) newsrc) ; It's already in the alist.
16022               (if (setq entry (assoc (caar prev) newsrc))
16023                   (setcdr (setq mentry (memq entry newsrc))
16024                           (cons (car rc) (cdr mentry)))
16025                 (setq newsrc (cons (car rc) newsrc))))
16026           (setq prev rc
16027                 rc (cdr rc)))))
16028
16029     (setq gnus-newsrc-alist newsrc)
16030     ;; We make the newsrc hashtb.
16031     (gnus-make-hashtable-from-newsrc-alist)
16032
16033     ;; Finally, if we read some options lines, we parse them.
16034     (or (string= gnus-newsrc-options "")
16035         (gnus-newsrc-parse-options gnus-newsrc-options))))
16036
16037 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16038 ;; The return value will be a list on the form
16039 ;; ((regexp1 . ignore)
16040 ;;  (regexp2 . subscribe)...)
16041 ;; When handling new newsgroups, groups that match a `ignore' regexp
16042 ;; will be ignored, and groups that match a `subscribe' regexp will be
16043 ;; subscribed.  A line like
16044 ;; options -n !all rec.all
16045 ;; will lead to a list that looks like
16046 ;; (("^rec\\..+" . subscribe)
16047 ;;  ("^.+" . ignore))
16048 ;; So all "rec.*" groups will be subscribed, while all the other
16049 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16050 ;; different from "options -n rec.all !all".
16051 (defun gnus-newsrc-parse-options (options)
16052   (let (out eol)
16053     (save-excursion
16054       (gnus-set-work-buffer)
16055       (insert (regexp-quote options))
16056       ;; First we treat all continuation lines.
16057       (goto-char (point-min))
16058       (while (re-search-forward "\n[ \t]+" nil t)
16059         (replace-match " " t t))
16060       ;; Then we transform all "all"s into ".+"s.
16061       (goto-char (point-min))
16062       (while (re-search-forward "\\ball\\b" nil t)
16063         (replace-match ".+" t t))
16064       (goto-char (point-min))
16065       ;; We remove all other options than the "-n" ones.
16066       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16067         (replace-match " ")
16068         (forward-char -1))
16069       (goto-char (point-min))
16070
16071       ;; We are only interested in "options -n" lines - we
16072       ;; ignore the other option lines.
16073       (while (re-search-forward "[ \t]-n" nil t)
16074         (setq eol
16075               (or (save-excursion
16076                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16077                          (- (point) 2)))
16078                   (gnus-point-at-eol)))
16079         ;; Search for all "words"...
16080         (while (re-search-forward "[^ \t,\n]+" eol t)
16081           (if (= (char-after (match-beginning 0)) ?!)
16082               ;; If the word begins with a bang (!), this is a "not"
16083               ;; spec.  We put this spec (minus the bang) and the
16084               ;; symbol `ignore' into the list.
16085               (setq out (cons (cons (concat
16086                                      "^" (buffer-substring
16087                                           (1+ (match-beginning 0))
16088                                           (match-end 0)))
16089                                     'ignore) out))
16090             ;; There was no bang, so this is a "yes" spec.
16091             (setq out (cons (cons (concat "^" (match-string 0))
16092                                   'subscribe) out)))))
16093
16094       (setq gnus-newsrc-options-n out))))
16095
16096 (defun gnus-save-newsrc-file (&optional force)
16097   "Save .newsrc file."
16098   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16099   ;; from the variable gnus-newsrc-alist.
16100   (when (and (or gnus-newsrc-alist gnus-killed-list)
16101              gnus-current-startup-file)
16102     (save-excursion
16103       (if (and (or gnus-use-dribble-file gnus-slave)
16104                (not force)
16105                (or (not gnus-dribble-buffer)
16106                    (not (buffer-name gnus-dribble-buffer))
16107                    (zerop (save-excursion
16108                             (set-buffer gnus-dribble-buffer)
16109                             (buffer-size)))))
16110           (gnus-message 4 "(No changes need to be saved)")
16111         (run-hooks 'gnus-save-newsrc-hook)
16112         (if gnus-slave
16113             (gnus-slave-save-newsrc)
16114           ;; Save .newsrc.
16115           (when gnus-save-newsrc-file
16116             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16117             (gnus-gnus-to-newsrc-format)
16118             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16119           ;; Save .newsrc.eld.
16120           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16121           (make-local-variable 'version-control)
16122           (setq version-control 'never)
16123           (setq buffer-file-name
16124                 (concat gnus-current-startup-file ".eld"))
16125           (gnus-add-current-to-buffer-list)
16126           (buffer-disable-undo (current-buffer))
16127           (erase-buffer)
16128           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16129           (gnus-gnus-to-quick-newsrc-format)
16130           (run-hooks 'gnus-save-quick-newsrc-hook)
16131           (save-buffer)
16132           (kill-buffer (current-buffer))
16133           (gnus-message
16134            5 "Saving %s.eld...done" gnus-current-startup-file))
16135         (gnus-dribble-delete-file)))))
16136
16137 (defun gnus-gnus-to-quick-newsrc-format ()
16138   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16139   (insert ";; Gnus startup file.\n")
16140   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16141   (insert ";; to read .newsrc.\n")
16142   (insert "(setq gnus-newsrc-file-version "
16143           (prin1-to-string gnus-version) ")\n")
16144   (let ((variables
16145          (if gnus-save-killed-list gnus-variable-list
16146            ;; Remove the `gnus-killed-list' from the list of variables
16147            ;; to be saved, if required.
16148            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16149         ;; Peel off the "dummy" group.
16150         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16151         variable)
16152     ;; Insert the variables into the file.
16153     (while variables
16154       (when (and (boundp (setq variable (pop variables)))
16155                  (symbol-value variable))
16156         (insert "(setq " (symbol-name variable) " '"
16157                 (prin1-to-string (symbol-value variable)) ")\n")))))
16158
16159 (defun gnus-gnus-to-newsrc-format ()
16160   ;; Generate and save the .newsrc file.
16161   (let ((newsrc (cdr gnus-newsrc-alist))
16162         info ranges range)
16163     (save-excursion
16164       (set-buffer (create-file-buffer gnus-current-startup-file))
16165       (setq buffer-file-name gnus-current-startup-file)
16166       (buffer-disable-undo (current-buffer))
16167       (erase-buffer)
16168       ;; Write options.
16169       (if gnus-newsrc-options (insert gnus-newsrc-options))
16170       ;; Write subscribed and unsubscribed.
16171       (while newsrc
16172         (setq info (car newsrc))
16173         (if (not (gnus-info-method info))
16174             ;; Don't write foreign groups to .newsrc.
16175             (progn
16176               (insert (gnus-info-group info)
16177                       (if (> (gnus-info-level info) gnus-level-subscribed)
16178                           "!" ":"))
16179               (if (setq ranges (gnus-info-read info))
16180                   (progn
16181                     (insert " ")
16182                     (if (not (listp (cdr ranges)))
16183                         (if (= (car ranges) (cdr ranges))
16184                             (insert (int-to-string (car ranges)))
16185                           (insert (int-to-string (car ranges)) "-"
16186                                   (int-to-string (cdr ranges))))
16187                       (while ranges
16188                         (setq range (car ranges)
16189                               ranges (cdr ranges))
16190                         (if (or (atom range) (= (car range) (cdr range)))
16191                             (insert (int-to-string
16192                                      (or (and (atom range) range)
16193                                          (car range))))
16194                           (insert (int-to-string (car range)) "-"
16195                                   (int-to-string (cdr range))))
16196                         (if ranges (insert ","))))))
16197               (insert "\n")))
16198         (setq newsrc (cdr newsrc)))
16199       (make-local-variable 'version-control)
16200       (setq version-control 'never)
16201       ;; It has been reported that sometime the modtime on the .newsrc
16202       ;; file seems to be off.  We really do want to overwrite it, so
16203       ;; we clear the modtime here before saving.  It's a bit odd,
16204       ;; though...
16205       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16206       ;; delete the silly thing entirely first.  but this fails to provide
16207       ;; such niceties as .newsrc~ creation.
16208       (if gnus-modtime-botch
16209           (delete-file gnus-startup-file)
16210         (clear-visited-file-modtime))
16211       (run-hooks 'gnus-save-standard-newsrc-hook)
16212       (save-buffer)
16213       (kill-buffer (current-buffer)))))
16214
16215 \f
16216 ;;;
16217 ;;; Slave functions.
16218 ;;;
16219
16220 (defun gnus-slave-save-newsrc ()
16221   (save-excursion
16222     (set-buffer gnus-dribble-buffer)
16223     (let ((slave-name
16224            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16225       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16226
16227 (defun gnus-master-read-slave-newsrc ()
16228   (let ((slave-files
16229          (directory-files
16230           (file-name-directory gnus-current-startup-file)
16231           t (concat
16232              "^" (regexp-quote
16233                   (concat
16234                    (file-name-nondirectory gnus-current-startup-file)
16235                    "-slave-")))
16236           t))
16237         file)
16238     (if (not slave-files)
16239         ()                              ; There are no slave files to read.
16240       (gnus-message 7 "Reading slave newsrcs...")
16241       (save-excursion
16242         (set-buffer (get-buffer-create " *gnus slave*"))
16243         (buffer-disable-undo (current-buffer))
16244         (setq slave-files
16245               (sort (mapcar (lambda (file)
16246                               (list (nth 5 (file-attributes file)) file))
16247                             slave-files)
16248                     (lambda (f1 f2)
16249                       (or (< (caar f1) (caar f2))
16250                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16251         (while slave-files
16252           (erase-buffer)
16253           (setq file (nth 1 (car slave-files)))
16254           (insert-file-contents file)
16255           (if (condition-case ()
16256                   (progn
16257                     (eval-buffer (current-buffer))
16258                     t)
16259                 (error
16260                  (gnus-message 3 "Possible error in %s" file)
16261                  (ding)
16262                  (sit-for 2)
16263                  nil))
16264               (or gnus-slave ; Slaves shouldn't delete these files.
16265                   (condition-case ()
16266                       (delete-file file)
16267                     (error nil))))
16268           (setq slave-files (cdr slave-files))))
16269       (gnus-message 7 "Reading slave newsrcs...done"))))
16270
16271 \f
16272 ;;;
16273 ;;; Group description.
16274 ;;;
16275
16276 (defun gnus-read-all-descriptions-files ()
16277   (let ((methods (cons gnus-select-method 
16278                        (nconc
16279                         (when gnus-message-archive-method
16280                           (list "archive"))
16281                         gnus-secondary-select-methods))))
16282     (while methods
16283       (gnus-read-descriptions-file (car methods))
16284       (setq methods (cdr methods)))
16285     t))
16286
16287 (defun gnus-read-descriptions-file (&optional method)
16288   (let ((method (or method gnus-select-method)))
16289     (when (stringp method)
16290       (setq method (gnus-server-to-method method)))
16291     ;; We create the hashtable whether we manage to read the desc file
16292     ;; to avoid trying to re-read after a failed read.
16293     (or gnus-description-hashtb
16294         (setq gnus-description-hashtb
16295               (gnus-make-hashtable (length gnus-active-hashtb))))
16296     ;; Mark this method's desc file as read.
16297     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16298                   gnus-description-hashtb)
16299
16300     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16301     (cond
16302      ((not (gnus-check-server method))
16303       (gnus-message 1 "Couldn't open server")
16304       nil)
16305      ((not (gnus-request-list-newsgroups method))
16306       (gnus-message 1 "Couldn't read newsgroups descriptions")
16307       nil)
16308      (t
16309       (let (group)
16310         (save-excursion
16311           (save-restriction
16312             (set-buffer nntp-server-buffer)
16313             (goto-char (point-min))
16314             (if (or (search-forward "\n.\n" nil t)
16315                     (goto-char (point-max)))
16316                 (progn
16317                   (beginning-of-line)
16318                   (narrow-to-region (point-min) (point))))
16319             (goto-char (point-min))
16320             (while (not (eobp))
16321               ;; If we get an error, we set group to 0, which is not a
16322               ;; symbol...
16323               (setq group
16324                     (condition-case ()
16325                         (let ((obarray gnus-description-hashtb))
16326                           ;; Group is set to a symbol interned in this
16327                           ;; hash table.
16328                           (read nntp-server-buffer))
16329                       (error 0)))
16330               (skip-chars-forward " \t")
16331               ;; ...  which leads to this line being effectively ignored.
16332               (and (symbolp group)
16333                    (set group (buffer-substring
16334                                (point) (progn (end-of-line) (point)))))
16335               (forward-line 1))))
16336         (gnus-message 5 "Reading descriptions file...done")
16337         t)))))
16338
16339 (defun gnus-group-get-description (group)
16340   "Get the description of a group by sending XGTITLE to the server."
16341   (when (gnus-request-group-description group)
16342     (save-excursion
16343       (set-buffer nntp-server-buffer)
16344       (goto-char (point-min))
16345       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16346         (match-string 1)))))
16347
16348 \f
16349 ;;;
16350 ;;; Buffering of read articles.
16351 ;;;
16352
16353 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16354 (defvar gnus-backlog-articles nil)
16355 (defvar gnus-backlog-hashtb nil)
16356
16357 (defun gnus-backlog-buffer ()
16358   "Return the backlog buffer."
16359   (or (get-buffer gnus-backlog-buffer)
16360       (save-excursion
16361         (set-buffer (get-buffer-create gnus-backlog-buffer))
16362         (buffer-disable-undo (current-buffer))
16363         (setq buffer-read-only t)
16364         (gnus-add-current-to-buffer-list)
16365         (get-buffer gnus-backlog-buffer))))
16366
16367 (defun gnus-backlog-setup ()
16368   "Initialize backlog variables."
16369   (unless gnus-backlog-hashtb
16370     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16371
16372 (defun gnus-backlog-shutdown ()
16373   "Clear all backlog variables and buffers."
16374   (when (get-buffer gnus-backlog-buffer)
16375     (kill-buffer gnus-backlog-buffer))
16376   (setq gnus-backlog-hashtb nil
16377         gnus-backlog-articles nil))
16378
16379 (defun gnus-backlog-enter-article (group number buffer)
16380   (gnus-backlog-setup)
16381   (let ((ident (intern (concat group ":" (int-to-string number))
16382                        gnus-backlog-hashtb))
16383         b)
16384     (if (memq ident gnus-backlog-articles)
16385         () ; It's already kept.
16386       ;; Remove the oldest article, if necessary.
16387       (and (numberp gnus-keep-backlog)
16388            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16389            (gnus-backlog-remove-oldest-article))
16390       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16391       ;; Insert the new article.
16392       (save-excursion
16393         (set-buffer (gnus-backlog-buffer))
16394         (let (buffer-read-only)
16395           (goto-char (point-max))
16396           (or (bolp) (insert "\n"))
16397           (setq b (point))
16398           (insert-buffer-substring buffer)
16399           ;; Tag the beginning of the article with the ident.
16400           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16401
16402 (defun gnus-backlog-remove-oldest-article ()
16403   (save-excursion
16404     (set-buffer (gnus-backlog-buffer))
16405     (goto-char (point-min))
16406     (if (zerop (buffer-size))
16407         () ; The buffer is empty.
16408       (let ((ident (get-text-property (point) 'gnus-backlog))
16409             buffer-read-only)
16410         ;; Remove the ident from the list of articles.
16411         (when ident
16412           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16413         ;; Delete the article itself.
16414         (delete-region
16415          (point) (next-single-property-change
16416                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16417
16418 (defun gnus-backlog-request-article (group number buffer)
16419   (gnus-backlog-setup)
16420   (let ((ident (intern (concat group ":" (int-to-string number))
16421                        gnus-backlog-hashtb))
16422         beg end)
16423     (when (memq ident gnus-backlog-articles)
16424       ;; It was in the backlog.
16425       (save-excursion
16426         (set-buffer (gnus-backlog-buffer))
16427         (if (not (setq beg (text-property-any
16428                             (point-min) (point-max) 'gnus-backlog
16429                             ident)))
16430             ;; It wasn't in the backlog after all.
16431             (ignore
16432              (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16433           ;; Find the end (i. e., the beginning of the next article).
16434           (setq end
16435                 (next-single-property-change
16436                  (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16437       (let ((buffer-read-only nil))
16438         (erase-buffer)
16439         (insert-buffer-substring gnus-backlog-buffer beg end)
16440         t))))
16441
16442 ;; Allow redefinition of Gnus functions.
16443
16444 (gnus-ems-redefine)
16445
16446 (provide 'gnus)
16447
16448 ;;; gnus.el ends here