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