*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus-load)
29 (require 'gnus-group)
30 (require 'gnus-spec)
31 (require 'gnus-range)
32 (require 'gnus-int)
33 (require 'gnus-undo)
34 (require 'gnus)
35
36 (defvar gnus-kill-summary-on-exit t
37   "*If non-nil, kill the summary buffer when you exit from it.
38 If nil, the summary will become a \"*Dead Summary*\" buffer, and
39 it will be killed sometime later.")
40
41 (defvar gnus-fetch-old-headers nil
42   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
43 If an unread article in the group refers to an older, already read (or
44 just marked as read) article, the old article will not normally be
45 displayed in the Summary buffer.  If this variable is non-nil, Gnus
46 will attempt to grab the headers to the old articles, and thereby
47 build complete threads.  If it has the value `some', only enough
48 headers to connect otherwise loose threads will be displayed.
49 This variable can also be a number.  In that case, no more than that
50 number of old headers will be fetched.
51
52 The server has to support NOV for any of this to work.")
53
54 (defvar gnus-summary-make-false-root 'adopt
55   "*nil means that Gnus won't gather loose threads.
56 If the root of a thread has expired or been read in a previous
57 session, the information necessary to build a complete thread has been
58 lost.  Instead of having many small sub-threads from this original thread
59 scattered all over the summary buffer, Gnus can gather them.
60
61 If non-nil, Gnus will try to gather all loose sub-threads from an
62 original thread into one large thread.
63
64 If this variable is non-nil, it should be one of `none', `adopt',
65 `dummy' or `empty'.
66
67 If this variable is `none', Gnus will not make a false root, but just
68 present the sub-threads after another.
69 If this variable is `dummy', Gnus will create a dummy root that will
70 have all the sub-threads as children.
71 If this variable is `adopt', Gnus will make one of the \"children\"
72 the parent and mark all the step-children as such.
73 If this variable is `empty', the \"children\" are printed with empty
74 subject fields.  (Or rather, they will be printed with a string
75 given by the `gnus-summary-same-subject' variable.)")
76
77 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
78   "*A regexp to match subjects to be excluded from loose thread gathering.
79 As loose thread gathering is done on subjects only, that means that
80 there can be many false gatherings performed.  By rooting out certain
81 common subjects, gathering might become saner.")
82
83 (defvar gnus-summary-gather-subject-limit nil
84   "*Maximum length of subject comparisons when gathering loose threads.
85 Use nil to compare full subjects.  Setting this variable to a low
86 number will help gather threads that have been corrupted by
87 newsreaders chopping off subject lines, but it might also mean that
88 unrelated articles that have subject that happen to begin with the
89 same few characters will be incorrectly gathered.
90
91 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
92 comparing subjects.")
93
94 (defvar gnus-simplify-ignored-prefixes nil
95   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily.")
96
97 (defvar gnus-build-sparse-threads nil
98   "*If non-nil, fill in the gaps in threads.
99 If `some', only fill in the gaps that are needed to tie loose threads
100 together.  If `more', fill in all leaf nodes that Gnus can find.  If
101 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
102
103 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
104   "Function used for gathering loose threads.
105 There are two pre-defined functions: `gnus-gather-threads-by-subject',
106 which only takes Subjects into consideration; and
107 `gnus-gather-threads-by-references', which compared the References
108 headers of the articles to find matches.")
109
110 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
111 (defvar gnus-summary-same-subject ""
112   "*String indicating that the current article has the same subject as the previous.
113 This variable will only be used if the value of
114 `gnus-summary-make-false-root' is `empty'.")
115
116 (defvar gnus-summary-goto-unread t
117   "*If non-nil, marking commands will go to the next unread article.
118 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
119 whether it is read or not.")
120
121 (defvar gnus-summary-default-score 0
122   "*Default article score level.
123 If this variable is nil, scoring will be disabled.")
124
125 (defvar gnus-summary-zcore-fuzz 0
126   "*Fuzziness factor for the zcore in the summary buffer.
127 Articles with scores closer than this to `gnus-summary-default-score'
128 will not be marked.")
129
130 (defvar gnus-simplify-subject-fuzzy-regexp nil
131   "*Strings to be removed when doing fuzzy matches.
132 This can either be a regular expression or list of regular expressions
133 that will be removed from subject strings if fuzzy subject
134 simplification is selected.")
135
136 (defvar gnus-show-threads t
137   "*If non-nil, display threads in summary mode.")
138
139 (defvar gnus-thread-hide-subtree nil
140   "*If non-nil, hide all threads initially.
141 If threads are hidden, you have to run the command
142 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
143 to expose hidden threads.")
144
145 (defvar gnus-thread-hide-killed t
146   "*If non-nil, hide killed threads automatically.")
147
148 (defvar gnus-thread-ignore-subject nil
149   "*If non-nil, ignore subjects and do all threading based on the Reference header.
150 If nil, which is the default, articles that have different subjects
151 from their parents will start separate threads.")
152
153 (defvar gnus-thread-operation-ignore-subject t
154   "*If non-nil, subjects will be ignored when doing thread commands.
155 This affects commands like `gnus-summary-kill-thread' and
156 `gnus-summary-lower-thread'.
157
158 If this variable is nil, articles in the same thread with different
159 subjects will not be included in the operation in question.  If this
160 variable is `fuzzy', only articles that have subjects that are fuzzily
161 equal will be included.")
162
163 (defvar gnus-thread-indent-level 4
164   "*Number that says how much each sub-thread should be indented.")
165
166 (defvar gnus-auto-extend-newsgroup t
167   "*If non-nil, extend newsgroup forward and backward when requested.")
168
169 (defvar gnus-auto-select-first t
170   "*If nil, don't select the first unread article when entering a group.
171 If this variable is `best', select the highest-scored unread article
172 in the group.  If neither nil nor `best', select the first unread
173 article.
174
175 If you want to prevent automatic selection of the first unread article
176 in some newsgroups, set the variable to nil in
177 `gnus-select-group-hook'.")
178
179 (defvar gnus-auto-select-next t
180   "*If non-nil, offer to go to the next group from the end of the previous.
181 If the value is t and the next newsgroup is empty, Gnus will exit
182 summary mode and go back to group mode.  If the value is neither nil
183 nor t, Gnus will select the following unread newsgroup.  In
184 particular, if the value is the symbol `quietly', the next unread
185 newsgroup will be selected without any confirmation, and if it is
186 `almost-quietly', the next group will be selected without any
187 confirmation if you are located on the last article in the group.
188 Finally, if this variable is `slightly-quietly', the `Z n' command
189 will go to the next group without confirmation.")
190
191 (defvar gnus-auto-select-same nil
192   "*If non-nil, select the next article with the same subject.")
193
194 (defvar gnus-summary-check-current nil
195   "*If non-nil, consider the current article when moving.
196 The \"unread\" movement commands will stay on the same line if the
197 current article is unread.")
198
199 (defvar gnus-auto-center-summary t
200   "*If non-nil, always center the current summary buffer.
201 In particular, if `vertical' do only vertical recentering.  If non-nil
202 and non-`vertical', do both horizontal and vertical recentering.")
203
204 (defvar gnus-show-all-headers nil
205   "*If non-nil, don't hide any headers.")
206
207 (defvar gnus-single-article-buffer t
208   "*If non-nil, display all articles in the same buffer.
209 If nil, each group will get its own article buffer.")
210
211 (defvar gnus-break-pages t
212   "*If non-nil, do page breaking on articles.
213 The page delimiter is specified by the `gnus-page-delimiter'
214 variable.")
215
216 (defvar gnus-show-mime nil
217   "*If non-nil, do mime processing of articles.
218 The articles will simply be fed to the function given by
219 `gnus-show-mime-method'.")
220
221 (defvar gnus-move-split-methods nil
222   "*Variable used to suggest where articles are to be moved to.
223 It uses the same syntax as the `gnus-split-methods' variable.")
224
225 ;; Mark variables suggested by Thomas Michanek
226 ;; <Thomas.Michanek@telelogic.se>.
227 (defvar gnus-unread-mark ? 
228   "*Mark used for unread articles.")
229 (defvar gnus-ticked-mark ?!
230   "*Mark used for ticked articles.")
231 (defvar gnus-dormant-mark ??
232   "*Mark used for dormant articles.")
233 (defvar gnus-del-mark ?r
234   "*Mark used for del'd articles.")
235 (defvar gnus-read-mark ?R
236   "*Mark used for read articles.")
237 (defvar gnus-expirable-mark ?E
238   "*Mark used for expirable articles.")
239 (defvar gnus-killed-mark ?K
240   "*Mark used for killed articles.")
241 (defvar gnus-souped-mark ?F
242   "*Mark used for killed articles.")
243 (defvar gnus-kill-file-mark ?X
244   "*Mark used for articles killed by kill files.")
245 (defvar gnus-low-score-mark ?Y
246   "*Mark used for articles with a low score.")
247 (defvar gnus-catchup-mark ?C
248   "*Mark used for articles that are caught up.")
249 (defvar gnus-replied-mark ?A
250   "*Mark used for articles that have been replied to.")
251 (defvar gnus-cached-mark ?*
252   "*Mark used for articles that are in the cache.")
253 (defvar gnus-saved-mark ?S
254   "*Mark used for articles that have been saved to.")
255 (defvar gnus-ancient-mark ?O
256   "*Mark used for ancient articles.")
257 (defvar gnus-sparse-mark ?Q
258   "*Mark used for sparsely reffed articles.")
259 (defvar gnus-canceled-mark ?G
260   "*Mark used for canceled articles.")
261 (defvar gnus-duplicate-mark ?M
262   "*Mark used for duplicate articles.")
263 (defvar gnus-score-over-mark ?+
264   "*Score mark used for articles with high scores.")
265 (defvar gnus-score-below-mark ?-
266   "*Score mark used for articles with low scores.")
267 (defvar gnus-empty-thread-mark ? 
268   "*There is no thread under the article.")
269 (defvar gnus-not-empty-thread-mark ?=
270   "*There is a thread under the article.")
271
272 (defvar gnus-view-pseudo-asynchronously nil
273   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
274
275 (defvar gnus-view-pseudos nil
276   "*If `automatic', pseudo-articles will be viewed automatically.
277 If `not-confirm', pseudos will be viewed automatically, and the user
278 will not be asked to confirm the command.")
279
280 (defvar gnus-view-pseudos-separately t
281   "*If non-nil, one pseudo-article will be created for each file to be viewed.
282 If nil, all files that use the same viewing command will be given as a
283 list of parameters to that command.")
284
285 (defvar gnus-insert-pseudo-articles t
286   "*If non-nil, insert pseudo-articles when decoding articles.")
287
288 (defvar gnus-summary-dummy-line-format
289   "*  %(:                          :%) %S\n"
290   "*The format specification for the dummy roots in the summary buffer.
291 It works along the same lines as a normal formatting string,
292 with some simple extensions.
293
294 %S  The subject")
295
296 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
297   "*The format specification for the summary mode line.
298 It works along the same lines as a normal formatting string,
299 with some simple extensions:
300
301 %G  Group name
302 %p  Unprefixed group name
303 %A  Current article number
304 %V  Gnus version
305 %U  Number of unread articles in the group
306 %e  Number of unselected articles in the group
307 %Z  A string with unread/unselected article counts
308 %g  Shortish group name
309 %S  Subject of the current article
310 %u  User-defined spec
311 %s  Current score file name
312 %d  Number of dormant articles
313 %r  Number of articles that have been marked as read in this session
314 %E  Number of articles expunged by the score files")
315
316 (defvar gnus-summary-mark-below 0
317   "*Mark all articles with a score below this variable as read.
318 This variable is local to each summary buffer and usually set by the
319 score file.")
320
321 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
322   "*List of functions used for sorting articles in the summary buffer.
323 This variable is only used when not using a threaded display.")
324
325 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
326   "*List of functions used for sorting threads in the summary buffer.
327 By default, threads are sorted by article number.
328
329 Each function takes two threads and return non-nil if the first thread
330 should be sorted before the other.  If you use more than one function,
331 the primary sort function should be the last.  You should probably
332 always include `gnus-thread-sort-by-number' in the list of sorting
333 functions -- preferably first.
334
335 Ready-mady functions include `gnus-thread-sort-by-number',
336 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
337 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
338 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
339
340 (defvar gnus-thread-score-function '+
341   "*Function used for calculating the total score of a thread.
342
343 The function is called with the scores of the article and each
344 subthread and should then return the score of the thread.
345
346 Some functions you can use are `+', `max', or `min'.")
347
348 (defvar gnus-summary-expunge-below nil
349   "All articles that have a score less than this variable will be expunged.")
350
351 (defvar gnus-thread-expunge-below nil
352   "All threads that have a total score less than this variable will be expunged.
353 See `gnus-thread-score-function' for en explanation of what a
354 \"thread score\" is.")
355
356 (defvar gnus-summary-mode-hook nil
357   "*A hook for Gnus summary mode.
358 This hook is run before any variables are set in the summary buffer.")
359
360 (defvar gnus-summary-menu-hook nil
361   "*Hook run after the creation of the summary mode menu.")
362
363 (defvar gnus-summary-exit-hook nil
364   "*A hook called on exit from the summary buffer.")
365
366 (defvar gnus-summary-prepare-hook nil
367   "*A hook called after the summary buffer has been generated.
368 If you want to modify the summary buffer, you can use this hook.")
369
370 (defvar gnus-summary-generate-hook nil
371   "*A hook run just before generating the summary buffer.
372 This hook is commonly used to customize threading variables and the
373 like.")
374
375 (defvar gnus-select-group-hook nil
376   "*A hook called when a newsgroup is selected.
377
378 If you'd like to simplify subjects like the
379 `gnus-summary-next-same-subject' command does, you can use the
380 following hook:
381
382  (setq gnus-select-group-hook
383       (list
384         (lambda ()
385           (mapcar (lambda (header)
386                      (mail-header-set-subject
387                       header
388                       (gnus-simplify-subject
389                        (mail-header-subject header) 're-only)))
390                   gnus-newsgroup-headers))))")
391
392 (defvar gnus-select-article-hook nil
393   "*A hook called when an article is selected.")
394
395 (defvar gnus-visual-mark-article-hook
396   (list 'gnus-highlight-selected-summary)
397   "*Hook run after selecting an article in the summary buffer.
398 It is meant to be used for highlighting the article in some way.  It
399 is not run if `gnus-visual' is nil.")
400
401 (defvar gnus-parse-headers-hook nil
402   "*A hook called before parsing the headers.")
403 (add-hook 'gnus-parse-headers-hook 'gnus-decode-rfc1522)
404
405 (defvar gnus-exit-group-hook nil
406   "*A hook called when exiting (not quitting) summary mode.")
407
408 (defvar gnus-summary-update-hook
409   (list 'gnus-summary-highlight-line)
410   "*A hook called when a summary line is changed.
411 The hook will not be called if `gnus-visual' is nil.
412
413 The default function `gnus-summary-highlight-line' will
414 highlight the line according to the `gnus-summary-highlight'
415 variable.")
416
417 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
418   "*A hook called when an article is selected for the first time.
419 The hook is intended to mark an article as read (or unread)
420 automatically when it is selected.")
421
422 (defvar gnus-group-no-more-groups-hook nil
423   "*A hook run when returning to group mode having no more (unread) groups.")
424
425 ;;; Internal variables
426
427 (defvar gnus-scores-exclude-files nil)
428
429 (defvar gnus-summary-display-table 
430   ;; Change the display table.  Odd characters have a tendency to mess
431   ;; up nicely formatted displays - we make all possible glyphs
432   ;; display only a single character.
433
434   ;; We start from the standard display table, if any.
435   (let ((table (or (copy-sequence standard-display-table)
436                    (make-display-table)))
437         ;; Nix out all the control chars...
438         (i 32))
439     (while (>= (setq i (1- i)) 0)
440       (aset table i [??]))
441     ;; ... but not newline and cr, of course. (cr is necessary for the
442     ;; selective display).
443     (aset table ?\n nil)
444     (aset table ?\r nil)
445     ;; We nix out any glyphs over 126 that are not set already.
446     (let ((i 256))
447       (while (>= (setq i (1- i)) 127)
448         ;; Only modify if the entry is nil.
449         (or (aref table i)
450             (aset table i [??]))))
451     table)
452   "Display table used in summary mode buffers.")
453
454 (defvar gnus-original-article nil)
455 (defvar gnus-article-internal-prepare-hook nil)
456 (defvar gnus-newsgroup-process-stack nil)
457
458 (defvar gnus-thread-indent-array nil)
459 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
460
461 ;; Avoid highlighting in kill files.
462 (defvar gnus-summary-inhibit-highlight nil)
463 (defvar gnus-newsgroup-selected-overlay nil)
464 (defvar gnus-inhibit-limiting nil)
465 (defvar gnus-newsgroup-adaptive-score-file nil)
466 (defvar gnus-current-score-file nil)
467 (defvar gnus-current-move-group nil)
468 (defvar gnus-current-copy-group nil)
469 (defvar gnus-current-crosspost-group nil)
470
471 (defvar gnus-newsgroup-dependencies nil)
472 (defvar gnus-newsgroup-adaptive nil)
473 (defvar gnus-summary-display-article-function nil)
474 (defvar gnus-summary-highlight-line-function nil
475   "Function called after highlighting a summary line.")
476
477 (defvar gnus-summary-line-format-alist
478   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
479     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
480     (?s gnus-tmp-subject-or-nil ?s)
481     (?n gnus-tmp-name ?s)
482     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
483         ?s)
484     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
485             gnus-tmp-from) ?s)
486     (?F gnus-tmp-from ?s)
487     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
488     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
489     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
490     (?o (gnus-date-iso8601 gnus-tmp-header) ?s)
491     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
492     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
493     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
494     (?L gnus-tmp-lines ?d)
495     (?I gnus-tmp-indentation ?s)
496     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
497     (?R gnus-tmp-replied ?c)
498     (?\[ gnus-tmp-opening-bracket ?c)
499     (?\] gnus-tmp-closing-bracket ?c)
500     (?\> (make-string gnus-tmp-level ? ) ?s)
501     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
502     (?i gnus-tmp-score ?d)
503     (?z gnus-tmp-score-char ?c)
504     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
505     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
506     (?U gnus-tmp-unread ?c)
507     (?t (gnus-summary-number-of-articles-in-thread
508          (and (boundp 'thread) (car thread)) gnus-tmp-level)
509         ?d)
510     (?e (gnus-summary-number-of-articles-in-thread
511          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
512         ?c)
513     (?u gnus-tmp-user-defined ?s)
514     (?P (gnus-pick-line-number) ?d))
515   "An alist of format specifications that can appear in summary lines,
516 and what variables they correspond with, along with the type of the
517 variable (string, integer, character, etc).")
518
519 (defvar gnus-summary-dummy-line-format-alist
520   `((?S gnus-tmp-subject ?s)
521     (?N gnus-tmp-number ?d)
522     (?u gnus-tmp-user-defined ?s)))
523
524 (defvar gnus-summary-mode-line-format-alist
525   `((?G gnus-tmp-group-name ?s)
526     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
527     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
528     (?A gnus-tmp-article-number ?d)
529     (?Z gnus-tmp-unread-and-unselected ?s)
530     (?V gnus-version ?s)
531     (?U gnus-tmp-unread-and-unticked ?d)
532     (?S gnus-tmp-subject ?s)
533     (?e gnus-tmp-unselected ?d)
534     (?u gnus-tmp-user-defined ?s)
535     (?d (length gnus-newsgroup-dormant) ?d)
536     (?t (length gnus-newsgroup-marked) ?d)
537     (?r (length gnus-newsgroup-reads) ?d)
538     (?E gnus-newsgroup-expunged-tally ?d)
539     (?s (gnus-current-score-file-nondirectory) ?s)))
540
541 (defvar gnus-last-search-regexp nil
542   "Default regexp for article search command.")
543
544 (defvar gnus-last-shell-command nil
545   "Default shell command on article.")
546
547 (defvar gnus-newsgroup-begin nil)
548 (defvar gnus-newsgroup-end nil)
549 (defvar gnus-newsgroup-last-rmail nil)
550 (defvar gnus-newsgroup-last-mail nil)
551 (defvar gnus-newsgroup-last-folder nil)
552 (defvar gnus-newsgroup-last-file nil)
553 (defvar gnus-newsgroup-auto-expire nil)
554 (defvar gnus-newsgroup-active nil)
555
556 (defvar gnus-newsgroup-data nil)
557 (defvar gnus-newsgroup-data-reverse nil)
558 (defvar gnus-newsgroup-limit nil)
559 (defvar gnus-newsgroup-limits nil)
560
561 (defvar gnus-newsgroup-unreads nil
562   "List of unread articles in the current newsgroup.")
563
564 (defvar gnus-newsgroup-unselected nil
565   "List of unselected unread articles in the current newsgroup.")
566
567 (defvar gnus-newsgroup-reads nil
568   "Alist of read articles and article marks in the current newsgroup.")
569
570 (defvar gnus-newsgroup-expunged-tally nil)
571
572 (defvar gnus-newsgroup-marked nil
573   "List of ticked articles in the current newsgroup (a subset of unread art).")
574
575 (defvar gnus-newsgroup-killed nil
576   "List of ranges of articles that have been through the scoring process.")
577
578 (defvar gnus-newsgroup-cached nil
579   "List of articles that come from the article cache.")
580
581 (defvar gnus-newsgroup-saved nil
582   "List of articles that have been saved.")
583
584 (defvar gnus-newsgroup-kill-headers nil)
585
586 (defvar gnus-newsgroup-replied nil
587   "List of articles that have been replied to in the current newsgroup.")
588
589 (defvar gnus-newsgroup-expirable nil
590   "List of articles in the current newsgroup that can be expired.")
591
592 (defvar gnus-newsgroup-processable nil
593   "List of articles in the current newsgroup that can be processed.")
594
595 (defvar gnus-newsgroup-bookmarks nil
596   "List of articles in the current newsgroup that have bookmarks.")
597
598 (defvar gnus-newsgroup-dormant nil
599   "List of dormant articles in the current newsgroup.")
600
601 (defvar gnus-newsgroup-scored nil
602   "List of scored articles in the current newsgroup.")
603
604 (defvar gnus-newsgroup-headers nil
605   "List of article headers in the current newsgroup.")
606
607 (defvar gnus-newsgroup-threads nil)
608
609 (defvar gnus-newsgroup-prepared nil
610   "Whether the current group has been prepared properly.")
611
612 (defvar gnus-newsgroup-ancient nil
613   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
614
615 (defvar gnus-newsgroup-sparse nil)
616
617 (defvar gnus-current-article nil)
618 (defvar gnus-article-current nil)
619 (defvar gnus-current-headers nil)
620 (defvar gnus-have-all-headers nil)
621 (defvar gnus-last-article nil)
622 (defvar gnus-newsgroup-history nil)
623
624 (defconst gnus-summary-local-variables
625   '(gnus-newsgroup-name
626     gnus-newsgroup-begin gnus-newsgroup-end
627     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
628     gnus-newsgroup-last-folder gnus-newsgroup-last-file
629     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
630     gnus-newsgroup-unselected gnus-newsgroup-marked
631     gnus-newsgroup-reads gnus-newsgroup-saved
632     gnus-newsgroup-replied gnus-newsgroup-expirable
633     gnus-newsgroup-processable gnus-newsgroup-killed
634     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
635     gnus-newsgroup-headers gnus-newsgroup-threads
636     gnus-newsgroup-prepared gnus-summary-highlight-line-function
637     gnus-current-article gnus-current-headers gnus-have-all-headers
638     gnus-last-article gnus-article-internal-prepare-hook
639     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
640     gnus-newsgroup-scored gnus-newsgroup-kill-headers
641     gnus-thread-expunge-below
642     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
643     (gnus-summary-mark-below . global)
644     gnus-newsgroup-active gnus-scores-exclude-files
645     gnus-newsgroup-history gnus-newsgroup-ancient
646     gnus-newsgroup-sparse gnus-newsgroup-process-stack
647     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
648     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
649     (gnus-newsgroup-expunged-tally . 0)
650     gnus-cache-removable-articles gnus-newsgroup-cached
651     gnus-newsgroup-data gnus-newsgroup-data-reverse
652     gnus-newsgroup-limit gnus-newsgroup-limits)
653   "Variables that are buffer-local to the summary buffers.")
654
655 ;; Byte-compiler warning.
656 (defvar gnus-article-mode-map)
657
658 ;; Subject simplification.
659
660 (defsubst gnus-simplify-subject-re (subject)
661   "Remove \"Re:\" from subject lines."
662   (if (string-match "^[Rr][Ee]: *" subject)
663       (substring subject (match-end 0))
664     subject))
665
666 (defun gnus-simplify-subject (subject &optional re-only)
667   "Remove `Re:' and words in parentheses.
668 If RE-ONLY is non-nil, strip leading `Re:'s only."
669   (let ((case-fold-search t))           ;Ignore case.
670     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
671     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
672       (setq subject (substring subject (match-end 0))))
673     ;; Remove uninteresting prefixes.
674     (if (and (not re-only)
675              gnus-simplify-ignored-prefixes
676              (string-match gnus-simplify-ignored-prefixes subject))
677         (setq subject (substring subject (match-end 0))))
678     ;; Remove words in parentheses from end.
679     (unless re-only
680       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
681         (setq subject (substring subject 0 (match-beginning 0)))))
682     ;; Return subject string.
683     subject))
684
685 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
686 ;; all whitespace.
687 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
688 (defun gnus-simplify-buffer-fuzzy ()
689   (let ((case-fold-search t))
690     (goto-char (point-min))
691     (while (search-forward "\t" nil t)
692       (replace-match " " t t))
693     (goto-char (point-min))
694     (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *" nil t)
695     (goto-char (match-beginning 0))
696     (while (or
697             (looking-at "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
698             (looking-at "^[[].*: .*[]]$"))
699       (goto-char (point-min))
700       (while (re-search-forward "^ *\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;] *"
701                                 nil t)
702         (replace-match "" t t))
703       (goto-char (point-min))
704       (while (re-search-forward "^[[].*: .*[]]$" nil t)
705         (goto-char (match-end 0))
706         (delete-char -1)
707         (delete-region
708          (progn (goto-char (match-beginning 0)))
709          (re-search-forward ":"))))
710     (goto-char (point-min))
711     (while (re-search-forward " *[[{(][^()\n]*[]})] *$" nil t)
712       (replace-match "" t t))
713     (goto-char (point-min))
714     (while (re-search-forward "  +" nil t)
715       (replace-match " " t t))
716     (goto-char (point-min))
717     (while (re-search-forward " $" nil t)
718       (replace-match "" t t))
719     (goto-char (point-min))
720     (while (re-search-forward "^ +" nil t)
721       (replace-match "" t t))
722     (goto-char (point-min))
723     (when gnus-simplify-subject-fuzzy-regexp
724       (if (listp gnus-simplify-subject-fuzzy-regexp)
725           (let ((list gnus-simplify-subject-fuzzy-regexp))
726             (while list
727               (goto-char (point-min))
728               (while (re-search-forward (car list) nil t)
729                 (replace-match "" t t))
730               (setq list (cdr list))))
731         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
732           (replace-match "" t t))))))
733
734 (defun gnus-simplify-subject-fuzzy (subject)
735   "Siplify a subject string fuzzily."
736   (save-excursion
737     (gnus-set-work-buffer)
738     (let ((case-fold-search t))
739       (insert subject)
740       (inline (gnus-simplify-buffer-fuzzy))
741       (buffer-string))))
742
743 (defsubst gnus-simplify-subject-fully (subject)
744   "Simplify a subject string according to the user's wishes."
745   (cond
746    ((null gnus-summary-gather-subject-limit)
747     (gnus-simplify-subject-re subject))
748    ((eq gnus-summary-gather-subject-limit 'fuzzy)
749     (gnus-simplify-subject-fuzzy subject))
750    ((numberp gnus-summary-gather-subject-limit)
751     (gnus-limit-string (gnus-simplify-subject-re subject)
752                        gnus-summary-gather-subject-limit))
753    (t
754     subject)))
755
756 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
757   "Check whether two subjects are equal.  If optional argument
758 simple-first is t, first argument is already simplified."
759   (cond
760    ((null simple-first)
761     (equal (gnus-simplify-subject-fully s1)
762            (gnus-simplify-subject-fully s2)))
763    (t
764     (equal s1
765            (gnus-simplify-subject-fully s2)))))
766
767 (defun gnus-offer-save-summaries ()
768   "Offer to save all active summary buffers."
769   (save-excursion
770     (let ((buflist (buffer-list))
771           buffers bufname)
772       ;; Go through all buffers and find all summaries.
773       (while buflist
774         (and (setq bufname (buffer-name (car buflist)))
775              (string-match "Summary" bufname)
776              (save-excursion
777                (set-buffer bufname)
778                ;; We check that this is, indeed, a summary buffer.
779                (and (eq major-mode 'gnus-summary-mode)
780                     ;; Also make sure this isn't bogus.
781                     gnus-newsgroup-prepared))
782              (push bufname buffers))
783         (setq buflist (cdr buflist)))
784       ;; Go through all these summary buffers and offer to save them.
785       (when buffers
786         (map-y-or-n-p
787          "Update summary buffer %s? "
788          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
789          buffers)))))
790
791 (defun gnus-summary-bubble-group ()
792   "Increase the score of the current group.
793 This is a handy function to add to `gnus-summary-exit-hook' to
794 increase the score of each group you read."
795   (gnus-group-add-score gnus-newsgroup-name))
796
797 \f
798 ;;;
799 ;;; Gnus summary mode
800 ;;;
801
802 (put 'gnus-summary-mode 'mode-class 'special)
803
804 (when t
805   ;; Non-orthogonal keys
806
807   (gnus-define-keys gnus-summary-mode-map
808     " " gnus-summary-next-page
809     "\177" gnus-summary-prev-page
810     [delete] gnus-summary-prev-page
811     "\r" gnus-summary-scroll-up
812     "n" gnus-summary-next-unread-article
813     "p" gnus-summary-prev-unread-article
814     "N" gnus-summary-next-article
815     "P" gnus-summary-prev-article
816     "\M-\C-n" gnus-summary-next-same-subject
817     "\M-\C-p" gnus-summary-prev-same-subject
818     "\M-n" gnus-summary-next-unread-subject
819     "\M-p" gnus-summary-prev-unread-subject
820     "." gnus-summary-first-unread-article
821     "," gnus-summary-best-unread-article
822     "\M-s" gnus-summary-search-article-forward
823     "\M-r" gnus-summary-search-article-backward
824     "<" gnus-summary-beginning-of-article
825     ">" gnus-summary-end-of-article
826     "j" gnus-summary-goto-article
827     "^" gnus-summary-refer-parent-article
828     "\M-^" gnus-summary-refer-article
829     "u" gnus-summary-tick-article-forward
830     "!" gnus-summary-tick-article-forward
831     "U" gnus-summary-tick-article-backward
832     "d" gnus-summary-mark-as-read-forward
833     "D" gnus-summary-mark-as-read-backward
834     "E" gnus-summary-mark-as-expirable
835     "\M-u" gnus-summary-clear-mark-forward
836     "\M-U" gnus-summary-clear-mark-backward
837     "k" gnus-summary-kill-same-subject-and-select
838     "\C-k" gnus-summary-kill-same-subject
839     "\M-\C-k" gnus-summary-kill-thread
840     "\M-\C-l" gnus-summary-lower-thread
841     "e" gnus-summary-edit-article
842     "#" gnus-summary-mark-as-processable
843     "\M-#" gnus-summary-unmark-as-processable
844     "\M-\C-t" gnus-summary-toggle-threads
845     "\M-\C-s" gnus-summary-show-thread
846     "\M-\C-h" gnus-summary-hide-thread
847     "\M-\C-f" gnus-summary-next-thread
848     "\M-\C-b" gnus-summary-prev-thread
849     "\M-\C-u" gnus-summary-up-thread
850     "\M-\C-d" gnus-summary-down-thread
851     "&" gnus-summary-execute-command
852     "c" gnus-summary-catchup-and-exit
853     "\C-w" gnus-summary-mark-region-as-read
854     "\C-t" gnus-summary-toggle-truncation
855     "?" gnus-summary-mark-as-dormant
856     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
857     "\C-c\C-s\C-n" gnus-summary-sort-by-number
858     "\C-c\C-s\C-a" gnus-summary-sort-by-author
859     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
860     "\C-c\C-s\C-d" gnus-summary-sort-by-date
861     "\C-c\C-s\C-i" gnus-summary-sort-by-score
862     "=" gnus-summary-expand-window
863     "\C-x\C-s" gnus-summary-reselect-current-group
864     "\M-g" gnus-summary-rescan-group
865     "w" gnus-summary-stop-page-breaking
866     "\C-c\C-r" gnus-summary-caesar-message
867     "\M-t" gnus-summary-toggle-mime
868     "f" gnus-summary-followup
869     "F" gnus-summary-followup-with-original
870     "C" gnus-summary-cancel-article
871     "r" gnus-summary-reply
872     "R" gnus-summary-reply-with-original
873     "\C-c\C-f" gnus-summary-mail-forward
874     "o" gnus-summary-save-article
875     "\C-o" gnus-summary-save-article-mail
876     "|" gnus-summary-pipe-output
877     "\M-k" gnus-summary-edit-local-kill
878     "\M-K" gnus-summary-edit-global-kill
879     "V" gnus-version
880     "\C-c\C-d" gnus-summary-describe-group
881     "q" gnus-summary-exit
882     "Q" gnus-summary-exit-no-update
883     "\C-c\C-i" gnus-info-find-node
884     gnus-mouse-2 gnus-mouse-pick-article
885     "m" gnus-summary-mail-other-window
886     "a" gnus-summary-post-news
887     "x" gnus-summary-limit-to-unread
888     "s" gnus-summary-isearch-article
889     "t" gnus-article-hide-headers
890     "g" gnus-summary-show-article
891     "l" gnus-summary-goto-last-article
892     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
893     "\C-d" gnus-summary-enter-digest-group
894     "\M-\C-d" gnus-summary-read-document
895     "\C-c\C-b" gnus-bug
896     "*" gnus-cache-enter-article
897     "\M-*" gnus-cache-remove-article
898     "\M-&" gnus-summary-universal-argument
899     "\C-l" gnus-recenter
900     "\M-\C-g" gnus-summary-prepare 
901     "I" gnus-summary-increase-score
902     "L" gnus-summary-lower-score
903
904     "V" gnus-summary-score-map
905     "X" gnus-uu-extract-map
906     "S" gnus-summary-send-map)
907
908   ;; Sort of orthogonal keymap
909   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
910     "t" gnus-summary-tick-article-forward
911     "!" gnus-summary-tick-article-forward
912     "d" gnus-summary-mark-as-read-forward
913     "r" gnus-summary-mark-as-read-forward
914     "c" gnus-summary-clear-mark-forward
915     " " gnus-summary-clear-mark-forward
916     "e" gnus-summary-mark-as-expirable
917     "x" gnus-summary-mark-as-expirable
918     "?" gnus-summary-mark-as-dormant
919     "b" gnus-summary-set-bookmark
920     "B" gnus-summary-remove-bookmark
921     "#" gnus-summary-mark-as-processable
922     "\M-#" gnus-summary-unmark-as-processable
923     "S" gnus-summary-limit-include-expunged
924     "C" gnus-summary-catchup
925     "H" gnus-summary-catchup-to-here
926     "\C-c" gnus-summary-catchup-all
927     "k" gnus-summary-kill-same-subject-and-select
928     "K" gnus-summary-kill-same-subject
929     "P" gnus-uu-mark-map)
930
931   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
932     "c" gnus-summary-clear-above
933     "u" gnus-summary-tick-above
934     "m" gnus-summary-mark-above
935     "k" gnus-summary-kill-below)
936
937   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
938     "/" gnus-summary-limit-to-subject
939     "n" gnus-summary-limit-to-articles
940     "w" gnus-summary-pop-limit
941     "s" gnus-summary-limit-to-subject
942     "a" gnus-summary-limit-to-author
943     "u" gnus-summary-limit-to-unread
944     "m" gnus-summary-limit-to-marks
945     "v" gnus-summary-limit-to-score
946     "D" gnus-summary-limit-include-dormant
947     "d" gnus-summary-limit-exclude-dormant
948     ;;  "t" gnus-summary-limit-exclude-thread
949     "E" gnus-summary-limit-include-expunged
950     "c" gnus-summary-limit-exclude-childless-dormant
951     "C" gnus-summary-limit-mark-excluded-as-read)
952
953   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
954     "n" gnus-summary-next-unread-article
955     "p" gnus-summary-prev-unread-article
956     "N" gnus-summary-next-article
957     "P" gnus-summary-prev-article
958     "\C-n" gnus-summary-next-same-subject
959     "\C-p" gnus-summary-prev-same-subject
960     "\M-n" gnus-summary-next-unread-subject
961     "\M-p" gnus-summary-prev-unread-subject
962     "f" gnus-summary-first-unread-article
963     "b" gnus-summary-best-unread-article
964     "j" gnus-summary-goto-article
965     "g" gnus-summary-goto-subject
966     "l" gnus-summary-goto-last-article
967     "p" gnus-summary-pop-article)
968
969   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
970     "k" gnus-summary-kill-thread
971     "l" gnus-summary-lower-thread
972     "i" gnus-summary-raise-thread
973     "T" gnus-summary-toggle-threads
974     "t" gnus-summary-rethread-current
975     "^" gnus-summary-reparent-thread
976     "s" gnus-summary-show-thread
977     "S" gnus-summary-show-all-threads
978     "h" gnus-summary-hide-thread
979     "H" gnus-summary-hide-all-threads
980     "n" gnus-summary-next-thread
981     "p" gnus-summary-prev-thread
982     "u" gnus-summary-up-thread
983     "o" gnus-summary-top-thread
984     "d" gnus-summary-down-thread
985     "#" gnus-uu-mark-thread
986     "\M-#" gnus-uu-unmark-thread)
987
988   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
989     "c" gnus-summary-catchup-and-exit
990     "C" gnus-summary-catchup-all-and-exit
991     "E" gnus-summary-exit-no-update
992     "Q" gnus-summary-exit
993     "Z" gnus-summary-exit
994     "n" gnus-summary-catchup-and-goto-next-group
995     "R" gnus-summary-reselect-current-group
996     "G" gnus-summary-rescan-group
997     "N" gnus-summary-next-group
998     "P" gnus-summary-prev-group)
999
1000   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1001     " " gnus-summary-next-page
1002     "n" gnus-summary-next-page
1003     "\177" gnus-summary-prev-page
1004     [delete] gnus-summary-prev-page
1005     "p" gnus-summary-prev-page
1006     "\r" gnus-summary-scroll-up
1007     "<" gnus-summary-beginning-of-article
1008     ">" gnus-summary-end-of-article
1009     "b" gnus-summary-beginning-of-article
1010     "e" gnus-summary-end-of-article
1011     "^" gnus-summary-refer-parent-article
1012     "r" gnus-summary-refer-parent-article
1013     "R" gnus-summary-refer-references
1014     "g" gnus-summary-show-article
1015     "s" gnus-summary-isearch-article)
1016
1017   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1018     "b" gnus-article-add-buttons
1019     "B" gnus-article-add-buttons-to-head
1020     "o" gnus-article-treat-overstrike
1021     "e" gnus-article-emphasize
1022     "w" gnus-article-fill-cited-article
1023     "c" gnus-article-remove-cr
1024     "q" gnus-article-de-quoted-unreadable
1025     "f" gnus-article-display-x-face
1026     "l" gnus-summary-stop-page-breaking
1027     "r" gnus-summary-caesar-message
1028     "t" gnus-article-hide-headers
1029     "v" gnus-summary-verbose-headers
1030     "m" gnus-summary-toggle-mime)
1031
1032   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1033     "a" gnus-article-hide
1034     "h" gnus-article-hide-headers
1035     "b" gnus-article-hide-boring-headers
1036     "s" gnus-article-hide-signature
1037     "c" gnus-article-hide-citation
1038     "p" gnus-article-hide-pgp
1039     "P" gnus-article-hide-pem
1040     "\C-c" gnus-article-hide-citation-maybe)
1041
1042   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1043     "a" gnus-article-highlight
1044     "h" gnus-article-highlight-headers
1045     "c" gnus-article-highlight-citation
1046     "s" gnus-article-highlight-signature)
1047
1048   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1049     "z" gnus-article-date-ut
1050     "u" gnus-article-date-ut
1051     "l" gnus-article-date-local
1052     "e" gnus-article-date-lapsed
1053     "o" gnus-article-date-original)
1054
1055   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1056     "t" gnus-article-remove-trailing-blank-lines
1057     "l" gnus-article-strip-leading-blank-lines
1058     "m" gnus-article-strip-multiple-blank-lines
1059     "a" gnus-article-strip-blank-lines)
1060
1061   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1062     "v" gnus-version
1063     "f" gnus-summary-fetch-faq
1064     "d" gnus-summary-describe-group
1065     "h" gnus-summary-describe-briefly
1066     "i" gnus-info-find-node)
1067
1068   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1069     "e" gnus-summary-expire-articles
1070     "\M-\C-e" gnus-summary-expire-articles-now
1071     "\177" gnus-summary-delete-article
1072     [delete] gnus-summary-delete-article
1073     "m" gnus-summary-move-article
1074     "r" gnus-summary-respool-article
1075     "w" gnus-summary-edit-article
1076     "c" gnus-summary-copy-article
1077     "B" gnus-summary-crosspost-article
1078     "q" gnus-summary-respool-query
1079     "i" gnus-summary-import-article
1080     "p" gnus-summary-article-posted-p)
1081
1082   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1083     "o" gnus-summary-save-article
1084     "m" gnus-summary-save-article-mail
1085     "r" gnus-summary-save-article-rmail
1086     "f" gnus-summary-save-article-file
1087     "b" gnus-summary-save-article-body-file
1088     "h" gnus-summary-save-article-folder
1089     "v" gnus-summary-save-article-vm
1090     "p" gnus-summary-pipe-output
1091     "s" gnus-soup-add-article))
1092
1093 (defun gnus-summary-make-menu-bar ()
1094   (gnus-turn-off-edit-menu 'summary)
1095
1096   (unless (boundp 'gnus-summary-misc-menu)
1097
1098     (easy-menu-define
1099      gnus-summary-kill-menu gnus-summary-mode-map ""
1100      (cons
1101       "Score"
1102       (nconc
1103        (list
1104         ["Enter score..." gnus-summary-score-entry t])
1105        (gnus-make-score-map 'increase)
1106        (gnus-make-score-map 'lower)
1107        '(("Mark"
1108           ["Kill below" gnus-summary-kill-below t]
1109           ["Mark above" gnus-summary-mark-above t]
1110           ["Tick above" gnus-summary-tick-above t]
1111           ["Clear above" gnus-summary-clear-above t])
1112          ["Current score" gnus-summary-current-score t]
1113          ["Set score" gnus-summary-set-score t]
1114          ["Customize score file" gnus-score-customize t]
1115          ["Switch current score file..." gnus-score-change-score-file t]
1116          ["Set mark below..." gnus-score-set-mark-below t]
1117          ["Set expunge below..." gnus-score-set-expunge-below t]
1118          ["Edit current score file" gnus-score-edit-current-scores t]
1119          ["Edit score file" gnus-score-edit-file t]
1120          ["Trace score" gnus-score-find-trace t]
1121          ["Find words" gnus-score-find-favuorite-words t]
1122          ["Rescore buffer" gnus-summary-rescore t]
1123          ["Increase score..." gnus-summary-increase-score t]
1124          ["Lower score..." gnus-summary-lower-score t]))))
1125
1126     '(("Default header"
1127        ["Ask" (gnus-score-set-default 'gnus-score-default-header nil)
1128         :style radio 
1129         :selected (null gnus-score-default-header)]
1130        ["From" (gnus-score-set-default 'gnus-score-default-header 'a)
1131         :style radio 
1132         :selected (eq gnus-score-default-header 'a)]
1133        ["Subject" (gnus-score-set-default 'gnus-score-default-header 's)
1134         :style radio 
1135         :selected (eq gnus-score-default-header 's)]
1136        ["Article body"
1137         (gnus-score-set-default 'gnus-score-default-header 'b)
1138         :style radio 
1139         :selected (eq gnus-score-default-header 'b )]
1140        ["All headers"
1141         (gnus-score-set-default 'gnus-score-default-header 'h)
1142         :style radio 
1143         :selected (eq gnus-score-default-header 'h )]
1144        ["Message-Id" (gnus-score-set-default 'gnus-score-default-header 'i)
1145         :style radio 
1146         :selected (eq gnus-score-default-header 'i )]
1147        ["Thread" (gnus-score-set-default 'gnus-score-default-header 't)
1148         :style radio 
1149         :selected (eq gnus-score-default-header 't )]
1150        ["Crossposting"
1151         (gnus-score-set-default 'gnus-score-default-header 'x)
1152         :style radio 
1153         :selected (eq gnus-score-default-header 'x )]
1154        ["Lines" (gnus-score-set-default 'gnus-score-default-header 'l)
1155         :style radio 
1156         :selected (eq gnus-score-default-header 'l )]
1157        ["Date" (gnus-score-set-default 'gnus-score-default-header 'd)
1158         :style radio 
1159         :selected (eq gnus-score-default-header 'd )]
1160        ["Followups to author"
1161         (gnus-score-set-default 'gnus-score-default-header 'f)
1162         :style radio 
1163         :selected (eq gnus-score-default-header 'f )])
1164       ("Default type"
1165        ["Ask" (gnus-score-set-default 'gnus-score-default-type nil)
1166         :style radio 
1167         :selected (null gnus-score-default-type)]
1168        ;; The `:active' key is commented out in the following,
1169        ;; because the GNU Emacs hack to support radio buttons use
1170        ;; active to indicate which button is selected.  
1171        ["Substring" (gnus-score-set-default 'gnus-score-default-type 's)
1172         :style radio 
1173         ;; :active (not (memq gnus-score-default-header '(l d)))
1174         :selected (eq gnus-score-default-type 's)]
1175        ["Regexp" (gnus-score-set-default 'gnus-score-default-type 'r)
1176         :style radio
1177         ;; :active (not (memq gnus-score-default-header '(l d)))
1178         :selected (eq gnus-score-default-type 'r)]
1179        ["Exact" (gnus-score-set-default 'gnus-score-default-type 'e)
1180         :style radio
1181         ;; :active (not (memq gnus-score-default-header '(l d)))
1182         :selected (eq gnus-score-default-type 'e)]
1183        ["Fuzzy" (gnus-score-set-default 'gnus-score-default-type 'f)
1184         :style radio 
1185         ;; :active (not (memq gnus-score-default-header '(l d)))
1186         :selected (eq gnus-score-default-type 'f)]
1187        ["Before date" (gnus-score-set-default 'gnus-score-default-type 'b)
1188         :style radio 
1189         ;; :active (eq (gnus-score-default-header 'd))
1190         :selected (eq gnus-score-default-type 'b)]
1191        ["At date" (gnus-score-set-default 'gnus-score-default-type 'n)
1192         :style radio 
1193         ;; :active (eq (gnus-score-default-header 'd))
1194         :selected (eq gnus-score-default-type 'n)]
1195        ["After date" (gnus-score-set-default 'gnus-score-default-type 'a)
1196         :style radio 
1197         ;; :active (eq (gnus-score-default-header 'd))
1198         :selected (eq gnus-score-default-type 'a)]
1199        ["Less than number"
1200         (gnus-score-set-default 'gnus-score-default-type '<)
1201         :style radio 
1202         ;; :active (eq (gnus-score-default-header 'l))
1203         :selected (eq gnus-score-default-type '<)]
1204        ["Equal to number"
1205         (gnus-score-set-default 'gnus-score-default-type '=)
1206         :style radio 
1207         ;; :active (eq (gnus-score-default-header 'l))
1208         :selected (eq gnus-score-default-type '=)]
1209        ["Greater than number" 
1210         (gnus-score-set-default 'gnus-score-default-type '>)
1211         :style radio 
1212         ;; :active (eq (gnus-score-default-header 'l))
1213         :selected (eq gnus-score-default-type '>)])
1214       ["Default fold" gnus-score-default-fold-toggle
1215        :style toggle
1216        :selected gnus-score-default-fold]
1217       ("Default duration"
1218        ["Ask" (gnus-score-set-default 'gnus-score-default-duration nil)
1219         :style radio
1220         :selected (null gnus-score-default-duration)]
1221        ["Permanent"
1222         (gnus-score-set-default 'gnus-score-default-duration 'p)
1223         :style radio
1224         :selected (eq gnus-score-default-duration 'p)]
1225        ["Temporary"
1226         (gnus-score-set-default 'gnus-score-default-duration 't)
1227         :style radio
1228         :selected (eq gnus-score-default-duration 't)]
1229        ["Immediate" 
1230         (gnus-score-set-default 'gnus-score-default-duration 'i)
1231         :style radio
1232         :selected (eq gnus-score-default-duration 'i)]))
1233
1234     (easy-menu-define
1235      gnus-summary-article-menu gnus-summary-mode-map ""
1236      '("Article"
1237        ("Hide"
1238         ["All" gnus-article-hide t]
1239         ["Headers" gnus-article-hide-headers t]
1240         ["Signature" gnus-article-hide-signature t]
1241         ["Citation" gnus-article-hide-citation t]
1242         ["PGP" gnus-article-hide-pgp t]
1243         ["Boring headers" gnus-article-hide-boring-headers t])
1244        ("Highlight"
1245         ["All" gnus-article-highlight t]
1246         ["Headers" gnus-article-highlight-headers t]
1247         ["Signature" gnus-article-highlight-signature t]
1248         ["Citation" gnus-article-highlight-citation t])
1249        ("Date"
1250         ["Local" gnus-article-date-local t]
1251         ["UT" gnus-article-date-ut t]
1252         ["Original" gnus-article-date-original t]
1253         ["Lapsed" gnus-article-date-lapsed t])
1254        ("Filter"
1255         ("Remove Blanks"
1256          ["Leading" gnus-article-strip-leading-blank-lines t]
1257          ["Multiple" gnus-article-strip-multiple-blank-lines t]
1258          ["Trailing" gnus-article-remove-trailing-blank-lines t]
1259          ["All of the above" gnus-article-strip-blank-lines t])
1260         ["Overstrike" gnus-article-treat-overstrike t]
1261         ["Emphasis" gnus-article-emphasize t]
1262         ["Word wrap" gnus-article-fill-cited-article t]
1263         ["CR" gnus-article-remove-cr t]
1264         ["Show X-Face" gnus-article-display-x-face t]
1265         ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1266         ["Rot 13" gnus-summary-caesar-message t]
1267         ["Add buttons" gnus-article-add-buttons t]
1268         ["Add buttons to head" gnus-article-add-buttons-to-head t]
1269         ["Stop page breaking" gnus-summary-stop-page-breaking t]
1270         ["Toggle MIME" gnus-summary-toggle-mime t]
1271         ["Verbose header" gnus-summary-verbose-headers t]
1272         ["Toggle header" gnus-summary-toggle-header t])
1273        ("Output"
1274         ["Save in default format" gnus-summary-save-article t]
1275         ["Save in file" gnus-summary-save-article-file t]
1276         ["Save in Unix mail format" gnus-summary-save-article-mail t]
1277         ["Save in MH folder" gnus-summary-save-article-folder t]
1278         ["Save in VM folder" gnus-summary-save-article-vm t]
1279         ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1280         ["Save body in file" gnus-summary-save-article-body-file t]
1281         ["Pipe through a filter" gnus-summary-pipe-output t]
1282         ["Add to SOUP packet" gnus-soup-add-article t])
1283        ("Backend"
1284         ["Respool article..." gnus-summary-respool-article t]
1285         ["Move article..." gnus-summary-move-article
1286          (gnus-check-backend-function
1287           'request-move-article gnus-newsgroup-name)]
1288         ["Copy article..." gnus-summary-copy-article t]
1289         ["Crosspost article..." gnus-summary-crosspost-article
1290          (gnus-check-backend-function
1291           'request-replace-article gnus-newsgroup-name)]
1292         ["Import file..." gnus-summary-import-article t]
1293         ["Chek if posted" gnus-summary-article-posted-p t]
1294         ["Edit article" gnus-summary-edit-article
1295          (not (gnus-group-read-only-p))]
1296         ["Delete article" gnus-summary-delete-article
1297          (gnus-check-backend-function
1298           'request-expire-articles gnus-newsgroup-name)]
1299         ["Query respool" gnus-summary-respool-query t]
1300         ["Delete expirable articles" gnus-summary-expire-articles-now
1301          (gnus-check-backend-function
1302           'request-expire-articles gnus-newsgroup-name)])
1303        ("Extract"
1304         ["Uudecode" gnus-uu-decode-uu t]
1305         ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1306         ["Unshar" gnus-uu-decode-unshar t]
1307         ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1308         ["Save" gnus-uu-decode-save t]
1309         ["Binhex" gnus-uu-decode-binhex t]
1310         ["Postscript" gnus-uu-decode-postscript t])
1311        ["Enter digest buffer" gnus-summary-enter-digest-group t]
1312        ["Isearch article..." gnus-summary-isearch-article t]
1313        ["Search articles forward..." gnus-summary-search-article-forward t]
1314        ["Search articles backward..." gnus-summary-search-article-backward t]
1315        ["Beginning of the article" gnus-summary-beginning-of-article t]
1316        ["End of the article" gnus-summary-end-of-article t]
1317        ["Fetch parent of article" gnus-summary-refer-parent-article t]
1318        ["Fetch referenced articles" gnus-summary-refer-references t]
1319        ["Fetch article with id..." gnus-summary-refer-article t]
1320        ["Redisplay" gnus-summary-show-article t]))
1321
1322     (easy-menu-define
1323      gnus-summary-thread-menu gnus-summary-mode-map ""
1324      '("Threads"
1325        ["Toggle threading" gnus-summary-toggle-threads t]
1326        ["Hide threads" gnus-summary-hide-all-threads t]
1327        ["Show threads" gnus-summary-show-all-threads t]
1328        ["Hide thread" gnus-summary-hide-thread t]
1329        ["Show thread" gnus-summary-show-thread t]
1330        ["Go to next thread" gnus-summary-next-thread t]
1331        ["Go to previous thread" gnus-summary-prev-thread t]
1332        ["Go down thread" gnus-summary-down-thread t]
1333        ["Go up thread" gnus-summary-up-thread t]
1334        ["Top of thread" gnus-summary-top-thread t]
1335        ["Mark thread as read" gnus-summary-kill-thread t]
1336        ["Lower thread score" gnus-summary-lower-thread t]
1337        ["Raise thread score" gnus-summary-raise-thread t]
1338        ["Rethread current" gnus-summary-rethread-current t]
1339        ))
1340
1341     (easy-menu-define
1342      gnus-summary-post-menu gnus-summary-mode-map ""
1343      '("Post"
1344        ["Post an article" gnus-summary-post-news t]
1345        ["Followup" gnus-summary-followup t]
1346        ["Followup and yank" gnus-summary-followup-with-original t]
1347        ["Supersede article" gnus-summary-supersede-article t]
1348        ["Cancel article" gnus-summary-cancel-article t]
1349        ["Reply" gnus-summary-reply t]
1350        ["Reply and yank" gnus-summary-reply-with-original t]
1351        ["Mail forward" gnus-summary-mail-forward t]
1352        ["Post forward" gnus-summary-post-forward t]
1353        ["Digest and mail" gnus-uu-digest-mail-forward t]
1354        ["Digest and post" gnus-uu-digest-post-forward t]
1355        ["Resend message" gnus-summary-resend-message t]
1356        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1357        ["Send a mail" gnus-summary-mail-other-window t]
1358        ["Uuencode and post" gnus-uu-post-news t]
1359        ;;("Draft"
1360        ;;["Send" gnus-summary-send-draft t]
1361        ;;["Send bounced" gnus-resend-bounced-mail t])
1362        ))
1363
1364     (easy-menu-define
1365      gnus-summary-misc-menu gnus-summary-mode-map ""
1366      '("Misc"
1367        ("Mark"
1368         ("Read"
1369          ["Mark as read" gnus-summary-mark-as-read-forward t]
1370          ["Mark same subject and select"
1371           gnus-summary-kill-same-subject-and-select t]
1372          ["Mark same subject" gnus-summary-kill-same-subject t]
1373          ["Catchup" gnus-summary-catchup t]
1374          ["Catchup all" gnus-summary-catchup-all t]
1375          ["Catchup to here" gnus-summary-catchup-to-here t]
1376          ["Catchup region" gnus-summary-mark-region-as-read t]
1377          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1378         ("Various"
1379          ["Tick" gnus-summary-tick-article-forward t]
1380          ["Mark as dormant" gnus-summary-mark-as-dormant t]
1381          ["Remove marks" gnus-summary-clear-mark-forward t]
1382          ["Set expirable mark" gnus-summary-mark-as-expirable t]
1383          ["Set bookmark" gnus-summary-set-bookmark t]
1384          ["Remove bookmark" gnus-summary-remove-bookmark t])
1385         ("Limit"
1386          ["Marks..." gnus-summary-limit-to-marks t]
1387          ["Subject..." gnus-summary-limit-to-subject t]
1388          ["Author..." gnus-summary-limit-to-author t]
1389          ["Score" gnus-summary-limit-to-score t]
1390          ["Unread" gnus-summary-limit-to-unread t]
1391          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1392          ["Articles" gnus-summary-limit-to-articles t]
1393          ["Pop limit" gnus-summary-pop-limit t]
1394          ["Show dormant" gnus-summary-limit-include-dormant t]
1395          ["Hide childless dormant" 
1396           gnus-summary-limit-exclude-childless-dormant t]
1397          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1398          ["Show expunged" gnus-summary-show-all-expunged t])
1399         ("Process mark"
1400          ["Set mark" gnus-summary-mark-as-processable t]
1401          ["Remove mark" gnus-summary-unmark-as-processable t]
1402          ["Remove all marks" gnus-summary-unmark-all-processable t]
1403          ["Mark above" gnus-uu-mark-over t]
1404          ["Mark series" gnus-uu-mark-series t]
1405          ["Mark region" gnus-uu-mark-region t]
1406          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1407          ["Mark all" gnus-uu-mark-all t]
1408          ["Mark buffer" gnus-uu-mark-buffer t]
1409          ["Mark sparse" gnus-uu-mark-sparse t]
1410          ["Mark thread" gnus-uu-mark-thread t]
1411          ["Unmark thread" gnus-uu-unmark-thread t]
1412          ("Process Mark Sets"
1413           ["Kill" gnus-summary-kill-process-mark t]
1414           ["Yank" gnus-summary-yank-process-mark
1415            gnus-newsgroup-process-stack]
1416           ["Save" gnus-summary-save-process-mark t])))
1417        ("Scroll article"
1418         ["Page forward" gnus-summary-next-page t]
1419         ["Page backward" gnus-summary-prev-page t]
1420         ["Line forward" gnus-summary-scroll-up t])
1421        ("Move"
1422         ["Next unread article" gnus-summary-next-unread-article t]
1423         ["Previous unread article" gnus-summary-prev-unread-article t]
1424         ["Next article" gnus-summary-next-article t]
1425         ["Previous article" gnus-summary-prev-article t]
1426         ["Next unread subject" gnus-summary-next-unread-subject t]
1427         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1428         ["Next article same subject" gnus-summary-next-same-subject t]
1429         ["Previous article same subject" gnus-summary-prev-same-subject t]
1430         ["First unread article" gnus-summary-first-unread-article t]
1431         ["Best unread article" gnus-summary-best-unread-article t]
1432         ["Go to subject number..." gnus-summary-goto-subject t]
1433         ["Go to article number..." gnus-summary-goto-article t]
1434         ["Go to the last article" gnus-summary-goto-last-article t]
1435         ["Pop article off history" gnus-summary-pop-article t]) 
1436        ("Sort"
1437         ["Sort by number" gnus-summary-sort-by-number t]
1438         ["Sort by author" gnus-summary-sort-by-author t]
1439         ["Sort by subject" gnus-summary-sort-by-subject t]
1440         ["Sort by date" gnus-summary-sort-by-date t]
1441         ["Sort by score" gnus-summary-sort-by-score t])
1442        ("Help"
1443         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1444         ["Describe group" gnus-summary-describe-group t]
1445         ["Read manual" gnus-info-find-node t])
1446        ("Cache"
1447         ["Enter article" gnus-cache-enter-article t]
1448         ["Remove article" gnus-cache-remove-article t])
1449        ("Modes"
1450         ["Pick and read" gnus-pick-mode t]
1451         ["Binary" gnus-binary-mode t])
1452        ["Filter articles..." gnus-summary-execute-command t]
1453        ["Run command on subjects..." gnus-summary-universal-argument t]
1454        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1455        ["Expand window" gnus-summary-expand-window t]
1456        ["Expire expirable articles" gnus-summary-expire-articles
1457         (gnus-check-backend-function
1458          'request-expire-articles gnus-newsgroup-name)]
1459        ["Regenerate buffer" gnus-summary-prepare t]
1460        ["Edit local kill file" gnus-summary-edit-local-kill t]
1461        ["Edit main kill file" gnus-summary-edit-global-kill t]
1462        ("Exit"
1463         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1464         ["Catchup all and exit" gnus-summary-catchup-and-exit t]
1465         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1466         ["Exit group" gnus-summary-exit t]
1467         ["Exit group without updating" gnus-summary-exit-no-update t]
1468         ["Exit and goto next group" gnus-summary-next-group t]
1469         ["Exit and goto prev group" gnus-summary-prev-group t]
1470         ["Reselect group" gnus-summary-reselect-current-group t]
1471         ["Rescan group" gnus-summary-rescan-group t])))
1472
1473     (run-hooks 'gnus-summary-menu-hook)))
1474
1475 (defun gnus-score-set-default (var value)
1476   "A version of set that updates the GNU Emacs menu-bar."
1477   (set var value)
1478   ;; It is the message that forces the active status to be updated.
1479   (message ""))
1480
1481 (defun gnus-make-score-map (type)
1482   "Make a summary score map of type TYPE."
1483   (if t
1484       nil
1485     (let ((headers '(("author" "from" string)
1486                      ("subject" "subject" string)
1487                      ("article body" "body" string)
1488                      ("article head" "head" string)
1489                      ("xref" "xref" string)
1490                      ("lines" "lines" number)
1491                      ("followups to author" "followup" string)))
1492           (types '((number ("less than" <)
1493                            ("greater than" >)
1494                            ("equal" =))
1495                    (string ("substring" s)
1496                            ("exact string" e)
1497                            ("fuzzy string" f)
1498                            ("regexp" r))))
1499           (perms '(("temporary" (current-time-string))
1500                    ("permanent" nil)
1501                    ("immediate" now)))
1502           header)
1503       (list 
1504        (apply 
1505         'nconc
1506         (list
1507          (if (eq type 'lower)
1508              "Lower score"
1509            "Increase score"))
1510         (let (outh)
1511           (while headers
1512             (setq header (car headers))
1513             (setq outh 
1514                   (cons 
1515                    (apply 
1516                     'nconc
1517                     (list (car header))
1518                     (let ((ts (cdr (assoc (nth 2 header) types)))
1519                           outt)
1520                       (while ts
1521                         (setq outt
1522                               (cons 
1523                                (apply 
1524                                 'nconc
1525                                 (list (caar ts))
1526                                 (let ((ps perms)
1527                                       outp)
1528                                   (while ps
1529                                     (setq outp
1530                                           (cons
1531                                            (vector
1532                                             (caar ps) 
1533                                             (list
1534                                              'gnus-summary-score-entry
1535                                              (nth 1 header)
1536                                              (if (or (string= (nth 1 header) 
1537                                                               "head")
1538                                                      (string= (nth 1 header)
1539                                                               "body"))
1540                                                  ""
1541                                                (list 'gnus-summary-header 
1542                                                      (nth 1 header)))
1543                                              (list 'quote (nth 1 (car ts)))
1544                                              (list 'gnus-score-default nil)
1545                                              (nth 1 (car ps))
1546                                              t)
1547                                             t)
1548                                            outp))
1549                                     (setq ps (cdr ps)))
1550                                   (list (nreverse outp))))
1551                                outt))
1552                         (setq ts (cdr ts)))
1553                       (list (nreverse outt))))
1554                    outh))
1555             (setq headers (cdr headers)))
1556           (list (nreverse outh))))))))
1557
1558 \f
1559
1560 (defun gnus-summary-mode (&optional group)
1561   "Major mode for reading articles.
1562
1563 All normal editing commands are switched off.
1564 \\<gnus-summary-mode-map>
1565 Each line in this buffer represents one article.  To read an
1566 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1567 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1568 respectively.
1569
1570 You can also post articles and send mail from this buffer.  To
1571 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1572 of an article, type `\\[gnus-summary-reply]'.
1573
1574 There are approx. one gazillion commands you can execute in this
1575 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1576
1577 The following commands are available:
1578
1579 \\{gnus-summary-mode-map}"
1580   (interactive)
1581   (when (and menu-bar-mode
1582              (gnus-visual-p 'summary-menu 'menu))
1583     (gnus-summary-make-menu-bar))
1584   (kill-all-local-variables)
1585   (gnus-summary-make-local-variables)
1586   (gnus-make-thread-indent-array)
1587   (gnus-simplify-mode-line)
1588   (setq major-mode 'gnus-summary-mode)
1589   (setq mode-name "Summary")
1590   (make-local-variable 'minor-mode-alist)
1591   (use-local-map gnus-summary-mode-map)
1592   (buffer-disable-undo (current-buffer))
1593   (setq buffer-read-only t)             ;Disable modification
1594   (setq truncate-lines t)
1595   (setq selective-display t)
1596   (setq selective-display-ellipses t)   ;Display `...'
1597   (setq buffer-display-table gnus-summary-display-table)
1598   (gnus-set-default-directory)
1599   (setq gnus-newsgroup-name group)
1600   (make-local-variable 'gnus-summary-line-format)
1601   (make-local-variable 'gnus-summary-line-format-spec)
1602   (make-local-variable 'gnus-summary-mark-positions)
1603   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1604   (gnus-update-summary-mark-positions)
1605   (gnus-make-local-hook 'post-command-hook)
1606   (gnus-add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1607   (run-hooks 'gnus-summary-mode-hook))
1608
1609 (defun gnus-summary-make-local-variables ()
1610   "Make all the local summary buffer variables."
1611   (let ((locals gnus-summary-local-variables)
1612         global local)
1613     (while (setq local (pop locals))
1614       (if (consp local)
1615           (progn
1616             (if (eq (cdr local) 'global)
1617                 ;; Copy the global value of the variable.
1618                 (setq global (symbol-value (car local)))
1619               ;; Use the value from the list.
1620               (setq global (eval (cdr local))))
1621             (make-local-variable (car local))
1622             (set (car local) global))
1623         ;; Simple nil-valued local variable.
1624         (make-local-variable local)
1625         (set local nil)))))
1626
1627 (defun gnus-summary-clear-local-variables ()
1628   (let ((locals gnus-summary-local-variables))
1629     (while locals
1630       (if (consp (car locals))
1631           (and (vectorp (caar locals))
1632                (set (caar locals) nil))
1633         (and (vectorp (car locals))
1634              (set (car locals) nil)))
1635       (setq locals (cdr locals)))))
1636
1637 ;; Summary data functions.
1638
1639 (defmacro gnus-data-number (data)
1640   `(car ,data))
1641
1642 (defmacro gnus-data-set-number (data number)
1643   `(setcar ,data ,number))
1644
1645 (defmacro gnus-data-mark (data)
1646   `(nth 1 ,data))
1647
1648 (defmacro gnus-data-set-mark (data mark)
1649   `(setcar (nthcdr 1 ,data) ,mark))
1650
1651 (defmacro gnus-data-pos (data)
1652   `(nth 2 ,data))
1653
1654 (defmacro gnus-data-set-pos (data pos)
1655   `(setcar (nthcdr 2 ,data) ,pos))
1656
1657 (defmacro gnus-data-header (data)
1658   `(nth 3 ,data))
1659
1660 (defmacro gnus-data-level (data)
1661   `(nth 4 ,data))
1662
1663 (defmacro gnus-data-unread-p (data)
1664   `(= (nth 1 ,data) gnus-unread-mark))
1665
1666 (defmacro gnus-data-read-p (data)
1667   `(/= (nth 1 ,data) gnus-unread-mark))
1668
1669 (defmacro gnus-data-pseudo-p (data)
1670   `(consp (nth 3 ,data)))
1671
1672 (defmacro gnus-data-find (number)
1673   `(assq ,number gnus-newsgroup-data))
1674
1675 (defmacro gnus-data-find-list (number &optional data)
1676   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1677      (memq (assq ,number bdata)
1678            bdata)))
1679
1680 (defmacro gnus-data-make (number mark pos header level)
1681   `(list ,number ,mark ,pos ,header ,level))
1682
1683 (defun gnus-data-enter (after-article number mark pos header level offset)
1684   (let ((data (gnus-data-find-list after-article)))
1685     (or data (error "No such article: %d" after-article))
1686     (setcdr data (cons (gnus-data-make number mark pos header level)
1687                        (cdr data)))
1688     (setq gnus-newsgroup-data-reverse nil)
1689     (gnus-data-update-list (cddr data) offset)))
1690
1691 (defun gnus-data-enter-list (after-article list &optional offset)
1692   (when list
1693     (let ((data (and after-article (gnus-data-find-list after-article)))
1694           (ilist list))
1695       (or data (not after-article) (error "No such article: %d" after-article))
1696       ;; Find the last element in the list to be spliced into the main
1697       ;; list.
1698       (while (cdr list)
1699         (setq list (cdr list)))
1700       (if (not data)
1701           (progn
1702             (setcdr list gnus-newsgroup-data)
1703             (setq gnus-newsgroup-data ilist)
1704             (and offset (gnus-data-update-list (cdr list) offset)))
1705         (setcdr list (cdr data))
1706         (setcdr data ilist)
1707         (and offset (gnus-data-update-list (cdr data) offset)))
1708       (setq gnus-newsgroup-data-reverse nil))))
1709
1710 (defun gnus-data-remove (article &optional offset)
1711   (let ((data gnus-newsgroup-data))
1712     (if (= (gnus-data-number (car data)) article)
1713         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
1714               gnus-newsgroup-data-reverse nil)
1715       (while (cdr data)
1716         (and (= (gnus-data-number (cadr data)) article)
1717              (progn
1718                (setcdr data (cddr data))
1719                (and offset (gnus-data-update-list (cdr data) offset))
1720                (setq data nil
1721                      gnus-newsgroup-data-reverse nil)))
1722         (setq data (cdr data))))))
1723
1724 (defmacro gnus-data-list (backward)
1725   `(if ,backward
1726        (or gnus-newsgroup-data-reverse
1727            (setq gnus-newsgroup-data-reverse
1728                  (reverse gnus-newsgroup-data)))
1729      gnus-newsgroup-data))
1730
1731 (defun gnus-data-update-list (data offset)
1732   "Add OFFSET to the POS of all data entries in DATA."
1733   (while data
1734     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
1735     (setq data (cdr data))))
1736
1737 (defun gnus-data-compute-positions ()
1738   "Compute the positions of all articles."
1739   (let ((data gnus-newsgroup-data)
1740         pos)
1741     (while data
1742       (when (setq pos (text-property-any
1743                        (point-min) (point-max)
1744                        'gnus-number (gnus-data-number (car data))))
1745         (gnus-data-set-pos (car data) (+ pos 3)))
1746       (setq data (cdr data)))))
1747
1748 (defun gnus-summary-article-pseudo-p (article)
1749   "Say whether this article is a pseudo article or not."
1750   (not (vectorp (gnus-data-header (gnus-data-find article)))))
1751
1752 (defun gnus-article-parent-p (number)
1753   "Say whether this article is a parent or not."
1754   (let ((data (gnus-data-find-list number)))
1755     (and (cdr data)                     ; There has to be an article after...
1756          (< (gnus-data-level (car data)) ; And it has to have a higher level.
1757             (gnus-data-level (nth 1 data))))))
1758
1759 (defun gnus-article-children (number)
1760   "Return a list of all children to NUMBER."
1761   (let* ((data (gnus-data-find-list number))
1762          (level (gnus-data-level (car data)))
1763          children)
1764     (setq data (cdr data))
1765     (while (and data            
1766                 (= (gnus-data-level (car data)) (1+ level)))
1767       (push (gnus-data-number (car data)) children)
1768       (setq data (cdr data)))
1769     children))
1770
1771 (defmacro gnus-summary-skip-intangible ()
1772   "If the current article is intangible, then jump to a different article."
1773   '(let ((to (get-text-property (point) 'gnus-intangible)))
1774     (and to (gnus-summary-goto-subject to))))
1775
1776 (defmacro gnus-summary-article-intangible-p ()
1777   "Say whether this article is intangible or not."
1778   '(get-text-property (point) 'gnus-intangible))
1779
1780 ;; Some summary mode macros.
1781
1782 (defmacro gnus-summary-article-number ()
1783   "The article number of the article on the current line.
1784 If there isn's an article number here, then we return the current
1785 article number."
1786   '(progn
1787      (gnus-summary-skip-intangible)
1788      (or (get-text-property (point) 'gnus-number)
1789          (gnus-summary-last-subject))))
1790
1791 (defmacro gnus-summary-article-header (&optional number)
1792   `(gnus-data-header (gnus-data-find
1793                       ,(or number '(gnus-summary-article-number)))))
1794
1795 (defmacro gnus-summary-thread-level (&optional number)
1796   `(if (and (eq gnus-summary-make-false-root 'dummy)
1797             (get-text-property (point) 'gnus-intangible))
1798        0
1799      (gnus-data-level (gnus-data-find
1800                        ,(or number '(gnus-summary-article-number))))))
1801
1802 (defmacro gnus-summary-article-mark (&optional number)
1803   `(gnus-data-mark (gnus-data-find
1804                     ,(or number '(gnus-summary-article-number)))))
1805
1806 (defmacro gnus-summary-article-pos (&optional number)
1807   `(gnus-data-pos (gnus-data-find
1808                    ,(or number '(gnus-summary-article-number)))))
1809
1810 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
1811 (defmacro gnus-summary-article-subject (&optional number)
1812   "Return current subject string or nil if nothing."
1813   `(let ((headers
1814           ,(if number
1815                `(gnus-data-header (assq ,number gnus-newsgroup-data))
1816              '(gnus-data-header (assq (gnus-summary-article-number)
1817                                       gnus-newsgroup-data)))))
1818      (and headers
1819           (vectorp headers)
1820           (mail-header-subject headers))))
1821
1822 (defmacro gnus-summary-article-score (&optional number)
1823   "Return current article score."
1824   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
1825                   gnus-newsgroup-scored))
1826        gnus-summary-default-score 0))
1827
1828 (defun gnus-summary-article-children (&optional number)
1829   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
1830          (level (gnus-data-level (car data)))
1831          l children)
1832     (while (and (setq data (cdr data))
1833                 (> (setq l (gnus-data-level (car data))) level))
1834       (and (= (1+ level) l)
1835            (setq children (cons (gnus-data-number (car data))
1836                                 children))))
1837     (nreverse children)))
1838
1839 (defun gnus-summary-article-parent (&optional number)
1840   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
1841                                     (gnus-data-list t)))
1842          (level (gnus-data-level (car data))))
1843     (if (zerop level)
1844         () ; This is a root.
1845       ;; We search until we find an article with a level less than
1846       ;; this one.  That function has to be the parent.
1847       (while (and (setq data (cdr data))
1848                   (not (< (gnus-data-level (car data)) level))))
1849       (and data (gnus-data-number (car data))))))
1850
1851 (defun gnus-unread-mark-p (mark)
1852   "Say whether MARK is the unread mark."
1853   (= mark gnus-unread-mark))
1854
1855 (defun gnus-read-mark-p (mark)
1856   "Say whether MARK is one of the marks that mark as read.
1857 This is all marks except unread, ticked, dormant, and expirable."
1858   (not (or (= mark gnus-unread-mark)
1859            (= mark gnus-ticked-mark)
1860            (= mark gnus-dormant-mark)
1861            (= mark gnus-expirable-mark))))
1862
1863 (defmacro gnus-article-mark (number)
1864   `(cond
1865     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
1866     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
1867     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
1868     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
1869     (t (or (cdr (assq ,number gnus-newsgroup-reads))
1870            gnus-ancient-mark))))
1871
1872 ;; Saving hidden threads.
1873
1874 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
1875 (put 'gnus-save-hidden-threads 'lisp-indent-hook 0)
1876 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
1877
1878 (defmacro gnus-save-hidden-threads (&rest forms)
1879   "Save hidden threads, eval FORMS, and restore the hidden threads."
1880   (let ((config (make-symbol "config")))
1881     `(let ((,config (gnus-hidden-threads-configuration)))
1882        (unwind-protect
1883            (save-excursion
1884              ,@forms)
1885          (gnus-restore-hidden-threads-configuration ,config)))))
1886
1887 (defun gnus-hidden-threads-configuration ()
1888   "Return the current hidden threads configuration."
1889   (save-excursion
1890     (let (config)
1891       (goto-char (point-min))
1892       (while (search-forward "\r" nil t)
1893         (push (1- (point)) config))
1894       config)))
1895
1896 (defun gnus-restore-hidden-threads-configuration (config)
1897   "Restore hidden threads configuration from CONFIG."
1898   (let (point buffer-read-only)
1899     (while (setq point (pop config))
1900       (when (and (< point (point-max))
1901                  (goto-char point)
1902                  (= (following-char) ?\n))
1903         (subst-char-in-region point (1+ point) ?\n ?\r)))))
1904
1905 ;; Various summary mode internalish functions.
1906
1907 (defun gnus-mouse-pick-article (e)
1908   (interactive "e")
1909   (mouse-set-point e)
1910   (gnus-summary-next-page nil t))
1911
1912 (defun gnus-summary-setup-buffer (group)
1913   "Initialize summary buffer."
1914   (let ((buffer (concat "*Summary " group "*")))
1915     (if (get-buffer buffer)
1916         (progn
1917           (set-buffer buffer)
1918           (setq gnus-summary-buffer (current-buffer))
1919           (not gnus-newsgroup-prepared))
1920       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
1921       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
1922       (gnus-add-current-to-buffer-list)
1923       (gnus-summary-mode group)
1924       (when gnus-carpal
1925         (gnus-carpal-setup-buffer 'summary))
1926       (unless gnus-single-article-buffer
1927         (make-local-variable 'gnus-article-buffer)
1928         (make-local-variable 'gnus-article-current)
1929         (make-local-variable 'gnus-original-article-buffer))
1930       (setq gnus-newsgroup-name group)
1931       t)))
1932
1933 (defun gnus-set-global-variables ()
1934   ;; Set the global equivalents of the summary buffer-local variables
1935   ;; to the latest values they had.  These reflect the summary buffer
1936   ;; that was in action when the last article was fetched.
1937   (when (eq major-mode 'gnus-summary-mode)
1938     (setq gnus-summary-buffer (current-buffer))
1939     (let ((name gnus-newsgroup-name)
1940           (marked gnus-newsgroup-marked)
1941           (unread gnus-newsgroup-unreads)
1942           (headers gnus-current-headers)
1943           (data gnus-newsgroup-data)
1944           (summary gnus-summary-buffer)
1945           (article-buffer gnus-article-buffer)
1946           (original gnus-original-article-buffer)
1947           (gac gnus-article-current)
1948           (reffed gnus-reffed-article-number)
1949           (score-file gnus-current-score-file))
1950       (save-excursion
1951         (set-buffer gnus-group-buffer)
1952         (setq gnus-newsgroup-name name)
1953         (setq gnus-newsgroup-marked marked)
1954         (setq gnus-newsgroup-unreads unread)
1955         (setq gnus-current-headers headers)
1956         (setq gnus-newsgroup-data data)
1957         (setq gnus-article-current gac)
1958         (setq gnus-summary-buffer summary)
1959         (setq gnus-article-buffer article-buffer)
1960         (setq gnus-original-article-buffer original)
1961         (setq gnus-reffed-article-number reffed)
1962         (setq gnus-current-score-file score-file)
1963         ;; The article buffer also has local variables.
1964         (when (gnus-buffer-live-p gnus-article-buffer)
1965           (set-buffer gnus-article-buffer)
1966           (setq gnus-summary-buffer summary))))))
1967
1968 (defun gnus-summary-last-article-p (&optional article)
1969   "Return whether ARTICLE is the last article in the buffer."
1970   (if (not (setq article (or article (gnus-summary-article-number))))
1971       t ; All non-existant numbers are the last article. :-)
1972     (not (cdr (gnus-data-find-list article)))))
1973
1974 (defun gnus-make-thread-indent-array ()
1975   (let ((n 200))
1976     (unless (and gnus-thread-indent-array
1977                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
1978       (setq gnus-thread-indent-array (make-vector 201 "")
1979             gnus-thread-indent-array-level gnus-thread-indent-level)
1980       (while (>= n 0)
1981         (aset gnus-thread-indent-array n
1982               (make-string (* n gnus-thread-indent-level) ? ))
1983         (setq n (1- n))))))
1984
1985 (defun gnus-update-summary-mark-positions ()
1986   "Compute where the summary marks are to go."
1987   (save-excursion
1988     (when (and gnus-summary-buffer
1989                (get-buffer gnus-summary-buffer)
1990                (buffer-name (get-buffer gnus-summary-buffer)))
1991       (set-buffer gnus-summary-buffer))
1992     (let ((gnus-replied-mark 129)
1993           (gnus-score-below-mark 130)
1994           (gnus-score-over-mark 130)
1995           (thread nil)
1996           (gnus-visual nil)
1997           (spec gnus-summary-line-format-spec)
1998           pos)
1999       (save-excursion
2000         (gnus-set-work-buffer)
2001         (let ((gnus-summary-line-format-spec spec))
2002           (gnus-summary-insert-line
2003            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2004           (goto-char (point-min))
2005           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2006                                              (- (point) 2)))))
2007           (goto-char (point-min))
2008           (push (cons 'replied (and (search-forward "\201" nil t) 
2009                                     (- (point) 2)))
2010                 pos)
2011           (goto-char (point-min))
2012           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2013                 pos)))
2014       (setq gnus-summary-mark-positions pos))))
2015
2016 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2017   "Insert a dummy root in the summary buffer."
2018   (beginning-of-line)
2019   (gnus-add-text-properties
2020    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2021    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2022
2023 (defun gnus-summary-insert-line
2024   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
2025                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
2026                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
2027   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2028          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2029          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2030          (gnus-tmp-score-char
2031           (if (or (null gnus-summary-default-score)
2032                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2033                       gnus-summary-zcore-fuzz)) ? 
2034             (if (< gnus-tmp-score gnus-summary-default-score)
2035                 gnus-score-below-mark gnus-score-over-mark)))
2036          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
2037                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
2038                                   gnus-cached-mark)
2039                                  (gnus-tmp-replied gnus-replied-mark)
2040                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
2041                                   gnus-saved-mark)
2042                                  (t gnus-unread-mark)))
2043          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2044          (gnus-tmp-name
2045           (cond
2046            ((string-match "(.+)" gnus-tmp-from)
2047             (substring gnus-tmp-from
2048                        (1+ (match-beginning 0)) (1- (match-end 0))))
2049            ((string-match "<[^>]+> *$" gnus-tmp-from)
2050             (let ((beg (match-beginning 0)))
2051               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2052                        (substring gnus-tmp-from (1+ (match-beginning 0))
2053                                   (1- (match-end 0))))
2054                   (substring gnus-tmp-from 0 beg))))
2055            (t gnus-tmp-from)))
2056          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2057          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2058          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2059          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2060          (buffer-read-only nil))
2061     (when (string= gnus-tmp-name "")
2062       (setq gnus-tmp-name gnus-tmp-from))
2063     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
2064     (gnus-put-text-property
2065      (point)
2066      (progn (eval gnus-summary-line-format-spec) (point))
2067      'gnus-number gnus-tmp-number)
2068     (when (gnus-visual-p 'summary-highlight 'highlight)
2069       (forward-line -1)
2070       (run-hooks 'gnus-summary-update-hook)
2071       (forward-line 1))))
2072
2073 (defun gnus-summary-update-line (&optional dont-update)
2074   ;; Update summary line after change.
2075   (when (and gnus-summary-default-score
2076              (not gnus-summary-inhibit-highlight))
2077     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2078            (article (gnus-summary-article-number))
2079            (score (gnus-summary-article-score article)))
2080       (unless dont-update
2081         (if (and gnus-summary-mark-below
2082                  (< (gnus-summary-article-score)
2083                     gnus-summary-mark-below))
2084             ;; This article has a low score, so we mark it as read.
2085             (when (memq article gnus-newsgroup-unreads)
2086               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2087           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2088             ;; This article was previously marked as read on account
2089             ;; of a low score, but now it has risen, so we mark it as
2090             ;; unread.
2091             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2092         (gnus-summary-update-mark
2093          (if (or (null gnus-summary-default-score)
2094                  (<= (abs (- score gnus-summary-default-score))
2095                      gnus-summary-zcore-fuzz)) ? 
2096            (if (< score gnus-summary-default-score)
2097                gnus-score-below-mark gnus-score-over-mark)) 'score))
2098       ;; Do visual highlighting.
2099       (when (gnus-visual-p 'summary-highlight 'highlight)
2100         (run-hooks 'gnus-summary-update-hook)))))
2101
2102 (defvar gnus-tmp-new-adopts nil)
2103
2104 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2105   "Return the number of articles in THREAD.  
2106 This may be 0 in some cases -- if none of the articles in
2107 the thread are to be displayed."
2108   (let* ((number
2109           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2110           (cond
2111            ((not (listp thread))
2112             1)
2113            ((and (consp thread) (cdr thread))
2114             (apply
2115              '+ 1 (mapcar
2116                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2117            ((null thread)
2118             1)
2119            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2120             1)
2121            (t 0))))
2122     (when (and level (zerop level) gnus-tmp-new-adopts)
2123       (incf number
2124             (apply '+ (mapcar
2125                        'gnus-summary-number-of-articles-in-thread
2126                        gnus-tmp-new-adopts))))
2127     (if char
2128         (if (> number 1) gnus-not-empty-thread-mark
2129           gnus-empty-thread-mark)
2130       number)))
2131
2132 (defun gnus-summary-set-local-parameters (group)
2133  "Go through the local params of GROUP and set all variable specs in that list."
2134   (let ((params (gnus-group-find-parameter group))
2135         elem)
2136     (while params
2137       (setq elem (car params)
2138             params (cdr params))
2139       (and (consp elem)                 ; Has to be a cons.
2140            (consp (cdr elem))           ; The cdr has to be a list.
2141            (symbolp (car elem))         ; Has to be a symbol in there.
2142            (not (memq (car elem) 
2143                       '(quit-config to-address to-list to-group)))
2144            (progn                       ; So we set it.
2145              (make-local-variable (car elem))
2146              (set (car elem) (eval (nth 1 elem))))))))
2147
2148 (defun gnus-summary-read-group (group &optional show-all no-article
2149                                       kill-buffer no-display)
2150   "Start reading news in newsgroup GROUP.
2151 If SHOW-ALL is non-nil, already read articles are also listed.
2152 If NO-ARTICLE is non-nil, no article is selected initially.
2153 If NO-DISPLAY, don't generate a summary buffer."
2154   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2155   (let* ((new-group (gnus-summary-setup-buffer group))
2156          (quit-config (gnus-group-quit-config group))
2157          (did-select (and new-group (gnus-select-newsgroup group show-all))))
2158     (cond
2159      ;; This summary buffer exists already, so we just select it.
2160      ((not new-group)
2161       (gnus-set-global-variables)
2162       (when kill-buffer
2163         (gnus-kill-or-deaden-summary kill-buffer))
2164       (gnus-configure-windows 'summary 'force)
2165       (gnus-set-mode-line 'summary)
2166       (gnus-summary-position-point)
2167       (message "")
2168       t)
2169      ;; We couldn't select this group.
2170      ((null did-select)
2171       (when (and (eq major-mode 'gnus-summary-mode)
2172                  (not (equal (current-buffer) kill-buffer)))
2173         (kill-buffer (current-buffer))
2174         (if (not quit-config)
2175             (progn
2176               (set-buffer gnus-group-buffer)
2177               (gnus-group-jump-to-group group)
2178               (gnus-group-next-unread-group 1))
2179           (if (not (buffer-name (car quit-config)))
2180               (gnus-configure-windows 'group 'force)
2181             (set-buffer (car quit-config))
2182             (and (eq major-mode 'gnus-summary-mode)
2183                  (gnus-set-global-variables))
2184             (gnus-configure-windows (cdr quit-config)))))
2185       (gnus-message 3 "Can't select group")
2186       nil)
2187      ;; The user did a `C-g' while prompting for number of articles,
2188      ;; so we exit this group.
2189      ((eq did-select 'quit)
2190       (and (eq major-mode 'gnus-summary-mode)
2191            (not (equal (current-buffer) kill-buffer))
2192            (kill-buffer (current-buffer)))
2193       (when kill-buffer
2194         (gnus-kill-or-deaden-summary kill-buffer))
2195       (if (not quit-config)
2196           (progn
2197             (set-buffer gnus-group-buffer)
2198             (gnus-group-jump-to-group group)
2199             (gnus-group-next-unread-group 1)
2200             (gnus-configure-windows 'group 'force))
2201         (if (not (buffer-name (car quit-config)))
2202             (gnus-configure-windows 'group 'force)
2203           (set-buffer (car quit-config))
2204           (and (eq major-mode 'gnus-summary-mode)
2205                (gnus-set-global-variables))
2206           (gnus-configure-windows (cdr quit-config))))
2207       ;; Finally signal the quit.
2208       (signal 'quit nil))
2209      ;; The group was successfully selected.
2210      (t
2211       (gnus-set-global-variables)
2212       ;; Save the active value in effect when the group was entered.
2213       (setq gnus-newsgroup-active
2214             (gnus-copy-sequence
2215              (gnus-active gnus-newsgroup-name)))
2216       ;; You can change the summary buffer in some way with this hook.
2217       (run-hooks 'gnus-select-group-hook)
2218       ;; Set any local variables in the group parameters.
2219       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2220       (gnus-update-format-specifications
2221        nil 'summary 'summary-mode 'summary-dummy)
2222       ;; Do score processing.
2223       (when gnus-use-scoring
2224         (gnus-possibly-score-headers))
2225       ;; Check whether to fill in the gaps in the threads.
2226       (when gnus-build-sparse-threads
2227         (gnus-build-sparse-threads))
2228       ;; Find the initial limit.
2229       (if gnus-show-threads
2230           (if show-all
2231               (let ((gnus-newsgroup-dormant nil))
2232                 (gnus-summary-initial-limit show-all))
2233             (gnus-summary-initial-limit show-all))
2234         (setq gnus-newsgroup-limit 
2235               (mapcar 
2236                (lambda (header) (mail-header-number header))
2237                gnus-newsgroup-headers)))
2238       ;; Generate the summary buffer.
2239       (unless no-display
2240         (gnus-summary-prepare))
2241       (when gnus-use-trees
2242         (gnus-tree-open group)
2243         (setq gnus-summary-highlight-line-function
2244               'gnus-tree-highlight-article))
2245       ;; If the summary buffer is empty, but there are some low-scored
2246       ;; articles or some excluded dormants, we include these in the
2247       ;; buffer.
2248       (when (and (zerop (buffer-size))
2249                  (not no-display))
2250         (cond (gnus-newsgroup-dormant
2251                (gnus-summary-limit-include-dormant))
2252               ((and gnus-newsgroup-scored show-all)
2253                (gnus-summary-limit-include-expunged t))))
2254       ;; Function `gnus-apply-kill-file' must be called in this hook.
2255       (run-hooks 'gnus-apply-kill-hook)
2256       (if (and (zerop (buffer-size))
2257                (not no-display))
2258           (progn
2259             ;; This newsgroup is empty.
2260             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
2261             (gnus-message 6 "No unread news")
2262             (when kill-buffer
2263               (gnus-kill-or-deaden-summary kill-buffer))
2264             ;; Return nil from this function.
2265             nil)
2266         ;; Hide conversation thread subtrees.  We cannot do this in
2267         ;; gnus-summary-prepare-hook since kill processing may not
2268         ;; work with hidden articles.
2269         (and gnus-show-threads
2270              gnus-thread-hide-subtree
2271              (gnus-summary-hide-all-threads))
2272         ;; Show first unread article if requested.
2273         (if (and (not no-article)
2274                  (not no-display)
2275                  gnus-newsgroup-unreads
2276                  gnus-auto-select-first)
2277             (unless (if (eq gnus-auto-select-first 'best)
2278                         (gnus-summary-best-unread-article)
2279                       (gnus-summary-first-unread-article))
2280               (gnus-configure-windows 'summary))
2281           ;; Don't select any articles, just move point to the first
2282           ;; article in the group.
2283           (goto-char (point-min))
2284           (gnus-summary-position-point)
2285           (gnus-set-mode-line 'summary)
2286           (gnus-configure-windows 'summary 'force))
2287         (when kill-buffer
2288           (gnus-kill-or-deaden-summary kill-buffer))
2289         (when (get-buffer-window gnus-group-buffer t)
2290           ;; Gotta use windows, because recenter does wierd stuff if
2291           ;; the current buffer ain't the displayed window.
2292           (let ((owin (selected-window)))
2293             (select-window (get-buffer-window gnus-group-buffer t))
2294             (when (gnus-group-goto-group group)
2295               (recenter))
2296             (select-window owin))))
2297       ;; Mark this buffer as "prepared".
2298       (setq gnus-newsgroup-prepared t)
2299       t))))
2300
2301 (defun gnus-summary-prepare ()
2302   "Generate the summary buffer."
2303   (interactive)
2304   (let ((buffer-read-only nil))
2305     (erase-buffer)
2306     (setq gnus-newsgroup-data nil
2307           gnus-newsgroup-data-reverse nil)
2308     (run-hooks 'gnus-summary-generate-hook)
2309     ;; Generate the buffer, either with threads or without.
2310     (when gnus-newsgroup-headers
2311       (gnus-summary-prepare-threads
2312        (if gnus-show-threads
2313            (gnus-sort-gathered-threads
2314             (funcall gnus-summary-thread-gathering-function
2315                      (gnus-sort-threads
2316                       (gnus-cut-threads (gnus-make-threads)))))
2317          ;; Unthreaded display.
2318          (gnus-sort-articles gnus-newsgroup-headers))))
2319     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2320     ;; Call hooks for modifying summary buffer.
2321     (goto-char (point-min))
2322     (run-hooks 'gnus-summary-prepare-hook)))
2323
2324 (defsubst gnus-general-simplify-subject (subject)
2325   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2326   (setq subject
2327         (cond
2328          ;; Truncate the subject.
2329          ((numberp gnus-summary-gather-subject-limit)
2330           (setq subject (gnus-simplify-subject-re subject))
2331           (if (> (length subject) gnus-summary-gather-subject-limit)
2332               (substring subject 0 gnus-summary-gather-subject-limit)
2333             subject))
2334          ;; Fuzzily simplify it.
2335          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2336           (gnus-simplify-subject-fuzzy subject))
2337          ;; Just remove the leading "Re:".
2338          (t
2339           (gnus-simplify-subject-re subject))))
2340   
2341   (if (and gnus-summary-gather-exclude-subject
2342            (string-match gnus-summary-gather-exclude-subject subject))
2343       nil                               ; This article shouldn't be gathered
2344     subject))
2345
2346 (defun gnus-summary-simplify-subject-query ()
2347   "Query where the respool algorithm would put this article."
2348   (interactive)
2349   (gnus-set-global-variables)
2350   (gnus-summary-select-article)
2351   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2352
2353 (defun gnus-gather-threads-by-subject (threads)
2354   "Gather threads by looking at Subject headers."
2355   (if (not gnus-summary-make-false-root)
2356       threads
2357     (let ((hashtb (gnus-make-hashtable 1023))
2358           (prev threads)
2359           (result threads)
2360           subject hthread whole-subject)
2361       (while threads
2362         (setq subject (gnus-general-simplify-subject
2363                        (setq whole-subject (mail-header-subject 
2364                                             (caar threads)))))
2365         (if subject
2366             (if (setq hthread (gnus-gethash subject hashtb))
2367                 (progn
2368                   ;; We enter a dummy root into the thread, if we
2369                   ;; haven't done that already.
2370                   (unless (stringp (caar hthread))
2371                     (setcar hthread (list whole-subject (car hthread))))
2372                   ;; We add this new gathered thread to this gathered
2373                   ;; thread.
2374                   (setcdr (car hthread)
2375                           (nconc (cdar hthread) (list (car threads))))
2376                   ;; Remove it from the list of threads.
2377                   (setcdr prev (cdr threads))
2378                   (setq threads prev))
2379               ;; Enter this thread into the hash table.
2380               (gnus-sethash subject threads hashtb)))
2381         (setq prev threads)
2382         (setq threads (cdr threads)))
2383       result)))
2384
2385 (defun gnus-gather-threads-by-references (threads)
2386   "Gather threads by looking at References headers."
2387   (let ((idhashtb (gnus-make-hashtable 1023))
2388         (thhashtb (gnus-make-hashtable 1023))
2389         (prev threads)
2390         (result threads)
2391         ids references id gthread gid entered ref)
2392     (while threads
2393       (when (setq references (mail-header-references (caar threads)))
2394         (setq id (mail-header-id (caar threads))
2395               ids (gnus-split-references references)
2396               entered nil)
2397         (while (setq ref (pop ids))
2398           (setq ids (delete ref ids))
2399           (if (not (setq gid (gnus-gethash ref idhashtb)))
2400               (progn
2401                 (gnus-sethash ref id idhashtb)
2402                 (gnus-sethash id threads thhashtb))
2403             (setq gthread (gnus-gethash gid thhashtb))
2404             (unless entered
2405               ;; We enter a dummy root into the thread, if we
2406               ;; haven't done that already.
2407               (unless (stringp (caar gthread))
2408                 (setcar gthread (list (mail-header-subject (caar gthread))
2409                                       (car gthread))))
2410               ;; We add this new gathered thread to this gathered
2411               ;; thread.
2412               (setcdr (car gthread)
2413                       (nconc (cdar gthread) (list (car threads)))))
2414             ;; Add it into the thread hash table.
2415             (gnus-sethash id gthread thhashtb)
2416             (setq entered t)
2417             ;; Remove it from the list of threads.
2418             (setcdr prev (cdr threads))
2419             (setq threads prev))))
2420       (setq prev threads)
2421       (setq threads (cdr threads)))
2422     result))
2423
2424 (defun gnus-sort-gathered-threads (threads)
2425   "Sort subtreads inside each gathered thread by article number."
2426   (let ((result threads))
2427     (while threads
2428       (when (stringp (caar threads))
2429         (setcdr (car threads)
2430                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2431       (setq threads (cdr threads)))
2432     result))
2433
2434 (defun gnus-make-threads ()
2435   "Go through the dependency hashtb and find the roots.  Return all threads."
2436   (let (threads)
2437     (mapatoms
2438      (lambda (refs)
2439        (unless (car (symbol-value refs))
2440          ;; These threads do not refer back to any other articles,
2441          ;; so they're roots.
2442          (setq threads (append (cdr (symbol-value refs)) threads))))
2443      gnus-newsgroup-dependencies)
2444     threads))
2445
2446 (defun gnus-build-sparse-threads ()
2447   (let ((headers gnus-newsgroup-headers)
2448         (deps gnus-newsgroup-dependencies)
2449         header references generation relations 
2450         cthread subject child end pthread relation)
2451     ;; First we create an alist of generations/relations, where 
2452     ;; generations is how much we trust the ralation, and the relation
2453     ;; is parent/child.
2454     (gnus-message 7 "Making sparse threads...")
2455     (save-excursion
2456       (nnheader-set-temp-buffer " *gnus sparse threads*")
2457       (while (setq header (pop headers))
2458         (when (and (setq references (mail-header-references header))
2459                    (not (string= references "")))
2460           (insert references)
2461           (setq child (mail-header-id header)
2462                 subject (mail-header-subject header))
2463           (setq generation 0)
2464           (while (search-backward ">" nil t)
2465             (setq end (1+ (point)))
2466             (when (search-backward "<" nil t)
2467               (push (list (incf generation) 
2468                           child (setq child (buffer-substring (point) end))
2469                           subject)
2470                     relations)))
2471           (push (list (1+ generation) child nil subject) relations)
2472           (erase-buffer)))
2473       (kill-buffer (current-buffer)))
2474     ;; Sort over trustworthiness.
2475     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
2476     (while (setq relation (pop relations))
2477       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
2478                 (unless (car (symbol-value cthread))
2479                   ;; Make this article the parent of these threads.
2480                   (setcar (symbol-value cthread)
2481                           (vector gnus-reffed-article-number 
2482                                   (cadddr relation) 
2483                                   "" ""
2484                                   (cadr relation) 
2485                                   (or (caddr relation) "") 0 0 "")))
2486               (set cthread (list (vector gnus-reffed-article-number
2487                                          (cadddr relation) 
2488                                          "" "" (cadr relation) 
2489                                          (or (caddr relation) "") 0 0 ""))))
2490         (push gnus-reffed-article-number gnus-newsgroup-limit)
2491         (push gnus-reffed-article-number gnus-newsgroup-sparse)
2492         (push (cons gnus-reffed-article-number gnus-sparse-mark)
2493               gnus-newsgroup-reads)
2494         (decf gnus-reffed-article-number)
2495         ;; Make this new thread the child of its parent.
2496         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
2497             (setcdr (symbol-value pthread)
2498                     (nconc (cdr (symbol-value pthread))
2499                            (list (symbol-value cthread))))
2500           (set pthread (list nil (symbol-value cthread))))))
2501     (gnus-message 7 "Making sparse threads...done")))
2502
2503 (defun gnus-build-old-threads ()
2504   ;; Look at all the articles that refer back to old articles, and
2505   ;; fetch the headers for the articles that aren't there.  This will
2506   ;; build complete threads - if the roots haven't been expired by the
2507   ;; server, that is.
2508   (let (id heads)
2509     (mapatoms
2510      (lambda (refs)
2511        (when (not (car (symbol-value refs)))
2512          (setq heads (cdr (symbol-value refs)))
2513          (while heads
2514            (if (memq (mail-header-number (caar heads))
2515                      gnus-newsgroup-dormant)
2516                (setq heads (cdr heads))
2517              (setq id (symbol-name refs))
2518              (while (and (setq id (gnus-build-get-header id))
2519                          (not (car (gnus-gethash
2520                                     id gnus-newsgroup-dependencies)))))
2521              (setq heads nil)))))
2522      gnus-newsgroup-dependencies)))
2523
2524 (defun gnus-build-get-header (id)
2525   ;; Look through the buffer of NOV lines and find the header to
2526   ;; ID.  Enter this line into the dependencies hash table, and return
2527   ;; the id of the parent article (if any).
2528   (let ((deps gnus-newsgroup-dependencies)
2529         found header)
2530     (prog1
2531         (save-excursion
2532           (set-buffer nntp-server-buffer)
2533           (goto-char (point-min))
2534           (while (and (not found) (search-forward id nil t))
2535             (beginning-of-line)
2536             (setq found (looking-at
2537                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
2538                                  (regexp-quote id))))
2539             (or found (beginning-of-line 2)))
2540           (when found
2541             (beginning-of-line)
2542             (and
2543              (setq header (gnus-nov-parse-line
2544                            (read (current-buffer)) deps))
2545              (gnus-parent-id (mail-header-references header)))))
2546       (when header
2547         (let ((number (mail-header-number header)))
2548           (push number gnus-newsgroup-limit)
2549           (push header gnus-newsgroup-headers)
2550           (if (memq number gnus-newsgroup-unselected)
2551               (progn
2552                 (push number gnus-newsgroup-unreads)
2553                 (setq gnus-newsgroup-unselected
2554                       (delq number gnus-newsgroup-unselected)))
2555             (push number gnus-newsgroup-ancient)))))))
2556
2557 (defun gnus-summary-update-article-line (article header)
2558   "Update the line for ARTICLE using HEADERS."
2559   (let* ((id (mail-header-id header))
2560          (thread (gnus-id-to-thread id)))
2561     (unless thread
2562       (error "Article in no thread"))
2563     ;; Update the thread.
2564     (setcar thread header)
2565     (gnus-summary-goto-subject article)
2566     (let* ((datal (gnus-data-find-list article))
2567            (data (car datal))
2568            (length (when (cdr datal)
2569                      (- (gnus-data-pos data)
2570                         (gnus-data-pos (cadr datal)))))
2571            (buffer-read-only nil)
2572            (level (gnus-summary-thread-level)))
2573       (gnus-delete-line)
2574       (gnus-summary-insert-line
2575        header level nil (gnus-article-mark article)
2576        (memq article gnus-newsgroup-replied)
2577        (memq article gnus-newsgroup-expirable)
2578        (mail-header-subject header)
2579        nil (cdr (assq article gnus-newsgroup-scored))
2580        (memq article gnus-newsgroup-processable))
2581       (when length
2582         (gnus-data-update-list
2583          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
2584      
2585 (defun gnus-summary-update-article (article &optional iheader)
2586   "Update ARTICLE in the summary buffer."
2587   (set-buffer gnus-summary-buffer)
2588   (let* ((header (or iheader (gnus-summary-article-header article)))
2589          (id (mail-header-id header))
2590          (data (gnus-data-find article))
2591          (thread (gnus-id-to-thread id))
2592          (references (mail-header-references header))
2593          (parent
2594           (gnus-id-to-thread
2595            (or (gnus-parent-id 
2596                 (if (and references
2597                          (not (equal "" references)))
2598                     references))
2599                "none")))
2600          (buffer-read-only nil)
2601          (old (car thread))
2602          (number (mail-header-number header))
2603          pos)
2604     (when thread
2605       ;; !!! Should this be in or not?
2606       (unless iheader
2607         (setcar thread nil))
2608       (when parent
2609         (delq thread parent))
2610       (if (gnus-summary-insert-subject id header iheader)
2611           ;; Set the (possibly) new article number in the data structure.
2612           (gnus-data-set-number data (gnus-id-to-article id))
2613         (setcar thread old)
2614         nil))))
2615
2616 (defun gnus-rebuild-thread (id)
2617   "Rebuild the thread containing ID."
2618   (let ((buffer-read-only nil)
2619         current thread data)
2620     (if (not gnus-show-threads)
2621         (setq thread (list (car (gnus-id-to-thread id))))
2622       ;; Get the thread this article is part of.
2623       (setq thread (gnus-remove-thread id)))
2624     (setq current (save-excursion
2625                     (and (zerop (forward-line -1))
2626                          (gnus-summary-article-number))))
2627     ;; If this is a gathered thread, we have to go some re-gathering.
2628     (when (stringp (car thread))
2629       (let ((subject (car thread))
2630             roots thr)
2631         (setq thread (cdr thread))
2632         (while thread
2633           (unless (memq (setq thr (gnus-id-to-thread
2634                                       (gnus-root-id
2635                                        (mail-header-id (caar thread)))))
2636                         roots)
2637             (push thr roots))
2638           (setq thread (cdr thread)))
2639         ;; We now have all (unique) roots.
2640         (if (= (length roots) 1)
2641             ;; All the loose roots are now one solid root.
2642             (setq thread (car roots))
2643           (setq thread (cons subject (gnus-sort-threads roots))))))
2644     (let (threads)
2645       ;; We then insert this thread into the summary buffer.
2646       (let (gnus-newsgroup-data gnus-newsgroup-threads)
2647         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
2648         (setq data (nreverse gnus-newsgroup-data))
2649         (setq threads gnus-newsgroup-threads))
2650       ;; We splice the new data into the data structure.
2651       (gnus-data-enter-list current data)
2652       (gnus-data-compute-positions)
2653       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
2654
2655 (defun gnus-number-to-header (number)
2656   "Return the header for article NUMBER."
2657   (let ((headers gnus-newsgroup-headers))
2658     (while (and headers
2659                 (not (= number (mail-header-number (car headers)))))
2660       (pop headers))
2661     (when headers
2662       (car headers))))
2663
2664 (defun gnus-parent-headers (headers &optional generation)
2665   "Return the headers of the GENERATIONeth parent of HEADERS."
2666   (unless generation 
2667     (setq generation 1))
2668   (let (references parent)
2669     (while (and headers (not (zerop generation)))
2670       (setq references (mail-header-references headers))
2671       (when (and references
2672                  (setq parent (gnus-parent-id references))
2673                  (setq headers (car (gnus-id-to-thread parent))))
2674         (decf generation)))
2675     headers))
2676
2677 (defun gnus-id-to-thread (id)
2678   "Return the (sub-)thread where ID appears."
2679   (gnus-gethash id gnus-newsgroup-dependencies))
2680
2681 (defun gnus-id-to-article (id)
2682   "Return the article number of ID."
2683   (let ((thread (gnus-id-to-thread id)))
2684     (when (and thread
2685                (car thread))
2686       (mail-header-number (car thread)))))
2687
2688 (defun gnus-id-to-header (id)
2689   "Return the article headers of ID."
2690   (car (gnus-id-to-thread id)))
2691
2692 (defun gnus-article-displayed-root-p (article)
2693   "Say whether ARTICLE is a root(ish) article."
2694   (let ((level (gnus-summary-thread-level article))
2695         (refs (mail-header-references  (gnus-summary-article-header article)))
2696         particle)
2697     (cond 
2698      ((null level) nil)
2699      ((zerop level) t)
2700      ((null refs) t)
2701      ((null (gnus-parent-id refs)) t)
2702      ((and (= 1 level)
2703            (null (setq particle (gnus-id-to-article
2704                                  (gnus-parent-id refs))))
2705            (null (gnus-summary-thread-level particle)))))))
2706
2707 (defun gnus-root-id (id)
2708   "Return the id of the root of the thread where ID appears."
2709   (let (last-id prev)
2710     (while (and id (setq prev (car (gnus-gethash 
2711                                     id gnus-newsgroup-dependencies))))
2712       (setq last-id id
2713             id (gnus-parent-id (mail-header-references prev))))
2714     last-id))
2715
2716 (defun gnus-remove-thread (id &optional dont-remove)
2717   "Remove the thread that has ID in it."
2718   (let ((dep gnus-newsgroup-dependencies)
2719         headers thread last-id)
2720     ;; First go up in this thread until we find the root.
2721     (setq last-id (gnus-root-id id))
2722     (setq headers (list (car (gnus-id-to-thread last-id))
2723                         (caadr (gnus-id-to-thread last-id))))
2724     ;; We have now found the real root of this thread.  It might have
2725     ;; been gathered into some loose thread, so we have to search
2726     ;; through the threads to find the thread we wanted.
2727     (let ((threads gnus-newsgroup-threads)
2728           sub)
2729       (while threads
2730         (setq sub (car threads))
2731         (if (stringp (car sub))
2732             ;; This is a gathered thread, so we look at the roots
2733             ;; below it to find whether this article is in this
2734             ;; gathered root.
2735             (progn
2736               (setq sub (cdr sub))
2737               (while sub
2738                 (when (member (caar sub) headers)
2739                   (setq thread (car threads)
2740                         threads nil
2741                         sub nil))
2742                 (setq sub (cdr sub))))
2743           ;; It's an ordinary thread, so we check it.
2744           (when (eq (car sub) (car headers))
2745             (setq thread sub
2746                   threads nil)))
2747         (setq threads (cdr threads)))
2748       ;; If this article is in no thread, then it's a root.
2749       (if thread
2750           (unless dont-remove
2751             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
2752         (setq thread (gnus-gethash last-id dep)))
2753       (when thread
2754         (prog1
2755             thread ; We return this thread.
2756           (unless dont-remove
2757             (if (stringp (car thread))
2758                 (progn
2759                   ;; If we use dummy roots, then we have to remove the
2760                   ;; dummy root as well.
2761                   (when (eq gnus-summary-make-false-root 'dummy)
2762                     ;; Uhm.
2763                     )
2764                   (setq thread (cdr thread))
2765                   (while thread
2766                     (gnus-remove-thread-1 (car thread))
2767                     (setq thread (cdr thread))))
2768               (gnus-remove-thread-1 thread))))))))
2769
2770 (defun gnus-remove-thread-1 (thread)
2771   "Remove the thread THREAD recursively."
2772   (let ((number (mail-header-number (car thread)))
2773         pos)
2774     (when (setq pos (text-property-any
2775                      (point-min) (point-max) 'gnus-number number))
2776       (goto-char pos)
2777       (gnus-delete-line)
2778       (gnus-data-remove number))
2779     (setq thread (cdr thread))
2780     (while thread
2781       (gnus-remove-thread-1 (pop thread)))))
2782
2783 (defun gnus-sort-threads (threads)
2784   "Sort THREADS."
2785   (if (not gnus-thread-sort-functions)
2786       threads
2787     (let ((func (if (= 1 (length gnus-thread-sort-functions))
2788                     (car gnus-thread-sort-functions)
2789                   `(lambda (t1 t2)
2790                      ,(gnus-make-sort-function 
2791                        (reverse gnus-thread-sort-functions))))))
2792       (gnus-message 7 "Sorting threads...")
2793       (prog1
2794           (sort threads func)
2795         (gnus-message 7 "Sorting threads...done")))))
2796
2797 (defun gnus-sort-articles (articles)
2798   "Sort ARTICLES."
2799   (when gnus-article-sort-functions
2800     (let ((func (if (= 1 (length gnus-article-sort-functions))
2801                     (car gnus-article-sort-functions)
2802                   `(lambda (t1 t2)
2803                      ,(gnus-make-sort-function 
2804                        (reverse gnus-article-sort-functions))))))
2805       (gnus-message 7 "Sorting articles...")
2806       (prog1
2807           (setq gnus-newsgroup-headers (sort articles func))
2808         (gnus-message 7 "Sorting articles...done")))))
2809
2810 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2811 (defmacro gnus-thread-header (thread)
2812   ;; Return header of first article in THREAD.
2813   ;; Note that THREAD must never, ever be anything else than a variable -
2814   ;; using some other form will lead to serious barfage.
2815   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
2816   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
2817   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
2818         (vector thread) 2))
2819
2820 (defsubst gnus-article-sort-by-number (h1 h2)
2821   "Sort articles by article number."
2822   (< (mail-header-number h1)
2823      (mail-header-number h2)))
2824
2825 (defun gnus-thread-sort-by-number (h1 h2)
2826   "Sort threads by root article number."
2827   (gnus-article-sort-by-number
2828    (gnus-thread-header h1) (gnus-thread-header h2)))
2829
2830 (defsubst gnus-article-sort-by-author (h1 h2)
2831   "Sort articles by root author."
2832   (string-lessp
2833    (let ((extract (funcall
2834                    gnus-extract-address-components
2835                    (mail-header-from h1))))
2836      (or (car extract) (cdr extract)))
2837    (let ((extract (funcall
2838                    gnus-extract-address-components
2839                    (mail-header-from h2))))
2840      (or (car extract) (cdr extract)))))
2841
2842 (defun gnus-thread-sort-by-author (h1 h2)
2843   "Sort threads by root author."
2844   (gnus-article-sort-by-author
2845    (gnus-thread-header h1)  (gnus-thread-header h2)))
2846
2847 (defsubst gnus-article-sort-by-subject (h1 h2)
2848   "Sort articles by root subject."
2849   (string-lessp
2850    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
2851    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
2852
2853 (defun gnus-thread-sort-by-subject (h1 h2)
2854   "Sort threads by root subject."
2855   (gnus-article-sort-by-subject
2856    (gnus-thread-header h1) (gnus-thread-header h2)))
2857
2858 (defsubst gnus-article-sort-by-date (h1 h2)
2859   "Sort articles by root article date."
2860   (string-lessp
2861    (inline (gnus-sortable-date (mail-header-date h1)))
2862    (inline (gnus-sortable-date (mail-header-date h2)))))
2863
2864 (defun gnus-thread-sort-by-date (h1 h2)
2865   "Sort threads by root article date."
2866   (gnus-article-sort-by-date
2867    (gnus-thread-header h1) (gnus-thread-header h2)))
2868
2869 (defsubst gnus-article-sort-by-score (h1 h2)
2870   "Sort articles by root article score.
2871 Unscored articles will be counted as having a score of zero."
2872   (> (or (cdr (assq (mail-header-number h1)
2873                     gnus-newsgroup-scored))
2874          gnus-summary-default-score 0)
2875      (or (cdr (assq (mail-header-number h2)
2876                     gnus-newsgroup-scored))
2877          gnus-summary-default-score 0)))
2878
2879 (defun gnus-thread-sort-by-score (h1 h2)
2880   "Sort threads by root article score."
2881   (gnus-article-sort-by-score
2882    (gnus-thread-header h1) (gnus-thread-header h2)))
2883
2884 (defun gnus-thread-sort-by-total-score (h1 h2)
2885   "Sort threads by the sum of all scores in the thread.
2886 Unscored articles will be counted as having a score of zero."
2887   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
2888
2889 (defun gnus-thread-total-score (thread)
2890   ;;  This function find the total score of THREAD.
2891   (cond ((null thread)
2892          0)
2893         ((consp thread)
2894          (if (stringp (car thread))
2895              (apply gnus-thread-score-function 0
2896                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
2897            (gnus-thread-total-score-1 thread)))
2898         (t
2899          (gnus-thread-total-score-1 (list thread)))))
2900
2901 (defun gnus-thread-total-score-1 (root)
2902   ;; This function find the total score of the thread below ROOT.
2903   (setq root (car root))
2904   (apply gnus-thread-score-function
2905          (or (append
2906               (mapcar 'gnus-thread-total-score
2907                       (cdr (gnus-gethash (mail-header-id root)
2908                                          gnus-newsgroup-dependencies)))
2909                  (if (> (mail-header-number root) 0)
2910                      (list (or (cdr (assq (mail-header-number root) 
2911                                           gnus-newsgroup-scored))
2912                                gnus-summary-default-score 0))))
2913              (list gnus-summary-default-score)
2914              '(0))))
2915
2916 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
2917 (defvar gnus-tmp-prev-subject nil)
2918 (defvar gnus-tmp-false-parent nil)
2919 (defvar gnus-tmp-root-expunged nil)
2920 (defvar gnus-tmp-dummy-line nil)
2921
2922 (defun gnus-summary-prepare-threads (threads)
2923   "Prepare summary buffer from THREADS and indentation LEVEL.
2924 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
2925 or a straight list of headers."
2926   (gnus-message 7 "Generating summary...")
2927
2928   (setq gnus-newsgroup-threads threads)
2929   (beginning-of-line)
2930
2931   (let ((gnus-tmp-level 0)
2932         (default-score (or gnus-summary-default-score 0))
2933         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
2934         thread number subject stack state gnus-tmp-gathered beg-match
2935         new-roots gnus-tmp-new-adopts thread-end
2936         gnus-tmp-header gnus-tmp-unread
2937         gnus-tmp-replied gnus-tmp-subject-or-nil
2938         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
2939         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
2940         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
2941
2942     (setq gnus-tmp-prev-subject nil)
2943
2944     (if (vectorp (car threads))
2945         ;; If this is a straight (sic) list of headers, then a
2946         ;; threaded summary display isn't required, so we just create
2947         ;; an unthreaded one.
2948         (gnus-summary-prepare-unthreaded threads)
2949
2950       ;; Do the threaded display.
2951
2952       (while (or threads stack gnus-tmp-new-adopts new-roots)
2953
2954         (if (and (= gnus-tmp-level 0)
2955                  (not (setq gnus-tmp-dummy-line nil))
2956                  (or (not stack)
2957                      (= (caar stack) 0))
2958                  (not gnus-tmp-false-parent)
2959                  (or gnus-tmp-new-adopts new-roots))
2960             (if gnus-tmp-new-adopts
2961                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
2962                       thread (list (car gnus-tmp-new-adopts))
2963                       gnus-tmp-header (caar thread)
2964                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
2965               (if new-roots
2966                   (setq thread (list (car new-roots))
2967                         gnus-tmp-header (caar thread)
2968                         new-roots (cdr new-roots))))
2969
2970           (if threads
2971               ;; If there are some threads, we do them before the
2972               ;; threads on the stack.
2973               (setq thread threads
2974                     gnus-tmp-header (caar thread))
2975             ;; There were no current threads, so we pop something off
2976             ;; the stack.
2977             (setq state (car stack)
2978                   gnus-tmp-level (car state)
2979                   thread (cdr state)
2980                   stack (cdr stack)
2981                   gnus-tmp-header (caar thread))))
2982
2983         (setq gnus-tmp-false-parent nil)
2984         (setq gnus-tmp-root-expunged nil)
2985         (setq thread-end nil)
2986
2987         (if (stringp gnus-tmp-header)
2988             ;; The header is a dummy root.
2989             (cond
2990              ((eq gnus-summary-make-false-root 'adopt)
2991               ;; We let the first article adopt the rest.
2992               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
2993                                                (cddar thread)))
2994               (setq gnus-tmp-gathered
2995                     (nconc (mapcar
2996                             (lambda (h) (mail-header-number (car h)))
2997                             (cddar thread))
2998                            gnus-tmp-gathered))
2999               (setq thread (cons (list (caar thread)
3000                                        (cadar thread))
3001                                  (cdr thread)))
3002               (setq gnus-tmp-level -1
3003                     gnus-tmp-false-parent t))
3004              ((eq gnus-summary-make-false-root 'empty)
3005               ;; We print adopted articles with empty subject fields.
3006               (setq gnus-tmp-gathered
3007                     (nconc (mapcar
3008                             (lambda (h) (mail-header-number (car h)))
3009                             (cddar thread))
3010                            gnus-tmp-gathered))
3011               (setq gnus-tmp-level -1))
3012              ((eq gnus-summary-make-false-root 'dummy)
3013               ;; We remember that we probably want to output a dummy
3014               ;; root.
3015               (setq gnus-tmp-dummy-line gnus-tmp-header)
3016               (setq gnus-tmp-prev-subject gnus-tmp-header))
3017              (t
3018               ;; We do not make a root for the gathered
3019               ;; sub-threads at all.
3020               (setq gnus-tmp-level -1)))
3021
3022           (setq number (mail-header-number gnus-tmp-header)
3023                 subject (mail-header-subject gnus-tmp-header))
3024
3025           (cond
3026            ;; If the thread has changed subject, we might want to make
3027            ;; this subthread into a root.
3028            ((and (null gnus-thread-ignore-subject)
3029                  (not (zerop gnus-tmp-level))
3030                  gnus-tmp-prev-subject
3031                  (not (inline
3032                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3033             (setq new-roots (nconc new-roots (list (car thread)))
3034                   thread-end t
3035                   gnus-tmp-header nil))
3036            ;; If the article lies outside the current limit,
3037            ;; then we do not display it.
3038            ((and (not (memq number gnus-newsgroup-limit))
3039                  (not gnus-tmp-dummy-line))
3040             (setq gnus-tmp-gathered
3041                   (nconc (mapcar
3042                           (lambda (h) (mail-header-number (car h)))
3043                           (cdar thread))
3044                          gnus-tmp-gathered))
3045             (setq gnus-tmp-new-adopts (if (cdar thread)
3046                                           (append gnus-tmp-new-adopts
3047                                                   (cdar thread))
3048                                         gnus-tmp-new-adopts)
3049                   thread-end t
3050                   gnus-tmp-header nil)
3051             (when (zerop gnus-tmp-level)
3052               (setq gnus-tmp-root-expunged t)))
3053            ;; Perhaps this article is to be marked as read?
3054            ((and gnus-summary-mark-below
3055                  (< (or (cdr (assq number gnus-newsgroup-scored))
3056                         default-score)
3057                     gnus-summary-mark-below)
3058                  ;; Don't touch sparse articles.
3059                  (not (memq number gnus-newsgroup-sparse))
3060                  (not (memq number gnus-newsgroup-ancient)))
3061             (setq gnus-newsgroup-unreads
3062                   (delq number gnus-newsgroup-unreads))
3063             (if gnus-newsgroup-auto-expire
3064                 (push number gnus-newsgroup-expirable)
3065               (push (cons number gnus-low-score-mark)
3066                     gnus-newsgroup-reads))))
3067
3068           (when gnus-tmp-header
3069             ;; We may have an old dummy line to output before this
3070             ;; article.
3071             (when gnus-tmp-dummy-line
3072               (gnus-summary-insert-dummy-line
3073                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3074               (setq gnus-tmp-dummy-line nil))
3075
3076             ;; Compute the mark.
3077             (setq gnus-tmp-unread (gnus-article-mark number))
3078
3079             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3080                                   gnus-tmp-header gnus-tmp-level)
3081                   gnus-newsgroup-data)
3082
3083             ;; Actually insert the line.
3084             (setq
3085              gnus-tmp-subject-or-nil
3086              (cond
3087               ((and gnus-thread-ignore-subject
3088                     gnus-tmp-prev-subject
3089                     (not (inline (gnus-subject-equal
3090                                   gnus-tmp-prev-subject subject))))
3091                subject)
3092               ((zerop gnus-tmp-level)
3093                (if (and (eq gnus-summary-make-false-root 'empty)
3094                         (memq number gnus-tmp-gathered)
3095                         gnus-tmp-prev-subject
3096                         (inline (gnus-subject-equal
3097                                  gnus-tmp-prev-subject subject)))
3098                    gnus-summary-same-subject
3099                  subject))
3100               (t gnus-summary-same-subject)))
3101             (if (and (eq gnus-summary-make-false-root 'adopt)
3102                      (= gnus-tmp-level 1)
3103                      (memq number gnus-tmp-gathered))
3104                 (setq gnus-tmp-opening-bracket ?\<
3105                       gnus-tmp-closing-bracket ?\>)
3106               (setq gnus-tmp-opening-bracket ?\[
3107                     gnus-tmp-closing-bracket ?\]))
3108             (setq
3109              gnus-tmp-indentation
3110              (aref gnus-thread-indent-array gnus-tmp-level)
3111              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3112              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3113                                 gnus-summary-default-score 0)
3114              gnus-tmp-score-char
3115              (if (or (null gnus-summary-default-score)
3116                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3117                          gnus-summary-zcore-fuzz)) ? 
3118                (if (< gnus-tmp-score gnus-summary-default-score)
3119                    gnus-score-below-mark gnus-score-over-mark))
3120              gnus-tmp-replied
3121              (cond ((memq number gnus-newsgroup-processable)
3122                     gnus-process-mark)
3123                    ((memq number gnus-newsgroup-cached)
3124                     gnus-cached-mark)
3125                    ((memq number gnus-newsgroup-replied)
3126                     gnus-replied-mark)
3127                    ((memq number gnus-newsgroup-saved)
3128                     gnus-saved-mark)
3129                    (t gnus-unread-mark))
3130              gnus-tmp-from (mail-header-from gnus-tmp-header)
3131              gnus-tmp-name
3132              (cond
3133               ((string-match "(.+)" gnus-tmp-from)
3134                (substring gnus-tmp-from
3135                           (1+ (match-beginning 0)) (1- (match-end 0))))
3136               ((string-match "<[^>]+> *$" gnus-tmp-from)
3137                (setq beg-match (match-beginning 0))
3138                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3139                         (substring gnus-tmp-from (1+ (match-beginning 0))
3140                                    (1- (match-end 0))))
3141                    (substring gnus-tmp-from 0 beg-match)))
3142               (t gnus-tmp-from)))
3143             (when (string= gnus-tmp-name "")
3144               (setq gnus-tmp-name gnus-tmp-from))
3145             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
3146             (gnus-put-text-property
3147              (point)
3148              (progn (eval gnus-summary-line-format-spec) (point))
3149              'gnus-number number)
3150             (when gnus-visual-p
3151               (forward-line -1)
3152               (run-hooks 'gnus-summary-update-hook)
3153               (forward-line 1))
3154
3155             (setq gnus-tmp-prev-subject subject)))
3156
3157         (when (nth 1 thread)
3158           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3159         (incf gnus-tmp-level)
3160         (setq threads (if thread-end nil (cdar thread)))
3161         (unless threads
3162           (setq gnus-tmp-level 0)))))
3163   (gnus-message 7 "Generating summary...done"))
3164
3165 (defun gnus-summary-prepare-unthreaded (headers)
3166   "Generate an unthreaded summary buffer based on HEADERS."
3167   (let (header number mark)
3168
3169     (while headers
3170       ;; We may have to root out some bad articles...
3171       (when (memq (setq number (mail-header-number
3172                                 (setq header (pop headers))))
3173                   gnus-newsgroup-limit)
3174         ;; Mark article as read when it has a low score.
3175         (when (and gnus-summary-mark-below
3176                    (< (or (cdr (assq number gnus-newsgroup-scored))
3177                           gnus-summary-default-score 0)
3178                       gnus-summary-mark-below)
3179                    (not (memq number gnus-newsgroup-ancient)))
3180           (setq gnus-newsgroup-unreads
3181                 (delq number gnus-newsgroup-unreads))
3182           (if gnus-newsgroup-auto-expire
3183               (push number gnus-newsgroup-expirable)
3184             (push (cons number gnus-low-score-mark)
3185                   gnus-newsgroup-reads)))
3186
3187         (setq mark (gnus-article-mark number))
3188         (setq gnus-newsgroup-data
3189               (cons (gnus-data-make number mark (1+ (point)) header 0)
3190                     gnus-newsgroup-data))
3191         (gnus-summary-insert-line
3192          header 0 nil mark (memq number gnus-newsgroup-replied)
3193          (memq number gnus-newsgroup-expirable)
3194          (mail-header-subject header) nil
3195          (cdr (assq number gnus-newsgroup-scored))
3196          (memq number gnus-newsgroup-processable))))))
3197
3198 (defun gnus-select-newsgroup (group &optional read-all)
3199   "Select newsgroup GROUP.
3200 If READ-ALL is non-nil, all articles in the group are selected."
3201   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3202          (info (nth 2 entry))
3203          articles fetched-articles cached)
3204
3205     (or (gnus-check-server
3206          (setq gnus-current-select-method (gnus-find-method-for-group group)))
3207         (error "Couldn't open server"))
3208
3209     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3210         (gnus-activate-group group)     ; Or we can activate it...
3211         (progn                          ; Or we bug out.
3212           (when (equal major-mode 'gnus-summary-mode)
3213             (kill-buffer (current-buffer)))
3214           (error "Couldn't request group %s: %s"
3215                  group (gnus-status-message group))))
3216
3217     (unless (gnus-request-group group t)
3218       (when (equal major-mode 'gnus-summary-mode)
3219         (kill-buffer (current-buffer)))
3220       (error "Couldn't request group %s: %s"
3221              group (gnus-status-message group)))      
3222
3223     (setq gnus-newsgroup-name group)
3224     (setq gnus-newsgroup-unselected nil)
3225     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3226
3227     ;; Adjust and set lists of article marks.
3228     (when info
3229       (gnus-adjust-marked-articles info))
3230
3231     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3232     (when (gnus-virtual-group-p group)
3233       (setq cached gnus-newsgroup-cached))
3234
3235     (setq gnus-newsgroup-unreads
3236           (gnus-set-difference
3237            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3238            gnus-newsgroup-dormant))
3239
3240     (setq gnus-newsgroup-processable nil)
3241
3242     (setq articles (gnus-articles-to-read group read-all))
3243
3244     (cond
3245      ((null articles)
3246       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3247       'quit)
3248      ((eq articles 0) nil)
3249      (t
3250       ;; Init the dependencies hash table.
3251       (setq gnus-newsgroup-dependencies
3252             (gnus-make-hashtable (length articles)))
3253       ;; Retrieve the headers and read them in.
3254       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3255       (setq gnus-newsgroup-headers
3256             (if (eq 'nov
3257                     (setq gnus-headers-retrieved-by
3258                           (gnus-retrieve-headers
3259                            articles gnus-newsgroup-name
3260                            ;; We might want to fetch old headers, but
3261                            ;; not if there is only 1 article.
3262                            (and gnus-fetch-old-headers
3263                                 (or (and
3264                                      (not (eq gnus-fetch-old-headers 'some))
3265                                      (not (numberp gnus-fetch-old-headers)))
3266                                     (> (length articles) 1))))))
3267                 (gnus-get-newsgroup-headers-xover articles)
3268               (gnus-get-newsgroup-headers)))
3269       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3270
3271       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3272       (when cached
3273         (setq gnus-newsgroup-cached cached))
3274
3275       ;; Suppress duplicates?
3276       (when gnus-suppress-duplicates
3277         (gnus-dup-suppress-articles))
3278
3279       ;; Set the initial limit.
3280       (setq gnus-newsgroup-limit (copy-sequence articles))
3281       ;; Remove canceled articles from the list of unread articles.
3282       (setq gnus-newsgroup-unreads
3283             (gnus-set-sorted-intersection
3284              gnus-newsgroup-unreads
3285              (setq fetched-articles
3286                    (mapcar (lambda (headers) (mail-header-number headers))
3287                            gnus-newsgroup-headers))))
3288       ;; Removed marked articles that do not exist.
3289       (gnus-update-missing-marks
3290        (gnus-sorted-complement fetched-articles articles))
3291       ;; We might want to build some more threads first.
3292       (and gnus-fetch-old-headers
3293            (eq gnus-headers-retrieved-by 'nov)
3294            (gnus-build-old-threads))
3295       ;; Check whether auto-expire is to be done in this group.
3296       (setq gnus-newsgroup-auto-expire
3297             (gnus-group-auto-expirable-p group))
3298       ;; Set up the article buffer now, if necessary.
3299       (unless gnus-single-article-buffer
3300         (gnus-article-setup-buffer))
3301       ;; First and last article in this newsgroup.
3302       (when gnus-newsgroup-headers
3303         (setq gnus-newsgroup-begin
3304               (mail-header-number (car gnus-newsgroup-headers))
3305               gnus-newsgroup-end
3306               (mail-header-number
3307                (gnus-last-element gnus-newsgroup-headers))))
3308       ;; GROUP is successfully selected.
3309       (or gnus-newsgroup-headers t)))))
3310
3311 (defun gnus-articles-to-read (group read-all)
3312   ;; Find out what articles the user wants to read.
3313   (let* ((articles
3314           ;; Select all articles if `read-all' is non-nil, or if there
3315           ;; are no unread articles.
3316           (if (or read-all
3317                   (and (zerop (length gnus-newsgroup-marked))
3318                        (zerop (length gnus-newsgroup-unreads)))
3319                   (eq (gnus-group-find-parameter group 'display)
3320                       'all))
3321               (gnus-uncompress-range (gnus-active group))
3322             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3323                           (copy-sequence gnus-newsgroup-unreads))
3324                   '<)))
3325          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3326          (scored (length scored-list))
3327          (number (length articles))
3328          (marked (+ (length gnus-newsgroup-marked)
3329                     (length gnus-newsgroup-dormant)))
3330          (select
3331           (cond
3332            ((numberp read-all)
3333             read-all)
3334            (t
3335             (condition-case ()
3336                 (cond
3337                  ((and (or (<= scored marked) (= scored number))
3338                        (numberp gnus-large-newsgroup)
3339                        (> number gnus-large-newsgroup))
3340                   (let ((input
3341                          (read-string
3342                           (format
3343                            "How many articles from %s (default %d): "
3344                            gnus-newsgroup-name number))))
3345                     (if (string-match "^[ \t]*$" input) number input)))
3346                  ((and (> scored marked) (< scored number)
3347                        (> (- scored number) 20))
3348                   (let ((input
3349                          (read-string
3350                           (format "%s %s (%d scored, %d total): "
3351                                   "How many articles from"
3352                                   group scored number))))
3353                     (if (string-match "^[ \t]*$" input)
3354                         number input)))
3355                  (t number))
3356               (quit nil))))))
3357     (setq select (if (stringp select) (string-to-number select) select))
3358     (if (or (null select) (zerop select))
3359         select
3360       (if (and (not (zerop scored)) (<= (abs select) scored))
3361           (progn
3362             (setq articles (sort scored-list '<))
3363             (setq number (length articles)))
3364         (setq articles (copy-sequence articles)))
3365
3366       (if (< (abs select) number)
3367           (if (< select 0)
3368               ;; Select the N oldest articles.
3369               (setcdr (nthcdr (1- (abs select)) articles) nil)
3370             ;; Select the N most recent articles.
3371             (setq articles (nthcdr (- number select) articles))))
3372       (setq gnus-newsgroup-unselected
3373             (gnus-sorted-intersection
3374              gnus-newsgroup-unreads
3375              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3376       articles)))
3377
3378 (defun gnus-killed-articles (killed articles)
3379   (let (out)
3380     (while articles
3381       (if (inline (gnus-member-of-range (car articles) killed))
3382           (setq out (cons (car articles) out)))
3383       (setq articles (cdr articles)))
3384     out))
3385
3386 (defun gnus-uncompress-marks (marks)
3387   "Uncompress the mark ranges in MARKS."
3388   (let ((uncompressed '(score bookmark))
3389         out)
3390     (while marks
3391       (if (memq (caar marks) uncompressed)
3392           (push (car marks) out)
3393         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
3394       (setq marks (cdr marks)))
3395     out))
3396
3397 (defun gnus-adjust-marked-articles (info)
3398   "Set all article lists and remove all marks that are no longer legal."
3399   (let* ((marked-lists (gnus-info-marks info))
3400          (active (gnus-active (gnus-info-group info)))
3401          (min (car active))
3402          (max (cdr active))
3403          (types gnus-article-mark-lists)
3404          (uncompressed '(score bookmark killed))
3405          marks var articles article mark)
3406
3407     (while marked-lists
3408       (setq marks (pop marked-lists))
3409       (set (setq var (intern (format "gnus-newsgroup-%s"
3410                                      (car (rassq (setq mark (car marks))
3411                                                  types)))))
3412            (if (memq (car marks) uncompressed) (cdr marks)
3413              (gnus-uncompress-range (cdr marks))))
3414
3415       (setq articles (symbol-value var))
3416
3417       ;; All articles have to be subsets of the active articles.
3418       (cond
3419        ;; Adjust "simple" lists.
3420        ((memq mark '(tick dormant expirable reply save))
3421         (while articles
3422           (when (or (< (setq article (pop articles)) min) (> article max))
3423             (set var (delq article (symbol-value var))))))
3424        ;; Adjust assocs.
3425        ((memq mark uncompressed)
3426         (while articles
3427           (when (or (not (consp (setq article (pop articles))))
3428                     (< (car article) min)
3429                     (> (car article) max))
3430             (set var (delq article (symbol-value var))))))))))
3431
3432 (defun gnus-update-missing-marks (missing)
3433   "Go through the list of MISSING articles and remove them mark lists."
3434   (when missing
3435     (let ((types gnus-article-mark-lists)
3436           var m)
3437       ;; Go through all types.
3438       (while types
3439         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
3440         (when (symbol-value var)
3441           ;; This list has articles.  So we delete all missing articles
3442           ;; from it.
3443           (setq m missing)
3444           (while m
3445             (set var (delq (pop m) (symbol-value var)))))))))
3446
3447 (defun gnus-update-marks ()
3448   "Enter the various lists of marked articles into the newsgroup info list."
3449   (let ((types gnus-article-mark-lists)
3450         (info (gnus-get-info gnus-newsgroup-name))
3451         (uncompressed '(score bookmark killed))
3452         type list newmarked symbol)
3453     (when info
3454       ;; Add all marks lists that are non-nil to the list of marks lists.
3455       (while types
3456         (setq type (pop types))
3457         (when (setq list (symbol-value
3458                           (setq symbol
3459                                 (intern (format "gnus-newsgroup-%s"
3460                                                 (car type))))))
3461           (push (cons (cdr type)
3462                       (if (memq (cdr type) uncompressed) list
3463                         (gnus-compress-sequence 
3464                          (set symbol (sort list '<)) t)))
3465                 newmarked)))
3466
3467       ;; Enter these new marks into the info of the group.
3468       (if (nthcdr 3 info)
3469           (setcar (nthcdr 3 info) newmarked)
3470         ;; Add the marks lists to the end of the info.
3471         (when newmarked
3472           (setcdr (nthcdr 2 info) (list newmarked))))
3473
3474       ;; Cut off the end of the info if there's nothing else there.
3475       (let ((i 5))
3476         (while (and (> i 2)
3477                     (not (nth i info)))
3478           (when (nthcdr (decf i) info)
3479             (setcdr (nthcdr i info) nil)))))))
3480
3481 (defun gnus-set-mode-line (where)
3482   "This function sets the mode line of the article or summary buffers.
3483 If WHERE is `summary', the summary mode line format will be used."
3484   ;; Is this mode line one we keep updated?
3485   (when (memq where gnus-updated-mode-lines)
3486     (let (mode-string)
3487       (save-excursion
3488         ;; We evaluate this in the summary buffer since these
3489         ;; variables are buffer-local to that buffer.
3490         (set-buffer gnus-summary-buffer)
3491         ;; We bind all these variables that are used in the `eval' form
3492         ;; below.
3493         (let* ((mformat (symbol-value
3494                          (intern
3495                           (format "gnus-%s-mode-line-format-spec" where))))
3496                (gnus-tmp-group-name gnus-newsgroup-name)
3497                (gnus-tmp-article-number (or gnus-current-article 0))
3498                (gnus-tmp-unread gnus-newsgroup-unreads)
3499                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
3500                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
3501                (gnus-tmp-unread-and-unselected
3502                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
3503                             (zerop gnus-tmp-unselected)) "")
3504                       ((zerop gnus-tmp-unselected)
3505                        (format "{%d more}" gnus-tmp-unread-and-unticked))
3506                       (t (format "{%d(+%d) more}"
3507                                  gnus-tmp-unread-and-unticked
3508                                  gnus-tmp-unselected))))
3509                (gnus-tmp-subject
3510                 (if (and gnus-current-headers
3511                          (vectorp gnus-current-headers))
3512                     (gnus-mode-string-quote
3513                      (mail-header-subject gnus-current-headers)) ""))
3514                max-len
3515                gnus-tmp-header);; passed as argument to any user-format-funcs
3516           (setq mode-string (eval mformat))
3517           (setq max-len (max 4 (if gnus-mode-non-string-length
3518                                    (- (window-width)
3519                                       gnus-mode-non-string-length)
3520                                  (length mode-string))))
3521           ;; We might have to chop a bit of the string off...
3522           (when (> (length mode-string) max-len)
3523             (setq mode-string
3524                   (concat (gnus-truncate-string mode-string (- max-len 3))
3525                           "...")))
3526           ;; Pad the mode string a bit.
3527           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
3528       ;; Update the mode line.
3529       (setq mode-line-buffer-identification 
3530             (gnus-mode-line-buffer-identification
3531              (list mode-string)))
3532       (set-buffer-modified-p t))))
3533
3534 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
3535   "Go through the HEADERS list and add all Xrefs to a hash table.
3536 The resulting hash table is returned, or nil if no Xrefs were found."
3537   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
3538          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
3539          (xref-hashtb (make-vector 63 0))
3540          start group entry number xrefs header)
3541     (while headers
3542       (setq header (pop headers))
3543       (when (and (setq xrefs (mail-header-xref header))
3544                  (not (memq (setq number (mail-header-number header))
3545                             unreads)))
3546         (setq start 0)
3547         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
3548           (setq start (match-end 0))
3549           (setq group (if prefix
3550                           (concat prefix (substring xrefs (match-beginning 1)
3551                                                     (match-end 1)))
3552                         (substring xrefs (match-beginning 1) (match-end 1))))
3553           (setq number
3554                 (string-to-int (substring xrefs (match-beginning 2)
3555                                           (match-end 2))))
3556           (if (setq entry (gnus-gethash group xref-hashtb))
3557               (setcdr entry (cons number (cdr entry)))
3558             (gnus-sethash group (cons number nil) xref-hashtb)))))
3559     (and start xref-hashtb)))
3560
3561 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
3562   "Look through all the headers and mark the Xrefs as read."
3563   (let ((virtual (gnus-virtual-group-p from-newsgroup))
3564         name entry info xref-hashtb idlist method nth4)
3565     (save-excursion
3566       (set-buffer gnus-group-buffer)
3567       (when (setq xref-hashtb
3568                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
3569         (mapatoms
3570          (lambda (group)
3571            (unless (string= from-newsgroup (setq name (symbol-name group)))
3572              (setq idlist (symbol-value group))
3573              ;; Dead groups are not updated.
3574              (and (prog1
3575                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
3576                             info (nth 2 entry))
3577                     (if (stringp (setq nth4 (gnus-info-method info)))
3578                         (setq nth4 (gnus-server-to-method nth4))))
3579                   ;; Only do the xrefs if the group has the same
3580                   ;; select method as the group we have just read.
3581                   (or (gnus-methods-equal-p
3582                        nth4 (gnus-find-method-for-group from-newsgroup))
3583                       virtual
3584                       (equal nth4 (setq method (gnus-find-method-for-group
3585                                                 from-newsgroup)))
3586                       (and (equal (car nth4) (car method))
3587                            (equal (nth 1 nth4) (nth 1 method))))
3588                   gnus-use-cross-reference
3589                   (or (not (eq gnus-use-cross-reference t))
3590                       virtual
3591                       ;; Only do cross-references on subscribed
3592                       ;; groups, if that is what is wanted.
3593                       (<= (gnus-info-level info) gnus-level-subscribed))
3594                   (gnus-group-make-articles-read name idlist))))
3595          xref-hashtb)))))
3596
3597 (defun gnus-group-make-articles-read (group articles)
3598   "Update the info of GROUP to say that only ARTICLES are unread."
3599   (let* ((num 0)
3600          (entry (gnus-gethash group gnus-newsrc-hashtb))
3601          (info (nth 2 entry))
3602          (active (gnus-active group))
3603          range)
3604     ;; First peel off all illegal article numbers.
3605     (when active
3606       (let ((ids articles)
3607             id first)
3608         (while (setq id (pop ids))
3609           (when (and first (> id (cdr active)))
3610             ;; We'll end up in this situation in one particular
3611             ;; obscure situation.  If you re-scan a group and get
3612             ;; a new article that is cross-posted to a different
3613             ;; group that has not been re-scanned, you might get
3614             ;; crossposted article that has a higher number than
3615             ;; Gnus believes possible.  So we re-activate this
3616             ;; group as well.  This might mean doing the
3617             ;; crossposting thingy will *increase* the number
3618             ;; of articles in some groups.  Tsk, tsk.
3619             (setq active (or (gnus-activate-group group) active)))
3620           (when (or (> id (cdr active))
3621                     (< id (car active)))
3622             (setq articles (delq id articles))))))
3623     (gnus-undo-register
3624       `(progn
3625          (gnus-info-set-marks ,info ,(gnus-info-marks info))
3626          (gnus-info-set-read ,info ,(gnus-info-read info))
3627          (gnus-group-update-group group t)))
3628     ;; If the read list is nil, we init it.
3629     (and active
3630          (null (gnus-info-read info))
3631          (> (car active) 1)
3632          (gnus-info-set-read info (cons 1 (1- (car active)))))
3633     ;; Then we add the read articles to the range.
3634     (gnus-info-set-read
3635      info
3636      (setq range
3637            (gnus-add-to-range
3638             (gnus-info-read info) (setq articles (sort articles '<)))))
3639     ;; Then we have to re-compute how many unread
3640     ;; articles there are in this group.
3641     (when active
3642       (cond
3643        ((not range)
3644         (setq num (- (1+ (cdr active)) (car active))))
3645        ((not (listp (cdr range)))
3646         (setq num (- (cdr active) (- (1+ (cdr range))
3647                                      (car range)))))
3648        (t
3649         (while range
3650           (if (numberp (car range))
3651               (setq num (1+ num))
3652             (setq num (+ num (- (1+ (cdar range)) (caar range)))))
3653           (setq range (cdr range)))
3654         (setq num (- (cdr active) num))))
3655       ;; Update the number of unread articles.
3656       (setcar entry num)
3657       ;; Update the group buffer.
3658       (gnus-group-update-group group t))))
3659
3660 (defun gnus-methods-equal-p (m1 m2)
3661   (let ((m1 (or m1 gnus-select-method))
3662         (m2 (or m2 gnus-select-method)))
3663     (or (equal m1 m2)
3664         (and (eq (car m1) (car m2))
3665              (or (not (memq 'address (assoc (symbol-name (car m1))
3666                                             gnus-valid-select-methods)))
3667                  (equal (nth 1 m1) (nth 1 m2)))))))
3668
3669 (defsubst gnus-header-value ()
3670   (buffer-substring (match-end 0) (gnus-point-at-eol)))
3671
3672 (defvar gnus-newsgroup-none-id 0)
3673
3674 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
3675   (let ((cur nntp-server-buffer)
3676         (dependencies
3677          (or dependencies
3678              (save-excursion (set-buffer gnus-summary-buffer)
3679                              gnus-newsgroup-dependencies)))
3680         headers id id-dep ref-dep end ref)
3681     (save-excursion
3682       (set-buffer nntp-server-buffer)
3683       ;; Translate all TAB characters into SPACE characters.
3684       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
3685       (run-hooks 'gnus-parse-headers-hook)
3686       (let ((case-fold-search t)
3687             in-reply-to header p lines)
3688         (goto-char (point-min))
3689         ;; Search to the beginning of the next header.  Error messages
3690         ;; do not begin with 2 or 3.
3691         (while (re-search-forward "^[23][0-9]+ " nil t)
3692           (setq id nil
3693                 ref nil)
3694           ;; This implementation of this function, with nine
3695           ;; search-forwards instead of the one re-search-forward and
3696           ;; a case (which basically was the old function) is actually
3697           ;; about twice as fast, even though it looks messier.  You
3698           ;; can't have everything, I guess.  Speed and elegance
3699           ;; doesn't always go hand in hand.
3700           (setq
3701            header
3702            (vector
3703             ;; Number.
3704             (prog1
3705                 (read cur)
3706               (end-of-line)
3707               (setq p (point))
3708               (narrow-to-region (point)
3709                                 (or (and (search-forward "\n.\n" nil t)
3710                                          (- (point) 2))
3711                                     (point))))
3712             ;; Subject.
3713             (progn
3714               (goto-char p)
3715               (if (search-forward "\nsubject: " nil t)
3716                   (gnus-header-value) "(none)"))
3717             ;; From.
3718             (progn
3719               (goto-char p)
3720               (if (search-forward "\nfrom: " nil t)
3721                   (gnus-header-value) "(nobody)"))
3722             ;; Date.
3723             (progn
3724               (goto-char p)
3725               (if (search-forward "\ndate: " nil t)
3726                   (gnus-header-value) ""))
3727             ;; Message-ID.
3728             (progn
3729               (goto-char p)
3730               (if (search-forward "\nmessage-id: " nil t)
3731                   (setq id (gnus-header-value))
3732                 ;; If there was no message-id, we just fake one to make
3733                 ;; subsequent routines simpler.
3734                 (setq id (concat "none+"
3735                                  (int-to-string
3736                                   (setq gnus-newsgroup-none-id
3737                                         (1+ gnus-newsgroup-none-id)))))))
3738             ;; References.
3739             (progn
3740               (goto-char p)
3741               (if (search-forward "\nreferences: " nil t)
3742                   (progn
3743                     (setq end (point))
3744                     (prog1
3745                         (gnus-header-value)
3746                       (setq ref
3747                             (buffer-substring
3748                              (progn
3749                                (end-of-line)
3750                                (search-backward ">" end t)
3751                                (1+ (point)))
3752                              (progn
3753                                (search-backward "<" end t)
3754                                (point))))))
3755                 ;; Get the references from the in-reply-to header if there
3756                 ;; were no references and the in-reply-to header looks
3757                 ;; promising.
3758                 (if (and (search-forward "\nin-reply-to: " nil t)
3759                          (setq in-reply-to (gnus-header-value))
3760                          (string-match "<[^>]+>" in-reply-to))
3761                     (setq ref (substring in-reply-to (match-beginning 0)
3762                                          (match-end 0)))
3763                   (setq ref ""))))
3764             ;; Chars.
3765             0
3766             ;; Lines.
3767             (progn
3768               (goto-char p)
3769               (if (search-forward "\nlines: " nil t)
3770                   (if (numberp (setq lines (read cur)))
3771                       lines 0)
3772                 0))
3773             ;; Xref.
3774             (progn
3775               (goto-char p)
3776               (and (search-forward "\nxref: " nil t)
3777                    (gnus-header-value)))))
3778           (when (equal id ref)
3779             (setq ref nil))
3780           ;; We do the threading while we read the headers.  The
3781           ;; message-id and the last reference are both entered into
3782           ;; the same hash table.  Some tippy-toeing around has to be
3783           ;; done in case an article has arrived before the article
3784           ;; which it refers to.
3785           (if (boundp (setq id-dep (intern id dependencies)))
3786               (if (and (car (symbol-value id-dep))
3787                        (not force-new))
3788                   ;; An article with this Message-ID has already
3789                   ;; been seen, so we ignore this one, except we add
3790                   ;; any additional Xrefs (in case the two articles
3791                   ;; came from different servers).
3792                   (progn
3793                     (mail-header-set-xref
3794                      (car (symbol-value id-dep))
3795                      (concat (or (mail-header-xref
3796                                   (car (symbol-value id-dep))) "")
3797                              (or (mail-header-xref header) "")))
3798                     (setq header nil))
3799                 (setcar (symbol-value id-dep) header))
3800             (set id-dep (list header)))
3801           (when header
3802             (if (boundp (setq ref-dep (intern ref dependencies)))
3803                 (setcdr (symbol-value ref-dep)
3804                         (nconc (cdr (symbol-value ref-dep))
3805                                (list (symbol-value id-dep))))
3806               (set ref-dep (list nil (symbol-value id-dep))))
3807             (setq headers (cons header headers)))
3808           (goto-char (point-max))
3809           (widen))
3810         (nreverse headers)))))
3811
3812 ;; The following macros and functions were written by Felix Lee
3813 ;; <flee@cse.psu.edu>.
3814
3815 (defmacro gnus-nov-read-integer ()
3816   '(prog1
3817        (if (= (following-char) ?\t)
3818            0
3819          (let ((num (condition-case nil (read buffer) (error nil))))
3820            (if (numberp num) num 0)))
3821      (or (eobp) (forward-char 1))))
3822
3823 (defmacro gnus-nov-skip-field ()
3824   '(search-forward "\t" eol 'move))
3825
3826 (defmacro gnus-nov-field ()
3827   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3828
3829 ;; Goes through the xover lines and returns a list of vectors
3830 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
3831                                                   force-new dependencies)
3832   "Parse the news overview data in the server buffer, and return a
3833 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
3834   ;; Get the Xref when the users reads the articles since most/some
3835   ;; NNTP servers do not include Xrefs when using XOVER.
3836   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
3837   (let ((cur nntp-server-buffer)
3838         (dependencies (or dependencies gnus-newsgroup-dependencies))
3839         number headers header)
3840     (save-excursion
3841       (set-buffer nntp-server-buffer)
3842       ;; Allow the user to mangle the headers before parsing them.
3843       (run-hooks 'gnus-parse-headers-hook)
3844       (goto-char (point-min))
3845       (while (and sequence (not (eobp)))
3846         (setq number (read cur))
3847         (while (and sequence (< (car sequence) number))
3848           (setq sequence (cdr sequence)))
3849         (and sequence
3850              (eq number (car sequence))
3851              (progn
3852                (setq sequence (cdr sequence))
3853                (if (setq header
3854                          (inline (gnus-nov-parse-line
3855                                   number dependencies force-new)))
3856                    (setq headers (cons header headers)))))
3857         (forward-line 1))
3858       (setq headers (nreverse headers)))
3859     headers))
3860
3861 ;; This function has to be called with point after the article number
3862 ;; on the beginning of the line.
3863 (defun gnus-nov-parse-line (number dependencies &optional force-new)
3864   (let ((none 0)
3865         (eol (gnus-point-at-eol))
3866         (buffer (current-buffer))
3867         header ref id id-dep ref-dep)
3868
3869     ;; overview: [num subject from date id refs chars lines misc]
3870     (narrow-to-region (point) eol)
3871     (or (eobp) (forward-char))
3872
3873     (condition-case nil
3874         (setq header
3875               (vector
3876                number                   ; number
3877                (gnus-nov-field)         ; subject
3878                (gnus-nov-field)         ; from
3879                (gnus-nov-field)         ; date
3880                (setq id (or (gnus-nov-field)
3881                             (concat "none+"
3882                                     (int-to-string
3883                                      (setq none (1+ none)))))) ; id
3884                (progn
3885                  (save-excursion
3886                    (let ((beg (point)))
3887                      (search-forward "\t" eol)
3888                      (if (search-backward ">" beg t)
3889                          (setq ref
3890                                (buffer-substring
3891                                 (1+ (point))
3892                                 (search-backward "<" beg t)))
3893                        (setq ref nil))))
3894                  (gnus-nov-field))      ; refs
3895                (gnus-nov-read-integer)  ; chars
3896                (gnus-nov-read-integer)  ; lines
3897                (if (= (following-char) ?\n)
3898                    nil
3899                  (gnus-nov-field))      ; misc
3900                ))
3901       (error (progn
3902                (gnus-error 4 "Strange nov line")
3903                (setq header nil)
3904                (goto-char eol))))
3905
3906     (widen)
3907
3908     ;; We build the thread tree.
3909     (when header
3910       (when (equal id ref)
3911         ;; This article refers back to itself.  Naughty, naughty.
3912         (setq ref nil))
3913       (if (boundp (setq id-dep (intern id dependencies)))
3914           (if (and (car (symbol-value id-dep))
3915                    (not force-new))
3916               ;; An article with this Message-ID has already been seen,
3917               ;; so we ignore this one, except we add any additional
3918               ;; Xrefs (in case the two articles came from different
3919               ;; servers.
3920               (progn
3921                 (mail-header-set-xref
3922                  (car (symbol-value id-dep))
3923                  (concat (or (mail-header-xref
3924                               (car (symbol-value id-dep))) "")
3925                          (or (mail-header-xref header) "")))
3926                 (setq header nil))
3927             (setcar (symbol-value id-dep) header))
3928         (set id-dep (list header))))
3929     (when header
3930       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
3931           (setcdr (symbol-value ref-dep)
3932                   (nconc (cdr (symbol-value ref-dep))
3933                          (list (symbol-value id-dep))))
3934         (set ref-dep (list nil (symbol-value id-dep)))))
3935     header))
3936
3937 (defun gnus-article-get-xrefs ()
3938   "Fill in the Xref value in `gnus-current-headers', if necessary.
3939 This is meant to be called in `gnus-article-internal-prepare-hook'."
3940   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
3941                                  gnus-current-headers)))
3942     (or (not gnus-use-cross-reference)
3943         (not headers)
3944         (and (mail-header-xref headers)
3945              (not (string= (mail-header-xref headers) "")))
3946         (let ((case-fold-search t)
3947               xref)
3948           (save-restriction
3949             (nnheader-narrow-to-headers)
3950             (goto-char (point-min))
3951             (if (or (and (eq (downcase (following-char)) ?x)
3952                          (looking-at "Xref:"))
3953                     (search-forward "\nXref:" nil t))
3954                 (progn
3955                   (goto-char (1+ (match-end 0)))
3956                   (setq xref (buffer-substring (point)
3957                                                (progn (end-of-line) (point))))
3958                   (mail-header-set-xref headers xref))))))))
3959
3960 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
3961   "Find article ID and insert the summary line for that article."
3962   (let ((header (if (and old-header use-old-header)
3963                     old-header (gnus-read-header id)))
3964         (number (and (numberp id) id))
3965         pos)
3966     (when header
3967       ;; Rebuild the thread that this article is part of and go to the
3968       ;; article we have fetched.
3969       (when (and (not gnus-show-threads)
3970                  old-header)
3971         (when (setq pos (text-property-any
3972                          (point-min) (point-max) 'gnus-number 
3973                          (mail-header-number old-header)))
3974           (goto-char pos)
3975           (gnus-delete-line)
3976           (gnus-data-remove (mail-header-number old-header))))
3977       (when old-header
3978         (mail-header-set-number header (mail-header-number old-header)))
3979       (setq gnus-newsgroup-sparse
3980             (delq (setq number (mail-header-number header)) 
3981                   gnus-newsgroup-sparse))
3982       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
3983       (gnus-rebuild-thread (mail-header-id header))
3984       (gnus-summary-goto-subject number nil t))
3985     (when (and (numberp number)
3986                (> number 0))
3987       ;; We have to update the boundaries even if we can't fetch the
3988       ;; article if ID is a number -- so that the next `P' or `N'
3989       ;; command will fetch the previous (or next) article even
3990       ;; if the one we tried to fetch this time has been canceled.
3991       (and (> number gnus-newsgroup-end)
3992            (setq gnus-newsgroup-end number))
3993       (and (< number gnus-newsgroup-begin)
3994            (setq gnus-newsgroup-begin number))
3995       (setq gnus-newsgroup-unselected
3996             (delq number gnus-newsgroup-unselected)))
3997     ;; Report back a success?
3998     (and header (mail-header-number header))))
3999
4000 ;;; Process/prefix in the summary buffer
4001
4002 (defun gnus-summary-work-articles (n)
4003   "Return a list of articles to be worked upon.  The prefix argument,
4004 the list of process marked articles, and the current article will be
4005 taken into consideration."
4006   (cond
4007    (n
4008     ;; A numerical prefix has been given.
4009     (let ((backward (< n 0))
4010           (n (abs (prefix-numeric-value n)))
4011           articles article)
4012       (save-excursion
4013         (while
4014             (and (> n 0)
4015                  (push (setq article (gnus-summary-article-number))
4016                        articles)
4017                  (if backward
4018                      (gnus-summary-find-prev nil article)
4019                    (gnus-summary-find-next nil article)))
4020           (decf n)))
4021       (nreverse articles)))
4022    ((and (boundp 'transient-mark-mode)
4023          transient-mark-mode
4024          mark-active)
4025     ;; Work on the region between point and mark.
4026     (let ((max (max (point) (mark)))
4027           articles article)
4028       (save-excursion
4029         (goto-char (min (point) (mark)))
4030         (while
4031             (and
4032              (push (setq article (gnus-summary-article-number)) articles)
4033              (gnus-summary-find-next nil article)
4034              (< (point) max)))
4035         (nreverse articles))))
4036    (gnus-newsgroup-processable
4037     ;; There are process-marked articles present.
4038     ;; Save current state.
4039     (gnus-summary-save-process-mark)
4040     ;; Return the list.
4041     (reverse gnus-newsgroup-processable))
4042    (t
4043     ;; Just return the current article.
4044     (list (gnus-summary-article-number)))))
4045
4046 (defun gnus-summary-save-process-mark ()
4047   "Push the current set of process marked articles on the stack."
4048   (interactive)
4049   (push (copy-sequence gnus-newsgroup-processable)
4050         gnus-newsgroup-process-stack))
4051
4052 (defun gnus-summary-kill-process-mark ()
4053   "Push the current set of process marked articles on the stack and unmark."
4054   (interactive)
4055   (gnus-summary-save-process-mark)
4056   (gnus-summary-unmark-all-processable))
4057
4058 (defun gnus-summary-yank-process-mark ()
4059   "Pop the last process mark state off the stack and restore it."
4060   (interactive)
4061   (unless gnus-newsgroup-process-stack
4062     (error "Empty mark stack"))
4063   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4064
4065 (defun gnus-summary-process-mark-set (set)
4066   "Make SET into the current process marked articles."
4067   (gnus-summary-unmark-all-processable)
4068   (while set
4069     (gnus-summary-set-process-mark (pop set))))
4070
4071 ;;; Searching and stuff
4072
4073 (defun gnus-summary-search-group (&optional backward use-level)
4074   "Search for next unread newsgroup.
4075 If optional argument BACKWARD is non-nil, search backward instead."
4076   (save-excursion
4077     (set-buffer gnus-group-buffer)
4078     (if (gnus-group-search-forward
4079          backward nil (if use-level (gnus-group-group-level) nil))
4080         (gnus-group-group-name))))
4081
4082 (defun gnus-summary-best-group (&optional exclude-group)
4083   "Find the name of the best unread group.
4084 If EXCLUDE-GROUP, do not go to this group."
4085   (save-excursion
4086     (set-buffer gnus-group-buffer)
4087     (save-excursion
4088       (gnus-group-best-unread-group exclude-group))))
4089
4090 (defun gnus-summary-find-next (&optional unread article backward)
4091   (if backward (gnus-summary-find-prev)
4092     (let* ((dummy (gnus-summary-article-intangible-p))
4093            (article (or article (gnus-summary-article-number)))
4094            (arts (gnus-data-find-list article))
4095            result)
4096       (when (and (not dummy)
4097                  (or (not gnus-summary-check-current)
4098                      (not unread)
4099                      (not (gnus-data-unread-p (car arts)))))
4100         (setq arts (cdr arts)))
4101       (when (setq result
4102                   (if unread
4103                       (progn
4104                         (while arts
4105                           (when (gnus-data-unread-p (car arts))
4106                             (setq result (car arts)
4107                                   arts nil))
4108                           (setq arts (cdr arts)))
4109                         result)
4110                     (car arts)))
4111         (goto-char (gnus-data-pos result))
4112         (gnus-data-number result)))))
4113
4114 (defun gnus-summary-find-prev (&optional unread article)
4115   (let* ((eobp (eobp))
4116          (article (or article (gnus-summary-article-number)))
4117          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4118          result)
4119     (when (and (not eobp)
4120                (or (not gnus-summary-check-current)
4121                    (not unread)
4122                    (not (gnus-data-unread-p (car arts)))))
4123       (setq arts (cdr arts)))
4124     (if (setq result
4125               (if unread
4126                   (progn
4127                     (while arts
4128                       (and (gnus-data-unread-p (car arts))
4129                            (setq result (car arts)
4130                                  arts nil))
4131                       (setq arts (cdr arts)))
4132                     result)
4133                 (car arts)))
4134         (progn
4135           (goto-char (gnus-data-pos result))
4136           (gnus-data-number result)))))
4137
4138 (defun gnus-summary-find-subject (subject &optional unread backward article)
4139   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4140          (article (or article (gnus-summary-article-number)))
4141          (articles (gnus-data-list backward))
4142          (arts (gnus-data-find-list article articles))
4143          result)
4144     (when (or (not gnus-summary-check-current)
4145               (not unread)
4146               (not (gnus-data-unread-p (car arts))))
4147       (setq arts (cdr arts)))
4148     (while arts
4149       (and (or (not unread)
4150                (gnus-data-unread-p (car arts)))
4151            (vectorp (gnus-data-header (car arts)))
4152            (gnus-subject-equal
4153             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4154            (setq result (car arts)
4155                  arts nil))
4156       (setq arts (cdr arts)))
4157     (and result
4158          (goto-char (gnus-data-pos result))
4159          (gnus-data-number result))))
4160
4161 (defun gnus-summary-search-forward (&optional unread subject backward)
4162   "Search forward for an article.
4163 If UNREAD, look for unread articles.  If SUBJECT, look for
4164 articles with that subject.  If BACKWARD, search backward instead."
4165   (cond (subject (gnus-summary-find-subject subject unread backward))
4166         (backward (gnus-summary-find-prev unread))
4167         (t (gnus-summary-find-next unread))))
4168
4169 (defun gnus-recenter (&optional n)
4170   "Center point in window and redisplay frame.
4171 Also do horizontal recentering."
4172   (interactive "P")
4173   (when (and gnus-auto-center-summary
4174              (not (eq gnus-auto-center-summary 'vertical)))
4175     (gnus-horizontal-recenter))
4176   (recenter n))
4177
4178 (defun gnus-summary-recenter ()
4179   "Center point in the summary window.
4180 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4181 displayed, no centering will be performed."
4182   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4183   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4184   (let* ((top (cond ((< (window-height) 4) 0)
4185                     ((< (window-height) 7) 1)
4186                     (t 2)))
4187          (height (1- (window-height)))
4188          (bottom (save-excursion (goto-char (point-max))
4189                                  (forward-line (- height))
4190                                  (point)))
4191          (window (get-buffer-window (current-buffer))))
4192     ;; The user has to want it.
4193     (when gnus-auto-center-summary
4194       (when (get-buffer-window gnus-article-buffer)
4195        ;; Only do recentering when the article buffer is displayed,
4196        ;; Set the window start to either `bottom', which is the biggest
4197        ;; possible valid number, or the second line from the top,
4198        ;; whichever is the least.
4199        (set-window-start
4200         window (min bottom (save-excursion 
4201                              (forward-line (- top)) (point)))))
4202       ;; Do horizontal recentering while we're at it.
4203       (when (and (get-buffer-window (current-buffer) t)
4204                  (not (eq gnus-auto-center-summary 'vertical)))
4205         (let ((selected (selected-window)))
4206           (select-window (get-buffer-window (current-buffer) t))
4207           (gnus-summary-position-point)
4208           (gnus-horizontal-recenter)
4209           (select-window selected))))))
4210
4211 (defun gnus-summary-jump-to-group (newsgroup)
4212   "Move point to NEWSGROUP in group mode buffer."
4213   ;; Keep update point of group mode buffer if visible.
4214   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4215       (save-window-excursion
4216         ;; Take care of tree window mode.
4217         (if (get-buffer-window gnus-group-buffer)
4218             (pop-to-buffer gnus-group-buffer))
4219         (gnus-group-jump-to-group newsgroup))
4220     (save-excursion
4221       ;; Take care of tree window mode.
4222       (if (get-buffer-window gnus-group-buffer)
4223           (pop-to-buffer gnus-group-buffer)
4224         (set-buffer gnus-group-buffer))
4225       (gnus-group-jump-to-group newsgroup))))
4226
4227 ;; This function returns a list of article numbers based on the
4228 ;; difference between the ranges of read articles in this group and
4229 ;; the range of active articles.
4230 (defun gnus-list-of-unread-articles (group)
4231   (let* ((read (gnus-info-read (gnus-get-info group)))
4232          (active (or (gnus-active group) (gnus-activate-group group)))
4233          (last (cdr active))
4234          first nlast unread)
4235     ;; If none are read, then all are unread.
4236     (if (not read)
4237         (setq first (car active))
4238       ;; If the range of read articles is a single range, then the
4239       ;; first unread article is the article after the last read
4240       ;; article.  Sounds logical, doesn't it?
4241       (if (not (listp (cdr read)))
4242           (setq first (1+ (cdr read)))
4243         ;; `read' is a list of ranges.
4244         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
4245                                 (caar read))) 1)
4246             (setq first 1))
4247         (while read
4248           (if first
4249               (while (< first nlast)
4250                 (setq unread (cons first unread))
4251                 (setq first (1+ first))))
4252           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4253           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4254           (setq read (cdr read)))))
4255     ;; And add the last unread articles.
4256     (while (<= first last)
4257       (setq unread (cons first unread))
4258       (setq first (1+ first)))
4259     ;; Return the list of unread articles.
4260     (nreverse unread)))
4261
4262 (defun gnus-list-of-read-articles (group)
4263   "Return a list of unread, unticked and non-dormant articles."
4264   (let* ((info (gnus-get-info group))
4265          (marked (gnus-info-marks info))
4266          (active (gnus-active group)))
4267     (and info active
4268          (gnus-set-difference
4269           (gnus-sorted-complement
4270            (gnus-uncompress-range active)
4271            (gnus-list-of-unread-articles group))
4272           (append
4273            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4274            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4275
4276 ;; Various summary commands
4277
4278 (defun gnus-summary-universal-argument (arg)
4279   "Perform any operation on all articles that are process/prefixed."
4280   (interactive "P")
4281   (gnus-set-global-variables)
4282   (let ((articles (gnus-summary-work-articles arg))
4283         func article)
4284     (if (eq
4285          (setq
4286           func
4287           (key-binding
4288            (read-key-sequence
4289             (substitute-command-keys
4290              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4291              ))))
4292          'undefined)
4293         (gnus-error 1 "Undefined key")
4294       (save-excursion
4295         (while articles
4296           (gnus-summary-goto-subject (setq article (pop articles)))
4297           (command-execute func)
4298           (gnus-summary-remove-process-mark article)))))
4299   (gnus-summary-position-point))
4300
4301 (defun gnus-summary-toggle-truncation (&optional arg)
4302   "Toggle truncation of summary lines.
4303 With arg, turn line truncation on iff arg is positive."
4304   (interactive "P")
4305   (setq truncate-lines
4306         (if (null arg) (not truncate-lines)
4307           (> (prefix-numeric-value arg) 0)))
4308   (redraw-display))
4309
4310 (defun gnus-summary-reselect-current-group (&optional all rescan)
4311   "Exit and then reselect the current newsgroup.
4312 The prefix argument ALL means to select all articles."
4313   (interactive "P")
4314   (gnus-set-global-variables)
4315   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4316     (error "Ephemeral groups can't be reselected"))
4317   (let ((current-subject (gnus-summary-article-number))
4318         (group gnus-newsgroup-name))
4319     (setq gnus-newsgroup-begin nil)
4320     (gnus-summary-exit)
4321     ;; We have to adjust the point of group mode buffer because the
4322     ;; current point was moved to the next unread newsgroup by
4323     ;; exiting.
4324     (gnus-summary-jump-to-group group)
4325     (when rescan
4326       (save-excursion
4327         (gnus-group-get-new-news-this-group 1)))
4328     (gnus-group-read-group all t)
4329     (gnus-summary-goto-subject current-subject nil t)))
4330
4331 (defun gnus-summary-rescan-group (&optional all)
4332   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4333   (interactive "P")
4334   (gnus-summary-reselect-current-group all t))
4335
4336 (defun gnus-summary-update-info ()
4337   (let ((group gnus-newsgroup-name))
4338     (when gnus-newsgroup-kill-headers
4339       (setq gnus-newsgroup-killed
4340             (gnus-compress-sequence
4341              (nconc
4342               (gnus-set-sorted-intersection
4343                (gnus-uncompress-range gnus-newsgroup-killed)
4344                (setq gnus-newsgroup-unselected
4345                      (sort gnus-newsgroup-unselected '<)))
4346               (setq gnus-newsgroup-unreads
4347                     (sort gnus-newsgroup-unreads '<))) t)))
4348     (unless (listp (cdr gnus-newsgroup-killed))
4349       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
4350     (let ((headers gnus-newsgroup-headers))
4351       (run-hooks 'gnus-exit-group-hook)
4352       (unless gnus-save-score
4353         (setq gnus-newsgroup-scored nil))
4354       ;; Set the new ranges of read articles.
4355       (gnus-update-read-articles
4356        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
4357       ;; Set the current article marks.
4358       (gnus-update-marks)
4359       ;; Do the cross-ref thing.
4360       (when gnus-use-cross-reference
4361         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
4362       ;; Do adaptive scoring, and possibly save score files.
4363       (when gnus-newsgroup-adaptive
4364         (gnus-score-adaptive))
4365       (when gnus-use-scoring
4366         (gnus-score-save))
4367       ;; Do not switch windows but change the buffer to work.
4368       (set-buffer gnus-group-buffer)
4369       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
4370           (gnus-group-update-group group)))))
4371
4372 (defun gnus-summary-exit (&optional temporary)
4373   "Exit reading current newsgroup, and then return to group selection mode.
4374 gnus-exit-group-hook is called with no arguments if that value is non-nil."
4375   (interactive)
4376   (gnus-set-global-variables)
4377   (gnus-kill-save-kill-buffer)
4378   (let* ((group gnus-newsgroup-name)
4379          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
4380          (mode major-mode)
4381          (buf (current-buffer)))
4382     (run-hooks 'gnus-summary-prepare-exit-hook)
4383     ;; If we have several article buffers, we kill them at exit.
4384     (unless gnus-single-article-buffer
4385       (gnus-kill-buffer gnus-original-article-buffer)
4386       (setq gnus-article-current nil))
4387     (when gnus-use-cache
4388       (gnus-cache-possibly-remove-articles)
4389       (gnus-cache-save-buffers))
4390     (gnus-async-prefetch-remove-group group)
4391     (when gnus-suppress-duplicates
4392       (gnus-dup-enter-articles))
4393     (when gnus-use-trees
4394       (gnus-tree-close group))
4395     ;; Make all changes in this group permanent.
4396     (unless quit-config
4397       (gnus-summary-update-info))
4398     (gnus-close-group group)
4399     ;; Make sure where I was, and go to next newsgroup.
4400     (set-buffer gnus-group-buffer)
4401     (unless quit-config
4402       (gnus-group-jump-to-group group))
4403     (run-hooks 'gnus-summary-exit-hook)
4404     (unless quit-config
4405       (gnus-group-next-unread-group 1))
4406     (if temporary
4407         nil                             ;Nothing to do.
4408       ;; If we have several article buffers, we kill them at exit.
4409       (unless gnus-single-article-buffer
4410         (gnus-kill-buffer gnus-article-buffer)
4411         (gnus-kill-buffer gnus-original-article-buffer)
4412         (setq gnus-article-current nil))
4413       (set-buffer buf)
4414       (if (not gnus-kill-summary-on-exit)
4415           (gnus-deaden-summary)
4416         ;; We set all buffer-local variables to nil.  It is unclear why
4417         ;; this is needed, but if we don't, buffer-local variables are
4418         ;; not garbage-collected, it seems.  This would the lead to en
4419         ;; ever-growing Emacs.
4420         (gnus-summary-clear-local-variables)
4421         (when (get-buffer gnus-article-buffer)
4422           (bury-buffer gnus-article-buffer))
4423         ;; We clear the global counterparts of the buffer-local
4424         ;; variables as well, just to be on the safe side.
4425         (set-buffer gnus-group-buffer)
4426         (gnus-summary-clear-local-variables)
4427         ;; Return to group mode buffer.
4428         (if (eq mode 'gnus-summary-mode)
4429             (gnus-kill-buffer buf)))
4430       (setq gnus-current-select-method gnus-select-method)
4431       (pop-to-buffer gnus-group-buffer)
4432       ;; Clear the current group name.
4433       (if (not quit-config)
4434           (progn
4435             (gnus-group-jump-to-group group)
4436             (gnus-group-next-unread-group 1)
4437             (gnus-configure-windows 'group 'force))
4438         (if (not (buffer-name (car quit-config)))
4439             (gnus-configure-windows 'group 'force)
4440           (set-buffer (car quit-config))
4441           (cond ((eq major-mode 'gnus-summary-mode)
4442                  (gnus-set-global-variables))
4443                 ((eq major-mode 'gnus-article-mode)
4444                  (save-excursion
4445                    ;; The `gnus-summary-buffer' variable may point
4446                    ;; to the old summary buffer when using a single
4447                    ;; article buffer.
4448                    (unless (gnus-buffer-live-p gnus-summary-buffer)
4449                      (set-buffer gnus-group-buffer))
4450                    (set-buffer gnus-summary-buffer)
4451                    (gnus-set-global-variables))))
4452           (gnus-configure-windows (cdr quit-config) 'force)))
4453       (unless quit-config
4454         (setq gnus-newsgroup-name nil)))))
4455
4456 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
4457 (defun gnus-summary-exit-no-update (&optional no-questions)
4458   "Quit reading current newsgroup without updating read article info."
4459   (interactive)
4460   (gnus-set-global-variables)
4461   (let* ((group gnus-newsgroup-name)
4462          (quit-config (gnus-group-quit-config group)))
4463     (when (or no-questions
4464               gnus-expert-user
4465               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
4466       ;; If we have several article buffers, we kill them at exit.
4467       (unless gnus-single-article-buffer
4468         (gnus-kill-buffer gnus-article-buffer)
4469         (gnus-kill-buffer gnus-original-article-buffer)
4470         (setq gnus-article-current nil))
4471       (if (not gnus-kill-summary-on-exit)
4472           (gnus-deaden-summary)
4473         (gnus-close-group group)
4474         (gnus-summary-clear-local-variables)
4475         (set-buffer gnus-group-buffer)
4476         (gnus-summary-clear-local-variables)
4477         (when (get-buffer gnus-summary-buffer)
4478           (kill-buffer gnus-summary-buffer)))
4479       (unless gnus-single-article-buffer
4480         (setq gnus-article-current nil))
4481       (when gnus-use-trees
4482         (gnus-tree-close group))
4483       (gnus-async-prefetch-remove-group group)
4484       (when (get-buffer gnus-article-buffer)
4485         (bury-buffer gnus-article-buffer))
4486       ;; Return to the group buffer.
4487       (gnus-configure-windows 'group 'force)
4488       ;; Clear the current group name.
4489       (setq gnus-newsgroup-name nil)
4490       (when (equal (gnus-group-group-name) group)
4491         (gnus-group-next-unread-group 1))
4492       (when quit-config
4493         (if (not (buffer-name (car quit-config)))
4494             (gnus-configure-windows 'group 'force)
4495           (set-buffer (car quit-config))
4496           (when (eq major-mode 'gnus-summary-mode)
4497             (gnus-set-global-variables))
4498           (gnus-configure-windows (cdr quit-config)))))))
4499
4500 ;;; Dead summaries.
4501
4502 (defvar gnus-dead-summary-mode-map nil)
4503
4504 (unless gnus-dead-summary-mode-map
4505   (setq gnus-dead-summary-mode-map (make-keymap))
4506   (suppress-keymap gnus-dead-summary-mode-map)
4507   (substitute-key-definition
4508    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
4509   (let ((keys '("\C-d" "\r" "\177")))
4510     (while keys
4511       (define-key gnus-dead-summary-mode-map
4512         (pop keys) 'gnus-summary-wake-up-the-dead))))
4513
4514 (defvar gnus-dead-summary-mode nil
4515   "Minor mode for Gnus summary buffers.")
4516
4517 (defun gnus-dead-summary-mode (&optional arg)
4518   "Minor mode for Gnus summary buffers."
4519   (interactive "P")
4520   (when (eq major-mode 'gnus-summary-mode)
4521     (make-local-variable 'gnus-dead-summary-mode)
4522     (setq gnus-dead-summary-mode
4523           (if (null arg) (not gnus-dead-summary-mode)
4524             (> (prefix-numeric-value arg) 0)))
4525     (when gnus-dead-summary-mode
4526       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
4527         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
4528       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
4529         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
4530               minor-mode-map-alist)))))
4531
4532 (defun gnus-deaden-summary ()
4533   "Make the current summary buffer into a dead summary buffer."
4534   ;; Kill any previous dead summary buffer.
4535   (when (and gnus-dead-summary
4536              (buffer-name gnus-dead-summary))
4537     (save-excursion
4538       (set-buffer gnus-dead-summary)
4539       (when gnus-dead-summary-mode
4540         (kill-buffer (current-buffer)))))
4541   ;; Make this the current dead summary.
4542   (setq gnus-dead-summary (current-buffer))
4543   (gnus-dead-summary-mode 1)
4544   (let ((name (buffer-name)))
4545     (when (string-match "Summary" name)
4546       (rename-buffer
4547        (concat (substring name 0 (match-beginning 0)) "Dead "
4548                (substring name (match-beginning 0))) t))))
4549
4550 (defun gnus-kill-or-deaden-summary (buffer)
4551   "Kill or deaden the summary BUFFER."
4552   (when (and (buffer-name buffer)
4553              (not gnus-single-article-buffer))
4554     (save-excursion
4555       (set-buffer buffer)
4556       (gnus-kill-buffer gnus-article-buffer)
4557       (gnus-kill-buffer gnus-original-article-buffer)))
4558   (cond (gnus-kill-summary-on-exit
4559          (when (and gnus-use-trees
4560                     (and (get-buffer buffer)
4561                          (buffer-name (get-buffer buffer))))
4562            (save-excursion
4563              (set-buffer (get-buffer buffer))
4564              (gnus-tree-close gnus-newsgroup-name)))
4565          (gnus-kill-buffer buffer))
4566         ((and (get-buffer buffer)
4567               (buffer-name (get-buffer buffer)))
4568          (save-excursion
4569            (set-buffer buffer)
4570            (gnus-deaden-summary)))))
4571
4572 (defun gnus-summary-wake-up-the-dead (&rest args)
4573   "Wake up the dead summary buffer."
4574   (interactive)
4575   (gnus-dead-summary-mode -1)
4576   (let ((name (buffer-name)))
4577     (when (string-match "Dead " name)
4578       (rename-buffer
4579        (concat (substring name 0 (match-beginning 0))
4580                (substring name (match-end 0))) t)))
4581   (gnus-message 3 "This dead summary is now alive again"))
4582
4583 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
4584 (defun gnus-summary-fetch-faq (&optional faq-dir)
4585   "Fetch the FAQ for the current group.
4586 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
4587 in."
4588   (interactive
4589    (list
4590     (if current-prefix-arg
4591         (completing-read
4592          "Faq dir: " (and (listp gnus-group-faq-directory)
4593                           gnus-group-faq-directory)))))
4594   (let (gnus-faq-buffer)
4595     (and (setq gnus-faq-buffer
4596                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
4597          (gnus-configure-windows 'summary-faq))))
4598
4599 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4600 (defun gnus-summary-describe-group (&optional force)
4601   "Describe the current newsgroup."
4602   (interactive "P")
4603   (gnus-group-describe-group force gnus-newsgroup-name))
4604
4605 (defun gnus-summary-describe-briefly ()
4606   "Describe summary mode commands briefly."
4607   (interactive)
4608   (gnus-message 6
4609                 (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")))
4610
4611 ;; Walking around group mode buffer from summary mode.
4612
4613 (defun gnus-summary-next-group (&optional no-article target-group backward)
4614   "Exit current newsgroup and then select next unread newsgroup.
4615 If prefix argument NO-ARTICLE is non-nil, no article is selected
4616 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
4617 previous group instead."
4618   (interactive "P")
4619   (gnus-set-global-variables)
4620   (let ((current-group gnus-newsgroup-name)
4621         (current-buffer (current-buffer))
4622         entered)
4623     ;; First we semi-exit this group to update Xrefs and all variables.
4624     ;; We can't do a real exit, because the window conf must remain
4625     ;; the same in case the user is prompted for info, and we don't
4626     ;; want the window conf to change before that...
4627     (gnus-summary-exit t)
4628     (while (not entered)
4629       ;; Then we find what group we are supposed to enter.
4630       (set-buffer gnus-group-buffer)
4631       (gnus-group-jump-to-group current-group)
4632       (setq target-group
4633             (or target-group
4634                 (if (eq gnus-keep-same-level 'best)
4635                     (gnus-summary-best-group gnus-newsgroup-name)
4636                   (gnus-summary-search-group backward gnus-keep-same-level))))
4637       (if (not target-group)
4638           ;; There are no further groups, so we return to the group
4639           ;; buffer.
4640           (progn
4641             (gnus-message 5 "Returning to the group buffer")
4642             (setq entered t)
4643             (set-buffer current-buffer)
4644             (gnus-summary-exit)
4645             (run-hooks 'gnus-group-no-more-groups-hook))
4646         ;; We try to enter the target group.
4647         (gnus-group-jump-to-group target-group)
4648         (let ((unreads (gnus-group-group-unread)))
4649           (if (and (or (eq t unreads)
4650                        (and unreads (not (zerop unreads))))
4651                    (gnus-summary-read-group
4652                     target-group nil no-article current-buffer))
4653               (setq entered t)
4654             (setq current-group target-group
4655                   target-group nil)))))))
4656
4657 (defun gnus-summary-prev-group (&optional no-article)
4658   "Exit current newsgroup and then select previous unread newsgroup.
4659 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
4660   (interactive "P")
4661   (gnus-summary-next-group no-article nil t))
4662
4663 ;; Walking around summary lines.
4664
4665 (defun gnus-summary-first-subject (&optional unread)
4666   "Go to the first unread subject.
4667 If UNREAD is non-nil, go to the first unread article.
4668 Returns the article selected or nil if there are no unread articles."
4669   (interactive "P")
4670   (prog1
4671       (cond
4672        ;; Empty summary.
4673        ((null gnus-newsgroup-data)
4674         (gnus-message 3 "No articles in the group")
4675         nil)
4676        ;; Pick the first article.
4677        ((not unread)
4678         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
4679         (gnus-data-number (car gnus-newsgroup-data)))
4680        ;; No unread articles.
4681        ((null gnus-newsgroup-unreads)
4682         (gnus-message 3 "No more unread articles")
4683         nil)
4684        ;; Find the first unread article.
4685        (t
4686         (let ((data gnus-newsgroup-data))
4687           (while (and data
4688                       (not (gnus-data-unread-p (car data))))
4689             (setq data (cdr data)))
4690           (if data
4691               (progn
4692                 (goto-char (gnus-data-pos (car data)))
4693                 (gnus-data-number (car data)))))))
4694     (gnus-summary-position-point)))
4695
4696 (defun gnus-summary-next-subject (n &optional unread dont-display)
4697   "Go to next N'th summary line.
4698 If N is negative, go to the previous N'th subject line.
4699 If UNREAD is non-nil, only unread articles are selected.
4700 The difference between N and the actual number of steps taken is
4701 returned."
4702   (interactive "p")
4703   (let ((backward (< n 0))
4704         (n (abs n)))
4705     (while (and (> n 0)
4706                 (if backward
4707                     (gnus-summary-find-prev unread)
4708                   (gnus-summary-find-next unread)))
4709       (setq n (1- n)))
4710     (if (/= 0 n) (gnus-message 7 "No more%s articles"
4711                                (if unread " unread" "")))
4712     (unless dont-display
4713       (gnus-summary-recenter)
4714       (gnus-summary-position-point))
4715     n))
4716
4717 (defun gnus-summary-next-unread-subject (n)
4718   "Go to next N'th unread summary line."
4719   (interactive "p")
4720   (gnus-summary-next-subject n t))
4721
4722 (defun gnus-summary-prev-subject (n &optional unread)
4723   "Go to previous N'th summary line.
4724 If optional argument UNREAD is non-nil, only unread article is selected."
4725   (interactive "p")
4726   (gnus-summary-next-subject (- n) unread))
4727
4728 (defun gnus-summary-prev-unread-subject (n)
4729   "Go to previous N'th unread summary line."
4730   (interactive "p")
4731   (gnus-summary-next-subject (- n) t))
4732
4733 (defun gnus-summary-goto-subject (article &optional force silent)
4734   "Go the subject line of ARTICLE.
4735 If FORCE, also allow jumping to articles not currently shown."
4736   (let ((b (point))
4737         (data (gnus-data-find article)))
4738     ;; We read in the article if we have to.
4739     (and (not data)
4740          force
4741          (gnus-summary-insert-subject article (and (vectorp force) force) t)
4742          (setq data (gnus-data-find article)))
4743     (goto-char b)
4744     (if (not data)
4745         (progn
4746           (unless silent
4747             (gnus-message 3 "Can't find article %d" article))
4748           nil)
4749       (goto-char (gnus-data-pos data))
4750       article)))
4751
4752 ;; Walking around summary lines with displaying articles.
4753
4754 (defun gnus-summary-expand-window (&optional arg)
4755   "Make the summary buffer take up the entire Emacs frame.
4756 Given a prefix, will force an `article' buffer configuration."
4757   (interactive "P")
4758   (gnus-set-global-variables)
4759   (if arg
4760       (gnus-configure-windows 'article 'force)
4761     (gnus-configure-windows 'summary 'force)))
4762
4763 (defun gnus-summary-display-article (article &optional all-header)
4764   "Display ARTICLE in article buffer."
4765   (gnus-set-global-variables)
4766   (if (null article)
4767       nil
4768     (prog1
4769         (if gnus-summary-display-article-function
4770             (funcall gnus-summary-display-article-function article all-header)
4771           (gnus-article-prepare article all-header))
4772       (run-hooks 'gnus-select-article-hook)
4773       (when (and gnus-current-article
4774                  (not (zerop gnus-current-article)))
4775         (gnus-summary-goto-subject gnus-current-article))
4776       (gnus-summary-recenter)
4777       (when gnus-use-trees
4778         (gnus-possibly-generate-tree article)
4779         (gnus-highlight-selected-tree article))
4780       ;; Successfully display article.
4781       (gnus-article-set-window-start
4782        (cdr (assq article gnus-newsgroup-bookmarks))))))
4783
4784 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
4785   "Select the current article.
4786 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
4787 non-nil, the article will be re-fetched even if it already present in
4788 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
4789 be displayed."
4790   ;; Make sure we are in the summary buffer to work around bbdb bug.
4791   (unless (eq major-mode 'gnus-summary-mode)
4792     (set-buffer gnus-summary-buffer))
4793   (let ((article (or article (gnus-summary-article-number)))
4794         (all-headers (not (not all-headers))) ;Must be T or NIL.
4795         gnus-summary-display-article-function
4796         did)
4797     (and (not pseudo)
4798          (gnus-summary-article-pseudo-p article)
4799          (error "This is a pseudo-article."))
4800     (prog1
4801         (save-excursion
4802           (set-buffer gnus-summary-buffer)
4803           (if (or (and gnus-single-article-buffer
4804                        (or (null gnus-current-article)
4805                            (null gnus-article-current)
4806                            (null (get-buffer gnus-article-buffer))
4807                            (not (eq article (cdr gnus-article-current)))
4808                            (not (equal (car gnus-article-current)
4809                                        gnus-newsgroup-name))))
4810                   (and (not gnus-single-article-buffer)
4811                        (or (null gnus-current-article)
4812                            (not (eq gnus-current-article article))))
4813                   force)
4814               ;; The requested article is different from the current article.
4815               (prog1
4816                   (gnus-summary-display-article article all-headers)
4817                 (setq did article))
4818             (if (or all-headers gnus-show-all-headers)
4819                 (gnus-article-show-all-headers))
4820             'old))
4821       (if did
4822           (gnus-article-set-window-start
4823            (cdr (assq article gnus-newsgroup-bookmarks)))))))
4824
4825 (defun gnus-summary-set-current-mark (&optional current-mark)
4826   "Obsolete function."
4827   nil)
4828
4829 (defun gnus-summary-next-article (&optional unread subject backward push)
4830   "Select the next article.
4831 If UNREAD, only unread articles are selected.
4832 If SUBJECT, only articles with SUBJECT are selected.
4833 If BACKWARD, the previous article is selected instead of the next."
4834   (interactive "P")
4835   (gnus-set-global-variables)
4836   (cond
4837    ;; Is there such an article?
4838    ((and (gnus-summary-search-forward unread subject backward)
4839          (or (gnus-summary-display-article (gnus-summary-article-number))
4840              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
4841     (gnus-summary-position-point))
4842    ;; If not, we try the first unread, if that is wanted.
4843    ((and subject
4844          gnus-auto-select-same
4845          ;; Make sure that we don't select the current article.
4846          (not (eq (gnus-summary-article-number)
4847                   (save-excursion
4848                     (gnus-summary-first-subject t)
4849                     (gnus-summary-article-number))))
4850          (gnus-summary-first-unread-article))
4851     (gnus-summary-position-point)
4852     (gnus-message 6 "Wrapped"))
4853    ;; Try to get next/previous article not displayed in this group.
4854    ((and gnus-auto-extend-newsgroup
4855          (not unread) (not subject))
4856     (gnus-summary-goto-article
4857      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
4858      nil t))
4859    ;; Go to next/previous group.
4860    (t
4861     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
4862         (gnus-summary-jump-to-group gnus-newsgroup-name))
4863     (let ((cmd last-command-char)
4864           (group
4865            (if (eq gnus-keep-same-level 'best)
4866                (gnus-summary-best-group gnus-newsgroup-name)
4867              (gnus-summary-search-group backward gnus-keep-same-level))))
4868       ;; For some reason, the group window gets selected.  We change
4869       ;; it back.
4870       (select-window (get-buffer-window (current-buffer)))
4871       ;; Select next unread newsgroup automagically.
4872       (cond
4873        ((or (not gnus-auto-select-next)
4874             (not cmd))
4875         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
4876        ((or (eq gnus-auto-select-next 'quietly)
4877             (and (eq gnus-auto-select-next 'slightly-quietly)
4878                  push)
4879             (and (eq gnus-auto-select-next 'almost-quietly)
4880                  (gnus-summary-last-article-p)))
4881         ;; Select quietly.
4882         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
4883             (gnus-summary-exit)
4884           (gnus-message 7 "No more%s articles (%s)..."
4885                         (if unread " unread" "")
4886                         (if group (concat "selecting " group)
4887                           "exiting"))
4888           (gnus-summary-next-group nil group backward)))
4889        (t
4890         (gnus-summary-walk-group-buffer
4891          gnus-newsgroup-name cmd unread backward)))))))
4892
4893 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
4894   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
4895                       (?\C-p (gnus-group-prev-unread-group 1))))
4896         (cursor-in-echo-area t)
4897         keve key group ended)
4898     (save-excursion
4899       (set-buffer gnus-group-buffer)
4900       (gnus-summary-jump-to-group from-group)
4901       (setq group
4902             (if (eq gnus-keep-same-level 'best)
4903                 (gnus-summary-best-group gnus-newsgroup-name)
4904               (gnus-summary-search-group backward gnus-keep-same-level))))
4905     (while (not ended)
4906       (gnus-message
4907        5 "No more%s articles%s" (if unread " unread" "")
4908        (if (and group
4909                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
4910            (format " (Type %s for %s [%s])"
4911                    (single-key-description cmd) group
4912                    (car (gnus-gethash group gnus-newsrc-hashtb)))
4913          (format " (Type %s to exit %s)"
4914                  (single-key-description cmd)
4915                  gnus-newsgroup-name)))
4916       ;; Confirm auto selection.
4917       (setq key (car (setq keve (gnus-read-event-char))))
4918       (setq ended t)
4919       (cond
4920        ((assq key keystrokes)
4921         (let ((obuf (current-buffer)))
4922           (switch-to-buffer gnus-group-buffer)
4923           (and group
4924                (gnus-group-jump-to-group group))
4925           (eval (cadr (assq key keystrokes)))
4926           (setq group (gnus-group-group-name))
4927           (switch-to-buffer obuf))
4928         (setq ended nil))
4929        ((equal key cmd)
4930         (if (or (not group)
4931                 (gnus-ephemeral-group-p gnus-newsgroup-name))
4932             (gnus-summary-exit)
4933           (gnus-summary-next-group nil group backward)))
4934        (t
4935         (push (cdr keve) unread-command-events))))))
4936
4937 (defun gnus-summary-next-unread-article ()
4938   "Select unread article after current one."
4939   (interactive)
4940   (gnus-summary-next-article t (and gnus-auto-select-same
4941                                     (gnus-summary-article-subject))))
4942
4943 (defun gnus-summary-prev-article (&optional unread subject)
4944   "Select the article after the current one.
4945 If UNREAD is non-nil, only unread articles are selected."
4946   (interactive "P")
4947   (gnus-summary-next-article unread subject t))
4948
4949 (defun gnus-summary-prev-unread-article ()
4950   "Select unread article before current one."
4951   (interactive)
4952   (gnus-summary-prev-article t (and gnus-auto-select-same
4953                                     (gnus-summary-article-subject))))
4954
4955 (defun gnus-summary-next-page (&optional lines circular)
4956   "Show next page of the selected article.
4957 If at the end of the current article, select the next article.
4958 LINES says how many lines should be scrolled up.
4959
4960 If CIRCULAR is non-nil, go to the start of the article instead of
4961 selecting the next article when reaching the end of the current
4962 article."
4963   (interactive "P")
4964   (setq gnus-summary-buffer (current-buffer))
4965   (gnus-set-global-variables)
4966   (let ((article (gnus-summary-article-number))
4967         (endp nil))
4968     (gnus-configure-windows 'article)
4969     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
4970         (if (and (eq gnus-summary-goto-unread 'never)
4971                  (not (gnus-summary-last-article-p article)))
4972             (gnus-summary-next-article)
4973           (gnus-summary-next-unread-article))
4974       (if (or (null gnus-current-article)
4975               (null gnus-article-current)
4976               (/= article (cdr gnus-article-current))
4977               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
4978           ;; Selected subject is different from current article's.
4979           (gnus-summary-display-article article)
4980         (gnus-eval-in-buffer-window gnus-article-buffer
4981           (setq endp (gnus-article-next-page lines)))
4982         (when endp
4983           (cond (circular
4984                  (gnus-summary-beginning-of-article))
4985                 (lines
4986                  (gnus-message 3 "End of message"))
4987                 ((null lines)
4988                  (if (and (eq gnus-summary-goto-unread 'never)
4989                           (not (gnus-summary-last-article-p article)))
4990                      (gnus-summary-next-article)
4991                    (gnus-summary-next-unread-article)))))))
4992     (gnus-summary-recenter)
4993     (gnus-summary-position-point)))
4994
4995 (defun gnus-summary-prev-page (&optional lines)
4996   "Show previous page of selected article.
4997 Argument LINES specifies lines to be scrolled down."
4998   (interactive "P")
4999   (gnus-set-global-variables)
5000   (let ((article (gnus-summary-article-number)))
5001     (gnus-configure-windows 'article)
5002     (if (or (null gnus-current-article)
5003             (null gnus-article-current)
5004             (/= article (cdr gnus-article-current))
5005             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5006         ;; Selected subject is different from current article's.
5007         (gnus-summary-display-article article)
5008       (gnus-summary-recenter)
5009       (gnus-eval-in-buffer-window gnus-article-buffer
5010         (gnus-article-prev-page lines))))
5011   (gnus-summary-position-point))
5012
5013 (defun gnus-summary-scroll-up (lines)
5014   "Scroll up (or down) one line current article.
5015 Argument LINES specifies lines to be scrolled up (or down if negative)."
5016   (interactive "p")
5017   (gnus-set-global-variables)
5018   (gnus-configure-windows 'article)
5019   (gnus-summary-show-thread)
5020   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5021     (gnus-eval-in-buffer-window gnus-article-buffer
5022       (cond ((> lines 0)
5023              (if (gnus-article-next-page lines)
5024                  (gnus-message 3 "End of message")))
5025             ((< lines 0)
5026              (gnus-article-prev-page (- lines))))))
5027   (gnus-summary-recenter)
5028   (gnus-summary-position-point))
5029
5030 (defun gnus-summary-next-same-subject ()
5031   "Select next article which has the same subject as current one."
5032   (interactive)
5033   (gnus-set-global-variables)
5034   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5035
5036 (defun gnus-summary-prev-same-subject ()
5037   "Select previous article which has the same subject as current one."
5038   (interactive)
5039   (gnus-set-global-variables)
5040   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5041
5042 (defun gnus-summary-next-unread-same-subject ()
5043   "Select next unread article which has the same subject as current one."
5044   (interactive)
5045   (gnus-set-global-variables)
5046   (gnus-summary-next-article t (gnus-summary-article-subject)))
5047
5048 (defun gnus-summary-prev-unread-same-subject ()
5049   "Select previous unread article which has the same subject as current one."
5050   (interactive)
5051   (gnus-set-global-variables)
5052   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5053
5054 (defun gnus-summary-first-unread-article ()
5055   "Select the first unread article.
5056 Return nil if there are no unread articles."
5057   (interactive)
5058   (gnus-set-global-variables)
5059   (prog1
5060       (when (gnus-summary-first-subject t)
5061         (gnus-summary-show-thread)
5062         (gnus-summary-first-subject t)
5063         (gnus-summary-display-article (gnus-summary-article-number)))
5064     (gnus-summary-position-point)))
5065
5066 (defun gnus-summary-best-unread-article ()
5067   "Select the unread article with the highest score."
5068   (interactive)
5069   (gnus-set-global-variables)
5070   (let ((best -1000000)
5071         (data gnus-newsgroup-data)
5072         article score)
5073     (while data
5074       (and (gnus-data-unread-p (car data))
5075            (> (setq score
5076                     (gnus-summary-article-score (gnus-data-number (car data))))
5077               best)
5078            (setq best score
5079                  article (gnus-data-number (car data))))
5080       (setq data (cdr data)))
5081     (prog1
5082         (if article
5083             (gnus-summary-goto-article article)
5084           (error "No unread articles"))
5085       (gnus-summary-position-point))))
5086
5087 (defun gnus-summary-last-subject ()
5088   "Go to the last displayed subject line in the group."
5089   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5090     (when article
5091       (gnus-summary-goto-subject article))))
5092
5093 (defun gnus-summary-goto-article (article &optional all-headers force)
5094   "Fetch ARTICLE and display it if it exists.
5095 If ALL-HEADERS is non-nil, no header lines are hidden."
5096   (interactive
5097    (list
5098     (string-to-int
5099      (completing-read
5100       "Article number: "
5101       (mapcar (lambda (number) (list (int-to-string number)))
5102               gnus-newsgroup-limit)))
5103     current-prefix-arg
5104     t))
5105   (prog1
5106       (if (gnus-summary-goto-subject article force)
5107           (gnus-summary-display-article article all-headers)
5108         (gnus-message 4 "Couldn't go to article %s" article) nil)
5109     (gnus-summary-position-point)))
5110
5111 (defun gnus-summary-goto-last-article ()
5112   "Go to the previously read article."
5113   (interactive)
5114   (prog1
5115       (and gnus-last-article
5116            (gnus-summary-goto-article gnus-last-article))
5117     (gnus-summary-position-point)))
5118
5119 (defun gnus-summary-pop-article (number)
5120   "Pop one article off the history and go to the previous.
5121 NUMBER articles will be popped off."
5122   (interactive "p")
5123   (let (to)
5124     (setq gnus-newsgroup-history
5125           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5126     (if to
5127         (gnus-summary-goto-article (car to))
5128       (error "Article history empty")))
5129   (gnus-summary-position-point))
5130
5131 ;; Summary commands and functions for limiting the summary buffer.
5132
5133 (defun gnus-summary-limit-to-articles (n)
5134   "Limit the summary buffer to the next N articles.
5135 If not given a prefix, use the process marked articles instead."
5136   (interactive "P")
5137   (gnus-set-global-variables)
5138   (prog1
5139       (let ((articles (gnus-summary-work-articles n)))
5140         (setq gnus-newsgroup-processable nil)
5141         (gnus-summary-limit articles))
5142     (gnus-summary-position-point)))
5143
5144 (defun gnus-summary-pop-limit (&optional total)
5145   "Restore the previous limit.
5146 If given a prefix, remove all limits."
5147   (interactive "P")
5148   (gnus-set-global-variables)
5149   (when total 
5150     (setq gnus-newsgroup-limits
5151           (list (mapcar (lambda (h) (mail-header-number h))
5152                         gnus-newsgroup-headers))))
5153   (unless gnus-newsgroup-limits
5154     (error "No limit to pop"))
5155   (prog1
5156       (gnus-summary-limit nil 'pop)
5157     (gnus-summary-position-point)))
5158
5159 (defun gnus-summary-limit-to-subject (subject &optional header)
5160   "Limit the summary buffer to articles that have subjects that match a regexp."
5161   (interactive "sRegexp: ")
5162   (unless header
5163     (setq header "subject"))
5164   (when (not (equal "" subject))
5165     (prog1
5166         (let ((articles (gnus-summary-find-matching
5167                          (or header "subject") subject 'all)))
5168           (or articles (error "Found no matches for \"%s\"" subject))
5169           (gnus-summary-limit articles))
5170       (gnus-summary-position-point))))
5171
5172 (defun gnus-summary-limit-to-author (from)
5173   "Limit the summary buffer to articles that have authors that match a regexp."
5174   (interactive "sRegexp: ")
5175   (gnus-summary-limit-to-subject from "from"))
5176
5177 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5178 (make-obsolete
5179  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5180
5181 (defun gnus-summary-limit-to-unread (&optional all)
5182   "Limit the summary buffer to articles that are not marked as read.
5183 If ALL is non-nil, limit strictly to unread articles."
5184   (interactive "P")
5185   (if all
5186       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5187     (gnus-summary-limit-to-marks
5188      ;; Concat all the marks that say that an article is read and have
5189      ;; those removed.
5190      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5191            gnus-killed-mark gnus-kill-file-mark
5192            gnus-low-score-mark gnus-expirable-mark
5193            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5194            gnus-duplicate-mark)
5195      'reverse)))
5196
5197 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
5198 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
5199
5200 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5201   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5202 If REVERSE, limit the summary buffer to articles that are not marked
5203 with MARKS.  MARKS can either be a string of marks or a list of marks.
5204 Returns how many articles were removed."
5205   (interactive "sMarks: ")
5206   (gnus-set-global-variables)
5207   (prog1
5208       (let ((data gnus-newsgroup-data)
5209             (marks (if (listp marks) marks
5210                      (append marks nil))) ; Transform to list.
5211             articles)
5212         (while data
5213           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
5214                  (memq (gnus-data-mark (car data)) marks))
5215                (setq articles (cons (gnus-data-number (car data)) articles)))
5216           (setq data (cdr data)))
5217         (gnus-summary-limit articles))
5218     (gnus-summary-position-point)))
5219
5220 (defun gnus-summary-limit-to-score (&optional score)
5221   "Limit to articles with score at or above SCORE."
5222   (interactive "P")
5223   (gnus-set-global-variables)
5224   (setq score (if score
5225                   (prefix-numeric-value score)
5226                 (or gnus-summary-default-score 0)))
5227   (let ((data gnus-newsgroup-data)
5228         articles)
5229     (while data
5230       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5231                 score)
5232         (push (gnus-data-number (car data)) articles))
5233       (setq data (cdr data)))
5234     (prog1
5235         (gnus-summary-limit articles)
5236       (gnus-summary-position-point))))
5237
5238 (defun gnus-summary-limit-include-dormant ()
5239   "Display all the hidden articles that are marked as dormant."
5240   (interactive)
5241   (gnus-set-global-variables)
5242   (or gnus-newsgroup-dormant
5243       (error "There are no dormant articles in this group"))
5244   (prog1
5245       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5246     (gnus-summary-position-point)))
5247
5248 (defun gnus-summary-limit-exclude-dormant ()
5249   "Hide all dormant articles."
5250   (interactive)
5251   (gnus-set-global-variables)
5252   (prog1
5253       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5254     (gnus-summary-position-point)))
5255
5256 (defun gnus-summary-limit-exclude-childless-dormant ()
5257   "Hide all dormant articles that have no children."
5258   (interactive)
5259   (gnus-set-global-variables)
5260   (let ((data (gnus-data-list t))
5261         articles d children)
5262     ;; Find all articles that are either not dormant or have
5263     ;; children.
5264     (while (setq d (pop data))
5265       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
5266                 (and (setq children 
5267                            (gnus-article-children (gnus-data-number d)))
5268                      (let (found)
5269                        (while children
5270                          (when (memq (car children) articles)
5271                            (setq children nil
5272                                  found t))
5273                          (pop children))
5274                        found)))
5275         (push (gnus-data-number d) articles)))
5276     ;; Do the limiting.
5277     (prog1
5278         (gnus-summary-limit articles)
5279       (gnus-summary-position-point))))
5280
5281 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
5282   "Mark all unread excluded articles as read.
5283 If ALL, mark even excluded ticked and dormants as read."
5284   (interactive "P")
5285   (let ((articles (gnus-sorted-complement
5286                    (sort
5287                     (mapcar (lambda (h) (mail-header-number h))
5288                             gnus-newsgroup-headers)
5289                     '<)
5290                    (sort gnus-newsgroup-limit '<)))
5291         article)
5292     (setq gnus-newsgroup-unreads nil)
5293     (if all
5294         (setq gnus-newsgroup-dormant nil
5295               gnus-newsgroup-marked nil
5296               gnus-newsgroup-reads
5297               (nconc
5298                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
5299                gnus-newsgroup-reads))
5300       (while (setq article (pop articles))
5301         (unless (or (memq article gnus-newsgroup-dormant)
5302                     (memq article gnus-newsgroup-marked))
5303           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
5304
5305 (defun gnus-summary-limit (articles &optional pop)
5306   (if pop
5307       ;; We pop the previous limit off the stack and use that.
5308       (setq articles (car gnus-newsgroup-limits)
5309             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
5310     ;; We use the new limit, so we push the old limit on the stack.
5311     (setq gnus-newsgroup-limits
5312           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
5313   ;; Set the limit.
5314   (setq gnus-newsgroup-limit articles)
5315   (let ((total (length gnus-newsgroup-data))
5316         (data (gnus-data-find-list (gnus-summary-article-number)))
5317         (gnus-summary-mark-below nil)   ; Inhibit this.
5318         found)
5319     ;; This will do all the work of generating the new summary buffer
5320     ;; according to the new limit.
5321     (gnus-summary-prepare)
5322     ;; Hide any threads, possibly.
5323     (and gnus-show-threads
5324          gnus-thread-hide-subtree
5325          (gnus-summary-hide-all-threads))
5326     ;; Try to return to the article you were at, or one in the
5327     ;; neighborhood.
5328     (when data
5329       ;; We try to find some article after the current one.
5330       (while data
5331         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
5332           (setq data nil
5333                 found t))
5334         (setq data (cdr data))))
5335     (unless found
5336       ;; If there is no data, that means that we were after the last
5337       ;; article.  The same goes when we can't find any articles
5338       ;; after the current one.
5339       (goto-char (point-max))
5340       (gnus-summary-find-prev))
5341     ;; We return how many articles were removed from the summary
5342     ;; buffer as a result of the new limit.
5343     (- total (length gnus-newsgroup-data))))
5344
5345 (defsubst gnus-invisible-cut-children (threads)
5346   (let ((num 0))
5347     (while threads
5348       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
5349         (incf num))
5350       (pop threads))
5351     (< num 2)))
5352
5353 (defsubst gnus-cut-thread (thread)
5354   "Go forwards in the thread until we find an article that we want to display."
5355   (when (or (eq gnus-fetch-old-headers 'some)
5356             (eq gnus-build-sparse-threads 'some)
5357             (eq gnus-build-sparse-threads 'more))
5358     ;; Deal with old-fetched headers and sparse threads.
5359     (while (and
5360             thread
5361             (or
5362              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
5363              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
5364             (or (<= (length (cdr thread)) 1)
5365                 (gnus-invisible-cut-children (cdr thread))))
5366       (setq thread (cadr thread))))
5367   thread)
5368
5369 (defun gnus-cut-threads (threads)
5370   "Cut off all uninteresting articles from the beginning of threads."
5371   (when (or (eq gnus-fetch-old-headers 'some)
5372             (eq gnus-build-sparse-threads 'some)
5373             (eq gnus-build-sparse-threads 'more))
5374     (let ((th threads))
5375       (while th
5376         (setcar th (gnus-cut-thread (car th)))
5377         (setq th (cdr th)))))
5378   ;; Remove nixed out threads.
5379   (delq nil threads))
5380
5381 (defun gnus-summary-initial-limit (&optional show-if-empty)
5382   "Figure out what the initial limit is supposed to be on group entry.
5383 This entails weeding out unwanted dormants, low-scored articles,
5384 fetch-old-headers verbiage, and so on."
5385   ;; Most groups have nothing to remove.
5386   (if (or gnus-inhibit-limiting
5387           (and (null gnus-newsgroup-dormant)
5388                (not (eq gnus-fetch-old-headers 'some))
5389                (null gnus-summary-expunge-below)
5390                (not (eq gnus-build-sparse-threads 'some))
5391                (not (eq gnus-build-sparse-threads 'more))
5392                (null gnus-thread-expunge-below)
5393                (not gnus-use-nocem)))
5394       () ; Do nothing.
5395     (push gnus-newsgroup-limit gnus-newsgroup-limits)
5396     (setq gnus-newsgroup-limit nil)
5397     (mapatoms
5398      (lambda (node)
5399        (unless (car (symbol-value node))
5400          ;; These threads have no parents -- they are roots.
5401          (let ((nodes (cdr (symbol-value node)))
5402                thread)
5403            (while nodes
5404              (if (and gnus-thread-expunge-below
5405                       (< (gnus-thread-total-score (car nodes))
5406                          gnus-thread-expunge-below))
5407                  (gnus-expunge-thread (pop nodes))
5408                (setq thread (pop nodes))
5409                (gnus-summary-limit-children thread))))))
5410      gnus-newsgroup-dependencies)
5411     ;; If this limitation resulted in an empty group, we might
5412     ;; pop the previous limit and use it instead.
5413     (when (and (not gnus-newsgroup-limit)
5414                show-if-empty)
5415       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
5416     gnus-newsgroup-limit))
5417
5418 (defun gnus-summary-limit-children (thread)
5419   "Return 1 if this subthread is visible and 0 if it is not."
5420   ;; First we get the number of visible children to this thread.  This
5421   ;; is done by recursing down the thread using this function, so this
5422   ;; will really go down to a leaf article first, before slowly
5423   ;; working its way up towards the root.
5424   (when thread
5425     (let ((children
5426            (if (cdr thread)
5427                (apply '+ (mapcar 'gnus-summary-limit-children
5428                                  (cdr thread)))
5429              0))
5430           (number (mail-header-number (car thread)))
5431           score)
5432       (if (and
5433            (not (memq number gnus-newsgroup-marked))
5434            (or
5435             ;; If this article is dormant and has absolutely no visible
5436             ;; children, then this article isn't visible.
5437             (and (memq number gnus-newsgroup-dormant)
5438                  (= children 0))
5439             ;; If this is "fetch-old-headered" and there is only one
5440             ;; visible child (or less), then we don't want this article.
5441             (and (eq gnus-fetch-old-headers 'some)
5442                  (memq number gnus-newsgroup-ancient)
5443                  (zerop children))
5444             ;; If this is a sparsely inserted article with no children,
5445             ;; we don't want it.
5446             (and (eq gnus-build-sparse-threads 'some)
5447                  (memq number gnus-newsgroup-sparse)
5448                  (zerop children))
5449             ;; If we use expunging, and this article is really
5450             ;; low-scored, then we don't want this article.
5451             (when (and gnus-summary-expunge-below
5452                        (< (setq score
5453                                 (or (cdr (assq number gnus-newsgroup-scored))
5454                                     gnus-summary-default-score))
5455                           gnus-summary-expunge-below))
5456               ;; We increase the expunge-tally here, but that has
5457               ;; nothing to do with the limits, really.
5458               (incf gnus-newsgroup-expunged-tally)
5459               ;; We also mark as read here, if that's wanted.
5460               (when (and gnus-summary-mark-below
5461                          (< score gnus-summary-mark-below))
5462                 (setq gnus-newsgroup-unreads
5463                       (delq number gnus-newsgroup-unreads))
5464                 (if gnus-newsgroup-auto-expire
5465                     (push number gnus-newsgroup-expirable)
5466                   (push (cons number gnus-low-score-mark)
5467                         gnus-newsgroup-reads)))
5468               t)
5469             ;; Check NoCeM things.
5470             (and gnus-use-nocem
5471                  (gnus-nocem-unwanted-article-p
5472                   (mail-header-id (car thread))))))
5473           ;; Nope, invisible article.
5474           0
5475         ;; Ok, this article is to be visible, so we add it to the limit
5476         ;; and return 1.
5477         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
5478         1))))
5479
5480 (defun gnus-expunge-thread (thread)
5481   "Mark all articles in THREAD as read."
5482   (let* ((number (mail-header-number (car thread))))
5483     (incf gnus-newsgroup-expunged-tally)
5484     ;; We also mark as read here, if that's wanted.
5485     (setq gnus-newsgroup-unreads
5486           (delq number gnus-newsgroup-unreads))
5487     (if gnus-newsgroup-auto-expire
5488         (push number gnus-newsgroup-expirable)
5489       (push (cons number gnus-low-score-mark)
5490             gnus-newsgroup-reads)))
5491   ;; Go recursively through all subthreads.
5492   (mapcar 'gnus-expunge-thread (cdr thread)))
5493
5494 ;; Summary article oriented commands
5495
5496 (defun gnus-summary-refer-parent-article (n)
5497   "Refer parent article N times.
5498 If N is negative, go to ancestor -N instead.
5499 The difference between N and the number of articles fetched is returned."
5500   (interactive "p")
5501   (gnus-set-global-variables)
5502   (let ((skip 1)
5503         error header ref)
5504     (when (not (natnump n))
5505       (setq skip (abs n)
5506             n 1))
5507     (while (and (> n 0)
5508                 (not error))
5509       (setq header (gnus-summary-article-header))
5510       (setq ref
5511             ;; If we try to find the parent of the currently
5512             ;; displayed article, then we take a look at the actual
5513             ;; References header, since this is slightly more
5514             ;; reliable than the References field we got from the
5515             ;; server.
5516             (if (and (eq (mail-header-number header)
5517                          (cdr gnus-article-current))
5518                      (equal gnus-newsgroup-name
5519                             (car gnus-article-current)))
5520                 (save-excursion
5521                   (set-buffer gnus-original-article-buffer)
5522                   (nnheader-narrow-to-headers)
5523                   (prog1
5524                       (message-fetch-field "references")
5525                     (widen)))
5526               ;; It's not the current article, so we take a bet on
5527               ;; the value we got from the server.
5528               (mail-header-references header)))
5529       (if ref 
5530           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
5531             (gnus-message 1 "Couldn't find parent"))
5532         (gnus-message 1 "No references in article %d"
5533                       (gnus-summary-article-number))
5534         (setq error t))
5535       (decf n))
5536     (gnus-summary-position-point)
5537     n))
5538
5539 (defun gnus-summary-refer-references ()
5540   "Fetch all articles mentioned in the References header.
5541 Return how many articles were fetched."
5542   (interactive)
5543   (gnus-set-global-variables)
5544   (let ((ref (mail-header-references (gnus-summary-article-header)))
5545         (current (gnus-summary-article-number))
5546         (n 0))
5547     ;; For each Message-ID in the References header...
5548     (while (string-match "<[^>]*>" ref)
5549       (incf n)
5550       ;; ... fetch that article.
5551       (gnus-summary-refer-article
5552        (prog1 (match-string 0 ref)
5553          (setq ref (substring ref (match-end 0))))))
5554     (gnus-summary-goto-subject current)
5555     (gnus-summary-position-point)
5556     n))
5557
5558 (defun gnus-summary-refer-article (message-id)
5559   "Fetch an article specified by MESSAGE-ID."
5560   (interactive "sMessage-ID: ")
5561   (when (and (stringp message-id)
5562              (not (zerop (length message-id))))
5563     ;; Construct the correct Message-ID if necessary.
5564     ;; Suggested by tale@pawl.rpi.edu.
5565     (unless (string-match "^<" message-id)
5566       (setq message-id (concat "<" message-id)))
5567     (unless (string-match ">$" message-id)
5568       (setq message-id (concat message-id ">")))
5569     (let* ((header (gnus-id-to-header message-id))
5570            (sparse (and header
5571                         (memq (mail-header-number header)
5572                               gnus-newsgroup-sparse))))
5573       (if header
5574           (prog1
5575               ;; The article is present in the buffer, to we just go to it.
5576               (gnus-summary-goto-article 
5577                (mail-header-number header) nil header)
5578             (when sparse
5579               (gnus-summary-update-article (mail-header-number header))))
5580         ;; We fetch the article
5581         (let ((gnus-override-method 
5582                (and (gnus-news-group-p gnus-newsgroup-name)
5583                     gnus-refer-article-method))
5584               number)
5585           ;; Start the special refer-article method, if necessary.
5586           (when (and gnus-refer-article-method
5587                      (gnus-news-group-p gnus-newsgroup-name))
5588             (gnus-check-server gnus-refer-article-method))
5589           ;; Fetch the header, and display the article.
5590           (if (setq number (gnus-summary-insert-subject message-id))
5591               (gnus-summary-select-article nil nil nil number)
5592             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
5593
5594 (defun gnus-summary-enter-digest-group (&optional force)
5595   "Enter an nndoc group based on the current article.
5596 If FORCE, force a digest interpretation.  If not, try
5597 to guess what the document format is."
5598   (interactive "P")
5599   (gnus-set-global-variables)
5600   (let ((conf gnus-current-window-configuration))
5601     (save-excursion
5602       (gnus-summary-select-article))
5603     (setq gnus-current-window-configuration conf)
5604     (let* ((name (format "%s-%d"
5605                          (gnus-group-prefixed-name
5606                           gnus-newsgroup-name (list 'nndoc ""))
5607                          (save-excursion
5608                            (set-buffer gnus-summary-buffer)
5609                            gnus-current-article)))
5610            (ogroup gnus-newsgroup-name)
5611            (params (append (gnus-info-params (gnus-get-info ogroup))
5612                            (list (cons 'to-group ogroup))))
5613            (case-fold-search t)
5614            (buf (current-buffer))
5615            dig)
5616       (save-excursion
5617         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
5618         (insert-buffer-substring gnus-original-article-buffer)
5619         ;; Remove lines that may lead nndoc to misinterpret the
5620         ;; document type.
5621         (narrow-to-region
5622          (goto-char (point-min))
5623          (or (search-forward "\n\n" nil t) (point)))
5624         (goto-char (point-min))
5625         (delete-matching-lines "^\\(Path\\):\\|^From ")
5626         (widen))
5627       (unwind-protect
5628           (if (gnus-group-read-ephemeral-group
5629                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
5630                             (nndoc-article-type ,(if force 'digest 'guess))) t)
5631               ;; Make all postings to this group go to the parent group.
5632               (nconc (gnus-info-params (gnus-get-info name))
5633                      params)
5634             ;; Couldn't select this doc group.
5635             (switch-to-buffer buf)
5636             (gnus-set-global-variables)
5637             (gnus-configure-windows 'summary)
5638             (gnus-message 3 "Article couldn't be entered?"))
5639         (kill-buffer dig)))))
5640
5641 (defun gnus-summary-read-document (n)
5642   "Open a new group based on the current article(s).
5643 Obeys the standard process/prefix convention."
5644   (interactive "P")
5645   (let* ((articles (gnus-summary-work-articles n))
5646          (ogroup gnus-newsgroup-name)
5647          (params (append (gnus-info-params (gnus-get-info ogroup))
5648                          (list (cons 'to-group ogroup))))
5649          article group egroup groups vgroup)
5650     (while (setq article (pop articles))
5651       (setq group (format "%s-%d" gnus-newsgroup-name article))
5652       (gnus-summary-remove-process-mark article)
5653       (when (gnus-summary-display-article article)
5654         (save-excursion
5655           (nnheader-temp-write nil
5656             (insert-buffer-substring gnus-original-article-buffer)
5657             ;; Remove some headers that may lead nndoc to make
5658             ;; the wrong guess.
5659             (message-narrow-to-head)
5660             (goto-char (point-min))
5661             (delete-matching-lines "^\\(Path\\):\\|^From ")
5662             (widen)
5663             (if (setq egroup
5664                       (gnus-group-read-ephemeral-group
5665                        group `(nndoc ,group (nndoc-address ,(current-buffer))
5666                                      (nndoc-article-type guess))
5667                        t nil t))
5668                 (progn
5669                   ;; Make all postings to this group go to the parent group.
5670                   (nconc (gnus-info-params (gnus-get-info egroup))
5671                          params)
5672                   (push egroup groups))
5673               ;; Couldn't select this doc group.
5674               (gnus-error 3 "Article couldn't be entered"))))))
5675     ;; Now we have selected all the documents.
5676     (cond
5677      ((not groups)
5678       (error "None of the articles could be interpreted as documents"))
5679      ((gnus-group-read-ephemeral-group
5680        (setq vgroup (format
5681                      "%s-%s" gnus-newsgroup-name
5682                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
5683        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
5684        t
5685        (cons (current-buffer) 'summary)))
5686      (t
5687       (error "Couldn't select virtual nndoc group")))))
5688       
5689 (defun gnus-summary-isearch-article (&optional regexp-p)
5690   "Do incremental search forward on the current article.
5691 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
5692   (interactive "P")
5693   (gnus-set-global-variables)
5694   (gnus-summary-select-article)
5695   (gnus-configure-windows 'article)
5696   (gnus-eval-in-buffer-window gnus-article-buffer
5697     ;;(goto-char (point-min))
5698     (isearch-forward regexp-p)))
5699
5700 (defun gnus-summary-search-article-forward (regexp &optional backward)
5701   "Search for an article containing REGEXP forward.
5702 If BACKWARD, search backward instead."
5703   (interactive
5704    (list (read-string
5705           (format "Search article %s (regexp%s): "
5706                   (if current-prefix-arg "backward" "forward")
5707                   (if gnus-last-search-regexp
5708                       (concat ", default " gnus-last-search-regexp)
5709                     "")))
5710          current-prefix-arg))
5711   (gnus-set-global-variables)
5712   (if (string-equal regexp "")
5713       (setq regexp (or gnus-last-search-regexp ""))
5714     (setq gnus-last-search-regexp regexp))
5715   (if (gnus-summary-search-article regexp backward)
5716       (gnus-summary-show-thread)
5717     (error "Search failed: \"%s\"" regexp)))
5718
5719 (defun gnus-summary-search-article-backward (regexp)
5720   "Search for an article containing REGEXP backward."
5721   (interactive
5722    (list (read-string
5723           (format "Search article backward (regexp%s): "
5724                   (if gnus-last-search-regexp
5725                       (concat ", default " gnus-last-search-regexp)
5726                     "")))))
5727   (gnus-summary-search-article-forward regexp 'backward))
5728
5729 (defun gnus-summary-search-article (regexp &optional backward)
5730   "Search for an article containing REGEXP.
5731 Optional argument BACKWARD means do search for backward.
5732 `gnus-select-article-hook' is not called during the search."
5733   (let ((gnus-select-article-hook nil)  ;Disable hook.
5734         (gnus-article-display-hook nil)
5735         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
5736         (gnus-use-article-prefetch nil)
5737         (sum (current-buffer))
5738         (found nil)
5739         point)
5740     (gnus-save-hidden-threads
5741       (gnus-summary-select-article)
5742       (set-buffer gnus-article-buffer)
5743       (when backward
5744         (forward-line -1))
5745       (while (not found)
5746         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
5747         (if (if backward
5748                 (re-search-backward regexp nil t)
5749               (re-search-forward regexp nil t))
5750             ;; We found the regexp.
5751             (progn
5752               (setq found 'found)
5753               (beginning-of-line)
5754               (set-window-start
5755                (get-buffer-window (current-buffer))
5756                (point))
5757               (forward-line 1)
5758               (set-buffer sum)
5759               (setq point (point)))
5760           ;; We didn't find it, so we go to the next article.
5761           (set-buffer sum)
5762           (if (not (if backward (gnus-summary-find-prev)
5763                      (gnus-summary-find-next)))
5764               ;; No more articles.
5765               (setq found t)
5766             ;; Select the next article and adjust point.
5767             (gnus-summary-select-article)
5768             (set-buffer gnus-article-buffer)
5769             (widen)
5770             (goto-char (if backward (point-max) (point-min))))))
5771       (gnus-message 7 ""))
5772     ;; Return whether we found the regexp.
5773     (when (eq found 'found)
5774       (goto-char point)
5775       (gnus-summary-show-thread)
5776       (gnus-summary-goto-subject gnus-current-article)
5777       (gnus-summary-position-point)
5778       t)))
5779
5780 (defun gnus-summary-find-matching (header regexp &optional backward unread
5781                                           not-case-fold)
5782   "Return a list of all articles that match REGEXP on HEADER.
5783 The search stars on the current article and goes forwards unless
5784 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
5785 If UNREAD is non-nil, only unread articles will
5786 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
5787 in the comparisons."
5788   (let ((data (if (eq backward 'all) gnus-newsgroup-data
5789                 (gnus-data-find-list
5790                  (gnus-summary-article-number) (gnus-data-list backward))))
5791         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
5792         (case-fold-search (not not-case-fold))
5793         articles d)
5794     (or (fboundp (intern (concat "mail-header-" header)))
5795         (error "%s is not a valid header" header))
5796     (while data
5797       (setq d (car data))
5798       (and (or (not unread)             ; We want all articles...
5799                (gnus-data-unread-p d))  ; Or just unreads.
5800            (vectorp (gnus-data-header d)) ; It's not a pseudo.
5801            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
5802            (setq articles (cons (gnus-data-number d) articles))) ; Success!
5803       (setq data (cdr data)))
5804     (nreverse articles)))
5805
5806 (defun gnus-summary-execute-command (header regexp command &optional backward)
5807   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
5808 If HEADER is an empty string (or nil), the match is done on the entire
5809 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
5810   (interactive
5811    (list (let ((completion-ignore-case t))
5812            (completing-read
5813             "Header name: "
5814             (mapcar (lambda (string) (list string))
5815                     '("Number" "Subject" "From" "Lines" "Date"
5816                       "Message-ID" "Xref" "References" "Body"))
5817             nil 'require-match))
5818          (read-string "Regexp: ")
5819          (read-key-sequence "Command: ")
5820          current-prefix-arg))
5821   (when (equal header "Body")
5822     (setq header ""))
5823   (gnus-set-global-variables)
5824   ;; Hidden thread subtrees must be searched as well.
5825   (gnus-summary-show-all-threads)
5826   ;; We don't want to change current point nor window configuration.
5827   (save-excursion
5828     (save-window-excursion
5829       (gnus-message 6 "Executing %s..." (key-description command))
5830       ;; We'd like to execute COMMAND interactively so as to give arguments.
5831       (gnus-execute header regexp
5832                     `(call-interactively ',(key-binding command))
5833                     backward)
5834       (gnus-message 6 "Executing %s...done" (key-description command)))))
5835
5836 (defun gnus-summary-beginning-of-article ()
5837   "Scroll the article back to the beginning."
5838   (interactive)
5839   (gnus-set-global-variables)
5840   (gnus-summary-select-article)
5841   (gnus-configure-windows 'article)
5842   (gnus-eval-in-buffer-window gnus-article-buffer
5843     (widen)
5844     (goto-char (point-min))
5845     (and gnus-break-pages (gnus-narrow-to-page))))
5846
5847 (defun gnus-summary-end-of-article ()
5848   "Scroll to the end of the article."
5849   (interactive)
5850   (gnus-set-global-variables)
5851   (gnus-summary-select-article)
5852   (gnus-configure-windows 'article)
5853   (gnus-eval-in-buffer-window gnus-article-buffer
5854     (widen)
5855     (goto-char (point-max))
5856     (recenter -3)
5857     (and gnus-break-pages (gnus-narrow-to-page))))
5858
5859 (defun gnus-summary-show-article (&optional arg)
5860   "Force re-fetching of the current article.
5861 If ARG (the prefix) is non-nil, show the raw article without any
5862 article massaging functions being run."
5863   (interactive "P")
5864   (gnus-set-global-variables)
5865   (if (not arg)
5866       ;; Select the article the normal way.
5867       (gnus-summary-select-article nil 'force)
5868     ;; Bind the article treatment functions to nil.
5869     (let ((gnus-have-all-headers t)
5870           gnus-article-display-hook
5871           gnus-article-prepare-hook
5872           gnus-break-pages
5873           gnus-visual)
5874       (gnus-summary-select-article nil 'force)))
5875   (gnus-summary-goto-subject gnus-current-article)
5876 ;  (gnus-configure-windows 'article)
5877   (gnus-summary-position-point))
5878
5879 (defun gnus-summary-verbose-headers (&optional arg)
5880   "Toggle permanent full header display.
5881 If ARG is a positive number, turn header display on.
5882 If ARG is a negative number, turn header display off."
5883   (interactive "P")
5884   (gnus-set-global-variables)
5885   (setq gnus-show-all-headers
5886         (cond ((or (not (numberp arg))
5887                    (zerop arg))
5888                (not gnus-show-all-headers))
5889               ((natnump arg)
5890                t)))
5891   (gnus-summary-show-article))
5892
5893 (defun gnus-summary-toggle-header (&optional arg)
5894   "Show the headers if they are hidden, or hide them if they are shown.
5895 If ARG is a positive number, show the entire header.
5896 If ARG is a negative number, hide the unwanted header lines."
5897   (interactive "P")
5898   (gnus-set-global-variables)
5899   (save-excursion
5900     (set-buffer gnus-article-buffer)
5901     (let* ((buffer-read-only nil)
5902            (inhibit-point-motion-hooks t)
5903            (hidden (text-property-any
5904                     (goto-char (point-min)) (search-forward "\n\n")
5905                     'invisible t))
5906            e)
5907       (goto-char (point-min))
5908       (when (search-forward "\n\n" nil t)
5909         (delete-region (point-min) (1- (point))))
5910       (goto-char (point-min))
5911       (save-excursion
5912         (set-buffer gnus-original-article-buffer)
5913         (goto-char (point-min))
5914         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
5915       (insert-buffer-substring gnus-original-article-buffer 1 e)
5916       (let ((article-inhibit-hiding t))
5917         (run-hooks 'gnus-article-display-hook))
5918       (if (or (not hidden) (and (numberp arg) (< arg 0)))
5919           (gnus-article-hide-headers)))))
5920
5921 (defun gnus-summary-show-all-headers ()
5922   "Make all header lines visible."
5923   (interactive)
5924   (gnus-set-global-variables)
5925   (gnus-article-show-all-headers))
5926
5927 (defun gnus-summary-toggle-mime (&optional arg)
5928   "Toggle MIME processing.
5929 If ARG is a positive number, turn MIME processing on."
5930   (interactive "P")
5931   (gnus-set-global-variables)
5932   (setq gnus-show-mime
5933         (if (null arg) (not gnus-show-mime)
5934           (> (prefix-numeric-value arg) 0)))
5935   (gnus-summary-select-article t 'force))
5936
5937 (defun gnus-summary-caesar-message (&optional arg)
5938   "Caesar rotate the current article by 13.
5939 The numerical prefix specifies how manu places to rotate each letter
5940 forward."
5941   (interactive "P")
5942   (gnus-set-global-variables)
5943   (gnus-summary-select-article)
5944   (let ((mail-header-separator ""))
5945     (gnus-eval-in-buffer-window gnus-article-buffer
5946       (save-restriction
5947         (widen)
5948         (let ((start (window-start))
5949               buffer-read-only)
5950           (message-caesar-buffer-body arg)
5951           (set-window-start (get-buffer-window (current-buffer)) start))))))
5952
5953 (defun gnus-summary-stop-page-breaking ()
5954   "Stop page breaking in the current article."
5955   (interactive)
5956   (gnus-set-global-variables)
5957   (gnus-summary-select-article)
5958   (gnus-eval-in-buffer-window gnus-article-buffer
5959     (widen)))
5960
5961 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
5962   "Move the current article to a different newsgroup.
5963 If N is a positive number, move the N next articles.
5964 If N is a negative number, move the N previous articles.
5965 If N is nil and any articles have been marked with the process mark,
5966 move those articles instead.
5967 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5968 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5969 re-spool using this method.
5970
5971 For this function to work, both the current newsgroup and the
5972 newsgroup that you want to move to have to support the `request-move'
5973 and `request-accept' functions."
5974   (interactive "P")
5975   (unless action (setq action 'move))
5976   (gnus-set-global-variables)
5977   ;; Check whether the source group supports the required functions.
5978   (cond ((and (eq action 'move)
5979               (not (gnus-check-backend-function
5980                     'request-move-article gnus-newsgroup-name)))
5981          (error "The current group does not support article moving"))
5982         ((and (eq action 'crosspost)
5983               (not (gnus-check-backend-function
5984                     'request-replace-article gnus-newsgroup-name)))
5985          (error "The current group does not support article editing")))
5986   (let ((articles (gnus-summary-work-articles n))
5987         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
5988         (names '((move "Move" "Moving")
5989                  (copy "Copy" "Copying")
5990                  (crosspost "Crosspost" "Crossposting")))
5991         (copy-buf (save-excursion
5992                     (nnheader-set-temp-buffer " *copy article*")))
5993         art-group to-method new-xref article to-groups)
5994     (unless (assq action names)
5995       (error "Unknown action %s" action))
5996     ;; Read the newsgroup name.
5997     (when (and (not to-newsgroup)
5998                (not select-method))
5999       (setq to-newsgroup
6000             (gnus-read-move-group-name
6001              (cadr (assq action names))
6002              (symbol-value (intern (format "gnus-current-%s-group" action)))
6003              articles prefix))
6004       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6005     (setq to-method (or select-method 
6006                         (gnus-group-name-to-method to-newsgroup)))
6007     ;; Check the method we are to move this article to...
6008     (or (gnus-check-backend-function 'request-accept-article (car to-method))
6009         (error "%s does not support article copying" (car to-method)))
6010     (or (gnus-check-server to-method)
6011         (error "Can't open server %s" (car to-method)))
6012     (gnus-message 6 "%s to %s: %s..."
6013                   (caddr (assq action names))
6014                   (or (car select-method) to-newsgroup) articles)
6015     (while articles
6016       (setq article (pop articles))
6017       (setq
6018        art-group
6019        (cond
6020         ;; Move the article.
6021         ((eq action 'move)
6022          (gnus-request-move-article
6023           article                       ; Article to move
6024           gnus-newsgroup-name           ; From newsgrouo
6025           (nth 1 (gnus-find-method-for-group
6026                   gnus-newsgroup-name)) ; Server
6027           (list 'gnus-request-accept-article
6028                 to-newsgroup (list 'quote select-method)
6029                 (not articles))         ; Accept form
6030           (not articles)))              ; Only save nov last time
6031         ;; Copy the article.
6032         ((eq action 'copy)
6033          (save-excursion
6034            (set-buffer copy-buf)
6035            (gnus-request-article-this-buffer article gnus-newsgroup-name)
6036            (gnus-request-accept-article
6037             to-newsgroup select-method (not articles))))
6038         ;; Crosspost the article.
6039         ((eq action 'crosspost)
6040          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
6041            (setq new-xref (concat gnus-newsgroup-name ":" article))
6042            (if (and xref (not (string= xref "")))
6043                (progn
6044                  (when (string-match "^Xref: " xref)
6045                    (setq xref (substring xref (match-end 0))))
6046                  (setq new-xref (concat xref " " new-xref)))
6047              (setq new-xref (concat (system-name) " " new-xref)))
6048            (save-excursion
6049              (set-buffer copy-buf)
6050              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6051              (nnheader-replace-header "xref" new-xref)
6052              (gnus-request-accept-article
6053               to-newsgroup select-method (not articles)))))))
6054       (if (not art-group)
6055           (gnus-message 1 "Couldn't %s article %s"
6056                         (cadr (assq action names)) article)
6057         (let* ((entry
6058                 (or
6059                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6060                  (gnus-gethash
6061                   (gnus-group-prefixed-name
6062                    (car art-group)
6063                    (or select-method 
6064                        (gnus-find-method-for-group to-newsgroup)))
6065                   gnus-newsrc-hashtb)))
6066                (info (nth 2 entry))
6067                (to-group (gnus-info-group info)))
6068           ;; Update the group that has been moved to.
6069           (when (and info
6070                      (memq action '(move copy)))
6071             (unless (member to-group to-groups)
6072               (push to-group to-groups))
6073
6074             (unless (memq article gnus-newsgroup-unreads)
6075               (gnus-info-set-read
6076                info (gnus-add-to-range (gnus-info-read info)
6077                                        (list (cdr art-group)))))
6078
6079             ;; Copy any marks over to the new group.
6080             (let ((marks gnus-article-mark-lists)
6081                   (to-article (cdr art-group)))
6082
6083               ;; See whether the article is to be put in the cache.
6084               (when gnus-use-cache
6085                 (gnus-cache-possibly-enter-article
6086                  to-group to-article
6087                  (let ((header (copy-sequence
6088                                 (gnus-summary-article-header article))))
6089                    (mail-header-set-number header to-article)
6090                    header)
6091                  (memq article gnus-newsgroup-marked)
6092                  (memq article gnus-newsgroup-dormant)
6093                  (memq article gnus-newsgroup-unreads)))
6094
6095               (while marks
6096                 (when (memq article (symbol-value
6097                                      (intern (format "gnus-newsgroup-%s"
6098                                                      (caar marks)))))
6099                   ;; If the other group is the same as this group,
6100                   ;; then we have to add the mark to the list.
6101                   (when (equal to-group gnus-newsgroup-name)
6102                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6103                          (cons to-article
6104                                (symbol-value
6105                                 (intern (format "gnus-newsgroup-%s"
6106                                                 (caar marks)))))))
6107                   ;; Copy mark to other group.
6108                   (gnus-add-marked-articles
6109                    to-group (cdar marks) (list to-article) info))
6110                 (setq marks (cdr marks)))))
6111
6112           ;; Update the Xref header in this article to point to
6113           ;; the new crossposted article we have just created.
6114           (when (eq action 'crosspost)
6115             (save-excursion
6116               (set-buffer copy-buf)
6117               (gnus-request-article-this-buffer article gnus-newsgroup-name)
6118               (nnheader-replace-header
6119                "xref" (concat new-xref " " (gnus-group-prefixed-name
6120                                             (car art-group) to-method)
6121                               ":" (cdr art-group)))
6122               (gnus-request-replace-article
6123                article gnus-newsgroup-name (current-buffer)))))
6124
6125         (gnus-summary-goto-subject article)
6126         (when (eq action 'move)
6127           (gnus-summary-mark-article article gnus-canceled-mark)))
6128       (gnus-summary-remove-process-mark article))
6129     ;; Re-activate all groups that have been moved to.
6130     (while to-groups
6131       (gnus-activate-group (pop to-groups)))
6132     
6133     (gnus-kill-buffer copy-buf)
6134     (gnus-summary-position-point)
6135     (gnus-set-mode-line 'summary)))
6136
6137 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
6138   "Move the current article to a different newsgroup.
6139 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6140 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6141 re-spool using this method."
6142   (interactive "P")
6143   (gnus-summary-move-article n nil select-method 'copy))
6144
6145 (defun gnus-summary-crosspost-article (&optional n)
6146   "Crosspost the current article to some other group."
6147   (interactive "P")
6148   (gnus-summary-move-article n nil nil 'crosspost))
6149
6150 (defvar gnus-summary-respool-default-method nil
6151   "Default method for respooling an article.  
6152 If nil, use to the current newsgroup method.")
6153
6154 (defun gnus-summary-respool-article (&optional n method)
6155   "Respool the current article.
6156 The article will be squeezed through the mail spooling process again,
6157 which means that it will be put in some mail newsgroup or other
6158 depending on `nnmail-split-methods'.
6159 If N is a positive number, respool the N next articles.
6160 If N is a negative number, respool the N previous articles.
6161 If N is nil and any articles have been marked with the process mark,
6162 respool those articles instead.
6163
6164 Respooling can be done both from mail groups and \"real\" newsgroups.
6165 In the former case, the articles in question will be moved from the
6166 current group into whatever groups they are destined to.  In the
6167 latter case, they will be copied into the relevant groups."
6168   (interactive 
6169    (list current-prefix-arg
6170          (let* ((methods (gnus-methods-using 'respool))
6171                 (methname
6172                  (symbol-name (or gnus-summary-respool-default-method
6173                                   (car (gnus-find-method-for-group
6174                                         gnus-newsgroup-name)))))
6175                 (method
6176                  (gnus-completing-read 
6177                   methname "What backend do you want to use when respooling?"
6178                   methods nil t nil 'gnus-method-history))
6179                 ms)
6180            (cond
6181             ((zerop (length (setq ms (gnus-servers-using-backend method))))
6182              (list (intern method) ""))
6183             ((= 1 (length ms))
6184              (car ms))
6185             (t
6186              (cdr (completing-read 
6187                    "Server name: "
6188                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
6189   (gnus-set-global-variables)
6190   (unless method
6191     (error "No method given for respooling"))
6192   (if (assoc (symbol-name
6193               (car (gnus-find-method-for-group gnus-newsgroup-name)))
6194              (gnus-methods-using 'respool))
6195       (gnus-summary-move-article n nil method)
6196     (gnus-summary-copy-article n nil method)))
6197
6198 (defun gnus-summary-import-article (file)
6199   "Import a random file into a mail newsgroup."
6200   (interactive "fImport file: ")
6201   (gnus-set-global-variables)
6202   (let ((group gnus-newsgroup-name)
6203         (now (current-time))
6204         atts lines)
6205     (or (gnus-check-backend-function 'request-accept-article group)
6206         (error "%s does not support article importing" group))
6207     (or (file-readable-p file)
6208         (not (file-regular-p file))
6209         (error "Can't read %s" file))
6210     (save-excursion
6211       (set-buffer (get-buffer-create " *import file*"))
6212       (buffer-disable-undo (current-buffer))
6213       (erase-buffer)
6214       (insert-file-contents file)
6215       (goto-char (point-min))
6216       (unless (nnheader-article-p)
6217         ;; This doesn't look like an article, so we fudge some headers.
6218         (setq atts (file-attributes file)
6219               lines (count-lines (point-min) (point-max)))
6220         (insert "From: " (read-string "From: ") "\n"
6221                 "Subject: " (read-string "Subject: ") "\n"
6222                 "Date: " (timezone-make-date-arpa-standard
6223                           (current-time-string (nth 5 atts))
6224                           (current-time-zone now)
6225                           (current-time-zone now)) "\n"
6226                 "Message-ID: " (message-make-message-id) "\n"
6227                 "Lines: " (int-to-string lines) "\n"
6228                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
6229       (gnus-request-accept-article group nil t)
6230       (kill-buffer (current-buffer)))))
6231
6232 (defun gnus-summary-article-posted-p ()
6233   "Say whether the current (mail) article is available from `gnus-select-method' as well.
6234 This will be the case if the article has both been mailed and posted."
6235   (interactive)
6236   (let ((id (mail-header-references (gnus-summary-article-header)))
6237         (gnus-override-method
6238          (or gnus-refer-article-method gnus-select-method)))
6239     (if (gnus-request-head id "")
6240         (gnus-message 2 "The current message was found on %s"
6241                       gnus-override-method)
6242       (gnus-message 2 "The current message couldn't be found on %s"
6243                     gnus-override-method)
6244       nil)))
6245
6246 (defun gnus-summary-expire-articles (&optional now)
6247   "Expire all articles that are marked as expirable in the current group."
6248   (interactive)
6249   (gnus-set-global-variables)
6250   (when (gnus-check-backend-function
6251          'request-expire-articles gnus-newsgroup-name)
6252     ;; This backend supports expiry.
6253     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
6254            (expirable (if total
6255                           (gnus-list-of-read-articles gnus-newsgroup-name)
6256                         (setq gnus-newsgroup-expirable
6257                               (sort gnus-newsgroup-expirable '<))))
6258            (expiry-wait (if now 'immediate
6259                           (gnus-group-find-parameter
6260                            gnus-newsgroup-name 'expiry-wait)))
6261            es)
6262       (when expirable
6263         ;; There are expirable articles in this group, so we run them
6264         ;; through the expiry process.
6265         (gnus-message 6 "Expiring articles...")
6266         ;; The list of articles that weren't expired is returned.
6267         (if expiry-wait
6268             (let ((nnmail-expiry-wait-function nil)
6269                   (nnmail-expiry-wait expiry-wait))
6270               (setq es (gnus-request-expire-articles
6271                         expirable gnus-newsgroup-name)))
6272           (setq es (gnus-request-expire-articles
6273                     expirable gnus-newsgroup-name)))
6274         (unless total
6275           (setq gnus-newsgroup-expirable es))
6276         ;; We go through the old list of expirable, and mark all
6277         ;; really expired articles as nonexistent.
6278         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
6279           (let ((gnus-use-cache nil))
6280             (while expirable
6281               (unless (memq (car expirable) es)
6282                 (when (gnus-data-find (car expirable))
6283                   (gnus-summary-mark-article
6284                    (car expirable) gnus-canceled-mark)))
6285               (setq expirable (cdr expirable)))))
6286         (gnus-message 6 "Expiring articles...done")))))
6287
6288 (defun gnus-summary-expire-articles-now ()
6289   "Expunge all expirable articles in the current group.
6290 This means that *all* articles that are marked as expirable will be
6291 deleted forever, right now."
6292   (interactive)
6293   (gnus-set-global-variables)
6294   (or gnus-expert-user
6295       (gnus-y-or-n-p
6296        "Are you really, really, really sure you want to delete all these messages? ")
6297       (error "Phew!"))
6298   (gnus-summary-expire-articles t))
6299
6300 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6301 (defun gnus-summary-delete-article (&optional n)
6302   "Delete the N next (mail) articles.
6303 This command actually deletes articles.  This is not a marking
6304 command.  The article will disappear forever from your life, never to
6305 return.
6306 If N is negative, delete backwards.
6307 If N is nil and articles have been marked with the process mark,
6308 delete these instead."
6309   (interactive "P")
6310   (gnus-set-global-variables)
6311   (or (gnus-check-backend-function 'request-expire-articles
6312                                    gnus-newsgroup-name)
6313       (error "The current newsgroup does not support article deletion."))
6314   ;; Compute the list of articles to delete.
6315   (let ((articles (gnus-summary-work-articles n))
6316         not-deleted)
6317     (if (and gnus-novice-user
6318              (not (gnus-y-or-n-p
6319                    (format "Do you really want to delete %s forever? "
6320                            (if (> (length articles) 1) 
6321                                (format "these %s articles" (length articles))
6322                              "this article")))))
6323         ()
6324       ;; Delete the articles.
6325       (setq not-deleted (gnus-request-expire-articles
6326                          articles gnus-newsgroup-name 'force))
6327       (while articles
6328         (gnus-summary-remove-process-mark (car articles))
6329         ;; The backend might not have been able to delete the article
6330         ;; after all.
6331         (or (memq (car articles) not-deleted)
6332             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
6333         (setq articles (cdr articles))))
6334     (gnus-summary-position-point)
6335     (gnus-set-mode-line 'summary)
6336     not-deleted))
6337
6338 (defun gnus-summary-edit-article (&optional force)
6339   "Edit the current article.
6340 This will have permanent effect only in mail groups.
6341 If FORCE is non-nil, allow editing of articles even in read-only
6342 groups." 
6343   (interactive "P")
6344   (save-excursion
6345     (set-buffer gnus-summary-buffer)
6346     (gnus-set-global-variables)
6347     (when (and (not force)
6348                (gnus-group-read-only-p))
6349       (error "The current newsgroup does not support article editing."))
6350     ;; Select article if needed.
6351     (unless (eq (gnus-summary-article-number)
6352                 gnus-current-article)
6353       (gnus-summary-select-article t))
6354     (gnus-article-edit-article
6355      `(lambda ()
6356         (gnus-summary-edit-article-done
6357          ,(or (mail-header-references gnus-current-headers) "")
6358          ,(gnus-group-read-only-p) ,gnus-summary-buffer)))))
6359
6360 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
6361
6362 (defun gnus-summary-edit-article-done (&optional references read-only buffer)
6363   "Make edits to the current article permanent."
6364   (interactive)
6365   ;; Replace the article.
6366   (if (and (not read-only)
6367            (not (gnus-request-replace-article
6368                  (cdr gnus-article-current) (car gnus-article-current)
6369                  (current-buffer))))
6370       (error "Couldn't replace article.")
6371     ;; Update the summary buffer.
6372     (if (and references
6373              (equal (message-tokenize-header references " ")
6374                     (message-tokenize-header
6375                      (or (message-fetch-field "references") "") " ")))
6376         ;; We only have to update this line.
6377         (save-excursion
6378           (save-restriction
6379             (message-narrow-to-head)
6380             (let ((header (nnheader-parse-head t)))
6381               (set-buffer buffer)
6382               (mail-header-set-number header (cdr gnus-article-current))
6383               (gnus-summary-update-article-line
6384                (cdr gnus-article-current) header))))
6385       ;; Update threads.
6386       (set-buffer (or buffer gnus-summary-buffer))
6387       (gnus-summary-update-article (cdr gnus-article-current)))
6388     ;; Prettify the article buffer again.
6389     (save-excursion
6390       (set-buffer gnus-article-buffer)
6391       (run-hooks 'gnus-article-display-hook))
6392     ;; Prettify the summary buffer line.
6393     (when (gnus-visual-p 'summary-highlight 'highlight)
6394       (run-hooks 'gnus-visual-mark-article-hook))))
6395
6396 (defun gnus-summary-edit-wash (key)
6397   "Perform editing command in the article buffer."
6398   (interactive 
6399    (list
6400     (progn
6401       (message "%s" (concat (this-command-keys) "- "))
6402       (read-char))))
6403   (message "")
6404   (gnus-summary-edit-article)
6405   (execute-kbd-macro (concat (this-command-keys) key))
6406   (gnus-article-edit-done))
6407
6408 ;;; Respooling
6409
6410 (defun gnus-summary-respool-query ()
6411   "Query where the respool algorithm would put this article."
6412   (interactive)
6413   (gnus-set-global-variables)
6414   (gnus-summary-select-article)
6415   (save-excursion
6416     (set-buffer gnus-article-buffer)
6417     (save-restriction
6418       (goto-char (point-min))
6419       (search-forward "\n\n")
6420       (narrow-to-region (point-min) (point))
6421       (pp-eval-expression
6422        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
6423
6424 ;; Summary marking commands.
6425
6426 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
6427   "Mark articles which has the same subject as read, and then select the next.
6428 If UNMARK is positive, remove any kind of mark.
6429 If UNMARK is negative, tick articles."
6430   (interactive "P")
6431   (gnus-set-global-variables)
6432   (if unmark
6433       (setq unmark (prefix-numeric-value unmark)))
6434   (let ((count
6435          (gnus-summary-mark-same-subject
6436           (gnus-summary-article-subject) unmark)))
6437     ;; Select next unread article.  If auto-select-same mode, should
6438     ;; select the first unread article.
6439     (gnus-summary-next-article t (and gnus-auto-select-same
6440                                       (gnus-summary-article-subject)))
6441     (gnus-message 7 "%d article%s marked as %s"
6442                   count (if (= count 1) " is" "s are")
6443                   (if unmark "unread" "read"))))
6444
6445 (defun gnus-summary-kill-same-subject (&optional unmark)
6446   "Mark articles which has the same subject as read.
6447 If UNMARK is positive, remove any kind of mark.
6448 If UNMARK is negative, tick articles."
6449   (interactive "P")
6450   (gnus-set-global-variables)
6451   (if unmark
6452       (setq unmark (prefix-numeric-value unmark)))
6453   (let ((count
6454          (gnus-summary-mark-same-subject
6455           (gnus-summary-article-subject) unmark)))
6456     ;; If marked as read, go to next unread subject.
6457     (if (null unmark)
6458         ;; Go to next unread subject.
6459         (gnus-summary-next-subject 1 t))
6460     (gnus-message 7 "%d articles are marked as %s"
6461                   count (if unmark "unread" "read"))))
6462
6463 (defun gnus-summary-mark-same-subject (subject &optional unmark)
6464   "Mark articles with same SUBJECT as read, and return marked number.
6465 If optional argument UNMARK is positive, remove any kinds of marks.
6466 If optional argument UNMARK is negative, mark articles as unread instead."
6467   (let ((count 1))
6468     (save-excursion
6469       (cond
6470        ((null unmark)                   ; Mark as read.
6471         (while (and
6472                 (progn
6473                   (gnus-summary-mark-article-as-read gnus-killed-mark)
6474                   (gnus-summary-show-thread) t)
6475                 (gnus-summary-find-subject subject))
6476           (setq count (1+ count))))
6477        ((> unmark 0)                    ; Tick.
6478         (while (and
6479                 (progn
6480                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
6481                   (gnus-summary-show-thread) t)
6482                 (gnus-summary-find-subject subject))
6483           (setq count (1+ count))))
6484        (t                               ; Mark as unread.
6485         (while (and
6486                 (progn
6487                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
6488                   (gnus-summary-show-thread) t)
6489                 (gnus-summary-find-subject subject))
6490           (setq count (1+ count)))))
6491       (gnus-set-mode-line 'summary)
6492       ;; Return the number of marked articles.
6493       count)))
6494
6495 (defun gnus-summary-mark-as-processable (n &optional unmark)
6496   "Set the process mark on the next N articles.
6497 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
6498 the process mark instead.  The difference between N and the actual
6499 number of articles marked is returned."
6500   (interactive "p")
6501   (gnus-set-global-variables)
6502   (let ((backward (< n 0))
6503         (n (abs n)))
6504     (while (and
6505             (> n 0)
6506             (if unmark
6507                 (gnus-summary-remove-process-mark
6508                  (gnus-summary-article-number))
6509               (gnus-summary-set-process-mark (gnus-summary-article-number)))
6510             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
6511       (setq n (1- n)))
6512     (if (/= 0 n) (gnus-message 7 "No more articles"))
6513     (gnus-summary-recenter)
6514     (gnus-summary-position-point)
6515     n))
6516
6517 (defun gnus-summary-unmark-as-processable (n)
6518   "Remove the process mark from the next N articles.
6519 If N is negative, mark backward instead.  The difference between N and
6520 the actual number of articles marked is returned."
6521   (interactive "p")
6522   (gnus-set-global-variables)
6523   (gnus-summary-mark-as-processable n t))
6524
6525 (defun gnus-summary-unmark-all-processable ()
6526   "Remove the process mark from all articles."
6527   (interactive)
6528   (gnus-set-global-variables)
6529   (save-excursion
6530     (while gnus-newsgroup-processable
6531       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
6532   (gnus-summary-position-point))
6533
6534 (defun gnus-summary-mark-as-expirable (n)
6535   "Mark N articles forward as expirable.
6536 If N is negative, mark backward instead.  The difference between N and
6537 the actual number of articles marked is returned."
6538   (interactive "p")
6539   (gnus-set-global-variables)
6540   (gnus-summary-mark-forward n gnus-expirable-mark))
6541
6542 (defun gnus-summary-mark-article-as-replied (article)
6543   "Mark ARTICLE replied and update the summary line."
6544   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
6545   (let ((buffer-read-only nil))
6546     (when (gnus-summary-goto-subject article)
6547       (gnus-summary-update-secondary-mark article))))
6548
6549 (defun gnus-summary-set-bookmark (article)
6550   "Set a bookmark in current article."
6551   (interactive (list (gnus-summary-article-number)))
6552   (gnus-set-global-variables)
6553   (if (or (not (get-buffer gnus-article-buffer))
6554           (not gnus-current-article)
6555           (not gnus-article-current)
6556           (not (equal gnus-newsgroup-name (car gnus-article-current))))
6557       (error "No current article selected"))
6558   ;; Remove old bookmark, if one exists.
6559   (let ((old (assq article gnus-newsgroup-bookmarks)))
6560     (if old (setq gnus-newsgroup-bookmarks
6561                   (delq old gnus-newsgroup-bookmarks))))
6562   ;; Set the new bookmark, which is on the form
6563   ;; (article-number . line-number-in-body).
6564   (setq gnus-newsgroup-bookmarks
6565         (cons
6566          (cons article
6567                (save-excursion
6568                  (set-buffer gnus-article-buffer)
6569                  (count-lines
6570                   (min (point)
6571                        (save-excursion
6572                          (goto-char (point-min))
6573                          (search-forward "\n\n" nil t)
6574                          (point)))
6575                   (point))))
6576          gnus-newsgroup-bookmarks))
6577   (gnus-message 6 "A bookmark has been added to the current article."))
6578
6579 (defun gnus-summary-remove-bookmark (article)
6580   "Remove the bookmark from the current article."
6581   (interactive (list (gnus-summary-article-number)))
6582   (gnus-set-global-variables)
6583   ;; Remove old bookmark, if one exists.
6584   (let ((old (assq article gnus-newsgroup-bookmarks)))
6585     (if old
6586         (progn
6587           (setq gnus-newsgroup-bookmarks
6588                 (delq old gnus-newsgroup-bookmarks))
6589           (gnus-message 6 "Removed bookmark."))
6590       (gnus-message 6 "No bookmark in current article."))))
6591
6592 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6593 (defun gnus-summary-mark-as-dormant (n)
6594   "Mark N articles forward as dormant.
6595 If N is negative, mark backward instead.  The difference between N and
6596 the actual number of articles marked is returned."
6597   (interactive "p")
6598   (gnus-set-global-variables)
6599   (gnus-summary-mark-forward n gnus-dormant-mark))
6600
6601 (defun gnus-summary-set-process-mark (article)
6602   "Set the process mark on ARTICLE and update the summary line."
6603   (setq gnus-newsgroup-processable
6604         (cons article
6605               (delq article gnus-newsgroup-processable)))
6606   (when (gnus-summary-goto-subject article)
6607     (gnus-summary-show-thread)
6608     (gnus-summary-update-secondary-mark article)))
6609
6610 (defun gnus-summary-remove-process-mark (article)
6611   "Remove the process mark from ARTICLE and update the summary line."
6612   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
6613   (when (gnus-summary-goto-subject article)
6614     (gnus-summary-show-thread)
6615     (gnus-summary-update-secondary-mark article)))
6616
6617 (defun gnus-summary-set-saved-mark (article)
6618   "Set the process mark on ARTICLE and update the summary line."
6619   (push article gnus-newsgroup-saved)
6620   (when (gnus-summary-goto-subject article)
6621     (gnus-summary-update-secondary-mark article)))
6622
6623 (defun gnus-summary-mark-forward (n &optional mark no-expire)
6624   "Mark N articles as read forwards.
6625 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
6626 The difference between N and the actual number of articles marked is
6627 returned."
6628   (interactive "p")
6629   (gnus-set-global-variables)
6630   (let ((backward (< n 0))
6631         (gnus-summary-goto-unread
6632          (and gnus-summary-goto-unread
6633               (not (eq gnus-summary-goto-unread 'never))
6634               (not (memq mark (list gnus-unread-mark
6635                                     gnus-ticked-mark gnus-dormant-mark)))))
6636         (n (abs n))
6637         (mark (or mark gnus-del-mark)))
6638     (while (and (> n 0)
6639                 (gnus-summary-mark-article nil mark no-expire)
6640                 (zerop (gnus-summary-next-subject
6641                         (if backward -1 1)
6642                         (and gnus-summary-goto-unread
6643                              (not (eq gnus-summary-goto-unread 'never)))
6644                         t)))
6645       (setq n (1- n)))
6646     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
6647     (gnus-summary-recenter)
6648     (gnus-summary-position-point)
6649     (gnus-set-mode-line 'summary)
6650     n))
6651
6652 (defun gnus-summary-mark-article-as-read (mark)
6653   "Mark the current article quickly as read with MARK."
6654   (let ((article (gnus-summary-article-number)))
6655     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6656     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6657     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6658     (setq gnus-newsgroup-reads
6659           (cons (cons article mark) gnus-newsgroup-reads))
6660     ;; Possibly remove from cache, if that is used.
6661     (and gnus-use-cache (gnus-cache-enter-remove-article article))
6662     ;; Allow the backend to change the mark.
6663     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
6664     ;; Check for auto-expiry.
6665     (when (and gnus-newsgroup-auto-expire
6666                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6667                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6668                    (= mark gnus-ancient-mark)
6669                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
6670                    (= mark gnus-duplicate-mark)))
6671       (setq mark gnus-expirable-mark)
6672       (push article gnus-newsgroup-expirable))
6673     ;; Set the mark in the buffer.
6674     (gnus-summary-update-mark mark 'unread)
6675     t))
6676
6677 (defun gnus-summary-mark-article-as-unread (mark)
6678   "Mark the current article quickly as unread with MARK."
6679   (let ((article (gnus-summary-article-number)))
6680     (if (< article 0)
6681         (gnus-error 1 "Unmarkable article")
6682       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6683       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6684       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6685       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
6686       (cond ((= mark gnus-ticked-mark)
6687              (push article gnus-newsgroup-marked))
6688             ((= mark gnus-dormant-mark)
6689              (push article gnus-newsgroup-dormant))
6690             (t
6691              (push article gnus-newsgroup-unreads)))
6692       (setq gnus-newsgroup-reads
6693             (delq (assq article gnus-newsgroup-reads)
6694                   gnus-newsgroup-reads))
6695
6696       ;; See whether the article is to be put in the cache.
6697       (and gnus-use-cache
6698            (vectorp (gnus-summary-article-header article))
6699            (save-excursion
6700              (gnus-cache-possibly-enter-article
6701               gnus-newsgroup-name article
6702               (gnus-summary-article-header article)
6703               (= mark gnus-ticked-mark)
6704               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6705
6706       ;; Fix the mark.
6707       (gnus-summary-update-mark mark 'unread))
6708     t))
6709
6710 (defun gnus-summary-mark-article (&optional article mark no-expire)
6711   "Mark ARTICLE with MARK.  MARK can be any character.
6712 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
6713 `??' (dormant) and `?E' (expirable).
6714 If MARK is nil, then the default character `?D' is used.
6715 If ARTICLE is nil, then the article on the current line will be
6716 marked."
6717   ;; The mark might be a string.
6718   (and (stringp mark)
6719        (setq mark (aref mark 0)))
6720   ;; If no mark is given, then we check auto-expiring.
6721   (and (not no-expire)
6722        gnus-newsgroup-auto-expire
6723        (or (not mark)
6724            (and (numberp mark)
6725                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6726                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6727                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
6728                     (= mark gnus-duplicate-mark))))
6729        (setq mark gnus-expirable-mark))
6730   (let* ((mark (or mark gnus-del-mark))
6731          (article (or article (gnus-summary-article-number))))
6732     (or article (error "No article on current line"))
6733     (if (or (= mark gnus-unread-mark)
6734             (= mark gnus-ticked-mark)
6735             (= mark gnus-dormant-mark))
6736         (gnus-mark-article-as-unread article mark)
6737       (gnus-mark-article-as-read article mark))
6738
6739     ;; See whether the article is to be put in the cache.
6740     (and gnus-use-cache
6741          (not (= mark gnus-canceled-mark))
6742          (vectorp (gnus-summary-article-header article))
6743          (save-excursion
6744            (gnus-cache-possibly-enter-article
6745             gnus-newsgroup-name article
6746             (gnus-summary-article-header article)
6747             (= mark gnus-ticked-mark)
6748             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6749
6750     (if (gnus-summary-goto-subject article nil t)
6751         (let ((buffer-read-only nil))
6752           (gnus-summary-show-thread)
6753           ;; Fix the mark.
6754           (gnus-summary-update-mark mark 'unread)
6755           t))))
6756
6757 (defun gnus-summary-update-secondary-mark (article)
6758   "Update the secondary (read, process, cache) mark."
6759   (gnus-summary-update-mark
6760    (cond ((memq article gnus-newsgroup-processable)
6761           gnus-process-mark)
6762          ((memq article gnus-newsgroup-cached)
6763           gnus-cached-mark)
6764          ((memq article gnus-newsgroup-replied)
6765           gnus-replied-mark)
6766          ((memq article gnus-newsgroup-saved)
6767           gnus-saved-mark)
6768          (t gnus-unread-mark))
6769    'replied)
6770   (when (gnus-visual-p 'summary-highlight 'highlight)
6771     (run-hooks 'gnus-summary-update-hook))
6772   t)
6773
6774 (defun gnus-summary-update-mark (mark type)
6775   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
6776         (buffer-read-only nil))
6777     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
6778     (and (looking-at "\r") (setq forward (1+ forward)))
6779     (when (and forward
6780                (<= (+ forward (point)) (point-max)))
6781       ;; Go to the right position on the line.
6782       (goto-char (+ forward (point)))
6783       ;; Replace the old mark with the new mark.
6784       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
6785       ;; Optionally update the marks by some user rule.
6786       (when (eq type 'unread)
6787         (gnus-data-set-mark
6788          (gnus-data-find (gnus-summary-article-number)) mark)
6789         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
6790
6791 (defun gnus-mark-article-as-read (article &optional mark)
6792   "Enter ARTICLE in the pertinent lists and remove it from others."
6793   ;; Make the article expirable.
6794   (let ((mark (or mark gnus-del-mark)))
6795     (if (= mark gnus-expirable-mark)
6796         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
6797       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
6798     ;; Remove from unread and marked lists.
6799     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6800     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6801     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6802     (push (cons article mark) gnus-newsgroup-reads)
6803     ;; Possibly remove from cache, if that is used.
6804     (when gnus-use-cache
6805       (gnus-cache-enter-remove-article article))))
6806
6807 (defun gnus-mark-article-as-unread (article &optional mark)
6808   "Enter ARTICLE in the pertinent lists and remove it from others."
6809   (let ((mark (or mark gnus-ticked-mark)))
6810     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
6811           gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
6812           gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
6813           gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6814
6815     ;; Unsuppress duplicates?
6816     (when gnus-suppress-duplicates
6817       (gnus-dup-unsuppress-article article))
6818
6819     (cond ((= mark gnus-ticked-mark)
6820            (push article gnus-newsgroup-marked))
6821           ((= mark gnus-dormant-mark)
6822            (push article gnus-newsgroup-dormant))
6823           (t
6824            (push article gnus-newsgroup-unreads)))
6825     (setq gnus-newsgroup-reads
6826           (delq (assq article gnus-newsgroup-reads)
6827                 gnus-newsgroup-reads))))
6828
6829 (defalias 'gnus-summary-mark-as-unread-forward
6830   'gnus-summary-tick-article-forward)
6831 (make-obsolete 'gnus-summary-mark-as-unread-forward
6832                'gnus-summary-tick-article-forward)
6833 (defun gnus-summary-tick-article-forward (n)
6834   "Tick N articles forwards.
6835 If N is negative, tick backwards instead.
6836 The difference between N and the number of articles ticked is returned."
6837   (interactive "p")
6838   (gnus-summary-mark-forward n gnus-ticked-mark))
6839
6840 (defalias 'gnus-summary-mark-as-unread-backward
6841   'gnus-summary-tick-article-backward)
6842 (make-obsolete 'gnus-summary-mark-as-unread-backward
6843                'gnus-summary-tick-article-backward)
6844 (defun gnus-summary-tick-article-backward (n)
6845   "Tick N articles backwards.
6846 The difference between N and the number of articles ticked is returned."
6847   (interactive "p")
6848   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
6849
6850 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6851 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6852 (defun gnus-summary-tick-article (&optional article clear-mark)
6853   "Mark current article as unread.
6854 Optional 1st argument ARTICLE specifies article number to be marked as unread.
6855 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
6856   (interactive)
6857   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
6858                                        gnus-ticked-mark)))
6859
6860 (defun gnus-summary-mark-as-read-forward (n)
6861   "Mark N articles as read forwards.
6862 If N is negative, mark backwards instead.
6863 The difference between N and the actual number of articles marked is
6864 returned."
6865   (interactive "p")
6866   (gnus-summary-mark-forward n gnus-del-mark t))
6867
6868 (defun gnus-summary-mark-as-read-backward (n)
6869   "Mark the N articles as read backwards.
6870 The difference between N and the actual number of articles marked is
6871 returned."
6872   (interactive "p")
6873   (gnus-summary-mark-forward (- n) gnus-del-mark t))
6874
6875 (defun gnus-summary-mark-as-read (&optional article mark)
6876   "Mark current article as read.
6877 ARTICLE specifies the article to be marked as read.
6878 MARK specifies a string to be inserted at the beginning of the line."
6879   (gnus-summary-mark-article article mark))
6880
6881 (defun gnus-summary-clear-mark-forward (n)
6882   "Clear marks from N articles forward.
6883 If N is negative, clear backward instead.
6884 The difference between N and the number of marks cleared is returned."
6885   (interactive "p")
6886   (gnus-summary-mark-forward n gnus-unread-mark))
6887
6888 (defun gnus-summary-clear-mark-backward (n)
6889   "Clear marks from N articles backward.
6890 The difference between N and the number of marks cleared is returned."
6891   (interactive "p")
6892   (gnus-summary-mark-forward (- n) gnus-unread-mark))
6893
6894 (defun gnus-summary-mark-unread-as-read ()
6895   "Intended to be used by `gnus-summary-mark-article-hook'."
6896   (when (memq gnus-current-article gnus-newsgroup-unreads)
6897     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
6898
6899 (defun gnus-summary-mark-read-and-unread-as-read ()
6900   "Intended to be used by `gnus-summary-mark-article-hook'."
6901   (let ((mark (gnus-summary-article-mark)))
6902     (when (or (gnus-unread-mark-p mark)
6903               (gnus-read-mark-p mark))
6904       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
6905
6906 (defun gnus-summary-mark-region-as-read (point mark all)
6907   "Mark all unread articles between point and mark as read.
6908 If given a prefix, mark all articles between point and mark as read,
6909 even ticked and dormant ones."
6910   (interactive "r\nP")
6911   (save-excursion
6912     (let (article)
6913       (goto-char point)
6914       (beginning-of-line)
6915       (while (and
6916               (< (point) mark)
6917               (progn
6918                 (when (or all
6919                           (memq (setq article (gnus-summary-article-number))
6920                                 gnus-newsgroup-unreads))
6921                   (gnus-summary-mark-article article gnus-del-mark))
6922                 t)
6923               (gnus-summary-find-next))))))
6924
6925 (defun gnus-summary-mark-below (score mark)
6926   "Mark articles with score less than SCORE with MARK."
6927   (interactive "P\ncMark: ")
6928   (gnus-set-global-variables)
6929   (setq score (if score
6930                   (prefix-numeric-value score)
6931                 (or gnus-summary-default-score 0)))
6932   (save-excursion
6933     (set-buffer gnus-summary-buffer)
6934     (goto-char (point-min))
6935     (while 
6936         (progn
6937           (and (< (gnus-summary-article-score) score)
6938                (gnus-summary-mark-article nil mark))
6939           (gnus-summary-find-next)))))
6940
6941 (defun gnus-summary-kill-below (&optional score)
6942   "Mark articles with score below SCORE as read."
6943   (interactive "P")
6944   (gnus-set-global-variables)
6945   (gnus-summary-mark-below score gnus-killed-mark))
6946
6947 (defun gnus-summary-clear-above (&optional score)
6948   "Clear all marks from articles with score above SCORE."
6949   (interactive "P")
6950   (gnus-set-global-variables)
6951   (gnus-summary-mark-above score gnus-unread-mark))
6952
6953 (defun gnus-summary-tick-above (&optional score)
6954   "Tick all articles with score above SCORE."
6955   (interactive "P")
6956   (gnus-set-global-variables)
6957   (gnus-summary-mark-above score gnus-ticked-mark))
6958
6959 (defun gnus-summary-mark-above (score mark)
6960   "Mark articles with score over SCORE with MARK."
6961   (interactive "P\ncMark: ")
6962   (gnus-set-global-variables)
6963   (setq score (if score
6964                   (prefix-numeric-value score)
6965                 (or gnus-summary-default-score 0)))
6966   (save-excursion
6967     (set-buffer gnus-summary-buffer)
6968     (goto-char (point-min))
6969     (while (and (progn
6970                   (if (> (gnus-summary-article-score) score)
6971                       (gnus-summary-mark-article nil mark))
6972                   t)
6973                 (gnus-summary-find-next)))))
6974
6975 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6976 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
6977 (defun gnus-summary-limit-include-expunged (&optional no-error)
6978   "Display all the hidden articles that were expunged for low scores."
6979   (interactive)
6980   (gnus-set-global-variables)
6981   (let ((buffer-read-only nil))
6982     (let ((scored gnus-newsgroup-scored)
6983           headers h)
6984       (while scored
6985         (or (gnus-summary-goto-subject (caar scored))
6986             (and (setq h (gnus-summary-article-header (caar scored)))
6987                  (< (cdar scored) gnus-summary-expunge-below)
6988                  (setq headers (cons h headers))))
6989         (setq scored (cdr scored)))
6990       (if (not headers)
6991           (when (not no-error)
6992             (error "No expunged articles hidden."))
6993         (goto-char (point-min))
6994         (gnus-summary-prepare-unthreaded (nreverse headers))
6995         (goto-char (point-min))
6996         (gnus-summary-position-point)
6997         t))))
6998
6999 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7000   "Mark all articles not marked as unread in this newsgroup as read.
7001 If prefix argument ALL is non-nil, all articles are marked as read.
7002 If QUIETLY is non-nil, no questions will be asked.
7003 If TO-HERE is non-nil, it should be a point in the buffer.  All
7004 articles before this point will be marked as read.
7005 The number of articles marked as read is returned."
7006   (interactive "P")
7007   (gnus-set-global-variables)
7008   (prog1
7009       (when (or quietly
7010                 (not gnus-interactive-catchup) ;Without confirmation?
7011                 gnus-expert-user
7012                 (gnus-y-or-n-p
7013                  (if all
7014                      "Mark absolutely all articles as read? "
7015                    "Mark all unread articles as read? ")))
7016         (if (and not-mark
7017                  (not gnus-newsgroup-adaptive)
7018                  (not gnus-newsgroup-auto-expire)
7019                  (not gnus-suppress-duplicates))
7020             (progn
7021               (when all
7022                 (setq gnus-newsgroup-marked nil
7023                       gnus-newsgroup-dormant nil))
7024               (setq gnus-newsgroup-unreads nil))
7025           ;; We actually mark all articles as canceled, which we
7026           ;; have to do when using auto-expiry or adaptive scoring.
7027           (gnus-summary-show-all-threads)
7028           (when (gnus-summary-first-subject (not all))
7029             (while (and
7030                     (if to-here (< (point) to-here) t)
7031                     (gnus-summary-mark-article-as-read gnus-catchup-mark)
7032                     (gnus-summary-find-next (not all)))))
7033           (unless to-here
7034             (setq gnus-newsgroup-unreads nil))
7035           (gnus-set-mode-line 'summary)))
7036     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
7037       (when (and (not to-here) (eq 'nnvirtual (car method)))
7038         (nnvirtual-catchup-group
7039          (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
7040     (gnus-summary-position-point)))
7041
7042 (defun gnus-summary-catchup-to-here (&optional all)
7043   "Mark all unticked articles before the current one as read.
7044 If ALL is non-nil, also mark ticked and dormant articles as read."
7045   (interactive "P")
7046   (gnus-set-global-variables)
7047   (save-excursion
7048     (gnus-save-hidden-threads
7049       (let ((beg (point)))
7050         ;; We check that there are unread articles.
7051         (when (or all (gnus-summary-find-prev))
7052           (gnus-summary-catchup all t beg)))))
7053   (gnus-summary-position-point))
7054
7055 (defun gnus-summary-catchup-all (&optional quietly)
7056   "Mark all articles in this newsgroup as read."
7057   (interactive "P")
7058   (gnus-set-global-variables)
7059   (gnus-summary-catchup t quietly))
7060
7061 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7062   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7063 If prefix argument ALL is non-nil, all articles are marked as read."
7064   (interactive "P")
7065   (gnus-set-global-variables)
7066   (gnus-summary-catchup all quietly nil 'fast)
7067   ;; Select next newsgroup or exit.
7068   (if (eq gnus-auto-select-next 'quietly)
7069       (gnus-summary-next-group nil)
7070     (gnus-summary-exit)))
7071
7072 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7073   "Mark all articles in this newsgroup as read, and then exit."
7074   (interactive "P")
7075   (gnus-set-global-variables)
7076   (gnus-summary-catchup-and-exit t quietly))
7077
7078 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
7079 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
7080   "Mark all articles in this group as read and select the next group.
7081 If given a prefix, mark all articles, unread as well as ticked, as
7082 read."
7083   (interactive "P")
7084   (gnus-set-global-variables)
7085   (save-excursion
7086     (gnus-summary-catchup all))
7087   (gnus-summary-next-article t nil nil t))
7088
7089 ;; Thread-based commands.
7090
7091 (defun gnus-summary-articles-in-thread (&optional article)
7092   "Return a list of all articles in the current thread.
7093 If ARTICLE is non-nil, return all articles in the thread that starts
7094 with that article."
7095   (let* ((article (or article (gnus-summary-article-number)))
7096          (data (gnus-data-find-list article))
7097          (top-level (gnus-data-level (car data)))
7098          (top-subject
7099           (cond ((null gnus-thread-operation-ignore-subject)
7100                  (gnus-simplify-subject-re
7101                   (mail-header-subject (gnus-data-header (car data)))))
7102                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
7103                  (gnus-simplify-subject-fuzzy
7104                   (mail-header-subject (gnus-data-header (car data)))))
7105                 (t nil)))
7106          (end-point (save-excursion
7107                       (if (gnus-summary-go-to-next-thread) 
7108                           (point) (point-max))))
7109          articles)
7110     (while (and data
7111                 (< (gnus-data-pos (car data)) end-point))
7112       (when (or (not top-subject)
7113                 (string= top-subject
7114                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
7115                              (gnus-simplify-subject-fuzzy
7116                               (mail-header-subject
7117                                (gnus-data-header (car data))))
7118                            (gnus-simplify-subject-re
7119                             (mail-header-subject
7120                              (gnus-data-header (car data)))))))
7121         (push (gnus-data-number (car data)) articles))
7122       (unless (and (setq data (cdr data))
7123                    (> (gnus-data-level (car data)) top-level))
7124         (setq data nil)))
7125     ;; Return the list of articles.
7126     (nreverse articles)))
7127
7128 (defun gnus-summary-rethread-current ()
7129   "Rethread the thread the current article is part of."
7130   (interactive)
7131   (gnus-set-global-variables)
7132   (let* ((gnus-show-threads t)
7133          (article (gnus-summary-article-number))
7134          (id (mail-header-id (gnus-summary-article-header)))
7135          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
7136     (unless id
7137       (error "No article on the current line"))
7138     (gnus-rebuild-thread id)
7139     (gnus-summary-goto-subject article)))
7140
7141 (defun gnus-summary-reparent-thread ()
7142   "Make current article child of the marked (or previous) article.
7143
7144 Note that the re-threading will only work if `gnus-thread-ignore-subject'
7145 is non-nil or the Subject: of both articles are the same."
7146   (interactive)
7147   (or (not (gnus-group-read-only-p))
7148       (error "The current newsgroup does not support article editing."))
7149   (or (<= (length gnus-newsgroup-processable) 1)
7150       (error "No more than one article may be marked."))
7151   (save-window-excursion
7152     (let ((gnus-article-buffer " *reparent*")
7153           (current-article (gnus-summary-article-number))
7154           ; first grab the marked article, otherwise one line up.
7155           (parent-article (if (not (null gnus-newsgroup-processable))
7156                               (car gnus-newsgroup-processable)
7157                             (save-excursion
7158                               (if (eq (forward-line -1) 0)
7159                                   (gnus-summary-article-number)
7160                                 (error "Beginning of summary buffer."))))))
7161       (or (not (eq current-article parent-article))
7162           (error "An article may not be self-referential."))
7163       (let ((message-id (mail-header-id 
7164                          (gnus-summary-article-header parent-article))))
7165         (or (and message-id (not (equal message-id "")))
7166             (error "No message-id in desired parent."))
7167         (gnus-summary-select-article t t nil current-article)
7168         (set-buffer gnus-article-buffer)
7169         (setq buffer-read-only nil)
7170         (let ((buf (format "%s" (buffer-string))))
7171           (erase-buffer)
7172           (insert buf))
7173         (goto-char (point-min))
7174         (if (search-forward-regexp "^References: " nil t)
7175             (insert message-id " " )
7176           (insert "References: " message-id "\n"))
7177         (or (gnus-request-replace-article current-article
7178                                           (car gnus-article-current)
7179                                           gnus-article-buffer)
7180             (error "Couldn't replace article."))
7181         (set-buffer gnus-summary-buffer)
7182         (gnus-summary-unmark-all-processable)
7183         (gnus-summary-rethread-current)
7184         (gnus-message 3 "Article %d is now the child of article %d."
7185                       current-article parent-article)))))
7186
7187 (defun gnus-summary-toggle-threads (&optional arg)
7188   "Toggle showing conversation threads.
7189 If ARG is positive number, turn showing conversation threads on."
7190   (interactive "P")
7191   (gnus-set-global-variables)
7192   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
7193     (setq gnus-show-threads
7194           (if (null arg) (not gnus-show-threads)
7195             (> (prefix-numeric-value arg) 0)))
7196     (gnus-summary-prepare)
7197     (gnus-summary-goto-subject current)
7198     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
7199     (gnus-summary-position-point)))
7200
7201 (defun gnus-summary-show-all-threads ()
7202   "Show all threads."
7203   (interactive)
7204   (gnus-set-global-variables)
7205   (save-excursion
7206     (let ((buffer-read-only nil))
7207       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7208   (gnus-summary-position-point))
7209
7210 (defun gnus-summary-show-thread ()
7211   "Show thread subtrees.
7212 Returns nil if no thread was there to be shown."
7213   (interactive)
7214   (gnus-set-global-variables)
7215   (let ((buffer-read-only nil)
7216         (orig (point))
7217         ;; first goto end then to beg, to have point at beg after let
7218         (end (progn (end-of-line) (point)))
7219         (beg (progn (beginning-of-line) (point))))
7220     (prog1
7221         ;; Any hidden lines here?
7222         (search-forward "\r" end t)
7223       (subst-char-in-region beg end ?\^M ?\n t)
7224       (goto-char orig)
7225       (gnus-summary-position-point))))
7226
7227 (defun gnus-summary-hide-all-threads ()
7228   "Hide all thread subtrees."
7229   (interactive)
7230   (gnus-set-global-variables)
7231   (save-excursion
7232     (goto-char (point-min))
7233     (gnus-summary-hide-thread)
7234     (while (zerop (gnus-summary-next-thread 1 t))
7235       (gnus-summary-hide-thread)))
7236   (gnus-summary-position-point))
7237
7238 (defun gnus-summary-hide-thread ()
7239   "Hide thread subtrees.
7240 Returns nil if no threads were there to be hidden."
7241   (interactive)
7242   (gnus-set-global-variables)
7243   (let ((buffer-read-only nil)
7244         (start (point))
7245         (article (gnus-summary-article-number)))
7246     (goto-char start)
7247     ;; Go forward until either the buffer ends or the subthread
7248     ;; ends.
7249     (when (and (not (eobp))
7250                (or (zerop (gnus-summary-next-thread 1 t))
7251                    (goto-char (point-max))))
7252       (prog1
7253           (if (and (> (point) start)
7254                    (search-backward "\n" start t))
7255               (progn
7256                 (subst-char-in-region start (point) ?\n ?\^M)
7257                 (gnus-summary-goto-subject article))
7258             (goto-char start)
7259             nil)
7260         ;;(gnus-summary-position-point)
7261         ))))
7262
7263 (defun gnus-summary-go-to-next-thread (&optional previous)
7264   "Go to the same level (or less) next thread.
7265 If PREVIOUS is non-nil, go to previous thread instead.
7266 Return the article number moved to, or nil if moving was impossible."
7267   (let ((level (gnus-summary-thread-level))
7268         (way (if previous -1 1))
7269         (beg (point)))
7270     (forward-line way)
7271     (while (and (not (eobp))
7272                 (< level (gnus-summary-thread-level)))
7273       (forward-line way))
7274     (if (eobp)
7275         (progn
7276           (goto-char beg)
7277           nil)
7278       (setq beg (point))
7279       (prog1
7280           (gnus-summary-article-number)
7281         (goto-char beg)))))
7282
7283 (defun gnus-summary-next-thread (n &optional silent)
7284   "Go to the same level next N'th thread.
7285 If N is negative, search backward instead.
7286 Returns the difference between N and the number of skips actually
7287 done.
7288
7289 If SILENT, don't output messages."
7290   (interactive "p")
7291   (gnus-set-global-variables)
7292   (let ((backward (< n 0))
7293         (n (abs n)))
7294     (while (and (> n 0)
7295                 (gnus-summary-go-to-next-thread backward))
7296       (decf n))
7297     (unless silent 
7298       (gnus-summary-position-point))
7299     (when (and (not silent) (/= 0 n))
7300       (gnus-message 7 "No more threads"))
7301     n))
7302
7303 (defun gnus-summary-prev-thread (n)
7304   "Go to the same level previous N'th thread.
7305 Returns the difference between N and the number of skips actually
7306 done."
7307   (interactive "p")
7308   (gnus-set-global-variables)
7309   (gnus-summary-next-thread (- n)))
7310
7311 (defun gnus-summary-go-down-thread ()
7312   "Go down one level in the current thread."
7313   (let ((children (gnus-summary-article-children)))
7314     (and children
7315          (gnus-summary-goto-subject (car children)))))
7316
7317 (defun gnus-summary-go-up-thread ()
7318   "Go up one level in the current thread."
7319   (let ((parent (gnus-summary-article-parent)))
7320     (and parent
7321          (gnus-summary-goto-subject parent))))
7322
7323 (defun gnus-summary-down-thread (n)
7324   "Go down thread N steps.
7325 If N is negative, go up instead.
7326 Returns the difference between N and how many steps down that were
7327 taken."
7328   (interactive "p")
7329   (gnus-set-global-variables)
7330   (let ((up (< n 0))
7331         (n (abs n)))
7332     (while (and (> n 0)
7333                 (if up (gnus-summary-go-up-thread)
7334                   (gnus-summary-go-down-thread)))
7335       (setq n (1- n)))
7336     (gnus-summary-position-point)
7337     (if (/= 0 n) (gnus-message 7 "Can't go further"))
7338     n))
7339
7340 (defun gnus-summary-up-thread (n)
7341   "Go up thread N steps.
7342 If N is negative, go up instead.
7343 Returns the difference between N and how many steps down that were
7344 taken."
7345   (interactive "p")
7346   (gnus-set-global-variables)
7347   (gnus-summary-down-thread (- n)))
7348
7349 (defun gnus-summary-top-thread ()
7350   "Go to the top of the thread."
7351   (interactive)
7352   (gnus-set-global-variables)
7353   (while (gnus-summary-go-up-thread))
7354   (gnus-summary-article-number))
7355
7356 (defun gnus-summary-kill-thread (&optional unmark)
7357   "Mark articles under current thread as read.
7358 If the prefix argument is positive, remove any kinds of marks.
7359 If the prefix argument is negative, tick articles instead."
7360   (interactive "P")
7361   (gnus-set-global-variables)
7362   (when unmark
7363     (setq unmark (prefix-numeric-value unmark)))
7364   (let ((articles (gnus-summary-articles-in-thread)))
7365     (save-excursion
7366       ;; Expand the thread.
7367       (gnus-summary-show-thread)
7368       ;; Mark all the articles.
7369       (while articles
7370         (gnus-summary-goto-subject (car articles))
7371         (cond ((null unmark)
7372                (gnus-summary-mark-article-as-read gnus-killed-mark))
7373               ((> unmark 0)
7374                (gnus-summary-mark-article-as-unread gnus-unread-mark))
7375               (t
7376                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
7377         (setq articles (cdr articles))))
7378     ;; Hide killed subtrees.
7379     (and (null unmark)
7380          gnus-thread-hide-killed
7381          (gnus-summary-hide-thread))
7382     ;; If marked as read, go to next unread subject.
7383     (if (null unmark)
7384         ;; Go to next unread subject.
7385         (gnus-summary-next-subject 1 t)))
7386   (gnus-set-mode-line 'summary))
7387
7388 ;; Summary sorting commands
7389
7390 (defun gnus-summary-sort-by-number (&optional reverse)
7391   "Sort summary buffer by article number.
7392 Argument REVERSE means reverse order."
7393   (interactive "P")
7394   (gnus-summary-sort 'number reverse))
7395
7396 (defun gnus-summary-sort-by-author (&optional reverse)
7397   "Sort summary buffer by author name alphabetically.
7398 If case-fold-search is non-nil, case of letters is ignored.
7399 Argument REVERSE means reverse order."
7400   (interactive "P")
7401   (gnus-summary-sort 'author reverse))
7402
7403 (defun gnus-summary-sort-by-subject (&optional reverse)
7404   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
7405 If case-fold-search is non-nil, case of letters is ignored.
7406 Argument REVERSE means reverse order."
7407   (interactive "P")
7408   (gnus-summary-sort 'subject reverse))
7409
7410 (defun gnus-summary-sort-by-date (&optional reverse)
7411   "Sort summary buffer by date.
7412 Argument REVERSE means reverse order."
7413   (interactive "P")
7414   (gnus-summary-sort 'date reverse))
7415
7416 (defun gnus-summary-sort-by-score (&optional reverse)
7417   "Sort summary buffer by score.
7418 Argument REVERSE means reverse order."
7419   (interactive "P")
7420   (gnus-summary-sort 'score reverse))
7421
7422 (defun gnus-summary-sort (predicate reverse)
7423   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
7424   (gnus-set-global-variables)
7425   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
7426          (article (intern (format "gnus-article-sort-by-%s" predicate)))
7427          (gnus-thread-sort-functions
7428           (list
7429            (if (not reverse)
7430                thread
7431              `(lambda (t1 t2)
7432                 (,thread t2 t1)))))
7433          (gnus-article-sort-functions
7434           (list
7435            (if (not reverse)
7436                article
7437              `(lambda (t1 t2)
7438                 (,article t2 t1)))))
7439          (buffer-read-only)
7440          (gnus-summary-prepare-hook nil))
7441     ;; We do the sorting by regenerating the threads.
7442     (gnus-summary-prepare)
7443     ;; Hide subthreads if needed.
7444     (when (and gnus-show-threads gnus-thread-hide-subtree)
7445       (gnus-summary-hide-all-threads))))
7446
7447 ;; Summary saving commands.
7448
7449 (defun gnus-summary-save-article (&optional n not-saved)
7450   "Save the current article using the default saver function.
7451 If N is a positive number, save the N next articles.
7452 If N is a negative number, save the N previous articles.
7453 If N is nil and any articles have been marked with the process mark,
7454 save those articles instead.
7455 The variable `gnus-default-article-saver' specifies the saver function."
7456   (interactive "P")
7457   (gnus-set-global-variables)
7458   (let* ((articles (gnus-summary-work-articles n))
7459          (save-buffer (save-excursion 
7460                         (nnheader-set-temp-buffer " *Gnus Save*")))
7461          (num (length articles))
7462          header article file)
7463     (while articles
7464       (setq header (gnus-summary-article-header
7465                     (setq article (pop articles))))
7466       (if (not (vectorp header))
7467           ;; This is a pseudo-article.
7468           (if (assq 'name header)
7469               (gnus-copy-file (cdr (assq 'name header)))
7470             (gnus-message 1 "Article %d is unsaveable" article))
7471         ;; This is a real article.
7472         (save-window-excursion
7473           (gnus-summary-select-article t nil nil article))
7474         (save-excursion
7475           (set-buffer save-buffer)
7476           (erase-buffer)
7477           (insert-buffer-substring gnus-original-article-buffer))
7478         (setq file (gnus-article-save save-buffer file num))
7479         (gnus-summary-remove-process-mark article)
7480         (unless not-saved
7481           (gnus-summary-set-saved-mark article))))
7482     (gnus-kill-buffer save-buffer)
7483     (gnus-summary-position-point)
7484     n))
7485
7486 (defun gnus-summary-pipe-output (&optional arg)
7487   "Pipe the current article to a subprocess.
7488 If N is a positive number, pipe the N next articles.
7489 If N is a negative number, pipe the N previous articles.
7490 If N is nil and any articles have been marked with the process mark,
7491 pipe those articles instead."
7492   (interactive "P")
7493   (gnus-set-global-variables)
7494   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
7495     (gnus-summary-save-article arg t))
7496   (gnus-configure-windows 'pipe))
7497
7498 (defun gnus-summary-save-article-mail (&optional arg)
7499   "Append the current article to an mail file.
7500 If N is a positive number, save the N next articles.
7501 If N is a negative number, save the N previous articles.
7502 If N is nil and any articles have been marked with the process mark,
7503 save those articles instead."
7504   (interactive "P")
7505   (gnus-set-global-variables)
7506   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
7507     (gnus-summary-save-article arg)))
7508
7509 (defun gnus-summary-save-article-rmail (&optional arg)
7510   "Append the current article to an rmail file.
7511 If N is a positive number, save the N next articles.
7512 If N is a negative number, save the N previous articles.
7513 If N is nil and any articles have been marked with the process mark,
7514 save those articles instead."
7515   (interactive "P")
7516   (gnus-set-global-variables)
7517   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
7518     (gnus-summary-save-article arg)))
7519
7520 (defun gnus-summary-save-article-file (&optional arg)
7521   "Append the current article to a file.
7522 If N is a positive number, save the N next articles.
7523 If N is a negative number, save the N previous articles.
7524 If N is nil and any articles have been marked with the process mark,
7525 save those articles instead."
7526   (interactive "P")
7527   (gnus-set-global-variables)
7528   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
7529     (gnus-summary-save-article arg)))
7530
7531 (defun gnus-summary-save-article-body-file (&optional arg)
7532   "Append the current article body to a file.
7533 If N is a positive number, save the N next articles.
7534 If N is a negative number, save the N previous articles.
7535 If N is nil and any articles have been marked with the process mark,
7536 save those articles instead."
7537   (interactive "P")
7538   (gnus-set-global-variables)
7539   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
7540     (gnus-summary-save-article arg)))
7541
7542 (defun gnus-get-split-value (methods)
7543   "Return a value based on the split METHODS."
7544   (let (split-name method result match)
7545     (when methods
7546       (save-excursion
7547         (set-buffer gnus-original-article-buffer)
7548         (save-restriction
7549           (nnheader-narrow-to-headers)
7550           (while methods
7551             (goto-char (point-min))
7552             (setq method (pop methods))
7553             (setq match (car method))
7554             (when (cond
7555                    ((stringp match)
7556                     ;; Regular expression.
7557                     (condition-case ()
7558                         (re-search-forward match nil t)
7559                       (error nil)))
7560                    ((gnus-functionp match)
7561                     ;; Function.
7562                     (save-restriction
7563                       (widen)
7564                       (setq result (funcall match gnus-newsgroup-name))))
7565                    ((consp match)
7566                     ;; Form.
7567                     (save-restriction
7568                       (widen)
7569                       (setq result (eval match)))))
7570               (setq split-name (append (cdr method) split-name))
7571               (cond ((stringp result)
7572                      (push result split-name))
7573                     ((consp result)
7574                      (setq split-name (append result split-name)))))))))
7575     split-name))
7576
7577 (defun gnus-read-move-group-name (prompt default articles prefix)
7578   "Read a group name."
7579   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
7580          (minibuffer-confirm-incomplete nil) ; XEmacs
7581          group-map
7582          (dum (mapatoms
7583                (lambda (g) 
7584                  (and (boundp g)
7585                       (symbol-name g)
7586                       (memq 'respool
7587                             (assoc (symbol-name
7588                                     (car (gnus-find-method-for-group
7589                                           (symbol-name g))))
7590                                    gnus-valid-select-methods))
7591                       (push (list (symbol-name g)) group-map)))
7592                gnus-active-hashtb))
7593          (prom
7594           (format "%s %s to:"
7595                   prompt
7596                   (if (> (length articles) 1)
7597                       (format "these %d articles" (length articles))
7598                     "this article")))
7599          (to-newsgroup
7600           (cond
7601            ((null split-name)
7602             (gnus-completing-read default prom
7603                                   group-map nil nil prefix
7604                                   'gnus-group-history))
7605            ((= 1 (length split-name))
7606             (gnus-completing-read (car split-name) prom group-map
7607                                   nil nil nil
7608                                   'gnus-group-history))
7609            (t
7610             (gnus-completing-read nil prom 
7611                                   (mapcar (lambda (el) (list el))
7612                                           (nreverse split-name))
7613                                   nil nil nil
7614                                   'gnus-group-history)))))
7615     (when to-newsgroup
7616       (if (or (string= to-newsgroup "")
7617               (string= to-newsgroup prefix))
7618           (setq to-newsgroup (or default "")))
7619       (or (gnus-active to-newsgroup)
7620           (gnus-activate-group to-newsgroup)
7621           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
7622                                      to-newsgroup))
7623               (or (and (gnus-request-create-group 
7624                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
7625                        (gnus-activate-group to-newsgroup nil nil
7626                                             (gnus-group-name-to-method
7627                                              to-newsgroup)))
7628                   (error "Couldn't create group %s" to-newsgroup)))
7629           (error "No such group: %s" to-newsgroup)))
7630     to-newsgroup))
7631
7632 ;; Summary extract commands
7633
7634 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
7635   (let ((buffer-read-only nil)
7636         (article (gnus-summary-article-number))
7637         after-article b e)
7638     (or (gnus-summary-goto-subject article)
7639         (error (format "No such article: %d" article)))
7640     (gnus-summary-position-point)
7641     ;; If all commands are to be bunched up on one line, we collect
7642     ;; them here.
7643     (if gnus-view-pseudos-separately
7644         ()
7645       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
7646             files action)
7647         (while ps
7648           (setq action (cdr (assq 'action (car ps))))
7649           (setq files (list (cdr (assq 'name (car ps)))))
7650           (while (and ps (cdr ps)
7651                       (string= (or action "1")
7652                                (or (cdr (assq 'action (cadr ps))) "2")))
7653             (setq files (cons (cdr (assq 'name (cadr ps))) files))
7654             (setcdr ps (cddr ps)))
7655           (if (not files)
7656               ()
7657             (if (not (string-match "%s" action))
7658                 (setq files (cons " " files)))
7659             (setq files (cons " " files))
7660             (and (assq 'execute (car ps))
7661                  (setcdr (assq 'execute (car ps))
7662                          (funcall (if (string-match "%s" action)
7663                                       'format 'concat)
7664                                   action
7665                                   (mapconcat (lambda (f) f) files " ")))))
7666           (setq ps (cdr ps)))))
7667     (if (and gnus-view-pseudos (not not-view))
7668         (while pslist
7669           (and (assq 'execute (car pslist))
7670                (gnus-execute-command (cdr (assq 'execute (car pslist)))
7671                                      (eq gnus-view-pseudos 'not-confirm)))
7672           (setq pslist (cdr pslist)))
7673       (save-excursion
7674         (while pslist
7675           (setq after-article (or (cdr (assq 'article (car pslist)))
7676                                   (gnus-summary-article-number)))
7677           (gnus-summary-goto-subject after-article)
7678           (forward-line 1)
7679           (setq b (point))
7680           (insert "    " (file-name-nondirectory
7681                                 (cdr (assq 'name (car pslist))))
7682                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
7683           (setq e (point))
7684           (forward-line -1)             ; back to `b'
7685           (gnus-add-text-properties
7686            b (1- e) (list 'gnus-number gnus-reffed-article-number
7687                           gnus-mouse-face-prop gnus-mouse-face))
7688           (gnus-data-enter
7689            after-article gnus-reffed-article-number
7690            gnus-unread-mark b (car pslist) 0 (- e b))
7691           (push gnus-reffed-article-number gnus-newsgroup-unreads)
7692           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
7693           (setq pslist (cdr pslist)))))))
7694
7695 (defun gnus-pseudos< (p1 p2)
7696   (let ((c1 (cdr (assq 'action p1)))
7697         (c2 (cdr (assq 'action p2))))
7698     (and c1 c2 (string< c1 c2))))
7699
7700 (defun gnus-request-pseudo-article (props)
7701   (cond ((assq 'execute props)
7702          (gnus-execute-command (cdr (assq 'execute props)))))
7703   (let ((gnus-current-article (gnus-summary-article-number)))
7704     (run-hooks 'gnus-mark-article-hook)))
7705
7706 (defun gnus-execute-command (command &optional automatic)
7707   (save-excursion
7708     (gnus-article-setup-buffer)
7709     (set-buffer gnus-article-buffer)
7710     (setq buffer-read-only nil)
7711     (let ((command (if automatic command (read-string "Command: " command)))
7712           ;; Just binding this here doesn't help, because there might
7713           ;; be output from the process after exiting the scope of 
7714           ;; this `let'.
7715           ;; (buffer-read-only nil)
7716           )
7717       (erase-buffer)
7718       (insert "$ " command "\n\n")
7719       (if gnus-view-pseudo-asynchronously
7720           (start-process "gnus-execute" nil shell-file-name
7721                          shell-command-switch command)
7722         (call-process shell-file-name nil t nil
7723                       shell-command-switch command)))))
7724
7725 ;; Summary kill commands.
7726
7727 (defun gnus-summary-edit-global-kill (article)
7728   "Edit the \"global\" kill file."
7729   (interactive (list (gnus-summary-article-number)))
7730   (gnus-set-global-variables)
7731   (gnus-group-edit-global-kill article))
7732
7733 (defun gnus-summary-edit-local-kill ()
7734   "Edit a local kill file applied to the current newsgroup."
7735   (interactive)
7736   (gnus-set-global-variables)
7737   (setq gnus-current-headers (gnus-summary-article-header))
7738   (gnus-set-global-variables)
7739   (gnus-group-edit-local-kill
7740    (gnus-summary-article-number) gnus-newsgroup-name))
7741
7742 ;;; Header reading.
7743
7744 (defun gnus-read-header (id &optional header)
7745   "Read the headers of article ID and enter them into the Gnus system."
7746   (let ((group gnus-newsgroup-name)
7747         (gnus-override-method 
7748          (and (gnus-news-group-p gnus-newsgroup-name)
7749               gnus-refer-article-method))       
7750         where)
7751     ;; First we check to see whether the header in question is already
7752     ;; fetched.
7753     (if (stringp id)
7754         ;; This is a Message-ID.
7755         (setq header (or header (gnus-id-to-header id)))
7756       ;; This is an article number.
7757       (setq header (or header (gnus-summary-article-header id))))
7758     (if (and header
7759              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
7760         ;; We have found the header.
7761         header
7762       ;; We have to really fetch the header to this article.
7763       (when (setq where (gnus-request-head id group))
7764         (save-excursion
7765           (set-buffer nntp-server-buffer)
7766           (goto-char (point-max))
7767           (insert ".\n")
7768           (goto-char (point-min))
7769           (insert "211 ")
7770           (princ (cond
7771                   ((numberp id) id)
7772                   ((cdr where) (cdr where))
7773                   (header (mail-header-number header))
7774                   (t gnus-reffed-article-number))
7775                  (current-buffer))
7776           (insert " Article retrieved.\n"))
7777         ;(when (and header
7778         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
7779         ;  (setcar (gnus-id-to-thread id) nil))
7780         (if (not (setq header (car (gnus-get-newsgroup-headers))))
7781             ()                          ; Malformed head.
7782           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
7783             (if (and (stringp id)
7784                      (not (string= (gnus-group-real-name group)
7785                                    (car where))))
7786                 ;; If we fetched by Message-ID and the article came
7787                 ;; from a different group, we fudge some bogus article
7788                 ;; numbers for this article.
7789                 (mail-header-set-number header gnus-reffed-article-number))
7790             (decf gnus-reffed-article-number)
7791             (gnus-remove-header (mail-header-number header))
7792             (push header gnus-newsgroup-headers)
7793             (setq gnus-current-headers header)
7794             (push (mail-header-number header) gnus-newsgroup-limit))
7795           header)))))
7796
7797 (defun gnus-remove-header (number)
7798   "Remove header NUMBER from `gnus-newsgroup-headers'."
7799   (if (and gnus-newsgroup-headers
7800            (= number (mail-header-number (car gnus-newsgroup-headers))))
7801       (pop gnus-newsgroup-headers)
7802     (let ((headers gnus-newsgroup-headers))
7803       (while (and (cdr headers)
7804                   (not (= number (mail-header-number (cadr headers)))))
7805         (pop headers))
7806       (when (cdr headers)
7807         (setcdr headers (cddr headers))))))
7808
7809 ;;;
7810 ;;; summary highlights
7811 ;;;
7812
7813 (defun gnus-highlight-selected-summary ()
7814   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7815   ;; Highlight selected article in summary buffer
7816   (when gnus-summary-selected-face
7817     (save-excursion
7818       (let* ((beg (progn (beginning-of-line) (point)))
7819              (end (progn (end-of-line) (point)))
7820              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
7821              (from (if (get-text-property beg gnus-mouse-face-prop) 
7822                        beg
7823                      (or (next-single-property-change 
7824                           beg gnus-mouse-face-prop nil end) 
7825                          beg)))
7826              (to
7827               (if (= from end)
7828                   (- from 2)
7829                 (or (next-single-property-change
7830                      from gnus-mouse-face-prop nil end)
7831                     end))))
7832         ;; If no mouse-face prop on line we will have to = from = end,
7833         ;; so we highlight the entire line instead.
7834         (when (= (+ to 2) from)
7835           (setq from beg)
7836           (setq to end))
7837         (if gnus-newsgroup-selected-overlay
7838             ;; Move old overlay.
7839             (gnus-move-overlay
7840              gnus-newsgroup-selected-overlay from to (current-buffer))
7841           ;; Create new overlay.
7842           (gnus-overlay-put
7843            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
7844            'face gnus-summary-selected-face))))))
7845
7846 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
7847 (defun gnus-summary-highlight-line ()
7848   "Highlight current line according to `gnus-summary-highlight'."
7849   (let* ((list gnus-summary-highlight)
7850          (p (point))
7851          (end (progn (end-of-line) (point)))
7852          ;; now find out where the line starts and leave point there.
7853          (beg (progn (beginning-of-line) (point)))
7854          (article (gnus-summary-article-number))
7855          (score (or (cdr (assq (or article gnus-current-article)
7856                                gnus-newsgroup-scored))
7857                     gnus-summary-default-score 0))
7858          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
7859          (inhibit-read-only t))
7860     ;; Eval the cars of the lists until we find a match.
7861     (let ((default gnus-summary-default-score))
7862       (while (and list
7863                   (not (eval (caar list))))
7864         (setq list (cdr list))))
7865     (let ((face (cdar list)))
7866       (unless (eq face (get-text-property beg 'face))
7867         (gnus-put-text-property 
7868          beg end 'face 
7869          (setq face (if (boundp face) (symbol-value face) face)))
7870         (when gnus-summary-highlight-line-function
7871           (funcall gnus-summary-highlight-line-function article face))))
7872     (goto-char p)))
7873
7874 (defun gnus-update-read-articles (group unread)
7875   "Update the list of read articles in GROUP."
7876   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
7877          (entry (gnus-gethash group gnus-newsrc-hashtb))
7878          (info (nth 2 entry))
7879          (prev 1)
7880          (unread (sort (copy-sequence unread) '<))
7881          read)
7882     (if (or (not info) (not active))
7883         ;; There is no info on this group if it was, in fact,
7884         ;; killed.  Gnus stores no information on killed groups, so
7885         ;; there's nothing to be done.
7886         ;; One could store the information somewhere temporarily,
7887         ;; perhaps...  Hmmm...
7888         ()
7889       ;; Remove any negative articles numbers.
7890       (while (and unread (< (car unread) 0))
7891         (setq unread (cdr unread)))
7892       ;; Remove any expired article numbers
7893       (while (and unread (< (car unread) (car active)))
7894         (setq unread (cdr unread)))
7895       ;; Compute the ranges of read articles by looking at the list of
7896       ;; unread articles.
7897       (while unread
7898         (if (/= (car unread) prev)
7899             (setq read (cons (if (= prev (1- (car unread))) prev
7900                                (cons prev (1- (car unread)))) read)))
7901         (setq prev (1+ (car unread)))
7902         (setq unread (cdr unread)))
7903       (when (<= prev (cdr active))
7904         (setq read (cons (cons prev (cdr active)) read)))
7905       (gnus-undo-register
7906         `(progn
7907            (gnus-info-set-marks ,info ,(gnus-info-marks info))
7908            (gnus-info-set-read ,info ,(gnus-info-read info))
7909            (gnus-get-unread-articles-in-group ,info (gnus-active ,group))))
7910       ;; Enter this list into the group info.
7911       (gnus-info-set-read
7912        info (if (> (length read) 1) (nreverse read) read))
7913       ;; Set the number of unread articles in gnus-newsrc-hashtb.
7914       (gnus-get-unread-articles-in-group info (gnus-active group))
7915       t)))
7916
7917 (provide 'gnus-sum)
7918
7919 ;;; gnus-sum.el ends here