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