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