*** 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            (save-excursion
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   "Return the number of articles in THREAD.  
2097 This may be 0 in some cases -- if none of the articles in
2098 the thread are to be displayed."
2099   (let* ((number
2100           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2101           (cond
2102            ((not (listp thread))
2103             1)
2104            ((and (consp thread) (cdr thread))
2105             (apply
2106              '+ 1 (mapcar
2107                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2108            ((null thread)
2109             1)
2110            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2111             1)
2112            (t 0))))
2113     (when (and level (zerop level) gnus-tmp-new-adopts)
2114       (incf number
2115             (apply '+ (mapcar
2116                        'gnus-summary-number-of-articles-in-thread
2117                        gnus-tmp-new-adopts))))
2118     (if char
2119         (if (> number 1) gnus-not-empty-thread-mark
2120           gnus-empty-thread-mark)
2121       number)))
2122
2123 (defun gnus-summary-set-local-parameters (group)
2124  "Go through the local params of GROUP and set all variable specs in that list."
2125   (let ((params (gnus-group-find-parameter group))
2126         elem)
2127     (while params
2128       (setq elem (car params)
2129             params (cdr params))
2130       (and (consp elem)                 ; Has to be a cons.
2131            (consp (cdr elem))           ; The cdr has to be a list.
2132            (symbolp (car elem))         ; Has to be a symbol in there.
2133            (not (memq (car elem) 
2134                       '(quit-config to-address to-list to-group)))
2135            (progn                       ; So we set it.
2136              (make-local-variable (car elem))
2137              (set (car elem) (eval (nth 1 elem))))))))
2138
2139 (defun gnus-summary-read-group (group &optional show-all no-article
2140                                       kill-buffer no-display)
2141   "Start reading news in newsgroup GROUP.
2142 If SHOW-ALL is non-nil, already read articles are also listed.
2143 If NO-ARTICLE is non-nil, no article is selected initially.
2144 If NO-DISPLAY, don't generate a summary buffer."
2145   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2146   (let* ((new-group (gnus-summary-setup-buffer group))
2147          (quit-config (gnus-group-quit-config group))
2148          (did-select (and new-group (gnus-select-newsgroup group show-all))))
2149     (cond
2150      ;; This summary buffer exists already, so we just select it.
2151      ((not new-group)
2152       (gnus-set-global-variables)
2153       (when kill-buffer
2154         (gnus-kill-or-deaden-summary kill-buffer))
2155       (gnus-configure-windows 'summary 'force)
2156       (gnus-set-mode-line 'summary)
2157       (gnus-summary-position-point)
2158       (message "")
2159       t)
2160      ;; We couldn't select this group.
2161      ((null did-select)
2162       (when (and (eq major-mode 'gnus-summary-mode)
2163                  (not (equal (current-buffer) kill-buffer)))
2164         (kill-buffer (current-buffer))
2165         (if (not quit-config)
2166             (progn
2167               (set-buffer gnus-group-buffer)
2168               (gnus-group-jump-to-group group)
2169               (gnus-group-next-unread-group 1))
2170           (if (not (buffer-name (car quit-config)))
2171               (gnus-configure-windows 'group 'force)
2172             (set-buffer (car quit-config))
2173             (and (eq major-mode 'gnus-summary-mode)
2174                  (gnus-set-global-variables))
2175             (gnus-configure-windows (cdr quit-config)))))
2176       (gnus-message 3 "Can't select group")
2177       nil)
2178      ;; The user did a `C-g' while prompting for number of articles,
2179      ;; so we exit this group.
2180      ((eq did-select 'quit)
2181       (and (eq major-mode 'gnus-summary-mode)
2182            (not (equal (current-buffer) kill-buffer))
2183            (kill-buffer (current-buffer)))
2184       (when kill-buffer
2185         (gnus-kill-or-deaden-summary kill-buffer))
2186       (if (not quit-config)
2187           (progn
2188             (set-buffer gnus-group-buffer)
2189             (gnus-group-jump-to-group group)
2190             (gnus-group-next-unread-group 1)
2191             (gnus-configure-windows 'group 'force))
2192         (if (not (buffer-name (car quit-config)))
2193             (gnus-configure-windows 'group 'force)
2194           (set-buffer (car quit-config))
2195           (and (eq major-mode 'gnus-summary-mode)
2196                (gnus-set-global-variables))
2197           (gnus-configure-windows (cdr quit-config))))
2198       ;; Finally signal the quit.
2199       (signal 'quit nil))
2200      ;; The group was successfully selected.
2201      (t
2202       (gnus-set-global-variables)
2203       ;; Save the active value in effect when the group was entered.
2204       (setq gnus-newsgroup-active
2205             (gnus-copy-sequence
2206              (gnus-active gnus-newsgroup-name)))
2207       ;; You can change the summary buffer in some way with this hook.
2208       (run-hooks 'gnus-select-group-hook)
2209       ;; Set any local variables in the group parameters.
2210       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2211       (gnus-update-format-specifications
2212        nil 'summary 'summary-mode 'summary-dummy)
2213       ;; Do score processing.
2214       (when gnus-use-scoring
2215         (gnus-possibly-score-headers))
2216       ;; Check whether to fill in the gaps in the threads.
2217       (when gnus-build-sparse-threads
2218         (gnus-build-sparse-threads))
2219       ;; Find the initial limit.
2220       (if gnus-show-threads
2221           (if show-all
2222               (let ((gnus-newsgroup-dormant nil))
2223                 (gnus-summary-initial-limit show-all))
2224             (gnus-summary-initial-limit show-all))
2225         (setq gnus-newsgroup-limit 
2226               (mapcar 
2227                (lambda (header) (mail-header-number header))
2228                gnus-newsgroup-headers)))
2229       ;; Generate the summary buffer.
2230       (unless no-display
2231         (gnus-summary-prepare))
2232       (when gnus-use-trees
2233         (gnus-tree-open group)
2234         (setq gnus-summary-highlight-line-function
2235               'gnus-tree-highlight-article))
2236       ;; If the summary buffer is empty, but there are some low-scored
2237       ;; articles or some excluded dormants, we include these in the
2238       ;; buffer.
2239       (when (and (zerop (buffer-size))
2240                  (not no-display))
2241         (cond (gnus-newsgroup-dormant
2242                (gnus-summary-limit-include-dormant))
2243               ((and gnus-newsgroup-scored show-all)
2244                (gnus-summary-limit-include-expunged t))))
2245       ;; Function `gnus-apply-kill-file' must be called in this hook.
2246       (run-hooks 'gnus-apply-kill-hook)
2247       (if (and (zerop (buffer-size))
2248                (not no-display))
2249           (progn
2250             ;; This newsgroup is empty.
2251             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
2252             (gnus-message 6 "No unread news")
2253             (when kill-buffer
2254               (gnus-kill-or-deaden-summary kill-buffer))
2255             ;; Return nil from this function.
2256             nil)
2257         ;; Hide conversation thread subtrees.  We cannot do this in
2258         ;; gnus-summary-prepare-hook since kill processing may not
2259         ;; work with hidden articles.
2260         (and gnus-show-threads
2261              gnus-thread-hide-subtree
2262              (gnus-summary-hide-all-threads))
2263         ;; Show first unread article if requested.
2264         (if (and (not no-article)
2265                  (not no-display)
2266                  gnus-newsgroup-unreads
2267                  gnus-auto-select-first)
2268             (unless (if (eq gnus-auto-select-first 'best)
2269                         (gnus-summary-best-unread-article)
2270                       (gnus-summary-first-unread-article))
2271               (gnus-configure-windows 'summary))
2272           ;; Don't select any articles, just move point to the first
2273           ;; article in the group.
2274           (goto-char (point-min))
2275           (gnus-summary-position-point)
2276           (gnus-set-mode-line 'summary)
2277           (gnus-configure-windows 'summary 'force))
2278         (when kill-buffer
2279           (gnus-kill-or-deaden-summary kill-buffer))
2280         (when (get-buffer-window gnus-group-buffer t)
2281           ;; Gotta use windows, because recenter does wierd stuff if
2282           ;; the current buffer ain't the displayed window.
2283           (let ((owin (selected-window)))
2284             (select-window (get-buffer-window gnus-group-buffer t))
2285             (when (gnus-group-goto-group group)
2286               (recenter))
2287             (select-window owin))))
2288       ;; Mark this buffer as "prepared".
2289       (setq gnus-newsgroup-prepared t)
2290       t))))
2291
2292 (defun gnus-summary-prepare ()
2293   "Generate the summary buffer."
2294   (interactive)
2295   (let ((buffer-read-only nil))
2296     (erase-buffer)
2297     (setq gnus-newsgroup-data nil
2298           gnus-newsgroup-data-reverse nil)
2299     (run-hooks 'gnus-summary-generate-hook)
2300     ;; Generate the buffer, either with threads or without.
2301     (when gnus-newsgroup-headers
2302       (gnus-summary-prepare-threads
2303        (if gnus-show-threads
2304            (gnus-sort-gathered-threads
2305             (funcall gnus-summary-thread-gathering-function
2306                      (gnus-sort-threads
2307                       (gnus-cut-threads (gnus-make-threads)))))
2308          ;; Unthreaded display.
2309          (gnus-sort-articles gnus-newsgroup-headers))))
2310     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2311     ;; Call hooks for modifying summary buffer.
2312     (goto-char (point-min))
2313     (run-hooks 'gnus-summary-prepare-hook)))
2314
2315 (defsubst gnus-general-simplify-subject (subject)
2316   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2317   (setq subject
2318         (cond
2319          ;; Truncate the subject.
2320          ((numberp gnus-summary-gather-subject-limit)
2321           (setq subject (gnus-simplify-subject-re subject))
2322           (if (> (length subject) gnus-summary-gather-subject-limit)
2323               (substring subject 0 gnus-summary-gather-subject-limit)
2324             subject))
2325          ;; Fuzzily simplify it.
2326          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2327           (gnus-simplify-subject-fuzzy subject))
2328          ;; Just remove the leading "Re:".
2329          (t
2330           (gnus-simplify-subject-re subject))))
2331   
2332   (if (and gnus-summary-gather-exclude-subject
2333            (string-match gnus-summary-gather-exclude-subject subject))
2334       nil                               ; This article shouldn't be gathered
2335     subject))
2336
2337 (defun gnus-summary-simplify-subject-query ()
2338   "Query where the respool algorithm would put this article."
2339   (interactive)
2340   (gnus-set-global-variables)
2341   (gnus-summary-select-article)
2342   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2343
2344 (defun gnus-gather-threads-by-subject (threads)
2345   "Gather threads by looking at Subject headers."
2346   (if (not gnus-summary-make-false-root)
2347       threads
2348     (let ((hashtb (gnus-make-hashtable 1023))
2349           (prev threads)
2350           (result threads)
2351           subject hthread whole-subject)
2352       (while threads
2353         (setq subject (gnus-general-simplify-subject
2354                        (setq whole-subject (mail-header-subject 
2355                                             (caar threads)))))
2356         (if subject
2357             (if (setq hthread (gnus-gethash subject hashtb))
2358                 (progn
2359                   ;; We enter a dummy root into the thread, if we
2360                   ;; haven't done that already.
2361                   (unless (stringp (caar hthread))
2362                     (setcar hthread (list whole-subject (car hthread))))
2363                   ;; We add this new gathered thread to this gathered
2364                   ;; thread.
2365                   (setcdr (car hthread)
2366                           (nconc (cdar hthread) (list (car threads))))
2367                   ;; Remove it from the list of threads.
2368                   (setcdr prev (cdr threads))
2369                   (setq threads prev))
2370               ;; Enter this thread into the hash table.
2371               (gnus-sethash subject threads hashtb)))
2372         (setq prev threads)
2373         (setq threads (cdr threads)))
2374       result)))
2375
2376 (defun gnus-gather-threads-by-references (threads)
2377   "Gather threads by looking at References headers."
2378   (let ((idhashtb (gnus-make-hashtable 1023))
2379         (thhashtb (gnus-make-hashtable 1023))
2380         (prev threads)
2381         (result threads)
2382         ids references id gthread gid entered)
2383     (while threads
2384       (when (setq references (mail-header-references (caar threads)))
2385         (setq id (mail-header-id (caar threads))
2386               ids (gnus-split-references references)
2387               entered nil)
2388         (while ids
2389           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
2390               (progn
2391                 (gnus-sethash (car ids) id idhashtb)
2392                 (gnus-sethash id threads thhashtb))
2393             (setq gthread (gnus-gethash gid thhashtb))
2394             (unless entered
2395               ;; We enter a dummy root into the thread, if we
2396               ;; haven't done that already.
2397               (unless (stringp (caar gthread))
2398                 (setcar gthread (list (mail-header-subject (caar gthread))
2399                                       (car gthread))))
2400               ;; We add this new gathered thread to this gathered
2401               ;; thread.
2402               (setcdr (car gthread)
2403                       (nconc (cdar gthread) (list (car threads)))))
2404             ;; Add it into the thread hash table.
2405             (gnus-sethash id gthread thhashtb)
2406             (setq entered t)
2407             ;; Remove it from the list of threads.
2408             (setcdr prev (cdr threads))
2409             (setq threads prev))
2410           (setq ids (cdr ids))))
2411       (setq prev threads)
2412       (setq threads (cdr threads)))
2413     result))
2414
2415 (defun gnus-sort-gathered-threads (threads)
2416   "Sort subtreads inside each gathered thread by article number."
2417   (let ((result threads))
2418     (while threads
2419       (when (stringp (caar threads))
2420         (setcdr (car threads)
2421                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2422       (setq threads (cdr threads)))
2423     result))
2424
2425 (defun gnus-make-threads ()
2426   "Go through the dependency hashtb and find the roots.  Return all threads."
2427   (let (threads)
2428     (mapatoms
2429      (lambda (refs)
2430        (unless (car (symbol-value refs))
2431          ;; These threads do not refer back to any other articles,
2432          ;; so they're roots.
2433          (setq threads (append (cdr (symbol-value refs)) threads))))
2434      gnus-newsgroup-dependencies)
2435     threads))
2436
2437 (defun gnus-build-sparse-threads ()
2438   (let ((headers gnus-newsgroup-headers)
2439         (deps gnus-newsgroup-dependencies)
2440         header references generation relations 
2441         cthread subject child end pthread relation)
2442     ;; First we create an alist of generations/relations, where 
2443     ;; generations is how much we trust the ralation, and the relation
2444     ;; is parent/child.
2445     (gnus-message 7 "Making sparse threads...")
2446     (save-excursion
2447       (nnheader-set-temp-buffer " *gnus sparse threads*")
2448       (while (setq header (pop headers))
2449         (when (and (setq references (mail-header-references header))
2450                    (not (string= references "")))
2451           (insert references)
2452           (setq child (mail-header-id header)
2453                 subject (mail-header-subject header))
2454           (setq generation 0)
2455           (while (search-backward ">" nil t)
2456             (setq end (1+ (point)))
2457             (when (search-backward "<" nil t)
2458               (push (list (incf generation) 
2459                           child (setq child (buffer-substring (point) end))
2460                           subject)
2461                     relations)))
2462           (push (list (1+ generation) child nil subject) relations)
2463           (erase-buffer)))
2464       (kill-buffer (current-buffer)))
2465     ;; Sort over trustworthiness.
2466     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
2467     (while (setq relation (pop relations))
2468       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
2469                 (unless (car (symbol-value cthread))
2470                   ;; Make this article the parent of these threads.
2471                   (setcar (symbol-value cthread)
2472                           (vector gnus-reffed-article-number 
2473                                   (cadddr relation) 
2474                                   "" ""
2475                                   (cadr relation) 
2476                                   (or (caddr relation) "") 0 0 "")))
2477               (set cthread (list (vector gnus-reffed-article-number
2478                                          (cadddr relation) 
2479                                          "" "" (cadr relation) 
2480                                          (or (caddr relation) "") 0 0 ""))))
2481         (push gnus-reffed-article-number gnus-newsgroup-limit)
2482         (push gnus-reffed-article-number gnus-newsgroup-sparse)
2483         (push (cons gnus-reffed-article-number gnus-sparse-mark)
2484               gnus-newsgroup-reads)
2485         (decf gnus-reffed-article-number)
2486         ;; Make this new thread the child of its parent.
2487         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
2488             (setcdr (symbol-value pthread)
2489                     (nconc (cdr (symbol-value pthread))
2490                            (list (symbol-value cthread))))
2491           (set pthread (list nil (symbol-value cthread))))))
2492     (gnus-message 7 "Making sparse threads...done")))
2493
2494 (defun gnus-build-old-threads ()
2495   ;; Look at all the articles that refer back to old articles, and
2496   ;; fetch the headers for the articles that aren't there.  This will
2497   ;; build complete threads - if the roots haven't been expired by the
2498   ;; server, that is.
2499   (let (id heads)
2500     (mapatoms
2501      (lambda (refs)
2502        (when (not (car (symbol-value refs)))
2503          (setq heads (cdr (symbol-value refs)))
2504          (while heads
2505            (if (memq (mail-header-number (caar heads))
2506                      gnus-newsgroup-dormant)
2507                (setq heads (cdr heads))
2508              (setq id (symbol-name refs))
2509              (while (and (setq id (gnus-build-get-header id))
2510                          (not (car (gnus-gethash
2511                                     id gnus-newsgroup-dependencies)))))
2512              (setq heads nil)))))
2513      gnus-newsgroup-dependencies)))
2514
2515 (defun gnus-build-get-header (id)
2516   ;; Look through the buffer of NOV lines and find the header to
2517   ;; ID.  Enter this line into the dependencies hash table, and return
2518   ;; the id of the parent article (if any).
2519   (let ((deps gnus-newsgroup-dependencies)
2520         found header)
2521     (prog1
2522         (save-excursion
2523           (set-buffer nntp-server-buffer)
2524           (goto-char (point-min))
2525           (while (and (not found) (search-forward id nil t))
2526             (beginning-of-line)
2527             (setq found (looking-at
2528                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
2529                                  (regexp-quote id))))
2530             (or found (beginning-of-line 2)))
2531           (when found
2532             (beginning-of-line)
2533             (and
2534              (setq header (gnus-nov-parse-line
2535                            (read (current-buffer)) deps))
2536              (gnus-parent-id (mail-header-references header)))))
2537       (when header
2538         (let ((number (mail-header-number header)))
2539           (push number gnus-newsgroup-limit)
2540           (push header gnus-newsgroup-headers)
2541           (if (memq number gnus-newsgroup-unselected)
2542               (progn
2543                 (push number gnus-newsgroup-unreads)
2544                 (setq gnus-newsgroup-unselected
2545                       (delq number gnus-newsgroup-unselected)))
2546             (push number gnus-newsgroup-ancient)))))))
2547
2548 (defun gnus-summary-update-article-line (article header)
2549   "Update the line for ARTICLE using HEADERS."
2550   (let* ((id (mail-header-id header))
2551          (thread (gnus-id-to-thread id)))
2552     (unless thread
2553       (error "Article in no thread"))
2554     ;; Update the thread.
2555     (setcar thread header)
2556     (gnus-summary-goto-subject article)
2557     (let* ((datal (gnus-data-find-list article))
2558            (data (car datal))
2559            (length (when (cdr datal)
2560                      (- (gnus-data-pos data)
2561                         (gnus-data-pos (cadr datal)))))
2562            (buffer-read-only nil)
2563            (level (gnus-summary-thread-level)))
2564       (gnus-delete-line)
2565       (gnus-summary-insert-line
2566        header level nil (gnus-article-mark article)
2567        (memq article gnus-newsgroup-replied)
2568        (memq article gnus-newsgroup-expirable)
2569        (mail-header-subject header)
2570        nil (cdr (assq article gnus-newsgroup-scored))
2571        (memq article gnus-newsgroup-processable))
2572       (when length
2573         (gnus-data-update-list
2574          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
2575      
2576 (defun gnus-summary-update-article (article &optional iheader)
2577   "Update ARTICLE in the summary buffer."
2578   (set-buffer gnus-summary-buffer)
2579   (let* ((header (or iheader (gnus-summary-article-header article)))
2580          (id (mail-header-id header))
2581          (data (gnus-data-find article))
2582          (thread (gnus-id-to-thread id))
2583          (references (mail-header-references header))
2584          (parent
2585           (gnus-id-to-thread
2586            (or (gnus-parent-id 
2587                 (if (and references
2588                          (not (equal "" references)))
2589                     references))
2590                "none")))
2591          (buffer-read-only nil)
2592          (old (car thread))
2593          (number (mail-header-number header))
2594          pos)
2595     (when thread
2596       ;; !!! Should this be in or not?
2597       (unless iheader
2598         (setcar thread nil))
2599       (when parent
2600         (delq thread parent))
2601       (if (gnus-summary-insert-subject id header iheader)
2602           ;; Set the (possibly) new article number in the data structure.
2603           (gnus-data-set-number data (gnus-id-to-article id))
2604         (setcar thread old)
2605         nil))))
2606
2607 (defun gnus-rebuild-thread (id)
2608   "Rebuild the thread containing ID."
2609   (let ((buffer-read-only nil)
2610         current thread data)
2611     (if (not gnus-show-threads)
2612         (setq thread (list (car (gnus-id-to-thread id))))
2613       ;; Get the thread this article is part of.
2614       (setq thread (gnus-remove-thread id)))
2615     (setq current (save-excursion
2616                     (and (zerop (forward-line -1))
2617                          (gnus-summary-article-number))))
2618     ;; If this is a gathered thread, we have to go some re-gathering.
2619     (when (stringp (car thread))
2620       (let ((subject (car thread))
2621             roots thr)
2622         (setq thread (cdr thread))
2623         (while thread
2624           (unless (memq (setq thr (gnus-id-to-thread
2625                                       (gnus-root-id
2626                                        (mail-header-id (caar thread)))))
2627                         roots)
2628             (push thr roots))
2629           (setq thread (cdr thread)))
2630         ;; We now have all (unique) roots.
2631         (if (= (length roots) 1)
2632             ;; All the loose roots are now one solid root.
2633             (setq thread (car roots))
2634           (setq thread (cons subject (gnus-sort-threads roots))))))
2635     (let (threads)
2636       ;; We then insert this thread into the summary buffer.
2637       (let (gnus-newsgroup-data gnus-newsgroup-threads)
2638         (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
2639         (setq data (nreverse gnus-newsgroup-data))
2640         (setq threads gnus-newsgroup-threads))
2641       ;; We splice the new data into the data structure.
2642       (gnus-data-enter-list current data)
2643       (gnus-data-compute-positions)
2644       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
2645
2646 (defun gnus-number-to-header (number)
2647   "Return the header for article NUMBER."
2648   (let ((headers gnus-newsgroup-headers))
2649     (while (and headers
2650                 (not (= number (mail-header-number (car headers)))))
2651       (pop headers))
2652     (when headers
2653       (car headers))))
2654
2655 (defun gnus-parent-headers (headers &optional generation)
2656   "Return the headers of the GENERATIONeth parent of HEADERS."
2657   (unless generation 
2658     (setq generation 1))
2659   (let (references parent)
2660     (while (and headers (not (zerop generation)))
2661       (setq references (mail-header-references headers))
2662       (when (and references
2663                  (setq parent (gnus-parent-id references))
2664                  (setq headers (car (gnus-id-to-thread parent))))
2665         (decf generation)))
2666     headers))
2667
2668 (defun gnus-id-to-thread (id)
2669   "Return the (sub-)thread where ID appears."
2670   (gnus-gethash id gnus-newsgroup-dependencies))
2671
2672 (defun gnus-id-to-article (id)
2673   "Return the article number of ID."
2674   (let ((thread (gnus-id-to-thread id)))
2675     (when (and thread
2676                (car thread))
2677       (mail-header-number (car thread)))))
2678
2679 (defun gnus-id-to-header (id)
2680   "Return the article headers of ID."
2681   (car (gnus-id-to-thread id)))
2682
2683 (defun gnus-article-displayed-root-p (article)
2684   "Say whether ARTICLE is a root(ish) article."
2685   (let ((level (gnus-summary-thread-level article))
2686         (refs (mail-header-references  (gnus-summary-article-header article)))
2687         particle)
2688     (cond 
2689      ((null level) nil)
2690      ((zerop level) t)
2691      ((null refs) t)
2692      ((null (gnus-parent-id refs)) t)
2693      ((and (= 1 level)
2694            (null (setq particle (gnus-id-to-article
2695                                  (gnus-parent-id refs))))
2696            (null (gnus-summary-thread-level particle)))))))
2697
2698 (defun gnus-root-id (id)
2699   "Return the id of the root of the thread where ID appears."
2700   (let (last-id prev)
2701     (while (and id (setq prev (car (gnus-gethash 
2702                                     id gnus-newsgroup-dependencies))))
2703       (setq last-id id
2704             id (gnus-parent-id (mail-header-references prev))))
2705     last-id))
2706
2707 (defun gnus-remove-thread (id &optional dont-remove)
2708   "Remove the thread that has ID in it."
2709   (let ((dep gnus-newsgroup-dependencies)
2710         headers thread last-id)
2711     ;; First go up in this thread until we find the root.
2712     (setq last-id (gnus-root-id id))
2713     (setq headers (list (car (gnus-id-to-thread last-id))
2714                         (caadr (gnus-id-to-thread last-id))))
2715     ;; We have now found the real root of this thread.  It might have
2716     ;; been gathered into some loose thread, so we have to search
2717     ;; through the threads to find the thread we wanted.
2718     (let ((threads gnus-newsgroup-threads)
2719           sub)
2720       (while threads
2721         (setq sub (car threads))
2722         (if (stringp (car sub))
2723             ;; This is a gathered thread, so we look at the roots
2724             ;; below it to find whether this article is in this
2725             ;; gathered root.
2726             (progn
2727               (setq sub (cdr sub))
2728               (while sub
2729                 (when (member (caar sub) headers)
2730                   (setq thread (car threads)
2731                         threads nil
2732                         sub nil))
2733                 (setq sub (cdr sub))))
2734           ;; It's an ordinary thread, so we check it.
2735           (when (eq (car sub) (car headers))
2736             (setq thread sub
2737                   threads nil)))
2738         (setq threads (cdr threads)))
2739       ;; If this article is in no thread, then it's a root.
2740       (if thread
2741           (unless dont-remove
2742             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
2743         (setq thread (gnus-gethash last-id dep)))
2744       (when thread
2745         (prog1
2746             thread ; We return this thread.
2747           (unless dont-remove
2748             (if (stringp (car thread))
2749                 (progn
2750                   ;; If we use dummy roots, then we have to remove the
2751                   ;; dummy root as well.
2752                   (when (eq gnus-summary-make-false-root 'dummy)
2753                     ;; Uhm.
2754                     )
2755                   (setq thread (cdr thread))
2756                   (while thread
2757                     (gnus-remove-thread-1 (car thread))
2758                     (setq thread (cdr thread))))
2759               (gnus-remove-thread-1 thread))))))))
2760
2761 (defun gnus-remove-thread-1 (thread)
2762   "Remove the thread THREAD recursively."
2763   (let ((number (mail-header-number (car thread)))
2764         pos)
2765     (when (setq pos (text-property-any
2766                      (point-min) (point-max) 'gnus-number number))
2767       (goto-char pos)
2768       (gnus-delete-line)
2769       (gnus-data-remove number))
2770     (setq thread (cdr thread))
2771     (while thread
2772       (gnus-remove-thread-1 (pop thread)))))
2773
2774 (defun gnus-sort-threads (threads)
2775   "Sort THREADS."
2776   (if (not gnus-thread-sort-functions)
2777       threads
2778     (let ((func (if (= 1 (length gnus-thread-sort-functions))
2779                     (car gnus-thread-sort-functions)
2780                   `(lambda (t1 t2)
2781                      ,(gnus-make-sort-function 
2782                        (reverse gnus-thread-sort-functions))))))
2783       (gnus-message 7 "Sorting threads...")
2784       (prog1
2785           (sort threads func)
2786         (gnus-message 7 "Sorting threads...done")))))
2787
2788 (defun gnus-sort-articles (articles)
2789   "Sort ARTICLES."
2790   (when gnus-article-sort-functions
2791     (let ((func (if (= 1 (length gnus-article-sort-functions))
2792                     (car gnus-article-sort-functions)
2793                   `(lambda (t1 t2)
2794                      ,(gnus-make-sort-function 
2795                        (reverse gnus-article-sort-functions))))))
2796       (gnus-message 7 "Sorting articles...")
2797       (prog1
2798           (setq gnus-newsgroup-headers (sort articles func))
2799         (gnus-message 7 "Sorting articles...done")))))
2800
2801 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2802 (defmacro gnus-thread-header (thread)
2803   ;; Return header of first article in THREAD.
2804   ;; Note that THREAD must never, ever be anything else than a variable -
2805   ;; using some other form will lead to serious barfage.
2806   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
2807   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
2808   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
2809         (vector thread) 2))
2810
2811 (defsubst gnus-article-sort-by-number (h1 h2)
2812   "Sort articles by article number."
2813   (< (mail-header-number h1)
2814      (mail-header-number h2)))
2815
2816 (defun gnus-thread-sort-by-number (h1 h2)
2817   "Sort threads by root article number."
2818   (gnus-article-sort-by-number
2819    (gnus-thread-header h1) (gnus-thread-header h2)))
2820
2821 (defsubst gnus-article-sort-by-author (h1 h2)
2822   "Sort articles by root author."
2823   (string-lessp
2824    (let ((extract (funcall
2825                    gnus-extract-address-components
2826                    (mail-header-from h1))))
2827      (or (car extract) (cdr extract)))
2828    (let ((extract (funcall
2829                    gnus-extract-address-components
2830                    (mail-header-from h2))))
2831      (or (car extract) (cdr extract)))))
2832
2833 (defun gnus-thread-sort-by-author (h1 h2)
2834   "Sort threads by root author."
2835   (gnus-article-sort-by-author
2836    (gnus-thread-header h1)  (gnus-thread-header h2)))
2837
2838 (defsubst gnus-article-sort-by-subject (h1 h2)
2839   "Sort articles by root subject."
2840   (string-lessp
2841    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
2842    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
2843
2844 (defun gnus-thread-sort-by-subject (h1 h2)
2845   "Sort threads by root subject."
2846   (gnus-article-sort-by-subject
2847    (gnus-thread-header h1) (gnus-thread-header h2)))
2848
2849 (defsubst gnus-article-sort-by-date (h1 h2)
2850   "Sort articles by root article date."
2851   (string-lessp
2852    (inline (gnus-sortable-date (mail-header-date h1)))
2853    (inline (gnus-sortable-date (mail-header-date h2)))))
2854
2855 (defun gnus-thread-sort-by-date (h1 h2)
2856   "Sort threads by root article date."
2857   (gnus-article-sort-by-date
2858    (gnus-thread-header h1) (gnus-thread-header h2)))
2859
2860 (defsubst gnus-article-sort-by-score (h1 h2)
2861   "Sort articles by root article score.
2862 Unscored articles will be counted as having a score of zero."
2863   (> (or (cdr (assq (mail-header-number h1)
2864                     gnus-newsgroup-scored))
2865          gnus-summary-default-score 0)
2866      (or (cdr (assq (mail-header-number h2)
2867                     gnus-newsgroup-scored))
2868          gnus-summary-default-score 0)))
2869
2870 (defun gnus-thread-sort-by-score (h1 h2)
2871   "Sort threads by root article score."
2872   (gnus-article-sort-by-score
2873    (gnus-thread-header h1) (gnus-thread-header h2)))
2874
2875 (defun gnus-thread-sort-by-total-score (h1 h2)
2876   "Sort threads by the sum of all scores in the thread.
2877 Unscored articles will be counted as having a score of zero."
2878   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
2879
2880 (defun gnus-thread-total-score (thread)
2881   ;;  This function find the total score of THREAD.
2882   (cond ((null thread)
2883          0)
2884         ((consp thread)
2885          (if (stringp (car thread))
2886              (apply gnus-thread-score-function 0
2887                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
2888            (gnus-thread-total-score-1 thread)))
2889         (t
2890          (gnus-thread-total-score-1 (list thread)))))
2891
2892 (defun gnus-thread-total-score-1 (root)
2893   ;; This function find the total score of the thread below ROOT.
2894   (setq root (car root))
2895   (apply gnus-thread-score-function
2896          (or (append
2897               (mapcar 'gnus-thread-total-score
2898                       (cdr (gnus-gethash (mail-header-id root)
2899                                          gnus-newsgroup-dependencies)))
2900                  (if (> (mail-header-number root) 0)
2901                      (list (or (cdr (assq (mail-header-number root) 
2902                                           gnus-newsgroup-scored))
2903                                gnus-summary-default-score 0))))
2904              (list gnus-summary-default-score)
2905              '(0))))
2906
2907 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
2908 (defvar gnus-tmp-prev-subject nil)
2909 (defvar gnus-tmp-false-parent nil)
2910 (defvar gnus-tmp-root-expunged nil)
2911 (defvar gnus-tmp-dummy-line nil)
2912
2913 (defun gnus-summary-prepare-threads (threads)
2914   "Prepare summary buffer from THREADS and indentation LEVEL.
2915 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
2916 or a straight list of headers."
2917   (gnus-message 7 "Generating summary...")
2918
2919   (setq gnus-newsgroup-threads threads)
2920   (beginning-of-line)
2921
2922   (let ((gnus-tmp-level 0)
2923         (default-score (or gnus-summary-default-score 0))
2924         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
2925         thread number subject stack state gnus-tmp-gathered beg-match
2926         new-roots gnus-tmp-new-adopts thread-end
2927         gnus-tmp-header gnus-tmp-unread
2928         gnus-tmp-replied gnus-tmp-subject-or-nil
2929         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
2930         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
2931         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
2932
2933     (setq gnus-tmp-prev-subject nil)
2934
2935     (if (vectorp (car threads))
2936         ;; If this is a straight (sic) list of headers, then a
2937         ;; threaded summary display isn't required, so we just create
2938         ;; an unthreaded one.
2939         (gnus-summary-prepare-unthreaded threads)
2940
2941       ;; Do the threaded display.
2942
2943       (while (or threads stack gnus-tmp-new-adopts new-roots)
2944
2945         (if (and (= gnus-tmp-level 0)
2946                  (not (setq gnus-tmp-dummy-line nil))
2947                  (or (not stack)
2948                      (= (caar stack) 0))
2949                  (not gnus-tmp-false-parent)
2950                  (or gnus-tmp-new-adopts new-roots))
2951             (if gnus-tmp-new-adopts
2952                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
2953                       thread (list (car gnus-tmp-new-adopts))
2954                       gnus-tmp-header (caar thread)
2955                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
2956               (if new-roots
2957                   (setq thread (list (car new-roots))
2958                         gnus-tmp-header (caar thread)
2959                         new-roots (cdr new-roots))))
2960
2961           (if threads
2962               ;; If there are some threads, we do them before the
2963               ;; threads on the stack.
2964               (setq thread threads
2965                     gnus-tmp-header (caar thread))
2966             ;; There were no current threads, so we pop something off
2967             ;; the stack.
2968             (setq state (car stack)
2969                   gnus-tmp-level (car state)
2970                   thread (cdr state)
2971                   stack (cdr stack)
2972                   gnus-tmp-header (caar thread))))
2973
2974         (setq gnus-tmp-false-parent nil)
2975         (setq gnus-tmp-root-expunged nil)
2976         (setq thread-end nil)
2977
2978         (if (stringp gnus-tmp-header)
2979             ;; The header is a dummy root.
2980             (cond
2981              ((eq gnus-summary-make-false-root 'adopt)
2982               ;; We let the first article adopt the rest.
2983               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
2984                                                (cddar thread)))
2985               (setq gnus-tmp-gathered
2986                     (nconc (mapcar
2987                             (lambda (h) (mail-header-number (car h)))
2988                             (cddar thread))
2989                            gnus-tmp-gathered))
2990               (setq thread (cons (list (caar thread)
2991                                        (cadar thread))
2992                                  (cdr thread)))
2993               (setq gnus-tmp-level -1
2994                     gnus-tmp-false-parent t))
2995              ((eq gnus-summary-make-false-root 'empty)
2996               ;; We print adopted articles with empty subject fields.
2997               (setq gnus-tmp-gathered
2998                     (nconc (mapcar
2999                             (lambda (h) (mail-header-number (car h)))
3000                             (cddar thread))
3001                            gnus-tmp-gathered))
3002               (setq gnus-tmp-level -1))
3003              ((eq gnus-summary-make-false-root 'dummy)
3004               ;; We remember that we probably want to output a dummy
3005               ;; root.
3006               (setq gnus-tmp-dummy-line gnus-tmp-header)
3007               (setq gnus-tmp-prev-subject gnus-tmp-header))
3008              (t
3009               ;; We do not make a root for the gathered
3010               ;; sub-threads at all.
3011               (setq gnus-tmp-level -1)))
3012
3013           (setq number (mail-header-number gnus-tmp-header)
3014                 subject (mail-header-subject gnus-tmp-header))
3015
3016           (cond
3017            ;; If the thread has changed subject, we might want to make
3018            ;; this subthread into a root.
3019            ((and (null gnus-thread-ignore-subject)
3020                  (not (zerop gnus-tmp-level))
3021                  gnus-tmp-prev-subject
3022                  (not (inline
3023                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3024             (setq new-roots (nconc new-roots (list (car thread)))
3025                   thread-end t
3026                   gnus-tmp-header nil))
3027            ;; If the article lies outside the current limit,
3028            ;; then we do not display it.
3029            ((and (not (memq number gnus-newsgroup-limit))
3030                  (not gnus-tmp-dummy-line))
3031             (setq gnus-tmp-gathered
3032                   (nconc (mapcar
3033                           (lambda (h) (mail-header-number (car h)))
3034                           (cdar thread))
3035                          gnus-tmp-gathered))
3036             (setq gnus-tmp-new-adopts (if (cdar thread)
3037                                           (append gnus-tmp-new-adopts
3038                                                   (cdar thread))
3039                                         gnus-tmp-new-adopts)
3040                   thread-end t
3041                   gnus-tmp-header nil)
3042             (when (zerop gnus-tmp-level)
3043               (setq gnus-tmp-root-expunged t)))
3044            ;; Perhaps this article is to be marked as read?
3045            ((and gnus-summary-mark-below
3046                  (< (or (cdr (assq number gnus-newsgroup-scored))
3047                         default-score)
3048                     gnus-summary-mark-below)
3049                  ;; Don't touch sparse articles.
3050                  (not (memq number gnus-newsgroup-sparse))
3051                  (not (memq number gnus-newsgroup-ancient)))
3052             (setq gnus-newsgroup-unreads
3053                   (delq number gnus-newsgroup-unreads))
3054             (if gnus-newsgroup-auto-expire
3055                 (push number gnus-newsgroup-expirable)
3056               (push (cons number gnus-low-score-mark)
3057                     gnus-newsgroup-reads))))
3058
3059           (when gnus-tmp-header
3060             ;; We may have an old dummy line to output before this
3061             ;; article.
3062             (when gnus-tmp-dummy-line
3063               (gnus-summary-insert-dummy-line
3064                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3065               (setq gnus-tmp-dummy-line nil))
3066
3067             ;; Compute the mark.
3068             (setq gnus-tmp-unread (gnus-article-mark number))
3069
3070             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3071                                   gnus-tmp-header gnus-tmp-level)
3072                   gnus-newsgroup-data)
3073
3074             ;; Actually insert the line.
3075             (setq
3076              gnus-tmp-subject-or-nil
3077              (cond
3078               ((and gnus-thread-ignore-subject
3079                     gnus-tmp-prev-subject
3080                     (not (inline (gnus-subject-equal
3081                                   gnus-tmp-prev-subject subject))))
3082                subject)
3083               ((zerop gnus-tmp-level)
3084                (if (and (eq gnus-summary-make-false-root 'empty)
3085                         (memq number gnus-tmp-gathered)
3086                         gnus-tmp-prev-subject
3087                         (inline (gnus-subject-equal
3088                                  gnus-tmp-prev-subject subject)))
3089                    gnus-summary-same-subject
3090                  subject))
3091               (t gnus-summary-same-subject)))
3092             (if (and (eq gnus-summary-make-false-root 'adopt)
3093                      (= gnus-tmp-level 1)
3094                      (memq number gnus-tmp-gathered))
3095                 (setq gnus-tmp-opening-bracket ?\<
3096                       gnus-tmp-closing-bracket ?\>)
3097               (setq gnus-tmp-opening-bracket ?\[
3098                     gnus-tmp-closing-bracket ?\]))
3099             (setq
3100              gnus-tmp-indentation
3101              (aref gnus-thread-indent-array gnus-tmp-level)
3102              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3103              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3104                                 gnus-summary-default-score 0)
3105              gnus-tmp-score-char
3106              (if (or (null gnus-summary-default-score)
3107                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3108                          gnus-summary-zcore-fuzz)) ? 
3109                (if (< gnus-tmp-score gnus-summary-default-score)
3110                    gnus-score-below-mark gnus-score-over-mark))
3111              gnus-tmp-replied
3112              (cond ((memq number gnus-newsgroup-processable)
3113                     gnus-process-mark)
3114                    ((memq number gnus-newsgroup-cached)
3115                     gnus-cached-mark)
3116                    ((memq number gnus-newsgroup-replied)
3117                     gnus-replied-mark)
3118                    ((memq number gnus-newsgroup-saved)
3119                     gnus-saved-mark)
3120                    (t gnus-unread-mark))
3121              gnus-tmp-from (mail-header-from gnus-tmp-header)
3122              gnus-tmp-name
3123              (cond
3124               ((string-match "(.+)" gnus-tmp-from)
3125                (substring gnus-tmp-from
3126                           (1+ (match-beginning 0)) (1- (match-end 0))))
3127               ((string-match "<[^>]+> *$" gnus-tmp-from)
3128                (setq beg-match (match-beginning 0))
3129                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3130                         (substring gnus-tmp-from (1+ (match-beginning 0))
3131                                    (1- (match-end 0))))
3132                    (substring gnus-tmp-from 0 beg-match)))
3133               (t gnus-tmp-from)))
3134             (when (string= gnus-tmp-name "")
3135               (setq gnus-tmp-name gnus-tmp-from))
3136             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
3137             (gnus-put-text-property
3138              (point)
3139              (progn (eval gnus-summary-line-format-spec) (point))
3140              'gnus-number number)
3141             (when gnus-visual-p
3142               (forward-line -1)
3143               (run-hooks 'gnus-summary-update-hook)
3144               (forward-line 1))
3145
3146             (setq gnus-tmp-prev-subject subject)))
3147
3148         (when (nth 1 thread)
3149           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3150         (incf gnus-tmp-level)
3151         (setq threads (if thread-end nil (cdar thread)))
3152         (unless threads
3153           (setq gnus-tmp-level 0)))))
3154   (gnus-message 7 "Generating summary...done"))
3155
3156 (defun gnus-summary-prepare-unthreaded (headers)
3157   "Generate an unthreaded summary buffer based on HEADERS."
3158   (let (header number mark)
3159
3160     (while headers
3161       ;; We may have to root out some bad articles...
3162       (when (memq (setq number (mail-header-number
3163                                 (setq header (pop headers))))
3164                   gnus-newsgroup-limit)
3165         ;; Mark article as read when it has a low score.
3166         (when (and gnus-summary-mark-below
3167                    (< (or (cdr (assq number gnus-newsgroup-scored))
3168                           gnus-summary-default-score 0)
3169                       gnus-summary-mark-below)
3170                    (not (memq number gnus-newsgroup-ancient)))
3171           (setq gnus-newsgroup-unreads
3172                 (delq number gnus-newsgroup-unreads))
3173           (if gnus-newsgroup-auto-expire
3174               (push number gnus-newsgroup-expirable)
3175             (push (cons number gnus-low-score-mark)
3176                   gnus-newsgroup-reads)))
3177
3178         (setq mark (gnus-article-mark number))
3179         (setq gnus-newsgroup-data
3180               (cons (gnus-data-make number mark (1+ (point)) header 0)
3181                     gnus-newsgroup-data))
3182         (gnus-summary-insert-line
3183          header 0 nil mark (memq number gnus-newsgroup-replied)
3184          (memq number gnus-newsgroup-expirable)
3185          (mail-header-subject header) nil
3186          (cdr (assq number gnus-newsgroup-scored))
3187          (memq number gnus-newsgroup-processable))))))
3188
3189 (defun gnus-select-newsgroup (group &optional read-all)
3190   "Select newsgroup GROUP.
3191 If READ-ALL is non-nil, all articles in the group are selected."
3192   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3193          (info (nth 2 entry))
3194          articles fetched-articles cached)
3195
3196     (or (gnus-check-server
3197          (setq gnus-current-select-method (gnus-find-method-for-group group)))
3198         (error "Couldn't open server"))
3199
3200     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3201         (gnus-activate-group group)     ; Or we can activate it...
3202         (progn                          ; Or we bug out.
3203           (when (equal major-mode 'gnus-summary-mode)
3204             (kill-buffer (current-buffer)))
3205           (error "Couldn't request group %s: %s"
3206                  group (gnus-status-message group))))
3207
3208     (unless (gnus-request-group group t)
3209       (when (equal major-mode 'gnus-summary-mode)
3210         (kill-buffer (current-buffer)))
3211       (error "Couldn't request group %s: %s"
3212              group (gnus-status-message group)))      
3213
3214     (setq gnus-newsgroup-name group)
3215     (setq gnus-newsgroup-unselected nil)
3216     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3217
3218     ;; Adjust and set lists of article marks.
3219     (when info
3220       (gnus-adjust-marked-articles info))
3221
3222     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3223     (when (gnus-virtual-group-p group)
3224       (setq cached gnus-newsgroup-cached))
3225
3226     (setq gnus-newsgroup-unreads
3227           (gnus-set-difference
3228            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3229            gnus-newsgroup-dormant))
3230
3231     (setq gnus-newsgroup-processable nil)
3232
3233     (setq articles (gnus-articles-to-read group read-all))
3234
3235     (cond
3236      ((null articles)
3237       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3238       'quit)
3239      ((eq articles 0) nil)
3240      (t
3241       ;; Init the dependencies hash table.
3242       (setq gnus-newsgroup-dependencies
3243             (gnus-make-hashtable (length articles)))
3244       ;; Retrieve the headers and read them in.
3245       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3246       (setq gnus-newsgroup-headers
3247             (if (eq 'nov
3248                     (setq gnus-headers-retrieved-by
3249                           (gnus-retrieve-headers
3250                            articles gnus-newsgroup-name
3251                            ;; We might want to fetch old headers, but
3252                            ;; not if there is only 1 article.
3253                            (and gnus-fetch-old-headers
3254                                 (or (and
3255                                      (not (eq gnus-fetch-old-headers 'some))
3256                                      (not (numberp gnus-fetch-old-headers)))
3257                                     (> (length articles) 1))))))
3258                 (gnus-get-newsgroup-headers-xover articles)
3259               (gnus-get-newsgroup-headers)))
3260       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3261
3262       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3263       (when cached
3264         (setq gnus-newsgroup-cached cached))
3265
3266       ;; Suppress duplicates?
3267       (when gnus-suppress-duplicates
3268         (gnus-dup-suppress-articles))
3269
3270       ;; Set the initial limit.
3271       (setq gnus-newsgroup-limit (copy-sequence articles))
3272       ;; Remove canceled articles from the list of unread articles.
3273       (setq gnus-newsgroup-unreads
3274             (gnus-set-sorted-intersection
3275              gnus-newsgroup-unreads
3276              (setq fetched-articles
3277                    (mapcar (lambda (headers) (mail-header-number headers))
3278                            gnus-newsgroup-headers))))
3279       ;; Removed marked articles that do not exist.
3280       (gnus-update-missing-marks
3281        (gnus-sorted-complement fetched-articles articles))
3282       ;; We might want to build some more threads first.
3283       (and gnus-fetch-old-headers
3284            (eq gnus-headers-retrieved-by 'nov)
3285            (gnus-build-old-threads))
3286       ;; Check whether auto-expire is to be done in this group.
3287       (setq gnus-newsgroup-auto-expire
3288             (gnus-group-auto-expirable-p group))
3289       ;; Set up the article buffer now, if necessary.
3290       (unless gnus-single-article-buffer
3291         (gnus-article-setup-buffer))
3292       ;; First and last article in this newsgroup.
3293       (when gnus-newsgroup-headers
3294         (setq gnus-newsgroup-begin
3295               (mail-header-number (car gnus-newsgroup-headers))
3296               gnus-newsgroup-end
3297               (mail-header-number
3298                (gnus-last-element gnus-newsgroup-headers))))
3299       ;; GROUP is successfully selected.
3300       (or gnus-newsgroup-headers t)))))
3301
3302 (defun gnus-articles-to-read (group read-all)
3303   ;; Find out what articles the user wants to read.
3304   (let* ((articles
3305           ;; Select all articles if `read-all' is non-nil, or if there
3306           ;; are no unread articles.
3307           (if (or read-all
3308                   (and (zerop (length gnus-newsgroup-marked))
3309                        (zerop (length gnus-newsgroup-unreads)))
3310                   (eq (gnus-group-find-parameter group 'display)
3311                       'all))
3312               (gnus-uncompress-range (gnus-active group))
3313             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3314                           (copy-sequence gnus-newsgroup-unreads))
3315                   '<)))
3316          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3317          (scored (length scored-list))
3318          (number (length articles))
3319          (marked (+ (length gnus-newsgroup-marked)
3320                     (length gnus-newsgroup-dormant)))
3321          (select
3322           (cond
3323            ((numberp read-all)
3324             read-all)
3325            (t
3326             (condition-case ()
3327                 (cond
3328                  ((and (or (<= scored marked) (= scored number))
3329                        (numberp gnus-large-newsgroup)
3330                        (> number gnus-large-newsgroup))
3331                   (let ((input
3332                          (read-string
3333                           (format
3334                            "How many articles from %s (default %d): "
3335                            gnus-newsgroup-name number))))
3336                     (if (string-match "^[ \t]*$" input) number input)))
3337                  ((and (> scored marked) (< scored number)
3338                        (> (- scored number) 20))
3339                   (let ((input
3340                          (read-string
3341                           (format "%s %s (%d scored, %d total): "
3342                                   "How many articles from"
3343                                   group scored number))))
3344                     (if (string-match "^[ \t]*$" input)
3345                         number input)))
3346                  (t number))
3347               (quit nil))))))
3348     (setq select (if (stringp select) (string-to-number select) select))
3349     (if (or (null select) (zerop select))
3350         select
3351       (if (and (not (zerop scored)) (<= (abs select) scored))
3352           (progn
3353             (setq articles (sort scored-list '<))
3354             (setq number (length articles)))
3355         (setq articles (copy-sequence articles)))
3356
3357       (if (< (abs select) number)
3358           (if (< select 0)
3359               ;; Select the N oldest articles.
3360               (setcdr (nthcdr (1- (abs select)) articles) nil)
3361             ;; Select the N most recent articles.
3362             (setq articles (nthcdr (- number select) articles))))
3363       (setq gnus-newsgroup-unselected
3364             (gnus-sorted-intersection
3365              gnus-newsgroup-unreads
3366              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3367       articles)))
3368
3369 (defun gnus-killed-articles (killed articles)
3370   (let (out)
3371     (while articles
3372       (if (inline (gnus-member-of-range (car articles) killed))
3373           (setq out (cons (car articles) out)))
3374       (setq articles (cdr articles)))
3375     out))
3376
3377 (defun gnus-uncompress-marks (marks)
3378   "Uncompress the mark ranges in MARKS."
3379   (let ((uncompressed '(score bookmark))
3380         out)
3381     (while marks
3382       (if (memq (caar marks) uncompressed)
3383           (push (car marks) out)
3384         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
3385       (setq marks (cdr marks)))
3386     out))
3387
3388 (defun gnus-adjust-marked-articles (info)
3389   "Set all article lists and remove all marks that are no longer legal."
3390   (let* ((marked-lists (gnus-info-marks info))
3391          (active (gnus-active (gnus-info-group info)))
3392          (min (car active))
3393          (max (cdr active))
3394          (types gnus-article-mark-lists)
3395          (uncompressed '(score bookmark killed))
3396          marks var articles article mark)
3397
3398     (while marked-lists
3399       (setq marks (pop marked-lists))
3400       (set (setq var (intern (format "gnus-newsgroup-%s"
3401                                      (car (rassq (setq mark (car marks))
3402                                                  types)))))
3403            (if (memq (car marks) uncompressed) (cdr marks)
3404              (gnus-uncompress-range (cdr marks))))
3405
3406       (setq articles (symbol-value var))
3407
3408       ;; All articles have to be subsets of the active articles.
3409       (cond
3410        ;; Adjust "simple" lists.
3411        ((memq mark '(tick dormant expirable reply save))
3412         (while articles
3413           (when (or (< (setq article (pop articles)) min) (> article max))
3414             (set var (delq article (symbol-value var))))))
3415        ;; Adjust assocs.
3416        ((memq mark uncompressed)
3417         (while articles
3418           (when (or (not (consp (setq article (pop articles))))
3419                     (< (car article) min)
3420                     (> (car article) max))
3421             (set var (delq article (symbol-value var))))))))))
3422
3423 (defun gnus-update-missing-marks (missing)
3424   "Go through the list of MISSING articles and remove them mark lists."
3425   (when missing
3426     (let ((types gnus-article-mark-lists)
3427           var m)
3428       ;; Go through all types.
3429       (while types
3430         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
3431         (when (symbol-value var)
3432           ;; This list has articles.  So we delete all missing articles
3433           ;; from it.
3434           (setq m missing)
3435           (while m
3436             (set var (delq (pop m) (symbol-value var)))))))))
3437
3438 (defun gnus-update-marks ()
3439   "Enter the various lists of marked articles into the newsgroup info list."
3440   (let ((types gnus-article-mark-lists)
3441         (info (gnus-get-info gnus-newsgroup-name))
3442         (uncompressed '(score bookmark killed))
3443         type list newmarked symbol)
3444     (when info
3445       ;; Add all marks lists that are non-nil to the list of marks lists.
3446       (while types
3447         (setq type (pop types))
3448         (when (setq list (symbol-value
3449                           (setq symbol
3450                                 (intern (format "gnus-newsgroup-%s"
3451                                                 (car type))))))
3452           (push (cons (cdr type)
3453                       (if (memq (cdr type) uncompressed) list
3454                         (gnus-compress-sequence 
3455                          (set symbol (sort list '<)) t)))
3456                 newmarked)))
3457
3458       ;; Enter these new marks into the info of the group.
3459       (if (nthcdr 3 info)
3460           (setcar (nthcdr 3 info) newmarked)
3461         ;; Add the marks lists to the end of the info.
3462         (when newmarked
3463           (setcdr (nthcdr 2 info) (list newmarked))))
3464
3465       ;; Cut off the end of the info if there's nothing else there.
3466       (let ((i 5))
3467         (while (and (> i 2)
3468                     (not (nth i info)))
3469           (when (nthcdr (decf i) info)
3470             (setcdr (nthcdr i info) nil)))))))
3471
3472 (defun gnus-set-mode-line (where)
3473   "This function sets the mode line of the article or summary buffers.
3474 If WHERE is `summary', the summary mode line format will be used."
3475   ;; Is this mode line one we keep updated?
3476   (when (memq where gnus-updated-mode-lines)
3477     (let (mode-string)
3478       (save-excursion
3479         ;; We evaluate this in the summary buffer since these
3480         ;; variables are buffer-local to that buffer.
3481         (set-buffer gnus-summary-buffer)
3482         ;; We bind all these variables that are used in the `eval' form
3483         ;; below.
3484         (let* ((mformat (symbol-value
3485                          (intern
3486                           (format "gnus-%s-mode-line-format-spec" where))))
3487                (gnus-tmp-group-name gnus-newsgroup-name)
3488                (gnus-tmp-article-number (or gnus-current-article 0))
3489                (gnus-tmp-unread gnus-newsgroup-unreads)
3490                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
3491                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
3492                (gnus-tmp-unread-and-unselected
3493                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
3494                             (zerop gnus-tmp-unselected)) "")
3495                       ((zerop gnus-tmp-unselected)
3496                        (format "{%d more}" gnus-tmp-unread-and-unticked))
3497                       (t (format "{%d(+%d) more}"
3498                                  gnus-tmp-unread-and-unticked
3499                                  gnus-tmp-unselected))))
3500                (gnus-tmp-subject
3501                 (if (and gnus-current-headers
3502                          (vectorp gnus-current-headers))
3503                     (gnus-mode-string-quote
3504                      (mail-header-subject gnus-current-headers)) ""))
3505                max-len
3506                gnus-tmp-header);; passed as argument to any user-format-funcs
3507           (setq mode-string (eval mformat))
3508           (setq max-len (max 4 (if gnus-mode-non-string-length
3509                                    (- (window-width)
3510                                       gnus-mode-non-string-length)
3511                                  (length mode-string))))
3512           ;; We might have to chop a bit of the string off...
3513           (when (> (length mode-string) max-len)
3514             (setq mode-string
3515                   (concat (gnus-truncate-string mode-string (- max-len 3))
3516                           "...")))
3517           ;; Pad the mode string a bit.
3518           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
3519       ;; Update the mode line.
3520       (setq mode-line-buffer-identification 
3521             (gnus-mode-line-buffer-identification
3522              (list mode-string)))
3523       (set-buffer-modified-p t))))
3524
3525 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
3526   "Go through the HEADERS list and add all Xrefs to a hash table.
3527 The resulting hash table is returned, or nil if no Xrefs were found."
3528   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
3529          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
3530          (xref-hashtb (make-vector 63 0))
3531          start group entry number xrefs header)
3532     (while headers
3533       (setq header (pop headers))
3534       (when (and (setq xrefs (mail-header-xref header))
3535                  (not (memq (setq number (mail-header-number header))
3536                             unreads)))
3537         (setq start 0)
3538         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
3539           (setq start (match-end 0))
3540           (setq group (if prefix
3541                           (concat prefix (substring xrefs (match-beginning 1)
3542                                                     (match-end 1)))
3543                         (substring xrefs (match-beginning 1) (match-end 1))))
3544           (setq number
3545                 (string-to-int (substring xrefs (match-beginning 2)
3546                                           (match-end 2))))
3547           (if (setq entry (gnus-gethash group xref-hashtb))
3548               (setcdr entry (cons number (cdr entry)))
3549             (gnus-sethash group (cons number nil) xref-hashtb)))))
3550     (and start xref-hashtb)))
3551
3552 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
3553   "Look through all the headers and mark the Xrefs as read."
3554   (let ((virtual (gnus-virtual-group-p from-newsgroup))
3555         name entry info xref-hashtb idlist method nth4)
3556     (save-excursion
3557       (set-buffer gnus-group-buffer)
3558       (when (setq xref-hashtb
3559                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
3560         (mapatoms
3561          (lambda (group)
3562            (unless (string= from-newsgroup (setq name (symbol-name group)))
3563              (setq idlist (symbol-value group))
3564              ;; Dead groups are not updated.
3565              (and (prog1
3566                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
3567                             info (nth 2 entry))
3568                     (if (stringp (setq nth4 (gnus-info-method info)))
3569                         (setq nth4 (gnus-server-to-method nth4))))
3570                   ;; Only do the xrefs if the group has the same
3571                   ;; select method as the group we have just read.
3572                   (or (gnus-methods-equal-p
3573                        nth4 (gnus-find-method-for-group from-newsgroup))
3574                       virtual
3575                       (equal nth4 (setq method (gnus-find-method-for-group
3576                                                 from-newsgroup)))
3577                       (and (equal (car nth4) (car method))
3578                            (equal (nth 1 nth4) (nth 1 method))))
3579                   gnus-use-cross-reference
3580                   (or (not (eq gnus-use-cross-reference t))
3581                       virtual
3582                       ;; Only do cross-references on subscribed
3583                       ;; groups, if that is what is wanted.
3584                       (<= (gnus-info-level info) gnus-level-subscribed))
3585                   (gnus-group-make-articles-read name idlist))))
3586          xref-hashtb)))))
3587
3588 (defun gnus-group-make-articles-read (group articles)
3589   "Update the info of GROUP to say that only ARTICLES are unread."
3590   (let* ((num 0)
3591          (entry (gnus-gethash group gnus-newsrc-hashtb))
3592          (info (nth 2 entry))
3593          (active (gnus-active group))
3594          range)
3595     ;; First peel off all illegal article numbers.
3596     (when active
3597       (let ((ids articles)
3598             id first)
3599         (while (setq id (pop ids))
3600           (when (and first (> id (cdr active)))
3601             ;; We'll end up in this situation in one particular
3602             ;; obscure situation.  If you re-scan a group and get
3603             ;; a new article that is cross-posted to a different
3604             ;; group that has not been re-scanned, you might get
3605             ;; crossposted article that has a higher number than
3606             ;; Gnus believes possible.  So we re-activate this
3607             ;; group as well.  This might mean doing the
3608             ;; crossposting thingy will *increase* the number
3609             ;; of articles in some groups.  Tsk, tsk.
3610             (setq active (or (gnus-activate-group group) active)))
3611           (when (or (> id (cdr active))
3612                     (< id (car active)))
3613             (setq articles (delq id articles))))))
3614     (gnus-undo-register
3615       `(progn
3616          (gnus-info-set-marks ,info ,(gnus-info-marks info))
3617          (gnus-info-set-read ,info ,(gnus-info-read info))
3618          (gnus-group-update-group group t)))
3619     ;; If the read list is nil, we init it.
3620     (and active
3621          (null (gnus-info-read info))
3622          (> (car active) 1)
3623          (gnus-info-set-read info (cons 1 (1- (car active)))))
3624     ;; Then we add the read articles to the range.
3625     (gnus-info-set-read
3626      info
3627      (setq range
3628            (gnus-add-to-range
3629             (gnus-info-read info) (setq articles (sort articles '<)))))
3630     ;; Then we have to re-compute how many unread
3631     ;; articles there are in this group.
3632     (when active
3633       (cond
3634        ((not range)
3635         (setq num (- (1+ (cdr active)) (car active))))
3636        ((not (listp (cdr range)))
3637         (setq num (- (cdr active) (- (1+ (cdr range))
3638                                      (car range)))))
3639        (t
3640         (while range
3641           (if (numberp (car range))
3642               (setq num (1+ num))
3643             (setq num (+ num (- (1+ (cdar range)) (caar range)))))
3644           (setq range (cdr range)))
3645         (setq num (- (cdr active) num))))
3646       ;; Update the number of unread articles.
3647       (setcar entry num)
3648       ;; Update the group buffer.
3649       (gnus-group-update-group group t))))
3650
3651 (defun gnus-methods-equal-p (m1 m2)
3652   (let ((m1 (or m1 gnus-select-method))
3653         (m2 (or m2 gnus-select-method)))
3654     (or (equal m1 m2)
3655         (and (eq (car m1) (car m2))
3656              (or (not (memq 'address (assoc (symbol-name (car m1))
3657                                             gnus-valid-select-methods)))
3658                  (equal (nth 1 m1) (nth 1 m2)))))))
3659
3660 (defsubst gnus-header-value ()
3661   (buffer-substring (match-end 0) (gnus-point-at-eol)))
3662
3663 (defvar gnus-newsgroup-none-id 0)
3664
3665 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
3666   (let ((cur nntp-server-buffer)
3667         (dependencies
3668          (or dependencies
3669              (save-excursion (set-buffer gnus-summary-buffer)
3670                              gnus-newsgroup-dependencies)))
3671         headers id id-dep ref-dep end ref)
3672     (save-excursion
3673       (set-buffer nntp-server-buffer)
3674       ;; Translate all TAB characters into SPACE characters.
3675       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
3676       (run-hooks 'gnus-parse-headers-hook)
3677       (let ((case-fold-search t)
3678             in-reply-to header p lines)
3679         (goto-char (point-min))
3680         ;; Search to the beginning of the next header.  Error messages
3681         ;; do not begin with 2 or 3.
3682         (while (re-search-forward "^[23][0-9]+ " nil t)
3683           (setq id nil
3684                 ref nil)
3685           ;; This implementation of this function, with nine
3686           ;; search-forwards instead of the one re-search-forward and
3687           ;; a case (which basically was the old function) is actually
3688           ;; about twice as fast, even though it looks messier.  You
3689           ;; can't have everything, I guess.  Speed and elegance
3690           ;; doesn't always go hand in hand.
3691           (setq
3692            header
3693            (vector
3694             ;; Number.
3695             (prog1
3696                 (read cur)
3697               (end-of-line)
3698               (setq p (point))
3699               (narrow-to-region (point)
3700                                 (or (and (search-forward "\n.\n" nil t)
3701                                          (- (point) 2))
3702                                     (point))))
3703             ;; Subject.
3704             (progn
3705               (goto-char p)
3706               (if (search-forward "\nsubject: " nil t)
3707                   (gnus-header-value) "(none)"))
3708             ;; From.
3709             (progn
3710               (goto-char p)
3711               (if (search-forward "\nfrom: " nil t)
3712                   (gnus-header-value) "(nobody)"))
3713             ;; Date.
3714             (progn
3715               (goto-char p)
3716               (if (search-forward "\ndate: " nil t)
3717                   (gnus-header-value) ""))
3718             ;; Message-ID.
3719             (progn
3720               (goto-char p)
3721               (if (search-forward "\nmessage-id: " nil t)
3722                   (setq id (gnus-header-value))
3723                 ;; If there was no message-id, we just fake one to make
3724                 ;; subsequent routines simpler.
3725                 (setq id (concat "none+"
3726                                  (int-to-string
3727                                   (setq gnus-newsgroup-none-id
3728                                         (1+ gnus-newsgroup-none-id)))))))
3729             ;; References.
3730             (progn
3731               (goto-char p)
3732               (if (search-forward "\nreferences: " nil t)
3733                   (progn
3734                     (setq end (point))
3735                     (prog1
3736                         (gnus-header-value)
3737                       (setq ref
3738                             (buffer-substring
3739                              (progn
3740                                (end-of-line)
3741                                (search-backward ">" end t)
3742                                (1+ (point)))
3743                              (progn
3744                                (search-backward "<" end t)
3745                                (point))))))
3746                 ;; Get the references from the in-reply-to header if there
3747                 ;; were no references and the in-reply-to header looks
3748                 ;; promising.
3749                 (if (and (search-forward "\nin-reply-to: " nil t)
3750                          (setq in-reply-to (gnus-header-value))
3751                          (string-match "<[^>]+>" in-reply-to))
3752                     (setq ref (substring in-reply-to (match-beginning 0)
3753                                          (match-end 0)))
3754                   (setq ref ""))))
3755             ;; Chars.
3756             0
3757             ;; Lines.
3758             (progn
3759               (goto-char p)
3760               (if (search-forward "\nlines: " nil t)
3761                   (if (numberp (setq lines (read cur)))
3762                       lines 0)
3763                 0))
3764             ;; Xref.
3765             (progn
3766               (goto-char p)
3767               (and (search-forward "\nxref: " nil t)
3768                    (gnus-header-value)))))
3769           (when (equal id ref)
3770             (setq ref nil))
3771           ;; We do the threading while we read the headers.  The
3772           ;; message-id and the last reference are both entered into
3773           ;; the same hash table.  Some tippy-toeing around has to be
3774           ;; done in case an article has arrived before the article
3775           ;; which it refers to.
3776           (if (boundp (setq id-dep (intern id dependencies)))
3777               (if (and (car (symbol-value id-dep))
3778                        (not force-new))
3779                   ;; An article with this Message-ID has already
3780                   ;; been seen, so we ignore this one, except we add
3781                   ;; any additional Xrefs (in case the two articles
3782                   ;; came from different servers).
3783                   (progn
3784                     (mail-header-set-xref
3785                      (car (symbol-value id-dep))
3786                      (concat (or (mail-header-xref
3787                                   (car (symbol-value id-dep))) "")
3788                              (or (mail-header-xref header) "")))
3789                     (setq header nil))
3790                 (setcar (symbol-value id-dep) header))
3791             (set id-dep (list header)))
3792           (when header
3793             (if (boundp (setq ref-dep (intern ref dependencies)))
3794                 (setcdr (symbol-value ref-dep)
3795                         (nconc (cdr (symbol-value ref-dep))
3796                                (list (symbol-value id-dep))))
3797               (set ref-dep (list nil (symbol-value id-dep))))
3798             (setq headers (cons header headers)))
3799           (goto-char (point-max))
3800           (widen))
3801         (nreverse headers)))))
3802
3803 ;; The following macros and functions were written by Felix Lee
3804 ;; <flee@cse.psu.edu>.
3805
3806 (defmacro gnus-nov-read-integer ()
3807   '(prog1
3808        (if (= (following-char) ?\t)
3809            0
3810          (let ((num (condition-case nil (read buffer) (error nil))))
3811            (if (numberp num) num 0)))
3812      (or (eobp) (forward-char 1))))
3813
3814 (defmacro gnus-nov-skip-field ()
3815   '(search-forward "\t" eol 'move))
3816
3817 (defmacro gnus-nov-field ()
3818   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3819
3820 ;; Goes through the xover lines and returns a list of vectors
3821 (defun gnus-get-newsgroup-headers-xover (sequence &optional 
3822                                                   force-new dependencies)
3823   "Parse the news overview data in the server buffer, and return a
3824 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
3825   ;; Get the Xref when the users reads the articles since most/some
3826   ;; NNTP servers do not include Xrefs when using XOVER.
3827   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
3828   (let ((cur nntp-server-buffer)
3829         (dependencies (or dependencies gnus-newsgroup-dependencies))
3830         number headers header)
3831     (save-excursion
3832       (set-buffer nntp-server-buffer)
3833       ;; Allow the user to mangle the headers before parsing them.
3834       (run-hooks 'gnus-parse-headers-hook)
3835       (goto-char (point-min))
3836       (while (and sequence (not (eobp)))
3837         (setq number (read cur))
3838         (while (and sequence (< (car sequence) number))
3839           (setq sequence (cdr sequence)))
3840         (and sequence
3841              (eq number (car sequence))
3842              (progn
3843                (setq sequence (cdr sequence))
3844                (if (setq header
3845                          (inline (gnus-nov-parse-line
3846                                   number dependencies force-new)))
3847                    (setq headers (cons header headers)))))
3848         (forward-line 1))
3849       (setq headers (nreverse headers)))
3850     headers))
3851
3852 ;; This function has to be called with point after the article number
3853 ;; on the beginning of the line.
3854 (defun gnus-nov-parse-line (number dependencies &optional force-new)
3855   (let ((none 0)
3856         (eol (gnus-point-at-eol))
3857         (buffer (current-buffer))
3858         header ref id id-dep ref-dep)
3859
3860     ;; overview: [num subject from date id refs chars lines misc]
3861     (narrow-to-region (point) eol)
3862     (or (eobp) (forward-char))
3863
3864     (condition-case nil
3865         (setq header
3866               (vector
3867                number                   ; number
3868                (gnus-nov-field)         ; subject
3869                (gnus-nov-field)         ; from
3870                (gnus-nov-field)         ; date
3871                (setq id (or (gnus-nov-field)
3872                             (concat "none+"
3873                                     (int-to-string
3874                                      (setq none (1+ none)))))) ; id
3875                (progn
3876                  (save-excursion
3877                    (let ((beg (point)))
3878                      (search-forward "\t" eol)
3879                      (if (search-backward ">" beg t)
3880                          (setq ref
3881                                (buffer-substring
3882                                 (1+ (point))
3883                                 (search-backward "<" beg t)))
3884                        (setq ref nil))))
3885                  (gnus-nov-field))      ; refs
3886                (gnus-nov-read-integer)  ; chars
3887                (gnus-nov-read-integer)  ; lines
3888                (if (= (following-char) ?\n)
3889                    nil
3890                  (gnus-nov-field))      ; misc
3891                ))
3892       (error (progn
3893                (gnus-error 4 "Strange nov line")
3894                (setq header nil)
3895                (goto-char eol))))
3896
3897     (widen)
3898
3899     ;; We build the thread tree.
3900     (when header
3901       (when (equal id ref)
3902         ;; This article refers back to itself.  Naughty, naughty.
3903         (setq ref nil))
3904       (if (boundp (setq id-dep (intern id dependencies)))
3905           (if (and (car (symbol-value id-dep))
3906                    (not force-new))
3907               ;; An article with this Message-ID has already been seen,
3908               ;; so we ignore this one, except we add any additional
3909               ;; Xrefs (in case the two articles came from different
3910               ;; servers.
3911               (progn
3912                 (mail-header-set-xref
3913                  (car (symbol-value id-dep))
3914                  (concat (or (mail-header-xref
3915                               (car (symbol-value id-dep))) "")
3916                          (or (mail-header-xref header) "")))
3917                 (setq header nil))
3918             (setcar (symbol-value id-dep) header))
3919         (set id-dep (list header))))
3920     (when header
3921       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
3922           (setcdr (symbol-value ref-dep)
3923                   (nconc (cdr (symbol-value ref-dep))
3924                          (list (symbol-value id-dep))))
3925         (set ref-dep (list nil (symbol-value id-dep)))))
3926     header))
3927
3928 (defun gnus-article-get-xrefs ()
3929   "Fill in the Xref value in `gnus-current-headers', if necessary.
3930 This is meant to be called in `gnus-article-internal-prepare-hook'."
3931   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
3932                                  gnus-current-headers)))
3933     (or (not gnus-use-cross-reference)
3934         (not headers)
3935         (and (mail-header-xref headers)
3936              (not (string= (mail-header-xref headers) "")))
3937         (let ((case-fold-search t)
3938               xref)
3939           (save-restriction
3940             (nnheader-narrow-to-headers)
3941             (goto-char (point-min))
3942             (if (or (and (eq (downcase (following-char)) ?x)
3943                          (looking-at "Xref:"))
3944                     (search-forward "\nXref:" nil t))
3945                 (progn
3946                   (goto-char (1+ (match-end 0)))
3947                   (setq xref (buffer-substring (point)
3948                                                (progn (end-of-line) (point))))
3949                   (mail-header-set-xref headers xref))))))))
3950
3951 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
3952   "Find article ID and insert the summary line for that article."
3953   (let ((header (if (and old-header use-old-header)
3954                     old-header (gnus-read-header id)))
3955         (number (and (numberp id) id))
3956         pos)
3957     (when header
3958       ;; Rebuild the thread that this article is part of and go to the
3959       ;; article we have fetched.
3960       (when (and (not gnus-show-threads)
3961                  old-header)
3962         (when (setq pos (text-property-any
3963                          (point-min) (point-max) 'gnus-number 
3964                          (mail-header-number old-header)))
3965           (goto-char pos)
3966           (gnus-delete-line)
3967           (gnus-data-remove (mail-header-number old-header))))
3968       (when old-header
3969         (mail-header-set-number header (mail-header-number old-header)))
3970       (setq gnus-newsgroup-sparse
3971             (delq (setq number (mail-header-number header)) 
3972                   gnus-newsgroup-sparse))
3973       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
3974       (gnus-rebuild-thread (mail-header-id header))
3975       (gnus-summary-goto-subject number nil t))
3976     (when (and (numberp number)
3977                (> number 0))
3978       ;; We have to update the boundaries even if we can't fetch the
3979       ;; article if ID is a number -- so that the next `P' or `N'
3980       ;; command will fetch the previous (or next) article even
3981       ;; if the one we tried to fetch this time has been canceled.
3982       (and (> number gnus-newsgroup-end)
3983            (setq gnus-newsgroup-end number))
3984       (and (< number gnus-newsgroup-begin)
3985            (setq gnus-newsgroup-begin number))
3986       (setq gnus-newsgroup-unselected
3987             (delq number gnus-newsgroup-unselected)))
3988     ;; Report back a success?
3989     (and header (mail-header-number header))))
3990
3991 ;;; Process/prefix in the summary buffer
3992
3993 (defun gnus-summary-work-articles (n)
3994   "Return a list of articles to be worked upon.  The prefix argument,
3995 the list of process marked articles, and the current article will be
3996 taken into consideration."
3997   (cond
3998    (n
3999     ;; A numerical prefix has been given.
4000     (let ((backward (< n 0))
4001           (n (abs (prefix-numeric-value n)))
4002           articles article)
4003       (save-excursion
4004         (while
4005             (and (> n 0)
4006                  (push (setq article (gnus-summary-article-number))
4007                        articles)
4008                  (if backward
4009                      (gnus-summary-find-prev nil article)
4010                    (gnus-summary-find-next nil article)))
4011           (decf n)))
4012       (nreverse articles)))
4013    ((and (boundp 'transient-mark-mode)
4014          transient-mark-mode
4015          mark-active)
4016     ;; Work on the region between point and mark.
4017     (let ((max (max (point) (mark)))
4018           articles article)
4019       (save-excursion
4020         (goto-char (min (point) (mark)))
4021         (while
4022             (and
4023              (push (setq article (gnus-summary-article-number)) articles)
4024              (gnus-summary-find-next nil article)
4025              (< (point) max)))
4026         (nreverse articles))))
4027    (gnus-newsgroup-processable
4028     ;; There are process-marked articles present.
4029     ;; Save current state.
4030     (gnus-summary-save-process-mark)
4031     ;; Return the list.
4032     (reverse gnus-newsgroup-processable))
4033    (t
4034     ;; Just return the current article.
4035     (list (gnus-summary-article-number)))))
4036
4037 (defun gnus-summary-save-process-mark ()
4038   "Push the current set of process marked articles on the stack."
4039   (interactive)
4040   (push (copy-sequence gnus-newsgroup-processable)
4041         gnus-newsgroup-process-stack))
4042
4043 (defun gnus-summary-kill-process-mark ()
4044   "Push the current set of process marked articles on the stack and unmark."
4045   (interactive)
4046   (gnus-summary-save-process-mark)
4047   (gnus-summary-unmark-all-processable))
4048
4049 (defun gnus-summary-yank-process-mark ()
4050   "Pop the last process mark state off the stack and restore it."
4051   (interactive)
4052   (unless gnus-newsgroup-process-stack
4053     (error "Empty mark stack"))
4054   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4055
4056 (defun gnus-summary-process-mark-set (set)
4057   "Make SET into the current process marked articles."
4058   (gnus-summary-unmark-all-processable)
4059   (while set
4060     (gnus-summary-set-process-mark (pop set))))
4061
4062 ;;; Searching and stuff
4063
4064 (defun gnus-summary-search-group (&optional backward use-level)
4065   "Search for next unread newsgroup.
4066 If optional argument BACKWARD is non-nil, search backward instead."
4067   (save-excursion
4068     (set-buffer gnus-group-buffer)
4069     (if (gnus-group-search-forward
4070          backward nil (if use-level (gnus-group-group-level) nil))
4071         (gnus-group-group-name))))
4072
4073 (defun gnus-summary-best-group (&optional exclude-group)
4074   "Find the name of the best unread group.
4075 If EXCLUDE-GROUP, do not go to this group."
4076   (save-excursion
4077     (set-buffer gnus-group-buffer)
4078     (save-excursion
4079       (gnus-group-best-unread-group exclude-group))))
4080
4081 (defun gnus-summary-find-next (&optional unread article backward)
4082   (if backward (gnus-summary-find-prev)
4083     (let* ((dummy (gnus-summary-article-intangible-p))
4084            (article (or article (gnus-summary-article-number)))
4085            (arts (gnus-data-find-list article))
4086            result)
4087       (when (and (not dummy)
4088                  (or (not gnus-summary-check-current)
4089                      (not unread)
4090                      (not (gnus-data-unread-p (car arts)))))
4091         (setq arts (cdr arts)))
4092       (when (setq result
4093                   (if unread
4094                       (progn
4095                         (while arts
4096                           (when (gnus-data-unread-p (car arts))
4097                             (setq result (car arts)
4098                                   arts nil))
4099                           (setq arts (cdr arts)))
4100                         result)
4101                     (car arts)))
4102         (goto-char (gnus-data-pos result))
4103         (gnus-data-number result)))))
4104
4105 (defun gnus-summary-find-prev (&optional unread article)
4106   (let* ((eobp (eobp))
4107          (article (or article (gnus-summary-article-number)))
4108          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4109          result)
4110     (when (and (not eobp)
4111                (or (not gnus-summary-check-current)
4112                    (not unread)
4113                    (not (gnus-data-unread-p (car arts)))))
4114       (setq arts (cdr arts)))
4115     (if (setq result
4116               (if unread
4117                   (progn
4118                     (while arts
4119                       (and (gnus-data-unread-p (car arts))
4120                            (setq result (car arts)
4121                                  arts nil))
4122                       (setq arts (cdr arts)))
4123                     result)
4124                 (car arts)))
4125         (progn
4126           (goto-char (gnus-data-pos result))
4127           (gnus-data-number result)))))
4128
4129 (defun gnus-summary-find-subject (subject &optional unread backward article)
4130   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4131          (article (or article (gnus-summary-article-number)))
4132          (articles (gnus-data-list backward))
4133          (arts (gnus-data-find-list article articles))
4134          result)
4135     (when (or (not gnus-summary-check-current)
4136               (not unread)
4137               (not (gnus-data-unread-p (car arts))))
4138       (setq arts (cdr arts)))
4139     (while arts
4140       (and (or (not unread)
4141                (gnus-data-unread-p (car arts)))
4142            (vectorp (gnus-data-header (car arts)))
4143            (gnus-subject-equal
4144             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4145            (setq result (car arts)
4146                  arts nil))
4147       (setq arts (cdr arts)))
4148     (and result
4149          (goto-char (gnus-data-pos result))
4150          (gnus-data-number result))))
4151
4152 (defun gnus-summary-search-forward (&optional unread subject backward)
4153   "Search forward for an article.
4154 If UNREAD, look for unread articles.  If SUBJECT, look for
4155 articles with that subject.  If BACKWARD, search backward instead."
4156   (cond (subject (gnus-summary-find-subject subject unread backward))
4157         (backward (gnus-summary-find-prev unread))
4158         (t (gnus-summary-find-next unread))))
4159
4160 (defun gnus-recenter (&optional n)
4161   "Center point in window and redisplay frame.
4162 Also do horizontal recentering."
4163   (interactive "P")
4164   (when (and gnus-auto-center-summary
4165              (not (eq gnus-auto-center-summary 'vertical)))
4166     (gnus-horizontal-recenter))
4167   (recenter n))
4168
4169 (defun gnus-summary-recenter ()
4170   "Center point in the summary window.
4171 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4172 displayed, no centering will be performed."
4173   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4174   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4175   (let* ((top (cond ((< (window-height) 4) 0)
4176                     ((< (window-height) 7) 1)
4177                     (t 2)))
4178          (height (1- (window-height)))
4179          (bottom (save-excursion (goto-char (point-max))
4180                                  (forward-line (- height))
4181                                  (point)))
4182          (window (get-buffer-window (current-buffer))))
4183     ;; The user has to want it.
4184     (when gnus-auto-center-summary
4185       (when (get-buffer-window gnus-article-buffer)
4186        ;; Only do recentering when the article buffer is displayed,
4187        ;; Set the window start to either `bottom', which is the biggest
4188        ;; possible valid number, or the second line from the top,
4189        ;; whichever is the least.
4190        (set-window-start
4191         window (min bottom (save-excursion 
4192                              (forward-line (- top)) (point)))))
4193       ;; Do horizontal recentering while we're at it.
4194       (when (and (get-buffer-window (current-buffer) t)
4195                  (not (eq gnus-auto-center-summary 'vertical)))
4196         (let ((selected (selected-window)))
4197           (select-window (get-buffer-window (current-buffer) t))
4198           (gnus-summary-position-point)
4199           (gnus-horizontal-recenter)
4200           (select-window selected))))))
4201
4202 (defun gnus-summary-jump-to-group (newsgroup)
4203   "Move point to NEWSGROUP in group mode buffer."
4204   ;; Keep update point of group mode buffer if visible.
4205   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4206       (save-window-excursion
4207         ;; Take care of tree window mode.
4208         (if (get-buffer-window gnus-group-buffer)
4209             (pop-to-buffer gnus-group-buffer))
4210         (gnus-group-jump-to-group newsgroup))
4211     (save-excursion
4212       ;; Take care of tree window mode.
4213       (if (get-buffer-window gnus-group-buffer)
4214           (pop-to-buffer gnus-group-buffer)
4215         (set-buffer gnus-group-buffer))
4216       (gnus-group-jump-to-group newsgroup))))
4217
4218 ;; This function returns a list of article numbers based on the
4219 ;; difference between the ranges of read articles in this group and
4220 ;; the range of active articles.
4221 (defun gnus-list-of-unread-articles (group)
4222   (let* ((read (gnus-info-read (gnus-get-info group)))
4223          (active (gnus-active group))
4224          (last (cdr active))
4225          first nlast unread)
4226     ;; If none are read, then all are unread.
4227     (if (not read)
4228         (setq first (car active))
4229       ;; If the range of read articles is a single range, then the
4230       ;; first unread article is the article after the last read
4231       ;; article.  Sounds logical, doesn't it?
4232       (if (not (listp (cdr read)))
4233           (setq first (1+ (cdr read)))
4234         ;; `read' is a list of ranges.
4235         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
4236                                 (caar read))) 1)
4237             (setq first 1))
4238         (while read
4239           (if first
4240               (while (< first nlast)
4241                 (setq unread (cons first unread))
4242                 (setq first (1+ first))))
4243           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4244           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4245           (setq read (cdr read)))))
4246     ;; And add the last unread articles.
4247     (while (<= first last)
4248       (setq unread (cons first unread))
4249       (setq first (1+ first)))
4250     ;; Return the list of unread articles.
4251     (nreverse unread)))
4252
4253 (defun gnus-list-of-read-articles (group)
4254   "Return a list of unread, unticked and non-dormant articles."
4255   (let* ((info (gnus-get-info group))
4256          (marked (gnus-info-marks info))
4257          (active (gnus-active group)))
4258     (and info active
4259          (gnus-set-difference
4260           (gnus-sorted-complement
4261            (gnus-uncompress-range active)
4262            (gnus-list-of-unread-articles group))
4263           (append
4264            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4265            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4266
4267 ;; Various summary commands
4268
4269 (defun gnus-summary-universal-argument (arg)
4270   "Perform any operation on all articles that are process/prefixed."
4271   (interactive "P")
4272   (gnus-set-global-variables)
4273   (let ((articles (gnus-summary-work-articles arg))
4274         func article)
4275     (if (eq
4276          (setq
4277           func
4278           (key-binding
4279            (read-key-sequence
4280             (substitute-command-keys
4281              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4282              ))))
4283          'undefined)
4284         (gnus-error 1 "Undefined key")
4285       (save-excursion
4286         (while articles
4287           (gnus-summary-goto-subject (setq article (pop articles)))
4288           (command-execute func)
4289           (gnus-summary-remove-process-mark article)))))
4290   (gnus-summary-position-point))
4291
4292 (defun gnus-summary-toggle-truncation (&optional arg)
4293   "Toggle truncation of summary lines.
4294 With arg, turn line truncation on iff arg is positive."
4295   (interactive "P")
4296   (setq truncate-lines
4297         (if (null arg) (not truncate-lines)
4298           (> (prefix-numeric-value arg) 0)))
4299   (redraw-display))
4300
4301 (defun gnus-summary-reselect-current-group (&optional all rescan)
4302   "Exit and then reselect the current newsgroup.
4303 The prefix argument ALL means to select all articles."
4304   (interactive "P")
4305   (gnus-set-global-variables)
4306   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4307     (error "Ephemeral groups can't be reselected"))
4308   (let ((current-subject (gnus-summary-article-number))
4309         (group gnus-newsgroup-name))
4310     (setq gnus-newsgroup-begin nil)
4311     (gnus-summary-exit)
4312     ;; We have to adjust the point of group mode buffer because the
4313     ;; current point was moved to the next unread newsgroup by
4314     ;; exiting.
4315     (gnus-summary-jump-to-group group)
4316     (when rescan
4317       (save-excursion
4318         (gnus-group-get-new-news-this-group 1)))
4319     (gnus-group-read-group all t)
4320     (gnus-summary-goto-subject current-subject nil t)))
4321
4322 (defun gnus-summary-rescan-group (&optional all)
4323   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4324   (interactive "P")
4325   (gnus-summary-reselect-current-group all t))
4326
4327 (defun gnus-summary-update-info ()
4328   (let ((group gnus-newsgroup-name))
4329     (when gnus-newsgroup-kill-headers
4330       (setq gnus-newsgroup-killed
4331             (gnus-compress-sequence
4332              (nconc
4333               (gnus-set-sorted-intersection
4334                (gnus-uncompress-range gnus-newsgroup-killed)
4335                (setq gnus-newsgroup-unselected
4336                      (sort gnus-newsgroup-unselected '<)))
4337               (setq gnus-newsgroup-unreads
4338                     (sort gnus-newsgroup-unreads '<))) t)))
4339     (unless (listp (cdr gnus-newsgroup-killed))
4340       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
4341     (let ((headers gnus-newsgroup-headers))
4342       (run-hooks 'gnus-exit-group-hook)
4343       (unless gnus-save-score
4344         (setq gnus-newsgroup-scored nil))
4345       ;; Set the new ranges of read articles.
4346       (gnus-update-read-articles
4347        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
4348       ;; Set the current article marks.
4349       (gnus-update-marks)
4350       ;; Do the cross-ref thing.
4351       (when gnus-use-cross-reference
4352         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
4353       ;; Do adaptive scoring, and possibly save score files.
4354       (when gnus-newsgroup-adaptive
4355         (gnus-score-adaptive))
4356       (when gnus-use-scoring
4357         (gnus-score-save))
4358       ;; Do not switch windows but change the buffer to work.
4359       (set-buffer gnus-group-buffer)
4360       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
4361           (gnus-group-update-group group)))))
4362
4363 (defun gnus-summary-exit (&optional temporary)
4364   "Exit reading current newsgroup, and then return to group selection mode.
4365 gnus-exit-group-hook is called with no arguments if that value is non-nil."
4366   (interactive)
4367   (gnus-set-global-variables)
4368   (gnus-kill-save-kill-buffer)
4369   (let* ((group gnus-newsgroup-name)
4370          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
4371          (mode major-mode)
4372          (buf (current-buffer)))
4373     (run-hooks 'gnus-summary-prepare-exit-hook)
4374     ;; If we have several article buffers, we kill them at exit.
4375     (unless gnus-single-article-buffer
4376       (gnus-kill-buffer gnus-original-article-buffer)
4377       (setq gnus-article-current nil))
4378     (when gnus-use-cache
4379       (gnus-cache-possibly-remove-articles)
4380       (gnus-cache-save-buffers))
4381     (gnus-async-prefetch-remove-group group)
4382     (when gnus-suppress-duplicates
4383       (gnus-dup-enter-articles))
4384     (when gnus-use-trees
4385       (gnus-tree-close group))
4386     ;; Make all changes in this group permanent.
4387     (unless quit-config
4388       (gnus-summary-update-info))
4389     (gnus-close-group group)
4390     ;; Make sure where I was, and go to next newsgroup.
4391     (set-buffer gnus-group-buffer)
4392     (unless quit-config
4393       (gnus-group-jump-to-group group))
4394     (run-hooks 'gnus-summary-exit-hook)
4395     (unless quit-config
4396       (gnus-group-next-unread-group 1))
4397     (if temporary
4398         nil                             ;Nothing to do.
4399       ;; If we have several article buffers, we kill them at exit.
4400       (unless gnus-single-article-buffer
4401         (gnus-kill-buffer gnus-article-buffer)
4402         (gnus-kill-buffer gnus-original-article-buffer)
4403         (setq gnus-article-current nil))
4404       (set-buffer buf)
4405       (if (not gnus-kill-summary-on-exit)
4406           (gnus-deaden-summary)
4407         ;; We set all buffer-local variables to nil.  It is unclear why
4408         ;; this is needed, but if we don't, buffer-local variables are
4409         ;; not garbage-collected, it seems.  This would the lead to en
4410         ;; ever-growing Emacs.
4411         (gnus-summary-clear-local-variables)
4412         (when (get-buffer gnus-article-buffer)
4413           (bury-buffer gnus-article-buffer))
4414         ;; We clear the global counterparts of the buffer-local
4415         ;; variables as well, just to be on the safe side.
4416         (gnus-configure-windows 'group 'force)
4417         (gnus-summary-clear-local-variables)
4418         ;; Return to group mode buffer.
4419         (if (eq mode 'gnus-summary-mode)
4420             (gnus-kill-buffer buf)))
4421       (setq gnus-current-select-method gnus-select-method)
4422       (pop-to-buffer gnus-group-buffer)
4423       ;; Clear the current group name.
4424       (if (not quit-config)
4425           (progn
4426             (gnus-group-jump-to-group group)
4427             (gnus-group-next-unread-group 1)
4428             (gnus-configure-windows 'group 'force))
4429         (if (not (buffer-name (car quit-config)))
4430             (gnus-configure-windows 'group 'force)
4431           (set-buffer (car quit-config))
4432           (and (eq major-mode 'gnus-summary-mode)
4433                (gnus-set-global-variables))
4434           (gnus-configure-windows (cdr quit-config))))
4435       (unless quit-config
4436         (setq gnus-newsgroup-name nil)))))
4437
4438 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
4439 (defun gnus-summary-exit-no-update (&optional no-questions)
4440   "Quit reading current newsgroup without updating read article info."
4441   (interactive)
4442   (gnus-set-global-variables)
4443   (let* ((group gnus-newsgroup-name)
4444          (quit-config (gnus-group-quit-config group)))
4445     (when (or no-questions
4446               gnus-expert-user
4447               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
4448       ;; If we have several article buffers, we kill them at exit.
4449       (unless gnus-single-article-buffer
4450         (gnus-kill-buffer gnus-article-buffer)
4451         (gnus-kill-buffer gnus-original-article-buffer)
4452         (setq gnus-article-current nil))
4453       (if (not gnus-kill-summary-on-exit)
4454           (gnus-deaden-summary)
4455         (gnus-close-group group)
4456         (gnus-summary-clear-local-variables)
4457         (set-buffer gnus-group-buffer)
4458         (gnus-summary-clear-local-variables)
4459         (when (get-buffer gnus-summary-buffer)
4460           (kill-buffer gnus-summary-buffer)))
4461       (unless gnus-single-article-buffer
4462         (setq gnus-article-current nil))
4463       (when gnus-use-trees
4464         (gnus-tree-close group))
4465       (gnus-async-prefetch-remove-group group)
4466       (when (get-buffer gnus-article-buffer)
4467         (bury-buffer gnus-article-buffer))
4468       ;; Return to the group buffer.
4469       (gnus-configure-windows 'group 'force)
4470       ;; Clear the current group name.
4471       (setq gnus-newsgroup-name nil)
4472       (when (equal (gnus-group-group-name) group)
4473         (gnus-group-next-unread-group 1))
4474       (when quit-config
4475         (if (not (buffer-name (car quit-config)))
4476             (gnus-configure-windows 'group 'force)
4477           (set-buffer (car quit-config))
4478           (when (eq major-mode 'gnus-summary-mode)
4479             (gnus-set-global-variables))
4480           (gnus-configure-windows (cdr quit-config)))))))
4481
4482 ;;; Dead summaries.
4483
4484 (defvar gnus-dead-summary-mode-map nil)
4485
4486 (unless gnus-dead-summary-mode-map
4487   (setq gnus-dead-summary-mode-map (make-keymap))
4488   (suppress-keymap gnus-dead-summary-mode-map)
4489   (substitute-key-definition
4490    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
4491   (let ((keys '("\C-d" "\r" "\177")))
4492     (while keys
4493       (define-key gnus-dead-summary-mode-map
4494         (pop keys) 'gnus-summary-wake-up-the-dead))))
4495
4496 (defvar gnus-dead-summary-mode nil
4497   "Minor mode for Gnus summary buffers.")
4498
4499 (defun gnus-dead-summary-mode (&optional arg)
4500   "Minor mode for Gnus summary buffers."
4501   (interactive "P")
4502   (when (eq major-mode 'gnus-summary-mode)
4503     (make-local-variable 'gnus-dead-summary-mode)
4504     (setq gnus-dead-summary-mode
4505           (if (null arg) (not gnus-dead-summary-mode)
4506             (> (prefix-numeric-value arg) 0)))
4507     (when gnus-dead-summary-mode
4508       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
4509         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
4510       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
4511         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
4512               minor-mode-map-alist)))))
4513
4514 (defun gnus-deaden-summary ()
4515   "Make the current summary buffer into a dead summary buffer."
4516   ;; Kill any previous dead summary buffer.
4517   (when (and gnus-dead-summary
4518              (buffer-name gnus-dead-summary))
4519     (save-excursion
4520       (set-buffer gnus-dead-summary)
4521       (when gnus-dead-summary-mode
4522         (kill-buffer (current-buffer)))))
4523   ;; Make this the current dead summary.
4524   (setq gnus-dead-summary (current-buffer))
4525   (gnus-dead-summary-mode 1)
4526   (let ((name (buffer-name)))
4527     (when (string-match "Summary" name)
4528       (rename-buffer
4529        (concat (substring name 0 (match-beginning 0)) "Dead "
4530                (substring name (match-beginning 0))) t))))
4531
4532 (defun gnus-kill-or-deaden-summary (buffer)
4533   "Kill or deaden the summary BUFFER."
4534   (when (and (buffer-name buffer)
4535              (not gnus-single-article-buffer))
4536     (save-excursion
4537       (set-buffer buffer)
4538       (gnus-kill-buffer gnus-article-buffer)
4539       (gnus-kill-buffer gnus-original-article-buffer)))
4540   (cond (gnus-kill-summary-on-exit
4541          (when (and gnus-use-trees
4542                     (and (get-buffer buffer)
4543                          (buffer-name (get-buffer buffer))))
4544            (save-excursion
4545              (set-buffer (get-buffer buffer))
4546              (gnus-tree-close gnus-newsgroup-name)))
4547          (gnus-kill-buffer buffer))
4548         ((and (get-buffer buffer)
4549               (buffer-name (get-buffer buffer)))
4550          (save-excursion
4551            (set-buffer buffer)
4552            (gnus-deaden-summary)))))
4553
4554 (defun gnus-summary-wake-up-the-dead (&rest args)
4555   "Wake up the dead summary buffer."
4556   (interactive)
4557   (gnus-dead-summary-mode -1)
4558   (let ((name (buffer-name)))
4559     (when (string-match "Dead " name)
4560       (rename-buffer
4561        (concat (substring name 0 (match-beginning 0))
4562                (substring name (match-end 0))) t)))
4563   (gnus-message 3 "This dead summary is now alive again"))
4564
4565 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
4566 (defun gnus-summary-fetch-faq (&optional faq-dir)
4567   "Fetch the FAQ for the current group.
4568 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
4569 in."
4570   (interactive
4571    (list
4572     (if current-prefix-arg
4573         (completing-read
4574          "Faq dir: " (and (listp gnus-group-faq-directory)
4575                           gnus-group-faq-directory)))))
4576   (let (gnus-faq-buffer)
4577     (and (setq gnus-faq-buffer
4578                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
4579          (gnus-configure-windows 'summary-faq))))
4580
4581 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
4582 (defun gnus-summary-describe-group (&optional force)
4583   "Describe the current newsgroup."
4584   (interactive "P")
4585   (gnus-group-describe-group force gnus-newsgroup-name))
4586
4587 (defun gnus-summary-describe-briefly ()
4588   "Describe summary mode commands briefly."
4589   (interactive)
4590   (gnus-message 6
4591                 (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")))
4592
4593 ;; Walking around group mode buffer from summary mode.
4594
4595 (defun gnus-summary-next-group (&optional no-article target-group backward)
4596   "Exit current newsgroup and then select next unread newsgroup.
4597 If prefix argument NO-ARTICLE is non-nil, no article is selected
4598 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
4599 previous group instead."
4600   (interactive "P")
4601   (gnus-set-global-variables)
4602   (let ((current-group gnus-newsgroup-name)
4603         (current-buffer (current-buffer))
4604         entered)
4605     ;; First we semi-exit this group to update Xrefs and all variables.
4606     ;; We can't do a real exit, because the window conf must remain
4607     ;; the same in case the user is prompted for info, and we don't
4608     ;; want the window conf to change before that...
4609     (gnus-summary-exit t)
4610     (while (not entered)
4611       ;; Then we find what group we are supposed to enter.
4612       (set-buffer gnus-group-buffer)
4613       (gnus-group-jump-to-group current-group)
4614       (setq target-group
4615             (or target-group
4616                 (if (eq gnus-keep-same-level 'best)
4617                     (gnus-summary-best-group gnus-newsgroup-name)
4618                   (gnus-summary-search-group backward gnus-keep-same-level))))
4619       (if (not target-group)
4620           ;; There are no further groups, so we return to the group
4621           ;; buffer.
4622           (progn
4623             (gnus-message 5 "Returning to the group buffer")
4624             (setq entered t)
4625             (set-buffer current-buffer)
4626             (gnus-summary-exit))
4627         ;; We try to enter the target group.
4628         (gnus-group-jump-to-group target-group)
4629         (let ((unreads (gnus-group-group-unread)))
4630           (if (and (or (eq t unreads)
4631                        (and unreads (not (zerop unreads))))
4632                    (gnus-summary-read-group
4633                     target-group nil no-article current-buffer))
4634               (setq entered t)
4635             (setq current-group target-group
4636                   target-group nil)))))))
4637
4638 (defun gnus-summary-prev-group (&optional no-article)
4639   "Exit current newsgroup and then select previous unread newsgroup.
4640 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
4641   (interactive "P")
4642   (gnus-summary-next-group no-article nil t))
4643
4644 ;; Walking around summary lines.
4645
4646 (defun gnus-summary-first-subject (&optional unread)
4647   "Go to the first unread subject.
4648 If UNREAD is non-nil, go to the first unread article.
4649 Returns the article selected or nil if there are no unread articles."
4650   (interactive "P")
4651   (prog1
4652       (cond
4653        ;; Empty summary.
4654        ((null gnus-newsgroup-data)
4655         (gnus-message 3 "No articles in the group")
4656         nil)
4657        ;; Pick the first article.
4658        ((not unread)
4659         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
4660         (gnus-data-number (car gnus-newsgroup-data)))
4661        ;; No unread articles.
4662        ((null gnus-newsgroup-unreads)
4663         (gnus-message 3 "No more unread articles")
4664         nil)
4665        ;; Find the first unread article.
4666        (t
4667         (let ((data gnus-newsgroup-data))
4668           (while (and data
4669                       (not (gnus-data-unread-p (car data))))
4670             (setq data (cdr data)))
4671           (if data
4672               (progn
4673                 (goto-char (gnus-data-pos (car data)))
4674                 (gnus-data-number (car data)))))))
4675     (gnus-summary-position-point)))
4676
4677 (defun gnus-summary-next-subject (n &optional unread dont-display)
4678   "Go to next N'th summary line.
4679 If N is negative, go to the previous N'th subject line.
4680 If UNREAD is non-nil, only unread articles are selected.
4681 The difference between N and the actual number of steps taken is
4682 returned."
4683   (interactive "p")
4684   (let ((backward (< n 0))
4685         (n (abs n)))
4686     (while (and (> n 0)
4687                 (if backward
4688                     (gnus-summary-find-prev unread)
4689                   (gnus-summary-find-next unread)))
4690       (setq n (1- n)))
4691     (if (/= 0 n) (gnus-message 7 "No more%s articles"
4692                                (if unread " unread" "")))
4693     (unless dont-display
4694       (gnus-summary-recenter)
4695       (gnus-summary-position-point))
4696     n))
4697
4698 (defun gnus-summary-next-unread-subject (n)
4699   "Go to next N'th unread summary line."
4700   (interactive "p")
4701   (gnus-summary-next-subject n t))
4702
4703 (defun gnus-summary-prev-subject (n &optional unread)
4704   "Go to previous N'th summary line.
4705 If optional argument UNREAD is non-nil, only unread article is selected."
4706   (interactive "p")
4707   (gnus-summary-next-subject (- n) unread))
4708
4709 (defun gnus-summary-prev-unread-subject (n)
4710   "Go to previous N'th unread summary line."
4711   (interactive "p")
4712   (gnus-summary-next-subject (- n) t))
4713
4714 (defun gnus-summary-goto-subject (article &optional force silent)
4715   "Go the subject line of ARTICLE.
4716 If FORCE, also allow jumping to articles not currently shown."
4717   (let ((b (point))
4718         (data (gnus-data-find article)))
4719     ;; We read in the article if we have to.
4720     (and (not data)
4721          force
4722          (gnus-summary-insert-subject article (and (vectorp force) force) t)
4723          (setq data (gnus-data-find article)))
4724     (goto-char b)
4725     (if (not data)
4726         (progn
4727           (unless silent
4728             (gnus-message 3 "Can't find article %d" article))
4729           nil)
4730       (goto-char (gnus-data-pos data))
4731       article)))
4732
4733 ;; Walking around summary lines with displaying articles.
4734
4735 (defun gnus-summary-expand-window (&optional arg)
4736   "Make the summary buffer take up the entire Emacs frame.
4737 Given a prefix, will force an `article' buffer configuration."
4738   (interactive "P")
4739   (gnus-set-global-variables)
4740   (if arg
4741       (gnus-configure-windows 'article 'force)
4742     (gnus-configure-windows 'summary 'force)))
4743
4744 (defun gnus-summary-display-article (article &optional all-header)
4745   "Display ARTICLE in article buffer."
4746   (gnus-set-global-variables)
4747   (if (null article)
4748       nil
4749     (prog1
4750         (if gnus-summary-display-article-function
4751             (funcall gnus-summary-display-article-function article all-header)
4752           (gnus-article-prepare article all-header))
4753       (run-hooks 'gnus-select-article-hook)
4754       (when (and gnus-current-article
4755                  (not (zerop gnus-current-article)))
4756         (gnus-summary-goto-subject gnus-current-article))
4757       (gnus-summary-recenter)
4758       (when gnus-use-trees
4759         (gnus-possibly-generate-tree article)
4760         (gnus-highlight-selected-tree article))
4761       ;; Successfully display article.
4762       (gnus-article-set-window-start
4763        (cdr (assq article gnus-newsgroup-bookmarks))))))
4764
4765 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
4766   "Select the current article.
4767 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
4768 non-nil, the article will be re-fetched even if it already present in
4769 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
4770 be displayed."
4771   ;; Make sure we are in the summary buffer to work around bbdb bug.
4772   (unless (eq major-mode 'gnus-summary-mode)
4773     (set-buffer gnus-summary-buffer))
4774   (let ((article (or article (gnus-summary-article-number)))
4775         (all-headers (not (not all-headers))) ;Must be T or NIL.
4776         gnus-summary-display-article-function
4777         did)
4778     (and (not pseudo)
4779          (gnus-summary-article-pseudo-p article)
4780          (error "This is a pseudo-article."))
4781     (prog1
4782         (save-excursion
4783           (set-buffer gnus-summary-buffer)
4784           (if (or (and gnus-single-article-buffer
4785                        (or (null gnus-current-article)
4786                            (null gnus-article-current)
4787                            (null (get-buffer gnus-article-buffer))
4788                            (not (eq article (cdr gnus-article-current)))
4789                            (not (equal (car gnus-article-current)
4790                                        gnus-newsgroup-name))))
4791                   (and (not gnus-single-article-buffer)
4792                        (or (null gnus-current-article)
4793                            (not (eq gnus-current-article article))))
4794                   force)
4795               ;; The requested article is different from the current article.
4796               (prog1
4797                   (gnus-summary-display-article article all-headers)
4798                 (setq did article))
4799             (if (or all-headers gnus-show-all-headers)
4800                 (gnus-article-show-all-headers))
4801             'old))
4802       (if did
4803           (gnus-article-set-window-start
4804            (cdr (assq article gnus-newsgroup-bookmarks)))))))
4805
4806 (defun gnus-summary-set-current-mark (&optional current-mark)
4807   "Obsolete function."
4808   nil)
4809
4810 (defun gnus-summary-next-article (&optional unread subject backward push)
4811   "Select the next article.
4812 If UNREAD, only unread articles are selected.
4813 If SUBJECT, only articles with SUBJECT are selected.
4814 If BACKWARD, the previous article is selected instead of the next."
4815   (interactive "P")
4816   (gnus-set-global-variables)
4817   (cond
4818    ;; Is there such an article?
4819    ((and (gnus-summary-search-forward unread subject backward)
4820          (or (gnus-summary-display-article (gnus-summary-article-number))
4821              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
4822     (gnus-summary-position-point))
4823    ;; If not, we try the first unread, if that is wanted.
4824    ((and subject
4825          gnus-auto-select-same
4826          ;; Make sure that we don't select the current article.
4827          (not (eq (gnus-summary-article-number)
4828                   (save-excursion
4829                     (gnus-summary-first-subject t)
4830                     (gnus-summary-article-number))))
4831          (gnus-summary-first-unread-article))
4832     (gnus-summary-position-point)
4833     (gnus-message 6 "Wrapped"))
4834    ;; Try to get next/previous article not displayed in this group.
4835    ((and gnus-auto-extend-newsgroup
4836          (not unread) (not subject))
4837     (gnus-summary-goto-article
4838      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
4839      nil t))
4840    ;; Go to next/previous group.
4841    (t
4842     (or (gnus-ephemeral-group-p gnus-newsgroup-name)
4843         (gnus-summary-jump-to-group gnus-newsgroup-name))
4844     (let ((cmd last-command-char)
4845           (group
4846            (if (eq gnus-keep-same-level 'best)
4847                (gnus-summary-best-group gnus-newsgroup-name)
4848              (gnus-summary-search-group backward gnus-keep-same-level))))
4849       ;; For some reason, the group window gets selected.  We change
4850       ;; it back.
4851       (select-window (get-buffer-window (current-buffer)))
4852       ;; Select next unread newsgroup automagically.
4853       (cond
4854        ((or (not gnus-auto-select-next)
4855             (not cmd))
4856         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
4857        ((or (eq gnus-auto-select-next 'quietly)
4858             (and (eq gnus-auto-select-next 'slightly-quietly)
4859                  push)
4860             (and (eq gnus-auto-select-next 'almost-quietly)
4861                  (gnus-summary-last-article-p)))
4862         ;; Select quietly.
4863         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
4864             (gnus-summary-exit)
4865           (gnus-message 7 "No more%s articles (%s)..."
4866                         (if unread " unread" "")
4867                         (if group (concat "selecting " group)
4868                           "exiting"))
4869           (gnus-summary-next-group nil group backward)))
4870        (t
4871         (gnus-summary-walk-group-buffer
4872          gnus-newsgroup-name cmd unread backward)))))))
4873
4874 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
4875   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
4876                       (?\C-p (gnus-group-prev-unread-group 1))))
4877         (cursor-in-echo-area t)
4878         keve key group ended)
4879     (save-excursion
4880       (set-buffer gnus-group-buffer)
4881       (gnus-summary-jump-to-group from-group)
4882       (setq group
4883             (if (eq gnus-keep-same-level 'best)
4884                 (gnus-summary-best-group gnus-newsgroup-name)
4885               (gnus-summary-search-group backward gnus-keep-same-level))))
4886     (while (not ended)
4887       (gnus-message
4888        5 "No more%s articles%s" (if unread " unread" "")
4889        (if (and group
4890                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
4891            (format " (Type %s for %s [%s])"
4892                    (single-key-description cmd) group
4893                    (car (gnus-gethash group gnus-newsrc-hashtb)))
4894          (format " (Type %s to exit %s)"
4895                  (single-key-description cmd)
4896                  gnus-newsgroup-name)))
4897       ;; Confirm auto selection.
4898       (setq key (car (setq keve (gnus-read-event-char))))
4899       (setq ended t)
4900       (cond
4901        ((assq key keystrokes)
4902         (let ((obuf (current-buffer)))
4903           (switch-to-buffer gnus-group-buffer)
4904           (and group
4905                (gnus-group-jump-to-group group))
4906           (eval (cadr (assq key keystrokes)))
4907           (setq group (gnus-group-group-name))
4908           (switch-to-buffer obuf))
4909         (setq ended nil))
4910        ((equal key cmd)
4911         (if (or (not group)
4912                 (gnus-ephemeral-group-p gnus-newsgroup-name))
4913             (gnus-summary-exit)
4914           (gnus-summary-next-group nil group backward)))
4915        (t
4916         (push (cdr keve) unread-command-events))))))
4917
4918 (defun gnus-summary-next-unread-article ()
4919   "Select unread article after current one."
4920   (interactive)
4921   (gnus-summary-next-article t (and gnus-auto-select-same
4922                                     (gnus-summary-article-subject))))
4923
4924 (defun gnus-summary-prev-article (&optional unread subject)
4925   "Select the article after the current one.
4926 If UNREAD is non-nil, only unread articles are selected."
4927   (interactive "P")
4928   (gnus-summary-next-article unread subject t))
4929
4930 (defun gnus-summary-prev-unread-article ()
4931   "Select unread article before current one."
4932   (interactive)
4933   (gnus-summary-prev-article t (and gnus-auto-select-same
4934                                     (gnus-summary-article-subject))))
4935
4936 (defun gnus-summary-next-page (&optional lines circular)
4937   "Show next page of the selected article.
4938 If at the end of the current article, select the next article.
4939 LINES says how many lines should be scrolled up.
4940
4941 If CIRCULAR is non-nil, go to the start of the article instead of
4942 selecting the next article when reaching the end of the current
4943 article."
4944   (interactive "P")
4945   (setq gnus-summary-buffer (current-buffer))
4946   (gnus-set-global-variables)
4947   (let ((article (gnus-summary-article-number))
4948         (endp nil))
4949     (gnus-configure-windows 'article)
4950     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
4951         (if (and (eq gnus-summary-goto-unread 'never)
4952                  (not (gnus-summary-last-article-p article)))
4953             (gnus-summary-next-article)
4954           (gnus-summary-next-unread-article))
4955       (if (or (null gnus-current-article)
4956               (null gnus-article-current)
4957               (/= article (cdr gnus-article-current))
4958               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
4959           ;; Selected subject is different from current article's.
4960           (gnus-summary-display-article article)
4961         (gnus-eval-in-buffer-window gnus-article-buffer
4962           (setq endp (gnus-article-next-page lines)))
4963         (when endp
4964           (cond (circular
4965                  (gnus-summary-beginning-of-article))
4966                 (lines
4967                  (gnus-message 3 "End of message"))
4968                 ((null lines)
4969                  (if (and (eq gnus-summary-goto-unread 'never)
4970                           (not (gnus-summary-last-article-p article)))
4971                      (gnus-summary-next-article)
4972                    (gnus-summary-next-unread-article)))))))
4973     (gnus-summary-recenter)
4974     (gnus-summary-position-point)))
4975
4976 (defun gnus-summary-prev-page (&optional lines)
4977   "Show previous page of selected article.
4978 Argument LINES specifies lines to be scrolled down."
4979   (interactive "P")
4980   (gnus-set-global-variables)
4981   (let ((article (gnus-summary-article-number)))
4982     (gnus-configure-windows 'article)
4983     (if (or (null gnus-current-article)
4984             (null gnus-article-current)
4985             (/= article (cdr gnus-article-current))
4986             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
4987         ;; Selected subject is different from current article's.
4988         (gnus-summary-display-article article)
4989       (gnus-summary-recenter)
4990       (gnus-eval-in-buffer-window gnus-article-buffer
4991         (gnus-article-prev-page lines))))
4992   (gnus-summary-position-point))
4993
4994 (defun gnus-summary-scroll-up (lines)
4995   "Scroll up (or down) one line current article.
4996 Argument LINES specifies lines to be scrolled up (or down if negative)."
4997   (interactive "p")
4998   (gnus-set-global-variables)
4999   (gnus-configure-windows 'article)
5000   (gnus-summary-show-thread)
5001   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5002     (gnus-eval-in-buffer-window gnus-article-buffer
5003       (cond ((> lines 0)
5004              (if (gnus-article-next-page lines)
5005                  (gnus-message 3 "End of message")))
5006             ((< lines 0)
5007              (gnus-article-prev-page (- lines))))))
5008   (gnus-summary-recenter)
5009   (gnus-summary-position-point))
5010
5011 (defun gnus-summary-next-same-subject ()
5012   "Select next article which has the same subject as current one."
5013   (interactive)
5014   (gnus-set-global-variables)
5015   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5016
5017 (defun gnus-summary-prev-same-subject ()
5018   "Select previous article which has the same subject as current one."
5019   (interactive)
5020   (gnus-set-global-variables)
5021   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5022
5023 (defun gnus-summary-next-unread-same-subject ()
5024   "Select next unread article which has the same subject as current one."
5025   (interactive)
5026   (gnus-set-global-variables)
5027   (gnus-summary-next-article t (gnus-summary-article-subject)))
5028
5029 (defun gnus-summary-prev-unread-same-subject ()
5030   "Select previous unread article which has the same subject as current one."
5031   (interactive)
5032   (gnus-set-global-variables)
5033   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5034
5035 (defun gnus-summary-first-unread-article ()
5036   "Select the first unread article.
5037 Return nil if there are no unread articles."
5038   (interactive)
5039   (gnus-set-global-variables)
5040   (prog1
5041       (when (gnus-summary-first-subject t)
5042         (gnus-summary-show-thread)
5043         (gnus-summary-first-subject t)
5044         (gnus-summary-display-article (gnus-summary-article-number)))
5045     (gnus-summary-position-point)))
5046
5047 (defun gnus-summary-best-unread-article ()
5048   "Select the unread article with the highest score."
5049   (interactive)
5050   (gnus-set-global-variables)
5051   (let ((best -1000000)
5052         (data gnus-newsgroup-data)
5053         article score)
5054     (while data
5055       (and (gnus-data-unread-p (car data))
5056            (> (setq score
5057                     (gnus-summary-article-score (gnus-data-number (car data))))
5058               best)
5059            (setq best score
5060                  article (gnus-data-number (car data))))
5061       (setq data (cdr data)))
5062     (prog1
5063         (if article
5064             (gnus-summary-goto-article article)
5065           (error "No unread articles"))
5066       (gnus-summary-position-point))))
5067
5068 (defun gnus-summary-last-subject ()
5069   "Go to the last displayed subject line in the group."
5070   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5071     (when article
5072       (gnus-summary-goto-subject article))))
5073
5074 (defun gnus-summary-goto-article (article &optional all-headers force)
5075   "Fetch ARTICLE and display it if it exists.
5076 If ALL-HEADERS is non-nil, no header lines are hidden."
5077   (interactive
5078    (list
5079     (string-to-int
5080      (completing-read
5081       "Article number: "
5082       (mapcar (lambda (number) (list (int-to-string number)))
5083               gnus-newsgroup-limit)))
5084     current-prefix-arg
5085     t))
5086   (prog1
5087       (if (gnus-summary-goto-subject article force)
5088           (gnus-summary-display-article article all-headers)
5089         (gnus-message 4 "Couldn't go to article %s" article) nil)
5090     (gnus-summary-position-point)))
5091
5092 (defun gnus-summary-goto-last-article ()
5093   "Go to the previously read article."
5094   (interactive)
5095   (prog1
5096       (and gnus-last-article
5097            (gnus-summary-goto-article gnus-last-article))
5098     (gnus-summary-position-point)))
5099
5100 (defun gnus-summary-pop-article (number)
5101   "Pop one article off the history and go to the previous.
5102 NUMBER articles will be popped off."
5103   (interactive "p")
5104   (let (to)
5105     (setq gnus-newsgroup-history
5106           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5107     (if to
5108         (gnus-summary-goto-article (car to))
5109       (error "Article history empty")))
5110   (gnus-summary-position-point))
5111
5112 ;; Summary commands and functions for limiting the summary buffer.
5113
5114 (defun gnus-summary-limit-to-articles (n)
5115   "Limit the summary buffer to the next N articles.
5116 If not given a prefix, use the process marked articles instead."
5117   (interactive "P")
5118   (gnus-set-global-variables)
5119   (prog1
5120       (let ((articles (gnus-summary-work-articles n)))
5121         (setq gnus-newsgroup-processable nil)
5122         (gnus-summary-limit articles))
5123     (gnus-summary-position-point)))
5124
5125 (defun gnus-summary-pop-limit (&optional total)
5126   "Restore the previous limit.
5127 If given a prefix, remove all limits."
5128   (interactive "P")
5129   (gnus-set-global-variables)
5130   (when total 
5131     (setq gnus-newsgroup-limits
5132           (list (mapcar (lambda (h) (mail-header-number h))
5133                         gnus-newsgroup-headers))))
5134   (unless gnus-newsgroup-limits
5135     (error "No limit to pop"))
5136   (prog1
5137       (gnus-summary-limit nil 'pop)
5138     (gnus-summary-position-point)))
5139
5140 (defun gnus-summary-limit-to-subject (subject &optional header)
5141   "Limit the summary buffer to articles that have subjects that match a regexp."
5142   (interactive "sRegexp: ")
5143   (unless header
5144     (setq header "subject"))
5145   (when (not (equal "" subject))
5146     (prog1
5147         (let ((articles (gnus-summary-find-matching
5148                          (or header "subject") subject 'all)))
5149           (or articles (error "Found no matches for \"%s\"" subject))
5150           (gnus-summary-limit articles))
5151       (gnus-summary-position-point))))
5152
5153 (defun gnus-summary-limit-to-author (from)
5154   "Limit the summary buffer to articles that have authors that match a regexp."
5155   (interactive "sRegexp: ")
5156   (gnus-summary-limit-to-subject from "from"))
5157
5158 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5159 (make-obsolete
5160  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5161
5162 (defun gnus-summary-limit-to-unread (&optional all)
5163   "Limit the summary buffer to articles that are not marked as read.
5164 If ALL is non-nil, limit strictly to unread articles."
5165   (interactive "P")
5166   (if all
5167       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5168     (gnus-summary-limit-to-marks
5169      ;; Concat all the marks that say that an article is read and have
5170      ;; those removed.
5171      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5172            gnus-killed-mark gnus-kill-file-mark
5173            gnus-low-score-mark gnus-expirable-mark
5174            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5175            gnus-duplicate-mark)
5176      'reverse)))
5177
5178 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
5179 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
5180
5181 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5182   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5183 If REVERSE, limit the summary buffer to articles that are not marked
5184 with MARKS.  MARKS can either be a string of marks or a list of marks.
5185 Returns how many articles were removed."
5186   (interactive "sMarks: ")
5187   (gnus-set-global-variables)
5188   (prog1
5189       (let ((data gnus-newsgroup-data)
5190             (marks (if (listp marks) marks
5191                      (append marks nil))) ; Transform to list.
5192             articles)
5193         (while data
5194           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
5195                  (memq (gnus-data-mark (car data)) marks))
5196                (setq articles (cons (gnus-data-number (car data)) articles)))
5197           (setq data (cdr data)))
5198         (gnus-summary-limit articles))
5199     (gnus-summary-position-point)))
5200
5201 (defun gnus-summary-limit-to-score (&optional score)
5202   "Limit to articles with score at or above SCORE."
5203   (interactive "P")
5204   (gnus-set-global-variables)
5205   (setq score (if score
5206                   (prefix-numeric-value score)
5207                 (or gnus-summary-default-score 0)))
5208   (let ((data gnus-newsgroup-data)
5209         articles)
5210     (while data
5211       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5212                 score)
5213         (push (gnus-data-number (car data)) articles))
5214       (setq data (cdr data)))
5215     (prog1
5216         (gnus-summary-limit articles)
5217       (gnus-summary-position-point))))
5218
5219 (defun gnus-summary-limit-include-dormant ()
5220   "Display all the hidden articles that are marked as dormant."
5221   (interactive)
5222   (gnus-set-global-variables)
5223   (or gnus-newsgroup-dormant
5224       (error "There are no dormant articles in this group"))
5225   (prog1
5226       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5227     (gnus-summary-position-point)))
5228
5229 (defun gnus-summary-limit-exclude-dormant ()
5230   "Hide all dormant articles."
5231   (interactive)
5232   (gnus-set-global-variables)
5233   (prog1
5234       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5235     (gnus-summary-position-point)))
5236
5237 (defun gnus-summary-limit-exclude-childless-dormant ()
5238   "Hide all dormant articles that have no children."
5239   (interactive)
5240   (gnus-set-global-variables)
5241   (let ((data (gnus-data-list t))
5242         articles d children)
5243     ;; Find all articles that are either not dormant or have
5244     ;; children.
5245     (while (setq d (pop data))
5246       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
5247                 (and (setq children 
5248                            (gnus-article-children (gnus-data-number d)))
5249                      (let (found)
5250                        (while children
5251                          (when (memq (car children) articles)
5252                            (setq children nil
5253                                  found t))
5254                          (pop children))
5255                        found)))
5256         (push (gnus-data-number d) articles)))
5257     ;; Do the limiting.
5258     (prog1
5259         (gnus-summary-limit articles)
5260       (gnus-summary-position-point))))
5261
5262 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
5263   "Mark all unread excluded articles as read.
5264 If ALL, mark even excluded ticked and dormants as read."
5265   (interactive "P")
5266   (let ((articles (gnus-sorted-complement
5267                    (sort
5268                     (mapcar (lambda (h) (mail-header-number h))
5269                             gnus-newsgroup-headers)
5270                     '<)
5271                    (sort gnus-newsgroup-limit '<)))
5272         article)
5273     (setq gnus-newsgroup-unreads nil)
5274     (if all
5275         (setq gnus-newsgroup-dormant nil
5276               gnus-newsgroup-marked nil
5277               gnus-newsgroup-reads
5278               (nconc
5279                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
5280                gnus-newsgroup-reads))
5281       (while (setq article (pop articles))
5282         (unless (or (memq article gnus-newsgroup-dormant)
5283                     (memq article gnus-newsgroup-marked))
5284           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
5285
5286 (defun gnus-summary-limit (articles &optional pop)
5287   (if pop
5288       ;; We pop the previous limit off the stack and use that.
5289       (setq articles (car gnus-newsgroup-limits)
5290             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
5291     ;; We use the new limit, so we push the old limit on the stack.
5292     (setq gnus-newsgroup-limits
5293           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
5294   ;; Set the limit.
5295   (setq gnus-newsgroup-limit articles)
5296   (let ((total (length gnus-newsgroup-data))
5297         (data (gnus-data-find-list (gnus-summary-article-number)))
5298         (gnus-summary-mark-below nil)   ; Inhibit this.
5299         found)
5300     ;; This will do all the work of generating the new summary buffer
5301     ;; according to the new limit.
5302     (gnus-summary-prepare)
5303     ;; Hide any threads, possibly.
5304     (and gnus-show-threads
5305          gnus-thread-hide-subtree
5306          (gnus-summary-hide-all-threads))
5307     ;; Try to return to the article you were at, or one in the
5308     ;; neighborhood.
5309     (when data
5310       ;; We try to find some article after the current one.
5311       (while data
5312         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
5313           (setq data nil
5314                 found t))
5315         (setq data (cdr data))))
5316     (unless found
5317       ;; If there is no data, that means that we were after the last
5318       ;; article.  The same goes when we can't find any articles
5319       ;; after the current one.
5320       (goto-char (point-max))
5321       (gnus-summary-find-prev))
5322     ;; We return how many articles were removed from the summary
5323     ;; buffer as a result of the new limit.
5324     (- total (length gnus-newsgroup-data))))
5325
5326 (defsubst gnus-invisible-cut-children (threads)
5327   (let ((num 0))
5328     (while threads
5329       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
5330         (incf num))
5331       (pop threads))
5332     (< num 2)))
5333
5334 (defsubst gnus-cut-thread (thread)
5335   "Go forwards in the thread until we find an article that we want to display."
5336   (when (or (eq gnus-fetch-old-headers 'some)
5337             (eq gnus-build-sparse-threads 'some)
5338             (eq gnus-build-sparse-threads 'more))
5339     ;; Deal with old-fetched headers and sparse threads.
5340     (while (and
5341             thread
5342             (or
5343              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
5344              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
5345             (or (<= (length (cdr thread)) 1)
5346                 (gnus-invisible-cut-children (cdr thread))))
5347       (setq thread (cadr thread))))
5348   thread)
5349
5350 (defun gnus-cut-threads (threads)
5351   "Cut off all uninteresting articles from the beginning of threads."
5352   (when (or (eq gnus-fetch-old-headers 'some)
5353             (eq gnus-build-sparse-threads 'some)
5354             (eq gnus-build-sparse-threads 'more))
5355     (let ((th threads))
5356       (while th
5357         (setcar th (gnus-cut-thread (car th)))
5358         (setq th (cdr th)))))
5359   ;; Remove nixed out threads.
5360   (delq nil threads))
5361
5362 (defun gnus-summary-initial-limit (&optional show-if-empty)
5363   "Figure out what the initial limit is supposed to be on group entry.
5364 This entails weeding out unwanted dormants, low-scored articles,
5365 fetch-old-headers verbiage, and so on."
5366   ;; Most groups have nothing to remove.
5367   (if (or gnus-inhibit-limiting
5368           (and (null gnus-newsgroup-dormant)
5369                (not (eq gnus-fetch-old-headers 'some))
5370                (null gnus-summary-expunge-below)
5371                (not (eq gnus-build-sparse-threads 'some))
5372                (not (eq gnus-build-sparse-threads 'more))
5373                (null gnus-thread-expunge-below)
5374                (not gnus-use-nocem)))
5375       () ; Do nothing.
5376     (push gnus-newsgroup-limit gnus-newsgroup-limits)
5377     (setq gnus-newsgroup-limit nil)
5378     (mapatoms
5379      (lambda (node)
5380        (unless (car (symbol-value node))
5381          ;; These threads have no parents -- they are roots.
5382          (let ((nodes (cdr (symbol-value node)))
5383                thread)
5384            (while nodes
5385              (if (and gnus-thread-expunge-below
5386                       (< (gnus-thread-total-score (car nodes))
5387                          gnus-thread-expunge-below))
5388                  (gnus-expunge-thread (pop nodes))
5389                (setq thread (pop nodes))
5390                (gnus-summary-limit-children thread))))))
5391      gnus-newsgroup-dependencies)
5392     ;; If this limitation resulted in an empty group, we might
5393     ;; pop the previous limit and use it instead.
5394     (when (and (not gnus-newsgroup-limit)
5395                show-if-empty)
5396       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
5397     gnus-newsgroup-limit))
5398
5399 (defun gnus-summary-limit-children (thread)
5400   "Return 1 if this subthread is visible and 0 if it is not."
5401   ;; First we get the number of visible children to this thread.  This
5402   ;; is done by recursing down the thread using this function, so this
5403   ;; will really go down to a leaf article first, before slowly
5404   ;; working its way up towards the root.
5405   (when thread
5406     (let ((children
5407            (if (cdr thread)
5408                (apply '+ (mapcar 'gnus-summary-limit-children
5409                                  (cdr thread)))
5410              0))
5411           (number (mail-header-number (car thread)))
5412           score)
5413       (if (and
5414            (not (memq number gnus-newsgroup-marked))
5415            (or
5416             ;; If this article is dormant and has absolutely no visible
5417             ;; children, then this article isn't visible.
5418             (and (memq number gnus-newsgroup-dormant)
5419                  (= children 0))
5420             ;; If this is "fetch-old-headered" and there is only one
5421             ;; visible child (or less), then we don't want this article.
5422             (and (eq gnus-fetch-old-headers 'some)
5423                  (memq number gnus-newsgroup-ancient)
5424                  (zerop children))
5425             ;; If this is a sparsely inserted article with no children,
5426             ;; we don't want it.
5427             (and (eq gnus-build-sparse-threads 'some)
5428                  (memq number gnus-newsgroup-sparse)
5429                  (zerop children))
5430             ;; If we use expunging, and this article is really
5431             ;; low-scored, then we don't want this article.
5432             (when (and gnus-summary-expunge-below
5433                        (< (setq score
5434                                 (or (cdr (assq number gnus-newsgroup-scored))
5435                                     gnus-summary-default-score))
5436                           gnus-summary-expunge-below))
5437               ;; We increase the expunge-tally here, but that has
5438               ;; nothing to do with the limits, really.
5439               (incf gnus-newsgroup-expunged-tally)
5440               ;; We also mark as read here, if that's wanted.
5441               (when (and gnus-summary-mark-below
5442                          (< score gnus-summary-mark-below))
5443                 (setq gnus-newsgroup-unreads
5444                       (delq number gnus-newsgroup-unreads))
5445                 (if gnus-newsgroup-auto-expire
5446                     (push number gnus-newsgroup-expirable)
5447                   (push (cons number gnus-low-score-mark)
5448                         gnus-newsgroup-reads)))
5449               t)
5450             ;; Check NoCeM things.
5451             (and gnus-use-nocem
5452                  (gnus-nocem-unwanted-article-p
5453                   (mail-header-id (car thread))))))
5454           ;; Nope, invisible article.
5455           0
5456         ;; Ok, this article is to be visible, so we add it to the limit
5457         ;; and return 1.
5458         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
5459         1))))
5460
5461 (defun gnus-expunge-thread (thread)
5462   "Mark all articles in THREAD as read."
5463   (let* ((number (mail-header-number (car thread))))
5464     (incf gnus-newsgroup-expunged-tally)
5465     ;; We also mark as read here, if that's wanted.
5466     (setq gnus-newsgroup-unreads
5467           (delq number gnus-newsgroup-unreads))
5468     (if gnus-newsgroup-auto-expire
5469         (push number gnus-newsgroup-expirable)
5470       (push (cons number gnus-low-score-mark)
5471             gnus-newsgroup-reads)))
5472   ;; Go recursively through all subthreads.
5473   (mapcar 'gnus-expunge-thread (cdr thread)))
5474
5475 ;; Summary article oriented commands
5476
5477 (defun gnus-summary-refer-parent-article (n)
5478   "Refer parent article N times.
5479 If N is negative, go to ancestor -N instead.
5480 The difference between N and the number of articles fetched is returned."
5481   (interactive "p")
5482   (gnus-set-global-variables)
5483   (let ((skip 1)
5484         error header ref)
5485     (when (not (natnump n))
5486       (setq skip (abs n)
5487             n 1))
5488     (while (and (> n 0)
5489                 (not error))
5490       (setq header (gnus-summary-article-header))
5491       (setq ref
5492             ;; If we try to find the parent of the currently
5493             ;; displayed article, then we take a look at the actual
5494             ;; References header, since this is slightly more
5495             ;; reliable than the References field we got from the
5496             ;; server.
5497             (if (and (eq (mail-header-number header)
5498                          (cdr gnus-article-current))
5499                      (equal gnus-newsgroup-name
5500                             (car gnus-article-current)))
5501                 (save-excursion
5502                   (set-buffer gnus-original-article-buffer)
5503                   (nnheader-narrow-to-headers)
5504                   (prog1
5505                       (message-fetch-field "references")
5506                     (widen)))
5507               ;; It's not the current article, so we take a bet on
5508               ;; the value we got from the server.
5509               (mail-header-references header)))
5510       (if ref 
5511           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
5512             (gnus-message 1 "Couldn't find parent"))
5513         (gnus-message 1 "No references in article %d"
5514                       (gnus-summary-article-number))
5515         (setq error t))
5516       (decf n))
5517     (gnus-summary-position-point)
5518     n))
5519
5520 (defun gnus-summary-refer-references ()
5521   "Fetch all articles mentioned in the References header.
5522 Return how many articles were fetched."
5523   (interactive)
5524   (gnus-set-global-variables)
5525   (let ((ref (mail-header-references (gnus-summary-article-header)))
5526         (current (gnus-summary-article-number))
5527         (n 0))
5528     ;; For each Message-ID in the References header...
5529     (while (string-match "<[^>]*>" ref)
5530       (incf n)
5531       ;; ... fetch that article.
5532       (gnus-summary-refer-article
5533        (prog1 (match-string 0 ref)
5534          (setq ref (substring ref (match-end 0))))))
5535     (gnus-summary-goto-subject current)
5536     (gnus-summary-position-point)
5537     n))
5538
5539 (defun gnus-summary-refer-article (message-id)
5540   "Fetch an article specified by MESSAGE-ID."
5541   (interactive "sMessage-ID: ")
5542   (when (and (stringp message-id)
5543              (not (zerop (length message-id))))
5544     ;; Construct the correct Message-ID if necessary.
5545     ;; Suggested by tale@pawl.rpi.edu.
5546     (unless (string-match "^<" message-id)
5547       (setq message-id (concat "<" message-id)))
5548     (unless (string-match ">$" message-id)
5549       (setq message-id (concat message-id ">")))
5550     (let* ((header (gnus-id-to-header message-id))
5551            (sparse (and header
5552                         (memq (mail-header-number header)
5553                               gnus-newsgroup-sparse))))
5554       (if header
5555           (prog1
5556               ;; The article is present in the buffer, to we just go to it.
5557               (gnus-summary-goto-article 
5558                (mail-header-number header) nil header)
5559             (when sparse
5560               (gnus-summary-update-article (mail-header-number header))))
5561         ;; We fetch the article
5562         (let ((gnus-override-method 
5563                (and (gnus-news-group-p gnus-newsgroup-name)
5564                     gnus-refer-article-method))
5565               number)
5566           ;; Start the special refer-article method, if necessary.
5567           (when (and gnus-refer-article-method
5568                      (gnus-news-group-p gnus-newsgroup-name))
5569             (gnus-check-server gnus-refer-article-method))
5570           ;; Fetch the header, and display the article.
5571           (if (setq number (gnus-summary-insert-subject message-id))
5572               (gnus-summary-select-article nil nil nil number)
5573             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
5574
5575 (defun gnus-summary-enter-digest-group (&optional force)
5576   "Enter an nndoc group based on the current article.
5577 If FORCE, force a digest interpretation.  If not, try
5578 to guess what the document format is."
5579   (interactive "P")
5580   (gnus-set-global-variables)
5581   (gnus-summary-select-article)
5582   (let* ((name (format "%s-%d"
5583                        (gnus-group-prefixed-name
5584                         gnus-newsgroup-name (list 'nndoc ""))
5585                        gnus-current-article))
5586          (ogroup gnus-newsgroup-name)
5587          (params (append (gnus-info-params (gnus-get-info ogroup))
5588                          (list (cons 'to-group ogroup))))
5589          (case-fold-search t)
5590          (buf (current-buffer))
5591          dig)
5592     (save-excursion
5593       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
5594       (insert-buffer-substring gnus-original-article-buffer)
5595       (narrow-to-region
5596        (goto-char (point-min))
5597        (or (search-forward "\n\n" nil t) (point)))
5598       (goto-char (point-min))
5599       (delete-matching-lines "^\\(Path\\):\\|^From ")
5600       (widen))
5601     (unwind-protect
5602         (if (gnus-group-read-ephemeral-group
5603              name `(nndoc ,name (nndoc-address
5604                                  ,(get-buffer dig))
5605                           (nndoc-article-type ,(if force 'digest 'guess))) t)
5606             ;; Make all postings to this group go to the parent group.
5607             (nconc (gnus-info-params (gnus-get-info name))
5608                    params)
5609           ;; Couldn't select this doc group.
5610           (switch-to-buffer buf)
5611           (gnus-set-global-variables)
5612           (gnus-configure-windows 'summary)
5613           (gnus-message 3 "Article couldn't be entered?"))
5614       (kill-buffer dig))))
5615
5616 (defun gnus-summary-read-document (n)
5617   "Open a new group based on the current article(s).
5618 Obeys the standard process/prefix convention."
5619   (interactive "P")
5620   (let* ((articles (gnus-summary-work-articles n))
5621          (ogroup gnus-newsgroup-name)
5622          (params (append (gnus-info-params (gnus-get-info ogroup))
5623                          (list (cons 'to-group ogroup))))
5624          article group egroup groups vgroup)
5625     (while (setq article (pop articles))
5626       (setq group (format "%s-%d" gnus-newsgroup-name gnus-current-article))
5627       (gnus-summary-remove-process-mark article)
5628       (when (gnus-summary-display-article article)
5629         (save-excursion
5630           (nnheader-temp-write nil
5631             (insert-buffer-substring gnus-original-article-buffer)
5632             ;; Remove some headers that may lead nndoc to make
5633             ;; the wrong guess.
5634             (message-narrow-to-head)
5635             (goto-char (point-min))
5636             (delete-matching-lines "^\\(Path\\):\\|^From ")
5637             (widen)
5638             (if (setq egroup
5639                       (gnus-group-read-ephemeral-group
5640                        group `(nndoc ,group (nndoc-address ,(current-buffer))
5641                                      (nndoc-article-type guess))
5642                        t nil t))
5643                 (progn
5644                   ;; Make all postings to this group go to the parent group.
5645                   (nconc (gnus-info-params (gnus-get-info egroup))
5646                          params)
5647                   (push egroup groups))
5648               ;; Couldn't select this doc group.
5649               (gnus-error 3 "Article couldn't be entered"))))))
5650     ;; Now we have selected all the documents.
5651     (cond
5652      ((not groups)
5653       (error "None of the articles could be interpreted as documents"))
5654      ((gnus-group-read-ephemeral-group
5655        (setq vgroup (format
5656                      "%s-%s" gnus-newsgroup-name
5657                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
5658        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
5659        t
5660        (cons (current-buffer) 'summary)))
5661      (t
5662       (error "Couldn't select virtual nndoc group")))))
5663       
5664 (defun gnus-summary-isearch-article (&optional regexp-p)
5665   "Do incremental search forward on the current article.
5666 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
5667   (interactive "P")
5668   (gnus-set-global-variables)
5669   (gnus-summary-select-article)
5670   (gnus-configure-windows 'article)
5671   (gnus-eval-in-buffer-window gnus-article-buffer
5672     ;;(goto-char (point-min))
5673     (isearch-forward regexp-p)))
5674
5675 (defun gnus-summary-search-article-forward (regexp &optional backward)
5676   "Search for an article containing REGEXP forward.
5677 If BACKWARD, search backward instead."
5678   (interactive
5679    (list (read-string
5680           (format "Search article %s (regexp%s): "
5681                   (if current-prefix-arg "backward" "forward")
5682                   (if gnus-last-search-regexp
5683                       (concat ", default " gnus-last-search-regexp)
5684                     "")))
5685          current-prefix-arg))
5686   (gnus-set-global-variables)
5687   (if (string-equal regexp "")
5688       (setq regexp (or gnus-last-search-regexp ""))
5689     (setq gnus-last-search-regexp regexp))
5690   (if (gnus-summary-search-article regexp backward)
5691       (gnus-summary-show-thread)
5692     (error "Search failed: \"%s\"" regexp)))
5693
5694 (defun gnus-summary-search-article-backward (regexp)
5695   "Search for an article containing REGEXP backward."
5696   (interactive
5697    (list (read-string
5698           (format "Search article backward (regexp%s): "
5699                   (if gnus-last-search-regexp
5700                       (concat ", default " gnus-last-search-regexp)
5701                     "")))))
5702   (gnus-summary-search-article-forward regexp 'backward))
5703
5704 (defun gnus-summary-search-article (regexp &optional backward)
5705   "Search for an article containing REGEXP.
5706 Optional argument BACKWARD means do search for backward.
5707 `gnus-select-article-hook' is not called during the search."
5708   (let ((gnus-select-article-hook nil)  ;Disable hook.
5709         (gnus-article-display-hook nil)
5710         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
5711         (gnus-use-article-prefetch nil)
5712         (re-search
5713          (if backward
5714              're-search-backward 're-search-forward))
5715         (sum (current-buffer))
5716         (found nil)
5717         point)
5718     (gnus-save-hidden-threads
5719       (gnus-summary-select-article)
5720       (set-buffer gnus-article-buffer)
5721       (when backward
5722         (forward-line -1))
5723       (while (not found)
5724         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
5725         (if (if backward
5726                 (re-search-backward regexp nil t)
5727               (re-search-forward regexp nil t))
5728             ;; We found the regexp.
5729             (progn
5730               (setq found 'found)
5731               (beginning-of-line)
5732               (set-window-start
5733                (get-buffer-window (current-buffer))
5734                (point))
5735               (forward-line 1)
5736               (set-buffer sum)
5737               (setq point (point)))
5738           ;; We didn't find it, so we go to the next article.
5739           (set-buffer sum)
5740           (if (not (if backward (gnus-summary-find-prev)
5741                      (gnus-summary-find-next)))
5742               ;; No more articles.
5743               (setq found t)
5744             ;; Select the next article and adjust point.
5745             (gnus-summary-select-article)
5746             (set-buffer gnus-article-buffer)
5747             (widen)
5748             (goto-char (if backward (point-max) (point-min))))))
5749       (gnus-message 7 ""))
5750     ;; Return whether we found the regexp.
5751     (when (eq found 'found)
5752       (goto-char point)
5753       (gnus-summary-show-thread)
5754       (gnus-summary-goto-subject gnus-current-article)
5755       (gnus-summary-position-point)
5756       t)))
5757
5758 (defun gnus-summary-find-matching (header regexp &optional backward unread
5759                                           not-case-fold)
5760   "Return a list of all articles that match REGEXP on HEADER.
5761 The search stars on the current article and goes forwards unless
5762 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
5763 If UNREAD is non-nil, only unread articles will
5764 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
5765 in the comparisons."
5766   (let ((data (if (eq backward 'all) gnus-newsgroup-data
5767                 (gnus-data-find-list
5768                  (gnus-summary-article-number) (gnus-data-list backward))))
5769         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
5770         (case-fold-search (not not-case-fold))
5771         articles d)
5772     (or (fboundp (intern (concat "mail-header-" header)))
5773         (error "%s is not a valid header" header))
5774     (while data
5775       (setq d (car data))
5776       (and (or (not unread)             ; We want all articles...
5777                (gnus-data-unread-p d))  ; Or just unreads.
5778            (vectorp (gnus-data-header d)) ; It's not a pseudo.
5779            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
5780            (setq articles (cons (gnus-data-number d) articles))) ; Success!
5781       (setq data (cdr data)))
5782     (nreverse articles)))
5783
5784 (defun gnus-summary-execute-command (header regexp command &optional backward)
5785   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
5786 If HEADER is an empty string (or nil), the match is done on the entire
5787 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
5788   (interactive
5789    (list (let ((completion-ignore-case t))
5790            (completing-read
5791             "Header name: "
5792             (mapcar (lambda (string) (list string))
5793                     '("Number" "Subject" "From" "Lines" "Date"
5794                       "Message-ID" "Xref" "References" "Body"))
5795             nil 'require-match))
5796          (read-string "Regexp: ")
5797          (read-key-sequence "Command: ")
5798          current-prefix-arg))
5799   (when (equal header "Body")
5800     (setq header ""))
5801   (gnus-set-global-variables)
5802   ;; Hidden thread subtrees must be searched as well.
5803   (gnus-summary-show-all-threads)
5804   ;; We don't want to change current point nor window configuration.
5805   (save-excursion
5806     (save-window-excursion
5807       (gnus-message 6 "Executing %s..." (key-description command))
5808       ;; We'd like to execute COMMAND interactively so as to give arguments.
5809       (gnus-execute header regexp
5810                     `(call-interactively ',(key-binding command))
5811                     backward)
5812       (gnus-message 6 "Executing %s...done" (key-description command)))))
5813
5814 (defun gnus-summary-beginning-of-article ()
5815   "Scroll the article back to the beginning."
5816   (interactive)
5817   (gnus-set-global-variables)
5818   (gnus-summary-select-article)
5819   (gnus-configure-windows 'article)
5820   (gnus-eval-in-buffer-window gnus-article-buffer
5821     (widen)
5822     (goto-char (point-min))
5823     (and gnus-break-pages (gnus-narrow-to-page))))
5824
5825 (defun gnus-summary-end-of-article ()
5826   "Scroll to the end of the article."
5827   (interactive)
5828   (gnus-set-global-variables)
5829   (gnus-summary-select-article)
5830   (gnus-configure-windows 'article)
5831   (gnus-eval-in-buffer-window gnus-article-buffer
5832     (widen)
5833     (goto-char (point-max))
5834     (recenter -3)
5835     (and gnus-break-pages (gnus-narrow-to-page))))
5836
5837 (defun gnus-summary-show-article (&optional arg)
5838   "Force re-fetching of the current article.
5839 If ARG (the prefix) is non-nil, show the raw article without any
5840 article massaging functions being run."
5841   (interactive "P")
5842   (gnus-set-global-variables)
5843   (if (not arg)
5844       ;; Select the article the normal way.
5845       (gnus-summary-select-article nil 'force)
5846     ;; Bind the article treatment functions to nil.
5847     (let ((gnus-have-all-headers t)
5848           gnus-article-display-hook
5849           gnus-article-prepare-hook
5850           gnus-break-pages
5851           gnus-visual)
5852       (gnus-summary-select-article nil 'force)))
5853   (gnus-summary-goto-subject gnus-current-article)
5854 ;  (gnus-configure-windows 'article)
5855   (gnus-summary-position-point))
5856
5857 (defun gnus-summary-verbose-headers (&optional arg)
5858   "Toggle permanent full header display.
5859 If ARG is a positive number, turn header display on.
5860 If ARG is a negative number, turn header display off."
5861   (interactive "P")
5862   (gnus-set-global-variables)
5863   (setq gnus-show-all-headers
5864         (cond ((or (not (numberp arg))
5865                    (zerop arg))
5866                (not gnus-show-all-headers))
5867               ((natnump arg)
5868                t)))
5869   (gnus-summary-show-article))
5870
5871 (defun gnus-summary-toggle-header (&optional arg)
5872   "Show the headers if they are hidden, or hide them if they are shown.
5873 If ARG is a positive number, show the entire header.
5874 If ARG is a negative number, hide the unwanted header lines."
5875   (interactive "P")
5876   (gnus-set-global-variables)
5877   (save-excursion
5878     (set-buffer gnus-article-buffer)
5879     (let* ((buffer-read-only nil)
5880            (inhibit-point-motion-hooks t)
5881            (hidden (text-property-any
5882                     (goto-char (point-min)) (search-forward "\n\n")
5883                     'invisible t))
5884            e)
5885       (goto-char (point-min))
5886       (when (search-forward "\n\n" nil t)
5887         (delete-region (point-min) (1- (point))))
5888       (goto-char (point-min))
5889       (save-excursion
5890         (set-buffer gnus-original-article-buffer)
5891         (goto-char (point-min))
5892         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
5893       (insert-buffer-substring gnus-original-article-buffer 1 e)
5894       (let ((article-inhibit-hiding t))
5895         (run-hooks 'gnus-article-display-hook))
5896       (if (or (not hidden) (and (numberp arg) (< arg 0)))
5897           (gnus-article-hide-headers)))))
5898
5899 (defun gnus-summary-show-all-headers ()
5900   "Make all header lines visible."
5901   (interactive)
5902   (gnus-set-global-variables)
5903   (gnus-article-show-all-headers))
5904
5905 (defun gnus-summary-toggle-mime (&optional arg)
5906   "Toggle MIME processing.
5907 If ARG is a positive number, turn MIME processing on."
5908   (interactive "P")
5909   (gnus-set-global-variables)
5910   (setq gnus-show-mime
5911         (if (null arg) (not gnus-show-mime)
5912           (> (prefix-numeric-value arg) 0)))
5913   (gnus-summary-select-article t 'force))
5914
5915 (defun gnus-summary-caesar-message (&optional arg)
5916   "Caesar rotate the current article by 13.
5917 The numerical prefix specifies how manu places to rotate each letter
5918 forward."
5919   (interactive "P")
5920   (gnus-set-global-variables)
5921   (gnus-summary-select-article)
5922   (let ((mail-header-separator ""))
5923     (gnus-eval-in-buffer-window gnus-article-buffer
5924       (save-restriction
5925         (widen)
5926         (let ((start (window-start))
5927               buffer-read-only)
5928           (message-caesar-buffer-body arg)
5929           (set-window-start (get-buffer-window (current-buffer)) start))))))
5930
5931 (defun gnus-summary-stop-page-breaking ()
5932   "Stop page breaking in the current article."
5933   (interactive)
5934   (gnus-set-global-variables)
5935   (gnus-summary-select-article)
5936   (gnus-eval-in-buffer-window gnus-article-buffer
5937     (widen)))
5938
5939 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
5940   "Move the current article to a different newsgroup.
5941 If N is a positive number, move the N next articles.
5942 If N is a negative number, move the N previous articles.
5943 If N is nil and any articles have been marked with the process mark,
5944 move those articles instead.
5945 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5946 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5947 re-spool using this method.
5948
5949 For this function to work, both the current newsgroup and the
5950 newsgroup that you want to move to have to support the `request-move'
5951 and `request-accept' functions."
5952   (interactive "P")
5953   (unless action (setq action 'move))
5954   (gnus-set-global-variables)
5955   ;; Check whether the source group supports the required functions.
5956   (cond ((and (eq action 'move)
5957               (not (gnus-check-backend-function
5958                     'request-move-article gnus-newsgroup-name)))
5959          (error "The current group does not support article moving"))
5960         ((and (eq action 'crosspost)
5961               (not (gnus-check-backend-function
5962                     'request-replace-article gnus-newsgroup-name)))
5963          (error "The current group does not support article editing")))
5964   (let ((articles (gnus-summary-work-articles n))
5965         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
5966         (names '((move "Move" "Moving")
5967                  (copy "Copy" "Copying")
5968                  (crosspost "Crosspost" "Crossposting")))
5969         (copy-buf (save-excursion
5970                     (nnheader-set-temp-buffer " *copy article*")))
5971         art-group to-method new-xref article to-groups)
5972     (unless (assq action names)
5973       (error "Unknown action %s" action))
5974     ;; Read the newsgroup name.
5975     (when (and (not to-newsgroup)
5976                (not select-method))
5977       (setq to-newsgroup
5978             (gnus-read-move-group-name
5979              (cadr (assq action names))
5980              (symbol-value (intern (format "gnus-current-%s-group" action)))
5981              articles prefix))
5982       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
5983     (setq to-method (or select-method 
5984                         (gnus-group-name-to-method to-newsgroup)))
5985     ;; Check the method we are to move this article to...
5986     (or (gnus-check-backend-function 'request-accept-article (car to-method))
5987         (error "%s does not support article copying" (car to-method)))
5988     (or (gnus-check-server to-method)
5989         (error "Can't open server %s" (car to-method)))
5990     (gnus-message 6 "%s to %s: %s..."
5991                   (caddr (assq action names))
5992                   (or (car select-method) to-newsgroup) articles)
5993     (while articles
5994       (setq article (pop articles))
5995       (setq
5996        art-group
5997        (cond
5998         ;; Move the article.
5999         ((eq action 'move)
6000          (gnus-request-move-article
6001           article                       ; Article to move
6002           gnus-newsgroup-name           ; From newsgrouo
6003           (nth 1 (gnus-find-method-for-group
6004                   gnus-newsgroup-name)) ; Server
6005           (list 'gnus-request-accept-article
6006                 to-newsgroup (list 'quote select-method)
6007                 (not articles))         ; Accept form
6008           (not articles)))              ; Only save nov last time
6009         ;; Copy the article.
6010         ((eq action 'copy)
6011          (save-excursion
6012            (set-buffer copy-buf)
6013            (gnus-request-article-this-buffer article gnus-newsgroup-name)
6014            (gnus-request-accept-article
6015             to-newsgroup select-method (not articles))))
6016         ;; Crosspost the article.
6017         ((eq action 'crosspost)
6018          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
6019            (setq new-xref (concat gnus-newsgroup-name ":" article))
6020            (if (and xref (not (string= xref "")))
6021                (progn
6022                  (when (string-match "^Xref: " xref)
6023                    (setq xref (substring xref (match-end 0))))
6024                  (setq new-xref (concat xref " " new-xref)))
6025              (setq new-xref (concat (system-name) " " new-xref)))
6026            (save-excursion
6027              (set-buffer copy-buf)
6028              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6029              (nnheader-replace-header "xref" new-xref)
6030              (gnus-request-accept-article
6031               to-newsgroup select-method (not articles)))))))
6032       (if (not art-group)
6033           (gnus-message 1 "Couldn't %s article %s"
6034                         (cadr (assq action names)) article)
6035         (let* ((entry
6036                 (or
6037                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6038                  (gnus-gethash
6039                   (gnus-group-prefixed-name
6040                    (car art-group)
6041                    (or select-method 
6042                        (gnus-find-method-for-group to-newsgroup)))
6043                   gnus-newsrc-hashtb)))
6044                (info (nth 2 entry))
6045                (to-group (gnus-info-group info)))
6046           ;; Update the group that has been moved to.
6047           (when (and info
6048                      (memq action '(move copy)))
6049             (unless (member to-group to-groups)
6050               (push to-group to-groups))
6051
6052             (unless (memq article gnus-newsgroup-unreads)
6053               (gnus-info-set-read
6054                info (gnus-add-to-range (gnus-info-read info)
6055                                        (list (cdr art-group)))))
6056
6057             ;; Copy any marks over to the new group.
6058             (let ((marks gnus-article-mark-lists)
6059                   (to-article (cdr art-group)))
6060
6061               ;; See whether the article is to be put in the cache.
6062               (when gnus-use-cache
6063                 (gnus-cache-possibly-enter-article
6064                  to-group to-article
6065                  (let ((header (copy-sequence
6066                                 (gnus-summary-article-header article))))
6067                    (mail-header-set-number header to-article)
6068                    header)
6069                  (memq article gnus-newsgroup-marked)
6070                  (memq article gnus-newsgroup-dormant)
6071                  (memq article gnus-newsgroup-unreads)))
6072
6073               (while marks
6074                 (when (memq article (symbol-value
6075                                      (intern (format "gnus-newsgroup-%s"
6076                                                      (caar marks)))))
6077                   ;; If the other group is the same as this group,
6078                   ;; then we have to add the mark to the list.
6079                   (when (equal to-group gnus-newsgroup-name)
6080                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6081                          (cons to-article
6082                                (symbol-value
6083                                 (intern (format "gnus-newsgroup-%s"
6084                                                 (caar marks)))))))
6085                   ;; Copy mark to other group.
6086                   (gnus-add-marked-articles
6087                    to-group (cdar marks) (list to-article) info))
6088                 (setq marks (cdr marks)))))
6089
6090           ;; Update the Xref header in this article to point to
6091           ;; the new crossposted article we have just created.
6092           (when (eq action 'crosspost)
6093             (save-excursion
6094               (set-buffer copy-buf)
6095               (gnus-request-article-this-buffer article gnus-newsgroup-name)
6096               (nnheader-replace-header
6097                "xref" (concat new-xref " " (gnus-group-prefixed-name
6098                                             (car art-group) to-method)
6099                               ":" (cdr art-group)))
6100               (gnus-request-replace-article
6101                article gnus-newsgroup-name (current-buffer)))))
6102
6103         (gnus-summary-goto-subject article)
6104         (when (eq action 'move)
6105           (gnus-summary-mark-article article gnus-canceled-mark)))
6106       (gnus-summary-remove-process-mark article))
6107     ;; Re-activate all groups that have been moved to.
6108     (while to-groups
6109       (gnus-activate-group (pop to-groups)))
6110     
6111     (gnus-kill-buffer copy-buf)
6112     (gnus-summary-position-point)
6113     (gnus-set-mode-line 'summary)))
6114
6115 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
6116   "Move the current article to a different newsgroup.
6117 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6118 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6119 re-spool using this method."
6120   (interactive "P")
6121   (gnus-summary-move-article n nil select-method 'copy))
6122
6123 (defun gnus-summary-crosspost-article (&optional n)
6124   "Crosspost the current article to some other group."
6125   (interactive "P")
6126   (gnus-summary-move-article n nil nil 'crosspost))
6127
6128 (defvar gnus-summary-respool-default-method nil
6129   "Default method for respooling an article.  
6130 If nil, use to the current newsgroup method.")
6131
6132 (defun gnus-summary-respool-article (&optional n method)
6133   "Respool the current article.
6134 The article will be squeezed through the mail spooling process again,
6135 which means that it will be put in some mail newsgroup or other
6136 depending on `nnmail-split-methods'.
6137 If N is a positive number, respool the N next articles.
6138 If N is a negative number, respool the N previous articles.
6139 If N is nil and any articles have been marked with the process mark,
6140 respool those articles instead.
6141
6142 Respooling can be done both from mail groups and \"real\" newsgroups.
6143 In the former case, the articles in question will be moved from the
6144 current group into whatever groups they are destined to.  In the
6145 latter case, they will be copied into the relevant groups."
6146   (interactive 
6147    (list current-prefix-arg
6148          (let* ((methods (gnus-methods-using 'respool))
6149                 (methname
6150                  (symbol-name (or gnus-summary-respool-default-method
6151                                   (car (gnus-find-method-for-group
6152                                         gnus-newsgroup-name)))))
6153                 (method
6154                  (gnus-completing-read 
6155                   methname "What backend do you want to use when respooling?"
6156                   methods nil t nil 'gnus-method-history))
6157                 ms)
6158            (cond
6159             ((zerop (length (setq ms (gnus-servers-using-backend method))))
6160              (list (intern method) ""))
6161             ((= 1 (length ms))
6162              (car ms))
6163             (t
6164              (cdr (completing-read 
6165                    "Server name: "
6166                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
6167   (gnus-set-global-variables)
6168   (unless method
6169     (error "No method given for respooling"))
6170   (if (assoc (symbol-name
6171               (car (gnus-find-method-for-group gnus-newsgroup-name)))
6172              (gnus-methods-using 'respool))
6173       (gnus-summary-move-article n nil method)
6174     (gnus-summary-copy-article n nil method)))
6175
6176 (defun gnus-summary-import-article (file)
6177   "Import a random file into a mail newsgroup."
6178   (interactive "fImport file: ")
6179   (gnus-set-global-variables)
6180   (let ((group gnus-newsgroup-name)
6181         (now (current-time))
6182         atts lines)
6183     (or (gnus-check-backend-function 'request-accept-article group)
6184         (error "%s does not support article importing" group))
6185     (or (file-readable-p file)
6186         (not (file-regular-p file))
6187         (error "Can't read %s" file))
6188     (save-excursion
6189       (set-buffer (get-buffer-create " *import file*"))
6190       (buffer-disable-undo (current-buffer))
6191       (erase-buffer)
6192       (insert-file-contents file)
6193       (goto-char (point-min))
6194       (unless (nnheader-article-p)
6195         ;; This doesn't look like an article, so we fudge some headers.
6196         (setq atts (file-attributes file)
6197               lines (count-lines (point-min) (point-max)))
6198         (insert "From: " (read-string "From: ") "\n"
6199                 "Subject: " (read-string "Subject: ") "\n"
6200                 "Date: " (timezone-make-date-arpa-standard
6201                           (current-time-string (nth 5 atts))
6202                           (current-time-zone now)
6203                           (current-time-zone now)) "\n"
6204                 "Message-ID: " (message-make-message-id) "\n"
6205                 "Lines: " (int-to-string lines) "\n"
6206                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
6207       (gnus-request-accept-article group nil t)
6208       (kill-buffer (current-buffer)))))
6209
6210 (defun gnus-summary-article-posted-p ()
6211   "Say whether the current (mail) article is available from `gnus-select-method' as well.
6212 This will be the case if the article has both been mailed and posted."
6213   (interactive)
6214   (let ((id (mail-header-references (gnus-summary-article-header)))
6215         (gnus-override-method
6216          (or gnus-refer-article-method gnus-select-method)))
6217     (if (gnus-request-head id "")
6218         (gnus-message 2 "The current message was found on %s"
6219                       gnus-override-method)
6220       (gnus-message 2 "The current message couldn't be found on %s"
6221                     gnus-override-method)
6222       nil)))
6223
6224 (defun gnus-summary-expire-articles (&optional now)
6225   "Expire all articles that are marked as expirable in the current group."
6226   (interactive)
6227   (gnus-set-global-variables)
6228   (when (gnus-check-backend-function
6229          'request-expire-articles gnus-newsgroup-name)
6230     ;; This backend supports expiry.
6231     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
6232            (expirable (if total
6233                           (gnus-list-of-read-articles gnus-newsgroup-name)
6234                         (setq gnus-newsgroup-expirable
6235                               (sort gnus-newsgroup-expirable '<))))
6236            (expiry-wait (if now 'immediate
6237                           (gnus-group-find-parameter
6238                            gnus-newsgroup-name 'expiry-wait)))
6239            es)
6240       (when expirable
6241         ;; There are expirable articles in this group, so we run them
6242         ;; through the expiry process.
6243         (gnus-message 6 "Expiring articles...")
6244         ;; The list of articles that weren't expired is returned.
6245         (if expiry-wait
6246             (let ((nnmail-expiry-wait-function nil)
6247                   (nnmail-expiry-wait expiry-wait))
6248               (setq es (gnus-request-expire-articles
6249                         expirable gnus-newsgroup-name)))
6250           (setq es (gnus-request-expire-articles
6251                     expirable gnus-newsgroup-name)))
6252         (unless total
6253           (setq gnus-newsgroup-expirable es))
6254         ;; We go through the old list of expirable, and mark all
6255         ;; really expired articles as nonexistent.
6256         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
6257           (let ((gnus-use-cache nil))
6258             (while expirable
6259               (unless (memq (car expirable) es)
6260                 (when (gnus-data-find (car expirable))
6261                   (gnus-summary-mark-article
6262                    (car expirable) gnus-canceled-mark)))
6263               (setq expirable (cdr expirable)))))
6264         (gnus-message 6 "Expiring articles...done")))))
6265
6266 (defun gnus-summary-expire-articles-now ()
6267   "Expunge all expirable articles in the current group.
6268 This means that *all* articles that are marked as expirable will be
6269 deleted forever, right now."
6270   (interactive)
6271   (gnus-set-global-variables)
6272   (or gnus-expert-user
6273       (gnus-y-or-n-p
6274        "Are you really, really, really sure you want to delete all these messages? ")
6275       (error "Phew!"))
6276   (gnus-summary-expire-articles t))
6277
6278 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6279 (defun gnus-summary-delete-article (&optional n)
6280   "Delete the N next (mail) articles.
6281 This command actually deletes articles.  This is not a marking
6282 command.  The article will disappear forever from your life, never to
6283 return.
6284 If N is negative, delete backwards.
6285 If N is nil and articles have been marked with the process mark,
6286 delete these instead."
6287   (interactive "P")
6288   (gnus-set-global-variables)
6289   (or (gnus-check-backend-function 'request-expire-articles
6290                                    gnus-newsgroup-name)
6291       (error "The current newsgroup does not support article deletion."))
6292   ;; Compute the list of articles to delete.
6293   (let ((articles (gnus-summary-work-articles n))
6294         not-deleted)
6295     (if (and gnus-novice-user
6296              (not (gnus-y-or-n-p
6297                    (format "Do you really want to delete %s forever? "
6298                            (if (> (length articles) 1) 
6299                                (format "these %s articles" (length articles))
6300                              "this article")))))
6301         ()
6302       ;; Delete the articles.
6303       (setq not-deleted (gnus-request-expire-articles
6304                          articles gnus-newsgroup-name 'force))
6305       (while articles
6306         (gnus-summary-remove-process-mark (car articles))
6307         ;; The backend might not have been able to delete the article
6308         ;; after all.
6309         (or (memq (car articles) not-deleted)
6310             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
6311         (setq articles (cdr articles))))
6312     (gnus-summary-position-point)
6313     (gnus-set-mode-line 'summary)
6314     not-deleted))
6315
6316 (defun gnus-summary-edit-article (&optional force)
6317   "Edit the current article.
6318 This will have permanent effect only in mail groups.
6319 If FORCE is non-nil, allow editing of articles even in read-only
6320 groups." 
6321   (interactive "P")
6322   (save-excursion
6323     (set-buffer gnus-summary-buffer)
6324     (gnus-set-global-variables)
6325     (when (and (not force)
6326                (gnus-group-read-only-p))
6327       (error "The current newsgroup does not support article editing."))
6328     ;; Select article if needed.
6329     (unless (eq (gnus-summary-article-number)
6330                 gnus-current-article)
6331       (gnus-summary-select-article t))
6332     (gnus-article-edit-article
6333      `(lambda ()
6334         (gnus-summary-edit-article-done
6335          ,(or (mail-header-references gnus-current-headers) "")
6336          ,(gnus-group-read-only-p) ,gnus-summary-buffer)))))
6337
6338 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
6339
6340 (defun gnus-summary-edit-article-done (references read-only buffer)
6341   "Make edits to the current article permanent."
6342   (interactive)
6343   ;; Replace the article.
6344   (if (and (not read-only)
6345            (not (gnus-request-replace-article
6346                  (cdr gnus-article-current) (car gnus-article-current)
6347                  (current-buffer))))
6348       (error "Couldn't replace article.")
6349     ;; Update the summary buffer.
6350     (if (equal (message-tokenize-header references " ")
6351                (message-tokenize-header
6352                 (or (message-fetch-field "references") "") " "))
6353         ;; We only have to update this line.
6354         (save-excursion
6355           (save-restriction
6356             (message-narrow-to-head)
6357             (let ((header (nnheader-parse-head t)))
6358               (set-buffer buffer)
6359               (mail-header-set-number header (cdr gnus-article-current))
6360               (gnus-summary-update-article-line
6361                (cdr gnus-article-current) header))))
6362       ;; Update threads.
6363       (set-buffer buffer)
6364       (gnus-summary-update-article (cdr gnus-article-current)))
6365     ;; Prettify the article buffer again.
6366     (save-excursion
6367       (set-buffer gnus-article-buffer)
6368       (run-hooks 'gnus-article-display-hook))
6369     ;; Prettify the summary buffer line.
6370     (when (gnus-visual-p 'summary-highlight 'highlight)
6371       (run-hooks 'gnus-visual-mark-article-hook))))
6372
6373 (defun gnus-summary-edit-wash (key)
6374   "Perform editing command in the article buffer."
6375   (interactive 
6376    (list
6377     (progn
6378       (message "%s" (concat (this-command-keys) "- "))
6379       (read-char))))
6380   (message "")
6381   (gnus-summary-edit-article)
6382   (execute-kbd-macro (concat (this-command-keys) key))
6383   (gnus-article-edit-done))
6384
6385 ;;; Respooling
6386
6387 (defun gnus-summary-respool-query ()
6388   "Query where the respool algorithm would put this article."
6389   (interactive)
6390   (gnus-set-global-variables)
6391   (gnus-summary-select-article)
6392   (save-excursion
6393     (set-buffer gnus-article-buffer)
6394     (save-restriction
6395       (goto-char (point-min))
6396       (search-forward "\n\n")
6397       (narrow-to-region (point-min) (point))
6398       (pp-eval-expression
6399        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
6400
6401 ;; Summary marking commands.
6402
6403 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
6404   "Mark articles which has the same subject as read, and then select the next.
6405 If UNMARK is positive, remove any kind of mark.
6406 If UNMARK is negative, tick articles."
6407   (interactive "P")
6408   (gnus-set-global-variables)
6409   (if unmark
6410       (setq unmark (prefix-numeric-value unmark)))
6411   (let ((count
6412          (gnus-summary-mark-same-subject
6413           (gnus-summary-article-subject) unmark)))
6414     ;; Select next unread article.  If auto-select-same mode, should
6415     ;; select the first unread article.
6416     (gnus-summary-next-article t (and gnus-auto-select-same
6417                                       (gnus-summary-article-subject)))
6418     (gnus-message 7 "%d article%s marked as %s"
6419                   count (if (= count 1) " is" "s are")
6420                   (if unmark "unread" "read"))))
6421
6422 (defun gnus-summary-kill-same-subject (&optional unmark)
6423   "Mark articles which has the same subject as read.
6424 If UNMARK is positive, remove any kind of mark.
6425 If UNMARK is negative, tick articles."
6426   (interactive "P")
6427   (gnus-set-global-variables)
6428   (if unmark
6429       (setq unmark (prefix-numeric-value unmark)))
6430   (let ((count
6431          (gnus-summary-mark-same-subject
6432           (gnus-summary-article-subject) unmark)))
6433     ;; If marked as read, go to next unread subject.
6434     (if (null unmark)
6435         ;; Go to next unread subject.
6436         (gnus-summary-next-subject 1 t))
6437     (gnus-message 7 "%d articles are marked as %s"
6438                   count (if unmark "unread" "read"))))
6439
6440 (defun gnus-summary-mark-same-subject (subject &optional unmark)
6441   "Mark articles with same SUBJECT as read, and return marked number.
6442 If optional argument UNMARK is positive, remove any kinds of marks.
6443 If optional argument UNMARK is negative, mark articles as unread instead."
6444   (let ((count 1))
6445     (save-excursion
6446       (cond
6447        ((null unmark)                   ; Mark as read.
6448         (while (and
6449                 (progn
6450                   (gnus-summary-mark-article-as-read gnus-killed-mark)
6451                   (gnus-summary-show-thread) t)
6452                 (gnus-summary-find-subject subject))
6453           (setq count (1+ count))))
6454        ((> unmark 0)                    ; Tick.
6455         (while (and
6456                 (progn
6457                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
6458                   (gnus-summary-show-thread) t)
6459                 (gnus-summary-find-subject subject))
6460           (setq count (1+ count))))
6461        (t                               ; Mark as unread.
6462         (while (and
6463                 (progn
6464                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
6465                   (gnus-summary-show-thread) t)
6466                 (gnus-summary-find-subject subject))
6467           (setq count (1+ count)))))
6468       (gnus-set-mode-line 'summary)
6469       ;; Return the number of marked articles.
6470       count)))
6471
6472 (defun gnus-summary-mark-as-processable (n &optional unmark)
6473   "Set the process mark on the next N articles.
6474 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
6475 the process mark instead.  The difference between N and the actual
6476 number of articles marked is returned."
6477   (interactive "p")
6478   (gnus-set-global-variables)
6479   (let ((backward (< n 0))
6480         (n (abs n)))
6481     (while (and
6482             (> n 0)
6483             (if unmark
6484                 (gnus-summary-remove-process-mark
6485                  (gnus-summary-article-number))
6486               (gnus-summary-set-process-mark (gnus-summary-article-number)))
6487             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
6488       (setq n (1- n)))
6489     (if (/= 0 n) (gnus-message 7 "No more articles"))
6490     (gnus-summary-recenter)
6491     (gnus-summary-position-point)
6492     n))
6493
6494 (defun gnus-summary-unmark-as-processable (n)
6495   "Remove the process mark from the next N articles.
6496 If N is negative, mark backward instead.  The difference between N and
6497 the actual number of articles marked is returned."
6498   (interactive "p")
6499   (gnus-set-global-variables)
6500   (gnus-summary-mark-as-processable n t))
6501
6502 (defun gnus-summary-unmark-all-processable ()
6503   "Remove the process mark from all articles."
6504   (interactive)
6505   (gnus-set-global-variables)
6506   (save-excursion
6507     (while gnus-newsgroup-processable
6508       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
6509   (gnus-summary-position-point))
6510
6511 (defun gnus-summary-mark-as-expirable (n)
6512   "Mark N articles forward as expirable.
6513 If N is negative, mark backward instead.  The difference between N and
6514 the actual number of articles marked is returned."
6515   (interactive "p")
6516   (gnus-set-global-variables)
6517   (gnus-summary-mark-forward n gnus-expirable-mark))
6518
6519 (defun gnus-summary-mark-article-as-replied (article)
6520   "Mark ARTICLE replied and update the summary line."
6521   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
6522   (let ((buffer-read-only nil))
6523     (when (gnus-summary-goto-subject article)
6524       (gnus-summary-update-secondary-mark article))))
6525
6526 (defun gnus-summary-set-bookmark (article)
6527   "Set a bookmark in current article."
6528   (interactive (list (gnus-summary-article-number)))
6529   (gnus-set-global-variables)
6530   (if (or (not (get-buffer gnus-article-buffer))
6531           (not gnus-current-article)
6532           (not gnus-article-current)
6533           (not (equal gnus-newsgroup-name (car gnus-article-current))))
6534       (error "No current article selected"))
6535   ;; Remove old bookmark, if one exists.
6536   (let ((old (assq article gnus-newsgroup-bookmarks)))
6537     (if old (setq gnus-newsgroup-bookmarks
6538                   (delq old gnus-newsgroup-bookmarks))))
6539   ;; Set the new bookmark, which is on the form
6540   ;; (article-number . line-number-in-body).
6541   (setq gnus-newsgroup-bookmarks
6542         (cons
6543          (cons article
6544                (save-excursion
6545                  (set-buffer gnus-article-buffer)
6546                  (count-lines
6547                   (min (point)
6548                        (save-excursion
6549                          (goto-char (point-min))
6550                          (search-forward "\n\n" nil t)
6551                          (point)))
6552                   (point))))
6553          gnus-newsgroup-bookmarks))
6554   (gnus-message 6 "A bookmark has been added to the current article."))
6555
6556 (defun gnus-summary-remove-bookmark (article)
6557   "Remove the bookmark from the current article."
6558   (interactive (list (gnus-summary-article-number)))
6559   (gnus-set-global-variables)
6560   ;; Remove old bookmark, if one exists.
6561   (let ((old (assq article gnus-newsgroup-bookmarks)))
6562     (if old
6563         (progn
6564           (setq gnus-newsgroup-bookmarks
6565                 (delq old gnus-newsgroup-bookmarks))
6566           (gnus-message 6 "Removed bookmark."))
6567       (gnus-message 6 "No bookmark in current article."))))
6568
6569 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6570 (defun gnus-summary-mark-as-dormant (n)
6571   "Mark N articles forward as dormant.
6572 If N is negative, mark backward instead.  The difference between N and
6573 the actual number of articles marked is returned."
6574   (interactive "p")
6575   (gnus-set-global-variables)
6576   (gnus-summary-mark-forward n gnus-dormant-mark))
6577
6578 (defun gnus-summary-set-process-mark (article)
6579   "Set the process mark on ARTICLE and update the summary line."
6580   (setq gnus-newsgroup-processable
6581         (cons article
6582               (delq article gnus-newsgroup-processable)))
6583   (when (gnus-summary-goto-subject article)
6584     (gnus-summary-show-thread)
6585     (gnus-summary-update-secondary-mark article)))
6586
6587 (defun gnus-summary-remove-process-mark (article)
6588   "Remove the process mark from ARTICLE and update the summary line."
6589   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
6590   (when (gnus-summary-goto-subject article)
6591     (gnus-summary-show-thread)
6592     (gnus-summary-update-secondary-mark article)))
6593
6594 (defun gnus-summary-set-saved-mark (article)
6595   "Set the process mark on ARTICLE and update the summary line."
6596   (push article gnus-newsgroup-saved)
6597   (when (gnus-summary-goto-subject article)
6598     (gnus-summary-update-secondary-mark article)))
6599
6600 (defun gnus-summary-mark-forward (n &optional mark no-expire)
6601   "Mark N articles as read forwards.
6602 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
6603 The difference between N and the actual number of articles marked is
6604 returned."
6605   (interactive "p")
6606   (gnus-set-global-variables)
6607   (let ((backward (< n 0))
6608         (gnus-summary-goto-unread
6609          (and gnus-summary-goto-unread
6610               (not (eq gnus-summary-goto-unread 'never))
6611               (not (memq mark (list gnus-unread-mark
6612                                     gnus-ticked-mark gnus-dormant-mark)))))
6613         (n (abs n))
6614         (mark (or mark gnus-del-mark)))
6615     (while (and (> n 0)
6616                 (gnus-summary-mark-article nil mark no-expire)
6617                 (zerop (gnus-summary-next-subject
6618                         (if backward -1 1)
6619                         (and gnus-summary-goto-unread
6620                              (not (eq gnus-summary-goto-unread 'never)))
6621                         t)))
6622       (setq n (1- n)))
6623     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
6624     (gnus-summary-recenter)
6625     (gnus-summary-position-point)
6626     (gnus-set-mode-line 'summary)
6627     n))
6628
6629 (defun gnus-summary-mark-article-as-read (mark)
6630   "Mark the current article quickly as read with MARK."
6631   (let ((article (gnus-summary-article-number)))
6632     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6633     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6634     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6635     (setq gnus-newsgroup-reads
6636           (cons (cons article mark) gnus-newsgroup-reads))
6637     ;; Possibly remove from cache, if that is used.
6638     (and gnus-use-cache (gnus-cache-enter-remove-article article))
6639     ;; Allow the backend to change the mark.
6640     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
6641     ;; Check for auto-expiry.
6642     (when (and gnus-newsgroup-auto-expire
6643                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6644                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6645                    (= mark gnus-ancient-mark)
6646                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
6647                    (= mark gnus-duplicate-mark)))
6648       (setq mark gnus-expirable-mark)
6649       (push article gnus-newsgroup-expirable))
6650     ;; Set the mark in the buffer.
6651     (gnus-summary-update-mark mark 'unread)
6652     t))
6653
6654 (defun gnus-summary-mark-article-as-unread (mark)
6655   "Mark the current article quickly as unread with MARK."
6656   (let ((article (gnus-summary-article-number)))
6657     (if (< article 0)
6658         (gnus-error 1 "Unmarkable article")
6659       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6660       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6661       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6662       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
6663       (cond ((= mark gnus-ticked-mark)
6664              (push article gnus-newsgroup-marked))
6665             ((= mark gnus-dormant-mark)
6666              (push article gnus-newsgroup-dormant))
6667             (t
6668              (push article gnus-newsgroup-unreads)))
6669       (setq gnus-newsgroup-reads
6670             (delq (assq article gnus-newsgroup-reads)
6671                   gnus-newsgroup-reads))
6672
6673       ;; See whether the article is to be put in the cache.
6674       (and gnus-use-cache
6675            (vectorp (gnus-summary-article-header article))
6676            (save-excursion
6677              (gnus-cache-possibly-enter-article
6678               gnus-newsgroup-name article
6679               (gnus-summary-article-header article)
6680               (= mark gnus-ticked-mark)
6681               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6682
6683       ;; Fix the mark.
6684       (gnus-summary-update-mark mark 'unread))
6685     t))
6686
6687 (defun gnus-summary-mark-article (&optional article mark no-expire)
6688   "Mark ARTICLE with MARK.  MARK can be any character.
6689 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
6690 `??' (dormant) and `?E' (expirable).
6691 If MARK is nil, then the default character `?D' is used.
6692 If ARTICLE is nil, then the article on the current line will be
6693 marked."
6694   ;; The mark might be a string.
6695   (and (stringp mark)
6696        (setq mark (aref mark 0)))
6697   ;; If no mark is given, then we check auto-expiring.
6698   (and (not no-expire)
6699        gnus-newsgroup-auto-expire
6700        (or (not mark)
6701            (and (numberp mark)
6702                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6703                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6704                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
6705                     (= mark gnus-duplicate-mark))))
6706        (setq mark gnus-expirable-mark))
6707   (let* ((mark (or mark gnus-del-mark))
6708          (article (or article (gnus-summary-article-number))))
6709     (or article (error "No article on current line"))
6710     (if (or (= mark gnus-unread-mark)
6711             (= mark gnus-ticked-mark)
6712             (= mark gnus-dormant-mark))
6713         (gnus-mark-article-as-unread article mark)
6714       (gnus-mark-article-as-read article mark))
6715
6716     ;; See whether the article is to be put in the cache.
6717     (and gnus-use-cache
6718          (not (= mark gnus-canceled-mark))
6719          (vectorp (gnus-summary-article-header article))
6720          (save-excursion
6721            (gnus-cache-possibly-enter-article
6722             gnus-newsgroup-name article
6723             (gnus-summary-article-header article)
6724             (= mark gnus-ticked-mark)
6725             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6726
6727     (if (gnus-summary-goto-subject article nil t)
6728         (let ((buffer-read-only nil))
6729           (gnus-summary-show-thread)
6730           ;; Fix the mark.
6731           (gnus-summary-update-mark mark 'unread)
6732           t))))
6733
6734 (defun gnus-summary-update-secondary-mark (article)
6735   "Update the secondary (read, process, cache) mark."
6736   (gnus-summary-update-mark
6737    (cond ((memq article gnus-newsgroup-processable)
6738           gnus-process-mark)
6739          ((memq article gnus-newsgroup-cached)
6740           gnus-cached-mark)
6741          ((memq article gnus-newsgroup-replied)
6742           gnus-replied-mark)
6743          ((memq article gnus-newsgroup-saved)
6744           gnus-saved-mark)
6745          (t gnus-unread-mark))
6746    'replied)
6747   (when (gnus-visual-p 'summary-highlight 'highlight)
6748     (run-hooks 'gnus-summary-update-hook))
6749   t)
6750
6751 (defun gnus-summary-update-mark (mark type)
6752   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
6753         (buffer-read-only nil))
6754     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
6755     (and (looking-at "\r") (setq forward (1+ forward)))
6756     (when (and forward
6757                (<= (+ forward (point)) (point-max)))
6758       ;; Go to the right position on the line.
6759       (goto-char (+ forward (point)))
6760       ;; Replace the old mark with the new mark.
6761       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
6762       ;; Optionally update the marks by some user rule.
6763       (when (eq type 'unread)
6764         (gnus-data-set-mark
6765          (gnus-data-find (gnus-summary-article-number)) mark)
6766         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
6767
6768 (defun gnus-mark-article-as-read (article &optional mark)
6769   "Enter ARTICLE in the pertinent lists and remove it from others."
6770   ;; Make the article expirable.
6771   (let ((mark (or mark gnus-del-mark)))
6772     (if (= mark gnus-expirable-mark)
6773         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
6774       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
6775     ;; Remove from unread and marked lists.
6776     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6777     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6778     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6779     (push (cons article mark) gnus-newsgroup-reads)
6780     ;; Possibly remove from cache, if that is used.
6781     (when gnus-use-cache
6782       (gnus-cache-enter-remove-article article))))
6783
6784 (defun gnus-mark-article-as-unread (article &optional mark)
6785   "Enter ARTICLE in the pertinent lists and remove it from others."
6786   (let ((mark (or mark gnus-ticked-mark)))
6787     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
6788           gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
6789           gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
6790           gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6791
6792     ;; Unsuppress duplicates?
6793     (when gnus-suppress-duplicates
6794       (gnus-dup-unsuppress-article article))
6795
6796     (cond ((= mark gnus-ticked-mark)
6797            (push article gnus-newsgroup-marked))
6798           ((= mark gnus-dormant-mark)
6799            (push article gnus-newsgroup-dormant))
6800           (t
6801            (push article gnus-newsgroup-unreads)))
6802     (setq gnus-newsgroup-reads
6803           (delq (assq article gnus-newsgroup-reads)
6804                 gnus-newsgroup-reads))))
6805
6806 (defalias 'gnus-summary-mark-as-unread-forward
6807   'gnus-summary-tick-article-forward)
6808 (make-obsolete 'gnus-summary-mark-as-unread-forward
6809                'gnus-summary-tick-article-forward)
6810 (defun gnus-summary-tick-article-forward (n)
6811   "Tick N articles forwards.
6812 If N is negative, tick backwards instead.
6813 The difference between N and the number of articles ticked is returned."
6814   (interactive "p")
6815   (gnus-summary-mark-forward n gnus-ticked-mark))
6816
6817 (defalias 'gnus-summary-mark-as-unread-backward
6818   'gnus-summary-tick-article-backward)
6819 (make-obsolete 'gnus-summary-mark-as-unread-backward
6820                'gnus-summary-tick-article-backward)
6821 (defun gnus-summary-tick-article-backward (n)
6822   "Tick N articles backwards.
6823 The difference between N and the number of articles ticked is returned."
6824   (interactive "p")
6825   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
6826
6827 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6828 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6829 (defun gnus-summary-tick-article (&optional article clear-mark)
6830   "Mark current article as unread.
6831 Optional 1st argument ARTICLE specifies article number to be marked as unread.
6832 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
6833   (interactive)
6834   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
6835                                        gnus-ticked-mark)))
6836
6837 (defun gnus-summary-mark-as-read-forward (n)
6838   "Mark N articles as read forwards.
6839 If N is negative, mark backwards instead.
6840 The difference between N and the actual number of articles marked is
6841 returned."
6842   (interactive "p")
6843   (gnus-summary-mark-forward n gnus-del-mark t))
6844
6845 (defun gnus-summary-mark-as-read-backward (n)
6846   "Mark the N articles as read backwards.
6847 The difference between N and the actual number of articles marked is
6848 returned."
6849   (interactive "p")
6850   (gnus-summary-mark-forward (- n) gnus-del-mark t))
6851
6852 (defun gnus-summary-mark-as-read (&optional article mark)
6853   "Mark current article as read.
6854 ARTICLE specifies the article to be marked as read.
6855 MARK specifies a string to be inserted at the beginning of the line."
6856   (gnus-summary-mark-article article mark))
6857
6858 (defun gnus-summary-clear-mark-forward (n)
6859   "Clear marks from N articles forward.
6860 If N is negative, clear backward instead.
6861 The difference between N and the number of marks cleared is returned."
6862   (interactive "p")
6863   (gnus-summary-mark-forward n gnus-unread-mark))
6864
6865 (defun gnus-summary-clear-mark-backward (n)
6866   "Clear marks from N articles backward.
6867 The difference between N and the number of marks cleared is returned."
6868   (interactive "p")
6869   (gnus-summary-mark-forward (- n) gnus-unread-mark))
6870
6871 (defun gnus-summary-mark-unread-as-read ()
6872   "Intended to be used by `gnus-summary-mark-article-hook'."
6873   (when (memq gnus-current-article gnus-newsgroup-unreads)
6874     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
6875
6876 (defun gnus-summary-mark-read-and-unread-as-read ()
6877   "Intended to be used by `gnus-summary-mark-article-hook'."
6878   (let ((mark (gnus-summary-article-mark)))
6879     (when (or (gnus-unread-mark-p mark)
6880               (gnus-read-mark-p mark))
6881       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
6882
6883 (defun gnus-summary-mark-region-as-read (point mark all)
6884   "Mark all unread articles between point and mark as read.
6885 If given a prefix, mark all articles between point and mark as read,
6886 even ticked and dormant ones."
6887   (interactive "r\nP")
6888   (save-excursion
6889     (let (article)
6890       (goto-char point)
6891       (beginning-of-line)
6892       (while (and
6893               (< (point) mark)
6894               (progn
6895                 (when (or all
6896                           (memq (setq article (gnus-summary-article-number))
6897                                 gnus-newsgroup-unreads))
6898                   (gnus-summary-mark-article article gnus-del-mark))
6899                 t)
6900               (gnus-summary-find-next))))))
6901
6902 (defun gnus-summary-mark-below (score mark)
6903   "Mark articles with score less than SCORE with MARK."
6904   (interactive "P\ncMark: ")
6905   (gnus-set-global-variables)
6906   (setq score (if score
6907                   (prefix-numeric-value score)
6908                 (or gnus-summary-default-score 0)))
6909   (save-excursion
6910     (set-buffer gnus-summary-buffer)
6911     (goto-char (point-min))
6912     (while 
6913         (progn
6914           (and (< (gnus-summary-article-score) score)
6915                (gnus-summary-mark-article nil mark))
6916           (gnus-summary-find-next)))))
6917
6918 (defun gnus-summary-kill-below (&optional score)
6919   "Mark articles with score below SCORE as read."
6920   (interactive "P")
6921   (gnus-set-global-variables)
6922   (gnus-summary-mark-below score gnus-killed-mark))
6923
6924 (defun gnus-summary-clear-above (&optional score)
6925   "Clear all marks from articles with score above SCORE."
6926   (interactive "P")
6927   (gnus-set-global-variables)
6928   (gnus-summary-mark-above score gnus-unread-mark))
6929
6930 (defun gnus-summary-tick-above (&optional score)
6931   "Tick all articles with score above SCORE."
6932   (interactive "P")
6933   (gnus-set-global-variables)
6934   (gnus-summary-mark-above score gnus-ticked-mark))
6935
6936 (defun gnus-summary-mark-above (score mark)
6937   "Mark articles with score over SCORE with MARK."
6938   (interactive "P\ncMark: ")
6939   (gnus-set-global-variables)
6940   (setq score (if score
6941                   (prefix-numeric-value score)
6942                 (or gnus-summary-default-score 0)))
6943   (save-excursion
6944     (set-buffer gnus-summary-buffer)
6945     (goto-char (point-min))
6946     (while (and (progn
6947                   (if (> (gnus-summary-article-score) score)
6948                       (gnus-summary-mark-article nil mark))
6949                   t)
6950                 (gnus-summary-find-next)))))
6951
6952 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6953 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
6954 (defun gnus-summary-limit-include-expunged (&optional no-error)
6955   "Display all the hidden articles that were expunged for low scores."
6956   (interactive)
6957   (gnus-set-global-variables)
6958   (let ((buffer-read-only nil))
6959     (let ((scored gnus-newsgroup-scored)
6960           headers h)
6961       (while scored
6962         (or (gnus-summary-goto-subject (caar scored))
6963             (and (setq h (gnus-summary-article-header (caar scored)))
6964                  (< (cdar scored) gnus-summary-expunge-below)
6965                  (setq headers (cons h headers))))
6966         (setq scored (cdr scored)))
6967       (if (not headers)
6968           (when (not no-error)
6969             (error "No expunged articles hidden."))
6970         (goto-char (point-min))
6971         (gnus-summary-prepare-unthreaded (nreverse headers))
6972         (goto-char (point-min))
6973         (gnus-summary-position-point)
6974         t))))
6975
6976 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
6977   "Mark all articles not marked as unread in this newsgroup as read.
6978 If prefix argument ALL is non-nil, all articles are marked as read.
6979 If QUIETLY is non-nil, no questions will be asked.
6980 If TO-HERE is non-nil, it should be a point in the buffer.  All
6981 articles before this point will be marked as read.
6982 The number of articles marked as read is returned."
6983   (interactive "P")
6984   (gnus-set-global-variables)
6985   (prog1
6986       (when (or quietly
6987                 (not gnus-interactive-catchup) ;Without confirmation?
6988                 gnus-expert-user
6989                 (gnus-y-or-n-p
6990                  (if all
6991                      "Mark absolutely all articles as read? "
6992                    "Mark all unread articles as read? ")))
6993         (if (and not-mark
6994                  (not gnus-newsgroup-adaptive)
6995                  (not gnus-newsgroup-auto-expire)
6996                  (not gnus-suppress-duplicates))
6997             (progn
6998               (when all
6999                 (setq gnus-newsgroup-marked nil
7000                       gnus-newsgroup-dormant nil))
7001               (setq gnus-newsgroup-unreads nil))
7002           ;; We actually mark all articles as canceled, which we
7003           ;; have to do when using auto-expiry or adaptive scoring.
7004           (gnus-summary-show-all-threads)
7005           (when (gnus-summary-first-subject (not all))
7006             (while (and
7007                     (if to-here (< (point) to-here) t)
7008                     (gnus-summary-mark-article-as-read gnus-catchup-mark)
7009                     (gnus-summary-find-next (not all)))))
7010           (unless to-here
7011             (setq gnus-newsgroup-unreads nil))
7012           (gnus-set-mode-line 'summary)))
7013     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
7014       (when (and (not to-here) (eq 'nnvirtual (car method)))
7015         (nnvirtual-catchup-group
7016          (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
7017     (gnus-summary-position-point)))
7018
7019 (defun gnus-summary-catchup-to-here (&optional all)
7020   "Mark all unticked articles before the current one as read.
7021 If ALL is non-nil, also mark ticked and dormant articles as read."
7022   (interactive "P")
7023   (gnus-set-global-variables)
7024   (save-excursion
7025     (gnus-save-hidden-threads
7026       (let ((beg (point)))
7027         ;; We check that there are unread articles.
7028         (when (or all (gnus-summary-find-prev))
7029           (gnus-summary-catchup all t beg)))))
7030   (gnus-summary-position-point))
7031
7032 (defun gnus-summary-catchup-all (&optional quietly)
7033   "Mark all articles in this newsgroup as read."
7034   (interactive "P")
7035   (gnus-set-global-variables)
7036   (gnus-summary-catchup t quietly))
7037
7038 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7039   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7040 If prefix argument ALL is non-nil, all articles are marked as read."
7041   (interactive "P")
7042   (gnus-set-global-variables)
7043   (gnus-summary-catchup all quietly nil 'fast)
7044   ;; Select next newsgroup or exit.
7045   (if (eq gnus-auto-select-next 'quietly)
7046       (gnus-summary-next-group nil)
7047     (gnus-summary-exit)))
7048
7049 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
7050   "Mark all articles in this newsgroup as read, and then exit."
7051   (interactive "P")
7052   (gnus-set-global-variables)
7053   (gnus-summary-catchup-and-exit t quietly))
7054
7055 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
7056 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
7057   "Mark all articles in this group as read and select the next group.
7058 If given a prefix, mark all articles, unread as well as ticked, as
7059 read."
7060   (interactive "P")
7061   (gnus-set-global-variables)
7062   (save-excursion
7063     (gnus-summary-catchup all))
7064   (gnus-summary-next-article t nil nil t))
7065
7066 ;; Thread-based commands.
7067
7068 (defun gnus-summary-articles-in-thread (&optional article)
7069   "Return a list of all articles in the current thread.
7070 If ARTICLE is non-nil, return all articles in the thread that starts
7071 with that article."
7072   (let* ((article (or article (gnus-summary-article-number)))
7073          (data (gnus-data-find-list article))
7074          (top-level (gnus-data-level (car data)))
7075          (top-subject
7076           (cond ((null gnus-thread-operation-ignore-subject)
7077                  (gnus-simplify-subject-re
7078                   (mail-header-subject (gnus-data-header (car data)))))
7079                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
7080                  (gnus-simplify-subject-fuzzy
7081                   (mail-header-subject (gnus-data-header (car data)))))
7082                 (t nil)))
7083          (end-point (save-excursion
7084                       (if (gnus-summary-go-to-next-thread) 
7085                           (point) (point-max))))
7086          articles)
7087     (while (and data
7088                 (< (gnus-data-pos (car data)) end-point))
7089       (when (or (not top-subject)
7090                 (string= top-subject
7091                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
7092                              (gnus-simplify-subject-fuzzy
7093                               (mail-header-subject
7094                                (gnus-data-header (car data))))
7095                            (gnus-simplify-subject-re
7096                             (mail-header-subject
7097                              (gnus-data-header (car data)))))))
7098         (push (gnus-data-number (car data)) articles))
7099       (unless (and (setq data (cdr data))
7100                    (> (gnus-data-level (car data)) top-level))
7101         (setq data nil)))
7102     ;; Return the list of articles.
7103     (nreverse articles)))
7104
7105 (defun gnus-summary-rethread-current ()
7106   "Rethread the thread the current article is part of."
7107   (interactive)
7108   (gnus-set-global-variables)
7109   (let* ((gnus-show-threads t)
7110          (article (gnus-summary-article-number))
7111          (id (mail-header-id (gnus-summary-article-header)))
7112          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
7113     (unless id
7114       (error "No article on the current line"))
7115     (gnus-rebuild-thread id)
7116     (gnus-summary-goto-subject article)))
7117
7118 (defun gnus-summary-reparent-thread ()
7119   "Make current article child of the marked (or previous) article.
7120
7121 Note that the re-threading will only work if `gnus-thread-ignore-subject'
7122 is non-nil or the Subject: of both articles are the same."
7123   (interactive)
7124   (or (not (gnus-group-read-only-p))
7125       (error "The current newsgroup does not support article editing."))
7126   (or (<= (length gnus-newsgroup-processable) 1)
7127       (error "No more than one article may be marked."))
7128   (save-window-excursion
7129     (let ((gnus-article-buffer " *reparent*")
7130           (current-article (gnus-summary-article-number))
7131           ; first grab the marked article, otherwise one line up.
7132           (parent-article (if (not (null gnus-newsgroup-processable))
7133                               (car gnus-newsgroup-processable)
7134                             (save-excursion
7135                               (if (eq (forward-line -1) 0)
7136                                   (gnus-summary-article-number)
7137                                 (error "Beginning of summary buffer."))))))
7138       (or (not (eq current-article parent-article))
7139           (error "An article may not be self-referential."))
7140       (let ((message-id (mail-header-id 
7141                          (gnus-summary-article-header parent-article))))
7142         (or (and message-id (not (equal message-id "")))
7143             (error "No message-id in desired parent."))
7144         (gnus-summary-select-article t t nil current-article)
7145         (set-buffer gnus-article-buffer)
7146         (setq buffer-read-only nil)
7147         (let ((buf (format "%s" (buffer-string))))
7148           (erase-buffer)
7149           (insert buf))
7150         (goto-char (point-min))
7151         (if (search-forward-regexp "^References: " nil t)
7152             (insert message-id " " )
7153           (insert "References: " message-id "\n"))
7154         (or (gnus-request-replace-article current-article
7155                                           (car gnus-article-current)
7156                                           gnus-article-buffer)
7157             (error "Couldn't replace article."))
7158         (set-buffer gnus-summary-buffer)
7159         (gnus-summary-unmark-all-processable)
7160         (gnus-summary-rethread-current)
7161         (gnus-message 3 "Article %d is now the child of article %d."
7162                       current-article parent-article)))))
7163
7164 (defun gnus-summary-toggle-threads (&optional arg)
7165   "Toggle showing conversation threads.
7166 If ARG is positive number, turn showing conversation threads on."
7167   (interactive "P")
7168   (gnus-set-global-variables)
7169   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
7170     (setq gnus-show-threads
7171           (if (null arg) (not gnus-show-threads)
7172             (> (prefix-numeric-value arg) 0)))
7173     (gnus-summary-prepare)
7174     (gnus-summary-goto-subject current)
7175     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
7176     (gnus-summary-position-point)))
7177
7178 (defun gnus-summary-show-all-threads ()
7179   "Show all threads."
7180   (interactive)
7181   (gnus-set-global-variables)
7182   (save-excursion
7183     (let ((buffer-read-only nil))
7184       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
7185   (gnus-summary-position-point))
7186
7187 (defun gnus-summary-show-thread ()
7188   "Show thread subtrees.
7189 Returns nil if no thread was there to be shown."
7190   (interactive)
7191   (gnus-set-global-variables)
7192   (let ((buffer-read-only nil)
7193         (orig (point))
7194         ;; first goto end then to beg, to have point at beg after let
7195         (end (progn (end-of-line) (point)))
7196         (beg (progn (beginning-of-line) (point))))
7197     (prog1
7198         ;; Any hidden lines here?
7199         (search-forward "\r" end t)
7200       (subst-char-in-region beg end ?\^M ?\n t)
7201       (goto-char orig)
7202       (gnus-summary-position-point))))
7203
7204 (defun gnus-summary-hide-all-threads ()
7205   "Hide all thread subtrees."
7206   (interactive)
7207   (gnus-set-global-variables)
7208   (save-excursion
7209     (goto-char (point-min))
7210     (gnus-summary-hide-thread)
7211     (while (zerop (gnus-summary-next-thread 1 t))
7212       (gnus-summary-hide-thread)))
7213   (gnus-summary-position-point))
7214
7215 (defun gnus-summary-hide-thread ()
7216   "Hide thread subtrees.
7217 Returns nil if no threads were there to be hidden."
7218   (interactive)
7219   (gnus-set-global-variables)
7220   (let ((buffer-read-only nil)
7221         (start (point))
7222         (article (gnus-summary-article-number)))
7223     (goto-char start)
7224     ;; Go forward until either the buffer ends or the subthread
7225     ;; ends.
7226     (when (and (not (eobp))
7227                (or (zerop (gnus-summary-next-thread 1 t))
7228                    (goto-char (point-max))))
7229       (prog1
7230           (if (and (> (point) start)
7231                    (search-backward "\n" start t))
7232               (progn
7233                 (subst-char-in-region start (point) ?\n ?\^M)
7234                 (gnus-summary-goto-subject article))
7235             (goto-char start)
7236             nil)
7237         ;;(gnus-summary-position-point)
7238         ))))
7239
7240 (defun gnus-summary-go-to-next-thread (&optional previous)
7241   "Go to the same level (or less) next thread.
7242 If PREVIOUS is non-nil, go to previous thread instead.
7243 Return the article number moved to, or nil if moving was impossible."
7244   (let ((level (gnus-summary-thread-level))
7245         (way (if previous -1 1))
7246         (beg (point)))
7247     (forward-line way)
7248     (while (and (not (eobp))
7249                 (< level (gnus-summary-thread-level)))
7250       (forward-line way))
7251     (if (eobp)
7252         (progn
7253           (goto-char beg)
7254           nil)
7255       (setq beg (point))
7256       (prog1
7257           (gnus-summary-article-number)
7258         (goto-char beg)))))
7259
7260 (defun gnus-summary-go-to-next-thread-old (&optional previous)
7261   "Go to the same level (or less) next thread.
7262 If PREVIOUS is non-nil, go to previous thread instead.
7263 Return the article number moved to, or nil if moving was impossible."
7264   (if (and (eq gnus-summary-make-false-root 'dummy)
7265            (gnus-summary-article-intangible-p))
7266       (let ((beg (point)))
7267         (while (and (zerop (forward-line 1))
7268                     (not (gnus-summary-article-intangible-p))
7269                     (not (zerop (save-excursion 
7270                                   (gnus-summary-thread-level))))))
7271         (if (eobp)
7272             (progn
7273               (goto-char beg)
7274               nil)
7275           (point)))
7276     (let* ((level (gnus-summary-thread-level))
7277            (article (gnus-summary-article-number))
7278            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
7279            oart)
7280       (while data
7281         (if (<= (gnus-data-level (car data)) level)
7282             (setq oart (gnus-data-number (car data))
7283                   data nil)
7284           (setq data (cdr data))))
7285       (and oart
7286            (gnus-summary-goto-subject oart)))))
7287
7288 (defun gnus-summary-next-thread (n &optional silent)
7289   "Go to the same level next N'th thread.
7290 If N is negative, search backward instead.
7291 Returns the difference between N and the number of skips actually
7292 done.
7293
7294 If SILENT, don't output messages."
7295   (interactive "p")
7296   (gnus-set-global-variables)
7297   (let ((backward (< n 0))
7298         (n (abs n))
7299         old dum int)
7300     (while (and (> n 0)
7301                 (gnus-summary-go-to-next-thread backward))
7302       (decf n))
7303     (unless silent 
7304       (gnus-summary-position-point))
7305     (when (and (not silent) (/= 0 n))
7306       (gnus-message 7 "No more threads"))
7307     n))
7308
7309 (defun gnus-summary-prev-thread (n)
7310   "Go to the same level previous N'th thread.
7311 Returns the difference between N and the number of skips actually
7312 done."
7313   (interactive "p")
7314   (gnus-set-global-variables)
7315   (gnus-summary-next-thread (- n)))
7316
7317 (defun gnus-summary-go-down-thread ()
7318   "Go down one level in the current thread."
7319   (let ((children (gnus-summary-article-children)))
7320     (and children
7321          (gnus-summary-goto-subject (car children)))))
7322
7323 (defun gnus-summary-go-up-thread ()
7324   "Go up one level in the current thread."
7325   (let ((parent (gnus-summary-article-parent)))
7326     (and parent
7327          (gnus-summary-goto-subject parent))))
7328
7329 (defun gnus-summary-down-thread (n)
7330   "Go down thread N steps.
7331 If N is negative, go up instead.
7332 Returns the difference between N and how many steps down that were
7333 taken."
7334   (interactive "p")
7335   (gnus-set-global-variables)
7336   (let ((up (< n 0))
7337         (n (abs n)))
7338     (while (and (> n 0)
7339                 (if up (gnus-summary-go-up-thread)
7340                   (gnus-summary-go-down-thread)))
7341       (setq n (1- n)))
7342     (gnus-summary-position-point)
7343     (if (/= 0 n) (gnus-message 7 "Can't go further"))
7344     n))
7345
7346 (defun gnus-summary-up-thread (n)
7347   "Go up thread N steps.
7348 If N is negative, go up instead.
7349 Returns the difference between N and how many steps down that were
7350 taken."
7351   (interactive "p")
7352   (gnus-set-global-variables)
7353   (gnus-summary-down-thread (- n)))
7354
7355 (defun gnus-summary-top-thread ()
7356   "Go to the top of the thread."
7357   (interactive)
7358   (gnus-set-global-variables)
7359   (while (gnus-summary-go-up-thread))
7360   (gnus-summary-article-number))
7361
7362 (defun gnus-summary-kill-thread (&optional unmark)
7363   "Mark articles under current thread as read.
7364 If the prefix argument is positive, remove any kinds of marks.
7365 If the prefix argument is negative, tick articles instead."
7366   (interactive "P")
7367   (gnus-set-global-variables)
7368   (when unmark
7369     (setq unmark (prefix-numeric-value unmark)))
7370   (let ((articles (gnus-summary-articles-in-thread)))
7371     (save-excursion
7372       ;; Expand the thread.
7373       (gnus-summary-show-thread)
7374       ;; Mark all the articles.
7375       (while articles
7376         (gnus-summary-goto-subject (car articles))
7377         (cond ((null unmark)
7378                (gnus-summary-mark-article-as-read gnus-killed-mark))
7379               ((> unmark 0)
7380                (gnus-summary-mark-article-as-unread gnus-unread-mark))
7381               (t
7382                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
7383         (setq articles (cdr articles))))
7384     ;; Hide killed subtrees.
7385     (and (null unmark)
7386          gnus-thread-hide-killed
7387          (gnus-summary-hide-thread))
7388     ;; If marked as read, go to next unread subject.
7389     (if (null unmark)
7390         ;; Go to next unread subject.
7391         (gnus-summary-next-subject 1 t)))
7392   (gnus-set-mode-line 'summary))
7393
7394 ;; Summary sorting commands
7395
7396 (defun gnus-summary-sort-by-number (&optional reverse)
7397   "Sort summary buffer by article number.
7398 Argument REVERSE means reverse order."
7399   (interactive "P")
7400   (gnus-summary-sort 'number reverse))
7401
7402 (defun gnus-summary-sort-by-author (&optional reverse)
7403   "Sort summary buffer by author name alphabetically.
7404 If case-fold-search is non-nil, case of letters is ignored.
7405 Argument REVERSE means reverse order."
7406   (interactive "P")
7407   (gnus-summary-sort 'author reverse))
7408
7409 (defun gnus-summary-sort-by-subject (&optional reverse)
7410   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
7411 If case-fold-search is non-nil, case of letters is ignored.
7412 Argument REVERSE means reverse order."
7413   (interactive "P")
7414   (gnus-summary-sort 'subject reverse))
7415
7416 (defun gnus-summary-sort-by-date (&optional reverse)
7417   "Sort summary buffer by date.
7418 Argument REVERSE means reverse order."
7419   (interactive "P")
7420   (gnus-summary-sort 'date reverse))
7421
7422 (defun gnus-summary-sort-by-score (&optional reverse)
7423   "Sort summary buffer by score.
7424 Argument REVERSE means reverse order."
7425   (interactive "P")
7426   (gnus-summary-sort 'score reverse))
7427
7428 (defun gnus-summary-sort (predicate reverse)
7429   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
7430   (gnus-set-global-variables)
7431   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
7432          (article (intern (format "gnus-article-sort-by-%s" predicate)))
7433          (gnus-thread-sort-functions
7434           (list
7435            (if (not reverse)
7436                thread
7437              `(lambda (t1 t2)
7438                 (,thread t2 t1)))))
7439          (gnus-article-sort-functions
7440           (list
7441            (if (not reverse)
7442                article
7443              `(lambda (t1 t2)
7444                 (,article t2 t1)))))
7445          (buffer-read-only)
7446          (gnus-summary-prepare-hook nil))
7447     ;; We do the sorting by regenerating the threads.
7448     (gnus-summary-prepare)
7449     ;; Hide subthreads if needed.
7450     (when (and gnus-show-threads gnus-thread-hide-subtree)
7451       (gnus-summary-hide-all-threads))))
7452
7453 ;; Summary saving commands.
7454
7455 (defun gnus-summary-save-article (&optional n not-saved)
7456   "Save the current article using the default saver function.
7457 If N is a positive number, save the N next articles.
7458 If N is a negative number, save the N previous articles.
7459 If N is nil and any articles have been marked with the process mark,
7460 save those articles instead.
7461 The variable `gnus-default-article-saver' specifies the saver function."
7462   (interactive "P")
7463   (gnus-set-global-variables)
7464   (let* ((articles (gnus-summary-work-articles n))
7465          (save-buffer (save-excursion 
7466                         (nnheader-set-temp-buffer " *Gnus Save*")))
7467          (num (length articles))
7468          header article file)
7469     (while articles
7470       (setq header (gnus-summary-article-header
7471                     (setq article (pop articles))))
7472       (if (not (vectorp header))
7473           ;; This is a pseudo-article.
7474           (if (assq 'name header)
7475               (gnus-copy-file (cdr (assq 'name header)))
7476             (gnus-message 1 "Article %d is unsaveable" article))
7477         ;; This is a real article.
7478         (save-window-excursion
7479           (gnus-summary-select-article t nil nil article))
7480         (save-excursion
7481           (set-buffer save-buffer)
7482           (erase-buffer)
7483           (insert-buffer-substring gnus-original-article-buffer))
7484         (setq file (gnus-article-save save-buffer file num))
7485         (gnus-summary-remove-process-mark article)
7486         (unless not-saved
7487           (gnus-summary-set-saved-mark article))))
7488     (gnus-kill-buffer save-buffer)
7489     (gnus-summary-position-point)
7490     n))
7491
7492 (defun gnus-summary-pipe-output (&optional arg)
7493   "Pipe the current article to a subprocess.
7494 If N is a positive number, pipe the N next articles.
7495 If N is a negative number, pipe the N previous articles.
7496 If N is nil and any articles have been marked with the process mark,
7497 pipe those articles instead."
7498   (interactive "P")
7499   (gnus-set-global-variables)
7500   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
7501     (gnus-summary-save-article arg t))
7502   (gnus-configure-windows 'pipe))
7503
7504 (defun gnus-summary-save-article-mail (&optional arg)
7505   "Append the current article to an mail file.
7506 If N is a positive number, save the N next articles.
7507 If N is a negative number, save the N previous articles.
7508 If N is nil and any articles have been marked with the process mark,
7509 save those articles instead."
7510   (interactive "P")
7511   (gnus-set-global-variables)
7512   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
7513     (gnus-summary-save-article arg)))
7514
7515 (defun gnus-summary-save-article-rmail (&optional arg)
7516   "Append the current article to an rmail file.
7517 If N is a positive number, save the N next articles.
7518 If N is a negative number, save the N previous articles.
7519 If N is nil and any articles have been marked with the process mark,
7520 save those articles instead."
7521   (interactive "P")
7522   (gnus-set-global-variables)
7523   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
7524     (gnus-summary-save-article arg)))
7525
7526 (defun gnus-summary-save-article-file (&optional arg)
7527   "Append the current article to a file.
7528 If N is a positive number, save the N next articles.
7529 If N is a negative number, save the N previous articles.
7530 If N is nil and any articles have been marked with the process mark,
7531 save those articles instead."
7532   (interactive "P")
7533   (gnus-set-global-variables)
7534   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
7535     (gnus-summary-save-article arg)))
7536
7537 (defun gnus-summary-save-article-body-file (&optional arg)
7538   "Append the current article body to a file.
7539 If N is a positive number, save the N next articles.
7540 If N is a negative number, save the N previous articles.
7541 If N is nil and any articles have been marked with the process mark,
7542 save those articles instead."
7543   (interactive "P")
7544   (gnus-set-global-variables)
7545   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
7546     (gnus-summary-save-article arg)))
7547
7548 (defun gnus-get-split-value (methods)
7549   "Return a value based on the split METHODS."
7550   (let (split-name method result match)
7551     (when methods
7552       (save-excursion
7553         (set-buffer gnus-original-article-buffer)
7554         (save-restriction
7555           (nnheader-narrow-to-headers)
7556           (while methods
7557             (goto-char (point-min))
7558             (setq method (pop methods))
7559             (setq match (car method))
7560             (when (cond
7561                    ((stringp match)
7562                     ;; Regular expression.
7563                     (condition-case ()
7564                         (re-search-forward match nil t)
7565                       (error nil)))
7566                    ((gnus-functionp match)
7567                     ;; Function.
7568                     (save-restriction
7569                       (widen)
7570                       (setq result (funcall match gnus-newsgroup-name))))
7571                    ((consp match)
7572                     ;; Form.
7573                     (save-restriction
7574                       (widen)
7575                       (setq result (eval match)))))
7576               (setq split-name (append (cdr method) split-name))
7577               (cond ((stringp result)
7578                      (push result split-name))
7579                     ((consp result)
7580                      (setq split-name (append result split-name)))))))))
7581     split-name))
7582
7583 (defun gnus-read-move-group-name (prompt default articles prefix)
7584   "Read a group name."
7585   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
7586          (minibuffer-confirm-incomplete nil) ; XEmacs
7587          group-map
7588          (dum (mapatoms
7589                (lambda (g) 
7590                  (and (boundp g)
7591                       (symbol-name g)
7592                       (memq 'respool
7593                             (assoc (symbol-name
7594                                     (car (gnus-find-method-for-group
7595                                           (symbol-name g))))
7596                                    gnus-valid-select-methods))
7597                       (push (list (symbol-name g)) group-map)))
7598                gnus-active-hashtb))
7599          (prom
7600           (format "%s %s to:"
7601                   prompt
7602                   (if (> (length articles) 1)
7603                       (format "these %d articles" (length articles))
7604                     "this article")))
7605          (to-newsgroup
7606           (cond
7607            ((null split-name)
7608             (gnus-completing-read default prom
7609                                   group-map nil nil prefix
7610                                   'gnus-group-history))
7611            ((= 1 (length split-name))
7612             (gnus-completing-read (car split-name) prom group-map
7613                                   nil nil nil
7614                                   'gnus-group-history))
7615            (t
7616             (gnus-completing-read nil prom 
7617                                   (mapcar (lambda (el) (list el))
7618                                           (nreverse split-name))
7619                                   nil nil nil
7620                                   'gnus-group-history)))))
7621     (when to-newsgroup
7622       (if (or (string= to-newsgroup "")
7623               (string= to-newsgroup prefix))
7624           (setq to-newsgroup (or default "")))
7625       (or (gnus-active to-newsgroup)
7626           (gnus-activate-group to-newsgroup)
7627           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
7628                                      to-newsgroup))
7629               (or (and (gnus-request-create-group 
7630                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
7631                        (gnus-activate-group to-newsgroup nil nil
7632                                             (gnus-group-name-to-method
7633                                              to-newsgroup)))
7634                   (error "Couldn't create group %s" to-newsgroup)))
7635           (error "No such group: %s" to-newsgroup)))
7636     to-newsgroup))
7637
7638 ;; Summary extract commands
7639
7640 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
7641   (let ((buffer-read-only nil)
7642         (article (gnus-summary-article-number))
7643         after-article b e)
7644     (or (gnus-summary-goto-subject article)
7645         (error (format "No such article: %d" article)))
7646     (gnus-summary-position-point)
7647     ;; If all commands are to be bunched up on one line, we collect
7648     ;; them here.
7649     (if gnus-view-pseudos-separately
7650         ()
7651       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
7652             files action)
7653         (while ps
7654           (setq action (cdr (assq 'action (car ps))))
7655           (setq files (list (cdr (assq 'name (car ps)))))
7656           (while (and ps (cdr ps)
7657                       (string= (or action "1")
7658                                (or (cdr (assq 'action (cadr ps))) "2")))
7659             (setq files (cons (cdr (assq 'name (cadr ps))) files))
7660             (setcdr ps (cddr ps)))
7661           (if (not files)
7662               ()
7663             (if (not (string-match "%s" action))
7664                 (setq files (cons " " files)))
7665             (setq files (cons " " files))
7666             (and (assq 'execute (car ps))
7667                  (setcdr (assq 'execute (car ps))
7668                          (funcall (if (string-match "%s" action)
7669                                       'format 'concat)
7670                                   action
7671                                   (mapconcat (lambda (f) f) files " ")))))
7672           (setq ps (cdr ps)))))
7673     (if (and gnus-view-pseudos (not not-view))
7674         (while pslist
7675           (and (assq 'execute (car pslist))
7676                (gnus-execute-command (cdr (assq 'execute (car pslist)))
7677                                      (eq gnus-view-pseudos 'not-confirm)))
7678           (setq pslist (cdr pslist)))
7679       (save-excursion
7680         (while pslist
7681           (setq after-article (or (cdr (assq 'article (car pslist)))
7682                                   (gnus-summary-article-number)))
7683           (gnus-summary-goto-subject after-article)
7684           (forward-line 1)
7685           (setq b (point))
7686           (insert "    " (file-name-nondirectory
7687                                 (cdr (assq 'name (car pslist))))
7688                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
7689           (setq e (point))
7690           (forward-line -1)             ; back to `b'
7691           (gnus-add-text-properties
7692            b (1- e) (list 'gnus-number gnus-reffed-article-number
7693                           gnus-mouse-face-prop gnus-mouse-face))
7694           (gnus-data-enter
7695            after-article gnus-reffed-article-number
7696            gnus-unread-mark b (car pslist) 0 (- e b))
7697           (push gnus-reffed-article-number gnus-newsgroup-unreads)
7698           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
7699           (setq pslist (cdr pslist)))))))
7700
7701 (defun gnus-pseudos< (p1 p2)
7702   (let ((c1 (cdr (assq 'action p1)))
7703         (c2 (cdr (assq 'action p2))))
7704     (and c1 c2 (string< c1 c2))))
7705
7706 (defun gnus-request-pseudo-article (props)
7707   (cond ((assq 'execute props)
7708          (gnus-execute-command (cdr (assq 'execute props)))))
7709   (let ((gnus-current-article (gnus-summary-article-number)))
7710     (run-hooks 'gnus-mark-article-hook)))
7711
7712 (defun gnus-execute-command (command &optional automatic)
7713   (save-excursion
7714     (gnus-article-setup-buffer)
7715     (set-buffer gnus-article-buffer)
7716     (setq buffer-read-only nil)
7717     (let ((command (if automatic command (read-string "Command: " command)))
7718           ;; Just binding this here doesn't help, because there might
7719           ;; be output from the process after exiting the scope of 
7720           ;; this `let'.
7721           ;; (buffer-read-only nil)
7722           )
7723       (erase-buffer)
7724       (insert "$ " command "\n\n")
7725       (if gnus-view-pseudo-asynchronously
7726           (start-process "gnus-execute" nil shell-file-name
7727                          shell-command-switch command)
7728         (call-process shell-file-name nil t nil
7729                       shell-command-switch command)))))
7730
7731 ;; Summary kill commands.
7732
7733 (defun gnus-summary-edit-global-kill (article)
7734   "Edit the \"global\" kill file."
7735   (interactive (list (gnus-summary-article-number)))
7736   (gnus-set-global-variables)
7737   (gnus-group-edit-global-kill article))
7738
7739 (defun gnus-summary-edit-local-kill ()
7740   "Edit a local kill file applied to the current newsgroup."
7741   (interactive)
7742   (gnus-set-global-variables)
7743   (setq gnus-current-headers (gnus-summary-article-header))
7744   (gnus-set-global-variables)
7745   (gnus-group-edit-local-kill
7746    (gnus-summary-article-number) gnus-newsgroup-name))
7747
7748 ;;; Header reading.
7749
7750 (defun gnus-read-header (id &optional header)
7751   "Read the headers of article ID and enter them into the Gnus system."
7752   (let ((group gnus-newsgroup-name)
7753         (gnus-override-method 
7754          (and (gnus-news-group-p gnus-newsgroup-name)
7755               gnus-refer-article-method))       
7756         where)
7757     ;; First we check to see whether the header in question is already
7758     ;; fetched.
7759     (if (stringp id)
7760         ;; This is a Message-ID.
7761         (setq header (or header (gnus-id-to-header id)))
7762       ;; This is an article number.
7763       (setq header (or header (gnus-summary-article-header id))))
7764     (if (and header
7765              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
7766         ;; We have found the header.
7767         header
7768       ;; We have to really fetch the header to this article.
7769       (when (setq where (gnus-request-head id group))
7770         (save-excursion
7771           (set-buffer nntp-server-buffer)
7772           (goto-char (point-max))
7773           (insert ".\n")
7774           (goto-char (point-min))
7775           (insert "211 ")
7776           (princ (cond
7777                   ((numberp id) id)
7778                   ((cdr where) (cdr where))
7779                   (header (mail-header-number header))
7780                   (t gnus-reffed-article-number))
7781                  (current-buffer))
7782           (insert " Article retrieved.\n"))
7783         ;(when (and header
7784         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
7785         ;  (setcar (gnus-id-to-thread id) nil))
7786         (if (not (setq header (car (gnus-get-newsgroup-headers))))
7787             ()                          ; Malformed head.
7788           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
7789             (if (and (stringp id)
7790                      (not (string= (gnus-group-real-name group)
7791                                    (car where))))
7792                 ;; If we fetched by Message-ID and the article came
7793                 ;; from a different group, we fudge some bogus article
7794                 ;; numbers for this article.
7795                 (mail-header-set-number header gnus-reffed-article-number))
7796             (decf gnus-reffed-article-number)
7797             (gnus-remove-header (mail-header-number header))
7798             (push header gnus-newsgroup-headers)
7799             (setq gnus-current-headers header)
7800             (push (mail-header-number header) gnus-newsgroup-limit))
7801           header)))))
7802
7803 (defun gnus-remove-header (number)
7804   "Remove header NUMBER from `gnus-newsgroup-headers'."
7805   (if (and gnus-newsgroup-headers
7806            (= number (mail-header-number (car gnus-newsgroup-headers))))
7807       (pop gnus-newsgroup-headers)
7808     (let ((headers gnus-newsgroup-headers))
7809       (while (and (cdr headers)
7810                   (not (= number (mail-header-number (cadr headers)))))
7811         (pop headers))
7812       (when (cdr headers)
7813         (setcdr headers (cddr headers))))))
7814
7815 ;;;
7816 ;;; summary highlights
7817 ;;;
7818
7819 (defun gnus-highlight-selected-summary ()
7820   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
7821   ;; Highlight selected article in summary buffer
7822   (when gnus-summary-selected-face
7823     (save-excursion
7824       (let* ((beg (progn (beginning-of-line) (point)))
7825              (end (progn (end-of-line) (point)))
7826              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
7827              (from (if (get-text-property beg gnus-mouse-face-prop) 
7828                        beg
7829                      (or (next-single-property-change 
7830                           beg gnus-mouse-face-prop nil end) 
7831                          beg)))
7832              (to
7833               (if (= from end)
7834                   (- from 2)
7835                 (or (next-single-property-change
7836                      from gnus-mouse-face-prop nil end)
7837                     end))))
7838         ;; If no mouse-face prop on line we will have to = from = end,
7839         ;; so we highlight the entire line instead.
7840         (when (= (+ to 2) from)
7841           (setq from beg)
7842           (setq to end))
7843         (if gnus-newsgroup-selected-overlay
7844             ;; Move old overlay.
7845             (gnus-move-overlay
7846              gnus-newsgroup-selected-overlay from to (current-buffer))
7847           ;; Create new overlay.
7848           (gnus-overlay-put
7849            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
7850            'face gnus-summary-selected-face))))))
7851
7852 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
7853 (defun gnus-summary-highlight-line ()
7854   "Highlight current line according to `gnus-summary-highlight'."
7855   (let* ((list gnus-summary-highlight)
7856          (p (point))
7857          (end (progn (end-of-line) (point)))
7858          ;; now find out where the line starts and leave point there.
7859          (beg (progn (beginning-of-line) (point)))
7860          (article (gnus-summary-article-number))
7861          (score (or (cdr (assq (or article gnus-current-article)
7862                                gnus-newsgroup-scored))
7863                     gnus-summary-default-score 0))
7864          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
7865          (inhibit-read-only t))
7866     ;; Eval the cars of the lists until we find a match.
7867     (let ((default gnus-summary-default-score))
7868       (while (and list
7869                   (not (eval (caar list))))
7870         (setq list (cdr list))))
7871     (let ((face (cdar list)))
7872       (unless (eq face (get-text-property beg 'face))
7873         (gnus-put-text-property 
7874          beg end 'face 
7875          (setq face (if (boundp face) (symbol-value face) face)))
7876         (when gnus-summary-highlight-line-function
7877           (funcall gnus-summary-highlight-line-function article face))))
7878     (goto-char p)))
7879
7880 (provide 'gnus-sum)
7881
7882 ;;; gnus-sum.el ends here