Sync up with Pterodactyl Gnus v0.83.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   ;; Avoid byte-compile warnings.
44   (defvar gnus-article-decoded-p)
45   (defvar gnus-decode-encoded-word-function)
46   )
47
48 (eval-and-compile
49   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, marking commands will go to the next unread article.
186 If `never', commands that usually go to the next unread article, will
187 go to the next article, whether it is read or not.
188 If nil, only the marking commands will go to the next (un)read article."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-auto-select-next t
289   "*If non-nil, offer to go to the next group from the end of the previous.
290 If the value is t and the next newsgroup is empty, Gnus will exit
291 summary mode and go back to group mode.  If the value is neither nil
292 nor t, Gnus will select the following unread newsgroup.  In
293 particular, if the value is the symbol `quietly', the next unread
294 newsgroup will be selected without any confirmation, and if it is
295 `almost-quietly', the next group will be selected without any
296 confirmation if you are located on the last article in the group.
297 Finally, if this variable is `slightly-quietly', the `Z n' command
298 will go to the next group without confirmation."
299   :group 'gnus-summary-maneuvering
300   :type '(choice (const :tag "off" nil)
301                  (const quietly)
302                  (const almost-quietly)
303                  (const slightly-quietly)
304                  (sexp :menu-tag "on" t)))
305
306 (defcustom gnus-auto-select-same nil
307   "*If non-nil, select the next article with the same subject.
308 If there are no more articles with the same subject, go to
309 the first unread article."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-summary-check-current nil
314   "*If non-nil, consider the current article when moving.
315 The \"unread\" movement commands will stay on the same line if the
316 current article is unread."
317   :group 'gnus-summary-maneuvering
318   :type 'boolean)
319
320 (defcustom gnus-auto-center-summary t
321   "*If non-nil, always center the current summary buffer.
322 In particular, if `vertical' do only vertical recentering.  If non-nil
323 and non-`vertical', do both horizontal and vertical recentering."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "none" nil)
326                  (const vertical)
327                  (integer :tag "height")
328                  (sexp :menu-tag "both" t)))
329
330 (defcustom gnus-show-all-headers nil
331   "*If non-nil, don't hide any headers."
332   :group 'gnus-article-hiding
333   :group 'gnus-article-headers
334   :type 'boolean)
335
336 (defcustom gnus-summary-ignore-duplicates nil
337   "*If non-nil, ignore articles with identical Message-ID headers."
338   :group 'gnus-summary
339   :type 'boolean)
340
341 (defcustom gnus-single-article-buffer t
342   "*If non-nil, display all articles in the same buffer.
343 If nil, each group will get its own article buffer."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-break-pages t
348   "*If non-nil, do page breaking on articles.
349 The page delimiter is specified by the `gnus-page-delimiter'
350 variable."
351   :group 'gnus-article-various
352   :type 'boolean)
353
354 (defcustom gnus-show-mime t
355   "*If non-nil, do mime processing of articles.
356 The articles will simply be fed to the function given by
357 `gnus-article-display-method-for-mime'."
358   :group 'gnus-article-mime
359   :type 'boolean)
360
361 (defcustom gnus-move-split-methods nil
362   "*Variable used to suggest where articles are to be moved to.
363 It uses the same syntax as the `gnus-split-methods' variable."
364   :group 'gnus-summary-mail
365   :type '(repeat (choice (list :value (fun) function)
366                          (cons :value ("" "") regexp (repeat string))
367                          (sexp :value nil))))
368
369 (defcustom gnus-unread-mark ? ;Whitespace
370   "*Mark used for unread articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-ticked-mark ?!
375   "*Mark used for ticked articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-dormant-mark ??
380   "*Mark used for dormant articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-del-mark ?r
385   "*Mark used for del'd articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-read-mark ?R
390   "*Mark used for read articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-expirable-mark ?E
395   "*Mark used for expirable articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-killed-mark ?K
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-souped-mark ?F
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-kill-file-mark ?X
410   "*Mark used for articles killed by kill files."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-low-score-mark ?Y
415   "*Mark used for articles with a low score."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-catchup-mark ?C
420   "*Mark used for articles that are caught up."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-replied-mark ?A
425   "*Mark used for articles that have been replied to."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-cached-mark ?*
430   "*Mark used for articles that are in the cache."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-saved-mark ?S
435   "*Mark used for articles that have been saved to."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-ancient-mark ?O
440   "*Mark used for ancient articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-sparse-mark ?Q
445   "*Mark used for sparsely reffed articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-canceled-mark ?G
450   "*Mark used for canceled articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-duplicate-mark ?M
455   "*Mark used for duplicate articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-undownloaded-mark ?@
460   "*Mark used for articles that weren't downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-downloadable-mark ?%
465   "*Mark used for articles that are to be downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-unsendable-mark ?=
470   "*Mark used for articles that won't be sent."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-over-mark ?+
475   "*Score mark used for articles with high scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-below-mark ?-
480   "*Score mark used for articles with low scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-empty-thread-mark ? ;Whitespace
485   "*There is no thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-not-empty-thread-mark ?=
490   "*There is a thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-view-pseudo-asynchronously nil
495   "*If non-nil, Gnus will view pseudo-articles asynchronously."
496   :group 'gnus-extract-view
497   :type 'boolean)
498
499 (defcustom gnus-auto-expirable-marks
500   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
501         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
502         gnus-souped-mark gnus-duplicate-mark)
503   "*The list of marks converted into expiration if a group is auto-expirable."
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :group 'gnus-summary
510   :type 'boolean)
511
512 (defcustom gnus-view-pseudos nil
513   "*If `automatic', pseudo-articles will be viewed automatically.
514 If `not-confirm', pseudos will be viewed automatically, and the user
515 will not be asked to confirm the command."
516   :group 'gnus-extract-view
517   :type '(choice (const :tag "off" nil)
518                  (const automatic)
519                  (const not-confirm)))
520
521 (defcustom gnus-view-pseudos-separately t
522   "*If non-nil, one pseudo-article will be created for each file to be viewed.
523 If nil, all files that use the same viewing command will be given as a
524 list of parameters to that command."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-insert-pseudo-articles t
529   "*If non-nil, insert pseudo-articles when decoding articles."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-summary-dummy-line-format
534   "  %(:                          :%) %S\n"
535   "*The format specification for the dummy roots in the summary buffer.
536 It works along the same lines as a normal formatting string,
537 with some simple extensions.
538
539 %S  The subject"
540   :group 'gnus-threading
541   :type 'string)
542
543 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
544   "*The format specification for the summary mode line.
545 It works along the same lines as a normal formatting string,
546 with some simple extensions:
547
548 %G  Group name
549 %p  Unprefixed group name
550 %A  Current article number
551 %z  Current article score
552 %V  Gnus version
553 %U  Number of unread articles in the group
554 %e  Number of unselected articles in the group
555 %Z  A string with unread/unselected article counts
556 %g  Shortish group name
557 %S  Subject of the current article
558 %u  User-defined spec
559 %s  Current score file name
560 %d  Number of dormant articles
561 %r  Number of articles that have been marked as read in this session
562 %E  Number of articles expunged by the score files"
563   :group 'gnus-summary-format
564   :type 'string)
565
566 (defcustom gnus-summary-mark-below 0
567   "*Mark all articles with a score below this variable as read.
568 This variable is local to each summary buffer and usually set by the
569 score file."
570   :group 'gnus-score-default
571   :type 'integer)
572
573 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
574   "*List of functions used for sorting articles in the summary buffer.
575 This variable is only used when not using a threaded display."
576   :group 'gnus-summary-sort
577   :type '(repeat (choice (function-item gnus-article-sort-by-number)
578                          (function-item gnus-article-sort-by-author)
579                          (function-item gnus-article-sort-by-subject)
580                          (function-item gnus-article-sort-by-date)
581                          (function-item gnus-article-sort-by-score)
582                          (function :tag "other"))))
583
584 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
585   "*List of functions used for sorting threads in the summary buffer.
586 By default, threads are sorted by article number.
587
588 Each function takes two threads and return non-nil if the first thread
589 should be sorted before the other.  If you use more than one function,
590 the primary sort function should be the last.  You should probably
591 always include `gnus-thread-sort-by-number' in the list of sorting
592 functions -- preferably first.
593
594 Ready-made functions include `gnus-thread-sort-by-number',
595 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
596 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
597 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
600                          (function-item gnus-thread-sort-by-author)
601                          (function-item gnus-thread-sort-by-subject)
602                          (function-item gnus-thread-sort-by-date)
603                          (function-item gnus-thread-sort-by-score)
604                          (function-item gnus-thread-sort-by-total-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-score-function '+
608   "*Function used for calculating the total score of a thread.
609
610 The function is called with the scores of the article and each
611 subthread and should then return the score of the thread.
612
613 Some functions you can use are `+', `max', or `min'."
614   :group 'gnus-summary-sort
615   :type 'function)
616
617 (defcustom gnus-summary-expunge-below nil
618   "All articles that have a score less than this variable will be expunged.
619 This variable is local to the summary buffers."
620   :group 'gnus-score-default
621   :type '(choice (const :tag "off" nil)
622                  integer))
623
624 (defcustom gnus-thread-expunge-below nil
625   "All threads that have a total score less than this variable will be expunged.
626 See `gnus-thread-score-function' for en explanation of what a
627 \"thread score\" is.
628
629 This variable is local to the summary buffers."
630   :group 'gnus-threading
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-summary-mode-hook nil
636   "*A hook for Gnus summary mode.
637 This hook is run before any variables are set in the summary buffer."
638   :group 'gnus-summary-various
639   :type 'hook)
640
641 (defcustom gnus-summary-menu-hook nil
642   "*Hook run after the creation of the summary mode menu."
643   :group 'gnus-summary-visual
644   :type 'hook)
645
646 (defcustom gnus-summary-exit-hook nil
647   "*A hook called on exit from the summary buffer.
648 It will be called with point in the group buffer."
649   :group 'gnus-summary-exit
650   :type 'hook)
651
652 (defcustom gnus-summary-prepare-hook nil
653   "*A hook called after the summary buffer has been generated.
654 If you want to modify the summary buffer, you can use this hook."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-prepared-hook nil
659   "*A hook called as the last thing after the summary buffer has been generated."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-generate-hook nil
664   "*A hook run just before generating the summary buffer.
665 This hook is commonly used to customize threading variables and the
666 like."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-select-group-hook nil
671   "*A hook called when a newsgroup is selected.
672
673 If you'd like to simplify subjects like the
674 `gnus-summary-next-same-subject' command does, you can use the
675 following hook:
676
677  (setq gnus-select-group-hook
678       (list
679         (lambda ()
680           (mapcar (lambda (header)
681                      (mail-header-set-subject
682                       header
683                       (gnus-simplify-subject
684                        (mail-header-subject header) 're-only)))
685                   gnus-newsgroup-headers))))"
686   :group 'gnus-group-select
687   :type 'hook)
688
689 (defcustom gnus-select-article-hook nil
690   "*A hook called when an article is selected."
691   :group 'gnus-summary-choose
692   :type 'hook)
693
694 (defcustom gnus-visual-mark-article-hook
695   (list 'gnus-highlight-selected-summary)
696   "*Hook run after selecting an article in the summary buffer.
697 It is meant to be used for highlighting the article in some way.  It
698 is not run if `gnus-visual' is nil."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
703   "*A hook called before parsing the headers."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-exit-group-hook nil
708   "*A hook called when exiting (not quitting) summary mode."
709   :group 'gnus-various
710   :type 'hook)
711
712 (defcustom gnus-summary-update-hook
713   (list 'gnus-summary-highlight-line)
714   "*A hook called when a summary line is changed.
715 The hook will not be called if `gnus-visual' is nil.
716
717 The default function `gnus-summary-highlight-line' will
718 highlight the line according to the `gnus-summary-highlight'
719 variable."
720   :group 'gnus-summary-visual
721   :type 'hook)
722
723 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
724   "*A hook called when an article is selected for the first time.
725 The hook is intended to mark an article as read (or unread)
726 automatically when it is selected."
727   :group 'gnus-summary-choose
728   :type 'hook)
729
730 (defcustom gnus-group-no-more-groups-hook nil
731   "*A hook run when returning to group mode having no more (unread) groups."
732   :group 'gnus-group-select
733   :type 'hook)
734
735 (defcustom gnus-ps-print-hook nil
736   "*A hook run before ps-printing something from Gnus."
737   :group 'gnus-summary
738   :type 'hook)
739
740 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
741   "Face used for highlighting the current article in the summary buffer."
742   :group 'gnus-summary-visual
743   :type 'face)
744
745 (defcustom gnus-summary-highlight
746   '(((= mark gnus-canceled-mark)
747      . gnus-summary-cancelled-face)
748     ((and (> score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-high-ticked-face)
752     ((and (< score default)
753           (or (= mark gnus-dormant-mark)
754               (= mark gnus-ticked-mark)))
755      . gnus-summary-low-ticked-face)
756     ((or (= mark gnus-dormant-mark)
757          (= mark gnus-ticked-mark))
758      . gnus-summary-normal-ticked-face)
759     ((and (> score default) (= mark gnus-ancient-mark))
760      . gnus-summary-high-ancient-face)
761     ((and (< score default) (= mark gnus-ancient-mark))
762      . gnus-summary-low-ancient-face)
763     ((= mark gnus-ancient-mark)
764      . gnus-summary-normal-ancient-face)
765     ((and (> score default) (= mark gnus-unread-mark))
766      . gnus-summary-high-unread-face)
767     ((and (< score default) (= mark gnus-unread-mark))
768      . gnus-summary-low-unread-face)
769     ((= mark gnus-unread-mark)
770      . gnus-summary-normal-unread-face)
771     ((and (> score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-high-unread-face)
774     ((and (< score default) (memq mark (list gnus-downloadable-mark
775                                              gnus-undownloaded-mark)))
776      . gnus-summary-low-unread-face)
777     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
778      . gnus-summary-normal-unread-face)
779     ((> score default)
780      . gnus-summary-high-read-face)
781     ((< score default)
782      . gnus-summary-low-read-face)
783     (t
784      . gnus-summary-normal-read-face))
785   "*Controls the highlighting of summary buffer lines.
786
787 A list of (FORM . FACE) pairs.  When deciding how a a particular
788 summary line should be displayed, each form is evaluated.  The content
789 of the face field after the first true form is used.  You can change
790 how those summary lines are displayed, by editing the face field.
791
792 You can use the following variables in the FORM field.
793
794 score:   The articles score
795 default: The default article score.
796 below:   The score below which articles are automatically marked as read.
797 mark:    The articles mark."
798   :group 'gnus-summary-visual
799   :type '(repeat (cons (sexp :tag "Form" nil)
800                        face)))
801
802 (defcustom gnus-alter-header-function nil
803   "Function called to allow alteration of article header structures.
804 The function is called with one parameter, the article header vector,
805 which it may alter in any way.")
806
807 (defcustom gnus-extra-headers nil
808   "*Extra headers to parse."
809   :group 'gnus-summary
810   :type '(repeat symbol))
811
812 (defcustom gnus-ignored-from-addresses
813   (and user-mail-address (regexp-quote user-mail-address))
814   "*Regexp of From headers that may be suppressed in favor of To headers."
815   :group 'gnus-summary
816   :type 'regexp)
817
818 (defcustom gnus-group-charset-alist
819   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
820     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
821     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
822     ("^relcom\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
827     (".*" iso-8859-1))
828   "Alist of regexps (to match group names) and default charsets to be used when reading."
829   :type '(repeat (list (regexp :tag "Group")
830                        (symbol :tag "Charset")))
831   :group 'gnus-charset)
832
833 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
834   "List of charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat symbol)
838   :group 'gnus-charset)
839
840 ;;; Internal variables
841
842 (defvar gnus-scores-exclude-files nil)
843 (defvar gnus-page-broken nil)
844 (defvar gnus-inhibit-mime-unbuttonizing nil)
845
846 (defvar gnus-original-article nil)
847 (defvar gnus-article-internal-prepare-hook nil)
848 (defvar gnus-newsgroup-process-stack nil)
849
850 (defvar gnus-thread-indent-array nil)
851 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
852
853 ;; Avoid highlighting in kill files.
854 (defvar gnus-summary-inhibit-highlight nil)
855 (defvar gnus-newsgroup-selected-overlay nil)
856 (defvar gnus-inhibit-limiting nil)
857 (defvar gnus-newsgroup-adaptive-score-file nil)
858 (defvar gnus-current-score-file nil)
859 (defvar gnus-current-move-group nil)
860 (defvar gnus-current-copy-group nil)
861 (defvar gnus-current-crosspost-group nil)
862
863 (defvar gnus-newsgroup-dependencies nil)
864 (defvar gnus-newsgroup-adaptive nil)
865 (defvar gnus-summary-display-article-function nil)
866 (defvar gnus-summary-highlight-line-function nil
867   "Function called after highlighting a summary line.")
868
869 (defvar gnus-summary-line-format-alist
870   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
871     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
872     (?s gnus-tmp-subject-or-nil ?s)
873     (?n gnus-tmp-name ?s)
874     (?A (std11-address-string
875          (car (mime-read-field 'From gnus-tmp-header))) ?s)
876     (?a (or (std11-full-name-string
877              (car (mime-read-field 'From gnus-tmp-header)))
878             gnus-tmp-from) ?s)
879     (?F gnus-tmp-from ?s)
880     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
881     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
882     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
883     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
884     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
885     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
886     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
887     (?L gnus-tmp-lines ?d)
888     (?I gnus-tmp-indentation ?s)
889     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
890     (?R gnus-tmp-replied ?c)
891     (?\[ gnus-tmp-opening-bracket ?c)
892     (?\] gnus-tmp-closing-bracket ?c)
893     (?\> (make-string gnus-tmp-level ? ) ?s)
894     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
895     (?i gnus-tmp-score ?d)
896     (?z gnus-tmp-score-char ?c)
897     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
898     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
899     (?U gnus-tmp-unread ?c)
900     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
901     (?t (gnus-summary-number-of-articles-in-thread
902          (and (boundp 'thread) (car thread)) gnus-tmp-level)
903         ?d)
904     (?e (gnus-summary-number-of-articles-in-thread
905          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
906         ?c)
907     (?u gnus-tmp-user-defined ?s)
908     (?P (gnus-pick-line-number) ?d))
909   "An alist of format specifications that can appear in summary lines,
910 and what variables they correspond with, along with the type of the
911 variable (string, integer, character, etc).")
912
913 (defvar gnus-summary-dummy-line-format-alist
914   `((?S gnus-tmp-subject ?s)
915     (?N gnus-tmp-number ?d)
916     (?u gnus-tmp-user-defined ?s)))
917
918 (defvar gnus-summary-mode-line-format-alist
919   `((?G gnus-tmp-group-name ?s)
920     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
921     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
922     (?A gnus-tmp-article-number ?d)
923     (?Z gnus-tmp-unread-and-unselected ?s)
924     (?V gnus-version ?s)
925     (?U gnus-tmp-unread-and-unticked ?d)
926     (?S gnus-tmp-subject ?s)
927     (?e gnus-tmp-unselected ?d)
928     (?u gnus-tmp-user-defined ?s)
929     (?d (length gnus-newsgroup-dormant) ?d)
930     (?t (length gnus-newsgroup-marked) ?d)
931     (?r (length gnus-newsgroup-reads) ?d)
932     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
933     (?E gnus-newsgroup-expunged-tally ?d)
934     (?s (gnus-current-score-file-nondirectory) ?s)))
935
936 (defvar gnus-last-search-regexp nil
937   "Default regexp for article search command.")
938
939 (defvar gnus-last-shell-command nil
940   "Default shell command on article.")
941
942 (defvar gnus-newsgroup-begin nil)
943 (defvar gnus-newsgroup-end nil)
944 (defvar gnus-newsgroup-last-rmail nil)
945 (defvar gnus-newsgroup-last-mail nil)
946 (defvar gnus-newsgroup-last-folder nil)
947 (defvar gnus-newsgroup-last-file nil)
948 (defvar gnus-newsgroup-auto-expire nil)
949 (defvar gnus-newsgroup-active nil)
950
951 (defvar gnus-newsgroup-data nil)
952 (defvar gnus-newsgroup-data-reverse nil)
953 (defvar gnus-newsgroup-limit nil)
954 (defvar gnus-newsgroup-limits nil)
955
956 (defvar gnus-newsgroup-unreads nil
957   "List of unread articles in the current newsgroup.")
958
959 (defvar gnus-newsgroup-unselected nil
960   "List of unselected unread articles in the current newsgroup.")
961
962 (defvar gnus-newsgroup-reads nil
963   "Alist of read articles and article marks in the current newsgroup.")
964
965 (defvar gnus-newsgroup-expunged-tally nil)
966
967 (defvar gnus-newsgroup-marked nil
968   "List of ticked articles in the current newsgroup (a subset of unread art).")
969
970 (defvar gnus-newsgroup-killed nil
971   "List of ranges of articles that have been through the scoring process.")
972
973 (defvar gnus-newsgroup-cached nil
974   "List of articles that come from the article cache.")
975
976 (defvar gnus-newsgroup-saved nil
977   "List of articles that have been saved.")
978
979 (defvar gnus-newsgroup-kill-headers nil)
980
981 (defvar gnus-newsgroup-replied nil
982   "List of articles that have been replied to in the current newsgroup.")
983
984 (defvar gnus-newsgroup-expirable nil
985   "List of articles in the current newsgroup that can be expired.")
986
987 (defvar gnus-newsgroup-processable nil
988   "List of articles in the current newsgroup that can be processed.")
989
990 (defvar gnus-newsgroup-downloadable nil
991   "List of articles in the current newsgroup that can be processed.")
992
993 (defvar gnus-newsgroup-undownloaded nil
994   "List of articles in the current newsgroup that haven't been downloaded..")
995
996 (defvar gnus-newsgroup-unsendable nil
997   "List of articles in the current newsgroup that won't be sent.")
998
999 (defvar gnus-newsgroup-bookmarks nil
1000   "List of articles in the current newsgroup that have bookmarks.")
1001
1002 (defvar gnus-newsgroup-dormant nil
1003   "List of dormant articles in the current newsgroup.")
1004
1005 (defvar gnus-newsgroup-scored nil
1006   "List of scored articles in the current newsgroup.")
1007
1008 (defvar gnus-newsgroup-headers nil
1009   "List of article headers in the current newsgroup.")
1010
1011 (defvar gnus-newsgroup-threads nil)
1012
1013 (defvar gnus-newsgroup-prepared nil
1014   "Whether the current group has been prepared properly.")
1015
1016 (defvar gnus-newsgroup-ancient nil
1017   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1018
1019 (defvar gnus-newsgroup-sparse nil)
1020
1021 (defvar gnus-current-article nil)
1022 (defvar gnus-article-current nil)
1023 (defvar gnus-current-headers nil)
1024 (defvar gnus-have-all-headers nil)
1025 (defvar gnus-last-article nil)
1026 (defvar gnus-newsgroup-history nil)
1027 (defvar gnus-newsgroup-charset nil)
1028
1029 (defconst gnus-summary-local-variables
1030   '(gnus-newsgroup-name
1031     gnus-newsgroup-begin gnus-newsgroup-end
1032     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1033     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1034     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1035     gnus-newsgroup-unselected gnus-newsgroup-marked
1036     gnus-newsgroup-reads gnus-newsgroup-saved
1037     gnus-newsgroup-replied gnus-newsgroup-expirable
1038     gnus-newsgroup-processable gnus-newsgroup-killed
1039     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1040     gnus-newsgroup-unsendable
1041     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1042     gnus-newsgroup-headers gnus-newsgroup-threads
1043     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1044     gnus-current-article gnus-current-headers gnus-have-all-headers
1045     gnus-last-article gnus-article-internal-prepare-hook
1046     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1047     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1048     gnus-thread-expunge-below
1049     gnus-score-alist gnus-current-score-file
1050     (gnus-summary-expunge-below . global)
1051     (gnus-summary-mark-below . global)
1052     gnus-newsgroup-active gnus-scores-exclude-files
1053     gnus-newsgroup-history gnus-newsgroup-ancient
1054     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1055     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1056     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1057     (gnus-newsgroup-expunged-tally . 0)
1058     gnus-cache-removable-articles gnus-newsgroup-cached
1059     gnus-newsgroup-data gnus-newsgroup-data-reverse
1060     gnus-newsgroup-limit gnus-newsgroup-limits
1061     gnus-newsgroup-charset)
1062   "Variables that are buffer-local to the summary buffers.")
1063
1064 ;; Byte-compiler warning.
1065 (defvar gnus-article-mode-map)
1066
1067 ;; Subject simplification.
1068
1069 (defun gnus-simplify-whitespace (str)
1070   "Remove excessive whitespace."
1071   (let ((mystr str))
1072     ;; Multiple spaces.
1073     (while (string-match "[ \t][ \t]+" mystr)
1074       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1075                           " "
1076                           (substring mystr (match-end 0)))))
1077     ;; Leading spaces.
1078     (when (string-match "^[ \t]+" mystr)
1079       (setq mystr (substring mystr (match-end 0))))
1080     ;; Trailing spaces.
1081     (when (string-match "[ \t]+$" mystr)
1082       (setq mystr (substring mystr 0 (match-beginning 0))))
1083     mystr))
1084
1085 (defsubst gnus-simplify-subject-re (subject)
1086   "Remove \"Re:\" from subject lines."
1087   (if (string-match "^[Rr][Ee]: *" subject)
1088       (substring subject (match-end 0))
1089     subject))
1090
1091 (defun gnus-simplify-subject (subject &optional re-only)
1092   "Remove `Re:' and words in parentheses.
1093 If RE-ONLY is non-nil, strip leading `Re:'s only."
1094   (let ((case-fold-search t))           ;Ignore case.
1095     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1096     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1097       (setq subject (substring subject (match-end 0))))
1098     ;; Remove uninteresting prefixes.
1099     (when (and (not re-only)
1100                gnus-simplify-ignored-prefixes
1101                (string-match gnus-simplify-ignored-prefixes subject))
1102       (setq subject (substring subject (match-end 0))))
1103     ;; Remove words in parentheses from end.
1104     (unless re-only
1105       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1106         (setq subject (substring subject 0 (match-beginning 0)))))
1107     ;; Return subject string.
1108     subject))
1109
1110 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1111 ;; all whitespace.
1112 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1113   (goto-char (point-min))
1114   (while (re-search-forward regexp nil t)
1115       (replace-match (or newtext ""))))
1116
1117 (defun gnus-simplify-buffer-fuzzy ()
1118   "Simplify string in the buffer fuzzily.
1119 The string in the accessible portion of the current buffer is simplified.
1120 It is assumed to be a single-line subject.
1121 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1122 matter is removed.  Additional things can be deleted by setting
1123 gnus-simplify-subject-fuzzy-regexp."
1124   (let ((case-fold-search t)
1125         (modified-tick))
1126     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1127
1128     (while (not (eq modified-tick (buffer-modified-tick)))
1129       (setq modified-tick (buffer-modified-tick))
1130       (cond
1131        ((listp gnus-simplify-subject-fuzzy-regexp)
1132         (mapcar 'gnus-simplify-buffer-fuzzy-step
1133                 gnus-simplify-subject-fuzzy-regexp))
1134        (gnus-simplify-subject-fuzzy-regexp
1135         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1136       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1137       (gnus-simplify-buffer-fuzzy-step
1138        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1139       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1140
1141     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1142     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1143     (gnus-simplify-buffer-fuzzy-step " $")
1144     (gnus-simplify-buffer-fuzzy-step "^ +")))
1145
1146 (defun gnus-simplify-subject-fuzzy (subject)
1147   "Simplify a subject string fuzzily.
1148 See `gnus-simplify-buffer-fuzzy' for details."
1149   (save-excursion
1150     (gnus-set-work-buffer)
1151     (let ((case-fold-search t))
1152       ;; Remove uninteresting prefixes.
1153       (when (and gnus-simplify-ignored-prefixes
1154                  (string-match gnus-simplify-ignored-prefixes subject))
1155         (setq subject (substring subject (match-end 0))))
1156       (insert subject)
1157       (inline (gnus-simplify-buffer-fuzzy))
1158       (buffer-string))))
1159
1160 (defsubst gnus-simplify-subject-fully (subject)
1161   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1162   (cond
1163    (gnus-simplify-subject-functions
1164     (gnus-map-function gnus-simplify-subject-functions subject))
1165    ((null gnus-summary-gather-subject-limit)
1166     (gnus-simplify-subject-re subject))
1167    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1168     (gnus-simplify-subject-fuzzy subject))
1169    ((numberp gnus-summary-gather-subject-limit)
1170     (gnus-limit-string (gnus-simplify-subject-re subject)
1171                        gnus-summary-gather-subject-limit))
1172    (t
1173     subject)))
1174
1175 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1176   "Check whether two subjects are equal.
1177 If optional argument simple-first is t, first argument is already
1178 simplified."
1179   (cond
1180    ((null simple-first)
1181     (equal (gnus-simplify-subject-fully s1)
1182            (gnus-simplify-subject-fully s2)))
1183    (t
1184     (equal s1
1185            (gnus-simplify-subject-fully s2)))))
1186
1187 (defun gnus-summary-bubble-group ()
1188   "Increase the score of the current group.
1189 This is a handy function to add to `gnus-summary-exit-hook' to
1190 increase the score of each group you read."
1191   (gnus-group-add-score gnus-newsgroup-name))
1192
1193 \f
1194 ;;;
1195 ;;; Gnus summary mode
1196 ;;;
1197
1198 (put 'gnus-summary-mode 'mode-class 'special)
1199
1200 (when t
1201   ;; Non-orthogonal keys
1202
1203   (gnus-define-keys gnus-summary-mode-map
1204     " " gnus-summary-next-page
1205     "\177" gnus-summary-prev-page
1206     [delete] gnus-summary-prev-page
1207     [backspace] gnus-summary-prev-page
1208     "\r" gnus-summary-scroll-up
1209     "\M-\r" gnus-summary-scroll-down
1210     "n" gnus-summary-next-unread-article
1211     "p" gnus-summary-prev-unread-article
1212     "N" gnus-summary-next-article
1213     "P" gnus-summary-prev-article
1214     "\M-\C-n" gnus-summary-next-same-subject
1215     "\M-\C-p" gnus-summary-prev-same-subject
1216     "\M-n" gnus-summary-next-unread-subject
1217     "\M-p" gnus-summary-prev-unread-subject
1218     "." gnus-summary-first-unread-article
1219     "," gnus-summary-best-unread-article
1220     "\M-s" gnus-summary-search-article-forward
1221     "\M-r" gnus-summary-search-article-backward
1222     "<" gnus-summary-beginning-of-article
1223     ">" gnus-summary-end-of-article
1224     "j" gnus-summary-goto-article
1225     "^" gnus-summary-refer-parent-article
1226     "\M-^" gnus-summary-refer-article
1227     "u" gnus-summary-tick-article-forward
1228     "!" gnus-summary-tick-article-forward
1229     "U" gnus-summary-tick-article-backward
1230     "d" gnus-summary-mark-as-read-forward
1231     "D" gnus-summary-mark-as-read-backward
1232     "E" gnus-summary-mark-as-expirable
1233     "\M-u" gnus-summary-clear-mark-forward
1234     "\M-U" gnus-summary-clear-mark-backward
1235     "k" gnus-summary-kill-same-subject-and-select
1236     "\C-k" gnus-summary-kill-same-subject
1237     "\M-\C-k" gnus-summary-kill-thread
1238     "\M-\C-l" gnus-summary-lower-thread
1239     "e" gnus-summary-edit-article
1240     "#" gnus-summary-mark-as-processable
1241     "\M-#" gnus-summary-unmark-as-processable
1242     "\M-\C-t" gnus-summary-toggle-threads
1243     "\M-\C-s" gnus-summary-show-thread
1244     "\M-\C-h" gnus-summary-hide-thread
1245     "\M-\C-f" gnus-summary-next-thread
1246     "\M-\C-b" gnus-summary-prev-thread
1247     "\M-\C-u" gnus-summary-up-thread
1248     "\M-\C-d" gnus-summary-down-thread
1249     "&" gnus-summary-execute-command
1250     "c" gnus-summary-catchup-and-exit
1251     "\C-w" gnus-summary-mark-region-as-read
1252     "\C-t" gnus-summary-toggle-truncation
1253     "?" gnus-summary-mark-as-dormant
1254     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1255     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1256     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1257     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1258     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1259     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1260     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1261     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1262     "=" gnus-summary-expand-window
1263     "\C-x\C-s" gnus-summary-reselect-current-group
1264     "\M-g" gnus-summary-rescan-group
1265     "w" gnus-summary-stop-page-breaking
1266     "\C-c\C-r" gnus-summary-caesar-message
1267     "\M-t" gnus-summary-toggle-mime
1268     "f" gnus-summary-followup
1269     "F" gnus-summary-followup-with-original
1270     "C" gnus-summary-cancel-article
1271     "r" gnus-summary-reply
1272     "R" gnus-summary-reply-with-original
1273     "\C-c\C-f" gnus-summary-mail-forward
1274     "o" gnus-summary-save-article
1275     "\C-o" gnus-summary-save-article-mail
1276     "|" gnus-summary-pipe-output
1277     "\M-k" gnus-summary-edit-local-kill
1278     "\M-K" gnus-summary-edit-global-kill
1279     ;; "V" gnus-version
1280     "\C-c\C-d" gnus-summary-describe-group
1281     "q" gnus-summary-exit
1282     "Q" gnus-summary-exit-no-update
1283     "\C-c\C-i" gnus-info-find-node
1284     gnus-mouse-2 gnus-mouse-pick-article
1285     "m" gnus-summary-mail-other-window
1286     "a" gnus-summary-post-news
1287     "x" gnus-summary-limit-to-unread
1288     "s" gnus-summary-isearch-article
1289     "t" gnus-article-hide-headers
1290     "g" gnus-summary-show-article
1291     "l" gnus-summary-goto-last-article
1292     "v" gnus-summary-preview-mime-message
1293     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1294     "\C-d" gnus-summary-enter-digest-group
1295     "\M-\C-d" gnus-summary-read-document
1296     "\M-\C-e" gnus-summary-edit-parameters
1297     "\M-\C-g" gnus-summary-customize-parameters
1298     "\C-c\C-b" gnus-bug
1299     "*" gnus-cache-enter-article
1300     "\M-*" gnus-cache-remove-article
1301     "\M-&" gnus-summary-universal-argument
1302     "\C-l" gnus-recenter
1303     "I" gnus-summary-increase-score
1304     "L" gnus-summary-lower-score
1305     "\M-i" gnus-symbolic-argument
1306     "h" gnus-summary-select-article-buffer
1307
1308     "V" gnus-summary-score-map
1309     "X" gnus-uu-extract-map
1310     "S" gnus-summary-send-map)
1311
1312   ;; Sort of orthogonal keymap
1313   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1314     "t" gnus-summary-tick-article-forward
1315     "!" gnus-summary-tick-article-forward
1316     "d" gnus-summary-mark-as-read-forward
1317     "r" gnus-summary-mark-as-read-forward
1318     "c" gnus-summary-clear-mark-forward
1319     " " gnus-summary-clear-mark-forward
1320     "e" gnus-summary-mark-as-expirable
1321     "x" gnus-summary-mark-as-expirable
1322     "?" gnus-summary-mark-as-dormant
1323     "b" gnus-summary-set-bookmark
1324     "B" gnus-summary-remove-bookmark
1325     "#" gnus-summary-mark-as-processable
1326     "\M-#" gnus-summary-unmark-as-processable
1327     "S" gnus-summary-limit-include-expunged
1328     "C" gnus-summary-catchup
1329     "H" gnus-summary-catchup-to-here
1330     "\C-c" gnus-summary-catchup-all
1331     "k" gnus-summary-kill-same-subject-and-select
1332     "K" gnus-summary-kill-same-subject
1333     "P" gnus-uu-mark-map)
1334
1335   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1336     "c" gnus-summary-clear-above
1337     "u" gnus-summary-tick-above
1338     "m" gnus-summary-mark-above
1339     "k" gnus-summary-kill-below)
1340
1341   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1342     "/" gnus-summary-limit-to-subject
1343     "n" gnus-summary-limit-to-articles
1344     "w" gnus-summary-pop-limit
1345     "s" gnus-summary-limit-to-subject
1346     "a" gnus-summary-limit-to-author
1347     "u" gnus-summary-limit-to-unread
1348     "m" gnus-summary-limit-to-marks
1349     "M" gnus-summary-limit-exclude-marks
1350     "v" gnus-summary-limit-to-score
1351     "*" gnus-summary-limit-include-cached
1352     "D" gnus-summary-limit-include-dormant
1353     "T" gnus-summary-limit-include-thread
1354     "d" gnus-summary-limit-exclude-dormant
1355     "t" gnus-summary-limit-to-age
1356     "E" gnus-summary-limit-include-expunged
1357     "c" gnus-summary-limit-exclude-childless-dormant
1358     "C" gnus-summary-limit-mark-excluded-as-read)
1359
1360   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1361     "n" gnus-summary-next-unread-article
1362     "p" gnus-summary-prev-unread-article
1363     "N" gnus-summary-next-article
1364     "P" gnus-summary-prev-article
1365     "\C-n" gnus-summary-next-same-subject
1366     "\C-p" gnus-summary-prev-same-subject
1367     "\M-n" gnus-summary-next-unread-subject
1368     "\M-p" gnus-summary-prev-unread-subject
1369     "f" gnus-summary-first-unread-article
1370     "b" gnus-summary-best-unread-article
1371     "j" gnus-summary-goto-article
1372     "g" gnus-summary-goto-subject
1373     "l" gnus-summary-goto-last-article
1374     "o" gnus-summary-pop-article)
1375
1376   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1377     "k" gnus-summary-kill-thread
1378     "l" gnus-summary-lower-thread
1379     "i" gnus-summary-raise-thread
1380     "T" gnus-summary-toggle-threads
1381     "t" gnus-summary-rethread-current
1382     "^" gnus-summary-reparent-thread
1383     "s" gnus-summary-show-thread
1384     "S" gnus-summary-show-all-threads
1385     "h" gnus-summary-hide-thread
1386     "H" gnus-summary-hide-all-threads
1387     "n" gnus-summary-next-thread
1388     "p" gnus-summary-prev-thread
1389     "u" gnus-summary-up-thread
1390     "o" gnus-summary-top-thread
1391     "d" gnus-summary-down-thread
1392     "#" gnus-uu-mark-thread
1393     "\M-#" gnus-uu-unmark-thread)
1394
1395   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1396     "g" gnus-summary-prepare
1397     "c" gnus-summary-insert-cached-articles)
1398
1399   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1400     "c" gnus-summary-catchup-and-exit
1401     "C" gnus-summary-catchup-all-and-exit
1402     "E" gnus-summary-exit-no-update
1403     "Q" gnus-summary-exit
1404     "Z" gnus-summary-exit
1405     "n" gnus-summary-catchup-and-goto-next-group
1406     "R" gnus-summary-reselect-current-group
1407     "G" gnus-summary-rescan-group
1408     "N" gnus-summary-next-group
1409     "s" gnus-summary-save-newsrc
1410     "P" gnus-summary-prev-group)
1411
1412   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1413     " " gnus-summary-next-page
1414     "n" gnus-summary-next-page
1415     "\177" gnus-summary-prev-page
1416     [delete] gnus-summary-prev-page
1417     "p" gnus-summary-prev-page
1418     "\r" gnus-summary-scroll-up
1419     "\M-\r" gnus-summary-scroll-down
1420     "<" gnus-summary-beginning-of-article
1421     ">" gnus-summary-end-of-article
1422     "b" gnus-summary-beginning-of-article
1423     "e" gnus-summary-end-of-article
1424     "^" gnus-summary-refer-parent-article
1425     "r" gnus-summary-refer-parent-article
1426     "R" gnus-summary-refer-references
1427     "T" gnus-summary-refer-thread
1428     "g" gnus-summary-show-article
1429     "s" gnus-summary-isearch-article
1430     "P" gnus-summary-print-article)
1431
1432   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1433     "b" gnus-article-add-buttons
1434     "B" gnus-article-add-buttons-to-head
1435     "o" gnus-article-treat-overstrike
1436     "e" gnus-article-emphasize
1437     "w" gnus-article-fill-cited-article
1438     "Q" gnus-article-fill-long-lines
1439     "C" gnus-article-capitalize-sentences
1440     "c" gnus-article-remove-cr
1441     "f" gnus-article-display-x-face
1442     "l" gnus-summary-stop-page-breaking
1443     "r" gnus-summary-caesar-message
1444     "t" gnus-article-hide-headers
1445     "v" gnus-summary-verbose-headers
1446     "m" gnus-summary-toggle-mime
1447     "h" gnus-article-treat-html
1448     "H" gnus-article-strip-headers-in-body
1449     "d" gnus-article-treat-dumbquotes)
1450
1451   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1452     "a" gnus-article-hide
1453     "h" gnus-article-hide-headers
1454     "b" gnus-article-hide-boring-headers
1455     "s" gnus-article-hide-signature
1456     "c" gnus-article-hide-citation
1457     "C" gnus-article-hide-citation-in-followups
1458     "p" gnus-article-hide-pgp
1459     "B" gnus-article-strip-banner
1460     "P" gnus-article-hide-pem
1461     "\C-c" gnus-article-hide-citation-maybe)
1462
1463   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1464     "a" gnus-article-highlight
1465     "h" gnus-article-highlight-headers
1466     "c" gnus-article-highlight-citation
1467     "s" gnus-article-highlight-signature)
1468
1469   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1470     "z" gnus-article-date-ut
1471     "u" gnus-article-date-ut
1472     "l" gnus-article-date-local
1473     "e" gnus-article-date-lapsed
1474     "o" gnus-article-date-original
1475     "i" gnus-article-date-iso8601
1476     "s" gnus-article-date-user)
1477
1478   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1479     "t" gnus-article-remove-trailing-blank-lines
1480     "l" gnus-article-strip-leading-blank-lines
1481     "m" gnus-article-strip-multiple-blank-lines
1482     "a" gnus-article-strip-blank-lines
1483     "A" gnus-article-strip-all-blank-lines
1484     "s" gnus-article-strip-leading-space
1485     "e" gnus-article-strip-trailing-space)
1486
1487   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1488     "v" gnus-version
1489     "f" gnus-summary-fetch-faq
1490     "d" gnus-summary-describe-group
1491     "h" gnus-summary-describe-briefly
1492     "i" gnus-info-find-node)
1493
1494   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1495     "e" gnus-summary-expire-articles
1496     "\M-\C-e" gnus-summary-expire-articles-now
1497     "\177" gnus-summary-delete-article
1498     [delete] gnus-summary-delete-article
1499     "m" gnus-summary-move-article
1500     "r" gnus-summary-respool-article
1501     "w" gnus-summary-edit-article
1502     "c" gnus-summary-copy-article
1503     "B" gnus-summary-crosspost-article
1504     "q" gnus-summary-respool-query
1505     "t" gnus-summary-respool-trace
1506     "i" gnus-summary-import-article
1507     "p" gnus-summary-article-posted-p)
1508
1509   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1510     "o" gnus-summary-save-article
1511     "m" gnus-summary-save-article-mail
1512     "F" gnus-summary-write-article-file
1513     "r" gnus-summary-save-article-rmail
1514     "f" gnus-summary-save-article-file
1515     "b" gnus-summary-save-article-body-file
1516     "h" gnus-summary-save-article-folder
1517     "v" gnus-summary-save-article-vm
1518     "p" gnus-summary-pipe-output
1519     "s" gnus-soup-add-article)
1520
1521   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1522     "b" gnus-summary-display-buttonized
1523     "m" gnus-summary-repair-multipart
1524     "v" gnus-article-view-part
1525     "o" gnus-article-save-part
1526     "c" gnus-article-copy-part
1527     "e" gnus-article-externalize-part
1528     "i" gnus-article-inline-part
1529     "|" gnus-article-pipe-part)
1530   )
1531
1532 (defun gnus-summary-make-menu-bar ()
1533   (gnus-turn-off-edit-menu 'summary)
1534
1535   (unless (boundp 'gnus-summary-misc-menu)
1536
1537     (easy-menu-define
1538      gnus-summary-kill-menu gnus-summary-mode-map ""
1539      (cons
1540       "Score"
1541       (nconc
1542        (list
1543         ["Enter score..." gnus-summary-score-entry t]
1544         ["Customize" gnus-score-customize t])
1545        (gnus-make-score-map 'increase)
1546        (gnus-make-score-map 'lower)
1547        '(("Mark"
1548           ["Kill below" gnus-summary-kill-below t]
1549           ["Mark above" gnus-summary-mark-above t]
1550           ["Tick above" gnus-summary-tick-above t]
1551           ["Clear above" gnus-summary-clear-above t])
1552          ["Current score" gnus-summary-current-score t]
1553          ["Set score" gnus-summary-set-score t]
1554          ["Switch current score file..." gnus-score-change-score-file t]
1555          ["Set mark below..." gnus-score-set-mark-below t]
1556          ["Set expunge below..." gnus-score-set-expunge-below t]
1557          ["Edit current score file" gnus-score-edit-current-scores t]
1558          ["Edit score file" gnus-score-edit-file t]
1559          ["Trace score" gnus-score-find-trace t]
1560          ["Find words" gnus-score-find-favourite-words t]
1561          ["Rescore buffer" gnus-summary-rescore t]
1562          ["Increase score..." gnus-summary-increase-score t]
1563          ["Lower score..." gnus-summary-lower-score t]))))
1564
1565     ;; Define both the Article menu in the summary buffer and the equivalent
1566     ;; Commands menu in the article buffer here for consistency.
1567     (let ((innards
1568            '(("Hide"
1569               ["All" gnus-article-hide t]
1570               ["Headers" gnus-article-hide-headers t]
1571               ["Signature" gnus-article-hide-signature t]
1572               ["Citation" gnus-article-hide-citation t]
1573               ["PGP" gnus-article-hide-pgp t]
1574               ["Banner" gnus-article-strip-banner t]
1575               ["Boring headers" gnus-article-hide-boring-headers t])
1576              ("Highlight"
1577               ["All" gnus-article-highlight t]
1578               ["Headers" gnus-article-highlight-headers t]
1579               ["Signature" gnus-article-highlight-signature t]
1580               ["Citation" gnus-article-highlight-citation t])
1581              ("Date"
1582               ["Local" gnus-article-date-local t]
1583               ["ISO8601" gnus-article-date-iso8601 t]
1584               ["UT" gnus-article-date-ut t]
1585               ["Original" gnus-article-date-original t]
1586               ["Lapsed" gnus-article-date-lapsed t]
1587               ["User-defined" gnus-article-date-user t])
1588              ("Washing"
1589               ("Remove Blanks"
1590                ["Leading" gnus-article-strip-leading-blank-lines t]
1591                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1592                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1593                ["All of the above" gnus-article-strip-blank-lines t]
1594                ["All" gnus-article-strip-all-blank-lines t]
1595                ["Leading space" gnus-article-strip-leading-space t]
1596                ["Trailing space" gnus-article-strip-trailing-space t])
1597               ["Overstrike" gnus-article-treat-overstrike t]
1598               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1599               ["Emphasis" gnus-article-emphasize t]
1600               ["Word wrap" gnus-article-fill-cited-article t]
1601               ["Fill long lines" gnus-article-fill-long-lines t]
1602               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1603               ["CR" gnus-article-remove-cr t]
1604               ["Show X-Face" gnus-article-display-x-face t]
1605               ["UnHTMLize" gnus-article-treat-html t]
1606               ["Rot 13" gnus-summary-caesar-message t]
1607               ["Unix pipe" gnus-summary-pipe-message t]
1608               ["Add buttons" gnus-article-add-buttons t]
1609               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1610               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1611               ["Toggle MIME" gnus-summary-toggle-mime t]
1612               ["Verbose header" gnus-summary-verbose-headers t]
1613               ["Toggle header" gnus-summary-toggle-header t])
1614              ("Output"
1615               ["Save in default format" gnus-summary-save-article t]
1616               ["Save in file" gnus-summary-save-article-file t]
1617               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1618               ["Save in MH folder" gnus-summary-save-article-folder t]
1619               ["Save in VM folder" gnus-summary-save-article-vm t]
1620               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1621               ["Save body in file" gnus-summary-save-article-body-file t]
1622               ["Pipe through a filter" gnus-summary-pipe-output t]
1623               ["Add to SOUP packet" gnus-soup-add-article t]
1624               ["Print" gnus-summary-print-article t])
1625              ("Backend"
1626               ["Respool article..." gnus-summary-respool-article t]
1627               ["Move article..." gnus-summary-move-article
1628                (gnus-check-backend-function
1629                 'request-move-article gnus-newsgroup-name)]
1630               ["Copy article..." gnus-summary-copy-article t]
1631               ["Crosspost article..." gnus-summary-crosspost-article
1632                (gnus-check-backend-function
1633                 'request-replace-article gnus-newsgroup-name)]
1634               ["Import file..." gnus-summary-import-article t]
1635               ["Check if posted" gnus-summary-article-posted-p t]
1636               ["Edit article" gnus-summary-edit-article
1637                (not (gnus-group-read-only-p))]
1638               ["Delete article" gnus-summary-delete-article
1639                (gnus-check-backend-function
1640                 'request-expire-articles gnus-newsgroup-name)]
1641               ["Query respool" gnus-summary-respool-query t]
1642               ["Trace respool" gnus-summary-respool-trace t]
1643               ["Delete expirable articles" gnus-summary-expire-articles-now
1644                (gnus-check-backend-function
1645                 'request-expire-articles gnus-newsgroup-name)])
1646              ("Extract"
1647               ["Uudecode" gnus-uu-decode-uu t]
1648               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1649               ["Unshar" gnus-uu-decode-unshar t]
1650               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1651               ["Save" gnus-uu-decode-save t]
1652               ["Binhex" gnus-uu-decode-binhex t]
1653               ["Postscript" gnus-uu-decode-postscript t])
1654              ("Cache"
1655               ["Enter article" gnus-cache-enter-article t]
1656               ["Remove article" gnus-cache-remove-article t])
1657              ["Select article buffer" gnus-summary-select-article-buffer t]
1658              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1659              ["Isearch article..." gnus-summary-isearch-article t]
1660              ["Beginning of the article" gnus-summary-beginning-of-article t]
1661              ["End of the article" gnus-summary-end-of-article t]
1662              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1663              ["Fetch referenced articles" gnus-summary-refer-references t]
1664              ["Fetch current thread" gnus-summary-refer-thread t]
1665              ["Fetch article with id..." gnus-summary-refer-article t]
1666              ["Redisplay" gnus-summary-show-article t])))
1667       (easy-menu-define
1668        gnus-summary-article-menu gnus-summary-mode-map ""
1669        (cons "Article" innards))
1670
1671       (easy-menu-define
1672        gnus-article-commands-menu gnus-article-mode-map ""
1673        (cons "Commands" innards)))
1674
1675     (easy-menu-define
1676      gnus-summary-thread-menu gnus-summary-mode-map ""
1677      '("Threads"
1678        ["Toggle threading" gnus-summary-toggle-threads t]
1679        ["Hide threads" gnus-summary-hide-all-threads t]
1680        ["Show threads" gnus-summary-show-all-threads t]
1681        ["Hide thread" gnus-summary-hide-thread t]
1682        ["Show thread" gnus-summary-show-thread t]
1683        ["Go to next thread" gnus-summary-next-thread t]
1684        ["Go to previous thread" gnus-summary-prev-thread t]
1685        ["Go down thread" gnus-summary-down-thread t]
1686        ["Go up thread" gnus-summary-up-thread t]
1687        ["Top of thread" gnus-summary-top-thread t]
1688        ["Mark thread as read" gnus-summary-kill-thread t]
1689        ["Lower thread score" gnus-summary-lower-thread t]
1690        ["Raise thread score" gnus-summary-raise-thread t]
1691        ["Rethread current" gnus-summary-rethread-current t]
1692        ))
1693
1694     (easy-menu-define
1695      gnus-summary-post-menu gnus-summary-mode-map ""
1696      '("Post"
1697        ["Post an article" gnus-summary-post-news t]
1698        ["Followup" gnus-summary-followup t]
1699        ["Followup and yank" gnus-summary-followup-with-original t]
1700        ["Supersede article" gnus-summary-supersede-article t]
1701        ["Cancel article" gnus-summary-cancel-article t]
1702        ["Reply" gnus-summary-reply t]
1703        ["Reply and yank" gnus-summary-reply-with-original t]
1704        ["Wide reply" gnus-summary-wide-reply t]
1705        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1706        ["Mail forward" gnus-summary-mail-forward t]
1707        ["Post forward" gnus-summary-post-forward t]
1708        ["Digest and mail" gnus-summary-mail-digest t]
1709        ["Digest and post" gnus-summary-post-digest t]
1710        ["Resend message" gnus-summary-resend-message t]
1711        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1712        ["Send a mail" gnus-summary-mail-other-window t]
1713        ["Uuencode and post" gnus-uu-post-news t]
1714        ["Followup via news" gnus-summary-followup-to-mail t]
1715        ["Followup via news and yank"
1716         gnus-summary-followup-to-mail-with-original t]
1717        ;;("Draft"
1718        ;;["Send" gnus-summary-send-draft t]
1719        ;;["Send bounced" gnus-resend-bounced-mail t])
1720        ))
1721
1722     (easy-menu-define
1723      gnus-summary-misc-menu gnus-summary-mode-map ""
1724      '("Misc"
1725        ("Mark Read"
1726         ["Mark as read" gnus-summary-mark-as-read-forward t]
1727         ["Mark same subject and select"
1728          gnus-summary-kill-same-subject-and-select t]
1729         ["Mark same subject" gnus-summary-kill-same-subject t]
1730         ["Catchup" gnus-summary-catchup t]
1731         ["Catchup all" gnus-summary-catchup-all t]
1732         ["Catchup to here" gnus-summary-catchup-to-here t]
1733         ["Catchup region" gnus-summary-mark-region-as-read t]
1734         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1735        ("Mark Various"
1736         ["Tick" gnus-summary-tick-article-forward t]
1737         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1738         ["Remove marks" gnus-summary-clear-mark-forward t]
1739         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1740         ["Set bookmark" gnus-summary-set-bookmark t]
1741         ["Remove bookmark" gnus-summary-remove-bookmark t])
1742        ("Mark Limit"
1743         ["Marks..." gnus-summary-limit-to-marks t]
1744         ["Subject..." gnus-summary-limit-to-subject t]
1745         ["Author..." gnus-summary-limit-to-author t]
1746         ["Age..." gnus-summary-limit-to-age t]
1747         ["Score" gnus-summary-limit-to-score t]
1748         ["Unread" gnus-summary-limit-to-unread t]
1749         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1750         ["Articles" gnus-summary-limit-to-articles t]
1751         ["Pop limit" gnus-summary-pop-limit t]
1752         ["Show dormant" gnus-summary-limit-include-dormant t]
1753         ["Hide childless dormant"
1754          gnus-summary-limit-exclude-childless-dormant t]
1755         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1756         ["Hide marked" gnus-summary-limit-exclude-marks t]
1757         ["Show expunged" gnus-summary-show-all-expunged t])
1758        ("Process Mark"
1759         ["Set mark" gnus-summary-mark-as-processable t]
1760         ["Remove mark" gnus-summary-unmark-as-processable t]
1761         ["Remove all marks" gnus-summary-unmark-all-processable t]
1762         ["Mark above" gnus-uu-mark-over t]
1763         ["Mark series" gnus-uu-mark-series t]
1764         ["Mark region" gnus-uu-mark-region t]
1765         ["Unmark region" gnus-uu-unmark-region t]
1766         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1767         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1768         ["Mark all" gnus-uu-mark-all t]
1769         ["Mark buffer" gnus-uu-mark-buffer t]
1770         ["Mark sparse" gnus-uu-mark-sparse t]
1771         ["Mark thread" gnus-uu-mark-thread t]
1772         ["Unmark thread" gnus-uu-unmark-thread t]
1773         ("Process Mark Sets"
1774          ["Kill" gnus-summary-kill-process-mark t]
1775          ["Yank" gnus-summary-yank-process-mark
1776           gnus-newsgroup-process-stack]
1777          ["Save" gnus-summary-save-process-mark t]))
1778        ("Scroll article"
1779         ["Page forward" gnus-summary-next-page t]
1780         ["Page backward" gnus-summary-prev-page t]
1781         ["Line forward" gnus-summary-scroll-up t])
1782        ("Move"
1783         ["Next unread article" gnus-summary-next-unread-article t]
1784         ["Previous unread article" gnus-summary-prev-unread-article t]
1785         ["Next article" gnus-summary-next-article t]
1786         ["Previous article" gnus-summary-prev-article t]
1787         ["Next unread subject" gnus-summary-next-unread-subject t]
1788         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1789         ["Next article same subject" gnus-summary-next-same-subject t]
1790         ["Previous article same subject" gnus-summary-prev-same-subject t]
1791         ["First unread article" gnus-summary-first-unread-article t]
1792         ["Best unread article" gnus-summary-best-unread-article t]
1793         ["Go to subject number..." gnus-summary-goto-subject t]
1794         ["Go to article number..." gnus-summary-goto-article t]
1795         ["Go to the last article" gnus-summary-goto-last-article t]
1796         ["Pop article off history" gnus-summary-pop-article t])
1797        ("Sort"
1798         ["Sort by number" gnus-summary-sort-by-number t]
1799         ["Sort by author" gnus-summary-sort-by-author t]
1800         ["Sort by subject" gnus-summary-sort-by-subject t]
1801         ["Sort by date" gnus-summary-sort-by-date t]
1802         ["Sort by score" gnus-summary-sort-by-score t]
1803         ["Sort by lines" gnus-summary-sort-by-lines t]
1804         ["Sort by characters" gnus-summary-sort-by-chars t])
1805        ("Help"
1806         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1807         ["Describe group" gnus-summary-describe-group t]
1808         ["Read manual" gnus-info-find-node t])
1809        ("Modes"
1810         ["Pick and read" gnus-pick-mode t]
1811         ["Binary" gnus-binary-mode t])
1812        ("Regeneration"
1813         ["Regenerate" gnus-summary-prepare t]
1814         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1815         ["Toggle threading" gnus-summary-toggle-threads t])
1816        ["Filter articles..." gnus-summary-execute-command t]
1817        ["Run command on subjects..." gnus-summary-universal-argument t]
1818        ["Search articles forward..." gnus-summary-search-article-forward t]
1819        ["Search articles backward..." gnus-summary-search-article-backward t]
1820        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1821        ["Expand window" gnus-summary-expand-window t]
1822        ["Expire expirable articles" gnus-summary-expire-articles
1823         (gnus-check-backend-function
1824          'request-expire-articles gnus-newsgroup-name)]
1825        ["Edit local kill file" gnus-summary-edit-local-kill t]
1826        ["Edit main kill file" gnus-summary-edit-global-kill t]
1827        ["Edit group parameters" gnus-summary-edit-parameters t]
1828        ["Customize group parameters" gnus-summary-customize-parameters t]
1829        ["Send a bug report" gnus-bug t]
1830        ("Exit"
1831         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1832         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1833         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1834         ["Exit group" gnus-summary-exit t]
1835         ["Exit group without updating" gnus-summary-exit-no-update t]
1836         ["Exit and goto next group" gnus-summary-next-group t]
1837         ["Exit and goto prev group" gnus-summary-prev-group t]
1838         ["Reselect group" gnus-summary-reselect-current-group t]
1839         ["Rescan group" gnus-summary-rescan-group t]
1840         ["Update dribble" gnus-summary-save-newsrc t])))
1841
1842     (gnus-run-hooks 'gnus-summary-menu-hook)))
1843
1844 (defun gnus-score-set-default (var value)
1845   "A version of set that updates the GNU Emacs menu-bar."
1846   (set var value)
1847   ;; It is the message that forces the active status to be updated.
1848   (message ""))
1849
1850 (defun gnus-make-score-map (type)
1851   "Make a summary score map of type TYPE."
1852   (if t
1853       nil
1854     (let ((headers '(("author" "from" string)
1855                      ("subject" "subject" string)
1856                      ("article body" "body" string)
1857                      ("article head" "head" string)
1858                      ("xref" "xref" string)
1859                      ("extra header" "extra" string)
1860                      ("lines" "lines" number)
1861                      ("followups to author" "followup" string)))
1862           (types '((number ("less than" <)
1863                            ("greater than" >)
1864                            ("equal" =))
1865                    (string ("substring" s)
1866                            ("exact string" e)
1867                            ("fuzzy string" f)
1868                            ("regexp" r))))
1869           (perms '(("temporary" (current-time-string))
1870                    ("permanent" nil)
1871                    ("immediate" now)))
1872           header)
1873       (list
1874        (apply
1875         'nconc
1876         (list
1877          (if (eq type 'lower)
1878              "Lower score"
1879            "Increase score"))
1880         (let (outh)
1881           (while headers
1882             (setq header (car headers))
1883             (setq outh
1884                   (cons
1885                    (apply
1886                     'nconc
1887                     (list (car header))
1888                     (let ((ts (cdr (assoc (nth 2 header) types)))
1889                           outt)
1890                       (while ts
1891                         (setq outt
1892                               (cons
1893                                (apply
1894                                 'nconc
1895                                 (list (caar ts))
1896                                 (let ((ps perms)
1897                                       outp)
1898                                   (while ps
1899                                     (setq outp
1900                                           (cons
1901                                            (vector
1902                                             (caar ps)
1903                                             (list
1904                                              'gnus-summary-score-entry
1905                                              (nth 1 header)
1906                                              (if (or (string= (nth 1 header)
1907                                                               "head")
1908                                                      (string= (nth 1 header)
1909                                                               "body"))
1910                                                  ""
1911                                                (list 'gnus-summary-header
1912                                                      (nth 1 header)))
1913                                              (list 'quote (nth 1 (car ts)))
1914                                              (list 'gnus-score-default nil)
1915                                              (nth 1 (car ps))
1916                                              t)
1917                                             t)
1918                                            outp))
1919                                     (setq ps (cdr ps)))
1920                                   (list (nreverse outp))))
1921                                outt))
1922                         (setq ts (cdr ts)))
1923                       (list (nreverse outt))))
1924                    outh))
1925             (setq headers (cdr headers)))
1926           (list (nreverse outh))))))))
1927
1928 \f
1929
1930 (defun gnus-summary-mode (&optional group)
1931   "Major mode for reading articles.
1932
1933 All normal editing commands are switched off.
1934 \\<gnus-summary-mode-map>
1935 Each line in this buffer represents one article.  To read an
1936 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1937 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1938 respectively.
1939
1940 You can also post articles and send mail from this buffer.  To
1941 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1942 of an article, type `\\[gnus-summary-reply]'.
1943
1944 There are approx. one gazillion commands you can execute in this
1945 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1946
1947 The following commands are available:
1948
1949 \\{gnus-summary-mode-map}"
1950   (interactive)
1951   (when (gnus-visual-p 'summary-menu 'menu)
1952     (gnus-summary-make-menu-bar))
1953   (kill-all-local-variables)
1954   (gnus-summary-make-local-variables)
1955   (gnus-make-thread-indent-array)
1956   (gnus-simplify-mode-line)
1957   (setq major-mode 'gnus-summary-mode)
1958   (setq mode-name "Summary")
1959   (make-local-variable 'minor-mode-alist)
1960   (use-local-map gnus-summary-mode-map)
1961   (buffer-disable-undo)
1962   (setq buffer-read-only t)             ;Disable modification
1963   (setq truncate-lines t)
1964   (setq selective-display t)
1965   (setq selective-display-ellipses t)   ;Display `...'
1966   (gnus-summary-set-display-table)
1967   (gnus-set-default-directory)
1968   (setq gnus-newsgroup-name group)
1969   (make-local-variable 'gnus-summary-line-format)
1970   (make-local-variable 'gnus-summary-line-format-spec)
1971   (make-local-variable 'gnus-summary-dummy-line-format)
1972   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1973   (make-local-variable 'gnus-summary-mark-positions)
1974   (make-local-hook 'pre-command-hook)
1975   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1976   (gnus-run-hooks 'gnus-summary-mode-hook)
1977   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1978   (gnus-update-summary-mark-positions))
1979
1980 (defun gnus-summary-make-local-variables ()
1981   "Make all the local summary buffer variables."
1982   (let (global)
1983     (dolist (local gnus-summary-local-variables)
1984       (if (consp local)
1985           (progn
1986             (if (eq (cdr local) 'global)
1987                 ;; Copy the global value of the variable.
1988                 (setq global (symbol-value (car local)))
1989               ;; Use the value from the list.
1990               (setq global (eval (cdr local))))
1991             (set (make-local-variable (car local)) global))
1992         ;; Simple nil-valued local variable.
1993         (set (make-local-variable local) nil)))))
1994
1995 (defun gnus-summary-clear-local-variables ()
1996   (let ((locals gnus-summary-local-variables))
1997     (while locals
1998       (if (consp (car locals))
1999           (and (vectorp (caar locals))
2000                (set (caar locals) nil))
2001         (and (vectorp (car locals))
2002              (set (car locals) nil)))
2003       (setq locals (cdr locals)))))
2004
2005 ;; Summary data functions.
2006
2007 (defmacro gnus-data-number (data)
2008   `(car ,data))
2009
2010 (defmacro gnus-data-set-number (data number)
2011   `(setcar ,data ,number))
2012
2013 (defmacro gnus-data-mark (data)
2014   `(nth 1 ,data))
2015
2016 (defmacro gnus-data-set-mark (data mark)
2017   `(setcar (nthcdr 1 ,data) ,mark))
2018
2019 (defmacro gnus-data-pos (data)
2020   `(nth 2 ,data))
2021
2022 (defmacro gnus-data-set-pos (data pos)
2023   `(setcar (nthcdr 2 ,data) ,pos))
2024
2025 (defmacro gnus-data-header (data)
2026   `(nth 3 ,data))
2027
2028 (defmacro gnus-data-set-header (data header)
2029   `(setf (nth 3 ,data) ,header))
2030
2031 (defmacro gnus-data-level (data)
2032   `(nth 4 ,data))
2033
2034 (defmacro gnus-data-unread-p (data)
2035   `(= (nth 1 ,data) gnus-unread-mark))
2036
2037 (defmacro gnus-data-read-p (data)
2038   `(/= (nth 1 ,data) gnus-unread-mark))
2039
2040 (defmacro gnus-data-pseudo-p (data)
2041   `(consp (nth 3 ,data)))
2042
2043 (defmacro gnus-data-find (number)
2044   `(assq ,number gnus-newsgroup-data))
2045
2046 (defmacro gnus-data-find-list (number &optional data)
2047   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2048      (memq (assq ,number bdata)
2049            bdata)))
2050
2051 (defmacro gnus-data-make (number mark pos header level)
2052   `(list ,number ,mark ,pos ,header ,level))
2053
2054 (defun gnus-data-enter (after-article number mark pos header level offset)
2055   (let ((data (gnus-data-find-list after-article)))
2056     (unless data
2057       (error "No such article: %d" after-article))
2058     (setcdr data (cons (gnus-data-make number mark pos header level)
2059                        (cdr data)))
2060     (setq gnus-newsgroup-data-reverse nil)
2061     (gnus-data-update-list (cddr data) offset)))
2062
2063 (defun gnus-data-enter-list (after-article list &optional offset)
2064   (when list
2065     (let ((data (and after-article (gnus-data-find-list after-article)))
2066           (ilist list))
2067       (if (not (or data
2068                    after-article))
2069           (let ((odata gnus-newsgroup-data))
2070             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2071             (when offset
2072               (gnus-data-update-list odata offset)))
2073         ;; Find the last element in the list to be spliced into the main
2074         ;; list.
2075         (while (cdr list)
2076           (setq list (cdr list)))
2077         (if (not data)
2078             (progn
2079               (setcdr list gnus-newsgroup-data)
2080               (setq gnus-newsgroup-data ilist)
2081               (when offset
2082                 (gnus-data-update-list (cdr list) offset)))
2083           (setcdr list (cdr data))
2084           (setcdr data ilist)
2085           (when offset
2086             (gnus-data-update-list (cdr list) offset))))
2087       (setq gnus-newsgroup-data-reverse nil))))
2088
2089 (defun gnus-data-remove (article &optional offset)
2090   (let ((data gnus-newsgroup-data))
2091     (if (= (gnus-data-number (car data)) article)
2092         (progn
2093           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2094                 gnus-newsgroup-data-reverse nil)
2095           (when offset
2096             (gnus-data-update-list gnus-newsgroup-data offset)))
2097       (while (cdr data)
2098         (when (= (gnus-data-number (cadr data)) article)
2099           (setcdr data (cddr data))
2100           (when offset
2101             (gnus-data-update-list (cdr data) offset))
2102           (setq data nil
2103                 gnus-newsgroup-data-reverse nil))
2104         (setq data (cdr data))))))
2105
2106 (defmacro gnus-data-list (backward)
2107   `(if ,backward
2108        (or gnus-newsgroup-data-reverse
2109            (setq gnus-newsgroup-data-reverse
2110                  (reverse gnus-newsgroup-data)))
2111      gnus-newsgroup-data))
2112
2113 (defun gnus-data-update-list (data offset)
2114   "Add OFFSET to the POS of all data entries in DATA."
2115   (setq gnus-newsgroup-data-reverse nil)
2116   (while data
2117     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2118     (setq data (cdr data))))
2119
2120 (defun gnus-summary-article-pseudo-p (article)
2121   "Say whether this article is a pseudo article or not."
2122   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2123
2124 (defmacro gnus-summary-article-sparse-p (article)
2125   "Say whether this article is a sparse article or not."
2126   `(memq ,article gnus-newsgroup-sparse))
2127
2128 (defmacro gnus-summary-article-ancient-p (article)
2129   "Say whether this article is a sparse article or not."
2130   `(memq ,article gnus-newsgroup-ancient))
2131
2132 (defun gnus-article-parent-p (number)
2133   "Say whether this article is a parent or not."
2134   (let ((data (gnus-data-find-list number)))
2135     (and (cdr data)                     ; There has to be an article after...
2136          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2137             (gnus-data-level (nth 1 data))))))
2138
2139 (defun gnus-article-children (number)
2140   "Return a list of all children to NUMBER."
2141   (let* ((data (gnus-data-find-list number))
2142          (level (gnus-data-level (car data)))
2143          children)
2144     (setq data (cdr data))
2145     (while (and data
2146                 (= (gnus-data-level (car data)) (1+ level)))
2147       (push (gnus-data-number (car data)) children)
2148       (setq data (cdr data)))
2149     children))
2150
2151 (defmacro gnus-summary-skip-intangible ()
2152   "If the current article is intangible, then jump to a different article."
2153   '(let ((to (get-text-property (point) 'gnus-intangible)))
2154      (and to (gnus-summary-goto-subject to))))
2155
2156 (defmacro gnus-summary-article-intangible-p ()
2157   "Say whether this article is intangible or not."
2158   '(get-text-property (point) 'gnus-intangible))
2159
2160 (defun gnus-article-read-p (article)
2161   "Say whether ARTICLE is read or not."
2162   (not (or (memq article gnus-newsgroup-marked)
2163            (memq article gnus-newsgroup-unreads)
2164            (memq article gnus-newsgroup-unselected)
2165            (memq article gnus-newsgroup-dormant))))
2166
2167 ;; Some summary mode macros.
2168
2169 (defmacro gnus-summary-article-number ()
2170   "The article number of the article on the current line.
2171 If there isn's an article number here, then we return the current
2172 article number."
2173   '(progn
2174      (gnus-summary-skip-intangible)
2175      (or (get-text-property (point) 'gnus-number)
2176          (gnus-summary-last-subject))))
2177
2178 (defmacro gnus-summary-article-header (&optional number)
2179   "Return the header of article NUMBER."
2180   `(gnus-data-header (gnus-data-find
2181                       ,(or number '(gnus-summary-article-number)))))
2182
2183 (defmacro gnus-summary-thread-level (&optional number)
2184   "Return the level of thread that starts with article NUMBER."
2185   `(if (and (eq gnus-summary-make-false-root 'dummy)
2186             (get-text-property (point) 'gnus-intangible))
2187        0
2188      (gnus-data-level (gnus-data-find
2189                        ,(or number '(gnus-summary-article-number))))))
2190
2191 (defmacro gnus-summary-article-mark (&optional number)
2192   "Return the mark of article NUMBER."
2193   `(gnus-data-mark (gnus-data-find
2194                     ,(or number '(gnus-summary-article-number)))))
2195
2196 (defmacro gnus-summary-article-pos (&optional number)
2197   "Return the position of the line of article NUMBER."
2198   `(gnus-data-pos (gnus-data-find
2199                    ,(or number '(gnus-summary-article-number)))))
2200
2201 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2202 (defmacro gnus-summary-article-subject (&optional number)
2203   "Return current subject string or nil if nothing."
2204   `(let ((headers
2205           ,(if number
2206                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2207              '(gnus-data-header (assq (gnus-summary-article-number)
2208                                       gnus-newsgroup-data)))))
2209      (and headers
2210           (vectorp headers)
2211           (mail-header-subject headers))))
2212
2213 (defmacro gnus-summary-article-score (&optional number)
2214   "Return current article score."
2215   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2216                   gnus-newsgroup-scored))
2217        gnus-summary-default-score 0))
2218
2219 (defun gnus-summary-article-children (&optional number)
2220   "Return a list of article numbers that are children of article NUMBER."
2221   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2222          (level (gnus-data-level (car data)))
2223          l children)
2224     (while (and (setq data (cdr data))
2225                 (> (setq l (gnus-data-level (car data))) level))
2226       (and (= (1+ level) l)
2227            (push (gnus-data-number (car data))
2228                  children)))
2229     (nreverse children)))
2230
2231 (defun gnus-summary-article-parent (&optional number)
2232   "Return the article number of the parent of article NUMBER."
2233   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2234                                     (gnus-data-list t)))
2235          (level (gnus-data-level (car data))))
2236     (if (zerop level)
2237         ()                              ; This is a root.
2238       ;; We search until we find an article with a level less than
2239       ;; this one.  That function has to be the parent.
2240       (while (and (setq data (cdr data))
2241                   (not (< (gnus-data-level (car data)) level))))
2242       (and data (gnus-data-number (car data))))))
2243
2244 (defun gnus-unread-mark-p (mark)
2245   "Say whether MARK is the unread mark."
2246   (= mark gnus-unread-mark))
2247
2248 (defun gnus-read-mark-p (mark)
2249   "Say whether MARK is one of the marks that mark as read.
2250 This is all marks except unread, ticked, dormant, and expirable."
2251   (not (or (= mark gnus-unread-mark)
2252            (= mark gnus-ticked-mark)
2253            (= mark gnus-dormant-mark)
2254            (= mark gnus-expirable-mark))))
2255
2256 (defmacro gnus-article-mark (number)
2257   "Return the MARK of article NUMBER.
2258 This macro should only be used when computing the mark the \"first\"
2259 time; i.e., when generating the summary lines.  After that,
2260 `gnus-summary-article-mark' should be used to examine the
2261 marks of articles."
2262   `(cond
2263     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2264     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2265     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2266     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2267     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2268     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2269     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2270     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2271            gnus-ancient-mark))))
2272
2273 ;; Saving hidden threads.
2274
2275 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2276 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2277
2278 (defmacro gnus-save-hidden-threads (&rest forms)
2279   "Save hidden threads, eval FORMS, and restore the hidden threads."
2280   (let ((config (make-symbol "config")))
2281     `(let ((,config (gnus-hidden-threads-configuration)))
2282        (unwind-protect
2283            (save-excursion
2284              ,@forms)
2285          (gnus-restore-hidden-threads-configuration ,config)))))
2286
2287 (defun gnus-data-compute-positions ()
2288   "Compute the positions of all articles."
2289   (setq gnus-newsgroup-data-reverse nil)
2290   (let ((data gnus-newsgroup-data))
2291     (save-excursion
2292       (gnus-save-hidden-threads
2293         (gnus-summary-show-all-threads)
2294         (goto-char (point-min))
2295         (while data
2296           (while (get-text-property (point) 'gnus-intangible)
2297             (forward-line 1))
2298           (gnus-data-set-pos (car data) (+ (point) 3))
2299           (setq data (cdr data))
2300           (forward-line 1))))))
2301
2302 (defun gnus-hidden-threads-configuration ()
2303   "Return the current hidden threads configuration."
2304   (save-excursion
2305     (let (config)
2306       (goto-char (point-min))
2307       (while (search-forward "\r" nil t)
2308         (push (1- (point)) config))
2309       config)))
2310
2311 (defun gnus-restore-hidden-threads-configuration (config)
2312   "Restore hidden threads configuration from CONFIG."
2313   (let (point buffer-read-only)
2314     (while (setq point (pop config))
2315       (when (and (< point (point-max))
2316                  (goto-char point)
2317                  (eq (char-after) ?\n))
2318         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2319
2320 ;; Various summary mode internalish functions.
2321
2322 (defun gnus-mouse-pick-article (e)
2323   (interactive "e")
2324   (mouse-set-point e)
2325   (gnus-summary-next-page nil t))
2326
2327 (defun gnus-summary-set-display-table ()
2328   ;; Change the display table.  Odd characters have a tendency to mess
2329   ;; up nicely formatted displays - we make all possible glyphs
2330   ;; display only a single character.
2331
2332   ;; We start from the standard display table, if any.
2333   (let ((table (or (copy-sequence standard-display-table)
2334                    (make-display-table)))
2335         (i 32))
2336     ;; Nix out all the control chars...
2337     (while (>= (setq i (1- i)) 0)
2338       (aset table i [??]))
2339     ;; ... but not newline and cr, of course.  (cr is necessary for the
2340     ;; selective display).
2341     (aset table ?\n nil)
2342     (aset table ?\r nil)
2343     ;; We keep TAB as well.
2344     (aset table ?\t nil)
2345     ;; We nix out any glyphs over 126 that are not set already.
2346     (let ((i 256))
2347       (while (>= (setq i (1- i)) 127)
2348         ;; Only modify if the entry is nil.
2349         (unless (aref table i)
2350           (aset table i [??]))))
2351     (setq buffer-display-table table)))
2352
2353 (defun gnus-summary-setup-buffer (group)
2354   "Initialize summary buffer."
2355   (let ((buffer (concat "*Summary " group "*")))
2356     (if (get-buffer buffer)
2357         (progn
2358           (set-buffer buffer)
2359           (setq gnus-summary-buffer (current-buffer))
2360           (not gnus-newsgroup-prepared))
2361       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2362       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2363       (gnus-summary-mode group)
2364       (when gnus-carpal
2365         (gnus-carpal-setup-buffer 'summary))
2366       (unless gnus-single-article-buffer
2367         (make-local-variable 'gnus-article-buffer)
2368         (make-local-variable 'gnus-article-current)
2369         (make-local-variable 'gnus-original-article-buffer))
2370       (setq gnus-newsgroup-name group)
2371       t)))
2372
2373 (defun gnus-set-global-variables ()
2374   ;; Set the global equivalents of the summary buffer-local variables
2375   ;; to the latest values they had.  These reflect the summary buffer
2376   ;; that was in action when the last article was fetched.
2377   (when (eq major-mode 'gnus-summary-mode)
2378     (setq gnus-summary-buffer (current-buffer))
2379     (let ((name gnus-newsgroup-name)
2380           (marked gnus-newsgroup-marked)
2381           (unread gnus-newsgroup-unreads)
2382           (headers gnus-current-headers)
2383           (data gnus-newsgroup-data)
2384           (summary gnus-summary-buffer)
2385           (article-buffer gnus-article-buffer)
2386           (original gnus-original-article-buffer)
2387           (gac gnus-article-current)
2388           (reffed gnus-reffed-article-number)
2389           (score-file gnus-current-score-file)
2390           (default-charset gnus-newsgroup-charset))
2391       (save-excursion
2392         (set-buffer gnus-group-buffer)
2393         (setq gnus-newsgroup-name name
2394               gnus-newsgroup-marked marked
2395               gnus-newsgroup-unreads unread
2396               gnus-current-headers headers
2397               gnus-newsgroup-data data
2398               gnus-article-current gac
2399               gnus-summary-buffer summary
2400               gnus-article-buffer article-buffer
2401               gnus-original-article-buffer original
2402               gnus-reffed-article-number reffed
2403               gnus-current-score-file score-file
2404               gnus-newsgroup-charset default-charset)
2405         ;; The article buffer also has local variables.
2406         (when (gnus-buffer-live-p gnus-article-buffer)
2407           (set-buffer gnus-article-buffer)
2408           (setq gnus-summary-buffer summary))))))
2409
2410 (defun gnus-summary-article-unread-p (article)
2411   "Say whether ARTICLE is unread or not."
2412   (memq article gnus-newsgroup-unreads))
2413
2414 (defun gnus-summary-first-article-p (&optional article)
2415   "Return whether ARTICLE is the first article in the buffer."
2416   (if (not (setq article (or article (gnus-summary-article-number))))
2417       nil
2418     (eq article (caar gnus-newsgroup-data))))
2419
2420 (defun gnus-summary-last-article-p (&optional article)
2421   "Return whether ARTICLE is the last article in the buffer."
2422   (if (not (setq article (or article (gnus-summary-article-number))))
2423       ;; All non-existent numbers are the last article.  :-)
2424       t
2425     (not (cdr (gnus-data-find-list article)))))
2426
2427 (defun gnus-make-thread-indent-array ()
2428   (let ((n 200))
2429     (unless (and gnus-thread-indent-array
2430                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2431       (setq gnus-thread-indent-array (make-vector 201 "")
2432             gnus-thread-indent-array-level gnus-thread-indent-level)
2433       (while (>= n 0)
2434         (aset gnus-thread-indent-array n
2435               (make-string (* n gnus-thread-indent-level) ? ))
2436         (setq n (1- n))))))
2437
2438 (defun gnus-update-summary-mark-positions ()
2439   "Compute where the summary marks are to go."
2440   (save-excursion
2441     (when (gnus-buffer-exists-p gnus-summary-buffer)
2442       (set-buffer gnus-summary-buffer))
2443     (let ((gnus-replied-mark 129)
2444           (gnus-score-below-mark 130)
2445           (gnus-score-over-mark 130)
2446           (gnus-download-mark 131)
2447           (spec gnus-summary-line-format-spec)
2448           gnus-visual pos)
2449       (save-excursion
2450         (gnus-set-work-buffer)
2451         (let ((gnus-summary-line-format-spec spec)
2452               (gnus-newsgroup-downloadable '((0 . t))))
2453           (gnus-summary-insert-line
2454            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2455            0 nil 128 t nil "" nil 1)
2456           (goto-char (point-min))
2457           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2458                                              (- (point) 2)))))
2459           (goto-char (point-min))
2460           (push (cons 'replied (and (search-forward "\201" nil t)
2461                                     (- (point) 2)))
2462                 pos)
2463           (goto-char (point-min))
2464           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2465                 pos)
2466           (goto-char (point-min))
2467           (push (cons 'download
2468                       (and (search-forward "\203" nil t) (- (point) 2)))
2469                 pos)))
2470       (setq gnus-summary-mark-positions pos))))
2471
2472 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2473   "Insert a dummy root in the summary buffer."
2474   (beginning-of-line)
2475   (gnus-add-text-properties
2476    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2477    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2478
2479 (defun gnus-summary-from-or-to-or-newsgroups (header)
2480   (let ((to (cdr (assq 'To (mail-header-extra header))))
2481         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2482         (mail-parse-charset gnus-newsgroup-charset))
2483     (cond
2484      ((and to
2485            gnus-ignored-from-addresses
2486            (string-match gnus-ignored-from-addresses
2487                          (mail-header-from header)))
2488       (concat "-> "
2489               (or (car (funcall gnus-extract-address-components
2490                                 (funcall
2491                                  gnus-decode-encoded-word-function to)))
2492                   (funcall gnus-decode-encoded-word-function to))))
2493      ((and newsgroups
2494            gnus-ignored-from-addresses
2495            (string-match gnus-ignored-from-addresses
2496                          (mail-header-from header)))
2497       (concat "=> " newsgroups))
2498      (t
2499       (or (car (funcall gnus-extract-address-components
2500                         (mail-header-from header)))
2501           (mail-header-from header))))))
2502
2503 (defun gnus-summary-insert-line (gnus-tmp-header
2504                                  gnus-tmp-level gnus-tmp-current
2505                                  gnus-tmp-unread gnus-tmp-replied
2506                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2507                                  &optional gnus-tmp-dummy gnus-tmp-score
2508                                  gnus-tmp-process)
2509   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2510          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2511          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2512          (gnus-tmp-score-char
2513           (if (or (null gnus-summary-default-score)
2514                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2515                       gnus-summary-zcore-fuzz))
2516               ? ;Whitespace
2517             (if (< gnus-tmp-score gnus-summary-default-score)
2518                 gnus-score-below-mark gnus-score-over-mark)))
2519          (gnus-tmp-replied
2520           (cond (gnus-tmp-process gnus-process-mark)
2521                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2522                  gnus-cached-mark)
2523                 (gnus-tmp-replied gnus-replied-mark)
2524                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2525                  gnus-saved-mark)
2526                 (t gnus-unread-mark)))
2527          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2528          (gnus-tmp-name
2529           (cond
2530            ((string-match "<[^>]+> *$" gnus-tmp-from)
2531             (let ((beg (match-beginning 0)))
2532               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2533                        (substring gnus-tmp-from (1+ (match-beginning 0))
2534                                   (1- (match-end 0))))
2535                   (substring gnus-tmp-from 0 beg))))
2536            ((string-match "(.+)" gnus-tmp-from)
2537             (substring gnus-tmp-from
2538                        (1+ (match-beginning 0)) (1- (match-end 0))))
2539            (t gnus-tmp-from)))
2540          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2541          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2542          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2543          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2544          (buffer-read-only nil))
2545     (when (string= gnus-tmp-name "")
2546       (setq gnus-tmp-name gnus-tmp-from))
2547     (unless (numberp gnus-tmp-lines)
2548       (setq gnus-tmp-lines 0))
2549     (gnus-put-text-property-excluding-characters-with-faces
2550      (point)
2551      (progn (eval gnus-summary-line-format-spec) (point))
2552      'gnus-number gnus-tmp-number)
2553     (when (gnus-visual-p 'summary-highlight 'highlight)
2554       (forward-line -1)
2555       (gnus-run-hooks 'gnus-summary-update-hook)
2556       (forward-line 1))))
2557
2558 (defun gnus-summary-update-line (&optional dont-update)
2559   ;; Update summary line after change.
2560   (when (and gnus-summary-default-score
2561              (not gnus-summary-inhibit-highlight))
2562     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2563            (article (gnus-summary-article-number))
2564            (score (gnus-summary-article-score article)))
2565       (unless dont-update
2566         (if (and gnus-summary-mark-below
2567                  (< (gnus-summary-article-score)
2568                     gnus-summary-mark-below))
2569             ;; This article has a low score, so we mark it as read.
2570             (when (memq article gnus-newsgroup-unreads)
2571               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2572           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2573             ;; This article was previously marked as read on account
2574             ;; of a low score, but now it has risen, so we mark it as
2575             ;; unread.
2576             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2577         (gnus-summary-update-mark
2578          (if (or (null gnus-summary-default-score)
2579                  (<= (abs (- score gnus-summary-default-score))
2580                      gnus-summary-zcore-fuzz))
2581              ? ;Whitespace
2582            (if (< score gnus-summary-default-score)
2583                gnus-score-below-mark gnus-score-over-mark))
2584          'score))
2585       ;; Do visual highlighting.
2586       (when (gnus-visual-p 'summary-highlight 'highlight)
2587         (gnus-run-hooks 'gnus-summary-update-hook)))))
2588
2589 (defvar gnus-tmp-new-adopts nil)
2590
2591 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2592   "Return the number of articles in THREAD.
2593 This may be 0 in some cases -- if none of the articles in
2594 the thread are to be displayed."
2595   (let* ((number
2596           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2597           (cond
2598            ((not (listp thread))
2599             1)
2600            ((and (consp thread) (cdr thread))
2601             (apply
2602              '+ 1 (mapcar
2603                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2604            ((null thread)
2605             1)
2606            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2607             1)
2608            (t 0))))
2609     (when (and level (zerop level) gnus-tmp-new-adopts)
2610       (incf number
2611             (apply '+ (mapcar
2612                        'gnus-summary-number-of-articles-in-thread
2613                        gnus-tmp-new-adopts))))
2614     (if char
2615         (if (> number 1) gnus-not-empty-thread-mark
2616           gnus-empty-thread-mark)
2617       number)))
2618
2619 (defun gnus-summary-set-local-parameters (group)
2620   "Go through the local params of GROUP and set all variable specs in that list."
2621   (let ((params (gnus-group-find-parameter group))
2622         elem)
2623     (while params
2624       (setq elem (car params)
2625             params (cdr params))
2626       (and (consp elem)                 ; Has to be a cons.
2627            (consp (cdr elem))           ; The cdr has to be a list.
2628            (symbolp (car elem))         ; Has to be a symbol in there.
2629            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2630            (ignore-errors               ; So we set it.
2631              (make-local-variable (car elem))
2632              (set (car elem) (eval (nth 1 elem))))))))
2633
2634 (defun gnus-summary-read-group (group &optional show-all no-article
2635                                       kill-buffer no-display backward
2636                                       select-articles)
2637   "Start reading news in newsgroup GROUP.
2638 If SHOW-ALL is non-nil, already read articles are also listed.
2639 If NO-ARTICLE is non-nil, no article is selected initially.
2640 If NO-DISPLAY, don't generate a summary buffer."
2641   (let (result)
2642     (while (and group
2643                 (null (setq result
2644                             (let ((gnus-auto-select-next nil))
2645                               (or (gnus-summary-read-group-1
2646                                    group show-all no-article
2647                                    kill-buffer no-display
2648                                    select-articles)
2649                                   (setq show-all nil
2650                                         select-articles nil)))))
2651                 (eq gnus-auto-select-next 'quietly))
2652       (set-buffer gnus-group-buffer)
2653       ;; The entry function called above goes to the next
2654       ;; group automatically, so we go two groups back
2655       ;; if we are searching for the previous group.
2656       (when backward
2657         (gnus-group-prev-unread-group 2))
2658       (if (not (equal group (gnus-group-group-name)))
2659           (setq group (gnus-group-group-name))
2660         (setq group nil)))
2661     result))
2662
2663 (defun gnus-summary-read-group-1 (group show-all no-article
2664                                         kill-buffer no-display
2665                                         &optional select-articles)
2666   ;; Killed foreign groups can't be entered.
2667   (when (and (not (gnus-group-native-p group))
2668              (not (gnus-gethash group gnus-newsrc-hashtb)))
2669     (error "Dead non-native groups can't be entered"))
2670   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2671   (let* ((new-group (gnus-summary-setup-buffer group))
2672          (quit-config (gnus-group-quit-config group))
2673          (did-select (and new-group (gnus-select-newsgroup
2674                                      group show-all select-articles))))
2675     (cond
2676      ;; This summary buffer exists already, so we just select it.
2677      ((not new-group)
2678       (gnus-set-global-variables)
2679       (when kill-buffer
2680         (gnus-kill-or-deaden-summary kill-buffer))
2681       (gnus-configure-windows 'summary 'force)
2682       (gnus-set-mode-line 'summary)
2683       (gnus-summary-position-point)
2684       (message "")
2685       t)
2686      ;; We couldn't select this group.
2687      ((null did-select)
2688       (when (and (eq major-mode 'gnus-summary-mode)
2689                  (not (equal (current-buffer) kill-buffer)))
2690         (kill-buffer (current-buffer))
2691         (if (not quit-config)
2692             (progn
2693               ;; Update the info -- marks might need to be removed,
2694               ;; for instance.
2695               (gnus-summary-update-info)
2696               (set-buffer gnus-group-buffer)
2697               (gnus-group-jump-to-group group)
2698               (gnus-group-next-unread-group 1))
2699           (gnus-handle-ephemeral-exit quit-config)))
2700       (gnus-message 3 "Can't select group")
2701       nil)
2702      ;; The user did a `C-g' while prompting for number of articles,
2703      ;; so we exit this group.
2704      ((eq did-select 'quit)
2705       (and (eq major-mode 'gnus-summary-mode)
2706            (not (equal (current-buffer) kill-buffer))
2707            (kill-buffer (current-buffer)))
2708       (when kill-buffer
2709         (gnus-kill-or-deaden-summary kill-buffer))
2710       (if (not quit-config)
2711           (progn
2712             (set-buffer gnus-group-buffer)
2713             (gnus-group-jump-to-group group)
2714             (gnus-group-next-unread-group 1)
2715             (gnus-configure-windows 'group 'force))
2716         (gnus-handle-ephemeral-exit quit-config))
2717       ;; Finally signal the quit.
2718       (signal 'quit nil))
2719      ;; The group was successfully selected.
2720      (t
2721       (gnus-set-global-variables)
2722       ;; Save the active value in effect when the group was entered.
2723       (setq gnus-newsgroup-active
2724             (gnus-copy-sequence
2725              (gnus-active gnus-newsgroup-name)))
2726       ;; You can change the summary buffer in some way with this hook.
2727       (gnus-run-hooks 'gnus-select-group-hook)
2728       ;; Set any local variables in the group parameters.
2729       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2730       (gnus-update-format-specifications
2731        nil 'summary 'summary-mode 'summary-dummy)
2732       ;; Do score processing.
2733       (when gnus-use-scoring
2734         (gnus-possibly-score-headers))
2735       ;; Check whether to fill in the gaps in the threads.
2736       (when gnus-build-sparse-threads
2737         (gnus-build-sparse-threads))
2738       ;; Find the initial limit.
2739       (if gnus-show-threads
2740           (if show-all
2741               (let ((gnus-newsgroup-dormant nil))
2742                 (gnus-summary-initial-limit show-all))
2743             (gnus-summary-initial-limit show-all))
2744         (setq gnus-newsgroup-limit
2745               (mapcar
2746                (lambda (header) (mail-header-number header))
2747                gnus-newsgroup-headers)))
2748       ;; Generate the summary buffer.
2749       (unless no-display
2750         (gnus-summary-prepare))
2751       (when gnus-use-trees
2752         (gnus-tree-open group)
2753         (setq gnus-summary-highlight-line-function
2754               'gnus-tree-highlight-article))
2755       ;; If the summary buffer is empty, but there are some low-scored
2756       ;; articles or some excluded dormants, we include these in the
2757       ;; buffer.
2758       (when (and (zerop (buffer-size))
2759                  (not no-display))
2760         (cond (gnus-newsgroup-dormant
2761                (gnus-summary-limit-include-dormant))
2762               ((and gnus-newsgroup-scored show-all)
2763                (gnus-summary-limit-include-expunged t))))
2764       ;; Function `gnus-apply-kill-file' must be called in this hook.
2765       (gnus-run-hooks 'gnus-apply-kill-hook)
2766       (if (and (zerop (buffer-size))
2767                (not no-display))
2768           (progn
2769             ;; This newsgroup is empty.
2770             (gnus-summary-catchup-and-exit nil t)
2771             (gnus-message 6 "No unread news")
2772             (when kill-buffer
2773               (gnus-kill-or-deaden-summary kill-buffer))
2774             ;; Return nil from this function.
2775             nil)
2776         ;; Hide conversation thread subtrees.  We cannot do this in
2777         ;; gnus-summary-prepare-hook since kill processing may not
2778         ;; work with hidden articles.
2779         (and gnus-show-threads
2780              gnus-thread-hide-subtree
2781              (gnus-summary-hide-all-threads))
2782         (when kill-buffer
2783           (gnus-kill-or-deaden-summary kill-buffer))
2784         ;; Show first unread article if requested.
2785         (if (and (not no-article)
2786                  (not no-display)
2787                  gnus-newsgroup-unreads
2788                  gnus-auto-select-first)
2789             (progn
2790               (gnus-configure-windows 'summary)
2791               (cond
2792                ((eq gnus-auto-select-first 'best)
2793                 (gnus-summary-best-unread-article))
2794                ((eq gnus-auto-select-first t)
2795                 (gnus-summary-first-unread-article))
2796                ((gnus-functionp gnus-auto-select-first)
2797                 (funcall gnus-auto-select-first))))
2798           ;; Don't select any articles, just move point to the first
2799           ;; article in the group.
2800           (goto-char (point-min))
2801           (gnus-summary-position-point)
2802           (gnus-configure-windows 'summary 'force)
2803           (gnus-set-mode-line 'summary))
2804         (when (get-buffer-window gnus-group-buffer t)
2805           ;; Gotta use windows, because recenter does weird stuff if
2806           ;; the current buffer ain't the displayed window.
2807           (let ((owin (selected-window)))
2808             (select-window (get-buffer-window gnus-group-buffer t))
2809             (when (gnus-group-goto-group group)
2810               (recenter))
2811             (select-window owin)))
2812         ;; Mark this buffer as "prepared".
2813         (setq gnus-newsgroup-prepared t)
2814         (gnus-run-hooks 'gnus-summary-prepared-hook)
2815         t)))))
2816
2817 (defun gnus-summary-prepare ()
2818   "Generate the summary buffer."
2819   (interactive)
2820   (let ((buffer-read-only nil))
2821     (erase-buffer)
2822     (setq gnus-newsgroup-data nil
2823           gnus-newsgroup-data-reverse nil)
2824     (gnus-run-hooks 'gnus-summary-generate-hook)
2825     ;; Generate the buffer, either with threads or without.
2826     (when gnus-newsgroup-headers
2827       (gnus-summary-prepare-threads
2828        (if gnus-show-threads
2829            (gnus-sort-gathered-threads
2830             (funcall gnus-summary-thread-gathering-function
2831                      (gnus-sort-threads
2832                       (gnus-cut-threads (gnus-make-threads)))))
2833          ;; Unthreaded display.
2834          (gnus-sort-articles gnus-newsgroup-headers))))
2835     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2836     ;; Call hooks for modifying summary buffer.
2837     (goto-char (point-min))
2838     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2839
2840 (defsubst gnus-general-simplify-subject (subject)
2841   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2842   (setq subject
2843         (cond
2844          ;; Truncate the subject.
2845          (gnus-simplify-subject-functions
2846           (gnus-map-function gnus-simplify-subject-functions subject))
2847          ((numberp gnus-summary-gather-subject-limit)
2848           (setq subject (gnus-simplify-subject-re subject))
2849           (if (> (length subject) gnus-summary-gather-subject-limit)
2850               (substring subject 0 gnus-summary-gather-subject-limit)
2851             subject))
2852          ;; Fuzzily simplify it.
2853          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2854           (gnus-simplify-subject-fuzzy subject))
2855          ;; Just remove the leading "Re:".
2856          (t
2857           (gnus-simplify-subject-re subject))))
2858
2859   (if (and gnus-summary-gather-exclude-subject
2860            (string-match gnus-summary-gather-exclude-subject subject))
2861       nil                               ; This article shouldn't be gathered
2862     subject))
2863
2864 (defun gnus-summary-simplify-subject-query ()
2865   "Query where the respool algorithm would put this article."
2866   (interactive)
2867   (gnus-summary-select-article)
2868   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2869
2870 (defun gnus-gather-threads-by-subject (threads)
2871   "Gather threads by looking at Subject headers."
2872   (if (not gnus-summary-make-false-root)
2873       threads
2874     (let ((hashtb (gnus-make-hashtable 1024))
2875           (prev threads)
2876           (result threads)
2877           subject hthread whole-subject)
2878       (while threads
2879         (setq subject (gnus-general-simplify-subject
2880                        (setq whole-subject (mail-header-subject
2881                                             (caar threads)))))
2882         (when subject
2883           (if (setq hthread (gnus-gethash subject hashtb))
2884               (progn
2885                 ;; We enter a dummy root into the thread, if we
2886                 ;; haven't done that already.
2887                 (unless (stringp (caar hthread))
2888                   (setcar hthread (list whole-subject (car hthread))))
2889                 ;; We add this new gathered thread to this gathered
2890                 ;; thread.
2891                 (setcdr (car hthread)
2892                         (nconc (cdar hthread) (list (car threads))))
2893                 ;; Remove it from the list of threads.
2894                 (setcdr prev (cdr threads))
2895                 (setq threads prev))
2896             ;; Enter this thread into the hash table.
2897             (gnus-sethash subject threads hashtb)))
2898         (setq prev threads)
2899         (setq threads (cdr threads)))
2900       result)))
2901
2902 (defun gnus-gather-threads-by-references (threads)
2903   "Gather threads by looking at References headers."
2904   (let ((idhashtb (gnus-make-hashtable 1024))
2905         (thhashtb (gnus-make-hashtable 1024))
2906         (prev threads)
2907         (result threads)
2908         ids references id gthread gid entered ref)
2909     (while threads
2910       (when (setq references (mail-header-references (caar threads)))
2911         (setq id (mail-header-id (caar threads))
2912               ids (gnus-split-references references)
2913               entered nil)
2914         (while (setq ref (pop ids))
2915           (setq ids (delete ref ids))
2916           (if (not (setq gid (gnus-gethash ref idhashtb)))
2917               (progn
2918                 (gnus-sethash ref id idhashtb)
2919                 (gnus-sethash id threads thhashtb))
2920             (setq gthread (gnus-gethash gid thhashtb))
2921             (unless entered
2922               ;; We enter a dummy root into the thread, if we
2923               ;; haven't done that already.
2924               (unless (stringp (caar gthread))
2925                 (setcar gthread (list (mail-header-subject (caar gthread))
2926                                       (car gthread))))
2927               ;; We add this new gathered thread to this gathered
2928               ;; thread.
2929               (setcdr (car gthread)
2930                       (nconc (cdar gthread) (list (car threads)))))
2931             ;; Add it into the thread hash table.
2932             (gnus-sethash id gthread thhashtb)
2933             (setq entered t)
2934             ;; Remove it from the list of threads.
2935             (setcdr prev (cdr threads))
2936             (setq threads prev))))
2937       (setq prev threads)
2938       (setq threads (cdr threads)))
2939     result))
2940
2941 (defun gnus-sort-gathered-threads (threads)
2942   "Sort subtreads inside each gathered thread by article number."
2943   (let ((result threads))
2944     (while threads
2945       (when (stringp (caar threads))
2946         (setcdr (car threads)
2947                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2948       (setq threads (cdr threads)))
2949     result))
2950
2951 (defun gnus-thread-loop-p (root thread)
2952   "Say whether ROOT is in THREAD."
2953   (let ((stack (list thread))
2954         (infloop 0)
2955         th)
2956     (while (setq thread (pop stack))
2957       (setq th (cdr thread))
2958       (while (and th
2959                   (not (eq (caar th) root)))
2960         (pop th))
2961       (if th
2962           ;; We have found a loop.
2963           (let (ref-dep)
2964             (setcdr thread (delq (car th) (cdr thread)))
2965             (if (boundp (setq ref-dep (intern "none"
2966                                               gnus-newsgroup-dependencies)))
2967                 (setcdr (symbol-value ref-dep)
2968                         (nconc (cdr (symbol-value ref-dep))
2969                                (list (car th))))
2970               (set ref-dep (list nil (car th))))
2971             (setq infloop 1
2972                   stack nil))
2973         ;; Push all the subthreads onto the stack.
2974         (push (cdr thread) stack)))
2975     infloop))
2976
2977 (defun gnus-make-threads ()
2978   "Go through the dependency hashtb and find the roots.  Return all threads."
2979   (let (threads)
2980     (while (catch 'infloop
2981              (mapatoms
2982               (lambda (refs)
2983                 ;; Deal with self-referencing References loops.
2984                 (when (and (car (symbol-value refs))
2985                            (not (zerop
2986                                  (apply
2987                                   '+
2988                                   (mapcar
2989                                    (lambda (thread)
2990                                      (gnus-thread-loop-p
2991                                       (car (symbol-value refs)) thread))
2992                                    (cdr (symbol-value refs)))))))
2993                   (setq threads nil)
2994                   (throw 'infloop t))
2995                 (unless (car (symbol-value refs))
2996                   ;; These threads do not refer back to any other articles,
2997                   ;; so they're roots.
2998                   (setq threads (append (cdr (symbol-value refs)) threads))))
2999               gnus-newsgroup-dependencies)))
3000     threads))
3001
3002 ;; Build the thread tree.
3003 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3004   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3005
3006 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3007 if it was already present.
3008
3009 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3010 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3011 Message-IDs will be renamed be renamed to a unique Message-ID before
3012 being entered.
3013
3014 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3015   (let* ((id (mail-header-id header))
3016          (id-dep (and id (intern id dependencies)))
3017          ref ref-dep ref-header)
3018     ;; Enter this `header' in the `dependencies' table.
3019     (cond
3020      ((not id-dep)
3021       (setq header nil))
3022      ;; The first two cases do the normal part: enter a new `header'
3023      ;; in the `dependencies' table.
3024      ((not (boundp id-dep))
3025       (set id-dep (list header)))
3026      ((null (car (symbol-value id-dep)))
3027       (setcar (symbol-value id-dep) header))
3028
3029      ;; From here the `header' was already present in the
3030      ;; `dependencies' table.
3031      (force-new
3032       ;; Overrides an existing entry;
3033       ;; just set the header part of the entry.
3034       (setcar (symbol-value id-dep) header))
3035
3036      ;; Renames the existing `header' to a unique Message-ID.
3037      ((not gnus-summary-ignore-duplicates)
3038       ;; An article with this Message-ID has already been seen.
3039       ;; We rename the Message-ID.
3040       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3041            (list header))
3042       (mail-header-set-id header id))
3043
3044      ;; The last case ignores an existing entry, except it adds any
3045      ;; additional Xrefs (in case the two articles came from different
3046      ;; servers.
3047      ;; Also sets `header' to `nil' meaning that the `dependencies'
3048      ;; table was *not* modified.
3049      (t
3050       (mail-header-set-xref
3051        (car (symbol-value id-dep))
3052        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3053                    "")
3054                (or (mail-header-xref header) "")))
3055       (setq header nil)))
3056
3057     (when header
3058       ;; First check if that we are not creating a References loop.
3059       (setq ref (gnus-parent-id (mail-header-references header)))
3060       (while (and ref
3061                   (setq ref-dep (intern-soft ref dependencies))
3062                   (boundp ref-dep)
3063                   (setq ref-header (car (symbol-value ref-dep))))
3064         (if (string= id ref)
3065             ;; Yuk!  This is a reference loop.  Make the article be a
3066             ;; root article.
3067             (progn
3068               (mail-header-set-references (car (symbol-value id-dep)) "none")
3069               (setq ref nil))
3070           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3071       (setq ref (gnus-parent-id (mail-header-references header)))
3072       (setq ref-dep (intern (or ref "none") dependencies))
3073       (if (boundp ref-dep)
3074           (setcdr (symbol-value ref-dep)
3075                   (nconc (cdr (symbol-value ref-dep))
3076                          (list (symbol-value id-dep))))
3077         (set ref-dep (list nil (symbol-value id-dep)))))
3078     header))
3079
3080 (defun gnus-build-sparse-threads ()
3081   (let ((headers gnus-newsgroup-headers)
3082         (gnus-summary-ignore-duplicates t)
3083         header references generation relations
3084         subject child end new-child date)
3085     ;; First we create an alist of generations/relations, where
3086     ;; generations is how much we trust the relation, and the relation
3087     ;; is parent/child.
3088     (gnus-message 7 "Making sparse threads...")
3089     (save-excursion
3090       (nnheader-set-temp-buffer " *gnus sparse threads*")
3091       (while (setq header (pop headers))
3092         (when (and (setq references (mail-header-references header))
3093                    (not (string= references "")))
3094           (insert references)
3095           (setq child (mail-header-id header)
3096                 subject (mail-header-subject header)
3097                 date (mail-header-date header)
3098                 generation 0)
3099           (while (search-backward ">" nil t)
3100             (setq end (1+ (point)))
3101             (when (search-backward "<" nil t)
3102               (setq new-child (buffer-substring (point) end))
3103               (push (list (incf generation)
3104                           child (setq child new-child)
3105                           subject date)
3106                     relations)))
3107           (when child
3108             (push (list (1+ generation) child nil subject) relations))
3109           (erase-buffer)))
3110       (kill-buffer (current-buffer)))
3111     ;; Sort over trustworthiness.
3112     (mapcar
3113      (lambda (relation)
3114        (when (gnus-dependencies-add-header
3115               (make-full-mail-header
3116                gnus-reffed-article-number
3117                (nth 3 relation) "" (or (nth 4 relation) "")
3118                (nth 1 relation)
3119                (or (nth 2 relation) "") 0 0 "")
3120               gnus-newsgroup-dependencies nil)
3121          (push gnus-reffed-article-number gnus-newsgroup-limit)
3122          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3123          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3124                gnus-newsgroup-reads)
3125          (decf gnus-reffed-article-number)))
3126      (sort relations 'car-less-than-car))
3127     (gnus-message 7 "Making sparse threads...done")))
3128
3129 (defun gnus-build-old-threads ()
3130   ;; Look at all the articles that refer back to old articles, and
3131   ;; fetch the headers for the articles that aren't there.  This will
3132   ;; build complete threads - if the roots haven't been expired by the
3133   ;; server, that is.
3134   (let (id heads)
3135     (mapatoms
3136      (lambda (refs)
3137        (when (not (car (symbol-value refs)))
3138          (setq heads (cdr (symbol-value refs)))
3139          (while heads
3140            (if (memq (mail-header-number (caar heads))
3141                      gnus-newsgroup-dormant)
3142                (setq heads (cdr heads))
3143              (setq id (symbol-name refs))
3144              (while (and (setq id (gnus-build-get-header id))
3145                          (not (car (gnus-id-to-thread id)))))
3146              (setq heads nil)))))
3147      gnus-newsgroup-dependencies)))
3148
3149 ;; This function has to be called with point after the article number
3150 ;; on the beginning of the line.
3151 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3152   (let ((eol (gnus-point-at-eol))
3153         (buffer (current-buffer))
3154         header)
3155
3156     ;; overview: [num subject from date id refs chars lines misc]
3157     (unless (eobp)
3158       (forward-char))
3159
3160     (setq header
3161           (make-full-mail-header
3162            number                               ; number
3163            (nnheader-nov-field)                 ; subject
3164            (nnheader-nov-field)                 ; from
3165            (nnheader-nov-field)                 ; date
3166            (nnheader-nov-read-message-id)       ; id
3167            (nnheader-nov-field)                 ; refs
3168            (nnheader-nov-read-integer)          ; chars
3169            (nnheader-nov-read-integer)          ; lines
3170            (unless (eobp)
3171              (nnheader-nov-field))              ; misc
3172            (nnheader-nov-parse-extra)))         ; extra
3173
3174     (when gnus-alter-header-function
3175       (funcall gnus-alter-header-function header))
3176     (gnus-dependencies-add-header header dependencies force-new)))
3177
3178 (defun gnus-build-get-header (id)
3179   ;; Look through the buffer of NOV lines and find the header to
3180   ;; ID.  Enter this line into the dependencies hash table, and return
3181   ;; the id of the parent article (if any).
3182   (let ((deps gnus-newsgroup-dependencies)
3183         found header)
3184     (prog1
3185         (save-excursion
3186           (set-buffer nntp-server-buffer)
3187           (let ((case-fold-search nil))
3188             (goto-char (point-min))
3189             (while (and (not found)
3190                         (search-forward id nil t))
3191               (beginning-of-line)
3192               (setq found (looking-at
3193                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3194                                    (regexp-quote id))))
3195               (or found (beginning-of-line 2)))
3196             (when found
3197               (beginning-of-line)
3198               (and
3199                (setq header (gnus-nov-parse-line
3200                              (read (current-buffer)) deps))
3201                (gnus-parent-id (mail-header-references header))))))
3202       (when header
3203         (let ((number (mail-header-number header)))
3204           (push number gnus-newsgroup-limit)
3205           (push header gnus-newsgroup-headers)
3206           (if (memq number gnus-newsgroup-unselected)
3207               (progn
3208                 (push number gnus-newsgroup-unreads)
3209                 (setq gnus-newsgroup-unselected
3210                       (delq number gnus-newsgroup-unselected)))
3211             (push number gnus-newsgroup-ancient)))))))
3212
3213 (defun gnus-build-all-threads ()
3214   "Read all the headers."
3215   (let ((gnus-summary-ignore-duplicates t)
3216         (dependencies gnus-newsgroup-dependencies)
3217         header article)
3218     (save-excursion
3219       (set-buffer nntp-server-buffer)
3220       (let ((case-fold-search nil))
3221         (goto-char (point-min))
3222         (while (not (eobp))
3223           (ignore-errors
3224             (setq article (read (current-buffer))
3225                   header (gnus-nov-parse-line
3226                           article dependencies)))
3227           (when header
3228             (save-excursion
3229               (set-buffer gnus-summary-buffer)
3230               (push header gnus-newsgroup-headers)
3231               (if (memq (setq article (mail-header-number header))
3232                         gnus-newsgroup-unselected)
3233                   (progn
3234                     (push article gnus-newsgroup-unreads)
3235                     (setq gnus-newsgroup-unselected
3236                           (delq article gnus-newsgroup-unselected)))
3237                 (push article gnus-newsgroup-ancient)))
3238             (forward-line 1)))))))
3239
3240 (defun gnus-summary-update-article-line (article header)
3241   "Update the line for ARTICLE using HEADERS."
3242   (let* ((id (mail-header-id header))
3243          (thread (gnus-id-to-thread id)))
3244     (unless thread
3245       (error "Article in no thread"))
3246     ;; Update the thread.
3247     (setcar thread header)
3248     (gnus-summary-goto-subject article)
3249     (let* ((datal (gnus-data-find-list article))
3250            (data (car datal))
3251            (length (when (cdr datal)
3252                      (- (gnus-data-pos data)
3253                         (gnus-data-pos (cadr datal)))))
3254            (buffer-read-only nil)
3255            (level (gnus-summary-thread-level)))
3256       (gnus-delete-line)
3257       (gnus-summary-insert-line
3258        header level nil (gnus-article-mark article)
3259        (memq article gnus-newsgroup-replied)
3260        (memq article gnus-newsgroup-expirable)
3261        ;; Only insert the Subject string when it's different
3262        ;; from the previous Subject string.
3263        (if (gnus-subject-equal
3264             (condition-case ()
3265                 (mail-header-subject
3266                  (gnus-data-header
3267                   (cadr
3268                    (gnus-data-find-list
3269                     article
3270                     (gnus-data-list t)))))
3271               ;; Error on the side of excessive subjects.
3272               (error ""))
3273             (mail-header-subject header))
3274            ""
3275          (mail-header-subject header))
3276        nil (cdr (assq article gnus-newsgroup-scored))
3277        (memq article gnus-newsgroup-processable))
3278       (when length
3279         (gnus-data-update-list
3280          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3281
3282 (defun gnus-summary-update-article (article &optional iheader)
3283   "Update ARTICLE in the summary buffer."
3284   (set-buffer gnus-summary-buffer)
3285   (let* ((header (gnus-summary-article-header article))
3286          (id (mail-header-id header))
3287          (data (gnus-data-find article))
3288          (thread (gnus-id-to-thread id))
3289          (references (mail-header-references header))
3290          (parent
3291           (gnus-id-to-thread
3292            (or (gnus-parent-id
3293                 (when (and references
3294                            (not (equal "" references)))
3295                   references))
3296                "none")))
3297          (buffer-read-only nil)
3298          (old (car thread)))
3299     (when thread
3300       (unless iheader
3301         (setcar thread nil)
3302         (when parent
3303           (delq thread parent)))
3304       (if (gnus-summary-insert-subject id header)
3305           ;; Set the (possibly) new article number in the data structure.
3306           (gnus-data-set-number data (gnus-id-to-article id))
3307         (setcar thread old)
3308         nil))))
3309
3310 (defun gnus-rebuild-thread (id &optional line)
3311   "Rebuild the thread containing ID.
3312 If LINE, insert the rebuilt thread starting on line LINE."
3313   (let ((buffer-read-only nil)
3314         old-pos current thread data)
3315     (if (not gnus-show-threads)
3316         (setq thread (list (car (gnus-id-to-thread id))))
3317       ;; Get the thread this article is part of.
3318       (setq thread (gnus-remove-thread id)))
3319     (setq old-pos (gnus-point-at-bol))
3320     (setq current (save-excursion
3321                     (and (zerop (forward-line -1))
3322                          (gnus-summary-article-number))))
3323     ;; If this is a gathered thread, we have to go some re-gathering.
3324     (when (stringp (car thread))
3325       (let ((subject (car thread))
3326             roots thr)
3327         (setq thread (cdr thread))
3328         (while thread
3329           (unless (memq (setq thr (gnus-id-to-thread
3330                                    (gnus-root-id
3331                                     (mail-header-id (caar thread)))))
3332                         roots)
3333             (push thr roots))
3334           (setq thread (cdr thread)))
3335         ;; We now have all (unique) roots.
3336         (if (= (length roots) 1)
3337             ;; All the loose roots are now one solid root.
3338             (setq thread (car roots))
3339           (setq thread (cons subject (gnus-sort-threads roots))))))
3340     (let (threads)
3341       ;; We then insert this thread into the summary buffer.
3342       (when line
3343         (goto-char (point-min))
3344         (forward-line (1- line)))
3345       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3346         (if gnus-show-threads
3347             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3348           (gnus-summary-prepare-unthreaded thread))
3349         (setq data (nreverse gnus-newsgroup-data))
3350         (setq threads gnus-newsgroup-threads))
3351       ;; We splice the new data into the data structure.
3352       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3353       ;;!!! then we want to insert at the beginning of the buffer.
3354       ;;!!! That happens to be true with Gnus now, but that may
3355       ;;!!! change in the future.  Perhaps.
3356       (gnus-data-enter-list
3357        (if line nil current) data (- (point) old-pos))
3358       (setq gnus-newsgroup-threads
3359             (nconc threads gnus-newsgroup-threads))
3360       (gnus-data-compute-positions))))
3361
3362 (defun gnus-number-to-header (number)
3363   "Return the header for article NUMBER."
3364   (let ((headers gnus-newsgroup-headers))
3365     (while (and headers
3366                 (not (= number (mail-header-number (car headers)))))
3367       (pop headers))
3368     (when headers
3369       (car headers))))
3370
3371 (defun gnus-parent-headers (in-headers &optional generation)
3372   "Return the headers of the GENERATIONeth parent of HEADERS."
3373   (unless generation
3374     (setq generation 1))
3375   (let ((parent t)
3376         (headers in-headers)
3377         references)
3378     (while (and parent
3379                 (not (zerop generation))
3380                 (setq references (mail-header-references headers)))
3381       (setq headers (if (and references
3382                              (setq parent (gnus-parent-id references)))
3383                         (car (gnus-id-to-thread parent))
3384                       nil))
3385       (decf generation))
3386     (and (not (eq headers in-headers))
3387          headers)))
3388
3389 (defun gnus-id-to-thread (id)
3390   "Return the (sub-)thread where ID appears."
3391   (gnus-gethash id gnus-newsgroup-dependencies))
3392
3393 (defun gnus-id-to-article (id)
3394   "Return the article number of ID."
3395   (let ((thread (gnus-id-to-thread id)))
3396     (when (and thread
3397                (car thread))
3398       (mail-header-number (car thread)))))
3399
3400 (defun gnus-id-to-header (id)
3401   "Return the article headers of ID."
3402   (car (gnus-id-to-thread id)))
3403
3404 (defun gnus-article-displayed-root-p (article)
3405   "Say whether ARTICLE is a root(ish) article."
3406   (let ((level (gnus-summary-thread-level article))
3407         (refs (mail-header-references  (gnus-summary-article-header article)))
3408         particle)
3409     (cond
3410      ((null level) nil)
3411      ((zerop level) t)
3412      ((null refs) t)
3413      ((null (gnus-parent-id refs)) t)
3414      ((and (= 1 level)
3415            (null (setq particle (gnus-id-to-article
3416                                  (gnus-parent-id refs))))
3417            (null (gnus-summary-thread-level particle)))))))
3418
3419 (defun gnus-root-id (id)
3420   "Return the id of the root of the thread where ID appears."
3421   (let (last-id prev)
3422     (while (and id (setq prev (car (gnus-id-to-thread id))))
3423       (setq last-id id
3424             id (gnus-parent-id (mail-header-references prev))))
3425     last-id))
3426
3427 (defun gnus-articles-in-thread (thread)
3428   "Return the list of articles in THREAD."
3429   (cons (mail-header-number (car thread))
3430         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3431
3432 (defun gnus-remove-thread (id &optional dont-remove)
3433   "Remove the thread that has ID in it."
3434   (let (headers thread last-id)
3435     ;; First go up in this thread until we find the root.
3436     (setq last-id (gnus-root-id id)
3437           headers (message-flatten-list (gnus-id-to-thread last-id)))
3438     ;; We have now found the real root of this thread.  It might have
3439     ;; been gathered into some loose thread, so we have to search
3440     ;; through the threads to find the thread we wanted.
3441     (let ((threads gnus-newsgroup-threads)
3442           sub)
3443       (while threads
3444         (setq sub (car threads))
3445         (if (stringp (car sub))
3446             ;; This is a gathered thread, so we look at the roots
3447             ;; below it to find whether this article is in this
3448             ;; gathered root.
3449             (progn
3450               (setq sub (cdr sub))
3451               (while sub
3452                 (when (member (caar sub) headers)
3453                   (setq thread (car threads)
3454                         threads nil
3455                         sub nil))
3456                 (setq sub (cdr sub))))
3457           ;; It's an ordinary thread, so we check it.
3458           (when (eq (car sub) (car headers))
3459             (setq thread sub
3460                   threads nil)))
3461         (setq threads (cdr threads)))
3462       ;; If this article is in no thread, then it's a root.
3463       (if thread
3464           (unless dont-remove
3465             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3466         (setq thread (gnus-id-to-thread last-id)))
3467       (when thread
3468         (prog1
3469             thread                      ; We return this thread.
3470           (unless dont-remove
3471             (if (stringp (car thread))
3472                 (progn
3473                   ;; If we use dummy roots, then we have to remove the
3474                   ;; dummy root as well.
3475                   (when (eq gnus-summary-make-false-root 'dummy)
3476                     ;; We go to the dummy root by going to
3477                     ;; the first sub-"thread", and then one line up.
3478                     (gnus-summary-goto-article
3479                      (mail-header-number (caadr thread)))
3480                     (forward-line -1)
3481                     (gnus-delete-line)
3482                     (gnus-data-compute-positions))
3483                   (setq thread (cdr thread))
3484                   (while thread
3485                     (gnus-remove-thread-1 (car thread))
3486                     (setq thread (cdr thread))))
3487               (gnus-summary-show-all-threads)
3488               (gnus-remove-thread-1 thread))))))))
3489
3490 (defun gnus-remove-thread-1 (thread)
3491   "Remove the thread THREAD recursively."
3492   (let ((number (mail-header-number (pop thread)))
3493         d)
3494     (setq thread (reverse thread))
3495     (while thread
3496       (gnus-remove-thread-1 (pop thread)))
3497     (when (setq d (gnus-data-find number))
3498       (goto-char (gnus-data-pos d))
3499       (gnus-data-remove
3500        number
3501        (- (gnus-point-at-bol)
3502           (prog1
3503               (1+ (gnus-point-at-eol))
3504             (gnus-delete-line)))))))
3505
3506 (defun gnus-sort-threads (threads)
3507   "Sort THREADS."
3508   (if (not gnus-thread-sort-functions)
3509       threads
3510     (gnus-message 8 "Sorting threads...")
3511     (prog1
3512         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3513       (gnus-message 8 "Sorting threads...done"))))
3514
3515 (defun gnus-sort-articles (articles)
3516   "Sort ARTICLES."
3517   (when gnus-article-sort-functions
3518     (gnus-message 7 "Sorting articles...")
3519     (prog1
3520         (setq gnus-newsgroup-headers
3521               (sort articles (gnus-make-sort-function
3522                               gnus-article-sort-functions)))
3523       (gnus-message 7 "Sorting articles...done"))))
3524
3525 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3526 (defmacro gnus-thread-header (thread)
3527   ;; Return header of first article in THREAD.
3528   ;; Note that THREAD must never, ever be anything else than a variable -
3529   ;; using some other form will lead to serious barfage.
3530   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3531   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3532   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3533         (vector thread) 2))
3534
3535 (defsubst gnus-article-sort-by-number (h1 h2)
3536   "Sort articles by article number."
3537   (< (mail-header-number h1)
3538      (mail-header-number h2)))
3539
3540 (defun gnus-thread-sort-by-number (h1 h2)
3541   "Sort threads by root article number."
3542   (gnus-article-sort-by-number
3543    (gnus-thread-header h1) (gnus-thread-header h2)))
3544
3545 (defsubst gnus-article-sort-by-lines (h1 h2)
3546   "Sort articles by article Lines header."
3547   (< (mail-header-lines h1)
3548      (mail-header-lines h2)))
3549
3550 (defun gnus-thread-sort-by-lines (h1 h2)
3551   "Sort threads by root article Lines header."
3552   (gnus-article-sort-by-lines
3553    (gnus-thread-header h1) (gnus-thread-header h2)))
3554
3555 (defsubst gnus-article-sort-by-chars (h1 h2)
3556   "Sort articles by octet length."
3557   (< (mail-header-chars h1)
3558      (mail-header-chars h2)))
3559
3560 (defun gnus-thread-sort-by-chars (h1 h2)
3561   "Sort threads by root article octet length."
3562   (gnus-article-sort-by-chars
3563    (gnus-thread-header h1) (gnus-thread-header h2)))
3564
3565 (defsubst gnus-article-sort-by-author (h1 h2)
3566   "Sort articles by root author."
3567   (string-lessp
3568    (let ((addr (mime-read-field 'From h1)))
3569      (or (std11-full-name-string addr)
3570          (std11-address-string addr)
3571          ""))
3572    (let ((addr (mime-read-field 'From h2)))
3573      (or (std11-full-name-string addr)
3574          (std11-address-string addr)
3575          ""))
3576    ))
3577
3578 (defun gnus-thread-sort-by-author (h1 h2)
3579   "Sort threads by root author."
3580   (gnus-article-sort-by-author
3581    (gnus-thread-header h1)  (gnus-thread-header h2)))
3582
3583 (defsubst gnus-article-sort-by-subject (h1 h2)
3584   "Sort articles by root subject."
3585   (string-lessp
3586    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3587    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3588
3589 (defun gnus-thread-sort-by-subject (h1 h2)
3590   "Sort threads by root subject."
3591   (gnus-article-sort-by-subject
3592    (gnus-thread-header h1) (gnus-thread-header h2)))
3593
3594 (defsubst gnus-article-sort-by-date (h1 h2)
3595   "Sort articles by root article date."
3596   (time-less-p
3597    (gnus-date-get-time (mail-header-date h1))
3598    (gnus-date-get-time (mail-header-date h2))))
3599
3600 (defun gnus-thread-sort-by-date (h1 h2)
3601   "Sort threads by root article date."
3602   (gnus-article-sort-by-date
3603    (gnus-thread-header h1) (gnus-thread-header h2)))
3604
3605 (defsubst gnus-article-sort-by-score (h1 h2)
3606   "Sort articles by root article score.
3607 Unscored articles will be counted as having a score of zero."
3608   (> (or (cdr (assq (mail-header-number h1)
3609                     gnus-newsgroup-scored))
3610          gnus-summary-default-score 0)
3611      (or (cdr (assq (mail-header-number h2)
3612                     gnus-newsgroup-scored))
3613          gnus-summary-default-score 0)))
3614
3615 (defun gnus-thread-sort-by-score (h1 h2)
3616   "Sort threads by root article score."
3617   (gnus-article-sort-by-score
3618    (gnus-thread-header h1) (gnus-thread-header h2)))
3619
3620 (defun gnus-thread-sort-by-total-score (h1 h2)
3621   "Sort threads by the sum of all scores in the thread.
3622 Unscored articles will be counted as having a score of zero."
3623   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3624
3625 (defun gnus-thread-total-score (thread)
3626   ;; This function find the total score of THREAD.
3627   (cond ((null thread)
3628          0)
3629         ((consp thread)
3630          (if (stringp (car thread))
3631              (apply gnus-thread-score-function 0
3632                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3633            (gnus-thread-total-score-1 thread)))
3634         (t
3635          (gnus-thread-total-score-1 (list thread)))))
3636
3637 (defun gnus-thread-total-score-1 (root)
3638   ;; This function find the total score of the thread below ROOT.
3639   (setq root (car root))
3640   (apply gnus-thread-score-function
3641          (or (append
3642               (mapcar 'gnus-thread-total-score
3643                       (cdr (gnus-id-to-thread (mail-header-id root))))
3644               (when (> (mail-header-number root) 0)
3645                 (list (or (cdr (assq (mail-header-number root)
3646                                      gnus-newsgroup-scored))
3647                           gnus-summary-default-score 0))))
3648              (list gnus-summary-default-score)
3649              '(0))))
3650
3651 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3652 (defvar gnus-tmp-prev-subject nil)
3653 (defvar gnus-tmp-false-parent nil)
3654 (defvar gnus-tmp-root-expunged nil)
3655 (defvar gnus-tmp-dummy-line nil)
3656
3657 (defvar gnus-tmp-header)
3658 (defun gnus-extra-header (type &optional header)
3659   "Return the extra header of TYPE."
3660   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3661       ""))
3662
3663 (defun gnus-summary-prepare-threads (threads)
3664   "Prepare summary buffer from THREADS and indentation LEVEL.
3665 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3666 or a straight list of headers."
3667   (gnus-message 7 "Generating summary...")
3668
3669   (setq gnus-newsgroup-threads threads)
3670   (beginning-of-line)
3671
3672   (let ((gnus-tmp-level 0)
3673         (default-score (or gnus-summary-default-score 0))
3674         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3675         thread number subject stack state gnus-tmp-gathered beg-match
3676         new-roots gnus-tmp-new-adopts thread-end
3677         gnus-tmp-header gnus-tmp-unread
3678         gnus-tmp-replied gnus-tmp-subject-or-nil
3679         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3680         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3681         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3682
3683     (setq gnus-tmp-prev-subject nil)
3684
3685     (if (vectorp (car threads))
3686         ;; If this is a straight (sic) list of headers, then a
3687         ;; threaded summary display isn't required, so we just create
3688         ;; an unthreaded one.
3689         (gnus-summary-prepare-unthreaded threads)
3690
3691       ;; Do the threaded display.
3692
3693       (while (or threads stack gnus-tmp-new-adopts new-roots)
3694
3695         (if (and (= gnus-tmp-level 0)
3696                  (or (not stack)
3697                      (= (caar stack) 0))
3698                  (not gnus-tmp-false-parent)
3699                  (or gnus-tmp-new-adopts new-roots))
3700             (if gnus-tmp-new-adopts
3701                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3702                       thread (list (car gnus-tmp-new-adopts))
3703                       gnus-tmp-header (caar thread)
3704                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3705               (when new-roots
3706                 (setq thread (list (car new-roots))
3707                       gnus-tmp-header (caar thread)
3708                       new-roots (cdr new-roots))))
3709
3710           (if threads
3711               ;; If there are some threads, we do them before the
3712               ;; threads on the stack.
3713               (setq thread threads
3714                     gnus-tmp-header (caar thread))
3715             ;; There were no current threads, so we pop something off
3716             ;; the stack.
3717             (setq state (car stack)
3718                   gnus-tmp-level (car state)
3719                   thread (cdr state)
3720                   stack (cdr stack)
3721                   gnus-tmp-header (caar thread))))
3722
3723         (setq gnus-tmp-false-parent nil)
3724         (setq gnus-tmp-root-expunged nil)
3725         (setq thread-end nil)
3726
3727         (if (stringp gnus-tmp-header)
3728             ;; The header is a dummy root.
3729             (cond
3730              ((eq gnus-summary-make-false-root 'adopt)
3731               ;; We let the first article adopt the rest.
3732               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3733                                                (cddar thread)))
3734               (setq gnus-tmp-gathered
3735                     (nconc (mapcar
3736                             (lambda (h) (mail-header-number (car h)))
3737                             (cddar thread))
3738                            gnus-tmp-gathered))
3739               (setq thread (cons (list (caar thread)
3740                                        (cadar thread))
3741                                  (cdr thread)))
3742               (setq gnus-tmp-level -1
3743                     gnus-tmp-false-parent t))
3744              ((eq gnus-summary-make-false-root 'empty)
3745               ;; We print adopted articles with empty subject fields.
3746               (setq gnus-tmp-gathered
3747                     (nconc (mapcar
3748                             (lambda (h) (mail-header-number (car h)))
3749                             (cddar thread))
3750                            gnus-tmp-gathered))
3751               (setq gnus-tmp-level -1))
3752              ((eq gnus-summary-make-false-root 'dummy)
3753               ;; We remember that we probably want to output a dummy
3754               ;; root.
3755               (setq gnus-tmp-dummy-line gnus-tmp-header)
3756               (setq gnus-tmp-prev-subject gnus-tmp-header))
3757              (t
3758               ;; We do not make a root for the gathered
3759               ;; sub-threads at all.
3760               (setq gnus-tmp-level -1)))
3761
3762           (setq number (mail-header-number gnus-tmp-header)
3763                 subject (mail-header-subject gnus-tmp-header))
3764
3765           (cond
3766            ;; If the thread has changed subject, we might want to make
3767            ;; this subthread into a root.
3768            ((and (null gnus-thread-ignore-subject)
3769                  (not (zerop gnus-tmp-level))
3770                  gnus-tmp-prev-subject
3771                  (not (inline
3772                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3773             (setq new-roots (nconc new-roots (list (car thread)))
3774                   thread-end t
3775                   gnus-tmp-header nil))
3776            ;; If the article lies outside the current limit,
3777            ;; then we do not display it.
3778            ((not (memq number gnus-newsgroup-limit))
3779             (setq gnus-tmp-gathered
3780                   (nconc (mapcar
3781                           (lambda (h) (mail-header-number (car h)))
3782                           (cdar thread))
3783                          gnus-tmp-gathered))
3784             (setq gnus-tmp-new-adopts (if (cdar thread)
3785                                           (append gnus-tmp-new-adopts
3786                                                   (cdar thread))
3787                                         gnus-tmp-new-adopts)
3788                   thread-end t
3789                   gnus-tmp-header nil)
3790             (when (zerop gnus-tmp-level)
3791               (setq gnus-tmp-root-expunged t)))
3792            ;; Perhaps this article is to be marked as read?
3793            ((and gnus-summary-mark-below
3794                  (< (or (cdr (assq number gnus-newsgroup-scored))
3795                         default-score)
3796                     gnus-summary-mark-below)
3797                  ;; Don't touch sparse articles.
3798                  (not (gnus-summary-article-sparse-p number))
3799                  (not (gnus-summary-article-ancient-p number)))
3800             (setq gnus-newsgroup-unreads
3801                   (delq number gnus-newsgroup-unreads))
3802             (if gnus-newsgroup-auto-expire
3803                 (push number gnus-newsgroup-expirable)
3804               (push (cons number gnus-low-score-mark)
3805                     gnus-newsgroup-reads))))
3806
3807           (when gnus-tmp-header
3808             ;; We may have an old dummy line to output before this
3809             ;; article.
3810             (when (and gnus-tmp-dummy-line
3811                        (gnus-subject-equal
3812                         gnus-tmp-dummy-line
3813                         (mail-header-subject gnus-tmp-header)))
3814               (gnus-summary-insert-dummy-line
3815                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3816               (setq gnus-tmp-dummy-line nil))
3817
3818             ;; Compute the mark.
3819             (setq gnus-tmp-unread (gnus-article-mark number))
3820
3821             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3822                                   gnus-tmp-header gnus-tmp-level)
3823                   gnus-newsgroup-data)
3824
3825             ;; Actually insert the line.
3826             (setq
3827              gnus-tmp-subject-or-nil
3828              (cond
3829               ((and gnus-thread-ignore-subject
3830                     gnus-tmp-prev-subject
3831                     (not (inline (gnus-subject-equal
3832                                   gnus-tmp-prev-subject subject))))
3833                subject)
3834               ((zerop gnus-tmp-level)
3835                (if (and (eq gnus-summary-make-false-root 'empty)
3836                         (memq number gnus-tmp-gathered)
3837                         gnus-tmp-prev-subject
3838                         (inline (gnus-subject-equal
3839                                  gnus-tmp-prev-subject subject)))
3840                    gnus-summary-same-subject
3841                  subject))
3842               (t gnus-summary-same-subject)))
3843             (if (and (eq gnus-summary-make-false-root 'adopt)
3844                      (= gnus-tmp-level 1)
3845                      (memq number gnus-tmp-gathered))
3846                 (setq gnus-tmp-opening-bracket ?\<
3847                       gnus-tmp-closing-bracket ?\>)
3848               (setq gnus-tmp-opening-bracket ?\[
3849                     gnus-tmp-closing-bracket ?\]))
3850             (setq
3851              gnus-tmp-indentation
3852              (aref gnus-thread-indent-array gnus-tmp-level)
3853              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3854              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3855                                 gnus-summary-default-score 0)
3856              gnus-tmp-score-char
3857              (if (or (null gnus-summary-default-score)
3858                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3859                          gnus-summary-zcore-fuzz))
3860                  ? ;Whitespace
3861                (if (< gnus-tmp-score gnus-summary-default-score)
3862                    gnus-score-below-mark gnus-score-over-mark))
3863              gnus-tmp-replied
3864              (cond ((memq number gnus-newsgroup-processable)
3865                     gnus-process-mark)
3866                    ((memq number gnus-newsgroup-cached)
3867                     gnus-cached-mark)
3868                    ((memq number gnus-newsgroup-replied)
3869                     gnus-replied-mark)
3870                    ((memq number gnus-newsgroup-saved)
3871                     gnus-saved-mark)
3872                    (t gnus-unread-mark))
3873              gnus-tmp-from (mail-header-from gnus-tmp-header)
3874              gnus-tmp-name
3875              (cond
3876               ((string-match "<[^>]+> *$" gnus-tmp-from)
3877                (setq beg-match (match-beginning 0))
3878                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3879                         (substring gnus-tmp-from (1+ (match-beginning 0))
3880                                    (1- (match-end 0))))
3881                    (substring gnus-tmp-from 0 beg-match)))
3882               ((string-match "(.+)" gnus-tmp-from)
3883                (substring gnus-tmp-from
3884                           (1+ (match-beginning 0)) (1- (match-end 0))))
3885               (t gnus-tmp-from)))
3886             (when (string= gnus-tmp-name "")
3887               (setq gnus-tmp-name gnus-tmp-from))
3888             (unless (numberp gnus-tmp-lines)
3889               (setq gnus-tmp-lines 0))
3890             (gnus-put-text-property
3891              (point)
3892              (progn (eval gnus-summary-line-format-spec) (point))
3893              'gnus-number number)
3894             (when gnus-visual-p
3895               (forward-line -1)
3896               (gnus-run-hooks 'gnus-summary-update-hook)
3897               (forward-line 1))
3898
3899             (setq gnus-tmp-prev-subject subject)))
3900
3901         (when (nth 1 thread)
3902           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3903         (incf gnus-tmp-level)
3904         (setq threads (if thread-end nil (cdar thread)))
3905         (unless threads
3906           (setq gnus-tmp-level 0)))))
3907   (gnus-message 7 "Generating summary...done"))
3908
3909 (defun gnus-summary-prepare-unthreaded (headers)
3910   "Generate an unthreaded summary buffer based on HEADERS."
3911   (let (header number mark)
3912
3913     (beginning-of-line)
3914
3915     (while headers
3916       ;; We may have to root out some bad articles...
3917       (when (memq (setq number (mail-header-number
3918                                 (setq header (pop headers))))
3919                   gnus-newsgroup-limit)
3920         ;; Mark article as read when it has a low score.
3921         (when (and gnus-summary-mark-below
3922                    (< (or (cdr (assq number gnus-newsgroup-scored))
3923                           gnus-summary-default-score 0)
3924                       gnus-summary-mark-below)
3925                    (not (gnus-summary-article-ancient-p number)))
3926           (setq gnus-newsgroup-unreads
3927                 (delq number gnus-newsgroup-unreads))
3928           (if gnus-newsgroup-auto-expire
3929               (push number gnus-newsgroup-expirable)
3930             (push (cons number gnus-low-score-mark)
3931                   gnus-newsgroup-reads)))
3932
3933         (setq mark (gnus-article-mark number))
3934         (push (gnus-data-make number mark (1+ (point)) header 0)
3935               gnus-newsgroup-data)
3936         (gnus-summary-insert-line
3937          header 0 number
3938          mark (memq number gnus-newsgroup-replied)
3939          (memq number gnus-newsgroup-expirable)
3940          (mail-header-subject header) nil
3941          (cdr (assq number gnus-newsgroup-scored))
3942          (memq number gnus-newsgroup-processable))))))
3943
3944 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3945   "Select newsgroup GROUP.
3946 If READ-ALL is non-nil, all articles in the group are selected.
3947 If SELECT-ARTICLES, only select those articles from GROUP."
3948   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3949          ;;!!! Dirty hack; should be removed.
3950          (gnus-summary-ignore-duplicates
3951           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3952               t
3953             gnus-summary-ignore-duplicates))
3954          (info (nth 2 entry))
3955          articles fetched-articles cached)
3956
3957     (unless (gnus-check-server
3958              (setq gnus-current-select-method
3959                    (gnus-find-method-for-group group)))
3960       (error "Couldn't open server"))
3961
3962     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3963         (gnus-activate-group group)     ; Or we can activate it...
3964         (progn                          ; Or we bug out.
3965           (when (equal major-mode 'gnus-summary-mode)
3966             (kill-buffer (current-buffer)))
3967           (error "Couldn't request group %s: %s"
3968                  group (gnus-status-message group))))
3969
3970     (unless (gnus-request-group group t)
3971       (when (equal major-mode 'gnus-summary-mode)
3972         (kill-buffer (current-buffer)))
3973       (error "Couldn't request group %s: %s"
3974              group (gnus-status-message group)))
3975
3976     (setq gnus-newsgroup-name group)
3977     (setq gnus-newsgroup-unselected nil)
3978     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3979     (gnus-summary-setup-default-charset)
3980
3981     ;; Adjust and set lists of article marks.
3982     (when info
3983       (gnus-adjust-marked-articles info))
3984
3985     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3986     (setq cached
3987           (if (gnus-virtual-group-p group)
3988               gnus-newsgroup-cached
3989             (gnus-cache-articles-in-group group)))
3990
3991     (setq gnus-newsgroup-unreads
3992           (gnus-set-difference
3993            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3994            gnus-newsgroup-dormant))
3995
3996     (setq gnus-newsgroup-processable nil)
3997
3998     (gnus-update-read-articles group gnus-newsgroup-unreads)
3999
4000     (if (setq articles select-articles)
4001         (setq gnus-newsgroup-unselected
4002               (gnus-sorted-intersection
4003                gnus-newsgroup-unreads
4004                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4005       (setq articles (gnus-articles-to-read group read-all)))
4006
4007     (cond
4008      ((null articles)
4009       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4010       'quit)
4011      ((eq articles 0) nil)
4012      (t
4013       ;; Init the dependencies hash table.
4014       (setq gnus-newsgroup-dependencies
4015             (gnus-make-hashtable (length articles)))
4016       (gnus-set-global-variables)
4017       ;; Retrieve the headers and read them in.
4018       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4019       (setq gnus-newsgroup-headers
4020             (gnus-retrieve-parsed-headers
4021              articles gnus-newsgroup-name
4022              ;; We might want to fetch old headers, but
4023              ;; not if there is only 1 article.
4024              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4025                            (not (numberp gnus-fetch-old-headers)))
4026                       (> (length articles) 1))
4027                   gnus-fetch-old-headers)))
4028       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4029
4030       ;; Suppress duplicates?
4031       (when gnus-suppress-duplicates
4032         (gnus-dup-suppress-articles))
4033
4034       ;; Set the initial limit.
4035       (setq gnus-newsgroup-limit (copy-sequence articles))
4036       ;; Remove canceled articles from the list of unread articles.
4037       (setq gnus-newsgroup-unreads
4038             (gnus-set-sorted-intersection
4039              gnus-newsgroup-unreads
4040              (setq fetched-articles
4041                    (mapcar (lambda (headers) (mail-header-number headers))
4042                            gnus-newsgroup-headers))))
4043       ;; Removed marked articles that do not exist.
4044       (gnus-update-missing-marks
4045        (gnus-sorted-complement fetched-articles articles))
4046
4047       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4048       (when cached
4049         (setq gnus-newsgroup-cached cached))
4050
4051       ;; We might want to build some more threads first.
4052       (when (and gnus-fetch-old-headers
4053                  (eq gnus-headers-retrieved-by 'nov))
4054         (if (eq gnus-fetch-old-headers 'invisible)
4055             (gnus-build-all-threads)
4056           (gnus-build-old-threads)))
4057       ;; Let the Gnus agent mark articles as read.
4058       (when gnus-agent
4059         (gnus-agent-get-undownloaded-list))
4060       ;; Check whether auto-expire is to be done in this group.
4061       (setq gnus-newsgroup-auto-expire
4062             (gnus-group-auto-expirable-p group))
4063       ;; Set up the article buffer now, if necessary.
4064       (unless gnus-single-article-buffer
4065         (gnus-article-setup-buffer))
4066       ;; First and last article in this newsgroup.
4067       (when gnus-newsgroup-headers
4068         (setq gnus-newsgroup-begin
4069               (mail-header-number (car gnus-newsgroup-headers))
4070               gnus-newsgroup-end
4071               (mail-header-number
4072                (gnus-last-element gnus-newsgroup-headers))))
4073       ;; GROUP is successfully selected.
4074       (or gnus-newsgroup-headers t)))))
4075
4076 (defun gnus-articles-to-read (group &optional read-all)
4077   ;; Find out what articles the user wants to read.
4078   (let* ((articles
4079           ;; Select all articles if `read-all' is non-nil, or if there
4080           ;; are no unread articles.
4081           (if (or read-all
4082                   (and (zerop (length gnus-newsgroup-marked))
4083                        (zerop (length gnus-newsgroup-unreads)))
4084                   (eq (gnus-group-find-parameter group 'display)
4085                       'all))
4086               (gnus-uncompress-range (gnus-active group))
4087             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4088                           (copy-sequence gnus-newsgroup-unreads))
4089                   '<)))
4090          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4091          (scored (length scored-list))
4092          (number (length articles))
4093          (marked (+ (length gnus-newsgroup-marked)
4094                     (length gnus-newsgroup-dormant)))
4095          (select
4096           (cond
4097            ((numberp read-all)
4098             read-all)
4099            (t
4100             (condition-case ()
4101                 (cond
4102                  ((and (or (<= scored marked) (= scored number))
4103                        (natnump gnus-large-newsgroup)
4104                        (> number gnus-large-newsgroup))
4105                   (let* ((minibuffer-setup-hook (append
4106                                                  minibuffer-setup-hook
4107                                                  '(beginning-of-line)))
4108                          (input (read-string
4109                                  (format
4110                                   "How many articles from %s (max %d): "
4111                                   (gnus-limit-string gnus-newsgroup-name 35)
4112                                   number)
4113                                  (number-to-string gnus-large-newsgroup))))
4114                     (if (string-match "^[ \t]*$" input)
4115                         number
4116                       input)))
4117                  ((and (> scored marked) (< scored number)
4118                        (> (- scored number) 20))
4119                   (let ((input
4120                          (read-string
4121                           (format "%s %s (%d scored, %d total): "
4122                                   "How many articles from"
4123                                   group scored number))))
4124                     (if (string-match "^[ \t]*$" input)
4125                         number input)))
4126                  (t number))
4127               (quit nil))))))
4128     (setq select (if (stringp select) (string-to-number select) select))
4129     (if (or (null select) (zerop select))
4130         select
4131       (if (and (not (zerop scored)) (<= (abs select) scored))
4132           (progn
4133             (setq articles (sort scored-list '<))
4134             (setq number (length articles)))
4135         (setq articles (copy-sequence articles)))
4136
4137       (when (< (abs select) number)
4138         (if (< select 0)
4139             ;; Select the N oldest articles.
4140             (setcdr (nthcdr (1- (abs select)) articles) nil)
4141           ;; Select the N most recent articles.
4142           (setq articles (nthcdr (- number select) articles))))
4143       (setq gnus-newsgroup-unselected
4144             (gnus-sorted-intersection
4145              gnus-newsgroup-unreads
4146              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4147       articles)))
4148
4149 (defun gnus-killed-articles (killed articles)
4150   (let (out)
4151     (while articles
4152       (when (inline (gnus-member-of-range (car articles) killed))
4153         (push (car articles) out))
4154       (setq articles (cdr articles)))
4155     out))
4156
4157 (defun gnus-uncompress-marks (marks)
4158   "Uncompress the mark ranges in MARKS."
4159   (let ((uncompressed '(score bookmark))
4160         out)
4161     (while marks
4162       (if (memq (caar marks) uncompressed)
4163           (push (car marks) out)
4164         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4165       (setq marks (cdr marks)))
4166     out))
4167
4168 (defun gnus-adjust-marked-articles (info)
4169   "Set all article lists and remove all marks that are no longer valid."
4170   (let* ((marked-lists (gnus-info-marks info))
4171          (active (gnus-active (gnus-info-group info)))
4172          (min (car active))
4173          (max (cdr active))
4174          (types gnus-article-mark-lists)
4175          (uncompressed '(score bookmark killed))
4176          marks var articles article mark)
4177
4178     (while marked-lists
4179       (setq marks (pop marked-lists))
4180       (set (setq var (intern (format "gnus-newsgroup-%s"
4181                                      (car (rassq (setq mark (car marks))
4182                                                  types)))))
4183            (if (memq (car marks) uncompressed) (cdr marks)
4184              (gnus-uncompress-range (cdr marks))))
4185
4186       (setq articles (symbol-value var))
4187
4188       ;; All articles have to be subsets of the active articles.
4189       (cond
4190        ;; Adjust "simple" lists.
4191        ((memq mark '(tick dormant expire reply save))
4192         (while articles
4193           (when (or (< (setq article (pop articles)) min) (> article max))
4194             (set var (delq article (symbol-value var))))))
4195        ;; Adjust assocs.
4196        ((memq mark uncompressed)
4197         (when (not (listp (cdr (symbol-value var))))
4198           (set var (list (symbol-value var))))
4199         (when (not (listp (cdr articles)))
4200           (setq articles (list articles)))
4201         (while articles
4202           (when (or (not (consp (setq article (pop articles))))
4203                     (< (car article) min)
4204                     (> (car article) max))
4205             (set var (delq article (symbol-value var))))))))))
4206
4207 (defun gnus-update-missing-marks (missing)
4208   "Go through the list of MISSING articles and remove them from the mark lists."
4209   (when missing
4210     (let ((types gnus-article-mark-lists)
4211           var m)
4212       ;; Go through all types.
4213       (while types
4214         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4215         (when (symbol-value var)
4216           ;; This list has articles.  So we delete all missing articles
4217           ;; from it.
4218           (setq m missing)
4219           (while m
4220             (set var (delq (pop m) (symbol-value var)))))))))
4221
4222 (defun gnus-update-marks ()
4223   "Enter the various lists of marked articles into the newsgroup info list."
4224   (let ((types gnus-article-mark-lists)
4225         (info (gnus-get-info gnus-newsgroup-name))
4226         (uncompressed '(score bookmark killed))
4227         type list newmarked symbol delta-marks)
4228     (when info
4229       ;; Add all marks lists that are non-nil to the list of marks lists.
4230       (while (setq type (pop types))
4231         (when (setq list (symbol-value
4232                           (setq symbol
4233                                 (intern (format "gnus-newsgroup-%s"
4234                                                 (car type))))))
4235
4236           ;; Get rid of the entries of the articles that have the
4237           ;; default score.
4238           (when (and (eq (cdr type) 'score)
4239                      gnus-save-score
4240                      list)
4241             (let* ((arts list)
4242                    (prev (cons nil list))
4243                    (all prev))
4244               (while arts
4245                 (if (or (not (consp (car arts)))
4246                         (= (cdar arts) gnus-summary-default-score))
4247                     (setcdr prev (cdr arts))
4248                   (setq prev arts))
4249                 (setq arts (cdr arts)))
4250               (setq list (cdr all))))
4251
4252           (when (gnus-check-backend-function 'request-set-mark
4253                                              gnus-newsgroup-name)
4254             ;; uncompressed:s are not proper flags (they are cons cells)
4255             ;; cache is a internal gnus flag
4256             (unless (memq (cdr type) (cons 'cache uncompressed))
4257               (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4258                      (list (gnus-compress-sequence (sort list '<)))
4259                      (del (gnus-remove-from-range old list))
4260                      (add (gnus-remove-from-range list old)))
4261                 (if add
4262                     (push (list add 'add (list (cdr type))) delta-marks))
4263                 (if del
4264                     (push (list del 'del (list (cdr type))) delta-marks)))))
4265           
4266           (push (cons (cdr type)
4267                       (if (memq (cdr type) uncompressed) list
4268                         (gnus-compress-sequence
4269                          (set symbol (sort list '<)) t)))
4270                 newmarked)))
4271
4272       (when delta-marks
4273         (unless (gnus-check-group gnus-newsgroup-name)
4274           (error "Can't open server for %s" gnus-newsgroup-name))
4275         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4276           
4277       ;; Enter these new marks into the info of the group.
4278       (if (nthcdr 3 info)
4279           (setcar (nthcdr 3 info) newmarked)
4280         ;; Add the marks lists to the end of the info.
4281         (when newmarked
4282           (setcdr (nthcdr 2 info) (list newmarked))))
4283
4284       ;; Cut off the end of the info if there's nothing else there.
4285       (let ((i 5))
4286         (while (and (> i 2)
4287                     (not (nth i info)))
4288           (when (nthcdr (decf i) info)
4289             (setcdr (nthcdr i info) nil)))))))
4290
4291 (defun gnus-set-mode-line (where)
4292   "This function sets the mode line of the article or summary buffers.
4293 If WHERE is `summary', the summary mode line format will be used."
4294   ;; Is this mode line one we keep updated?
4295   (when (and (memq where gnus-updated-mode-lines)
4296              (symbol-value
4297               (intern (format "gnus-%s-mode-line-format-spec" where))))
4298     (let (mode-string)
4299       (save-excursion
4300         ;; We evaluate this in the summary buffer since these
4301         ;; variables are buffer-local to that buffer.
4302         (set-buffer gnus-summary-buffer)
4303         ;; We bind all these variables that are used in the `eval' form
4304         ;; below.
4305         (let* ((mformat (symbol-value
4306                          (intern
4307                           (format "gnus-%s-mode-line-format-spec" where))))
4308                (gnus-tmp-group-name gnus-newsgroup-name)
4309                (gnus-tmp-article-number (or gnus-current-article 0))
4310                (gnus-tmp-unread gnus-newsgroup-unreads)
4311                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4312                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4313                (gnus-tmp-unread-and-unselected
4314                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4315                             (zerop gnus-tmp-unselected))
4316                        "")
4317                       ((zerop gnus-tmp-unselected)
4318                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4319                       (t (format "{%d(+%d) more}"
4320                                  gnus-tmp-unread-and-unticked
4321                                  gnus-tmp-unselected))))
4322                (gnus-tmp-subject
4323                 (if (and gnus-current-headers
4324                          (vectorp gnus-current-headers))
4325                     (gnus-mode-string-quote
4326                      (mail-header-subject gnus-current-headers))
4327                   ""))
4328                bufname-length max-len
4329                gnus-tmp-header);; passed as argument to any user-format-funcs
4330           (setq mode-string (eval mformat))
4331           (setq bufname-length (if (string-match "%b" mode-string)
4332                                    (- (length
4333                                        (buffer-name
4334                                         (if (eq where 'summary)
4335                                             nil
4336                                           (get-buffer gnus-article-buffer))))
4337                                       2)
4338                                  0))
4339           (setq max-len (max 4 (if gnus-mode-non-string-length
4340                                    (- (window-width)
4341                                       gnus-mode-non-string-length
4342                                       bufname-length)
4343                                  (length mode-string))))
4344           ;; We might have to chop a bit of the string off...
4345           (when (> (length mode-string) max-len)
4346             (setq mode-string
4347                   (concat (gnus-truncate-string mode-string (- max-len 3))
4348                           "...")))
4349           ;; Pad the mode string a bit.
4350           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4351       ;; Update the mode line.
4352       (setq mode-line-buffer-identification
4353             (gnus-mode-line-buffer-identification (list mode-string)))
4354       (set-buffer-modified-p t))))
4355
4356 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4357   "Go through the HEADERS list and add all Xrefs to a hash table.
4358 The resulting hash table is returned, or nil if no Xrefs were found."
4359   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4360          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4361          (xref-hashtb (gnus-make-hashtable))
4362          start group entry number xrefs header)
4363     (while headers
4364       (setq header (pop headers))
4365       (when (and (setq xrefs (mail-header-xref header))
4366                  (not (memq (setq number (mail-header-number header))
4367                             unreads)))
4368         (setq start 0)
4369         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4370           (setq start (match-end 0))
4371           (setq group (if prefix
4372                           (concat prefix (substring xrefs (match-beginning 1)
4373                                                     (match-end 1)))
4374                         (substring xrefs (match-beginning 1) (match-end 1))))
4375           (setq number
4376                 (string-to-int (substring xrefs (match-beginning 2)
4377                                           (match-end 2))))
4378           (if (setq entry (gnus-gethash group xref-hashtb))
4379               (setcdr entry (cons number (cdr entry)))
4380             (gnus-sethash group (cons number nil) xref-hashtb)))))
4381     (and start xref-hashtb)))
4382
4383 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4384   "Look through all the headers and mark the Xrefs as read."
4385   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4386         name entry info xref-hashtb idlist method nth4)
4387     (save-excursion
4388       (set-buffer gnus-group-buffer)
4389       (when (setq xref-hashtb
4390                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4391         (mapatoms
4392          (lambda (group)
4393            (unless (string= from-newsgroup (setq name (symbol-name group)))
4394              (setq idlist (symbol-value group))
4395              ;; Dead groups are not updated.
4396              (and (prog1
4397                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4398                             info (nth 2 entry))
4399                     (when (stringp (setq nth4 (gnus-info-method info)))
4400                       (setq nth4 (gnus-server-to-method nth4))))
4401                   ;; Only do the xrefs if the group has the same
4402                   ;; select method as the group we have just read.
4403                   (or (gnus-methods-equal-p
4404                        nth4 (gnus-find-method-for-group from-newsgroup))
4405                       virtual
4406                       (equal nth4 (setq method (gnus-find-method-for-group
4407                                                 from-newsgroup)))
4408                       (and (equal (car nth4) (car method))
4409                            (equal (nth 1 nth4) (nth 1 method))))
4410                   gnus-use-cross-reference
4411                   (or (not (eq gnus-use-cross-reference t))
4412                       virtual
4413                       ;; Only do cross-references on subscribed
4414                       ;; groups, if that is what is wanted.
4415                       (<= (gnus-info-level info) gnus-level-subscribed))
4416                   (gnus-group-make-articles-read name idlist))))
4417          xref-hashtb)))))
4418
4419 (defun gnus-compute-read-articles (group articles)
4420   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4421          (info (nth 2 entry))
4422          (active (gnus-active group))
4423          ninfo)
4424     (when entry
4425       ;; First peel off all invalid article numbers.
4426       (when active
4427         (let ((ids articles)
4428               id first)
4429           (while (setq id (pop ids))
4430             (when (and first (> id (cdr active)))
4431               ;; We'll end up in this situation in one particular
4432               ;; obscure situation.  If you re-scan a group and get
4433               ;; a new article that is cross-posted to a different
4434               ;; group that has not been re-scanned, you might get
4435               ;; crossposted article that has a higher number than
4436               ;; Gnus believes possible.  So we re-activate this
4437               ;; group as well.  This might mean doing the
4438               ;; crossposting thingy will *increase* the number
4439               ;; of articles in some groups.  Tsk, tsk.
4440               (setq active (or (gnus-activate-group group) active)))
4441             (when (or (> id (cdr active))
4442                       (< id (car active)))
4443               (setq articles (delq id articles))))))
4444       ;; If the read list is nil, we init it.
4445       (if (and active
4446                (null (gnus-info-read info))
4447                (> (car active) 1))
4448           (setq ninfo (cons 1 (1- (car active))))
4449         (setq ninfo (gnus-info-read info)))
4450       ;; Then we add the read articles to the range.
4451       (gnus-add-to-range
4452        ninfo (setq articles (sort articles '<))))))
4453
4454 (defun gnus-group-make-articles-read (group articles)
4455   "Update the info of GROUP to say that ARTICLES are read."
4456   (let* ((num 0)
4457          (entry (gnus-gethash group gnus-newsrc-hashtb))
4458          (info (nth 2 entry))
4459          (active (gnus-active group))
4460          range)
4461     (when entry
4462       (setq range (gnus-compute-read-articles group articles))
4463       (save-excursion
4464         (set-buffer gnus-group-buffer)
4465         (gnus-undo-register
4466           `(progn
4467              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4468              (gnus-info-set-read ',info ',(gnus-info-read info))
4469              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4470              (gnus-group-update-group ,group t))))
4471       ;; Add the read articles to the range.
4472       (gnus-info-set-read info range)
4473       ;; Then we have to re-compute how many unread
4474       ;; articles there are in this group.
4475       (when active
4476         (cond
4477          ((not range)
4478           (setq num (- (1+ (cdr active)) (car active))))
4479          ((not (listp (cdr range)))
4480           (setq num (- (cdr active) (- (1+ (cdr range))
4481                                        (car range)))))
4482          (t
4483           (while range
4484             (if (numberp (car range))
4485                 (setq num (1+ num))
4486               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4487             (setq range (cdr range)))
4488           (setq num (- (cdr active) num))))
4489         ;; Update the number of unread articles.
4490         (setcar entry num)
4491         ;; Update the group buffer.
4492         (gnus-group-update-group group t)))))
4493
4494 (defvar gnus-newsgroup-none-id 0)
4495
4496 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4497   (let ((cur nntp-server-buffer)
4498         (dependencies
4499          (or dependencies
4500              (save-excursion (set-buffer gnus-summary-buffer)
4501                              gnus-newsgroup-dependencies)))
4502         headers id end ref
4503         (mail-parse-charset gnus-newsgroup-charset))
4504     (save-excursion
4505       (set-buffer nntp-server-buffer)
4506       ;; Translate all TAB characters into SPACE characters.
4507       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4508       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4509       (gnus-run-hooks 'gnus-parse-headers-hook)
4510       (let ((case-fold-search t)
4511             in-reply-to header p lines chars ctype)
4512         (goto-char (point-min))
4513         ;; Search to the beginning of the next header.  Error messages
4514         ;; do not begin with 2 or 3.
4515         (while (re-search-forward "^[23][0-9]+ " nil t)
4516           (setq id nil
4517                 ref nil)
4518           ;; This implementation of this function, with nine
4519           ;; search-forwards instead of the one re-search-forward and
4520           ;; a case (which basically was the old function) is actually
4521           ;; about twice as fast, even though it looks messier.  You
4522           ;; can't have everything, I guess.  Speed and elegance
4523           ;; doesn't always go hand in hand.
4524           (setq
4525            header
4526            (make-full-mail-header
4527             ;; Number.
4528             (prog1
4529                 (read cur)
4530               (end-of-line)
4531               (setq p (point))
4532               (narrow-to-region (point)
4533                                 (or (and (search-forward "\n.\n" nil t)
4534                                          (- (point) 2))
4535                                     (point))))
4536             ;; Subject.
4537             (progn
4538               (goto-char p)
4539               (if (search-forward "\nsubject: " nil t)
4540                   (buffer-substring (match-end 0) (std11-field-end))
4541                 "(none)"))
4542             ;; From.
4543             (progn
4544               (goto-char p)
4545               (if (search-forward "\nfrom: " nil t)
4546                   (buffer-substring (match-end 0) (std11-field-end))
4547                 "(nobody)"))
4548             ;; Date.
4549             (progn
4550               (goto-char p)
4551               (if (search-forward "\ndate: " nil t)
4552                   (buffer-substring (match-end 0) (std11-field-end))
4553                 ""))
4554             ;; Message-ID.
4555             (progn
4556               (goto-char p)
4557               (setq id (if (re-search-forward
4558                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4559                            ;; We do it this way to make sure the Message-ID
4560                            ;; is (somewhat) syntactically valid.
4561                            (buffer-substring (match-beginning 1)
4562                                              (match-end 1))
4563                          ;; If there was no message-id, we just fake one
4564                          ;; to make subsequent routines simpler.
4565                          (nnheader-generate-fake-message-id))))
4566             ;; References.
4567             (progn
4568               (goto-char p)
4569               (if (search-forward "\nreferences: " nil t)
4570                   (progn
4571                     (setq end (point))
4572                     (prog1
4573                         (buffer-substring (match-end 0) (std11-field-end))
4574                       (setq ref
4575                             (buffer-substring
4576                              (progn
4577                                ;; (end-of-line)
4578                                (search-backward ">" end t)
4579                                (1+ (point)))
4580                              (progn
4581                                (search-backward "<" end t)
4582                                (point))))))
4583                 ;; Get the references from the in-reply-to header if there
4584                 ;; were no references and the in-reply-to header looks
4585                 ;; promising.
4586                 (if (and (search-forward "\nin-reply-to: " nil t)
4587                          (setq in-reply-to
4588                                (buffer-substring (match-end 0)
4589                                                  (std11-field-end)))
4590                          (string-match "<[^>]+>" in-reply-to))
4591                     (let (ref2)
4592                       (setq ref (substring in-reply-to (match-beginning 0)
4593                                            (match-end 0)))
4594                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4595                         (setq ref2 (substring in-reply-to (match-beginning 0)
4596                                               (match-end 0)))
4597                         (when (> (length ref2) (length ref))
4598                           (setq ref ref2)))
4599                       ref)
4600                   (setq ref nil))))
4601             ;; Chars.
4602             (progn
4603               (goto-char p)
4604               (if (search-forward "\nchars: " nil t)
4605                   (if (numberp (setq chars (ignore-errors (read cur))))
4606                       chars 0)
4607                 0))
4608             ;; Lines.
4609             (progn
4610               (goto-char p)
4611               (if (search-forward "\nlines: " nil t)
4612                   (if (numberp (setq lines (ignore-errors (read cur))))
4613                       lines 0)
4614                 0))
4615             ;; Xref.
4616             (progn
4617               (goto-char p)
4618               (and (search-forward "\nxref: " nil t)
4619                    (buffer-substring (match-end 0) (std11-field-end))))
4620             ;; Extra.
4621             (when gnus-extra-headers
4622               (let ((extra gnus-extra-headers)
4623                     out)
4624                 (while extra
4625                   (goto-char p)
4626                   (when (search-forward
4627                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4628                     (push (cons (car extra)
4629                                 (buffer-substring (match-end 0)
4630                                                   (std11-field-end)))
4631                           out))
4632                   (pop extra))
4633                 out))))
4634           (goto-char p)
4635           (if (and (search-forward "\ncontent-type: " nil t)
4636                    (setq ctype
4637                          (buffer-substring (match-end 0) (std11-field-end))))
4638               (mime-entity-set-content-type-internal
4639                header (mime-parse-Content-Type ctype)))
4640           (when (equal id ref)
4641             (setq ref nil))
4642
4643           (when gnus-alter-header-function
4644             (funcall gnus-alter-header-function header)
4645             (setq id (mail-header-id header)
4646                   ref (gnus-parent-id (mail-header-references header))))
4647
4648           (when (setq header
4649                       (gnus-dependencies-add-header
4650                        header dependencies force-new))
4651             (push header headers))
4652           (goto-char (point-max))
4653           (widen))
4654         (nreverse headers)))))
4655
4656 ;; Goes through the xover lines and returns a list of vectors
4657 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4658                                                   force-new dependencies
4659                                                   group also-fetch-heads)
4660   "Parse the news overview data in the server buffer, and return a
4661 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4662   ;; Get the Xref when the users reads the articles since most/some
4663   ;; NNTP servers do not include Xrefs when using XOVER.
4664   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4665   (let ((mail-parse-charset gnus-newsgroup-charset)
4666         (cur nntp-server-buffer)
4667         (dependencies (or dependencies gnus-newsgroup-dependencies))
4668         number headers header)
4669     (save-excursion
4670       (set-buffer nntp-server-buffer)
4671       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4672       ;; Allow the user to mangle the headers before parsing them.
4673       (gnus-run-hooks 'gnus-parse-headers-hook)
4674       (goto-char (point-min))
4675       (while (not (eobp))
4676         (condition-case ()
4677             (while (and sequence (not (eobp)))
4678               (setq number (read cur))
4679               (while (and sequence
4680                           (< (car sequence) number))
4681                 (setq sequence (cdr sequence)))
4682               (and sequence
4683                    (eq number (car sequence))
4684                    (progn
4685                      (setq sequence (cdr sequence))
4686                      (setq header (inline
4687                                     (gnus-nov-parse-line
4688                                      number dependencies force-new))))
4689                    (push header headers))
4690               (forward-line 1))
4691           (error
4692            (gnus-error 4 "Strange nov line (%d)"
4693                        (count-lines (point-min) (point)))))
4694         (forward-line 1))
4695       ;; A common bug in inn is that if you have posted an article and
4696       ;; then retrieves the active file, it will answer correctly --
4697       ;; the new article is included.  However, a NOV entry for the
4698       ;; article may not have been generated yet, so this may fail.
4699       ;; We work around this problem by retrieving the last few
4700       ;; headers using HEAD.
4701       (if (or (not also-fetch-heads)
4702               (not sequence))
4703           ;; We (probably) got all the headers.
4704           (nreverse headers)
4705         (let ((gnus-nov-is-evil t))
4706           (nconc
4707            (nreverse headers)
4708            (gnus-retrieve-parsed-headers sequence group)
4709            ))))))
4710
4711 (defun gnus-article-get-xrefs ()
4712   "Fill in the Xref value in `gnus-current-headers', if necessary.
4713 This is meant to be called in `gnus-article-internal-prepare-hook'."
4714   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4715                                  gnus-current-headers)))
4716     (or (not gnus-use-cross-reference)
4717         (not headers)
4718         (and (mail-header-xref headers)
4719              (not (string= (mail-header-xref headers) "")))
4720         (let ((case-fold-search t)
4721               xref)
4722           (save-restriction
4723             (nnheader-narrow-to-headers)
4724             (goto-char (point-min))
4725             (when (or (and (eq (downcase (char-after)) ?x)
4726                            (looking-at "Xref:"))
4727                       (search-forward "\nXref:" nil t))
4728               (goto-char (1+ (match-end 0)))
4729               (setq xref (buffer-substring (point)
4730                                            (progn (end-of-line) (point))))
4731               (mail-header-set-xref headers xref)))))))
4732
4733 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4734   "Find article ID and insert the summary line for that article.
4735 OLD-HEADER can either be a header or a line number to insert
4736 the subject line on."
4737   (let* ((line (and (numberp old-header) old-header))
4738          (old-header (and (vectorp old-header) old-header))
4739          (header (cond ((and old-header use-old-header)
4740                         old-header)
4741                        ((and (numberp id)
4742                              (gnus-number-to-header id))
4743                         (gnus-number-to-header id))
4744                        (t
4745                         (gnus-read-header id))))
4746          (number (and (numberp id) id))
4747          d)
4748     (when header
4749       ;; Rebuild the thread that this article is part of and go to the
4750       ;; article we have fetched.
4751       (when (and (not gnus-show-threads)
4752                  old-header)
4753         (when (and number
4754                    (setq d (gnus-data-find (mail-header-number old-header))))
4755           (goto-char (gnus-data-pos d))
4756           (gnus-data-remove
4757            number
4758            (- (gnus-point-at-bol)
4759               (prog1
4760                   (1+ (gnus-point-at-eol))
4761                 (gnus-delete-line))))))
4762       (when old-header
4763         (mail-header-set-number header (mail-header-number old-header)))
4764       (setq gnus-newsgroup-sparse
4765             (delq (setq number (mail-header-number header))
4766                   gnus-newsgroup-sparse))
4767       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4768       (push number gnus-newsgroup-limit)
4769       (gnus-rebuild-thread (mail-header-id header) line)
4770       (gnus-summary-goto-subject number nil t))
4771     (when (and (numberp number)
4772                (> number 0))
4773       ;; We have to update the boundaries even if we can't fetch the
4774       ;; article if ID is a number -- so that the next `P' or `N'
4775       ;; command will fetch the previous (or next) article even
4776       ;; if the one we tried to fetch this time has been canceled.
4777       (when (> number gnus-newsgroup-end)
4778         (setq gnus-newsgroup-end number))
4779       (when (< number gnus-newsgroup-begin)
4780         (setq gnus-newsgroup-begin number))
4781       (setq gnus-newsgroup-unselected
4782             (delq number gnus-newsgroup-unselected)))
4783     ;; Report back a success?
4784     (and header (mail-header-number header))))
4785
4786 ;;; Process/prefix in the summary buffer
4787
4788 (defun gnus-summary-work-articles (n)
4789   "Return a list of articles to be worked upon.
4790 The prefix argument, the list of process marked articles, and the
4791 current article will be taken into consideration."
4792   (save-excursion
4793     (set-buffer gnus-summary-buffer)
4794     (cond
4795      (n
4796       ;; A numerical prefix has been given.
4797       (setq n (prefix-numeric-value n))
4798       (let ((backward (< n 0))
4799             (n (abs (prefix-numeric-value n)))
4800             articles article)
4801         (save-excursion
4802           (while
4803               (and (> n 0)
4804                    (push (setq article (gnus-summary-article-number))
4805                          articles)
4806                    (if backward
4807                        (gnus-summary-find-prev nil article)
4808                      (gnus-summary-find-next nil article)))
4809             (decf n)))
4810         (nreverse articles)))
4811      ((and (gnus-region-active-p) (mark))
4812       (message "region active")
4813       ;; Work on the region between point and mark.
4814       (let ((max (max (point) (mark)))
4815             articles article)
4816         (save-excursion
4817           (goto-char (min (point) (mark)))
4818           (while
4819               (and
4820                (push (setq article (gnus-summary-article-number)) articles)
4821                (gnus-summary-find-next nil article)
4822                (< (point) max)))
4823           (nreverse articles))))
4824      (gnus-newsgroup-processable
4825       ;; There are process-marked articles present.
4826       ;; Save current state.
4827       (gnus-summary-save-process-mark)
4828       ;; Return the list.
4829       (reverse gnus-newsgroup-processable))
4830      (t
4831       ;; Just return the current article.
4832       (list (gnus-summary-article-number))))))
4833
4834 (defmacro gnus-summary-iterate (arg &rest forms)
4835   "Iterate over the process/prefixed articles and do FORMS.
4836 ARG is the interactive prefix given to the command.  FORMS will be
4837 executed with point over the summary line of the articles."
4838   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4839     `(let ((,articles (gnus-summary-work-articles ,arg)))
4840        (while ,articles
4841          (gnus-summary-goto-subject (car ,articles))
4842          ,@forms))))
4843
4844 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4845 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4846
4847 (defun gnus-summary-save-process-mark ()
4848   "Push the current set of process marked articles on the stack."
4849   (interactive)
4850   (push (copy-sequence gnus-newsgroup-processable)
4851         gnus-newsgroup-process-stack))
4852
4853 (defun gnus-summary-kill-process-mark ()
4854   "Push the current set of process marked articles on the stack and unmark."
4855   (interactive)
4856   (gnus-summary-save-process-mark)
4857   (gnus-summary-unmark-all-processable))
4858
4859 (defun gnus-summary-yank-process-mark ()
4860   "Pop the last process mark state off the stack and restore it."
4861   (interactive)
4862   (unless gnus-newsgroup-process-stack
4863     (error "Empty mark stack"))
4864   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4865
4866 (defun gnus-summary-process-mark-set (set)
4867   "Make SET into the current process marked articles."
4868   (gnus-summary-unmark-all-processable)
4869   (while set
4870     (gnus-summary-set-process-mark (pop set))))
4871
4872 ;;; Searching and stuff
4873
4874 (defun gnus-summary-search-group (&optional backward use-level)
4875   "Search for next unread newsgroup.
4876 If optional argument BACKWARD is non-nil, search backward instead."
4877   (save-excursion
4878     (set-buffer gnus-group-buffer)
4879     (when (gnus-group-search-forward
4880            backward nil (if use-level (gnus-group-group-level) nil))
4881       (gnus-group-group-name))))
4882
4883 (defun gnus-summary-best-group (&optional exclude-group)
4884   "Find the name of the best unread group.
4885 If EXCLUDE-GROUP, do not go to this group."
4886   (save-excursion
4887     (set-buffer gnus-group-buffer)
4888     (save-excursion
4889       (gnus-group-best-unread-group exclude-group))))
4890
4891 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4892   (if backward (gnus-summary-find-prev)
4893     (let* ((dummy (gnus-summary-article-intangible-p))
4894            (article (or article (gnus-summary-article-number)))
4895            (arts (gnus-data-find-list article))
4896            result)
4897       (when (and (not dummy)
4898                  (or (not gnus-summary-check-current)
4899                      (not unread)
4900                      (not (gnus-data-unread-p (car arts)))))
4901         (setq arts (cdr arts)))
4902       (when (setq result
4903                   (if unread
4904                       (progn
4905                         (while arts
4906                           (when (or (and undownloaded
4907                                          (eq gnus-undownloaded-mark
4908                                              (gnus-data-mark (car arts))))
4909                                     (gnus-data-unread-p (car arts)))
4910                             (setq result (car arts)
4911                                   arts nil))
4912                           (setq arts (cdr arts)))
4913                         result)
4914                     (car arts)))
4915         (goto-char (gnus-data-pos result))
4916         (gnus-data-number result)))))
4917
4918 (defun gnus-summary-find-prev (&optional unread article)
4919   (let* ((eobp (eobp))
4920          (article (or article (gnus-summary-article-number)))
4921          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4922          result)
4923     (when (and (not eobp)
4924                (or (not gnus-summary-check-current)
4925                    (not unread)
4926                    (not (gnus-data-unread-p (car arts)))))
4927       (setq arts (cdr arts)))
4928     (when (setq result
4929                 (if unread
4930                     (progn
4931                       (while arts
4932                         (when (gnus-data-unread-p (car arts))
4933                           (setq result (car arts)
4934                                 arts nil))
4935                         (setq arts (cdr arts)))
4936                       result)
4937                   (car arts)))
4938       (goto-char (gnus-data-pos result))
4939       (gnus-data-number result))))
4940
4941 (defun gnus-summary-find-subject (subject &optional unread backward article)
4942   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4943          (article (or article (gnus-summary-article-number)))
4944          (articles (gnus-data-list backward))
4945          (arts (gnus-data-find-list article articles))
4946          result)
4947     (when (or (not gnus-summary-check-current)
4948               (not unread)
4949               (not (gnus-data-unread-p (car arts))))
4950       (setq arts (cdr arts)))
4951     (while arts
4952       (and (or (not unread)
4953                (gnus-data-unread-p (car arts)))
4954            (vectorp (gnus-data-header (car arts)))
4955            (gnus-subject-equal
4956             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4957            (setq result (car arts)
4958                  arts nil))
4959       (setq arts (cdr arts)))
4960     (and result
4961          (goto-char (gnus-data-pos result))
4962          (gnus-data-number result))))
4963
4964 (defun gnus-summary-search-forward (&optional unread subject backward)
4965   "Search forward for an article.
4966 If UNREAD, look for unread articles.  If SUBJECT, look for
4967 articles with that subject.  If BACKWARD, search backward instead."
4968   (cond (subject (gnus-summary-find-subject subject unread backward))
4969         (backward (gnus-summary-find-prev unread))
4970         (t (gnus-summary-find-next unread))))
4971
4972 (defun gnus-recenter (&optional n)
4973   "Center point in window and redisplay frame.
4974 Also do horizontal recentering."
4975   (interactive "P")
4976   (when (and gnus-auto-center-summary
4977              (not (eq gnus-auto-center-summary 'vertical)))
4978     (gnus-horizontal-recenter))
4979   (recenter n))
4980
4981 (defun gnus-summary-recenter ()
4982   "Center point in the summary window.
4983 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4984 displayed, no centering will be performed."
4985   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4986   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4987   (let* ((top (cond ((< (window-height) 4) 0)
4988                     ((< (window-height) 7) 1)
4989                     (t (if (numberp gnus-auto-center-summary)
4990                            gnus-auto-center-summary
4991                          2))))
4992          (height (1- (window-height)))
4993          (bottom (save-excursion (goto-char (point-max))
4994                                  (forward-line (- height))
4995                                  (point)))
4996          (window (get-buffer-window (current-buffer))))
4997     ;; The user has to want it.
4998     (when gnus-auto-center-summary
4999       (when (get-buffer-window gnus-article-buffer)
5000         ;; Only do recentering when the article buffer is displayed,
5001         ;; Set the window start to either `bottom', which is the biggest
5002         ;; possible valid number, or the second line from the top,
5003         ;; whichever is the least.
5004         (set-window-start
5005          window (min bottom (save-excursion
5006                               (forward-line (- top)) (point)))))
5007       ;; Do horizontal recentering while we're at it.
5008       (when (and (get-buffer-window (current-buffer) t)
5009                  (not (eq gnus-auto-center-summary 'vertical)))
5010         (let ((selected (selected-window)))
5011           (select-window (get-buffer-window (current-buffer) t))
5012           (gnus-summary-position-point)
5013           (gnus-horizontal-recenter)
5014           (select-window selected))))))
5015
5016 (defun gnus-summary-jump-to-group (newsgroup)
5017   "Move point to NEWSGROUP in group mode buffer."
5018   ;; Keep update point of group mode buffer if visible.
5019   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5020       (save-window-excursion
5021         ;; Take care of tree window mode.
5022         (when (get-buffer-window gnus-group-buffer)
5023           (pop-to-buffer gnus-group-buffer))
5024         (gnus-group-jump-to-group newsgroup))
5025     (save-excursion
5026       ;; Take care of tree window mode.
5027       (if (get-buffer-window gnus-group-buffer)
5028           (pop-to-buffer gnus-group-buffer)
5029         (set-buffer gnus-group-buffer))
5030       (gnus-group-jump-to-group newsgroup))))
5031
5032 ;; This function returns a list of article numbers based on the
5033 ;; difference between the ranges of read articles in this group and
5034 ;; the range of active articles.
5035 (defun gnus-list-of-unread-articles (group)
5036   (let* ((read (gnus-info-read (gnus-get-info group)))
5037          (active (or (gnus-active group) (gnus-activate-group group)))
5038          (last (cdr active))
5039          first nlast unread)
5040     ;; If none are read, then all are unread.
5041     (if (not read)
5042         (setq first (car active))
5043       ;; If the range of read articles is a single range, then the
5044       ;; first unread article is the article after the last read
5045       ;; article.  Sounds logical, doesn't it?
5046       (if (not (listp (cdr read)))
5047           (setq first (max (car active) (1+ (cdr read))))
5048         ;; `read' is a list of ranges.
5049         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5050                                   (caar read)))
5051                   1)
5052           (setq first (car active)))
5053         (while read
5054           (when first
5055             (while (< first nlast)
5056               (push first unread)
5057               (setq first (1+ first))))
5058           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5059           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5060           (setq read (cdr read)))))
5061     ;; And add the last unread articles.
5062     (while (<= first last)
5063       (push first unread)
5064       (setq first (1+ first)))
5065     ;; Return the list of unread articles.
5066     (delq 0 (nreverse unread))))
5067
5068 (defun gnus-list-of-read-articles (group)
5069   "Return a list of unread, unticked and non-dormant articles."
5070   (let* ((info (gnus-get-info group))
5071          (marked (gnus-info-marks info))
5072          (active (gnus-active group)))
5073     (and info active
5074          (gnus-set-difference
5075           (gnus-sorted-complement
5076            (gnus-uncompress-range active)
5077            (gnus-list-of-unread-articles group))
5078           (append
5079            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5080            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5081
5082 ;; Various summary commands
5083
5084 (defun gnus-summary-select-article-buffer ()
5085   "Reconfigure windows to show article buffer."
5086   (interactive)
5087   (if (not (gnus-buffer-live-p gnus-article-buffer))
5088       (error "There is no article buffer for this summary buffer")
5089     (gnus-configure-windows 'article)
5090     (select-window (get-buffer-window gnus-article-buffer))))
5091
5092 (defun gnus-summary-universal-argument (arg)
5093   "Perform any operation on all articles that are process/prefixed."
5094   (interactive "P")
5095   (let ((articles (gnus-summary-work-articles arg))
5096         func article)
5097     (if (eq
5098          (setq
5099           func
5100           (key-binding
5101            (read-key-sequence
5102             (substitute-command-keys
5103              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5104              ))))
5105          'undefined)
5106         (gnus-error 1 "Undefined key")
5107       (save-excursion
5108         (while articles
5109           (gnus-summary-goto-subject (setq article (pop articles)))
5110           (let (gnus-newsgroup-processable)
5111             (command-execute func))
5112           (gnus-summary-remove-process-mark article)))))
5113   (gnus-summary-position-point))
5114
5115 (defun gnus-summary-toggle-truncation (&optional arg)
5116   "Toggle truncation of summary lines.
5117 With arg, turn line truncation on iff arg is positive."
5118   (interactive "P")
5119   (setq truncate-lines
5120         (if (null arg) (not truncate-lines)
5121           (> (prefix-numeric-value arg) 0)))
5122   (redraw-display))
5123
5124 (defun gnus-summary-reselect-current-group (&optional all rescan)
5125   "Exit and then reselect the current newsgroup.
5126 The prefix argument ALL means to select all articles."
5127   (interactive "P")
5128   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5129     (error "Ephemeral groups can't be reselected"))
5130   (let ((current-subject (gnus-summary-article-number))
5131         (group gnus-newsgroup-name))
5132     (setq gnus-newsgroup-begin nil)
5133     (gnus-summary-exit)
5134     ;; We have to adjust the point of group mode buffer because
5135     ;; point was moved to the next unread newsgroup by exiting.
5136     (gnus-summary-jump-to-group group)
5137     (when rescan
5138       (save-excursion
5139         (gnus-group-get-new-news-this-group 1)))
5140     (gnus-group-read-group all t)
5141     (gnus-summary-goto-subject current-subject nil t)))
5142
5143 (defun gnus-summary-rescan-group (&optional all)
5144   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5145   (interactive "P")
5146   (gnus-summary-reselect-current-group all t))
5147
5148 (defun gnus-summary-update-info (&optional non-destructive)
5149   (save-excursion
5150     (let ((group gnus-newsgroup-name))
5151       (when group
5152         (when gnus-newsgroup-kill-headers
5153           (setq gnus-newsgroup-killed
5154                 (gnus-compress-sequence
5155                  (nconc
5156                   (gnus-set-sorted-intersection
5157                    (gnus-uncompress-range gnus-newsgroup-killed)
5158                    (setq gnus-newsgroup-unselected
5159                          (sort gnus-newsgroup-unselected '<)))
5160                   (setq gnus-newsgroup-unreads
5161                         (sort gnus-newsgroup-unreads '<)))
5162                  t)))
5163         (unless (listp (cdr gnus-newsgroup-killed))
5164           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5165         (let ((headers gnus-newsgroup-headers))
5166           ;; Set the new ranges of read articles.
5167           (save-excursion
5168             (set-buffer gnus-group-buffer)
5169             (gnus-undo-force-boundary))
5170           (gnus-update-read-articles
5171            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5172           ;; Set the current article marks.
5173           (let ((gnus-newsgroup-scored
5174                  (if (and (not gnus-save-score)
5175                           (not non-destructive))
5176                      nil
5177                    gnus-newsgroup-scored)))
5178             (save-excursion
5179               (gnus-update-marks)))
5180           ;; Do the cross-ref thing.
5181           (when gnus-use-cross-reference
5182             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5183           ;; Do not switch windows but change the buffer to work.
5184           (set-buffer gnus-group-buffer)
5185           (unless (gnus-ephemeral-group-p group)
5186             (gnus-group-update-group group)))))))
5187
5188 (defun gnus-summary-save-newsrc (&optional force)
5189   "Save the current number of read/marked articles in the dribble buffer.
5190 The dribble buffer will then be saved.
5191 If FORCE (the prefix), also save the .newsrc file(s)."
5192   (interactive "P")
5193   (gnus-summary-update-info t)
5194   (if force
5195       (gnus-save-newsrc-file)
5196     (gnus-dribble-save)))
5197
5198 (defun gnus-summary-exit (&optional temporary)
5199   "Exit reading current newsgroup, and then return to group selection mode.
5200 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5201   (interactive)
5202   (gnus-set-global-variables)
5203   (gnus-kill-save-kill-buffer)
5204   (gnus-async-halt-prefetch)
5205   (let* ((group gnus-newsgroup-name)
5206          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5207          (mode major-mode)
5208          (group-point nil)
5209          (buf (current-buffer)))
5210     (unless quit-config
5211       ;; Do adaptive scoring, and possibly save score files.
5212       (when gnus-newsgroup-adaptive
5213         (gnus-score-adaptive))
5214       (when gnus-use-scoring
5215         (gnus-score-save)))
5216     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5217     ;; If we have several article buffers, we kill them at exit.
5218     (unless gnus-single-article-buffer
5219       (gnus-kill-buffer gnus-original-article-buffer)
5220       (setq gnus-article-current nil))
5221     (when gnus-use-cache
5222       (gnus-cache-possibly-remove-articles)
5223       (gnus-cache-save-buffers))
5224     (gnus-async-prefetch-remove-group group)
5225     (when gnus-suppress-duplicates
5226       (gnus-dup-enter-articles))
5227     (when gnus-use-trees
5228       (gnus-tree-close group))
5229     ;; Remove entries for this group.
5230     (nnmail-purge-split-history (gnus-group-real-name group))
5231     ;; Make all changes in this group permanent.
5232     (unless quit-config
5233       (gnus-run-hooks 'gnus-exit-group-hook)
5234       (gnus-summary-update-info))
5235     (gnus-close-group group)
5236     ;; Make sure where we were, and go to next newsgroup.
5237     (set-buffer gnus-group-buffer)
5238     (unless quit-config
5239       (gnus-group-jump-to-group group))
5240     (gnus-run-hooks 'gnus-summary-exit-hook)
5241     (unless (or quit-config
5242                 ;; If this group has disappeared from the summary
5243                 ;; buffer, don't skip forwards.
5244                 (not (string= group (gnus-group-group-name))))
5245       (gnus-group-next-unread-group 1))
5246     (setq group-point (point))
5247     (if temporary
5248         nil                             ;Nothing to do.
5249       ;; If we have several article buffers, we kill them at exit.
5250       (unless gnus-single-article-buffer
5251         (gnus-kill-buffer gnus-article-buffer)
5252         (gnus-kill-buffer gnus-original-article-buffer)
5253         (setq gnus-article-current nil))
5254       (set-buffer buf)
5255       (if (not gnus-kill-summary-on-exit)
5256           (gnus-deaden-summary)
5257         ;; We set all buffer-local variables to nil.  It is unclear why
5258         ;; this is needed, but if we don't, buffer-local variables are
5259         ;; not garbage-collected, it seems.  This would the lead to en
5260         ;; ever-growing Emacs.
5261         (gnus-summary-clear-local-variables)
5262         (when (get-buffer gnus-article-buffer)
5263           (bury-buffer gnus-article-buffer))
5264         ;; We clear the global counterparts of the buffer-local
5265         ;; variables as well, just to be on the safe side.
5266         (set-buffer gnus-group-buffer)
5267         (gnus-summary-clear-local-variables)
5268         ;; Return to group mode buffer.
5269         (when (eq mode 'gnus-summary-mode)
5270           (gnus-kill-buffer buf)))
5271       (setq gnus-current-select-method gnus-select-method)
5272       (pop-to-buffer gnus-group-buffer)
5273       (if (not quit-config)
5274           (progn
5275             (goto-char group-point)
5276             (gnus-configure-windows 'group 'force))
5277         (gnus-handle-ephemeral-exit quit-config))
5278       ;; Clear the current group name.
5279       (unless quit-config
5280         (setq gnus-newsgroup-name nil)))))
5281
5282 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5283 (defun gnus-summary-exit-no-update (&optional no-questions)
5284   "Quit reading current newsgroup without updating read article info."
5285   (interactive)
5286   (let* ((group gnus-newsgroup-name)
5287          (quit-config (gnus-group-quit-config group)))
5288     (when (or no-questions
5289               gnus-expert-user
5290               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5291       (gnus-async-halt-prefetch)
5292       (mapcar 'funcall
5293               (delq 'gnus-summary-expire-articles
5294                     (copy-list gnus-summary-prepare-exit-hook)))
5295       ;; If we have several article buffers, we kill them at exit.
5296       (unless gnus-single-article-buffer
5297         (gnus-kill-buffer gnus-article-buffer)
5298         (gnus-kill-buffer gnus-original-article-buffer)
5299         (setq gnus-article-current nil))
5300       (if (not gnus-kill-summary-on-exit)
5301           (gnus-deaden-summary)
5302         (gnus-close-group group)
5303         (gnus-summary-clear-local-variables)
5304         (set-buffer gnus-group-buffer)
5305         (gnus-summary-clear-local-variables)
5306         (when (get-buffer gnus-summary-buffer)
5307           (kill-buffer gnus-summary-buffer)))
5308       (unless gnus-single-article-buffer
5309         (setq gnus-article-current nil))
5310       (when gnus-use-trees
5311         (gnus-tree-close group))
5312       (gnus-async-prefetch-remove-group group)
5313       (when (get-buffer gnus-article-buffer)
5314         (bury-buffer gnus-article-buffer))
5315       ;; Return to the group buffer.
5316       (gnus-configure-windows 'group 'force)
5317       ;; Clear the current group name.
5318       (setq gnus-newsgroup-name nil)
5319       (when (equal (gnus-group-group-name) group)
5320         (gnus-group-next-unread-group 1))
5321       (when quit-config
5322         (gnus-handle-ephemeral-exit quit-config)))))
5323
5324 (defun gnus-handle-ephemeral-exit (quit-config)
5325   "Handle movement when leaving an ephemeral group.
5326 The state which existed when entering the ephemeral is reset."
5327   (if (not (buffer-name (car quit-config)))
5328       (gnus-configure-windows 'group 'force)
5329     (set-buffer (car quit-config))
5330     (cond ((eq major-mode 'gnus-summary-mode)
5331            (gnus-set-global-variables))
5332           ((eq major-mode 'gnus-article-mode)
5333            (save-excursion
5334              ;; The `gnus-summary-buffer' variable may point
5335              ;; to the old summary buffer when using a single
5336              ;; article buffer.
5337              (unless (gnus-buffer-live-p gnus-summary-buffer)
5338                (set-buffer gnus-group-buffer))
5339              (set-buffer gnus-summary-buffer)
5340              (gnus-set-global-variables))))
5341     (if (or (eq (cdr quit-config) 'article)
5342             (eq (cdr quit-config) 'pick))
5343         (progn
5344           ;; The current article may be from the ephemeral group
5345           ;; thus it is best that we reload this article
5346           (gnus-summary-show-article)
5347           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5348               (gnus-configure-windows 'pick 'force)
5349             (gnus-configure-windows (cdr quit-config) 'force)))
5350       (gnus-configure-windows (cdr quit-config) 'force))
5351     (when (eq major-mode 'gnus-summary-mode)
5352       (gnus-summary-next-subject 1 nil t)
5353       (gnus-summary-recenter)
5354       (gnus-summary-position-point))))
5355
5356 (defun gnus-summary-preview-mime-message ()
5357   "MIME decode and play this message."
5358   (interactive)
5359   (let ((gnus-break-pages nil)
5360         (gnus-show-mime t))
5361     (gnus-summary-select-article gnus-show-all-headers t))
5362   (select-window (get-buffer-window gnus-article-buffer)))
5363
5364 ;;; Dead summaries.
5365
5366 (defvar gnus-dead-summary-mode-map nil)
5367
5368 (unless gnus-dead-summary-mode-map
5369   (setq gnus-dead-summary-mode-map (make-keymap))
5370   (suppress-keymap gnus-dead-summary-mode-map)
5371   (substitute-key-definition
5372    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5373   (let ((keys '("\C-d" "\r" "\177" [delete])))
5374     (while keys
5375       (define-key gnus-dead-summary-mode-map
5376         (pop keys) 'gnus-summary-wake-up-the-dead))))
5377
5378 (defvar gnus-dead-summary-mode nil
5379   "Minor mode for Gnus summary buffers.")
5380
5381 (defun gnus-dead-summary-mode (&optional arg)
5382   "Minor mode for Gnus summary buffers."
5383   (interactive "P")
5384   (when (eq major-mode 'gnus-summary-mode)
5385     (make-local-variable 'gnus-dead-summary-mode)
5386     (setq gnus-dead-summary-mode
5387           (if (null arg) (not gnus-dead-summary-mode)
5388             (> (prefix-numeric-value arg) 0)))
5389     (when gnus-dead-summary-mode
5390       (gnus-add-minor-mode
5391        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5392
5393 (defun gnus-deaden-summary ()
5394   "Make the current summary buffer into a dead summary buffer."
5395   ;; Kill any previous dead summary buffer.
5396   (when (and gnus-dead-summary
5397              (buffer-name gnus-dead-summary))
5398     (save-excursion
5399       (set-buffer gnus-dead-summary)
5400       (when gnus-dead-summary-mode
5401         (kill-buffer (current-buffer)))))
5402   ;; Make this the current dead summary.
5403   (setq gnus-dead-summary (current-buffer))
5404   (gnus-dead-summary-mode 1)
5405   (let ((name (buffer-name)))
5406     (when (string-match "Summary" name)
5407       (rename-buffer
5408        (concat (substring name 0 (match-beginning 0)) "Dead "
5409                (substring name (match-beginning 0)))
5410        t))))
5411
5412 (defun gnus-kill-or-deaden-summary (buffer)
5413   "Kill or deaden the summary BUFFER."
5414   (save-excursion
5415     (when (and (buffer-name buffer)
5416                (not gnus-single-article-buffer))
5417       (save-excursion
5418         (set-buffer buffer)
5419         (gnus-kill-buffer gnus-article-buffer)
5420         (gnus-kill-buffer gnus-original-article-buffer)))
5421     (cond (gnus-kill-summary-on-exit
5422            (when (and gnus-use-trees
5423                       (gnus-buffer-exists-p buffer))
5424              (save-excursion
5425                (set-buffer buffer)
5426                (gnus-tree-close gnus-newsgroup-name)))
5427            (gnus-kill-buffer buffer))
5428           ((gnus-buffer-exists-p buffer)
5429            (save-excursion
5430              (set-buffer buffer)
5431              (gnus-deaden-summary))))))
5432
5433 (defun gnus-summary-wake-up-the-dead (&rest args)
5434   "Wake up the dead summary buffer."
5435   (interactive)
5436   (gnus-dead-summary-mode -1)
5437   (let ((name (buffer-name)))
5438     (when (string-match "Dead " name)
5439       (rename-buffer
5440        (concat (substring name 0 (match-beginning 0))
5441                (substring name (match-end 0)))
5442        t)))
5443   (gnus-message 3 "This dead summary is now alive again"))
5444
5445 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5446 (defun gnus-summary-fetch-faq (&optional faq-dir)
5447   "Fetch the FAQ for the current group.
5448 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5449 in."
5450   (interactive
5451    (list
5452     (when current-prefix-arg
5453       (completing-read
5454        "Faq dir: " (and (listp gnus-group-faq-directory)
5455                         (mapcar (lambda (file) (list file))
5456                                 gnus-group-faq-directory))))))
5457   (let (gnus-faq-buffer)
5458     (when (setq gnus-faq-buffer
5459                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5460       (gnus-configure-windows 'summary-faq))))
5461
5462 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5463 (defun gnus-summary-describe-group (&optional force)
5464   "Describe the current newsgroup."
5465   (interactive "P")
5466   (gnus-group-describe-group force gnus-newsgroup-name))
5467
5468 (defun gnus-summary-describe-briefly ()
5469   "Describe summary mode commands briefly."
5470   (interactive)
5471   (gnus-message 6 (substitute-command-keys "\\<gnus-summary-mode-map>\\[gnus-summary-next-page]:Select  \\[gnus-summary-next-unread-article]:Forward  \\[gnus-summary-prev-unread-article]:Backward  \\[gnus-summary-exit]:Exit  \\[gnus-info-find-node]:Run Info        \\[gnus-summary-describe-briefly]:This help")))
5472
5473 ;; Walking around group mode buffer from summary mode.
5474
5475 (defun gnus-summary-next-group (&optional no-article target-group backward)
5476   "Exit current newsgroup and then select next unread newsgroup.
5477 If prefix argument NO-ARTICLE is non-nil, no article is selected
5478 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5479 previous group instead."
5480   (interactive "P")
5481   ;; Stop pre-fetching.
5482   (gnus-async-halt-prefetch)
5483   (let ((current-group gnus-newsgroup-name)
5484         (current-buffer (current-buffer))
5485         entered)
5486     ;; First we semi-exit this group to update Xrefs and all variables.
5487     ;; We can't do a real exit, because the window conf must remain
5488     ;; the same in case the user is prompted for info, and we don't
5489     ;; want the window conf to change before that...
5490     (gnus-summary-exit t)
5491     (while (not entered)
5492       ;; Then we find what group we are supposed to enter.
5493       (set-buffer gnus-group-buffer)
5494       (gnus-group-jump-to-group current-group)
5495       (setq target-group
5496             (or target-group
5497                 (if (eq gnus-keep-same-level 'best)
5498                     (gnus-summary-best-group gnus-newsgroup-name)
5499                   (gnus-summary-search-group backward gnus-keep-same-level))))
5500       (if (not target-group)
5501           ;; There are no further groups, so we return to the group
5502           ;; buffer.
5503           (progn
5504             (gnus-message 5 "Returning to the group buffer")
5505             (setq entered t)
5506             (when (gnus-buffer-live-p current-buffer)
5507               (set-buffer current-buffer)
5508               (gnus-summary-exit))
5509             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5510         ;; We try to enter the target group.
5511         (gnus-group-jump-to-group target-group)
5512         (let ((unreads (gnus-group-group-unread)))
5513           (if (and (or (eq t unreads)
5514                        (and unreads (not (zerop unreads))))
5515                    (gnus-summary-read-group
5516                     target-group nil no-article
5517                     (and (buffer-name current-buffer) current-buffer)
5518                     nil backward))
5519               (setq entered t)
5520             (setq current-group target-group
5521                   target-group nil)))))))
5522
5523 (defun gnus-summary-prev-group (&optional no-article)
5524   "Exit current newsgroup and then select previous unread newsgroup.
5525 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5526   (interactive "P")
5527   (gnus-summary-next-group no-article nil t))
5528
5529 ;; Walking around summary lines.
5530
5531 (defun gnus-summary-first-subject (&optional unread undownloaded)
5532   "Go to the first unread subject.
5533 If UNREAD is non-nil, go to the first unread article.
5534 Returns the article selected or nil if there are no unread articles."
5535   (interactive "P")
5536   (prog1
5537       (cond
5538        ;; Empty summary.
5539        ((null gnus-newsgroup-data)
5540         (gnus-message 3 "No articles in the group")
5541         nil)
5542        ;; Pick the first article.
5543        ((not unread)
5544         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5545         (gnus-data-number (car gnus-newsgroup-data)))
5546        ;; No unread articles.
5547        ((null gnus-newsgroup-unreads)
5548         (gnus-message 3 "No more unread articles")
5549         nil)
5550        ;; Find the first unread article.
5551        (t
5552         (let ((data gnus-newsgroup-data))
5553           (while (and data
5554                       (and (not (and undownloaded
5555                                      (eq gnus-undownloaded-mark
5556                                          (gnus-data-mark (car data)))))
5557                            (not (gnus-data-unread-p (car data)))))
5558             (setq data (cdr data)))
5559           (when data
5560             (goto-char (gnus-data-pos (car data)))
5561             (gnus-data-number (car data))))))
5562     (gnus-summary-position-point)))
5563
5564 (defun gnus-summary-next-subject (n &optional unread dont-display)
5565   "Go to next N'th summary line.
5566 If N is negative, go to the previous N'th subject line.
5567 If UNREAD is non-nil, only unread articles are selected.
5568 The difference between N and the actual number of steps taken is
5569 returned."
5570   (interactive "p")
5571   (let ((backward (< n 0))
5572         (n (abs n)))
5573     (while (and (> n 0)
5574                 (if backward
5575                     (gnus-summary-find-prev unread)
5576                   (gnus-summary-find-next unread)))
5577       (gnus-summary-show-thread)
5578       (setq n (1- n)))
5579     (when (/= 0 n)
5580       (gnus-message 7 "No more%s articles"
5581                     (if unread " unread" "")))
5582     (unless dont-display
5583       (gnus-summary-recenter)
5584       (gnus-summary-position-point))
5585     n))
5586
5587 (defun gnus-summary-next-unread-subject (n)
5588   "Go to next N'th unread summary line."
5589   (interactive "p")
5590   (gnus-summary-next-subject n t))
5591
5592 (defun gnus-summary-prev-subject (n &optional unread)
5593   "Go to previous N'th summary line.
5594 If optional argument UNREAD is non-nil, only unread article is selected."
5595   (interactive "p")
5596   (gnus-summary-next-subject (- n) unread))
5597
5598 (defun gnus-summary-prev-unread-subject (n)
5599   "Go to previous N'th unread summary line."
5600   (interactive "p")
5601   (gnus-summary-next-subject (- n) t))
5602
5603 (defun gnus-summary-goto-subject (article &optional force silent)
5604   "Go the subject line of ARTICLE.
5605 If FORCE, also allow jumping to articles not currently shown."
5606   (interactive "nArticle number: ")
5607   (let ((b (point))
5608         (data (gnus-data-find article)))
5609     ;; We read in the article if we have to.
5610     (and (not data)
5611          force
5612          (gnus-summary-insert-subject
5613           article
5614           (if (or (numberp force) (vectorp force)) force)
5615           t)
5616          (setq data (gnus-data-find article)))
5617     (goto-char b)
5618     (if (not data)
5619         (progn
5620           (unless silent
5621             (gnus-message 3 "Can't find article %d" article))
5622           nil)
5623       (goto-char (gnus-data-pos data))
5624       (gnus-summary-position-point)
5625       article)))
5626
5627 ;; Walking around summary lines with displaying articles.
5628
5629 (defun gnus-summary-expand-window (&optional arg)
5630   "Make the summary buffer take up the entire Emacs frame.
5631 Given a prefix, will force an `article' buffer configuration."
5632   (interactive "P")
5633   (if arg
5634       (gnus-configure-windows 'article 'force)
5635     (gnus-configure-windows 'summary 'force)))
5636
5637 (defun gnus-summary-display-article (article &optional all-header)
5638   "Display ARTICLE in article buffer."
5639   (gnus-set-global-variables)
5640   (if (null article)
5641       nil
5642     (prog1
5643         (if gnus-summary-display-article-function
5644             (funcall gnus-summary-display-article-function article all-header)
5645           (gnus-article-prepare article all-header))
5646       (gnus-run-hooks 'gnus-select-article-hook)
5647       (when (and gnus-current-article
5648                  (not (zerop gnus-current-article)))
5649         (gnus-summary-goto-subject gnus-current-article))
5650       (gnus-summary-recenter)
5651       (when (and gnus-use-trees gnus-show-threads)
5652         (gnus-possibly-generate-tree article)
5653         (gnus-highlight-selected-tree article))
5654       ;; Successfully display article.
5655       (gnus-article-set-window-start
5656        (cdr (assq article gnus-newsgroup-bookmarks))))))
5657
5658 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5659   "Select the current article.
5660 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5661 non-nil, the article will be re-fetched even if it already present in
5662 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5663 be displayed."
5664   ;; Make sure we are in the summary buffer to work around bbdb bug.
5665   (unless (eq major-mode 'gnus-summary-mode)
5666     (set-buffer gnus-summary-buffer))
5667   (let ((article (or article (gnus-summary-article-number)))
5668         (all-headers (not (not all-headers))) ;Must be T or NIL.
5669         gnus-summary-display-article-function
5670         did)
5671     (and (not pseudo)
5672          (gnus-summary-article-pseudo-p article)
5673          (error "This is a pseudo-article"))
5674     (prog1
5675         (save-excursion
5676           (set-buffer gnus-summary-buffer)
5677           (if (or (and gnus-single-article-buffer
5678                        (or (null gnus-current-article)
5679                            (null gnus-article-current)
5680                            (null (get-buffer gnus-article-buffer))
5681                            (not (eq article (cdr gnus-article-current)))
5682                            (not (equal (car gnus-article-current)
5683                                        gnus-newsgroup-name))))
5684                   (and (not gnus-single-article-buffer)
5685                        (or (null gnus-current-article)
5686                            (not (eq gnus-current-article article))))
5687                   force)
5688               ;; The requested article is different from the current article.
5689               (prog1
5690                   (gnus-summary-display-article article all-headers)
5691                 (setq did article)
5692                 (when (or all-headers gnus-show-all-headers)
5693                   (gnus-article-show-all-headers)))
5694             (when (or all-headers gnus-show-all-headers)
5695               (gnus-article-show-all-headers))
5696             'old))
5697       (when did
5698         (gnus-article-set-window-start
5699          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5700
5701 (defun gnus-summary-set-current-mark (&optional current-mark)
5702   "Obsolete function."
5703   nil)
5704
5705 (defun gnus-summary-next-article (&optional unread subject backward push)
5706   "Select the next article.
5707 If UNREAD, only unread articles are selected.
5708 If SUBJECT, only articles with SUBJECT are selected.
5709 If BACKWARD, the previous article is selected instead of the next."
5710   (interactive "P")
5711   (cond
5712    ;; Is there such an article?
5713    ((and (gnus-summary-search-forward unread subject backward)
5714          (or (gnus-summary-display-article (gnus-summary-article-number))
5715              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5716     (gnus-summary-position-point))
5717    ;; If not, we try the first unread, if that is wanted.
5718    ((and subject
5719          gnus-auto-select-same
5720          (gnus-summary-first-unread-article))
5721     (gnus-summary-position-point)
5722     (gnus-message 6 "Wrapped"))
5723    ;; Try to get next/previous article not displayed in this group.
5724    ((and gnus-auto-extend-newsgroup
5725          (not unread) (not subject))
5726     (gnus-summary-goto-article
5727      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5728      nil (count-lines (point-min) (point))))
5729    ;; Go to next/previous group.
5730    (t
5731     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5732       (gnus-summary-jump-to-group gnus-newsgroup-name))
5733     (let ((cmd last-command-char)
5734           (point
5735            (save-excursion
5736              (set-buffer gnus-group-buffer)
5737              (point)))
5738           (group
5739            (if (eq gnus-keep-same-level 'best)
5740                (gnus-summary-best-group gnus-newsgroup-name)
5741              (gnus-summary-search-group backward gnus-keep-same-level))))
5742       ;; For some reason, the group window gets selected.  We change
5743       ;; it back.
5744       (select-window (get-buffer-window (current-buffer)))
5745       ;; Select next unread newsgroup automagically.
5746       (cond
5747        ((or (not gnus-auto-select-next)
5748             (not cmd))
5749         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5750        ((or (eq gnus-auto-select-next 'quietly)
5751             (and (eq gnus-auto-select-next 'slightly-quietly)
5752                  push)
5753             (and (eq gnus-auto-select-next 'almost-quietly)
5754                  (gnus-summary-last-article-p)))
5755         ;; Select quietly.
5756         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5757             (gnus-summary-exit)
5758           (gnus-message 7 "No more%s articles (%s)..."
5759                         (if unread " unread" "")
5760                         (if group (concat "selecting " group)
5761                           "exiting"))
5762           (gnus-summary-next-group nil group backward)))
5763        (t
5764         (when (gnus-key-press-event-p last-input-event)
5765           (gnus-summary-walk-group-buffer
5766            gnus-newsgroup-name cmd unread backward point))))))))
5767
5768 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5769   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5770                       (?\C-p (gnus-group-prev-unread-group 1))))
5771         (cursor-in-echo-area t)
5772         keve key group ended)
5773     (save-excursion
5774       (set-buffer gnus-group-buffer)
5775       (goto-char start)
5776       (setq group
5777             (if (eq gnus-keep-same-level 'best)
5778                 (gnus-summary-best-group gnus-newsgroup-name)
5779               (gnus-summary-search-group backward gnus-keep-same-level))))
5780     (while (not ended)
5781       (gnus-message
5782        5 "No more%s articles%s" (if unread " unread" "")
5783        (if (and group
5784                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5785            (format " (Type %s for %s [%s])"
5786                    (single-key-description cmd) group
5787                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5788          (format " (Type %s to exit %s)"
5789                  (single-key-description cmd)
5790                  gnus-newsgroup-name)))
5791       ;; Confirm auto selection.
5792       (setq key (car (setq keve (gnus-read-event-char))))
5793       (setq ended t)
5794       (cond
5795        ((assq key keystrokes)
5796         (let ((obuf (current-buffer)))
5797           (switch-to-buffer gnus-group-buffer)
5798           (when group
5799             (gnus-group-jump-to-group group))
5800           (eval (cadr (assq key keystrokes)))
5801           (setq group (gnus-group-group-name))
5802           (switch-to-buffer obuf))
5803         (setq ended nil))
5804        ((equal key cmd)
5805         (if (or (not group)
5806                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5807             (gnus-summary-exit)
5808           (gnus-summary-next-group nil group backward)))
5809        (t
5810         (push (cdr keve) unread-command-events))))))
5811
5812 (defun gnus-summary-next-unread-article ()
5813   "Select unread article after current one."
5814   (interactive)
5815   (gnus-summary-next-article
5816    (or (not (eq gnus-summary-goto-unread 'never))
5817        (gnus-summary-last-article-p (gnus-summary-article-number)))
5818    (and gnus-auto-select-same
5819         (gnus-summary-article-subject))))
5820
5821 (defun gnus-summary-prev-article (&optional unread subject)
5822   "Select the article after the current one.
5823 If UNREAD is non-nil, only unread articles are selected."
5824   (interactive "P")
5825   (gnus-summary-next-article unread subject t))
5826
5827 (defun gnus-summary-prev-unread-article ()
5828   "Select unread article before current one."
5829   (interactive)
5830   (gnus-summary-prev-article
5831    (or (not (eq gnus-summary-goto-unread 'never))
5832        (gnus-summary-first-article-p (gnus-summary-article-number)))
5833    (and gnus-auto-select-same
5834         (gnus-summary-article-subject))))
5835
5836 (defun gnus-summary-next-page (&optional lines circular)
5837   "Show next page of the selected article.
5838 If at the end of the current article, select the next article.
5839 LINES says how many lines should be scrolled up.
5840
5841 If CIRCULAR is non-nil, go to the start of the article instead of
5842 selecting the next article when reaching the end of the current
5843 article."
5844   (interactive "P")
5845   (setq gnus-summary-buffer (current-buffer))
5846   (gnus-set-global-variables)
5847   (let ((article (gnus-summary-article-number))
5848         (article-window (get-buffer-window gnus-article-buffer t))
5849         endp)
5850     ;; If the buffer is empty, we have no article.
5851     (unless article
5852       (error "No article to select"))
5853     (gnus-configure-windows 'article)
5854     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5855         (if (and (eq gnus-summary-goto-unread 'never)
5856                  (not (gnus-summary-last-article-p article)))
5857             (gnus-summary-next-article)
5858           (gnus-summary-next-unread-article))
5859       (if (or (null gnus-current-article)
5860               (null gnus-article-current)
5861               (/= article (cdr gnus-article-current))
5862               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5863           ;; Selected subject is different from current article's.
5864           (gnus-summary-display-article article)
5865         (when article-window
5866           (gnus-eval-in-buffer-window gnus-article-buffer
5867             (setq endp (gnus-article-next-page lines)))
5868           (when endp
5869             (cond (circular
5870                    (gnus-summary-beginning-of-article))
5871                   (lines
5872                    (gnus-message 3 "End of message"))
5873                   ((null lines)
5874                    (if (and (eq gnus-summary-goto-unread 'never)
5875                             (not (gnus-summary-last-article-p article)))
5876                        (gnus-summary-next-article)
5877                      (gnus-summary-next-unread-article))))))))
5878     (gnus-summary-recenter)
5879     (gnus-summary-position-point)))
5880
5881 (defun gnus-summary-prev-page (&optional lines move)
5882   "Show previous page of selected article.
5883 Argument LINES specifies lines to be scrolled down.
5884 If MOVE, move to the previous unread article if point is at
5885 the beginning of the buffer."
5886   (interactive "P")
5887   (let ((article (gnus-summary-article-number))
5888         (article-window (get-buffer-window gnus-article-buffer t))
5889         endp)
5890     (gnus-configure-windows 'article)
5891     (if (or (null gnus-current-article)
5892             (null gnus-article-current)
5893             (/= article (cdr gnus-article-current))
5894             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5895         ;; Selected subject is different from current article's.
5896         (gnus-summary-display-article article)
5897       (gnus-summary-recenter)
5898       (when article-window
5899         (gnus-eval-in-buffer-window gnus-article-buffer
5900           (setq endp (gnus-article-prev-page lines)))
5901         (when (and move endp)
5902           (cond (lines
5903                  (gnus-message 3 "Beginning of message"))
5904                 ((null lines)
5905                  (if (and (eq gnus-summary-goto-unread 'never)
5906                           (not (gnus-summary-first-article-p article)))
5907                      (gnus-summary-prev-article)
5908                    (gnus-summary-prev-unread-article))))))))
5909   (gnus-summary-position-point))
5910
5911 (defun gnus-summary-prev-page-or-article (&optional lines)
5912   "Show previous page of selected article.
5913 Argument LINES specifies lines to be scrolled down.
5914 If at the beginning of the article, go to the next article."
5915   (interactive "P")
5916   (gnus-summary-prev-page lines t))
5917
5918 (defun gnus-summary-scroll-up (lines)
5919   "Scroll up (or down) one line current article.
5920 Argument LINES specifies lines to be scrolled up (or down if negative)."
5921   (interactive "p")
5922   (gnus-configure-windows 'article)
5923   (gnus-summary-show-thread)
5924   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5925     (gnus-eval-in-buffer-window gnus-article-buffer
5926       (cond ((> lines 0)
5927              (when (gnus-article-next-page lines)
5928                (gnus-message 3 "End of message")))
5929             ((< lines 0)
5930              (gnus-article-prev-page (- lines))))))
5931   (gnus-summary-recenter)
5932   (gnus-summary-position-point))
5933
5934 (defun gnus-summary-scroll-down (lines)
5935   "Scroll down (or up) one line current article.
5936 Argument LINES specifies lines to be scrolled down (or up if negative)."
5937   (interactive "p")
5938   (gnus-summary-scroll-up (- lines)))
5939
5940 (defun gnus-summary-next-same-subject ()
5941   "Select next article which has the same subject as current one."
5942   (interactive)
5943   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5944
5945 (defun gnus-summary-prev-same-subject ()
5946   "Select previous article which has the same subject as current one."
5947   (interactive)
5948   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5949
5950 (defun gnus-summary-next-unread-same-subject ()
5951   "Select next unread article which has the same subject as current one."
5952   (interactive)
5953   (gnus-summary-next-article t (gnus-summary-article-subject)))
5954
5955 (defun gnus-summary-prev-unread-same-subject ()
5956   "Select previous unread article which has the same subject as current one."
5957   (interactive)
5958   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5959
5960 (defun gnus-summary-first-unread-article ()
5961   "Select the first unread article.
5962 Return nil if there are no unread articles."
5963   (interactive)
5964   (prog1
5965       (when (gnus-summary-first-subject t)
5966         (gnus-summary-show-thread)
5967         (gnus-summary-first-subject t)
5968         (gnus-summary-display-article (gnus-summary-article-number)))
5969     (gnus-summary-position-point)))
5970
5971 (defun gnus-summary-first-unread-subject ()
5972   "Place the point on the subject line of the first unread article.
5973 Return nil if there are no unread articles."
5974   (interactive)
5975   (prog1
5976       (when (gnus-summary-first-subject t)
5977         (gnus-summary-show-thread)
5978         (gnus-summary-first-subject t))
5979     (gnus-summary-position-point)))
5980
5981 (defun gnus-summary-first-article ()
5982   "Select the first article.
5983 Return nil if there are no articles."
5984   (interactive)
5985   (prog1
5986       (when (gnus-summary-first-subject)
5987         (gnus-summary-show-thread)
5988         (gnus-summary-first-subject)
5989         (gnus-summary-display-article (gnus-summary-article-number)))
5990     (gnus-summary-position-point)))
5991
5992 (defun gnus-summary-best-unread-article ()
5993   "Select the unread article with the highest score."
5994   (interactive)
5995   (let ((best -1000000)
5996         (data gnus-newsgroup-data)
5997         article score)
5998     (while data
5999       (and (gnus-data-unread-p (car data))
6000            (> (setq score
6001                     (gnus-summary-article-score (gnus-data-number (car data))))
6002               best)
6003            (setq best score
6004                  article (gnus-data-number (car data))))
6005       (setq data (cdr data)))
6006     (prog1
6007         (if article
6008             (gnus-summary-goto-article article)
6009           (error "No unread articles"))
6010       (gnus-summary-position-point))))
6011
6012 (defun gnus-summary-last-subject ()
6013   "Go to the last displayed subject line in the group."
6014   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6015     (when article
6016       (gnus-summary-goto-subject article))))
6017
6018 (defun gnus-summary-goto-article (article &optional all-headers force)
6019   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6020 If ALL-HEADERS is non-nil, no header lines are hidden.
6021 If FORCE, go to the article even if it isn't displayed.  If FORCE
6022 is a number, it is the line the article is to be displayed on."
6023   (interactive
6024    (list
6025     (completing-read
6026      "Article number or Message-ID: "
6027      (mapcar (lambda (number) (list (int-to-string number)))
6028              gnus-newsgroup-limit))
6029     current-prefix-arg
6030     t))
6031   (prog1
6032       (if (and (stringp article)
6033                (string-match "@" article))
6034           (gnus-summary-refer-article article)
6035         (when (stringp article)
6036           (setq article (string-to-number article)))
6037         (if (gnus-summary-goto-subject article force)
6038             (gnus-summary-display-article article all-headers)
6039           (gnus-message 4 "Couldn't go to article %s" article) nil))
6040     (gnus-summary-position-point)))
6041
6042 (defun gnus-summary-goto-last-article ()
6043   "Go to the previously read article."
6044   (interactive)
6045   (prog1
6046       (when gnus-last-article
6047         (gnus-summary-goto-article gnus-last-article nil t))
6048     (gnus-summary-position-point)))
6049
6050 (defun gnus-summary-pop-article (number)
6051   "Pop one article off the history and go to the previous.
6052 NUMBER articles will be popped off."
6053   (interactive "p")
6054   (let (to)
6055     (setq gnus-newsgroup-history
6056           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6057     (if to
6058         (gnus-summary-goto-article (car to) nil t)
6059       (error "Article history empty")))
6060   (gnus-summary-position-point))
6061
6062 ;; Summary commands and functions for limiting the summary buffer.
6063
6064 (defun gnus-summary-limit-to-articles (n)
6065   "Limit the summary buffer to the next N articles.
6066 If not given a prefix, use the process marked articles instead."
6067   (interactive "P")
6068   (prog1
6069       (let ((articles (gnus-summary-work-articles n)))
6070         (setq gnus-newsgroup-processable nil)
6071         (gnus-summary-limit articles))
6072     (gnus-summary-position-point)))
6073
6074 (defun gnus-summary-pop-limit (&optional total)
6075   "Restore the previous limit.
6076 If given a prefix, remove all limits."
6077   (interactive "P")
6078   (when total
6079     (setq gnus-newsgroup-limits
6080           (list (mapcar (lambda (h) (mail-header-number h))
6081                         gnus-newsgroup-headers))))
6082   (unless gnus-newsgroup-limits
6083     (error "No limit to pop"))
6084   (prog1
6085       (gnus-summary-limit nil 'pop)
6086     (gnus-summary-position-point)))
6087
6088 (defun gnus-summary-limit-to-subject (subject &optional header)
6089   "Limit the summary buffer to articles that have subjects that match a regexp."
6090   (interactive "sLimit to subject (regexp): ")
6091   (unless header
6092     (setq header "subject"))
6093   (when (not (equal "" subject))
6094     (prog1
6095         (let ((articles (gnus-summary-find-matching
6096                          (or header "subject") subject 'all)))
6097           (unless articles
6098             (error "Found no matches for \"%s\"" subject))
6099           (gnus-summary-limit articles))
6100       (gnus-summary-position-point))))
6101
6102 (defun gnus-summary-limit-to-author (from)
6103   "Limit the summary buffer to articles that have authors that match a regexp."
6104   (interactive "sLimit to author (regexp): ")
6105   (gnus-summary-limit-to-subject from "from"))
6106
6107 (defun gnus-summary-limit-to-age (age &optional younger-p)
6108   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6109 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6110 articles that are younger than AGE days."
6111   (interactive "nTime in days: \nP")
6112   (prog1
6113       (let ((data gnus-newsgroup-data)
6114             (cutoff (days-to-time age))
6115             articles d date is-younger)
6116         (while (setq d (pop data))
6117           (when (and (vectorp (gnus-data-header d))
6118                      (setq date (mail-header-date (gnus-data-header d))))
6119             (setq is-younger (time-less-p
6120                               (time-since (date-to-time date))
6121                               cutoff))
6122             (when (if younger-p
6123                       is-younger
6124                     (not is-younger))
6125               (push (gnus-data-number d) articles))))
6126         (gnus-summary-limit (nreverse articles)))
6127     (gnus-summary-position-point)))
6128
6129 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6130 (make-obsolete
6131  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6132
6133 (defun gnus-summary-limit-to-unread (&optional all)
6134   "Limit the summary buffer to articles that are not marked as read.
6135 If ALL is non-nil, limit strictly to unread articles."
6136   (interactive "P")
6137   (if all
6138       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6139     (gnus-summary-limit-to-marks
6140      ;; Concat all the marks that say that an article is read and have
6141      ;; those removed.
6142      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6143            gnus-killed-mark gnus-kill-file-mark
6144            gnus-low-score-mark gnus-expirable-mark
6145            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6146            gnus-duplicate-mark gnus-souped-mark)
6147      'reverse)))
6148
6149 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6150 (make-obsolete 'gnus-summary-delete-marked-with
6151                'gnus-summary-limit-exlude-marks)
6152
6153 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6154   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6155 If REVERSE, limit the summary buffer to articles that are marked
6156 with MARKS.  MARKS can either be a string of marks or a list of marks.
6157 Returns how many articles were removed."
6158   (interactive "sMarks: ")
6159   (gnus-summary-limit-to-marks marks t))
6160
6161 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6162   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6163 If REVERSE (the prefix), limit the summary buffer to articles that are
6164 not marked with MARKS.  MARKS can either be a string of marks or a
6165 list of marks.
6166 Returns how many articles were removed."
6167   (interactive "sMarks: \nP")
6168   (prog1
6169       (let ((data gnus-newsgroup-data)
6170             (marks (if (listp marks) marks
6171                      (append marks nil))) ; Transform to list.
6172             articles)
6173         (while data
6174           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6175                   (memq (gnus-data-mark (car data)) marks))
6176             (push (gnus-data-number (car data)) articles))
6177           (setq data (cdr data)))
6178         (gnus-summary-limit articles))
6179     (gnus-summary-position-point)))
6180
6181 (defun gnus-summary-limit-to-score (&optional score)
6182   "Limit to articles with score at or above SCORE."
6183   (interactive "P")
6184   (setq score (if score
6185                   (prefix-numeric-value score)
6186                 (or gnus-summary-default-score 0)))
6187   (let ((data gnus-newsgroup-data)
6188         articles)
6189     (while data
6190       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6191                 score)
6192         (push (gnus-data-number (car data)) articles))
6193       (setq data (cdr data)))
6194     (prog1
6195         (gnus-summary-limit articles)
6196       (gnus-summary-position-point))))
6197
6198 (defun gnus-summary-limit-include-thread (id)
6199   "Display all the hidden articles that in the current thread."
6200   (interactive (list (mail-header-id (gnus-summary-article-header))))
6201   (let ((articles (gnus-articles-in-thread
6202                    (gnus-id-to-thread (gnus-root-id id)))))
6203     (prog1
6204         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6205       (gnus-summary-position-point))))
6206
6207 (defun gnus-summary-limit-include-dormant ()
6208   "Display all the hidden articles that are marked as dormant.
6209 Note that this command only works on a subset of the articles currently
6210 fetched for this group."
6211   (interactive)
6212   (unless gnus-newsgroup-dormant
6213     (error "There are no dormant articles in this group"))
6214   (prog1
6215       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6216     (gnus-summary-position-point)))
6217
6218 (defun gnus-summary-limit-exclude-dormant ()
6219   "Hide all dormant articles."
6220   (interactive)
6221   (prog1
6222       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6223     (gnus-summary-position-point)))
6224
6225 (defun gnus-summary-limit-exclude-childless-dormant ()
6226   "Hide all dormant articles that have no children."
6227   (interactive)
6228   (let ((data (gnus-data-list t))
6229         articles d children)
6230     ;; Find all articles that are either not dormant or have
6231     ;; children.
6232     (while (setq d (pop data))
6233       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6234                 (and (setq children
6235                            (gnus-article-children (gnus-data-number d)))
6236                      (let (found)
6237                        (while children
6238                          (when (memq (car children) articles)
6239                            (setq children nil
6240                                  found t))
6241                          (pop children))
6242                        found)))
6243         (push (gnus-data-number d) articles)))
6244     ;; Do the limiting.
6245     (prog1
6246         (gnus-summary-limit articles)
6247       (gnus-summary-position-point))))
6248
6249 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6250   "Mark all unread excluded articles as read.
6251 If ALL, mark even excluded ticked and dormants as read."
6252   (interactive "P")
6253   (let ((articles (gnus-sorted-complement
6254                    (sort
6255                     (mapcar (lambda (h) (mail-header-number h))
6256                             gnus-newsgroup-headers)
6257                     '<)
6258                    (sort gnus-newsgroup-limit '<)))
6259         article)
6260     (setq gnus-newsgroup-unreads
6261           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6262     (if all
6263         (setq gnus-newsgroup-dormant nil
6264               gnus-newsgroup-marked nil
6265               gnus-newsgroup-reads
6266               (nconc
6267                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6268                gnus-newsgroup-reads))
6269       (while (setq article (pop articles))
6270         (unless (or (memq article gnus-newsgroup-dormant)
6271                     (memq article gnus-newsgroup-marked))
6272           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6273
6274 (defun gnus-summary-limit (articles &optional pop)
6275   (if pop
6276       ;; We pop the previous limit off the stack and use that.
6277       (setq articles (car gnus-newsgroup-limits)
6278             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6279     ;; We use the new limit, so we push the old limit on the stack.
6280     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6281   ;; Set the limit.
6282   (setq gnus-newsgroup-limit articles)
6283   (let ((total (length gnus-newsgroup-data))
6284         (data (gnus-data-find-list (gnus-summary-article-number)))
6285         (gnus-summary-mark-below nil)   ; Inhibit this.
6286         found)
6287     ;; This will do all the work of generating the new summary buffer
6288     ;; according to the new limit.
6289     (gnus-summary-prepare)
6290     ;; Hide any threads, possibly.
6291     (and gnus-show-threads
6292          gnus-thread-hide-subtree
6293          (gnus-summary-hide-all-threads))
6294     ;; Try to return to the article you were at, or one in the
6295     ;; neighborhood.
6296     (when data
6297       ;; We try to find some article after the current one.
6298       (while data
6299         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6300           (setq data nil
6301                 found t))
6302         (setq data (cdr data))))
6303     (unless found
6304       ;; If there is no data, that means that we were after the last
6305       ;; article.  The same goes when we can't find any articles
6306       ;; after the current one.
6307       (goto-char (point-max))
6308       (gnus-summary-find-prev))
6309     (gnus-set-mode-line 'summary)
6310     ;; We return how many articles were removed from the summary
6311     ;; buffer as a result of the new limit.
6312     (- total (length gnus-newsgroup-data))))
6313
6314 (defsubst gnus-invisible-cut-children (threads)
6315   (let ((num 0))
6316     (while threads
6317       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6318         (incf num))
6319       (pop threads))
6320     (< num 2)))
6321
6322 (defsubst gnus-cut-thread (thread)
6323   "Go forwards in the thread until we find an article that we want to display."
6324   (when (or (eq gnus-fetch-old-headers 'some)
6325             (eq gnus-fetch-old-headers 'invisible)
6326             (eq gnus-build-sparse-threads 'some)
6327             (eq gnus-build-sparse-threads 'more))
6328     ;; Deal with old-fetched headers and sparse threads.
6329     (while (and
6330             thread
6331             (or
6332              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6333              (gnus-summary-article-ancient-p
6334               (mail-header-number (car thread))))
6335             (if (or (<= (length (cdr thread)) 1)
6336                     (eq gnus-fetch-old-headers 'invisible))
6337                 (setq gnus-newsgroup-limit
6338                       (delq (mail-header-number (car thread))
6339                             gnus-newsgroup-limit)
6340                       thread (cadr thread))
6341               (when (gnus-invisible-cut-children (cdr thread))
6342                 (let ((th (cdr thread)))
6343                   (while th
6344                     (if (memq (mail-header-number (caar th))
6345                               gnus-newsgroup-limit)
6346                         (setq thread (car th)
6347                               th nil)
6348                       (setq th (cdr th))))))))))
6349   thread)
6350
6351 (defun gnus-cut-threads (threads)
6352   "Cut off all uninteresting articles from the beginning of threads."
6353   (when (or (eq gnus-fetch-old-headers 'some)
6354             (eq gnus-fetch-old-headers 'invisible)
6355             (eq gnus-build-sparse-threads 'some)
6356             (eq gnus-build-sparse-threads 'more))
6357     (let ((th threads))
6358       (while th
6359         (setcar th (gnus-cut-thread (car th)))
6360         (setq th (cdr th)))))
6361   ;; Remove nixed out threads.
6362   (delq nil threads))
6363
6364 (defun gnus-summary-initial-limit (&optional show-if-empty)
6365   "Figure out what the initial limit is supposed to be on group entry.
6366 This entails weeding out unwanted dormants, low-scored articles,
6367 fetch-old-headers verbiage, and so on."
6368   ;; Most groups have nothing to remove.
6369   (if (or gnus-inhibit-limiting
6370           (and (null gnus-newsgroup-dormant)
6371                (not (eq gnus-fetch-old-headers 'some))
6372                (not (eq gnus-fetch-old-headers 'invisible))
6373                (null gnus-summary-expunge-below)
6374                (not (eq gnus-build-sparse-threads 'some))
6375                (not (eq gnus-build-sparse-threads 'more))
6376                (null gnus-thread-expunge-below)
6377                (not gnus-use-nocem)))
6378       ()                                ; Do nothing.
6379     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6380     (setq gnus-newsgroup-limit nil)
6381     (mapatoms
6382      (lambda (node)
6383        (unless (car (symbol-value node))
6384          ;; These threads have no parents -- they are roots.
6385          (let ((nodes (cdr (symbol-value node)))
6386                thread)
6387            (while nodes
6388              (if (and gnus-thread-expunge-below
6389                       (< (gnus-thread-total-score (car nodes))
6390                          gnus-thread-expunge-below))
6391                  (gnus-expunge-thread (pop nodes))
6392                (setq thread (pop nodes))
6393                (gnus-summary-limit-children thread))))))
6394      gnus-newsgroup-dependencies)
6395     ;; If this limitation resulted in an empty group, we might
6396     ;; pop the previous limit and use it instead.
6397     (when (and (not gnus-newsgroup-limit)
6398                show-if-empty)
6399       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6400     gnus-newsgroup-limit))
6401
6402 (defun gnus-summary-limit-children (thread)
6403   "Return 1 if this subthread is visible and 0 if it is not."
6404   ;; First we get the number of visible children to this thread.  This
6405   ;; is done by recursing down the thread using this function, so this
6406   ;; will really go down to a leaf article first, before slowly
6407   ;; working its way up towards the root.
6408   (when thread
6409     (let ((children
6410            (if (cdr thread)
6411                (apply '+ (mapcar 'gnus-summary-limit-children
6412                                  (cdr thread)))
6413              0))
6414           (number (mail-header-number (car thread)))
6415           score)
6416       (if (and
6417            (not (memq number gnus-newsgroup-marked))
6418            (or
6419             ;; If this article is dormant and has absolutely no visible
6420             ;; children, then this article isn't visible.
6421             (and (memq number gnus-newsgroup-dormant)
6422                  (zerop children))
6423             ;; If this is "fetch-old-headered" and there is no
6424             ;; visible children, then we don't want this article.
6425             (and (eq gnus-fetch-old-headers 'some)
6426                  (gnus-summary-article-ancient-p number)
6427                  (zerop children))
6428             ;; If this is "fetch-old-headered" and `invisible', then
6429             ;; we don't want this article.
6430             (and (eq gnus-fetch-old-headers 'invisible)
6431                  (gnus-summary-article-ancient-p number))
6432             ;; If this is a sparsely inserted article with no children,
6433             ;; we don't want it.
6434             (and (eq gnus-build-sparse-threads 'some)
6435                  (gnus-summary-article-sparse-p number)
6436                  (zerop children))
6437             ;; If we use expunging, and this article is really
6438             ;; low-scored, then we don't want this article.
6439             (when (and gnus-summary-expunge-below
6440                        (< (setq score
6441                                 (or (cdr (assq number gnus-newsgroup-scored))
6442                                     gnus-summary-default-score))
6443                           gnus-summary-expunge-below))
6444               ;; We increase the expunge-tally here, but that has
6445               ;; nothing to do with the limits, really.
6446               (incf gnus-newsgroup-expunged-tally)
6447               ;; We also mark as read here, if that's wanted.
6448               (when (and gnus-summary-mark-below
6449                          (< score gnus-summary-mark-below))
6450                 (setq gnus-newsgroup-unreads
6451                       (delq number gnus-newsgroup-unreads))
6452                 (if gnus-newsgroup-auto-expire
6453                     (push number gnus-newsgroup-expirable)
6454                   (push (cons number gnus-low-score-mark)
6455                         gnus-newsgroup-reads)))
6456               t)
6457             ;; Check NoCeM things.
6458             (if (and gnus-use-nocem
6459                      (gnus-nocem-unwanted-article-p
6460                       (mail-header-id (car thread))))
6461                 (progn
6462                   (setq gnus-newsgroup-unreads
6463                         (delq number gnus-newsgroup-unreads))
6464                   t))))
6465           ;; Nope, invisible article.
6466           0
6467         ;; Ok, this article is to be visible, so we add it to the limit
6468         ;; and return 1.
6469         (push number gnus-newsgroup-limit)
6470         1))))
6471
6472 (defun gnus-expunge-thread (thread)
6473   "Mark all articles in THREAD as read."
6474   (let* ((number (mail-header-number (car thread))))
6475     (incf gnus-newsgroup-expunged-tally)
6476     ;; We also mark as read here, if that's wanted.
6477     (setq gnus-newsgroup-unreads
6478           (delq number gnus-newsgroup-unreads))
6479     (if gnus-newsgroup-auto-expire
6480         (push number gnus-newsgroup-expirable)
6481       (push (cons number gnus-low-score-mark)
6482             gnus-newsgroup-reads)))
6483   ;; Go recursively through all subthreads.
6484   (mapcar 'gnus-expunge-thread (cdr thread)))
6485
6486 ;; Summary article oriented commands
6487
6488 (defun gnus-summary-refer-parent-article (n)
6489   "Refer parent article N times.
6490 If N is negative, go to ancestor -N instead.
6491 The difference between N and the number of articles fetched is returned."
6492   (interactive "p")
6493   (let ((skip 1)
6494         error header ref)
6495     (when (not (natnump n))
6496       (setq skip (abs n)
6497             n 1))
6498     (while (and (> n 0)
6499                 (not error))
6500       (setq header (gnus-summary-article-header))
6501       (if (and (eq (mail-header-number header)
6502                    (cdr gnus-article-current))
6503                (equal gnus-newsgroup-name
6504                       (car gnus-article-current)))
6505           ;; If we try to find the parent of the currently
6506           ;; displayed article, then we take a look at the actual
6507           ;; References header, since this is slightly more
6508           ;; reliable than the References field we got from the
6509           ;; server.
6510           (save-excursion
6511             (set-buffer gnus-original-article-buffer)
6512             (nnheader-narrow-to-headers)
6513             (unless (setq ref (message-fetch-field "references"))
6514               (setq ref (message-fetch-field "in-reply-to")))
6515             (widen))
6516         (setq ref
6517               ;; It's not the current article, so we take a bet on
6518               ;; the value we got from the server.
6519               (mail-header-references header)))
6520       (if (and ref
6521                (not (equal ref "")))
6522           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6523             (gnus-message 1 "Couldn't find parent"))
6524         (gnus-message 1 "No references in article %d"
6525                       (gnus-summary-article-number))
6526         (setq error t))
6527       (decf n))
6528     (gnus-summary-position-point)
6529     n))
6530
6531 (defun gnus-summary-refer-references ()
6532   "Fetch all articles mentioned in the References header.
6533 Return the number of articles fetched."
6534   (interactive)
6535   (let ((ref (mail-header-references (gnus-summary-article-header)))
6536         (current (gnus-summary-article-number))
6537         (n 0))
6538     (if (or (not ref)
6539             (equal ref ""))
6540         (error "No References in the current article")
6541       ;; For each Message-ID in the References header...
6542       (while (string-match "<[^>]*>" ref)
6543         (incf n)
6544         ;; ... fetch that article.
6545         (gnus-summary-refer-article
6546          (prog1 (match-string 0 ref)
6547            (setq ref (substring ref (match-end 0))))))
6548       (gnus-summary-goto-subject current)
6549       (gnus-summary-position-point)
6550       n)))
6551
6552 (defun gnus-summary-refer-thread (&optional limit)
6553   "Fetch all articles in the current thread.
6554 If LIMIT (the numerical prefix), fetch that many old headers instead
6555 of what's specified by the `gnus-refer-thread-limit' variable."
6556   (interactive "P")
6557   (let ((id (mail-header-id (gnus-summary-article-header)))
6558         (limit (if limit (prefix-numeric-value limit)
6559                  gnus-refer-thread-limit)))
6560     ;; We want to fetch LIMIT *old* headers, but we also have to
6561     ;; re-fetch all the headers in the current buffer, because many of
6562     ;; them may be undisplayed.  So we adjust LIMIT.
6563     (when (numberp limit)
6564       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6565     (unless (eq gnus-fetch-old-headers 'invisible)
6566       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6567       ;; Retrieve the headers and read them in.
6568       (if (eq (gnus-retrieve-headers
6569                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6570               'nov)
6571           (gnus-build-all-threads)
6572         (error "Can't fetch thread from backends that don't support NOV"))
6573       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6574     (gnus-summary-limit-include-thread id)))
6575
6576 (defun gnus-summary-refer-article (message-id &optional arg)
6577   "Fetch an article specified by MESSAGE-ID.
6578 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6579 or `gnus-select-method', no matter what backend the article comes from."
6580   (interactive "sMessage-ID: \nP")
6581   (when (and (stringp message-id)
6582              (not (zerop (length message-id))))
6583     ;; Construct the correct Message-ID if necessary.
6584     ;; Suggested by tale@pawl.rpi.edu.
6585     (unless (string-match "^<" message-id)
6586       (setq message-id (concat "<" message-id)))
6587     (unless (string-match ">$" message-id)
6588       (setq message-id (concat message-id ">")))
6589     (let* ((header (gnus-id-to-header message-id))
6590            (sparse (and header
6591                         (gnus-summary-article-sparse-p
6592                          (mail-header-number header))
6593                         (memq (mail-header-number header)
6594                               gnus-newsgroup-limit))))
6595       (cond
6596        ;; If the article is present in the buffer we just go to it.
6597        ((and header
6598              (or (not (gnus-summary-article-sparse-p
6599                        (mail-header-number header)))
6600                  sparse))
6601         (prog1
6602             (gnus-summary-goto-article
6603              (mail-header-number header) nil t)
6604           (when sparse
6605             (gnus-summary-update-article (mail-header-number header)))))
6606        (t
6607         ;; We fetch the article
6608         (let ((gnus-override-method
6609                (cond ((gnus-news-group-p gnus-newsgroup-name)
6610                       gnus-refer-article-method)
6611                      (arg
6612                       (or gnus-refer-article-method gnus-select-method))
6613                      (t nil)))
6614               number)
6615           ;; Start the special refer-article method, if necessary.
6616           (when (and gnus-refer-article-method
6617                      (gnus-news-group-p gnus-newsgroup-name))
6618             (gnus-check-server gnus-refer-article-method))
6619           ;; Fetch the header, and display the article.
6620           (if (setq number (gnus-summary-insert-subject message-id))
6621               (gnus-summary-select-article nil nil nil number)
6622             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6623
6624 (defun gnus-summary-edit-parameters ()
6625   "Edit the group parameters of the current group."
6626   (interactive)
6627   (gnus-group-edit-group gnus-newsgroup-name 'params))
6628
6629 (defun gnus-summary-customize-parameters ()
6630   "Customize the group parameters of the current group."
6631   (interactive)
6632   (gnus-group-customize gnus-newsgroup-name))
6633
6634 (defun gnus-summary-enter-digest-group (&optional force)
6635   "Enter an nndoc group based on the current article.
6636 If FORCE, force a digest interpretation.  If not, try
6637 to guess what the document format is."
6638   (interactive "P")
6639   (let ((conf gnus-current-window-configuration))
6640     (save-excursion
6641       (gnus-summary-select-article))
6642     (setq gnus-current-window-configuration conf)
6643     (let* ((name (format "%s-%d"
6644                          (gnus-group-prefixed-name
6645                           gnus-newsgroup-name (list 'nndoc ""))
6646                          (save-excursion
6647                            (set-buffer gnus-summary-buffer)
6648                            gnus-current-article)))
6649            (ogroup gnus-newsgroup-name)
6650            (params (append (gnus-info-params (gnus-get-info ogroup))
6651                            (list (cons 'to-group ogroup))
6652                            (list (cons 'save-article-group ogroup))))
6653            (case-fold-search t)
6654            (buf (current-buffer))
6655            dig)
6656       (save-excursion
6657         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6658         (insert-buffer-substring gnus-original-article-buffer)
6659         ;; Remove lines that may lead nndoc to misinterpret the
6660         ;; document type.
6661         (narrow-to-region
6662          (goto-char (point-min))
6663          (or (search-forward "\n\n" nil t) (point)))
6664         (goto-char (point-min))
6665         (delete-matching-lines "^\\(Path\\):\\|^From ")
6666         (widen))
6667       (unwind-protect
6668           (if (gnus-group-read-ephemeral-group
6669                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6670                             (nndoc-article-type
6671                              ,(if force 'digest 'guess))) t)
6672               ;; Make all postings to this group go to the parent group.
6673               (nconc (gnus-info-params (gnus-get-info name))
6674                      params)
6675             ;; Couldn't select this doc group.
6676             (switch-to-buffer buf)
6677             (gnus-set-global-variables)
6678             (gnus-configure-windows 'summary)
6679             (gnus-message 3 "Article couldn't be entered?"))
6680         (kill-buffer dig)))))
6681
6682 (defun gnus-summary-read-document (n)
6683   "Open a new group based on the current article(s).
6684 This will allow you to read digests and other similar
6685 documents as newsgroups.
6686 Obeys the standard process/prefix convention."
6687   (interactive "P")
6688   (let* ((articles (gnus-summary-work-articles n))
6689          (ogroup gnus-newsgroup-name)
6690          (params (append (gnus-info-params (gnus-get-info ogroup))
6691                          (list (cons 'to-group ogroup))))
6692          article group egroup groups vgroup)
6693     (while (setq article (pop articles))
6694       (setq group (format "%s-%d" gnus-newsgroup-name article))
6695       (gnus-summary-remove-process-mark article)
6696       (when (gnus-summary-display-article article)
6697         (save-excursion
6698           (with-temp-buffer
6699             (insert-buffer-substring gnus-original-article-buffer)
6700             ;; Remove some headers that may lead nndoc to make
6701             ;; the wrong guess.
6702             (message-narrow-to-head)
6703             (goto-char (point-min))
6704             (delete-matching-lines "^\\(Path\\):\\|^From ")
6705             (widen)
6706             (if (setq egroup
6707                       (gnus-group-read-ephemeral-group
6708                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6709                                      (nndoc-article-type guess))
6710                        t nil t))
6711                 (progn
6712                   ;; Make all postings to this group go to the parent group.
6713                   (nconc (gnus-info-params (gnus-get-info egroup))
6714                          params)
6715                   (push egroup groups))
6716               ;; Couldn't select this doc group.
6717               (gnus-error 3 "Article couldn't be entered"))))))
6718     ;; Now we have selected all the documents.
6719     (cond
6720      ((not groups)
6721       (error "None of the articles could be interpreted as documents"))
6722      ((gnus-group-read-ephemeral-group
6723        (setq vgroup (format
6724                      "nnvirtual:%s-%s" gnus-newsgroup-name
6725                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6726        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6727        t
6728        (cons (current-buffer) 'summary)))
6729      (t
6730       (error "Couldn't select virtual nndoc group")))))
6731
6732 (defun gnus-summary-isearch-article (&optional regexp-p)
6733   "Do incremental search forward on the current article.
6734 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6735   (interactive "P")
6736   (gnus-summary-select-article)
6737   (gnus-configure-windows 'article)
6738   (gnus-eval-in-buffer-window gnus-article-buffer
6739     (save-restriction
6740       (widen)
6741       (isearch-forward regexp-p))))
6742
6743 (defun gnus-summary-search-article-forward (regexp &optional backward)
6744   "Search for an article containing REGEXP forward.
6745 If BACKWARD, search backward instead."
6746   (interactive
6747    (list (read-string
6748           (format "Search article %s (regexp%s): "
6749                   (if current-prefix-arg "backward" "forward")
6750                   (if gnus-last-search-regexp
6751                       (concat ", default " gnus-last-search-regexp)
6752                     "")))
6753          current-prefix-arg))
6754   (if (string-equal regexp "")
6755       (setq regexp (or gnus-last-search-regexp ""))
6756     (setq gnus-last-search-regexp regexp))
6757   (if (gnus-summary-search-article regexp backward)
6758       (gnus-summary-show-thread)
6759     (error "Search failed: \"%s\"" regexp)))
6760
6761 (defun gnus-summary-search-article-backward (regexp)
6762   "Search for an article containing REGEXP backward."
6763   (interactive
6764    (list (read-string
6765           (format "Search article backward (regexp%s): "
6766                   (if gnus-last-search-regexp
6767                       (concat ", default " gnus-last-search-regexp)
6768                     "")))))
6769   (gnus-summary-search-article-forward regexp 'backward))
6770
6771 (defun gnus-summary-search-article (regexp &optional backward)
6772   "Search for an article containing REGEXP.
6773 Optional argument BACKWARD means do search for backward.
6774 `gnus-select-article-hook' is not called during the search."
6775   ;; We have to require this here to make sure that the following
6776   ;; dynamic binding isn't shadowed by autoloading.
6777   (require 'gnus-async)
6778   (require 'gnus-art)
6779   (let ((gnus-select-article-hook nil)  ;Disable hook.
6780         (gnus-article-display-hook nil)
6781         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6782         (gnus-use-article-prefetch nil)
6783         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6784         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6785         (sum (current-buffer))
6786         (found nil)
6787         point)
6788     (gnus-save-hidden-threads
6789       (gnus-summary-select-article)
6790       (set-buffer gnus-article-buffer)
6791       (when backward
6792         (forward-line -1))
6793       (while (not found)
6794         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6795         (if (if backward
6796                 (re-search-backward regexp nil t)
6797               (re-search-forward regexp nil t))
6798             ;; We found the regexp.
6799             (progn
6800               (setq found 'found)
6801               (beginning-of-line)
6802               (set-window-start
6803                (get-buffer-window (current-buffer))
6804                (point))
6805               (forward-line 1)
6806               (set-buffer sum)
6807               (setq point (point)))
6808           ;; We didn't find it, so we go to the next article.
6809           (set-buffer sum)
6810           (setq found 'not)
6811           (while (eq found 'not)
6812             (if (not (if backward (gnus-summary-find-prev)
6813                        (gnus-summary-find-next)))
6814                 ;; No more articles.
6815                 (setq found t)
6816               ;; Select the next article and adjust point.
6817               (unless (gnus-summary-article-sparse-p
6818                        (gnus-summary-article-number))
6819                 (setq found nil)
6820                 (gnus-summary-select-article)
6821                 (set-buffer gnus-article-buffer)
6822                 (widen)
6823                 (goto-char (if backward (point-max) (point-min))))))))
6824       (gnus-message 7 ""))
6825     ;; Return whether we found the regexp.
6826     (when (eq found 'found)
6827       (goto-char point)
6828       (gnus-summary-show-thread)
6829       (gnus-summary-goto-subject gnus-current-article)
6830       (gnus-summary-position-point)
6831       t)))
6832
6833 (defun gnus-summary-find-matching (header regexp &optional backward unread
6834                                           not-case-fold)
6835   "Return a list of all articles that match REGEXP on HEADER.
6836 The search stars on the current article and goes forwards unless
6837 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6838 If UNREAD is non-nil, only unread articles will
6839 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6840 in the comparisons."
6841   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6842                 (gnus-data-find-list
6843                  (gnus-summary-article-number) (gnus-data-list backward))))
6844         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6845         (case-fold-search (not not-case-fold))
6846         articles d)
6847     (unless (fboundp (intern (concat "mail-header-" header)))
6848       (error "%s is not a valid header" header))
6849     (while data
6850       (setq d (car data))
6851       (and (or (not unread)             ; We want all articles...
6852                (gnus-data-unread-p d))  ; Or just unreads.
6853            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6854            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6855            (push (gnus-data-number d) articles)) ; Success!
6856       (setq data (cdr data)))
6857     (nreverse articles)))
6858
6859 (defun gnus-summary-execute-command (header regexp command &optional backward)
6860   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6861 If HEADER is an empty string (or nil), the match is done on the entire
6862 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6863   (interactive
6864    (list (let ((completion-ignore-case t))
6865            (completing-read
6866             "Header name: "
6867             (mapcar (lambda (string) (list string))
6868                     '("Number" "Subject" "From" "Lines" "Date"
6869                       "Message-ID" "Xref" "References" "Body"))
6870             nil 'require-match))
6871          (read-string "Regexp: ")
6872          (read-key-sequence "Command: ")
6873          current-prefix-arg))
6874   (when (equal header "Body")
6875     (setq header ""))
6876   ;; Hidden thread subtrees must be searched as well.
6877   (gnus-summary-show-all-threads)
6878   ;; We don't want to change current point nor window configuration.
6879   (save-excursion
6880     (save-window-excursion
6881       (gnus-message 6 "Executing %s..." (key-description command))
6882       ;; We'd like to execute COMMAND interactively so as to give arguments.
6883       (gnus-execute header regexp
6884                     `(call-interactively ',(key-binding command))
6885                     backward)
6886       (gnus-message 6 "Executing %s...done" (key-description command)))))
6887
6888 (defun gnus-summary-beginning-of-article ()
6889   "Scroll the article back to the beginning."
6890   (interactive)
6891   (gnus-summary-select-article)
6892   (gnus-configure-windows 'article)
6893   (gnus-eval-in-buffer-window gnus-article-buffer
6894     (widen)
6895     (goto-char (point-min))
6896     (when gnus-page-broken
6897       (gnus-narrow-to-page))))
6898
6899 (defun gnus-summary-end-of-article ()
6900   "Scroll to the end of the article."
6901   (interactive)
6902   (gnus-summary-select-article)
6903   (gnus-configure-windows 'article)
6904   (gnus-eval-in-buffer-window gnus-article-buffer
6905     (widen)
6906     (goto-char (point-max))
6907     (recenter -3)
6908     (when gnus-page-broken
6909       (gnus-narrow-to-page))))
6910
6911 (defun gnus-summary-print-article (&optional filename n)
6912   "Generate and print a PostScript image of the N next (mail) articles.
6913
6914 If N is negative, print the N previous articles.  If N is nil and articles
6915 have been marked with the process mark, print these instead.
6916
6917 If the optional first argument FILENAME is nil, send the image to the
6918 printer.  If FILENAME is a string, save the PostScript image in a file with
6919 that name.  If FILENAME is a number, prompt the user for the name of the file
6920 to save in."
6921   (interactive (list (ps-print-preprint current-prefix-arg)
6922                      current-prefix-arg))
6923   (dolist (article (gnus-summary-work-articles n))
6924     (gnus-summary-select-article nil nil 'pseudo article)
6925     (gnus-eval-in-buffer-window gnus-article-buffer
6926       (let ((buffer (generate-new-buffer " *print*")))
6927         (unwind-protect
6928             (progn
6929               (copy-to-buffer buffer (point-min) (point-max))
6930               (set-buffer buffer)
6931               (gnus-article-delete-invisible-text)
6932               (let ((ps-left-header
6933                      (list
6934                       (concat "("
6935                               (mail-header-subject gnus-current-headers) ")")
6936                       (concat "("
6937                               (mail-header-from gnus-current-headers) ")")))
6938                     (ps-right-header
6939                      (list
6940                       "/pagenumberstring load"
6941                       (concat "("
6942                               (mail-header-date gnus-current-headers) ")"))))
6943                 (gnus-run-hooks 'gnus-ps-print-hook)
6944                 (save-excursion
6945                   (ps-print-buffer-with-faces filename))))
6946           (kill-buffer buffer))))))
6947
6948 (defun gnus-summary-show-article (&optional arg)
6949   "Force re-fetching of the current article.
6950 If ARG (the prefix) is non-nil, show the raw article without any
6951 article massaging functions being run."
6952   (interactive "P")
6953   (if (not arg)
6954       ;; Select the article the normal way.
6955       (gnus-summary-select-article nil 'force)
6956     ;; We have to require this here to make sure that the following
6957     ;; dynamic binding isn't shadowed by autoloading.
6958     (require 'gnus-async)
6959     (require 'gnus-art)
6960     ;; Bind the article treatment functions to nil.
6961     (let ((gnus-have-all-headers t)
6962           gnus-article-display-hook
6963           gnus-article-prepare-hook
6964           gnus-article-decode-hook
6965           gnus-break-pages
6966           gnus-show-mime
6967           gnus-visual)
6968       (gnus-summary-select-article nil 'force)))
6969   (gnus-summary-goto-subject gnus-current-article)
6970   (gnus-summary-position-point))
6971
6972 (defun gnus-summary-verbose-headers (&optional arg)
6973   "Toggle permanent full header display.
6974 If ARG is a positive number, turn header display on.
6975 If ARG is a negative number, turn header display off."
6976   (interactive "P")
6977   (setq gnus-show-all-headers
6978         (cond ((or (not (numberp arg))
6979                    (zerop arg))
6980                (not gnus-show-all-headers))
6981               ((natnump arg)
6982                t)))
6983   (gnus-summary-show-article))
6984
6985 (defun gnus-summary-toggle-header (&optional arg)
6986   "Show the headers if they are hidden, or hide them if they are shown.
6987 If ARG is a positive number, show the entire header.
6988 If ARG is a negative number, hide the unwanted header lines."
6989   (interactive "P")
6990   (save-excursion
6991     (set-buffer gnus-article-buffer)
6992     (save-restriction
6993       (let* ((buffer-read-only nil)
6994              (inhibit-point-motion-hooks t)
6995              hidden e)
6996         (save-restriction 
6997           (message-narrow-to-head)
6998           (setq hidden (gnus-article-hidden-text-p 'headers)))
6999         (goto-char (point-min))
7000         (when (search-forward "\n\n" nil t)
7001           (delete-region (point-min) (1- (point))))
7002         (goto-char (point-min))
7003         (save-excursion
7004           (set-buffer gnus-original-article-buffer)
7005           (goto-char (point-min))
7006           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7007         (insert-buffer-substring gnus-original-article-buffer 1 e)
7008         (save-restriction
7009           (narrow-to-region (point-min) (point))
7010           (article-decode-encoded-words)
7011           (if (or hidden
7012                   (and (numberp arg) (< arg 0)))
7013               (let ((gnus-treat-hide-headers nil)
7014                     (gnus-treat-hide-boring-headers nil))
7015                 (gnus-treat-article 'head))
7016             (gnus-treat-article 'head)))))))
7017
7018 (defun gnus-summary-show-all-headers ()
7019   "Make all header lines visible."
7020   (interactive)
7021   (gnus-article-show-all-headers))
7022
7023 (defun gnus-summary-toggle-mime (&optional arg)
7024   "Toggle MIME processing.
7025 If ARG is a positive number, turn MIME processing on."
7026   (interactive "P")
7027   (setq gnus-show-mime
7028         (if (null arg)
7029             (not gnus-show-mime)
7030           (> (prefix-numeric-value arg) 0)))
7031   (gnus-summary-select-article t 'force))
7032
7033 (defun gnus-summary-caesar-message (&optional arg)
7034   "Caesar rotate the current article by 13.
7035 The numerical prefix specifies how many places to rotate each letter
7036 forward."
7037   (interactive "P")
7038   (gnus-summary-select-article)
7039   (let ((mail-header-separator ""))
7040     (gnus-eval-in-buffer-window gnus-article-buffer
7041       (save-restriction
7042         (widen)
7043         (let ((start (window-start))
7044               buffer-read-only)
7045           (message-caesar-buffer-body arg)
7046           (set-window-start (get-buffer-window (current-buffer)) start))))))
7047
7048 (defun gnus-summary-stop-page-breaking ()
7049   "Stop page breaking in the current article."
7050   (interactive)
7051   (gnus-summary-select-article)
7052   (gnus-eval-in-buffer-window gnus-article-buffer
7053     (widen)
7054     (when (gnus-visual-p 'page-marker)
7055       (let ((buffer-read-only nil))
7056         (gnus-remove-text-with-property 'gnus-prev)
7057         (gnus-remove-text-with-property 'gnus-next))
7058       (setq gnus-page-broken nil))))
7059
7060 (defun gnus-summary-move-article (&optional n to-newsgroup
7061                                             select-method action)
7062   "Move the current article to a different newsgroup.
7063 If N is a positive number, move the N next articles.
7064 If N is a negative number, move the N previous articles.
7065 If N is nil and any articles have been marked with the process mark,
7066 move those articles instead.
7067 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7068 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7069 re-spool using this method.
7070
7071 For this function to work, both the current newsgroup and the
7072 newsgroup that you want to move to have to support the `request-move'
7073 and `request-accept' functions."
7074   (interactive "P")
7075   (unless action
7076     (setq action 'move))
7077   ;; Disable marking as read.
7078   (let (gnus-mark-article-hook)
7079     (save-window-excursion
7080       (gnus-summary-select-article)))
7081   ;; Check whether the source group supports the required functions.
7082   (cond ((and (eq action 'move)
7083               (not (gnus-check-backend-function
7084                     'request-move-article gnus-newsgroup-name)))
7085          (error "The current group does not support article moving"))
7086         ((and (eq action 'crosspost)
7087               (not (gnus-check-backend-function
7088                     'request-replace-article gnus-newsgroup-name)))
7089          (error "The current group does not support article editing")))
7090   (let ((articles (gnus-summary-work-articles n))
7091         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7092         (names '((move "Move" "Moving")
7093                  (copy "Copy" "Copying")
7094                  (crosspost "Crosspost" "Crossposting")))
7095         (copy-buf (save-excursion
7096                     (nnheader-set-temp-buffer " *copy article*")))
7097         (default-marks gnus-article-mark-lists)
7098         (no-expire-marks (delete '(expirable . expire)
7099                                  (copy-sequence gnus-article-mark-lists)))
7100         art-group to-method new-xref article to-groups)
7101     (unless (assq action names)
7102       (error "Unknown action %s" action))
7103     ;; Read the newsgroup name.
7104     (when (and (not to-newsgroup)
7105                (not select-method))
7106       (setq to-newsgroup
7107             (gnus-read-move-group-name
7108              (cadr (assq action names))
7109              (symbol-value (intern (format "gnus-current-%s-group" action)))
7110              articles prefix))
7111       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7112     (setq to-method (or select-method
7113                         (gnus-group-name-to-method to-newsgroup)))
7114     ;; Check the method we are to move this article to...
7115     (unless (gnus-check-backend-function
7116              'request-accept-article (car to-method))
7117       (error "%s does not support article copying" (car to-method)))
7118     (unless (gnus-check-server to-method)
7119       (error "Can't open server %s" (car to-method)))
7120     (gnus-message 6 "%s to %s: %s..."
7121                   (caddr (assq action names))
7122                   (or (car select-method) to-newsgroup) articles)
7123     (while articles
7124       (setq article (pop articles))
7125       (setq
7126        art-group
7127        (cond
7128         ;; Move the article.
7129         ((eq action 'move)
7130          ;; Remove this article from future suppression.
7131          (gnus-dup-unsuppress-article article)
7132          (gnus-request-move-article
7133           article                       ; Article to move
7134           gnus-newsgroup-name           ; From newsgroup
7135           (nth 1 (gnus-find-method-for-group
7136                   gnus-newsgroup-name)) ; Server
7137           (list 'gnus-request-accept-article
7138                 to-newsgroup (list 'quote select-method)
7139                 (not articles) t)               ; Accept form
7140           (not articles)))              ; Only save nov last time
7141         ;; Copy the article.
7142         ((eq action 'copy)
7143          (save-excursion
7144            (set-buffer copy-buf)
7145            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7146              (gnus-request-accept-article
7147               to-newsgroup select-method (not articles) t))))
7148         ;; Crosspost the article.
7149         ((eq action 'crosspost)
7150          (let ((xref (message-tokenize-header
7151                       (mail-header-xref (gnus-summary-article-header article))
7152                       " ")))
7153            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7154                                   ":" article))
7155            (unless xref
7156              (setq xref (list (system-name))))
7157            (setq new-xref
7158                  (concat
7159                   (mapconcat 'identity
7160                              (delete "Xref:" (delete new-xref xref))
7161                              " ")
7162                   " " new-xref))
7163            (save-excursion
7164              (set-buffer copy-buf)
7165              ;; First put the article in the destination group.
7166              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7167              (when (consp (setq art-group
7168                                 (gnus-request-accept-article
7169                                  to-newsgroup select-method (not articles))))
7170                (setq new-xref (concat new-xref " " (car art-group)
7171                                       ":" (cdr art-group)))
7172                ;; Now we have the new Xrefs header, so we insert
7173                ;; it and replace the new article.
7174                (nnheader-replace-header "Xref" new-xref)
7175                (gnus-request-replace-article
7176                 (cdr art-group) to-newsgroup (current-buffer))
7177                art-group))))))
7178       (cond
7179        ((not art-group)
7180         (gnus-message 1 "Couldn't %s article %s"
7181                       (cadr (assq action names)) article))
7182        ((and (eq art-group 'junk)
7183              (eq action 'move))
7184         (gnus-summary-mark-article article gnus-canceled-mark)
7185         (gnus-message 4 "Deleted article %s" article))
7186        (t
7187         (let* ((pto-group (gnus-group-prefixed-name
7188                            (car art-group) to-method))
7189                (entry
7190                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7191                (info (nth 2 entry))
7192                (to-group (gnus-info-group info)))
7193           ;; Update the group that has been moved to.
7194           (when (and info
7195                      (memq action '(move copy)))
7196             (unless (member to-group to-groups)
7197               (push to-group to-groups))
7198
7199             (unless (memq article gnus-newsgroup-unreads)
7200               (gnus-info-set-read
7201                info (gnus-add-to-range (gnus-info-read info)
7202                                        (list (cdr art-group)))))
7203
7204             ;; Copy any marks over to the new group.
7205             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7206                              default-marks
7207                            no-expire-marks))
7208                   (to-article (cdr art-group)))
7209
7210               ;; See whether the article is to be put in the cache.
7211               (when gnus-use-cache
7212                 (gnus-cache-possibly-enter-article
7213                  to-group to-article
7214                  (let ((header (copy-sequence
7215                                 (gnus-summary-article-header article))))
7216                    (mail-header-set-number header to-article)
7217                    header)
7218                  (memq article gnus-newsgroup-marked)
7219                  (memq article gnus-newsgroup-dormant)
7220                  (memq article gnus-newsgroup-unreads)))
7221
7222               (when (and (equal to-group gnus-newsgroup-name)
7223                          (not (memq article gnus-newsgroup-unreads)))
7224                 ;; Mark this article as read in this group.
7225                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7226                 (setcdr (gnus-active to-group) to-article)
7227                 (setcdr gnus-newsgroup-active to-article))
7228
7229               (while marks
7230                 (when (memq article (symbol-value
7231                                      (intern (format "gnus-newsgroup-%s"
7232                                                      (caar marks)))))
7233                   ;; If the other group is the same as this group,
7234                   ;; then we have to add the mark to the list.
7235                   (when (equal to-group gnus-newsgroup-name)
7236                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7237                          (cons to-article
7238                                (symbol-value
7239                                 (intern (format "gnus-newsgroup-%s"
7240                                                 (caar marks)))))))
7241                   ;; Copy the marks to other group.
7242                   (gnus-add-marked-articles
7243                    to-group (cdar marks) (list to-article) info))
7244                 (setq marks (cdr marks)))
7245
7246               (gnus-dribble-enter
7247                (concat "(gnus-group-set-info '"
7248                        (gnus-prin1-to-string (gnus-get-info to-group))
7249                        ")"))))
7250
7251           ;; Update the Xref header in this article to point to
7252           ;; the new crossposted article we have just created.
7253           (when (eq action 'crosspost)
7254             (save-excursion
7255               (set-buffer copy-buf)
7256               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7257               (nnheader-replace-header "Xref" new-xref)
7258               (gnus-request-replace-article
7259                article gnus-newsgroup-name (current-buffer)))))
7260
7261         ;;;!!!Why is this necessary?
7262         (set-buffer gnus-summary-buffer)
7263
7264         (gnus-summary-goto-subject article)
7265         (when (eq action 'move)
7266           (gnus-summary-mark-article article gnus-canceled-mark))))
7267       (gnus-summary-remove-process-mark article))
7268     ;; Re-activate all groups that have been moved to.
7269     (while to-groups
7270       (save-excursion
7271         (set-buffer gnus-group-buffer)
7272         (when (gnus-group-goto-group (car to-groups) t)
7273           (gnus-group-get-new-news-this-group 1 t))
7274         (pop to-groups)))
7275
7276     (gnus-kill-buffer copy-buf)
7277     (gnus-summary-position-point)
7278     (gnus-set-mode-line 'summary)))
7279
7280 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7281   "Move the current article to a different newsgroup.
7282 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7283 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7284 re-spool using this method."
7285   (interactive "P")
7286   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7287
7288 (defun gnus-summary-crosspost-article (&optional n)
7289   "Crosspost the current article to some other group."
7290   (interactive "P")
7291   (gnus-summary-move-article n nil nil 'crosspost))
7292
7293 (defcustom gnus-summary-respool-default-method nil
7294   "Default method for respooling an article.
7295 If nil, use to the current newsgroup method."
7296   :type '(choice (gnus-select-method :value (nnml ""))
7297                  (const nil))
7298   :group 'gnus-summary-mail)
7299
7300 (defun gnus-summary-respool-article (&optional n method)
7301   "Respool the current article.
7302 The article will be squeezed through the mail spooling process again,
7303 which means that it will be put in some mail newsgroup or other
7304 depending on `nnmail-split-methods'.
7305 If N is a positive number, respool the N next articles.
7306 If N is a negative number, respool the N previous articles.
7307 If N is nil and any articles have been marked with the process mark,
7308 respool those articles instead.
7309
7310 Respooling can be done both from mail groups and \"real\" newsgroups.
7311 In the former case, the articles in question will be moved from the
7312 current group into whatever groups they are destined to.  In the
7313 latter case, they will be copied into the relevant groups."
7314   (interactive
7315    (list current-prefix-arg
7316          (let* ((methods (gnus-methods-using 'respool))
7317                 (methname
7318                  (symbol-name (or gnus-summary-respool-default-method
7319                                   (car (gnus-find-method-for-group
7320                                         gnus-newsgroup-name)))))
7321                 (method
7322                  (gnus-completing-read
7323                   methname "What backend do you want to use when respooling?"
7324                   methods nil t nil 'gnus-mail-method-history))
7325                 ms)
7326            (cond
7327             ((zerop (length (setq ms (gnus-servers-using-backend
7328                                       (intern method)))))
7329              (list (intern method) ""))
7330             ((= 1 (length ms))
7331              (car ms))
7332             (t
7333              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7334                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7335                            ms-alist))))))))
7336   (unless method
7337     (error "No method given for respooling"))
7338   (if (assoc (symbol-name
7339               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7340              (gnus-methods-using 'respool))
7341       (gnus-summary-move-article n nil method)
7342     (gnus-summary-copy-article n nil method)))
7343
7344 (defun gnus-summary-import-article (file)
7345   "Import an arbitrary file into a mail newsgroup."
7346   (interactive "fImport file: ")
7347   (let ((group gnus-newsgroup-name)
7348         (now (current-time))
7349         atts lines)
7350     (unless (gnus-check-backend-function 'request-accept-article group)
7351       (error "%s does not support article importing" group))
7352     (or (file-readable-p file)
7353         (not (file-regular-p file))
7354         (error "Can't read %s" file))
7355     (save-excursion
7356       (set-buffer (gnus-get-buffer-create " *import file*"))
7357       (erase-buffer)
7358       (nnheader-insert-file-contents file)
7359       (goto-char (point-min))
7360       (unless (nnheader-article-p)
7361         ;; This doesn't look like an article, so we fudge some headers.
7362         (setq atts (file-attributes file)
7363               lines (count-lines (point-min) (point-max)))
7364         (insert "From: " (read-string "From: ") "\n"
7365                 "Subject: " (read-string "Subject: ") "\n"
7366                 "Date: " (message-make-date (nth 5 atts))
7367                 "\n"
7368                 "Message-ID: " (message-make-message-id) "\n"
7369                 "Lines: " (int-to-string lines) "\n"
7370                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7371       (gnus-request-accept-article group nil t)
7372       (kill-buffer (current-buffer)))))
7373
7374 (defun gnus-summary-article-posted-p ()
7375   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7376 This will be the case if the article has both been mailed and posted."
7377   (interactive)
7378   (let ((id (mail-header-references (gnus-summary-article-header)))
7379         (gnus-override-method
7380          (or gnus-refer-article-method gnus-select-method)))
7381     (if (gnus-request-head id "")
7382         (gnus-message 2 "The current message was found on %s"
7383                       gnus-override-method)
7384       (gnus-message 2 "The current message couldn't be found on %s"
7385                     gnus-override-method)
7386       nil)))
7387
7388 (defun gnus-summary-expire-articles (&optional now)
7389   "Expire all articles that are marked as expirable in the current group."
7390   (interactive)
7391   (when (gnus-check-backend-function
7392          'request-expire-articles gnus-newsgroup-name)
7393     ;; This backend supports expiry.
7394     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7395            (expirable (if total
7396                           (progn
7397                             ;; We need to update the info for
7398                             ;; this group for `gnus-list-of-read-articles'
7399                             ;; to give us the right answer.
7400                             (gnus-run-hooks 'gnus-exit-group-hook)
7401                             (gnus-summary-update-info)
7402                             (gnus-list-of-read-articles gnus-newsgroup-name))
7403                         (setq gnus-newsgroup-expirable
7404                               (sort gnus-newsgroup-expirable '<))))
7405            (expiry-wait (if now 'immediate
7406                           (gnus-group-find-parameter
7407                            gnus-newsgroup-name 'expiry-wait)))
7408            es)
7409       (when expirable
7410         ;; There are expirable articles in this group, so we run them
7411         ;; through the expiry process.
7412         (gnus-message 6 "Expiring articles...")
7413         (unless (gnus-check-group gnus-newsgroup-name)
7414           (error "Can't open server for %s" gnus-newsgroup-name))
7415         ;; The list of articles that weren't expired is returned.
7416         (save-excursion
7417           (if expiry-wait
7418               (let ((nnmail-expiry-wait-function nil)
7419                     (nnmail-expiry-wait expiry-wait))
7420                 (setq es (gnus-request-expire-articles
7421                           expirable gnus-newsgroup-name)))
7422             (setq es (gnus-request-expire-articles
7423                       expirable gnus-newsgroup-name))))
7424         (unless total
7425           (setq gnus-newsgroup-expirable es))
7426         ;; We go through the old list of expirable, and mark all
7427         ;; really expired articles as nonexistent.
7428         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7429           (let ((gnus-use-cache nil))
7430             (while expirable
7431               (unless (memq (car expirable) es)
7432                 (when (gnus-data-find (car expirable))
7433                   (gnus-summary-mark-article
7434                    (car expirable) gnus-canceled-mark)))
7435               (setq expirable (cdr expirable)))))
7436         (gnus-message 6 "Expiring articles...done")))))
7437
7438 (defun gnus-summary-expire-articles-now ()
7439   "Expunge all expirable articles in the current group.
7440 This means that *all* articles that are marked as expirable will be
7441 deleted forever, right now."
7442   (interactive)
7443   (or gnus-expert-user
7444       (gnus-yes-or-no-p
7445        "Are you really, really, really sure you want to delete all these messages? ")
7446       (error "Phew!"))
7447   (gnus-summary-expire-articles t))
7448
7449 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7450 (defun gnus-summary-delete-article (&optional n)
7451   "Delete the N next (mail) articles.
7452 This command actually deletes articles.  This is not a marking
7453 command.  The article will disappear forever from your life, never to
7454 return.
7455 If N is negative, delete backwards.
7456 If N is nil and articles have been marked with the process mark,
7457 delete these instead."
7458   (interactive "P")
7459   (unless (gnus-check-backend-function 'request-expire-articles
7460                                        gnus-newsgroup-name)
7461     (error "The current newsgroup does not support article deletion"))
7462   ;; Compute the list of articles to delete.
7463   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7464         not-deleted)
7465     (if (and gnus-novice-user
7466              (not (gnus-yes-or-no-p
7467                    (format "Do you really want to delete %s forever? "
7468                            (if (> (length articles) 1)
7469                                (format "these %s articles" (length articles))
7470                              "this article")))))
7471         ()
7472       ;; Delete the articles.
7473       (setq not-deleted (gnus-request-expire-articles
7474                          articles gnus-newsgroup-name 'force))
7475       (while articles
7476         (gnus-summary-remove-process-mark (car articles))
7477         ;; The backend might not have been able to delete the article
7478         ;; after all.
7479         (unless (memq (car articles) not-deleted)
7480           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7481         (setq articles (cdr articles)))
7482       (when not-deleted
7483         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7484     (gnus-summary-position-point)
7485     (gnus-set-mode-line 'summary)
7486     not-deleted))
7487
7488 (defun gnus-summary-edit-article (&optional force)
7489   "Edit the current article.
7490 This will have permanent effect only in mail groups.
7491 If FORCE is non-nil, allow editing of articles even in read-only
7492 groups."
7493   (interactive "P")
7494   (let ((mail-parse-charset gnus-newsgroup-charset))
7495     (save-excursion
7496       (set-buffer gnus-summary-buffer)
7497       (gnus-set-global-variables)
7498       (when (and (not force)
7499                  (gnus-group-read-only-p))
7500         (error "The current newsgroup does not support article editing"))
7501       (gnus-summary-show-article t)
7502       (gnus-article-edit-article
7503        'ignore
7504        `(lambda (no-highlight)
7505           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7506             (gnus-summary-edit-article-done
7507              ,(or (mail-header-references gnus-current-headers) "")
7508              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7509
7510 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7511
7512 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7513                                                  no-highlight)
7514   "Make edits to the current article permanent."
7515   (interactive)
7516   ;; Replace the article.
7517   (let ((buf (current-buffer)))
7518     (with-temp-buffer
7519       (insert-buffer-substring buf)
7520       (if (and (not read-only)
7521                (not (gnus-request-replace-article
7522                      (cdr gnus-article-current) (car gnus-article-current)
7523                      (current-buffer) t)))
7524           (error "Couldn't replace article")
7525         ;; Update the summary buffer.
7526         (if (and references
7527                  (equal (message-tokenize-header references " ")
7528                         (message-tokenize-header
7529                          (or (message-fetch-field "references") "") " ")))
7530             ;; We only have to update this line.
7531             (save-excursion
7532               (save-restriction
7533                 (message-narrow-to-head)
7534                 (let ((head (buffer-string))
7535                       header)
7536                   (with-temp-buffer
7537                     (insert (format "211 %d Article retrieved.\n"
7538                                     (cdr gnus-article-current)))
7539                     (insert head)
7540                     (insert ".\n")
7541                     (let ((nntp-server-buffer (current-buffer)))
7542                       (setq header (car (gnus-get-newsgroup-headers
7543                                          (save-excursion
7544                                            (set-buffer gnus-summary-buffer)
7545                                            gnus-newsgroup-dependencies)
7546                                          t))))
7547                     (save-excursion
7548                       (set-buffer gnus-summary-buffer)
7549                       (gnus-data-set-header
7550                        (gnus-data-find (cdr gnus-article-current))
7551                        header)
7552                       (gnus-summary-update-article-line
7553                        (cdr gnus-article-current) header))))))
7554           ;; Update threads.
7555           (set-buffer (or buffer gnus-summary-buffer))
7556           (gnus-summary-update-article (cdr gnus-article-current)))
7557         ;; Prettify the article buffer again.
7558         (unless no-highlight
7559           (save-excursion
7560             (set-buffer gnus-article-buffer)
7561             ;;;!!! Fix this -- article should be rehighlighted.
7562             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7563             (set-buffer gnus-original-article-buffer)
7564             (gnus-request-article
7565              (cdr gnus-article-current)
7566              (car gnus-article-current) (current-buffer))))
7567         ;; Prettify the summary buffer line.
7568         (when (gnus-visual-p 'summary-highlight 'highlight)
7569           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7570
7571 (defun gnus-summary-edit-wash (key)
7572   "Perform editing command KEY in the article buffer."
7573   (interactive
7574    (list
7575     (progn
7576       (message "%s" (concat (this-command-keys) "- "))
7577       (read-char))))
7578   (message "")
7579   (gnus-summary-edit-article)
7580   (execute-kbd-macro (concat (this-command-keys) key))
7581   (gnus-article-edit-done))
7582
7583 ;;; Respooling
7584
7585 (defun gnus-summary-respool-query (&optional silent trace)
7586   "Query where the respool algorithm would put this article."
7587   (interactive)
7588   (let (gnus-mark-article-hook)
7589     (gnus-summary-select-article)
7590     (save-excursion
7591       (set-buffer gnus-original-article-buffer)
7592       (save-restriction
7593         (message-narrow-to-head)
7594         (let ((groups (nnmail-article-group 'identity trace)))
7595           (unless silent
7596             (if groups
7597                 (message "This message would go to %s"
7598                          (mapconcat 'car groups ", "))
7599               (message "This message would go to no groups"))
7600             groups))))))
7601
7602 (defun gnus-summary-respool-trace ()
7603   "Trace where the respool algorithm would put this article.
7604 Display a buffer showing all fancy splitting patterns which matched."
7605   (interactive)
7606   (gnus-summary-respool-query nil t))
7607
7608 ;; Summary marking commands.
7609
7610 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7611   "Mark articles which has the same subject as read, and then select the next.
7612 If UNMARK is positive, remove any kind of mark.
7613 If UNMARK is negative, tick articles."
7614   (interactive "P")
7615   (when unmark
7616     (setq unmark (prefix-numeric-value unmark)))
7617   (let ((count
7618          (gnus-summary-mark-same-subject
7619           (gnus-summary-article-subject) unmark)))
7620     ;; Select next unread article.  If auto-select-same mode, should
7621     ;; select the first unread article.
7622     (gnus-summary-next-article t (and gnus-auto-select-same
7623                                       (gnus-summary-article-subject)))
7624     (gnus-message 7 "%d article%s marked as %s"
7625                   count (if (= count 1) " is" "s are")
7626                   (if unmark "unread" "read"))))
7627
7628 (defun gnus-summary-kill-same-subject (&optional unmark)
7629   "Mark articles which has the same subject as read.
7630 If UNMARK is positive, remove any kind of mark.
7631 If UNMARK is negative, tick articles."
7632   (interactive "P")
7633   (when unmark
7634     (setq unmark (prefix-numeric-value unmark)))
7635   (let ((count
7636          (gnus-summary-mark-same-subject
7637           (gnus-summary-article-subject) unmark)))
7638     ;; If marked as read, go to next unread subject.
7639     (when (null unmark)
7640       ;; Go to next unread subject.
7641       (gnus-summary-next-subject 1 t))
7642     (gnus-message 7 "%d articles are marked as %s"
7643                   count (if unmark "unread" "read"))))
7644
7645 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7646   "Mark articles with same SUBJECT as read, and return marked number.
7647 If optional argument UNMARK is positive, remove any kinds of marks.
7648 If optional argument UNMARK is negative, mark articles as unread instead."
7649   (let ((count 1))
7650     (save-excursion
7651       (cond
7652        ((null unmark)                   ; Mark as read.
7653         (while (and
7654                 (progn
7655                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7656                   (gnus-summary-show-thread) t)
7657                 (gnus-summary-find-subject subject))
7658           (setq count (1+ count))))
7659        ((> unmark 0)                    ; Tick.
7660         (while (and
7661                 (progn
7662                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7663                   (gnus-summary-show-thread) t)
7664                 (gnus-summary-find-subject subject))
7665           (setq count (1+ count))))
7666        (t                               ; Mark as unread.
7667         (while (and
7668                 (progn
7669                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7670                   (gnus-summary-show-thread) t)
7671                 (gnus-summary-find-subject subject))
7672           (setq count (1+ count)))))
7673       (gnus-set-mode-line 'summary)
7674       ;; Return the number of marked articles.
7675       count)))
7676
7677 (defun gnus-summary-mark-as-processable (n &optional unmark)
7678   "Set the process mark on the next N articles.
7679 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7680 the process mark instead.  The difference between N and the actual
7681 number of articles marked is returned."
7682   (interactive "p")
7683   (let ((backward (< n 0))
7684         (n (abs n)))
7685     (while (and
7686             (> n 0)
7687             (if unmark
7688                 (gnus-summary-remove-process-mark
7689                  (gnus-summary-article-number))
7690               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7691             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7692       (setq n (1- n)))
7693     (when (/= 0 n)
7694       (gnus-message 7 "No more articles"))
7695     (gnus-summary-recenter)
7696     (gnus-summary-position-point)
7697     n))
7698
7699 (defun gnus-summary-unmark-as-processable (n)
7700   "Remove the process mark from the next N articles.
7701 If N is negative, unmark backward instead.  The difference between N and
7702 the actual number of articles unmarked is returned."
7703   (interactive "p")
7704   (gnus-summary-mark-as-processable n t))
7705
7706 (defun gnus-summary-unmark-all-processable ()
7707   "Remove the process mark from all articles."
7708   (interactive)
7709   (save-excursion
7710     (while gnus-newsgroup-processable
7711       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7712   (gnus-summary-position-point))
7713
7714 (defun gnus-summary-mark-as-expirable (n)
7715   "Mark N articles forward as expirable.
7716 If N is negative, mark backward instead.  The difference between N and
7717 the actual number of articles marked is returned."
7718   (interactive "p")
7719   (gnus-summary-mark-forward n gnus-expirable-mark))
7720
7721 (defun gnus-summary-mark-article-as-replied (article)
7722   "Mark ARTICLE replied and update the summary line."
7723   (push article gnus-newsgroup-replied)
7724   (let ((buffer-read-only nil))
7725     (when (gnus-summary-goto-subject article nil t)
7726       (gnus-summary-update-secondary-mark article))))
7727
7728 (defun gnus-summary-set-bookmark (article)
7729   "Set a bookmark in current article."
7730   (interactive (list (gnus-summary-article-number)))
7731   (when (or (not (get-buffer gnus-article-buffer))
7732             (not gnus-current-article)
7733             (not gnus-article-current)
7734             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7735     (error "No current article selected"))
7736   ;; Remove old bookmark, if one exists.
7737   (let ((old (assq article gnus-newsgroup-bookmarks)))
7738     (when old
7739       (setq gnus-newsgroup-bookmarks
7740             (delq old gnus-newsgroup-bookmarks))))
7741   ;; Set the new bookmark, which is on the form
7742   ;; (article-number . line-number-in-body).
7743   (push
7744    (cons article
7745          (save-excursion
7746            (set-buffer gnus-article-buffer)
7747            (count-lines
7748             (min (point)
7749                  (save-excursion
7750                    (goto-char (point-min))
7751                    (search-forward "\n\n" nil t)
7752                    (point)))
7753             (point))))
7754    gnus-newsgroup-bookmarks)
7755   (gnus-message 6 "A bookmark has been added to the current article."))
7756
7757 (defun gnus-summary-remove-bookmark (article)
7758   "Remove the bookmark from the current article."
7759   (interactive (list (gnus-summary-article-number)))
7760   ;; Remove old bookmark, if one exists.
7761   (let ((old (assq article gnus-newsgroup-bookmarks)))
7762     (if old
7763         (progn
7764           (setq gnus-newsgroup-bookmarks
7765                 (delq old gnus-newsgroup-bookmarks))
7766           (gnus-message 6 "Removed bookmark."))
7767       (gnus-message 6 "No bookmark in current article."))))
7768
7769 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7770 (defun gnus-summary-mark-as-dormant (n)
7771   "Mark N articles forward as dormant.
7772 If N is negative, mark backward instead.  The difference between N and
7773 the actual number of articles marked is returned."
7774   (interactive "p")
7775   (gnus-summary-mark-forward n gnus-dormant-mark))
7776
7777 (defun gnus-summary-set-process-mark (article)
7778   "Set the process mark on ARTICLE and update the summary line."
7779   (setq gnus-newsgroup-processable
7780         (cons article
7781               (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-remove-process-mark (article)
7788   "Remove the process mark from ARTICLE and update the summary line."
7789   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7790   (when (gnus-summary-goto-subject article)
7791     (gnus-summary-show-thread)
7792     (gnus-summary-goto-subject article)
7793     (gnus-summary-update-secondary-mark article)))
7794
7795 (defun gnus-summary-set-saved-mark (article)
7796   "Set the process mark on ARTICLE and update the summary line."
7797   (push article gnus-newsgroup-saved)
7798   (when (gnus-summary-goto-subject article)
7799     (gnus-summary-update-secondary-mark article)))
7800
7801 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7802   "Mark N articles as read forwards.
7803 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7804 The difference between N and the actual number of articles marked is
7805 returned."
7806   (interactive "p")
7807   (gnus-summary-show-thread)
7808   (let ((backward (< n 0))
7809         (gnus-summary-goto-unread
7810          (and gnus-summary-goto-unread
7811               (not (eq gnus-summary-goto-unread 'never))
7812               (not (memq mark (list gnus-unread-mark
7813                                     gnus-ticked-mark gnus-dormant-mark)))))
7814         (n (abs n))
7815         (mark (or mark gnus-del-mark)))
7816     (while (and (> n 0)
7817                 (gnus-summary-mark-article nil mark no-expire)
7818                 (zerop (gnus-summary-next-subject
7819                         (if backward -1 1)
7820                         (and gnus-summary-goto-unread
7821                              (not (eq gnus-summary-goto-unread 'never)))
7822                         t)))
7823       (setq n (1- n)))
7824     (when (/= 0 n)
7825       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7826     (gnus-summary-recenter)
7827     (gnus-summary-position-point)
7828     (gnus-set-mode-line 'summary)
7829     n))
7830
7831 (defun gnus-summary-mark-article-as-read (mark)
7832   "Mark the current article quickly as read with MARK."
7833   (let ((article (gnus-summary-article-number)))
7834     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7835     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7836     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7837     (push (cons article mark) gnus-newsgroup-reads)
7838     ;; Possibly remove from cache, if that is used.
7839     (when gnus-use-cache
7840       (gnus-cache-enter-remove-article article))
7841     ;; Allow the backend to change the mark.
7842     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7843     ;; Check for auto-expiry.
7844     (when (and gnus-newsgroup-auto-expire
7845                (memq mark gnus-auto-expirable-marks))
7846       (setq mark gnus-expirable-mark)
7847       ;; Let the backend know about the mark change.
7848       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7849       (push article gnus-newsgroup-expirable))
7850     ;; Set the mark in the buffer.
7851     (gnus-summary-update-mark mark 'unread)
7852     t))
7853
7854 (defun gnus-summary-mark-article-as-unread (mark)
7855   "Mark the current article quickly as unread with MARK."
7856   (let* ((article (gnus-summary-article-number))
7857          (old-mark (gnus-summary-article-mark article)))
7858     ;; Allow the backend to change the mark.
7859     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7860     (if (eq mark old-mark)
7861         t
7862       (if (<= article 0)
7863           (progn
7864             (gnus-error 1 "Can't mark negative article numbers")
7865             nil)
7866         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7867         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7868         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7869         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7870         (cond ((= mark gnus-ticked-mark)
7871                (push article gnus-newsgroup-marked))
7872               ((= mark gnus-dormant-mark)
7873                (push article gnus-newsgroup-dormant))
7874               (t
7875                (push article gnus-newsgroup-unreads)))
7876         (gnus-pull article gnus-newsgroup-reads)
7877
7878         ;; See whether the article is to be put in the cache.
7879         (and gnus-use-cache
7880              (vectorp (gnus-summary-article-header article))
7881              (save-excursion
7882                (gnus-cache-possibly-enter-article
7883                 gnus-newsgroup-name article
7884                 (gnus-summary-article-header article)
7885                 (= mark gnus-ticked-mark)
7886                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7887
7888         ;; Fix the mark.
7889         (gnus-summary-update-mark mark 'unread)
7890         t))))
7891
7892 (defun gnus-summary-mark-article (&optional article mark no-expire)
7893   "Mark ARTICLE with MARK.  MARK can be any character.
7894 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7895 `??' (dormant) and `?E' (expirable).
7896 If MARK is nil, then the default character `?r' is used.
7897 If ARTICLE is nil, then the article on the current line will be
7898 marked."
7899   ;; The mark might be a string.
7900   (when (stringp mark)
7901     (setq mark (aref mark 0)))
7902   ;; If no mark is given, then we check auto-expiring.
7903   (when (null mark)
7904     (setq mark gnus-del-mark))
7905   (when (and (not no-expire)
7906              gnus-newsgroup-auto-expire
7907              (memq mark gnus-auto-expirable-marks))
7908     (setq mark gnus-expirable-mark))
7909   (let ((article (or article (gnus-summary-article-number)))
7910         (old-mark (gnus-summary-article-mark article)))
7911     ;; Allow the backend to change the mark.
7912     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7913     (if (eq mark old-mark)
7914         t
7915       (unless article
7916         (error "No article on current line"))
7917       (if (not (if (or (= mark gnus-unread-mark)
7918                        (= mark gnus-ticked-mark)
7919                        (= mark gnus-dormant-mark))
7920                    (gnus-mark-article-as-unread article mark)
7921                  (gnus-mark-article-as-read article mark)))
7922           t
7923         ;; See whether the article is to be put in the cache.
7924         (and gnus-use-cache
7925              (not (= mark gnus-canceled-mark))
7926              (vectorp (gnus-summary-article-header article))
7927              (save-excursion
7928                (gnus-cache-possibly-enter-article
7929                 gnus-newsgroup-name article
7930                 (gnus-summary-article-header article)
7931                 (= mark gnus-ticked-mark)
7932                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7933
7934         (when (gnus-summary-goto-subject article nil t)
7935           (let ((buffer-read-only nil))
7936             (gnus-summary-show-thread)
7937             ;; Fix the mark.
7938             (gnus-summary-update-mark mark 'unread)
7939             t))))))
7940
7941 (defun gnus-summary-update-secondary-mark (article)
7942   "Update the secondary (read, process, cache) mark."
7943   (gnus-summary-update-mark
7944    (cond ((memq article gnus-newsgroup-processable)
7945           gnus-process-mark)
7946          ((memq article gnus-newsgroup-cached)
7947           gnus-cached-mark)
7948          ((memq article gnus-newsgroup-replied)
7949           gnus-replied-mark)
7950          ((memq article gnus-newsgroup-saved)
7951           gnus-saved-mark)
7952          (t gnus-unread-mark))
7953    'replied)
7954   (when (gnus-visual-p 'summary-highlight 'highlight)
7955     (gnus-run-hooks 'gnus-summary-update-hook))
7956   t)
7957
7958 (defun gnus-summary-update-mark (mark type)
7959   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7960         (buffer-read-only nil))
7961     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7962     (when forward
7963       (when (looking-at "\r")
7964         (incf forward))
7965       (when (<= (+ forward (point)) (point-max))
7966         ;; Go to the right position on the line.
7967         (goto-char (+ forward (point)))
7968         ;; Replace the old mark with the new mark.
7969         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
7970         ;; Optionally update the marks by some user rule.
7971         (when (eq type 'unread)
7972           (gnus-data-set-mark
7973            (gnus-data-find (gnus-summary-article-number)) mark)
7974           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
7975
7976 (defun gnus-mark-article-as-read (article &optional mark)
7977   "Enter ARTICLE in the pertinent lists and remove it from others."
7978   ;; Make the article expirable.
7979   (let ((mark (or mark gnus-del-mark)))
7980     (if (= mark gnus-expirable-mark)
7981         (push article gnus-newsgroup-expirable)
7982       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7983     ;; Remove from unread and marked lists.
7984     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7985     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7986     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7987     (push (cons article mark) gnus-newsgroup-reads)
7988     ;; Possibly remove from cache, if that is used.
7989     (when gnus-use-cache
7990       (gnus-cache-enter-remove-article article))
7991     t))
7992
7993 (defun gnus-mark-article-as-unread (article &optional mark)
7994   "Enter ARTICLE in the pertinent lists and remove it from others."
7995   (let ((mark (or mark gnus-ticked-mark)))
7996     (if (<= article 0)
7997         (progn
7998           (gnus-error 1 "Can't mark negative article numbers")
7999           nil)
8000       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8001             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8002             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8003             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8004
8005       ;; Unsuppress duplicates?
8006       (when gnus-suppress-duplicates
8007         (gnus-dup-unsuppress-article article))
8008
8009       (cond ((= mark gnus-ticked-mark)
8010              (push article gnus-newsgroup-marked))
8011             ((= mark gnus-dormant-mark)
8012              (push article gnus-newsgroup-dormant))
8013             (t
8014              (push article gnus-newsgroup-unreads)))
8015       (gnus-pull article gnus-newsgroup-reads)
8016       t)))
8017
8018 (defalias 'gnus-summary-mark-as-unread-forward
8019   'gnus-summary-tick-article-forward)
8020 (make-obsolete 'gnus-summary-mark-as-unread-forward
8021                'gnus-summary-tick-article-forward)
8022 (defun gnus-summary-tick-article-forward (n)
8023   "Tick N articles forwards.
8024 If N is negative, tick backwards instead.
8025 The difference between N and the number of articles ticked is returned."
8026   (interactive "p")
8027   (gnus-summary-mark-forward n gnus-ticked-mark))
8028
8029 (defalias 'gnus-summary-mark-as-unread-backward
8030   'gnus-summary-tick-article-backward)
8031 (make-obsolete 'gnus-summary-mark-as-unread-backward
8032                'gnus-summary-tick-article-backward)
8033 (defun gnus-summary-tick-article-backward (n)
8034   "Tick N articles backwards.
8035 The difference between N and the number of articles ticked is returned."
8036   (interactive "p")
8037   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8038
8039 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8040 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8041 (defun gnus-summary-tick-article (&optional article clear-mark)
8042   "Mark current article as unread.
8043 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8044 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8045   (interactive)
8046   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8047                                        gnus-ticked-mark)))
8048
8049 (defun gnus-summary-mark-as-read-forward (n)
8050   "Mark N articles as read forwards.
8051 If N is negative, mark backwards instead.
8052 The difference between N and the actual number of articles marked is
8053 returned."
8054   (interactive "p")
8055   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8056
8057 (defun gnus-summary-mark-as-read-backward (n)
8058   "Mark the N articles as read backwards.
8059 The difference between N and the actual number of articles marked is
8060 returned."
8061   (interactive "p")
8062   (gnus-summary-mark-forward
8063    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8064
8065 (defun gnus-summary-mark-as-read (&optional article mark)
8066   "Mark current article as read.
8067 ARTICLE specifies the article to be marked as read.
8068 MARK specifies a string to be inserted at the beginning of the line."
8069   (gnus-summary-mark-article article mark))
8070
8071 (defun gnus-summary-clear-mark-forward (n)
8072   "Clear marks from N articles forward.
8073 If N is negative, clear backward instead.
8074 The difference between N and the number of marks cleared is returned."
8075   (interactive "p")
8076   (gnus-summary-mark-forward n gnus-unread-mark))
8077
8078 (defun gnus-summary-clear-mark-backward (n)
8079   "Clear marks from N articles backward.
8080 The difference between N and the number of marks cleared is returned."
8081   (interactive "p")
8082   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8083
8084 (defun gnus-summary-mark-unread-as-read ()
8085   "Intended to be used by `gnus-summary-mark-article-hook'."
8086   (when (memq gnus-current-article gnus-newsgroup-unreads)
8087     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8088
8089 (defun gnus-summary-mark-read-and-unread-as-read ()
8090   "Intended to be used by `gnus-summary-mark-article-hook'."
8091   (let ((mark (gnus-summary-article-mark)))
8092     (when (or (gnus-unread-mark-p mark)
8093               (gnus-read-mark-p mark))
8094       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8095
8096 (defun gnus-summary-mark-region-as-read (point mark all)
8097   "Mark all unread articles between point and mark as read.
8098 If given a prefix, mark all articles between point and mark as read,
8099 even ticked and dormant ones."
8100   (interactive "r\nP")
8101   (save-excursion
8102     (let (article)
8103       (goto-char point)
8104       (beginning-of-line)
8105       (while (and
8106               (< (point) mark)
8107               (progn
8108                 (when (or all
8109                           (memq (setq article (gnus-summary-article-number))
8110                                 gnus-newsgroup-unreads))
8111                   (gnus-summary-mark-article article gnus-del-mark))
8112                 t)
8113               (gnus-summary-find-next))))))
8114
8115 (defun gnus-summary-mark-below (score mark)
8116   "Mark articles with score less than SCORE with MARK."
8117   (interactive "P\ncMark: ")
8118   (setq score (if score
8119                   (prefix-numeric-value score)
8120                 (or gnus-summary-default-score 0)))
8121   (save-excursion
8122     (set-buffer gnus-summary-buffer)
8123     (goto-char (point-min))
8124     (while
8125         (progn
8126           (and (< (gnus-summary-article-score) score)
8127                (gnus-summary-mark-article nil mark))
8128           (gnus-summary-find-next)))))
8129
8130 (defun gnus-summary-kill-below (&optional score)
8131   "Mark articles with score below SCORE as read."
8132   (interactive "P")
8133   (gnus-summary-mark-below score gnus-killed-mark))
8134
8135 (defun gnus-summary-clear-above (&optional score)
8136   "Clear all marks from articles with score above SCORE."
8137   (interactive "P")
8138   (gnus-summary-mark-above score gnus-unread-mark))
8139
8140 (defun gnus-summary-tick-above (&optional score)
8141   "Tick all articles with score above SCORE."
8142   (interactive "P")
8143   (gnus-summary-mark-above score gnus-ticked-mark))
8144
8145 (defun gnus-summary-mark-above (score mark)
8146   "Mark articles with score over SCORE with MARK."
8147   (interactive "P\ncMark: ")
8148   (setq score (if score
8149                   (prefix-numeric-value score)
8150                 (or gnus-summary-default-score 0)))
8151   (save-excursion
8152     (set-buffer gnus-summary-buffer)
8153     (goto-char (point-min))
8154     (while (and (progn
8155                   (when (> (gnus-summary-article-score) score)
8156                     (gnus-summary-mark-article nil mark))
8157                   t)
8158                 (gnus-summary-find-next)))))
8159
8160 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8161 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8162 (defun gnus-summary-limit-include-expunged (&optional no-error)
8163   "Display all the hidden articles that were expunged for low scores."
8164   (interactive)
8165   (let ((buffer-read-only nil))
8166     (let ((scored gnus-newsgroup-scored)
8167           headers h)
8168       (while scored
8169         (unless (gnus-summary-goto-subject (caar scored))
8170           (and (setq h (gnus-summary-article-header (caar scored)))
8171                (< (cdar scored) gnus-summary-expunge-below)
8172                (push h headers)))
8173         (setq scored (cdr scored)))
8174       (if (not headers)
8175           (when (not no-error)
8176             (error "No expunged articles hidden"))
8177         (goto-char (point-min))
8178         (gnus-summary-prepare-unthreaded (nreverse headers))
8179         (goto-char (point-min))
8180         (gnus-summary-position-point)
8181         t))))
8182
8183 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8184   "Mark all unread articles in this newsgroup as read.
8185 If prefix argument ALL is non-nil, ticked and dormant articles will
8186 also be marked as read.
8187 If QUIETLY is non-nil, no questions will be asked.
8188 If TO-HERE is non-nil, it should be a point in the buffer.  All
8189 articles before this point will be marked as read.
8190 Note that this function will only catch up the unread article
8191 in the current summary buffer limitation.
8192 The number of articles marked as read is returned."
8193   (interactive "P")
8194   (prog1
8195       (save-excursion
8196         (when (or quietly
8197                   (not gnus-interactive-catchup) ;Without confirmation?
8198                   gnus-expert-user
8199                   (gnus-y-or-n-p
8200                    (if all
8201                        "Mark absolutely all articles as read? "
8202                      "Mark all unread articles as read? ")))
8203           (if (and not-mark
8204                    (not gnus-newsgroup-adaptive)
8205                    (not gnus-newsgroup-auto-expire)
8206                    (not gnus-suppress-duplicates)
8207                    (or (not gnus-use-cache)
8208                        (eq gnus-use-cache 'passive)))
8209               (progn
8210                 (when all
8211                   (setq gnus-newsgroup-marked nil
8212                         gnus-newsgroup-dormant nil))
8213                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8214             ;; We actually mark all articles as canceled, which we
8215             ;; have to do when using auto-expiry or adaptive scoring.
8216             (gnus-summary-show-all-threads)
8217             (when (gnus-summary-first-subject (not all) t)
8218               (while (and
8219                       (if to-here (< (point) to-here) t)
8220                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8221                       (gnus-summary-find-next (not all) nil nil t))))
8222             (gnus-set-mode-line 'summary))
8223           t))
8224     (gnus-summary-position-point)))
8225
8226 (defun gnus-summary-catchup-to-here (&optional all)
8227   "Mark all unticked articles before the current one as read.
8228 If ALL is non-nil, also mark ticked and dormant articles as read."
8229   (interactive "P")
8230   (save-excursion
8231     (gnus-save-hidden-threads
8232       (let ((beg (point)))
8233         ;; We check that there are unread articles.
8234         (when (or all (gnus-summary-find-prev))
8235           (gnus-summary-catchup all t beg)))))
8236   (gnus-summary-position-point))
8237
8238 (defun gnus-summary-catchup-all (&optional quietly)
8239   "Mark all articles in this newsgroup as read."
8240   (interactive "P")
8241   (gnus-summary-catchup t quietly))
8242
8243 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8244   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8245 If prefix argument ALL is non-nil, all articles are marked as read."
8246   (interactive "P")
8247   (when (gnus-summary-catchup all quietly nil 'fast)
8248     ;; Select next newsgroup or exit.
8249     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8250              (eq gnus-auto-select-next 'quietly))
8251         (gnus-summary-next-group nil)
8252       (gnus-summary-exit))))
8253
8254 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8255   "Mark all articles in this newsgroup as read, and then exit."
8256   (interactive "P")
8257   (gnus-summary-catchup-and-exit t quietly))
8258
8259 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8260 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8261   "Mark all articles in this group as read and select the next group.
8262 If given a prefix, mark all articles, unread as well as ticked, as
8263 read."
8264   (interactive "P")
8265   (save-excursion
8266     (gnus-summary-catchup all))
8267   (gnus-summary-next-article t nil nil t))
8268
8269 ;; Thread-based commands.
8270
8271 (defun gnus-summary-articles-in-thread (&optional article)
8272   "Return a list of all articles in the current thread.
8273 If ARTICLE is non-nil, return all articles in the thread that starts
8274 with that article."
8275   (let* ((article (or article (gnus-summary-article-number)))
8276          (data (gnus-data-find-list article))
8277          (top-level (gnus-data-level (car data)))
8278          (top-subject
8279           (cond ((null gnus-thread-operation-ignore-subject)
8280                  (gnus-simplify-subject-re
8281                   (mail-header-subject (gnus-data-header (car data)))))
8282                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8283                  (gnus-simplify-subject-fuzzy
8284                   (mail-header-subject (gnus-data-header (car data)))))
8285                 (t nil)))
8286          (end-point (save-excursion
8287                       (if (gnus-summary-go-to-next-thread)
8288                           (point) (point-max))))
8289          articles)
8290     (while (and data
8291                 (< (gnus-data-pos (car data)) end-point))
8292       (when (or (not top-subject)
8293                 (string= top-subject
8294                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8295                              (gnus-simplify-subject-fuzzy
8296                               (mail-header-subject
8297                                (gnus-data-header (car data))))
8298                            (gnus-simplify-subject-re
8299                             (mail-header-subject
8300                              (gnus-data-header (car data)))))))
8301         (push (gnus-data-number (car data)) articles))
8302       (unless (and (setq data (cdr data))
8303                    (> (gnus-data-level (car data)) top-level))
8304         (setq data nil)))
8305     ;; Return the list of articles.
8306     (nreverse articles)))
8307
8308 (defun gnus-summary-rethread-current ()
8309   "Rethread the thread the current article is part of."
8310   (interactive)
8311   (let* ((gnus-show-threads t)
8312          (article (gnus-summary-article-number))
8313          (id (mail-header-id (gnus-summary-article-header)))
8314          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8315     (unless id
8316       (error "No article on the current line"))
8317     (gnus-rebuild-thread id)
8318     (gnus-summary-goto-subject article)))
8319
8320 (defun gnus-summary-reparent-thread ()
8321   "Make the current article child of the marked (or previous) article.
8322
8323 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8324 is non-nil or the Subject: of both articles are the same."
8325   (interactive)
8326   (unless (not (gnus-group-read-only-p))
8327     (error "The current newsgroup does not support article editing"))
8328   (unless (<= (length gnus-newsgroup-processable) 1)
8329     (error "No more than one article may be marked"))
8330   (save-window-excursion
8331     (let ((gnus-article-buffer " *reparent*")
8332           (current-article (gnus-summary-article-number))
8333           ;; First grab the marked article, otherwise one line up.
8334           (parent-article (if (not (null gnus-newsgroup-processable))
8335                               (car gnus-newsgroup-processable)
8336                             (save-excursion
8337                               (if (eq (forward-line -1) 0)
8338                                   (gnus-summary-article-number)
8339                                 (error "Beginning of summary buffer"))))))
8340       (unless (not (eq current-article parent-article))
8341         (error "An article may not be self-referential"))
8342       (let ((message-id (mail-header-id
8343                          (gnus-summary-article-header parent-article))))
8344         (unless (and message-id (not (equal message-id "")))
8345           (error "No message-id in desired parent"))
8346         (gnus-with-article current-article
8347           (goto-char (point-min))
8348           (if (re-search-forward "^References: " nil t)
8349               (progn
8350                 (re-search-forward "^[^ \t]" nil t)
8351                 (forward-line -1)
8352                 (end-of-line)
8353                 (insert " " message-id))
8354             (insert "References: " message-id "\n")))
8355         (set-buffer gnus-summary-buffer)
8356         (gnus-summary-unmark-all-processable)
8357         (gnus-summary-update-article current-article)
8358         (gnus-summary-rethread-current)
8359         (gnus-message 3 "Article %d is now the child of article %d"
8360                       current-article parent-article)))))
8361
8362 (defun gnus-summary-toggle-threads (&optional arg)
8363   "Toggle showing conversation threads.
8364 If ARG is positive number, turn showing conversation threads on."
8365   (interactive "P")
8366   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8367     (setq gnus-show-threads
8368           (if (null arg) (not gnus-show-threads)
8369             (> (prefix-numeric-value arg) 0)))
8370     (gnus-summary-prepare)
8371     (gnus-summary-goto-subject current)
8372     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8373     (gnus-summary-position-point)))
8374
8375 (defun gnus-summary-show-all-threads ()
8376   "Show all threads."
8377   (interactive)
8378   (save-excursion
8379     (let ((buffer-read-only nil))
8380       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8381   (gnus-summary-position-point))
8382
8383 (defun gnus-summary-show-thread ()
8384   "Show thread subtrees.
8385 Returns nil if no thread was there to be shown."
8386   (interactive)
8387   (let ((buffer-read-only nil)
8388         (orig (point))
8389         ;; first goto end then to beg, to have point at beg after let
8390         (end (progn (end-of-line) (point)))
8391         (beg (progn (beginning-of-line) (point))))
8392     (prog1
8393         ;; Any hidden lines here?
8394         (search-forward "\r" end t)
8395       (subst-char-in-region beg end ?\^M ?\n t)
8396       (goto-char orig)
8397       (gnus-summary-position-point))))
8398
8399 (defun gnus-summary-hide-all-threads ()
8400   "Hide all thread subtrees."
8401   (interactive)
8402   (save-excursion
8403     (goto-char (point-min))
8404     (gnus-summary-hide-thread)
8405     (while (zerop (gnus-summary-next-thread 1 t))
8406       (gnus-summary-hide-thread)))
8407   (gnus-summary-position-point))
8408
8409 (defun gnus-summary-hide-thread ()
8410   "Hide thread subtrees.
8411 Returns nil if no threads were there to be hidden."
8412   (interactive)
8413   (let ((buffer-read-only nil)
8414         (start (point))
8415         (article (gnus-summary-article-number)))
8416     (goto-char start)
8417     ;; Go forward until either the buffer ends or the subthread
8418     ;; ends.
8419     (when (and (not (eobp))
8420                (or (zerop (gnus-summary-next-thread 1 t))
8421                    (goto-char (point-max))))
8422       (prog1
8423           (if (and (> (point) start)
8424                    (search-backward "\n" start t))
8425               (progn
8426                 (subst-char-in-region start (point) ?\n ?\^M)
8427                 (gnus-summary-goto-subject article))
8428             (goto-char start)
8429             nil)
8430         ;;(gnus-summary-position-point)
8431         ))))
8432
8433 (defun gnus-summary-go-to-next-thread (&optional previous)
8434   "Go to the same level (or less) next thread.
8435 If PREVIOUS is non-nil, go to previous thread instead.
8436 Return the article number moved to, or nil if moving was impossible."
8437   (let ((level (gnus-summary-thread-level))
8438         (way (if previous -1 1))
8439         (beg (point)))
8440     (forward-line way)
8441     (while (and (not (eobp))
8442                 (< level (gnus-summary-thread-level)))
8443       (forward-line way))
8444     (if (eobp)
8445         (progn
8446           (goto-char beg)
8447           nil)
8448       (setq beg (point))
8449       (prog1
8450           (gnus-summary-article-number)
8451         (goto-char beg)))))
8452
8453 (defun gnus-summary-next-thread (n &optional silent)
8454   "Go to the same level next N'th thread.
8455 If N is negative, search backward instead.
8456 Returns the difference between N and the number of skips actually
8457 done.
8458
8459 If SILENT, don't output messages."
8460   (interactive "p")
8461   (let ((backward (< n 0))
8462         (n (abs n)))
8463     (while (and (> n 0)
8464                 (gnus-summary-go-to-next-thread backward))
8465       (decf n))
8466     (unless silent
8467       (gnus-summary-position-point))
8468     (when (and (not silent) (/= 0 n))
8469       (gnus-message 7 "No more threads"))
8470     n))
8471
8472 (defun gnus-summary-prev-thread (n)
8473   "Go to the same level previous N'th thread.
8474 Returns the difference between N and the number of skips actually
8475 done."
8476   (interactive "p")
8477   (gnus-summary-next-thread (- n)))
8478
8479 (defun gnus-summary-go-down-thread ()
8480   "Go down one level in the current thread."
8481   (let ((children (gnus-summary-article-children)))
8482     (when children
8483       (gnus-summary-goto-subject (car children)))))
8484
8485 (defun gnus-summary-go-up-thread ()
8486   "Go up one level in the current thread."
8487   (let ((parent (gnus-summary-article-parent)))
8488     (when parent
8489       (gnus-summary-goto-subject parent))))
8490
8491 (defun gnus-summary-down-thread (n)
8492   "Go down thread N steps.
8493 If N is negative, go up instead.
8494 Returns the difference between N and how many steps down that were
8495 taken."
8496   (interactive "p")
8497   (let ((up (< n 0))
8498         (n (abs n)))
8499     (while (and (> n 0)
8500                 (if up (gnus-summary-go-up-thread)
8501                   (gnus-summary-go-down-thread)))
8502       (setq n (1- n)))
8503     (gnus-summary-position-point)
8504     (when (/= 0 n)
8505       (gnus-message 7 "Can't go further"))
8506     n))
8507
8508 (defun gnus-summary-up-thread (n)
8509   "Go up thread N steps.
8510 If N is negative, go up instead.
8511 Returns the difference between N and how many steps down that were
8512 taken."
8513   (interactive "p")
8514   (gnus-summary-down-thread (- n)))
8515
8516 (defun gnus-summary-top-thread ()
8517   "Go to the top of the thread."
8518   (interactive)
8519   (while (gnus-summary-go-up-thread))
8520   (gnus-summary-article-number))
8521
8522 (defun gnus-summary-kill-thread (&optional unmark)
8523   "Mark articles under current thread as read.
8524 If the prefix argument is positive, remove any kinds of marks.
8525 If the prefix argument is negative, tick articles instead."
8526   (interactive "P")
8527   (when unmark
8528     (setq unmark (prefix-numeric-value unmark)))
8529   (let ((articles (gnus-summary-articles-in-thread)))
8530     (save-excursion
8531       ;; Expand the thread.
8532       (gnus-summary-show-thread)
8533       ;; Mark all the articles.
8534       (while articles
8535         (gnus-summary-goto-subject (car articles))
8536         (cond ((null unmark)
8537                (gnus-summary-mark-article-as-read gnus-killed-mark))
8538               ((> unmark 0)
8539                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8540               (t
8541                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8542         (setq articles (cdr articles))))
8543     ;; Hide killed subtrees.
8544     (and (null unmark)
8545          gnus-thread-hide-killed
8546          (gnus-summary-hide-thread))
8547     ;; If marked as read, go to next unread subject.
8548     (when (null unmark)
8549       ;; Go to next unread subject.
8550       (gnus-summary-next-subject 1 t)))
8551   (gnus-set-mode-line 'summary))
8552
8553 ;; Summary sorting commands
8554
8555 (defun gnus-summary-sort-by-number (&optional reverse)
8556   "Sort the summary buffer by article number.
8557 Argument REVERSE means reverse order."
8558   (interactive "P")
8559   (gnus-summary-sort 'number reverse))
8560
8561 (defun gnus-summary-sort-by-author (&optional reverse)
8562   "Sort the summary buffer by author name alphabetically.
8563 If case-fold-search is non-nil, case of letters is ignored.
8564 Argument REVERSE means reverse order."
8565   (interactive "P")
8566   (gnus-summary-sort 'author reverse))
8567
8568 (defun gnus-summary-sort-by-subject (&optional reverse)
8569   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8570 If case-fold-search is non-nil, case of letters is ignored.
8571 Argument REVERSE means reverse order."
8572   (interactive "P")
8573   (gnus-summary-sort 'subject reverse))
8574
8575 (defun gnus-summary-sort-by-date (&optional reverse)
8576   "Sort the summary buffer by date.
8577 Argument REVERSE means reverse order."
8578   (interactive "P")
8579   (gnus-summary-sort 'date reverse))
8580
8581 (defun gnus-summary-sort-by-score (&optional reverse)
8582   "Sort the summary buffer by score.
8583 Argument REVERSE means reverse order."
8584   (interactive "P")
8585   (gnus-summary-sort 'score reverse))
8586
8587 (defun gnus-summary-sort-by-lines (&optional reverse)
8588   "Sort the summary buffer by the number of lines.
8589 Argument REVERSE means reverse order."
8590   (interactive "P")
8591   (gnus-summary-sort 'lines reverse))
8592
8593 (defun gnus-summary-sort-by-chars (&optional reverse)
8594   "Sort the summary buffer by article length.
8595 Argument REVERSE means reverse order."
8596   (interactive "P")
8597   (gnus-summary-sort 'chars reverse))   
8598
8599 (defun gnus-summary-sort (predicate reverse)
8600   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8601   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8602          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8603          (gnus-thread-sort-functions
8604           (if (not reverse)
8605               thread
8606             `(lambda (t1 t2)
8607                (,thread t2 t1))))
8608          (gnus-article-sort-functions
8609           (if (not reverse)
8610               article
8611             `(lambda (t1 t2)
8612                (,article t2 t1))))
8613          (buffer-read-only)
8614          (gnus-summary-prepare-hook nil))
8615     ;; We do the sorting by regenerating the threads.
8616     (gnus-summary-prepare)
8617     ;; Hide subthreads if needed.
8618     (when (and gnus-show-threads gnus-thread-hide-subtree)
8619       (gnus-summary-hide-all-threads))))
8620
8621 ;; Summary saving commands.
8622
8623 (defun gnus-summary-save-article (&optional n not-saved)
8624   "Save the current article using the default saver function.
8625 If N is a positive number, save the N next articles.
8626 If N is a negative number, save the N previous articles.
8627 If N is nil and any articles have been marked with the process mark,
8628 save those articles instead.
8629 The variable `gnus-default-article-saver' specifies the saver function."
8630   (interactive "P")
8631   (let* ((articles (gnus-summary-work-articles n))
8632          (save-buffer (save-excursion
8633                         (nnheader-set-temp-buffer " *Gnus Save*")))
8634          (num (length articles))
8635          header file)
8636     (dolist (article articles)
8637       (setq header (gnus-summary-article-header article))
8638       (if (not (vectorp header))
8639           ;; This is a pseudo-article.
8640           (if (assq 'name header)
8641               (gnus-copy-file (cdr (assq 'name header)))
8642             (gnus-message 1 "Article %d is unsaveable" article))
8643         ;; This is a real article.
8644         (save-window-excursion
8645           (gnus-summary-select-article t nil nil article))
8646         (save-excursion
8647           (set-buffer save-buffer)
8648           (erase-buffer)
8649           (insert-buffer-substring gnus-original-article-buffer))
8650         (setq file (gnus-article-save save-buffer file num))
8651         (gnus-summary-remove-process-mark article)
8652         (unless not-saved
8653           (gnus-summary-set-saved-mark article))))
8654     (gnus-kill-buffer save-buffer)
8655     (gnus-summary-position-point)
8656     (gnus-set-mode-line 'summary)
8657     n))
8658
8659 (defun gnus-summary-pipe-output (&optional arg)
8660   "Pipe the current article to a subprocess.
8661 If N is a positive number, pipe the N next articles.
8662 If N is a negative number, pipe the N previous articles.
8663 If N is nil and any articles have been marked with the process mark,
8664 pipe those articles instead."
8665   (interactive "P")
8666   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8667     (gnus-summary-save-article arg t))
8668   (gnus-configure-windows 'pipe))
8669
8670 (defun gnus-summary-save-article-mail (&optional arg)
8671   "Append the current article to an mail file.
8672 If N is a positive number, save the N next articles.
8673 If N is a negative number, save the N previous articles.
8674 If N is nil and any articles have been marked with the process mark,
8675 save those articles instead."
8676   (interactive "P")
8677   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8678     (gnus-summary-save-article arg)))
8679
8680 (defun gnus-summary-save-article-rmail (&optional arg)
8681   "Append the current article to an rmail file.
8682 If N is a positive number, save the N next articles.
8683 If N is a negative number, save the N previous articles.
8684 If N is nil and any articles have been marked with the process mark,
8685 save those articles instead."
8686   (interactive "P")
8687   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8688     (gnus-summary-save-article arg)))
8689
8690 (defun gnus-summary-save-article-file (&optional arg)
8691   "Append the current article to a file.
8692 If N is a positive number, save the N next articles.
8693 If N is a negative number, save the N previous articles.
8694 If N is nil and any articles have been marked with the process mark,
8695 save those articles instead."
8696   (interactive "P")
8697   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8698     (gnus-summary-save-article arg)))
8699
8700 (defun gnus-summary-write-article-file (&optional arg)
8701   "Write the current article to a file, deleting the previous file.
8702 If N is a positive number, save the N next articles.
8703 If N is a negative number, save the N previous articles.
8704 If N is nil and any articles have been marked with the process mark,
8705 save those articles instead."
8706   (interactive "P")
8707   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8708     (gnus-summary-save-article arg)))
8709
8710 (defun gnus-summary-save-article-body-file (&optional arg)
8711   "Append the current article body to a file.
8712 If N is a positive number, save the N next articles.
8713 If N is a negative number, save the N previous articles.
8714 If N is nil and any articles have been marked with the process mark,
8715 save those articles instead."
8716   (interactive "P")
8717   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8718     (gnus-summary-save-article arg)))
8719
8720 (defun gnus-summary-pipe-message (program)
8721   "Pipe the current article through PROGRAM."
8722   (interactive "sProgram: ")
8723   (gnus-summary-select-article)
8724   (let ((mail-header-separator ""))
8725     (gnus-eval-in-buffer-window gnus-article-buffer
8726       (save-restriction
8727         (widen)
8728         (let ((start (window-start))
8729               buffer-read-only)
8730           (message-pipe-buffer-body program)
8731           (set-window-start (get-buffer-window (current-buffer)) start))))))
8732
8733 (defun gnus-get-split-value (methods)
8734   "Return a value based on the split METHODS."
8735   (let (split-name method result match)
8736     (when methods
8737       (save-excursion
8738         (set-buffer gnus-original-article-buffer)
8739         (save-restriction
8740           (nnheader-narrow-to-headers)
8741           (while methods
8742             (goto-char (point-min))
8743             (setq method (pop methods))
8744             (setq match (car method))
8745             (when (cond
8746                    ((stringp match)
8747                     ;; Regular expression.
8748                     (ignore-errors
8749                       (re-search-forward match nil t)))
8750                    ((gnus-functionp match)
8751                     ;; Function.
8752                     (save-restriction
8753                       (widen)
8754                       (setq result (funcall match gnus-newsgroup-name))))
8755                    ((consp match)
8756                     ;; Form.
8757                     (save-restriction
8758                       (widen)
8759                       (setq result (eval match)))))
8760               (setq split-name (append (cdr method) split-name))
8761               (cond ((stringp result)
8762                      (push (expand-file-name
8763                             result gnus-article-save-directory)
8764                            split-name))
8765                     ((consp result)
8766                      (setq split-name (append result split-name)))))))))
8767     (nreverse split-name)))
8768
8769 (defun gnus-valid-move-group-p (group)
8770   (and (boundp group)
8771        (symbol-name group)
8772        (symbol-value group)
8773        (memq 'respool
8774              (assoc (symbol-name
8775                      (car (gnus-find-method-for-group
8776                            (symbol-name group))))
8777                     gnus-valid-select-methods))))
8778
8779 (defun gnus-read-move-group-name (prompt default articles prefix)
8780   "Read a group name."
8781   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8782          (minibuffer-confirm-incomplete nil) ; XEmacs
8783          (prom
8784           (format "%s %s to:"
8785                   prompt
8786                   (if (> (length articles) 1)
8787                       (format "these %d articles" (length articles))
8788                     "this article")))
8789          (to-newsgroup
8790           (cond
8791            ((null split-name)
8792             (gnus-completing-read default prom
8793                                   gnus-active-hashtb
8794                                   'gnus-valid-move-group-p
8795                                   nil prefix
8796                                   'gnus-group-history))
8797            ((= 1 (length split-name))
8798             (gnus-completing-read (car split-name) prom
8799                                   gnus-active-hashtb
8800                                   'gnus-valid-move-group-p
8801                                   nil nil
8802                                   'gnus-group-history))
8803            (t
8804             (gnus-completing-read nil prom
8805                                   (mapcar (lambda (el) (list el))
8806                                           (nreverse split-name))
8807                                   nil nil nil
8808                                   'gnus-group-history)))))
8809     (when to-newsgroup
8810       (if (or (string= to-newsgroup "")
8811               (string= to-newsgroup prefix))
8812           (setq to-newsgroup default))
8813       (unless to-newsgroup
8814         (error "No group name entered"))
8815       (or (gnus-active to-newsgroup)
8816           (gnus-activate-group to-newsgroup)
8817           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8818                                      to-newsgroup))
8819               (or (and (gnus-request-create-group
8820                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8821                        (gnus-activate-group to-newsgroup nil nil
8822                                             (gnus-group-name-to-method
8823                                              to-newsgroup)))
8824                   (error "Couldn't create group %s" to-newsgroup)))
8825           (error "No such group: %s" to-newsgroup)))
8826     to-newsgroup))
8827
8828 ;; Summary extract commands
8829
8830 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8831   (let ((buffer-read-only nil)
8832         (article (gnus-summary-article-number))
8833         after-article b e)
8834     (unless (gnus-summary-goto-subject article)
8835       (error "No such article: %d" article))
8836     (gnus-summary-position-point)
8837     ;; If all commands are to be bunched up on one line, we collect
8838     ;; them here.
8839     (unless gnus-view-pseudos-separately
8840       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8841             files action)
8842         (while ps
8843           (setq action (cdr (assq 'action (car ps))))
8844           (setq files (list (cdr (assq 'name (car ps)))))
8845           (while (and ps (cdr ps)
8846                       (string= (or action "1")
8847                                (or (cdr (assq 'action (cadr ps))) "2")))
8848             (push (cdr (assq 'name (cadr ps))) files)
8849             (setcdr ps (cddr ps)))
8850           (when files
8851             (when (not (string-match "%s" action))
8852               (push " " files))
8853             (push " " files)
8854             (when (assq 'execute (car ps))
8855               (setcdr (assq 'execute (car ps))
8856                       (funcall (if (string-match "%s" action)
8857                                    'format 'concat)
8858                                action
8859                                (mapconcat
8860                                 (lambda (f)
8861                                   (if (equal f " ")
8862                                       f
8863                                     (gnus-quote-arg-for-sh-or-csh f)))
8864                                 files " ")))))
8865           (setq ps (cdr ps)))))
8866     (if (and gnus-view-pseudos (not not-view))
8867         (while pslist
8868           (when (assq 'execute (car pslist))
8869             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8870                                   (eq gnus-view-pseudos 'not-confirm)))
8871           (setq pslist (cdr pslist)))
8872       (save-excursion
8873         (while pslist
8874           (setq after-article (or (cdr (assq 'article (car pslist)))
8875                                   (gnus-summary-article-number)))
8876           (gnus-summary-goto-subject after-article)
8877           (forward-line 1)
8878           (setq b (point))
8879           (insert "    " (file-name-nondirectory
8880                           (cdr (assq 'name (car pslist))))
8881                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8882           (setq e (point))
8883           (forward-line -1)             ; back to `b'
8884           (gnus-add-text-properties
8885            b (1- e) (list 'gnus-number gnus-reffed-article-number
8886                           gnus-mouse-face-prop gnus-mouse-face))
8887           (gnus-data-enter
8888            after-article gnus-reffed-article-number
8889            gnus-unread-mark b (car pslist) 0 (- e b))
8890           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8891           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8892           (setq pslist (cdr pslist)))))))
8893
8894 (defun gnus-pseudos< (p1 p2)
8895   (let ((c1 (cdr (assq 'action p1)))
8896         (c2 (cdr (assq 'action p2))))
8897     (and c1 c2 (string< c1 c2))))
8898
8899 (defun gnus-request-pseudo-article (props)
8900   (cond ((assq 'execute props)
8901          (gnus-execute-command (cdr (assq 'execute props)))))
8902   (let ((gnus-current-article (gnus-summary-article-number)))
8903     (gnus-run-hooks 'gnus-mark-article-hook)))
8904
8905 (defun gnus-execute-command (command &optional automatic)
8906   (save-excursion
8907     (gnus-article-setup-buffer)
8908     (set-buffer gnus-article-buffer)
8909     (setq buffer-read-only nil)
8910     (let ((command (if automatic command
8911                      (read-string "Command: " (cons command 0)))))
8912       (erase-buffer)
8913       (insert "$ " command "\n\n")
8914       (if gnus-view-pseudo-asynchronously
8915           (start-process "gnus-execute" (current-buffer) shell-file-name
8916                          shell-command-switch command)
8917         (call-process shell-file-name nil t nil
8918                       shell-command-switch command)))))
8919
8920 ;; Summary kill commands.
8921
8922 (defun gnus-summary-edit-global-kill (article)
8923   "Edit the \"global\" kill file."
8924   (interactive (list (gnus-summary-article-number)))
8925   (gnus-group-edit-global-kill article))
8926
8927 (defun gnus-summary-edit-local-kill ()
8928   "Edit a local kill file applied to the current newsgroup."
8929   (interactive)
8930   (setq gnus-current-headers (gnus-summary-article-header))
8931   (gnus-group-edit-local-kill
8932    (gnus-summary-article-number) gnus-newsgroup-name))
8933
8934 ;;; Header reading.
8935
8936 (defun gnus-read-header (id &optional header)
8937   "Read the headers of article ID and enter them into the Gnus system."
8938   (let ((group gnus-newsgroup-name)
8939         (gnus-override-method
8940          (and (gnus-news-group-p gnus-newsgroup-name)
8941               gnus-refer-article-method))
8942         where)
8943     ;; First we check to see whether the header in question is already
8944     ;; fetched.
8945     (if (stringp id)
8946         ;; This is a Message-ID.
8947         (setq header (or header (gnus-id-to-header id)))
8948       ;; This is an article number.
8949       (setq header (or header (gnus-summary-article-header id))))
8950     (if (and header
8951              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8952         ;; We have found the header.
8953         header
8954       ;; If this is a sparse article, we have to nix out its
8955       ;; previous entry in the thread hashtb.
8956       (when (and header
8957                  (gnus-summary-article-sparse-p (mail-header-number header)))
8958         (let* ((parent (gnus-parent-id (mail-header-references header)))
8959                (thread (and parent (gnus-id-to-thread parent))))
8960           (when thread
8961             (delq (assq header thread) thread))))
8962       ;; We have to really fetch the header to this article.
8963       (save-excursion
8964         (set-buffer nntp-server-buffer)
8965         (when (setq where (gnus-request-head id group))
8966           (nnheader-fold-continuation-lines)
8967           (goto-char (point-max))
8968           (insert ".\n")
8969           (goto-char (point-min))
8970           (insert "211 ")
8971           (princ (cond
8972                   ((numberp id) id)
8973                   ((cdr where) (cdr where))
8974                   (header (mail-header-number header))
8975                   (t gnus-reffed-article-number))
8976                  (current-buffer))
8977           (insert " Article retrieved.\n"))
8978         (if (or (not where)
8979                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8980             ()                          ; Malformed head.
8981           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8982             (when (and (stringp id)
8983                        (not (string= (gnus-group-real-name group)
8984                                      (car where))))
8985               ;; If we fetched by Message-ID and the article came
8986               ;; from a different group, we fudge some bogus article
8987               ;; numbers for this article.
8988               (mail-header-set-number header gnus-reffed-article-number))
8989             (save-excursion
8990               (set-buffer gnus-summary-buffer)
8991               (decf gnus-reffed-article-number)
8992               (gnus-remove-header (mail-header-number header))
8993               (push header gnus-newsgroup-headers)
8994               (setq gnus-current-headers header)
8995               (push (mail-header-number header) gnus-newsgroup-limit)))
8996           header)))))
8997
8998 (defun gnus-remove-header (number)
8999   "Remove header NUMBER from `gnus-newsgroup-headers'."
9000   (if (and gnus-newsgroup-headers
9001            (= number (mail-header-number (car gnus-newsgroup-headers))))
9002       (pop gnus-newsgroup-headers)
9003     (let ((headers gnus-newsgroup-headers))
9004       (while (and (cdr headers)
9005                   (not (= number (mail-header-number (cadr headers)))))
9006         (pop headers))
9007       (when (cdr headers)
9008         (setcdr headers (cddr headers))))))
9009
9010 ;;;
9011 ;;; summary highlights
9012 ;;;
9013
9014 (defun gnus-highlight-selected-summary ()
9015   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9016   ;; Highlight selected article in summary buffer
9017   (when gnus-summary-selected-face
9018     (save-excursion
9019       (let* ((beg (progn (beginning-of-line) (point)))
9020              (end (progn (end-of-line) (point)))
9021              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9022              (from (if (get-text-property beg gnus-mouse-face-prop)
9023                        beg
9024                      (or (next-single-property-change
9025                           beg gnus-mouse-face-prop nil end)
9026                          beg)))
9027              (to
9028               (if (= from end)
9029                   (- from 2)
9030                 (or (next-single-property-change
9031                      from gnus-mouse-face-prop nil end)
9032                     end))))
9033         ;; If no mouse-face prop on line we will have to = from = end,
9034         ;; so we highlight the entire line instead.
9035         (when (= (+ to 2) from)
9036           (setq from beg)
9037           (setq to end))
9038         (if gnus-newsgroup-selected-overlay
9039             ;; Move old overlay.
9040             (gnus-move-overlay
9041              gnus-newsgroup-selected-overlay from to (current-buffer))
9042           ;; Create new overlay.
9043           (gnus-overlay-put
9044            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9045            'face gnus-summary-selected-face))))))
9046
9047 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9048 (defun gnus-summary-highlight-line ()
9049   "Highlight current line according to `gnus-summary-highlight'."
9050   (let* ((list gnus-summary-highlight)
9051          (p (point))
9052          (end (progn (end-of-line) (point)))
9053          ;; now find out where the line starts and leave point there.
9054          (beg (progn (beginning-of-line) (point)))
9055          (article (gnus-summary-article-number))
9056          (score (or (cdr (assq (or article gnus-current-article)
9057                                gnus-newsgroup-scored))
9058                     gnus-summary-default-score 0))
9059          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9060          (inhibit-read-only t))
9061     ;; Eval the cars of the lists until we find a match.
9062     (let ((default gnus-summary-default-score))
9063       (while (and list
9064                   (not (eval (caar list))))
9065         (setq list (cdr list))))
9066     (let ((face (cdar list)))
9067       (unless (eq face (get-text-property beg 'face))
9068         (gnus-put-text-property-excluding-characters-with-faces
9069          beg end 'face
9070          (setq face (if (boundp face) (symbol-value face) face)))
9071         (when gnus-summary-highlight-line-function
9072           (funcall gnus-summary-highlight-line-function article face))))
9073     (goto-char p)))
9074
9075 (defun gnus-update-read-articles (group unread &optional compute)
9076   "Update the list of read articles in GROUP."
9077   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9078          (entry (gnus-gethash group gnus-newsrc-hashtb))
9079          (info (nth 2 entry))
9080          (prev 1)
9081          (unread (sort (copy-sequence unread) '<))
9082          read)
9083     (if (or (not info) (not active))
9084         ;; There is no info on this group if it was, in fact,
9085         ;; killed.  Gnus stores no information on killed groups, so
9086         ;; there's nothing to be done.
9087         ;; One could store the information somewhere temporarily,
9088         ;; perhaps...  Hmmm...
9089         ()
9090       ;; Remove any negative articles numbers.
9091       (while (and unread (< (car unread) 0))
9092         (setq unread (cdr unread)))
9093       ;; Remove any expired article numbers
9094       (while (and unread (< (car unread) (car active)))
9095         (setq unread (cdr unread)))
9096       ;; Compute the ranges of read articles by looking at the list of
9097       ;; unread articles.
9098       (while unread
9099         (when (/= (car unread) prev)
9100           (push (if (= prev (1- (car unread))) prev
9101                   (cons prev (1- (car unread))))
9102                 read))
9103         (setq prev (1+ (car unread)))
9104         (setq unread (cdr unread)))
9105       (when (<= prev (cdr active))
9106         (push (cons prev (cdr active)) read))
9107       (setq read (if (> (length read) 1) (nreverse read) read))
9108       (if compute
9109           read
9110         (save-excursion
9111           (set-buffer gnus-group-buffer)
9112           (gnus-undo-register
9113             `(progn
9114                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9115                (gnus-info-set-read ',info ',(gnus-info-read info))
9116                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9117                (gnus-group-update-group ,group t))))
9118        ;; Propagate the read marks to the backend.
9119        (if (gnus-check-backend-function 'request-set-mark group)
9120            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9121                  (add (gnus-remove-from-range read (gnus-info-read info))))
9122              (when (or add del)
9123                (unless (gnus-check-group group)
9124                  (error "Can't open server for %s" group))
9125                (gnus-request-set-mark
9126                 group (delq nil (list (if add (list add 'add '(read)))
9127                                       (if del (list del 'del '(read)))))))))
9128         ;; Enter this list into the group info.
9129         (gnus-info-set-read info read)
9130         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9131         (gnus-get-unread-articles-in-group info (gnus-active group))
9132         t))))
9133
9134 (defun gnus-offer-save-summaries ()
9135   "Offer to save all active summary buffers."
9136   (save-excursion
9137     (let ((buflist (buffer-list))
9138           buffers bufname)
9139       ;; Go through all buffers and find all summaries.
9140       (while buflist
9141         (and (setq bufname (buffer-name (car buflist)))
9142              (string-match "Summary" bufname)
9143              (save-excursion
9144                (set-buffer bufname)
9145                ;; We check that this is, indeed, a summary buffer.
9146                (and (eq major-mode 'gnus-summary-mode)
9147                     ;; Also make sure this isn't bogus.
9148                     gnus-newsgroup-prepared
9149                     ;; Also make sure that this isn't a dead summary buffer.
9150                     (not gnus-dead-summary-mode)))
9151              (push bufname buffers))
9152         (setq buflist (cdr buflist)))
9153       ;; Go through all these summary buffers and offer to save them.
9154       (when buffers
9155         (map-y-or-n-p
9156          "Update summary buffer %s? "
9157          (lambda (buf)
9158            (switch-to-buffer buf)
9159            (gnus-summary-exit))
9160          buffers)))))
9161
9162
9163 ;;; @ for mime-partial
9164 ;;;
9165
9166 (defun gnus-request-partial-message ()
9167   (save-excursion
9168     (let ((number (gnus-summary-article-number))
9169           (group gnus-newsgroup-name)
9170           (mother gnus-article-buffer))
9171       (set-buffer (get-buffer-create " *Partial Article*"))
9172       (erase-buffer)
9173       (setq mime-preview-buffer mother)
9174       (gnus-request-article-this-buffer number group)
9175       (mime-parse-buffer)
9176       )))
9177
9178 (autoload 'mime-combine-message/partial-pieces-automatically
9179   "mime-partial"
9180   "Internal method to combine message/partial messages automatically.")
9181
9182 (mime-add-condition
9183  'action '((type . message)(subtype . partial)
9184            (major-mode . gnus-original-article-mode)
9185            (method . mime-combine-message/partial-pieces-automatically)
9186            (summary-buffer-exp . gnus-summary-buffer)
9187            (request-partial-message-method . gnus-request-partial-message)
9188            ))
9189
9190
9191 ;;; @ for message/rfc822
9192 ;;;
9193
9194 (defun gnus-mime-extract-message/rfc822 (entity situation)
9195   (let (group article num cwin swin cur)
9196     (with-current-buffer (mime-entity-buffer entity)
9197       (save-restriction
9198         (narrow-to-region (mime-entity-body-start entity)
9199                           (mime-entity-body-end entity))
9200         (setq group (or (cdr (assq 'group situation))
9201                         (completing-read "Group: "
9202                                          gnus-active-hashtb
9203                                          nil
9204                                          (gnus-read-active-file-p)
9205                                          gnus-newsgroup-name))
9206               article (gnus-request-accept-article group)
9207               )
9208         ))
9209     (when (and (consp article)
9210                (numberp (setq article (cdr article))))
9211       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9212             cwin (get-buffer-window (current-buffer) t)
9213             )
9214       (save-window-excursion
9215         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9216             (select-window swin)
9217           (set-buffer gnus-summary-buffer)
9218           )
9219         (setq cur gnus-current-article)
9220         (forward-line num)
9221         (let (gnus-show-threads)
9222           (gnus-summary-goto-subject article t)
9223           )
9224         (gnus-summary-clear-mark-forward 1)
9225         (gnus-summary-goto-subject cur)
9226         )
9227       (when (and cwin (window-frame cwin))
9228         (select-frame (window-frame cwin))
9229         )
9230       (when (boundp 'mime-acting-situation-to-override)
9231         (set-alist 'mime-acting-situation-to-override
9232                    'group
9233                    group)
9234         (set-alist 'mime-acting-situation-to-override
9235                    'after-method
9236                    `(progn
9237                       (save-current-buffer
9238                         (set-buffer gnus-group-buffer)
9239                         (gnus-activate-group ,group)
9240                         )
9241                       (gnus-summary-goto-article ,cur
9242                                                  gnus-show-all-headers)
9243                       ))
9244         (set-alist 'mime-acting-situation-to-override
9245                    'number num)
9246         )
9247       )))
9248
9249 (mime-add-condition
9250  'action '((type . message)(subtype . rfc822)
9251            (major-mode . gnus-original-article-mode)
9252            (method . gnus-mime-extract-message/rfc822)
9253            (mode . "extract")
9254            ))
9255
9256 (mime-add-condition
9257  'action '((type . message)(subtype . news)
9258            (major-mode . gnus-original-article-mode)
9259            (method . gnus-mime-extract-message/rfc822)
9260            (mode . "extract")
9261            ))
9262
9263 (defun gnus-mime-extract-multipart (entity situation)
9264   (let ((children (mime-entity-children entity))
9265         mime-acting-situation-to-override
9266         f)
9267     (while children
9268       (mime-play-entity (car children)
9269                         (cons (assq 'mode situation)
9270                               mime-acting-situation-to-override))
9271       (setq children (cdr children)))
9272     (if (setq f (cdr (assq 'after-method
9273                            mime-acting-situation-to-override)))
9274         (eval f)
9275       )))  
9276
9277 (mime-add-condition
9278  'action '((type . multipart)
9279            (method . gnus-mime-extract-multipart)
9280            (mode . "extract")
9281            )
9282  'with-default)
9283
9284
9285 ;;; @ end
9286 ;;;
9287
9288 (defun gnus-summary-setup-default-charset ()
9289   "Setup newsgroup default charset."
9290   (let ((name (and gnus-newsgroup-name
9291                    (gnus-group-real-name gnus-newsgroup-name))))
9292     (setq gnus-newsgroup-charset
9293           (or (and gnus-newsgroup-name
9294                    (or (gnus-group-find-parameter gnus-newsgroup-name
9295                                                   'charset)
9296                        (let ((alist gnus-group-charset-alist)
9297                              elem (charset nil))
9298                          (while (setq elem (pop alist))
9299                            (when (and name
9300                                       (string-match (car elem) name))
9301                              (setq alist nil
9302                                    charset (cadr elem))))
9303                          charset)))
9304               gnus-default-charset))))
9305
9306 ;;;
9307 ;;; Mime Commands
9308 ;;;
9309
9310 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9311   "Display the current article buffer fully MIME-buttonized.
9312 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9313 treated as multipart/mixed."
9314   (interactive "P")
9315   (require 'gnus-art)
9316   (let ((gnus-unbuttonized-mime-types nil)
9317         (gnus-mime-display-multipart-as-mixed show-all-parts))
9318     (gnus-summary-show-article)))
9319
9320 (defun gnus-summary-repair-multipart (article)
9321   "Add a Content-Type header to a multipart article without one."
9322   (interactive (list (gnus-summary-article-number)))
9323   (gnus-with-article article
9324     (message-narrow-to-head)
9325     (goto-char (point-max))
9326     (widen)
9327     (when (search-forward "\n--" nil t)
9328       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9329         (message-narrow-to-head)
9330         (message-remove-header "Mime-Version")
9331         (message-remove-header "Content-Type")
9332         (goto-char (point-max))
9333         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9334                         separator))
9335         (insert "Mime-Version: 1.0\n")
9336         (widen))))
9337   (let (gnus-mark-article-hook)
9338     (gnus-summary-select-article t t nil article)))
9339
9340 (defun gnus-summary-toggle-display-buttonized ()
9341   "Toggle the buttonizing of the article buffer."
9342   (interactive)
9343   (require 'gnus-art)
9344   (if (setq gnus-inhibit-mime-unbuttonizing
9345             (not gnus-inhibit-mime-unbuttonizing))
9346       (let ((gnus-unbuttonized-mime-types nil))
9347         (gnus-summary-show-article))
9348     (gnus-summary-show-article)))
9349
9350 ;;;
9351 ;;; with article
9352 ;;;
9353
9354 (defmacro gnus-with-article (article &rest forms)
9355   "Select ARTICLE and perform FORMS in the original article buffer.
9356 Then replace the article with the result."
9357   `(progn
9358      ;; We don't want the article to be marked as read.
9359      (let (gnus-mark-article-hook)
9360        (gnus-summary-select-article t t nil ,article))
9361      (set-buffer gnus-original-article-buffer)
9362      ,@forms
9363      (if (not (gnus-check-backend-function
9364                'request-replace-article (car gnus-article-current)))
9365          (gnus-message 5 "Read-only group; not replacing")
9366        (unless (gnus-request-replace-article
9367                 ,article (car gnus-article-current)
9368                 (current-buffer) t)
9369          (error "Couldn't replace article")))
9370      ;; The cache and backlog have to be flushed somewhat.
9371      (when gnus-keep-backlog
9372        (gnus-backlog-remove-article
9373         (car gnus-article-current) (cdr gnus-article-current)))
9374      (when gnus-use-cache
9375        (gnus-cache-update-article
9376         (car gnus-article-current) (cdr gnus-article-current)))))
9377
9378 (put 'gnus-with-article 'lisp-indent-function 1)
9379 (put 'gnus-with-article 'edebug-form-spec '(form body))
9380
9381 ;;;
9382 ;;; Generic summary marking commands
9383 ;;;
9384
9385 (defvar gnus-summary-marking-alist
9386   '((read gnus-del-mark "d")
9387     (unread gnus-unread-mark "u")
9388     (ticked gnus-ticked-mark "!")
9389     (dormant gnus-dormant-mark "?")
9390     (expirable gnus-expirable-mark "e"))
9391   "An alist of names/marks/keystrokes.")
9392
9393 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9394 (defvar gnus-summary-mark-map)
9395
9396 (defun gnus-summary-make-all-marking-commands ()
9397   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9398   (dolist (elem gnus-summary-marking-alist)
9399     (apply 'gnus-summary-make-marking-command elem)))
9400
9401 (defun gnus-summary-make-marking-command (name mark keystroke)
9402   (let ((map (make-sparse-keymap)))
9403     (define-key gnus-summary-generic-mark-map keystroke map)
9404     (dolist (lway `((next "next" next nil "n")
9405                     (next-unread "next unread" next t "N")
9406                     (prev "previous" prev nil "p")
9407                     (prev-unread "previous unread" prev t "P")
9408                     (nomove "" nil nil ,keystroke)))
9409       (let ((func (gnus-summary-make-marking-command-1
9410                    mark (car lway) lway name)))
9411         (setq func (eval func))
9412         (define-key map (nth 4 lway) func)))))
9413       
9414 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9415   `(defun ,(intern
9416             (format "gnus-summary-put-mark-as-%s%s"
9417                     name (if (eq way 'nomove)
9418                              ""
9419                            (concat "-" (symbol-name way)))))
9420      (n)
9421      ,(format
9422        "Mark the current article as %s%s.
9423 If N, the prefix, then repeat N times.
9424 If N is negative, move in reverse order.
9425 The difference between N and the actual number of articles marked is
9426 returned."
9427        name (cadr lway))
9428      (interactive "p")
9429      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9430     
9431 (defun gnus-summary-generic-mark (n mark move unread)
9432   "Mark N articles with MARK."
9433   (unless (eq major-mode 'gnus-summary-mode)
9434     (error "This command can only be used in the summary buffer"))
9435   (gnus-summary-show-thread)
9436   (let ((nummove
9437          (cond
9438           ((eq move 'next) 1)
9439           ((eq move 'prev) -1)
9440           (t 0))))
9441     (if (zerop nummove)
9442         (setq n 1)
9443       (when (< n 0)
9444         (setq n (abs n)
9445               nummove (* -1 nummove))))
9446     (while (and (> n 0)
9447                 (gnus-summary-mark-article nil mark)
9448                 (zerop (gnus-summary-next-subject nummove unread t)))
9449       (setq n (1- n)))
9450     (when (/= 0 n)
9451       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9452     (gnus-summary-recenter)
9453     (gnus-summary-position-point)
9454     (gnus-set-mode-line 'summary)
9455     n))
9456
9457 (gnus-summary-make-all-marking-commands)
9458
9459 (gnus-ems-redefine)
9460
9461 (provide 'gnus-sum)
9462
9463 (run-hooks 'gnus-sum-load-hook)
9464
9465 ;;; gnus-sum.el ends here