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