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