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