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