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