gnus.el (gnus-version-number): Update to 6.10.065.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996,97,98,99 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-group)
34 (require 'gnus-spec)
35 (require 'gnus-range)
36 (require 'gnus-int)
37 (require 'gnus-undo)
38 (require 'gnus-util)
39 (require 'mime-view)
40
41 (eval-when-compile
42   (require 'mime-play)
43   ;; Avoid byte-compile warnings.
44   (defvar gnus-article-decoded-p)
45   (defvar gnus-decode-encoded-word-function)
46   )
47
48 (eval-and-compile
49   (autoload 'gnus-cache-articles-in-group "gnus-cache"))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, marking commands will go to the next unread article.
186 If `never', commands that usually go to the next unread article, will
187 go to the next article, whether it is read or not.
188 If nil, only the marking commands will go to the next (un)read article."
189   :group 'gnus-summary-marks
190   :link '(custom-manual "(gnus)Setting Marks")
191   :type '(choice (const :tag "off" nil)
192                  (const never)
193                  (sexp :menu-tag "on" t)))
194
195 (defcustom gnus-summary-default-score 0
196   "*Default article score level.
197 All scores generated by the score files will be added to this score.
198 If this variable is nil, scoring will be disabled."
199   :group 'gnus-score-default
200   :type '(choice (const :tag "disable")
201                  integer))
202
203 (defcustom gnus-summary-zcore-fuzz 0
204   "*Fuzziness factor for the zcore in the summary buffer.
205 Articles with scores closer than this to `gnus-summary-default-score'
206 will not be marked."
207   :group 'gnus-summary-format
208   :type 'integer)
209
210 (defcustom gnus-simplify-subject-fuzzy-regexp nil
211   "*Strings to be removed when doing fuzzy matches.
212 This can either be a regular expression or list of regular expressions
213 that will be removed from subject strings if fuzzy subject
214 simplification is selected."
215   :group 'gnus-thread
216   :type '(repeat regexp))
217
218 (defcustom gnus-show-threads t
219   "*If non-nil, display threads in summary mode."
220   :group 'gnus-thread
221   :type 'boolean)
222
223 (defcustom gnus-thread-hide-subtree nil
224   "*If non-nil, hide all threads initially.
225 If threads are hidden, you have to run the command
226 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
227 to expose hidden threads."
228   :group 'gnus-thread
229   :type 'boolean)
230
231 (defcustom gnus-thread-hide-killed t
232   "*If non-nil, hide killed threads automatically."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-ignore-subject t
237   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
238 If nil, articles that have different subjects from their parents will
239 start separate threads."
240   :group 'gnus-thread
241   :type 'boolean)
242
243 (defcustom gnus-thread-operation-ignore-subject t
244   "*If non-nil, subjects will be ignored when doing thread commands.
245 This affects commands like `gnus-summary-kill-thread' and
246 `gnus-summary-lower-thread'.
247
248 If this variable is nil, articles in the same thread with different
249 subjects will not be included in the operation in question.  If this
250 variable is `fuzzy', only articles that have subjects that are fuzzily
251 equal will be included."
252   :group 'gnus-thread
253   :type '(choice (const :tag "off" nil)
254                  (const fuzzy)
255                  (sexp :tag "on" t)))
256
257 (defcustom gnus-thread-indent-level 4
258   "*Number that says how much each sub-thread should be indented."
259   :group 'gnus-thread
260   :type 'integer)
261
262 (defcustom gnus-auto-extend-newsgroup t
263   "*If non-nil, extend newsgroup forward and backward when requested."
264   :group 'gnus-summary-choose
265   :type 'boolean)
266
267 (defcustom gnus-auto-select-first t
268   "*If nil, don't select the first unread article when entering a group.
269 If this variable is `best', select the highest-scored unread article
270 in the group.  If t, select the first unread article.
271
272 This variable can also be a function to place point on a likely
273 subject line.  Useful values include `gnus-summary-first-unread-subject',
274 `gnus-summary-first-unread-article' and
275 `gnus-summary-best-unread-article'.
276
277 If you want to prevent automatic selection of the first unread article
278 in some newsgroups, set the variable to nil in
279 `gnus-select-group-hook'."
280   :group 'gnus-group-select
281   :type '(choice (const :tag "none" nil)
282                  (const best)
283                  (sexp :menu-tag "first" t)
284                  (function-item gnus-summary-first-unread-subject)
285                  (function-item gnus-summary-first-unread-article)
286                  (function-item gnus-summary-best-unread-article)))
287
288 (defcustom gnus-auto-select-next t
289   "*If non-nil, offer to go to the next group from the end of the previous.
290 If the value is t and the next newsgroup is empty, Gnus will exit
291 summary mode and go back to group mode.  If the value is neither nil
292 nor t, Gnus will select the following unread newsgroup.  In
293 particular, if the value is the symbol `quietly', the next unread
294 newsgroup will be selected without any confirmation, and if it is
295 `almost-quietly', the next group will be selected without any
296 confirmation if you are located on the last article in the group.
297 Finally, if this variable is `slightly-quietly', the `Z n' command
298 will go to the next group without confirmation."
299   :group 'gnus-summary-maneuvering
300   :type '(choice (const :tag "off" nil)
301                  (const quietly)
302                  (const almost-quietly)
303                  (const slightly-quietly)
304                  (sexp :menu-tag "on" t)))
305
306 (defcustom gnus-auto-select-same nil
307   "*If non-nil, select the next article with the same subject.
308 If there are no more articles with the same subject, go to
309 the first unread article."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-summary-check-current nil
314   "*If non-nil, consider the current article when moving.
315 The \"unread\" movement commands will stay on the same line if the
316 current article is unread."
317   :group 'gnus-summary-maneuvering
318   :type 'boolean)
319
320 (defcustom gnus-auto-center-summary t
321   "*If non-nil, always center the current summary buffer.
322 In particular, if `vertical' do only vertical recentering.  If non-nil
323 and non-`vertical', do both horizontal and vertical recentering."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "none" nil)
326                  (const vertical)
327                  (integer :tag "height")
328                  (sexp :menu-tag "both" t)))
329
330 (defcustom gnus-show-all-headers nil
331   "*If non-nil, don't hide any headers."
332   :group 'gnus-article-hiding
333   :group 'gnus-article-headers
334   :type 'boolean)
335
336 (defcustom gnus-summary-ignore-duplicates nil
337   "*If non-nil, ignore articles with identical Message-ID headers."
338   :group 'gnus-summary
339   :type 'boolean)
340
341 (defcustom gnus-single-article-buffer t
342   "*If non-nil, display all articles in the same buffer.
343 If nil, each group will get its own article buffer."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-break-pages t
348   "*If non-nil, do page breaking on articles.
349 The page delimiter is specified by the `gnus-page-delimiter'
350 variable."
351   :group 'gnus-article-various
352   :type 'boolean)
353
354 (defcustom gnus-show-mime t
355   "*If non-nil, do mime processing of articles.
356 The articles will simply be fed to the function given by
357 `gnus-article-display-method-for-mime'."
358   :group 'gnus-article-mime
359   :type 'boolean)
360
361 (defcustom gnus-move-split-methods nil
362   "*Variable used to suggest where articles are to be moved to.
363 It uses the same syntax as the `gnus-split-methods' variable."
364   :group 'gnus-summary-mail
365   :type '(repeat (choice (list :value (fun) function)
366                          (cons :value ("" "") regexp (repeat string))
367                          (sexp :value nil))))
368
369 (defcustom gnus-unread-mark ? ;Whitespace
370   "*Mark used for unread articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-ticked-mark ?!
375   "*Mark used for ticked articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-dormant-mark ??
380   "*Mark used for dormant articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-del-mark ?r
385   "*Mark used for del'd articles."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-read-mark ?R
390   "*Mark used for read articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-expirable-mark ?E
395   "*Mark used for expirable articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-killed-mark ?K
400   "*Mark used for killed articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-souped-mark ?F
405   "*Mark used for killed articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-kill-file-mark ?X
410   "*Mark used for articles killed by kill files."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-low-score-mark ?Y
415   "*Mark used for articles with a low score."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-catchup-mark ?C
420   "*Mark used for articles that are caught up."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-replied-mark ?A
425   "*Mark used for articles that have been replied to."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-cached-mark ?*
430   "*Mark used for articles that are in the cache."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-saved-mark ?S
435   "*Mark used for articles that have been saved to."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-ancient-mark ?O
440   "*Mark used for ancient articles."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-sparse-mark ?Q
445   "*Mark used for sparsely reffed articles."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-canceled-mark ?G
450   "*Mark used for canceled articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-duplicate-mark ?M
455   "*Mark used for duplicate articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-undownloaded-mark ?@
460   "*Mark used for articles that weren't downloaded."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-downloadable-mark ?%
465   "*Mark used for articles that are to be downloaded."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-unsendable-mark ?=
470   "*Mark used for articles that won't be sent."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-score-over-mark ?+
475   "*Score mark used for articles with high scores."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-score-below-mark ?-
480   "*Score mark used for articles with low scores."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-empty-thread-mark ? ;Whitespace
485   "*There is no thread under the article."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-not-empty-thread-mark ?=
490   "*There is a thread under the article."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-view-pseudo-asynchronously nil
495   "*If non-nil, Gnus will view pseudo-articles asynchronously."
496   :group 'gnus-extract-view
497   :type 'boolean)
498
499 (defcustom gnus-auto-expirable-marks
500   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
501         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
502         gnus-souped-mark gnus-duplicate-mark)
503   "*The list of marks converted into expiration if a group is auto-expirable."
504   :group 'gnus-summary
505   :type '(repeat character))
506
507 (defcustom gnus-inhibit-user-auto-expire t
508   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
509   :group 'gnus-summary
510   :type 'boolean)
511
512 (defcustom gnus-view-pseudos nil
513   "*If `automatic', pseudo-articles will be viewed automatically.
514 If `not-confirm', pseudos will be viewed automatically, and the user
515 will not be asked to confirm the command."
516   :group 'gnus-extract-view
517   :type '(choice (const :tag "off" nil)
518                  (const automatic)
519                  (const not-confirm)))
520
521 (defcustom gnus-view-pseudos-separately t
522   "*If non-nil, one pseudo-article will be created for each file to be viewed.
523 If nil, all files that use the same viewing command will be given as a
524 list of parameters to that command."
525   :group 'gnus-extract-view
526   :type 'boolean)
527
528 (defcustom gnus-insert-pseudo-articles t
529   "*If non-nil, insert pseudo-articles when decoding articles."
530   :group 'gnus-extract-view
531   :type 'boolean)
532
533 (defcustom gnus-summary-dummy-line-format
534   "  %(:                          :%) %S\n"
535   "*The format specification for the dummy roots in the summary buffer.
536 It works along the same lines as a normal formatting string,
537 with some simple extensions.
538
539 %S  The subject"
540   :group 'gnus-threading
541   :type 'string)
542
543 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
544   "*The format specification for the summary mode line.
545 It works along the same lines as a normal formatting string,
546 with some simple extensions:
547
548 %G  Group name
549 %p  Unprefixed group name
550 %A  Current article number
551 %z  Current article score
552 %V  Gnus version
553 %U  Number of unread articles in the group
554 %e  Number of unselected articles in the group
555 %Z  A string with unread/unselected article counts
556 %g  Shortish group name
557 %S  Subject of the current article
558 %u  User-defined spec
559 %s  Current score file name
560 %d  Number of dormant articles
561 %r  Number of articles that have been marked as read in this session
562 %E  Number of articles expunged by the score files"
563   :group 'gnus-summary-format
564   :type 'string)
565
566 (defcustom gnus-summary-mark-below 0
567   "*Mark all articles with a score below this variable as read.
568 This variable is local to each summary buffer and usually set by the
569 score file."
570   :group 'gnus-score-default
571   :type 'integer)
572
573 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
574   "*List of functions used for sorting articles in the summary buffer.
575 This variable is only used when not using a threaded display."
576   :group 'gnus-summary-sort
577   :type '(repeat (choice (function-item gnus-article-sort-by-number)
578                          (function-item gnus-article-sort-by-author)
579                          (function-item gnus-article-sort-by-subject)
580                          (function-item gnus-article-sort-by-date)
581                          (function-item gnus-article-sort-by-score)
582                          (function :tag "other"))))
583
584 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
585   "*List of functions used for sorting threads in the summary buffer.
586 By default, threads are sorted by article number.
587
588 Each function takes two threads and return non-nil if the first thread
589 should be sorted before the other.  If you use more than one function,
590 the primary sort function should be the last.  You should probably
591 always include `gnus-thread-sort-by-number' in the list of sorting
592 functions -- preferably first.
593
594 Ready-made functions include `gnus-thread-sort-by-number',
595 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
596 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
597 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
600                          (function-item gnus-thread-sort-by-author)
601                          (function-item gnus-thread-sort-by-subject)
602                          (function-item gnus-thread-sort-by-date)
603                          (function-item gnus-thread-sort-by-score)
604                          (function-item gnus-thread-sort-by-total-score)
605                          (function :tag "other"))))
606
607 (defcustom gnus-thread-score-function '+
608   "*Function used for calculating the total score of a thread.
609
610 The function is called with the scores of the article and each
611 subthread and should then return the score of the thread.
612
613 Some functions you can use are `+', `max', or `min'."
614   :group 'gnus-summary-sort
615   :type 'function)
616
617 (defcustom gnus-summary-expunge-below nil
618   "All articles that have a score less than this variable will be expunged.
619 This variable is local to the summary buffers."
620   :group 'gnus-score-default
621   :type '(choice (const :tag "off" nil)
622                  integer))
623
624 (defcustom gnus-thread-expunge-below nil
625   "All threads that have a total score less than this variable will be expunged.
626 See `gnus-thread-score-function' for en explanation of what a
627 \"thread score\" is.
628
629 This variable is local to the summary buffers."
630   :group 'gnus-threading
631   :group 'gnus-score-default
632   :type '(choice (const :tag "off" nil)
633                  integer))
634
635 (defcustom gnus-summary-mode-hook nil
636   "*A hook for Gnus summary mode.
637 This hook is run before any variables are set in the summary buffer."
638   :group 'gnus-summary-various
639   :type 'hook)
640
641 (defcustom gnus-summary-menu-hook nil
642   "*Hook run after the creation of the summary mode menu."
643   :group 'gnus-summary-visual
644   :type 'hook)
645
646 (defcustom gnus-summary-exit-hook nil
647   "*A hook called on exit from the summary buffer.
648 It will be called with point in the group buffer."
649   :group 'gnus-summary-exit
650   :type 'hook)
651
652 (defcustom gnus-summary-prepare-hook nil
653   "*A hook called after the summary buffer has been generated.
654 If you want to modify the summary buffer, you can use this hook."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-prepared-hook nil
659   "*A hook called as the last thing after the summary buffer has been generated."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-generate-hook nil
664   "*A hook run just before generating the summary buffer.
665 This hook is commonly used to customize threading variables and the
666 like."
667   :group 'gnus-summary-various
668   :type 'hook)
669
670 (defcustom gnus-select-group-hook nil
671   "*A hook called when a newsgroup is selected.
672
673 If you'd like to simplify subjects like the
674 `gnus-summary-next-same-subject' command does, you can use the
675 following hook:
676
677  (setq gnus-select-group-hook
678       (list
679         (lambda ()
680           (mapcar (lambda (header)
681                      (mail-header-set-subject
682                       header
683                       (gnus-simplify-subject
684                        (mail-header-subject header) 're-only)))
685                   gnus-newsgroup-headers))))"
686   :group 'gnus-group-select
687   :type 'hook)
688
689 (defcustom gnus-select-article-hook nil
690   "*A hook called when an article is selected."
691   :group 'gnus-summary-choose
692   :type 'hook)
693
694 (defcustom gnus-visual-mark-article-hook
695   (list 'gnus-highlight-selected-summary)
696   "*Hook run after selecting an article in the summary buffer.
697 It is meant to be used for highlighting the article in some way.  It
698 is not run if `gnus-visual' is nil."
699   :group 'gnus-summary-visual
700   :type 'hook)
701
702 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
703   "*A hook called before parsing the headers."
704   :group 'gnus-various
705   :type 'hook)
706
707 (defcustom gnus-exit-group-hook nil
708   "*A hook called when exiting (not quitting) summary mode."
709   :group 'gnus-various
710   :type 'hook)
711
712 (defcustom gnus-summary-update-hook
713   (list 'gnus-summary-highlight-line)
714   "*A hook called when a summary line is changed.
715 The hook will not be called if `gnus-visual' is nil.
716
717 The default function `gnus-summary-highlight-line' will
718 highlight the line according to the `gnus-summary-highlight'
719 variable."
720   :group 'gnus-summary-visual
721   :type 'hook)
722
723 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
724   "*A hook called when an article is selected for the first time.
725 The hook is intended to mark an article as read (or unread)
726 automatically when it is selected."
727   :group 'gnus-summary-choose
728   :type 'hook)
729
730 (defcustom gnus-group-no-more-groups-hook nil
731   "*A hook run when returning to group mode having no more (unread) groups."
732   :group 'gnus-group-select
733   :type 'hook)
734
735 (defcustom gnus-ps-print-hook nil
736   "*A hook run before ps-printing something from Gnus."
737   :group 'gnus-summary
738   :type 'hook)
739
740 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
741   "Face used for highlighting the current article in the summary buffer."
742   :group 'gnus-summary-visual
743   :type 'face)
744
745 (defcustom gnus-summary-highlight
746   '(((= mark gnus-canceled-mark)
747      . gnus-summary-cancelled-face)
748     ((and (> score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-high-ticked-face)
752     ((and (< score default)
753           (or (= mark gnus-dormant-mark)
754               (= mark gnus-ticked-mark)))
755      . gnus-summary-low-ticked-face)
756     ((or (= mark gnus-dormant-mark)
757          (= mark gnus-ticked-mark))
758      . gnus-summary-normal-ticked-face)
759     ((and (> score default) (= mark gnus-ancient-mark))
760      . gnus-summary-high-ancient-face)
761     ((and (< score default) (= mark gnus-ancient-mark))
762      . gnus-summary-low-ancient-face)
763     ((= mark gnus-ancient-mark)
764      . gnus-summary-normal-ancient-face)
765     ((and (> score default) (= mark gnus-unread-mark))
766      . gnus-summary-high-unread-face)
767     ((and (< score default) (= mark gnus-unread-mark))
768      . gnus-summary-low-unread-face)
769     ((= mark gnus-unread-mark)
770      . gnus-summary-normal-unread-face)
771     ((and (> score default) (memq mark (list gnus-downloadable-mark
772                                              gnus-undownloaded-mark)))
773      . gnus-summary-high-unread-face)
774     ((and (< score default) (memq mark (list gnus-downloadable-mark
775                                              gnus-undownloaded-mark)))
776      . gnus-summary-low-unread-face)
777     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
778      . gnus-summary-normal-unread-face)
779     ((> score default)
780      . gnus-summary-high-read-face)
781     ((< score default)
782      . gnus-summary-low-read-face)
783     (t
784      . gnus-summary-normal-read-face))
785   "*Controls the highlighting of summary buffer lines.
786
787 A list of (FORM . FACE) pairs.  When deciding how a a particular
788 summary line should be displayed, each form is evaluated.  The content
789 of the face field after the first true form is used.  You can change
790 how those summary lines are displayed, by editing the face field.
791
792 You can use the following variables in the FORM field.
793
794 score:   The articles score
795 default: The default article score.
796 below:   The score below which articles are automatically marked as read.
797 mark:    The articles mark."
798   :group 'gnus-summary-visual
799   :type '(repeat (cons (sexp :tag "Form" nil)
800                        face)))
801
802 (defcustom gnus-alter-header-function nil
803   "Function called to allow alteration of article header structures.
804 The function is called with one parameter, the article header vector,
805 which it may alter in any way.")
806
807 (defcustom gnus-extra-headers nil
808   "*Extra headers to parse."
809   :group 'gnus-summary
810   :type '(repeat symbol))
811
812 (defcustom gnus-ignored-from-addresses
813   (and user-mail-address (regexp-quote user-mail-address))
814   "*Regexp of From headers that may be suppressed in favor of To headers."
815   :group 'gnus-summary
816   :type 'regexp)
817
818 (defcustom gnus-group-charset-alist
819   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
820     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
821     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
822     ("^relcom\\>" koi8-r)
823     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
824     ("^israel\\>" iso-8859-1)
825     ("^han\\>" euc-kr)
826     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
827     (".*" iso-8859-1))
828   "Alist of regexps (to match group names) and default charsets to be used when reading."
829   :type '(repeat (list (regexp :tag "Group")
830                        (symbol :tag "Charset")))
831   :group 'gnus-charset)
832
833 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit)
834   "List of charsets that should be ignored.
835 When these charsets are used in the \"charset\" parameter, the
836 default charset will be used instead."
837   :type '(repeat symbol)
838   :group 'gnus-charset)
839
840 (defcustom gnus-group-highlight-words-alist nil
841   "Alist of group regexps and highlight regexps.
842 This variable uses the same syntax as `gnus-emphasis-alist'."
843   :type '(repeat (cons (regexp :tag "Group")
844                        (repeat (list (regexp :tag "Highlight regexp")
845                                      (number :tag "Group for entire word" 0)
846                                      (number :tag "Group for displayed part" 0)
847                                      (symbol :tag "Face" 
848                                              gnus-emphasis-highlight-words)))))
849   :group 'gnus-summary-visual)
850
851 ;;; Internal variables
852
853 (defvar gnus-scores-exclude-files nil)
854 (defvar gnus-page-broken nil)
855 (defvar gnus-inhibit-mime-unbuttonizing nil)
856
857 (defvar gnus-original-article nil)
858 (defvar gnus-article-internal-prepare-hook nil)
859 (defvar gnus-newsgroup-process-stack nil)
860
861 (defvar gnus-thread-indent-array nil)
862 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
863
864 ;; Avoid highlighting in kill files.
865 (defvar gnus-summary-inhibit-highlight nil)
866 (defvar gnus-newsgroup-selected-overlay nil)
867 (defvar gnus-inhibit-limiting nil)
868 (defvar gnus-newsgroup-adaptive-score-file nil)
869 (defvar gnus-current-score-file nil)
870 (defvar gnus-current-move-group nil)
871 (defvar gnus-current-copy-group nil)
872 (defvar gnus-current-crosspost-group nil)
873
874 (defvar gnus-newsgroup-dependencies nil)
875 (defvar gnus-newsgroup-adaptive nil)
876 (defvar gnus-summary-display-article-function nil)
877 (defvar gnus-summary-highlight-line-function nil
878   "Function called after highlighting a summary line.")
879
880 (defvar gnus-summary-line-format-alist
881   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
882     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
883     (?s gnus-tmp-subject-or-nil ?s)
884     (?n gnus-tmp-name ?s)
885     (?A (std11-address-string
886          (car (mime-read-field 'From gnus-tmp-header))) ?s)
887     (?a (or (std11-full-name-string
888              (car (mime-read-field 'From gnus-tmp-header)))
889             gnus-tmp-from) ?s)
890     (?F gnus-tmp-from ?s)
891     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
892     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
893     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
894     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
895     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
896     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
897     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
898     (?L gnus-tmp-lines ?d)
899     (?I gnus-tmp-indentation ?s)
900     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
901     (?R gnus-tmp-replied ?c)
902     (?\[ gnus-tmp-opening-bracket ?c)
903     (?\] gnus-tmp-closing-bracket ?c)
904     (?\> (make-string gnus-tmp-level ? ) ?s)
905     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
906     (?i gnus-tmp-score ?d)
907     (?z gnus-tmp-score-char ?c)
908     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
909     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
910     (?U gnus-tmp-unread ?c)
911     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
912     (?t (gnus-summary-number-of-articles-in-thread
913          (and (boundp 'thread) (car thread)) gnus-tmp-level)
914         ?d)
915     (?e (gnus-summary-number-of-articles-in-thread
916          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
917         ?c)
918     (?u gnus-tmp-user-defined ?s)
919     (?P (gnus-pick-line-number) ?d))
920   "An alist of format specifications that can appear in summary lines,
921 and what variables they correspond with, along with the type of the
922 variable (string, integer, character, etc).")
923
924 (defvar gnus-summary-dummy-line-format-alist
925   `((?S gnus-tmp-subject ?s)
926     (?N gnus-tmp-number ?d)
927     (?u gnus-tmp-user-defined ?s)))
928
929 (defvar gnus-summary-mode-line-format-alist
930   `((?G gnus-tmp-group-name ?s)
931     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
932     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
933     (?A gnus-tmp-article-number ?d)
934     (?Z gnus-tmp-unread-and-unselected ?s)
935     (?V gnus-version ?s)
936     (?U gnus-tmp-unread-and-unticked ?d)
937     (?S gnus-tmp-subject ?s)
938     (?e gnus-tmp-unselected ?d)
939     (?u gnus-tmp-user-defined ?s)
940     (?d (length gnus-newsgroup-dormant) ?d)
941     (?t (length gnus-newsgroup-marked) ?d)
942     (?r (length gnus-newsgroup-reads) ?d)
943     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
944     (?E gnus-newsgroup-expunged-tally ?d)
945     (?s (gnus-current-score-file-nondirectory) ?s)))
946
947 (defvar gnus-last-search-regexp nil
948   "Default regexp for article search command.")
949
950 (defvar gnus-last-shell-command nil
951   "Default shell command on article.")
952
953 (defvar gnus-newsgroup-begin nil)
954 (defvar gnus-newsgroup-end nil)
955 (defvar gnus-newsgroup-last-rmail nil)
956 (defvar gnus-newsgroup-last-mail nil)
957 (defvar gnus-newsgroup-last-folder nil)
958 (defvar gnus-newsgroup-last-file nil)
959 (defvar gnus-newsgroup-auto-expire nil)
960 (defvar gnus-newsgroup-active nil)
961
962 (defvar gnus-newsgroup-data nil)
963 (defvar gnus-newsgroup-data-reverse nil)
964 (defvar gnus-newsgroup-limit nil)
965 (defvar gnus-newsgroup-limits nil)
966
967 (defvar gnus-newsgroup-unreads nil
968   "List of unread articles in the current newsgroup.")
969
970 (defvar gnus-newsgroup-unselected nil
971   "List of unselected unread articles in the current newsgroup.")
972
973 (defvar gnus-newsgroup-reads nil
974   "Alist of read articles and article marks in the current newsgroup.")
975
976 (defvar gnus-newsgroup-expunged-tally nil)
977
978 (defvar gnus-newsgroup-marked nil
979   "List of ticked articles in the current newsgroup (a subset of unread art).")
980
981 (defvar gnus-newsgroup-killed nil
982   "List of ranges of articles that have been through the scoring process.")
983
984 (defvar gnus-newsgroup-cached nil
985   "List of articles that come from the article cache.")
986
987 (defvar gnus-newsgroup-saved nil
988   "List of articles that have been saved.")
989
990 (defvar gnus-newsgroup-kill-headers nil)
991
992 (defvar gnus-newsgroup-replied nil
993   "List of articles that have been replied to in the current newsgroup.")
994
995 (defvar gnus-newsgroup-expirable nil
996   "List of articles in the current newsgroup that can be expired.")
997
998 (defvar gnus-newsgroup-processable nil
999   "List of articles in the current newsgroup that can be processed.")
1000
1001 (defvar gnus-newsgroup-downloadable nil
1002   "List of articles in the current newsgroup that can be processed.")
1003
1004 (defvar gnus-newsgroup-undownloaded nil
1005   "List of articles in the current newsgroup that haven't been downloaded..")
1006
1007 (defvar gnus-newsgroup-unsendable nil
1008   "List of articles in the current newsgroup that won't be sent.")
1009
1010 (defvar gnus-newsgroup-bookmarks nil
1011   "List of articles in the current newsgroup that have bookmarks.")
1012
1013 (defvar gnus-newsgroup-dormant nil
1014   "List of dormant articles in the current newsgroup.")
1015
1016 (defvar gnus-newsgroup-scored nil
1017   "List of scored articles in the current newsgroup.")
1018
1019 (defvar gnus-newsgroup-headers nil
1020   "List of article headers in the current newsgroup.")
1021
1022 (defvar gnus-newsgroup-threads nil)
1023
1024 (defvar gnus-newsgroup-prepared nil
1025   "Whether the current group has been prepared properly.")
1026
1027 (defvar gnus-newsgroup-ancient nil
1028   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-sparse nil)
1031
1032 (defvar gnus-current-article nil)
1033 (defvar gnus-article-current nil)
1034 (defvar gnus-current-headers nil)
1035 (defvar gnus-have-all-headers nil)
1036 (defvar gnus-last-article nil)
1037 (defvar gnus-newsgroup-history nil)
1038 (defvar gnus-newsgroup-charset nil)
1039 (defvar gnus-newsgroup-emphasis-alist nil)
1040
1041 (defconst gnus-summary-local-variables
1042   '(gnus-newsgroup-name
1043     gnus-newsgroup-begin gnus-newsgroup-end
1044     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1045     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1046     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1047     gnus-newsgroup-unselected gnus-newsgroup-marked
1048     gnus-newsgroup-reads gnus-newsgroup-saved
1049     gnus-newsgroup-replied gnus-newsgroup-expirable
1050     gnus-newsgroup-processable gnus-newsgroup-killed
1051     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1052     gnus-newsgroup-unsendable
1053     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1054     gnus-newsgroup-headers gnus-newsgroup-threads
1055     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1056     gnus-current-article gnus-current-headers gnus-have-all-headers
1057     gnus-last-article gnus-article-internal-prepare-hook
1058     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1059     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1060     gnus-thread-expunge-below
1061     gnus-score-alist gnus-current-score-file
1062     (gnus-summary-expunge-below . global)
1063     (gnus-summary-mark-below . global)
1064     gnus-newsgroup-active gnus-scores-exclude-files
1065     gnus-newsgroup-history gnus-newsgroup-ancient
1066     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1067     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1068     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1069     (gnus-newsgroup-expunged-tally . 0)
1070     gnus-cache-removable-articles gnus-newsgroup-cached
1071     gnus-newsgroup-data gnus-newsgroup-data-reverse
1072     gnus-newsgroup-limit gnus-newsgroup-limits
1073     gnus-newsgroup-charset gnus-newsgroup-emphasis-alist)
1074   "Variables that are buffer-local to the summary buffers.")
1075
1076 ;; Byte-compiler warning.
1077 (defvar gnus-article-mode-map)
1078
1079 ;; Subject simplification.
1080
1081 (defun gnus-simplify-whitespace (str)
1082   "Remove excessive whitespace."
1083   (let ((mystr str))
1084     ;; Multiple spaces.
1085     (while (string-match "[ \t][ \t]+" mystr)
1086       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1087                           " "
1088                           (substring mystr (match-end 0)))))
1089     ;; Leading spaces.
1090     (when (string-match "^[ \t]+" mystr)
1091       (setq mystr (substring mystr (match-end 0))))
1092     ;; Trailing spaces.
1093     (when (string-match "[ \t]+$" mystr)
1094       (setq mystr (substring mystr 0 (match-beginning 0))))
1095     mystr))
1096
1097 (defsubst gnus-simplify-subject-re (subject)
1098   "Remove \"Re:\" from subject lines."
1099   (if (string-match "^[Rr][Ee]: *" subject)
1100       (substring subject (match-end 0))
1101     subject))
1102
1103 (defun gnus-simplify-subject (subject &optional re-only)
1104   "Remove `Re:' and words in parentheses.
1105 If RE-ONLY is non-nil, strip leading `Re:'s only."
1106   (let ((case-fold-search t))           ;Ignore case.
1107     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1108     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1109       (setq subject (substring subject (match-end 0))))
1110     ;; Remove uninteresting prefixes.
1111     (when (and (not re-only)
1112                gnus-simplify-ignored-prefixes
1113                (string-match gnus-simplify-ignored-prefixes subject))
1114       (setq subject (substring subject (match-end 0))))
1115     ;; Remove words in parentheses from end.
1116     (unless re-only
1117       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1118         (setq subject (substring subject 0 (match-beginning 0)))))
1119     ;; Return subject string.
1120     subject))
1121
1122 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1123 ;; all whitespace.
1124 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1125   (goto-char (point-min))
1126   (while (re-search-forward regexp nil t)
1127       (replace-match (or newtext ""))))
1128
1129 (defun gnus-simplify-buffer-fuzzy ()
1130   "Simplify string in the buffer fuzzily.
1131 The string in the accessible portion of the current buffer is simplified.
1132 It is assumed to be a single-line subject.
1133 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1134 matter is removed.  Additional things can be deleted by setting
1135 gnus-simplify-subject-fuzzy-regexp."
1136   (let ((case-fold-search t)
1137         (modified-tick))
1138     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1139
1140     (while (not (eq modified-tick (buffer-modified-tick)))
1141       (setq modified-tick (buffer-modified-tick))
1142       (cond
1143        ((listp gnus-simplify-subject-fuzzy-regexp)
1144         (mapcar 'gnus-simplify-buffer-fuzzy-step
1145                 gnus-simplify-subject-fuzzy-regexp))
1146        (gnus-simplify-subject-fuzzy-regexp
1147         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1148       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1149       (gnus-simplify-buffer-fuzzy-step
1150        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1151       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1152
1153     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1154     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1155     (gnus-simplify-buffer-fuzzy-step " $")
1156     (gnus-simplify-buffer-fuzzy-step "^ +")))
1157
1158 (defun gnus-simplify-subject-fuzzy (subject)
1159   "Simplify a subject string fuzzily.
1160 See `gnus-simplify-buffer-fuzzy' for details."
1161   (save-excursion
1162     (gnus-set-work-buffer)
1163     (let ((case-fold-search t))
1164       ;; Remove uninteresting prefixes.
1165       (when (and gnus-simplify-ignored-prefixes
1166                  (string-match gnus-simplify-ignored-prefixes subject))
1167         (setq subject (substring subject (match-end 0))))
1168       (insert subject)
1169       (inline (gnus-simplify-buffer-fuzzy))
1170       (buffer-string))))
1171
1172 (defsubst gnus-simplify-subject-fully (subject)
1173   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1174   (cond
1175    (gnus-simplify-subject-functions
1176     (gnus-map-function gnus-simplify-subject-functions subject))
1177    ((null gnus-summary-gather-subject-limit)
1178     (gnus-simplify-subject-re subject))
1179    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1180     (gnus-simplify-subject-fuzzy subject))
1181    ((numberp gnus-summary-gather-subject-limit)
1182     (gnus-limit-string (gnus-simplify-subject-re subject)
1183                        gnus-summary-gather-subject-limit))
1184    (t
1185     subject)))
1186
1187 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1188   "Check whether two subjects are equal.
1189 If optional argument simple-first is t, first argument is already
1190 simplified."
1191   (cond
1192    ((null simple-first)
1193     (equal (gnus-simplify-subject-fully s1)
1194            (gnus-simplify-subject-fully s2)))
1195    (t
1196     (equal s1
1197            (gnus-simplify-subject-fully s2)))))
1198
1199 (defun gnus-summary-bubble-group ()
1200   "Increase the score of the current group.
1201 This is a handy function to add to `gnus-summary-exit-hook' to
1202 increase the score of each group you read."
1203   (gnus-group-add-score gnus-newsgroup-name))
1204
1205 \f
1206 ;;;
1207 ;;; Gnus summary mode
1208 ;;;
1209
1210 (put 'gnus-summary-mode 'mode-class 'special)
1211
1212 (when t
1213   ;; Non-orthogonal keys
1214
1215   (gnus-define-keys gnus-summary-mode-map
1216     " " gnus-summary-next-page
1217     "\177" gnus-summary-prev-page
1218     [delete] gnus-summary-prev-page
1219     [backspace] gnus-summary-prev-page
1220     "\r" gnus-summary-scroll-up
1221     "\M-\r" gnus-summary-scroll-down
1222     "n" gnus-summary-next-unread-article
1223     "p" gnus-summary-prev-unread-article
1224     "N" gnus-summary-next-article
1225     "P" gnus-summary-prev-article
1226     "\M-\C-n" gnus-summary-next-same-subject
1227     "\M-\C-p" gnus-summary-prev-same-subject
1228     "\M-n" gnus-summary-next-unread-subject
1229     "\M-p" gnus-summary-prev-unread-subject
1230     "." gnus-summary-first-unread-article
1231     "," gnus-summary-best-unread-article
1232     "\M-s" gnus-summary-search-article-forward
1233     "\M-r" gnus-summary-search-article-backward
1234     "<" gnus-summary-beginning-of-article
1235     ">" gnus-summary-end-of-article
1236     "j" gnus-summary-goto-article
1237     "^" gnus-summary-refer-parent-article
1238     "\M-^" gnus-summary-refer-article
1239     "u" gnus-summary-tick-article-forward
1240     "!" gnus-summary-tick-article-forward
1241     "U" gnus-summary-tick-article-backward
1242     "d" gnus-summary-mark-as-read-forward
1243     "D" gnus-summary-mark-as-read-backward
1244     "E" gnus-summary-mark-as-expirable
1245     "\M-u" gnus-summary-clear-mark-forward
1246     "\M-U" gnus-summary-clear-mark-backward
1247     "k" gnus-summary-kill-same-subject-and-select
1248     "\C-k" gnus-summary-kill-same-subject
1249     "\M-\C-k" gnus-summary-kill-thread
1250     "\M-\C-l" gnus-summary-lower-thread
1251     "e" gnus-summary-edit-article
1252     "#" gnus-summary-mark-as-processable
1253     "\M-#" gnus-summary-unmark-as-processable
1254     "\M-\C-t" gnus-summary-toggle-threads
1255     "\M-\C-s" gnus-summary-show-thread
1256     "\M-\C-h" gnus-summary-hide-thread
1257     "\M-\C-f" gnus-summary-next-thread
1258     "\M-\C-b" gnus-summary-prev-thread
1259     "\M-\C-u" gnus-summary-up-thread
1260     "\M-\C-d" gnus-summary-down-thread
1261     "&" gnus-summary-execute-command
1262     "c" gnus-summary-catchup-and-exit
1263     "\C-w" gnus-summary-mark-region-as-read
1264     "\C-t" gnus-summary-toggle-truncation
1265     "?" gnus-summary-mark-as-dormant
1266     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1267     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1268     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1269     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1270     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1271     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1272     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1273     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1274     "=" gnus-summary-expand-window
1275     "\C-x\C-s" gnus-summary-reselect-current-group
1276     "\M-g" gnus-summary-rescan-group
1277     "w" gnus-summary-stop-page-breaking
1278     "\C-c\C-r" gnus-summary-caesar-message
1279     "\M-t" gnus-summary-toggle-mime
1280     "f" gnus-summary-followup
1281     "F" gnus-summary-followup-with-original
1282     "C" gnus-summary-cancel-article
1283     "r" gnus-summary-reply
1284     "R" gnus-summary-reply-with-original
1285     "\C-c\C-f" gnus-summary-mail-forward
1286     "o" gnus-summary-save-article
1287     "\C-o" gnus-summary-save-article-mail
1288     "|" gnus-summary-pipe-output
1289     "\M-k" gnus-summary-edit-local-kill
1290     "\M-K" gnus-summary-edit-global-kill
1291     ;; "V" gnus-version
1292     "\C-c\C-d" gnus-summary-describe-group
1293     "q" gnus-summary-exit
1294     "Q" gnus-summary-exit-no-update
1295     "\C-c\C-i" gnus-info-find-node
1296     gnus-mouse-2 gnus-mouse-pick-article
1297     "m" gnus-summary-mail-other-window
1298     "a" gnus-summary-post-news
1299     "x" gnus-summary-limit-to-unread
1300     "s" gnus-summary-isearch-article
1301     "t" gnus-article-toggle-headers
1302     "g" gnus-summary-show-article
1303     "l" gnus-summary-goto-last-article
1304     "v" gnus-summary-preview-mime-message
1305     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1306     "\C-d" gnus-summary-enter-digest-group
1307     "\M-\C-d" gnus-summary-read-document
1308     "\M-\C-e" gnus-summary-edit-parameters
1309     "\M-\C-g" gnus-summary-customize-parameters
1310     "\C-c\C-b" gnus-bug
1311     "*" gnus-cache-enter-article
1312     "\M-*" gnus-cache-remove-article
1313     "\M-&" gnus-summary-universal-argument
1314     "\C-l" gnus-recenter
1315     "I" gnus-summary-increase-score
1316     "L" gnus-summary-lower-score
1317     "\M-i" gnus-symbolic-argument
1318     "h" gnus-summary-select-article-buffer
1319
1320     "V" gnus-summary-score-map
1321     "X" gnus-uu-extract-map
1322     "S" gnus-summary-send-map)
1323
1324   ;; Sort of orthogonal keymap
1325   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1326     "t" gnus-summary-tick-article-forward
1327     "!" gnus-summary-tick-article-forward
1328     "d" gnus-summary-mark-as-read-forward
1329     "r" gnus-summary-mark-as-read-forward
1330     "c" gnus-summary-clear-mark-forward
1331     " " gnus-summary-clear-mark-forward
1332     "e" gnus-summary-mark-as-expirable
1333     "x" gnus-summary-mark-as-expirable
1334     "?" gnus-summary-mark-as-dormant
1335     "b" gnus-summary-set-bookmark
1336     "B" gnus-summary-remove-bookmark
1337     "#" gnus-summary-mark-as-processable
1338     "\M-#" gnus-summary-unmark-as-processable
1339     "S" gnus-summary-limit-include-expunged
1340     "C" gnus-summary-catchup
1341     "H" gnus-summary-catchup-to-here
1342     "\C-c" gnus-summary-catchup-all
1343     "k" gnus-summary-kill-same-subject-and-select
1344     "K" gnus-summary-kill-same-subject
1345     "P" gnus-uu-mark-map)
1346
1347   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1348     "c" gnus-summary-clear-above
1349     "u" gnus-summary-tick-above
1350     "m" gnus-summary-mark-above
1351     "k" gnus-summary-kill-below)
1352
1353   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1354     "/" gnus-summary-limit-to-subject
1355     "n" gnus-summary-limit-to-articles
1356     "w" gnus-summary-pop-limit
1357     "s" gnus-summary-limit-to-subject
1358     "a" gnus-summary-limit-to-author
1359     "u" gnus-summary-limit-to-unread
1360     "m" gnus-summary-limit-to-marks
1361     "M" gnus-summary-limit-exclude-marks
1362     "v" gnus-summary-limit-to-score
1363     "*" gnus-summary-limit-include-cached
1364     "D" gnus-summary-limit-include-dormant
1365     "T" gnus-summary-limit-include-thread
1366     "d" gnus-summary-limit-exclude-dormant
1367     "t" gnus-summary-limit-to-age
1368     "x" gnus-summary-limit-to-extra 
1369     "E" gnus-summary-limit-include-expunged
1370     "c" gnus-summary-limit-exclude-childless-dormant
1371     "C" gnus-summary-limit-mark-excluded-as-read)
1372
1373   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1374     "n" gnus-summary-next-unread-article
1375     "p" gnus-summary-prev-unread-article
1376     "N" gnus-summary-next-article
1377     "P" gnus-summary-prev-article
1378     "\C-n" gnus-summary-next-same-subject
1379     "\C-p" gnus-summary-prev-same-subject
1380     "\M-n" gnus-summary-next-unread-subject
1381     "\M-p" gnus-summary-prev-unread-subject
1382     "f" gnus-summary-first-unread-article
1383     "b" gnus-summary-best-unread-article
1384     "j" gnus-summary-goto-article
1385     "g" gnus-summary-goto-subject
1386     "l" gnus-summary-goto-last-article
1387     "o" gnus-summary-pop-article)
1388
1389   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1390     "k" gnus-summary-kill-thread
1391     "l" gnus-summary-lower-thread
1392     "i" gnus-summary-raise-thread
1393     "T" gnus-summary-toggle-threads
1394     "t" gnus-summary-rethread-current
1395     "^" gnus-summary-reparent-thread
1396     "s" gnus-summary-show-thread
1397     "S" gnus-summary-show-all-threads
1398     "h" gnus-summary-hide-thread
1399     "H" gnus-summary-hide-all-threads
1400     "n" gnus-summary-next-thread
1401     "p" gnus-summary-prev-thread
1402     "u" gnus-summary-up-thread
1403     "o" gnus-summary-top-thread
1404     "d" gnus-summary-down-thread
1405     "#" gnus-uu-mark-thread
1406     "\M-#" gnus-uu-unmark-thread)
1407
1408   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1409     "g" gnus-summary-prepare
1410     "c" gnus-summary-insert-cached-articles)
1411
1412   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1413     "c" gnus-summary-catchup-and-exit
1414     "C" gnus-summary-catchup-all-and-exit
1415     "E" gnus-summary-exit-no-update
1416     "Q" gnus-summary-exit
1417     "Z" gnus-summary-exit
1418     "n" gnus-summary-catchup-and-goto-next-group
1419     "R" gnus-summary-reselect-current-group
1420     "G" gnus-summary-rescan-group
1421     "N" gnus-summary-next-group
1422     "s" gnus-summary-save-newsrc
1423     "P" gnus-summary-prev-group)
1424
1425   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1426     " " gnus-summary-next-page
1427     "n" gnus-summary-next-page
1428     "\177" gnus-summary-prev-page
1429     [delete] gnus-summary-prev-page
1430     "p" gnus-summary-prev-page
1431     "\r" gnus-summary-scroll-up
1432     "\M-\r" gnus-summary-scroll-down
1433     "<" gnus-summary-beginning-of-article
1434     ">" gnus-summary-end-of-article
1435     "b" gnus-summary-beginning-of-article
1436     "e" gnus-summary-end-of-article
1437     "^" gnus-summary-refer-parent-article
1438     "r" gnus-summary-refer-parent-article
1439     "D" gnus-summary-enter-digest-group
1440     "R" gnus-summary-refer-references
1441     "T" gnus-summary-refer-thread
1442     "g" gnus-summary-show-article
1443     "s" gnus-summary-isearch-article
1444     "P" gnus-summary-print-article
1445     "t" gnus-article-babel)
1446
1447   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1448     "b" gnus-article-add-buttons
1449     "B" gnus-article-add-buttons-to-head
1450     "o" gnus-article-treat-overstrike
1451     "e" gnus-article-emphasize
1452     "w" gnus-article-fill-cited-article
1453     "Q" gnus-article-fill-long-lines
1454     "C" gnus-article-capitalize-sentences
1455     "c" gnus-article-remove-cr
1456     "f" gnus-article-display-x-face
1457     "l" gnus-summary-stop-page-breaking
1458     "r" gnus-summary-caesar-message
1459     "t" gnus-article-toggle-headers
1460     "v" gnus-summary-verbose-headers
1461     "m" gnus-summary-toggle-mime
1462     "h" gnus-article-treat-html
1463     "H" gnus-article-strip-headers-in-body
1464     "d" gnus-article-treat-dumbquotes)
1465
1466   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1467     "a" gnus-article-hide
1468     "h" gnus-article-toggle-headers
1469     "b" gnus-article-hide-boring-headers
1470     "s" gnus-article-hide-signature
1471     "c" gnus-article-hide-citation
1472     "C" gnus-article-hide-citation-in-followups
1473     "p" gnus-article-hide-pgp
1474     "B" gnus-article-strip-banner
1475     "P" gnus-article-hide-pem
1476     "\C-c" gnus-article-hide-citation-maybe)
1477
1478   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1479     "a" gnus-article-highlight
1480     "h" gnus-article-highlight-headers
1481     "c" gnus-article-highlight-citation
1482     "s" gnus-article-highlight-signature)
1483
1484   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1485     "z" gnus-article-date-ut
1486     "u" gnus-article-date-ut
1487     "l" gnus-article-date-local
1488     "e" gnus-article-date-lapsed
1489     "o" gnus-article-date-original
1490     "i" gnus-article-date-iso8601
1491     "s" gnus-article-date-user)
1492
1493   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1494     "t" gnus-article-remove-trailing-blank-lines
1495     "l" gnus-article-strip-leading-blank-lines
1496     "m" gnus-article-strip-multiple-blank-lines
1497     "a" gnus-article-strip-blank-lines
1498     "A" gnus-article-strip-all-blank-lines
1499     "s" gnus-article-strip-leading-space
1500     "e" gnus-article-strip-trailing-space)
1501
1502   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1503     "v" gnus-version
1504     "f" gnus-summary-fetch-faq
1505     "d" gnus-summary-describe-group
1506     "h" gnus-summary-describe-briefly
1507     "i" gnus-info-find-node)
1508
1509   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1510     "e" gnus-summary-expire-articles
1511     "\M-\C-e" gnus-summary-expire-articles-now
1512     "\177" gnus-summary-delete-article
1513     [delete] gnus-summary-delete-article
1514     "m" gnus-summary-move-article
1515     "r" gnus-summary-respool-article
1516     "w" gnus-summary-edit-article
1517     "c" gnus-summary-copy-article
1518     "B" gnus-summary-crosspost-article
1519     "q" gnus-summary-respool-query
1520     "t" gnus-summary-respool-trace
1521     "i" gnus-summary-import-article
1522     "p" gnus-summary-article-posted-p)
1523
1524   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1525     "o" gnus-summary-save-article
1526     "m" gnus-summary-save-article-mail
1527     "F" gnus-summary-write-article-file
1528     "r" gnus-summary-save-article-rmail
1529     "f" gnus-summary-save-article-file
1530     "b" gnus-summary-save-article-body-file
1531     "h" gnus-summary-save-article-folder
1532     "v" gnus-summary-save-article-vm
1533     "p" gnus-summary-pipe-output
1534     "s" gnus-soup-add-article)
1535
1536   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1537     "b" gnus-summary-display-buttonized
1538     "m" gnus-summary-repair-multipart
1539     "v" gnus-article-view-part
1540     "o" gnus-article-save-part
1541     "c" gnus-article-copy-part
1542     "e" gnus-article-externalize-part
1543     "i" gnus-article-inline-part
1544     "|" gnus-article-pipe-part)
1545   )
1546
1547 (defun gnus-summary-make-menu-bar ()
1548   (gnus-turn-off-edit-menu 'summary)
1549
1550   (unless (boundp 'gnus-summary-misc-menu)
1551
1552     (easy-menu-define
1553      gnus-summary-kill-menu gnus-summary-mode-map ""
1554      (cons
1555       "Score"
1556       (nconc
1557        (list
1558         ["Enter score..." gnus-summary-score-entry t]
1559         ["Customize" gnus-score-customize t])
1560        (gnus-make-score-map 'increase)
1561        (gnus-make-score-map 'lower)
1562        '(("Mark"
1563           ["Kill below" gnus-summary-kill-below t]
1564           ["Mark above" gnus-summary-mark-above t]
1565           ["Tick above" gnus-summary-tick-above t]
1566           ["Clear above" gnus-summary-clear-above t])
1567          ["Current score" gnus-summary-current-score t]
1568          ["Set score" gnus-summary-set-score t]
1569          ["Switch current score file..." gnus-score-change-score-file t]
1570          ["Set mark below..." gnus-score-set-mark-below t]
1571          ["Set expunge below..." gnus-score-set-expunge-below t]
1572          ["Edit current score file" gnus-score-edit-current-scores t]
1573          ["Edit score file" gnus-score-edit-file t]
1574          ["Trace score" gnus-score-find-trace t]
1575          ["Find words" gnus-score-find-favourite-words t]
1576          ["Rescore buffer" gnus-summary-rescore t]
1577          ["Increase score..." gnus-summary-increase-score t]
1578          ["Lower score..." gnus-summary-lower-score t]))))
1579
1580     ;; Define both the Article menu in the summary buffer and the equivalent
1581     ;; Commands menu in the article buffer here for consistency.
1582     (let ((innards
1583            '(("Hide"
1584               ["All" gnus-article-hide t]
1585               ["Headers" gnus-article-toggle-headers t]
1586               ["Signature" gnus-article-hide-signature t]
1587               ["Citation" gnus-article-hide-citation t]
1588               ["PGP" gnus-article-hide-pgp t]
1589               ["Banner" gnus-article-strip-banner t]
1590               ["Boring headers" gnus-article-hide-boring-headers t])
1591              ("Highlight"
1592               ["All" gnus-article-highlight t]
1593               ["Headers" gnus-article-highlight-headers t]
1594               ["Signature" gnus-article-highlight-signature t]
1595               ["Citation" gnus-article-highlight-citation t])
1596              ("Date"
1597               ["Local" gnus-article-date-local t]
1598               ["ISO8601" gnus-article-date-iso8601 t]
1599               ["UT" gnus-article-date-ut t]
1600               ["Original" gnus-article-date-original t]
1601               ["Lapsed" gnus-article-date-lapsed t]
1602               ["User-defined" gnus-article-date-user t])
1603              ("Washing"
1604               ("Remove Blanks"
1605                ["Leading" gnus-article-strip-leading-blank-lines t]
1606                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1607                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1608                ["All of the above" gnus-article-strip-blank-lines t]
1609                ["All" gnus-article-strip-all-blank-lines t]
1610                ["Leading space" gnus-article-strip-leading-space t]
1611                ["Trailing space" gnus-article-strip-trailing-space t])
1612               ["Overstrike" gnus-article-treat-overstrike t]
1613               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1614               ["Emphasis" gnus-article-emphasize t]
1615               ["Word wrap" gnus-article-fill-cited-article t]
1616               ["Fill long lines" gnus-article-fill-long-lines t]
1617               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1618               ["CR" gnus-article-remove-cr t]
1619               ["Show X-Face" gnus-article-display-x-face t]
1620               ["UnHTMLize" gnus-article-treat-html t]
1621               ["Rot 13" gnus-summary-caesar-message t]
1622               ["Unix pipe" gnus-summary-pipe-message t]
1623               ["Add buttons" gnus-article-add-buttons t]
1624               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1625               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1626               ["Toggle MIME" gnus-summary-toggle-mime t]
1627               ["Verbose header" gnus-summary-verbose-headers t]
1628               ["Toggle header" gnus-summary-toggle-header t])
1629              ("Output"
1630               ["Save in default format" gnus-summary-save-article t]
1631               ["Save in file" gnus-summary-save-article-file t]
1632               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1633               ["Save in MH folder" gnus-summary-save-article-folder t]
1634               ["Save in VM folder" gnus-summary-save-article-vm t]
1635               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1636               ["Save body in file" gnus-summary-save-article-body-file t]
1637               ["Pipe through a filter" gnus-summary-pipe-output t]
1638               ["Add to SOUP packet" gnus-soup-add-article t]
1639               ["Print" gnus-summary-print-article t])
1640              ("Backend"
1641               ["Respool article..." gnus-summary-respool-article t]
1642               ["Move article..." gnus-summary-move-article
1643                (gnus-check-backend-function
1644                 'request-move-article gnus-newsgroup-name)]
1645               ["Copy article..." gnus-summary-copy-article t]
1646               ["Crosspost article..." gnus-summary-crosspost-article
1647                (gnus-check-backend-function
1648                 'request-replace-article gnus-newsgroup-name)]
1649               ["Import file..." gnus-summary-import-article t]
1650               ["Check if posted" gnus-summary-article-posted-p t]
1651               ["Edit article" gnus-summary-edit-article
1652                (not (gnus-group-read-only-p))]
1653               ["Delete article" gnus-summary-delete-article
1654                (gnus-check-backend-function
1655                 'request-expire-articles gnus-newsgroup-name)]
1656               ["Query respool" gnus-summary-respool-query t]
1657               ["Trace respool" gnus-summary-respool-trace t]
1658               ["Delete expirable articles" gnus-summary-expire-articles-now
1659                (gnus-check-backend-function
1660                 'request-expire-articles gnus-newsgroup-name)])
1661              ("Extract"
1662               ["Uudecode" gnus-uu-decode-uu t]
1663               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1664               ["Unshar" gnus-uu-decode-unshar t]
1665               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1666               ["Save" gnus-uu-decode-save t]
1667               ["Binhex" gnus-uu-decode-binhex t]
1668               ["Postscript" gnus-uu-decode-postscript t])
1669              ("Cache"
1670               ["Enter article" gnus-cache-enter-article t]
1671               ["Remove article" gnus-cache-remove-article t])
1672              ["Translate" gnus-article-babel t]
1673              ["Select article buffer" gnus-summary-select-article-buffer t]
1674              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1675              ["Isearch article..." gnus-summary-isearch-article t]
1676              ["Beginning of the article" gnus-summary-beginning-of-article t]
1677              ["End of the article" gnus-summary-end-of-article t]
1678              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1679              ["Fetch referenced articles" gnus-summary-refer-references t]
1680              ["Fetch current thread" gnus-summary-refer-thread t]
1681              ["Fetch article with id..." gnus-summary-refer-article t]
1682              ["Redisplay" gnus-summary-show-article t])))
1683       (easy-menu-define
1684        gnus-summary-article-menu gnus-summary-mode-map ""
1685        (cons "Article" innards))
1686
1687       (easy-menu-define
1688        gnus-article-commands-menu gnus-article-mode-map ""
1689        (cons "Commands" innards)))
1690
1691     (easy-menu-define
1692      gnus-summary-thread-menu gnus-summary-mode-map ""
1693      '("Threads"
1694        ["Toggle threading" gnus-summary-toggle-threads t]
1695        ["Hide threads" gnus-summary-hide-all-threads t]
1696        ["Show threads" gnus-summary-show-all-threads t]
1697        ["Hide thread" gnus-summary-hide-thread t]
1698        ["Show thread" gnus-summary-show-thread t]
1699        ["Go to next thread" gnus-summary-next-thread t]
1700        ["Go to previous thread" gnus-summary-prev-thread t]
1701        ["Go down thread" gnus-summary-down-thread t]
1702        ["Go up thread" gnus-summary-up-thread t]
1703        ["Top of thread" gnus-summary-top-thread t]
1704        ["Mark thread as read" gnus-summary-kill-thread t]
1705        ["Lower thread score" gnus-summary-lower-thread t]
1706        ["Raise thread score" gnus-summary-raise-thread t]
1707        ["Rethread current" gnus-summary-rethread-current t]
1708        ))
1709
1710     (easy-menu-define
1711      gnus-summary-post-menu gnus-summary-mode-map ""
1712      '("Post"
1713        ["Post an article" gnus-summary-post-news t]
1714        ["Followup" gnus-summary-followup t]
1715        ["Followup and yank" gnus-summary-followup-with-original t]
1716        ["Supersede article" gnus-summary-supersede-article t]
1717        ["Cancel article" gnus-summary-cancel-article t]
1718        ["Reply" gnus-summary-reply t]
1719        ["Reply and yank" gnus-summary-reply-with-original t]
1720        ["Wide reply" gnus-summary-wide-reply t]
1721        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1722        ["Mail forward" gnus-summary-mail-forward t]
1723        ["Post forward" gnus-summary-post-forward t]
1724        ["Digest and mail" gnus-summary-mail-digest t]
1725        ["Digest and post" gnus-summary-post-digest t]
1726        ["Resend message" gnus-summary-resend-message t]
1727        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1728        ["Send a mail" gnus-summary-mail-other-window t]
1729        ["Uuencode and post" gnus-uu-post-news t]
1730        ["Followup via news" gnus-summary-followup-to-mail t]
1731        ["Followup via news and yank"
1732         gnus-summary-followup-to-mail-with-original t]
1733        ;;("Draft"
1734        ;;["Send" gnus-summary-send-draft t]
1735        ;;["Send bounced" gnus-resend-bounced-mail t])
1736        ))
1737
1738     (easy-menu-define
1739      gnus-summary-misc-menu gnus-summary-mode-map ""
1740      '("Misc"
1741        ("Mark Read"
1742         ["Mark as read" gnus-summary-mark-as-read-forward t]
1743         ["Mark same subject and select"
1744          gnus-summary-kill-same-subject-and-select t]
1745         ["Mark same subject" gnus-summary-kill-same-subject t]
1746         ["Catchup" gnus-summary-catchup t]
1747         ["Catchup all" gnus-summary-catchup-all t]
1748         ["Catchup to here" gnus-summary-catchup-to-here t]
1749         ["Catchup region" gnus-summary-mark-region-as-read t]
1750         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1751        ("Mark Various"
1752         ["Tick" gnus-summary-tick-article-forward t]
1753         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1754         ["Remove marks" gnus-summary-clear-mark-forward t]
1755         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1756         ["Set bookmark" gnus-summary-set-bookmark t]
1757         ["Remove bookmark" gnus-summary-remove-bookmark t])
1758        ("Mark Limit"
1759         ["Marks..." gnus-summary-limit-to-marks t]
1760         ["Subject..." gnus-summary-limit-to-subject t]
1761         ["Author..." gnus-summary-limit-to-author t]
1762         ["Age..." gnus-summary-limit-to-age t]
1763         ["Extra..." gnus-summary-limit-to-extra t]
1764         ["Score" gnus-summary-limit-to-score t]
1765         ["Unread" gnus-summary-limit-to-unread t]
1766         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1767         ["Articles" gnus-summary-limit-to-articles t]
1768         ["Pop limit" gnus-summary-pop-limit t]
1769         ["Show dormant" gnus-summary-limit-include-dormant t]
1770         ["Hide childless dormant"
1771          gnus-summary-limit-exclude-childless-dormant t]
1772         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1773         ["Hide marked" gnus-summary-limit-exclude-marks t]
1774         ["Show expunged" gnus-summary-show-all-expunged t])
1775        ("Process Mark"
1776         ["Set mark" gnus-summary-mark-as-processable t]
1777         ["Remove mark" gnus-summary-unmark-as-processable t]
1778         ["Remove all marks" gnus-summary-unmark-all-processable t]
1779         ["Mark above" gnus-uu-mark-over t]
1780         ["Mark series" gnus-uu-mark-series t]
1781         ["Mark region" gnus-uu-mark-region t]
1782         ["Unmark region" gnus-uu-unmark-region t]
1783         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1784         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1785         ["Mark all" gnus-uu-mark-all t]
1786         ["Mark buffer" gnus-uu-mark-buffer t]
1787         ["Mark sparse" gnus-uu-mark-sparse t]
1788         ["Mark thread" gnus-uu-mark-thread t]
1789         ["Unmark thread" gnus-uu-unmark-thread t]
1790         ("Process Mark Sets"
1791          ["Kill" gnus-summary-kill-process-mark t]
1792          ["Yank" gnus-summary-yank-process-mark
1793           gnus-newsgroup-process-stack]
1794          ["Save" gnus-summary-save-process-mark t]))
1795        ("Scroll article"
1796         ["Page forward" gnus-summary-next-page t]
1797         ["Page backward" gnus-summary-prev-page t]
1798         ["Line forward" gnus-summary-scroll-up t])
1799        ("Move"
1800         ["Next unread article" gnus-summary-next-unread-article t]
1801         ["Previous unread article" gnus-summary-prev-unread-article t]
1802         ["Next article" gnus-summary-next-article t]
1803         ["Previous article" gnus-summary-prev-article t]
1804         ["Next unread subject" gnus-summary-next-unread-subject t]
1805         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1806         ["Next article same subject" gnus-summary-next-same-subject t]
1807         ["Previous article same subject" gnus-summary-prev-same-subject t]
1808         ["First unread article" gnus-summary-first-unread-article t]
1809         ["Best unread article" gnus-summary-best-unread-article t]
1810         ["Go to subject number..." gnus-summary-goto-subject t]
1811         ["Go to article number..." gnus-summary-goto-article t]
1812         ["Go to the last article" gnus-summary-goto-last-article t]
1813         ["Pop article off history" gnus-summary-pop-article t])
1814        ("Sort"
1815         ["Sort by number" gnus-summary-sort-by-number t]
1816         ["Sort by author" gnus-summary-sort-by-author t]
1817         ["Sort by subject" gnus-summary-sort-by-subject t]
1818         ["Sort by date" gnus-summary-sort-by-date t]
1819         ["Sort by score" gnus-summary-sort-by-score t]
1820         ["Sort by lines" gnus-summary-sort-by-lines t]
1821         ["Sort by characters" gnus-summary-sort-by-chars t])
1822        ("Help"
1823         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1824         ["Describe group" gnus-summary-describe-group t]
1825         ["Read manual" gnus-info-find-node t])
1826        ("Modes"
1827         ["Pick and read" gnus-pick-mode t]
1828         ["Binary" gnus-binary-mode t])
1829        ("Regeneration"
1830         ["Regenerate" gnus-summary-prepare t]
1831         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1832         ["Toggle threading" gnus-summary-toggle-threads t])
1833        ["Filter articles..." gnus-summary-execute-command t]
1834        ["Run command on subjects..." gnus-summary-universal-argument t]
1835        ["Search articles forward..." gnus-summary-search-article-forward t]
1836        ["Search articles backward..." gnus-summary-search-article-backward t]
1837        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1838        ["Expand window" gnus-summary-expand-window t]
1839        ["Expire expirable articles" gnus-summary-expire-articles
1840         (gnus-check-backend-function
1841          'request-expire-articles gnus-newsgroup-name)]
1842        ["Edit local kill file" gnus-summary-edit-local-kill t]
1843        ["Edit main kill file" gnus-summary-edit-global-kill t]
1844        ["Edit group parameters" gnus-summary-edit-parameters t]
1845        ["Customize group parameters" gnus-summary-customize-parameters t]
1846        ["Send a bug report" gnus-bug t]
1847        ("Exit"
1848         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1849         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1850         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1851         ["Exit group" gnus-summary-exit t]
1852         ["Exit group without updating" gnus-summary-exit-no-update t]
1853         ["Exit and goto next group" gnus-summary-next-group t]
1854         ["Exit and goto prev group" gnus-summary-prev-group t]
1855         ["Reselect group" gnus-summary-reselect-current-group t]
1856         ["Rescan group" gnus-summary-rescan-group t]
1857         ["Update dribble" gnus-summary-save-newsrc t])))
1858
1859     (gnus-run-hooks 'gnus-summary-menu-hook)))
1860
1861 (defun gnus-score-set-default (var value)
1862   "A version of set that updates the GNU Emacs menu-bar."
1863   (set var value)
1864   ;; It is the message that forces the active status to be updated.
1865   (message ""))
1866
1867 (defun gnus-make-score-map (type)
1868   "Make a summary score map of type TYPE."
1869   (if t
1870       nil
1871     (let ((headers '(("author" "from" string)
1872                      ("subject" "subject" string)
1873                      ("article body" "body" string)
1874                      ("article head" "head" string)
1875                      ("xref" "xref" string)
1876                      ("extra header" "extra" string)
1877                      ("lines" "lines" number)
1878                      ("followups to author" "followup" string)))
1879           (types '((number ("less than" <)
1880                            ("greater than" >)
1881                            ("equal" =))
1882                    (string ("substring" s)
1883                            ("exact string" e)
1884                            ("fuzzy string" f)
1885                            ("regexp" r))))
1886           (perms '(("temporary" (current-time-string))
1887                    ("permanent" nil)
1888                    ("immediate" now)))
1889           header)
1890       (list
1891        (apply
1892         'nconc
1893         (list
1894          (if (eq type 'lower)
1895              "Lower score"
1896            "Increase score"))
1897         (let (outh)
1898           (while headers
1899             (setq header (car headers))
1900             (setq outh
1901                   (cons
1902                    (apply
1903                     'nconc
1904                     (list (car header))
1905                     (let ((ts (cdr (assoc (nth 2 header) types)))
1906                           outt)
1907                       (while ts
1908                         (setq outt
1909                               (cons
1910                                (apply
1911                                 'nconc
1912                                 (list (caar ts))
1913                                 (let ((ps perms)
1914                                       outp)
1915                                   (while ps
1916                                     (setq outp
1917                                           (cons
1918                                            (vector
1919                                             (caar ps)
1920                                             (list
1921                                              'gnus-summary-score-entry
1922                                              (nth 1 header)
1923                                              (if (or (string= (nth 1 header)
1924                                                               "head")
1925                                                      (string= (nth 1 header)
1926                                                               "body"))
1927                                                  ""
1928                                                (list 'gnus-summary-header
1929                                                      (nth 1 header)))
1930                                              (list 'quote (nth 1 (car ts)))
1931                                              (list 'gnus-score-default nil)
1932                                              (nth 1 (car ps))
1933                                              t)
1934                                             t)
1935                                            outp))
1936                                     (setq ps (cdr ps)))
1937                                   (list (nreverse outp))))
1938                                outt))
1939                         (setq ts (cdr ts)))
1940                       (list (nreverse outt))))
1941                    outh))
1942             (setq headers (cdr headers)))
1943           (list (nreverse outh))))))))
1944
1945 \f
1946
1947 (defun gnus-summary-mode (&optional group)
1948   "Major mode for reading articles.
1949
1950 All normal editing commands are switched off.
1951 \\<gnus-summary-mode-map>
1952 Each line in this buffer represents one article.  To read an
1953 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1954 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1955 respectively.
1956
1957 You can also post articles and send mail from this buffer.  To
1958 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1959 of an article, type `\\[gnus-summary-reply]'.
1960
1961 There are approx. one gazillion commands you can execute in this
1962 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1963
1964 The following commands are available:
1965
1966 \\{gnus-summary-mode-map}"
1967   (interactive)
1968   (when (gnus-visual-p 'summary-menu 'menu)
1969     (gnus-summary-make-menu-bar))
1970   (kill-all-local-variables)
1971   (gnus-summary-make-local-variables)
1972   (gnus-make-thread-indent-array)
1973   (gnus-simplify-mode-line)
1974   (setq major-mode 'gnus-summary-mode)
1975   (setq mode-name "Summary")
1976   (make-local-variable 'minor-mode-alist)
1977   (use-local-map gnus-summary-mode-map)
1978   (buffer-disable-undo)
1979   (setq buffer-read-only t)             ;Disable modification
1980   (setq truncate-lines t)
1981   (setq selective-display t)
1982   (setq selective-display-ellipses t)   ;Display `...'
1983   (gnus-summary-set-display-table)
1984   (gnus-set-default-directory)
1985   (setq gnus-newsgroup-name group)
1986   (make-local-variable 'gnus-summary-line-format)
1987   (make-local-variable 'gnus-summary-line-format-spec)
1988   (make-local-variable 'gnus-summary-dummy-line-format)
1989   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1990   (make-local-variable 'gnus-summary-mark-positions)
1991   (make-local-hook 'pre-command-hook)
1992   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1993   (gnus-run-hooks 'gnus-summary-mode-hook)
1994   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1995   (gnus-update-summary-mark-positions))
1996
1997 (defun gnus-summary-make-local-variables ()
1998   "Make all the local summary buffer variables."
1999   (let (global)
2000     (dolist (local gnus-summary-local-variables)
2001       (if (consp local)
2002           (progn
2003             (if (eq (cdr local) 'global)
2004                 ;; Copy the global value of the variable.
2005                 (setq global (symbol-value (car local)))
2006               ;; Use the value from the list.
2007               (setq global (eval (cdr local))))
2008             (set (make-local-variable (car local)) global))
2009         ;; Simple nil-valued local variable.
2010         (set (make-local-variable local) nil)))))
2011
2012 (defun gnus-summary-clear-local-variables ()
2013   (let ((locals gnus-summary-local-variables))
2014     (while locals
2015       (if (consp (car locals))
2016           (and (vectorp (caar locals))
2017                (set (caar locals) nil))
2018         (and (vectorp (car locals))
2019              (set (car locals) nil)))
2020       (setq locals (cdr locals)))))
2021
2022 ;; Summary data functions.
2023
2024 (defmacro gnus-data-number (data)
2025   `(car ,data))
2026
2027 (defmacro gnus-data-set-number (data number)
2028   `(setcar ,data ,number))
2029
2030 (defmacro gnus-data-mark (data)
2031   `(nth 1 ,data))
2032
2033 (defmacro gnus-data-set-mark (data mark)
2034   `(setcar (nthcdr 1 ,data) ,mark))
2035
2036 (defmacro gnus-data-pos (data)
2037   `(nth 2 ,data))
2038
2039 (defmacro gnus-data-set-pos (data pos)
2040   `(setcar (nthcdr 2 ,data) ,pos))
2041
2042 (defmacro gnus-data-header (data)
2043   `(nth 3 ,data))
2044
2045 (defmacro gnus-data-set-header (data header)
2046   `(setf (nth 3 ,data) ,header))
2047
2048 (defmacro gnus-data-level (data)
2049   `(nth 4 ,data))
2050
2051 (defmacro gnus-data-unread-p (data)
2052   `(= (nth 1 ,data) gnus-unread-mark))
2053
2054 (defmacro gnus-data-read-p (data)
2055   `(/= (nth 1 ,data) gnus-unread-mark))
2056
2057 (defmacro gnus-data-pseudo-p (data)
2058   `(consp (nth 3 ,data)))
2059
2060 (defmacro gnus-data-find (number)
2061   `(assq ,number gnus-newsgroup-data))
2062
2063 (defmacro gnus-data-find-list (number &optional data)
2064   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2065      (memq (assq ,number bdata)
2066            bdata)))
2067
2068 (defmacro gnus-data-make (number mark pos header level)
2069   `(list ,number ,mark ,pos ,header ,level))
2070
2071 (defun gnus-data-enter (after-article number mark pos header level offset)
2072   (let ((data (gnus-data-find-list after-article)))
2073     (unless data
2074       (error "No such article: %d" after-article))
2075     (setcdr data (cons (gnus-data-make number mark pos header level)
2076                        (cdr data)))
2077     (setq gnus-newsgroup-data-reverse nil)
2078     (gnus-data-update-list (cddr data) offset)))
2079
2080 (defun gnus-data-enter-list (after-article list &optional offset)
2081   (when list
2082     (let ((data (and after-article (gnus-data-find-list after-article)))
2083           (ilist list))
2084       (if (not (or data
2085                    after-article))
2086           (let ((odata gnus-newsgroup-data))
2087             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2088             (when offset
2089               (gnus-data-update-list odata offset)))
2090         ;; Find the last element in the list to be spliced into the main
2091         ;; list.
2092         (while (cdr list)
2093           (setq list (cdr list)))
2094         (if (not data)
2095             (progn
2096               (setcdr list gnus-newsgroup-data)
2097               (setq gnus-newsgroup-data ilist)
2098               (when offset
2099                 (gnus-data-update-list (cdr list) offset)))
2100           (setcdr list (cdr data))
2101           (setcdr data ilist)
2102           (when offset
2103             (gnus-data-update-list (cdr list) offset))))
2104       (setq gnus-newsgroup-data-reverse nil))))
2105
2106 (defun gnus-data-remove (article &optional offset)
2107   (let ((data gnus-newsgroup-data))
2108     (if (= (gnus-data-number (car data)) article)
2109         (progn
2110           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2111                 gnus-newsgroup-data-reverse nil)
2112           (when offset
2113             (gnus-data-update-list gnus-newsgroup-data offset)))
2114       (while (cdr data)
2115         (when (= (gnus-data-number (cadr data)) article)
2116           (setcdr data (cddr data))
2117           (when offset
2118             (gnus-data-update-list (cdr data) offset))
2119           (setq data nil
2120                 gnus-newsgroup-data-reverse nil))
2121         (setq data (cdr data))))))
2122
2123 (defmacro gnus-data-list (backward)
2124   `(if ,backward
2125        (or gnus-newsgroup-data-reverse
2126            (setq gnus-newsgroup-data-reverse
2127                  (reverse gnus-newsgroup-data)))
2128      gnus-newsgroup-data))
2129
2130 (defun gnus-data-update-list (data offset)
2131   "Add OFFSET to the POS of all data entries in DATA."
2132   (setq gnus-newsgroup-data-reverse nil)
2133   (while data
2134     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2135     (setq data (cdr data))))
2136
2137 (defun gnus-summary-article-pseudo-p (article)
2138   "Say whether this article is a pseudo article or not."
2139   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2140
2141 (defmacro gnus-summary-article-sparse-p (article)
2142   "Say whether this article is a sparse article or not."
2143   `(memq ,article gnus-newsgroup-sparse))
2144
2145 (defmacro gnus-summary-article-ancient-p (article)
2146   "Say whether this article is a sparse article or not."
2147   `(memq ,article gnus-newsgroup-ancient))
2148
2149 (defun gnus-article-parent-p (number)
2150   "Say whether this article is a parent or not."
2151   (let ((data (gnus-data-find-list number)))
2152     (and (cdr data)                     ; There has to be an article after...
2153          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2154             (gnus-data-level (nth 1 data))))))
2155
2156 (defun gnus-article-children (number)
2157   "Return a list of all children to NUMBER."
2158   (let* ((data (gnus-data-find-list number))
2159          (level (gnus-data-level (car data)))
2160          children)
2161     (setq data (cdr data))
2162     (while (and data
2163                 (= (gnus-data-level (car data)) (1+ level)))
2164       (push (gnus-data-number (car data)) children)
2165       (setq data (cdr data)))
2166     children))
2167
2168 (defmacro gnus-summary-skip-intangible ()
2169   "If the current article is intangible, then jump to a different article."
2170   '(let ((to (get-text-property (point) 'gnus-intangible)))
2171      (and to (gnus-summary-goto-subject to))))
2172
2173 (defmacro gnus-summary-article-intangible-p ()
2174   "Say whether this article is intangible or not."
2175   '(get-text-property (point) 'gnus-intangible))
2176
2177 (defun gnus-article-read-p (article)
2178   "Say whether ARTICLE is read or not."
2179   (not (or (memq article gnus-newsgroup-marked)
2180            (memq article gnus-newsgroup-unreads)
2181            (memq article gnus-newsgroup-unselected)
2182            (memq article gnus-newsgroup-dormant))))
2183
2184 ;; Some summary mode macros.
2185
2186 (defmacro gnus-summary-article-number ()
2187   "The article number of the article on the current line.
2188 If there isn's an article number here, then we return the current
2189 article number."
2190   '(progn
2191      (gnus-summary-skip-intangible)
2192      (or (get-text-property (point) 'gnus-number)
2193          (gnus-summary-last-subject))))
2194
2195 (defmacro gnus-summary-article-header (&optional number)
2196   "Return the header of article NUMBER."
2197   `(gnus-data-header (gnus-data-find
2198                       ,(or number '(gnus-summary-article-number)))))
2199
2200 (defmacro gnus-summary-thread-level (&optional number)
2201   "Return the level of thread that starts with article NUMBER."
2202   `(if (and (eq gnus-summary-make-false-root 'dummy)
2203             (get-text-property (point) 'gnus-intangible))
2204        0
2205      (gnus-data-level (gnus-data-find
2206                        ,(or number '(gnus-summary-article-number))))))
2207
2208 (defmacro gnus-summary-article-mark (&optional number)
2209   "Return the mark of article NUMBER."
2210   `(gnus-data-mark (gnus-data-find
2211                     ,(or number '(gnus-summary-article-number)))))
2212
2213 (defmacro gnus-summary-article-pos (&optional number)
2214   "Return the position of the line of article NUMBER."
2215   `(gnus-data-pos (gnus-data-find
2216                    ,(or number '(gnus-summary-article-number)))))
2217
2218 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2219 (defmacro gnus-summary-article-subject (&optional number)
2220   "Return current subject string or nil if nothing."
2221   `(let ((headers
2222           ,(if number
2223                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2224              '(gnus-data-header (assq (gnus-summary-article-number)
2225                                       gnus-newsgroup-data)))))
2226      (and headers
2227           (vectorp headers)
2228           (mail-header-subject headers))))
2229
2230 (defmacro gnus-summary-article-score (&optional number)
2231   "Return current article score."
2232   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2233                   gnus-newsgroup-scored))
2234        gnus-summary-default-score 0))
2235
2236 (defun gnus-summary-article-children (&optional number)
2237   "Return a list of article numbers that are children of article NUMBER."
2238   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2239          (level (gnus-data-level (car data)))
2240          l children)
2241     (while (and (setq data (cdr data))
2242                 (> (setq l (gnus-data-level (car data))) level))
2243       (and (= (1+ level) l)
2244            (push (gnus-data-number (car data))
2245                  children)))
2246     (nreverse children)))
2247
2248 (defun gnus-summary-article-parent (&optional number)
2249   "Return the article number of the parent of article NUMBER."
2250   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2251                                     (gnus-data-list t)))
2252          (level (gnus-data-level (car data))))
2253     (if (zerop level)
2254         ()                              ; This is a root.
2255       ;; We search until we find an article with a level less than
2256       ;; this one.  That function has to be the parent.
2257       (while (and (setq data (cdr data))
2258                   (not (< (gnus-data-level (car data)) level))))
2259       (and data (gnus-data-number (car data))))))
2260
2261 (defun gnus-unread-mark-p (mark)
2262   "Say whether MARK is the unread mark."
2263   (= mark gnus-unread-mark))
2264
2265 (defun gnus-read-mark-p (mark)
2266   "Say whether MARK is one of the marks that mark as read.
2267 This is all marks except unread, ticked, dormant, and expirable."
2268   (not (or (= mark gnus-unread-mark)
2269            (= mark gnus-ticked-mark)
2270            (= mark gnus-dormant-mark)
2271            (= mark gnus-expirable-mark))))
2272
2273 (defmacro gnus-article-mark (number)
2274   "Return the MARK of article NUMBER.
2275 This macro should only be used when computing the mark the \"first\"
2276 time; i.e., when generating the summary lines.  After that,
2277 `gnus-summary-article-mark' should be used to examine the
2278 marks of articles."
2279   `(cond
2280     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2281     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2282     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2283     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2284     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2285     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2286     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2287     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2288            gnus-ancient-mark))))
2289
2290 ;; Saving hidden threads.
2291
2292 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2293 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2294
2295 (defmacro gnus-save-hidden-threads (&rest forms)
2296   "Save hidden threads, eval FORMS, and restore the hidden threads."
2297   (let ((config (make-symbol "config")))
2298     `(let ((,config (gnus-hidden-threads-configuration)))
2299        (unwind-protect
2300            (save-excursion
2301              ,@forms)
2302          (gnus-restore-hidden-threads-configuration ,config)))))
2303
2304 (defun gnus-data-compute-positions ()
2305   "Compute the positions of all articles."
2306   (setq gnus-newsgroup-data-reverse nil)
2307   (let ((data gnus-newsgroup-data))
2308     (save-excursion
2309       (gnus-save-hidden-threads
2310         (gnus-summary-show-all-threads)
2311         (goto-char (point-min))
2312         (while data
2313           (while (get-text-property (point) 'gnus-intangible)
2314             (forward-line 1))
2315           (gnus-data-set-pos (car data) (+ (point) 3))
2316           (setq data (cdr data))
2317           (forward-line 1))))))
2318
2319 (defun gnus-hidden-threads-configuration ()
2320   "Return the current hidden threads configuration."
2321   (save-excursion
2322     (let (config)
2323       (goto-char (point-min))
2324       (while (search-forward "\r" nil t)
2325         (push (1- (point)) config))
2326       config)))
2327
2328 (defun gnus-restore-hidden-threads-configuration (config)
2329   "Restore hidden threads configuration from CONFIG."
2330   (let (point buffer-read-only)
2331     (while (setq point (pop config))
2332       (when (and (< point (point-max))
2333                  (goto-char point)
2334                  (eq (char-after) ?\n))
2335         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2336
2337 ;; Various summary mode internalish functions.
2338
2339 (defun gnus-mouse-pick-article (e)
2340   (interactive "e")
2341   (mouse-set-point e)
2342   (gnus-summary-next-page nil t))
2343
2344 (defun gnus-summary-set-display-table ()
2345   ;; Change the display table.  Odd characters have a tendency to mess
2346   ;; up nicely formatted displays - we make all possible glyphs
2347   ;; display only a single character.
2348
2349   ;; We start from the standard display table, if any.
2350   (let ((table (or (copy-sequence standard-display-table)
2351                    (make-display-table)))
2352         (i 32))
2353     ;; Nix out all the control chars...
2354     (while (>= (setq i (1- i)) 0)
2355       (aset table i [??]))
2356     ;; ... but not newline and cr, of course.  (cr is necessary for the
2357     ;; selective display).
2358     (aset table ?\n nil)
2359     (aset table ?\r nil)
2360     ;; We keep TAB as well.
2361     (aset table ?\t nil)
2362     ;; We nix out any glyphs over 126 that are not set already.
2363     (let ((i 256))
2364       (while (>= (setq i (1- i)) 127)
2365         ;; Only modify if the entry is nil.
2366         (unless (aref table i)
2367           (aset table i [??]))))
2368     (setq buffer-display-table table)))
2369
2370 (defun gnus-summary-setup-buffer (group)
2371   "Initialize summary buffer."
2372   (let ((buffer (concat "*Summary " group "*")))
2373     (if (get-buffer buffer)
2374         (progn
2375           (set-buffer buffer)
2376           (setq gnus-summary-buffer (current-buffer))
2377           (not gnus-newsgroup-prepared))
2378       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2379       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2380       (gnus-summary-mode group)
2381       (when gnus-carpal
2382         (gnus-carpal-setup-buffer 'summary))
2383       (unless gnus-single-article-buffer
2384         (make-local-variable 'gnus-article-buffer)
2385         (make-local-variable 'gnus-article-current)
2386         (make-local-variable 'gnus-original-article-buffer))
2387       (setq gnus-newsgroup-name group)
2388       t)))
2389
2390 (defun gnus-set-global-variables ()
2391   ;; Set the global equivalents of the summary buffer-local variables
2392   ;; to the latest values they had.  These reflect the summary buffer
2393   ;; that was in action when the last article was fetched.
2394   (when (eq major-mode 'gnus-summary-mode)
2395     (setq gnus-summary-buffer (current-buffer))
2396     (let ((name gnus-newsgroup-name)
2397           (marked gnus-newsgroup-marked)
2398           (unread gnus-newsgroup-unreads)
2399           (headers gnus-current-headers)
2400           (data gnus-newsgroup-data)
2401           (summary gnus-summary-buffer)
2402           (article-buffer gnus-article-buffer)
2403           (original gnus-original-article-buffer)
2404           (gac gnus-article-current)
2405           (reffed gnus-reffed-article-number)
2406           (score-file gnus-current-score-file)
2407           (default-charset gnus-newsgroup-charset)
2408           (emphasis-alist gnus-newsgroup-emphasis-alist))
2409       (save-excursion
2410         (set-buffer gnus-group-buffer)
2411         (setq gnus-newsgroup-name name
2412               gnus-newsgroup-marked marked
2413               gnus-newsgroup-unreads unread
2414               gnus-current-headers headers
2415               gnus-newsgroup-data data
2416               gnus-article-current gac
2417               gnus-summary-buffer summary
2418               gnus-article-buffer article-buffer
2419               gnus-original-article-buffer original
2420               gnus-reffed-article-number reffed
2421               gnus-current-score-file score-file
2422               gnus-newsgroup-charset default-charset
2423               gnus-newsgroup-emphasis-alist emphasis-alist)
2424         ;; The article buffer also has local variables.
2425         (when (gnus-buffer-live-p gnus-article-buffer)
2426           (set-buffer gnus-article-buffer)
2427           (setq gnus-summary-buffer summary))))))
2428
2429 (defun gnus-summary-article-unread-p (article)
2430   "Say whether ARTICLE is unread or not."
2431   (memq article gnus-newsgroup-unreads))
2432
2433 (defun gnus-summary-first-article-p (&optional article)
2434   "Return whether ARTICLE is the first article in the buffer."
2435   (if (not (setq article (or article (gnus-summary-article-number))))
2436       nil
2437     (eq article (caar gnus-newsgroup-data))))
2438
2439 (defun gnus-summary-last-article-p (&optional article)
2440   "Return whether ARTICLE is the last article in the buffer."
2441   (if (not (setq article (or article (gnus-summary-article-number))))
2442       ;; All non-existent numbers are the last article.  :-)
2443       t
2444     (not (cdr (gnus-data-find-list article)))))
2445
2446 (defun gnus-make-thread-indent-array ()
2447   (let ((n 200))
2448     (unless (and gnus-thread-indent-array
2449                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2450       (setq gnus-thread-indent-array (make-vector 201 "")
2451             gnus-thread-indent-array-level gnus-thread-indent-level)
2452       (while (>= n 0)
2453         (aset gnus-thread-indent-array n
2454               (make-string (* n gnus-thread-indent-level) ? ))
2455         (setq n (1- n))))))
2456
2457 (defun gnus-update-summary-mark-positions ()
2458   "Compute where the summary marks are to go."
2459   (save-excursion
2460     (when (gnus-buffer-exists-p gnus-summary-buffer)
2461       (set-buffer gnus-summary-buffer))
2462     (let ((gnus-replied-mark 129)
2463           (gnus-score-below-mark 130)
2464           (gnus-score-over-mark 130)
2465           (gnus-download-mark 131)
2466           (spec gnus-summary-line-format-spec)
2467           gnus-visual pos)
2468       (save-excursion
2469         (gnus-set-work-buffer)
2470         (let ((gnus-summary-line-format-spec spec)
2471               (gnus-newsgroup-downloadable '((0 . t))))
2472           (gnus-summary-insert-line
2473            (make-full-mail-header 0 "" "" "" "" "" 0 0 "" nil)
2474            0 nil 128 t nil "" nil 1)
2475           (goto-char (point-min))
2476           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2477                                              (- (point) 2)))))
2478           (goto-char (point-min))
2479           (push (cons 'replied (and (search-forward "\201" nil t)
2480                                     (- (point) 2)))
2481                 pos)
2482           (goto-char (point-min))
2483           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2484                 pos)
2485           (goto-char (point-min))
2486           (push (cons 'download
2487                       (and (search-forward "\203" nil t) (- (point) 2)))
2488                 pos)))
2489       (setq gnus-summary-mark-positions pos))))
2490
2491 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2492   "Insert a dummy root in the summary buffer."
2493   (beginning-of-line)
2494   (gnus-add-text-properties
2495    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2496    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2497
2498 (defun gnus-summary-from-or-to-or-newsgroups (header)
2499   (let ((to (cdr (assq 'To (mail-header-extra header))))
2500         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2501         (mail-parse-charset gnus-newsgroup-charset))
2502     (cond
2503      ((and to
2504            gnus-ignored-from-addresses
2505            (string-match gnus-ignored-from-addresses
2506                          (mail-header-from header)))
2507       (concat "-> "
2508               (or (car (funcall gnus-extract-address-components
2509                                 (funcall
2510                                  gnus-decode-encoded-word-function to)))
2511                   (funcall gnus-decode-encoded-word-function to))))
2512      ((and newsgroups
2513            gnus-ignored-from-addresses
2514            (string-match gnus-ignored-from-addresses
2515                          (mail-header-from header)))
2516       (concat "=> " newsgroups))
2517      (t
2518       (or (car (funcall gnus-extract-address-components
2519                         (mail-header-from header)))
2520           (mail-header-from header))))))
2521
2522 (defun gnus-summary-insert-line (gnus-tmp-header
2523                                  gnus-tmp-level gnus-tmp-current
2524                                  gnus-tmp-unread gnus-tmp-replied
2525                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2526                                  &optional gnus-tmp-dummy gnus-tmp-score
2527                                  gnus-tmp-process)
2528   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2529          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2530          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2531          (gnus-tmp-score-char
2532           (if (or (null gnus-summary-default-score)
2533                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2534                       gnus-summary-zcore-fuzz))
2535               ? ;Whitespace
2536             (if (< gnus-tmp-score gnus-summary-default-score)
2537                 gnus-score-below-mark gnus-score-over-mark)))
2538          (gnus-tmp-replied
2539           (cond (gnus-tmp-process gnus-process-mark)
2540                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2541                  gnus-cached-mark)
2542                 (gnus-tmp-replied gnus-replied-mark)
2543                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2544                  gnus-saved-mark)
2545                 (t gnus-unread-mark)))
2546          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2547          (gnus-tmp-name
2548           (cond
2549            ((string-match "<[^>]+> *$" gnus-tmp-from)
2550             (let ((beg (match-beginning 0)))
2551               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2552                        (substring gnus-tmp-from (1+ (match-beginning 0))
2553                                   (1- (match-end 0))))
2554                   (substring gnus-tmp-from 0 beg))))
2555            ((string-match "(.+)" gnus-tmp-from)
2556             (substring gnus-tmp-from
2557                        (1+ (match-beginning 0)) (1- (match-end 0))))
2558            (t gnus-tmp-from)))
2559          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2560          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2561          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2562          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2563          (buffer-read-only nil))
2564     (when (string= gnus-tmp-name "")
2565       (setq gnus-tmp-name gnus-tmp-from))
2566     (unless (numberp gnus-tmp-lines)
2567       (setq gnus-tmp-lines 0))
2568     (gnus-put-text-property-excluding-characters-with-faces
2569      (point)
2570      (progn (eval gnus-summary-line-format-spec) (point))
2571      'gnus-number gnus-tmp-number)
2572     (when (gnus-visual-p 'summary-highlight 'highlight)
2573       (forward-line -1)
2574       (gnus-run-hooks 'gnus-summary-update-hook)
2575       (forward-line 1))))
2576
2577 (defun gnus-summary-update-line (&optional dont-update)
2578   ;; Update summary line after change.
2579   (when (and gnus-summary-default-score
2580              (not gnus-summary-inhibit-highlight))
2581     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2582            (article (gnus-summary-article-number))
2583            (score (gnus-summary-article-score article)))
2584       (unless dont-update
2585         (if (and gnus-summary-mark-below
2586                  (< (gnus-summary-article-score)
2587                     gnus-summary-mark-below))
2588             ;; This article has a low score, so we mark it as read.
2589             (when (memq article gnus-newsgroup-unreads)
2590               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2591           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2592             ;; This article was previously marked as read on account
2593             ;; of a low score, but now it has risen, so we mark it as
2594             ;; unread.
2595             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2596         (gnus-summary-update-mark
2597          (if (or (null gnus-summary-default-score)
2598                  (<= (abs (- score gnus-summary-default-score))
2599                      gnus-summary-zcore-fuzz))
2600              ? ;Whitespace
2601            (if (< score gnus-summary-default-score)
2602                gnus-score-below-mark gnus-score-over-mark))
2603          'score))
2604       ;; Do visual highlighting.
2605       (when (gnus-visual-p 'summary-highlight 'highlight)
2606         (gnus-run-hooks 'gnus-summary-update-hook)))))
2607
2608 (defvar gnus-tmp-new-adopts nil)
2609
2610 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2611   "Return the number of articles in THREAD.
2612 This may be 0 in some cases -- if none of the articles in
2613 the thread are to be displayed."
2614   (let* ((number
2615           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2616           (cond
2617            ((not (listp thread))
2618             1)
2619            ((and (consp thread) (cdr thread))
2620             (apply
2621              '+ 1 (mapcar
2622                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2623            ((null thread)
2624             1)
2625            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2626             1)
2627            (t 0))))
2628     (when (and level (zerop level) gnus-tmp-new-adopts)
2629       (incf number
2630             (apply '+ (mapcar
2631                        'gnus-summary-number-of-articles-in-thread
2632                        gnus-tmp-new-adopts))))
2633     (if char
2634         (if (> number 1) gnus-not-empty-thread-mark
2635           gnus-empty-thread-mark)
2636       number)))
2637
2638 (defun gnus-summary-set-local-parameters (group)
2639   "Go through the local params of GROUP and set all variable specs in that list."
2640   (let ((params (gnus-group-find-parameter group))
2641         elem)
2642     (while params
2643       (setq elem (car params)
2644             params (cdr params))
2645       (and (consp elem)                 ; Has to be a cons.
2646            (consp (cdr elem))           ; The cdr has to be a list.
2647            (symbolp (car elem))         ; Has to be a symbol in there.
2648            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2649            (ignore-errors               ; So we set it.
2650              (make-local-variable (car elem))
2651              (set (car elem) (eval (nth 1 elem))))))))
2652
2653 (defun gnus-summary-read-group (group &optional show-all no-article
2654                                       kill-buffer no-display backward
2655                                       select-articles)
2656   "Start reading news in newsgroup GROUP.
2657 If SHOW-ALL is non-nil, already read articles are also listed.
2658 If NO-ARTICLE is non-nil, no article is selected initially.
2659 If NO-DISPLAY, don't generate a summary buffer."
2660   (let (result)
2661     (while (and group
2662                 (null (setq result
2663                             (let ((gnus-auto-select-next nil))
2664                               (or (gnus-summary-read-group-1
2665                                    group show-all no-article
2666                                    kill-buffer no-display
2667                                    select-articles)
2668                                   (setq show-all nil
2669                                         select-articles nil)))))
2670                 (eq gnus-auto-select-next 'quietly))
2671       (set-buffer gnus-group-buffer)
2672       ;; The entry function called above goes to the next
2673       ;; group automatically, so we go two groups back
2674       ;; if we are searching for the previous group.
2675       (when backward
2676         (gnus-group-prev-unread-group 2))
2677       (if (not (equal group (gnus-group-group-name)))
2678           (setq group (gnus-group-group-name))
2679         (setq group nil)))
2680     result))
2681
2682 (defun gnus-summary-read-group-1 (group show-all no-article
2683                                         kill-buffer no-display
2684                                         &optional select-articles)
2685   ;; Killed foreign groups can't be entered.
2686   (when (and (not (gnus-group-native-p group))
2687              (not (gnus-gethash group gnus-newsrc-hashtb)))
2688     (error "Dead non-native groups can't be entered"))
2689   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2690   (let* ((new-group (gnus-summary-setup-buffer group))
2691          (quit-config (gnus-group-quit-config group))
2692          (did-select (and new-group (gnus-select-newsgroup
2693                                      group show-all select-articles))))
2694     (cond
2695      ;; This summary buffer exists already, so we just select it.
2696      ((not new-group)
2697       (gnus-set-global-variables)
2698       (when kill-buffer
2699         (gnus-kill-or-deaden-summary kill-buffer))
2700       (gnus-configure-windows 'summary 'force)
2701       (gnus-set-mode-line 'summary)
2702       (gnus-summary-position-point)
2703       (message "")
2704       t)
2705      ;; We couldn't select this group.
2706      ((null did-select)
2707       (when (and (eq major-mode 'gnus-summary-mode)
2708                  (not (equal (current-buffer) kill-buffer)))
2709         (kill-buffer (current-buffer))
2710         (if (not quit-config)
2711             (progn
2712               ;; Update the info -- marks might need to be removed,
2713               ;; for instance.
2714               (gnus-summary-update-info)
2715               (set-buffer gnus-group-buffer)
2716               (gnus-group-jump-to-group group)
2717               (gnus-group-next-unread-group 1))
2718           (gnus-handle-ephemeral-exit quit-config)))
2719       (gnus-message 3 "Can't select group")
2720       nil)
2721      ;; The user did a `C-g' while prompting for number of articles,
2722      ;; so we exit this group.
2723      ((eq did-select 'quit)
2724       (and (eq major-mode 'gnus-summary-mode)
2725            (not (equal (current-buffer) kill-buffer))
2726            (kill-buffer (current-buffer)))
2727       (when kill-buffer
2728         (gnus-kill-or-deaden-summary kill-buffer))
2729       (if (not quit-config)
2730           (progn
2731             (set-buffer gnus-group-buffer)
2732             (gnus-group-jump-to-group group)
2733             (gnus-group-next-unread-group 1)
2734             (gnus-configure-windows 'group 'force))
2735         (gnus-handle-ephemeral-exit quit-config))
2736       ;; Finally signal the quit.
2737       (signal 'quit nil))
2738      ;; The group was successfully selected.
2739      (t
2740       (gnus-set-global-variables)
2741       ;; Save the active value in effect when the group was entered.
2742       (setq gnus-newsgroup-active
2743             (gnus-copy-sequence
2744              (gnus-active gnus-newsgroup-name)))
2745       ;; You can change the summary buffer in some way with this hook.
2746       (gnus-run-hooks 'gnus-select-group-hook)
2747       ;; Set any local variables in the group parameters.
2748       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2749       (gnus-update-format-specifications
2750        nil 'summary 'summary-mode 'summary-dummy)
2751       ;; Do score processing.
2752       (when gnus-use-scoring
2753         (gnus-possibly-score-headers))
2754       ;; Check whether to fill in the gaps in the threads.
2755       (when gnus-build-sparse-threads
2756         (gnus-build-sparse-threads))
2757       ;; Find the initial limit.
2758       (if gnus-show-threads
2759           (if show-all
2760               (let ((gnus-newsgroup-dormant nil))
2761                 (gnus-summary-initial-limit show-all))
2762             (gnus-summary-initial-limit show-all))
2763         (setq gnus-newsgroup-limit
2764               (mapcar
2765                (lambda (header) (mail-header-number header))
2766                gnus-newsgroup-headers)))
2767       ;; Generate the summary buffer.
2768       (unless no-display
2769         (gnus-summary-prepare))
2770       (when gnus-use-trees
2771         (gnus-tree-open group)
2772         (setq gnus-summary-highlight-line-function
2773               'gnus-tree-highlight-article))
2774       ;; If the summary buffer is empty, but there are some low-scored
2775       ;; articles or some excluded dormants, we include these in the
2776       ;; buffer.
2777       (when (and (zerop (buffer-size))
2778                  (not no-display))
2779         (cond (gnus-newsgroup-dormant
2780                (gnus-summary-limit-include-dormant))
2781               ((and gnus-newsgroup-scored show-all)
2782                (gnus-summary-limit-include-expunged t))))
2783       ;; Function `gnus-apply-kill-file' must be called in this hook.
2784       (gnus-run-hooks 'gnus-apply-kill-hook)
2785       (if (and (zerop (buffer-size))
2786                (not no-display))
2787           (progn
2788             ;; This newsgroup is empty.
2789             (gnus-summary-catchup-and-exit nil t)
2790             (gnus-message 6 "No unread news")
2791             (when kill-buffer
2792               (gnus-kill-or-deaden-summary kill-buffer))
2793             ;; Return nil from this function.
2794             nil)
2795         ;; Hide conversation thread subtrees.  We cannot do this in
2796         ;; gnus-summary-prepare-hook since kill processing may not
2797         ;; work with hidden articles.
2798         (and gnus-show-threads
2799              gnus-thread-hide-subtree
2800              (gnus-summary-hide-all-threads))
2801         (when kill-buffer
2802           (gnus-kill-or-deaden-summary kill-buffer))
2803         ;; Show first unread article if requested.
2804         (if (and (not no-article)
2805                  (not no-display)
2806                  gnus-newsgroup-unreads
2807                  gnus-auto-select-first)
2808             (progn
2809               (gnus-configure-windows 'summary)
2810               (cond
2811                ((eq gnus-auto-select-first 'best)
2812                 (gnus-summary-best-unread-article))
2813                ((eq gnus-auto-select-first t)
2814                 (gnus-summary-first-unread-article))
2815                ((gnus-functionp gnus-auto-select-first)
2816                 (funcall gnus-auto-select-first))))
2817           ;; Don't select any articles, just move point to the first
2818           ;; article in the group.
2819           (goto-char (point-min))
2820           (gnus-summary-position-point)
2821           (gnus-configure-windows 'summary 'force)
2822           (gnus-set-mode-line 'summary))
2823         (when (get-buffer-window gnus-group-buffer t)
2824           ;; Gotta use windows, because recenter does weird stuff if
2825           ;; the current buffer ain't the displayed window.
2826           (let ((owin (selected-window)))
2827             (select-window (get-buffer-window gnus-group-buffer t))
2828             (when (gnus-group-goto-group group)
2829               (recenter))
2830             (select-window owin)))
2831         ;; Mark this buffer as "prepared".
2832         (setq gnus-newsgroup-prepared t)
2833         (gnus-run-hooks 'gnus-summary-prepared-hook)
2834         t)))))
2835
2836 (defun gnus-summary-prepare ()
2837   "Generate the summary buffer."
2838   (interactive)
2839   (let ((buffer-read-only nil))
2840     (erase-buffer)
2841     (setq gnus-newsgroup-data nil
2842           gnus-newsgroup-data-reverse nil)
2843     (gnus-run-hooks 'gnus-summary-generate-hook)
2844     ;; Generate the buffer, either with threads or without.
2845     (when gnus-newsgroup-headers
2846       (gnus-summary-prepare-threads
2847        (if gnus-show-threads
2848            (gnus-sort-gathered-threads
2849             (funcall gnus-summary-thread-gathering-function
2850                      (gnus-sort-threads
2851                       (gnus-cut-threads (gnus-make-threads)))))
2852          ;; Unthreaded display.
2853          (gnus-sort-articles gnus-newsgroup-headers))))
2854     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2855     ;; Call hooks for modifying summary buffer.
2856     (goto-char (point-min))
2857     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2858
2859 (defsubst gnus-general-simplify-subject (subject)
2860   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2861   (setq subject
2862         (cond
2863          ;; Truncate the subject.
2864          (gnus-simplify-subject-functions
2865           (gnus-map-function gnus-simplify-subject-functions subject))
2866          ((numberp gnus-summary-gather-subject-limit)
2867           (setq subject (gnus-simplify-subject-re subject))
2868           (if (> (length subject) gnus-summary-gather-subject-limit)
2869               (substring subject 0 gnus-summary-gather-subject-limit)
2870             subject))
2871          ;; Fuzzily simplify it.
2872          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2873           (gnus-simplify-subject-fuzzy subject))
2874          ;; Just remove the leading "Re:".
2875          (t
2876           (gnus-simplify-subject-re subject))))
2877
2878   (if (and gnus-summary-gather-exclude-subject
2879            (string-match gnus-summary-gather-exclude-subject subject))
2880       nil                               ; This article shouldn't be gathered
2881     subject))
2882
2883 (defun gnus-summary-simplify-subject-query ()
2884   "Query where the respool algorithm would put this article."
2885   (interactive)
2886   (gnus-summary-select-article)
2887   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2888
2889 (defun gnus-gather-threads-by-subject (threads)
2890   "Gather threads by looking at Subject headers."
2891   (if (not gnus-summary-make-false-root)
2892       threads
2893     (let ((hashtb (gnus-make-hashtable 1024))
2894           (prev threads)
2895           (result threads)
2896           subject hthread whole-subject)
2897       (while threads
2898         (setq subject (gnus-general-simplify-subject
2899                        (setq whole-subject (mail-header-subject
2900                                             (caar threads)))))
2901         (when subject
2902           (if (setq hthread (gnus-gethash subject hashtb))
2903               (progn
2904                 ;; We enter a dummy root into the thread, if we
2905                 ;; haven't done that already.
2906                 (unless (stringp (caar hthread))
2907                   (setcar hthread (list whole-subject (car hthread))))
2908                 ;; We add this new gathered thread to this gathered
2909                 ;; thread.
2910                 (setcdr (car hthread)
2911                         (nconc (cdar hthread) (list (car threads))))
2912                 ;; Remove it from the list of threads.
2913                 (setcdr prev (cdr threads))
2914                 (setq threads prev))
2915             ;; Enter this thread into the hash table.
2916             (gnus-sethash subject threads hashtb)))
2917         (setq prev threads)
2918         (setq threads (cdr threads)))
2919       result)))
2920
2921 (defun gnus-gather-threads-by-references (threads)
2922   "Gather threads by looking at References headers."
2923   (let ((idhashtb (gnus-make-hashtable 1024))
2924         (thhashtb (gnus-make-hashtable 1024))
2925         (prev threads)
2926         (result threads)
2927         ids references id gthread gid entered ref)
2928     (while threads
2929       (when (setq references (mail-header-references (caar threads)))
2930         (setq id (mail-header-id (caar threads))
2931               ids (gnus-split-references references)
2932               entered nil)
2933         (while (setq ref (pop ids))
2934           (setq ids (delete ref ids))
2935           (if (not (setq gid (gnus-gethash ref idhashtb)))
2936               (progn
2937                 (gnus-sethash ref id idhashtb)
2938                 (gnus-sethash id threads thhashtb))
2939             (setq gthread (gnus-gethash gid thhashtb))
2940             (unless entered
2941               ;; We enter a dummy root into the thread, if we
2942               ;; haven't done that already.
2943               (unless (stringp (caar gthread))
2944                 (setcar gthread (list (mail-header-subject (caar gthread))
2945                                       (car gthread))))
2946               ;; We add this new gathered thread to this gathered
2947               ;; thread.
2948               (setcdr (car gthread)
2949                       (nconc (cdar gthread) (list (car threads)))))
2950             ;; Add it into the thread hash table.
2951             (gnus-sethash id gthread thhashtb)
2952             (setq entered t)
2953             ;; Remove it from the list of threads.
2954             (setcdr prev (cdr threads))
2955             (setq threads prev))))
2956       (setq prev threads)
2957       (setq threads (cdr threads)))
2958     result))
2959
2960 (defun gnus-sort-gathered-threads (threads)
2961   "Sort subtreads inside each gathered thread by article number."
2962   (let ((result threads))
2963     (while threads
2964       (when (stringp (caar threads))
2965         (setcdr (car threads)
2966                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2967       (setq threads (cdr threads)))
2968     result))
2969
2970 (defun gnus-thread-loop-p (root thread)
2971   "Say whether ROOT is in THREAD."
2972   (let ((stack (list thread))
2973         (infloop 0)
2974         th)
2975     (while (setq thread (pop stack))
2976       (setq th (cdr thread))
2977       (while (and th
2978                   (not (eq (caar th) root)))
2979         (pop th))
2980       (if th
2981           ;; We have found a loop.
2982           (let (ref-dep)
2983             (setcdr thread (delq (car th) (cdr thread)))
2984             (if (boundp (setq ref-dep (intern "none"
2985                                               gnus-newsgroup-dependencies)))
2986                 (setcdr (symbol-value ref-dep)
2987                         (nconc (cdr (symbol-value ref-dep))
2988                                (list (car th))))
2989               (set ref-dep (list nil (car th))))
2990             (setq infloop 1
2991                   stack nil))
2992         ;; Push all the subthreads onto the stack.
2993         (push (cdr thread) stack)))
2994     infloop))
2995
2996 (defun gnus-make-threads ()
2997   "Go through the dependency hashtb and find the roots.  Return all threads."
2998   (let (threads)
2999     (while (catch 'infloop
3000              (mapatoms
3001               (lambda (refs)
3002                 ;; Deal with self-referencing References loops.
3003                 (when (and (car (symbol-value refs))
3004                            (not (zerop
3005                                  (apply
3006                                   '+
3007                                   (mapcar
3008                                    (lambda (thread)
3009                                      (gnus-thread-loop-p
3010                                       (car (symbol-value refs)) thread))
3011                                    (cdr (symbol-value refs)))))))
3012                   (setq threads nil)
3013                   (throw 'infloop t))
3014                 (unless (car (symbol-value refs))
3015                   ;; These threads do not refer back to any other articles,
3016                   ;; so they're roots.
3017                   (setq threads (append (cdr (symbol-value refs)) threads))))
3018               gnus-newsgroup-dependencies)))
3019     threads))
3020
3021 ;; Build the thread tree.
3022 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3023   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3024
3025 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3026 if it was already present.
3027
3028 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3029 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3030 Message-IDs will be renamed be renamed to a unique Message-ID before
3031 being entered.
3032
3033 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3034   (let* ((id (mail-header-id header))
3035          (id-dep (and id (intern id dependencies)))
3036          ref ref-dep ref-header)
3037     ;; Enter this `header' in the `dependencies' table.
3038     (cond
3039      ((not id-dep)
3040       (setq header nil))
3041      ;; The first two cases do the normal part: enter a new `header'
3042      ;; in the `dependencies' table.
3043      ((not (boundp id-dep))
3044       (set id-dep (list header)))
3045      ((null (car (symbol-value id-dep)))
3046       (setcar (symbol-value id-dep) header))
3047
3048      ;; From here the `header' was already present in the
3049      ;; `dependencies' table.
3050      (force-new
3051       ;; Overrides an existing entry;
3052       ;; just set the header part of the entry.
3053       (setcar (symbol-value id-dep) header))
3054
3055      ;; Renames the existing `header' to a unique Message-ID.
3056      ((not gnus-summary-ignore-duplicates)
3057       ;; An article with this Message-ID has already been seen.
3058       ;; We rename the Message-ID.
3059       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3060            (list header))
3061       (mail-header-set-id header id))
3062
3063      ;; The last case ignores an existing entry, except it adds any
3064      ;; additional Xrefs (in case the two articles came from different
3065      ;; servers.
3066      ;; Also sets `header' to `nil' meaning that the `dependencies'
3067      ;; table was *not* modified.
3068      (t
3069       (mail-header-set-xref
3070        (car (symbol-value id-dep))
3071        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3072                    "")
3073                (or (mail-header-xref header) "")))
3074       (setq header nil)))
3075
3076     (when header
3077       ;; First check if that we are not creating a References loop.
3078       (setq ref (gnus-parent-id (mail-header-references header)))
3079       (while (and ref
3080                   (setq ref-dep (intern-soft ref dependencies))
3081                   (boundp ref-dep)
3082                   (setq ref-header (car (symbol-value ref-dep))))
3083         (if (string= id ref)
3084             ;; Yuk!  This is a reference loop.  Make the article be a
3085             ;; root article.
3086             (progn
3087               (mail-header-set-references (car (symbol-value id-dep)) "none")
3088               (setq ref nil))
3089           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3090       (setq ref (gnus-parent-id (mail-header-references header)))
3091       (setq ref-dep (intern (or ref "none") dependencies))
3092       (if (boundp ref-dep)
3093           (setcdr (symbol-value ref-dep)
3094                   (nconc (cdr (symbol-value ref-dep))
3095                          (list (symbol-value id-dep))))
3096         (set ref-dep (list nil (symbol-value id-dep)))))
3097     header))
3098
3099 (defun gnus-build-sparse-threads ()
3100   (let ((headers gnus-newsgroup-headers)
3101         (gnus-summary-ignore-duplicates t)
3102         header references generation relations
3103         subject child end new-child date)
3104     ;; First we create an alist of generations/relations, where
3105     ;; generations is how much we trust the relation, and the relation
3106     ;; is parent/child.
3107     (gnus-message 7 "Making sparse threads...")
3108     (save-excursion
3109       (nnheader-set-temp-buffer " *gnus sparse threads*")
3110       (while (setq header (pop headers))
3111         (when (and (setq references (mail-header-references header))
3112                    (not (string= references "")))
3113           (insert references)
3114           (setq child (mail-header-id header)
3115                 subject (mail-header-subject header)
3116                 date (mail-header-date header)
3117                 generation 0)
3118           (while (search-backward ">" nil t)
3119             (setq end (1+ (point)))
3120             (when (search-backward "<" nil t)
3121               (setq new-child (buffer-substring (point) end))
3122               (push (list (incf generation)
3123                           child (setq child new-child)
3124                           subject date)
3125                     relations)))
3126           (when child
3127             (push (list (1+ generation) child nil subject) relations))
3128           (erase-buffer)))
3129       (kill-buffer (current-buffer)))
3130     ;; Sort over trustworthiness.
3131     (mapcar
3132      (lambda (relation)
3133        (when (gnus-dependencies-add-header
3134               (make-full-mail-header
3135                gnus-reffed-article-number
3136                (nth 3 relation) "" (or (nth 4 relation) "")
3137                (nth 1 relation)
3138                (or (nth 2 relation) "") 0 0 "")
3139               gnus-newsgroup-dependencies nil)
3140          (push gnus-reffed-article-number gnus-newsgroup-limit)
3141          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3142          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3143                gnus-newsgroup-reads)
3144          (decf gnus-reffed-article-number)))
3145      (sort relations 'car-less-than-car))
3146     (gnus-message 7 "Making sparse threads...done")))
3147
3148 (defun gnus-build-old-threads ()
3149   ;; Look at all the articles that refer back to old articles, and
3150   ;; fetch the headers for the articles that aren't there.  This will
3151   ;; build complete threads - if the roots haven't been expired by the
3152   ;; server, that is.
3153   (let (id heads)
3154     (mapatoms
3155      (lambda (refs)
3156        (when (not (car (symbol-value refs)))
3157          (setq heads (cdr (symbol-value refs)))
3158          (while heads
3159            (if (memq (mail-header-number (caar heads))
3160                      gnus-newsgroup-dormant)
3161                (setq heads (cdr heads))
3162              (setq id (symbol-name refs))
3163              (while (and (setq id (gnus-build-get-header id))
3164                          (not (car (gnus-id-to-thread id)))))
3165              (setq heads nil)))))
3166      gnus-newsgroup-dependencies)))
3167
3168 ;; This function has to be called with point after the article number
3169 ;; on the beginning of the line.
3170 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3171   (let ((eol (gnus-point-at-eol))
3172         (buffer (current-buffer))
3173         header)
3174
3175     ;; overview: [num subject from date id refs chars lines misc]
3176     (unless (eobp)
3177       (forward-char))
3178
3179     (setq header
3180           (make-full-mail-header
3181            number                               ; number
3182            (nnheader-nov-field)                 ; subject
3183            (nnheader-nov-field)                 ; from
3184            (nnheader-nov-field)                 ; date
3185            (nnheader-nov-read-message-id)       ; id
3186            (nnheader-nov-field)                 ; refs
3187            (nnheader-nov-read-integer)          ; chars
3188            (nnheader-nov-read-integer)          ; lines
3189            (unless (eobp)
3190              (nnheader-nov-field))              ; misc
3191            (nnheader-nov-parse-extra)))         ; extra
3192
3193     (when gnus-alter-header-function
3194       (funcall gnus-alter-header-function header))
3195     (gnus-dependencies-add-header header dependencies force-new)))
3196
3197 (defun gnus-build-get-header (id)
3198   ;; Look through the buffer of NOV lines and find the header to
3199   ;; ID.  Enter this line into the dependencies hash table, and return
3200   ;; the id of the parent article (if any).
3201   (let ((deps gnus-newsgroup-dependencies)
3202         found header)
3203     (prog1
3204         (save-excursion
3205           (set-buffer nntp-server-buffer)
3206           (let ((case-fold-search nil))
3207             (goto-char (point-min))
3208             (while (and (not found)
3209                         (search-forward id nil t))
3210               (beginning-of-line)
3211               (setq found (looking-at
3212                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3213                                    (regexp-quote id))))
3214               (or found (beginning-of-line 2)))
3215             (when found
3216               (beginning-of-line)
3217               (and
3218                (setq header (gnus-nov-parse-line
3219                              (read (current-buffer)) deps))
3220                (gnus-parent-id (mail-header-references header))))))
3221       (when header
3222         (let ((number (mail-header-number header)))
3223           (push number gnus-newsgroup-limit)
3224           (push header gnus-newsgroup-headers)
3225           (if (memq number gnus-newsgroup-unselected)
3226               (progn
3227                 (push number gnus-newsgroup-unreads)
3228                 (setq gnus-newsgroup-unselected
3229                       (delq number gnus-newsgroup-unselected)))
3230             (push number gnus-newsgroup-ancient)))))))
3231
3232 (defun gnus-build-all-threads ()
3233   "Read all the headers."
3234   (let ((gnus-summary-ignore-duplicates t)
3235         (dependencies gnus-newsgroup-dependencies)
3236         header article)
3237     (save-excursion
3238       (set-buffer nntp-server-buffer)
3239       (let ((case-fold-search nil))
3240         (goto-char (point-min))
3241         (while (not (eobp))
3242           (ignore-errors
3243             (setq article (read (current-buffer))
3244                   header (gnus-nov-parse-line
3245                           article dependencies)))
3246           (when header
3247             (save-excursion
3248               (set-buffer gnus-summary-buffer)
3249               (push header gnus-newsgroup-headers)
3250               (if (memq (setq article (mail-header-number header))
3251                         gnus-newsgroup-unselected)
3252                   (progn
3253                     (push article gnus-newsgroup-unreads)
3254                     (setq gnus-newsgroup-unselected
3255                           (delq article gnus-newsgroup-unselected)))
3256                 (push article gnus-newsgroup-ancient)))
3257             (forward-line 1)))))))
3258
3259 (defun gnus-summary-update-article-line (article header)
3260   "Update the line for ARTICLE using HEADERS."
3261   (let* ((id (mail-header-id header))
3262          (thread (gnus-id-to-thread id)))
3263     (unless thread
3264       (error "Article in no thread"))
3265     ;; Update the thread.
3266     (setcar thread header)
3267     (gnus-summary-goto-subject article)
3268     (let* ((datal (gnus-data-find-list article))
3269            (data (car datal))
3270            (length (when (cdr datal)
3271                      (- (gnus-data-pos data)
3272                         (gnus-data-pos (cadr datal)))))
3273            (buffer-read-only nil)
3274            (level (gnus-summary-thread-level)))
3275       (gnus-delete-line)
3276       (gnus-summary-insert-line
3277        header level nil (gnus-article-mark article)
3278        (memq article gnus-newsgroup-replied)
3279        (memq article gnus-newsgroup-expirable)
3280        ;; Only insert the Subject string when it's different
3281        ;; from the previous Subject string.
3282        (if (gnus-subject-equal
3283             (condition-case ()
3284                 (mail-header-subject
3285                  (gnus-data-header
3286                   (cadr
3287                    (gnus-data-find-list
3288                     article
3289                     (gnus-data-list t)))))
3290               ;; Error on the side of excessive subjects.
3291               (error ""))
3292             (mail-header-subject header))
3293            ""
3294          (mail-header-subject header))
3295        nil (cdr (assq article gnus-newsgroup-scored))
3296        (memq article gnus-newsgroup-processable))
3297       (when length
3298         (gnus-data-update-list
3299          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3300
3301 (defun gnus-summary-update-article (article &optional iheader)
3302   "Update ARTICLE in the summary buffer."
3303   (set-buffer gnus-summary-buffer)
3304   (let* ((header (gnus-summary-article-header article))
3305          (id (mail-header-id header))
3306          (data (gnus-data-find article))
3307          (thread (gnus-id-to-thread id))
3308          (references (mail-header-references header))
3309          (parent
3310           (gnus-id-to-thread
3311            (or (gnus-parent-id
3312                 (when (and references
3313                            (not (equal "" references)))
3314                   references))
3315                "none")))
3316          (buffer-read-only nil)
3317          (old (car thread)))
3318     (when thread
3319       (unless iheader
3320         (setcar thread nil)
3321         (when parent
3322           (delq thread parent)))
3323       (if (gnus-summary-insert-subject id header)
3324           ;; Set the (possibly) new article number in the data structure.
3325           (gnus-data-set-number data (gnus-id-to-article id))
3326         (setcar thread old)
3327         nil))))
3328
3329 (defun gnus-rebuild-thread (id &optional line)
3330   "Rebuild the thread containing ID.
3331 If LINE, insert the rebuilt thread starting on line LINE."
3332   (let ((buffer-read-only nil)
3333         old-pos current thread data)
3334     (if (not gnus-show-threads)
3335         (setq thread (list (car (gnus-id-to-thread id))))
3336       ;; Get the thread this article is part of.
3337       (setq thread (gnus-remove-thread id)))
3338     (setq old-pos (gnus-point-at-bol))
3339     (setq current (save-excursion
3340                     (and (zerop (forward-line -1))
3341                          (gnus-summary-article-number))))
3342     ;; If this is a gathered thread, we have to go some re-gathering.
3343     (when (stringp (car thread))
3344       (let ((subject (car thread))
3345             roots thr)
3346         (setq thread (cdr thread))
3347         (while thread
3348           (unless (memq (setq thr (gnus-id-to-thread
3349                                    (gnus-root-id
3350                                     (mail-header-id (caar thread)))))
3351                         roots)
3352             (push thr roots))
3353           (setq thread (cdr thread)))
3354         ;; We now have all (unique) roots.
3355         (if (= (length roots) 1)
3356             ;; All the loose roots are now one solid root.
3357             (setq thread (car roots))
3358           (setq thread (cons subject (gnus-sort-threads roots))))))
3359     (let (threads)
3360       ;; We then insert this thread into the summary buffer.
3361       (when line
3362         (goto-char (point-min))
3363         (forward-line (1- line)))
3364       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3365         (if gnus-show-threads
3366             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3367           (gnus-summary-prepare-unthreaded thread))
3368         (setq data (nreverse gnus-newsgroup-data))
3369         (setq threads gnus-newsgroup-threads))
3370       ;; We splice the new data into the data structure.
3371       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3372       ;;!!! then we want to insert at the beginning of the buffer.
3373       ;;!!! That happens to be true with Gnus now, but that may
3374       ;;!!! change in the future.  Perhaps.
3375       (gnus-data-enter-list
3376        (if line nil current) data (- (point) old-pos))
3377       (setq gnus-newsgroup-threads
3378             (nconc threads gnus-newsgroup-threads))
3379       (gnus-data-compute-positions))))
3380
3381 (defun gnus-number-to-header (number)
3382   "Return the header for article NUMBER."
3383   (let ((headers gnus-newsgroup-headers))
3384     (while (and headers
3385                 (not (= number (mail-header-number (car headers)))))
3386       (pop headers))
3387     (when headers
3388       (car headers))))
3389
3390 (defun gnus-parent-headers (in-headers &optional generation)
3391   "Return the headers of the GENERATIONeth parent of HEADERS."
3392   (unless generation
3393     (setq generation 1))
3394   (let ((parent t)
3395         (headers in-headers)
3396         references)
3397     (while (and parent
3398                 (not (zerop generation))
3399                 (setq references (mail-header-references headers)))
3400       (setq headers (if (and references
3401                              (setq parent (gnus-parent-id references)))
3402                         (car (gnus-id-to-thread parent))
3403                       nil))
3404       (decf generation))
3405     (and (not (eq headers in-headers))
3406          headers)))
3407
3408 (defun gnus-id-to-thread (id)
3409   "Return the (sub-)thread where ID appears."
3410   (gnus-gethash id gnus-newsgroup-dependencies))
3411
3412 (defun gnus-id-to-article (id)
3413   "Return the article number of ID."
3414   (let ((thread (gnus-id-to-thread id)))
3415     (when (and thread
3416                (car thread))
3417       (mail-header-number (car thread)))))
3418
3419 (defun gnus-id-to-header (id)
3420   "Return the article headers of ID."
3421   (car (gnus-id-to-thread id)))
3422
3423 (defun gnus-article-displayed-root-p (article)
3424   "Say whether ARTICLE is a root(ish) article."
3425   (let ((level (gnus-summary-thread-level article))
3426         (refs (mail-header-references  (gnus-summary-article-header article)))
3427         particle)
3428     (cond
3429      ((null level) nil)
3430      ((zerop level) t)
3431      ((null refs) t)
3432      ((null (gnus-parent-id refs)) t)
3433      ((and (= 1 level)
3434            (null (setq particle (gnus-id-to-article
3435                                  (gnus-parent-id refs))))
3436            (null (gnus-summary-thread-level particle)))))))
3437
3438 (defun gnus-root-id (id)
3439   "Return the id of the root of the thread where ID appears."
3440   (let (last-id prev)
3441     (while (and id (setq prev (car (gnus-id-to-thread id))))
3442       (setq last-id id
3443             id (gnus-parent-id (mail-header-references prev))))
3444     last-id))
3445
3446 (defun gnus-articles-in-thread (thread)
3447   "Return the list of articles in THREAD."
3448   (cons (mail-header-number (car thread))
3449         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3450
3451 (defun gnus-remove-thread (id &optional dont-remove)
3452   "Remove the thread that has ID in it."
3453   (let (headers thread last-id)
3454     ;; First go up in this thread until we find the root.
3455     (setq last-id (gnus-root-id id)
3456           headers (message-flatten-list (gnus-id-to-thread last-id)))
3457     ;; We have now found the real root of this thread.  It might have
3458     ;; been gathered into some loose thread, so we have to search
3459     ;; through the threads to find the thread we wanted.
3460     (let ((threads gnus-newsgroup-threads)
3461           sub)
3462       (while threads
3463         (setq sub (car threads))
3464         (if (stringp (car sub))
3465             ;; This is a gathered thread, so we look at the roots
3466             ;; below it to find whether this article is in this
3467             ;; gathered root.
3468             (progn
3469               (setq sub (cdr sub))
3470               (while sub
3471                 (when (member (caar sub) headers)
3472                   (setq thread (car threads)
3473                         threads nil
3474                         sub nil))
3475                 (setq sub (cdr sub))))
3476           ;; It's an ordinary thread, so we check it.
3477           (when (eq (car sub) (car headers))
3478             (setq thread sub
3479                   threads nil)))
3480         (setq threads (cdr threads)))
3481       ;; If this article is in no thread, then it's a root.
3482       (if thread
3483           (unless dont-remove
3484             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3485         (setq thread (gnus-id-to-thread last-id)))
3486       (when thread
3487         (prog1
3488             thread                      ; We return this thread.
3489           (unless dont-remove
3490             (if (stringp (car thread))
3491                 (progn
3492                   ;; If we use dummy roots, then we have to remove the
3493                   ;; dummy root as well.
3494                   (when (eq gnus-summary-make-false-root 'dummy)
3495                     ;; We go to the dummy root by going to
3496                     ;; the first sub-"thread", and then one line up.
3497                     (gnus-summary-goto-article
3498                      (mail-header-number (caadr thread)))
3499                     (forward-line -1)
3500                     (gnus-delete-line)
3501                     (gnus-data-compute-positions))
3502                   (setq thread (cdr thread))
3503                   (while thread
3504                     (gnus-remove-thread-1 (car thread))
3505                     (setq thread (cdr thread))))
3506               (gnus-summary-show-all-threads)
3507               (gnus-remove-thread-1 thread))))))))
3508
3509 (defun gnus-remove-thread-1 (thread)
3510   "Remove the thread THREAD recursively."
3511   (let ((number (mail-header-number (pop thread)))
3512         d)
3513     (setq thread (reverse thread))
3514     (while thread
3515       (gnus-remove-thread-1 (pop thread)))
3516     (when (setq d (gnus-data-find number))
3517       (goto-char (gnus-data-pos d))
3518       (gnus-data-remove
3519        number
3520        (- (gnus-point-at-bol)
3521           (prog1
3522               (1+ (gnus-point-at-eol))
3523             (gnus-delete-line)))))))
3524
3525 (defun gnus-sort-threads (threads)
3526   "Sort THREADS."
3527   (if (not gnus-thread-sort-functions)
3528       threads
3529     (gnus-message 8 "Sorting threads...")
3530     (prog1
3531         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3532       (gnus-message 8 "Sorting threads...done"))))
3533
3534 (defun gnus-sort-articles (articles)
3535   "Sort ARTICLES."
3536   (when gnus-article-sort-functions
3537     (gnus-message 7 "Sorting articles...")
3538     (prog1
3539         (setq gnus-newsgroup-headers
3540               (sort articles (gnus-make-sort-function
3541                               gnus-article-sort-functions)))
3542       (gnus-message 7 "Sorting articles...done"))))
3543
3544 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3545 (defmacro gnus-thread-header (thread)
3546   ;; Return header of first article in THREAD.
3547   ;; Note that THREAD must never, ever be anything else than a variable -
3548   ;; using some other form will lead to serious barfage.
3549   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3550   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3551   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3552         (vector thread) 2))
3553
3554 (defsubst gnus-article-sort-by-number (h1 h2)
3555   "Sort articles by article number."
3556   (< (mail-header-number h1)
3557      (mail-header-number h2)))
3558
3559 (defun gnus-thread-sort-by-number (h1 h2)
3560   "Sort threads by root article number."
3561   (gnus-article-sort-by-number
3562    (gnus-thread-header h1) (gnus-thread-header h2)))
3563
3564 (defsubst gnus-article-sort-by-lines (h1 h2)
3565   "Sort articles by article Lines header."
3566   (< (mail-header-lines h1)
3567      (mail-header-lines h2)))
3568
3569 (defun gnus-thread-sort-by-lines (h1 h2)
3570   "Sort threads by root article Lines header."
3571   (gnus-article-sort-by-lines
3572    (gnus-thread-header h1) (gnus-thread-header h2)))
3573
3574 (defsubst gnus-article-sort-by-chars (h1 h2)
3575   "Sort articles by octet length."
3576   (< (mail-header-chars h1)
3577      (mail-header-chars h2)))
3578
3579 (defun gnus-thread-sort-by-chars (h1 h2)
3580   "Sort threads by root article octet length."
3581   (gnus-article-sort-by-chars
3582    (gnus-thread-header h1) (gnus-thread-header h2)))
3583
3584 (defsubst gnus-article-sort-by-author (h1 h2)
3585   "Sort articles by root author."
3586   (string-lessp
3587    (let ((addr (car (mime-read-field 'From h1))))
3588      (or (std11-full-name-string addr)
3589          (std11-address-string addr)
3590          ""))
3591    (let ((addr (car (mime-read-field 'From h2))))
3592      (or (std11-full-name-string addr)
3593          (std11-address-string addr)
3594          ""))
3595    ))
3596
3597 (defun gnus-thread-sort-by-author (h1 h2)
3598   "Sort threads by root author."
3599   (gnus-article-sort-by-author
3600    (gnus-thread-header h1)  (gnus-thread-header h2)))
3601
3602 (defsubst gnus-article-sort-by-subject (h1 h2)
3603   "Sort articles by root subject."
3604   (string-lessp
3605    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3606    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3607
3608 (defun gnus-thread-sort-by-subject (h1 h2)
3609   "Sort threads by root subject."
3610   (gnus-article-sort-by-subject
3611    (gnus-thread-header h1) (gnus-thread-header h2)))
3612
3613 (defsubst gnus-article-sort-by-date (h1 h2)
3614   "Sort articles by root article date."
3615   (time-less-p
3616    (gnus-date-get-time (mail-header-date h1))
3617    (gnus-date-get-time (mail-header-date h2))))
3618
3619 (defun gnus-thread-sort-by-date (h1 h2)
3620   "Sort threads by root article date."
3621   (gnus-article-sort-by-date
3622    (gnus-thread-header h1) (gnus-thread-header h2)))
3623
3624 (defsubst gnus-article-sort-by-score (h1 h2)
3625   "Sort articles by root article score.
3626 Unscored articles will be counted as having a score of zero."
3627   (> (or (cdr (assq (mail-header-number h1)
3628                     gnus-newsgroup-scored))
3629          gnus-summary-default-score 0)
3630      (or (cdr (assq (mail-header-number h2)
3631                     gnus-newsgroup-scored))
3632          gnus-summary-default-score 0)))
3633
3634 (defun gnus-thread-sort-by-score (h1 h2)
3635   "Sort threads by root article score."
3636   (gnus-article-sort-by-score
3637    (gnus-thread-header h1) (gnus-thread-header h2)))
3638
3639 (defun gnus-thread-sort-by-total-score (h1 h2)
3640   "Sort threads by the sum of all scores in the thread.
3641 Unscored articles will be counted as having a score of zero."
3642   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3643
3644 (defun gnus-thread-total-score (thread)
3645   ;; This function find the total score of THREAD.
3646   (cond ((null thread)
3647          0)
3648         ((consp thread)
3649          (if (stringp (car thread))
3650              (apply gnus-thread-score-function 0
3651                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3652            (gnus-thread-total-score-1 thread)))
3653         (t
3654          (gnus-thread-total-score-1 (list thread)))))
3655
3656 (defun gnus-thread-total-score-1 (root)
3657   ;; This function find the total score of the thread below ROOT.
3658   (setq root (car root))
3659   (apply gnus-thread-score-function
3660          (or (append
3661               (mapcar 'gnus-thread-total-score
3662                       (cdr (gnus-id-to-thread (mail-header-id root))))
3663               (when (> (mail-header-number root) 0)
3664                 (list (or (cdr (assq (mail-header-number root)
3665                                      gnus-newsgroup-scored))
3666                           gnus-summary-default-score 0))))
3667              (list gnus-summary-default-score)
3668              '(0))))
3669
3670 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3671 (defvar gnus-tmp-prev-subject nil)
3672 (defvar gnus-tmp-false-parent nil)
3673 (defvar gnus-tmp-root-expunged nil)
3674 (defvar gnus-tmp-dummy-line nil)
3675
3676 (defvar gnus-tmp-header)
3677 (defun gnus-extra-header (type &optional header)
3678   "Return the extra header of TYPE."
3679   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3680       ""))
3681
3682 (defun gnus-summary-prepare-threads (threads)
3683   "Prepare summary buffer from THREADS and indentation LEVEL.
3684 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3685 or a straight list of headers."
3686   (gnus-message 7 "Generating summary...")
3687
3688   (setq gnus-newsgroup-threads threads)
3689   (beginning-of-line)
3690
3691   (let ((gnus-tmp-level 0)
3692         (default-score (or gnus-summary-default-score 0))
3693         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3694         thread number subject stack state gnus-tmp-gathered beg-match
3695         new-roots gnus-tmp-new-adopts thread-end
3696         gnus-tmp-header gnus-tmp-unread
3697         gnus-tmp-replied gnus-tmp-subject-or-nil
3698         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3699         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3700         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3701
3702     (setq gnus-tmp-prev-subject nil)
3703
3704     (if (vectorp (car threads))
3705         ;; If this is a straight (sic) list of headers, then a
3706         ;; threaded summary display isn't required, so we just create
3707         ;; an unthreaded one.
3708         (gnus-summary-prepare-unthreaded threads)
3709
3710       ;; Do the threaded display.
3711
3712       (while (or threads stack gnus-tmp-new-adopts new-roots)
3713
3714         (if (and (= gnus-tmp-level 0)
3715                  (or (not stack)
3716                      (= (caar stack) 0))
3717                  (not gnus-tmp-false-parent)
3718                  (or gnus-tmp-new-adopts new-roots))
3719             (if gnus-tmp-new-adopts
3720                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3721                       thread (list (car gnus-tmp-new-adopts))
3722                       gnus-tmp-header (caar thread)
3723                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3724               (when new-roots
3725                 (setq thread (list (car new-roots))
3726                       gnus-tmp-header (caar thread)
3727                       new-roots (cdr new-roots))))
3728
3729           (if threads
3730               ;; If there are some threads, we do them before the
3731               ;; threads on the stack.
3732               (setq thread threads
3733                     gnus-tmp-header (caar thread))
3734             ;; There were no current threads, so we pop something off
3735             ;; the stack.
3736             (setq state (car stack)
3737                   gnus-tmp-level (car state)
3738                   thread (cdr state)
3739                   stack (cdr stack)
3740                   gnus-tmp-header (caar thread))))
3741
3742         (setq gnus-tmp-false-parent nil)
3743         (setq gnus-tmp-root-expunged nil)
3744         (setq thread-end nil)
3745
3746         (if (stringp gnus-tmp-header)
3747             ;; The header is a dummy root.
3748             (cond
3749              ((eq gnus-summary-make-false-root 'adopt)
3750               ;; We let the first article adopt the rest.
3751               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3752                                                (cddar thread)))
3753               (setq gnus-tmp-gathered
3754                     (nconc (mapcar
3755                             (lambda (h) (mail-header-number (car h)))
3756                             (cddar thread))
3757                            gnus-tmp-gathered))
3758               (setq thread (cons (list (caar thread)
3759                                        (cadar thread))
3760                                  (cdr thread)))
3761               (setq gnus-tmp-level -1
3762                     gnus-tmp-false-parent t))
3763              ((eq gnus-summary-make-false-root 'empty)
3764               ;; We print adopted articles with empty subject fields.
3765               (setq gnus-tmp-gathered
3766                     (nconc (mapcar
3767                             (lambda (h) (mail-header-number (car h)))
3768                             (cddar thread))
3769                            gnus-tmp-gathered))
3770               (setq gnus-tmp-level -1))
3771              ((eq gnus-summary-make-false-root 'dummy)
3772               ;; We remember that we probably want to output a dummy
3773               ;; root.
3774               (setq gnus-tmp-dummy-line gnus-tmp-header)
3775               (setq gnus-tmp-prev-subject gnus-tmp-header))
3776              (t
3777               ;; We do not make a root for the gathered
3778               ;; sub-threads at all.
3779               (setq gnus-tmp-level -1)))
3780
3781           (setq number (mail-header-number gnus-tmp-header)
3782                 subject (mail-header-subject gnus-tmp-header))
3783
3784           (cond
3785            ;; If the thread has changed subject, we might want to make
3786            ;; this subthread into a root.
3787            ((and (null gnus-thread-ignore-subject)
3788                  (not (zerop gnus-tmp-level))
3789                  gnus-tmp-prev-subject
3790                  (not (inline
3791                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3792             (setq new-roots (nconc new-roots (list (car thread)))
3793                   thread-end t
3794                   gnus-tmp-header nil))
3795            ;; If the article lies outside the current limit,
3796            ;; then we do not display it.
3797            ((not (memq number gnus-newsgroup-limit))
3798             (setq gnus-tmp-gathered
3799                   (nconc (mapcar
3800                           (lambda (h) (mail-header-number (car h)))
3801                           (cdar thread))
3802                          gnus-tmp-gathered))
3803             (setq gnus-tmp-new-adopts (if (cdar thread)
3804                                           (append gnus-tmp-new-adopts
3805                                                   (cdar thread))
3806                                         gnus-tmp-new-adopts)
3807                   thread-end t
3808                   gnus-tmp-header nil)
3809             (when (zerop gnus-tmp-level)
3810               (setq gnus-tmp-root-expunged t)))
3811            ;; Perhaps this article is to be marked as read?
3812            ((and gnus-summary-mark-below
3813                  (< (or (cdr (assq number gnus-newsgroup-scored))
3814                         default-score)
3815                     gnus-summary-mark-below)
3816                  ;; Don't touch sparse articles.
3817                  (not (gnus-summary-article-sparse-p number))
3818                  (not (gnus-summary-article-ancient-p number)))
3819             (setq gnus-newsgroup-unreads
3820                   (delq number gnus-newsgroup-unreads))
3821             (if gnus-newsgroup-auto-expire
3822                 (push number gnus-newsgroup-expirable)
3823               (push (cons number gnus-low-score-mark)
3824                     gnus-newsgroup-reads))))
3825
3826           (when gnus-tmp-header
3827             ;; We may have an old dummy line to output before this
3828             ;; article.
3829             (when (and gnus-tmp-dummy-line
3830                        (gnus-subject-equal
3831                         gnus-tmp-dummy-line
3832                         (mail-header-subject gnus-tmp-header)))
3833               (gnus-summary-insert-dummy-line
3834                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3835               (setq gnus-tmp-dummy-line nil))
3836
3837             ;; Compute the mark.
3838             (setq gnus-tmp-unread (gnus-article-mark number))
3839
3840             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3841                                   gnus-tmp-header gnus-tmp-level)
3842                   gnus-newsgroup-data)
3843
3844             ;; Actually insert the line.
3845             (setq
3846              gnus-tmp-subject-or-nil
3847              (cond
3848               ((and gnus-thread-ignore-subject
3849                     gnus-tmp-prev-subject
3850                     (not (inline (gnus-subject-equal
3851                                   gnus-tmp-prev-subject subject))))
3852                subject)
3853               ((zerop gnus-tmp-level)
3854                (if (and (eq gnus-summary-make-false-root 'empty)
3855                         (memq number gnus-tmp-gathered)
3856                         gnus-tmp-prev-subject
3857                         (inline (gnus-subject-equal
3858                                  gnus-tmp-prev-subject subject)))
3859                    gnus-summary-same-subject
3860                  subject))
3861               (t gnus-summary-same-subject)))
3862             (if (and (eq gnus-summary-make-false-root 'adopt)
3863                      (= gnus-tmp-level 1)
3864                      (memq number gnus-tmp-gathered))
3865                 (setq gnus-tmp-opening-bracket ?\<
3866                       gnus-tmp-closing-bracket ?\>)
3867               (setq gnus-tmp-opening-bracket ?\[
3868                     gnus-tmp-closing-bracket ?\]))
3869             (setq
3870              gnus-tmp-indentation
3871              (aref gnus-thread-indent-array gnus-tmp-level)
3872              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3873              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3874                                 gnus-summary-default-score 0)
3875              gnus-tmp-score-char
3876              (if (or (null gnus-summary-default-score)
3877                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3878                          gnus-summary-zcore-fuzz))
3879                  ? ;Whitespace
3880                (if (< gnus-tmp-score gnus-summary-default-score)
3881                    gnus-score-below-mark gnus-score-over-mark))
3882              gnus-tmp-replied
3883              (cond ((memq number gnus-newsgroup-processable)
3884                     gnus-process-mark)
3885                    ((memq number gnus-newsgroup-cached)
3886                     gnus-cached-mark)
3887                    ((memq number gnus-newsgroup-replied)
3888                     gnus-replied-mark)
3889                    ((memq number gnus-newsgroup-saved)
3890                     gnus-saved-mark)
3891                    (t gnus-unread-mark))
3892              gnus-tmp-from (mail-header-from gnus-tmp-header)
3893              gnus-tmp-name
3894              (cond
3895               ((string-match "<[^>]+> *$" gnus-tmp-from)
3896                (setq beg-match (match-beginning 0))
3897                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3898                         (substring gnus-tmp-from (1+ (match-beginning 0))
3899                                    (1- (match-end 0))))
3900                    (substring gnus-tmp-from 0 beg-match)))
3901               ((string-match "(.+)" gnus-tmp-from)
3902                (substring gnus-tmp-from
3903                           (1+ (match-beginning 0)) (1- (match-end 0))))
3904               (t gnus-tmp-from)))
3905             (when (string= gnus-tmp-name "")
3906               (setq gnus-tmp-name gnus-tmp-from))
3907             (unless (numberp gnus-tmp-lines)
3908               (setq gnus-tmp-lines 0))
3909             (gnus-put-text-property
3910              (point)
3911              (progn (eval gnus-summary-line-format-spec) (point))
3912              'gnus-number number)
3913             (when gnus-visual-p
3914               (forward-line -1)
3915               (gnus-run-hooks 'gnus-summary-update-hook)
3916               (forward-line 1))
3917
3918             (setq gnus-tmp-prev-subject subject)))
3919
3920         (when (nth 1 thread)
3921           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3922         (incf gnus-tmp-level)
3923         (setq threads (if thread-end nil (cdar thread)))
3924         (unless threads
3925           (setq gnus-tmp-level 0)))))
3926   (gnus-message 7 "Generating summary...done"))
3927
3928 (defun gnus-summary-prepare-unthreaded (headers)
3929   "Generate an unthreaded summary buffer based on HEADERS."
3930   (let (header number mark)
3931
3932     (beginning-of-line)
3933
3934     (while headers
3935       ;; We may have to root out some bad articles...
3936       (when (memq (setq number (mail-header-number
3937                                 (setq header (pop headers))))
3938                   gnus-newsgroup-limit)
3939         ;; Mark article as read when it has a low score.
3940         (when (and gnus-summary-mark-below
3941                    (< (or (cdr (assq number gnus-newsgroup-scored))
3942                           gnus-summary-default-score 0)
3943                       gnus-summary-mark-below)
3944                    (not (gnus-summary-article-ancient-p number)))
3945           (setq gnus-newsgroup-unreads
3946                 (delq number gnus-newsgroup-unreads))
3947           (if gnus-newsgroup-auto-expire
3948               (push number gnus-newsgroup-expirable)
3949             (push (cons number gnus-low-score-mark)
3950                   gnus-newsgroup-reads)))
3951
3952         (setq mark (gnus-article-mark number))
3953         (push (gnus-data-make number mark (1+ (point)) header 0)
3954               gnus-newsgroup-data)
3955         (gnus-summary-insert-line
3956          header 0 number
3957          mark (memq number gnus-newsgroup-replied)
3958          (memq number gnus-newsgroup-expirable)
3959          (mail-header-subject header) nil
3960          (cdr (assq number gnus-newsgroup-scored))
3961          (memq number gnus-newsgroup-processable))))))
3962
3963 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3964   "Select newsgroup GROUP.
3965 If READ-ALL is non-nil, all articles in the group are selected.
3966 If SELECT-ARTICLES, only select those articles from GROUP."
3967   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3968          ;;!!! Dirty hack; should be removed.
3969          (gnus-summary-ignore-duplicates
3970           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3971               t
3972             gnus-summary-ignore-duplicates))
3973          (info (nth 2 entry))
3974          articles fetched-articles cached)
3975
3976     (unless (gnus-check-server
3977              (setq gnus-current-select-method
3978                    (gnus-find-method-for-group group)))
3979       (error "Couldn't open server"))
3980
3981     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3982         (gnus-activate-group group)     ; Or we can activate it...
3983         (progn                          ; Or we bug out.
3984           (when (equal major-mode 'gnus-summary-mode)
3985             (kill-buffer (current-buffer)))
3986           (error "Couldn't request group %s: %s"
3987                  group (gnus-status-message group))))
3988
3989     (unless (gnus-request-group group t)
3990       (when (equal major-mode 'gnus-summary-mode)
3991         (kill-buffer (current-buffer)))
3992       (error "Couldn't request group %s: %s"
3993              group (gnus-status-message group)))
3994
3995     (setq gnus-newsgroup-name group)
3996     (setq gnus-newsgroup-unselected nil)
3997     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3998     (gnus-summary-setup-default-charset)
3999     (gnus-summary-setup-highlight-words)
4000
4001     ;; Adjust and set lists of article marks.
4002     (when info
4003       (gnus-adjust-marked-articles info))
4004
4005     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4006     (setq cached
4007           (if (gnus-virtual-group-p group)
4008               gnus-newsgroup-cached
4009             (gnus-cache-articles-in-group group)))
4010
4011     (setq gnus-newsgroup-unreads
4012           (gnus-set-difference
4013            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4014            gnus-newsgroup-dormant))
4015
4016     (setq gnus-newsgroup-processable nil)
4017
4018     (gnus-update-read-articles group gnus-newsgroup-unreads)
4019
4020     (if (setq articles select-articles)
4021         (setq gnus-newsgroup-unselected
4022               (gnus-sorted-intersection
4023                gnus-newsgroup-unreads
4024                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4025       (setq articles (gnus-articles-to-read group read-all)))
4026
4027     (cond
4028      ((null articles)
4029       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4030       'quit)
4031      ((eq articles 0) nil)
4032      (t
4033       ;; Init the dependencies hash table.
4034       (setq gnus-newsgroup-dependencies
4035             (gnus-make-hashtable (length articles)))
4036       (gnus-set-global-variables)
4037       ;; Retrieve the headers and read them in.
4038       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4039       (setq gnus-newsgroup-headers
4040             (gnus-retrieve-parsed-headers
4041              articles gnus-newsgroup-name
4042              ;; We might want to fetch old headers, but
4043              ;; not if there is only 1 article.
4044              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4045                            (not (numberp gnus-fetch-old-headers)))
4046                       (> (length articles) 1))
4047                   gnus-fetch-old-headers)))
4048       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4049
4050       ;; Suppress duplicates?
4051       (when gnus-suppress-duplicates
4052         (gnus-dup-suppress-articles))
4053
4054       ;; Set the initial limit.
4055       (setq gnus-newsgroup-limit (copy-sequence articles))
4056       ;; Remove canceled articles from the list of unread articles.
4057       (setq gnus-newsgroup-unreads
4058             (gnus-set-sorted-intersection
4059              gnus-newsgroup-unreads
4060              (setq fetched-articles
4061                    (mapcar (lambda (headers) (mail-header-number headers))
4062                            gnus-newsgroup-headers))))
4063       ;; Removed marked articles that do not exist.
4064       (gnus-update-missing-marks
4065        (gnus-sorted-complement fetched-articles articles))
4066
4067       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4068       (when cached
4069         (setq gnus-newsgroup-cached cached))
4070
4071       ;; We might want to build some more threads first.
4072       (when (and gnus-fetch-old-headers
4073                  (eq gnus-headers-retrieved-by 'nov))
4074         (if (eq gnus-fetch-old-headers 'invisible)
4075             (gnus-build-all-threads)
4076           (gnus-build-old-threads)))
4077       ;; Let the Gnus agent mark articles as read.
4078       (when gnus-agent
4079         (gnus-agent-get-undownloaded-list))
4080       ;; Check whether auto-expire is to be done in this group.
4081       (setq gnus-newsgroup-auto-expire
4082             (gnus-group-auto-expirable-p group))
4083       ;; Set up the article buffer now, if necessary.
4084       (unless gnus-single-article-buffer
4085         (gnus-article-setup-buffer))
4086       ;; First and last article in this newsgroup.
4087       (when gnus-newsgroup-headers
4088         (setq gnus-newsgroup-begin
4089               (mail-header-number (car gnus-newsgroup-headers))
4090               gnus-newsgroup-end
4091               (mail-header-number
4092                (gnus-last-element gnus-newsgroup-headers))))
4093       ;; GROUP is successfully selected.
4094       (or gnus-newsgroup-headers t)))))
4095
4096 (defun gnus-articles-to-read (group &optional read-all)
4097   ;; Find out what articles the user wants to read.
4098   (let* ((articles
4099           ;; Select all articles if `read-all' is non-nil, or if there
4100           ;; are no unread articles.
4101           (if (or read-all
4102                   (and (zerop (length gnus-newsgroup-marked))
4103                        (zerop (length gnus-newsgroup-unreads)))
4104                   (eq (gnus-group-find-parameter group 'display)
4105                       'all))
4106               (gnus-uncompress-range (gnus-active group))
4107             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4108                           (copy-sequence gnus-newsgroup-unreads))
4109                   '<)))
4110          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4111          (scored (length scored-list))
4112          (number (length articles))
4113          (marked (+ (length gnus-newsgroup-marked)
4114                     (length gnus-newsgroup-dormant)))
4115          (select
4116           (cond
4117            ((numberp read-all)
4118             read-all)
4119            (t
4120             (condition-case ()
4121                 (cond
4122                  ((and (or (<= scored marked) (= scored number))
4123                        (natnump gnus-large-newsgroup)
4124                        (> number gnus-large-newsgroup))
4125                   (let* ((minibuffer-setup-hook (append
4126                                                  minibuffer-setup-hook
4127                                                  '(beginning-of-line)))
4128                          (input (read-string
4129                                  (format
4130                                   "How many articles from %s (max %d): "
4131                                   (gnus-limit-string gnus-newsgroup-name 35)
4132                                   number)
4133                                  (number-to-string gnus-large-newsgroup))))
4134                     (if (string-match "^[ \t]*$" input)
4135                         number
4136                       input)))
4137                  ((and (> scored marked) (< scored number)
4138                        (> (- scored number) 20))
4139                   (let ((input
4140                          (read-string
4141                           (format "%s %s (%d scored, %d total): "
4142                                   "How many articles from"
4143                                   group scored number))))
4144                     (if (string-match "^[ \t]*$" input)
4145                         number input)))
4146                  (t number))
4147               (quit nil))))))
4148     (setq select (if (stringp select) (string-to-number select) select))
4149     (if (or (null select) (zerop select))
4150         select
4151       (if (and (not (zerop scored)) (<= (abs select) scored))
4152           (progn
4153             (setq articles (sort scored-list '<))
4154             (setq number (length articles)))
4155         (setq articles (copy-sequence articles)))
4156
4157       (when (< (abs select) number)
4158         (if (< select 0)
4159             ;; Select the N oldest articles.
4160             (setcdr (nthcdr (1- (abs select)) articles) nil)
4161           ;; Select the N most recent articles.
4162           (setq articles (nthcdr (- number select) articles))))
4163       (setq gnus-newsgroup-unselected
4164             (gnus-sorted-intersection
4165              gnus-newsgroup-unreads
4166              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4167       articles)))
4168
4169 (defun gnus-killed-articles (killed articles)
4170   (let (out)
4171     (while articles
4172       (when (inline (gnus-member-of-range (car articles) killed))
4173         (push (car articles) out))
4174       (setq articles (cdr articles)))
4175     out))
4176
4177 (defun gnus-uncompress-marks (marks)
4178   "Uncompress the mark ranges in MARKS."
4179   (let ((uncompressed '(score bookmark))
4180         out)
4181     (while marks
4182       (if (memq (caar marks) uncompressed)
4183           (push (car marks) out)
4184         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4185       (setq marks (cdr marks)))
4186     out))
4187
4188 (defun gnus-adjust-marked-articles (info)
4189   "Set all article lists and remove all marks that are no longer valid."
4190   (let* ((marked-lists (gnus-info-marks info))
4191          (active (gnus-active (gnus-info-group info)))
4192          (min (car active))
4193          (max (cdr active))
4194          (types gnus-article-mark-lists)
4195          (uncompressed '(score bookmark killed))
4196          marks var articles article mark)
4197
4198     (while marked-lists
4199       (setq marks (pop marked-lists))
4200       (set (setq var (intern (format "gnus-newsgroup-%s"
4201                                      (car (rassq (setq mark (car marks))
4202                                                  types)))))
4203            (if (memq (car marks) uncompressed) (cdr marks)
4204              (gnus-uncompress-range (cdr marks))))
4205
4206       (setq articles (symbol-value var))
4207
4208       ;; All articles have to be subsets of the active articles.
4209       (cond
4210        ;; Adjust "simple" lists.
4211        ((memq mark '(tick dormant expire reply save))
4212         (while articles
4213           (when (or (< (setq article (pop articles)) min) (> article max))
4214             (set var (delq article (symbol-value var))))))
4215        ;; Adjust assocs.
4216        ((memq mark uncompressed)
4217         (when (not (listp (cdr (symbol-value var))))
4218           (set var (list (symbol-value var))))
4219         (when (not (listp (cdr articles)))
4220           (setq articles (list articles)))
4221         (while articles
4222           (when (or (not (consp (setq article (pop articles))))
4223                     (< (car article) min)
4224                     (> (car article) max))
4225             (set var (delq article (symbol-value var))))))))))
4226
4227 (defun gnus-update-missing-marks (missing)
4228   "Go through the list of MISSING articles and remove them from the mark lists."
4229   (when missing
4230     (let ((types gnus-article-mark-lists)
4231           var m)
4232       ;; Go through all types.
4233       (while types
4234         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4235         (when (symbol-value var)
4236           ;; This list has articles.  So we delete all missing articles
4237           ;; from it.
4238           (setq m missing)
4239           (while m
4240             (set var (delq (pop m) (symbol-value var)))))))))
4241
4242 (defun gnus-update-marks ()
4243   "Enter the various lists of marked articles into the newsgroup info list."
4244   (let ((types gnus-article-mark-lists)
4245         (info (gnus-get-info gnus-newsgroup-name))
4246         (uncompressed '(score bookmark killed))
4247         type list newmarked symbol delta-marks)
4248     (when info
4249       ;; Add all marks lists to the list of marks lists.
4250       (while (setq type (pop types))
4251         (setq list (symbol-value
4252                           (setq symbol
4253                                 (intern (format "gnus-newsgroup-%s"
4254                                                 (car type))))))
4255
4256         (when list
4257           ;; Get rid of the entries of the articles that have the
4258           ;; default score.
4259           (when (and (eq (cdr type) 'score)
4260                      gnus-save-score
4261                      list)
4262             (let* ((arts list)
4263                    (prev (cons nil list))
4264                    (all prev))
4265               (while arts
4266                 (if (or (not (consp (car arts)))
4267                         (= (cdar arts) gnus-summary-default-score))
4268                     (setcdr prev (cdr arts))
4269                   (setq prev arts))
4270                 (setq arts (cdr arts)))
4271               (setq list (cdr all)))))
4272
4273        (or (memq (cdr type) uncompressed)
4274            (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4275        
4276        (when (gnus-check-backend-function 'request-set-mark
4277                                           gnus-newsgroup-name)
4278          ;; uncompressed:s are not proper flags (they are cons cells)
4279          ;; cache is a internal gnus flag
4280          (unless (memq (cdr type) (cons 'cache uncompressed))
4281            (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4282                   (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4283                   (add (gnus-remove-from-range (gnus-copy-sequence list) old)))
4284              (if add
4285                  (push (list add 'add (list (cdr type))) delta-marks))
4286              (if del
4287                  (push (list del 'del (list (cdr type))) delta-marks)))))
4288           
4289         (when list
4290          (push (cons (cdr type) list) newmarked)))
4291
4292       (when delta-marks
4293         (unless (gnus-check-group gnus-newsgroup-name)
4294           (error "Can't open server for %s" gnus-newsgroup-name))
4295         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4296           
4297       ;; Enter these new marks into the info of the group.
4298       (if (nthcdr 3 info)
4299           (setcar (nthcdr 3 info) newmarked)
4300         ;; Add the marks lists to the end of the info.
4301         (when newmarked
4302           (setcdr (nthcdr 2 info) (list newmarked))))
4303
4304       ;; Cut off the end of the info if there's nothing else there.
4305       (let ((i 5))
4306         (while (and (> i 2)
4307                     (not (nth i info)))
4308           (when (nthcdr (decf i) info)
4309             (setcdr (nthcdr i info) nil)))))))
4310
4311 (defun gnus-set-mode-line (where)
4312   "This function sets the mode line of the article or summary buffers.
4313 If WHERE is `summary', the summary mode line format will be used."
4314   ;; Is this mode line one we keep updated?
4315   (when (and (memq where gnus-updated-mode-lines)
4316              (symbol-value
4317               (intern (format "gnus-%s-mode-line-format-spec" where))))
4318     (let (mode-string)
4319       (save-excursion
4320         ;; We evaluate this in the summary buffer since these
4321         ;; variables are buffer-local to that buffer.
4322         (set-buffer gnus-summary-buffer)
4323         ;; We bind all these variables that are used in the `eval' form
4324         ;; below.
4325         (let* ((mformat (symbol-value
4326                          (intern
4327                           (format "gnus-%s-mode-line-format-spec" where))))
4328                (gnus-tmp-group-name gnus-newsgroup-name)
4329                (gnus-tmp-article-number (or gnus-current-article 0))
4330                (gnus-tmp-unread gnus-newsgroup-unreads)
4331                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4332                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4333                (gnus-tmp-unread-and-unselected
4334                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4335                             (zerop gnus-tmp-unselected))
4336                        "")
4337                       ((zerop gnus-tmp-unselected)
4338                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4339                       (t (format "{%d(+%d) more}"
4340                                  gnus-tmp-unread-and-unticked
4341                                  gnus-tmp-unselected))))
4342                (gnus-tmp-subject
4343                 (if (and gnus-current-headers
4344                          (vectorp gnus-current-headers))
4345                     (gnus-mode-string-quote
4346                      (mail-header-subject gnus-current-headers))
4347                   ""))
4348                bufname-length max-len
4349                gnus-tmp-header);; passed as argument to any user-format-funcs
4350           (setq mode-string (eval mformat))
4351           (setq bufname-length (if (string-match "%b" mode-string)
4352                                    (- (length
4353                                        (buffer-name
4354                                         (if (eq where 'summary)
4355                                             nil
4356                                           (get-buffer gnus-article-buffer))))
4357                                       2)
4358                                  0))
4359           (setq max-len (max 4 (if gnus-mode-non-string-length
4360                                    (- (window-width)
4361                                       gnus-mode-non-string-length
4362                                       bufname-length)
4363                                  (length mode-string))))
4364           ;; We might have to chop a bit of the string off...
4365           (when (> (length mode-string) max-len)
4366             (setq mode-string
4367                   (concat (gnus-truncate-string mode-string (- max-len 3))
4368                           "...")))
4369           ;; Pad the mode string a bit.
4370           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4371       ;; Update the mode line.
4372       (setq mode-line-buffer-identification
4373             (gnus-mode-line-buffer-identification (list mode-string)))
4374       (set-buffer-modified-p t))))
4375
4376 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4377   "Go through the HEADERS list and add all Xrefs to a hash table.
4378 The resulting hash table is returned, or nil if no Xrefs were found."
4379   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4380          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4381          (xref-hashtb (gnus-make-hashtable))
4382          start group entry number xrefs header)
4383     (while headers
4384       (setq header (pop headers))
4385       (when (and (setq xrefs (mail-header-xref header))
4386                  (not (memq (setq number (mail-header-number header))
4387                             unreads)))
4388         (setq start 0)
4389         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4390           (setq start (match-end 0))
4391           (setq group (if prefix
4392                           (concat prefix (substring xrefs (match-beginning 1)
4393                                                     (match-end 1)))
4394                         (substring xrefs (match-beginning 1) (match-end 1))))
4395           (setq number
4396                 (string-to-int (substring xrefs (match-beginning 2)
4397                                           (match-end 2))))
4398           (if (setq entry (gnus-gethash group xref-hashtb))
4399               (setcdr entry (cons number (cdr entry)))
4400             (gnus-sethash group (cons number nil) xref-hashtb)))))
4401     (and start xref-hashtb)))
4402
4403 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4404   "Look through all the headers and mark the Xrefs as read."
4405   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4406         name entry info xref-hashtb idlist method nth4)
4407     (save-excursion
4408       (set-buffer gnus-group-buffer)
4409       (when (setq xref-hashtb
4410                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4411         (mapatoms
4412          (lambda (group)
4413            (unless (string= from-newsgroup (setq name (symbol-name group)))
4414              (setq idlist (symbol-value group))
4415              ;; Dead groups are not updated.
4416              (and (prog1
4417                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4418                             info (nth 2 entry))
4419                     (when (stringp (setq nth4 (gnus-info-method info)))
4420                       (setq nth4 (gnus-server-to-method nth4))))
4421                   ;; Only do the xrefs if the group has the same
4422                   ;; select method as the group we have just read.
4423                   (or (gnus-methods-equal-p
4424                        nth4 (gnus-find-method-for-group from-newsgroup))
4425                       virtual
4426                       (equal nth4 (setq method (gnus-find-method-for-group
4427                                                 from-newsgroup)))
4428                       (and (equal (car nth4) (car method))
4429                            (equal (nth 1 nth4) (nth 1 method))))
4430                   gnus-use-cross-reference
4431                   (or (not (eq gnus-use-cross-reference t))
4432                       virtual
4433                       ;; Only do cross-references on subscribed
4434                       ;; groups, if that is what is wanted.
4435                       (<= (gnus-info-level info) gnus-level-subscribed))
4436                   (gnus-group-make-articles-read name idlist))))
4437          xref-hashtb)))))
4438
4439 (defun gnus-compute-read-articles (group articles)
4440   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4441          (info (nth 2 entry))
4442          (active (gnus-active group))
4443          ninfo)
4444     (when entry
4445       ;; First peel off all invalid article numbers.
4446       (when active
4447         (let ((ids articles)
4448               id first)
4449           (while (setq id (pop ids))
4450             (when (and first (> id (cdr active)))
4451               ;; We'll end up in this situation in one particular
4452               ;; obscure situation.  If you re-scan a group and get
4453               ;; a new article that is cross-posted to a different
4454               ;; group that has not been re-scanned, you might get
4455               ;; crossposted article that has a higher number than
4456               ;; Gnus believes possible.  So we re-activate this
4457               ;; group as well.  This might mean doing the
4458               ;; crossposting thingy will *increase* the number
4459               ;; of articles in some groups.  Tsk, tsk.
4460               (setq active (or (gnus-activate-group group) active)))
4461             (when (or (> id (cdr active))
4462                       (< id (car active)))
4463               (setq articles (delq id articles))))))
4464       ;; If the read list is nil, we init it.
4465       (if (and active
4466                (null (gnus-info-read info))
4467                (> (car active) 1))
4468           (setq ninfo (cons 1 (1- (car active))))
4469         (setq ninfo (gnus-info-read info)))
4470       ;; Then we add the read articles to the range.
4471       (gnus-add-to-range
4472        ninfo (setq articles (sort articles '<))))))
4473
4474 (defun gnus-group-make-articles-read (group articles)
4475   "Update the info of GROUP to say that ARTICLES are read."
4476   (let* ((num 0)
4477          (entry (gnus-gethash group gnus-newsrc-hashtb))
4478          (info (nth 2 entry))
4479          (active (gnus-active group))
4480          range)
4481     (when entry
4482       (setq range (gnus-compute-read-articles group articles))
4483       (save-excursion
4484         (set-buffer gnus-group-buffer)
4485         (gnus-undo-register
4486           `(progn
4487              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4488              (gnus-info-set-read ',info ',(gnus-info-read info))
4489              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4490              (gnus-group-update-group ,group t))))
4491       ;; Add the read articles to the range.
4492       (gnus-info-set-read info range)
4493       ;; Then we have to re-compute how many unread
4494       ;; articles there are in this group.
4495       (when active
4496         (cond
4497          ((not range)
4498           (setq num (- (1+ (cdr active)) (car active))))
4499          ((not (listp (cdr range)))
4500           (setq num (- (cdr active) (- (1+ (cdr range))
4501                                        (car range)))))
4502          (t
4503           (while range
4504             (if (numberp (car range))
4505                 (setq num (1+ num))
4506               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4507             (setq range (cdr range)))
4508           (setq num (- (cdr active) num))))
4509         ;; Update the number of unread articles.
4510         (setcar entry num)
4511         ;; Update the group buffer.
4512         (gnus-group-update-group group t)))))
4513
4514 (defvar gnus-newsgroup-none-id 0)
4515
4516 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4517   (let ((cur nntp-server-buffer)
4518         (dependencies
4519          (or dependencies
4520              (save-excursion (set-buffer gnus-summary-buffer)
4521                              gnus-newsgroup-dependencies)))
4522         headers id end ref
4523         (mail-parse-charset gnus-newsgroup-charset))
4524     (save-excursion
4525       (set-buffer nntp-server-buffer)
4526       ;; Translate all TAB characters into SPACE characters.
4527       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4528       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4529       (gnus-run-hooks 'gnus-parse-headers-hook)
4530       (let ((case-fold-search t)
4531             in-reply-to header p lines chars ctype)
4532         (goto-char (point-min))
4533         ;; Search to the beginning of the next header.  Error messages
4534         ;; do not begin with 2 or 3.
4535         (while (re-search-forward "^[23][0-9]+ " nil t)
4536           (setq id nil
4537                 ref nil)
4538           ;; This implementation of this function, with nine
4539           ;; search-forwards instead of the one re-search-forward and
4540           ;; a case (which basically was the old function) is actually
4541           ;; about twice as fast, even though it looks messier.  You
4542           ;; can't have everything, I guess.  Speed and elegance
4543           ;; doesn't always go hand in hand.
4544           (setq
4545            header
4546            (make-full-mail-header
4547             ;; Number.
4548             (prog1
4549                 (read cur)
4550               (end-of-line)
4551               (setq p (point))
4552               (narrow-to-region (point)
4553                                 (or (and (search-forward "\n.\n" nil t)
4554                                          (- (point) 2))
4555                                     (point))))
4556             ;; Subject.
4557             (progn
4558               (goto-char p)
4559               (if (search-forward "\nsubject: " nil t)
4560                   (buffer-substring (match-end 0) (std11-field-end))
4561                 "(none)"))
4562             ;; From.
4563             (progn
4564               (goto-char p)
4565               (if (search-forward "\nfrom: " nil t)
4566                   (buffer-substring (match-end 0) (std11-field-end))
4567                 "(nobody)"))
4568             ;; Date.
4569             (progn
4570               (goto-char p)
4571               (if (search-forward "\ndate: " nil t)
4572                   (buffer-substring (match-end 0) (std11-field-end))
4573                 ""))
4574             ;; Message-ID.
4575             (progn
4576               (goto-char p)
4577               (setq id (if (re-search-forward
4578                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4579                            ;; We do it this way to make sure the Message-ID
4580                            ;; is (somewhat) syntactically valid.
4581                            (buffer-substring (match-beginning 1)
4582                                              (match-end 1))
4583                          ;; If there was no message-id, we just fake one
4584                          ;; to make subsequent routines simpler.
4585                          (nnheader-generate-fake-message-id))))
4586             ;; References.
4587             (progn
4588               (goto-char p)
4589               (if (search-forward "\nreferences: " nil t)
4590                   (progn
4591                     (setq end (point))
4592                     (prog1
4593                         (buffer-substring (match-end 0) (std11-field-end))
4594                       (setq ref
4595                             (buffer-substring
4596                              (progn
4597                                ;; (end-of-line)
4598                                (search-backward ">" end t)
4599                                (1+ (point)))
4600                              (progn
4601                                (search-backward "<" end t)
4602                                (point))))))
4603                 ;; Get the references from the in-reply-to header if there
4604                 ;; were no references and the in-reply-to header looks
4605                 ;; promising.
4606                 (if (and (search-forward "\nin-reply-to: " nil t)
4607                          (setq in-reply-to
4608                                (buffer-substring (match-end 0)
4609                                                  (std11-field-end)))
4610                          (string-match "<[^>]+>" in-reply-to))
4611                     (let (ref2)
4612                       (setq ref (substring in-reply-to (match-beginning 0)
4613                                            (match-end 0)))
4614                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4615                         (setq ref2 (substring in-reply-to (match-beginning 0)
4616                                               (match-end 0)))
4617                         (when (> (length ref2) (length ref))
4618                           (setq ref ref2)))
4619                       ref)
4620                   (setq ref nil))))
4621             ;; Chars.
4622             (progn
4623               (goto-char p)
4624               (if (search-forward "\nchars: " nil t)
4625                   (if (numberp (setq chars (ignore-errors (read cur))))
4626                       chars 0)
4627                 0))
4628             ;; Lines.
4629             (progn
4630               (goto-char p)
4631               (if (search-forward "\nlines: " nil t)
4632                   (if (numberp (setq lines (ignore-errors (read cur))))
4633                       lines 0)
4634                 0))
4635             ;; Xref.
4636             (progn
4637               (goto-char p)
4638               (and (search-forward "\nxref: " nil t)
4639                    (buffer-substring (match-end 0) (std11-field-end))))
4640             ;; Extra.
4641             (when gnus-extra-headers
4642               (let ((extra gnus-extra-headers)
4643                     out)
4644                 (while extra
4645                   (goto-char p)
4646                   (when (search-forward
4647                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4648                     (push (cons (car extra)
4649                                 (buffer-substring (match-end 0)
4650                                                   (std11-field-end)))
4651                           out))
4652                   (pop extra))
4653                 out))))
4654           (goto-char p)
4655           (if (and (search-forward "\ncontent-type: " nil t)
4656                    (setq ctype
4657                          (buffer-substring (match-end 0) (std11-field-end))))
4658               (mime-entity-set-content-type-internal
4659                header (mime-parse-Content-Type ctype)))
4660           (when (equal id ref)
4661             (setq ref nil))
4662
4663           (when gnus-alter-header-function
4664             (funcall gnus-alter-header-function header)
4665             (setq id (mail-header-id header)
4666                   ref (gnus-parent-id (mail-header-references header))))
4667
4668           (when (setq header
4669                       (gnus-dependencies-add-header
4670                        header dependencies force-new))
4671             (push header headers))
4672           (goto-char (point-max))
4673           (widen))
4674         (nreverse headers)))))
4675
4676 ;; Goes through the xover lines and returns a list of vectors
4677 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4678                                                   force-new dependencies
4679                                                   group also-fetch-heads)
4680   "Parse the news overview data in the server buffer, and return a
4681 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4682   ;; Get the Xref when the users reads the articles since most/some
4683   ;; NNTP servers do not include Xrefs when using XOVER.
4684   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4685   (let ((mail-parse-charset gnus-newsgroup-charset)
4686         (cur nntp-server-buffer)
4687         (dependencies (or dependencies gnus-newsgroup-dependencies))
4688         number headers header)
4689     (save-excursion
4690       (set-buffer nntp-server-buffer)
4691       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4692       ;; Allow the user to mangle the headers before parsing them.
4693       (gnus-run-hooks 'gnus-parse-headers-hook)
4694       (goto-char (point-min))
4695       (while (not (eobp))
4696         (condition-case ()
4697             (while (and sequence (not (eobp)))
4698               (setq number (read cur))
4699               (while (and sequence
4700                           (< (car sequence) number))
4701                 (setq sequence (cdr sequence)))
4702               (and sequence
4703                    (eq number (car sequence))
4704                    (progn
4705                      (setq sequence (cdr sequence))
4706                      (setq header (inline
4707                                     (gnus-nov-parse-line
4708                                      number dependencies force-new))))
4709                    (push header headers))
4710               (forward-line 1))
4711           (error
4712            (gnus-error 4 "Strange nov line (%d)"
4713                        (count-lines (point-min) (point)))))
4714         (forward-line 1))
4715       ;; A common bug in inn is that if you have posted an article and
4716       ;; then retrieves the active file, it will answer correctly --
4717       ;; the new article is included.  However, a NOV entry for the
4718       ;; article may not have been generated yet, so this may fail.
4719       ;; We work around this problem by retrieving the last few
4720       ;; headers using HEAD.
4721       (if (or (not also-fetch-heads)
4722               (not sequence))
4723           ;; We (probably) got all the headers.
4724           (nreverse headers)
4725         (let ((gnus-nov-is-evil t))
4726           (nconc
4727            (nreverse headers)
4728            (gnus-retrieve-parsed-headers sequence group)
4729            ))))))
4730
4731 (defun gnus-article-get-xrefs ()
4732   "Fill in the Xref value in `gnus-current-headers', if necessary.
4733 This is meant to be called in `gnus-article-internal-prepare-hook'."
4734   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4735                                  gnus-current-headers)))
4736     (or (not gnus-use-cross-reference)
4737         (not headers)
4738         (and (mail-header-xref headers)
4739              (not (string= (mail-header-xref headers) "")))
4740         (let ((case-fold-search t)
4741               xref)
4742           (save-restriction
4743             (nnheader-narrow-to-headers)
4744             (goto-char (point-min))
4745             (when (or (and (eq (downcase (char-after)) ?x)
4746                            (looking-at "Xref:"))
4747                       (search-forward "\nXref:" nil t))
4748               (goto-char (1+ (match-end 0)))
4749               (setq xref (buffer-substring (point)
4750                                            (progn (end-of-line) (point))))
4751               (mail-header-set-xref headers xref)))))))
4752
4753 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4754   "Find article ID and insert the summary line for that article.
4755 OLD-HEADER can either be a header or a line number to insert
4756 the subject line on."
4757   (let* ((line (and (numberp old-header) old-header))
4758          (old-header (and (vectorp old-header) old-header))
4759          (header (cond ((and old-header use-old-header)
4760                         old-header)
4761                        ((and (numberp id)
4762                              (gnus-number-to-header id))
4763                         (gnus-number-to-header id))
4764                        (t
4765                         (gnus-read-header id))))
4766          (number (and (numberp id) id))
4767          d)
4768     (when header
4769       ;; Rebuild the thread that this article is part of and go to the
4770       ;; article we have fetched.
4771       (when (and (not gnus-show-threads)
4772                  old-header)
4773         (when (and number
4774                    (setq d (gnus-data-find (mail-header-number old-header))))
4775           (goto-char (gnus-data-pos d))
4776           (gnus-data-remove
4777            number
4778            (- (gnus-point-at-bol)
4779               (prog1
4780                   (1+ (gnus-point-at-eol))
4781                 (gnus-delete-line))))))
4782       (when old-header
4783         (mail-header-set-number header (mail-header-number old-header)))
4784       (setq gnus-newsgroup-sparse
4785             (delq (setq number (mail-header-number header))
4786                   gnus-newsgroup-sparse))
4787       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4788       (push number gnus-newsgroup-limit)
4789       (gnus-rebuild-thread (mail-header-id header) line)
4790       (gnus-summary-goto-subject number nil t))
4791     (when (and (numberp number)
4792                (> number 0))
4793       ;; We have to update the boundaries even if we can't fetch the
4794       ;; article if ID is a number -- so that the next `P' or `N'
4795       ;; command will fetch the previous (or next) article even
4796       ;; if the one we tried to fetch this time has been canceled.
4797       (when (> number gnus-newsgroup-end)
4798         (setq gnus-newsgroup-end number))
4799       (when (< number gnus-newsgroup-begin)
4800         (setq gnus-newsgroup-begin number))
4801       (setq gnus-newsgroup-unselected
4802             (delq number gnus-newsgroup-unselected)))
4803     ;; Report back a success?
4804     (and header (mail-header-number header))))
4805
4806 ;;; Process/prefix in the summary buffer
4807
4808 (defun gnus-summary-work-articles (n)
4809   "Return a list of articles to be worked upon.
4810 The prefix argument, the list of process marked articles, and the
4811 current article will be taken into consideration."
4812   (save-excursion
4813     (set-buffer gnus-summary-buffer)
4814     (cond
4815      (n
4816       ;; A numerical prefix has been given.
4817       (setq n (prefix-numeric-value n))
4818       (let ((backward (< n 0))
4819             (n (abs (prefix-numeric-value n)))
4820             articles article)
4821         (save-excursion
4822           (while
4823               (and (> n 0)
4824                    (push (setq article (gnus-summary-article-number))
4825                          articles)
4826                    (if backward
4827                        (gnus-summary-find-prev nil article)
4828                      (gnus-summary-find-next nil article)))
4829             (decf n)))
4830         (nreverse articles)))
4831      ((and (gnus-region-active-p) (mark))
4832       (message "region active")
4833       ;; Work on the region between point and mark.
4834       (let ((max (max (point) (mark)))
4835             articles article)
4836         (save-excursion
4837           (goto-char (min (point) (mark)))
4838           (while
4839               (and
4840                (push (setq article (gnus-summary-article-number)) articles)
4841                (gnus-summary-find-next nil article)
4842                (< (point) max)))
4843           (nreverse articles))))
4844      (gnus-newsgroup-processable
4845       ;; There are process-marked articles present.
4846       ;; Save current state.
4847       (gnus-summary-save-process-mark)
4848       ;; Return the list.
4849       (reverse gnus-newsgroup-processable))
4850      (t
4851       ;; Just return the current article.
4852       (list (gnus-summary-article-number))))))
4853
4854 (defmacro gnus-summary-iterate (arg &rest forms)
4855   "Iterate over the process/prefixed articles and do FORMS.
4856 ARG is the interactive prefix given to the command.  FORMS will be
4857 executed with point over the summary line of the articles."
4858   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
4859     `(let ((,articles (gnus-summary-work-articles ,arg)))
4860        (while ,articles
4861          (gnus-summary-goto-subject (car ,articles))
4862          ,@forms
4863          (pop ,articles)))))
4864
4865 (put 'gnus-summary-iterate 'lisp-indent-function 1)
4866 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
4867
4868 (defun gnus-summary-save-process-mark ()
4869   "Push the current set of process marked articles on the stack."
4870   (interactive)
4871   (push (copy-sequence gnus-newsgroup-processable)
4872         gnus-newsgroup-process-stack))
4873
4874 (defun gnus-summary-kill-process-mark ()
4875   "Push the current set of process marked articles on the stack and unmark."
4876   (interactive)
4877   (gnus-summary-save-process-mark)
4878   (gnus-summary-unmark-all-processable))
4879
4880 (defun gnus-summary-yank-process-mark ()
4881   "Pop the last process mark state off the stack and restore it."
4882   (interactive)
4883   (unless gnus-newsgroup-process-stack
4884     (error "Empty mark stack"))
4885   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4886
4887 (defun gnus-summary-process-mark-set (set)
4888   "Make SET into the current process marked articles."
4889   (gnus-summary-unmark-all-processable)
4890   (while set
4891     (gnus-summary-set-process-mark (pop set))))
4892
4893 ;;; Searching and stuff
4894
4895 (defun gnus-summary-search-group (&optional backward use-level)
4896   "Search for next unread newsgroup.
4897 If optional argument BACKWARD is non-nil, search backward instead."
4898   (save-excursion
4899     (set-buffer gnus-group-buffer)
4900     (when (gnus-group-search-forward
4901            backward nil (if use-level (gnus-group-group-level) nil))
4902       (gnus-group-group-name))))
4903
4904 (defun gnus-summary-best-group (&optional exclude-group)
4905   "Find the name of the best unread group.
4906 If EXCLUDE-GROUP, do not go to this group."
4907   (save-excursion
4908     (set-buffer gnus-group-buffer)
4909     (save-excursion
4910       (gnus-group-best-unread-group exclude-group))))
4911
4912 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4913   (if backward (gnus-summary-find-prev)
4914     (let* ((dummy (gnus-summary-article-intangible-p))
4915            (article (or article (gnus-summary-article-number)))
4916            (arts (gnus-data-find-list article))
4917            result)
4918       (when (and (not dummy)
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 (or (and undownloaded
4928                                          (eq gnus-undownloaded-mark
4929                                              (gnus-data-mark (car arts))))
4930                                     (gnus-data-unread-p (car arts)))
4931                             (setq result (car arts)
4932                                   arts nil))
4933                           (setq arts (cdr arts)))
4934                         result)
4935                     (car arts)))
4936         (goto-char (gnus-data-pos result))
4937         (gnus-data-number result)))))
4938
4939 (defun gnus-summary-find-prev (&optional unread article)
4940   (let* ((eobp (eobp))
4941          (article (or article (gnus-summary-article-number)))
4942          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4943          result)
4944     (when (and (not eobp)
4945                (or (not gnus-summary-check-current)
4946                    (not unread)
4947                    (not (gnus-data-unread-p (car arts)))))
4948       (setq arts (cdr arts)))
4949     (when (setq result
4950                 (if unread
4951                     (progn
4952                       (while arts
4953                         (when (gnus-data-unread-p (car arts))
4954                           (setq result (car arts)
4955                                 arts nil))
4956                         (setq arts (cdr arts)))
4957                       result)
4958                   (car arts)))
4959       (goto-char (gnus-data-pos result))
4960       (gnus-data-number result))))
4961
4962 (defun gnus-summary-find-subject (subject &optional unread backward article)
4963   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4964          (article (or article (gnus-summary-article-number)))
4965          (articles (gnus-data-list backward))
4966          (arts (gnus-data-find-list article articles))
4967          result)
4968     (when (or (not gnus-summary-check-current)
4969               (not unread)
4970               (not (gnus-data-unread-p (car arts))))
4971       (setq arts (cdr arts)))
4972     (while arts
4973       (and (or (not unread)
4974                (gnus-data-unread-p (car arts)))
4975            (vectorp (gnus-data-header (car arts)))
4976            (gnus-subject-equal
4977             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4978            (setq result (car arts)
4979                  arts nil))
4980       (setq arts (cdr arts)))
4981     (and result
4982          (goto-char (gnus-data-pos result))
4983          (gnus-data-number result))))
4984
4985 (defun gnus-summary-search-forward (&optional unread subject backward)
4986   "Search forward for an article.
4987 If UNREAD, look for unread articles.  If SUBJECT, look for
4988 articles with that subject.  If BACKWARD, search backward instead."
4989   (cond (subject (gnus-summary-find-subject subject unread backward))
4990         (backward (gnus-summary-find-prev unread))
4991         (t (gnus-summary-find-next unread))))
4992
4993 (defun gnus-recenter (&optional n)
4994   "Center point in window and redisplay frame.
4995 Also do horizontal recentering."
4996   (interactive "P")
4997   (when (and gnus-auto-center-summary
4998              (not (eq gnus-auto-center-summary 'vertical)))
4999     (gnus-horizontal-recenter))
5000   (recenter n))
5001
5002 (defun gnus-summary-recenter ()
5003   "Center point in the summary window.
5004 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5005 displayed, no centering will be performed."
5006   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5007   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5008   (let* ((top (cond ((< (window-height) 4) 0)
5009                     ((< (window-height) 7) 1)
5010                     (t (if (numberp gnus-auto-center-summary)
5011                            gnus-auto-center-summary
5012                          2))))
5013          (height (1- (window-height)))
5014          (bottom (save-excursion (goto-char (point-max))
5015                                  (forward-line (- height))
5016                                  (point)))
5017          (window (get-buffer-window (current-buffer))))
5018     ;; The user has to want it.
5019     (when gnus-auto-center-summary
5020       (when (get-buffer-window gnus-article-buffer)
5021         ;; Only do recentering when the article buffer is displayed,
5022         ;; Set the window start to either `bottom', which is the biggest
5023         ;; possible valid number, or the second line from the top,
5024         ;; whichever is the least.
5025         (set-window-start
5026          window (min bottom (save-excursion
5027                               (forward-line (- top)) (point)))))
5028       ;; Do horizontal recentering while we're at it.
5029       (when (and (get-buffer-window (current-buffer) t)
5030                  (not (eq gnus-auto-center-summary 'vertical)))
5031         (let ((selected (selected-window)))
5032           (select-window (get-buffer-window (current-buffer) t))
5033           (gnus-summary-position-point)
5034           (gnus-horizontal-recenter)
5035           (select-window selected))))))
5036
5037 (defun gnus-summary-jump-to-group (newsgroup)
5038   "Move point to NEWSGROUP in group mode buffer."
5039   ;; Keep update point of group mode buffer if visible.
5040   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5041       (save-window-excursion
5042         ;; Take care of tree window mode.
5043         (when (get-buffer-window gnus-group-buffer)
5044           (pop-to-buffer gnus-group-buffer))
5045         (gnus-group-jump-to-group newsgroup))
5046     (save-excursion
5047       ;; Take care of tree window mode.
5048       (if (get-buffer-window gnus-group-buffer)
5049           (pop-to-buffer gnus-group-buffer)
5050         (set-buffer gnus-group-buffer))
5051       (gnus-group-jump-to-group newsgroup))))
5052
5053 ;; This function returns a list of article numbers based on the
5054 ;; difference between the ranges of read articles in this group and
5055 ;; the range of active articles.
5056 (defun gnus-list-of-unread-articles (group)
5057   (let* ((read (gnus-info-read (gnus-get-info group)))
5058          (active (or (gnus-active group) (gnus-activate-group group)))
5059          (last (cdr active))
5060          first nlast unread)
5061     ;; If none are read, then all are unread.
5062     (if (not read)
5063         (setq first (car active))
5064       ;; If the range of read articles is a single range, then the
5065       ;; first unread article is the article after the last read
5066       ;; article.  Sounds logical, doesn't it?
5067       (if (not (listp (cdr read)))
5068           (setq first (max (car active) (1+ (cdr read))))
5069         ;; `read' is a list of ranges.
5070         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5071                                   (caar read)))
5072                   1)
5073           (setq first (car active)))
5074         (while read
5075           (when first
5076             (while (< first nlast)
5077               (push first unread)
5078               (setq first (1+ first))))
5079           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5080           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5081           (setq read (cdr read)))))
5082     ;; And add the last unread articles.
5083     (while (<= first last)
5084       (push first unread)
5085       (setq first (1+ first)))
5086     ;; Return the list of unread articles.
5087     (delq 0 (nreverse unread))))
5088
5089 (defun gnus-list-of-read-articles (group)
5090   "Return a list of unread, unticked and non-dormant articles."
5091   (let* ((info (gnus-get-info group))
5092          (marked (gnus-info-marks info))
5093          (active (gnus-active group)))
5094     (and info active
5095          (gnus-set-difference
5096           (gnus-sorted-complement
5097            (gnus-uncompress-range active)
5098            (gnus-list-of-unread-articles group))
5099           (append
5100            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5101            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5102
5103 ;; Various summary commands
5104
5105 (defun gnus-summary-select-article-buffer ()
5106   "Reconfigure windows to show article buffer."
5107   (interactive)
5108   (if (not (gnus-buffer-live-p gnus-article-buffer))
5109       (error "There is no article buffer for this summary buffer")
5110     (gnus-configure-windows 'article)
5111     (select-window (get-buffer-window gnus-article-buffer))))
5112
5113 (defun gnus-summary-universal-argument (arg)
5114   "Perform any operation on all articles that are process/prefixed."
5115   (interactive "P")
5116   (let ((articles (gnus-summary-work-articles arg))
5117         func article)
5118     (if (eq
5119          (setq
5120           func
5121           (key-binding
5122            (read-key-sequence
5123             (substitute-command-keys
5124              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
5125              ))))
5126          'undefined)
5127         (gnus-error 1 "Undefined key")
5128       (save-excursion
5129         (while articles
5130           (gnus-summary-goto-subject (setq article (pop articles)))
5131           (let (gnus-newsgroup-processable)
5132             (command-execute func))
5133           (gnus-summary-remove-process-mark article)))))
5134   (gnus-summary-position-point))
5135
5136 (defun gnus-summary-toggle-truncation (&optional arg)
5137   "Toggle truncation of summary lines.
5138 With arg, turn line truncation on iff arg is positive."
5139   (interactive "P")
5140   (setq truncate-lines
5141         (if (null arg) (not truncate-lines)
5142           (> (prefix-numeric-value arg) 0)))
5143   (redraw-display))
5144
5145 (defun gnus-summary-reselect-current-group (&optional all rescan)
5146   "Exit and then reselect the current newsgroup.
5147 The prefix argument ALL means to select all articles."
5148   (interactive "P")
5149   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5150     (error "Ephemeral groups can't be reselected"))
5151   (let ((current-subject (gnus-summary-article-number))
5152         (group gnus-newsgroup-name))
5153     (setq gnus-newsgroup-begin nil)
5154     (gnus-summary-exit)
5155     ;; We have to adjust the point of group mode buffer because
5156     ;; point was moved to the next unread newsgroup by exiting.
5157     (gnus-summary-jump-to-group group)
5158     (when rescan
5159       (save-excursion
5160         (gnus-group-get-new-news-this-group 1)))
5161     (gnus-group-read-group all t)
5162     (gnus-summary-goto-subject current-subject nil t)))
5163
5164 (defun gnus-summary-rescan-group (&optional all)
5165   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5166   (interactive "P")
5167   (gnus-summary-reselect-current-group all t))
5168
5169 (defun gnus-summary-update-info (&optional non-destructive)
5170   (save-excursion
5171     (let ((group gnus-newsgroup-name))
5172       (when group
5173         (when gnus-newsgroup-kill-headers
5174           (setq gnus-newsgroup-killed
5175                 (gnus-compress-sequence
5176                  (nconc
5177                   (gnus-set-sorted-intersection
5178                    (gnus-uncompress-range gnus-newsgroup-killed)
5179                    (setq gnus-newsgroup-unselected
5180                          (sort gnus-newsgroup-unselected '<)))
5181                   (setq gnus-newsgroup-unreads
5182                         (sort gnus-newsgroup-unreads '<)))
5183                  t)))
5184         (unless (listp (cdr gnus-newsgroup-killed))
5185           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5186         (let ((headers gnus-newsgroup-headers))
5187           ;; Set the new ranges of read articles.
5188           (save-excursion
5189             (set-buffer gnus-group-buffer)
5190             (gnus-undo-force-boundary))
5191           (gnus-update-read-articles
5192            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5193           ;; Set the current article marks.
5194           (let ((gnus-newsgroup-scored
5195                  (if (and (not gnus-save-score)
5196                           (not non-destructive))
5197                      nil
5198                    gnus-newsgroup-scored)))
5199             (save-excursion
5200               (gnus-update-marks)))
5201           ;; Do the cross-ref thing.
5202           (when gnus-use-cross-reference
5203             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5204           ;; Do not switch windows but change the buffer to work.
5205           (set-buffer gnus-group-buffer)
5206           (unless (gnus-ephemeral-group-p group)
5207             (gnus-group-update-group group)))))))
5208
5209 (defun gnus-summary-save-newsrc (&optional force)
5210   "Save the current number of read/marked articles in the dribble buffer.
5211 The dribble buffer will then be saved.
5212 If FORCE (the prefix), also save the .newsrc file(s)."
5213   (interactive "P")
5214   (gnus-summary-update-info t)
5215   (if force
5216       (gnus-save-newsrc-file)
5217     (gnus-dribble-save)))
5218
5219 (defun gnus-summary-exit (&optional temporary)
5220   "Exit reading current newsgroup, and then return to group selection mode.
5221 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5222   (interactive)
5223   (gnus-set-global-variables)
5224   (gnus-kill-save-kill-buffer)
5225   (gnus-async-halt-prefetch)
5226   (let* ((group gnus-newsgroup-name)
5227          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5228          (mode major-mode)
5229          (group-point nil)
5230          (buf (current-buffer)))
5231     (unless quit-config
5232       ;; Do adaptive scoring, and possibly save score files.
5233       (when gnus-newsgroup-adaptive
5234         (gnus-score-adaptive))
5235       (when gnus-use-scoring
5236         (gnus-score-save)))
5237     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5238     ;; If we have several article buffers, we kill them at exit.
5239     (unless gnus-single-article-buffer
5240       (gnus-kill-buffer gnus-original-article-buffer)
5241       (setq gnus-article-current nil))
5242     (when gnus-use-cache
5243       (gnus-cache-possibly-remove-articles)
5244       (gnus-cache-save-buffers))
5245     (gnus-async-prefetch-remove-group group)
5246     (when gnus-suppress-duplicates
5247       (gnus-dup-enter-articles))
5248     (when gnus-use-trees
5249       (gnus-tree-close group))
5250     ;; Remove entries for this group.
5251     (nnmail-purge-split-history (gnus-group-real-name group))
5252     ;; Make all changes in this group permanent.
5253     (unless quit-config
5254       (gnus-run-hooks 'gnus-exit-group-hook)
5255       (gnus-summary-update-info))
5256     (gnus-close-group group)
5257     ;; Make sure where we were, and go to next newsgroup.
5258     (set-buffer gnus-group-buffer)
5259     (unless quit-config
5260       (gnus-group-jump-to-group group))
5261     (gnus-run-hooks 'gnus-summary-exit-hook)
5262     (unless (or quit-config
5263                 ;; If this group has disappeared from the summary
5264                 ;; buffer, don't skip forwards.
5265                 (not (string= group (gnus-group-group-name))))
5266       (gnus-group-next-unread-group 1))
5267     (setq group-point (point))
5268     (if temporary
5269         nil                             ;Nothing to do.
5270       ;; If we have several article buffers, we kill them at exit.
5271       (unless gnus-single-article-buffer
5272         (gnus-kill-buffer gnus-article-buffer)
5273         (gnus-kill-buffer gnus-original-article-buffer)
5274         (setq gnus-article-current nil))
5275       (set-buffer buf)
5276       (if (not gnus-kill-summary-on-exit)
5277           (gnus-deaden-summary)
5278         ;; We set all buffer-local variables to nil.  It is unclear why
5279         ;; this is needed, but if we don't, buffer-local variables are
5280         ;; not garbage-collected, it seems.  This would the lead to en
5281         ;; ever-growing Emacs.
5282         (gnus-summary-clear-local-variables)
5283         (when (get-buffer gnus-article-buffer)
5284           (bury-buffer gnus-article-buffer))
5285         ;; We clear the global counterparts of the buffer-local
5286         ;; variables as well, just to be on the safe side.
5287         (set-buffer gnus-group-buffer)
5288         (gnus-summary-clear-local-variables)
5289         ;; Return to group mode buffer.
5290         (when (eq mode 'gnus-summary-mode)
5291           (gnus-kill-buffer buf)))
5292       (setq gnus-current-select-method gnus-select-method)
5293       (pop-to-buffer gnus-group-buffer)
5294       (if (not quit-config)
5295           (progn
5296             (goto-char group-point)
5297             (gnus-configure-windows 'group 'force))
5298         (gnus-handle-ephemeral-exit quit-config))
5299       ;; Clear the current group name.
5300       (unless quit-config
5301         (setq gnus-newsgroup-name nil)))))
5302
5303 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5304 (defun gnus-summary-exit-no-update (&optional no-questions)
5305   "Quit reading current newsgroup without updating read article info."
5306   (interactive)
5307   (let* ((group gnus-newsgroup-name)
5308          (quit-config (gnus-group-quit-config group)))
5309     (when (or no-questions
5310               gnus-expert-user
5311               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5312       (gnus-async-halt-prefetch)
5313       (mapcar 'funcall
5314               (delq 'gnus-summary-expire-articles
5315                     (copy-list gnus-summary-prepare-exit-hook)))
5316       ;; If we have several article buffers, we kill them at exit.
5317       (unless gnus-single-article-buffer
5318         (gnus-kill-buffer gnus-article-buffer)
5319         (gnus-kill-buffer gnus-original-article-buffer)
5320         (setq gnus-article-current nil))
5321       (if (not gnus-kill-summary-on-exit)
5322           (gnus-deaden-summary)
5323         (gnus-close-group group)
5324         (gnus-summary-clear-local-variables)
5325         (set-buffer gnus-group-buffer)
5326         (gnus-summary-clear-local-variables)
5327         (when (get-buffer gnus-summary-buffer)
5328           (kill-buffer gnus-summary-buffer)))
5329       (unless gnus-single-article-buffer
5330         (setq gnus-article-current nil))
5331       (when gnus-use-trees
5332         (gnus-tree-close group))
5333       (gnus-async-prefetch-remove-group group)
5334       (when (get-buffer gnus-article-buffer)
5335         (bury-buffer gnus-article-buffer))
5336       ;; Return to the group buffer.
5337       (gnus-configure-windows 'group 'force)
5338       ;; Clear the current group name.
5339       (setq gnus-newsgroup-name nil)
5340       (when (equal (gnus-group-group-name) group)
5341         (gnus-group-next-unread-group 1))
5342       (when quit-config
5343         (gnus-handle-ephemeral-exit quit-config)))))
5344
5345 (defun gnus-handle-ephemeral-exit (quit-config)
5346   "Handle movement when leaving an ephemeral group.
5347 The state which existed when entering the ephemeral is reset."
5348   (if (not (buffer-name (car quit-config)))
5349       (gnus-configure-windows 'group 'force)
5350     (set-buffer (car quit-config))
5351     (cond ((eq major-mode 'gnus-summary-mode)
5352            (gnus-set-global-variables))
5353           ((eq major-mode 'gnus-article-mode)
5354            (save-excursion
5355              ;; The `gnus-summary-buffer' variable may point
5356              ;; to the old summary buffer when using a single
5357              ;; article buffer.
5358              (unless (gnus-buffer-live-p gnus-summary-buffer)
5359                (set-buffer gnus-group-buffer))
5360              (set-buffer gnus-summary-buffer)
5361              (gnus-set-global-variables))))
5362     (if (or (eq (cdr quit-config) 'article)
5363             (eq (cdr quit-config) 'pick))
5364         (progn
5365           ;; The current article may be from the ephemeral group
5366           ;; thus it is best that we reload this article
5367           (gnus-summary-show-article)
5368           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5369               (gnus-configure-windows 'pick 'force)
5370             (gnus-configure-windows (cdr quit-config) 'force)))
5371       (gnus-configure-windows (cdr quit-config) 'force))
5372     (when (eq major-mode 'gnus-summary-mode)
5373       (gnus-summary-next-subject 1 nil t)
5374       (gnus-summary-recenter)
5375       (gnus-summary-position-point))))
5376
5377 (defun gnus-summary-preview-mime-message ()
5378   "MIME decode and play this message."
5379   (interactive)
5380   (let ((gnus-break-pages nil)
5381         (gnus-show-mime t))
5382     (gnus-summary-select-article gnus-show-all-headers t))
5383   (select-window (get-buffer-window gnus-article-buffer)))
5384
5385 ;;; Dead summaries.
5386
5387 (defvar gnus-dead-summary-mode-map nil)
5388
5389 (unless gnus-dead-summary-mode-map
5390   (setq gnus-dead-summary-mode-map (make-keymap))
5391   (suppress-keymap gnus-dead-summary-mode-map)
5392   (substitute-key-definition
5393    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5394   (let ((keys '("\C-d" "\r" "\177" [delete])))
5395     (while keys
5396       (define-key gnus-dead-summary-mode-map
5397         (pop keys) 'gnus-summary-wake-up-the-dead))))
5398
5399 (defvar gnus-dead-summary-mode nil
5400   "Minor mode for Gnus summary buffers.")
5401
5402 (defun gnus-dead-summary-mode (&optional arg)
5403   "Minor mode for Gnus summary buffers."
5404   (interactive "P")
5405   (when (eq major-mode 'gnus-summary-mode)
5406     (make-local-variable 'gnus-dead-summary-mode)
5407     (setq gnus-dead-summary-mode
5408           (if (null arg) (not gnus-dead-summary-mode)
5409             (> (prefix-numeric-value arg) 0)))
5410     (when gnus-dead-summary-mode
5411       (gnus-add-minor-mode
5412        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5413
5414 (defun gnus-deaden-summary ()
5415   "Make the current summary buffer into a dead summary buffer."
5416   ;; Kill any previous dead summary buffer.
5417   (when (and gnus-dead-summary
5418              (buffer-name gnus-dead-summary))
5419     (save-excursion
5420       (set-buffer gnus-dead-summary)
5421       (when gnus-dead-summary-mode
5422         (kill-buffer (current-buffer)))))
5423   ;; Make this the current dead summary.
5424   (setq gnus-dead-summary (current-buffer))
5425   (gnus-dead-summary-mode 1)
5426   (let ((name (buffer-name)))
5427     (when (string-match "Summary" name)
5428       (rename-buffer
5429        (concat (substring name 0 (match-beginning 0)) "Dead "
5430                (substring name (match-beginning 0)))
5431        t))))
5432
5433 (defun gnus-kill-or-deaden-summary (buffer)
5434   "Kill or deaden the summary BUFFER."
5435   (save-excursion
5436     (when (and (buffer-name buffer)
5437                (not gnus-single-article-buffer))
5438       (save-excursion
5439         (set-buffer buffer)
5440         (gnus-kill-buffer gnus-article-buffer)
5441         (gnus-kill-buffer gnus-original-article-buffer)))
5442     (cond (gnus-kill-summary-on-exit
5443            (when (and gnus-use-trees
5444                       (gnus-buffer-exists-p buffer))
5445              (save-excursion
5446                (set-buffer buffer)
5447                (gnus-tree-close gnus-newsgroup-name)))
5448            (gnus-kill-buffer buffer))
5449           ((gnus-buffer-exists-p buffer)
5450            (save-excursion
5451              (set-buffer buffer)
5452              (gnus-deaden-summary))))))
5453
5454 (defun gnus-summary-wake-up-the-dead (&rest args)
5455   "Wake up the dead summary buffer."
5456   (interactive)
5457   (gnus-dead-summary-mode -1)
5458   (let ((name (buffer-name)))
5459     (when (string-match "Dead " name)
5460       (rename-buffer
5461        (concat (substring name 0 (match-beginning 0))
5462                (substring name (match-end 0)))
5463        t)))
5464   (gnus-message 3 "This dead summary is now alive again"))
5465
5466 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5467 (defun gnus-summary-fetch-faq (&optional faq-dir)
5468   "Fetch the FAQ for the current group.
5469 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5470 in."
5471   (interactive
5472    (list
5473     (when current-prefix-arg
5474       (completing-read
5475        "Faq dir: " (and (listp gnus-group-faq-directory)
5476                         (mapcar (lambda (file) (list file))
5477                                 gnus-group-faq-directory))))))
5478   (let (gnus-faq-buffer)
5479     (when (setq gnus-faq-buffer
5480                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5481       (gnus-configure-windows 'summary-faq))))
5482
5483 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5484 (defun gnus-summary-describe-group (&optional force)
5485   "Describe the current newsgroup."
5486   (interactive "P")
5487   (gnus-group-describe-group force gnus-newsgroup-name))
5488
5489 (defun gnus-summary-describe-briefly ()
5490   "Describe summary mode commands briefly."
5491   (interactive)
5492   (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")))
5493
5494 ;; Walking around group mode buffer from summary mode.
5495
5496 (defun gnus-summary-next-group (&optional no-article target-group backward)
5497   "Exit current newsgroup and then select next unread newsgroup.
5498 If prefix argument NO-ARTICLE is non-nil, no article is selected
5499 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5500 previous group instead."
5501   (interactive "P")
5502   ;; Stop pre-fetching.
5503   (gnus-async-halt-prefetch)
5504   (let ((current-group gnus-newsgroup-name)
5505         (current-buffer (current-buffer))
5506         entered)
5507     ;; First we semi-exit this group to update Xrefs and all variables.
5508     ;; We can't do a real exit, because the window conf must remain
5509     ;; the same in case the user is prompted for info, and we don't
5510     ;; want the window conf to change before that...
5511     (gnus-summary-exit t)
5512     (while (not entered)
5513       ;; Then we find what group we are supposed to enter.
5514       (set-buffer gnus-group-buffer)
5515       (gnus-group-jump-to-group current-group)
5516       (setq target-group
5517             (or target-group
5518                 (if (eq gnus-keep-same-level 'best)
5519                     (gnus-summary-best-group gnus-newsgroup-name)
5520                   (gnus-summary-search-group backward gnus-keep-same-level))))
5521       (if (not target-group)
5522           ;; There are no further groups, so we return to the group
5523           ;; buffer.
5524           (progn
5525             (gnus-message 5 "Returning to the group buffer")
5526             (setq entered t)
5527             (when (gnus-buffer-live-p current-buffer)
5528               (set-buffer current-buffer)
5529               (gnus-summary-exit))
5530             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5531         ;; We try to enter the target group.
5532         (gnus-group-jump-to-group target-group)
5533         (let ((unreads (gnus-group-group-unread)))
5534           (if (and (or (eq t unreads)
5535                        (and unreads (not (zerop unreads))))
5536                    (gnus-summary-read-group
5537                     target-group nil no-article
5538                     (and (buffer-name current-buffer) current-buffer)
5539                     nil backward))
5540               (setq entered t)
5541             (setq current-group target-group
5542                   target-group nil)))))))
5543
5544 (defun gnus-summary-prev-group (&optional no-article)
5545   "Exit current newsgroup and then select previous unread newsgroup.
5546 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5547   (interactive "P")
5548   (gnus-summary-next-group no-article nil t))
5549
5550 ;; Walking around summary lines.
5551
5552 (defun gnus-summary-first-subject (&optional unread undownloaded)
5553   "Go to the first unread subject.
5554 If UNREAD is non-nil, go to the first unread article.
5555 Returns the article selected or nil if there are no unread articles."
5556   (interactive "P")
5557   (prog1
5558       (cond
5559        ;; Empty summary.
5560        ((null gnus-newsgroup-data)
5561         (gnus-message 3 "No articles in the group")
5562         nil)
5563        ;; Pick the first article.
5564        ((not unread)
5565         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5566         (gnus-data-number (car gnus-newsgroup-data)))
5567        ;; No unread articles.
5568        ((null gnus-newsgroup-unreads)
5569         (gnus-message 3 "No more unread articles")
5570         nil)
5571        ;; Find the first unread article.
5572        (t
5573         (let ((data gnus-newsgroup-data))
5574           (while (and data
5575                       (and (not (and undownloaded
5576                                      (eq gnus-undownloaded-mark
5577                                          (gnus-data-mark (car data)))))
5578                            (not (gnus-data-unread-p (car data)))))
5579             (setq data (cdr data)))
5580           (when data
5581             (goto-char (gnus-data-pos (car data)))
5582             (gnus-data-number (car data))))))
5583     (gnus-summary-position-point)))
5584
5585 (defun gnus-summary-next-subject (n &optional unread dont-display)
5586   "Go to next N'th summary line.
5587 If N is negative, go to the previous N'th subject line.
5588 If UNREAD is non-nil, only unread articles are selected.
5589 The difference between N and the actual number of steps taken is
5590 returned."
5591   (interactive "p")
5592   (let ((backward (< n 0))
5593         (n (abs n)))
5594     (while (and (> n 0)
5595                 (if backward
5596                     (gnus-summary-find-prev unread)
5597                   (gnus-summary-find-next unread)))
5598       (gnus-summary-show-thread)
5599       (setq n (1- n)))
5600     (when (/= 0 n)
5601       (gnus-message 7 "No more%s articles"
5602                     (if unread " unread" "")))
5603     (unless dont-display
5604       (gnus-summary-recenter)
5605       (gnus-summary-position-point))
5606     n))
5607
5608 (defun gnus-summary-next-unread-subject (n)
5609   "Go to next N'th unread summary line."
5610   (interactive "p")
5611   (gnus-summary-next-subject n t))
5612
5613 (defun gnus-summary-prev-subject (n &optional unread)
5614   "Go to previous N'th summary line.
5615 If optional argument UNREAD is non-nil, only unread article is selected."
5616   (interactive "p")
5617   (gnus-summary-next-subject (- n) unread))
5618
5619 (defun gnus-summary-prev-unread-subject (n)
5620   "Go to previous N'th unread summary line."
5621   (interactive "p")
5622   (gnus-summary-next-subject (- n) t))
5623
5624 (defun gnus-summary-goto-subject (article &optional force silent)
5625   "Go the subject line of ARTICLE.
5626 If FORCE, also allow jumping to articles not currently shown."
5627   (interactive "nArticle number: ")
5628   (let ((b (point))
5629         (data (gnus-data-find article)))
5630     ;; We read in the article if we have to.
5631     (and (not data)
5632          force
5633          (gnus-summary-insert-subject
5634           article
5635           (if (or (numberp force) (vectorp force)) force)
5636           t)
5637          (setq data (gnus-data-find article)))
5638     (goto-char b)
5639     (if (not data)
5640         (progn
5641           (unless silent
5642             (gnus-message 3 "Can't find article %d" article))
5643           nil)
5644       (goto-char (gnus-data-pos data))
5645       (gnus-summary-position-point)
5646       article)))
5647
5648 ;; Walking around summary lines with displaying articles.
5649
5650 (defun gnus-summary-expand-window (&optional arg)
5651   "Make the summary buffer take up the entire Emacs frame.
5652 Given a prefix, will force an `article' buffer configuration."
5653   (interactive "P")
5654   (if arg
5655       (gnus-configure-windows 'article 'force)
5656     (gnus-configure-windows 'summary 'force)))
5657
5658 (defun gnus-summary-display-article (article &optional all-header)
5659   "Display ARTICLE in article buffer."
5660   (gnus-set-global-variables)
5661   (if (null article)
5662       nil
5663     (prog1
5664         (if gnus-summary-display-article-function
5665             (funcall gnus-summary-display-article-function article all-header)
5666           (gnus-article-prepare article all-header))
5667       (gnus-run-hooks 'gnus-select-article-hook)
5668       (when (and gnus-current-article
5669                  (not (zerop gnus-current-article)))
5670         (gnus-summary-goto-subject gnus-current-article))
5671       (gnus-summary-recenter)
5672       (when (and gnus-use-trees gnus-show-threads)
5673         (gnus-possibly-generate-tree article)
5674         (gnus-highlight-selected-tree article))
5675       ;; Successfully display article.
5676       (gnus-article-set-window-start
5677        (cdr (assq article gnus-newsgroup-bookmarks))))))
5678
5679 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5680   "Select the current article.
5681 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5682 non-nil, the article will be re-fetched even if it already present in
5683 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5684 be displayed."
5685   ;; Make sure we are in the summary buffer to work around bbdb bug.
5686   (unless (eq major-mode 'gnus-summary-mode)
5687     (set-buffer gnus-summary-buffer))
5688   (let ((article (or article (gnus-summary-article-number)))
5689         (all-headers (not (not all-headers))) ;Must be T or NIL.
5690         gnus-summary-display-article-function
5691         did)
5692     (and (not pseudo)
5693          (gnus-summary-article-pseudo-p article)
5694          (error "This is a pseudo-article"))
5695     (prog1
5696         (save-excursion
5697           (set-buffer gnus-summary-buffer)
5698           (if (or (and gnus-single-article-buffer
5699                        (or (null gnus-current-article)
5700                            (null gnus-article-current)
5701                            (null (get-buffer gnus-article-buffer))
5702                            (not (eq article (cdr gnus-article-current)))
5703                            (not (equal (car gnus-article-current)
5704                                        gnus-newsgroup-name))))
5705                   (and (not gnus-single-article-buffer)
5706                        (or (null gnus-current-article)
5707                            (not (eq gnus-current-article article))))
5708                   force)
5709               ;; The requested article is different from the current article.
5710               (prog1
5711                   (gnus-summary-display-article article all-headers)
5712                 (setq did article)
5713                 (when (or all-headers gnus-show-all-headers)
5714                   (gnus-article-show-all-headers)))
5715             (when (or all-headers gnus-show-all-headers)
5716               (gnus-article-show-all-headers))
5717             'old))
5718       (when did
5719         (gnus-article-set-window-start
5720          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5721
5722 (defun gnus-summary-set-current-mark (&optional current-mark)
5723   "Obsolete function."
5724   nil)
5725
5726 (defun gnus-summary-next-article (&optional unread subject backward push)
5727   "Select the next article.
5728 If UNREAD, only unread articles are selected.
5729 If SUBJECT, only articles with SUBJECT are selected.
5730 If BACKWARD, the previous article is selected instead of the next."
5731   (interactive "P")
5732   (cond
5733    ;; Is there such an article?
5734    ((and (gnus-summary-search-forward unread subject backward)
5735          (or (gnus-summary-display-article (gnus-summary-article-number))
5736              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5737     (gnus-summary-position-point))
5738    ;; If not, we try the first unread, if that is wanted.
5739    ((and subject
5740          gnus-auto-select-same
5741          (gnus-summary-first-unread-article))
5742     (gnus-summary-position-point)
5743     (gnus-message 6 "Wrapped"))
5744    ;; Try to get next/previous article not displayed in this group.
5745    ((and gnus-auto-extend-newsgroup
5746          (not unread) (not subject))
5747     (gnus-summary-goto-article
5748      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5749      nil (count-lines (point-min) (point))))
5750    ;; Go to next/previous group.
5751    (t
5752     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5753       (gnus-summary-jump-to-group gnus-newsgroup-name))
5754     (let ((cmd last-command-char)
5755           (point
5756            (save-excursion
5757              (set-buffer gnus-group-buffer)
5758              (point)))
5759           (group
5760            (if (eq gnus-keep-same-level 'best)
5761                (gnus-summary-best-group gnus-newsgroup-name)
5762              (gnus-summary-search-group backward gnus-keep-same-level))))
5763       ;; For some reason, the group window gets selected.  We change
5764       ;; it back.
5765       (select-window (get-buffer-window (current-buffer)))
5766       ;; Select next unread newsgroup automagically.
5767       (cond
5768        ((or (not gnus-auto-select-next)
5769             (not cmd))
5770         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5771        ((or (eq gnus-auto-select-next 'quietly)
5772             (and (eq gnus-auto-select-next 'slightly-quietly)
5773                  push)
5774             (and (eq gnus-auto-select-next 'almost-quietly)
5775                  (gnus-summary-last-article-p)))
5776         ;; Select quietly.
5777         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5778             (gnus-summary-exit)
5779           (gnus-message 7 "No more%s articles (%s)..."
5780                         (if unread " unread" "")
5781                         (if group (concat "selecting " group)
5782                           "exiting"))
5783           (gnus-summary-next-group nil group backward)))
5784        (t
5785         (when (gnus-key-press-event-p last-input-event)
5786           (gnus-summary-walk-group-buffer
5787            gnus-newsgroup-name cmd unread backward point))))))))
5788
5789 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5790   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5791                       (?\C-p (gnus-group-prev-unread-group 1))))
5792         (cursor-in-echo-area t)
5793         keve key group ended)
5794     (save-excursion
5795       (set-buffer gnus-group-buffer)
5796       (goto-char start)
5797       (setq group
5798             (if (eq gnus-keep-same-level 'best)
5799                 (gnus-summary-best-group gnus-newsgroup-name)
5800               (gnus-summary-search-group backward gnus-keep-same-level))))
5801     (while (not ended)
5802       (gnus-message
5803        5 "No more%s articles%s" (if unread " unread" "")
5804        (if (and group
5805                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5806            (format " (Type %s for %s [%s])"
5807                    (single-key-description cmd) group
5808                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5809          (format " (Type %s to exit %s)"
5810                  (single-key-description cmd)
5811                  gnus-newsgroup-name)))
5812       ;; Confirm auto selection.
5813       (setq key (car (setq keve (gnus-read-event-char))))
5814       (setq ended t)
5815       (cond
5816        ((assq key keystrokes)
5817         (let ((obuf (current-buffer)))
5818           (switch-to-buffer gnus-group-buffer)
5819           (when group
5820             (gnus-group-jump-to-group group))
5821           (eval (cadr (assq key keystrokes)))
5822           (setq group (gnus-group-group-name))
5823           (switch-to-buffer obuf))
5824         (setq ended nil))
5825        ((equal key cmd)
5826         (if (or (not group)
5827                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5828             (gnus-summary-exit)
5829           (gnus-summary-next-group nil group backward)))
5830        (t
5831         (push (cdr keve) unread-command-events))))))
5832
5833 (defun gnus-summary-next-unread-article ()
5834   "Select unread article after current one."
5835   (interactive)
5836   (gnus-summary-next-article
5837    (or (not (eq gnus-summary-goto-unread 'never))
5838        (gnus-summary-last-article-p (gnus-summary-article-number)))
5839    (and gnus-auto-select-same
5840         (gnus-summary-article-subject))))
5841
5842 (defun gnus-summary-prev-article (&optional unread subject)
5843   "Select the article after the current one.
5844 If UNREAD is non-nil, only unread articles are selected."
5845   (interactive "P")
5846   (gnus-summary-next-article unread subject t))
5847
5848 (defun gnus-summary-prev-unread-article ()
5849   "Select unread article before current one."
5850   (interactive)
5851   (gnus-summary-prev-article
5852    (or (not (eq gnus-summary-goto-unread 'never))
5853        (gnus-summary-first-article-p (gnus-summary-article-number)))
5854    (and gnus-auto-select-same
5855         (gnus-summary-article-subject))))
5856
5857 (defun gnus-summary-next-page (&optional lines circular)
5858   "Show next page of the selected article.
5859 If at the end of the current article, select the next article.
5860 LINES says how many lines should be scrolled up.
5861
5862 If CIRCULAR is non-nil, go to the start of the article instead of
5863 selecting the next article when reaching the end of the current
5864 article."
5865   (interactive "P")
5866   (setq gnus-summary-buffer (current-buffer))
5867   (gnus-set-global-variables)
5868   (let ((article (gnus-summary-article-number))
5869         (article-window (get-buffer-window gnus-article-buffer t))
5870         endp)
5871     ;; If the buffer is empty, we have no article.
5872     (unless article
5873       (error "No article to select"))
5874     (gnus-configure-windows 'article)
5875     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5876         (if (and (eq gnus-summary-goto-unread 'never)
5877                  (not (gnus-summary-last-article-p article)))
5878             (gnus-summary-next-article)
5879           (gnus-summary-next-unread-article))
5880       (if (or (null gnus-current-article)
5881               (null gnus-article-current)
5882               (/= article (cdr gnus-article-current))
5883               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5884           ;; Selected subject is different from current article's.
5885           (gnus-summary-display-article article)
5886         (when article-window
5887           (gnus-eval-in-buffer-window gnus-article-buffer
5888             (setq endp (gnus-article-next-page lines)))
5889           (when endp
5890             (cond (circular
5891                    (gnus-summary-beginning-of-article))
5892                   (lines
5893                    (gnus-message 3 "End of message"))
5894                   ((null lines)
5895                    (if (and (eq gnus-summary-goto-unread 'never)
5896                             (not (gnus-summary-last-article-p article)))
5897                        (gnus-summary-next-article)
5898                      (gnus-summary-next-unread-article))))))))
5899     (gnus-summary-recenter)
5900     (gnus-summary-position-point)))
5901
5902 (defun gnus-summary-prev-page (&optional lines move)
5903   "Show previous page of selected article.
5904 Argument LINES specifies lines to be scrolled down.
5905 If MOVE, move to the previous unread article if point is at
5906 the beginning of the buffer."
5907   (interactive "P")
5908   (let ((article (gnus-summary-article-number))
5909         (article-window (get-buffer-window gnus-article-buffer t))
5910         endp)
5911     (gnus-configure-windows 'article)
5912     (if (or (null gnus-current-article)
5913             (null gnus-article-current)
5914             (/= article (cdr gnus-article-current))
5915             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5916         ;; Selected subject is different from current article's.
5917         (gnus-summary-display-article article)
5918       (gnus-summary-recenter)
5919       (when article-window
5920         (gnus-eval-in-buffer-window gnus-article-buffer
5921           (setq endp (gnus-article-prev-page lines)))
5922         (when (and move endp)
5923           (cond (lines
5924                  (gnus-message 3 "Beginning of message"))
5925                 ((null lines)
5926                  (if (and (eq gnus-summary-goto-unread 'never)
5927                           (not (gnus-summary-first-article-p article)))
5928                      (gnus-summary-prev-article)
5929                    (gnus-summary-prev-unread-article))))))))
5930   (gnus-summary-position-point))
5931
5932 (defun gnus-summary-prev-page-or-article (&optional lines)
5933   "Show previous page of selected article.
5934 Argument LINES specifies lines to be scrolled down.
5935 If at the beginning of the article, go to the next article."
5936   (interactive "P")
5937   (gnus-summary-prev-page lines t))
5938
5939 (defun gnus-summary-scroll-up (lines)
5940   "Scroll up (or down) one line current article.
5941 Argument LINES specifies lines to be scrolled up (or down if negative)."
5942   (interactive "p")
5943   (gnus-configure-windows 'article)
5944   (gnus-summary-show-thread)
5945   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5946     (gnus-eval-in-buffer-window gnus-article-buffer
5947       (cond ((> lines 0)
5948              (when (gnus-article-next-page lines)
5949                (gnus-message 3 "End of message")))
5950             ((< lines 0)
5951              (gnus-article-prev-page (- lines))))))
5952   (gnus-summary-recenter)
5953   (gnus-summary-position-point))
5954
5955 (defun gnus-summary-scroll-down (lines)
5956   "Scroll down (or up) one line current article.
5957 Argument LINES specifies lines to be scrolled down (or up if negative)."
5958   (interactive "p")
5959   (gnus-summary-scroll-up (- lines)))
5960
5961 (defun gnus-summary-next-same-subject ()
5962   "Select next article which has the same subject as current one."
5963   (interactive)
5964   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5965
5966 (defun gnus-summary-prev-same-subject ()
5967   "Select previous article which has the same subject as current one."
5968   (interactive)
5969   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5970
5971 (defun gnus-summary-next-unread-same-subject ()
5972   "Select next unread article which has the same subject as current one."
5973   (interactive)
5974   (gnus-summary-next-article t (gnus-summary-article-subject)))
5975
5976 (defun gnus-summary-prev-unread-same-subject ()
5977   "Select previous unread article which has the same subject as current one."
5978   (interactive)
5979   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5980
5981 (defun gnus-summary-first-unread-article ()
5982   "Select the first unread article.
5983 Return nil if there are no unread articles."
5984   (interactive)
5985   (prog1
5986       (when (gnus-summary-first-subject t)
5987         (gnus-summary-show-thread)
5988         (gnus-summary-first-subject t)
5989         (gnus-summary-display-article (gnus-summary-article-number)))
5990     (gnus-summary-position-point)))
5991
5992 (defun gnus-summary-first-unread-subject ()
5993   "Place the point on the subject line of the first unread article.
5994 Return nil if there are no unread articles."
5995   (interactive)
5996   (prog1
5997       (when (gnus-summary-first-subject t)
5998         (gnus-summary-show-thread)
5999         (gnus-summary-first-subject t))
6000     (gnus-summary-position-point)))
6001
6002 (defun gnus-summary-first-article ()
6003   "Select the first article.
6004 Return nil if there are no articles."
6005   (interactive)
6006   (prog1
6007       (when (gnus-summary-first-subject)
6008         (gnus-summary-show-thread)
6009         (gnus-summary-first-subject)
6010         (gnus-summary-display-article (gnus-summary-article-number)))
6011     (gnus-summary-position-point)))
6012
6013 (defun gnus-summary-best-unread-article ()
6014   "Select the unread article with the highest score."
6015   (interactive)
6016   (let ((best -1000000)
6017         (data gnus-newsgroup-data)
6018         article score)
6019     (while data
6020       (and (gnus-data-unread-p (car data))
6021            (> (setq score
6022                     (gnus-summary-article-score (gnus-data-number (car data))))
6023               best)
6024            (setq best score
6025                  article (gnus-data-number (car data))))
6026       (setq data (cdr data)))
6027     (prog1
6028         (if article
6029             (gnus-summary-goto-article article)
6030           (error "No unread articles"))
6031       (gnus-summary-position-point))))
6032
6033 (defun gnus-summary-last-subject ()
6034   "Go to the last displayed subject line in the group."
6035   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6036     (when article
6037       (gnus-summary-goto-subject article))))
6038
6039 (defun gnus-summary-goto-article (article &optional all-headers force)
6040   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6041 If ALL-HEADERS is non-nil, no header lines are hidden.
6042 If FORCE, go to the article even if it isn't displayed.  If FORCE
6043 is a number, it is the line the article is to be displayed on."
6044   (interactive
6045    (list
6046     (completing-read
6047      "Article number or Message-ID: "
6048      (mapcar (lambda (number) (list (int-to-string number)))
6049              gnus-newsgroup-limit))
6050     current-prefix-arg
6051     t))
6052   (prog1
6053       (if (and (stringp article)
6054                (string-match "@" article))
6055           (gnus-summary-refer-article article)
6056         (when (stringp article)
6057           (setq article (string-to-number article)))
6058         (if (gnus-summary-goto-subject article force)
6059             (gnus-summary-display-article article all-headers)
6060           (gnus-message 4 "Couldn't go to article %s" article) nil))
6061     (gnus-summary-position-point)))
6062
6063 (defun gnus-summary-goto-last-article ()
6064   "Go to the previously read article."
6065   (interactive)
6066   (prog1
6067       (when gnus-last-article
6068         (gnus-summary-goto-article gnus-last-article nil t))
6069     (gnus-summary-position-point)))
6070
6071 (defun gnus-summary-pop-article (number)
6072   "Pop one article off the history and go to the previous.
6073 NUMBER articles will be popped off."
6074   (interactive "p")
6075   (let (to)
6076     (setq gnus-newsgroup-history
6077           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6078     (if to
6079         (gnus-summary-goto-article (car to) nil t)
6080       (error "Article history empty")))
6081   (gnus-summary-position-point))
6082
6083 ;; Summary commands and functions for limiting the summary buffer.
6084
6085 (defun gnus-summary-limit-to-articles (n)
6086   "Limit the summary buffer to the next N articles.
6087 If not given a prefix, use the process marked articles instead."
6088   (interactive "P")
6089   (prog1
6090       (let ((articles (gnus-summary-work-articles n)))
6091         (setq gnus-newsgroup-processable nil)
6092         (gnus-summary-limit articles))
6093     (gnus-summary-position-point)))
6094
6095 (defun gnus-summary-pop-limit (&optional total)
6096   "Restore the previous limit.
6097 If given a prefix, remove all limits."
6098   (interactive "P")
6099   (when total
6100     (setq gnus-newsgroup-limits
6101           (list (mapcar (lambda (h) (mail-header-number h))
6102                         gnus-newsgroup-headers))))
6103   (unless gnus-newsgroup-limits
6104     (error "No limit to pop"))
6105   (prog1
6106       (gnus-summary-limit nil 'pop)
6107     (gnus-summary-position-point)))
6108
6109 (defun gnus-summary-limit-to-subject (subject &optional header)
6110   "Limit the summary buffer to articles that have subjects that match a regexp."
6111   (interactive "sLimit to subject (regexp): ")
6112   (unless header
6113     (setq header "subject"))
6114   (when (not (equal "" subject))
6115     (prog1
6116         (let ((articles (gnus-summary-find-matching
6117                          (or header "subject") subject 'all)))
6118           (unless articles
6119             (error "Found no matches for \"%s\"" subject))
6120           (gnus-summary-limit articles))
6121       (gnus-summary-position-point))))
6122
6123 (defun gnus-summary-limit-to-author (from)
6124   "Limit the summary buffer to articles that have authors that match a regexp."
6125   (interactive "sLimit to author (regexp): ")
6126   (gnus-summary-limit-to-subject from "from"))
6127
6128 (defun gnus-summary-limit-to-age (age &optional younger-p)
6129   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6130 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6131 articles that are younger than AGE days."
6132   (interactive "nLimit to articles older than (in days): \nP")
6133   (prog1
6134       (let ((data gnus-newsgroup-data)
6135             (cutoff (days-to-time age))
6136             articles d date is-younger)
6137         (while (setq d (pop data))
6138           (when (and (vectorp (gnus-data-header d))
6139                      (setq date (mail-header-date (gnus-data-header d))))
6140             (setq is-younger (time-less-p
6141                               (time-since (date-to-time date))
6142                               cutoff))
6143             (when (if younger-p
6144                       is-younger
6145                     (not is-younger))
6146               (push (gnus-data-number d) articles))))
6147         (gnus-summary-limit (nreverse articles)))
6148     (gnus-summary-position-point)))
6149
6150 (defun gnus-summary-limit-to-extra (header regexp)
6151   "Limit the summary buffer to articles that match an 'extra' header."
6152   (interactive
6153    (let ((header
6154           (intern
6155            (gnus-completing-read
6156             (symbol-name (car gnus-extra-headers))      
6157             "Score extra header:"       
6158             (mapcar (lambda (x) 
6159                       (cons (symbol-name x) x))
6160                     gnus-extra-headers)
6161             nil                 
6162             t))))
6163      (list header
6164            (read-string (format "Limit to header %s (regexp): " header)))))
6165   (when (not (equal "" regexp))
6166     (prog1
6167         (let ((articles (gnus-summary-find-matching
6168                          (cons 'extra header) regexp 'all)))
6169           (unless articles
6170             (error "Found no matches for \"%s\"" regexp))
6171           (gnus-summary-limit articles))
6172       (gnus-summary-position-point))))
6173
6174 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6175 (make-obsolete
6176  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6177
6178 (defun gnus-summary-limit-to-unread (&optional all)
6179   "Limit the summary buffer to articles that are not marked as read.
6180 If ALL is non-nil, limit strictly to unread articles."
6181   (interactive "P")
6182   (if all
6183       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6184     (gnus-summary-limit-to-marks
6185      ;; Concat all the marks that say that an article is read and have
6186      ;; those removed.
6187      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6188            gnus-killed-mark gnus-kill-file-mark
6189            gnus-low-score-mark gnus-expirable-mark
6190            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6191            gnus-duplicate-mark gnus-souped-mark)
6192      'reverse)))
6193
6194 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6195 (make-obsolete 'gnus-summary-delete-marked-with
6196                'gnus-summary-limit-exlude-marks)
6197
6198 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6199   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6200 If REVERSE, limit the summary buffer to articles that are marked
6201 with MARKS.  MARKS can either be a string of marks or a list of marks.
6202 Returns how many articles were removed."
6203   (interactive "sMarks: ")
6204   (gnus-summary-limit-to-marks marks t))
6205
6206 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6207   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6208 If REVERSE (the prefix), limit the summary buffer to articles that are
6209 not marked with MARKS.  MARKS can either be a string of marks or a
6210 list of marks.
6211 Returns how many articles were removed."
6212   (interactive "sMarks: \nP")
6213   (prog1
6214       (let ((data gnus-newsgroup-data)
6215             (marks (if (listp marks) marks
6216                      (append marks nil))) ; Transform to list.
6217             articles)
6218         (while data
6219           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6220                   (memq (gnus-data-mark (car data)) marks))
6221             (push (gnus-data-number (car data)) articles))
6222           (setq data (cdr data)))
6223         (gnus-summary-limit articles))
6224     (gnus-summary-position-point)))
6225
6226 (defun gnus-summary-limit-to-score (&optional score)
6227   "Limit to articles with score at or above SCORE."
6228   (interactive "P")
6229   (setq score (if score
6230                   (prefix-numeric-value score)
6231                 (or gnus-summary-default-score 0)))
6232   (let ((data gnus-newsgroup-data)
6233         articles)
6234     (while data
6235       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6236                 score)
6237         (push (gnus-data-number (car data)) articles))
6238       (setq data (cdr data)))
6239     (prog1
6240         (gnus-summary-limit articles)
6241       (gnus-summary-position-point))))
6242
6243 (defun gnus-summary-limit-include-thread (id)
6244   "Display all the hidden articles that in the current thread."
6245   (interactive (list (mail-header-id (gnus-summary-article-header))))
6246   (let ((articles (gnus-articles-in-thread
6247                    (gnus-id-to-thread (gnus-root-id id)))))
6248     (prog1
6249         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6250       (gnus-summary-position-point))))
6251
6252 (defun gnus-summary-limit-include-dormant ()
6253   "Display all the hidden articles that are marked as dormant.
6254 Note that this command only works on a subset of the articles currently
6255 fetched for this group."
6256   (interactive)
6257   (unless gnus-newsgroup-dormant
6258     (error "There are no dormant articles in this group"))
6259   (prog1
6260       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6261     (gnus-summary-position-point)))
6262
6263 (defun gnus-summary-limit-exclude-dormant ()
6264   "Hide all dormant articles."
6265   (interactive)
6266   (prog1
6267       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6268     (gnus-summary-position-point)))
6269
6270 (defun gnus-summary-limit-exclude-childless-dormant ()
6271   "Hide all dormant articles that have no children."
6272   (interactive)
6273   (let ((data (gnus-data-list t))
6274         articles d children)
6275     ;; Find all articles that are either not dormant or have
6276     ;; children.
6277     (while (setq d (pop data))
6278       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6279                 (and (setq children
6280                            (gnus-article-children (gnus-data-number d)))
6281                      (let (found)
6282                        (while children
6283                          (when (memq (car children) articles)
6284                            (setq children nil
6285                                  found t))
6286                          (pop children))
6287                        found)))
6288         (push (gnus-data-number d) articles)))
6289     ;; Do the limiting.
6290     (prog1
6291         (gnus-summary-limit articles)
6292       (gnus-summary-position-point))))
6293
6294 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6295   "Mark all unread excluded articles as read.
6296 If ALL, mark even excluded ticked and dormants as read."
6297   (interactive "P")
6298   (let ((articles (gnus-sorted-complement
6299                    (sort
6300                     (mapcar (lambda (h) (mail-header-number h))
6301                             gnus-newsgroup-headers)
6302                     '<)
6303                    (sort gnus-newsgroup-limit '<)))
6304         article)
6305     (setq gnus-newsgroup-unreads
6306           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6307     (if all
6308         (setq gnus-newsgroup-dormant nil
6309               gnus-newsgroup-marked nil
6310               gnus-newsgroup-reads
6311               (nconc
6312                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6313                gnus-newsgroup-reads))
6314       (while (setq article (pop articles))
6315         (unless (or (memq article gnus-newsgroup-dormant)
6316                     (memq article gnus-newsgroup-marked))
6317           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6318
6319 (defun gnus-summary-limit (articles &optional pop)
6320   (if pop
6321       ;; We pop the previous limit off the stack and use that.
6322       (setq articles (car gnus-newsgroup-limits)
6323             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6324     ;; We use the new limit, so we push the old limit on the stack.
6325     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6326   ;; Set the limit.
6327   (setq gnus-newsgroup-limit articles)
6328   (let ((total (length gnus-newsgroup-data))
6329         (data (gnus-data-find-list (gnus-summary-article-number)))
6330         (gnus-summary-mark-below nil)   ; Inhibit this.
6331         found)
6332     ;; This will do all the work of generating the new summary buffer
6333     ;; according to the new limit.
6334     (gnus-summary-prepare)
6335     ;; Hide any threads, possibly.
6336     (and gnus-show-threads
6337          gnus-thread-hide-subtree
6338          (gnus-summary-hide-all-threads))
6339     ;; Try to return to the article you were at, or one in the
6340     ;; neighborhood.
6341     (when data
6342       ;; We try to find some article after the current one.
6343       (while data
6344         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6345           (setq data nil
6346                 found t))
6347         (setq data (cdr data))))
6348     (unless found
6349       ;; If there is no data, that means that we were after the last
6350       ;; article.  The same goes when we can't find any articles
6351       ;; after the current one.
6352       (goto-char (point-max))
6353       (gnus-summary-find-prev))
6354     (gnus-set-mode-line 'summary)
6355     ;; We return how many articles were removed from the summary
6356     ;; buffer as a result of the new limit.
6357     (- total (length gnus-newsgroup-data))))
6358
6359 (defsubst gnus-invisible-cut-children (threads)
6360   (let ((num 0))
6361     (while threads
6362       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6363         (incf num))
6364       (pop threads))
6365     (< num 2)))
6366
6367 (defsubst gnus-cut-thread (thread)
6368   "Go forwards in the thread until we find an article that we want to display."
6369   (when (or (eq gnus-fetch-old-headers 'some)
6370             (eq gnus-fetch-old-headers 'invisible)
6371             (eq gnus-build-sparse-threads 'some)
6372             (eq gnus-build-sparse-threads 'more))
6373     ;; Deal with old-fetched headers and sparse threads.
6374     (while (and
6375             thread
6376             (or
6377              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6378              (gnus-summary-article-ancient-p
6379               (mail-header-number (car thread))))
6380             (if (or (<= (length (cdr thread)) 1)
6381                     (eq gnus-fetch-old-headers 'invisible))
6382                 (setq gnus-newsgroup-limit
6383                       (delq (mail-header-number (car thread))
6384                             gnus-newsgroup-limit)
6385                       thread (cadr thread))
6386               (when (gnus-invisible-cut-children (cdr thread))
6387                 (let ((th (cdr thread)))
6388                   (while th
6389                     (if (memq (mail-header-number (caar th))
6390                               gnus-newsgroup-limit)
6391                         (setq thread (car th)
6392                               th nil)
6393                       (setq th (cdr th))))))))))
6394   thread)
6395
6396 (defun gnus-cut-threads (threads)
6397   "Cut off all uninteresting articles from the beginning of threads."
6398   (when (or (eq gnus-fetch-old-headers 'some)
6399             (eq gnus-fetch-old-headers 'invisible)
6400             (eq gnus-build-sparse-threads 'some)
6401             (eq gnus-build-sparse-threads 'more))
6402     (let ((th threads))
6403       (while th
6404         (setcar th (gnus-cut-thread (car th)))
6405         (setq th (cdr th)))))
6406   ;; Remove nixed out threads.
6407   (delq nil threads))
6408
6409 (defun gnus-summary-initial-limit (&optional show-if-empty)
6410   "Figure out what the initial limit is supposed to be on group entry.
6411 This entails weeding out unwanted dormants, low-scored articles,
6412 fetch-old-headers verbiage, and so on."
6413   ;; Most groups have nothing to remove.
6414   (if (or gnus-inhibit-limiting
6415           (and (null gnus-newsgroup-dormant)
6416                (not (eq gnus-fetch-old-headers 'some))
6417                (not (eq gnus-fetch-old-headers 'invisible))
6418                (null gnus-summary-expunge-below)
6419                (not (eq gnus-build-sparse-threads 'some))
6420                (not (eq gnus-build-sparse-threads 'more))
6421                (null gnus-thread-expunge-below)
6422                (not gnus-use-nocem)))
6423       ()                                ; Do nothing.
6424     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6425     (setq gnus-newsgroup-limit nil)
6426     (mapatoms
6427      (lambda (node)
6428        (unless (car (symbol-value node))
6429          ;; These threads have no parents -- they are roots.
6430          (let ((nodes (cdr (symbol-value node)))
6431                thread)
6432            (while nodes
6433              (if (and gnus-thread-expunge-below
6434                       (< (gnus-thread-total-score (car nodes))
6435                          gnus-thread-expunge-below))
6436                  (gnus-expunge-thread (pop nodes))
6437                (setq thread (pop nodes))
6438                (gnus-summary-limit-children thread))))))
6439      gnus-newsgroup-dependencies)
6440     ;; If this limitation resulted in an empty group, we might
6441     ;; pop the previous limit and use it instead.
6442     (when (and (not gnus-newsgroup-limit)
6443                show-if-empty)
6444       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6445     gnus-newsgroup-limit))
6446
6447 (defun gnus-summary-limit-children (thread)
6448   "Return 1 if this subthread is visible and 0 if it is not."
6449   ;; First we get the number of visible children to this thread.  This
6450   ;; is done by recursing down the thread using this function, so this
6451   ;; will really go down to a leaf article first, before slowly
6452   ;; working its way up towards the root.
6453   (when thread
6454     (let ((children
6455            (if (cdr thread)
6456                (apply '+ (mapcar 'gnus-summary-limit-children
6457                                  (cdr thread)))
6458              0))
6459           (number (mail-header-number (car thread)))
6460           score)
6461       (if (and
6462            (not (memq number gnus-newsgroup-marked))
6463            (or
6464             ;; If this article is dormant and has absolutely no visible
6465             ;; children, then this article isn't visible.
6466             (and (memq number gnus-newsgroup-dormant)
6467                  (zerop children))
6468             ;; If this is "fetch-old-headered" and there is no
6469             ;; visible children, then we don't want this article.
6470             (and (eq gnus-fetch-old-headers 'some)
6471                  (gnus-summary-article-ancient-p number)
6472                  (zerop children))
6473             ;; If this is "fetch-old-headered" and `invisible', then
6474             ;; we don't want this article.
6475             (and (eq gnus-fetch-old-headers 'invisible)
6476                  (gnus-summary-article-ancient-p number))
6477             ;; If this is a sparsely inserted article with no children,
6478             ;; we don't want it.
6479             (and (eq gnus-build-sparse-threads 'some)
6480                  (gnus-summary-article-sparse-p number)
6481                  (zerop children))
6482             ;; If we use expunging, and this article is really
6483             ;; low-scored, then we don't want this article.
6484             (when (and gnus-summary-expunge-below
6485                        (< (setq score
6486                                 (or (cdr (assq number gnus-newsgroup-scored))
6487                                     gnus-summary-default-score))
6488                           gnus-summary-expunge-below))
6489               ;; We increase the expunge-tally here, but that has
6490               ;; nothing to do with the limits, really.
6491               (incf gnus-newsgroup-expunged-tally)
6492               ;; We also mark as read here, if that's wanted.
6493               (when (and gnus-summary-mark-below
6494                          (< score gnus-summary-mark-below))
6495                 (setq gnus-newsgroup-unreads
6496                       (delq number gnus-newsgroup-unreads))
6497                 (if gnus-newsgroup-auto-expire
6498                     (push number gnus-newsgroup-expirable)
6499                   (push (cons number gnus-low-score-mark)
6500                         gnus-newsgroup-reads)))
6501               t)
6502             ;; Check NoCeM things.
6503             (if (and gnus-use-nocem
6504                      (gnus-nocem-unwanted-article-p
6505                       (mail-header-id (car thread))))
6506                 (progn
6507                   (setq gnus-newsgroup-unreads
6508                         (delq number gnus-newsgroup-unreads))
6509                   t))))
6510           ;; Nope, invisible article.
6511           0
6512         ;; Ok, this article is to be visible, so we add it to the limit
6513         ;; and return 1.
6514         (push number gnus-newsgroup-limit)
6515         1))))
6516
6517 (defun gnus-expunge-thread (thread)
6518   "Mark all articles in THREAD as read."
6519   (let* ((number (mail-header-number (car thread))))
6520     (incf gnus-newsgroup-expunged-tally)
6521     ;; We also mark as read here, if that's wanted.
6522     (setq gnus-newsgroup-unreads
6523           (delq number gnus-newsgroup-unreads))
6524     (if gnus-newsgroup-auto-expire
6525         (push number gnus-newsgroup-expirable)
6526       (push (cons number gnus-low-score-mark)
6527             gnus-newsgroup-reads)))
6528   ;; Go recursively through all subthreads.
6529   (mapcar 'gnus-expunge-thread (cdr thread)))
6530
6531 ;; Summary article oriented commands
6532
6533 (defun gnus-summary-refer-parent-article (n)
6534   "Refer parent article N times.
6535 If N is negative, go to ancestor -N instead.
6536 The difference between N and the number of articles fetched is returned."
6537   (interactive "p")
6538   (let ((skip 1)
6539         error header ref)
6540     (when (not (natnump n))
6541       (setq skip (abs n)
6542             n 1))
6543     (while (and (> n 0)
6544                 (not error))
6545       (setq header (gnus-summary-article-header))
6546       (if (and (eq (mail-header-number header)
6547                    (cdr gnus-article-current))
6548                (equal gnus-newsgroup-name
6549                       (car gnus-article-current)))
6550           ;; If we try to find the parent of the currently
6551           ;; displayed article, then we take a look at the actual
6552           ;; References header, since this is slightly more
6553           ;; reliable than the References field we got from the
6554           ;; server.
6555           (save-excursion
6556             (set-buffer gnus-original-article-buffer)
6557             (nnheader-narrow-to-headers)
6558             (unless (setq ref (message-fetch-field "references"))
6559               (setq ref (message-fetch-field "in-reply-to")))
6560             (widen))
6561         (setq ref
6562               ;; It's not the current article, so we take a bet on
6563               ;; the value we got from the server.
6564               (mail-header-references header)))
6565       (if (and ref
6566                (not (equal ref "")))
6567           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6568             (gnus-message 1 "Couldn't find parent"))
6569         (gnus-message 1 "No references in article %d"
6570                       (gnus-summary-article-number))
6571         (setq error t))
6572       (decf n))
6573     (gnus-summary-position-point)
6574     n))
6575
6576 (defun gnus-summary-refer-references ()
6577   "Fetch all articles mentioned in the References header.
6578 Return the number of articles fetched."
6579   (interactive)
6580   (let ((ref (mail-header-references (gnus-summary-article-header)))
6581         (current (gnus-summary-article-number))
6582         (n 0))
6583     (if (or (not ref)
6584             (equal ref ""))
6585         (error "No References in the current article")
6586       ;; For each Message-ID in the References header...
6587       (while (string-match "<[^>]*>" ref)
6588         (incf n)
6589         ;; ... fetch that article.
6590         (gnus-summary-refer-article
6591          (prog1 (match-string 0 ref)
6592            (setq ref (substring ref (match-end 0))))))
6593       (gnus-summary-goto-subject current)
6594       (gnus-summary-position-point)
6595       n)))
6596
6597 (defun gnus-summary-refer-thread (&optional limit)
6598   "Fetch all articles in the current thread.
6599 If LIMIT (the numerical prefix), fetch that many old headers instead
6600 of what's specified by the `gnus-refer-thread-limit' variable."
6601   (interactive "P")
6602   (let ((id (mail-header-id (gnus-summary-article-header)))
6603         (limit (if limit (prefix-numeric-value limit)
6604                  gnus-refer-thread-limit)))
6605     ;; We want to fetch LIMIT *old* headers, but we also have to
6606     ;; re-fetch all the headers in the current buffer, because many of
6607     ;; them may be undisplayed.  So we adjust LIMIT.
6608     (when (numberp limit)
6609       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6610     (unless (eq gnus-fetch-old-headers 'invisible)
6611       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6612       ;; Retrieve the headers and read them in.
6613       (if (eq (gnus-retrieve-headers
6614                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6615               'nov)
6616           (gnus-build-all-threads)
6617         (error "Can't fetch thread from backends that don't support NOV"))
6618       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6619     (gnus-summary-limit-include-thread id)))
6620
6621 (defun gnus-summary-refer-article (message-id &optional arg)
6622   "Fetch an article specified by MESSAGE-ID.
6623 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6624 or `gnus-select-method', no matter what backend the article comes from."
6625   (interactive "sMessage-ID: \nP")
6626   (when (and (stringp message-id)
6627              (not (zerop (length message-id))))
6628     ;; Construct the correct Message-ID if necessary.
6629     ;; Suggested by tale@pawl.rpi.edu.
6630     (unless (string-match "^<" message-id)
6631       (setq message-id (concat "<" message-id)))
6632     (unless (string-match ">$" message-id)
6633       (setq message-id (concat message-id ">")))
6634     (let* ((header (gnus-id-to-header message-id))
6635            (sparse (and header
6636                         (gnus-summary-article-sparse-p
6637                          (mail-header-number header))
6638                         (memq (mail-header-number header)
6639                               gnus-newsgroup-limit))))
6640       (cond
6641        ;; If the article is present in the buffer we just go to it.
6642        ((and header
6643              (or (not (gnus-summary-article-sparse-p
6644                        (mail-header-number header)))
6645                  sparse))
6646         (prog1
6647             (gnus-summary-goto-article
6648              (mail-header-number header) nil t)
6649           (when sparse
6650             (gnus-summary-update-article (mail-header-number header)))))
6651        (t
6652         ;; We fetch the article
6653         (let ((gnus-override-method
6654                (cond ((gnus-news-group-p gnus-newsgroup-name)
6655                       gnus-refer-article-method)
6656                      (arg
6657                       (or gnus-refer-article-method gnus-select-method))
6658                      (t nil)))
6659               number)
6660           ;; Start the special refer-article method, if necessary.
6661           (when (and gnus-refer-article-method
6662                      (gnus-news-group-p gnus-newsgroup-name))
6663             (gnus-check-server gnus-refer-article-method))
6664           ;; Fetch the header, and display the article.
6665           (if (setq number (gnus-summary-insert-subject message-id))
6666               (gnus-summary-select-article nil nil nil number)
6667             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6668
6669 (defun gnus-summary-edit-parameters ()
6670   "Edit the group parameters of the current group."
6671   (interactive)
6672   (gnus-group-edit-group gnus-newsgroup-name 'params))
6673
6674 (defun gnus-summary-customize-parameters ()
6675   "Customize the group parameters of the current group."
6676   (interactive)
6677   (gnus-group-customize gnus-newsgroup-name))
6678
6679 (defun gnus-summary-enter-digest-group (&optional force)
6680   "Enter an nndoc group based on the current article.
6681 If FORCE, force a digest interpretation.  If not, try
6682 to guess what the document format is."
6683   (interactive "P")
6684   (let ((conf gnus-current-window-configuration))
6685     (save-excursion
6686       (gnus-summary-select-article))
6687     (setq gnus-current-window-configuration conf)
6688     (let* ((name (format "%s-%d"
6689                          (gnus-group-prefixed-name
6690                           gnus-newsgroup-name (list 'nndoc ""))
6691                          (save-excursion
6692                            (set-buffer gnus-summary-buffer)
6693                            gnus-current-article)))
6694            (ogroup gnus-newsgroup-name)
6695            (params (append (gnus-info-params (gnus-get-info ogroup))
6696                            (list (cons 'to-group ogroup))
6697                            (list (cons 'save-article-group ogroup))))
6698            (case-fold-search t)
6699            (buf (current-buffer))
6700            dig)
6701       (save-excursion
6702         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6703         (insert-buffer-substring gnus-original-article-buffer)
6704         ;; Remove lines that may lead nndoc to misinterpret the
6705         ;; document type.
6706         (narrow-to-region
6707          (goto-char (point-min))
6708          (or (search-forward "\n\n" nil t) (point)))
6709         (goto-char (point-min))
6710         (delete-matching-lines "^\\(Path\\):\\|^From ")
6711         (widen))
6712       (unwind-protect
6713           (if (gnus-group-read-ephemeral-group
6714                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6715                             (nndoc-article-type
6716                              ,(if force 'digest 'guess))) t)
6717               ;; Make all postings to this group go to the parent group.
6718               (nconc (gnus-info-params (gnus-get-info name))
6719                      params)
6720             ;; Couldn't select this doc group.
6721             (switch-to-buffer buf)
6722             (gnus-set-global-variables)
6723             (gnus-configure-windows 'summary)
6724             (gnus-message 3 "Article couldn't be entered?"))
6725         (kill-buffer dig)))))
6726
6727 (defun gnus-summary-read-document (n)
6728   "Open a new group based on the current article(s).
6729 This will allow you to read digests and other similar
6730 documents as newsgroups.
6731 Obeys the standard process/prefix convention."
6732   (interactive "P")
6733   (let* ((articles (gnus-summary-work-articles n))
6734          (ogroup gnus-newsgroup-name)
6735          (params (append (gnus-info-params (gnus-get-info ogroup))
6736                          (list (cons 'to-group ogroup))))
6737          article group egroup groups vgroup)
6738     (while (setq article (pop articles))
6739       (setq group (format "%s-%d" gnus-newsgroup-name article))
6740       (gnus-summary-remove-process-mark article)
6741       (when (gnus-summary-display-article article)
6742         (save-excursion
6743           (with-temp-buffer
6744             (insert-buffer-substring gnus-original-article-buffer)
6745             ;; Remove some headers that may lead nndoc to make
6746             ;; the wrong guess.
6747             (message-narrow-to-head)
6748             (goto-char (point-min))
6749             (delete-matching-lines "^\\(Path\\):\\|^From ")
6750             (widen)
6751             (if (setq egroup
6752                       (gnus-group-read-ephemeral-group
6753                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6754                                      (nndoc-article-type guess))
6755                        t nil t))
6756                 (progn
6757                   ;; Make all postings to this group go to the parent group.
6758                   (nconc (gnus-info-params (gnus-get-info egroup))
6759                          params)
6760                   (push egroup groups))
6761               ;; Couldn't select this doc group.
6762               (gnus-error 3 "Article couldn't be entered"))))))
6763     ;; Now we have selected all the documents.
6764     (cond
6765      ((not groups)
6766       (error "None of the articles could be interpreted as documents"))
6767      ((gnus-group-read-ephemeral-group
6768        (setq vgroup (format
6769                      "nnvirtual:%s-%s" gnus-newsgroup-name
6770                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6771        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6772        t
6773        (cons (current-buffer) 'summary)))
6774      (t
6775       (error "Couldn't select virtual nndoc group")))))
6776
6777 (defun gnus-summary-isearch-article (&optional regexp-p)
6778   "Do incremental search forward on the current article.
6779 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6780   (interactive "P")
6781   (gnus-summary-select-article)
6782   (gnus-configure-windows 'article)
6783   (gnus-eval-in-buffer-window gnus-article-buffer
6784     (save-restriction
6785       (widen)
6786       (isearch-forward regexp-p))))
6787
6788 (defun gnus-summary-search-article-forward (regexp &optional backward)
6789   "Search for an article containing REGEXP forward.
6790 If BACKWARD, search backward instead."
6791   (interactive
6792    (list (read-string
6793           (format "Search article %s (regexp%s): "
6794                   (if current-prefix-arg "backward" "forward")
6795                   (if gnus-last-search-regexp
6796                       (concat ", default " gnus-last-search-regexp)
6797                     "")))
6798          current-prefix-arg))
6799   (if (string-equal regexp "")
6800       (setq regexp (or gnus-last-search-regexp ""))
6801     (setq gnus-last-search-regexp regexp))
6802   (if (gnus-summary-search-article regexp backward)
6803       (gnus-summary-show-thread)
6804     (error "Search failed: \"%s\"" regexp)))
6805
6806 (defun gnus-summary-search-article-backward (regexp)
6807   "Search for an article containing REGEXP backward."
6808   (interactive
6809    (list (read-string
6810           (format "Search article backward (regexp%s): "
6811                   (if gnus-last-search-regexp
6812                       (concat ", default " gnus-last-search-regexp)
6813                     "")))))
6814   (gnus-summary-search-article-forward regexp 'backward))
6815
6816 (defun gnus-summary-search-article (regexp &optional backward)
6817   "Search for an article containing REGEXP.
6818 Optional argument BACKWARD means do search for backward.
6819 `gnus-select-article-hook' is not called during the search."
6820   ;; We have to require this here to make sure that the following
6821   ;; dynamic binding isn't shadowed by autoloading.
6822   (require 'gnus-async)
6823   (require 'gnus-art)
6824   (let ((gnus-select-article-hook nil)  ;Disable hook.
6825         (gnus-article-display-hook nil)
6826         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6827         (gnus-use-article-prefetch nil)
6828         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6829         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6830         (sum (current-buffer))
6831         (found nil)
6832         point)
6833     (gnus-save-hidden-threads
6834       (gnus-summary-select-article)
6835       (set-buffer gnus-article-buffer)
6836       (when backward
6837         (forward-line -1))
6838       (while (not found)
6839         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6840         (if (if backward
6841                 (re-search-backward regexp nil t)
6842               (re-search-forward regexp nil t))
6843             ;; We found the regexp.
6844             (progn
6845               (setq found 'found)
6846               (beginning-of-line)
6847               (set-window-start
6848                (get-buffer-window (current-buffer))
6849                (point))
6850               (forward-line 1)
6851               (set-buffer sum)
6852               (setq point (point)))
6853           ;; We didn't find it, so we go to the next article.
6854           (set-buffer sum)
6855           (setq found 'not)
6856           (while (eq found 'not)
6857             (if (not (if backward (gnus-summary-find-prev)
6858                        (gnus-summary-find-next)))
6859                 ;; No more articles.
6860                 (setq found t)
6861               ;; Select the next article and adjust point.
6862               (unless (gnus-summary-article-sparse-p
6863                        (gnus-summary-article-number))
6864                 (setq found nil)
6865                 (gnus-summary-select-article)
6866                 (set-buffer gnus-article-buffer)
6867                 (widen)
6868                 (goto-char (if backward (point-max) (point-min))))))))
6869       (gnus-message 7 ""))
6870     ;; Return whether we found the regexp.
6871     (when (eq found 'found)
6872       (goto-char point)
6873       (gnus-summary-show-thread)
6874       (gnus-summary-goto-subject gnus-current-article)
6875       (gnus-summary-position-point)
6876       t)))
6877
6878 (defun gnus-summary-find-matching (header regexp &optional backward unread
6879                                           not-case-fold)
6880   "Return a list of all articles that match REGEXP on HEADER.
6881 The search stars on the current article and goes forwards unless
6882 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6883 If UNREAD is non-nil, only unread articles will
6884 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6885 in the comparisons."
6886   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6887                 (gnus-data-find-list
6888                  (gnus-summary-article-number) (gnus-data-list backward))))
6889         (case-fold-search (not not-case-fold))
6890         articles d func)
6891     (if (consp header)
6892         (if (eq (car header) 'extra)
6893             (setq func
6894                   `(lambda (h)
6895                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
6896                          "")))
6897           (error "%s is an invalid header" header))
6898       (unless (fboundp (intern (concat "mail-header-" header)))
6899         (error "%s is not a valid header" header))
6900       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
6901     (while data
6902       (setq d (car data))
6903       (and (or (not unread)             ; We want all articles...
6904                (gnus-data-unread-p d))  ; Or just unreads.
6905            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6906            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6907            (push (gnus-data-number d) articles)) ; Success!
6908       (setq data (cdr data)))
6909     (nreverse articles)))
6910
6911 (defun gnus-summary-execute-command (header regexp command &optional backward)
6912   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6913 If HEADER is an empty string (or nil), the match is done on the entire
6914 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6915   (interactive
6916    (list (let ((completion-ignore-case t))
6917            (completing-read
6918             "Header name: "
6919             (mapcar (lambda (string) (list string))
6920                     '("Number" "Subject" "From" "Lines" "Date"
6921                       "Message-ID" "Xref" "References" "Body"))
6922             nil 'require-match))
6923          (read-string "Regexp: ")
6924          (read-key-sequence "Command: ")
6925          current-prefix-arg))
6926   (when (equal header "Body")
6927     (setq header ""))
6928   ;; Hidden thread subtrees must be searched as well.
6929   (gnus-summary-show-all-threads)
6930   ;; We don't want to change current point nor window configuration.
6931   (save-excursion
6932     (save-window-excursion
6933       (gnus-message 6 "Executing %s..." (key-description command))
6934       ;; We'd like to execute COMMAND interactively so as to give arguments.
6935       (gnus-execute header regexp
6936                     `(call-interactively ',(key-binding command))
6937                     backward)
6938       (gnus-message 6 "Executing %s...done" (key-description command)))))
6939
6940 (defun gnus-summary-beginning-of-article ()
6941   "Scroll the article back to the beginning."
6942   (interactive)
6943   (gnus-summary-select-article)
6944   (gnus-configure-windows 'article)
6945   (gnus-eval-in-buffer-window gnus-article-buffer
6946     (widen)
6947     (goto-char (point-min))
6948     (when gnus-page-broken
6949       (gnus-narrow-to-page))))
6950
6951 (defun gnus-summary-end-of-article ()
6952   "Scroll to the end of the article."
6953   (interactive)
6954   (gnus-summary-select-article)
6955   (gnus-configure-windows 'article)
6956   (gnus-eval-in-buffer-window gnus-article-buffer
6957     (widen)
6958     (goto-char (point-max))
6959     (recenter -3)
6960     (when gnus-page-broken
6961       (gnus-narrow-to-page))))
6962
6963 (defun gnus-summary-print-article (&optional filename n)
6964   "Generate and print a PostScript image of the N next (mail) articles.
6965
6966 If N is negative, print the N previous articles.  If N is nil and articles
6967 have been marked with the process mark, print these instead.
6968
6969 If the optional first argument FILENAME is nil, send the image to the
6970 printer.  If FILENAME is a string, save the PostScript image in a file with
6971 that name.  If FILENAME is a number, prompt the user for the name of the file
6972 to save in."
6973   (interactive (list (ps-print-preprint current-prefix-arg)
6974                      current-prefix-arg))
6975   (dolist (article (gnus-summary-work-articles n))
6976     (gnus-summary-select-article nil nil 'pseudo article)
6977     (gnus-eval-in-buffer-window gnus-article-buffer
6978       (let ((buffer (generate-new-buffer " *print*")))
6979         (unwind-protect
6980             (progn
6981               (copy-to-buffer buffer (point-min) (point-max))
6982               (set-buffer buffer)
6983               (gnus-article-delete-invisible-text)
6984               (let ((ps-left-header
6985                      (list
6986                       (concat "("
6987                               (mail-header-subject gnus-current-headers) ")")
6988                       (concat "("
6989                               (mail-header-from gnus-current-headers) ")")))
6990                     (ps-right-header
6991                      (list
6992                       "/pagenumberstring load"
6993                       (concat "("
6994                               (mail-header-date gnus-current-headers) ")"))))
6995                 (gnus-run-hooks 'gnus-ps-print-hook)
6996                 (save-excursion
6997                   (ps-print-buffer-with-faces filename))))
6998           (kill-buffer buffer))))))
6999
7000 (defun gnus-summary-show-article (&optional arg)
7001   "Force re-fetching of the current article.
7002 If ARG (the prefix) is non-nil, show the raw article without any
7003 article massaging functions being run."
7004   (interactive "P")
7005   (if (not arg)
7006       ;; Select the article the normal way.
7007       (gnus-summary-select-article nil 'force)
7008     ;; We have to require this here to make sure that the following
7009     ;; dynamic binding isn't shadowed by autoloading.
7010     (require 'gnus-async)
7011     (require 'gnus-art)
7012     ;; Bind the article treatment functions to nil.
7013     (let ((gnus-have-all-headers t)
7014           gnus-article-display-hook
7015           gnus-article-prepare-hook
7016           gnus-article-decode-hook
7017           gnus-break-pages
7018           gnus-show-mime
7019           gnus-visual)
7020       (gnus-summary-select-article nil 'force)))
7021   (gnus-summary-goto-subject gnus-current-article)
7022   (gnus-summary-position-point))
7023
7024 (defun gnus-summary-verbose-headers (&optional arg)
7025   "Toggle permanent full header display.
7026 If ARG is a positive number, turn header display on.
7027 If ARG is a negative number, turn header display off."
7028   (interactive "P")
7029   (setq gnus-show-all-headers
7030         (cond ((or (not (numberp arg))
7031                    (zerop arg))
7032                (not gnus-show-all-headers))
7033               ((natnump arg)
7034                t)))
7035   (gnus-summary-show-article))
7036
7037 (defun gnus-summary-toggle-header (&optional arg)
7038   "Show the headers if they are hidden, or hide them if they are shown.
7039 If ARG is a positive number, show the entire header.
7040 If ARG is a negative number, hide the unwanted header lines."
7041   (interactive "P")
7042   (save-excursion
7043     (set-buffer gnus-article-buffer)
7044     (save-restriction
7045       (let* ((buffer-read-only nil)
7046              (inhibit-point-motion-hooks t)
7047              hidden e)
7048         (save-restriction 
7049           (article-narrow-to-head)
7050           (setq hidden (gnus-article-hidden-text-p 'headers)))
7051         (goto-char (point-min))
7052         (when (search-forward "\n\n" nil t)
7053           (delete-region (point-min) (1- (point))))
7054         (goto-char (point-min))
7055         (save-excursion
7056           (set-buffer gnus-original-article-buffer)
7057           (goto-char (point-min))
7058           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7059         (insert-buffer-substring gnus-original-article-buffer 1 e)
7060         (save-restriction
7061           (narrow-to-region (point-min) (point))
7062           (article-decode-encoded-words)
7063           (if (or hidden
7064                   (and (numberp arg) (< arg 0)))
7065               (let ((gnus-treat-hide-headers nil)
7066                     (gnus-treat-hide-boring-headers nil))
7067                 (gnus-treat-article 'head))
7068             (gnus-treat-article 'head)))))))
7069
7070 (defun gnus-summary-show-all-headers ()
7071   "Make all header lines visible."
7072   (interactive)
7073   (gnus-article-show-all-headers))
7074
7075 (defun gnus-summary-toggle-mime (&optional arg)
7076   "Toggle MIME processing.
7077 If ARG is a positive number, turn MIME processing on."
7078   (interactive "P")
7079   (setq gnus-show-mime
7080         (if (null arg)
7081             (not gnus-show-mime)
7082           (> (prefix-numeric-value arg) 0)))
7083   (gnus-summary-select-article t 'force))
7084
7085 (defun gnus-summary-caesar-message (&optional arg)
7086   "Caesar rotate the current article by 13.
7087 The numerical prefix specifies how many places to rotate each letter
7088 forward."
7089   (interactive "P")
7090   (gnus-summary-select-article)
7091   (let ((mail-header-separator ""))
7092     (gnus-eval-in-buffer-window gnus-article-buffer
7093       (save-restriction
7094         (widen)
7095         (let ((start (window-start))
7096               buffer-read-only)
7097           (message-caesar-buffer-body arg)
7098           (set-window-start (get-buffer-window (current-buffer)) start))))))
7099
7100 (defun gnus-summary-stop-page-breaking ()
7101   "Stop page breaking in the current article."
7102   (interactive)
7103   (gnus-summary-select-article)
7104   (gnus-eval-in-buffer-window gnus-article-buffer
7105     (widen)
7106     (when (gnus-visual-p 'page-marker)
7107       (let ((buffer-read-only nil))
7108         (gnus-remove-text-with-property 'gnus-prev)
7109         (gnus-remove-text-with-property 'gnus-next))
7110       (setq gnus-page-broken nil))))
7111
7112 (defun gnus-summary-move-article (&optional n to-newsgroup
7113                                             select-method action)
7114   "Move the current article to a different newsgroup.
7115 If N is a positive number, move the N next articles.
7116 If N is a negative number, move the N previous articles.
7117 If N is nil and any articles have been marked with the process mark,
7118 move those articles instead.
7119 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7120 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7121 re-spool using this method.
7122
7123 For this function to work, both the current newsgroup and the
7124 newsgroup that you want to move to have to support the `request-move'
7125 and `request-accept' functions."
7126   (interactive "P")
7127   (unless action
7128     (setq action 'move))
7129   ;; Disable marking as read.
7130   (let (gnus-mark-article-hook)
7131     (save-window-excursion
7132       (gnus-summary-select-article)))
7133   ;; Check whether the source group supports the required functions.
7134   (cond ((and (eq action 'move)
7135               (not (gnus-check-backend-function
7136                     'request-move-article gnus-newsgroup-name)))
7137          (error "The current group does not support article moving"))
7138         ((and (eq action 'crosspost)
7139               (not (gnus-check-backend-function
7140                     'request-replace-article gnus-newsgroup-name)))
7141          (error "The current group does not support article editing")))
7142   (let ((articles (gnus-summary-work-articles n))
7143         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
7144         (names '((move "Move" "Moving")
7145                  (copy "Copy" "Copying")
7146                  (crosspost "Crosspost" "Crossposting")))
7147         (copy-buf (save-excursion
7148                     (nnheader-set-temp-buffer " *copy article*")))
7149         (default-marks gnus-article-mark-lists)
7150         (no-expire-marks (delete '(expirable . expire)
7151                                  (copy-sequence gnus-article-mark-lists)))
7152         art-group to-method new-xref article to-groups)
7153     (unless (assq action names)
7154       (error "Unknown action %s" action))
7155     ;; Read the newsgroup name.
7156     (when (and (not to-newsgroup)
7157                (not select-method))
7158       (setq to-newsgroup
7159             (gnus-read-move-group-name
7160              (cadr (assq action names))
7161              (symbol-value (intern (format "gnus-current-%s-group" action)))
7162              articles prefix))
7163       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7164     (setq to-method (or select-method
7165                         (gnus-group-name-to-method to-newsgroup)))
7166     ;; Check the method we are to move this article to...
7167     (unless (gnus-check-backend-function
7168              'request-accept-article (car to-method))
7169       (error "%s does not support article copying" (car to-method)))
7170     (unless (gnus-check-server to-method)
7171       (error "Can't open server %s" (car to-method)))
7172     (gnus-message 6 "%s to %s: %s..."
7173                   (caddr (assq action names))
7174                   (or (car select-method) to-newsgroup) articles)
7175     (while articles
7176       (setq article (pop articles))
7177       (setq
7178        art-group
7179        (cond
7180         ;; Move the article.
7181         ((eq action 'move)
7182          ;; Remove this article from future suppression.
7183          (gnus-dup-unsuppress-article article)
7184          (gnus-request-move-article
7185           article                       ; Article to move
7186           gnus-newsgroup-name           ; From newsgroup
7187           (nth 1 (gnus-find-method-for-group
7188                   gnus-newsgroup-name)) ; Server
7189           (list 'gnus-request-accept-article
7190                 to-newsgroup (list 'quote select-method)
7191                 (not articles) t)               ; Accept form
7192           (not articles)))              ; Only save nov last time
7193         ;; Copy the article.
7194         ((eq action 'copy)
7195          (save-excursion
7196            (set-buffer copy-buf)
7197            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7198              (gnus-request-accept-article
7199               to-newsgroup select-method (not articles) t))))
7200         ;; Crosspost the article.
7201         ((eq action 'crosspost)
7202          (let ((xref (message-tokenize-header
7203                       (mail-header-xref (gnus-summary-article-header article))
7204                       " ")))
7205            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7206                                   ":" article))
7207            (unless xref
7208              (setq xref (list (system-name))))
7209            (setq new-xref
7210                  (concat
7211                   (mapconcat 'identity
7212                              (delete "Xref:" (delete new-xref xref))
7213                              " ")
7214                   " " new-xref))
7215            (save-excursion
7216              (set-buffer copy-buf)
7217              ;; First put the article in the destination group.
7218              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7219              (when (consp (setq art-group
7220                                 (gnus-request-accept-article
7221                                  to-newsgroup select-method (not articles))))
7222                (setq new-xref (concat new-xref " " (car art-group)
7223                                       ":" (cdr art-group)))
7224                ;; Now we have the new Xrefs header, so we insert
7225                ;; it and replace the new article.
7226                (nnheader-replace-header "Xref" new-xref)
7227                (gnus-request-replace-article
7228                 (cdr art-group) to-newsgroup (current-buffer))
7229                art-group))))))
7230       (cond
7231        ((not art-group)
7232         (gnus-message 1 "Couldn't %s article %s"
7233                       (cadr (assq action names)) article))
7234        ((and (eq art-group 'junk)
7235              (eq action 'move))
7236         (gnus-summary-mark-article article gnus-canceled-mark)
7237         (gnus-message 4 "Deleted article %s" article))
7238        (t
7239         (let* ((pto-group (gnus-group-prefixed-name
7240                            (car art-group) to-method))
7241                (entry
7242                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7243                (info (nth 2 entry))
7244                (to-group (gnus-info-group info)))
7245           ;; Update the group that has been moved to.
7246           (when (and info
7247                      (memq action '(move copy)))
7248             (unless (member to-group to-groups)
7249               (push to-group to-groups))
7250
7251             (unless (memq article gnus-newsgroup-unreads)
7252               (gnus-info-set-read
7253                info (gnus-add-to-range (gnus-info-read info)
7254                                        (list (cdr art-group)))))
7255
7256             ;; Copy any marks over to the new group.
7257             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7258                              default-marks
7259                            no-expire-marks))
7260                   (to-article (cdr art-group)))
7261
7262               ;; See whether the article is to be put in the cache.
7263               (when gnus-use-cache
7264                 (gnus-cache-possibly-enter-article
7265                  to-group to-article
7266                  (memq article gnus-newsgroup-marked)
7267                  (memq article gnus-newsgroup-dormant)
7268                  (memq article gnus-newsgroup-unreads)))
7269
7270               (when (and (equal to-group gnus-newsgroup-name)
7271                          (not (memq article gnus-newsgroup-unreads)))
7272                 ;; Mark this article as read in this group.
7273                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7274                 (setcdr (gnus-active to-group) to-article)
7275                 (setcdr gnus-newsgroup-active to-article))
7276
7277               (while marks
7278                 (when (memq article (symbol-value
7279                                      (intern (format "gnus-newsgroup-%s"
7280                                                      (caar marks)))))
7281                   ;; If the other group is the same as this group,
7282                   ;; then we have to add the mark to the list.
7283                   (when (equal to-group gnus-newsgroup-name)
7284                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7285                          (cons to-article
7286                                (symbol-value
7287                                 (intern (format "gnus-newsgroup-%s"
7288                                                 (caar marks)))))))
7289                   ;; Copy the marks to other group.
7290                   (gnus-add-marked-articles
7291                    to-group (cdar marks) (list to-article) info))
7292                 (setq marks (cdr marks)))
7293
7294               (gnus-dribble-enter
7295                (concat "(gnus-group-set-info '"
7296                        (gnus-prin1-to-string (gnus-get-info to-group))
7297                        ")"))))
7298
7299           ;; Update the Xref header in this article to point to
7300           ;; the new crossposted article we have just created.
7301           (when (eq action 'crosspost)
7302             (save-excursion
7303               (set-buffer copy-buf)
7304               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7305               (nnheader-replace-header "Xref" new-xref)
7306               (gnus-request-replace-article
7307                article gnus-newsgroup-name (current-buffer)))))
7308
7309         ;;;!!!Why is this necessary?
7310         (set-buffer gnus-summary-buffer)
7311
7312         (gnus-summary-goto-subject article)
7313         (when (eq action 'move)
7314           (gnus-summary-mark-article article gnus-canceled-mark))))
7315       (gnus-summary-remove-process-mark article))
7316     ;; Re-activate all groups that have been moved to.
7317     (while to-groups
7318       (save-excursion
7319         (set-buffer gnus-group-buffer)
7320         (when (gnus-group-goto-group (car to-groups) t)
7321           (gnus-group-get-new-news-this-group 1 t))
7322         (pop to-groups)))
7323
7324     (gnus-kill-buffer copy-buf)
7325     (gnus-summary-position-point)
7326     (gnus-set-mode-line 'summary)))
7327
7328 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7329   "Move the current article to a different newsgroup.
7330 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7331 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7332 re-spool using this method."
7333   (interactive "P")
7334   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7335
7336 (defun gnus-summary-crosspost-article (&optional n)
7337   "Crosspost the current article to some other group."
7338   (interactive "P")
7339   (gnus-summary-move-article n nil nil 'crosspost))
7340
7341 (defcustom gnus-summary-respool-default-method nil
7342   "Default method for respooling an article.
7343 If nil, use to the current newsgroup method."
7344   :type '(choice (gnus-select-method :value (nnml ""))
7345                  (const nil))
7346   :group 'gnus-summary-mail)
7347
7348 (defun gnus-summary-respool-article (&optional n method)
7349   "Respool the current article.
7350 The article will be squeezed through the mail spooling process again,
7351 which means that it will be put in some mail newsgroup or other
7352 depending on `nnmail-split-methods'.
7353 If N is a positive number, respool the N next articles.
7354 If N is a negative number, respool the N previous articles.
7355 If N is nil and any articles have been marked with the process mark,
7356 respool those articles instead.
7357
7358 Respooling can be done both from mail groups and \"real\" newsgroups.
7359 In the former case, the articles in question will be moved from the
7360 current group into whatever groups they are destined to.  In the
7361 latter case, they will be copied into the relevant groups."
7362   (interactive
7363    (list current-prefix-arg
7364          (let* ((methods (gnus-methods-using 'respool))
7365                 (methname
7366                  (symbol-name (or gnus-summary-respool-default-method
7367                                   (car (gnus-find-method-for-group
7368                                         gnus-newsgroup-name)))))
7369                 (method
7370                  (gnus-completing-read
7371                   methname "What backend do you want to use when respooling?"
7372                   methods nil t nil 'gnus-mail-method-history))
7373                 ms)
7374            (cond
7375             ((zerop (length (setq ms (gnus-servers-using-backend
7376                                       (intern method)))))
7377              (list (intern method) ""))
7378             ((= 1 (length ms))
7379              (car ms))
7380             (t
7381              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7382                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7383                            ms-alist))))))))
7384   (unless method
7385     (error "No method given for respooling"))
7386   (if (assoc (symbol-name
7387               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7388              (gnus-methods-using 'respool))
7389       (gnus-summary-move-article n nil method)
7390     (gnus-summary-copy-article n nil method)))
7391
7392 (defun gnus-summary-import-article (file)
7393   "Import an arbitrary file into a mail newsgroup."
7394   (interactive "fImport file: ")
7395   (let ((group gnus-newsgroup-name)
7396         (now (current-time))
7397         atts lines)
7398     (unless (gnus-check-backend-function 'request-accept-article group)
7399       (error "%s does not support article importing" group))
7400     (or (file-readable-p file)
7401         (not (file-regular-p file))
7402         (error "Can't read %s" file))
7403     (save-excursion
7404       (set-buffer (gnus-get-buffer-create " *import file*"))
7405       (erase-buffer)
7406       (nnheader-insert-file-contents file)
7407       (goto-char (point-min))
7408       (unless (nnheader-article-p)
7409         ;; This doesn't look like an article, so we fudge some headers.
7410         (setq atts (file-attributes file)
7411               lines (count-lines (point-min) (point-max)))
7412         (insert "From: " (read-string "From: ") "\n"
7413                 "Subject: " (read-string "Subject: ") "\n"
7414                 "Date: " (message-make-date (nth 5 atts))
7415                 "\n"
7416                 "Message-ID: " (message-make-message-id) "\n"
7417                 "Lines: " (int-to-string lines) "\n"
7418                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7419       (gnus-request-accept-article group nil t)
7420       (kill-buffer (current-buffer)))))
7421
7422 (defun gnus-summary-article-posted-p ()
7423   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7424 This will be the case if the article has both been mailed and posted."
7425   (interactive)
7426   (let ((id (mail-header-references (gnus-summary-article-header)))
7427         (gnus-override-method
7428          (or gnus-refer-article-method gnus-select-method)))
7429     (if (gnus-request-head id "")
7430         (gnus-message 2 "The current message was found on %s"
7431                       gnus-override-method)
7432       (gnus-message 2 "The current message couldn't be found on %s"
7433                     gnus-override-method)
7434       nil)))
7435
7436 (defun gnus-summary-expire-articles (&optional now)
7437   "Expire all articles that are marked as expirable in the current group."
7438   (interactive)
7439   (when (gnus-check-backend-function
7440          'request-expire-articles gnus-newsgroup-name)
7441     ;; This backend supports expiry.
7442     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7443            (expirable (if total
7444                           (progn
7445                             ;; We need to update the info for
7446                             ;; this group for `gnus-list-of-read-articles'
7447                             ;; to give us the right answer.
7448                             (gnus-run-hooks 'gnus-exit-group-hook)
7449                             (gnus-summary-update-info)
7450                             (gnus-list-of-read-articles gnus-newsgroup-name))
7451                         (setq gnus-newsgroup-expirable
7452                               (sort gnus-newsgroup-expirable '<))))
7453            (expiry-wait (if now 'immediate
7454                           (gnus-group-find-parameter
7455                            gnus-newsgroup-name 'expiry-wait)))
7456            es)
7457       (when expirable
7458         ;; There are expirable articles in this group, so we run them
7459         ;; through the expiry process.
7460         (gnus-message 6 "Expiring articles...")
7461         (unless (gnus-check-group gnus-newsgroup-name)
7462           (error "Can't open server for %s" gnus-newsgroup-name))
7463         ;; The list of articles that weren't expired is returned.
7464         (save-excursion
7465           (if expiry-wait
7466               (let ((nnmail-expiry-wait-function nil)
7467                     (nnmail-expiry-wait expiry-wait))
7468                 (setq es (gnus-request-expire-articles
7469                           expirable gnus-newsgroup-name)))
7470             (setq es (gnus-request-expire-articles
7471                       expirable gnus-newsgroup-name))))
7472         (unless total
7473           (setq gnus-newsgroup-expirable es))
7474         ;; We go through the old list of expirable, and mark all
7475         ;; really expired articles as nonexistent.
7476         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7477           (let ((gnus-use-cache nil))
7478             (while expirable
7479               (unless (memq (car expirable) es)
7480                 (when (gnus-data-find (car expirable))
7481                   (gnus-summary-mark-article
7482                    (car expirable) gnus-canceled-mark)))
7483               (setq expirable (cdr expirable)))))
7484         (gnus-message 6 "Expiring articles...done")))))
7485
7486 (defun gnus-summary-expire-articles-now ()
7487   "Expunge all expirable articles in the current group.
7488 This means that *all* articles that are marked as expirable will be
7489 deleted forever, right now."
7490   (interactive)
7491   (or gnus-expert-user
7492       (gnus-yes-or-no-p
7493        "Are you really, really, really sure you want to delete all these messages? ")
7494       (error "Phew!"))
7495   (gnus-summary-expire-articles t))
7496
7497 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7498 (defun gnus-summary-delete-article (&optional n)
7499   "Delete the N next (mail) articles.
7500 This command actually deletes articles.  This is not a marking
7501 command.  The article will disappear forever from your life, never to
7502 return.
7503 If N is negative, delete backwards.
7504 If N is nil and articles have been marked with the process mark,
7505 delete these instead."
7506   (interactive "P")
7507   (unless (gnus-check-backend-function 'request-expire-articles
7508                                        gnus-newsgroup-name)
7509     (error "The current newsgroup does not support article deletion"))
7510   ;; Compute the list of articles to delete.
7511   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7512         not-deleted)
7513     (if (and gnus-novice-user
7514              (not (gnus-yes-or-no-p
7515                    (format "Do you really want to delete %s forever? "
7516                            (if (> (length articles) 1)
7517                                (format "these %s articles" (length articles))
7518                              "this article")))))
7519         ()
7520       ;; Delete the articles.
7521       (setq not-deleted (gnus-request-expire-articles
7522                          articles gnus-newsgroup-name 'force))
7523       (while articles
7524         (gnus-summary-remove-process-mark (car articles))
7525         ;; The backend might not have been able to delete the article
7526         ;; after all.
7527         (unless (memq (car articles) not-deleted)
7528           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7529         (setq articles (cdr articles)))
7530       (when not-deleted
7531         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7532     (gnus-summary-position-point)
7533     (gnus-set-mode-line 'summary)
7534     not-deleted))
7535
7536 (defun gnus-summary-edit-article (&optional force)
7537   "Edit the current article.
7538 This will have permanent effect only in mail groups.
7539 If FORCE is non-nil, allow editing of articles even in read-only
7540 groups."
7541   (interactive "P")
7542   (let ((mail-parse-charset gnus-newsgroup-charset))
7543     (save-excursion
7544       (set-buffer gnus-summary-buffer)
7545       (gnus-set-global-variables)
7546       (when (and (not force)
7547                  (gnus-group-read-only-p))
7548         (error "The current newsgroup does not support article editing"))
7549       (gnus-summary-show-article t)
7550       (gnus-article-edit-article
7551        'ignore
7552        `(lambda (no-highlight)
7553           (let ((mail-parse-charset ',gnus-newsgroup-charset))
7554             (gnus-summary-edit-article-done
7555              ,(or (mail-header-references gnus-current-headers) "")
7556              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7557
7558 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7559
7560 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7561                                                  no-highlight)
7562   "Make edits to the current article permanent."
7563   (interactive)
7564   ;; Replace the article.
7565   (let ((buf (current-buffer)))
7566     (with-temp-buffer
7567       (insert-buffer-substring buf)
7568       (if (and (not read-only)
7569                (not (gnus-request-replace-article
7570                      (cdr gnus-article-current) (car gnus-article-current)
7571                      (current-buffer) t)))
7572           (error "Couldn't replace article")
7573         ;; Update the summary buffer.
7574         (if (and references
7575                  (equal (message-tokenize-header references " ")
7576                         (message-tokenize-header
7577                          (or (message-fetch-field "references") "") " ")))
7578             ;; We only have to update this line.
7579             (save-excursion
7580               (save-restriction
7581                 (message-narrow-to-head)
7582                 (let ((head (buffer-string))
7583                       header)
7584                   (with-temp-buffer
7585                     (insert (format "211 %d Article retrieved.\n"
7586                                     (cdr gnus-article-current)))
7587                     (insert head)
7588                     (insert ".\n")
7589                     (let ((nntp-server-buffer (current-buffer)))
7590                       (setq header (car (gnus-get-newsgroup-headers
7591                                          (save-excursion
7592                                            (set-buffer gnus-summary-buffer)
7593                                            gnus-newsgroup-dependencies)
7594                                          t))))
7595                     (save-excursion
7596                       (set-buffer gnus-summary-buffer)
7597                       (gnus-data-set-header
7598                        (gnus-data-find (cdr gnus-article-current))
7599                        header)
7600                       (gnus-summary-update-article-line
7601                        (cdr gnus-article-current) header))))))
7602           ;; Update threads.
7603           (set-buffer (or buffer gnus-summary-buffer))
7604           (gnus-summary-update-article (cdr gnus-article-current)))
7605         ;; Prettify the article buffer again.
7606         (unless no-highlight
7607           (save-excursion
7608             (set-buffer gnus-article-buffer)
7609             ;;;!!! Fix this -- article should be rehighlighted.
7610             ;;;(gnus-run-hooks 'gnus-article-display-hook)
7611             (set-buffer gnus-original-article-buffer)
7612             (gnus-request-article
7613              (cdr gnus-article-current)
7614              (car gnus-article-current) (current-buffer))))
7615         ;; Prettify the summary buffer line.
7616         (when (gnus-visual-p 'summary-highlight 'highlight)
7617           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7618
7619 (defun gnus-summary-edit-wash (key)
7620   "Perform editing command KEY in the article buffer."
7621   (interactive
7622    (list
7623     (progn
7624       (message "%s" (concat (this-command-keys) "- "))
7625       (read-char))))
7626   (message "")
7627   (gnus-summary-edit-article)
7628   (execute-kbd-macro (concat (this-command-keys) key))
7629   (gnus-article-edit-done))
7630
7631 ;;; Respooling
7632
7633 (defun gnus-summary-respool-query (&optional silent trace)
7634   "Query where the respool algorithm would put this article."
7635   (interactive)
7636   (let (gnus-mark-article-hook)
7637     (gnus-summary-select-article)
7638     (save-excursion
7639       (set-buffer gnus-original-article-buffer)
7640       (save-restriction
7641         (message-narrow-to-head)
7642         (let ((groups (nnmail-article-group 'identity trace)))
7643           (unless silent
7644             (if groups
7645                 (message "This message would go to %s"
7646                          (mapconcat 'car groups ", "))
7647               (message "This message would go to no groups"))
7648             groups))))))
7649
7650 (defun gnus-summary-respool-trace ()
7651   "Trace where the respool algorithm would put this article.
7652 Display a buffer showing all fancy splitting patterns which matched."
7653   (interactive)
7654   (gnus-summary-respool-query nil t))
7655
7656 ;; Summary marking commands.
7657
7658 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7659   "Mark articles which has the same subject as read, and then select the next.
7660 If UNMARK is positive, remove any kind of mark.
7661 If UNMARK is negative, tick articles."
7662   (interactive "P")
7663   (when unmark
7664     (setq unmark (prefix-numeric-value unmark)))
7665   (let ((count
7666          (gnus-summary-mark-same-subject
7667           (gnus-summary-article-subject) unmark)))
7668     ;; Select next unread article.  If auto-select-same mode, should
7669     ;; select the first unread article.
7670     (gnus-summary-next-article t (and gnus-auto-select-same
7671                                       (gnus-summary-article-subject)))
7672     (gnus-message 7 "%d article%s marked as %s"
7673                   count (if (= count 1) " is" "s are")
7674                   (if unmark "unread" "read"))))
7675
7676 (defun gnus-summary-kill-same-subject (&optional unmark)
7677   "Mark articles which has the same subject as read.
7678 If UNMARK is positive, remove any kind of mark.
7679 If UNMARK is negative, tick articles."
7680   (interactive "P")
7681   (when unmark
7682     (setq unmark (prefix-numeric-value unmark)))
7683   (let ((count
7684          (gnus-summary-mark-same-subject
7685           (gnus-summary-article-subject) unmark)))
7686     ;; If marked as read, go to next unread subject.
7687     (when (null unmark)
7688       ;; Go to next unread subject.
7689       (gnus-summary-next-subject 1 t))
7690     (gnus-message 7 "%d articles are marked as %s"
7691                   count (if unmark "unread" "read"))))
7692
7693 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7694   "Mark articles with same SUBJECT as read, and return marked number.
7695 If optional argument UNMARK is positive, remove any kinds of marks.
7696 If optional argument UNMARK is negative, mark articles as unread instead."
7697   (let ((count 1))
7698     (save-excursion
7699       (cond
7700        ((null unmark)                   ; Mark as read.
7701         (while (and
7702                 (progn
7703                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7704                   (gnus-summary-show-thread) t)
7705                 (gnus-summary-find-subject subject))
7706           (setq count (1+ count))))
7707        ((> unmark 0)                    ; Tick.
7708         (while (and
7709                 (progn
7710                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7711                   (gnus-summary-show-thread) t)
7712                 (gnus-summary-find-subject subject))
7713           (setq count (1+ count))))
7714        (t                               ; Mark as unread.
7715         (while (and
7716                 (progn
7717                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7718                   (gnus-summary-show-thread) t)
7719                 (gnus-summary-find-subject subject))
7720           (setq count (1+ count)))))
7721       (gnus-set-mode-line 'summary)
7722       ;; Return the number of marked articles.
7723       count)))
7724
7725 (defun gnus-summary-mark-as-processable (n &optional unmark)
7726   "Set the process mark on the next N articles.
7727 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7728 the process mark instead.  The difference between N and the actual
7729 number of articles marked is returned."
7730   (interactive "p")
7731   (let ((backward (< n 0))
7732         (n (abs n)))
7733     (while (and
7734             (> n 0)
7735             (if unmark
7736                 (gnus-summary-remove-process-mark
7737                  (gnus-summary-article-number))
7738               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7739             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7740       (setq n (1- n)))
7741     (when (/= 0 n)
7742       (gnus-message 7 "No more articles"))
7743     (gnus-summary-recenter)
7744     (gnus-summary-position-point)
7745     n))
7746
7747 (defun gnus-summary-unmark-as-processable (n)
7748   "Remove the process mark from the next N articles.
7749 If N is negative, unmark backward instead.  The difference between N and
7750 the actual number of articles unmarked is returned."
7751   (interactive "p")
7752   (gnus-summary-mark-as-processable n t))
7753
7754 (defun gnus-summary-unmark-all-processable ()
7755   "Remove the process mark from all articles."
7756   (interactive)
7757   (save-excursion
7758     (while gnus-newsgroup-processable
7759       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7760   (gnus-summary-position-point))
7761
7762 (defun gnus-summary-mark-as-expirable (n)
7763   "Mark N articles forward as expirable.
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-expirable-mark))
7768
7769 (defun gnus-summary-mark-article-as-replied (article)
7770   "Mark ARTICLE replied and update the summary line."
7771   (push article gnus-newsgroup-replied)
7772   (let ((buffer-read-only nil))
7773     (when (gnus-summary-goto-subject article nil t)
7774       (gnus-summary-update-secondary-mark article))))
7775
7776 (defun gnus-summary-set-bookmark (article)
7777   "Set a bookmark in current article."
7778   (interactive (list (gnus-summary-article-number)))
7779   (when (or (not (get-buffer gnus-article-buffer))
7780             (not gnus-current-article)
7781             (not gnus-article-current)
7782             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7783     (error "No current article selected"))
7784   ;; Remove old bookmark, if one exists.
7785   (let ((old (assq article gnus-newsgroup-bookmarks)))
7786     (when old
7787       (setq gnus-newsgroup-bookmarks
7788             (delq old gnus-newsgroup-bookmarks))))
7789   ;; Set the new bookmark, which is on the form
7790   ;; (article-number . line-number-in-body).
7791   (push
7792    (cons article
7793          (save-excursion
7794            (set-buffer gnus-article-buffer)
7795            (count-lines
7796             (min (point)
7797                  (save-excursion
7798                    (goto-char (point-min))
7799                    (search-forward "\n\n" nil t)
7800                    (point)))
7801             (point))))
7802    gnus-newsgroup-bookmarks)
7803   (gnus-message 6 "A bookmark has been added to the current article."))
7804
7805 (defun gnus-summary-remove-bookmark (article)
7806   "Remove the bookmark from the current article."
7807   (interactive (list (gnus-summary-article-number)))
7808   ;; Remove old bookmark, if one exists.
7809   (let ((old (assq article gnus-newsgroup-bookmarks)))
7810     (if old
7811         (progn
7812           (setq gnus-newsgroup-bookmarks
7813                 (delq old gnus-newsgroup-bookmarks))
7814           (gnus-message 6 "Removed bookmark."))
7815       (gnus-message 6 "No bookmark in current article."))))
7816
7817 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7818 (defun gnus-summary-mark-as-dormant (n)
7819   "Mark N articles forward as dormant.
7820 If N is negative, mark backward instead.  The difference between N and
7821 the actual number of articles marked is returned."
7822   (interactive "p")
7823   (gnus-summary-mark-forward n gnus-dormant-mark))
7824
7825 (defun gnus-summary-set-process-mark (article)
7826   "Set the process mark on ARTICLE and update the summary line."
7827   (setq gnus-newsgroup-processable
7828         (cons article
7829               (delq article gnus-newsgroup-processable)))
7830   (when (gnus-summary-goto-subject article)
7831     (gnus-summary-show-thread)
7832     (gnus-summary-goto-subject article)
7833     (gnus-summary-update-secondary-mark article)))
7834
7835 (defun gnus-summary-remove-process-mark (article)
7836   "Remove the process mark from ARTICLE and update the summary line."
7837   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7838   (when (gnus-summary-goto-subject article)
7839     (gnus-summary-show-thread)
7840     (gnus-summary-goto-subject article)
7841     (gnus-summary-update-secondary-mark article)))
7842
7843 (defun gnus-summary-set-saved-mark (article)
7844   "Set the process mark on ARTICLE and update the summary line."
7845   (push article gnus-newsgroup-saved)
7846   (when (gnus-summary-goto-subject article)
7847     (gnus-summary-update-secondary-mark article)))
7848
7849 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7850   "Mark N articles as read forwards.
7851 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7852 The difference between N and the actual number of articles marked is
7853 returned."
7854   (interactive "p")
7855   (gnus-summary-show-thread)
7856   (let ((backward (< n 0))
7857         (gnus-summary-goto-unread
7858          (and gnus-summary-goto-unread
7859               (not (eq gnus-summary-goto-unread 'never))
7860               (not (memq mark (list gnus-unread-mark
7861                                     gnus-ticked-mark gnus-dormant-mark)))))
7862         (n (abs n))
7863         (mark (or mark gnus-del-mark)))
7864     (while (and (> n 0)
7865                 (gnus-summary-mark-article nil mark no-expire)
7866                 (zerop (gnus-summary-next-subject
7867                         (if backward -1 1)
7868                         (and gnus-summary-goto-unread
7869                              (not (eq gnus-summary-goto-unread 'never)))
7870                         t)))
7871       (setq n (1- n)))
7872     (when (/= 0 n)
7873       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7874     (gnus-summary-recenter)
7875     (gnus-summary-position-point)
7876     (gnus-set-mode-line 'summary)
7877     n))
7878
7879 (defun gnus-summary-mark-article-as-read (mark)
7880   "Mark the current article quickly as read with MARK."
7881   (let ((article (gnus-summary-article-number)))
7882     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7883     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7884     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7885     (push (cons article mark) gnus-newsgroup-reads)
7886     ;; Possibly remove from cache, if that is used.
7887     (when gnus-use-cache
7888       (gnus-cache-enter-remove-article article))
7889     ;; Allow the backend to change the mark.
7890     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7891     ;; Check for auto-expiry.
7892     (when (and gnus-newsgroup-auto-expire
7893                (memq mark gnus-auto-expirable-marks))
7894       (setq mark gnus-expirable-mark)
7895       ;; Let the backend know about the mark change.
7896       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7897       (push article gnus-newsgroup-expirable))
7898     ;; Set the mark in the buffer.
7899     (gnus-summary-update-mark mark 'unread)
7900     t))
7901
7902 (defun gnus-summary-mark-article-as-unread (mark)
7903   "Mark the current article quickly as unread with MARK."
7904   (let* ((article (gnus-summary-article-number))
7905          (old-mark (gnus-summary-article-mark article)))
7906     ;; Allow the backend to change the mark.
7907     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7908     (if (eq mark old-mark)
7909         t
7910       (if (<= article 0)
7911           (progn
7912             (gnus-error 1 "Can't mark negative article numbers")
7913             nil)
7914         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7915         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7916         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7917         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7918         (cond ((= mark gnus-ticked-mark)
7919                (push article gnus-newsgroup-marked))
7920               ((= mark gnus-dormant-mark)
7921                (push article gnus-newsgroup-dormant))
7922               (t
7923                (push article gnus-newsgroup-unreads)))
7924         (gnus-pull article gnus-newsgroup-reads)
7925
7926         ;; See whether the article is to be put in the cache.
7927         (and gnus-use-cache
7928              (vectorp (gnus-summary-article-header article))
7929              (save-excursion
7930                (gnus-cache-possibly-enter-article
7931                 gnus-newsgroup-name article
7932                 (= mark gnus-ticked-mark)
7933                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7934
7935         ;; Fix the mark.
7936         (gnus-summary-update-mark mark 'unread)
7937         t))))
7938
7939 (defun gnus-summary-mark-article (&optional article mark no-expire)
7940   "Mark ARTICLE with MARK.  MARK can be any character.
7941 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7942 `??' (dormant) and `?E' (expirable).
7943 If MARK is nil, then the default character `?r' is used.
7944 If ARTICLE is nil, then the article on the current line will be
7945 marked."
7946   ;; The mark might be a string.
7947   (when (stringp mark)
7948     (setq mark (aref mark 0)))
7949   ;; If no mark is given, then we check auto-expiring.
7950   (when (null mark)
7951     (setq mark gnus-del-mark))
7952   (when (and (not no-expire)
7953              gnus-newsgroup-auto-expire
7954              (memq mark gnus-auto-expirable-marks))
7955     (setq mark gnus-expirable-mark))
7956   (let ((article (or article (gnus-summary-article-number)))
7957         (old-mark (gnus-summary-article-mark article)))
7958     ;; Allow the backend to change the mark.
7959     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7960     (if (eq mark old-mark)
7961         t
7962       (unless article
7963         (error "No article on current line"))
7964       (if (not (if (or (= mark gnus-unread-mark)
7965                        (= mark gnus-ticked-mark)
7966                        (= mark gnus-dormant-mark))
7967                    (gnus-mark-article-as-unread article mark)
7968                  (gnus-mark-article-as-read article mark)))
7969           t
7970         ;; See whether the article is to be put in the cache.
7971         (and gnus-use-cache
7972              (not (= mark gnus-canceled-mark))
7973              (vectorp (gnus-summary-article-header article))
7974              (save-excursion
7975                (gnus-cache-possibly-enter-article
7976                 gnus-newsgroup-name article
7977                 (= mark gnus-ticked-mark)
7978                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7979
7980         (when (gnus-summary-goto-subject article nil t)
7981           (let ((buffer-read-only nil))
7982             (gnus-summary-show-thread)
7983             ;; Fix the mark.
7984             (gnus-summary-update-mark mark 'unread)
7985             t))))))
7986
7987 (defun gnus-summary-update-secondary-mark (article)
7988   "Update the secondary (read, process, cache) mark."
7989   (gnus-summary-update-mark
7990    (cond ((memq article gnus-newsgroup-processable)
7991           gnus-process-mark)
7992          ((memq article gnus-newsgroup-cached)
7993           gnus-cached-mark)
7994          ((memq article gnus-newsgroup-replied)
7995           gnus-replied-mark)
7996          ((memq article gnus-newsgroup-saved)
7997           gnus-saved-mark)
7998          (t gnus-unread-mark))
7999    'replied)
8000   (when (gnus-visual-p 'summary-highlight 'highlight)
8001     (gnus-run-hooks 'gnus-summary-update-hook))
8002   t)
8003
8004 (defun gnus-summary-update-mark (mark type)
8005   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8006         (buffer-read-only nil))
8007     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8008     (when forward
8009       (when (looking-at "\r")
8010         (incf forward))
8011       (when (<= (+ forward (point)) (point-max))
8012         ;; Go to the right position on the line.
8013         (goto-char (+ forward (point)))
8014         ;; Replace the old mark with the new mark.
8015         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8016         ;; Optionally update the marks by some user rule.
8017         (when (eq type 'unread)
8018           (gnus-data-set-mark
8019            (gnus-data-find (gnus-summary-article-number)) mark)
8020           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8021
8022 (defun gnus-mark-article-as-read (article &optional mark)
8023   "Enter ARTICLE in the pertinent lists and remove it from others."
8024   ;; Make the article expirable.
8025   (let ((mark (or mark gnus-del-mark)))
8026     (if (= mark gnus-expirable-mark)
8027         (push article gnus-newsgroup-expirable)
8028       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8029     ;; Remove from unread and marked lists.
8030     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8031     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8032     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8033     (push (cons article mark) gnus-newsgroup-reads)
8034     ;; Possibly remove from cache, if that is used.
8035     (when gnus-use-cache
8036       (gnus-cache-enter-remove-article article))
8037     t))
8038
8039 (defun gnus-mark-article-as-unread (article &optional mark)
8040   "Enter ARTICLE in the pertinent lists and remove it from others."
8041   (let ((mark (or mark gnus-ticked-mark)))
8042     (if (<= article 0)
8043         (progn
8044           (gnus-error 1 "Can't mark negative article numbers")
8045           nil)
8046       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8047             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8048             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8049             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8050
8051       ;; Unsuppress duplicates?
8052       (when gnus-suppress-duplicates
8053         (gnus-dup-unsuppress-article article))
8054
8055       (cond ((= mark gnus-ticked-mark)
8056              (push article gnus-newsgroup-marked))
8057             ((= mark gnus-dormant-mark)
8058              (push article gnus-newsgroup-dormant))
8059             (t
8060              (push article gnus-newsgroup-unreads)))
8061       (gnus-pull article gnus-newsgroup-reads)
8062       t)))
8063
8064 (defalias 'gnus-summary-mark-as-unread-forward
8065   'gnus-summary-tick-article-forward)
8066 (make-obsolete 'gnus-summary-mark-as-unread-forward
8067                'gnus-summary-tick-article-forward)
8068 (defun gnus-summary-tick-article-forward (n)
8069   "Tick N articles forwards.
8070 If N is negative, tick backwards instead.
8071 The difference between N and the number of articles ticked is returned."
8072   (interactive "p")
8073   (gnus-summary-mark-forward n gnus-ticked-mark))
8074
8075 (defalias 'gnus-summary-mark-as-unread-backward
8076   'gnus-summary-tick-article-backward)
8077 (make-obsolete 'gnus-summary-mark-as-unread-backward
8078                'gnus-summary-tick-article-backward)
8079 (defun gnus-summary-tick-article-backward (n)
8080   "Tick N articles backwards.
8081 The difference between N and the number of articles ticked is returned."
8082   (interactive "p")
8083   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8084
8085 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8086 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8087 (defun gnus-summary-tick-article (&optional article clear-mark)
8088   "Mark current article as unread.
8089 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8090 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8091   (interactive)
8092   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8093                                        gnus-ticked-mark)))
8094
8095 (defun gnus-summary-mark-as-read-forward (n)
8096   "Mark N articles as read forwards.
8097 If N is negative, mark backwards instead.
8098 The difference between N and the actual number of articles marked is
8099 returned."
8100   (interactive "p")
8101   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8102
8103 (defun gnus-summary-mark-as-read-backward (n)
8104   "Mark the N articles as read backwards.
8105 The difference between N and the actual number of articles marked is
8106 returned."
8107   (interactive "p")
8108   (gnus-summary-mark-forward
8109    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8110
8111 (defun gnus-summary-mark-as-read (&optional article mark)
8112   "Mark current article as read.
8113 ARTICLE specifies the article to be marked as read.
8114 MARK specifies a string to be inserted at the beginning of the line."
8115   (gnus-summary-mark-article article mark))
8116
8117 (defun gnus-summary-clear-mark-forward (n)
8118   "Clear marks from N articles forward.
8119 If N is negative, clear backward instead.
8120 The difference between N and the number of marks cleared is returned."
8121   (interactive "p")
8122   (gnus-summary-mark-forward n gnus-unread-mark))
8123
8124 (defun gnus-summary-clear-mark-backward (n)
8125   "Clear marks from N articles backward.
8126 The difference between N and the number of marks cleared is returned."
8127   (interactive "p")
8128   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8129
8130 (defun gnus-summary-mark-unread-as-read ()
8131   "Intended to be used by `gnus-summary-mark-article-hook'."
8132   (when (memq gnus-current-article gnus-newsgroup-unreads)
8133     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8134
8135 (defun gnus-summary-mark-read-and-unread-as-read ()
8136   "Intended to be used by `gnus-summary-mark-article-hook'."
8137   (let ((mark (gnus-summary-article-mark)))
8138     (when (or (gnus-unread-mark-p mark)
8139               (gnus-read-mark-p mark))
8140       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8141
8142 (defun gnus-summary-mark-region-as-read (point mark all)
8143   "Mark all unread articles between point and mark as read.
8144 If given a prefix, mark all articles between point and mark as read,
8145 even ticked and dormant ones."
8146   (interactive "r\nP")
8147   (save-excursion
8148     (let (article)
8149       (goto-char point)
8150       (beginning-of-line)
8151       (while (and
8152               (< (point) mark)
8153               (progn
8154                 (when (or all
8155                           (memq (setq article (gnus-summary-article-number))
8156                                 gnus-newsgroup-unreads))
8157                   (gnus-summary-mark-article article gnus-del-mark))
8158                 t)
8159               (gnus-summary-find-next))))))
8160
8161 (defun gnus-summary-mark-below (score mark)
8162   "Mark articles with score less than SCORE with MARK."
8163   (interactive "P\ncMark: ")
8164   (setq score (if score
8165                   (prefix-numeric-value score)
8166                 (or gnus-summary-default-score 0)))
8167   (save-excursion
8168     (set-buffer gnus-summary-buffer)
8169     (goto-char (point-min))
8170     (while
8171         (progn
8172           (and (< (gnus-summary-article-score) score)
8173                (gnus-summary-mark-article nil mark))
8174           (gnus-summary-find-next)))))
8175
8176 (defun gnus-summary-kill-below (&optional score)
8177   "Mark articles with score below SCORE as read."
8178   (interactive "P")
8179   (gnus-summary-mark-below score gnus-killed-mark))
8180
8181 (defun gnus-summary-clear-above (&optional score)
8182   "Clear all marks from articles with score above SCORE."
8183   (interactive "P")
8184   (gnus-summary-mark-above score gnus-unread-mark))
8185
8186 (defun gnus-summary-tick-above (&optional score)
8187   "Tick all articles with score above SCORE."
8188   (interactive "P")
8189   (gnus-summary-mark-above score gnus-ticked-mark))
8190
8191 (defun gnus-summary-mark-above (score mark)
8192   "Mark articles with score over SCORE with MARK."
8193   (interactive "P\ncMark: ")
8194   (setq score (if score
8195                   (prefix-numeric-value score)
8196                 (or gnus-summary-default-score 0)))
8197   (save-excursion
8198     (set-buffer gnus-summary-buffer)
8199     (goto-char (point-min))
8200     (while (and (progn
8201                   (when (> (gnus-summary-article-score) score)
8202                     (gnus-summary-mark-article nil mark))
8203                   t)
8204                 (gnus-summary-find-next)))))
8205
8206 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8207 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8208 (defun gnus-summary-limit-include-expunged (&optional no-error)
8209   "Display all the hidden articles that were expunged for low scores."
8210   (interactive)
8211   (let ((buffer-read-only nil))
8212     (let ((scored gnus-newsgroup-scored)
8213           headers h)
8214       (while scored
8215         (unless (gnus-summary-goto-subject (caar scored))
8216           (and (setq h (gnus-summary-article-header (caar scored)))
8217                (< (cdar scored) gnus-summary-expunge-below)
8218                (push h headers)))
8219         (setq scored (cdr scored)))
8220       (if (not headers)
8221           (when (not no-error)
8222             (error "No expunged articles hidden"))
8223         (goto-char (point-min))
8224         (gnus-summary-prepare-unthreaded (nreverse headers))
8225         (goto-char (point-min))
8226         (gnus-summary-position-point)
8227         t))))
8228
8229 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8230   "Mark all unread articles in this newsgroup as read.
8231 If prefix argument ALL is non-nil, ticked and dormant articles will
8232 also be marked as read.
8233 If QUIETLY is non-nil, no questions will be asked.
8234 If TO-HERE is non-nil, it should be a point in the buffer.  All
8235 articles before this point will be marked as read.
8236 Note that this function will only catch up the unread article
8237 in the current summary buffer limitation.
8238 The number of articles marked as read is returned."
8239   (interactive "P")
8240   (prog1
8241       (save-excursion
8242         (when (or quietly
8243                   (not gnus-interactive-catchup) ;Without confirmation?
8244                   gnus-expert-user
8245                   (gnus-y-or-n-p
8246                    (if all
8247                        "Mark absolutely all articles as read? "
8248                      "Mark all unread articles as read? ")))
8249           (if (and not-mark
8250                    (not gnus-newsgroup-adaptive)
8251                    (not gnus-newsgroup-auto-expire)
8252                    (not gnus-suppress-duplicates)
8253                    (or (not gnus-use-cache)
8254                        (eq gnus-use-cache 'passive)))
8255               (progn
8256                 (when all
8257                   (setq gnus-newsgroup-marked nil
8258                         gnus-newsgroup-dormant nil))
8259                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8260             ;; We actually mark all articles as canceled, which we
8261             ;; have to do when using auto-expiry or adaptive scoring.
8262             (gnus-summary-show-all-threads)
8263             (when (gnus-summary-first-subject (not all) t)
8264               (while (and
8265                       (if to-here (< (point) to-here) t)
8266                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8267                       (gnus-summary-find-next (not all) nil nil t))))
8268             (gnus-set-mode-line 'summary))
8269           t))
8270     (gnus-summary-position-point)))
8271
8272 (defun gnus-summary-catchup-to-here (&optional all)
8273   "Mark all unticked articles before the current one as read.
8274 If ALL is non-nil, also mark ticked and dormant articles as read."
8275   (interactive "P")
8276   (save-excursion
8277     (gnus-save-hidden-threads
8278       (let ((beg (point)))
8279         ;; We check that there are unread articles.
8280         (when (or all (gnus-summary-find-prev))
8281           (gnus-summary-catchup all t beg)))))
8282   (gnus-summary-position-point))
8283
8284 (defun gnus-summary-catchup-all (&optional quietly)
8285   "Mark all articles in this newsgroup as read."
8286   (interactive "P")
8287   (gnus-summary-catchup t quietly))
8288
8289 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8290   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8291 If prefix argument ALL is non-nil, all articles are marked as read."
8292   (interactive "P")
8293   (when (gnus-summary-catchup all quietly nil 'fast)
8294     ;; Select next newsgroup or exit.
8295     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8296              (eq gnus-auto-select-next 'quietly))
8297         (gnus-summary-next-group nil)
8298       (gnus-summary-exit))))
8299
8300 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8301   "Mark all articles in this newsgroup as read, and then exit."
8302   (interactive "P")
8303   (gnus-summary-catchup-and-exit t quietly))
8304
8305 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8306 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8307   "Mark all articles in this group as read and select the next group.
8308 If given a prefix, mark all articles, unread as well as ticked, as
8309 read."
8310   (interactive "P")
8311   (save-excursion
8312     (gnus-summary-catchup all))
8313   (gnus-summary-next-article t nil nil t))
8314
8315 ;; Thread-based commands.
8316
8317 (defun gnus-summary-articles-in-thread (&optional article)
8318   "Return a list of all articles in the current thread.
8319 If ARTICLE is non-nil, return all articles in the thread that starts
8320 with that article."
8321   (let* ((article (or article (gnus-summary-article-number)))
8322          (data (gnus-data-find-list article))
8323          (top-level (gnus-data-level (car data)))
8324          (top-subject
8325           (cond ((null gnus-thread-operation-ignore-subject)
8326                  (gnus-simplify-subject-re
8327                   (mail-header-subject (gnus-data-header (car data)))))
8328                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8329                  (gnus-simplify-subject-fuzzy
8330                   (mail-header-subject (gnus-data-header (car data)))))
8331                 (t nil)))
8332          (end-point (save-excursion
8333                       (if (gnus-summary-go-to-next-thread)
8334                           (point) (point-max))))
8335          articles)
8336     (while (and data
8337                 (< (gnus-data-pos (car data)) end-point))
8338       (when (or (not top-subject)
8339                 (string= top-subject
8340                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8341                              (gnus-simplify-subject-fuzzy
8342                               (mail-header-subject
8343                                (gnus-data-header (car data))))
8344                            (gnus-simplify-subject-re
8345                             (mail-header-subject
8346                              (gnus-data-header (car data)))))))
8347         (push (gnus-data-number (car data)) articles))
8348       (unless (and (setq data (cdr data))
8349                    (> (gnus-data-level (car data)) top-level))
8350         (setq data nil)))
8351     ;; Return the list of articles.
8352     (nreverse articles)))
8353
8354 (defun gnus-summary-rethread-current ()
8355   "Rethread the thread the current article is part of."
8356   (interactive)
8357   (let* ((gnus-show-threads t)
8358          (article (gnus-summary-article-number))
8359          (id (mail-header-id (gnus-summary-article-header)))
8360          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8361     (unless id
8362       (error "No article on the current line"))
8363     (gnus-rebuild-thread id)
8364     (gnus-summary-goto-subject article)))
8365
8366 (defun gnus-summary-reparent-thread ()
8367   "Make the current article child of the marked (or previous) article.
8368
8369 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8370 is non-nil or the Subject: of both articles are the same."
8371   (interactive)
8372   (unless (not (gnus-group-read-only-p))
8373     (error "The current newsgroup does not support article editing"))
8374   (unless (<= (length gnus-newsgroup-processable) 1)
8375     (error "No more than one article may be marked"))
8376   (save-window-excursion
8377     (let ((gnus-article-buffer " *reparent*")
8378           (current-article (gnus-summary-article-number))
8379           ;; First grab the marked article, otherwise one line up.
8380           (parent-article (if (not (null gnus-newsgroup-processable))
8381                               (car gnus-newsgroup-processable)
8382                             (save-excursion
8383                               (if (eq (forward-line -1) 0)
8384                                   (gnus-summary-article-number)
8385                                 (error "Beginning of summary buffer"))))))
8386       (unless (not (eq current-article parent-article))
8387         (error "An article may not be self-referential"))
8388       (let ((message-id (mail-header-id
8389                          (gnus-summary-article-header parent-article))))
8390         (unless (and message-id (not (equal message-id "")))
8391           (error "No message-id in desired parent"))
8392         (gnus-with-article current-article
8393           (goto-char (point-min))
8394           (if (re-search-forward "^References: " nil t)
8395               (progn
8396                 (re-search-forward "^[^ \t]" nil t)
8397                 (forward-line -1)
8398                 (end-of-line)
8399                 (insert " " message-id))
8400             (insert "References: " message-id "\n")))
8401         (set-buffer gnus-summary-buffer)
8402         (gnus-summary-unmark-all-processable)
8403         (gnus-summary-update-article current-article)
8404         (gnus-summary-rethread-current)
8405         (gnus-message 3 "Article %d is now the child of article %d"
8406                       current-article parent-article)))))
8407
8408 (defun gnus-summary-toggle-threads (&optional arg)
8409   "Toggle showing conversation threads.
8410 If ARG is positive number, turn showing conversation threads on."
8411   (interactive "P")
8412   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8413     (setq gnus-show-threads
8414           (if (null arg) (not gnus-show-threads)
8415             (> (prefix-numeric-value arg) 0)))
8416     (gnus-summary-prepare)
8417     (gnus-summary-goto-subject current)
8418     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8419     (gnus-summary-position-point)))
8420
8421 (defun gnus-summary-show-all-threads ()
8422   "Show all threads."
8423   (interactive)
8424   (save-excursion
8425     (let ((buffer-read-only nil))
8426       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8427   (gnus-summary-position-point))
8428
8429 (defun gnus-summary-show-thread ()
8430   "Show thread subtrees.
8431 Returns nil if no thread was there to be shown."
8432   (interactive)
8433   (let ((buffer-read-only nil)
8434         (orig (point))
8435         ;; first goto end then to beg, to have point at beg after let
8436         (end (progn (end-of-line) (point)))
8437         (beg (progn (beginning-of-line) (point))))
8438     (prog1
8439         ;; Any hidden lines here?
8440         (search-forward "\r" end t)
8441       (subst-char-in-region beg end ?\^M ?\n t)
8442       (goto-char orig)
8443       (gnus-summary-position-point))))
8444
8445 (defun gnus-summary-hide-all-threads ()
8446   "Hide all thread subtrees."
8447   (interactive)
8448   (save-excursion
8449     (goto-char (point-min))
8450     (gnus-summary-hide-thread)
8451     (while (zerop (gnus-summary-next-thread 1 t))
8452       (gnus-summary-hide-thread)))
8453   (gnus-summary-position-point))
8454
8455 (defun gnus-summary-hide-thread ()
8456   "Hide thread subtrees.
8457 Returns nil if no threads were there to be hidden."
8458   (interactive)
8459   (let ((buffer-read-only nil)
8460         (start (point))
8461         (article (gnus-summary-article-number)))
8462     (goto-char start)
8463     ;; Go forward until either the buffer ends or the subthread
8464     ;; ends.
8465     (when (and (not (eobp))
8466                (or (zerop (gnus-summary-next-thread 1 t))
8467                    (goto-char (point-max))))
8468       (prog1
8469           (if (and (> (point) start)
8470                    (search-backward "\n" start t))
8471               (progn
8472                 (subst-char-in-region start (point) ?\n ?\^M)
8473                 (gnus-summary-goto-subject article))
8474             (goto-char start)
8475             nil)
8476         ;;(gnus-summary-position-point)
8477         ))))
8478
8479 (defun gnus-summary-go-to-next-thread (&optional previous)
8480   "Go to the same level (or less) next thread.
8481 If PREVIOUS is non-nil, go to previous thread instead.
8482 Return the article number moved to, or nil if moving was impossible."
8483   (let ((level (gnus-summary-thread-level))
8484         (way (if previous -1 1))
8485         (beg (point)))
8486     (forward-line way)
8487     (while (and (not (eobp))
8488                 (< level (gnus-summary-thread-level)))
8489       (forward-line way))
8490     (if (eobp)
8491         (progn
8492           (goto-char beg)
8493           nil)
8494       (setq beg (point))
8495       (prog1
8496           (gnus-summary-article-number)
8497         (goto-char beg)))))
8498
8499 (defun gnus-summary-next-thread (n &optional silent)
8500   "Go to the same level next N'th thread.
8501 If N is negative, search backward instead.
8502 Returns the difference between N and the number of skips actually
8503 done.
8504
8505 If SILENT, don't output messages."
8506   (interactive "p")
8507   (let ((backward (< n 0))
8508         (n (abs n)))
8509     (while (and (> n 0)
8510                 (gnus-summary-go-to-next-thread backward))
8511       (decf n))
8512     (unless silent
8513       (gnus-summary-position-point))
8514     (when (and (not silent) (/= 0 n))
8515       (gnus-message 7 "No more threads"))
8516     n))
8517
8518 (defun gnus-summary-prev-thread (n)
8519   "Go to the same level previous N'th thread.
8520 Returns the difference between N and the number of skips actually
8521 done."
8522   (interactive "p")
8523   (gnus-summary-next-thread (- n)))
8524
8525 (defun gnus-summary-go-down-thread ()
8526   "Go down one level in the current thread."
8527   (let ((children (gnus-summary-article-children)))
8528     (when children
8529       (gnus-summary-goto-subject (car children)))))
8530
8531 (defun gnus-summary-go-up-thread ()
8532   "Go up one level in the current thread."
8533   (let ((parent (gnus-summary-article-parent)))
8534     (when parent
8535       (gnus-summary-goto-subject parent))))
8536
8537 (defun gnus-summary-down-thread (n)
8538   "Go down thread N steps.
8539 If N is negative, go up instead.
8540 Returns the difference between N and how many steps down that were
8541 taken."
8542   (interactive "p")
8543   (let ((up (< n 0))
8544         (n (abs n)))
8545     (while (and (> n 0)
8546                 (if up (gnus-summary-go-up-thread)
8547                   (gnus-summary-go-down-thread)))
8548       (setq n (1- n)))
8549     (gnus-summary-position-point)
8550     (when (/= 0 n)
8551       (gnus-message 7 "Can't go further"))
8552     n))
8553
8554 (defun gnus-summary-up-thread (n)
8555   "Go up thread N steps.
8556 If N is negative, go up instead.
8557 Returns the difference between N and how many steps down that were
8558 taken."
8559   (interactive "p")
8560   (gnus-summary-down-thread (- n)))
8561
8562 (defun gnus-summary-top-thread ()
8563   "Go to the top of the thread."
8564   (interactive)
8565   (while (gnus-summary-go-up-thread))
8566   (gnus-summary-article-number))
8567
8568 (defun gnus-summary-kill-thread (&optional unmark)
8569   "Mark articles under current thread as read.
8570 If the prefix argument is positive, remove any kinds of marks.
8571 If the prefix argument is negative, tick articles instead."
8572   (interactive "P")
8573   (when unmark
8574     (setq unmark (prefix-numeric-value unmark)))
8575   (let ((articles (gnus-summary-articles-in-thread)))
8576     (save-excursion
8577       ;; Expand the thread.
8578       (gnus-summary-show-thread)
8579       ;; Mark all the articles.
8580       (while articles
8581         (gnus-summary-goto-subject (car articles))
8582         (cond ((null unmark)
8583                (gnus-summary-mark-article-as-read gnus-killed-mark))
8584               ((> unmark 0)
8585                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8586               (t
8587                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8588         (setq articles (cdr articles))))
8589     ;; Hide killed subtrees.
8590     (and (null unmark)
8591          gnus-thread-hide-killed
8592          (gnus-summary-hide-thread))
8593     ;; If marked as read, go to next unread subject.
8594     (when (null unmark)
8595       ;; Go to next unread subject.
8596       (gnus-summary-next-subject 1 t)))
8597   (gnus-set-mode-line 'summary))
8598
8599 ;; Summary sorting commands
8600
8601 (defun gnus-summary-sort-by-number (&optional reverse)
8602   "Sort the summary buffer by article number.
8603 Argument REVERSE means reverse order."
8604   (interactive "P")
8605   (gnus-summary-sort 'number reverse))
8606
8607 (defun gnus-summary-sort-by-author (&optional reverse)
8608   "Sort the summary buffer by author name alphabetically.
8609 If case-fold-search is non-nil, case of letters is ignored.
8610 Argument REVERSE means reverse order."
8611   (interactive "P")
8612   (gnus-summary-sort 'author reverse))
8613
8614 (defun gnus-summary-sort-by-subject (&optional reverse)
8615   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8616 If case-fold-search is non-nil, case of letters is ignored.
8617 Argument REVERSE means reverse order."
8618   (interactive "P")
8619   (gnus-summary-sort 'subject reverse))
8620
8621 (defun gnus-summary-sort-by-date (&optional reverse)
8622   "Sort the summary buffer by date.
8623 Argument REVERSE means reverse order."
8624   (interactive "P")
8625   (gnus-summary-sort 'date reverse))
8626
8627 (defun gnus-summary-sort-by-score (&optional reverse)
8628   "Sort the summary buffer by score.
8629 Argument REVERSE means reverse order."
8630   (interactive "P")
8631   (gnus-summary-sort 'score reverse))
8632
8633 (defun gnus-summary-sort-by-lines (&optional reverse)
8634   "Sort the summary buffer by the number of lines.
8635 Argument REVERSE means reverse order."
8636   (interactive "P")
8637   (gnus-summary-sort 'lines reverse))
8638
8639 (defun gnus-summary-sort-by-chars (&optional reverse)
8640   "Sort the summary buffer by article length.
8641 Argument REVERSE means reverse order."
8642   (interactive "P")
8643   (gnus-summary-sort 'chars reverse))   
8644
8645 (defun gnus-summary-sort (predicate reverse)
8646   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8647   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8648          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8649          (gnus-thread-sort-functions
8650           (if (not reverse)
8651               thread
8652             `(lambda (t1 t2)
8653                (,thread t2 t1))))
8654          (gnus-article-sort-functions
8655           (if (not reverse)
8656               article
8657             `(lambda (t1 t2)
8658                (,article t2 t1))))
8659          (buffer-read-only)
8660          (gnus-summary-prepare-hook nil))
8661     ;; We do the sorting by regenerating the threads.
8662     (gnus-summary-prepare)
8663     ;; Hide subthreads if needed.
8664     (when (and gnus-show-threads gnus-thread-hide-subtree)
8665       (gnus-summary-hide-all-threads))))
8666
8667 ;; Summary saving commands.
8668
8669 (defun gnus-summary-save-article (&optional n not-saved)
8670   "Save the current article using the default saver function.
8671 If N is a positive number, save the N next articles.
8672 If N is a negative number, save the N previous articles.
8673 If N is nil and any articles have been marked with the process mark,
8674 save those articles instead.
8675 The variable `gnus-default-article-saver' specifies the saver function."
8676   (interactive "P")
8677   (let* ((articles (gnus-summary-work-articles n))
8678          (save-buffer (save-excursion
8679                         (nnheader-set-temp-buffer " *Gnus Save*")))
8680          (num (length articles))
8681          header file)
8682     (dolist (article articles)
8683       (setq header (gnus-summary-article-header article))
8684       (if (not (vectorp header))
8685           ;; This is a pseudo-article.
8686           (if (assq 'name header)
8687               (gnus-copy-file (cdr (assq 'name header)))
8688             (gnus-message 1 "Article %d is unsaveable" article))
8689         ;; This is a real article.
8690         (save-window-excursion
8691           (gnus-summary-select-article t nil nil article))
8692         (save-excursion
8693           (set-buffer save-buffer)
8694           (erase-buffer)
8695           (insert-buffer-substring gnus-original-article-buffer))
8696         (setq file (gnus-article-save save-buffer file num))
8697         (gnus-summary-remove-process-mark article)
8698         (unless not-saved
8699           (gnus-summary-set-saved-mark article))))
8700     (gnus-kill-buffer save-buffer)
8701     (gnus-summary-position-point)
8702     (gnus-set-mode-line 'summary)
8703     n))
8704
8705 (defun gnus-summary-pipe-output (&optional arg)
8706   "Pipe the current article to a subprocess.
8707 If N is a positive number, pipe the N next articles.
8708 If N is a negative number, pipe the N previous articles.
8709 If N is nil and any articles have been marked with the process mark,
8710 pipe those articles instead."
8711   (interactive "P")
8712   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8713     (gnus-summary-save-article arg t))
8714   (gnus-configure-windows 'pipe))
8715
8716 (defun gnus-summary-save-article-mail (&optional arg)
8717   "Append the current article to an mail file.
8718 If N is a positive number, save the N next articles.
8719 If N is a negative number, save the N previous articles.
8720 If N is nil and any articles have been marked with the process mark,
8721 save those articles instead."
8722   (interactive "P")
8723   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8724     (gnus-summary-save-article arg)))
8725
8726 (defun gnus-summary-save-article-rmail (&optional arg)
8727   "Append the current article to an rmail file.
8728 If N is a positive number, save the N next articles.
8729 If N is a negative number, save the N previous articles.
8730 If N is nil and any articles have been marked with the process mark,
8731 save those articles instead."
8732   (interactive "P")
8733   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8734     (gnus-summary-save-article arg)))
8735
8736 (defun gnus-summary-save-article-file (&optional arg)
8737   "Append the current article to a file.
8738 If N is a positive number, save the N next articles.
8739 If N is a negative number, save the N previous articles.
8740 If N is nil and any articles have been marked with the process mark,
8741 save those articles instead."
8742   (interactive "P")
8743   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8744     (gnus-summary-save-article arg)))
8745
8746 (defun gnus-summary-write-article-file (&optional arg)
8747   "Write the current article to a file, deleting the previous file.
8748 If N is a positive number, save the N next articles.
8749 If N is a negative number, save the N previous articles.
8750 If N is nil and any articles have been marked with the process mark,
8751 save those articles instead."
8752   (interactive "P")
8753   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8754     (gnus-summary-save-article arg)))
8755
8756 (defun gnus-summary-save-article-body-file (&optional arg)
8757   "Append the current article body to a file.
8758 If N is a positive number, save the N next articles.
8759 If N is a negative number, save the N previous articles.
8760 If N is nil and any articles have been marked with the process mark,
8761 save those articles instead."
8762   (interactive "P")
8763   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8764     (gnus-summary-save-article arg)))
8765
8766 (defun gnus-summary-pipe-message (program)
8767   "Pipe the current article through PROGRAM."
8768   (interactive "sProgram: ")
8769   (gnus-summary-select-article)
8770   (let ((mail-header-separator ""))
8771     (gnus-eval-in-buffer-window gnus-article-buffer
8772       (save-restriction
8773         (widen)
8774         (let ((start (window-start))
8775               buffer-read-only)
8776           (message-pipe-buffer-body program)
8777           (set-window-start (get-buffer-window (current-buffer)) start))))))
8778
8779 (defun gnus-get-split-value (methods)
8780   "Return a value based on the split METHODS."
8781   (let (split-name method result match)
8782     (when methods
8783       (save-excursion
8784         (set-buffer gnus-original-article-buffer)
8785         (save-restriction
8786           (nnheader-narrow-to-headers)
8787           (while methods
8788             (goto-char (point-min))
8789             (setq method (pop methods))
8790             (setq match (car method))
8791             (when (cond
8792                    ((stringp match)
8793                     ;; Regular expression.
8794                     (ignore-errors
8795                       (re-search-forward match nil t)))
8796                    ((gnus-functionp match)
8797                     ;; Function.
8798                     (save-restriction
8799                       (widen)
8800                       (setq result (funcall match gnus-newsgroup-name))))
8801                    ((consp match)
8802                     ;; Form.
8803                     (save-restriction
8804                       (widen)
8805                       (setq result (eval match)))))
8806               (setq split-name (append (cdr method) split-name))
8807               (cond ((stringp result)
8808                      (push (expand-file-name
8809                             result gnus-article-save-directory)
8810                            split-name))
8811                     ((consp result)
8812                      (setq split-name (append result split-name)))))))))
8813     (nreverse split-name)))
8814
8815 (defun gnus-valid-move-group-p (group)
8816   (and (boundp group)
8817        (symbol-name group)
8818        (symbol-value group)
8819        (gnus-get-function (gnus-find-method-for-group
8820                            (symbol-name group)) 'request-accept-article t)))
8821
8822 (defun gnus-read-move-group-name (prompt default articles prefix)
8823   "Read a group name."
8824   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8825          (minibuffer-confirm-incomplete nil) ; XEmacs
8826          (prom
8827           (format "%s %s to:"
8828                   prompt
8829                   (if (> (length articles) 1)
8830                       (format "these %d articles" (length articles))
8831                     "this article")))
8832          (to-newsgroup
8833           (cond
8834            ((null split-name)
8835             (gnus-completing-read default prom
8836                                   gnus-active-hashtb
8837                                   'gnus-valid-move-group-p
8838                                   nil prefix
8839                                   'gnus-group-history))
8840            ((= 1 (length split-name))
8841             (gnus-completing-read (car split-name) prom
8842                                   gnus-active-hashtb
8843                                   'gnus-valid-move-group-p
8844                                   nil nil
8845                                   'gnus-group-history))
8846            (t
8847             (gnus-completing-read nil prom
8848                                   (mapcar (lambda (el) (list el))
8849                                           (nreverse split-name))
8850                                   nil nil nil
8851                                   'gnus-group-history)))))
8852     (when to-newsgroup
8853       (if (or (string= to-newsgroup "")
8854               (string= to-newsgroup prefix))
8855           (setq to-newsgroup default))
8856       (unless to-newsgroup
8857         (error "No group name entered"))
8858       (or (gnus-active to-newsgroup)
8859           (gnus-activate-group to-newsgroup)
8860           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8861                                      to-newsgroup))
8862               (or (and (gnus-request-create-group
8863                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8864                        (gnus-activate-group to-newsgroup nil nil
8865                                             (gnus-group-name-to-method
8866                                              to-newsgroup)))
8867                   (error "Couldn't create group %s" to-newsgroup)))
8868           (error "No such group: %s" to-newsgroup)))
8869     to-newsgroup))
8870
8871 (defun gnus-summary-save-parts (type dir n reverse)
8872   "Save parts matching TYPE to DIR.
8873 If REVERSE, save parts that do not match TYPE."
8874   (interactive
8875    (list (read-string "Save parts of type: " "image/.*")
8876          (read-file-name "Save to directory: " t nil t)
8877          current-prefix-arg))
8878   (gnus-summary-iterate n
8879     (let ((gnus-display-mime-function nil)
8880           (gnus-inhibit-treatment t))
8881       (gnus-summary-select-article))
8882     (save-excursion
8883       (set-buffer gnus-article-buffer)
8884       (let ((handles (or (mm-dissect-buffer) (mm-uu-dissect))))
8885         (when handles
8886           (gnus-summary-save-parts-1 type dir handles reverse))))))
8887
8888 (defun gnus-summary-save-parts-1 (type dir handle reverse)
8889   (if (stringp (car handle))
8890       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
8891               (cdr handle))
8892     (when (if reverse
8893               (not (string-match type (car (mm-handle-type handle))))
8894             (string-match type (car (mm-handle-type handle))))
8895       (let ((file (expand-file-name
8896                    (file-name-nondirectory
8897                     (or
8898                      (mail-content-type-get
8899                       (mm-handle-disposition handle) 'filename)
8900                      (concat gnus-newsgroup-name "." gnus-current-article)))
8901                    dir)))
8902         (unless (file-exists-p file)
8903           (mm-save-part-to-file handle file))))))
8904
8905 ;; Summary extract commands
8906
8907 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8908   (let ((buffer-read-only nil)
8909         (article (gnus-summary-article-number))
8910         after-article b e)
8911     (unless (gnus-summary-goto-subject article)
8912       (error "No such article: %d" article))
8913     (gnus-summary-position-point)
8914     ;; If all commands are to be bunched up on one line, we collect
8915     ;; them here.
8916     (unless gnus-view-pseudos-separately
8917       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8918             files action)
8919         (while ps
8920           (setq action (cdr (assq 'action (car ps))))
8921           (setq files (list (cdr (assq 'name (car ps)))))
8922           (while (and ps (cdr ps)
8923                       (string= (or action "1")
8924                                (or (cdr (assq 'action (cadr ps))) "2")))
8925             (push (cdr (assq 'name (cadr ps))) files)
8926             (setcdr ps (cddr ps)))
8927           (when files
8928             (when (not (string-match "%s" action))
8929               (push " " files))
8930             (push " " files)
8931             (when (assq 'execute (car ps))
8932               (setcdr (assq 'execute (car ps))
8933                       (funcall (if (string-match "%s" action)
8934                                    'format 'concat)
8935                                action
8936                                (mapconcat
8937                                 (lambda (f)
8938                                   (if (equal f " ")
8939                                       f
8940                                     (gnus-quote-arg-for-sh-or-csh f)))
8941                                 files " ")))))
8942           (setq ps (cdr ps)))))
8943     (if (and gnus-view-pseudos (not not-view))
8944         (while pslist
8945           (when (assq 'execute (car pslist))
8946             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8947                                   (eq gnus-view-pseudos 'not-confirm)))
8948           (setq pslist (cdr pslist)))
8949       (save-excursion
8950         (while pslist
8951           (setq after-article (or (cdr (assq 'article (car pslist)))
8952                                   (gnus-summary-article-number)))
8953           (gnus-summary-goto-subject after-article)
8954           (forward-line 1)
8955           (setq b (point))
8956           (insert "    " (file-name-nondirectory
8957                           (cdr (assq 'name (car pslist))))
8958                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8959           (setq e (point))
8960           (forward-line -1)             ; back to `b'
8961           (gnus-add-text-properties
8962            b (1- e) (list 'gnus-number gnus-reffed-article-number
8963                           gnus-mouse-face-prop gnus-mouse-face))
8964           (gnus-data-enter
8965            after-article gnus-reffed-article-number
8966            gnus-unread-mark b (car pslist) 0 (- e b))
8967           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8968           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8969           (setq pslist (cdr pslist)))))))
8970
8971 (defun gnus-pseudos< (p1 p2)
8972   (let ((c1 (cdr (assq 'action p1)))
8973         (c2 (cdr (assq 'action p2))))
8974     (and c1 c2 (string< c1 c2))))
8975
8976 (defun gnus-request-pseudo-article (props)
8977   (cond ((assq 'execute props)
8978          (gnus-execute-command (cdr (assq 'execute props)))))
8979   (let ((gnus-current-article (gnus-summary-article-number)))
8980     (gnus-run-hooks 'gnus-mark-article-hook)))
8981
8982 (defun gnus-execute-command (command &optional automatic)
8983   (save-excursion
8984     (gnus-article-setup-buffer)
8985     (set-buffer gnus-article-buffer)
8986     (setq buffer-read-only nil)
8987     (let ((command (if automatic command
8988                      (read-string "Command: " (cons command 0)))))
8989       (erase-buffer)
8990       (insert "$ " command "\n\n")
8991       (if gnus-view-pseudo-asynchronously
8992           (start-process "gnus-execute" (current-buffer) shell-file-name
8993                          shell-command-switch command)
8994         (call-process shell-file-name nil t nil
8995                       shell-command-switch command)))))
8996
8997 ;; Summary kill commands.
8998
8999 (defun gnus-summary-edit-global-kill (article)
9000   "Edit the \"global\" kill file."
9001   (interactive (list (gnus-summary-article-number)))
9002   (gnus-group-edit-global-kill article))
9003
9004 (defun gnus-summary-edit-local-kill ()
9005   "Edit a local kill file applied to the current newsgroup."
9006   (interactive)
9007   (setq gnus-current-headers (gnus-summary-article-header))
9008   (gnus-group-edit-local-kill
9009    (gnus-summary-article-number) gnus-newsgroup-name))
9010
9011 ;;; Header reading.
9012
9013 (defun gnus-read-header (id &optional header)
9014   "Read the headers of article ID and enter them into the Gnus system."
9015   (let ((group gnus-newsgroup-name)
9016         (gnus-override-method
9017          (and (gnus-news-group-p gnus-newsgroup-name)
9018               gnus-refer-article-method))
9019         where)
9020     ;; First we check to see whether the header in question is already
9021     ;; fetched.
9022     (if (stringp id)
9023         ;; This is a Message-ID.
9024         (setq header (or header (gnus-id-to-header id)))
9025       ;; This is an article number.
9026       (setq header (or header (gnus-summary-article-header id))))
9027     (if (and header
9028              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9029         ;; We have found the header.
9030         header
9031       ;; If this is a sparse article, we have to nix out its
9032       ;; previous entry in the thread hashtb.
9033       (when (and header
9034                  (gnus-summary-article-sparse-p (mail-header-number header)))
9035         (let* ((parent (gnus-parent-id (mail-header-references header)))
9036                (thread (and parent (gnus-id-to-thread parent))))
9037           (when thread
9038             (delq (assq header thread) thread))))
9039       ;; We have to really fetch the header to this article.
9040       (save-excursion
9041         (set-buffer nntp-server-buffer)
9042         (when (setq where (gnus-request-head id group))
9043           (nnheader-fold-continuation-lines)
9044           (goto-char (point-max))
9045           (insert ".\n")
9046           (goto-char (point-min))
9047           (insert "211 ")
9048           (princ (cond
9049                   ((numberp id) id)
9050                   ((cdr where) (cdr where))
9051                   (header (mail-header-number header))
9052                   (t gnus-reffed-article-number))
9053                  (current-buffer))
9054           (insert " Article retrieved.\n"))
9055         (if (or (not where)
9056                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9057             ()                          ; Malformed head.
9058           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9059             (when (and (stringp id)
9060                        (not (string= (gnus-group-real-name group)
9061                                      (car where))))
9062               ;; If we fetched by Message-ID and the article came
9063               ;; from a different group, we fudge some bogus article
9064               ;; numbers for this article.
9065               (mail-header-set-number header gnus-reffed-article-number))
9066             (save-excursion
9067               (set-buffer gnus-summary-buffer)
9068               (decf gnus-reffed-article-number)
9069               (gnus-remove-header (mail-header-number header))
9070               (push header gnus-newsgroup-headers)
9071               (setq gnus-current-headers header)
9072               (push (mail-header-number header) gnus-newsgroup-limit)))
9073           header)))))
9074
9075 (defun gnus-remove-header (number)
9076   "Remove header NUMBER from `gnus-newsgroup-headers'."
9077   (if (and gnus-newsgroup-headers
9078            (= number (mail-header-number (car gnus-newsgroup-headers))))
9079       (pop gnus-newsgroup-headers)
9080     (let ((headers gnus-newsgroup-headers))
9081       (while (and (cdr headers)
9082                   (not (= number (mail-header-number (cadr headers)))))
9083         (pop headers))
9084       (when (cdr headers)
9085         (setcdr headers (cddr headers))))))
9086
9087 ;;;
9088 ;;; summary highlights
9089 ;;;
9090
9091 (defun gnus-highlight-selected-summary ()
9092   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9093   ;; Highlight selected article in summary buffer
9094   (when gnus-summary-selected-face
9095     (save-excursion
9096       (let* ((beg (progn (beginning-of-line) (point)))
9097              (end (progn (end-of-line) (point)))
9098              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9099              (from (if (get-text-property beg gnus-mouse-face-prop)
9100                        beg
9101                      (or (next-single-property-change
9102                           beg gnus-mouse-face-prop nil end)
9103                          beg)))
9104              (to
9105               (if (= from end)
9106                   (- from 2)
9107                 (or (next-single-property-change
9108                      from gnus-mouse-face-prop nil end)
9109                     end))))
9110         ;; If no mouse-face prop on line we will have to = from = end,
9111         ;; so we highlight the entire line instead.
9112         (when (= (+ to 2) from)
9113           (setq from beg)
9114           (setq to end))
9115         (if gnus-newsgroup-selected-overlay
9116             ;; Move old overlay.
9117             (gnus-move-overlay
9118              gnus-newsgroup-selected-overlay from to (current-buffer))
9119           ;; Create new overlay.
9120           (gnus-overlay-put
9121            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9122            'face gnus-summary-selected-face))))))
9123
9124 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9125 (defun gnus-summary-highlight-line ()
9126   "Highlight current line according to `gnus-summary-highlight'."
9127   (let* ((list gnus-summary-highlight)
9128          (p (point))
9129          (end (progn (end-of-line) (point)))
9130          ;; now find out where the line starts and leave point there.
9131          (beg (progn (beginning-of-line) (point)))
9132          (article (gnus-summary-article-number))
9133          (score (or (cdr (assq (or article gnus-current-article)
9134                                gnus-newsgroup-scored))
9135                     gnus-summary-default-score 0))
9136          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9137          (inhibit-read-only t))
9138     ;; Eval the cars of the lists until we find a match.
9139     (let ((default gnus-summary-default-score))
9140       (while (and list
9141                   (not (eval (caar list))))
9142         (setq list (cdr list))))
9143     (let ((face (cdar list)))
9144       (unless (eq face (get-text-property beg 'face))
9145         (gnus-put-text-property-excluding-characters-with-faces
9146          beg end 'face
9147          (setq face (if (boundp face) (symbol-value face) face)))
9148         (when gnus-summary-highlight-line-function
9149           (funcall gnus-summary-highlight-line-function article face))))
9150     (goto-char p)))
9151
9152 (defun gnus-update-read-articles (group unread &optional compute)
9153   "Update the list of read articles in GROUP."
9154   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9155          (entry (gnus-gethash group gnus-newsrc-hashtb))
9156          (info (nth 2 entry))
9157          (prev 1)
9158          (unread (sort (copy-sequence unread) '<))
9159          read)
9160     (if (or (not info) (not active))
9161         ;; There is no info on this group if it was, in fact,
9162         ;; killed.  Gnus stores no information on killed groups, so
9163         ;; there's nothing to be done.
9164         ;; One could store the information somewhere temporarily,
9165         ;; perhaps...  Hmmm...
9166         ()
9167       ;; Remove any negative articles numbers.
9168       (while (and unread (< (car unread) 0))
9169         (setq unread (cdr unread)))
9170       ;; Remove any expired article numbers
9171       (while (and unread (< (car unread) (car active)))
9172         (setq unread (cdr unread)))
9173       ;; Compute the ranges of read articles by looking at the list of
9174       ;; unread articles.
9175       (while unread
9176         (when (/= (car unread) prev)
9177           (push (if (= prev (1- (car unread))) prev
9178                   (cons prev (1- (car unread))))
9179                 read))
9180         (setq prev (1+ (car unread)))
9181         (setq unread (cdr unread)))
9182       (when (<= prev (cdr active))
9183         (push (cons prev (cdr active)) read))
9184       (setq read (if (> (length read) 1) (nreverse read) read))
9185       (if compute
9186           read
9187         (save-excursion
9188           (set-buffer gnus-group-buffer)
9189           (gnus-undo-register
9190             `(progn
9191                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9192                (gnus-info-set-read ',info ',(gnus-info-read info))
9193                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
9194                (gnus-group-update-group ,group t))))
9195        ;; Propagate the read marks to the backend.
9196        (if (gnus-check-backend-function 'request-set-mark group)
9197            (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9198                  (add (gnus-remove-from-range read (gnus-info-read info))))
9199              (when (or add del)
9200                (unless (gnus-check-group group)
9201                  (error "Can't open server for %s" group))
9202                (gnus-request-set-mark
9203                 group (delq nil (list (if add (list add 'add '(read)))
9204                                       (if del (list del 'del '(read)))))))))
9205         ;; Enter this list into the group info.
9206         (gnus-info-set-read info read)
9207         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9208         (gnus-get-unread-articles-in-group info (gnus-active group))
9209         t))))
9210
9211 (defun gnus-offer-save-summaries ()
9212   "Offer to save all active summary buffers."
9213   (save-excursion
9214     (let ((buflist (buffer-list))
9215           buffers bufname)
9216       ;; Go through all buffers and find all summaries.
9217       (while buflist
9218         (and (setq bufname (buffer-name (car buflist)))
9219              (string-match "Summary" bufname)
9220              (save-excursion
9221                (set-buffer bufname)
9222                ;; We check that this is, indeed, a summary buffer.
9223                (and (eq major-mode 'gnus-summary-mode)
9224                     ;; Also make sure this isn't bogus.
9225                     gnus-newsgroup-prepared
9226                     ;; Also make sure that this isn't a dead summary buffer.
9227                     (not gnus-dead-summary-mode)))
9228              (push bufname buffers))
9229         (setq buflist (cdr buflist)))
9230       ;; Go through all these summary buffers and offer to save them.
9231       (when buffers
9232         (map-y-or-n-p
9233          "Update summary buffer %s? "
9234          (lambda (buf)
9235            (switch-to-buffer buf)
9236            (gnus-summary-exit))
9237          buffers)))))
9238
9239
9240 ;;; @ for mime-partial
9241 ;;;
9242
9243 (defun gnus-request-partial-message ()
9244   (save-excursion
9245     (let ((number (gnus-summary-article-number))
9246           (group gnus-newsgroup-name)
9247           (mother gnus-article-buffer))
9248       (set-buffer (get-buffer-create " *Partial Article*"))
9249       (erase-buffer)
9250       (setq mime-preview-buffer mother)
9251       (gnus-request-article-this-buffer number group)
9252       (mime-parse-buffer)
9253       )))
9254
9255 (autoload 'mime-combine-message/partial-pieces-automatically
9256   "mime-partial"
9257   "Internal method to combine message/partial messages automatically.")
9258
9259 (mime-add-condition
9260  'action '((type . message)(subtype . partial)
9261            (major-mode . gnus-original-article-mode)
9262            (method . mime-combine-message/partial-pieces-automatically)
9263            (summary-buffer-exp . gnus-summary-buffer)
9264            (request-partial-message-method . gnus-request-partial-message)
9265            ))
9266
9267
9268 ;;; @ for message/rfc822
9269 ;;;
9270
9271 (defun gnus-mime-extract-message/rfc822 (entity situation)
9272   (let (group article num cwin swin cur)
9273     (with-current-buffer (mime-entity-buffer entity)
9274       (save-restriction
9275         (narrow-to-region (mime-entity-body-start entity)
9276                           (mime-entity-body-end entity))
9277         (setq group (or (cdr (assq 'group situation))
9278                         (completing-read "Group: "
9279                                          gnus-active-hashtb
9280                                          nil
9281                                          (gnus-read-active-file-p)
9282                                          gnus-newsgroup-name))
9283               article (gnus-request-accept-article group)
9284               )
9285         ))
9286     (when (and (consp article)
9287                (numberp (setq article (cdr article))))
9288       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9289             cwin (get-buffer-window (current-buffer) t)
9290             )
9291       (save-window-excursion
9292         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9293             (select-window swin)
9294           (set-buffer gnus-summary-buffer)
9295           )
9296         (setq cur gnus-current-article)
9297         (forward-line num)
9298         (let (gnus-show-threads)
9299           (gnus-summary-goto-subject article t)
9300           )
9301         (gnus-summary-clear-mark-forward 1)
9302         (gnus-summary-goto-subject cur)
9303         )
9304       (when (and cwin (window-frame cwin))
9305         (select-frame (window-frame cwin))
9306         )
9307       (when (boundp 'mime-acting-situation-to-override)
9308         (set-alist 'mime-acting-situation-to-override
9309                    'group
9310                    group)
9311         (set-alist 'mime-acting-situation-to-override
9312                    'after-method
9313                    `(progn
9314                       (save-current-buffer
9315                         (set-buffer gnus-group-buffer)
9316                         (gnus-activate-group ,group)
9317                         )
9318                       (gnus-summary-goto-article ,cur
9319                                                  gnus-show-all-headers)
9320                       ))
9321         (set-alist 'mime-acting-situation-to-override
9322                    'number num)
9323         )
9324       )))
9325
9326 (mime-add-condition
9327  'action '((type . message)(subtype . rfc822)
9328            (major-mode . gnus-original-article-mode)
9329            (method . gnus-mime-extract-message/rfc822)
9330            (mode . "extract")
9331            ))
9332
9333 (mime-add-condition
9334  'action '((type . message)(subtype . news)
9335            (major-mode . gnus-original-article-mode)
9336            (method . gnus-mime-extract-message/rfc822)
9337            (mode . "extract")
9338            ))
9339
9340 (defun gnus-mime-extract-multipart (entity situation)
9341   (let ((children (mime-entity-children entity))
9342         mime-acting-situation-to-override
9343         f)
9344     (while children
9345       (mime-play-entity (car children)
9346                         (cons (assq 'mode situation)
9347                               mime-acting-situation-to-override))
9348       (setq children (cdr children)))
9349     (if (setq f (cdr (assq 'after-method
9350                            mime-acting-situation-to-override)))
9351         (eval f)
9352       )))  
9353
9354 (mime-add-condition
9355  'action '((type . multipart)
9356            (method . gnus-mime-extract-multipart)
9357            (mode . "extract")
9358            )
9359  'with-default)
9360
9361
9362 ;;; @ end
9363 ;;;
9364
9365 (defun gnus-summary-setup-default-charset ()
9366   "Setup newsgroup default charset."
9367   (let ((name (and gnus-newsgroup-name
9368                    (gnus-group-real-name gnus-newsgroup-name))))
9369     (setq gnus-newsgroup-charset
9370           (or (and gnus-newsgroup-name
9371                    (or (gnus-group-find-parameter gnus-newsgroup-name
9372                                                   'charset)
9373                        (let ((alist gnus-group-charset-alist)
9374                              elem (charset nil))
9375                          (while (setq elem (pop alist))
9376                            (when (and name
9377                                       (string-match (car elem) name))
9378                              (setq alist nil
9379                                    charset (cadr elem))))
9380                          charset)))
9381               gnus-default-charset))))
9382
9383 ;;;
9384 ;;; Mime Commands
9385 ;;;
9386
9387 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9388   "Display the current article buffer fully MIME-buttonized.
9389 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9390 treated as multipart/mixed."
9391   (interactive "P")
9392   (require 'gnus-art)
9393   (let ((gnus-unbuttonized-mime-types nil)
9394         (gnus-mime-display-multipart-as-mixed show-all-parts))
9395     (gnus-summary-show-article)))
9396
9397 (defun gnus-summary-repair-multipart (article)
9398   "Add a Content-Type header to a multipart article without one."
9399   (interactive (list (gnus-summary-article-number)))
9400   (gnus-with-article article
9401     (message-narrow-to-head)
9402     (goto-char (point-max))
9403     (widen)
9404     (when (search-forward "\n--" nil t)
9405       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9406         (message-narrow-to-head)
9407         (message-remove-header "Mime-Version")
9408         (message-remove-header "Content-Type")
9409         (goto-char (point-max))
9410         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9411                         separator))
9412         (insert "Mime-Version: 1.0\n")
9413         (widen))))
9414   (let (gnus-mark-article-hook)
9415     (gnus-summary-select-article t t nil article)))
9416
9417 (defun gnus-summary-toggle-display-buttonized ()
9418   "Toggle the buttonizing of the article buffer."
9419   (interactive)
9420   (require 'gnus-art)
9421   (if (setq gnus-inhibit-mime-unbuttonizing
9422             (not gnus-inhibit-mime-unbuttonizing))
9423       (let ((gnus-unbuttonized-mime-types nil))
9424         (gnus-summary-show-article))
9425     (gnus-summary-show-article)))
9426
9427 ;;;
9428 ;;; with article
9429 ;;;
9430
9431 (defmacro gnus-with-article (article &rest forms)
9432   "Select ARTICLE and perform FORMS in the original article buffer.
9433 Then replace the article with the result."
9434   `(progn
9435      ;; We don't want the article to be marked as read.
9436      (let (gnus-mark-article-hook)
9437        (gnus-summary-select-article t t nil ,article))
9438      (set-buffer gnus-original-article-buffer)
9439      ,@forms
9440      (if (not (gnus-check-backend-function
9441                'request-replace-article (car gnus-article-current)))
9442          (gnus-message 5 "Read-only group; not replacing")
9443        (unless (gnus-request-replace-article
9444                 ,article (car gnus-article-current)
9445                 (current-buffer) t)
9446          (error "Couldn't replace article")))
9447      ;; The cache and backlog have to be flushed somewhat.
9448      (when gnus-keep-backlog
9449        (gnus-backlog-remove-article
9450         (car gnus-article-current) (cdr gnus-article-current)))
9451      (when gnus-use-cache
9452        (gnus-cache-update-article
9453         (car gnus-article-current) (cdr gnus-article-current)))))
9454
9455 (put 'gnus-with-article 'lisp-indent-function 1)
9456 (put 'gnus-with-article 'edebug-form-spec '(form body))
9457
9458 ;;;
9459 ;;; Generic summary marking commands
9460 ;;;
9461
9462 (defvar gnus-summary-marking-alist
9463   '((read gnus-del-mark "d")
9464     (unread gnus-unread-mark "u")
9465     (ticked gnus-ticked-mark "!")
9466     (dormant gnus-dormant-mark "?")
9467     (expirable gnus-expirable-mark "e"))
9468   "An alist of names/marks/keystrokes.")
9469
9470 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
9471 (defvar gnus-summary-mark-map)
9472
9473 (defun gnus-summary-make-all-marking-commands ()
9474   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
9475   (dolist (elem gnus-summary-marking-alist)
9476     (apply 'gnus-summary-make-marking-command elem)))
9477
9478 (defun gnus-summary-make-marking-command (name mark keystroke)
9479   (let ((map (make-sparse-keymap)))
9480     (define-key gnus-summary-generic-mark-map keystroke map)
9481     (dolist (lway `((next "next" next nil "n")
9482                     (next-unread "next unread" next t "N")
9483                     (prev "previous" prev nil "p")
9484                     (prev-unread "previous unread" prev t "P")
9485                     (nomove "" nil nil ,keystroke)))
9486       (let ((func (gnus-summary-make-marking-command-1
9487                    mark (car lway) lway name)))
9488         (setq func (eval func))
9489         (define-key map (nth 4 lway) func)))))
9490       
9491 (defun gnus-summary-make-marking-command-1 (mark way lway name)      
9492   `(defun ,(intern
9493             (format "gnus-summary-put-mark-as-%s%s"
9494                     name (if (eq way 'nomove)
9495                              ""
9496                            (concat "-" (symbol-name way)))))
9497      (n)
9498      ,(format
9499        "Mark the current article as %s%s.
9500 If N, the prefix, then repeat N times.
9501 If N is negative, move in reverse order.
9502 The difference between N and the actual number of articles marked is
9503 returned."
9504        name (cadr lway))
9505      (interactive "p")
9506      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
9507     
9508 (defun gnus-summary-generic-mark (n mark move unread)
9509   "Mark N articles with MARK."
9510   (unless (eq major-mode 'gnus-summary-mode)
9511     (error "This command can only be used in the summary buffer"))
9512   (gnus-summary-show-thread)
9513   (let ((nummove
9514          (cond
9515           ((eq move 'next) 1)
9516           ((eq move 'prev) -1)
9517           (t 0))))
9518     (if (zerop nummove)
9519         (setq n 1)
9520       (when (< n 0)
9521         (setq n (abs n)
9522               nummove (* -1 nummove))))
9523     (while (and (> n 0)
9524                 (gnus-summary-mark-article nil mark)
9525                 (zerop (gnus-summary-next-subject nummove unread t)))
9526       (setq n (1- n)))
9527     (when (/= 0 n)
9528       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9529     (gnus-summary-recenter)
9530     (gnus-summary-position-point)
9531     (gnus-set-mode-line 'summary)
9532     n))
9533
9534 ;; Added by Shenghuo Zhu <zsh@cs.rochester.edu>
9535 (defun gnus-summary-setup-highlight-words (&optional highlight-words)
9536   "Setup newsgroup emphasis alist."
9537   (let ((name (and gnus-newsgroup-name
9538                    (gnus-group-real-name gnus-newsgroup-name))))
9539     (setq gnus-newsgroup-emphasis-alist 
9540           (nconc 
9541            (let ((alist gnus-group-highlight-words-alist) elem highlight)
9542              (while (setq elem (pop alist))
9543                (when (and name (string-match (car elem) name))
9544                  (setq alist nil
9545                        highlight (copy-list (cdr elem)))))
9546              highlight)
9547            (copy-list highlight-words)
9548            (if gnus-newsgroup-name
9549                (copy-list (gnus-group-find-parameter 
9550                            gnus-newsgroup-name 'highlight-words t)))
9551            gnus-emphasis-alist))))
9552
9553 (gnus-summary-make-all-marking-commands)
9554
9555 (gnus-ems-redefine)
9556
9557 (provide 'gnus-sum)
9558
9559 (run-hooks 'gnus-sum-load-hook)
9560
9561 ;;; gnus-sum.el ends here