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