*** 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     (if data
4775         ;; We try to find some article after the current one.
4776         (while data
4777           (and (gnus-summary-goto-subject
4778                 (gnus-data-number (car data)) nil t)
4779                (setq data nil
4780                      found t))
4781           (setq data (cdr data))))
4782     (or found
4783         ;; If there is no data, that means that we were after the last
4784         ;; article.  The same goes when we can't find any articles
4785         ;; after the current one.
4786         (progn
4787           (goto-char (point-max))
4788           (gnus-summary-find-prev)))
4789     ;; We return how many articles were removed from the summary
4790     ;; buffer as a result of the new limit.
4791     (- total (length gnus-newsgroup-data))))
4792
4793 (defsubst gnus-invisible-cut-children (threads)
4794   (let ((num 0))
4795     (while threads
4796       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
4797         (incf num))
4798       (pop threads))
4799     (< num 2)))
4800
4801 (defsubst gnus-cut-thread (thread)
4802   "Go forwards in the thread until we find an article that we want to display."
4803   (when (or (eq gnus-fetch-old-headers 'some)
4804             (eq gnus-build-sparse-threads 'some)
4805             (eq gnus-build-sparse-threads 'more))
4806     ;; Deal with old-fetched headers and sparse threads.
4807     (while (and
4808             thread
4809             (or
4810              (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
4811              (memq (mail-header-number (car thread)) gnus-newsgroup-ancient))
4812             (or (<= (length (cdr thread)) 1)
4813                 (gnus-invisible-cut-children (cdr thread))))
4814       (setq thread (cadr thread))))
4815   thread)
4816
4817 (defun gnus-cut-threads (threads)
4818   "Cut off all uninteresting articles from the beginning of threads."
4819   (when (or (eq gnus-fetch-old-headers 'some)
4820             (eq gnus-build-sparse-threads 'some)
4821             (eq gnus-build-sparse-threads 'more))
4822     (let ((th threads))
4823       (while th
4824         (setcar th (gnus-cut-thread (car th)))
4825         (setq th (cdr th)))))
4826   ;; Remove nixed out threads.
4827   (delq nil threads))
4828
4829 (defun gnus-summary-initial-limit (&optional show-if-empty)
4830   "Figure out what the initial limit is supposed to be on group entry.
4831 This entails weeding out unwanted dormants, low-scored articles,
4832 fetch-old-headers verbiage, and so on."
4833   ;; Most groups have nothing to remove.
4834   (if (or gnus-inhibit-limiting
4835           (and (null gnus-newsgroup-dormant)
4836                (not (eq gnus-fetch-old-headers 'some))
4837                (null gnus-summary-expunge-below)
4838                (not (eq gnus-build-sparse-threads 'some))
4839                (not (eq gnus-build-sparse-threads 'more))
4840                (null gnus-thread-expunge-below)
4841                (not gnus-use-nocem)))
4842       () ; Do nothing.
4843     (push gnus-newsgroup-limit gnus-newsgroup-limits)
4844     (setq gnus-newsgroup-limit nil)
4845     (mapatoms
4846      (lambda (node)
4847        (unless (car (symbol-value node))
4848          ;; These threads have no parents -- they are roots.
4849          (let ((nodes (cdr (symbol-value node)))
4850                thread)
4851            (while nodes
4852              (if (and gnus-thread-expunge-below
4853                       (< (gnus-thread-total-score (car nodes))
4854                          gnus-thread-expunge-below))
4855                  (gnus-expunge-thread (pop nodes))
4856                (setq thread (pop nodes))
4857                (gnus-summary-limit-children thread))))))
4858      gnus-newsgroup-dependencies)
4859     ;; If this limitation resulted in an empty group, we might
4860     ;; pop the previous limit and use it instead.
4861     (when (and (not gnus-newsgroup-limit)
4862                show-if-empty)
4863       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
4864     gnus-newsgroup-limit))
4865
4866 (defun gnus-summary-limit-children (thread)
4867   "Return 1 if this subthread is visible and 0 if it is not."
4868   ;; First we get the number of visible children to this thread.  This
4869   ;; is done by recursing down the thread using this function, so this
4870   ;; will really go down to a leaf article first, before slowly
4871   ;; working its way up towards the root.
4872   (when thread
4873     (let ((children
4874            (if (cdr thread)
4875                (apply '+ (mapcar 'gnus-summary-limit-children
4876                                  (cdr thread)))
4877              0))
4878           (number (mail-header-number (car thread)))
4879           score)
4880       (if (or
4881            ;; If this article is dormant and has absolutely no visible
4882            ;; children, then this article isn't visible.
4883            (and (memq number gnus-newsgroup-dormant)
4884                 (= children 0))
4885            ;; If this is "fetch-old-headered" and there is only one
4886            ;; visible child (or less), then we don't want this article.
4887            (and (eq gnus-fetch-old-headers 'some)
4888                 (memq number gnus-newsgroup-ancient)
4889                 (zerop children))
4890            ;; If this is a sparsely inserted article with no children,
4891            ;; we don't want it.
4892            (and (eq gnus-build-sparse-threads 'some)
4893                 (memq number gnus-newsgroup-sparse)
4894                 (zerop children))
4895            ;; If we use expunging, and this article is really
4896            ;; low-scored, then we don't want this article.
4897            (when (and gnus-summary-expunge-below
4898                       (< (setq score
4899                                (or (cdr (assq number gnus-newsgroup-scored))
4900                                    gnus-summary-default-score))
4901                          gnus-summary-expunge-below))
4902              ;; We increase the expunge-tally here, but that has
4903              ;; nothing to do with the limits, really.
4904              (incf gnus-newsgroup-expunged-tally)
4905              ;; We also mark as read here, if that's wanted.
4906              (when (and gnus-summary-mark-below
4907                         (< score gnus-summary-mark-below))
4908                (setq gnus-newsgroup-unreads
4909                      (delq number gnus-newsgroup-unreads))
4910                (if gnus-newsgroup-auto-expire
4911                    (push number gnus-newsgroup-expirable)
4912                  (push (cons number gnus-low-score-mark)
4913                        gnus-newsgroup-reads)))
4914              t)
4915            (and gnus-use-nocem
4916                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
4917           ;; Nope, invisible article.
4918           0
4919         ;; Ok, this article is to be visible, so we add it to the limit
4920         ;; and return 1.
4921         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
4922         1))))
4923
4924 (defun gnus-expunge-thread (thread)
4925   "Mark all articles in THREAD as read."
4926   (let* ((number (mail-header-number (car thread))))
4927     (incf gnus-newsgroup-expunged-tally)
4928     ;; We also mark as read here, if that's wanted.
4929     (setq gnus-newsgroup-unreads
4930           (delq number gnus-newsgroup-unreads))
4931     (if gnus-newsgroup-auto-expire
4932         (push number gnus-newsgroup-expirable)
4933       (push (cons number gnus-low-score-mark)
4934             gnus-newsgroup-reads)))
4935   ;; Go recursively through all subthreads.
4936   (mapcar 'gnus-expunge-thread (cdr thread)))
4937
4938 ;; Summary article oriented commands
4939
4940 (defun gnus-summary-refer-parent-article (n)
4941   "Refer parent article N times.
4942 The difference between N and the number of articles fetched is returned."
4943   (interactive "p")
4944   (gnus-set-global-variables)
4945   (while
4946       (and
4947        (> n 0)
4948        (let* ((header (gnus-summary-article-header))
4949               (ref
4950                ;; If we try to find the parent of the currently
4951                ;; displayed article, then we take a look at the actual
4952                ;; References header, since this is slightly more
4953                ;; reliable than the References field we got from the
4954                ;; server.
4955                (if (and (eq (mail-header-number header)
4956                             (cdr gnus-article-current))
4957                         (equal gnus-newsgroup-name
4958                                (car gnus-article-current)))
4959                    (save-excursion
4960                      (set-buffer gnus-original-article-buffer)
4961                      (nnheader-narrow-to-headers)
4962                      (prog1
4963                          (message-fetch-field "references")
4964                        (widen)))
4965                  ;; It's not the current article, so we take a bet on
4966                  ;; the value we got from the server.
4967                  (mail-header-references header))))
4968          (if (setq ref (or ref (mail-header-references header)))
4969              (or (gnus-summary-refer-article (gnus-parent-id ref))
4970                  (gnus-message 1 "Couldn't find parent"))
4971            (gnus-message 1 "No references in article %d"
4972                          (gnus-summary-article-number))
4973            nil)))
4974     (setq n (1- n)))
4975   (gnus-summary-position-point)
4976   n)
4977
4978 (defun gnus-summary-refer-references ()
4979   "Fetch all articles mentioned in the References header.
4980 Return how many articles were fetched."
4981   (interactive)
4982   (gnus-set-global-variables)
4983   (let ((ref (mail-header-references (gnus-summary-article-header)))
4984         (current (gnus-summary-article-number))
4985         (n 0))
4986     ;; For each Message-ID in the References header...
4987     (while (string-match "<[^>]*>" ref)
4988       (incf n)
4989       ;; ... fetch that article.
4990       (gnus-summary-refer-article
4991        (prog1 (match-string 0 ref)
4992          (setq ref (substring ref (match-end 0))))))
4993     (gnus-summary-goto-subject current)
4994     (gnus-summary-position-point)
4995     n))
4996
4997 (defun gnus-summary-refer-article (message-id)
4998   "Fetch an article specified by MESSAGE-ID."
4999   (interactive "sMessage-ID: ")
5000   (when (and (stringp message-id)
5001              (not (zerop (length message-id))))
5002     ;; Construct the correct Message-ID if necessary.
5003     ;; Suggested by tale@pawl.rpi.edu.
5004     (unless (string-match "^<" message-id)
5005       (setq message-id (concat "<" message-id)))
5006     (unless (string-match ">$" message-id)
5007       (setq message-id (concat message-id ">")))
5008     (let* ((header (gnus-id-to-header message-id))
5009            (sparse (and header
5010                         (memq (mail-header-number header)
5011                               gnus-newsgroup-sparse))))
5012       (if header
5013           (prog1
5014               ;; The article is present in the buffer, to we just go to it.
5015               (gnus-summary-goto-article 
5016                (mail-header-number header) nil header)
5017             (when sparse
5018               (gnus-summary-update-article (mail-header-number header))))
5019         ;; We fetch the article
5020         (let ((gnus-override-method 
5021                (and (gnus-news-group-p gnus-newsgroup-name)
5022                     gnus-refer-article-method))
5023               number)
5024           ;; Start the special refer-article method, if necessary.
5025           (when (and gnus-refer-article-method
5026                      (gnus-news-group-p gnus-newsgroup-name))
5027             (gnus-check-server gnus-refer-article-method))
5028           ;; Fetch the header, and display the article.
5029           (if (setq number (gnus-summary-insert-subject message-id))
5030               (gnus-summary-select-article nil nil nil number)
5031             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
5032
5033 (defun gnus-summary-enter-digest-group (&optional force)
5034   "Enter a digest group based on the current article."
5035   (interactive "P")
5036   (gnus-set-global-variables)
5037   (gnus-summary-select-article)
5038   (let ((name (format "%s-%d"
5039                       (gnus-group-prefixed-name
5040                        gnus-newsgroup-name (list 'nndoc ""))
5041                       gnus-current-article))
5042         (ogroup gnus-newsgroup-name)
5043         (case-fold-search t)
5044         (buf (current-buffer))
5045         dig)
5046     (save-excursion
5047       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
5048       (insert-buffer-substring gnus-original-article-buffer)
5049       (narrow-to-region
5050        (goto-char (point-min))
5051        (or (search-forward "\n\n" nil t) (point)))
5052       (goto-char (point-min))
5053       (delete-matching-lines "^\\(Path\\):\\|^From ")
5054       (widen))
5055     (unwind-protect
5056         (if (gnus-group-read-ephemeral-group
5057              name `(nndoc ,name (nndoc-address
5058                                  ,(get-buffer dig))
5059                           (nndoc-article-type ,(if force 'digest 'guess))) t)
5060             ;; Make all postings to this group go to the parent group.
5061             (nconc (gnus-info-params (gnus-get-info name))
5062                    (list (cons 'to-group ogroup)))
5063           ;; Couldn't select this doc group.
5064           (switch-to-buffer buf)
5065           (gnus-set-global-variables)
5066           (gnus-configure-windows 'summary)
5067           (gnus-message 3 "Article couldn't be entered?"))
5068       (kill-buffer dig))))
5069
5070 (defun gnus-summary-isearch-article (&optional regexp-p)
5071   "Do incremental search forward on the current article.
5072 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
5073   (interactive "P")
5074   (gnus-set-global-variables)
5075   (gnus-summary-select-article)
5076   (gnus-configure-windows 'article)
5077   (gnus-eval-in-buffer-window gnus-article-buffer
5078     ;;(goto-char (point-min))
5079     (isearch-forward regexp-p)))
5080
5081 (defun gnus-summary-search-article-forward (regexp &optional backward)
5082   "Search for an article containing REGEXP forward.
5083 If BACKWARD, search backward instead."
5084   (interactive
5085    (list (read-string
5086           (format "Search article %s (regexp%s): "
5087                   (if current-prefix-arg "backward" "forward")
5088                   (if gnus-last-search-regexp
5089                       (concat ", default " gnus-last-search-regexp)
5090                     "")))
5091          current-prefix-arg))
5092   (gnus-set-global-variables)
5093   (if (string-equal regexp "")
5094       (setq regexp (or gnus-last-search-regexp ""))
5095     (setq gnus-last-search-regexp regexp))
5096   (unless (gnus-summary-search-article regexp backward)
5097     (error "Search failed: \"%s\"" regexp)))
5098
5099 (defun gnus-summary-search-article-backward (regexp)
5100   "Search for an article containing REGEXP backward."
5101   (interactive
5102    (list (read-string
5103           (format "Search article backward (regexp%s): "
5104                   (if gnus-last-search-regexp
5105                       (concat ", default " gnus-last-search-regexp)
5106                     "")))))
5107   (gnus-summary-search-article-forward regexp 'backward))
5108
5109 (defun gnus-summary-search-article (regexp &optional backward)
5110   "Search for an article containing REGEXP.
5111 Optional argument BACKWARD means do search for backward.
5112 `gnus-select-article-hook' is not called during the search."
5113   (let ((gnus-select-article-hook nil)  ;Disable hook.
5114         (gnus-article-display-hook nil)
5115         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
5116         (re-search
5117          (if backward
5118              're-search-backward 're-search-forward))
5119         (sum (current-buffer))
5120         (found nil))
5121     (gnus-save-hidden-threads
5122       (gnus-summary-select-article)
5123       (set-buffer gnus-article-buffer)
5124       (when backward
5125         (forward-line -1))
5126       (while (not found)
5127         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
5128         (if (if backward
5129                 (re-search-backward regexp nil t)
5130               (re-search-forward regexp nil t))
5131             ;; We found the regexp.
5132             (progn
5133               (setq found 'found)
5134               (beginning-of-line)
5135               (set-window-start
5136                (get-buffer-window (current-buffer))
5137                (point))
5138               (forward-line 1)
5139               (set-buffer sum))
5140           ;; We didn't find it, so we go to the next article.
5141           (set-buffer sum)
5142           (if (not (if backward (gnus-summary-find-prev)
5143                      (gnus-summary-find-next)))
5144               ;; No more articles.
5145               (setq found t)
5146             ;; Select the next article and adjust point.
5147             (gnus-summary-select-article)
5148             (set-buffer gnus-article-buffer)
5149             (widen)
5150             (goto-char (if backward (point-max) (point-min))))))
5151       (gnus-message 7 ""))
5152     ;; Return whether we found the regexp.
5153     (when (eq found 'found)
5154       (gnus-summary-show-thread)
5155       (gnus-summary-goto-subject gnus-current-article)
5156       (gnus-summary-position-point)
5157       t)))
5158
5159 (defun gnus-summary-find-matching (header regexp &optional backward unread
5160                                           not-case-fold)
5161   "Return a list of all articles that match REGEXP on HEADER.
5162 The search stars on the current article and goes forwards unless
5163 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
5164 If UNREAD is non-nil, only unread articles will
5165 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
5166 in the comparisons."
5167   (let ((data (if (eq backward 'all) gnus-newsgroup-data
5168                 (gnus-data-find-list
5169                  (gnus-summary-article-number) (gnus-data-list backward))))
5170         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
5171         (case-fold-search (not not-case-fold))
5172         articles d)
5173     (or (fboundp (intern (concat "mail-header-" header)))
5174         (error "%s is not a valid header" header))
5175     (while data
5176       (setq d (car data))
5177       (and (or (not unread)             ; We want all articles...
5178                (gnus-data-unread-p d))  ; Or just unreads.
5179            (vectorp (gnus-data-header d)) ; It's not a pseudo.
5180            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
5181            (setq articles (cons (gnus-data-number d) articles))) ; Success!
5182       (setq data (cdr data)))
5183     (nreverse articles)))
5184
5185 (defun gnus-summary-execute-command (header regexp command &optional backward)
5186   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
5187 If HEADER is an empty string (or nil), the match is done on the entire
5188 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
5189   (interactive
5190    (list (let ((completion-ignore-case t))
5191            (completing-read
5192             "Header name: "
5193             (mapcar (lambda (string) (list string))
5194                     '("Number" "Subject" "From" "Lines" "Date"
5195                       "Message-ID" "Xref" "References" "Body"))
5196             nil 'require-match))
5197          (read-string "Regexp: ")
5198          (read-key-sequence "Command: ")
5199          current-prefix-arg))
5200   (when (equal header "Body")
5201     (setq header ""))
5202   (gnus-set-global-variables)
5203   ;; Hidden thread subtrees must be searched as well.
5204   (gnus-summary-show-all-threads)
5205   ;; We don't want to change current point nor window configuration.
5206   (save-excursion
5207     (save-window-excursion
5208       (gnus-message 6 "Executing %s..." (key-description command))
5209       ;; We'd like to execute COMMAND interactively so as to give arguments.
5210       (gnus-execute header regexp
5211                     `(lambda () (call-interactively ',(key-binding command)))
5212                     backward)
5213       (gnus-message 6 "Executing %s...done" (key-description command)))))
5214
5215 (defun gnus-summary-beginning-of-article ()
5216   "Scroll the article back to the beginning."
5217   (interactive)
5218   (gnus-set-global-variables)
5219   (gnus-summary-select-article)
5220   (gnus-configure-windows 'article)
5221   (gnus-eval-in-buffer-window gnus-article-buffer
5222     (widen)
5223     (goto-char (point-min))
5224     (and gnus-break-pages (gnus-narrow-to-page))))
5225
5226 (defun gnus-summary-end-of-article ()
5227   "Scroll to the end of the article."
5228   (interactive)
5229   (gnus-set-global-variables)
5230   (gnus-summary-select-article)
5231   (gnus-configure-windows 'article)
5232   (gnus-eval-in-buffer-window gnus-article-buffer
5233     (widen)
5234     (goto-char (point-max))
5235     (recenter -3)
5236     (and gnus-break-pages (gnus-narrow-to-page))))
5237
5238 (defun gnus-summary-show-article (&optional arg)
5239   "Force re-fetching of the current article.
5240 If ARG (the prefix) is non-nil, show the raw article without any
5241 article massaging functions being run."
5242   (interactive "P")
5243   (gnus-set-global-variables)
5244   (if (not arg)
5245       ;; Select the article the normal way.
5246       (gnus-summary-select-article nil 'force)
5247     ;; Bind the article treatment functions to nil.
5248     (let ((gnus-have-all-headers t)
5249           gnus-article-display-hook
5250           gnus-article-prepare-hook
5251           gnus-break-pages
5252           gnus-visual)
5253       (gnus-summary-select-article nil 'force)))
5254   (gnus-summary-goto-subject gnus-current-article)
5255 ;  (gnus-configure-windows 'article)
5256   (gnus-summary-position-point))
5257
5258 (defun gnus-summary-verbose-headers (&optional arg)
5259   "Toggle permanent full header display.
5260 If ARG is a positive number, turn header display on.
5261 If ARG is a negative number, turn header display off."
5262   (interactive "P")
5263   (gnus-set-global-variables)
5264   (gnus-summary-toggle-header arg)
5265   (setq gnus-show-all-headers
5266         (cond ((or (not (numberp arg))
5267                    (zerop arg))
5268                (not gnus-show-all-headers))
5269               ((natnump arg)
5270                t))))
5271
5272 (defun gnus-summary-toggle-header (&optional arg)
5273   "Show the headers if they are hidden, or hide them if they are shown.
5274 If ARG is a positive number, show the entire header.
5275 If ARG is a negative number, hide the unwanted header lines."
5276   (interactive "P")
5277   (gnus-set-global-variables)
5278   (save-excursion
5279     (set-buffer gnus-article-buffer)
5280     (let* ((buffer-read-only nil)
5281            (inhibit-point-motion-hooks t)
5282            (hidden (text-property-any
5283                     (goto-char (point-min)) (search-forward "\n\n")
5284                     'invisible t))
5285            e)
5286       (goto-char (point-min))
5287       (when (search-forward "\n\n" nil t)
5288         (delete-region (point-min) (1- (point))))
5289       (goto-char (point-min))
5290       (save-excursion
5291         (set-buffer gnus-original-article-buffer)
5292         (goto-char (point-min))
5293         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
5294       (insert-buffer-substring gnus-original-article-buffer 1 e)
5295       (let ((article-inhibit-hiding t))
5296         (run-hooks 'gnus-article-display-hook))
5297       (if (or (not hidden) (and (numberp arg) (< arg 0)))
5298           (gnus-article-hide-headers)))))
5299
5300 (defun gnus-summary-show-all-headers ()
5301   "Make all header lines visible."
5302   (interactive)
5303   (gnus-set-global-variables)
5304   (gnus-article-show-all-headers))
5305
5306 (defun gnus-summary-toggle-mime (&optional arg)
5307   "Toggle MIME processing.
5308 If ARG is a positive number, turn MIME processing on."
5309   (interactive "P")
5310   (gnus-set-global-variables)
5311   (setq gnus-show-mime
5312         (if (null arg) (not gnus-show-mime)
5313           (> (prefix-numeric-value arg) 0)))
5314   (gnus-summary-select-article t 'force))
5315
5316 (defun gnus-summary-caesar-message (&optional arg)
5317   "Caesar rotate the current article by 13.
5318 The numerical prefix specifies how manu places to rotate each letter
5319 forward."
5320   (interactive "P")
5321   (gnus-set-global-variables)
5322   (gnus-summary-select-article)
5323   (let ((mail-header-separator ""))
5324     (gnus-eval-in-buffer-window gnus-article-buffer
5325       (save-restriction
5326         (widen)
5327         (let ((start (window-start))
5328               buffer-read-only)
5329           (message-caesar-buffer-body arg)
5330           (set-window-start (get-buffer-window (current-buffer)) start))))))
5331
5332 (defun gnus-summary-stop-page-breaking ()
5333   "Stop page breaking in the current article."
5334   (interactive)
5335   (gnus-set-global-variables)
5336   (gnus-summary-select-article)
5337   (gnus-eval-in-buffer-window gnus-article-buffer
5338     (widen)))
5339
5340 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
5341   "Move the current article to a different newsgroup.
5342 If N is a positive number, move the N next articles.
5343 If N is a negative number, move the N previous articles.
5344 If N is nil and any articles have been marked with the process mark,
5345 move those articles instead.
5346 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5347 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5348 re-spool using this method.
5349
5350 For this function to work, both the current newsgroup and the
5351 newsgroup that you want to move to have to support the `request-move'
5352 and `request-accept' functions."
5353   (interactive "P")
5354   (unless action (setq action 'move))
5355   (gnus-set-global-variables)
5356   ;; Check whether the source group supports the required functions.
5357   (cond ((and (eq action 'move)
5358               (not (gnus-check-backend-function
5359                     'request-move-article gnus-newsgroup-name)))
5360          (error "The current group does not support article moving"))
5361         ((and (eq action 'crosspost)
5362               (not (gnus-check-backend-function
5363                     'request-replace-article gnus-newsgroup-name)))
5364          (error "The current group does not support article editing")))
5365   (let ((articles (gnus-summary-work-articles n))
5366         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
5367         (names '((move "Move" "Moving")
5368                  (copy "Copy" "Copying")
5369                  (crosspost "Crosspost" "Crossposting")))
5370         (copy-buf (save-excursion
5371                     (nnheader-set-temp-buffer " *copy article*")))
5372         art-group to-method new-xref article to-groups)
5373     (unless (assq action names)
5374       (error "Unknown action %s" action))
5375     ;; Read the newsgroup name.
5376     (when (and (not to-newsgroup)
5377                (not select-method))
5378       (setq to-newsgroup
5379             (gnus-read-move-group-name
5380              (cadr (assq action names))
5381              (symbol-value (intern (format "gnus-current-%s-group" action)))
5382              articles prefix))
5383       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
5384     (setq to-method (or select-method 
5385                         (gnus-group-name-to-method to-newsgroup)))
5386     ;; Check the method we are to move this article to...
5387     (or (gnus-check-backend-function 'request-accept-article (car to-method))
5388         (error "%s does not support article copying" (car to-method)))
5389     (or (gnus-check-server to-method)
5390         (error "Can't open server %s" (car to-method)))
5391     (gnus-message 6 "%s to %s: %s..."
5392                   (caddr (assq action names))
5393                   (or (car select-method) to-newsgroup) articles)
5394     (while articles
5395       (setq article (pop articles))
5396       (setq
5397        art-group
5398        (cond
5399         ;; Move the article.
5400         ((eq action 'move)
5401          (gnus-request-move-article
5402           article                       ; Article to move
5403           gnus-newsgroup-name           ; From newsgrouo
5404           (nth 1 (gnus-find-method-for-group
5405                   gnus-newsgroup-name)) ; Server
5406           (list 'gnus-request-accept-article
5407                 to-newsgroup (list 'quote select-method)
5408                 (not articles))         ; Accept form
5409           (not articles)))              ; Only save nov last time
5410         ;; Copy the article.
5411         ((eq action 'copy)
5412          (save-excursion
5413            (set-buffer copy-buf)
5414            (gnus-request-article-this-buffer article gnus-newsgroup-name)
5415            (gnus-request-accept-article
5416             to-newsgroup select-method (not articles))))
5417         ;; Crosspost the article.
5418         ((eq action 'crosspost)
5419          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
5420            (setq new-xref (concat gnus-newsgroup-name ":" article))
5421            (if (and xref (not (string= xref "")))
5422                (progn
5423                  (when (string-match "^Xref: " xref)
5424                    (setq xref (substring xref (match-end 0))))
5425                  (setq new-xref (concat xref " " new-xref)))
5426              (setq new-xref (concat (system-name) " " new-xref)))
5427            (save-excursion
5428              (set-buffer copy-buf)
5429              (gnus-request-article-this-buffer article gnus-newsgroup-name)
5430              (nnheader-replace-header "xref" new-xref)
5431              (gnus-request-accept-article
5432               to-newsgroup select-method (not articles)))))))
5433       (if (not art-group)
5434           (gnus-message 1 "Couldn't %s article %s"
5435                         (cadr (assq action names)) article)
5436         (let* ((entry
5437                 (or
5438                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
5439                  (gnus-gethash
5440                   (gnus-group-prefixed-name
5441                    (car art-group)
5442                    (or select-method 
5443                        (gnus-find-method-for-group to-newsgroup)))
5444                   gnus-newsrc-hashtb)))
5445                (info (nth 2 entry))
5446                (to-group (gnus-info-group info)))
5447           ;; Update the group that has been moved to.
5448           (when (and info
5449                      (memq action '(move copy)))
5450             (unless (member to-group to-groups)
5451               (push to-group to-groups))
5452
5453             (unless (memq article gnus-newsgroup-unreads)
5454               (gnus-info-set-read
5455                info (gnus-add-to-range (gnus-info-read info)
5456                                        (list (cdr art-group)))))
5457
5458             ;; Copy any marks over to the new group.
5459             (let ((marks gnus-article-mark-lists)
5460                   (to-article (cdr art-group)))
5461
5462               ;; See whether the article is to be put in the cache.
5463               (when gnus-use-cache
5464                 (gnus-cache-possibly-enter-article
5465                  to-group to-article
5466                  (let ((header (copy-sequence
5467                                 (gnus-summary-article-header article))))
5468                    (mail-header-set-number header to-article)
5469                    header)
5470                  (memq article gnus-newsgroup-marked)
5471                  (memq article gnus-newsgroup-dormant)
5472                  (memq article gnus-newsgroup-unreads)))
5473
5474               (while marks
5475                 (when (memq article (symbol-value
5476                                      (intern (format "gnus-newsgroup-%s"
5477                                                      (caar marks)))))
5478                   ;; If the other group is the same as this group,
5479                   ;; then we have to add the mark to the list.
5480                   (when (equal to-group gnus-newsgroup-name)
5481                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
5482                          (cons to-article
5483                                (symbol-value
5484                                 (intern (format "gnus-newsgroup-%s"
5485                                                 (caar marks)))))))
5486                   ;; Copy mark to other group.
5487                   (gnus-add-marked-articles
5488                    to-group (cdar marks) (list to-article) info))
5489                 (setq marks (cdr marks)))))
5490
5491           ;; Update the Xref header in this article to point to
5492           ;; the new crossposted article we have just created.
5493           (when (eq action 'crosspost)
5494             (save-excursion
5495               (set-buffer copy-buf)
5496               (gnus-request-article-this-buffer article gnus-newsgroup-name)
5497               (nnheader-replace-header
5498                "xref" (concat new-xref " " (gnus-group-prefixed-name
5499                                             (car art-group) to-method)
5500                               ":" (cdr art-group)))
5501               (gnus-request-replace-article
5502                article gnus-newsgroup-name (current-buffer)))))
5503
5504         (gnus-summary-goto-subject article)
5505         (when (eq action 'move)
5506           (gnus-summary-mark-article article gnus-canceled-mark)))
5507       (gnus-summary-remove-process-mark article))
5508     ;; Re-activate all groups that have been moved to.
5509     (while to-groups
5510       (gnus-activate-group (pop to-groups)))
5511     
5512     (gnus-kill-buffer copy-buf)
5513     (gnus-summary-position-point)
5514     (gnus-set-mode-line 'summary)))
5515
5516 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
5517   "Move the current article to a different newsgroup.
5518 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
5519 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
5520 re-spool using this method."
5521   (interactive "P")
5522   (gnus-summary-move-article n nil select-method 'copy))
5523
5524 (defun gnus-summary-crosspost-article (&optional n)
5525   "Crosspost the current article to some other group."
5526   (interactive "P")
5527   (gnus-summary-move-article n nil nil 'crosspost))
5528
5529 (defvar gnus-summary-respool-default-method nil
5530   "Default method for respooling an article.  
5531 If nil, use to the current newsgroup method.")
5532
5533 (defun gnus-summary-respool-article (&optional n method)
5534   "Respool the current article.
5535 The article will be squeezed through the mail spooling process again,
5536 which means that it will be put in some mail newsgroup or other
5537 depending on `nnmail-split-methods'.
5538 If N is a positive number, respool the N next articles.
5539 If N is a negative number, respool the N previous articles.
5540 If N is nil and any articles have been marked with the process mark,
5541 respool those articles instead.
5542
5543 Respooling can be done both from mail groups and \"real\" newsgroups.
5544 In the former case, the articles in question will be moved from the
5545 current group into whatever groups they are destined to.  In the
5546 latter case, they will be copied into the relevant groups."
5547   (interactive 
5548    (list current-prefix-arg
5549          (let* ((methods (gnus-methods-using 'respool))
5550                 (methname
5551                  (symbol-name (or gnus-summary-respool-default-method
5552                                   (car (gnus-find-method-for-group
5553                                         gnus-newsgroup-name)))))
5554                 (method
5555                  (gnus-completing-read 
5556                   methname "What backend do you want to use when respooling?"
5557                   methods nil t nil 'gnus-method-history))
5558                 ms)
5559            (cond
5560             ((zerop (length (setq ms (gnus-servers-using-backend method))))
5561              (list (intern method) ""))
5562             ((= 1 (length ms))
5563              (car ms))
5564             (t
5565              (cdr (completing-read 
5566                    "Server name: "
5567                    (mapcar (lambda (m) (cons (cadr m) m)) ms) nil t)))))))
5568   (gnus-set-global-variables)
5569   (unless method
5570     (error "No method given for respooling"))
5571   (if (assoc (symbol-name
5572               (car (gnus-find-method-for-group gnus-newsgroup-name)))
5573              (gnus-methods-using 'respool))
5574       (gnus-summary-move-article n nil method)
5575     (gnus-summary-copy-article n nil method)))
5576
5577 (defun gnus-summary-import-article (file)
5578   "Import a random file into a mail newsgroup."
5579   (interactive "fImport file: ")
5580   (gnus-set-global-variables)
5581   (let ((group gnus-newsgroup-name)
5582         (now (current-time))
5583         atts lines)
5584     (or (gnus-check-backend-function 'request-accept-article group)
5585         (error "%s does not support article importing" group))
5586     (or (file-readable-p file)
5587         (not (file-regular-p file))
5588         (error "Can't read %s" file))
5589     (save-excursion
5590       (set-buffer (get-buffer-create " *import file*"))
5591       (buffer-disable-undo (current-buffer))
5592       (erase-buffer)
5593       (insert-file-contents file)
5594       (goto-char (point-min))
5595       (unless (nnheader-article-p)
5596         ;; This doesn't look like an article, so we fudge some headers.
5597         (setq atts (file-attributes file)
5598               lines (count-lines (point-min) (point-max)))
5599         (insert "From: " (read-string "From: ") "\n"
5600                 "Subject: " (read-string "Subject: ") "\n"
5601                 "Date: " (timezone-make-date-arpa-standard
5602                           (current-time-string (nth 5 atts))
5603                           (current-time-zone now)
5604                           (current-time-zone now)) "\n"
5605                 "Message-ID: " (message-make-message-id) "\n"
5606                 "Lines: " (int-to-string lines) "\n"
5607                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
5608       (gnus-request-accept-article group nil t)
5609       (kill-buffer (current-buffer)))))
5610
5611 (defun gnus-summary-expire-articles (&optional now)
5612   "Expire all articles that are marked as expirable in the current group."
5613   (interactive)
5614   (gnus-set-global-variables)
5615   (when (gnus-check-backend-function
5616          'request-expire-articles gnus-newsgroup-name)
5617     ;; This backend supports expiry.
5618     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
5619            (expirable (if total
5620                           (gnus-list-of-read-articles gnus-newsgroup-name)
5621                         (setq gnus-newsgroup-expirable
5622                               (sort gnus-newsgroup-expirable '<))))
5623            (expiry-wait (if now 'immediate
5624                           (gnus-group-get-parameter
5625                            gnus-newsgroup-name 'expiry-wait)))
5626            es)
5627       (when expirable
5628         ;; There are expirable articles in this group, so we run them
5629         ;; through the expiry process.
5630         (gnus-message 6 "Expiring articles...")
5631         ;; The list of articles that weren't expired is returned.
5632         (if expiry-wait
5633             (let ((nnmail-expiry-wait-function nil)
5634                   (nnmail-expiry-wait expiry-wait))
5635               (setq es (gnus-request-expire-articles
5636                         expirable gnus-newsgroup-name)))
5637           (setq es (gnus-request-expire-articles
5638                     expirable gnus-newsgroup-name)))
5639         (or total (setq gnus-newsgroup-expirable es))
5640         ;; We go through the old list of expirable, and mark all
5641         ;; really expired articles as nonexistent.
5642         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
5643           (let ((gnus-use-cache nil))
5644             (while expirable
5645               (unless (memq (car expirable) es)
5646                 (when (gnus-data-find (car expirable))
5647                   (gnus-summary-mark-article
5648                    (car expirable) gnus-canceled-mark)))
5649               (setq expirable (cdr expirable)))))
5650         (gnus-message 6 "Expiring articles...done")))))
5651
5652 (defun gnus-summary-expire-articles-now ()
5653   "Expunge all expirable articles in the current group.
5654 This means that *all* articles that are marked as expirable will be
5655 deleted forever, right now."
5656   (interactive)
5657   (gnus-set-global-variables)
5658   (or gnus-expert-user
5659       (gnus-y-or-n-p
5660        "Are you really, really, really sure you want to delete all these messages? ")
5661       (error "Phew!"))
5662   (gnus-summary-expire-articles t))
5663
5664 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
5665 (defun gnus-summary-delete-article (&optional n)
5666   "Delete the N next (mail) articles.
5667 This command actually deletes articles.  This is not a marking
5668 command.  The article will disappear forever from your life, never to
5669 return.
5670 If N is negative, delete backwards.
5671 If N is nil and articles have been marked with the process mark,
5672 delete these instead."
5673   (interactive "P")
5674   (gnus-set-global-variables)
5675   (or (gnus-check-backend-function 'request-expire-articles
5676                                    gnus-newsgroup-name)
5677       (error "The current newsgroup does not support article deletion."))
5678   ;; Compute the list of articles to delete.
5679   (let ((articles (gnus-summary-work-articles n))
5680         not-deleted)
5681     (if (and gnus-novice-user
5682              (not (gnus-y-or-n-p
5683                    (format "Do you really want to delete %s forever? "
5684                            (if (> (length articles) 1) 
5685                                (format "these %s articles" (length articles))
5686                              "this article")))))
5687         ()
5688       ;; Delete the articles.
5689       (setq not-deleted (gnus-request-expire-articles
5690                          articles gnus-newsgroup-name 'force))
5691       (while articles
5692         (gnus-summary-remove-process-mark (car articles))
5693         ;; The backend might not have been able to delete the article
5694         ;; after all.
5695         (or (memq (car articles) not-deleted)
5696             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
5697         (setq articles (cdr articles))))
5698     (gnus-summary-position-point)
5699     (gnus-set-mode-line 'summary)
5700     not-deleted))
5701
5702 (defun gnus-summary-edit-article (&optional force)
5703   "Enter into a buffer and edit the current article.
5704 This will have permanent effect only in mail groups.
5705 If FORCE is non-nil, allow editing of articles even in read-only
5706 groups."
5707   (interactive "P")
5708   (save-excursion
5709     (set-buffer gnus-summary-buffer)
5710     (gnus-set-global-variables)
5711     (when (and (not force)
5712                (gnus-group-read-only-p))
5713       (error "The current newsgroup does not support article editing."))
5714     ;; Select article if needed.
5715     (unless (eq (gnus-summary-article-number)
5716                 gnus-current-article)
5717       (gnus-summary-select-article t))
5718     (gnus-article-edit-article
5719      `(lambda ()
5720         (gnus-summary-edit-article-done
5721          ,(mail-header-references gnus-current-headers)
5722          ,(gnus-group-read-only-p) ,gnus-summary-buffer)))))
5723
5724 (defun gnus-summary-edit-article-done (references read-only buffer)
5725   "Make edits to the current article permanent."
5726   (interactive)
5727   ;; Replace the article.
5728   (if (and (not read-only)
5729            (not (gnus-request-replace-article
5730                  (cdr gnus-article-current) (car gnus-article-current)
5731                  (current-buffer))))
5732       (error "Couldn't replace article.")
5733     ;; Update the summary buffer.
5734     (if (equal (message-tokenize-header references " ")
5735                (message-tokenize-header
5736                 (message-fetch-field "references") " "))
5737         ;; We only have to update this line.
5738         (save-restriction
5739           (message-narrow-to-head)
5740           (let ((header (nnheader-parse-head t)))
5741             (set-buffer buffer)
5742             (mail-header-set-number header (cdr gnus-article-current))
5743             (gnus-summary-update-article-line
5744              (cdr gnus-article-current) header)))
5745       ;; Update threads.
5746       (set-buffer buffer)
5747       (gnus-summary-update-article (cdr gnus-article-current)))
5748     ;; Prettify the article buffer again.
5749     (save-excursion
5750       (set-buffer gnus-article-buffer)
5751       (run-hooks 'gnus-article-display-hook))
5752     ;; Prettify the summary buffer line.
5753     (when (gnus-visual-p 'summary-highlight 'highlight)
5754       (run-hooks 'gnus-visual-mark-article-hook))))
5755
5756 (defun gnus-summary-edit-wash (key)
5757   "Perform editing command in the article buffer."
5758   (interactive 
5759    (list
5760     (progn
5761       (message "%s" (concat (this-command-keys) "- "))
5762       (read-char))))
5763   (message "")
5764   (gnus-summary-edit-article)
5765   (execute-kbd-macro (concat (this-command-keys) key))
5766   (gnus-article-edit-done))
5767
5768 ;;; Respooling
5769
5770 (defun gnus-summary-respool-query ()
5771   "Query where the respool algorithm would put this article."
5772   (interactive)
5773   (gnus-set-global-variables)
5774   (gnus-summary-select-article)
5775   (save-excursion
5776     (set-buffer gnus-article-buffer)
5777     (save-restriction
5778       (goto-char (point-min))
5779       (search-forward "\n\n")
5780       (narrow-to-region (point-min) (point))
5781       (pp-eval-expression
5782        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
5783
5784 ;; Summary marking commands.
5785
5786 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
5787   "Mark articles which has the same subject as read, and then select the next.
5788 If UNMARK is positive, remove any kind of mark.
5789 If UNMARK is negative, tick articles."
5790   (interactive "P")
5791   (gnus-set-global-variables)
5792   (if unmark
5793       (setq unmark (prefix-numeric-value unmark)))
5794   (let ((count
5795          (gnus-summary-mark-same-subject
5796           (gnus-summary-article-subject) unmark)))
5797     ;; Select next unread article.  If auto-select-same mode, should
5798     ;; select the first unread article.
5799     (gnus-summary-next-article t (and gnus-auto-select-same
5800                                       (gnus-summary-article-subject)))
5801     (gnus-message 7 "%d article%s marked as %s"
5802                   count (if (= count 1) " is" "s are")
5803                   (if unmark "unread" "read"))))
5804
5805 (defun gnus-summary-kill-same-subject (&optional unmark)
5806   "Mark articles which has the same subject as read.
5807 If UNMARK is positive, remove any kind of mark.
5808 If UNMARK is negative, tick articles."
5809   (interactive "P")
5810   (gnus-set-global-variables)
5811   (if unmark
5812       (setq unmark (prefix-numeric-value unmark)))
5813   (let ((count
5814          (gnus-summary-mark-same-subject
5815           (gnus-summary-article-subject) unmark)))
5816     ;; If marked as read, go to next unread subject.
5817     (if (null unmark)
5818         ;; Go to next unread subject.
5819         (gnus-summary-next-subject 1 t))
5820     (gnus-message 7 "%d articles are marked as %s"
5821                   count (if unmark "unread" "read"))))
5822
5823 (defun gnus-summary-mark-same-subject (subject &optional unmark)
5824   "Mark articles with same SUBJECT as read, and return marked number.
5825 If optional argument UNMARK is positive, remove any kinds of marks.
5826 If optional argument UNMARK is negative, mark articles as unread instead."
5827   (let ((count 1))
5828     (save-excursion
5829       (cond
5830        ((null unmark)                   ; Mark as read.
5831         (while (and
5832                 (progn
5833                   (gnus-summary-mark-article-as-read gnus-killed-mark)
5834                   (gnus-summary-show-thread) t)
5835                 (gnus-summary-find-subject subject))
5836           (setq count (1+ count))))
5837        ((> unmark 0)                    ; Tick.
5838         (while (and
5839                 (progn
5840                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
5841                   (gnus-summary-show-thread) t)
5842                 (gnus-summary-find-subject subject))
5843           (setq count (1+ count))))
5844        (t                               ; Mark as unread.
5845         (while (and
5846                 (progn
5847                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
5848                   (gnus-summary-show-thread) t)
5849                 (gnus-summary-find-subject subject))
5850           (setq count (1+ count)))))
5851       (gnus-set-mode-line 'summary)
5852       ;; Return the number of marked articles.
5853       count)))
5854
5855 (defun gnus-summary-mark-as-processable (n &optional unmark)
5856   "Set the process mark on the next N articles.
5857 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
5858 the process mark instead.  The difference between N and the actual
5859 number of articles marked is returned."
5860   (interactive "p")
5861   (gnus-set-global-variables)
5862   (let ((backward (< n 0))
5863         (n (abs n)))
5864     (while (and
5865             (> n 0)
5866             (if unmark
5867                 (gnus-summary-remove-process-mark
5868                  (gnus-summary-article-number))
5869               (gnus-summary-set-process-mark (gnus-summary-article-number)))
5870             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
5871       (setq n (1- n)))
5872     (if (/= 0 n) (gnus-message 7 "No more articles"))
5873     (gnus-summary-recenter)
5874     (gnus-summary-position-point)
5875     n))
5876
5877 (defun gnus-summary-unmark-as-processable (n)
5878   "Remove the process mark from the next N articles.
5879 If N is negative, mark backward instead.  The difference between N and
5880 the actual number of articles marked is returned."
5881   (interactive "p")
5882   (gnus-set-global-variables)
5883   (gnus-summary-mark-as-processable n t))
5884
5885 (defun gnus-summary-unmark-all-processable ()
5886   "Remove the process mark from all articles."
5887   (interactive)
5888   (gnus-set-global-variables)
5889   (save-excursion
5890     (while gnus-newsgroup-processable
5891       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
5892   (gnus-summary-position-point))
5893
5894 (defun gnus-summary-mark-as-expirable (n)
5895   "Mark N articles forward as expirable.
5896 If N is negative, mark backward instead.  The difference between N and
5897 the actual number of articles marked is returned."
5898   (interactive "p")
5899   (gnus-set-global-variables)
5900   (gnus-summary-mark-forward n gnus-expirable-mark))
5901
5902 (defun gnus-summary-mark-article-as-replied (article)
5903   "Mark ARTICLE replied and update the summary line."
5904   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
5905   (let ((buffer-read-only nil))
5906     (when (gnus-summary-goto-subject article)
5907       (gnus-summary-update-secondary-mark article))))
5908
5909 (defun gnus-summary-set-bookmark (article)
5910   "Set a bookmark in current article."
5911   (interactive (list (gnus-summary-article-number)))
5912   (gnus-set-global-variables)
5913   (if (or (not (get-buffer gnus-article-buffer))
5914           (not gnus-current-article)
5915           (not gnus-article-current)
5916           (not (equal gnus-newsgroup-name (car gnus-article-current))))
5917       (error "No current article selected"))
5918   ;; Remove old bookmark, if one exists.
5919   (let ((old (assq article gnus-newsgroup-bookmarks)))
5920     (if old (setq gnus-newsgroup-bookmarks
5921                   (delq old gnus-newsgroup-bookmarks))))
5922   ;; Set the new bookmark, which is on the form
5923   ;; (article-number . line-number-in-body).
5924   (setq gnus-newsgroup-bookmarks
5925         (cons
5926          (cons article
5927                (save-excursion
5928                  (set-buffer gnus-article-buffer)
5929                  (count-lines
5930                   (min (point)
5931                        (save-excursion
5932                          (goto-char (point-min))
5933                          (search-forward "\n\n" nil t)
5934                          (point)))
5935                   (point))))
5936          gnus-newsgroup-bookmarks))
5937   (gnus-message 6 "A bookmark has been added to the current article."))
5938
5939 (defun gnus-summary-remove-bookmark (article)
5940   "Remove the bookmark from the current article."
5941   (interactive (list (gnus-summary-article-number)))
5942   (gnus-set-global-variables)
5943   ;; Remove old bookmark, if one exists.
5944   (let ((old (assq article gnus-newsgroup-bookmarks)))
5945     (if old
5946         (progn
5947           (setq gnus-newsgroup-bookmarks
5948                 (delq old gnus-newsgroup-bookmarks))
5949           (gnus-message 6 "Removed bookmark."))
5950       (gnus-message 6 "No bookmark in current article."))))
5951
5952 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
5953 (defun gnus-summary-mark-as-dormant (n)
5954   "Mark N articles forward as dormant.
5955 If N is negative, mark backward instead.  The difference between N and
5956 the actual number of articles marked is returned."
5957   (interactive "p")
5958   (gnus-set-global-variables)
5959   (gnus-summary-mark-forward n gnus-dormant-mark))
5960
5961 (defun gnus-summary-set-process-mark (article)
5962   "Set the process mark on ARTICLE and update the summary line."
5963   (setq gnus-newsgroup-processable
5964         (cons article
5965               (delq article gnus-newsgroup-processable)))
5966   (when (gnus-summary-goto-subject article)
5967     (gnus-summary-show-thread)
5968     (gnus-summary-update-secondary-mark article)))
5969
5970 (defun gnus-summary-remove-process-mark (article)
5971   "Remove the process mark from ARTICLE and update the summary line."
5972   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
5973   (when (gnus-summary-goto-subject article)
5974     (gnus-summary-show-thread)
5975     (gnus-summary-update-secondary-mark article)))
5976
5977 (defun gnus-summary-set-saved-mark (article)
5978   "Set the process mark on ARTICLE and update the summary line."
5979   (push article gnus-newsgroup-saved)
5980   (when (gnus-summary-goto-subject article)
5981     (gnus-summary-update-secondary-mark article)))
5982
5983 (defun gnus-summary-mark-forward (n &optional mark no-expire)
5984   "Mark N articles as read forwards.
5985 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
5986 The difference between N and the actual number of articles marked is
5987 returned."
5988   (interactive "p")
5989   (gnus-set-global-variables)
5990   (let ((backward (< n 0))
5991         (gnus-summary-goto-unread
5992          (and gnus-summary-goto-unread
5993               (not (eq gnus-summary-goto-unread 'never))
5994               (not (memq mark (list gnus-unread-mark
5995                                     gnus-ticked-mark gnus-dormant-mark)))))
5996         (n (abs n))
5997         (mark (or mark gnus-del-mark)))
5998     (while (and (> n 0)
5999                 (gnus-summary-mark-article nil mark no-expire)
6000                 (zerop (gnus-summary-next-subject
6001                         (if backward -1 1)
6002                         (and gnus-summary-goto-unread
6003                              (not (eq gnus-summary-goto-unread 'never)))
6004                         t)))
6005       (setq n (1- n)))
6006     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
6007     (gnus-summary-recenter)
6008     (gnus-summary-position-point)
6009     (gnus-set-mode-line 'summary)
6010     n))
6011
6012 (defun gnus-summary-mark-article-as-read (mark)
6013   "Mark the current article quickly as read with MARK."
6014   (let ((article (gnus-summary-article-number)))
6015     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6016     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6017     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6018     (setq gnus-newsgroup-reads
6019           (cons (cons article mark) gnus-newsgroup-reads))
6020     ;; Possibly remove from cache, if that is used.
6021     (and gnus-use-cache (gnus-cache-enter-remove-article article))
6022     ;; Allow the backend to change the mark.
6023     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
6024     ;; Check for auto-expiry.
6025     (when (and gnus-newsgroup-auto-expire
6026                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6027                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6028                    (= mark gnus-ancient-mark)
6029                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
6030                    (= mark gnus-duplicate-mark)))
6031       (setq mark gnus-expirable-mark)
6032       (push article gnus-newsgroup-expirable))
6033     ;; Set the mark in the buffer.
6034     (gnus-summary-update-mark mark 'unread)
6035     t))
6036
6037 (defun gnus-summary-mark-article-as-unread (mark)
6038   "Mark the current article quickly as unread with MARK."
6039   (let ((article (gnus-summary-article-number)))
6040     (if (< article 0)
6041         (gnus-error 1 "Unmarkable article")
6042       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6043       (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6044       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6045       (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
6046       (cond ((= mark gnus-ticked-mark)
6047              (push article gnus-newsgroup-marked))
6048             ((= mark gnus-dormant-mark)
6049              (push article gnus-newsgroup-dormant))
6050             (t
6051              (push article gnus-newsgroup-unreads)))
6052       (setq gnus-newsgroup-reads
6053             (delq (assq article gnus-newsgroup-reads)
6054                   gnus-newsgroup-reads))
6055
6056       ;; See whether the article is to be put in the cache.
6057       (and gnus-use-cache
6058            (vectorp (gnus-summary-article-header article))
6059            (save-excursion
6060              (gnus-cache-possibly-enter-article
6061               gnus-newsgroup-name article
6062               (gnus-summary-article-header article)
6063               (= mark gnus-ticked-mark)
6064               (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6065
6066       ;; Fix the mark.
6067       (gnus-summary-update-mark mark 'unread))
6068     t))
6069
6070 (defun gnus-summary-mark-article (&optional article mark no-expire)
6071   "Mark ARTICLE with MARK.  MARK can be any character.
6072 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
6073 `??' (dormant) and `?E' (expirable).
6074 If MARK is nil, then the default character `?D' is used.
6075 If ARTICLE is nil, then the article on the current line will be
6076 marked."
6077   ;; The mark might be a string.
6078   (and (stringp mark)
6079        (setq mark (aref mark 0)))
6080   ;; If no mark is given, then we check auto-expiring.
6081   (and (not no-expire)
6082        gnus-newsgroup-auto-expire
6083        (or (not mark)
6084            (and (numberp mark)
6085                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
6086                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
6087                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
6088                     (= mark gnus-duplicate-mark))))
6089        (setq mark gnus-expirable-mark))
6090   (let* ((mark (or mark gnus-del-mark))
6091          (article (or article (gnus-summary-article-number))))
6092     (or article (error "No article on current line"))
6093     (if (or (= mark gnus-unread-mark)
6094             (= mark gnus-ticked-mark)
6095             (= mark gnus-dormant-mark))
6096         (gnus-mark-article-as-unread article mark)
6097       (gnus-mark-article-as-read article mark))
6098
6099     ;; See whether the article is to be put in the cache.
6100     (and gnus-use-cache
6101          (not (= mark gnus-canceled-mark))
6102          (vectorp (gnus-summary-article-header article))
6103          (save-excursion
6104            (gnus-cache-possibly-enter-article
6105             gnus-newsgroup-name article
6106             (gnus-summary-article-header article)
6107             (= mark gnus-ticked-mark)
6108             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
6109
6110     (if (gnus-summary-goto-subject article nil t)
6111         (let ((buffer-read-only nil))
6112           (gnus-summary-show-thread)
6113           ;; Fix the mark.
6114           (gnus-summary-update-mark mark 'unread)
6115           t))))
6116
6117 (defun gnus-summary-update-secondary-mark (article)
6118   "Update the secondary (read, process, cache) mark."
6119   (gnus-summary-update-mark
6120    (cond ((memq article gnus-newsgroup-processable)
6121           gnus-process-mark)
6122          ((memq article gnus-newsgroup-cached)
6123           gnus-cached-mark)
6124          ((memq article gnus-newsgroup-replied)
6125           gnus-replied-mark)
6126          ((memq article gnus-newsgroup-saved)
6127           gnus-saved-mark)
6128          (t gnus-unread-mark))
6129    'replied)
6130   (when (gnus-visual-p 'summary-highlight 'highlight)
6131     (run-hooks 'gnus-summary-update-hook))
6132   t)
6133
6134 (defun gnus-summary-update-mark (mark type)
6135   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
6136         (buffer-read-only nil))
6137     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
6138     (and (looking-at "\r") (setq forward (1+ forward)))
6139     (when (and forward
6140                (<= (+ forward (point)) (point-max)))
6141       ;; Go to the right position on the line.
6142       (goto-char (+ forward (point)))
6143       ;; Replace the old mark with the new mark.
6144       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
6145       ;; Optionally update the marks by some user rule.
6146       (when (eq type 'unread)
6147         (gnus-data-set-mark
6148          (gnus-data-find (gnus-summary-article-number)) mark)
6149         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
6150
6151 (defun gnus-mark-article-as-read (article &optional mark)
6152   "Enter ARTICLE in the pertinent lists and remove it from others."
6153   ;; Make the article expirable.
6154   (let ((mark (or mark gnus-del-mark)))
6155     (if (= mark gnus-expirable-mark)
6156         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
6157       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
6158     ;; Remove from unread and marked lists.
6159     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6160     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6161     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6162     (push (cons article mark) gnus-newsgroup-reads)
6163     ;; Possibly remove from cache, if that is used.
6164     (when gnus-use-cache
6165       (gnus-cache-enter-remove-article article))))
6166
6167 (defun gnus-mark-article-as-unread (article &optional mark)
6168   "Enter ARTICLE in the pertinent lists and remove it from others."
6169   (let ((mark (or mark gnus-ticked-mark)))
6170     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
6171     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
6172     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
6173     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
6174     (cond ((= mark gnus-ticked-mark)
6175            (push article gnus-newsgroup-marked))
6176           ((= mark gnus-dormant-mark)
6177            (push article gnus-newsgroup-dormant))
6178           (t
6179            (push article gnus-newsgroup-unreads)))
6180     (setq gnus-newsgroup-reads
6181           (delq (assq article gnus-newsgroup-reads)
6182                 gnus-newsgroup-reads))))
6183
6184 (defalias 'gnus-summary-mark-as-unread-forward
6185   'gnus-summary-tick-article-forward)
6186 (make-obsolete 'gnus-summary-mark-as-unread-forward
6187                'gnus-summary-tick-article-forward)
6188 (defun gnus-summary-tick-article-forward (n)
6189   "Tick N articles forwards.
6190 If N is negative, tick backwards instead.
6191 The difference between N and the number of articles ticked is returned."
6192   (interactive "p")
6193   (gnus-summary-mark-forward n gnus-ticked-mark))
6194
6195 (defalias 'gnus-summary-mark-as-unread-backward
6196   'gnus-summary-tick-article-backward)
6197 (make-obsolete 'gnus-summary-mark-as-unread-backward
6198                'gnus-summary-tick-article-backward)
6199 (defun gnus-summary-tick-article-backward (n)
6200   "Tick N articles backwards.
6201 The difference between N and the number of articles ticked is returned."
6202   (interactive "p")
6203   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
6204
6205 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6206 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
6207 (defun gnus-summary-tick-article (&optional article clear-mark)
6208   "Mark current article as unread.
6209 Optional 1st argument ARTICLE specifies article number to be marked as unread.
6210 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
6211   (interactive)
6212   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
6213                                        gnus-ticked-mark)))
6214
6215 (defun gnus-summary-mark-as-read-forward (n)
6216   "Mark N articles as read forwards.
6217 If N is negative, mark backwards instead.
6218 The difference between N and the actual number of articles marked is
6219 returned."
6220   (interactive "p")
6221   (gnus-summary-mark-forward n gnus-del-mark t))
6222
6223 (defun gnus-summary-mark-as-read-backward (n)
6224   "Mark the N articles as read backwards.
6225 The difference between N and the actual number of articles marked is
6226 returned."
6227   (interactive "p")
6228   (gnus-summary-mark-forward (- n) gnus-del-mark t))
6229
6230 (defun gnus-summary-mark-as-read (&optional article mark)
6231   "Mark current article as read.
6232 ARTICLE specifies the article to be marked as read.
6233 MARK specifies a string to be inserted at the beginning of the line."
6234   (gnus-summary-mark-article article mark))
6235
6236 (defun gnus-summary-clear-mark-forward (n)
6237   "Clear marks from N articles forward.
6238 If N is negative, clear backward instead.
6239 The difference between N and the number of marks cleared is returned."
6240   (interactive "p")
6241   (gnus-summary-mark-forward n gnus-unread-mark))
6242
6243 (defun gnus-summary-clear-mark-backward (n)
6244   "Clear marks from N articles backward.
6245 The difference between N and the number of marks cleared is returned."
6246   (interactive "p")
6247   (gnus-summary-mark-forward (- n) gnus-unread-mark))
6248
6249 (defun gnus-summary-mark-unread-as-read ()
6250   "Intended to be used by `gnus-summary-mark-article-hook'."
6251   (when (memq gnus-current-article gnus-newsgroup-unreads)
6252     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
6253
6254 (defun gnus-summary-mark-read-and-unread-as-read ()
6255   "Intended to be used by `gnus-summary-mark-article-hook'."
6256   (let ((mark (gnus-summary-article-mark)))
6257     (when (or (gnus-unread-mark-p mark)
6258               (gnus-read-mark-p mark))
6259       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
6260
6261 (defun gnus-summary-mark-region-as-read (point mark all)
6262   "Mark all unread articles between point and mark as read.
6263 If given a prefix, mark all articles between point and mark as read,
6264 even ticked and dormant ones."
6265   (interactive "r\nP")
6266   (save-excursion
6267     (let (article)
6268       (goto-char point)
6269       (beginning-of-line)
6270       (while (and
6271               (< (point) mark)
6272               (progn
6273                 (when (or all
6274                           (memq (setq article (gnus-summary-article-number))
6275                                 gnus-newsgroup-unreads))
6276                   (gnus-summary-mark-article article gnus-del-mark))
6277                 t)
6278               (gnus-summary-find-next))))))
6279
6280 (defun gnus-summary-mark-below (score mark)
6281   "Mark articles with score less than SCORE with MARK."
6282   (interactive "P\ncMark: ")
6283   (gnus-set-global-variables)
6284   (setq score (if score
6285                   (prefix-numeric-value score)
6286                 (or gnus-summary-default-score 0)))
6287   (save-excursion
6288     (set-buffer gnus-summary-buffer)
6289     (goto-char (point-min))
6290     (while 
6291         (progn
6292           (and (< (gnus-summary-article-score) score)
6293                (gnus-summary-mark-article nil mark))
6294           (gnus-summary-find-next)))))
6295
6296 (defun gnus-summary-kill-below (&optional score)
6297   "Mark articles with score below SCORE as read."
6298   (interactive "P")
6299   (gnus-set-global-variables)
6300   (gnus-summary-mark-below score gnus-killed-mark))
6301
6302 (defun gnus-summary-clear-above (&optional score)
6303   "Clear all marks from articles with score above SCORE."
6304   (interactive "P")
6305   (gnus-set-global-variables)
6306   (gnus-summary-mark-above score gnus-unread-mark))
6307
6308 (defun gnus-summary-tick-above (&optional score)
6309   "Tick all articles with score above SCORE."
6310   (interactive "P")
6311   (gnus-set-global-variables)
6312   (gnus-summary-mark-above score gnus-ticked-mark))
6313
6314 (defun gnus-summary-mark-above (score mark)
6315   "Mark articles with score over SCORE with MARK."
6316   (interactive "P\ncMark: ")
6317   (gnus-set-global-variables)
6318   (setq score (if score
6319                   (prefix-numeric-value score)
6320                 (or gnus-summary-default-score 0)))
6321   (save-excursion
6322     (set-buffer gnus-summary-buffer)
6323     (goto-char (point-min))
6324     (while (and (progn
6325                   (if (> (gnus-summary-article-score) score)
6326                       (gnus-summary-mark-article nil mark))
6327                   t)
6328                 (gnus-summary-find-next)))))
6329
6330 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
6331 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
6332 (defun gnus-summary-limit-include-expunged (&optional no-error)
6333   "Display all the hidden articles that were expunged for low scores."
6334   (interactive)
6335   (gnus-set-global-variables)
6336   (let ((buffer-read-only nil))
6337     (let ((scored gnus-newsgroup-scored)
6338           headers h)
6339       (while scored
6340         (or (gnus-summary-goto-subject (caar scored))
6341             (and (setq h (gnus-summary-article-header (caar scored)))
6342                  (< (cdar scored) gnus-summary-expunge-below)
6343                  (setq headers (cons h headers))))
6344         (setq scored (cdr scored)))
6345       (if (not headers)
6346           (when (not no-error)
6347             (error "No expunged articles hidden."))
6348         (goto-char (point-min))
6349         (gnus-summary-prepare-unthreaded (nreverse headers))
6350         (goto-char (point-min))
6351         (gnus-summary-position-point)
6352         t))))
6353
6354 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
6355   "Mark all articles not marked as unread in this newsgroup as read.
6356 If prefix argument ALL is non-nil, all articles are marked as read.
6357 If QUIETLY is non-nil, no questions will be asked.
6358 If TO-HERE is non-nil, it should be a point in the buffer.  All
6359 articles before this point will be marked as read.
6360 The number of articles marked as read is returned."
6361   (interactive "P")
6362   (gnus-set-global-variables)
6363   (prog1
6364       (if (or quietly
6365               (not gnus-interactive-catchup) ;Without confirmation?
6366               gnus-expert-user
6367               (gnus-y-or-n-p
6368                (if all
6369                    "Mark absolutely all articles as read? "
6370                  "Mark all unread articles as read? ")))
6371           (if (and not-mark
6372                    (not gnus-newsgroup-adaptive)
6373                    (not gnus-newsgroup-auto-expire))
6374               (progn
6375                 (when all
6376                   (setq gnus-newsgroup-marked nil
6377                         gnus-newsgroup-dormant nil))
6378                 (setq gnus-newsgroup-unreads nil))
6379             ;; We actually mark all articles as canceled, which we
6380             ;; have to do when using auto-expiry or adaptive scoring.
6381             (gnus-summary-show-all-threads)
6382             (if (gnus-summary-first-subject (not all))
6383                 (while (and
6384                         (if to-here (< (point) to-here) t)
6385                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
6386                         (gnus-summary-find-next (not all)))))
6387             (unless to-here
6388               (setq gnus-newsgroup-unreads nil))
6389             (gnus-set-mode-line 'summary)))
6390     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
6391       (if (and (not to-here) (eq 'nnvirtual (car method)))
6392           (nnvirtual-catchup-group
6393            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
6394     (gnus-summary-position-point)))
6395
6396 (defun gnus-summary-catchup-to-here (&optional all)
6397   "Mark all unticked articles before the current one as read.
6398 If ALL is non-nil, also mark ticked and dormant articles as read."
6399   (interactive "P")
6400   (gnus-set-global-variables)
6401   (save-excursion
6402     (gnus-save-hidden-threads
6403       (let ((beg (point)))
6404         ;; We check that there are unread articles.
6405         (when (or all (gnus-summary-find-prev))
6406           (gnus-summary-catchup all t beg)))))
6407   (gnus-summary-position-point))
6408
6409 (defun gnus-summary-catchup-all (&optional quietly)
6410   "Mark all articles in this newsgroup as read."
6411   (interactive "P")
6412   (gnus-set-global-variables)
6413   (gnus-summary-catchup t quietly))
6414
6415 (defun gnus-summary-catchup-and-exit (&optional all quietly)
6416   "Mark all articles not marked as unread in this newsgroup as read, then exit.
6417 If prefix argument ALL is non-nil, all articles are marked as read."
6418   (interactive "P")
6419   (gnus-set-global-variables)
6420   (gnus-summary-catchup all quietly nil 'fast)
6421   ;; Select next newsgroup or exit.
6422   (if (eq gnus-auto-select-next 'quietly)
6423       (gnus-summary-next-group nil)
6424     (gnus-summary-exit)))
6425
6426 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
6427   "Mark all articles in this newsgroup as read, and then exit."
6428   (interactive "P")
6429   (gnus-set-global-variables)
6430   (gnus-summary-catchup-and-exit t quietly))
6431
6432 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
6433 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
6434   "Mark all articles in this group as read and select the next group.
6435 If given a prefix, mark all articles, unread as well as ticked, as
6436 read."
6437   (interactive "P")
6438   (gnus-set-global-variables)
6439   (save-excursion
6440     (gnus-summary-catchup all))
6441   (gnus-summary-next-article t nil nil t))
6442
6443 ;; Thread-based commands.
6444
6445 (defun gnus-summary-articles-in-thread (&optional article)
6446   "Return a list of all articles in the current thread.
6447 If ARTICLE is non-nil, return all articles in the thread that starts
6448 with that article."
6449   (let* ((article (or article (gnus-summary-article-number)))
6450          (data (gnus-data-find-list article))
6451          (top-level (gnus-data-level (car data)))
6452          (top-subject
6453           (cond ((null gnus-thread-operation-ignore-subject)
6454                  (gnus-simplify-subject-re
6455                   (mail-header-subject (gnus-data-header (car data)))))
6456                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
6457                  (gnus-simplify-subject-fuzzy
6458                   (mail-header-subject (gnus-data-header (car data)))))
6459                 (t nil)))
6460          (end-point (save-excursion
6461                       (if (gnus-summary-go-to-next-thread) 
6462                           (point) (point-max))))
6463          articles)
6464     (while (and data
6465                 (< (gnus-data-pos (car data)) end-point))
6466       (when (or (not top-subject)
6467                 (string= top-subject
6468                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
6469                              (gnus-simplify-subject-fuzzy
6470                               (mail-header-subject
6471                                (gnus-data-header (car data))))
6472                            (gnus-simplify-subject-re
6473                             (mail-header-subject
6474                              (gnus-data-header (car data)))))))
6475         (push (gnus-data-number (car data)) articles))
6476       (unless (and (setq data (cdr data))
6477                    (> (gnus-data-level (car data)) top-level))
6478         (setq data nil)))
6479     ;; Return the list of articles.
6480     (nreverse articles)))
6481
6482 (defun gnus-summary-rethread-current ()
6483   "Rethread the thread the current article is part of."
6484   (interactive)
6485   (gnus-set-global-variables)
6486   (let* ((gnus-show-threads t)
6487          (article (gnus-summary-article-number))
6488          (id (mail-header-id (gnus-summary-article-header)))
6489          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
6490     (unless id
6491       (error "No article on the current line"))
6492     (gnus-rebuild-thread id)
6493     (gnus-summary-goto-subject article)))
6494
6495 (defun gnus-summary-reparent-thread ()
6496   "Make current article child of the marked (or previous) article.
6497
6498 Note that the re-threading will only work if `gnus-thread-ignore-subject'
6499 is non-nil or the Subject: of both articles are the same."
6500   (interactive)
6501   (or (not (gnus-group-read-only-p))
6502       (error "The current newsgroup does not support article editing."))
6503   (or (<= (length gnus-newsgroup-processable) 1)
6504       (error "No more than one article may be marked."))
6505   (save-window-excursion
6506     (let ((gnus-article-buffer " *reparent*")
6507           (current-article (gnus-summary-article-number))
6508           ; first grab the marked article, otherwise one line up.
6509           (parent-article (if (not (null gnus-newsgroup-processable))
6510                               (car gnus-newsgroup-processable)
6511                             (save-excursion
6512                               (if (eq (forward-line -1) 0)
6513                                   (gnus-summary-article-number)
6514                                 (error "Beginning of summary buffer."))))))
6515       (or (not (eq current-article parent-article))
6516           (error "An article may not be self-referential."))
6517       (let ((message-id (mail-header-id 
6518                          (gnus-summary-article-header parent-article))))
6519         (or (and message-id (not (equal message-id "")))
6520             (error "No message-id in desired parent."))
6521         (gnus-summary-select-article t t nil current-article)
6522         (set-buffer gnus-article-buffer)
6523         (setq buffer-read-only nil)
6524         (let ((buf (format "%s" (buffer-string))))
6525           (erase-buffer)
6526           (insert buf))
6527         (goto-char (point-min))
6528         (if (search-forward-regexp "^References: " nil t)
6529             (insert message-id " " )
6530           (insert "References: " message-id "\n"))
6531         (or (gnus-request-replace-article current-article
6532                                           (car gnus-article-current)
6533                                           gnus-article-buffer)
6534             (error "Couldn't replace article."))
6535         (set-buffer gnus-summary-buffer)
6536         (gnus-summary-unmark-all-processable)
6537         (gnus-summary-rethread-current)
6538         (gnus-message 3 "Article %d is now the child of article %d."
6539                       current-article parent-article)))))
6540
6541 (defun gnus-summary-toggle-threads (&optional arg)
6542   "Toggle showing conversation threads.
6543 If ARG is positive number, turn showing conversation threads on."
6544   (interactive "P")
6545   (gnus-set-global-variables)
6546   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
6547     (setq gnus-show-threads
6548           (if (null arg) (not gnus-show-threads)
6549             (> (prefix-numeric-value arg) 0)))
6550     (gnus-summary-prepare)
6551     (gnus-summary-goto-subject current)
6552     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
6553     (gnus-summary-position-point)))
6554
6555 (defun gnus-summary-show-all-threads ()
6556   "Show all threads."
6557   (interactive)
6558   (gnus-set-global-variables)
6559   (save-excursion
6560     (let ((buffer-read-only nil))
6561       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
6562   (gnus-summary-position-point))
6563
6564 (defun gnus-summary-show-thread ()
6565   "Show thread subtrees.
6566 Returns nil if no thread was there to be shown."
6567   (interactive)
6568   (gnus-set-global-variables)
6569   (let ((buffer-read-only nil)
6570         (orig (point))
6571         ;; first goto end then to beg, to have point at beg after let
6572         (end (progn (end-of-line) (point)))
6573         (beg (progn (beginning-of-line) (point))))
6574     (prog1
6575         ;; Any hidden lines here?
6576         (search-forward "\r" end t)
6577       (subst-char-in-region beg end ?\^M ?\n t)
6578       (goto-char orig)
6579       (gnus-summary-position-point))))
6580
6581 (defun gnus-summary-hide-all-threads ()
6582   "Hide all thread subtrees."
6583   (interactive)
6584   (gnus-set-global-variables)
6585   (save-excursion
6586     (goto-char (point-min))
6587     (gnus-summary-hide-thread)
6588     (while (zerop (gnus-summary-next-thread 1 t))
6589       (gnus-summary-hide-thread)))
6590   (gnus-summary-position-point))
6591
6592 (defun gnus-summary-hide-thread ()
6593   "Hide thread subtrees.
6594 Returns nil if no threads were there to be hidden."
6595   (interactive)
6596   (gnus-set-global-variables)
6597   (let ((buffer-read-only nil)
6598         (start (point))
6599         (article (gnus-summary-article-number)))
6600     (goto-char start)
6601     ;; Go forward until either the buffer ends or the subthread
6602     ;; ends.
6603     (when (and (not (eobp))
6604                (or (zerop (gnus-summary-next-thread 1 t))
6605                    (goto-char (point-max))))
6606       (prog1
6607           (if (and (> (point) start)
6608                    (search-backward "\n" start t))
6609               (progn
6610                 (subst-char-in-region start (point) ?\n ?\^M)
6611                 (gnus-summary-goto-subject article))
6612             (goto-char start)
6613             nil)
6614         ;;(gnus-summary-position-point)
6615         ))))
6616
6617 (defun gnus-summary-go-to-next-thread (&optional previous)
6618   "Go to the same level (or less) next thread.
6619 If PREVIOUS is non-nil, go to previous thread instead.
6620 Return the article number moved to, or nil if moving was impossible."
6621   (let ((level (gnus-summary-thread-level))
6622         (way (if previous -1 1))
6623         (beg (point)))
6624     (forward-line way)
6625     (while (and (not (eobp))
6626                 (< level (gnus-summary-thread-level)))
6627       (forward-line way))
6628     (if (eobp)
6629         (progn
6630           (goto-char beg)
6631           nil)
6632       (setq beg (point))
6633       (prog1
6634           (gnus-summary-article-number)
6635         (goto-char beg)))))
6636
6637 (defun gnus-summary-go-to-next-thread-old (&optional previous)
6638   "Go to the same level (or less) next thread.
6639 If PREVIOUS is non-nil, go to previous thread instead.
6640 Return the article number moved to, or nil if moving was impossible."
6641   (if (and (eq gnus-summary-make-false-root 'dummy)
6642            (gnus-summary-article-intangible-p))
6643       (let ((beg (point)))
6644         (while (and (zerop (forward-line 1))
6645                     (not (gnus-summary-article-intangible-p))
6646                     (not (zerop (save-excursion 
6647                                   (gnus-summary-thread-level))))))
6648         (if (eobp)
6649             (progn
6650               (goto-char beg)
6651               nil)
6652           (point)))
6653     (let* ((level (gnus-summary-thread-level))
6654            (article (gnus-summary-article-number))
6655            (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
6656            oart)
6657       (while data
6658         (if (<= (gnus-data-level (car data)) level)
6659             (setq oart (gnus-data-number (car data))
6660                   data nil)
6661           (setq data (cdr data))))
6662       (and oart
6663            (gnus-summary-goto-subject oart)))))
6664
6665 (defun gnus-summary-next-thread (n &optional silent)
6666   "Go to the same level next N'th thread.
6667 If N is negative, search backward instead.
6668 Returns the difference between N and the number of skips actually
6669 done.
6670
6671 If SILENT, don't output messages."
6672   (interactive "p")
6673   (gnus-set-global-variables)
6674   (let ((backward (< n 0))
6675         (n (abs n))
6676         old dum int)
6677     (while (and (> n 0)
6678                 (gnus-summary-go-to-next-thread backward))
6679       (decf n))
6680     (unless silent 
6681       (gnus-summary-position-point))
6682     (when (and (not silent) (/= 0 n))
6683       (gnus-message 7 "No more threads"))
6684     n))
6685
6686 (defun gnus-summary-prev-thread (n)
6687   "Go to the same level previous N'th thread.
6688 Returns the difference between N and the number of skips actually
6689 done."
6690   (interactive "p")
6691   (gnus-set-global-variables)
6692   (gnus-summary-next-thread (- n)))
6693
6694 (defun gnus-summary-go-down-thread ()
6695   "Go down one level in the current thread."
6696   (let ((children (gnus-summary-article-children)))
6697     (and children
6698          (gnus-summary-goto-subject (car children)))))
6699
6700 (defun gnus-summary-go-up-thread ()
6701   "Go up one level in the current thread."
6702   (let ((parent (gnus-summary-article-parent)))
6703     (and parent
6704          (gnus-summary-goto-subject parent))))
6705
6706 (defun gnus-summary-down-thread (n)
6707   "Go down thread N steps.
6708 If N is negative, go up instead.
6709 Returns the difference between N and how many steps down that were
6710 taken."
6711   (interactive "p")
6712   (gnus-set-global-variables)
6713   (let ((up (< n 0))
6714         (n (abs n)))
6715     (while (and (> n 0)
6716                 (if up (gnus-summary-go-up-thread)
6717                   (gnus-summary-go-down-thread)))
6718       (setq n (1- n)))
6719     (gnus-summary-position-point)
6720     (if (/= 0 n) (gnus-message 7 "Can't go further"))
6721     n))
6722
6723 (defun gnus-summary-up-thread (n)
6724   "Go up thread N steps.
6725 If N is negative, go up instead.
6726 Returns the difference between N and how many steps down that were
6727 taken."
6728   (interactive "p")
6729   (gnus-set-global-variables)
6730   (gnus-summary-down-thread (- n)))
6731
6732 (defun gnus-summary-top-thread ()
6733   "Go to the top of the thread."
6734   (interactive)
6735   (gnus-set-global-variables)
6736   (while (gnus-summary-go-up-thread))
6737   (gnus-summary-article-number))
6738
6739 (defun gnus-summary-kill-thread (&optional unmark)
6740   "Mark articles under current thread as read.
6741 If the prefix argument is positive, remove any kinds of marks.
6742 If the prefix argument is negative, tick articles instead."
6743   (interactive "P")
6744   (gnus-set-global-variables)
6745   (when unmark
6746     (setq unmark (prefix-numeric-value unmark)))
6747   (let ((articles (gnus-summary-articles-in-thread)))
6748     (save-excursion
6749       ;; Expand the thread.
6750       (gnus-summary-show-thread)
6751       ;; Mark all the articles.
6752       (while articles
6753         (gnus-summary-goto-subject (car articles))
6754         (cond ((null unmark)
6755                (gnus-summary-mark-article-as-read gnus-killed-mark))
6756               ((> unmark 0)
6757                (gnus-summary-mark-article-as-unread gnus-unread-mark))
6758               (t
6759                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
6760         (setq articles (cdr articles))))
6761     ;; Hide killed subtrees.
6762     (and (null unmark)
6763          gnus-thread-hide-killed
6764          (gnus-summary-hide-thread))
6765     ;; If marked as read, go to next unread subject.
6766     (if (null unmark)
6767         ;; Go to next unread subject.
6768         (gnus-summary-next-subject 1 t)))
6769   (gnus-set-mode-line 'summary))
6770
6771 ;; Summary sorting commands
6772
6773 (defun gnus-summary-sort-by-number (&optional reverse)
6774   "Sort summary buffer by article number.
6775 Argument REVERSE means reverse order."
6776   (interactive "P")
6777   (gnus-summary-sort 'number reverse))
6778
6779 (defun gnus-summary-sort-by-author (&optional reverse)
6780   "Sort summary buffer by author name alphabetically.
6781 If case-fold-search is non-nil, case of letters is ignored.
6782 Argument REVERSE means reverse order."
6783   (interactive "P")
6784   (gnus-summary-sort 'author reverse))
6785
6786 (defun gnus-summary-sort-by-subject (&optional reverse)
6787   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
6788 If case-fold-search is non-nil, case of letters is ignored.
6789 Argument REVERSE means reverse order."
6790   (interactive "P")
6791   (gnus-summary-sort 'subject reverse))
6792
6793 (defun gnus-summary-sort-by-date (&optional reverse)
6794   "Sort summary buffer by date.
6795 Argument REVERSE means reverse order."
6796   (interactive "P")
6797   (gnus-summary-sort 'date reverse))
6798
6799 (defun gnus-summary-sort-by-score (&optional reverse)
6800   "Sort summary buffer by score.
6801 Argument REVERSE means reverse order."
6802   (interactive "P")
6803   (gnus-summary-sort 'score reverse))
6804
6805 (defun gnus-summary-sort (predicate reverse)
6806   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
6807   (gnus-set-global-variables)
6808   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
6809          (article (intern (format "gnus-article-sort-by-%s" predicate)))
6810          (gnus-thread-sort-functions
6811           (list
6812            (if (not reverse)
6813                thread
6814              `(lambda (t1 t2)
6815                 (,thread t2 t1)))))
6816          (gnus-article-sort-functions
6817           (list
6818            (if (not reverse)
6819                article
6820              `(lambda (t1 t2)
6821                 (,article t2 t1)))))
6822          (buffer-read-only)
6823          (gnus-summary-prepare-hook nil))
6824     ;; We do the sorting by regenerating the threads.
6825     (gnus-summary-prepare)
6826     ;; Hide subthreads if needed.
6827     (when (and gnus-show-threads gnus-thread-hide-subtree)
6828       (gnus-summary-hide-all-threads))))
6829
6830 ;; Summary saving commands.
6831
6832 (defun gnus-summary-save-article (&optional n not-saved)
6833   "Save the current article using the default saver function.
6834 If N is a positive number, save the N next articles.
6835 If N is a negative number, save the N previous articles.
6836 If N is nil and any articles have been marked with the process mark,
6837 save those articles instead.
6838 The variable `gnus-default-article-saver' specifies the saver function."
6839   (interactive "P")
6840   (gnus-set-global-variables)
6841   (let ((articles (gnus-summary-work-articles n))
6842         (save-buffer (save-excursion 
6843                        (nnheader-set-temp-buffer " *Gnus Save*")))
6844         header article file)
6845     (while articles
6846       (setq header (gnus-summary-article-header
6847                     (setq article (pop articles))))
6848       (if (not (vectorp header))
6849           ;; This is a pseudo-article.
6850           (if (assq 'name header)
6851               (gnus-copy-file (cdr (assq 'name header)))
6852             (gnus-message 1 "Article %d is unsaveable" article))
6853         ;; This is a real article.
6854         (save-window-excursion
6855           (gnus-summary-select-article t nil nil article))
6856         (save-excursion
6857           (set-buffer save-buffer)
6858           (erase-buffer)
6859           (insert-buffer-substring gnus-original-article-buffer))
6860         (setq file (gnus-article-save save-buffer file))
6861         (gnus-summary-remove-process-mark article)
6862         (unless not-saved
6863           (gnus-summary-set-saved-mark article))))
6864     (gnus-kill-buffer save-buffer)
6865     (gnus-summary-position-point)
6866     n))
6867
6868 (defun gnus-summary-pipe-output (&optional arg)
6869   "Pipe the current article to a subprocess.
6870 If N is a positive number, pipe the N next articles.
6871 If N is a negative number, pipe the N previous articles.
6872 If N is nil and any articles have been marked with the process mark,
6873 pipe those articles instead."
6874   (interactive "P")
6875   (gnus-set-global-variables)
6876   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
6877     (gnus-summary-save-article arg t))
6878   (gnus-configure-windows 'pipe))
6879
6880 (defun gnus-summary-save-article-mail (&optional arg)
6881   "Append the current article to an mail file.
6882 If N is a positive number, save the N next articles.
6883 If N is a negative number, save the N previous articles.
6884 If N is nil and any articles have been marked with the process mark,
6885 save those articles instead."
6886   (interactive "P")
6887   (gnus-set-global-variables)
6888   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
6889     (gnus-summary-save-article arg)))
6890
6891 (defun gnus-summary-save-article-rmail (&optional arg)
6892   "Append the current article to an rmail file.
6893 If N is a positive number, save the N next articles.
6894 If N is a negative number, save the N previous articles.
6895 If N is nil and any articles have been marked with the process mark,
6896 save those articles instead."
6897   (interactive "P")
6898   (gnus-set-global-variables)
6899   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
6900     (gnus-summary-save-article arg)))
6901
6902 (defun gnus-summary-save-article-file (&optional arg)
6903   "Append the current article to a file.
6904 If N is a positive number, save the N next articles.
6905 If N is a negative number, save the N previous articles.
6906 If N is nil and any articles have been marked with the process mark,
6907 save those articles instead."
6908   (interactive "P")
6909   (gnus-set-global-variables)
6910   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
6911     (gnus-summary-save-article arg)))
6912
6913 (defun gnus-summary-save-article-body-file (&optional arg)
6914   "Append the current article body to a file.
6915 If N is a positive number, save the N next articles.
6916 If N is a negative number, save the N previous articles.
6917 If N is nil and any articles have been marked with the process mark,
6918 save those articles instead."
6919   (interactive "P")
6920   (gnus-set-global-variables)
6921   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
6922     (gnus-summary-save-article arg)))
6923
6924 (defun gnus-get-split-value (methods)
6925   "Return a value based on the split METHODS."
6926   (let (split-name method result match)
6927     (when methods
6928       (save-excursion
6929         (set-buffer gnus-original-article-buffer)
6930         (save-restriction
6931           (nnheader-narrow-to-headers)
6932           (while methods
6933             (goto-char (point-min))
6934             (setq method (pop methods))
6935             (setq match (car method))
6936             (when (cond
6937                    ((stringp match)
6938                     ;; Regular expression.
6939                     (condition-case ()
6940                         (re-search-forward match nil t)
6941                       (error nil)))
6942                    ((gnus-functionp match)
6943                     ;; Function.
6944                     (save-restriction
6945                       (widen)
6946                       (setq result (funcall match gnus-newsgroup-name))))
6947                    ((consp match)
6948                     ;; Form.
6949                     (save-restriction
6950                       (widen)
6951                       (setq result (eval match)))))
6952               (setq split-name (append (cdr method) split-name))
6953               (cond ((stringp result)
6954                      (push result split-name))
6955                     ((consp result)
6956                      (setq split-name (append result split-name)))))))))
6957     split-name))
6958
6959 (defun gnus-read-move-group-name (prompt default articles prefix)
6960   "Read a group name."
6961   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
6962          (minibuffer-confirm-incomplete nil) ; XEmacs
6963          group-map
6964          (dum (mapatoms
6965                (lambda (g) 
6966                  (and (boundp g)
6967                       (symbol-name g)
6968                       (memq 'respool
6969                             (assoc (symbol-name
6970                                     (car (gnus-find-method-for-group
6971                                           (symbol-name g))))
6972                                    gnus-valid-select-methods))
6973                       (push (list (symbol-name g)) group-map)))
6974                gnus-active-hashtb))
6975          (prom
6976           (format "%s %s to:"
6977                   prompt
6978                   (if (> (length articles) 1)
6979                       (format "these %d articles" (length articles))
6980                     "this article")))
6981          (to-newsgroup
6982           (cond
6983            ((null split-name)
6984             (gnus-completing-read default prom
6985                                   group-map nil nil prefix
6986                                   'gnus-group-history))
6987            ((= 1 (length split-name))
6988             (gnus-completing-read (car split-name) prom group-map
6989                                   nil nil nil
6990                                   'gnus-group-history))
6991            (t
6992             (gnus-completing-read nil prom 
6993                                   (mapcar (lambda (el) (list el))
6994                                           (nreverse split-name))
6995                                   nil nil nil
6996                                   'gnus-group-history)))))
6997     (when to-newsgroup
6998       (if (or (string= to-newsgroup "")
6999               (string= to-newsgroup prefix))
7000           (setq to-newsgroup (or default "")))
7001       (or (gnus-active to-newsgroup)
7002           (gnus-activate-group to-newsgroup)
7003           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
7004                                      to-newsgroup))
7005               (or (and (gnus-request-create-group 
7006                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
7007                        (gnus-activate-group to-newsgroup nil nil
7008                                             (gnus-group-name-to-method
7009                                              to-newsgroup)))
7010                   (error "Couldn't create group %s" to-newsgroup)))
7011           (error "No such group: %s" to-newsgroup)))
7012     to-newsgroup))
7013
7014 ;; Summary extract commands
7015
7016 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
7017   (let ((buffer-read-only nil)
7018         (article (gnus-summary-article-number))
7019         after-article b e)
7020     (or (gnus-summary-goto-subject article)
7021         (error (format "No such article: %d" article)))
7022     (gnus-summary-position-point)
7023     ;; If all commands are to be bunched up on one line, we collect
7024     ;; them here.
7025     (if gnus-view-pseudos-separately
7026         ()
7027       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
7028             files action)
7029         (while ps
7030           (setq action (cdr (assq 'action (car ps))))
7031           (setq files (list (cdr (assq 'name (car ps)))))
7032           (while (and ps (cdr ps)
7033                       (string= (or action "1")
7034                                (or (cdr (assq 'action (cadr ps))) "2")))
7035             (setq files (cons (cdr (assq 'name (cadr ps))) files))
7036             (setcdr ps (cddr ps)))
7037           (if (not files)
7038               ()
7039             (if (not (string-match "%s" action))
7040                 (setq files (cons " " files)))
7041             (setq files (cons " " files))
7042             (and (assq 'execute (car ps))
7043                  (setcdr (assq 'execute (car ps))
7044                          (funcall (if (string-match "%s" action)
7045                                       'format 'concat)
7046                                   action
7047                                   (mapconcat (lambda (f) f) files " ")))))
7048           (setq ps (cdr ps)))))
7049     (if (and gnus-view-pseudos (not not-view))
7050         (while pslist
7051           (and (assq 'execute (car pslist))
7052                (gnus-execute-command (cdr (assq 'execute (car pslist)))
7053                                      (eq gnus-view-pseudos 'not-confirm)))
7054           (setq pslist (cdr pslist)))
7055       (save-excursion
7056         (while pslist
7057           (setq after-article (or (cdr (assq 'article (car pslist)))
7058                                   (gnus-summary-article-number)))
7059           (gnus-summary-goto-subject after-article)
7060           (forward-line 1)
7061           (setq b (point))
7062           (insert "    " (file-name-nondirectory
7063                                 (cdr (assq 'name (car pslist))))
7064                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
7065           (setq e (point))
7066           (forward-line -1)             ; back to `b'
7067           (gnus-add-text-properties
7068            b (1- e) (list 'gnus-number gnus-reffed-article-number
7069                           gnus-mouse-face-prop gnus-mouse-face))
7070           (gnus-data-enter
7071            after-article gnus-reffed-article-number
7072            gnus-unread-mark b (car pslist) 0 (- e b))
7073           (push gnus-reffed-article-number gnus-newsgroup-unreads)
7074           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
7075           (setq pslist (cdr pslist)))))))
7076
7077 (defun gnus-pseudos< (p1 p2)
7078   (let ((c1 (cdr (assq 'action p1)))
7079         (c2 (cdr (assq 'action p2))))
7080     (and c1 c2 (string< c1 c2))))
7081
7082 (defun gnus-request-pseudo-article (props)
7083   (cond ((assq 'execute props)
7084          (gnus-execute-command (cdr (assq 'execute props)))))
7085   (let ((gnus-current-article (gnus-summary-article-number)))
7086     (run-hooks 'gnus-mark-article-hook)))
7087
7088 (defun gnus-execute-command (command &optional automatic)
7089   (save-excursion
7090     (gnus-article-setup-buffer)
7091     (set-buffer gnus-article-buffer)
7092     (setq buffer-read-only nil)
7093     (let ((command (if automatic command (read-string "Command: " command)))
7094           ;; Just binding this here doesn't help, because there might
7095           ;; be output from the process after exiting the scope of 
7096           ;; this `let'.
7097           ;; (buffer-read-only nil)
7098           )
7099       (erase-buffer)
7100       (insert "$ " command "\n\n")
7101       (if gnus-view-pseudo-asynchronously
7102           (start-process "gnus-execute" nil shell-file-name
7103                          shell-command-switch command)
7104         (call-process shell-file-name nil t nil
7105                       shell-command-switch command)))))
7106
7107 ;; Summary kill commands.
7108
7109 (defun gnus-summary-edit-global-kill (article)
7110   "Edit the \"global\" kill file."
7111   (interactive (list (gnus-summary-article-number)))
7112   (gnus-set-global-variables)
7113   (gnus-group-edit-global-kill article))
7114
7115 (defun gnus-summary-edit-local-kill ()
7116   "Edit a local kill file applied to the current newsgroup."
7117   (interactive)
7118   (gnus-set-global-variables)
7119   (setq gnus-current-headers (gnus-summary-article-header))
7120   (gnus-set-global-variables)
7121   (gnus-group-edit-local-kill
7122    (gnus-summary-article-number) gnus-newsgroup-name))
7123
7124 ;;; Header reading.
7125
7126 (defun gnus-read-header (id &optional header)
7127   "Read the headers of article ID and enter them into the Gnus system."
7128   (let ((group gnus-newsgroup-name)
7129         (gnus-override-method 
7130          (and (gnus-news-group-p gnus-newsgroup-name)
7131               gnus-refer-article-method))       
7132         where)
7133     ;; First we check to see whether the header in question is already
7134     ;; fetched.
7135     (if (stringp id)
7136         ;; This is a Message-ID.
7137         (setq header (or header (gnus-id-to-header id)))
7138       ;; This is an article number.
7139       (setq header (or header (gnus-summary-article-header id))))
7140     (if (and header
7141              (not (memq (mail-header-number header) gnus-newsgroup-sparse)))
7142         ;; We have found the header.
7143         header
7144       ;; We have to really fetch the header to this article.
7145       (when (setq where (gnus-request-head id group))
7146         (save-excursion
7147           (set-buffer nntp-server-buffer)
7148           (goto-char (point-max))
7149           (insert ".\n")
7150           (goto-char (point-min))
7151           (insert "211 ")
7152           (princ (cond
7153                   ((numberp id) id)
7154                   ((cdr where) (cdr where))
7155                   (header (mail-header-number header))
7156                   (t gnus-reffed-article-number))
7157                  (current-buffer))
7158           (insert " Article retrieved.\n"))
7159         ;(when (and header
7160         ;          (memq (mail-header-number header) gnus-newsgroup-sparse))
7161         ;  (setcar (gnus-id-to-thread id) nil))
7162         (if (not (setq header (car (gnus-get-newsgroup-headers))))
7163             ()                          ; Malformed head.
7164           (unless (memq (mail-header-number header) gnus-newsgroup-sparse)
7165             (if (and (stringp id)
7166                      (not (string= (gnus-group-real-name group)
7167                                    (car where))))
7168                 ;; If we fetched by Message-ID and the article came
7169                 ;; from a different group, we fudge some bogus article
7170                 ;; numbers for this article.
7171                 (mail-header-set-number header gnus-reffed-article-number))
7172             (decf gnus-reffed-article-number)
7173             (gnus-remove-header (mail-header-number header))
7174             (push header gnus-newsgroup-headers)
7175             (setq gnus-current-headers header)
7176             (push (mail-header-number header) gnus-newsgroup-limit))
7177           header)))))
7178
7179 (defun gnus-remove-header (number)
7180   "Remove header NUMBER from `gnus-newsgroup-headers'."
7181   (if (and gnus-newsgroup-headers
7182            (= number (mail-header-number (car gnus-newsgroup-headers))))
7183       (pop gnus-newsgroup-headers)
7184     (let ((headers gnus-newsgroup-headers))
7185       (while (and (cdr headers)
7186                   (not (= number (mail-header-number (cadr headers)))))
7187         (pop headers))
7188       (when (cdr headers)
7189         (setcdr headers (cddr headers))))))
7190
7191 (provide 'gnus-sum)
7192
7193 ;;; gnus-sum.el ends here