6748ce7560e9a2131b6e1a36212827bc8174d883
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
40 (autoload 'gnus-cache-write-active "gnus-cache")
41
42 (defcustom gnus-kill-summary-on-exit t
43   "*If non-nil, kill the summary buffer when you exit from it.
44 If nil, the summary will become a \"*Dead Summary*\" buffer, and
45 it will be killed sometime later."
46   :group 'gnus-summary-exit
47   :type 'boolean)
48
49 (defcustom gnus-fetch-old-headers nil
50   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
51 If an unread article in the group refers to an older, already read (or
52 just marked as read) article, the old article will not normally be
53 displayed in the Summary buffer.  If this variable is non-nil, Gnus
54 will attempt to grab the headers to the old articles, and thereby
55 build complete threads.  If it has the value `some', only enough
56 headers to connect otherwise loose threads will be displayed.  This
57 variable can also be a number.  In that case, no more than that number
58 of old headers will be fetched.  If it has the value `invisible', all
59 old headers will be fetched, but none will be displayed.
60
61 The server has to support NOV for any of this to work."
62   :group 'gnus-thread
63   :type '(choice (const :tag "off" nil)
64                  (const some)
65                  number
66                  (sexp :menu-tag "other" t)))
67
68 (defcustom gnus-refer-thread-limit 200
69   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
70 If t, fetch all the available old headers."
71   :group 'gnus-thread
72   :type '(choice number
73                  (sexp :menu-tag "other" t)))
74
75 (defcustom gnus-summary-make-false-root 'adopt
76   "*nil means that Gnus won't gather loose threads.
77 If the root of a thread has expired or been read in a previous
78 session, the information necessary to build a complete thread has been
79 lost.  Instead of having many small sub-threads from this original thread
80 scattered all over the summary buffer, Gnus can gather them.
81
82 If non-nil, Gnus will try to gather all loose sub-threads from an
83 original thread into one large thread.
84
85 If this variable is non-nil, it should be one of `none', `adopt',
86 `dummy' or `empty'.
87
88 If this variable is `none', Gnus will not make a false root, but just
89 present the sub-threads after another.
90 If this variable is `dummy', Gnus will create a dummy root that will
91 have all the sub-threads as children.
92 If this variable is `adopt', Gnus will make one of the \"children\"
93 the parent and mark all the step-children as such.
94 If this variable is `empty', the \"children\" are printed with empty
95 subject fields.  (Or rather, they will be printed with a string
96 given by the `gnus-summary-same-subject' variable.)"
97   :group 'gnus-thread
98   :type '(choice (const :tag "off" nil)
99                  (const none)
100                  (const dummy)
101                  (const adopt)
102                  (const empty)))
103
104 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
105   "*A regexp to match subjects to be excluded from loose thread gathering.
106 As loose thread gathering is done on subjects only, that means that
107 there can be many false gatherings performed.  By rooting out certain
108 common subjects, gathering might become saner."
109   :group 'gnus-thread
110   :type 'regexp)
111
112 (defcustom gnus-summary-gather-subject-limit nil
113   "*Maximum length of subject comparisons when gathering loose threads.
114 Use nil to compare full subjects.  Setting this variable to a low
115 number will help gather threads that have been corrupted by
116 newsreaders chopping off subject lines, but it might also mean that
117 unrelated articles that have subject that happen to begin with the
118 same few characters will be incorrectly gathered.
119
120 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
121 comparing subjects."
122   :group 'gnus-thread
123   :type '(choice (const :tag "off" nil)
124                  (const fuzzy)
125                  (sexp :menu-tag "on" t)))
126
127 (defcustom gnus-simplify-subject-functions nil
128   "List of functions taking a string argument that simplify subjects.
129 The functions are applied recursively.
130
131 Useful functions to put in this list include: `gnus-simplify-subject-re',
132 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
133   :group 'gnus-thread
134   :type '(repeat function))
135
136 (defcustom gnus-simplify-ignored-prefixes nil
137   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
138   :group 'gnus-thread
139   :type '(choice (const :tag "off" nil)
140                  regexp))
141
142 (defcustom gnus-build-sparse-threads nil
143   "*If non-nil, fill in the gaps in threads.
144 If `some', only fill in the gaps that are needed to tie loose threads
145 together.  If `more', fill in all leaf nodes that Gnus can find.  If
146 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  (const some)
150                  (const more)
151                  (sexp :menu-tag "all" t)))
152
153 (defcustom gnus-summary-thread-gathering-function
154   'gnus-gather-threads-by-subject
155   "*Function used for gathering loose threads.
156 There are two pre-defined functions: `gnus-gather-threads-by-subject',
157 which only takes Subjects into consideration; and
158 `gnus-gather-threads-by-references', which compared the References
159 headers of the articles to find matches."
160   :group 'gnus-thread
161   :type '(radio (function-item gnus-gather-threads-by-subject)
162                 (function-item gnus-gather-threads-by-references)
163                 (function :tag "other")))
164
165 (defcustom gnus-summary-same-subject ""
166   "*String indicating that the current article has the same subject as the previous.
167 This variable will only be used if the value of
168 `gnus-summary-make-false-root' is `empty'."
169   :group 'gnus-summary-format
170   :type 'string)
171
172 (defcustom gnus-summary-goto-unread t
173   "*If t, many commands will go to the next unread article.
174 This applies to marking commands as well as other commands that
175 \"naturally\" select the next article, like, for instance, `SPC' at
176 the end of an article.
177
178 If nil, the marking commands do NOT go to the next unread article
179 (they go to the next article instead).  If `never', commands that
180 usually go to the next unread article, will go to the next article,
181 whether it is read or not."
182   :group 'gnus-summary-marks
183   :link '(custom-manual "(gnus)Setting Marks")
184   :type '(choice (const :tag "off" nil)
185                  (const never)
186                  (sexp :menu-tag "on" t)))
187
188 (defcustom gnus-summary-default-score 0
189   "*Default article score level.
190 All scores generated by the score files will be added to this score.
191 If this variable is nil, scoring will be disabled."
192   :group 'gnus-score-default
193   :type '(choice (const :tag "disable")
194                  integer))
195
196 (defcustom gnus-summary-zcore-fuzz 0
197   "*Fuzziness factor for the zcore in the summary buffer.
198 Articles with scores closer than this to `gnus-summary-default-score'
199 will not be marked."
200   :group 'gnus-summary-format
201   :type 'integer)
202
203 (defcustom gnus-simplify-subject-fuzzy-regexp nil
204   "*Strings to be removed when doing fuzzy matches.
205 This can either be a regular expression or list of regular expressions
206 that will be removed from subject strings if fuzzy subject
207 simplification is selected."
208   :group 'gnus-thread
209   :type '(repeat regexp))
210
211 (defcustom gnus-show-threads t
212   "*If non-nil, display threads in summary mode."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-subtree nil
217   "*If non-nil, hide all threads initially.
218 If threads are hidden, you have to run the command
219 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
220 to expose hidden threads."
221   :group 'gnus-thread
222   :type 'boolean)
223
224 (defcustom gnus-thread-hide-killed t
225   "*If non-nil, hide killed threads automatically."
226   :group 'gnus-thread
227   :type 'boolean)
228
229 (defcustom gnus-thread-ignore-subject t
230   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
231 If nil, articles that have different subjects from their parents will
232 start separate threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-operation-ignore-subject t
237   "*If non-nil, subjects will be ignored when doing thread commands.
238 This affects commands like `gnus-summary-kill-thread' and
239 `gnus-summary-lower-thread'.
240
241 If this variable is nil, articles in the same thread with different
242 subjects will not be included in the operation in question.  If this
243 variable is `fuzzy', only articles that have subjects that are fuzzily
244 equal will be included."
245   :group 'gnus-thread
246   :type '(choice (const :tag "off" nil)
247                  (const fuzzy)
248                  (sexp :tag "on" t)))
249
250 (defcustom gnus-thread-indent-level 4
251   "*Number that says how much each sub-thread should be indented."
252   :group 'gnus-thread
253   :type 'integer)
254
255 (defcustom gnus-auto-extend-newsgroup t
256   "*If non-nil, extend newsgroup forward and backward when requested."
257   :group 'gnus-summary-choose
258   :type 'boolean)
259
260 (defcustom gnus-auto-select-first t
261   "*If nil, don't select the first unread article when entering a group.
262 If this variable is `best', select the highest-scored unread article
263 in the group.  If t, select the first unread article.
264
265 This variable can also be a function to place point on a likely
266 subject line.  Useful values include `gnus-summary-first-unread-subject',
267 `gnus-summary-first-unread-article' and
268 `gnus-summary-best-unread-article'.
269
270 If you want to prevent automatic selection of the first unread article
271 in some newsgroups, set the variable to nil in
272 `gnus-select-group-hook'."
273   :group 'gnus-group-select
274   :type '(choice (const :tag "none" nil)
275                  (const best)
276                  (sexp :menu-tag "first" t)
277                  (function-item gnus-summary-first-unread-subject)
278                  (function-item gnus-summary-first-unread-article)
279                  (function-item gnus-summary-best-unread-article)))
280
281 (defcustom gnus-auto-select-next t
282   "*If non-nil, offer to go to the next group from the end of the previous.
283 If the value is t and the next newsgroup is empty, Gnus will exit
284 summary mode and go back to group mode.  If the value is neither nil
285 nor t, Gnus will select the following unread newsgroup.  In
286 particular, if the value is the symbol `quietly', the next unread
287 newsgroup will be selected without any confirmation, and if it is
288 `almost-quietly', the next group will be selected without any
289 confirmation if you are located on the last article in the group.
290 Finally, if this variable is `slightly-quietly', the `Z n' command
291 will go to the next group without confirmation."
292   :group 'gnus-summary-maneuvering
293   :type '(choice (const :tag "off" nil)
294                  (const quietly)
295                  (const almost-quietly)
296                  (const slightly-quietly)
297                  (sexp :menu-tag "on" t)))
298
299 (defcustom gnus-auto-select-same nil
300   "*If non-nil, select the next article with the same subject.
301 If there are no more articles with the same subject, go to
302 the first unread article."
303   :group 'gnus-summary-maneuvering
304   :type 'boolean)
305
306 (defcustom gnus-summary-check-current nil
307   "*If non-nil, consider the current article when moving.
308 The \"unread\" movement commands will stay on the same line if the
309 current article is unread."
310   :group 'gnus-summary-maneuvering
311   :type 'boolean)
312
313 (defcustom gnus-auto-center-summary t
314   "*If non-nil, always center the current summary buffer.
315 In particular, if `vertical' do only vertical recentering.  If non-nil
316 and non-`vertical', do both horizontal and vertical recentering."
317   :group 'gnus-summary-maneuvering
318   :type '(choice (const :tag "none" nil)
319                  (const vertical)
320                  (integer :tag "height")
321                  (sexp :menu-tag "both" t)))
322
323 (defcustom gnus-show-all-headers nil
324   "*If non-nil, don't hide any headers."
325   :group 'gnus-article-hiding
326   :group 'gnus-article-headers
327   :type 'boolean)
328
329 (defcustom gnus-summary-ignore-duplicates nil
330   "*If non-nil, ignore articles with identical Message-ID headers."
331   :group 'gnus-summary
332   :type 'boolean)
333
334 (defcustom gnus-single-article-buffer t
335   "*If non-nil, display all articles in the same buffer.
336 If nil, each group will get its own article buffer."
337   :group 'gnus-article-various
338   :type 'boolean)
339
340 (defcustom gnus-break-pages t
341   "*If non-nil, do page breaking on articles.
342 The page delimiter is specified by the `gnus-page-delimiter'
343 variable."
344   :group 'gnus-article-various
345   :type 'boolean)
346
347 (defcustom gnus-move-split-methods nil
348   "*Variable used to suggest where articles are to be moved to.
349 It uses the same syntax as the `gnus-split-methods' variable."
350   :group 'gnus-summary-mail
351   :type '(repeat (choice (list :value (fun) function)
352                          (cons :value ("" "") regexp (repeat string))
353                          (sexp :value nil))))
354
355 (defcustom gnus-unread-mark ?  ;Whitespace
356   "*Mark used for unread articles."
357   :group 'gnus-summary-marks
358   :type 'character)
359
360 (defcustom gnus-ticked-mark ?!
361   "*Mark used for ticked articles."
362   :group 'gnus-summary-marks
363   :type 'character)
364
365 (defcustom gnus-dormant-mark ??
366   "*Mark used for dormant articles."
367   :group 'gnus-summary-marks
368   :type 'character)
369
370 (defcustom gnus-del-mark ?r
371   "*Mark used for del'd articles."
372   :group 'gnus-summary-marks
373   :type 'character)
374
375 (defcustom gnus-read-mark ?R
376   "*Mark used for read articles."
377   :group 'gnus-summary-marks
378   :type 'character)
379
380 (defcustom gnus-expirable-mark ?E
381   "*Mark used for expirable articles."
382   :group 'gnus-summary-marks
383   :type 'character)
384
385 (defcustom gnus-killed-mark ?K
386   "*Mark used for killed articles."
387   :group 'gnus-summary-marks
388   :type 'character)
389
390 (defcustom gnus-souped-mark ?F
391   "*Mark used for killed articles."
392   :group 'gnus-summary-marks
393   :type 'character)
394
395 (defcustom gnus-kill-file-mark ?X
396   "*Mark used for articles killed by kill files."
397   :group 'gnus-summary-marks
398   :type 'character)
399
400 (defcustom gnus-low-score-mark ?Y
401   "*Mark used for articles with a low score."
402   :group 'gnus-summary-marks
403   :type 'character)
404
405 (defcustom gnus-catchup-mark ?C
406   "*Mark used for articles that are caught up."
407   :group 'gnus-summary-marks
408   :type 'character)
409
410 (defcustom gnus-replied-mark ?A
411   "*Mark used for articles that have been replied to."
412   :group 'gnus-summary-marks
413   :type 'character)
414
415 (defcustom gnus-cached-mark ?*
416   "*Mark used for articles that are in the cache."
417   :group 'gnus-summary-marks
418   :type 'character)
419
420 (defcustom gnus-saved-mark ?S
421   "*Mark used for articles that have been saved to."
422   :group 'gnus-summary-marks
423   :type 'character)
424
425 (defcustom gnus-ancient-mark ?O
426   "*Mark used for ancient articles."
427   :group 'gnus-summary-marks
428   :type 'character)
429
430 (defcustom gnus-sparse-mark ?Q
431   "*Mark used for sparsely reffed articles."
432   :group 'gnus-summary-marks
433   :type 'character)
434
435 (defcustom gnus-canceled-mark ?G
436   "*Mark used for canceled articles."
437   :group 'gnus-summary-marks
438   :type 'character)
439
440 (defcustom gnus-duplicate-mark ?M
441   "*Mark used for duplicate articles."
442   :group 'gnus-summary-marks
443   :type 'character)
444
445 (defcustom gnus-undownloaded-mark ?@
446   "*Mark used for articles that weren't downloaded."
447   :group 'gnus-summary-marks
448   :type 'character)
449
450 (defcustom gnus-downloadable-mark ?%
451   "*Mark used for articles that are to be downloaded."
452   :group 'gnus-summary-marks
453   :type 'character)
454
455 (defcustom gnus-unsendable-mark ?=
456   "*Mark used for articles that won't be sent."
457   :group 'gnus-summary-marks
458   :type 'character)
459
460 (defcustom gnus-score-over-mark ?+
461   "*Score mark used for articles with high scores."
462   :group 'gnus-summary-marks
463   :type 'character)
464
465 (defcustom gnus-score-below-mark ?-
466   "*Score mark used for articles with low scores."
467   :group 'gnus-summary-marks
468   :type 'character)
469
470 (defcustom gnus-empty-thread-mark ?  ;Whitespace
471   "*There is no thread under the article."
472   :group 'gnus-summary-marks
473   :type 'character)
474
475 (defcustom gnus-not-empty-thread-mark ?=
476   "*There is a thread under the article."
477   :group 'gnus-summary-marks
478   :type 'character)
479
480 (defcustom gnus-view-pseudo-asynchronously nil
481   "*If non-nil, Gnus will view pseudo-articles asynchronously."
482   :group 'gnus-extract-view
483   :type 'boolean)
484
485 (defcustom gnus-auto-expirable-marks
486   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
487         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
488         gnus-souped-mark gnus-duplicate-mark)
489   "*The list of marks converted into expiration if a group is auto-expirable."
490   :group 'gnus-summary
491   :type '(repeat character))
492
493 (defcustom gnus-inhibit-user-auto-expire t
494   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
495   :group 'gnus-summary
496   :type 'boolean)
497
498 (defcustom gnus-view-pseudos nil
499   "*If `automatic', pseudo-articles will be viewed automatically.
500 If `not-confirm', pseudos will be viewed automatically, and the user
501 will not be asked to confirm the command."
502   :group 'gnus-extract-view
503   :type '(choice (const :tag "off" nil)
504                  (const automatic)
505                  (const not-confirm)))
506
507 (defcustom gnus-view-pseudos-separately t
508   "*If non-nil, one pseudo-article will be created for each file to be viewed.
509 If nil, all files that use the same viewing command will be given as a
510 list of parameters to that command."
511   :group 'gnus-extract-view
512   :type 'boolean)
513
514 (defcustom gnus-insert-pseudo-articles t
515   "*If non-nil, insert pseudo-articles when decoding articles."
516   :group 'gnus-extract-view
517   :type 'boolean)
518
519 (defcustom gnus-summary-dummy-line-format
520   "  %(:                          :%) %S\n"
521   "*The format specification for the dummy roots in the summary buffer.
522 It works along the same lines as a normal formatting string,
523 with some simple extensions.
524
525 %S  The subject"
526   :group 'gnus-threading
527   :type 'string)
528
529 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
530   "*The format specification for the summary mode line.
531 It works along the same lines as a normal formatting string,
532 with some simple extensions:
533
534 %G  Group name
535 %p  Unprefixed group name
536 %A  Current article number
537 %z  Current article score
538 %V  Gnus version
539 %U  Number of unread articles in the group
540 %e  Number of unselected articles in the group
541 %Z  A string with unread/unselected article counts
542 %g  Shortish group name
543 %S  Subject of the current article
544 %u  User-defined spec
545 %s  Current score file name
546 %d  Number of dormant articles
547 %r  Number of articles that have been marked as read in this session
548 %E  Number of articles expunged by the score files"
549   :group 'gnus-summary-format
550   :type 'string)
551
552 (defcustom gnus-list-identifiers nil
553   "Regexp that matches list identifiers to be removed from subject.
554 This can also be a list of regexps."
555   :group 'gnus-summary-format
556   :group 'gnus-article-hiding
557   :type '(choice (const :tag "none" nil)
558                  (regexp :value ".*")
559                  (repeat :value (".*") regexp)))
560
561 (defcustom gnus-summary-mark-below 0
562   "*Mark all articles with a score below this variable as read.
563 This variable is local to each summary buffer and usually set by the
564 score file."
565   :group 'gnus-score-default
566   :type 'integer)
567
568 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
569   "*List of functions used for sorting articles in the summary buffer.
570 This variable is only used when not using a threaded display."
571   :group 'gnus-summary-sort
572   :type '(repeat (choice (function-item gnus-article-sort-by-number)
573                          (function-item gnus-article-sort-by-author)
574                          (function-item gnus-article-sort-by-subject)
575                          (function-item gnus-article-sort-by-date)
576                          (function-item gnus-article-sort-by-score)
577                          (function :tag "other"))))
578
579 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
580   "*List of functions used for sorting threads in the summary buffer.
581 By default, threads are sorted by article number.
582
583 Each function takes two threads and return non-nil if the first thread
584 should be sorted before the other.  If you use more than one function,
585 the primary sort function should be the last.  You should probably
586 always include `gnus-thread-sort-by-number' in the list of sorting
587 functions -- preferably first.
588
589 Ready-made functions include `gnus-thread-sort-by-number',
590 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
591 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
592 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
593   :group 'gnus-summary-sort
594   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
595                          (function-item gnus-thread-sort-by-author)
596                          (function-item gnus-thread-sort-by-subject)
597                          (function-item gnus-thread-sort-by-date)
598                          (function-item gnus-thread-sort-by-score)
599                          (function-item gnus-thread-sort-by-total-score)
600                          (function :tag "other"))))
601
602 (defcustom gnus-thread-score-function '+
603   "*Function used for calculating the total score of a thread.
604
605 The function is called with the scores of the article and each
606 subthread and should then return the score of the thread.
607
608 Some functions you can use are `+', `max', or `min'."
609   :group 'gnus-summary-sort
610   :type 'function)
611
612 (defcustom gnus-summary-expunge-below nil
613   "All articles that have a score less than this variable will be expunged.
614 This variable is local to the summary buffers."
615   :group 'gnus-score-default
616   :type '(choice (const :tag "off" nil)
617                  integer))
618
619 (defcustom gnus-thread-expunge-below nil
620   "All threads that have a total score less than this variable will be expunged.
621 See `gnus-thread-score-function' for en explanation of what a
622 \"thread score\" is.
623
624 This variable is local to the summary buffers."
625   :group 'gnus-threading
626   :group 'gnus-score-default
627   :type '(choice (const :tag "off" nil)
628                  integer))
629
630 (defcustom gnus-summary-mode-hook nil
631   "*A hook for Gnus summary mode.
632 This hook is run before any variables are set in the summary buffer."
633   :group 'gnus-summary-various
634   :type 'hook)
635
636 (defcustom gnus-summary-menu-hook nil
637   "*Hook run after the creation of the summary mode menu."
638   :group 'gnus-summary-visual
639   :type 'hook)
640
641 (defcustom gnus-summary-exit-hook nil
642   "*A hook called on exit from the summary buffer.
643 It will be called with point in the group buffer."
644   :group 'gnus-summary-exit
645   :type 'hook)
646
647 (defcustom gnus-summary-prepare-hook nil
648   "*A hook called after the summary buffer has been generated.
649 If you want to modify the summary buffer, you can use this hook."
650   :group 'gnus-summary-various
651   :type 'hook)
652
653 (defcustom gnus-summary-prepared-hook nil
654   "*A hook called as the last thing after the summary buffer has been generated."
655   :group 'gnus-summary-various
656   :type 'hook)
657
658 (defcustom gnus-summary-generate-hook nil
659   "*A hook run just before generating the summary buffer.
660 This hook is commonly used to customize threading variables and the
661 like."
662   :group 'gnus-summary-various
663   :type 'hook)
664
665 (defcustom gnus-select-group-hook nil
666   "*A hook called when a newsgroup is selected.
667
668 If you'd like to simplify subjects like the
669 `gnus-summary-next-same-subject' command does, you can use the
670 following hook:
671
672  (setq gnus-select-group-hook
673       (list
674         (lambda ()
675           (mapcar (lambda (header)
676                      (mail-header-set-subject
677                       header
678                       (gnus-simplify-subject
679                        (mail-header-subject header) 're-only)))
680                   gnus-newsgroup-headers))))"
681   :group 'gnus-group-select
682   :type 'hook)
683
684 (defcustom gnus-select-article-hook nil
685   "*A hook called when an article is selected."
686   :group 'gnus-summary-choose
687   :type 'hook)
688
689 (defcustom gnus-visual-mark-article-hook
690   (list 'gnus-highlight-selected-summary)
691   "*Hook run after selecting an article in the summary buffer.
692 It is meant to be used for highlighting the article in some way.  It
693 is not run if `gnus-visual' is nil."
694   :group 'gnus-summary-visual
695   :type 'hook)
696
697 (defcustom gnus-parse-headers-hook nil
698   "*A hook called before parsing the headers."
699   :group 'gnus-various
700   :type 'hook)
701
702 (defcustom gnus-exit-group-hook nil
703   "*A hook called when exiting summary mode.
704 This hook is not called from the non-updating exit commands like `Q'."
705   :group 'gnus-various
706   :type 'hook)
707
708 (defcustom gnus-summary-update-hook
709   (list 'gnus-summary-highlight-line)
710   "*A hook called when a summary line is changed.
711 The hook will not be called if `gnus-visual' is nil.
712
713 The default function `gnus-summary-highlight-line' will
714 highlight the line according to the `gnus-summary-highlight'
715 variable."
716   :group 'gnus-summary-visual
717   :type 'hook)
718
719 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
720   "*A hook called when an article is selected for the first time.
721 The hook is intended to mark an article as read (or unread)
722 automatically when it is selected."
723   :group 'gnus-summary-choose
724   :type 'hook)
725
726 (defcustom gnus-group-no-more-groups-hook nil
727   "*A hook run when returning to group mode having no more (unread) groups."
728   :group 'gnus-group-select
729   :type 'hook)
730
731 (defcustom gnus-ps-print-hook nil
732   "*A hook run before ps-printing something from Gnus."
733   :group 'gnus-summary
734   :type 'hook)
735
736 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
737   "Face used for highlighting the current article in the summary buffer."
738   :group 'gnus-summary-visual
739   :type 'face)
740
741 (defcustom gnus-summary-highlight
742   '(((= mark gnus-canceled-mark)
743      . gnus-summary-cancelled-face)
744     ((and (> score default)
745           (or (= mark gnus-dormant-mark)
746               (= mark gnus-ticked-mark)))
747      . gnus-summary-high-ticked-face)
748     ((and (< score default)
749           (or (= mark gnus-dormant-mark)
750               (= mark gnus-ticked-mark)))
751      . gnus-summary-low-ticked-face)
752     ((or (= mark gnus-dormant-mark)
753          (= mark gnus-ticked-mark))
754      . gnus-summary-normal-ticked-face)
755     ((and (> score default) (= mark gnus-ancient-mark))
756      . gnus-summary-high-ancient-face)
757     ((and (< score default) (= mark gnus-ancient-mark))
758      . gnus-summary-low-ancient-face)
759     ((= mark gnus-ancient-mark)
760      . gnus-summary-normal-ancient-face)
761     ((and (> score default) (= mark gnus-unread-mark))
762      . gnus-summary-high-unread-face)
763     ((and (< score default) (= mark gnus-unread-mark))
764      . gnus-summary-low-unread-face)
765     ((= mark gnus-unread-mark)
766      . gnus-summary-normal-unread-face)
767     ((and (> score default) (memq mark (list gnus-downloadable-mark
768                                              gnus-undownloaded-mark)))
769      . gnus-summary-high-unread-face)
770     ((and (< score default) (memq mark (list gnus-downloadable-mark
771                                              gnus-undownloaded-mark)))
772      . gnus-summary-low-unread-face)
773     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
774      . gnus-summary-normal-unread-face)
775     ((> score default)
776      . gnus-summary-high-read-face)
777     ((< score default)
778      . gnus-summary-low-read-face)
779     (t
780      . gnus-summary-normal-read-face))
781   "*Controls the highlighting of summary buffer lines.
782
783 A list of (FORM . FACE) pairs.  When deciding how a a particular
784 summary line should be displayed, each form is evaluated.  The content
785 of the face field after the first true form is used.  You can change
786 how those summary lines are displayed, by editing the face field.
787
788 You can use the following variables in the FORM field.
789
790 score:   The articles score
791 default: The default article score.
792 below:   The score below which articles are automatically marked as read.
793 mark:    The articles mark."
794   :group 'gnus-summary-visual
795   :type '(repeat (cons (sexp :tag "Form" nil)
796                        face)))
797
798 (defcustom gnus-alter-header-function nil
799   "Function called to allow alteration of article header structures.
800 The function is called with one parameter, the article header vector,
801 which it may alter in any way.")
802
803 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
804   "Variable that says which function should be used to decode a string with encoded words.")
805
806 (defcustom gnus-extra-headers nil
807   "*Extra headers to parse."
808   :group 'gnus-summary
809   :type '(repeat symbol))
810
811 (defcustom gnus-ignored-from-addresses
812   (and user-mail-address (regexp-quote user-mail-address))
813   "*Regexp of From headers that may be suppressed in favor of To headers."
814   :group 'gnus-summary
815   :type 'regexp)
816
817 (defcustom gnus-group-charset-alist
818   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
819     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
820     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
821     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
822     ("^relcom\\>" koi8-r)
823     ("^fido7\\>" koi8-r)
824     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
825     ("^israel\\>" iso-8859-1)
826     ("^han\\>" euc-kr)
827     ("^alt.chinese.text.big5\\>" chinese-big5)
828     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
829     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
830     (".*" iso-8859-1))
831   "Alist of regexps (to match group names) and default charsets to be used when reading."
832   :type '(repeat (list (regexp :tag "Group")
833                        (symbol :tag "Charset")))
834   :group 'gnus-charset)
835
836 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
837   "List of charsets that should be ignored.
838 When these charsets are used in the \"charset\" parameter, the
839 default charset will be used instead."
840   :type '(repeat symbol)
841   :group 'gnus-charset)
842
843 (defcustom gnus-group-ignored-charsets-alist
844   '(("alt\\.chinese\\.text" iso-8859-1))
845   "Alist of regexps (to match group names) and charsets that should be ignored.
846 When these charsets are used in the \"charset\" parameter, the
847 default charset will be used instead."
848   :type '(repeat (cons (regexp :tag "Group")
849                        (repeat symbol)))
850   :group 'gnus-charset)
851
852 (defcustom gnus-group-highlight-words-alist nil
853   "Alist of group regexps and highlight regexps.
854 This variable uses the same syntax as `gnus-emphasis-alist'."
855   :type '(repeat (cons (regexp :tag "Group")
856                        (repeat (list (regexp :tag "Highlight regexp")
857                                      (number :tag "Group for entire word" 0)
858                                      (number :tag "Group for displayed part" 0)
859                                      (symbol :tag "Face"
860                                              gnus-emphasis-highlight-words)))))
861   :group 'gnus-summary-visual)
862
863 (defcustom gnus-summary-show-article-charset-alist
864   nil
865   "Alist of number and charset.
866 The article will be shown with the charset corresponding to the
867 numbered argument.
868 For example: ((1 . cn-gb-2312) (2 . big5))."
869   :type '(repeat (cons (number :tag "Argument" 1)
870                        (symbol :tag "Charset")))
871   :group 'gnus-charset)
872
873 (defcustom gnus-preserve-marks t
874   "Whether marks are preserved when moving, copying and respooling messages."
875   :type 'boolean
876   :group 'gnus-summary-marks)
877
878 (defcustom gnus-alter-articles-to-read-function nil
879   "Function to be called to alter the list of articles to be selected."
880   :type 'function
881   :group 'gnus-summary)
882
883 (defcustom gnus-orphan-score nil
884   "*All orphans get this score added.  Set in the score file."
885   :group 'gnus-score-default
886   :type '(choice (const nil)
887                  integer))
888
889 (defcustom gnus-summary-save-parts-default-mime "image/.*"
890   "*A regexp to match MIME parts when saving multiple parts of a message
891 with gnus-summary-save-parts (X m). This regexp will be used by default
892 when prompting the user for which type of files to save."
893   :group 'gnus-summary
894   :type 'regexp)
895
896
897 ;;; Internal variables
898
899 (defvar gnus-article-mime-handles nil)
900 (defvar gnus-article-decoded-p nil)
901 (defvar gnus-article-charset nil)
902 (defvar gnus-article-ignored-charsets nil)
903 (defvar gnus-scores-exclude-files nil)
904 (defvar gnus-page-broken nil)
905 (defvar gnus-inhibit-mime-unbuttonizing nil)
906
907 (defvar gnus-original-article nil)
908 (defvar gnus-article-internal-prepare-hook nil)
909 (defvar gnus-newsgroup-process-stack nil)
910
911 (defvar gnus-thread-indent-array nil)
912 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
913 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
914   "Function called to sort the articles within a thread after it has been gathered together.")
915
916 (defvar gnus-summary-save-parts-type-history nil)
917 (defvar gnus-summary-save-parts-last-directory nil)
918
919 ;; Avoid highlighting in kill files.
920 (defvar gnus-summary-inhibit-highlight nil)
921 (defvar gnus-newsgroup-selected-overlay nil)
922 (defvar gnus-inhibit-limiting nil)
923 (defvar gnus-newsgroup-adaptive-score-file nil)
924 (defvar gnus-current-score-file nil)
925 (defvar gnus-current-move-group nil)
926 (defvar gnus-current-copy-group nil)
927 (defvar gnus-current-crosspost-group nil)
928
929 (defvar gnus-newsgroup-dependencies nil)
930 (defvar gnus-newsgroup-adaptive nil)
931 (defvar gnus-summary-display-article-function nil)
932 (defvar gnus-summary-highlight-line-function nil
933   "Function called after highlighting a summary line.")
934
935 (defvar gnus-summary-line-format-alist
936   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
937     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
938     (?s gnus-tmp-subject-or-nil ?s)
939     (?n gnus-tmp-name ?s)
940     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
941         ?s)
942     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
943             gnus-tmp-from) ?s)
944     (?F gnus-tmp-from ?s)
945     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
946     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
947     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
948     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
949     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
950     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
951     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
952     (?L gnus-tmp-lines ?d)
953     (?I gnus-tmp-indentation ?s)
954     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
955     (?R gnus-tmp-replied ?c)
956     (?\[ gnus-tmp-opening-bracket ?c)
957     (?\] gnus-tmp-closing-bracket ?c)
958     (?\> (make-string gnus-tmp-level ? ) ?s)
959     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
960     (?i gnus-tmp-score ?d)
961     (?z gnus-tmp-score-char ?c)
962     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
963     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
964     (?U gnus-tmp-unread ?c)
965     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
966     (?t (gnus-summary-number-of-articles-in-thread
967          (and (boundp 'thread) (car thread)) gnus-tmp-level)
968         ?d)
969     (?e (gnus-summary-number-of-articles-in-thread
970          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
971         ?c)
972     (?u gnus-tmp-user-defined ?s)
973     (?P (gnus-pick-line-number) ?d))
974   "An alist of format specifications that can appear in summary lines.
975 These are paired with what variables they correspond with, along with
976 the type of the variable (string, integer, character, etc).")
977
978 (defvar gnus-summary-dummy-line-format-alist
979   `((?S gnus-tmp-subject ?s)
980     (?N gnus-tmp-number ?d)
981     (?u gnus-tmp-user-defined ?s)))
982
983 (defvar gnus-summary-mode-line-format-alist
984   `((?G gnus-tmp-group-name ?s)
985     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
986     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
987     (?A gnus-tmp-article-number ?d)
988     (?Z gnus-tmp-unread-and-unselected ?s)
989     (?V gnus-version ?s)
990     (?U gnus-tmp-unread-and-unticked ?d)
991     (?S gnus-tmp-subject ?s)
992     (?e gnus-tmp-unselected ?d)
993     (?u gnus-tmp-user-defined ?s)
994     (?d (length gnus-newsgroup-dormant) ?d)
995     (?t (length gnus-newsgroup-marked) ?d)
996     (?r (length gnus-newsgroup-reads) ?d)
997     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
998     (?E gnus-newsgroup-expunged-tally ?d)
999     (?s (gnus-current-score-file-nondirectory) ?s)))
1000
1001 (defvar gnus-last-search-regexp nil
1002   "Default regexp for article search command.")
1003
1004 (defvar gnus-last-shell-command nil
1005   "Default shell command on article.")
1006
1007 (defvar gnus-newsgroup-begin nil)
1008 (defvar gnus-newsgroup-end nil)
1009 (defvar gnus-newsgroup-last-rmail nil)
1010 (defvar gnus-newsgroup-last-mail nil)
1011 (defvar gnus-newsgroup-last-folder nil)
1012 (defvar gnus-newsgroup-last-file nil)
1013 (defvar gnus-newsgroup-auto-expire nil)
1014 (defvar gnus-newsgroup-active nil)
1015
1016 (defvar gnus-newsgroup-data nil)
1017 (defvar gnus-newsgroup-data-reverse nil)
1018 (defvar gnus-newsgroup-limit nil)
1019 (defvar gnus-newsgroup-limits nil)
1020
1021 (defvar gnus-newsgroup-unreads nil
1022   "List of unread articles in the current newsgroup.")
1023
1024 (defvar gnus-newsgroup-unselected nil
1025   "List of unselected unread articles in the current newsgroup.")
1026
1027 (defvar gnus-newsgroup-reads nil
1028   "Alist of read articles and article marks in the current newsgroup.")
1029
1030 (defvar gnus-newsgroup-expunged-tally nil)
1031
1032 (defvar gnus-newsgroup-marked nil
1033   "List of ticked articles in the current newsgroup (a subset of unread art).")
1034
1035 (defvar gnus-newsgroup-killed nil
1036   "List of ranges of articles that have been through the scoring process.")
1037
1038 (defvar gnus-newsgroup-cached nil
1039   "List of articles that come from the article cache.")
1040
1041 (defvar gnus-newsgroup-saved nil
1042   "List of articles that have been saved.")
1043
1044 (defvar gnus-newsgroup-kill-headers nil)
1045
1046 (defvar gnus-newsgroup-replied nil
1047   "List of articles that have been replied to in the current newsgroup.")
1048
1049 (defvar gnus-newsgroup-expirable nil
1050   "List of articles in the current newsgroup that can be expired.")
1051
1052 (defvar gnus-newsgroup-processable nil
1053   "List of articles in the current newsgroup that can be processed.")
1054
1055 (defvar gnus-newsgroup-downloadable nil
1056   "List of articles in the current newsgroup that can be processed.")
1057
1058 (defvar gnus-newsgroup-undownloaded nil
1059   "List of articles in the current newsgroup that haven't been downloaded..")
1060
1061 (defvar gnus-newsgroup-unsendable nil
1062   "List of articles in the current newsgroup that won't be sent.")
1063
1064 (defvar gnus-newsgroup-bookmarks nil
1065   "List of articles in the current newsgroup that have bookmarks.")
1066
1067 (defvar gnus-newsgroup-dormant nil
1068   "List of dormant articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-scored nil
1071   "List of scored articles in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-headers nil
1074   "List of article headers in the current newsgroup.")
1075
1076 (defvar gnus-newsgroup-threads nil)
1077
1078 (defvar gnus-newsgroup-prepared nil
1079   "Whether the current group has been prepared properly.")
1080
1081 (defvar gnus-newsgroup-ancient nil
1082   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1083
1084 (defvar gnus-newsgroup-sparse nil)
1085
1086 (defvar gnus-current-article nil)
1087 (defvar gnus-article-current nil)
1088 (defvar gnus-current-headers nil)
1089 (defvar gnus-have-all-headers nil)
1090 (defvar gnus-last-article nil)
1091 (defvar gnus-newsgroup-history nil)
1092 (defvar gnus-newsgroup-charset nil)
1093 (defvar gnus-newsgroup-ephemeral-charset nil)
1094 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1095
1096 (defvar gnus-article-before-search nil)
1097
1098 (defconst gnus-summary-local-variables
1099   '(gnus-newsgroup-name
1100     gnus-newsgroup-begin gnus-newsgroup-end
1101     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1102     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1103     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1104     gnus-newsgroup-unselected gnus-newsgroup-marked
1105     gnus-newsgroup-reads gnus-newsgroup-saved
1106     gnus-newsgroup-replied gnus-newsgroup-expirable
1107     gnus-newsgroup-processable gnus-newsgroup-killed
1108     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1109     gnus-newsgroup-unsendable
1110     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1111     gnus-newsgroup-headers gnus-newsgroup-threads
1112     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1113     gnus-current-article gnus-current-headers gnus-have-all-headers
1114     gnus-last-article gnus-article-internal-prepare-hook
1115     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1116     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1117     gnus-thread-expunge-below
1118     gnus-score-alist gnus-current-score-file
1119     (gnus-summary-expunge-below . global)
1120     (gnus-summary-mark-below . global)
1121     (gnus-orphan-score . global)
1122     gnus-newsgroup-active gnus-scores-exclude-files
1123     gnus-newsgroup-history gnus-newsgroup-ancient
1124     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1125     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1126     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1127     (gnus-newsgroup-expunged-tally . 0)
1128     gnus-cache-removable-articles gnus-newsgroup-cached
1129     gnus-newsgroup-data gnus-newsgroup-data-reverse
1130     gnus-newsgroup-limit gnus-newsgroup-limits
1131     gnus-newsgroup-charset)
1132   "Variables that are buffer-local to the summary buffers.")
1133
1134 (defvar gnus-newsgroup-variables nil
1135   "Variables that have separate values in the newsgroups.")
1136
1137 ;; Byte-compiler warning.
1138 (defvar gnus-article-mode-map)
1139
1140 ;; MIME stuff.
1141
1142 (defvar gnus-decode-encoded-word-methods
1143   '(mail-decode-encoded-word-string)
1144   "List of methods used to decode encoded words.
1145
1146 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1147 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1148 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1149 whose names match REGEXP.
1150
1151 For example:
1152 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1153  mail-decode-encoded-word-string
1154  (\"chinese\" . rfc1843-decode-string))")
1155
1156 (defvar gnus-decode-encoded-word-methods-cache nil)
1157
1158 (defun gnus-multi-decode-encoded-word-string (string)
1159   "Apply the functions from `gnus-encoded-word-methods' that match."
1160   (unless (and gnus-decode-encoded-word-methods-cache
1161                (eq gnus-newsgroup-name
1162                    (car gnus-decode-encoded-word-methods-cache)))
1163     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1164     (mapcar (lambda (x)
1165               (if (symbolp x)
1166                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1167                 (if (and gnus-newsgroup-name
1168                          (string-match (car x) gnus-newsgroup-name))
1169                     (nconc gnus-decode-encoded-word-methods-cache
1170                            (list (cdr x))))))
1171           gnus-decode-encoded-word-methods))
1172   (let ((xlist gnus-decode-encoded-word-methods-cache))
1173     (pop xlist)
1174     (while xlist
1175       (setq string (funcall (pop xlist) string))))
1176   string)
1177
1178 ;; Subject simplification.
1179
1180 (defun gnus-simplify-whitespace (str)
1181   "Remove excessive whitespace from STR."
1182   (let ((mystr str))
1183     ;; Multiple spaces.
1184     (while (string-match "[ \t][ \t]+" mystr)
1185       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1186                           " "
1187                           (substring mystr (match-end 0)))))
1188     ;; Leading spaces.
1189     (when (string-match "^[ \t]+" mystr)
1190       (setq mystr (substring mystr (match-end 0))))
1191     ;; Trailing spaces.
1192     (when (string-match "[ \t]+$" mystr)
1193       (setq mystr (substring mystr 0 (match-beginning 0))))
1194     mystr))
1195
1196 (defsubst gnus-simplify-subject-re (subject)
1197   "Remove \"Re:\" from subject lines."
1198   (if (string-match "^[Rr][Ee]: *" subject)
1199       (substring subject (match-end 0))
1200     subject))
1201
1202 (defun gnus-simplify-subject (subject &optional re-only)
1203   "Remove `Re:' and words in parentheses.
1204 If RE-ONLY is non-nil, strip leading `Re:'s only."
1205   (let ((case-fold-search t))           ;Ignore case.
1206     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1207     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1208       (setq subject (substring subject (match-end 0))))
1209     ;; Remove uninteresting prefixes.
1210     (when (and (not re-only)
1211                gnus-simplify-ignored-prefixes
1212                (string-match gnus-simplify-ignored-prefixes subject))
1213       (setq subject (substring subject (match-end 0))))
1214     ;; Remove words in parentheses from end.
1215     (unless re-only
1216       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1217         (setq subject (substring subject 0 (match-beginning 0)))))
1218     ;; Return subject string.
1219     subject))
1220
1221 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1222 ;; all whitespace.
1223 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1224   (goto-char (point-min))
1225   (while (re-search-forward regexp nil t)
1226     (replace-match (or newtext ""))))
1227
1228 (defun gnus-simplify-buffer-fuzzy ()
1229   "Simplify string in the buffer fuzzily.
1230 The string in the accessible portion of the current buffer is simplified.
1231 It is assumed to be a single-line subject.
1232 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1233 matter is removed.  Additional things can be deleted by setting
1234 `gnus-simplify-subject-fuzzy-regexp'."
1235   (let ((case-fold-search t)
1236         (modified-tick))
1237     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1238
1239     (while (not (eq modified-tick (buffer-modified-tick)))
1240       (setq modified-tick (buffer-modified-tick))
1241       (cond
1242        ((listp gnus-simplify-subject-fuzzy-regexp)
1243         (mapcar 'gnus-simplify-buffer-fuzzy-step
1244                 gnus-simplify-subject-fuzzy-regexp))
1245        (gnus-simplify-subject-fuzzy-regexp
1246         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1247       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1248       (gnus-simplify-buffer-fuzzy-step
1249        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1250       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1251
1252     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1253     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1254     (gnus-simplify-buffer-fuzzy-step " $")
1255     (gnus-simplify-buffer-fuzzy-step "^ +")))
1256
1257 (defun gnus-simplify-subject-fuzzy (subject)
1258   "Simplify a subject string fuzzily.
1259 See `gnus-simplify-buffer-fuzzy' for details."
1260   (save-excursion
1261     (gnus-set-work-buffer)
1262     (let ((case-fold-search t))
1263       ;; Remove uninteresting prefixes.
1264       (when (and gnus-simplify-ignored-prefixes
1265                  (string-match gnus-simplify-ignored-prefixes subject))
1266         (setq subject (substring subject (match-end 0))))
1267       (insert subject)
1268       (inline (gnus-simplify-buffer-fuzzy))
1269       (buffer-string))))
1270
1271 (defsubst gnus-simplify-subject-fully (subject)
1272   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1273   (cond
1274    (gnus-simplify-subject-functions
1275     (gnus-map-function gnus-simplify-subject-functions subject))
1276    ((null gnus-summary-gather-subject-limit)
1277     (gnus-simplify-subject-re subject))
1278    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1279     (gnus-simplify-subject-fuzzy subject))
1280    ((numberp gnus-summary-gather-subject-limit)
1281     (gnus-limit-string (gnus-simplify-subject-re subject)
1282                        gnus-summary-gather-subject-limit))
1283    (t
1284     subject)))
1285
1286 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1287   "Check whether two subjects are equal.
1288 If optional argument simple-first is t, first argument is already
1289 simplified."
1290   (cond
1291    ((null simple-first)
1292     (equal (gnus-simplify-subject-fully s1)
1293            (gnus-simplify-subject-fully s2)))
1294    (t
1295     (equal s1
1296            (gnus-simplify-subject-fully s2)))))
1297
1298 (defun gnus-summary-bubble-group ()
1299   "Increase the score of the current group.
1300 This is a handy function to add to `gnus-summary-exit-hook' to
1301 increase the score of each group you read."
1302   (gnus-group-add-score gnus-newsgroup-name))
1303
1304 \f
1305 ;;;
1306 ;;; Gnus summary mode
1307 ;;;
1308
1309 (put 'gnus-summary-mode 'mode-class 'special)
1310
1311 (when t
1312   ;; Non-orthogonal keys
1313
1314   (gnus-define-keys gnus-summary-mode-map
1315     " " gnus-summary-next-page
1316     "\177" gnus-summary-prev-page
1317     [delete] gnus-summary-prev-page
1318     [backspace] gnus-summary-prev-page
1319     "\r" gnus-summary-scroll-up
1320     "\M-\r" gnus-summary-scroll-down
1321     "n" gnus-summary-next-unread-article
1322     "p" gnus-summary-prev-unread-article
1323     "N" gnus-summary-next-article
1324     "P" gnus-summary-prev-article
1325     "\M-\C-n" gnus-summary-next-same-subject
1326     "\M-\C-p" gnus-summary-prev-same-subject
1327     "\M-n" gnus-summary-next-unread-subject
1328     "\M-p" gnus-summary-prev-unread-subject
1329     "." gnus-summary-first-unread-article
1330     "," gnus-summary-best-unread-article
1331     "\M-s" gnus-summary-search-article-forward
1332     "\M-r" gnus-summary-search-article-backward
1333     "<" gnus-summary-beginning-of-article
1334     ">" gnus-summary-end-of-article
1335     "j" gnus-summary-goto-article
1336     "^" gnus-summary-refer-parent-article
1337     "\M-^" gnus-summary-refer-article
1338     "u" gnus-summary-tick-article-forward
1339     "!" gnus-summary-tick-article-forward
1340     "U" gnus-summary-tick-article-backward
1341     "d" gnus-summary-mark-as-read-forward
1342     "D" gnus-summary-mark-as-read-backward
1343     "E" gnus-summary-mark-as-expirable
1344     "\M-u" gnus-summary-clear-mark-forward
1345     "\M-U" gnus-summary-clear-mark-backward
1346     "k" gnus-summary-kill-same-subject-and-select
1347     "\C-k" gnus-summary-kill-same-subject
1348     "\M-\C-k" gnus-summary-kill-thread
1349     "\M-\C-l" gnus-summary-lower-thread
1350     "e" gnus-summary-edit-article
1351     "#" gnus-summary-mark-as-processable
1352     "\M-#" gnus-summary-unmark-as-processable
1353     "\M-\C-t" gnus-summary-toggle-threads
1354     "\M-\C-s" gnus-summary-show-thread
1355     "\M-\C-h" gnus-summary-hide-thread
1356     "\M-\C-f" gnus-summary-next-thread
1357     "\M-\C-b" gnus-summary-prev-thread
1358     [(meta down)] gnus-summary-next-thread
1359     [(meta up)] gnus-summary-prev-thread
1360     "\M-\C-u" gnus-summary-up-thread
1361     "\M-\C-d" gnus-summary-down-thread
1362     "&" gnus-summary-execute-command
1363     "c" gnus-summary-catchup-and-exit
1364     "\C-w" gnus-summary-mark-region-as-read
1365     "\C-t" gnus-summary-toggle-truncation
1366     "?" gnus-summary-mark-as-dormant
1367     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1368     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1369     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1370     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1371     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1372     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1373     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1374     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1375     "=" gnus-summary-expand-window
1376     "\C-x\C-s" gnus-summary-reselect-current-group
1377     "\M-g" gnus-summary-rescan-group
1378     "w" gnus-summary-stop-page-breaking
1379     "\C-c\C-r" gnus-summary-caesar-message
1380     "f" gnus-summary-followup
1381     "F" gnus-summary-followup-with-original
1382     "C" gnus-summary-cancel-article
1383     "r" gnus-summary-reply
1384     "R" gnus-summary-reply-with-original
1385     "\C-c\C-f" gnus-summary-mail-forward
1386     "o" gnus-summary-save-article
1387     "\C-o" gnus-summary-save-article-mail
1388     "|" gnus-summary-pipe-output
1389     "\M-k" gnus-summary-edit-local-kill
1390     "\M-K" gnus-summary-edit-global-kill
1391     ;; "V" gnus-version
1392     "\C-c\C-d" gnus-summary-describe-group
1393     "q" gnus-summary-exit
1394     "Q" gnus-summary-exit-no-update
1395     "\C-c\C-i" gnus-info-find-node
1396     gnus-mouse-2 gnus-mouse-pick-article
1397     "m" gnus-summary-mail-other-window
1398     "a" gnus-summary-post-news
1399     "x" gnus-summary-limit-to-unread
1400     "s" gnus-summary-isearch-article
1401     "t" gnus-summary-toggle-header
1402     "g" gnus-summary-show-article
1403     "l" gnus-summary-goto-last-article
1404     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1405     "\C-d" gnus-summary-enter-digest-group
1406     "\M-\C-d" gnus-summary-read-document
1407     "\M-\C-e" gnus-summary-edit-parameters
1408     "\M-\C-a" gnus-summary-customize-parameters
1409     "\C-c\C-b" gnus-bug
1410     "*" gnus-cache-enter-article
1411     "\M-*" gnus-cache-remove-article
1412     "\M-&" gnus-summary-universal-argument
1413     "\C-l" gnus-recenter
1414     "I" gnus-summary-increase-score
1415     "L" gnus-summary-lower-score
1416     "\M-i" gnus-symbolic-argument
1417     "h" gnus-summary-select-article-buffer
1418
1419     "b" gnus-article-view-part
1420     "\M-t" gnus-summary-toggle-display-buttonized
1421
1422     "V" gnus-summary-score-map
1423     "X" gnus-uu-extract-map
1424     "S" gnus-summary-send-map)
1425
1426   ;; Sort of orthogonal keymap
1427   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1428     "t" gnus-summary-tick-article-forward
1429     "!" gnus-summary-tick-article-forward
1430     "d" gnus-summary-mark-as-read-forward
1431     "r" gnus-summary-mark-as-read-forward
1432     "c" gnus-summary-clear-mark-forward
1433     " " gnus-summary-clear-mark-forward
1434     "e" gnus-summary-mark-as-expirable
1435     "x" gnus-summary-mark-as-expirable
1436     "?" gnus-summary-mark-as-dormant
1437     "b" gnus-summary-set-bookmark
1438     "B" gnus-summary-remove-bookmark
1439     "#" gnus-summary-mark-as-processable
1440     "\M-#" gnus-summary-unmark-as-processable
1441     "S" gnus-summary-limit-include-expunged
1442     "C" gnus-summary-catchup
1443     "H" gnus-summary-catchup-to-here
1444     "\C-c" gnus-summary-catchup-all
1445     "k" gnus-summary-kill-same-subject-and-select
1446     "K" gnus-summary-kill-same-subject
1447     "P" gnus-uu-mark-map)
1448
1449   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1450     "c" gnus-summary-clear-above
1451     "u" gnus-summary-tick-above
1452     "m" gnus-summary-mark-above
1453     "k" gnus-summary-kill-below)
1454
1455   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1456     "/" gnus-summary-limit-to-subject
1457     "n" gnus-summary-limit-to-articles
1458     "w" gnus-summary-pop-limit
1459     "s" gnus-summary-limit-to-subject
1460     "a" gnus-summary-limit-to-author
1461     "u" gnus-summary-limit-to-unread
1462     "m" gnus-summary-limit-to-marks
1463     "M" gnus-summary-limit-exclude-marks
1464     "v" gnus-summary-limit-to-score
1465     "*" gnus-summary-limit-include-cached
1466     "D" gnus-summary-limit-include-dormant
1467     "T" gnus-summary-limit-include-thread
1468     "d" gnus-summary-limit-exclude-dormant
1469     "t" gnus-summary-limit-to-age
1470     "x" gnus-summary-limit-to-extra
1471     "E" gnus-summary-limit-include-expunged
1472     "c" gnus-summary-limit-exclude-childless-dormant
1473     "C" gnus-summary-limit-mark-excluded-as-read)
1474
1475   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1476     "n" gnus-summary-next-unread-article
1477     "p" gnus-summary-prev-unread-article
1478     "N" gnus-summary-next-article
1479     "P" gnus-summary-prev-article
1480     "\C-n" gnus-summary-next-same-subject
1481     "\C-p" gnus-summary-prev-same-subject
1482     "\M-n" gnus-summary-next-unread-subject
1483     "\M-p" gnus-summary-prev-unread-subject
1484     "f" gnus-summary-first-unread-article
1485     "b" gnus-summary-best-unread-article
1486     "j" gnus-summary-goto-article
1487     "g" gnus-summary-goto-subject
1488     "l" gnus-summary-goto-last-article
1489     "o" gnus-summary-pop-article)
1490
1491   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1492     "k" gnus-summary-kill-thread
1493     "l" gnus-summary-lower-thread
1494     "i" gnus-summary-raise-thread
1495     "T" gnus-summary-toggle-threads
1496     "t" gnus-summary-rethread-current
1497     "^" gnus-summary-reparent-thread
1498     "s" gnus-summary-show-thread
1499     "S" gnus-summary-show-all-threads
1500     "h" gnus-summary-hide-thread
1501     "H" gnus-summary-hide-all-threads
1502     "n" gnus-summary-next-thread
1503     "p" gnus-summary-prev-thread
1504     "u" gnus-summary-up-thread
1505     "o" gnus-summary-top-thread
1506     "d" gnus-summary-down-thread
1507     "#" gnus-uu-mark-thread
1508     "\M-#" gnus-uu-unmark-thread)
1509
1510   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1511     "g" gnus-summary-prepare
1512     "c" gnus-summary-insert-cached-articles)
1513
1514   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1515     "c" gnus-summary-catchup-and-exit
1516     "C" gnus-summary-catchup-all-and-exit
1517     "E" gnus-summary-exit-no-update
1518     "Q" gnus-summary-exit
1519     "Z" gnus-summary-exit
1520     "n" gnus-summary-catchup-and-goto-next-group
1521     "R" gnus-summary-reselect-current-group
1522     "G" gnus-summary-rescan-group
1523     "N" gnus-summary-next-group
1524     "s" gnus-summary-save-newsrc
1525     "P" gnus-summary-prev-group)
1526
1527   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1528     " " gnus-summary-next-page
1529     "n" gnus-summary-next-page
1530     "\177" gnus-summary-prev-page
1531     [delete] gnus-summary-prev-page
1532     "p" gnus-summary-prev-page
1533     "\r" gnus-summary-scroll-up
1534     "\M-\r" gnus-summary-scroll-down
1535     "<" gnus-summary-beginning-of-article
1536     ">" gnus-summary-end-of-article
1537     "b" gnus-summary-beginning-of-article
1538     "e" gnus-summary-end-of-article
1539     "^" gnus-summary-refer-parent-article
1540     "r" gnus-summary-refer-parent-article
1541     "D" gnus-summary-enter-digest-group
1542     "R" gnus-summary-refer-references
1543     "T" gnus-summary-refer-thread
1544     "g" gnus-summary-show-article
1545     "s" gnus-summary-isearch-article
1546     "P" gnus-summary-print-article
1547     "t" gnus-article-babel)
1548
1549   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1550     "b" gnus-article-add-buttons
1551     "B" gnus-article-add-buttons-to-head
1552     "o" gnus-article-treat-overstrike
1553     "e" gnus-article-emphasize
1554     "w" gnus-article-fill-cited-article
1555     "Q" gnus-article-fill-long-lines
1556     "C" gnus-article-capitalize-sentences
1557     "c" gnus-article-remove-cr
1558     "q" gnus-article-de-quoted-unreadable
1559     "6" gnus-article-de-base64-unreadable
1560     "Z" gnus-article-decode-HZ
1561     "h" gnus-article-wash-html
1562     "s" gnus-summary-force-verify-and-decrypt
1563     "f" gnus-article-display-x-face
1564     "l" gnus-summary-stop-page-breaking
1565     "r" gnus-summary-caesar-message
1566     "t" gnus-summary-toggle-header
1567     "v" gnus-summary-verbose-headers
1568     "H" gnus-article-strip-headers-in-body
1569     "d" gnus-article-treat-dumbquotes)
1570
1571   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1572     "a" gnus-article-hide
1573     "h" gnus-article-hide-headers
1574     "b" gnus-article-hide-boring-headers
1575     "s" gnus-article-hide-signature
1576     "c" gnus-article-hide-citation
1577     "C" gnus-article-hide-citation-in-followups
1578     "l" gnus-article-hide-list-identifiers
1579     "p" gnus-article-hide-pgp
1580     "B" gnus-article-strip-banner
1581     "P" gnus-article-hide-pem
1582     "\C-c" gnus-article-hide-citation-maybe)
1583
1584   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1585     "a" gnus-article-highlight
1586     "h" gnus-article-highlight-headers
1587     "c" gnus-article-highlight-citation
1588     "s" gnus-article-highlight-signature)
1589
1590   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1591     "w" gnus-article-decode-mime-words
1592     "c" gnus-article-decode-charset
1593     "v" gnus-mime-view-all-parts
1594     "b" gnus-article-view-part)
1595
1596   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1597     "z" gnus-article-date-ut
1598     "u" gnus-article-date-ut
1599     "l" gnus-article-date-local
1600     "e" gnus-article-date-lapsed
1601     "o" gnus-article-date-original
1602     "i" gnus-article-date-iso8601
1603     "s" gnus-article-date-user)
1604
1605   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1606     "t" gnus-article-remove-trailing-blank-lines
1607     "l" gnus-article-strip-leading-blank-lines
1608     "m" gnus-article-strip-multiple-blank-lines
1609     "a" gnus-article-strip-blank-lines
1610     "A" gnus-article-strip-all-blank-lines
1611     "s" gnus-article-strip-leading-space
1612     "e" gnus-article-strip-trailing-space)
1613
1614   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1615     "v" gnus-version
1616     "f" gnus-summary-fetch-faq
1617     "d" gnus-summary-describe-group
1618     "h" gnus-summary-describe-briefly
1619     "i" gnus-info-find-node)
1620
1621   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1622     "e" gnus-summary-expire-articles
1623     "\M-\C-e" gnus-summary-expire-articles-now
1624     "\177" gnus-summary-delete-article
1625     [delete] gnus-summary-delete-article
1626     [backspace] gnus-summary-delete-article
1627     "m" gnus-summary-move-article
1628     "r" gnus-summary-respool-article
1629     "w" gnus-summary-edit-article
1630     "c" gnus-summary-copy-article
1631     "B" gnus-summary-crosspost-article
1632     "q" gnus-summary-respool-query
1633     "t" gnus-summary-respool-trace
1634     "i" gnus-summary-import-article
1635     "p" gnus-summary-article-posted-p)
1636
1637   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1638     "o" gnus-summary-save-article
1639     "m" gnus-summary-save-article-mail
1640     "F" gnus-summary-write-article-file
1641     "r" gnus-summary-save-article-rmail
1642     "f" gnus-summary-save-article-file
1643     "b" gnus-summary-save-article-body-file
1644     "h" gnus-summary-save-article-folder
1645     "v" gnus-summary-save-article-vm
1646     "p" gnus-summary-pipe-output
1647     "s" gnus-soup-add-article)
1648
1649   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1650     "b" gnus-summary-display-buttonized
1651     "m" gnus-summary-repair-multipart
1652     "v" gnus-article-view-part
1653     "o" gnus-article-save-part
1654     "c" gnus-article-copy-part
1655     "C" gnus-article-view-part-as-charset
1656     "e" gnus-article-externalize-part
1657     "E" gnus-article-encrypt
1658     "i" gnus-article-inline-part
1659     "|" gnus-article-pipe-part))
1660
1661 (defun gnus-summary-make-menu-bar ()
1662   (gnus-turn-off-edit-menu 'summary)
1663
1664   (unless (boundp 'gnus-summary-misc-menu)
1665
1666     (easy-menu-define
1667      gnus-summary-kill-menu gnus-summary-mode-map ""
1668      (cons
1669       "Score"
1670       (nconc
1671        (list
1672         ["Enter score..." gnus-summary-score-entry t]
1673         ["Customize" gnus-score-customize t])
1674        (gnus-make-score-map 'increase)
1675        (gnus-make-score-map 'lower)
1676        '(("Mark"
1677           ["Kill below" gnus-summary-kill-below t]
1678           ["Mark above" gnus-summary-mark-above t]
1679           ["Tick above" gnus-summary-tick-above t]
1680           ["Clear above" gnus-summary-clear-above t])
1681          ["Current score" gnus-summary-current-score t]
1682          ["Set score" gnus-summary-set-score t]
1683          ["Switch current score file..." gnus-score-change-score-file t]
1684          ["Set mark below..." gnus-score-set-mark-below t]
1685          ["Set expunge below..." gnus-score-set-expunge-below t]
1686          ["Edit current score file" gnus-score-edit-current-scores t]
1687          ["Edit score file" gnus-score-edit-file t]
1688          ["Trace score" gnus-score-find-trace t]
1689          ["Find words" gnus-score-find-favourite-words t]
1690          ["Rescore buffer" gnus-summary-rescore t]
1691          ["Increase score..." gnus-summary-increase-score t]
1692          ["Lower score..." gnus-summary-lower-score t]))))
1693
1694     ;; Define both the Article menu in the summary buffer and the equivalent
1695     ;; Commands menu in the article buffer here for consistency.
1696     (let ((innards
1697            '(("Hide"
1698               ["All" gnus-article-hide t]
1699               ["Headers" gnus-article-hide-headers t]
1700               ["Signature" gnus-article-hide-signature t]
1701               ["Citation" gnus-article-hide-citation t]
1702               ["List identifiers" gnus-article-hide-list-identifiers t]
1703               ["PGP" gnus-article-hide-pgp t]
1704               ["Banner" gnus-article-strip-banner t]
1705               ["Boring headers" gnus-article-hide-boring-headers t])
1706              ("Highlight"
1707               ["All" gnus-article-highlight t]
1708               ["Headers" gnus-article-highlight-headers t]
1709               ["Signature" gnus-article-highlight-signature t]
1710               ["Citation" gnus-article-highlight-citation t])
1711              ("MIME"
1712               ["Words" gnus-article-decode-mime-words t]
1713               ["Charset" gnus-article-decode-charset t]
1714               ["QP" gnus-article-de-quoted-unreadable t]
1715               ["Base64" gnus-article-de-base64-unreadable t]
1716               ["View all" gnus-mime-view-all-parts t]
1717               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt]
1718               ["Encrypt" gnus-article-encrypt])
1719              ("Date"
1720               ["Local" gnus-article-date-local t]
1721               ["ISO8601" gnus-article-date-iso8601 t]
1722               ["UT" gnus-article-date-ut t]
1723               ["Original" gnus-article-date-original t]
1724               ["Lapsed" gnus-article-date-lapsed t]
1725               ["User-defined" gnus-article-date-user t])
1726              ("Washing"
1727               ("Remove Blanks"
1728                ["Leading" gnus-article-strip-leading-blank-lines t]
1729                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1730                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1731                ["All of the above" gnus-article-strip-blank-lines t]
1732                ["All" gnus-article-strip-all-blank-lines t]
1733                ["Leading space" gnus-article-strip-leading-space t]
1734                ["Trailing space" gnus-article-strip-trailing-space t])
1735               ["Overstrike" gnus-article-treat-overstrike t]
1736               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1737               ["Emphasis" gnus-article-emphasize t]
1738               ["Word wrap" gnus-article-fill-cited-article t]
1739               ["Fill long lines" gnus-article-fill-long-lines t]
1740               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1741               ["CR" gnus-article-remove-cr t]
1742               ["Show X-Face" gnus-article-display-x-face t]
1743               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1744               ["Base64" gnus-article-de-base64-unreadable t]
1745               ["Rot 13" gnus-summary-caesar-message t]
1746               ["Unix pipe" gnus-summary-pipe-message t]
1747               ["Add buttons" gnus-article-add-buttons t]
1748               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1749               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1750               ["Verbose header" gnus-summary-verbose-headers t]
1751               ["Toggle header" gnus-summary-toggle-header t]
1752               ["Html" gnus-article-wash-html t]
1753               ["HZ" gnus-article-decode-HZ t])
1754              ("Output"
1755               ["Save in default format" gnus-summary-save-article t]
1756               ["Save in file" gnus-summary-save-article-file t]
1757               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1758               ["Save in MH folder" gnus-summary-save-article-folder t]
1759               ["Save in VM folder" gnus-summary-save-article-vm t]
1760               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1761               ["Save body in file" gnus-summary-save-article-body-file t]
1762               ["Pipe through a filter" gnus-summary-pipe-output t]
1763               ["Add to SOUP packet" gnus-soup-add-article t]
1764               ["Print" gnus-summary-print-article t])
1765              ("Backend"
1766               ["Respool article..." gnus-summary-respool-article t]
1767               ["Move article..." gnus-summary-move-article
1768                (gnus-check-backend-function
1769                 'request-move-article gnus-newsgroup-name)]
1770               ["Copy article..." gnus-summary-copy-article t]
1771               ["Crosspost article..." gnus-summary-crosspost-article
1772                (gnus-check-backend-function
1773                 'request-replace-article gnus-newsgroup-name)]
1774               ["Import file..." gnus-summary-import-article t]
1775               ["Check if posted" gnus-summary-article-posted-p t]
1776               ["Edit article" gnus-summary-edit-article
1777                (not (gnus-group-read-only-p))]
1778               ["Delete article" gnus-summary-delete-article
1779                (gnus-check-backend-function
1780                 'request-expire-articles gnus-newsgroup-name)]
1781               ["Query respool" gnus-summary-respool-query t]
1782               ["Trace respool" gnus-summary-respool-trace t]
1783               ["Delete expirable articles" gnus-summary-expire-articles-now
1784                (gnus-check-backend-function
1785                 'request-expire-articles gnus-newsgroup-name)])
1786              ("Extract"
1787               ["Uudecode" gnus-uu-decode-uu t]
1788               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1789               ["Unshar" gnus-uu-decode-unshar t]
1790               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1791               ["Save" gnus-uu-decode-save t]
1792               ["Binhex" gnus-uu-decode-binhex t]
1793               ["Postscript" gnus-uu-decode-postscript t])
1794              ("Cache"
1795               ["Enter article" gnus-cache-enter-article t]
1796               ["Remove article" gnus-cache-remove-article t])
1797              ["Translate" gnus-article-babel t]
1798              ["Select article buffer" gnus-summary-select-article-buffer t]
1799              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1800              ["Isearch article..." gnus-summary-isearch-article t]
1801              ["Beginning of the article" gnus-summary-beginning-of-article t]
1802              ["End of the article" gnus-summary-end-of-article t]
1803              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1804              ["Fetch referenced articles" gnus-summary-refer-references t]
1805              ["Fetch current thread" gnus-summary-refer-thread t]
1806              ["Fetch article with id..." gnus-summary-refer-article t]
1807              ["Redisplay" gnus-summary-show-article t])))
1808       (easy-menu-define
1809        gnus-summary-article-menu gnus-summary-mode-map ""
1810        (cons "Article" innards))
1811
1812       (easy-menu-define
1813        gnus-article-commands-menu gnus-article-mode-map ""
1814        (cons "Commands" innards)))
1815
1816     (easy-menu-define
1817      gnus-summary-thread-menu gnus-summary-mode-map ""
1818      '("Threads"
1819        ["Toggle threading" gnus-summary-toggle-threads t]
1820        ["Hide threads" gnus-summary-hide-all-threads t]
1821        ["Show threads" gnus-summary-show-all-threads t]
1822        ["Hide thread" gnus-summary-hide-thread t]
1823        ["Show thread" gnus-summary-show-thread t]
1824        ["Go to next thread" gnus-summary-next-thread t]
1825        ["Go to previous thread" gnus-summary-prev-thread t]
1826        ["Go down thread" gnus-summary-down-thread t]
1827        ["Go up thread" gnus-summary-up-thread t]
1828        ["Top of thread" gnus-summary-top-thread t]
1829        ["Mark thread as read" gnus-summary-kill-thread t]
1830        ["Lower thread score" gnus-summary-lower-thread t]
1831        ["Raise thread score" gnus-summary-raise-thread t]
1832        ["Rethread current" gnus-summary-rethread-current t]))
1833
1834     (easy-menu-define
1835      gnus-summary-post-menu gnus-summary-mode-map ""
1836      '("Post"
1837        ["Post an article" gnus-summary-post-news t]
1838        ["Followup" gnus-summary-followup t]
1839        ["Followup and yank" gnus-summary-followup-with-original t]
1840        ["Supersede article" gnus-summary-supersede-article t]
1841        ["Cancel article" gnus-summary-cancel-article t]
1842        ["Reply" gnus-summary-reply t]
1843        ["Reply and yank" gnus-summary-reply-with-original t]
1844        ["Wide reply" gnus-summary-wide-reply t]
1845        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1846        ["Mail forward" gnus-summary-mail-forward t]
1847        ["Post forward" gnus-summary-post-forward t]
1848        ["Digest and mail" gnus-uu-digest-mail-forward t]
1849        ["Digest and post" gnus-uu-digest-post-forward t]
1850        ["Resend message" gnus-summary-resend-message t]
1851        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1852        ["Send a mail" gnus-summary-mail-other-window t]
1853        ["Uuencode and post" gnus-uu-post-news t]
1854        ["Followup via news" gnus-summary-followup-to-mail t]
1855        ["Followup via news and yank"
1856         gnus-summary-followup-to-mail-with-original t]
1857        ;;("Draft"
1858        ;;["Send" gnus-summary-send-draft t]
1859        ;;["Send bounced" gnus-resend-bounced-mail t])
1860        ))
1861
1862     (easy-menu-define
1863      gnus-summary-misc-menu gnus-summary-mode-map ""
1864      '("Misc"
1865        ("Mark Read"
1866         ["Mark as read" gnus-summary-mark-as-read-forward t]
1867         ["Mark same subject and select"
1868          gnus-summary-kill-same-subject-and-select t]
1869         ["Mark same subject" gnus-summary-kill-same-subject t]
1870         ["Catchup" gnus-summary-catchup t]
1871         ["Catchup all" gnus-summary-catchup-all t]
1872         ["Catchup to here" gnus-summary-catchup-to-here t]
1873         ["Catchup region" gnus-summary-mark-region-as-read t]
1874         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1875        ("Mark Various"
1876         ["Tick" gnus-summary-tick-article-forward t]
1877         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1878         ["Remove marks" gnus-summary-clear-mark-forward t]
1879         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1880         ["Set bookmark" gnus-summary-set-bookmark t]
1881         ["Remove bookmark" gnus-summary-remove-bookmark t])
1882        ("Mark Limit"
1883         ["Marks..." gnus-summary-limit-to-marks t]
1884         ["Subject..." gnus-summary-limit-to-subject t]
1885         ["Author..." gnus-summary-limit-to-author t]
1886         ["Age..." gnus-summary-limit-to-age t]
1887         ["Extra..." gnus-summary-limit-to-extra t]
1888         ["Score" gnus-summary-limit-to-score t]
1889         ["Unread" gnus-summary-limit-to-unread t]
1890         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1891         ["Articles" gnus-summary-limit-to-articles t]
1892         ["Pop limit" gnus-summary-pop-limit t]
1893         ["Show dormant" gnus-summary-limit-include-dormant t]
1894         ["Hide childless dormant"
1895          gnus-summary-limit-exclude-childless-dormant t]
1896         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1897         ["Hide marked" gnus-summary-limit-exclude-marks t]
1898         ["Show expunged" gnus-summary-show-all-expunged t])
1899        ("Process Mark"
1900         ["Set mark" gnus-summary-mark-as-processable t]
1901         ["Remove mark" gnus-summary-unmark-as-processable t]
1902         ["Remove all marks" gnus-summary-unmark-all-processable t]
1903         ["Mark above" gnus-uu-mark-over t]
1904         ["Mark series" gnus-uu-mark-series t]
1905         ["Mark region" gnus-uu-mark-region t]
1906         ["Unmark region" gnus-uu-unmark-region t]
1907         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1908         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1909         ["Mark all" gnus-uu-mark-all t]
1910         ["Mark buffer" gnus-uu-mark-buffer t]
1911         ["Mark sparse" gnus-uu-mark-sparse t]
1912         ["Mark thread" gnus-uu-mark-thread t]
1913         ["Unmark thread" gnus-uu-unmark-thread t]
1914         ("Process Mark Sets"
1915          ["Kill" gnus-summary-kill-process-mark t]
1916          ["Yank" gnus-summary-yank-process-mark
1917           gnus-newsgroup-process-stack]
1918          ["Save" gnus-summary-save-process-mark t]))
1919        ("Scroll article"
1920         ["Page forward" gnus-summary-next-page t]
1921         ["Page backward" gnus-summary-prev-page t]
1922         ["Line forward" gnus-summary-scroll-up t])
1923        ("Move"
1924         ["Next unread article" gnus-summary-next-unread-article t]
1925         ["Previous unread article" gnus-summary-prev-unread-article t]
1926         ["Next article" gnus-summary-next-article t]
1927         ["Previous article" gnus-summary-prev-article t]
1928         ["Next unread subject" gnus-summary-next-unread-subject t]
1929         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1930         ["Next article same subject" gnus-summary-next-same-subject t]
1931         ["Previous article same subject" gnus-summary-prev-same-subject t]
1932         ["First unread article" gnus-summary-first-unread-article t]
1933         ["Best unread article" gnus-summary-best-unread-article t]
1934         ["Go to subject number..." gnus-summary-goto-subject t]
1935         ["Go to article number..." gnus-summary-goto-article t]
1936         ["Go to the last article" gnus-summary-goto-last-article t]
1937         ["Pop article off history" gnus-summary-pop-article t])
1938        ("Sort"
1939         ["Sort by number" gnus-summary-sort-by-number t]
1940         ["Sort by author" gnus-summary-sort-by-author t]
1941         ["Sort by subject" gnus-summary-sort-by-subject t]
1942         ["Sort by date" gnus-summary-sort-by-date t]
1943         ["Sort by score" gnus-summary-sort-by-score t]
1944         ["Sort by lines" gnus-summary-sort-by-lines t]
1945         ["Sort by characters" gnus-summary-sort-by-chars t])
1946        ("Help"
1947         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1948         ["Describe group" gnus-summary-describe-group t]
1949         ["Read manual" gnus-info-find-node t])
1950        ("Modes"
1951         ["Pick and read" gnus-pick-mode t]
1952         ["Binary" gnus-binary-mode t])
1953        ("Regeneration"
1954         ["Regenerate" gnus-summary-prepare t]
1955         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1956         ["Toggle threading" gnus-summary-toggle-threads t])
1957        ["Filter articles..." gnus-summary-execute-command t]
1958        ["Run command on subjects..." gnus-summary-universal-argument t]
1959        ["Search articles forward..." gnus-summary-search-article-forward t]
1960        ["Search articles backward..." gnus-summary-search-article-backward t]
1961        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1962        ["Expand window" gnus-summary-expand-window t]
1963        ["Expire expirable articles" gnus-summary-expire-articles
1964         (gnus-check-backend-function
1965          'request-expire-articles gnus-newsgroup-name)]
1966        ["Edit local kill file" gnus-summary-edit-local-kill t]
1967        ["Edit main kill file" gnus-summary-edit-global-kill t]
1968        ["Edit group parameters" gnus-summary-edit-parameters t]
1969        ["Customize group parameters" gnus-summary-customize-parameters t]
1970        ["Send a bug report" gnus-bug t]
1971        ("Exit"
1972         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1973         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1974         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1975         ["Exit group" gnus-summary-exit t]
1976         ["Exit group without updating" gnus-summary-exit-no-update t]
1977         ["Exit and goto next group" gnus-summary-next-group t]
1978         ["Exit and goto prev group" gnus-summary-prev-group t]
1979         ["Reselect group" gnus-summary-reselect-current-group t]
1980         ["Rescan group" gnus-summary-rescan-group t]
1981         ["Update dribble" gnus-summary-save-newsrc t])))
1982
1983     (gnus-run-hooks 'gnus-summary-menu-hook)))
1984
1985 (defun gnus-score-set-default (var value)
1986   "A version of set that updates the GNU Emacs menu-bar."
1987   (set var value)
1988   ;; It is the message that forces the active status to be updated.
1989   (message ""))
1990
1991 (defun gnus-make-score-map (type)
1992   "Make a summary score map of type TYPE."
1993   (if t
1994       nil
1995     (let ((headers '(("author" "from" string)
1996                      ("subject" "subject" string)
1997                      ("article body" "body" string)
1998                      ("article head" "head" string)
1999                      ("xref" "xref" string)
2000                      ("extra header" "extra" string)
2001                      ("lines" "lines" number)
2002                      ("followups to author" "followup" string)))
2003           (types '((number ("less than" <)
2004                            ("greater than" >)
2005                            ("equal" =))
2006                    (string ("substring" s)
2007                            ("exact string" e)
2008                            ("fuzzy string" f)
2009                            ("regexp" r))))
2010           (perms '(("temporary" (current-time-string))
2011                    ("permanent" nil)
2012                    ("immediate" now)))
2013           header)
2014       (list
2015        (apply
2016         'nconc
2017         (list
2018          (if (eq type 'lower)
2019              "Lower score"
2020            "Increase score"))
2021         (let (outh)
2022           (while headers
2023             (setq header (car headers))
2024             (setq outh
2025                   (cons
2026                    (apply
2027                     'nconc
2028                     (list (car header))
2029                     (let ((ts (cdr (assoc (nth 2 header) types)))
2030                           outt)
2031                       (while ts
2032                         (setq outt
2033                               (cons
2034                                (apply
2035                                 'nconc
2036                                 (list (caar ts))
2037                                 (let ((ps perms)
2038                                       outp)
2039                                   (while ps
2040                                     (setq outp
2041                                           (cons
2042                                            (vector
2043                                             (caar ps)
2044                                             (list
2045                                              'gnus-summary-score-entry
2046                                              (nth 1 header)
2047                                              (if (or (string= (nth 1 header)
2048                                                               "head")
2049                                                      (string= (nth 1 header)
2050                                                               "body"))
2051                                                  ""
2052                                                (list 'gnus-summary-header
2053                                                      (nth 1 header)))
2054                                              (list 'quote (nth 1 (car ts)))
2055                                              (list 'gnus-score-delta-default
2056                                                    nil)
2057                                              (nth 1 (car ps))
2058                                              t)
2059                                             t)
2060                                            outp))
2061                                     (setq ps (cdr ps)))
2062                                   (list (nreverse outp))))
2063                                outt))
2064                         (setq ts (cdr ts)))
2065                       (list (nreverse outt))))
2066                    outh))
2067             (setq headers (cdr headers)))
2068           (list (nreverse outh))))))))
2069
2070 \f
2071
2072 (defun gnus-summary-mode (&optional group)
2073   "Major mode for reading articles.
2074
2075 All normal editing commands are switched off.
2076 \\<gnus-summary-mode-map>
2077 Each line in this buffer represents one article.  To read an
2078 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2079 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2080 respectively.
2081
2082 You can also post articles and send mail from this buffer.  To
2083 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2084 of an article, type `\\[gnus-summary-reply]'.
2085
2086 There are approx. one gazillion commands you can execute in this
2087 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2088
2089 The following commands are available:
2090
2091 \\{gnus-summary-mode-map}"
2092   (interactive)
2093   (when (gnus-visual-p 'summary-menu 'menu)
2094     (gnus-summary-make-menu-bar))
2095   (kill-all-local-variables)
2096   (gnus-summary-make-local-variables)
2097   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2098     (gnus-summary-make-local-variables))
2099   (gnus-make-thread-indent-array)
2100   (gnus-simplify-mode-line)
2101   (setq major-mode 'gnus-summary-mode)
2102   (setq mode-name "Summary")
2103   (make-local-variable 'minor-mode-alist)
2104   (use-local-map gnus-summary-mode-map)
2105   (buffer-disable-undo)
2106   (setq buffer-read-only t)             ;Disable modification
2107   (setq truncate-lines t)
2108   (setq selective-display t)
2109   (setq selective-display-ellipses t)   ;Display `...'
2110   (gnus-summary-set-display-table)
2111   (gnus-set-default-directory)
2112   (setq gnus-newsgroup-name group)
2113   (make-local-variable 'gnus-summary-line-format)
2114   (make-local-variable 'gnus-summary-line-format-spec)
2115   (make-local-variable 'gnus-summary-dummy-line-format)
2116   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2117   (make-local-variable 'gnus-summary-mark-positions)
2118   (make-local-hook 'pre-command-hook)
2119   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2120   (gnus-run-hooks 'gnus-summary-mode-hook)
2121   (mm-enable-multibyte-mule4)
2122   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2123   (gnus-update-summary-mark-positions))
2124
2125 (defun gnus-summary-make-local-variables ()
2126   "Make all the local summary buffer variables."
2127   (let (global)
2128     (dolist (local gnus-summary-local-variables)
2129       (if (consp local)
2130           (progn
2131             (if (eq (cdr local) 'global)
2132                 ;; Copy the global value of the variable.
2133                 (setq global (symbol-value (car local)))
2134               ;; Use the value from the list.
2135               (setq global (eval (cdr local))))
2136             (set (make-local-variable (car local)) global))
2137         ;; Simple nil-valued local variable.
2138         (set (make-local-variable local) nil)))))
2139
2140 (defun gnus-summary-clear-local-variables ()
2141   (let ((locals gnus-summary-local-variables))
2142     (while locals
2143       (if (consp (car locals))
2144           (and (vectorp (caar locals))
2145                (set (caar locals) nil))
2146         (and (vectorp (car locals))
2147              (set (car locals) nil)))
2148       (setq locals (cdr locals)))))
2149
2150 ;; Summary data functions.
2151
2152 (defmacro gnus-data-number (data)
2153   `(car ,data))
2154
2155 (defmacro gnus-data-set-number (data number)
2156   `(setcar ,data ,number))
2157
2158 (defmacro gnus-data-mark (data)
2159   `(nth 1 ,data))
2160
2161 (defmacro gnus-data-set-mark (data mark)
2162   `(setcar (nthcdr 1 ,data) ,mark))
2163
2164 (defmacro gnus-data-pos (data)
2165   `(nth 2 ,data))
2166
2167 (defmacro gnus-data-set-pos (data pos)
2168   `(setcar (nthcdr 2 ,data) ,pos))
2169
2170 (defmacro gnus-data-header (data)
2171   `(nth 3 ,data))
2172
2173 (defmacro gnus-data-set-header (data header)
2174   `(setf (nth 3 ,data) ,header))
2175
2176 (defmacro gnus-data-level (data)
2177   `(nth 4 ,data))
2178
2179 (defmacro gnus-data-unread-p (data)
2180   `(= (nth 1 ,data) gnus-unread-mark))
2181
2182 (defmacro gnus-data-read-p (data)
2183   `(/= (nth 1 ,data) gnus-unread-mark))
2184
2185 (defmacro gnus-data-pseudo-p (data)
2186   `(consp (nth 3 ,data)))
2187
2188 (defmacro gnus-data-find (number)
2189   `(assq ,number gnus-newsgroup-data))
2190
2191 (defmacro gnus-data-find-list (number &optional data)
2192   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2193      (memq (assq ,number bdata)
2194            bdata)))
2195
2196 (defmacro gnus-data-make (number mark pos header level)
2197   `(list ,number ,mark ,pos ,header ,level))
2198
2199 (defun gnus-data-enter (after-article number mark pos header level offset)
2200   (let ((data (gnus-data-find-list after-article)))
2201     (unless data
2202       (error "No such article: %d" after-article))
2203     (setcdr data (cons (gnus-data-make number mark pos header level)
2204                        (cdr data)))
2205     (setq gnus-newsgroup-data-reverse nil)
2206     (gnus-data-update-list (cddr data) offset)))
2207
2208 (defun gnus-data-enter-list (after-article list &optional offset)
2209   (when list
2210     (let ((data (and after-article (gnus-data-find-list after-article)))
2211           (ilist list))
2212       (if (not (or data
2213                    after-article))
2214           (let ((odata gnus-newsgroup-data))
2215             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2216             (when offset
2217               (gnus-data-update-list odata offset)))
2218         ;; Find the last element in the list to be spliced into the main
2219         ;; list.
2220         (while (cdr list)
2221           (setq list (cdr list)))
2222         (if (not data)
2223             (progn
2224               (setcdr list gnus-newsgroup-data)
2225               (setq gnus-newsgroup-data ilist)
2226               (when offset
2227                 (gnus-data-update-list (cdr list) offset)))
2228           (setcdr list (cdr data))
2229           (setcdr data ilist)
2230           (when offset
2231             (gnus-data-update-list (cdr list) offset))))
2232       (setq gnus-newsgroup-data-reverse nil))))
2233
2234 (defun gnus-data-remove (article &optional offset)
2235   (let ((data gnus-newsgroup-data))
2236     (if (= (gnus-data-number (car data)) article)
2237         (progn
2238           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2239                 gnus-newsgroup-data-reverse nil)
2240           (when offset
2241             (gnus-data-update-list gnus-newsgroup-data offset)))
2242       (while (cdr data)
2243         (when (= (gnus-data-number (cadr data)) article)
2244           (setcdr data (cddr data))
2245           (when offset
2246             (gnus-data-update-list (cdr data) offset))
2247           (setq data nil
2248                 gnus-newsgroup-data-reverse nil))
2249         (setq data (cdr data))))))
2250
2251 (defmacro gnus-data-list (backward)
2252   `(if ,backward
2253        (or gnus-newsgroup-data-reverse
2254            (setq gnus-newsgroup-data-reverse
2255                  (reverse gnus-newsgroup-data)))
2256      gnus-newsgroup-data))
2257
2258 (defun gnus-data-update-list (data offset)
2259   "Add OFFSET to the POS of all data entries in DATA."
2260   (setq gnus-newsgroup-data-reverse nil)
2261   (while data
2262     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2263     (setq data (cdr data))))
2264
2265 (defun gnus-summary-article-pseudo-p (article)
2266   "Say whether this article is a pseudo article or not."
2267   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2268
2269 (defmacro gnus-summary-article-sparse-p (article)
2270   "Say whether this article is a sparse article or not."
2271   `(memq ,article gnus-newsgroup-sparse))
2272
2273 (defmacro gnus-summary-article-ancient-p (article)
2274   "Say whether this article is a sparse article or not."
2275   `(memq ,article gnus-newsgroup-ancient))
2276
2277 (defun gnus-article-parent-p (number)
2278   "Say whether this article is a parent or not."
2279   (let ((data (gnus-data-find-list number)))
2280     (and (cdr data)                     ; There has to be an article after...
2281          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2282             (gnus-data-level (nth 1 data))))))
2283
2284 (defun gnus-article-children (number)
2285   "Return a list of all children to NUMBER."
2286   (let* ((data (gnus-data-find-list number))
2287          (level (gnus-data-level (car data)))
2288          children)
2289     (setq data (cdr data))
2290     (while (and data
2291                 (= (gnus-data-level (car data)) (1+ level)))
2292       (push (gnus-data-number (car data)) children)
2293       (setq data (cdr data)))
2294     children))
2295
2296 (defmacro gnus-summary-skip-intangible ()
2297   "If the current article is intangible, then jump to a different article."
2298   '(let ((to (get-text-property (point) 'gnus-intangible)))
2299      (and to (gnus-summary-goto-subject to))))
2300
2301 (defmacro gnus-summary-article-intangible-p ()
2302   "Say whether this article is intangible or not."
2303   '(get-text-property (point) 'gnus-intangible))
2304
2305 (defun gnus-article-read-p (article)
2306   "Say whether ARTICLE is read or not."
2307   (not (or (memq article gnus-newsgroup-marked)
2308            (memq article gnus-newsgroup-unreads)
2309            (memq article gnus-newsgroup-unselected)
2310            (memq article gnus-newsgroup-dormant))))
2311
2312 ;; Some summary mode macros.
2313
2314 (defmacro gnus-summary-article-number ()
2315   "The article number of the article on the current line.
2316 If there isn's an article number here, then we return the current
2317 article number."
2318   '(progn
2319      (gnus-summary-skip-intangible)
2320      (or (get-text-property (point) 'gnus-number)
2321          (gnus-summary-last-subject))))
2322
2323 (defmacro gnus-summary-article-header (&optional number)
2324   "Return the header of article NUMBER."
2325   `(gnus-data-header (gnus-data-find
2326                       ,(or number '(gnus-summary-article-number)))))
2327
2328 (defmacro gnus-summary-thread-level (&optional number)
2329   "Return the level of thread that starts with article NUMBER."
2330   `(if (and (eq gnus-summary-make-false-root 'dummy)
2331             (get-text-property (point) 'gnus-intangible))
2332        0
2333      (gnus-data-level (gnus-data-find
2334                        ,(or number '(gnus-summary-article-number))))))
2335
2336 (defmacro gnus-summary-article-mark (&optional number)
2337   "Return the mark of article NUMBER."
2338   `(gnus-data-mark (gnus-data-find
2339                     ,(or number '(gnus-summary-article-number)))))
2340
2341 (defmacro gnus-summary-article-pos (&optional number)
2342   "Return the position of the line of article NUMBER."
2343   `(gnus-data-pos (gnus-data-find
2344                    ,(or number '(gnus-summary-article-number)))))
2345
2346 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2347 (defmacro gnus-summary-article-subject (&optional number)
2348   "Return current subject string or nil if nothing."
2349   `(let ((headers
2350           ,(if number
2351                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2352              '(gnus-data-header (assq (gnus-summary-article-number)
2353                                       gnus-newsgroup-data)))))
2354      (and headers
2355           (vectorp headers)
2356           (mail-header-subject headers))))
2357
2358 (defmacro gnus-summary-article-score (&optional number)
2359   "Return current article score."
2360   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2361                   gnus-newsgroup-scored))
2362        gnus-summary-default-score 0))
2363
2364 (defun gnus-summary-article-children (&optional number)
2365   "Return a list of article numbers that are children of article NUMBER."
2366   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2367          (level (gnus-data-level (car data)))
2368          l children)
2369     (while (and (setq data (cdr data))
2370                 (> (setq l (gnus-data-level (car data))) level))
2371       (and (= (1+ level) l)
2372            (push (gnus-data-number (car data))
2373                  children)))
2374     (nreverse children)))
2375
2376 (defun gnus-summary-article-parent (&optional number)
2377   "Return the article number of the parent of article NUMBER."
2378   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2379                                     (gnus-data-list t)))
2380          (level (gnus-data-level (car data))))
2381     (if (zerop level)
2382         ()                              ; This is a root.
2383       ;; We search until we find an article with a level less than
2384       ;; this one.  That function has to be the parent.
2385       (while (and (setq data (cdr data))
2386                   (not (< (gnus-data-level (car data)) level))))
2387       (and data (gnus-data-number (car data))))))
2388
2389 (defun gnus-unread-mark-p (mark)
2390   "Say whether MARK is the unread mark."
2391   (= mark gnus-unread-mark))
2392
2393 (defun gnus-read-mark-p (mark)
2394   "Say whether MARK is one of the marks that mark as read.
2395 This is all marks except unread, ticked, dormant, and expirable."
2396   (not (or (= mark gnus-unread-mark)
2397            (= mark gnus-ticked-mark)
2398            (= mark gnus-dormant-mark)
2399            (= mark gnus-expirable-mark))))
2400
2401 (defmacro gnus-article-mark (number)
2402   "Return the MARK of article NUMBER.
2403 This macro should only be used when computing the mark the \"first\"
2404 time; i.e., when generating the summary lines.  After that,
2405 `gnus-summary-article-mark' should be used to examine the
2406 marks of articles."
2407   `(cond
2408     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2409     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2410     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2411     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2412     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2413     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2414     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2415     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2416            gnus-ancient-mark))))
2417
2418 ;; Saving hidden threads.
2419
2420 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2421 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2422
2423 (defmacro gnus-save-hidden-threads (&rest forms)
2424   "Save hidden threads, eval FORMS, and restore the hidden threads."
2425   (let ((config (make-symbol "config")))
2426     `(let ((,config (gnus-hidden-threads-configuration)))
2427        (unwind-protect
2428            (save-excursion
2429              ,@forms)
2430          (gnus-restore-hidden-threads-configuration ,config)))))
2431
2432 (defun gnus-data-compute-positions ()
2433   "Compute the positions of all articles."
2434   (setq gnus-newsgroup-data-reverse nil)
2435   (let ((data gnus-newsgroup-data))
2436     (save-excursion
2437       (gnus-save-hidden-threads
2438         (gnus-summary-show-all-threads)
2439         (goto-char (point-min))
2440         (while data
2441           (while (get-text-property (point) 'gnus-intangible)
2442             (forward-line 1))
2443           (gnus-data-set-pos (car data) (+ (point) 3))
2444           (setq data (cdr data))
2445           (forward-line 1))))))
2446
2447 (defun gnus-hidden-threads-configuration ()
2448   "Return the current hidden threads configuration."
2449   (save-excursion
2450     (let (config)
2451       (goto-char (point-min))
2452       (while (search-forward "\r" nil t)
2453         (push (1- (point)) config))
2454       config)))
2455
2456 (defun gnus-restore-hidden-threads-configuration (config)
2457   "Restore hidden threads configuration from CONFIG."
2458   (save-excursion
2459     (let (point buffer-read-only)
2460       (while (setq point (pop config))
2461         (when (and (< point (point-max))
2462                    (goto-char point)
2463                    (eq (char-after) ?\n))
2464           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2465
2466 ;; Various summary mode internalish functions.
2467
2468 (defun gnus-mouse-pick-article (e)
2469   (interactive "e")
2470   (mouse-set-point e)
2471   (gnus-summary-next-page nil t))
2472
2473 (defun gnus-summary-set-display-table ()
2474   "Change the display table.
2475 Odd characters have a tendency to mess
2476 up nicely formatted displays - we make all possible glyphs
2477 display only a single character."
2478
2479   ;; We start from the standard display table, if any.
2480   (let ((table (or (copy-sequence standard-display-table)
2481                    (make-display-table)))
2482         (i 32))
2483     ;; Nix out all the control chars...
2484     (while (>= (setq i (1- i)) 0)
2485       (aset table i [??]))
2486     ;; ... but not newline and cr, of course.  (cr is necessary for the
2487     ;; selective display).
2488     (aset table ?\n nil)
2489     (aset table ?\r nil)
2490     ;; We keep TAB as well.
2491     (aset table ?\t nil)
2492     ;; We nix out any glyphs over 126 that are not set already.
2493     (let ((i 256))
2494       (while (>= (setq i (1- i)) 127)
2495         ;; Only modify if the entry is nil.
2496         (unless (aref table i)
2497           (aset table i [??]))))
2498     (setq buffer-display-table table)))
2499
2500 (defun gnus-summary-setup-buffer (group)
2501   "Initialize summary buffer."
2502   (let ((buffer (concat "*Summary " group "*")))
2503     (if (get-buffer buffer)
2504         (progn
2505           (set-buffer buffer)
2506           (setq gnus-summary-buffer (current-buffer))
2507           (not gnus-newsgroup-prepared))
2508       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2509       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2510       (gnus-summary-mode group)
2511       (when gnus-carpal
2512         (gnus-carpal-setup-buffer 'summary))
2513       (unless gnus-single-article-buffer
2514         (make-local-variable 'gnus-article-buffer)
2515         (make-local-variable 'gnus-article-current)
2516         (make-local-variable 'gnus-original-article-buffer))
2517       (setq gnus-newsgroup-name group)
2518       t)))
2519
2520 (defun gnus-set-global-variables ()
2521   "Set the global equivalents of the buffer-local variables.
2522 They are set to the latest values they had.  These reflect the summary
2523 buffer that was in action when the last article was fetched."
2524   (when (eq major-mode 'gnus-summary-mode)
2525     (setq gnus-summary-buffer (current-buffer))
2526     (let ((name gnus-newsgroup-name)
2527           (marked gnus-newsgroup-marked)
2528           (unread gnus-newsgroup-unreads)
2529           (headers gnus-current-headers)
2530           (data gnus-newsgroup-data)
2531           (summary gnus-summary-buffer)
2532           (article-buffer gnus-article-buffer)
2533           (original gnus-original-article-buffer)
2534           (gac gnus-article-current)
2535           (reffed gnus-reffed-article-number)
2536           (score-file gnus-current-score-file)
2537           (default-charset gnus-newsgroup-charset)
2538           vlist)
2539       (let ((locals gnus-newsgroup-variables))
2540         (while locals
2541           (if (consp (car locals))
2542               (push (eval (caar locals)) vlist)
2543             (push (eval (car locals)) vlist))
2544           (setq locals (cdr locals)))
2545         (setq vlist (nreverse vlist)))
2546       (save-excursion
2547         (set-buffer gnus-group-buffer)
2548         (setq gnus-newsgroup-name name
2549               gnus-newsgroup-marked marked
2550               gnus-newsgroup-unreads unread
2551               gnus-current-headers headers
2552               gnus-newsgroup-data data
2553               gnus-article-current gac
2554               gnus-summary-buffer summary
2555               gnus-article-buffer article-buffer
2556               gnus-original-article-buffer original
2557               gnus-reffed-article-number reffed
2558               gnus-current-score-file score-file
2559               gnus-newsgroup-charset default-charset)
2560         (let ((locals gnus-newsgroup-variables))
2561           (while locals
2562             (if (consp (car locals))
2563                 (set (caar locals) (pop vlist))
2564               (set (car locals) (pop vlist)))
2565             (setq locals (cdr locals))))
2566         ;; The article buffer also has local variables.
2567         (when (gnus-buffer-live-p gnus-article-buffer)
2568           (set-buffer gnus-article-buffer)
2569           (setq gnus-summary-buffer summary))))))
2570
2571 (defun gnus-summary-article-unread-p (article)
2572   "Say whether ARTICLE is unread or not."
2573   (memq article gnus-newsgroup-unreads))
2574
2575 (defun gnus-summary-first-article-p (&optional article)
2576   "Return whether ARTICLE is the first article in the buffer."
2577   (if (not (setq article (or article (gnus-summary-article-number))))
2578       nil
2579     (eq article (caar gnus-newsgroup-data))))
2580
2581 (defun gnus-summary-last-article-p (&optional article)
2582   "Return whether ARTICLE is the last article in the buffer."
2583   (if (not (setq article (or article (gnus-summary-article-number))))
2584       ;; All non-existent numbers are the last article.  :-)
2585       t
2586     (not (cdr (gnus-data-find-list article)))))
2587
2588 (defun gnus-make-thread-indent-array ()
2589   (let ((n 200))
2590     (unless (and gnus-thread-indent-array
2591                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2592       (setq gnus-thread-indent-array (make-vector 201 "")
2593             gnus-thread-indent-array-level gnus-thread-indent-level)
2594       (while (>= n 0)
2595         (aset gnus-thread-indent-array n
2596               (make-string (* n gnus-thread-indent-level) ? ))
2597         (setq n (1- n))))))
2598
2599 (defun gnus-update-summary-mark-positions ()
2600   "Compute where the summary marks are to go."
2601   (save-excursion
2602     (when (gnus-buffer-exists-p gnus-summary-buffer)
2603       (set-buffer gnus-summary-buffer))
2604     (let ((gnus-replied-mark 129)
2605           (gnus-score-below-mark 130)
2606           (gnus-score-over-mark 130)
2607           (gnus-download-mark 131)
2608           (spec gnus-summary-line-format-spec)
2609           gnus-visual pos)
2610       (save-excursion
2611         (gnus-set-work-buffer)
2612         (let ((gnus-summary-line-format-spec spec)
2613               (gnus-newsgroup-downloadable '((0 . t))))
2614           (gnus-summary-insert-line
2615            [0 "" "" "" "" "" 0 0 "" nil]  0 nil 128 t nil "" nil 1)
2616           (goto-char (point-min))
2617           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2618                                              (- (point) 2)))))
2619           (goto-char (point-min))
2620           (push (cons 'replied (and (search-forward "\201" nil t)
2621                                     (- (point) 2)))
2622                 pos)
2623           (goto-char (point-min))
2624           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2625                 pos)
2626           (goto-char (point-min))
2627           (push (cons 'download
2628                       (and (search-forward "\203" nil t) (- (point) 2)))
2629                 pos)))
2630       (setq gnus-summary-mark-positions pos))))
2631
2632 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2633   "Insert a dummy root in the summary buffer."
2634   (beginning-of-line)
2635   (gnus-add-text-properties
2636    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2637    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2638
2639 (defun gnus-summary-from-or-to-or-newsgroups (header)
2640   (let ((to (cdr (assq 'To (mail-header-extra header))))
2641         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2642         (mail-parse-charset gnus-newsgroup-charset)
2643         (mail-parse-ignored-charsets
2644          (save-excursion (set-buffer gnus-summary-buffer)
2645                          gnus-newsgroup-ignored-charsets)))
2646     (cond
2647      ((and to
2648            gnus-ignored-from-addresses
2649            (string-match gnus-ignored-from-addresses
2650                          (mail-header-from header)))
2651       (concat "-> "
2652               (or (car (funcall gnus-extract-address-components
2653                                 (funcall
2654                                  gnus-decode-encoded-word-function to)))
2655                   (funcall gnus-decode-encoded-word-function to))))
2656      ((and newsgroups
2657            gnus-ignored-from-addresses
2658            (string-match gnus-ignored-from-addresses
2659                          (mail-header-from header)))
2660       (concat "=> " newsgroups))
2661      (t
2662       (or (car (funcall gnus-extract-address-components
2663                         (mail-header-from header)))
2664           (mail-header-from header))))))
2665
2666 (defun gnus-summary-insert-line (gnus-tmp-header
2667                                  gnus-tmp-level gnus-tmp-current
2668                                  gnus-tmp-unread gnus-tmp-replied
2669                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2670                                  &optional gnus-tmp-dummy gnus-tmp-score
2671                                  gnus-tmp-process)
2672   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2673          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2674          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2675          (gnus-tmp-score-char
2676           (if (or (null gnus-summary-default-score)
2677                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2678                       gnus-summary-zcore-fuzz))
2679               ?  ;Whitespace
2680             (if (< gnus-tmp-score gnus-summary-default-score)
2681                 gnus-score-below-mark gnus-score-over-mark)))
2682          (gnus-tmp-replied
2683           (cond (gnus-tmp-process gnus-process-mark)
2684                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2685                  gnus-cached-mark)
2686                 (gnus-tmp-replied gnus-replied-mark)
2687                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2688                  gnus-saved-mark)
2689                 (t gnus-unread-mark)))
2690          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2691          (gnus-tmp-name
2692           (cond
2693            ((string-match "<[^>]+> *$" gnus-tmp-from)
2694             (let ((beg (match-beginning 0)))
2695               (or (and (string-match "^\".+\"" gnus-tmp-from)
2696                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2697                   (substring gnus-tmp-from 0 beg))))
2698            ((string-match "(.+)" gnus-tmp-from)
2699             (substring gnus-tmp-from
2700                        (1+ (match-beginning 0)) (1- (match-end 0))))
2701            (t gnus-tmp-from)))
2702          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2703          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2704          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2705          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2706          (buffer-read-only nil))
2707     (when (string= gnus-tmp-name "")
2708       (setq gnus-tmp-name gnus-tmp-from))
2709     (unless (numberp gnus-tmp-lines)
2710       (setq gnus-tmp-lines 0))
2711     (gnus-put-text-property
2712      (point)
2713      (progn (eval gnus-summary-line-format-spec) (point))
2714      'gnus-number gnus-tmp-number)
2715     (when (gnus-visual-p 'summary-highlight 'highlight)
2716       (forward-line -1)
2717       (gnus-run-hooks 'gnus-summary-update-hook)
2718       (forward-line 1))))
2719
2720 (defun gnus-summary-update-line (&optional dont-update)
2721   "Update summary line after change."
2722   (when (and gnus-summary-default-score
2723              (not gnus-summary-inhibit-highlight))
2724     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2725            (article (gnus-summary-article-number))
2726            (score (gnus-summary-article-score article)))
2727       (unless dont-update
2728         (if (and gnus-summary-mark-below
2729                  (< (gnus-summary-article-score)
2730                     gnus-summary-mark-below))
2731             ;; This article has a low score, so we mark it as read.
2732             (when (memq article gnus-newsgroup-unreads)
2733               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2734           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2735             ;; This article was previously marked as read on account
2736             ;; of a low score, but now it has risen, so we mark it as
2737             ;; unread.
2738             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2739         (gnus-summary-update-mark
2740          (if (or (null gnus-summary-default-score)
2741                  (<= (abs (- score gnus-summary-default-score))
2742                      gnus-summary-zcore-fuzz))
2743              ?  ;Whitespace
2744            (if (< score gnus-summary-default-score)
2745                gnus-score-below-mark gnus-score-over-mark))
2746          'score))
2747       ;; Do visual highlighting.
2748       (when (gnus-visual-p 'summary-highlight 'highlight)
2749         (gnus-run-hooks 'gnus-summary-update-hook)))))
2750
2751 (defvar gnus-tmp-new-adopts nil)
2752
2753 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2754   "Return the number of articles in THREAD.
2755 This may be 0 in some cases -- if none of the articles in
2756 the thread are to be displayed."
2757   (let* ((number
2758           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2759           (cond
2760            ((not (listp thread))
2761             1)
2762            ((and (consp thread) (cdr thread))
2763             (apply
2764              '+ 1 (mapcar
2765                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2766            ((null thread)
2767             1)
2768            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2769             1)
2770            (t 0))))
2771     (when (and level (zerop level) gnus-tmp-new-adopts)
2772       (incf number
2773             (apply '+ (mapcar
2774                        'gnus-summary-number-of-articles-in-thread
2775                        gnus-tmp-new-adopts))))
2776     (if char
2777         (if (> number 1) gnus-not-empty-thread-mark
2778           gnus-empty-thread-mark)
2779       number)))
2780
2781 (defun gnus-summary-set-local-parameters (group)
2782   "Go through the local params of GROUP and set all variable specs in that list."
2783   (let ((params (gnus-group-find-parameter group))
2784         elem)
2785     (while params
2786       (setq elem (car params)
2787             params (cdr params))
2788       (and (consp elem)                 ; Has to be a cons.
2789            (consp (cdr elem))           ; The cdr has to be a list.
2790            (symbolp (car elem))         ; Has to be a symbol in there.
2791            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2792            (ignore-errors               ; So we set it.
2793              (make-local-variable (car elem))
2794              (set (car elem) (eval (nth 1 elem))))))))
2795
2796 (defun gnus-summary-read-group (group &optional show-all no-article
2797                                       kill-buffer no-display backward
2798                                       select-articles)
2799   "Start reading news in newsgroup GROUP.
2800 If SHOW-ALL is non-nil, already read articles are also listed.
2801 If NO-ARTICLE is non-nil, no article is selected initially.
2802 If NO-DISPLAY, don't generate a summary buffer."
2803   (let (result)
2804     (while (and group
2805                 (null (setq result
2806                             (let ((gnus-auto-select-next nil))
2807                               (or (gnus-summary-read-group-1
2808                                    group show-all no-article
2809                                    kill-buffer no-display
2810                                    select-articles)
2811                                   (setq show-all nil
2812                                         select-articles nil)))))
2813                 (eq gnus-auto-select-next 'quietly))
2814       (set-buffer gnus-group-buffer)
2815       ;; The entry function called above goes to the next
2816       ;; group automatically, so we go two groups back
2817       ;; if we are searching for the previous group.
2818       (when backward
2819         (gnus-group-prev-unread-group 2))
2820       (if (not (equal group (gnus-group-group-name)))
2821           (setq group (gnus-group-group-name))
2822         (setq group nil)))
2823     result))
2824
2825 (defun gnus-summary-read-group-1 (group show-all no-article
2826                                         kill-buffer no-display
2827                                         &optional select-articles)
2828   ;; Killed foreign groups can't be entered.
2829   (when (and (not (gnus-group-native-p group))
2830              (not (gnus-gethash group gnus-newsrc-hashtb)))
2831     (error "Dead non-native groups can't be entered"))
2832   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2833   (let* ((new-group (gnus-summary-setup-buffer group))
2834          (quit-config (gnus-group-quit-config group))
2835          (did-select (and new-group (gnus-select-newsgroup
2836                                      group show-all select-articles))))
2837     (cond
2838      ;; This summary buffer exists already, so we just select it.
2839      ((not new-group)
2840       (gnus-set-global-variables)
2841       (when kill-buffer
2842         (gnus-kill-or-deaden-summary kill-buffer))
2843       (gnus-configure-windows 'summary 'force)
2844       (gnus-set-mode-line 'summary)
2845       (gnus-summary-position-point)
2846       (message "")
2847       t)
2848      ;; We couldn't select this group.
2849      ((null did-select)
2850       (when (and (eq major-mode 'gnus-summary-mode)
2851                  (not (equal (current-buffer) kill-buffer)))
2852         (kill-buffer (current-buffer))
2853         (if (not quit-config)
2854             (progn
2855               ;; Update the info -- marks might need to be removed,
2856               ;; for instance.
2857               (gnus-summary-update-info)
2858               (set-buffer gnus-group-buffer)
2859               (gnus-group-jump-to-group group)
2860               (gnus-group-next-unread-group 1))
2861           (gnus-handle-ephemeral-exit quit-config)))
2862       (gnus-message 3 "Can't select group")
2863       nil)
2864      ;; The user did a `C-g' while prompting for number of articles,
2865      ;; so we exit this group.
2866      ((eq did-select 'quit)
2867       (and (eq major-mode 'gnus-summary-mode)
2868            (not (equal (current-buffer) kill-buffer))
2869            (kill-buffer (current-buffer)))
2870       (when kill-buffer
2871         (gnus-kill-or-deaden-summary kill-buffer))
2872       (if (not quit-config)
2873           (progn
2874             (set-buffer gnus-group-buffer)
2875             (gnus-group-jump-to-group group)
2876             (gnus-group-next-unread-group 1)
2877             (gnus-configure-windows 'group 'force))
2878         (gnus-handle-ephemeral-exit quit-config))
2879       ;; Finally signal the quit.
2880       (signal 'quit nil))
2881      ;; The group was successfully selected.
2882      (t
2883       (gnus-set-global-variables)
2884       ;; Save the active value in effect when the group was entered.
2885       (setq gnus-newsgroup-active
2886             (gnus-copy-sequence
2887              (gnus-active gnus-newsgroup-name)))
2888       ;; You can change the summary buffer in some way with this hook.
2889       (gnus-run-hooks 'gnus-select-group-hook)
2890       ;; Set any local variables in the group parameters.
2891       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2892       (gnus-update-format-specifications
2893        nil 'summary 'summary-mode 'summary-dummy)
2894       (gnus-update-summary-mark-positions)
2895       ;; Do score processing.
2896       (when gnus-use-scoring
2897         (gnus-possibly-score-headers))
2898       ;; Check whether to fill in the gaps in the threads.
2899       (when gnus-build-sparse-threads
2900         (gnus-build-sparse-threads))
2901       ;; Find the initial limit.
2902       (if gnus-show-threads
2903           (if show-all
2904               (let ((gnus-newsgroup-dormant nil))
2905                 (gnus-summary-initial-limit show-all))
2906             (gnus-summary-initial-limit show-all))
2907         ;; When untreaded, all articles are always shown.
2908         (setq gnus-newsgroup-limit
2909               (mapcar
2910                (lambda (header) (mail-header-number header))
2911                gnus-newsgroup-headers)))
2912       ;; Generate the summary buffer.
2913       (unless no-display
2914         (gnus-summary-prepare))
2915       (when gnus-use-trees
2916         (gnus-tree-open group)
2917         (setq gnus-summary-highlight-line-function
2918               'gnus-tree-highlight-article))
2919       ;; If the summary buffer is empty, but there are some low-scored
2920       ;; articles or some excluded dormants, we include these in the
2921       ;; buffer.
2922       (when (and (zerop (buffer-size))
2923                  (not no-display))
2924         (cond (gnus-newsgroup-dormant
2925                (gnus-summary-limit-include-dormant))
2926               ((and gnus-newsgroup-scored show-all)
2927                (gnus-summary-limit-include-expunged t))))
2928       ;; Function `gnus-apply-kill-file' must be called in this hook.
2929       (gnus-run-hooks 'gnus-apply-kill-hook)
2930       (if (and (zerop (buffer-size))
2931                (not no-display))
2932           (progn
2933             ;; This newsgroup is empty.
2934             (gnus-summary-catchup-and-exit nil t)
2935             (gnus-message 6 "No unread news")
2936             (when kill-buffer
2937               (gnus-kill-or-deaden-summary kill-buffer))
2938             ;; Return nil from this function.
2939             nil)
2940         ;; Hide conversation thread subtrees.  We cannot do this in
2941         ;; gnus-summary-prepare-hook since kill processing may not
2942         ;; work with hidden articles.
2943         (and gnus-show-threads
2944              gnus-thread-hide-subtree
2945              (gnus-summary-hide-all-threads))
2946         (when kill-buffer
2947           (gnus-kill-or-deaden-summary kill-buffer))
2948         ;; Show first unread article if requested.
2949         (if (and (not no-article)
2950                  (not no-display)
2951                  gnus-newsgroup-unreads
2952                  gnus-auto-select-first)
2953             (progn
2954               (gnus-configure-windows 'summary)
2955               (cond
2956                ((eq gnus-auto-select-first 'best)
2957                 (gnus-summary-best-unread-article))
2958                ((eq gnus-auto-select-first t)
2959                 (gnus-summary-first-unread-article))
2960                ((gnus-functionp gnus-auto-select-first)
2961                 (funcall gnus-auto-select-first))))
2962           ;; Don't select any articles, just move point to the first
2963           ;; article in the group.
2964           (goto-char (point-min))
2965           (gnus-summary-position-point)
2966           (gnus-configure-windows 'summary 'force)
2967           (gnus-set-mode-line 'summary))
2968         (when (get-buffer-window gnus-group-buffer t)
2969           ;; Gotta use windows, because recenter does weird stuff if
2970           ;; the current buffer ain't the displayed window.
2971           (let ((owin (selected-window)))
2972             (select-window (get-buffer-window gnus-group-buffer t))
2973             (when (gnus-group-goto-group group)
2974               (recenter))
2975             (select-window owin)))
2976         ;; Mark this buffer as "prepared".
2977         (setq gnus-newsgroup-prepared t)
2978         (gnus-run-hooks 'gnus-summary-prepared-hook)
2979         t)))))
2980
2981 (defun gnus-summary-prepare ()
2982   "Generate the summary buffer."
2983   (interactive)
2984   (let ((buffer-read-only nil))
2985     (erase-buffer)
2986     (setq gnus-newsgroup-data nil
2987           gnus-newsgroup-data-reverse nil)
2988     (gnus-run-hooks 'gnus-summary-generate-hook)
2989     ;; Generate the buffer, either with threads or without.
2990     (when gnus-newsgroup-headers
2991       (gnus-summary-prepare-threads
2992        (if gnus-show-threads
2993            (gnus-sort-gathered-threads
2994             (funcall gnus-summary-thread-gathering-function
2995                      (gnus-sort-threads
2996                       (gnus-cut-threads (gnus-make-threads)))))
2997          ;; Unthreaded display.
2998          (gnus-sort-articles gnus-newsgroup-headers))))
2999     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3000     ;; Call hooks for modifying summary buffer.
3001     (goto-char (point-min))
3002     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3003
3004 (defsubst gnus-general-simplify-subject (subject)
3005   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3006   (setq subject
3007         (cond
3008          ;; Truncate the subject.
3009          (gnus-simplify-subject-functions
3010           (gnus-map-function gnus-simplify-subject-functions subject))
3011          ((numberp gnus-summary-gather-subject-limit)
3012           (setq subject (gnus-simplify-subject-re subject))
3013           (if (> (length subject) gnus-summary-gather-subject-limit)
3014               (substring subject 0 gnus-summary-gather-subject-limit)
3015             subject))
3016          ;; Fuzzily simplify it.
3017          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3018           (gnus-simplify-subject-fuzzy subject))
3019          ;; Just remove the leading "Re:".
3020          (t
3021           (gnus-simplify-subject-re subject))))
3022
3023   (if (and gnus-summary-gather-exclude-subject
3024            (string-match gnus-summary-gather-exclude-subject subject))
3025       nil                               ; This article shouldn't be gathered
3026     subject))
3027
3028 (defun gnus-summary-simplify-subject-query ()
3029   "Query where the respool algorithm would put this article."
3030   (interactive)
3031   (gnus-summary-select-article)
3032   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3033
3034 (defun gnus-gather-threads-by-subject (threads)
3035   "Gather threads by looking at Subject headers."
3036   (if (not gnus-summary-make-false-root)
3037       threads
3038     (let ((hashtb (gnus-make-hashtable 1024))
3039           (prev threads)
3040           (result threads)
3041           subject hthread whole-subject)
3042       (while threads
3043         (setq subject (gnus-general-simplify-subject
3044                        (setq whole-subject (mail-header-subject
3045                                             (caar threads)))))
3046         (when subject
3047           (if (setq hthread (gnus-gethash subject hashtb))
3048               (progn
3049                 ;; We enter a dummy root into the thread, if we
3050                 ;; haven't done that already.
3051                 (unless (stringp (caar hthread))
3052                   (setcar hthread (list whole-subject (car hthread))))
3053                 ;; We add this new gathered thread to this gathered
3054                 ;; thread.
3055                 (setcdr (car hthread)
3056                         (nconc (cdar hthread) (list (car threads))))
3057                 ;; Remove it from the list of threads.
3058                 (setcdr prev (cdr threads))
3059                 (setq threads prev))
3060             ;; Enter this thread into the hash table.
3061             (gnus-sethash subject threads hashtb)))
3062         (setq prev threads)
3063         (setq threads (cdr threads)))
3064       result)))
3065
3066 (defun gnus-gather-threads-by-references (threads)
3067   "Gather threads by looking at References headers."
3068   (let ((idhashtb (gnus-make-hashtable 1024))
3069         (thhashtb (gnus-make-hashtable 1024))
3070         (prev threads)
3071         (result threads)
3072         ids references id gthread gid entered ref)
3073     (while threads
3074       (when (setq references (mail-header-references (caar threads)))
3075         (setq id (mail-header-id (caar threads))
3076               ids (gnus-split-references references)
3077               entered nil)
3078         (while (setq ref (pop ids))
3079           (setq ids (delete ref ids))
3080           (if (not (setq gid (gnus-gethash ref idhashtb)))
3081               (progn
3082                 (gnus-sethash ref id idhashtb)
3083                 (gnus-sethash id threads thhashtb))
3084             (setq gthread (gnus-gethash gid thhashtb))
3085             (unless entered
3086               ;; We enter a dummy root into the thread, if we
3087               ;; haven't done that already.
3088               (unless (stringp (caar gthread))
3089                 (setcar gthread (list (mail-header-subject (caar gthread))
3090                                       (car gthread))))
3091               ;; We add this new gathered thread to this gathered
3092               ;; thread.
3093               (setcdr (car gthread)
3094                       (nconc (cdar gthread) (list (car threads)))))
3095             ;; Add it into the thread hash table.
3096             (gnus-sethash id gthread thhashtb)
3097             (setq entered t)
3098             ;; Remove it from the list of threads.
3099             (setcdr prev (cdr threads))
3100             (setq threads prev))))
3101       (setq prev threads)
3102       (setq threads (cdr threads)))
3103     result))
3104
3105 (defun gnus-sort-gathered-threads (threads)
3106   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3107   (let ((result threads))
3108     (while threads
3109       (when (stringp (caar threads))
3110         (setcdr (car threads)
3111                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3112       (setq threads (cdr threads)))
3113     result))
3114
3115 (defun gnus-thread-loop-p (root thread)
3116   "Say whether ROOT is in THREAD."
3117   (let ((stack (list thread))
3118         (infloop 0)
3119         th)
3120     (while (setq thread (pop stack))
3121       (setq th (cdr thread))
3122       (while (and th
3123                   (not (eq (caar th) root)))
3124         (pop th))
3125       (if th
3126           ;; We have found a loop.
3127           (let (ref-dep)
3128             (setcdr thread (delq (car th) (cdr thread)))
3129             (if (boundp (setq ref-dep (intern "none"
3130                                               gnus-newsgroup-dependencies)))
3131                 (setcdr (symbol-value ref-dep)
3132                         (nconc (cdr (symbol-value ref-dep))
3133                                (list (car th))))
3134               (set ref-dep (list nil (car th))))
3135             (setq infloop 1
3136                   stack nil))
3137         ;; Push all the subthreads onto the stack.
3138         (push (cdr thread) stack)))
3139     infloop))
3140
3141 (defun gnus-make-threads ()
3142   "Go through the dependency hashtb and find the roots.  Return all threads."
3143   (let (threads)
3144     (while (catch 'infloop
3145              (mapatoms
3146               (lambda (refs)
3147                 ;; Deal with self-referencing References loops.
3148                 (when (and (car (symbol-value refs))
3149                            (not (zerop
3150                                  (apply
3151                                   '+
3152                                   (mapcar
3153                                    (lambda (thread)
3154                                      (gnus-thread-loop-p
3155                                       (car (symbol-value refs)) thread))
3156                                    (cdr (symbol-value refs)))))))
3157                   (setq threads nil)
3158                   (throw 'infloop t))
3159                 (unless (car (symbol-value refs))
3160                   ;; These threads do not refer back to any other articles,
3161                   ;; so they're roots.
3162                   (setq threads (append (cdr (symbol-value refs)) threads))))
3163               gnus-newsgroup-dependencies)))
3164     threads))
3165
3166 ;; Build the thread tree.
3167 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3168   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3169
3170 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3171 if it was already present.
3172
3173 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3174 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3175 Message-IDs will be renamed be renamed to a unique Message-ID before
3176 being entered.
3177
3178 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3179   (let* ((id (mail-header-id header))
3180          (id-dep (and id (intern id dependencies)))
3181          ref ref-dep ref-header)
3182     ;; Enter this `header' in the `dependencies' table.
3183     (cond
3184      ((not id-dep)
3185       (setq header nil))
3186      ;; The first two cases do the normal part: enter a new `header'
3187      ;; in the `dependencies' table.
3188      ((not (boundp id-dep))
3189       (set id-dep (list header)))
3190      ((null (car (symbol-value id-dep)))
3191       (setcar (symbol-value id-dep) header))
3192
3193      ;; From here the `header' was already present in the
3194      ;; `dependencies' table.
3195      (force-new
3196       ;; Overrides an existing entry;
3197       ;; just set the header part of the entry.
3198       (setcar (symbol-value id-dep) header))
3199
3200      ;; Renames the existing `header' to a unique Message-ID.
3201      ((not gnus-summary-ignore-duplicates)
3202       ;; An article with this Message-ID has already been seen.
3203       ;; We rename the Message-ID.
3204       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3205            (list header))
3206       (mail-header-set-id header id))
3207
3208      ;; The last case ignores an existing entry, except it adds any
3209      ;; additional Xrefs (in case the two articles came from different
3210      ;; servers.
3211      ;; Also sets `header' to `nil' meaning that the `dependencies'
3212      ;; table was *not* modified.
3213      (t
3214       (mail-header-set-xref
3215        (car (symbol-value id-dep))
3216        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3217                    "")
3218                (or (mail-header-xref header) "")))
3219       (setq header nil)))
3220
3221     (when header
3222       ;; First check if that we are not creating a References loop.
3223       (setq ref (gnus-parent-id (mail-header-references header)))
3224       (while (and ref
3225                   (setq ref-dep (intern-soft ref dependencies))
3226                   (boundp ref-dep)
3227                   (setq ref-header (car (symbol-value ref-dep))))
3228         (if (string= id ref)
3229             ;; Yuk!  This is a reference loop.  Make the article be a
3230             ;; root article.
3231             (progn
3232               (mail-header-set-references (car (symbol-value id-dep)) "none")
3233               (setq ref nil))
3234           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3235       (setq ref (gnus-parent-id (mail-header-references header)))
3236       (setq ref-dep (intern (or ref "none") dependencies))
3237       (if (boundp ref-dep)
3238           (setcdr (symbol-value ref-dep)
3239                   (nconc (cdr (symbol-value ref-dep))
3240                          (list (symbol-value id-dep))))
3241         (set ref-dep (list nil (symbol-value id-dep)))))
3242     header))
3243
3244 (defun gnus-build-sparse-threads ()
3245   (let ((headers gnus-newsgroup-headers)
3246         (mail-parse-charset gnus-newsgroup-charset)
3247         (gnus-summary-ignore-duplicates t)
3248         header references generation relations
3249         subject child end new-child date)
3250     ;; First we create an alist of generations/relations, where
3251     ;; generations is how much we trust the relation, and the relation
3252     ;; is parent/child.
3253     (gnus-message 7 "Making sparse threads...")
3254     (save-excursion
3255       (nnheader-set-temp-buffer " *gnus sparse threads*")
3256       (while (setq header (pop headers))
3257         (when (and (setq references (mail-header-references header))
3258                    (not (string= references "")))
3259           (insert references)
3260           (setq child (mail-header-id header)
3261                 subject (mail-header-subject header)
3262                 date (mail-header-date header)
3263                 generation 0)
3264           (while (search-backward ">" nil t)
3265             (setq end (1+ (point)))
3266             (when (search-backward "<" nil t)
3267               (setq new-child (buffer-substring (point) end))
3268               (push (list (incf generation)
3269                           child (setq child new-child)
3270                           subject date)
3271                     relations)))
3272           (when child
3273             (push (list (1+ generation) child nil subject) relations))
3274           (erase-buffer)))
3275       (kill-buffer (current-buffer)))
3276     ;; Sort over trustworthiness.
3277     (mapcar
3278      (lambda (relation)
3279        (when (gnus-dependencies-add-header
3280               (make-full-mail-header
3281                gnus-reffed-article-number
3282                (nth 3 relation) "" (or (nth 4 relation) "")
3283                (nth 1 relation)
3284                (or (nth 2 relation) "") 0 0 "")
3285               gnus-newsgroup-dependencies nil)
3286          (push gnus-reffed-article-number gnus-newsgroup-limit)
3287          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3288          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3289                gnus-newsgroup-reads)
3290          (decf gnus-reffed-article-number)))
3291      (sort relations 'car-less-than-car))
3292     (gnus-message 7 "Making sparse threads...done")))
3293
3294 (defun gnus-build-old-threads ()
3295   ;; Look at all the articles that refer back to old articles, and
3296   ;; fetch the headers for the articles that aren't there.  This will
3297   ;; build complete threads - if the roots haven't been expired by the
3298   ;; server, that is.
3299   (let ((mail-parse-charset gnus-newsgroup-charset)
3300         id heads)
3301     (mapatoms
3302      (lambda (refs)
3303        (when (not (car (symbol-value refs)))
3304          (setq heads (cdr (symbol-value refs)))
3305          (while heads
3306            (if (memq (mail-header-number (caar heads))
3307                      gnus-newsgroup-dormant)
3308                (setq heads (cdr heads))
3309              (setq id (symbol-name refs))
3310              (while (and (setq id (gnus-build-get-header id))
3311                          (not (car (gnus-id-to-thread id)))))
3312              (setq heads nil)))))
3313      gnus-newsgroup-dependencies)))
3314
3315 ;; This function has to be called with point after the article number
3316 ;; on the beginning of the line.
3317 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3318   (let ((eol (gnus-point-at-eol))
3319         (buffer (current-buffer))
3320         header)
3321
3322     ;; overview: [num subject from date id refs chars lines misc]
3323     (unwind-protect
3324         (progn
3325           (narrow-to-region (point) eol)
3326           (unless (eobp)
3327             (forward-char))
3328
3329           (setq header
3330                 (make-full-mail-header
3331                  number                 ; number
3332                  (funcall gnus-decode-encoded-word-function
3333                           (nnheader-nov-field)) ; subject
3334                  (funcall gnus-decode-encoded-word-function
3335                           (nnheader-nov-field)) ; from
3336                  (nnheader-nov-field)   ; date
3337                  (nnheader-nov-read-message-id) ; id
3338                  (nnheader-nov-field)   ; refs
3339                  (nnheader-nov-read-integer) ; chars
3340                  (nnheader-nov-read-integer) ; lines
3341                  (unless (eobp)
3342                    (if (looking-at "Xref: ")
3343                        (goto-char (match-end 0)))
3344                    (nnheader-nov-field)) ; Xref
3345                  (nnheader-nov-parse-extra)))) ; extra
3346
3347       (widen))
3348
3349     (when gnus-alter-header-function
3350       (funcall gnus-alter-header-function header))
3351     (gnus-dependencies-add-header header dependencies force-new)))
3352
3353 (defun gnus-build-get-header (id)
3354   "Look through the buffer of NOV lines and find the header to ID.
3355 Enter this line into the dependencies hash table, and return
3356 the id of the parent article (if any)."
3357   (let ((deps gnus-newsgroup-dependencies)
3358         found header)
3359     (prog1
3360         (save-excursion
3361           (set-buffer nntp-server-buffer)
3362           (let ((case-fold-search nil))
3363             (goto-char (point-min))
3364             (while (and (not found)
3365                         (search-forward id nil t))
3366               (beginning-of-line)
3367               (setq found (looking-at
3368                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3369                                    (regexp-quote id))))
3370               (or found (beginning-of-line 2)))
3371             (when found
3372               (beginning-of-line)
3373               (and
3374                (setq header (gnus-nov-parse-line
3375                              (read (current-buffer)) deps))
3376                (gnus-parent-id (mail-header-references header))))))
3377       (when header
3378         (let ((number (mail-header-number header)))
3379           (push number gnus-newsgroup-limit)
3380           (push header gnus-newsgroup-headers)
3381           (if (memq number gnus-newsgroup-unselected)
3382               (progn
3383                 (push number gnus-newsgroup-unreads)
3384                 (setq gnus-newsgroup-unselected
3385                       (delq number gnus-newsgroup-unselected)))
3386             (push number gnus-newsgroup-ancient)))))))
3387
3388 (defun gnus-build-all-threads ()
3389   "Read all the headers."
3390   (let ((gnus-summary-ignore-duplicates t)
3391         (mail-parse-charset gnus-newsgroup-charset)
3392         (dependencies gnus-newsgroup-dependencies)
3393         header article)
3394     (save-excursion
3395       (set-buffer nntp-server-buffer)
3396       (let ((case-fold-search nil))
3397         (goto-char (point-min))
3398         (while (not (eobp))
3399           (ignore-errors
3400             (setq article (read (current-buffer))
3401                   header (gnus-nov-parse-line article dependencies)))
3402           (when header
3403             (save-excursion
3404               (set-buffer gnus-summary-buffer)
3405               (push header gnus-newsgroup-headers)
3406               (if (memq (setq article (mail-header-number header))
3407                         gnus-newsgroup-unselected)
3408                   (progn
3409                     (push article gnus-newsgroup-unreads)
3410                     (setq gnus-newsgroup-unselected
3411                           (delq article gnus-newsgroup-unselected)))
3412                 (push article gnus-newsgroup-ancient)))
3413             (forward-line 1)))))))
3414
3415 (defun gnus-summary-update-article-line (article header)
3416   "Update the line for ARTICLE using HEADERS."
3417   (let* ((id (mail-header-id header))
3418          (thread (gnus-id-to-thread id)))
3419     (unless thread
3420       (error "Article in no thread"))
3421     ;; Update the thread.
3422     (setcar thread header)
3423     (gnus-summary-goto-subject article)
3424     (let* ((datal (gnus-data-find-list article))
3425            (data (car datal))
3426            (length (when (cdr datal)
3427                      (- (gnus-data-pos data)
3428                         (gnus-data-pos (cadr datal)))))
3429            (buffer-read-only nil)
3430            (level (gnus-summary-thread-level)))
3431       (gnus-delete-line)
3432       (gnus-summary-insert-line
3433        header level nil (gnus-article-mark article)
3434        (memq article gnus-newsgroup-replied)
3435        (memq article gnus-newsgroup-expirable)
3436        ;; Only insert the Subject string when it's different
3437        ;; from the previous Subject string.
3438        (if (and
3439             gnus-show-threads
3440             (gnus-subject-equal
3441              (condition-case ()
3442                  (mail-header-subject
3443                   (gnus-data-header
3444                    (cadr
3445                     (gnus-data-find-list
3446                      article
3447                      (gnus-data-list t)))))
3448                ;; Error on the side of excessive subjects.
3449                (error ""))
3450              (mail-header-subject header)))
3451            ""
3452          (mail-header-subject header))
3453        nil (cdr (assq article gnus-newsgroup-scored))
3454        (memq article gnus-newsgroup-processable))
3455       (when length
3456         (gnus-data-update-list
3457          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3458
3459 (defun gnus-summary-update-article (article &optional iheader)
3460   "Update ARTICLE in the summary buffer."
3461   (set-buffer gnus-summary-buffer)
3462   (let* ((header (gnus-summary-article-header article))
3463          (id (mail-header-id header))
3464          (data (gnus-data-find article))
3465          (thread (gnus-id-to-thread id))
3466          (references (mail-header-references header))
3467          (parent
3468           (gnus-id-to-thread
3469            (or (gnus-parent-id
3470                 (when (and references
3471                            (not (equal "" references)))
3472                   references))
3473                "none")))
3474          (buffer-read-only nil)
3475          (old (car thread)))
3476     (when thread
3477       (unless iheader
3478         (setcar thread nil)
3479         (when parent
3480           (delq thread parent)))
3481       (if (gnus-summary-insert-subject id header)
3482           ;; Set the (possibly) new article number in the data structure.
3483           (gnus-data-set-number data (gnus-id-to-article id))
3484         (setcar thread old)
3485         nil))))
3486
3487 (defun gnus-rebuild-thread (id &optional line)
3488   "Rebuild the thread containing ID.
3489 If LINE, insert the rebuilt thread starting on line LINE."
3490   (let ((buffer-read-only nil)
3491         old-pos current thread data)
3492     (if (not gnus-show-threads)
3493         (setq thread (list (car (gnus-id-to-thread id))))
3494       ;; Get the thread this article is part of.
3495       (setq thread (gnus-remove-thread id)))
3496     (setq old-pos (gnus-point-at-bol))
3497     (setq current (save-excursion
3498                     (and (zerop (forward-line -1))
3499                          (gnus-summary-article-number))))
3500     ;; If this is a gathered thread, we have to go some re-gathering.
3501     (when (stringp (car thread))
3502       (let ((subject (car thread))
3503             roots thr)
3504         (setq thread (cdr thread))
3505         (while thread
3506           (unless (memq (setq thr (gnus-id-to-thread
3507                                    (gnus-root-id
3508                                     (mail-header-id (caar thread)))))
3509                         roots)
3510             (push thr roots))
3511           (setq thread (cdr thread)))
3512         ;; We now have all (unique) roots.
3513         (if (= (length roots) 1)
3514             ;; All the loose roots are now one solid root.
3515             (setq thread (car roots))
3516           (setq thread (cons subject (gnus-sort-threads roots))))))
3517     (let (threads)
3518       ;; We then insert this thread into the summary buffer.
3519       (when line
3520         (goto-char (point-min))
3521         (forward-line (1- line)))
3522       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3523         (if gnus-show-threads
3524             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3525           (gnus-summary-prepare-unthreaded thread))
3526         (setq data (nreverse gnus-newsgroup-data))
3527         (setq threads gnus-newsgroup-threads))
3528       ;; We splice the new data into the data structure.
3529       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3530       ;;!!! then we want to insert at the beginning of the buffer.
3531       ;;!!! That happens to be true with Gnus now, but that may
3532       ;;!!! change in the future.  Perhaps.
3533       (gnus-data-enter-list
3534        (if line nil current) data (- (point) old-pos))
3535       (setq gnus-newsgroup-threads
3536             (nconc threads gnus-newsgroup-threads))
3537       (gnus-data-compute-positions))))
3538
3539 (defun gnus-number-to-header (number)
3540   "Return the header for article NUMBER."
3541   (let ((headers gnus-newsgroup-headers))
3542     (while (and headers
3543                 (not (= number (mail-header-number (car headers)))))
3544       (pop headers))
3545     (when headers
3546       (car headers))))
3547
3548 (defun gnus-parent-headers (in-headers &optional generation)
3549   "Return the headers of the GENERATIONeth parent of HEADERS."
3550   (unless generation
3551     (setq generation 1))
3552   (let ((parent t)
3553         (headers in-headers)
3554         references)
3555     (while (and parent
3556                 (not (zerop generation))
3557                 (setq references (mail-header-references headers)))
3558       (setq headers (if (and references
3559                              (setq parent (gnus-parent-id references)))
3560                         (car (gnus-id-to-thread parent))
3561                       nil))
3562       (decf generation))
3563     (and (not (eq headers in-headers))
3564          headers)))
3565
3566 (defun gnus-id-to-thread (id)
3567   "Return the (sub-)thread where ID appears."
3568   (gnus-gethash id gnus-newsgroup-dependencies))
3569
3570 (defun gnus-id-to-article (id)
3571   "Return the article number of ID."
3572   (let ((thread (gnus-id-to-thread id)))
3573     (when (and thread
3574                (car thread))
3575       (mail-header-number (car thread)))))
3576
3577 (defun gnus-id-to-header (id)
3578   "Return the article headers of ID."
3579   (car (gnus-id-to-thread id)))
3580
3581 (defun gnus-article-displayed-root-p (article)
3582   "Say whether ARTICLE is a root(ish) article."
3583   (let ((level (gnus-summary-thread-level article))
3584         (refs (mail-header-references  (gnus-summary-article-header article)))
3585         particle)
3586     (cond
3587      ((null level) nil)
3588      ((zerop level) t)
3589      ((null refs) t)
3590      ((null (gnus-parent-id refs)) t)
3591      ((and (= 1 level)
3592            (null (setq particle (gnus-id-to-article
3593                                  (gnus-parent-id refs))))
3594            (null (gnus-summary-thread-level particle)))))))
3595
3596 (defun gnus-root-id (id)
3597   "Return the id of the root of the thread where ID appears."
3598   (let (last-id prev)
3599     (while (and id (setq prev (car (gnus-id-to-thread id))))
3600       (setq last-id id
3601             id (gnus-parent-id (mail-header-references prev))))
3602     last-id))
3603
3604 (defun gnus-articles-in-thread (thread)
3605   "Return the list of articles in THREAD."
3606   (cons (mail-header-number (car thread))
3607         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3608
3609 (defun gnus-remove-thread (id &optional dont-remove)
3610   "Remove the thread that has ID in it."
3611   (let (headers thread last-id)
3612     ;; First go up in this thread until we find the root.
3613     (setq last-id (gnus-root-id id)
3614           headers (message-flatten-list (gnus-id-to-thread last-id)))
3615     ;; We have now found the real root of this thread.  It might have
3616     ;; been gathered into some loose thread, so we have to search
3617     ;; through the threads to find the thread we wanted.
3618     (let ((threads gnus-newsgroup-threads)
3619           sub)
3620       (while threads
3621         (setq sub (car threads))
3622         (if (stringp (car sub))
3623             ;; This is a gathered thread, so we look at the roots
3624             ;; below it to find whether this article is in this
3625             ;; gathered root.
3626             (progn
3627               (setq sub (cdr sub))
3628               (while sub
3629                 (when (member (caar sub) headers)
3630                   (setq thread (car threads)
3631                         threads nil
3632                         sub nil))
3633                 (setq sub (cdr sub))))
3634           ;; It's an ordinary thread, so we check it.
3635           (when (eq (car sub) (car headers))
3636             (setq thread sub
3637                   threads nil)))
3638         (setq threads (cdr threads)))
3639       ;; If this article is in no thread, then it's a root.
3640       (if thread
3641           (unless dont-remove
3642             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3643         (setq thread (gnus-id-to-thread last-id)))
3644       (when thread
3645         (prog1
3646             thread                      ; We return this thread.
3647           (unless dont-remove
3648             (if (stringp (car thread))
3649                 (progn
3650                   ;; If we use dummy roots, then we have to remove the
3651                   ;; dummy root as well.
3652                   (when (eq gnus-summary-make-false-root 'dummy)
3653                     ;; We go to the dummy root by going to
3654                     ;; the first sub-"thread", and then one line up.
3655                     (gnus-summary-goto-article
3656                      (mail-header-number (caadr thread)))
3657                     (forward-line -1)
3658                     (gnus-delete-line)
3659                     (gnus-data-compute-positions))
3660                   (setq thread (cdr thread))
3661                   (while thread
3662                     (gnus-remove-thread-1 (car thread))
3663                     (setq thread (cdr thread))))
3664               (gnus-remove-thread-1 thread))))))))
3665
3666 (defun gnus-remove-thread-1 (thread)
3667   "Remove the thread THREAD recursively."
3668   (let ((number (mail-header-number (pop thread)))
3669         d)
3670     (setq thread (reverse thread))
3671     (while thread
3672       (gnus-remove-thread-1 (pop thread)))
3673     (when (setq d (gnus-data-find number))
3674       (goto-char (gnus-data-pos d))
3675       (gnus-summary-show-thread)
3676       (gnus-data-remove
3677        number
3678        (- (gnus-point-at-bol)
3679           (prog1
3680               (1+ (gnus-point-at-eol))
3681             (gnus-delete-line)))))))
3682
3683 (defun gnus-sort-threads-1 (threads func)
3684   (sort (mapcar (lambda (thread)
3685                   (cons (car thread)
3686                         (and (cdr thread)
3687                              (gnus-sort-threads-1 (cdr thread) func))))
3688                 threads) func))
3689
3690 (defun gnus-sort-threads (threads)
3691   "Sort THREADS."
3692   (if (not gnus-thread-sort-functions)
3693       threads
3694     (gnus-message 8 "Sorting threads...")
3695     (prog1
3696         (gnus-sort-threads-1 
3697          threads 
3698          (gnus-make-sort-function gnus-thread-sort-functions))
3699       (gnus-message 8 "Sorting threads...done"))))
3700
3701 (defun gnus-sort-articles (articles)
3702   "Sort ARTICLES."
3703   (when gnus-article-sort-functions
3704     (gnus-message 7 "Sorting articles...")
3705     (prog1
3706         (setq gnus-newsgroup-headers
3707               (sort articles (gnus-make-sort-function
3708                               gnus-article-sort-functions)))
3709       (gnus-message 7 "Sorting articles...done"))))
3710
3711 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3712 (defmacro gnus-thread-header (thread)
3713   "Return header of first article in THREAD.
3714 Note that THREAD must never, ever be anything else than a variable -
3715 using some other form will lead to serious barfage."
3716   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3717   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3718   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3719         (vector thread) 2))
3720
3721 (defsubst gnus-article-sort-by-number (h1 h2)
3722   "Sort articles by article number."
3723   (< (mail-header-number h1)
3724      (mail-header-number h2)))
3725
3726 (defun gnus-thread-sort-by-number (h1 h2)
3727   "Sort threads by root article number."
3728   (gnus-article-sort-by-number
3729    (gnus-thread-header h1) (gnus-thread-header h2)))
3730
3731 (defsubst gnus-article-sort-by-lines (h1 h2)
3732   "Sort articles by article Lines header."
3733   (< (mail-header-lines h1)
3734      (mail-header-lines h2)))
3735
3736 (defun gnus-thread-sort-by-lines (h1 h2)
3737   "Sort threads by root article Lines header."
3738   (gnus-article-sort-by-lines
3739    (gnus-thread-header h1) (gnus-thread-header h2)))
3740
3741 (defsubst gnus-article-sort-by-chars (h1 h2)
3742   "Sort articles by octet length."
3743   (< (mail-header-chars h1)
3744      (mail-header-chars h2)))
3745
3746 (defun gnus-thread-sort-by-chars (h1 h2)
3747   "Sort threads by root article octet length."
3748   (gnus-article-sort-by-chars
3749    (gnus-thread-header h1) (gnus-thread-header h2)))
3750
3751 (defsubst gnus-article-sort-by-author (h1 h2)
3752   "Sort articles by root author."
3753   (string-lessp
3754    (let ((extract (funcall
3755                    gnus-extract-address-components
3756                    (mail-header-from h1))))
3757      (or (car extract) (cadr extract) ""))
3758    (let ((extract (funcall
3759                    gnus-extract-address-components
3760                    (mail-header-from h2))))
3761      (or (car extract) (cadr extract) ""))))
3762
3763 (defun gnus-thread-sort-by-author (h1 h2)
3764   "Sort threads by root author."
3765   (gnus-article-sort-by-author
3766    (gnus-thread-header h1)  (gnus-thread-header h2)))
3767
3768 (defsubst gnus-article-sort-by-subject (h1 h2)
3769   "Sort articles by root subject."
3770   (string-lessp
3771    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3772    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3773
3774 (defun gnus-thread-sort-by-subject (h1 h2)
3775   "Sort threads by root subject."
3776   (gnus-article-sort-by-subject
3777    (gnus-thread-header h1) (gnus-thread-header h2)))
3778
3779 (defsubst gnus-article-sort-by-date (h1 h2)
3780   "Sort articles by root article date."
3781   (time-less-p
3782    (gnus-date-get-time (mail-header-date h1))
3783    (gnus-date-get-time (mail-header-date h2))))
3784
3785 (defun gnus-thread-sort-by-date (h1 h2)
3786   "Sort threads by root article date."
3787   (gnus-article-sort-by-date
3788    (gnus-thread-header h1) (gnus-thread-header h2)))
3789
3790 (defsubst gnus-article-sort-by-score (h1 h2)
3791   "Sort articles by root article score.
3792 Unscored articles will be counted as having a score of zero."
3793   (> (or (cdr (assq (mail-header-number h1)
3794                     gnus-newsgroup-scored))
3795          gnus-summary-default-score 0)
3796      (or (cdr (assq (mail-header-number h2)
3797                     gnus-newsgroup-scored))
3798          gnus-summary-default-score 0)))
3799
3800 (defun gnus-thread-sort-by-score (h1 h2)
3801   "Sort threads by root article score."
3802   (gnus-article-sort-by-score
3803    (gnus-thread-header h1) (gnus-thread-header h2)))
3804
3805 (defun gnus-thread-sort-by-total-score (h1 h2)
3806   "Sort threads by the sum of all scores in the thread.
3807 Unscored articles will be counted as having a score of zero."
3808   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3809
3810 (defun gnus-thread-total-score (thread)
3811   ;; This function find the total score of THREAD.
3812   (cond ((null thread)
3813          0)
3814         ((consp thread)
3815          (if (stringp (car thread))
3816              (apply gnus-thread-score-function 0
3817                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3818            (gnus-thread-total-score-1 thread)))
3819         (t
3820          (gnus-thread-total-score-1 (list thread)))))
3821
3822 (defun gnus-thread-total-score-1 (root)
3823   ;; This function find the total score of the thread below ROOT.
3824   (setq root (car root))
3825   (apply gnus-thread-score-function
3826          (or (append
3827               (mapcar 'gnus-thread-total-score
3828                       (cdr (gnus-id-to-thread (mail-header-id root))))
3829               (when (> (mail-header-number root) 0)
3830                 (list (or (cdr (assq (mail-header-number root)
3831                                      gnus-newsgroup-scored))
3832                           gnus-summary-default-score 0))))
3833              (list gnus-summary-default-score)
3834              '(0))))
3835
3836 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3837 (defvar gnus-tmp-prev-subject nil)
3838 (defvar gnus-tmp-false-parent nil)
3839 (defvar gnus-tmp-root-expunged nil)
3840 (defvar gnus-tmp-dummy-line nil)
3841
3842 (defvar gnus-tmp-header)
3843 (defun gnus-extra-header (type &optional header)
3844   "Return the extra header of TYPE."
3845   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3846       ""))
3847
3848 (defun gnus-summary-prepare-threads (threads)
3849   "Prepare summary buffer from THREADS and indentation LEVEL.
3850 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3851 or a straight list of headers."
3852   (gnus-message 7 "Generating summary...")
3853
3854   (setq gnus-newsgroup-threads threads)
3855   (beginning-of-line)
3856
3857   (let ((gnus-tmp-level 0)
3858         (default-score (or gnus-summary-default-score 0))
3859         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3860         thread number subject stack state gnus-tmp-gathered beg-match
3861         new-roots gnus-tmp-new-adopts thread-end
3862         gnus-tmp-header gnus-tmp-unread
3863         gnus-tmp-replied gnus-tmp-subject-or-nil
3864         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3865         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3866         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3867
3868     (setq gnus-tmp-prev-subject nil)
3869
3870     (if (vectorp (car threads))
3871         ;; If this is a straight (sic) list of headers, then a
3872         ;; threaded summary display isn't required, so we just create
3873         ;; an unthreaded one.
3874         (gnus-summary-prepare-unthreaded threads)
3875
3876       ;; Do the threaded display.
3877
3878       (while (or threads stack gnus-tmp-new-adopts new-roots)
3879
3880         (if (and (= gnus-tmp-level 0)
3881                  (or (not stack)
3882                      (= (caar stack) 0))
3883                  (not gnus-tmp-false-parent)
3884                  (or gnus-tmp-new-adopts new-roots))
3885             (if gnus-tmp-new-adopts
3886                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3887                       thread (list (car gnus-tmp-new-adopts))
3888                       gnus-tmp-header (caar thread)
3889                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3890               (when new-roots
3891                 (setq thread (list (car new-roots))
3892                       gnus-tmp-header (caar thread)
3893                       new-roots (cdr new-roots))))
3894
3895           (if threads
3896               ;; If there are some threads, we do them before the
3897               ;; threads on the stack.
3898               (setq thread threads
3899                     gnus-tmp-header (caar thread))
3900             ;; There were no current threads, so we pop something off
3901             ;; the stack.
3902             (setq state (car stack)
3903                   gnus-tmp-level (car state)
3904                   thread (cdr state)
3905                   stack (cdr stack)
3906                   gnus-tmp-header (caar thread))))
3907
3908         (setq gnus-tmp-false-parent nil)
3909         (setq gnus-tmp-root-expunged nil)
3910         (setq thread-end nil)
3911
3912         (if (stringp gnus-tmp-header)
3913             ;; The header is a dummy root.
3914             (cond
3915              ((eq gnus-summary-make-false-root 'adopt)
3916               ;; We let the first article adopt the rest.
3917               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3918                                                (cddar thread)))
3919               (setq gnus-tmp-gathered
3920                     (nconc (mapcar
3921                             (lambda (h) (mail-header-number (car h)))
3922                             (cddar thread))
3923                            gnus-tmp-gathered))
3924               (setq thread (cons (list (caar thread)
3925                                        (cadar thread))
3926                                  (cdr thread)))
3927               (setq gnus-tmp-level -1
3928                     gnus-tmp-false-parent t))
3929              ((eq gnus-summary-make-false-root 'empty)
3930               ;; We print adopted articles with empty subject fields.
3931               (setq gnus-tmp-gathered
3932                     (nconc (mapcar
3933                             (lambda (h) (mail-header-number (car h)))
3934                             (cddar thread))
3935                            gnus-tmp-gathered))
3936               (setq gnus-tmp-level -1))
3937              ((eq gnus-summary-make-false-root 'dummy)
3938               ;; We remember that we probably want to output a dummy
3939               ;; root.
3940               (setq gnus-tmp-dummy-line gnus-tmp-header)
3941               (setq gnus-tmp-prev-subject gnus-tmp-header))
3942              (t
3943               ;; We do not make a root for the gathered
3944               ;; sub-threads at all.
3945               (setq gnus-tmp-level -1)))
3946
3947           (setq number (mail-header-number gnus-tmp-header)
3948                 subject (mail-header-subject gnus-tmp-header))
3949
3950           (cond
3951            ;; If the thread has changed subject, we might want to make
3952            ;; this subthread into a root.
3953            ((and (null gnus-thread-ignore-subject)
3954                  (not (zerop gnus-tmp-level))
3955                  gnus-tmp-prev-subject
3956                  (not (inline
3957                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3958             (setq new-roots (nconc new-roots (list (car thread)))
3959                   thread-end t
3960                   gnus-tmp-header nil))
3961            ;; If the article lies outside the current limit,
3962            ;; then we do not display it.
3963            ((not (memq number gnus-newsgroup-limit))
3964             (setq gnus-tmp-gathered
3965                   (nconc (mapcar
3966                           (lambda (h) (mail-header-number (car h)))
3967                           (cdar thread))
3968                          gnus-tmp-gathered))
3969             (setq gnus-tmp-new-adopts (if (cdar thread)
3970                                           (append gnus-tmp-new-adopts
3971                                                   (cdar thread))
3972                                         gnus-tmp-new-adopts)
3973                   thread-end t
3974                   gnus-tmp-header nil)
3975             (when (zerop gnus-tmp-level)
3976               (setq gnus-tmp-root-expunged t)))
3977            ;; Perhaps this article is to be marked as read?
3978            ((and gnus-summary-mark-below
3979                  (< (or (cdr (assq number gnus-newsgroup-scored))
3980                         default-score)
3981                     gnus-summary-mark-below)
3982                  ;; Don't touch sparse articles.
3983                  (not (gnus-summary-article-sparse-p number))
3984                  (not (gnus-summary-article-ancient-p number)))
3985             (setq gnus-newsgroup-unreads
3986                   (delq number gnus-newsgroup-unreads))
3987             (if gnus-newsgroup-auto-expire
3988                 (push number gnus-newsgroup-expirable)
3989               (push (cons number gnus-low-score-mark)
3990                     gnus-newsgroup-reads))))
3991
3992           (when gnus-tmp-header
3993             ;; We may have an old dummy line to output before this
3994             ;; article.
3995             (when (and gnus-tmp-dummy-line
3996                        (gnus-subject-equal
3997                         gnus-tmp-dummy-line
3998                         (mail-header-subject gnus-tmp-header)))
3999               (gnus-summary-insert-dummy-line
4000                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4001               (setq gnus-tmp-dummy-line nil))
4002
4003             ;; Compute the mark.
4004             (setq gnus-tmp-unread (gnus-article-mark number))
4005
4006             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4007                                   gnus-tmp-header gnus-tmp-level)
4008                   gnus-newsgroup-data)
4009
4010             ;; Actually insert the line.
4011             (setq
4012              gnus-tmp-subject-or-nil
4013              (cond
4014               ((and gnus-thread-ignore-subject
4015                     gnus-tmp-prev-subject
4016                     (not (inline (gnus-subject-equal
4017                                   gnus-tmp-prev-subject subject))))
4018                subject)
4019               ((zerop gnus-tmp-level)
4020                (if (and (eq gnus-summary-make-false-root 'empty)
4021                         (memq number gnus-tmp-gathered)
4022                         gnus-tmp-prev-subject
4023                         (inline (gnus-subject-equal
4024                                  gnus-tmp-prev-subject subject)))
4025                    gnus-summary-same-subject
4026                  subject))
4027               (t gnus-summary-same-subject)))
4028             (if (and (eq gnus-summary-make-false-root 'adopt)
4029                      (= gnus-tmp-level 1)
4030                      (memq number gnus-tmp-gathered))
4031                 (setq gnus-tmp-opening-bracket ?\<
4032                       gnus-tmp-closing-bracket ?\>)
4033               (setq gnus-tmp-opening-bracket ?\[
4034                     gnus-tmp-closing-bracket ?\]))
4035             (setq
4036              gnus-tmp-indentation
4037              (aref gnus-thread-indent-array gnus-tmp-level)
4038              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4039              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4040                                 gnus-summary-default-score 0)
4041              gnus-tmp-score-char
4042              (if (or (null gnus-summary-default-score)
4043                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4044                          gnus-summary-zcore-fuzz))
4045                  ?  ;Whitespace
4046                (if (< gnus-tmp-score gnus-summary-default-score)
4047                    gnus-score-below-mark gnus-score-over-mark))
4048              gnus-tmp-replied
4049              (cond ((memq number gnus-newsgroup-processable)
4050                     gnus-process-mark)
4051                    ((memq number gnus-newsgroup-cached)
4052                     gnus-cached-mark)
4053                    ((memq number gnus-newsgroup-replied)
4054                     gnus-replied-mark)
4055                    ((memq number gnus-newsgroup-saved)
4056                     gnus-saved-mark)
4057                    (t gnus-unread-mark))
4058              gnus-tmp-from (mail-header-from gnus-tmp-header)
4059              gnus-tmp-name
4060              (cond
4061               ((string-match "<[^>]+> *$" gnus-tmp-from)
4062                (setq beg-match (match-beginning 0))
4063                (or (and (string-match "^\".+\"" gnus-tmp-from)
4064                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4065                    (substring gnus-tmp-from 0 beg-match)))
4066               ((string-match "(.+)" gnus-tmp-from)
4067                (substring gnus-tmp-from
4068                           (1+ (match-beginning 0)) (1- (match-end 0))))
4069               (t gnus-tmp-from)))
4070             (when (string= gnus-tmp-name "")
4071               (setq gnus-tmp-name gnus-tmp-from))
4072             (unless (numberp gnus-tmp-lines)
4073               (setq gnus-tmp-lines 0))
4074             (gnus-put-text-property
4075              (point)
4076              (progn (eval gnus-summary-line-format-spec) (point))
4077              'gnus-number number)
4078             (when gnus-visual-p
4079               (forward-line -1)
4080               (gnus-run-hooks 'gnus-summary-update-hook)
4081               (forward-line 1))
4082
4083             (setq gnus-tmp-prev-subject subject)))
4084
4085         (when (nth 1 thread)
4086           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4087         (incf gnus-tmp-level)
4088         (setq threads (if thread-end nil (cdar thread)))
4089         (unless threads
4090           (setq gnus-tmp-level 0)))))
4091   (gnus-message 7 "Generating summary...done"))
4092
4093 (defun gnus-summary-prepare-unthreaded (headers)
4094   "Generate an unthreaded summary buffer based on HEADERS."
4095   (let (header number mark)
4096
4097     (beginning-of-line)
4098
4099     (while headers
4100       ;; We may have to root out some bad articles...
4101       (when (memq (setq number (mail-header-number
4102                                 (setq header (pop headers))))
4103                   gnus-newsgroup-limit)
4104         ;; Mark article as read when it has a low score.
4105         (when (and gnus-summary-mark-below
4106                    (< (or (cdr (assq number gnus-newsgroup-scored))
4107                           gnus-summary-default-score 0)
4108                       gnus-summary-mark-below)
4109                    (not (gnus-summary-article-ancient-p number)))
4110           (setq gnus-newsgroup-unreads
4111                 (delq number gnus-newsgroup-unreads))
4112           (if gnus-newsgroup-auto-expire
4113               (push number gnus-newsgroup-expirable)
4114             (push (cons number gnus-low-score-mark)
4115                   gnus-newsgroup-reads)))
4116
4117         (setq mark (gnus-article-mark number))
4118         (push (gnus-data-make number mark (1+ (point)) header 0)
4119               gnus-newsgroup-data)
4120         (gnus-summary-insert-line
4121          header 0 number
4122          mark (memq number gnus-newsgroup-replied)
4123          (memq number gnus-newsgroup-expirable)
4124          (mail-header-subject header) nil
4125          (cdr (assq number gnus-newsgroup-scored))
4126          (memq number gnus-newsgroup-processable))))))
4127
4128 (defun gnus-summary-remove-list-identifiers ()
4129   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4130   (let ((regexp (if (stringp gnus-list-identifiers)
4131                     gnus-list-identifiers
4132                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4133     (dolist (header gnus-newsgroup-headers)
4134       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp 
4135                                   " *\\)\\)+\\(Re: +\\)?\\)")
4136                           (mail-header-subject header))
4137         (mail-header-set-subject
4138          header (concat (substring (mail-header-subject header)
4139                                    0 (match-beginning 1))
4140                         (or
4141                          (match-string 3 (mail-header-subject header))
4142                          (match-string 5 (mail-header-subject header)))
4143                         (substring (mail-header-subject header)
4144                                    (match-end 1))))))))
4145
4146 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4147   "Select newsgroup GROUP.
4148 If READ-ALL is non-nil, all articles in the group are selected.
4149 If SELECT-ARTICLES, only select those articles from GROUP."
4150   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4151          ;;!!! Dirty hack; should be removed.
4152          (gnus-summary-ignore-duplicates
4153           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4154               t
4155             gnus-summary-ignore-duplicates))
4156          (info (nth 2 entry))
4157          articles fetched-articles cached)
4158
4159     (unless (gnus-check-server
4160              (setq gnus-current-select-method
4161                    (gnus-find-method-for-group group)))
4162       (error "Couldn't open server"))
4163
4164     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4165         (gnus-activate-group group)     ; Or we can activate it...
4166         (progn                          ; Or we bug out.
4167           (when (equal major-mode 'gnus-summary-mode)
4168             (kill-buffer (current-buffer)))
4169           (error "Couldn't request group %s: %s"
4170                  group (gnus-status-message group))))
4171
4172     (unless (gnus-request-group group t)
4173       (when (equal major-mode 'gnus-summary-mode)
4174         (kill-buffer (current-buffer)))
4175       (error "Couldn't request group %s: %s"
4176              group (gnus-status-message group)))
4177
4178     (setq gnus-newsgroup-name group)
4179     (setq gnus-newsgroup-unselected nil)
4180     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4181     (gnus-summary-setup-default-charset)
4182
4183     ;; Adjust and set lists of article marks.
4184     (when info
4185       (gnus-adjust-marked-articles info))
4186
4187     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4188     (when (gnus-virtual-group-p group)
4189       (setq cached gnus-newsgroup-cached))
4190
4191     (setq gnus-newsgroup-unreads
4192           (gnus-set-difference
4193            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4194            gnus-newsgroup-dormant))
4195
4196     (setq gnus-newsgroup-processable nil)
4197
4198     (gnus-update-read-articles group gnus-newsgroup-unreads)
4199
4200     (if (setq articles select-articles)
4201         (setq gnus-newsgroup-unselected
4202               (gnus-sorted-intersection
4203                gnus-newsgroup-unreads
4204                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4205       (setq articles (gnus-articles-to-read group read-all)))
4206
4207     (cond
4208      ((null articles)
4209       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4210       'quit)
4211      ((eq articles 0) nil)
4212      (t
4213       ;; Init the dependencies hash table.
4214       (setq gnus-newsgroup-dependencies
4215             (gnus-make-hashtable (length articles)))
4216       (gnus-set-global-variables)
4217       ;; Retrieve the headers and read them in.
4218       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4219       (setq gnus-newsgroup-headers
4220             (if (eq 'nov
4221                     (setq gnus-headers-retrieved-by
4222                           (gnus-retrieve-headers
4223                            articles gnus-newsgroup-name
4224                            ;; We might want to fetch old headers, but
4225                            ;; not if there is only 1 article.
4226                            (and (or (and
4227                                      (not (eq gnus-fetch-old-headers 'some))
4228                                      (not (numberp gnus-fetch-old-headers)))
4229                                     (> (length articles) 1))
4230                                 gnus-fetch-old-headers))))
4231                 (gnus-get-newsgroup-headers-xover
4232                  articles nil nil gnus-newsgroup-name t)
4233               (gnus-get-newsgroup-headers)))
4234       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4235
4236       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4237       (when cached
4238         (setq gnus-newsgroup-cached cached))
4239
4240       ;; Suppress duplicates?
4241       (when gnus-suppress-duplicates
4242         (gnus-dup-suppress-articles))
4243
4244       ;; Set the initial limit.
4245       (setq gnus-newsgroup-limit (copy-sequence articles))
4246       ;; Remove canceled articles from the list of unread articles.
4247       (setq gnus-newsgroup-unreads
4248             (gnus-set-sorted-intersection
4249              gnus-newsgroup-unreads
4250              (setq fetched-articles
4251                    (mapcar (lambda (headers) (mail-header-number headers))
4252                            gnus-newsgroup-headers))))
4253       ;; Removed marked articles that do not exist.
4254       (gnus-update-missing-marks
4255        (gnus-sorted-complement fetched-articles articles))
4256       ;; We might want to build some more threads first.
4257       (when (and gnus-fetch-old-headers
4258                  (eq gnus-headers-retrieved-by 'nov))
4259         (if (eq gnus-fetch-old-headers 'invisible)
4260             (gnus-build-all-threads)
4261           (gnus-build-old-threads)))
4262       ;; Let the Gnus agent mark articles as read.
4263       (when gnus-agent
4264         (gnus-agent-get-undownloaded-list))
4265       ;; Remove list identifiers from subject
4266       (when gnus-list-identifiers
4267         (gnus-summary-remove-list-identifiers))
4268       ;; Check whether auto-expire is to be done in this group.
4269       (setq gnus-newsgroup-auto-expire
4270             (gnus-group-auto-expirable-p group))
4271       ;; Set up the article buffer now, if necessary.
4272       (unless gnus-single-article-buffer
4273         (gnus-article-setup-buffer))
4274       ;; First and last article in this newsgroup.
4275       (when gnus-newsgroup-headers
4276         (setq gnus-newsgroup-begin
4277               (mail-header-number (car gnus-newsgroup-headers))
4278               gnus-newsgroup-end
4279               (mail-header-number
4280                (gnus-last-element gnus-newsgroup-headers))))
4281       ;; GROUP is successfully selected.
4282       (or gnus-newsgroup-headers t)))))
4283
4284 (defun gnus-articles-to-read (group &optional read-all)
4285   "Find out what articles the user wants to read."
4286   (let* ((articles
4287           ;; Select all articles if `read-all' is non-nil, or if there
4288           ;; are no unread articles.
4289           (if (or read-all
4290                   (and (zerop (length gnus-newsgroup-marked))
4291                        (zerop (length gnus-newsgroup-unreads)))
4292                   (eq (gnus-group-find-parameter group 'display)
4293                       'all))
4294               (or
4295                (gnus-uncompress-range (gnus-active group))
4296                (gnus-cache-articles-in-group group))
4297             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4298                           (copy-sequence gnus-newsgroup-unreads))
4299                   '<)))
4300          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4301          (scored (length scored-list))
4302          (number (length articles))
4303          (marked (+ (length gnus-newsgroup-marked)
4304                     (length gnus-newsgroup-dormant)))
4305          (select
4306           (cond
4307            ((numberp read-all)
4308             read-all)
4309            (t
4310             (condition-case ()
4311                 (cond
4312                  ((and (or (<= scored marked) (= scored number))
4313                        (numberp gnus-large-newsgroup)
4314                        (> number gnus-large-newsgroup))
4315                   (let ((input
4316                          (read-string
4317                           (format
4318                            "How many articles from %s (default %d): "
4319                            (gnus-limit-string gnus-newsgroup-name 35)
4320                            number))))
4321                     (if (string-match "^[ \t]*$" input) number input)))
4322                  ((and (> scored marked) (< scored number)
4323                        (> (- scored number) 20))
4324                   (let ((input
4325                          (read-string
4326                           (format "%s %s (%d scored, %d total): "
4327                                   "How many articles from"
4328                                   group scored number))))
4329                     (if (string-match "^[ \t]*$" input)
4330                         number input)))
4331                  (t number))
4332               (quit
4333                (message "Quit getting the articles to read")
4334                nil))))))
4335     (setq select (if (stringp select) (string-to-number select) select))
4336     (if (or (null select) (zerop select))
4337         select
4338       (if (and (not (zerop scored)) (<= (abs select) scored))
4339           (progn
4340             (setq articles (sort scored-list '<))
4341             (setq number (length articles)))
4342         (setq articles (copy-sequence articles)))
4343
4344       (when (< (abs select) number)
4345         (if (< select 0)
4346             ;; Select the N oldest articles.
4347             (setcdr (nthcdr (1- (abs select)) articles) nil)
4348           ;; Select the N most recent articles.
4349           (setq articles (nthcdr (- number select) articles))))
4350       (setq gnus-newsgroup-unselected
4351             (gnus-sorted-intersection
4352              gnus-newsgroup-unreads
4353              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4354       (when gnus-alter-articles-to-read-function
4355         (setq gnus-newsgroup-unreads
4356               (sort 
4357                (funcall gnus-alter-articles-to-read-function
4358                         gnus-newsgroup-name gnus-newsgroup-unreads)
4359                '<)))
4360       articles)))
4361
4362 (defun gnus-killed-articles (killed articles)
4363   (let (out)
4364     (while articles
4365       (when (inline (gnus-member-of-range (car articles) killed))
4366         (push (car articles) out))
4367       (setq articles (cdr articles)))
4368     out))
4369
4370 (defun gnus-uncompress-marks (marks)
4371   "Uncompress the mark ranges in MARKS."
4372   (let ((uncompressed '(score bookmark))
4373         out)
4374     (while marks
4375       (if (memq (caar marks) uncompressed)
4376           (push (car marks) out)
4377         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4378       (setq marks (cdr marks)))
4379     out))
4380
4381 (defun gnus-adjust-marked-articles (info)
4382   "Set all article lists and remove all marks that are no longer valid."
4383   (let* ((marked-lists (gnus-info-marks info))
4384          (active (gnus-active (gnus-info-group info)))
4385          (min (car active))
4386          (max (cdr active))
4387          (types gnus-article-mark-lists)
4388          (uncompressed '(score bookmark killed))
4389          marks var articles article mark)
4390
4391     (while marked-lists
4392       (setq marks (pop marked-lists))
4393       (set (setq var (intern (format "gnus-newsgroup-%s"
4394                                      (car (rassq (setq mark (car marks))
4395                                                  types)))))
4396            (if (memq (car marks) uncompressed) (cdr marks)
4397              (gnus-uncompress-range (cdr marks))))
4398
4399       (setq articles (symbol-value var))
4400
4401       ;; All articles have to be subsets of the active articles.
4402       (cond
4403        ;; Adjust "simple" lists.
4404        ((memq mark '(tick dormant expire reply save))
4405         (while articles
4406           (when (or (< (setq article (pop articles)) min) (> article max))
4407             (set var (delq article (symbol-value var))))))
4408        ;; Adjust assocs.
4409        ((memq mark uncompressed)
4410         (when (not (listp (cdr (symbol-value var))))
4411           (set var (list (symbol-value var))))
4412         (when (not (listp (cdr articles)))
4413           (setq articles (list articles)))
4414         (while articles
4415           (when (or (not (consp (setq article (pop articles))))
4416                     (< (car article) min)
4417                     (> (car article) max))
4418             (set var (delq article (symbol-value var))))))))))
4419
4420 (defun gnus-update-missing-marks (missing)
4421   "Go through the list of MISSING articles and remove them from the mark lists."
4422   (when missing
4423     (let ((types gnus-article-mark-lists)
4424           var m)
4425       ;; Go through all types.
4426       (while types
4427         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4428         (when (symbol-value var)
4429           ;; This list has articles.  So we delete all missing articles
4430           ;; from it.
4431           (setq m missing)
4432           (while m
4433             (set var (delq (pop m) (symbol-value var)))))))))
4434
4435 (defun gnus-update-marks ()
4436   "Enter the various lists of marked articles into the newsgroup info list."
4437   (let ((types gnus-article-mark-lists)
4438         (info (gnus-get-info gnus-newsgroup-name))
4439         (uncompressed '(score bookmark killed))
4440         type list newmarked symbol delta-marks)
4441     (when info
4442       ;; Add all marks lists to the list of marks lists.
4443       (while (setq type (pop types))
4444         (setq list (symbol-value
4445                     (setq symbol
4446                           (intern (format "gnus-newsgroup-%s"
4447                                           (car type))))))
4448
4449         (when list
4450           ;; Get rid of the entries of the articles that have the
4451           ;; default score.
4452           (when (and (eq (cdr type) 'score)
4453                      gnus-save-score
4454                      list)
4455             (let* ((arts list)
4456                    (prev (cons nil list))
4457                    (all prev))
4458               (while arts
4459                 (if (or (not (consp (car arts)))
4460                         (= (cdar arts) gnus-summary-default-score))
4461                     (setcdr prev (cdr arts))
4462                   (setq prev arts))
4463                 (setq arts (cdr arts)))
4464               (setq list (cdr all)))))
4465
4466         (unless (memq (cdr type) uncompressed)
4467           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4468        
4469         (when (gnus-check-backend-function
4470                'request-set-mark gnus-newsgroup-name)
4471           ;; propagate flags to server, with the following exceptions:
4472           ;; uncompressed:s are not proper flags (they are cons cells)
4473           ;; cache is a internal gnus flag
4474           ;; download are local to one gnus installation (well)
4475           ;; unsend are for nndraft groups only
4476           ;; xxx: generality of this?  this suits nnimap anyway
4477           (unless (memq (cdr type) (append '(cache download unsend)
4478                                            uncompressed))
4479             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4480                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4481                    (add (gnus-remove-from-range
4482                          (gnus-copy-sequence list) old)))
4483               (when add
4484                 (push (list add 'add (list (cdr type))) delta-marks))
4485               (when del
4486                 (push (list del 'del (list (cdr type))) delta-marks)))))
4487           
4488         (when list
4489           (push (cons (cdr type) list) newmarked)))
4490
4491       (when delta-marks
4492         (unless (gnus-check-group gnus-newsgroup-name)
4493           (error "Can't open server for %s" gnus-newsgroup-name))
4494         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4495           
4496       ;; Enter these new marks into the info of the group.
4497       (if (nthcdr 3 info)
4498           (setcar (nthcdr 3 info) newmarked)
4499         ;; Add the marks lists to the end of the info.
4500         (when newmarked
4501           (setcdr (nthcdr 2 info) (list newmarked))))
4502
4503       ;; Cut off the end of the info if there's nothing else there.
4504       (let ((i 5))
4505         (while (and (> i 2)
4506                     (not (nth i info)))
4507           (when (nthcdr (decf i) info)
4508             (setcdr (nthcdr i info) nil)))))))
4509
4510 (defun gnus-set-mode-line (where)
4511   "Set the mode line of the article or summary buffers.
4512 If WHERE is `summary', the summary mode line format will be used."
4513   ;; Is this mode line one we keep updated?
4514   (when (and (memq where gnus-updated-mode-lines)
4515              (symbol-value
4516               (intern (format "gnus-%s-mode-line-format-spec" where))))
4517     (let (mode-string)
4518       (save-excursion
4519         ;; We evaluate this in the summary buffer since these
4520         ;; variables are buffer-local to that buffer.
4521         (set-buffer gnus-summary-buffer)
4522         ;; We bind all these variables that are used in the `eval' form
4523         ;; below.
4524         (let* ((mformat (symbol-value
4525                          (intern
4526                           (format "gnus-%s-mode-line-format-spec" where))))
4527                (gnus-tmp-group-name (gnus-group-name-decode 
4528                                      gnus-newsgroup-name
4529                                      (gnus-group-name-charset 
4530                                       nil
4531                                       gnus-newsgroup-name)))
4532                (gnus-tmp-article-number (or gnus-current-article 0))
4533                (gnus-tmp-unread gnus-newsgroup-unreads)
4534                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4535                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4536                (gnus-tmp-unread-and-unselected
4537                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4538                             (zerop gnus-tmp-unselected))
4539                        "")
4540                       ((zerop gnus-tmp-unselected)
4541                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4542                       (t (format "{%d(+%d) more}"
4543                                  gnus-tmp-unread-and-unticked
4544                                  gnus-tmp-unselected))))
4545                (gnus-tmp-subject
4546                 (if (and gnus-current-headers
4547                          (vectorp gnus-current-headers))
4548                     (gnus-mode-string-quote
4549                      (mail-header-subject gnus-current-headers))
4550                   ""))
4551                bufname-length max-len
4552                gnus-tmp-header);; passed as argument to any user-format-funcs
4553           (setq mode-string (eval mformat))
4554           (setq bufname-length (if (string-match "%b" mode-string)
4555                                    (- (length
4556                                        (buffer-name
4557                                         (if (eq where 'summary)
4558                                             nil
4559                                           (get-buffer gnus-article-buffer))))
4560                                       2)
4561                                  0))
4562           (setq max-len (max 4 (if gnus-mode-non-string-length
4563                                    (- (window-width)
4564                                       gnus-mode-non-string-length
4565                                       bufname-length)
4566                                  (length mode-string))))
4567           ;; We might have to chop a bit of the string off...
4568           (when (> (length mode-string) max-len)
4569             (setq mode-string
4570                   (concat (truncate-string-to-width mode-string (- max-len 3))
4571                           "...")))
4572           ;; Pad the mode string a bit.
4573           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4574       ;; Update the mode line.
4575       (setq mode-line-buffer-identification
4576             (gnus-mode-line-buffer-identification (list mode-string)))
4577       (set-buffer-modified-p t))))
4578
4579 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4580   "Go through the HEADERS list and add all Xrefs to a hash table.
4581 The resulting hash table is returned, or nil if no Xrefs were found."
4582   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4583          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4584          (xref-hashtb (gnus-make-hashtable))
4585          start group entry number xrefs header)
4586     (while headers
4587       (setq header (pop headers))
4588       (when (and (setq xrefs (mail-header-xref header))
4589                  (not (memq (setq number (mail-header-number header))
4590                             unreads)))
4591         (setq start 0)
4592         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4593           (setq start (match-end 0))
4594           (setq group (if prefix
4595                           (concat prefix (substring xrefs (match-beginning 1)
4596                                                     (match-end 1)))
4597                         (substring xrefs (match-beginning 1) (match-end 1))))
4598           (setq number
4599                 (string-to-int (substring xrefs (match-beginning 2)
4600                                           (match-end 2))))
4601           (if (setq entry (gnus-gethash group xref-hashtb))
4602               (setcdr entry (cons number (cdr entry)))
4603             (gnus-sethash group (cons number nil) xref-hashtb)))))
4604     (and start xref-hashtb)))
4605
4606 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4607   "Look through all the headers and mark the Xrefs as read."
4608   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4609         name entry info xref-hashtb idlist method nth4)
4610     (save-excursion
4611       (set-buffer gnus-group-buffer)
4612       (when (setq xref-hashtb
4613                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4614         (mapatoms
4615          (lambda (group)
4616            (unless (string= from-newsgroup (setq name (symbol-name group)))
4617              (setq idlist (symbol-value group))
4618              ;; Dead groups are not updated.
4619              (and (prog1
4620                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4621                             info (nth 2 entry))
4622                     (when (stringp (setq nth4 (gnus-info-method info)))
4623                       (setq nth4 (gnus-server-to-method nth4))))
4624                   ;; Only do the xrefs if the group has the same
4625                   ;; select method as the group we have just read.
4626                   (or (gnus-methods-equal-p
4627                        nth4 (gnus-find-method-for-group from-newsgroup))
4628                       virtual
4629                       (equal nth4 (setq method (gnus-find-method-for-group
4630                                                 from-newsgroup)))
4631                       (and (equal (car nth4) (car method))
4632                            (equal (nth 1 nth4) (nth 1 method))))
4633                   gnus-use-cross-reference
4634                   (or (not (eq gnus-use-cross-reference t))
4635                       virtual
4636                       ;; Only do cross-references on subscribed
4637                       ;; groups, if that is what is wanted.
4638                       (<= (gnus-info-level info) gnus-level-subscribed))
4639                   (gnus-group-make-articles-read name idlist))))
4640          xref-hashtb)))))
4641
4642 (defun gnus-compute-read-articles (group articles)
4643   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4644          (info (nth 2 entry))
4645          (active (gnus-active group))
4646          ninfo)
4647     (when entry
4648       ;; First peel off all invalid article numbers.
4649       (when active
4650         (let ((ids articles)
4651               id first)
4652           (while (setq id (pop ids))
4653             (when (and first (> id (cdr active)))
4654               ;; We'll end up in this situation in one particular
4655               ;; obscure situation.  If you re-scan a group and get
4656               ;; a new article that is cross-posted to a different
4657               ;; group that has not been re-scanned, you might get
4658               ;; crossposted article that has a higher number than
4659               ;; Gnus believes possible.  So we re-activate this
4660               ;; group as well.  This might mean doing the
4661               ;; crossposting thingy will *increase* the number
4662               ;; of articles in some groups.  Tsk, tsk.
4663               (setq active (or (gnus-activate-group group) active)))
4664             (when (or (> id (cdr active))
4665                       (< id (car active)))
4666               (setq articles (delq id articles))))))
4667       ;; If the read list is nil, we init it.
4668       (if (and active
4669                (null (gnus-info-read info))
4670                (> (car active) 1))
4671           (setq ninfo (cons 1 (1- (car active))))
4672         (setq ninfo (gnus-info-read info)))
4673       ;; Then we add the read articles to the range.
4674       (gnus-add-to-range
4675        ninfo (setq articles (sort articles '<))))))
4676
4677 (defun gnus-group-make-articles-read (group articles)
4678   "Update the info of GROUP to say that ARTICLES are read."
4679   (let* ((num 0)
4680          (entry (gnus-gethash group gnus-newsrc-hashtb))
4681          (info (nth 2 entry))
4682          (active (gnus-active group))
4683          range)
4684     (when entry
4685       (setq range (gnus-compute-read-articles group articles))
4686       (save-excursion
4687         (set-buffer gnus-group-buffer)
4688         (gnus-undo-register
4689           `(progn
4690              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4691              (gnus-info-set-read ',info ',(gnus-info-read info))
4692              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4693              (gnus-group-update-group ,group t))))
4694       ;; Add the read articles to the range.
4695       (gnus-info-set-read info range)
4696       ;; Then we have to re-compute how many unread
4697       ;; articles there are in this group.
4698       (when active
4699         (cond
4700          ((not range)
4701           (setq num (- (1+ (cdr active)) (car active))))
4702          ((not (listp (cdr range)))
4703           (setq num (- (cdr active) (- (1+ (cdr range))
4704                                        (car range)))))
4705          (t
4706           (while range
4707             (if (numberp (car range))
4708                 (setq num (1+ num))
4709               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4710             (setq range (cdr range)))
4711           (setq num (- (cdr active) num))))
4712         ;; Update the number of unread articles.
4713         (setcar entry num)
4714         ;; Update the group buffer.
4715         (gnus-group-update-group group t)))))
4716
4717 (defvar gnus-newsgroup-none-id 0)
4718
4719 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4720   (let ((cur nntp-server-buffer)
4721         (dependencies
4722          (or dependencies
4723              (save-excursion (set-buffer gnus-summary-buffer)
4724                              gnus-newsgroup-dependencies)))
4725         headers id end ref
4726         (mail-parse-charset gnus-newsgroup-charset)
4727         (mail-parse-ignored-charsets
4728          (save-excursion (condition-case nil
4729                              (set-buffer gnus-summary-buffer)
4730                            (error))
4731                          gnus-newsgroup-ignored-charsets)))
4732     (save-excursion
4733       (set-buffer nntp-server-buffer)
4734       ;; Translate all TAB characters into SPACE characters.
4735       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4736       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4737       (gnus-run-hooks 'gnus-parse-headers-hook)
4738       (let ((case-fold-search t)
4739             in-reply-to header p lines chars)
4740         (goto-char (point-min))
4741         ;; Search to the beginning of the next header.  Error messages
4742         ;; do not begin with 2 or 3.
4743         (while (re-search-forward "^[23][0-9]+ " nil t)
4744           (setq id nil
4745                 ref nil)
4746           ;; This implementation of this function, with nine
4747           ;; search-forwards instead of the one re-search-forward and
4748           ;; a case (which basically was the old function) is actually
4749           ;; about twice as fast, even though it looks messier.  You
4750           ;; can't have everything, I guess.  Speed and elegance
4751           ;; doesn't always go hand in hand.
4752           (setq
4753            header
4754            (vector
4755             ;; Number.
4756             (prog1
4757                 (read cur)
4758               (end-of-line)
4759               (setq p (point))
4760               (narrow-to-region (point)
4761                                 (or (and (search-forward "\n.\n" nil t)
4762                                          (- (point) 2))
4763                                     (point))))
4764             ;; Subject.
4765             (progn
4766               (goto-char p)
4767               (if (search-forward "\nsubject: " nil t)
4768                   (funcall gnus-decode-encoded-word-function
4769                            (nnheader-header-value))
4770                 "(none)"))
4771             ;; From.
4772             (progn
4773               (goto-char p)
4774               (if (or (search-forward "\nfrom: " nil t)
4775                       (search-forward "\nfrom:" nil t))
4776                   (funcall gnus-decode-encoded-word-function
4777                            (nnheader-header-value))
4778                 "(nobody)"))
4779             ;; Date.
4780             (progn
4781               (goto-char p)
4782               (if (search-forward "\ndate: " nil t)
4783                   (nnheader-header-value) ""))
4784             ;; Message-ID.
4785             (progn
4786               (goto-char p)
4787               (setq id (if (re-search-forward
4788                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4789                            ;; We do it this way to make sure the Message-ID
4790                            ;; is (somewhat) syntactically valid.
4791                            (buffer-substring (match-beginning 1)
4792                                              (match-end 1))
4793                          ;; If there was no message-id, we just fake one
4794                          ;; to make subsequent routines simpler.
4795                          (nnheader-generate-fake-message-id))))
4796             ;; References.
4797             (progn
4798               (goto-char p)
4799               (if (search-forward "\nreferences: " nil t)
4800                   (progn
4801                     (setq end (point))
4802                     (prog1
4803                         (nnheader-header-value)
4804                       (setq ref
4805                             (buffer-substring
4806                              (progn
4807                                (end-of-line)
4808                                (search-backward ">" end t)
4809                                (1+ (point)))
4810                              (progn
4811                                (search-backward "<" end t)
4812                                (point))))))
4813                 ;; Get the references from the in-reply-to header if there
4814                 ;; were no references and the in-reply-to header looks
4815                 ;; promising.
4816                 (if (and (search-forward "\nin-reply-to: " nil t)
4817                          (setq in-reply-to (nnheader-header-value))
4818                          (string-match "<[^>]+>" in-reply-to))
4819                     (let (ref2)
4820                       (setq ref (substring in-reply-to (match-beginning 0)
4821                                            (match-end 0)))
4822                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4823                         (setq ref2 (substring in-reply-to (match-beginning 0)
4824                                               (match-end 0)))
4825                         (when (> (length ref2) (length ref))
4826                           (setq ref ref2)))
4827                       ref)
4828                   (setq ref nil))))
4829             ;; Chars.
4830             (progn
4831               (goto-char p)
4832               (if (search-forward "\nchars: " nil t)
4833                   (if (numberp (setq chars (ignore-errors (read cur))))
4834                       chars 0)
4835                 0))
4836             ;; Lines.
4837             (progn
4838               (goto-char p)
4839               (if (search-forward "\nlines: " nil t)
4840                   (if (numberp (setq lines (ignore-errors (read cur))))
4841                       lines 0)
4842                 0))
4843             ;; Xref.
4844             (progn
4845               (goto-char p)
4846               (and (search-forward "\nxref: " nil t)
4847                    (nnheader-header-value)))
4848             ;; Extra.
4849             (when gnus-extra-headers
4850               (let ((extra gnus-extra-headers)
4851                     out)
4852                 (while extra
4853                   (goto-char p)
4854                   (when (search-forward
4855                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4856                     (push (cons (car extra) (nnheader-header-value))
4857                           out))
4858                   (pop extra))
4859                 out))))
4860           (when (equal id ref)
4861             (setq ref nil))
4862
4863           (when gnus-alter-header-function
4864             (funcall gnus-alter-header-function header)
4865             (setq id (mail-header-id header)
4866                   ref (gnus-parent-id (mail-header-references header))))
4867
4868           (when (setq header
4869                       (gnus-dependencies-add-header
4870                        header dependencies force-new))
4871             (push header headers))
4872           (goto-char (point-max))
4873           (widen))
4874         (nreverse headers)))))
4875
4876 ;; Goes through the xover lines and returns a list of vectors
4877 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4878                                                   force-new dependencies
4879                                                   group also-fetch-heads)
4880   "Parse the news overview data in the server buffer.
4881 Return a list of headers that match SEQUENCE (see
4882 `nntp-retrieve-headers')."
4883   ;; Get the Xref when the users reads the articles since most/some
4884   ;; NNTP servers do not include Xrefs when using XOVER.
4885   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4886   (let ((mail-parse-charset gnus-newsgroup-charset)
4887         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4888         (cur nntp-server-buffer)
4889         (dependencies (or dependencies gnus-newsgroup-dependencies))
4890         number headers header)
4891     (save-excursion
4892       (set-buffer nntp-server-buffer)
4893       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4894       ;; Allow the user to mangle the headers before parsing them.
4895       (gnus-run-hooks 'gnus-parse-headers-hook)
4896       (goto-char (point-min))
4897       (while (not (eobp))
4898         (condition-case ()
4899             (while (and sequence (not (eobp)))
4900               (setq number (read cur))
4901               (while (and sequence
4902                           (< (car sequence) number))
4903                 (setq sequence (cdr sequence)))
4904               (and sequence
4905                    (eq number (car sequence))
4906                    (progn
4907                      (setq sequence (cdr sequence))
4908                      (setq header (inline
4909                                     (gnus-nov-parse-line
4910                                      number dependencies force-new))))
4911                    (push header headers))
4912               (forward-line 1))
4913           (error
4914            (gnus-error 4 "Strange nov line (%d)"
4915                        (count-lines (point-min) (point)))))
4916         (forward-line 1))
4917       ;; A common bug in inn is that if you have posted an article and
4918       ;; then retrieves the active file, it will answer correctly --
4919       ;; the new article is included.  However, a NOV entry for the
4920       ;; article may not have been generated yet, so this may fail.
4921       ;; We work around this problem by retrieving the last few
4922       ;; headers using HEAD.
4923       (if (or (not also-fetch-heads)
4924               (not sequence))
4925           ;; We (probably) got all the headers.
4926           (nreverse headers)
4927         (let ((gnus-nov-is-evil t))
4928           (nconc
4929            (nreverse headers)
4930            (when (gnus-retrieve-headers sequence group)
4931              (gnus-get-newsgroup-headers))))))))
4932
4933 (defun gnus-article-get-xrefs ()
4934   "Fill in the Xref value in `gnus-current-headers', if necessary.
4935 This is meant to be called in `gnus-article-internal-prepare-hook'."
4936   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4937                                  gnus-current-headers)))
4938     (or (not gnus-use-cross-reference)
4939         (not headers)
4940         (and (mail-header-xref headers)
4941              (not (string= (mail-header-xref headers) "")))
4942         (let ((case-fold-search t)
4943               xref)
4944           (save-restriction
4945             (nnheader-narrow-to-headers)
4946             (goto-char (point-min))
4947             (when (or (and (not (eobp))
4948                            (eq (downcase (char-after)) ?x)
4949                            (looking-at "Xref:"))
4950                       (search-forward "\nXref:" nil t))
4951               (goto-char (1+ (match-end 0)))
4952               (setq xref (buffer-substring (point)
4953                                            (progn (end-of-line) (point))))
4954               (mail-header-set-xref headers xref)))))))
4955
4956 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4957   "Find article ID and insert the summary line for that article.
4958 OLD-HEADER can either be a header or a line number to insert
4959 the subject line on."
4960   (let* ((line (and (numberp old-header) old-header))
4961          (old-header (and (vectorp old-header) old-header))
4962          (header (cond ((and old-header use-old-header)
4963                         old-header)
4964                        ((and (numberp id)
4965                              (gnus-number-to-header id))
4966                         (gnus-number-to-header id))
4967                        (t
4968                         (gnus-read-header id))))
4969          (number (and (numberp id) id))
4970          d)
4971     (when header
4972       ;; Rebuild the thread that this article is part of and go to the
4973       ;; article we have fetched.
4974       (when (and (not gnus-show-threads)
4975                  old-header)
4976         (when (and number
4977                    (setq d (gnus-data-find (mail-header-number old-header))))
4978           (goto-char (gnus-data-pos d))
4979           (gnus-data-remove
4980            number
4981            (- (gnus-point-at-bol)
4982               (prog1
4983                   (1+ (gnus-point-at-eol))
4984                 (gnus-delete-line))))))
4985       (when old-header
4986         (mail-header-set-number header (mail-header-number old-header)))
4987       (setq gnus-newsgroup-sparse
4988             (delq (setq number (mail-header-number header))
4989                   gnus-newsgroup-sparse))
4990       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4991       (push number gnus-newsgroup-limit)
4992       (gnus-rebuild-thread (mail-header-id header) line)
4993       (gnus-summary-goto-subject number nil t))
4994     (when (and (numberp number)
4995                (> number 0))
4996       ;; We have to update the boundaries even if we can't fetch the
4997       ;; article if ID is a number -- so that the next `P' or `N'
4998       ;; command will fetch the previous (or next) article even
4999       ;; if the one we tried to fetch this time has been canceled.
5000       (when (> number gnus-newsgroup-end)
5001         (setq gnus-newsgroup-end number))
5002       (when (< number gnus-newsgroup-begin)
5003         (setq gnus-newsgroup-begin number))
5004       (setq gnus-newsgroup-unselected
5005             (delq number gnus-newsgroup-unselected)))
5006     ;; Report back a success?
5007     (and header (mail-header-number header))))
5008
5009 ;;; Process/prefix in the summary buffer
5010
5011 (defun gnus-summary-work-articles (n)
5012   "Return a list of articles to be worked upon.
5013 The prefix argument, the list of process marked articles, and the
5014 current article will be taken into consideration."
5015   (save-excursion
5016     (set-buffer gnus-summary-buffer)
5017     (cond
5018      (n
5019       ;; A numerical prefix has been given.
5020       (setq n (prefix-numeric-value n))
5021       (let ((backward (< n 0))
5022             (n (abs (prefix-numeric-value n)))
5023             articles article)
5024         (save-excursion
5025           (while
5026               (and (> n 0)
5027                    (push (setq article (gnus-summary-article-number))
5028                          articles)
5029                    (if backward
5030                        (gnus-summary-find-prev nil article)
5031                      (gnus-summary-find-next nil article)))
5032             (decf n)))
5033         (nreverse articles)))
5034      ((and (gnus-region-active-p) (mark))
5035       (message "region active")
5036       ;; Work on the region between point and mark.
5037       (let ((max (max (point) (mark)))
5038             articles article)
5039         (save-excursion
5040           (goto-char (min (min (point) (mark))))
5041           (while
5042               (and
5043                (push (setq article (gnus-summary-article-number)) articles)
5044                (gnus-summary-find-next nil article)
5045                (< (point) max)))
5046           (nreverse articles))))
5047      (gnus-newsgroup-processable
5048       ;; There are process-marked articles present.
5049       ;; Save current state.
5050       (gnus-summary-save-process-mark)
5051       ;; Return the list.
5052       (reverse gnus-newsgroup-processable))
5053      (t
5054       ;; Just return the current article.
5055       (list (gnus-summary-article-number))))))
5056
5057 (defmacro gnus-summary-iterate (arg &rest forms)
5058   "Iterate over the process/prefixed articles and do FORMS.
5059 ARG is the interactive prefix given to the command.  FORMS will be
5060 executed with point over the summary line of the articles."
5061   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5062     `(let ((,articles (gnus-summary-work-articles ,arg)))
5063        (while ,articles
5064          (gnus-summary-goto-subject (car ,articles))
5065          ,@forms
5066          (pop ,articles)))))
5067
5068 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5069 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5070
5071 (defun gnus-summary-save-process-mark ()
5072   "Push the current set of process marked articles on the stack."
5073   (interactive)
5074   (push (copy-sequence gnus-newsgroup-processable)
5075         gnus-newsgroup-process-stack))
5076
5077 (defun gnus-summary-kill-process-mark ()
5078   "Push the current set of process marked articles on the stack and unmark."
5079   (interactive)
5080   (gnus-summary-save-process-mark)
5081   (gnus-summary-unmark-all-processable))
5082
5083 (defun gnus-summary-yank-process-mark ()
5084   "Pop the last process mark state off the stack and restore it."
5085   (interactive)
5086   (unless gnus-newsgroup-process-stack
5087     (error "Empty mark stack"))
5088   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5089
5090 (defun gnus-summary-process-mark-set (set)
5091   "Make SET into the current process marked articles."
5092   (gnus-summary-unmark-all-processable)
5093   (while set
5094     (gnus-summary-set-process-mark (pop set))))
5095
5096 ;;; Searching and stuff
5097
5098 (defun gnus-summary-search-group (&optional backward use-level)
5099   "Search for next unread newsgroup.
5100 If optional argument BACKWARD is non-nil, search backward instead."
5101   (save-excursion
5102     (set-buffer gnus-group-buffer)
5103     (when (gnus-group-search-forward
5104            backward nil (if use-level (gnus-group-group-level) nil))
5105       (gnus-group-group-name))))
5106
5107 (defun gnus-summary-best-group (&optional exclude-group)
5108   "Find the name of the best unread group.
5109 If EXCLUDE-GROUP, do not go to this group."
5110   (save-excursion
5111     (set-buffer gnus-group-buffer)
5112     (save-excursion
5113       (gnus-group-best-unread-group exclude-group))))
5114
5115 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5116   (if backward (gnus-summary-find-prev)
5117     (let* ((dummy (gnus-summary-article-intangible-p))
5118            (article (or article (gnus-summary-article-number)))
5119            (arts (gnus-data-find-list article))
5120            result)
5121       (when (and (not dummy)
5122                  (or (not gnus-summary-check-current)
5123                      (not unread)
5124                      (not (gnus-data-unread-p (car arts)))))
5125         (setq arts (cdr arts)))
5126       (when (setq result
5127                   (if unread
5128                       (progn
5129                         (while arts
5130                           (when (or (and undownloaded
5131                                          (eq gnus-undownloaded-mark
5132                                              (gnus-data-mark (car arts))))
5133                                     (gnus-data-unread-p (car arts)))
5134                             (setq result (car arts)
5135                                   arts nil))
5136                           (setq arts (cdr arts)))
5137                         result)
5138                     (car arts)))
5139         (goto-char (gnus-data-pos result))
5140         (gnus-data-number result)))))
5141
5142 (defun gnus-summary-find-prev (&optional unread article)
5143   (let* ((eobp (eobp))
5144          (article (or article (gnus-summary-article-number)))
5145          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5146          result)
5147     (when (and (not eobp)
5148                (or (not gnus-summary-check-current)
5149                    (not unread)
5150                    (not (gnus-data-unread-p (car arts)))))
5151       (setq arts (cdr arts)))
5152     (when (setq result
5153                 (if unread
5154                     (progn
5155                       (while arts
5156                         (when (gnus-data-unread-p (car arts))
5157                           (setq result (car arts)
5158                                 arts nil))
5159                         (setq arts (cdr arts)))
5160                       result)
5161                   (car arts)))
5162       (goto-char (gnus-data-pos result))
5163       (gnus-data-number result))))
5164
5165 (defun gnus-summary-find-subject (subject &optional unread backward article)
5166   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5167          (article (or article (gnus-summary-article-number)))
5168          (articles (gnus-data-list backward))
5169          (arts (gnus-data-find-list article articles))
5170          result)
5171     (when (or (not gnus-summary-check-current)
5172               (not unread)
5173               (not (gnus-data-unread-p (car arts))))
5174       (setq arts (cdr arts)))
5175     (while arts
5176       (and (or (not unread)
5177                (gnus-data-unread-p (car arts)))
5178            (vectorp (gnus-data-header (car arts)))
5179            (gnus-subject-equal
5180             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5181            (setq result (car arts)
5182                  arts nil))
5183       (setq arts (cdr arts)))
5184     (and result
5185          (goto-char (gnus-data-pos result))
5186          (gnus-data-number result))))
5187
5188 (defun gnus-summary-search-forward (&optional unread subject backward)
5189   "Search forward for an article.
5190 If UNREAD, look for unread articles.  If SUBJECT, look for
5191 articles with that subject.  If BACKWARD, search backward instead."
5192   (cond (subject (gnus-summary-find-subject subject unread backward))
5193         (backward (gnus-summary-find-prev unread))
5194         (t (gnus-summary-find-next unread))))
5195
5196 (defun gnus-recenter (&optional n)
5197   "Center point in window and redisplay frame.
5198 Also do horizontal recentering."
5199   (interactive "P")
5200   (when (and gnus-auto-center-summary
5201              (not (eq gnus-auto-center-summary 'vertical)))
5202     (gnus-horizontal-recenter))
5203   (recenter n))
5204
5205 (defun gnus-summary-recenter ()
5206   "Center point in the summary window.
5207 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5208 displayed, no centering will be performed."
5209   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5210   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5211   (interactive)
5212   (let* ((top (cond ((< (window-height) 4) 0)
5213                     ((< (window-height) 7) 1)
5214                     (t (if (numberp gnus-auto-center-summary)
5215                            gnus-auto-center-summary
5216                          2))))
5217          (height (1- (window-height)))
5218          (bottom (save-excursion (goto-char (point-max))
5219                                  (forward-line (- height))
5220                                  (point)))
5221          (window (get-buffer-window (current-buffer))))
5222     ;; The user has to want it.
5223     (when gnus-auto-center-summary
5224       (when (get-buffer-window gnus-article-buffer)
5225         ;; Only do recentering when the article buffer is displayed,
5226         ;; Set the window start to either `bottom', which is the biggest
5227         ;; possible valid number, or the second line from the top,
5228         ;; whichever is the least.
5229         (set-window-start
5230          window (min bottom (save-excursion
5231                               (forward-line (- top)) (point)))
5232          t))
5233       ;; Do horizontal recentering while we're at it.
5234       (when (and (get-buffer-window (current-buffer) t)
5235                  (not (eq gnus-auto-center-summary 'vertical)))
5236         (let ((selected (selected-window)))
5237           (select-window (get-buffer-window (current-buffer) t))
5238           (gnus-summary-position-point)
5239           (gnus-horizontal-recenter)
5240           (select-window selected))))))
5241
5242 (defun gnus-summary-jump-to-group (newsgroup)
5243   "Move point to NEWSGROUP in group mode buffer."
5244   ;; Keep update point of group mode buffer if visible.
5245   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5246       (save-window-excursion
5247         ;; Take care of tree window mode.
5248         (when (get-buffer-window gnus-group-buffer)
5249           (pop-to-buffer gnus-group-buffer))
5250         (gnus-group-jump-to-group newsgroup))
5251     (save-excursion
5252       ;; Take care of tree window mode.
5253       (if (get-buffer-window gnus-group-buffer)
5254           (pop-to-buffer gnus-group-buffer)
5255         (set-buffer gnus-group-buffer))
5256       (gnus-group-jump-to-group newsgroup))))
5257
5258 ;; This function returns a list of article numbers based on the
5259 ;; difference between the ranges of read articles in this group and
5260 ;; the range of active articles.
5261 (defun gnus-list-of-unread-articles (group)
5262   (let* ((read (gnus-info-read (gnus-get-info group)))
5263          (active (or (gnus-active group) (gnus-activate-group group)))
5264          (last (cdr active))
5265          first nlast unread)
5266     ;; If none are read, then all are unread.
5267     (if (not read)
5268         (setq first (car active))
5269       ;; If the range of read articles is a single range, then the
5270       ;; first unread article is the article after the last read
5271       ;; article.  Sounds logical, doesn't it?
5272       (if (and (not (listp (cdr read)))
5273                (or (< (car read) (car active))
5274                    (progn (setq read (list read))
5275                           nil)))
5276           (setq first (max (car active) (1+ (cdr read))))
5277         ;; `read' is a list of ranges.
5278         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5279                                   (caar read)))
5280                   1)
5281           (setq first (car active)))
5282         (while read
5283           (when first
5284             (while (< first nlast)
5285               (push first unread)
5286               (setq first (1+ first))))
5287           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5288           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5289           (setq read (cdr read)))))
5290     ;; And add the last unread articles.
5291     (while (<= first last)
5292       (push first unread)
5293       (setq first (1+ first)))
5294     ;; Return the list of unread articles.
5295     (delq 0 (nreverse unread))))
5296
5297 (defun gnus-list-of-read-articles (group)
5298   "Return a list of unread, unticked and non-dormant articles."
5299   (let* ((info (gnus-get-info group))
5300          (marked (gnus-info-marks info))
5301          (active (gnus-active group)))
5302     (and info active
5303          (gnus-set-difference
5304           (gnus-sorted-complement
5305            (gnus-uncompress-range active)
5306            (gnus-list-of-unread-articles group))
5307           (append
5308            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5309            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5310
5311 ;; Various summary commands
5312
5313 (defun gnus-summary-select-article-buffer ()
5314   "Reconfigure windows to show article buffer."
5315   (interactive)
5316   (if (not (gnus-buffer-live-p gnus-article-buffer))
5317       (error "There is no article buffer for this summary buffer")
5318     (gnus-configure-windows 'article)
5319     (select-window (get-buffer-window gnus-article-buffer))))
5320
5321 (defun gnus-summary-universal-argument (arg)
5322   "Perform any operation on all articles that are process/prefixed."
5323   (interactive "P")
5324   (let ((articles (gnus-summary-work-articles arg))
5325         func article)
5326     (if (eq
5327          (setq
5328           func
5329           (key-binding
5330            (read-key-sequence
5331             (substitute-command-keys
5332              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5333          'undefined)
5334         (gnus-error 1 "Undefined key")
5335       (save-excursion
5336         (while articles
5337           (gnus-summary-goto-subject (setq article (pop articles)))
5338           (let (gnus-newsgroup-processable)
5339             (command-execute func))
5340           (gnus-summary-remove-process-mark article)))))
5341   (gnus-summary-position-point))
5342
5343 (defun gnus-summary-toggle-truncation (&optional arg)
5344   "Toggle truncation of summary lines.
5345 With arg, turn line truncation on iff arg is positive."
5346   (interactive "P")
5347   (setq truncate-lines
5348         (if (null arg) (not truncate-lines)
5349           (> (prefix-numeric-value arg) 0)))
5350   (redraw-display))
5351
5352 (defun gnus-summary-reselect-current-group (&optional all rescan)
5353   "Exit and then reselect the current newsgroup.
5354 The prefix argument ALL means to select all articles."
5355   (interactive "P")
5356   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5357     (error "Ephemeral groups can't be reselected"))
5358   (let ((current-subject (gnus-summary-article-number))
5359         (group gnus-newsgroup-name))
5360     (setq gnus-newsgroup-begin nil)
5361     (gnus-summary-exit)
5362     ;; We have to adjust the point of group mode buffer because
5363     ;; point was moved to the next unread newsgroup by exiting.
5364     (gnus-summary-jump-to-group group)
5365     (when rescan
5366       (save-excursion
5367         (gnus-group-get-new-news-this-group 1)))
5368     (gnus-group-read-group all t)
5369     (gnus-summary-goto-subject current-subject nil t)))
5370
5371 (defun gnus-summary-rescan-group (&optional all)
5372   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5373   (interactive "P")
5374   (gnus-summary-reselect-current-group all t))
5375
5376 (defun gnus-summary-update-info (&optional non-destructive)
5377   (save-excursion
5378     (let ((group gnus-newsgroup-name))
5379       (when group
5380         (when gnus-newsgroup-kill-headers
5381           (setq gnus-newsgroup-killed
5382                 (gnus-compress-sequence
5383                  (nconc
5384                   (gnus-set-sorted-intersection
5385                    (gnus-uncompress-range gnus-newsgroup-killed)
5386                    (setq gnus-newsgroup-unselected
5387                          (sort gnus-newsgroup-unselected '<)))
5388                   (setq gnus-newsgroup-unreads
5389                         (sort gnus-newsgroup-unreads '<)))
5390                  t)))
5391         (unless (listp (cdr gnus-newsgroup-killed))
5392           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5393         (let ((headers gnus-newsgroup-headers))
5394           ;; Set the new ranges of read articles.
5395           (save-excursion
5396             (set-buffer gnus-group-buffer)
5397             (gnus-undo-force-boundary))
5398           (gnus-update-read-articles
5399            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5400           ;; Set the current article marks.
5401           (let ((gnus-newsgroup-scored
5402                  (if (and (not gnus-save-score)
5403                           (not non-destructive))
5404                      nil
5405                    gnus-newsgroup-scored)))
5406             (save-excursion
5407               (gnus-update-marks)))
5408           ;; Do the cross-ref thing.
5409           (when gnus-use-cross-reference
5410             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5411           ;; Do not switch windows but change the buffer to work.
5412           (set-buffer gnus-group-buffer)
5413           (unless (gnus-ephemeral-group-p group)
5414             (gnus-group-update-group group)))))))
5415
5416 (defun gnus-summary-save-newsrc (&optional force)
5417   "Save the current number of read/marked articles in the dribble buffer.
5418 The dribble buffer will then be saved.
5419 If FORCE (the prefix), also save the .newsrc file(s)."
5420   (interactive "P")
5421   (gnus-summary-update-info t)
5422   (if force
5423       (gnus-save-newsrc-file)
5424     (gnus-dribble-save)))
5425
5426 (defun gnus-summary-exit (&optional temporary)
5427   "Exit reading current newsgroup, and then return to group selection mode.
5428 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5429   (interactive)
5430   (gnus-set-global-variables)
5431   (when (gnus-buffer-live-p gnus-article-buffer)
5432     (save-excursion
5433       (set-buffer gnus-article-buffer)
5434       (mm-destroy-parts gnus-article-mime-handles)
5435       ;; Set it to nil for safety reason.
5436       (setq gnus-article-mime-handle-alist nil)
5437       (setq gnus-article-mime-handles nil)))
5438   (gnus-kill-save-kill-buffer)
5439   (gnus-async-halt-prefetch)
5440   (let* ((group gnus-newsgroup-name)
5441          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5442          (mode major-mode)
5443          (group-point nil)
5444          (buf (current-buffer)))
5445     (unless quit-config
5446       ;; Do adaptive scoring, and possibly save score files.
5447       (when gnus-newsgroup-adaptive
5448         (gnus-score-adaptive))
5449       (when gnus-use-scoring
5450         (gnus-score-save)))
5451     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5452     ;; If we have several article buffers, we kill them at exit.
5453     (unless gnus-single-article-buffer
5454       (gnus-kill-buffer gnus-original-article-buffer)
5455       (setq gnus-article-current nil))
5456     (when gnus-use-cache
5457       (gnus-cache-possibly-remove-articles)
5458       (gnus-cache-save-buffers))
5459     (gnus-async-prefetch-remove-group group)
5460     (when gnus-suppress-duplicates
5461       (gnus-dup-enter-articles))
5462     (when gnus-use-trees
5463       (gnus-tree-close group))
5464     (when gnus-use-cache
5465       (gnus-cache-write-active))
5466     ;; Remove entries for this group.
5467     (nnmail-purge-split-history (gnus-group-real-name group))
5468     ;; Make all changes in this group permanent.
5469     (unless quit-config
5470       (gnus-run-hooks 'gnus-exit-group-hook)
5471       (gnus-summary-update-info))
5472     (gnus-close-group group)
5473     ;; Make sure where we were, and go to next newsgroup.
5474     (set-buffer gnus-group-buffer)
5475     (unless quit-config
5476       (gnus-group-jump-to-group group))
5477     (gnus-run-hooks 'gnus-summary-exit-hook)
5478     (unless (or quit-config
5479                 ;; If this group has disappeared from the summary
5480                 ;; buffer, don't skip forwards.
5481                 (not (string= group (gnus-group-group-name))))
5482       (gnus-group-next-unread-group 1))
5483     (setq group-point (point))
5484     (if temporary
5485         nil                             ;Nothing to do.
5486       ;; If we have several article buffers, we kill them at exit.
5487       (unless gnus-single-article-buffer
5488         (gnus-kill-buffer gnus-article-buffer)
5489         (gnus-kill-buffer gnus-original-article-buffer)
5490         (setq gnus-article-current nil))
5491       (set-buffer buf)
5492       (if (not gnus-kill-summary-on-exit)
5493           (gnus-deaden-summary)
5494         ;; We set all buffer-local variables to nil.  It is unclear why
5495         ;; this is needed, but if we don't, buffer-local variables are
5496         ;; not garbage-collected, it seems.  This would the lead to en
5497         ;; ever-growing Emacs.
5498         (gnus-summary-clear-local-variables)
5499         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5500           (gnus-summary-clear-local-variables))
5501         (when (get-buffer gnus-article-buffer)
5502           (bury-buffer gnus-article-buffer))
5503         ;; We clear the global counterparts of the buffer-local
5504         ;; variables as well, just to be on the safe side.
5505         (set-buffer gnus-group-buffer)
5506         (gnus-summary-clear-local-variables)
5507         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5508           (gnus-summary-clear-local-variables))
5509         ;; Return to group mode buffer.
5510         (when (eq mode 'gnus-summary-mode)
5511           (gnus-kill-buffer buf)))
5512       (setq gnus-current-select-method gnus-select-method)
5513       (pop-to-buffer gnus-group-buffer)
5514       (if (not quit-config)
5515           (progn
5516             (goto-char group-point)
5517             (gnus-configure-windows 'group 'force))
5518         (gnus-handle-ephemeral-exit quit-config))
5519       ;; Clear the current group name.
5520       (unless quit-config
5521         (setq gnus-newsgroup-name nil)))))
5522
5523 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5524 (defun gnus-summary-exit-no-update (&optional no-questions)
5525   "Quit reading current newsgroup without updating read article info."
5526   (interactive)
5527   (let* ((group gnus-newsgroup-name)
5528          (quit-config (gnus-group-quit-config group)))
5529     (when (or no-questions
5530               gnus-expert-user
5531               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5532       (gnus-async-halt-prefetch)
5533       (mapcar 'funcall
5534               (delq 'gnus-summary-expire-articles
5535                     (copy-sequence gnus-summary-prepare-exit-hook)))
5536       (when (gnus-buffer-live-p gnus-article-buffer)
5537         (save-excursion
5538           (set-buffer gnus-article-buffer)
5539           (mm-destroy-parts gnus-article-mime-handles)
5540           ;; Set it to nil for safety reason.
5541           (setq gnus-article-mime-handle-alist nil)
5542           (setq gnus-article-mime-handles nil)))
5543       ;; If we have several article buffers, we kill them at exit.
5544       (unless gnus-single-article-buffer
5545         (gnus-kill-buffer gnus-article-buffer)
5546         (gnus-kill-buffer gnus-original-article-buffer)
5547         (setq gnus-article-current nil))
5548       (if (not gnus-kill-summary-on-exit)
5549           (gnus-deaden-summary)
5550         (gnus-close-group group)
5551         (gnus-summary-clear-local-variables)
5552         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5553           (gnus-summary-clear-local-variables))
5554         (set-buffer gnus-group-buffer)
5555         (gnus-summary-clear-local-variables)
5556         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5557           (gnus-summary-clear-local-variables))
5558         (when (get-buffer gnus-summary-buffer)
5559           (kill-buffer gnus-summary-buffer)))
5560       (unless gnus-single-article-buffer
5561         (setq gnus-article-current nil))
5562       (when gnus-use-trees
5563         (gnus-tree-close group))
5564       (gnus-async-prefetch-remove-group group)
5565       (when (get-buffer gnus-article-buffer)
5566         (bury-buffer gnus-article-buffer))
5567       ;; Return to the group buffer.
5568       (gnus-configure-windows 'group 'force)
5569       ;; Clear the current group name.
5570       (setq gnus-newsgroup-name nil)
5571       (when (equal (gnus-group-group-name) group)
5572         (gnus-group-next-unread-group 1))
5573       (when quit-config
5574         (gnus-handle-ephemeral-exit quit-config)))))
5575
5576 (defun gnus-handle-ephemeral-exit (quit-config)
5577   "Handle movement when leaving an ephemeral group.
5578 The state which existed when entering the ephemeral is reset."
5579   (if (not (buffer-name (car quit-config)))
5580       (gnus-configure-windows 'group 'force)
5581     (set-buffer (car quit-config))
5582     (cond ((eq major-mode 'gnus-summary-mode)
5583            (gnus-set-global-variables))
5584           ((eq major-mode 'gnus-article-mode)
5585            (save-excursion
5586              ;; The `gnus-summary-buffer' variable may point
5587              ;; to the old summary buffer when using a single
5588              ;; article buffer.
5589              (unless (gnus-buffer-live-p gnus-summary-buffer)
5590                (set-buffer gnus-group-buffer))
5591              (set-buffer gnus-summary-buffer)
5592              (gnus-set-global-variables))))
5593     (if (or (eq (cdr quit-config) 'article)
5594             (eq (cdr quit-config) 'pick))
5595         (progn
5596           ;; The current article may be from the ephemeral group
5597           ;; thus it is best that we reload this article
5598           (gnus-summary-show-article)
5599           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5600               (gnus-configure-windows 'pick 'force)
5601             (gnus-configure-windows (cdr quit-config) 'force)))
5602       (gnus-configure-windows (cdr quit-config) 'force))
5603     (when (eq major-mode 'gnus-summary-mode)
5604       (gnus-summary-next-subject 1 nil t)
5605       (gnus-summary-recenter)
5606       (gnus-summary-position-point))))
5607
5608 ;;; Dead summaries.
5609
5610 (defvar gnus-dead-summary-mode-map nil)
5611
5612 (unless gnus-dead-summary-mode-map
5613   (setq gnus-dead-summary-mode-map (make-keymap))
5614   (suppress-keymap gnus-dead-summary-mode-map)
5615   (substitute-key-definition
5616    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5617   (let ((keys '("\C-d" "\r" "\177" [delete])))
5618     (while keys
5619       (define-key gnus-dead-summary-mode-map
5620         (pop keys) 'gnus-summary-wake-up-the-dead))))
5621
5622 (defvar gnus-dead-summary-mode nil
5623   "Minor mode for Gnus summary buffers.")
5624
5625 (defun gnus-dead-summary-mode (&optional arg)
5626   "Minor mode for Gnus summary buffers."
5627   (interactive "P")
5628   (when (eq major-mode 'gnus-summary-mode)
5629     (make-local-variable 'gnus-dead-summary-mode)
5630     (setq gnus-dead-summary-mode
5631           (if (null arg) (not gnus-dead-summary-mode)
5632             (> (prefix-numeric-value arg) 0)))
5633     (when gnus-dead-summary-mode
5634       (gnus-add-minor-mode
5635        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5636
5637 (defun gnus-deaden-summary ()
5638   "Make the current summary buffer into a dead summary buffer."
5639   ;; Kill any previous dead summary buffer.
5640   (when (and gnus-dead-summary
5641              (buffer-name gnus-dead-summary))
5642     (save-excursion
5643       (set-buffer gnus-dead-summary)
5644       (when gnus-dead-summary-mode
5645         (kill-buffer (current-buffer)))))
5646   ;; Make this the current dead summary.
5647   (setq gnus-dead-summary (current-buffer))
5648   (gnus-dead-summary-mode 1)
5649   (let ((name (buffer-name)))
5650     (when (string-match "Summary" name)
5651       (rename-buffer
5652        (concat (substring name 0 (match-beginning 0)) "Dead "
5653                (substring name (match-beginning 0)))
5654        t)
5655       (bury-buffer))))
5656
5657 (defun gnus-kill-or-deaden-summary (buffer)
5658   "Kill or deaden the summary BUFFER."
5659   (save-excursion
5660     (when (and (buffer-name buffer)
5661                (not gnus-single-article-buffer))
5662       (save-excursion
5663         (set-buffer buffer)
5664         (gnus-kill-buffer gnus-article-buffer)
5665         (gnus-kill-buffer gnus-original-article-buffer)))
5666     (cond (gnus-kill-summary-on-exit
5667            (when (and gnus-use-trees
5668                       (gnus-buffer-exists-p buffer))
5669              (save-excursion
5670                (set-buffer buffer)
5671                (gnus-tree-close gnus-newsgroup-name)))
5672            (gnus-kill-buffer buffer))
5673           ((gnus-buffer-exists-p buffer)
5674            (save-excursion
5675              (set-buffer buffer)
5676              (gnus-deaden-summary))))))
5677
5678 (defun gnus-summary-wake-up-the-dead (&rest args)
5679   "Wake up the dead summary buffer."
5680   (interactive)
5681   (gnus-dead-summary-mode -1)
5682   (let ((name (buffer-name)))
5683     (when (string-match "Dead " name)
5684       (rename-buffer
5685        (concat (substring name 0 (match-beginning 0))
5686                (substring name (match-end 0)))
5687        t)))
5688   (gnus-message 3 "This dead summary is now alive again"))
5689
5690 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5691 (defun gnus-summary-fetch-faq (&optional faq-dir)
5692   "Fetch the FAQ for the current group.
5693 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5694 in."
5695   (interactive
5696    (list
5697     (when current-prefix-arg
5698       (completing-read
5699        "Faq dir: " (and (listp gnus-group-faq-directory)
5700                         (mapcar (lambda (file) (list file))
5701                                 gnus-group-faq-directory))))))
5702   (let (gnus-faq-buffer)
5703     (when (setq gnus-faq-buffer
5704                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5705       (gnus-configure-windows 'summary-faq))))
5706
5707 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5708 (defun gnus-summary-describe-group (&optional force)
5709   "Describe the current newsgroup."
5710   (interactive "P")
5711   (gnus-group-describe-group force gnus-newsgroup-name))
5712
5713 (defun gnus-summary-describe-briefly ()
5714   "Describe summary mode commands briefly."
5715   (interactive)
5716   (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")))
5717
5718 ;; Walking around group mode buffer from summary mode.
5719
5720 (defun gnus-summary-next-group (&optional no-article target-group backward)
5721   "Exit current newsgroup and then select next unread newsgroup.
5722 If prefix argument NO-ARTICLE is non-nil, no article is selected
5723 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5724 previous group instead."
5725   (interactive "P")
5726   ;; Stop pre-fetching.
5727   (gnus-async-halt-prefetch)
5728   (let ((current-group gnus-newsgroup-name)
5729         (current-buffer (current-buffer))
5730         entered)
5731     ;; First we semi-exit this group to update Xrefs and all variables.
5732     ;; We can't do a real exit, because the window conf must remain
5733     ;; the same in case the user is prompted for info, and we don't
5734     ;; want the window conf to change before that...
5735     (gnus-summary-exit t)
5736     (while (not entered)
5737       ;; Then we find what group we are supposed to enter.
5738       (set-buffer gnus-group-buffer)
5739       (gnus-group-jump-to-group current-group)
5740       (setq target-group
5741             (or target-group
5742                 (if (eq gnus-keep-same-level 'best)
5743                     (gnus-summary-best-group gnus-newsgroup-name)
5744                   (gnus-summary-search-group backward gnus-keep-same-level))))
5745       (if (not target-group)
5746           ;; There are no further groups, so we return to the group
5747           ;; buffer.
5748           (progn
5749             (gnus-message 5 "Returning to the group buffer")
5750             (setq entered t)
5751             (when (gnus-buffer-live-p current-buffer)
5752               (set-buffer current-buffer)
5753               (gnus-summary-exit))
5754             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5755         ;; We try to enter the target group.
5756         (gnus-group-jump-to-group target-group)
5757         (let ((unreads (gnus-group-group-unread)))
5758           (if (and (or (eq t unreads)
5759                        (and unreads (not (zerop unreads))))
5760                    (gnus-summary-read-group
5761                     target-group nil no-article
5762                     (and (buffer-name current-buffer) current-buffer)
5763                     nil backward))
5764               (setq entered t)
5765             (setq current-group target-group
5766                   target-group nil)))))))
5767
5768 (defun gnus-summary-prev-group (&optional no-article)
5769   "Exit current newsgroup and then select previous unread newsgroup.
5770 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5771   (interactive "P")
5772   (gnus-summary-next-group no-article nil t))
5773
5774 ;; Walking around summary lines.
5775
5776 (defun gnus-summary-first-subject (&optional unread undownloaded)
5777   "Go to the first unread subject.
5778 If UNREAD is non-nil, go to the first unread article.
5779 Returns the article selected or nil if there are no unread articles."
5780   (interactive "P")
5781   (prog1
5782       (cond
5783        ;; Empty summary.
5784        ((null gnus-newsgroup-data)
5785         (gnus-message 3 "No articles in the group")
5786         nil)
5787        ;; Pick the first article.
5788        ((not unread)
5789         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5790         (gnus-data-number (car gnus-newsgroup-data)))
5791        ;; No unread articles.
5792        ((null gnus-newsgroup-unreads)
5793         (gnus-message 3 "No more unread articles")
5794         nil)
5795        ;; Find the first unread article.
5796        (t
5797         (let ((data gnus-newsgroup-data))
5798           (while (and data
5799                       (and (not (and undownloaded
5800                                      (eq gnus-undownloaded-mark
5801                                          (gnus-data-mark (car data)))))
5802                            (not (gnus-data-unread-p (car data)))))
5803             (setq data (cdr data)))
5804           (when data
5805             (goto-char (gnus-data-pos (car data)))
5806             (gnus-data-number (car data))))))
5807     (gnus-summary-position-point)))
5808
5809 (defun gnus-summary-next-subject (n &optional unread dont-display)
5810   "Go to next N'th summary line.
5811 If N is negative, go to the previous N'th subject line.
5812 If UNREAD is non-nil, only unread articles are selected.
5813 The difference between N and the actual number of steps taken is
5814 returned."
5815   (interactive "p")
5816   (let ((backward (< n 0))
5817         (n (abs n)))
5818     (while (and (> n 0)
5819                 (if backward
5820                     (gnus-summary-find-prev unread)
5821                   (gnus-summary-find-next unread)))
5822       (unless (zerop (setq n (1- n)))
5823         (gnus-summary-show-thread)))
5824     (when (/= 0 n)
5825       (gnus-message 7 "No more%s articles"
5826                     (if unread " unread" "")))
5827     (unless dont-display
5828       (gnus-summary-recenter)
5829       (gnus-summary-position-point))
5830     n))
5831
5832 (defun gnus-summary-next-unread-subject (n)
5833   "Go to next N'th unread summary line."
5834   (interactive "p")
5835   (gnus-summary-next-subject n t))
5836
5837 (defun gnus-summary-prev-subject (n &optional unread)
5838   "Go to previous N'th summary line.
5839 If optional argument UNREAD is non-nil, only unread article is selected."
5840   (interactive "p")
5841   (gnus-summary-next-subject (- n) unread))
5842
5843 (defun gnus-summary-prev-unread-subject (n)
5844   "Go to previous N'th unread summary line."
5845   (interactive "p")
5846   (gnus-summary-next-subject (- n) t))
5847
5848 (defun gnus-summary-goto-subject (article &optional force silent)
5849   "Go the subject line of ARTICLE.
5850 If FORCE, also allow jumping to articles not currently shown."
5851   (interactive "nArticle number: ")
5852   (let ((b (point))
5853         (data (gnus-data-find article)))
5854     ;; We read in the article if we have to.
5855     (and (not data)
5856          force
5857          (gnus-summary-insert-subject
5858           article
5859           (if (or (numberp force) (vectorp force)) force)
5860           t)
5861          (setq data (gnus-data-find article)))
5862     (goto-char b)
5863     (if (not data)
5864         (progn
5865           (unless silent
5866             (gnus-message 3 "Can't find article %d" article))
5867           nil)
5868       (goto-char (gnus-data-pos data))
5869       (gnus-summary-position-point)
5870       article)))
5871
5872 ;; Walking around summary lines with displaying articles.
5873
5874 (defun gnus-summary-expand-window (&optional arg)
5875   "Make the summary buffer take up the entire Emacs frame.
5876 Given a prefix, will force an `article' buffer configuration."
5877   (interactive "P")
5878   (if arg
5879       (gnus-configure-windows 'article 'force)
5880     (gnus-configure-windows 'summary 'force)))
5881
5882 (defun gnus-summary-display-article (article &optional all-header)
5883   "Display ARTICLE in article buffer."
5884   (gnus-set-global-variables)
5885   (when (gnus-buffer-live-p gnus-article-buffer)
5886     (with-current-buffer gnus-article-buffer
5887       (setq gnus-article-charset gnus-newsgroup-charset)
5888       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
5889       (mm-enable-multibyte-mule4)))
5890   (if (null article)
5891       nil
5892     (prog1
5893         (if gnus-summary-display-article-function
5894             (funcall gnus-summary-display-article-function article all-header)
5895           (gnus-article-prepare article all-header))
5896       (gnus-run-hooks 'gnus-select-article-hook)
5897       (when (and gnus-current-article
5898                  (not (zerop gnus-current-article)))
5899         (gnus-summary-goto-subject gnus-current-article))
5900       (gnus-summary-recenter)
5901       (when (and gnus-use-trees gnus-show-threads)
5902         (gnus-possibly-generate-tree article)
5903         (gnus-highlight-selected-tree article))
5904       ;; Successfully display article.
5905       (gnus-article-set-window-start
5906        (cdr (assq article gnus-newsgroup-bookmarks))))))
5907
5908 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5909   "Select the current article.
5910 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5911 non-nil, the article will be re-fetched even if it already present in
5912 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5913 be displayed."
5914   ;; Make sure we are in the summary buffer to work around bbdb bug.
5915   (unless (eq major-mode 'gnus-summary-mode)
5916     (set-buffer gnus-summary-buffer))
5917   (let ((article (or article (gnus-summary-article-number)))
5918         (all-headers (not (not all-headers))) ;Must be T or NIL.
5919         gnus-summary-display-article-function)
5920     (and (not pseudo)
5921          (gnus-summary-article-pseudo-p article)
5922          (error "This is a pseudo-article"))
5923     (save-excursion
5924       (set-buffer gnus-summary-buffer)
5925       (if (or (and gnus-single-article-buffer
5926                    (or (null gnus-current-article)
5927                        (null gnus-article-current)
5928                        (null (get-buffer gnus-article-buffer))
5929                        (not (eq article (cdr gnus-article-current)))
5930                        (not (equal (car gnus-article-current)
5931                                    gnus-newsgroup-name))))
5932               (and (not gnus-single-article-buffer)
5933                    (or (null gnus-current-article)
5934                        (not (eq gnus-current-article article))))
5935               force)
5936           ;; The requested article is different from the current article.
5937           (progn
5938             (gnus-summary-display-article article all-headers)
5939             (when (gnus-buffer-live-p gnus-article-buffer)
5940                (with-current-buffer gnus-article-buffer
5941                 (if (not gnus-article-decoded-p) ;; a local variable
5942                     (mm-disable-multibyte-mule4))))
5943             (when (or all-headers gnus-show-all-headers)
5944               (gnus-article-show-all-headers))
5945             (gnus-article-set-window-start
5946              (cdr (assq article gnus-newsgroup-bookmarks)))
5947             article)
5948         (when (or all-headers gnus-show-all-headers)
5949           (gnus-article-show-all-headers))
5950         'old))))
5951
5952 (defun gnus-summary-force-verify-and-decrypt ()
5953   (interactive)
5954   (let ((mm-verify-option 'known)
5955         (mm-decrypt-option 'known))
5956     (gnus-summary-select-article nil 'force)))
5957
5958 (defun gnus-summary-set-current-mark (&optional current-mark)
5959   "Obsolete function."
5960   nil)
5961
5962 (defun gnus-summary-next-article (&optional unread subject backward push)
5963   "Select the next article.
5964 If UNREAD, only unread articles are selected.
5965 If SUBJECT, only articles with SUBJECT are selected.
5966 If BACKWARD, the previous article is selected instead of the next."
5967   (interactive "P")
5968   (cond
5969    ;; Is there such an article?
5970    ((and (gnus-summary-search-forward unread subject backward)
5971          (or (gnus-summary-display-article (gnus-summary-article-number))
5972              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5973     (gnus-summary-position-point))
5974    ;; If not, we try the first unread, if that is wanted.
5975    ((and subject
5976          gnus-auto-select-same
5977          (gnus-summary-first-unread-article))
5978     (gnus-summary-position-point)
5979     (gnus-message 6 "Wrapped"))
5980    ;; Try to get next/previous article not displayed in this group.
5981    ((and gnus-auto-extend-newsgroup
5982          (not unread) (not subject))
5983     (gnus-summary-goto-article
5984      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5985      nil (count-lines (point-min) (point))))
5986    ;; Go to next/previous group.
5987    (t
5988     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5989       (gnus-summary-jump-to-group gnus-newsgroup-name))
5990     (let ((cmd last-command-char)
5991           (point
5992            (save-excursion
5993              (set-buffer gnus-group-buffer)
5994              (point)))
5995           (group
5996            (if (eq gnus-keep-same-level 'best)
5997                (gnus-summary-best-group gnus-newsgroup-name)
5998              (gnus-summary-search-group backward gnus-keep-same-level))))
5999       ;; For some reason, the group window gets selected.  We change
6000       ;; it back.
6001       (select-window (get-buffer-window (current-buffer)))
6002       ;; Select next unread newsgroup automagically.
6003       (cond
6004        ((or (not gnus-auto-select-next)
6005             (not cmd))
6006         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6007        ((or (eq gnus-auto-select-next 'quietly)
6008             (and (eq gnus-auto-select-next 'slightly-quietly)
6009                  push)
6010             (and (eq gnus-auto-select-next 'almost-quietly)
6011                  (gnus-summary-last-article-p)))
6012         ;; Select quietly.
6013         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6014             (gnus-summary-exit)
6015           (gnus-message 7 "No more%s articles (%s)..."
6016                         (if unread " unread" "")
6017                         (if group (concat "selecting " group)
6018                           "exiting"))
6019           (gnus-summary-next-group nil group backward)))
6020        (t
6021         (when (gnus-key-press-event-p last-input-event)
6022           (gnus-summary-walk-group-buffer
6023            gnus-newsgroup-name cmd unread backward point))))))))
6024
6025 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6026   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6027                       (?\C-p (gnus-group-prev-unread-group 1))))
6028         (cursor-in-echo-area t)
6029         keve key group ended)
6030     (save-excursion
6031       (set-buffer gnus-group-buffer)
6032       (goto-char start)
6033       (setq group
6034             (if (eq gnus-keep-same-level 'best)
6035                 (gnus-summary-best-group gnus-newsgroup-name)
6036               (gnus-summary-search-group backward gnus-keep-same-level))))
6037     (while (not ended)
6038       (gnus-message
6039        5 "No more%s articles%s" (if unread " unread" "")
6040        (if (and group
6041                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6042            (format " (Type %s for %s [%s])"
6043                    (single-key-description cmd) group
6044                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6045          (format " (Type %s to exit %s)"
6046                  (single-key-description cmd)
6047                  gnus-newsgroup-name)))
6048       ;; Confirm auto selection.
6049       (setq key (car (setq keve (gnus-read-event-char))))
6050       (setq ended t)
6051       (cond
6052        ((assq key keystrokes)
6053         (let ((obuf (current-buffer)))
6054           (switch-to-buffer gnus-group-buffer)
6055           (when group
6056             (gnus-group-jump-to-group group))
6057           (eval (cadr (assq key keystrokes)))
6058           (setq group (gnus-group-group-name))
6059           (switch-to-buffer obuf))
6060         (setq ended nil))
6061        ((equal key cmd)
6062         (if (or (not group)
6063                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6064             (gnus-summary-exit)
6065           (gnus-summary-next-group nil group backward)))
6066        (t
6067         (push (cdr keve) unread-command-events))))))
6068
6069 (defun gnus-summary-next-unread-article ()
6070   "Select unread article after current one."
6071   (interactive)
6072   (gnus-summary-next-article
6073    (or (not (eq gnus-summary-goto-unread 'never))
6074        (gnus-summary-last-article-p (gnus-summary-article-number)))
6075    (and gnus-auto-select-same
6076         (gnus-summary-article-subject))))
6077
6078 (defun gnus-summary-prev-article (&optional unread subject)
6079   "Select the article after the current one.
6080 If UNREAD is non-nil, only unread articles are selected."
6081   (interactive "P")
6082   (gnus-summary-next-article unread subject t))
6083
6084 (defun gnus-summary-prev-unread-article ()
6085   "Select unread article before current one."
6086   (interactive)
6087   (gnus-summary-prev-article
6088    (or (not (eq gnus-summary-goto-unread 'never))
6089        (gnus-summary-first-article-p (gnus-summary-article-number)))
6090    (and gnus-auto-select-same
6091         (gnus-summary-article-subject))))
6092
6093 (defun gnus-summary-next-page (&optional lines circular)
6094   "Show next page of the selected article.
6095 If at the end of the current article, select the next article.
6096 LINES says how many lines should be scrolled up.
6097
6098 If CIRCULAR is non-nil, go to the start of the article instead of
6099 selecting the next article when reaching the end of the current
6100 article."
6101   (interactive "P")
6102   (setq gnus-summary-buffer (current-buffer))
6103   (gnus-set-global-variables)
6104   (let ((article (gnus-summary-article-number))
6105         (article-window (get-buffer-window gnus-article-buffer t))
6106         endp)
6107     ;; If the buffer is empty, we have no article.
6108     (unless article
6109       (error "No article to select"))
6110     (gnus-configure-windows 'article)
6111     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6112         (if (and (eq gnus-summary-goto-unread 'never)
6113                  (not (gnus-summary-last-article-p article)))
6114             (gnus-summary-next-article)
6115           (gnus-summary-next-unread-article))
6116       (if (or (null gnus-current-article)
6117               (null gnus-article-current)
6118               (/= article (cdr gnus-article-current))
6119               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6120           ;; Selected subject is different from current article's.
6121           (gnus-summary-display-article article)
6122         (when article-window
6123           (gnus-eval-in-buffer-window gnus-article-buffer
6124             (setq endp (gnus-article-next-page lines)))
6125           (when endp
6126             (cond (circular
6127                    (gnus-summary-beginning-of-article))
6128                   (lines
6129                    (gnus-message 3 "End of message"))
6130                   ((null lines)
6131                    (if (and (eq gnus-summary-goto-unread 'never)
6132                             (not (gnus-summary-last-article-p article)))
6133                        (gnus-summary-next-article)
6134                      (gnus-summary-next-unread-article))))))))
6135     (gnus-summary-recenter)
6136     (gnus-summary-position-point)))
6137
6138 (defun gnus-summary-prev-page (&optional lines move)
6139   "Show previous page of selected article.
6140 Argument LINES specifies lines to be scrolled down.
6141 If MOVE, move to the previous unread article if point is at
6142 the beginning of the buffer."
6143   (interactive "P")
6144   (let ((article (gnus-summary-article-number))
6145         (article-window (get-buffer-window gnus-article-buffer t))
6146         endp)
6147     (gnus-configure-windows 'article)
6148     (if (or (null gnus-current-article)
6149             (null gnus-article-current)
6150             (/= article (cdr gnus-article-current))
6151             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6152         ;; Selected subject is different from current article's.
6153         (gnus-summary-display-article article)
6154       (gnus-summary-recenter)
6155       (when article-window
6156         (gnus-eval-in-buffer-window gnus-article-buffer
6157           (setq endp (gnus-article-prev-page lines)))
6158         (when (and move endp)
6159           (cond (lines
6160                  (gnus-message 3 "Beginning of message"))
6161                 ((null lines)
6162                  (if (and (eq gnus-summary-goto-unread 'never)
6163                           (not (gnus-summary-first-article-p article)))
6164                      (gnus-summary-prev-article)
6165                    (gnus-summary-prev-unread-article))))))))
6166   (gnus-summary-position-point))
6167
6168 (defun gnus-summary-prev-page-or-article (&optional lines)
6169   "Show previous page of selected article.
6170 Argument LINES specifies lines to be scrolled down.
6171 If at the beginning of the article, go to the next article."
6172   (interactive "P")
6173   (gnus-summary-prev-page lines t))
6174
6175 (defun gnus-summary-scroll-up (lines)
6176   "Scroll up (or down) one line current article.
6177 Argument LINES specifies lines to be scrolled up (or down if negative)."
6178   (interactive "p")
6179   (gnus-configure-windows 'article)
6180   (gnus-summary-show-thread)
6181   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6182     (gnus-eval-in-buffer-window gnus-article-buffer
6183       (cond ((> lines 0)
6184              (when (gnus-article-next-page lines)
6185                (gnus-message 3 "End of message")))
6186             ((< lines 0)
6187              (gnus-article-prev-page (- lines))))))
6188   (gnus-summary-recenter)
6189   (gnus-summary-position-point))
6190
6191 (defun gnus-summary-scroll-down (lines)
6192   "Scroll down (or up) one line current article.
6193 Argument LINES specifies lines to be scrolled down (or up if negative)."
6194   (interactive "p")
6195   (gnus-summary-scroll-up (- lines)))
6196
6197 (defun gnus-summary-next-same-subject ()
6198   "Select next article which has the same subject as current one."
6199   (interactive)
6200   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6201
6202 (defun gnus-summary-prev-same-subject ()
6203   "Select previous article which has the same subject as current one."
6204   (interactive)
6205   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6206
6207 (defun gnus-summary-next-unread-same-subject ()
6208   "Select next unread article which has the same subject as current one."
6209   (interactive)
6210   (gnus-summary-next-article t (gnus-summary-article-subject)))
6211
6212 (defun gnus-summary-prev-unread-same-subject ()
6213   "Select previous unread article which has the same subject as current one."
6214   (interactive)
6215   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6216
6217 (defun gnus-summary-first-unread-article ()
6218   "Select the first unread article.
6219 Return nil if there are no unread articles."
6220   (interactive)
6221   (prog1
6222       (when (gnus-summary-first-subject t)
6223         (gnus-summary-show-thread)
6224         (gnus-summary-first-subject t)
6225         (gnus-summary-display-article (gnus-summary-article-number)))
6226     (gnus-summary-position-point)))
6227
6228 (defun gnus-summary-first-unread-subject ()
6229   "Place the point on the subject line of the first unread article.
6230 Return nil if there are no unread articles."
6231   (interactive)
6232   (prog1
6233       (when (gnus-summary-first-subject t)
6234         (gnus-summary-show-thread)
6235         (gnus-summary-first-subject t))
6236     (gnus-summary-position-point)))
6237
6238 (defun gnus-summary-first-article ()
6239   "Select the first article.
6240 Return nil if there are no articles."
6241   (interactive)
6242   (prog1
6243       (when (gnus-summary-first-subject)
6244         (gnus-summary-show-thread)
6245         (gnus-summary-first-subject)
6246         (gnus-summary-display-article (gnus-summary-article-number)))
6247     (gnus-summary-position-point)))
6248
6249 (defun gnus-summary-best-unread-article ()
6250   "Select the unread article with the highest score."
6251   (interactive)
6252   (let ((best -1000000)
6253         (data gnus-newsgroup-data)
6254         article score)
6255     (while data
6256       (and (gnus-data-unread-p (car data))
6257            (> (setq score
6258                     (gnus-summary-article-score (gnus-data-number (car data))))
6259               best)
6260            (setq best score
6261                  article (gnus-data-number (car data))))
6262       (setq data (cdr data)))
6263     (prog1
6264         (if article
6265             (gnus-summary-goto-article article)
6266           (error "No unread articles"))
6267       (gnus-summary-position-point))))
6268
6269 (defun gnus-summary-last-subject ()
6270   "Go to the last displayed subject line in the group."
6271   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6272     (when article
6273       (gnus-summary-goto-subject article))))
6274
6275 (defun gnus-summary-goto-article (article &optional all-headers force)
6276   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6277 If ALL-HEADERS is non-nil, no header lines are hidden.
6278 If FORCE, go to the article even if it isn't displayed.  If FORCE
6279 is a number, it is the line the article is to be displayed on."
6280   (interactive
6281    (list
6282     (completing-read
6283      "Article number or Message-ID: "
6284      (mapcar (lambda (number) (list (int-to-string number)))
6285              gnus-newsgroup-limit))
6286     current-prefix-arg
6287     t))
6288   (prog1
6289       (if (and (stringp article)
6290                (string-match "@" article))
6291           (gnus-summary-refer-article article)
6292         (when (stringp article)
6293           (setq article (string-to-number article)))
6294         (if (gnus-summary-goto-subject article force)
6295             (gnus-summary-display-article article all-headers)
6296           (gnus-message 4 "Couldn't go to article %s" article) nil))
6297     (gnus-summary-position-point)))
6298
6299 (defun gnus-summary-goto-last-article ()
6300   "Go to the previously read article."
6301   (interactive)
6302   (prog1
6303       (when gnus-last-article
6304         (gnus-summary-goto-article gnus-last-article nil t))
6305     (gnus-summary-position-point)))
6306
6307 (defun gnus-summary-pop-article (number)
6308   "Pop one article off the history and go to the previous.
6309 NUMBER articles will be popped off."
6310   (interactive "p")
6311   (let (to)
6312     (setq gnus-newsgroup-history
6313           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6314     (if to
6315         (gnus-summary-goto-article (car to) nil t)
6316       (error "Article history empty")))
6317   (gnus-summary-position-point))
6318
6319 ;; Summary commands and functions for limiting the summary buffer.
6320
6321 (defun gnus-summary-limit-to-articles (n)
6322   "Limit the summary buffer to the next N articles.
6323 If not given a prefix, use the process marked articles instead."
6324   (interactive "P")
6325   (prog1
6326       (let ((articles (gnus-summary-work-articles n)))
6327         (setq gnus-newsgroup-processable nil)
6328         (gnus-summary-limit articles))
6329     (gnus-summary-position-point)))
6330
6331 (defun gnus-summary-pop-limit (&optional total)
6332   "Restore the previous limit.
6333 If given a prefix, remove all limits."
6334   (interactive "P")
6335   (when total
6336     (setq gnus-newsgroup-limits
6337           (list (mapcar (lambda (h) (mail-header-number h))
6338                         gnus-newsgroup-headers))))
6339   (unless gnus-newsgroup-limits
6340     (error "No limit to pop"))
6341   (prog1
6342       (gnus-summary-limit nil 'pop)
6343     (gnus-summary-position-point)))
6344
6345 (defun gnus-summary-limit-to-subject (subject &optional header)
6346   "Limit the summary buffer to articles that have subjects that match a regexp."
6347   (interactive "sLimit to subject (regexp): ")
6348   (unless header
6349     (setq header "subject"))
6350   (when (not (equal "" subject))
6351     (prog1
6352         (let ((articles (gnus-summary-find-matching
6353                          (or header "subject") subject 'all)))
6354           (unless articles
6355             (error "Found no matches for \"%s\"" subject))
6356           (gnus-summary-limit articles))
6357       (gnus-summary-position-point))))
6358
6359 (defun gnus-summary-limit-to-author (from)
6360   "Limit the summary buffer to articles that have authors that match a regexp."
6361   (interactive "sLimit to author (regexp): ")
6362   (gnus-summary-limit-to-subject from "from"))
6363
6364 (defun gnus-summary-limit-to-age (age &optional younger-p)
6365   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6366 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6367 articles that are younger than AGE days."
6368   (interactive
6369    (let ((younger current-prefix-arg)
6370          (days-got nil)
6371          days)
6372      (while (not days-got)
6373        (setq days (if younger
6374                       (read-string "Limit to articles within (in days): ")
6375                     (read-string "Limit to articles old than (in days): ")))
6376        (when (> (length days) 0)
6377          (setq days (read days)))
6378        (if (numberp days)
6379            (setq days-got t)
6380          (message "Please enter a number.")
6381          (sleep-for 1)))
6382      (list days younger)))
6383   (prog1
6384       (let ((data gnus-newsgroup-data)
6385             (cutoff (days-to-time age))
6386             articles d date is-younger)
6387         (while (setq d (pop data))
6388           (when (and (vectorp (gnus-data-header d))
6389                      (setq date (mail-header-date (gnus-data-header d))))
6390             (setq is-younger (time-less-p
6391                               (time-since (condition-case ()
6392                                               (date-to-time date)
6393                                             (error '(0 0))))
6394                               cutoff))
6395             (when (if younger-p
6396                       is-younger
6397                     (not is-younger))
6398               (push (gnus-data-number d) articles))))
6399         (gnus-summary-limit (nreverse articles)))
6400     (gnus-summary-position-point)))
6401
6402 (defun gnus-summary-limit-to-extra (header regexp)
6403   "Limit the summary buffer to articles that match an 'extra' header."
6404   (interactive
6405    (let ((header
6406           (intern
6407            (gnus-completing-read
6408             (symbol-name (car gnus-extra-headers))
6409             "Limit extra header:"
6410             (mapcar (lambda (x)
6411                       (cons (symbol-name x) x))
6412                     gnus-extra-headers)
6413             nil
6414             t))))
6415      (list header
6416            (read-string (format "Limit to header %s (regexp): " header)))))
6417   (when (not (equal "" regexp))
6418     (prog1
6419         (let ((articles (gnus-summary-find-matching
6420                          (cons 'extra header) regexp 'all)))
6421           (unless articles
6422             (error "Found no matches for \"%s\"" regexp))
6423           (gnus-summary-limit articles))
6424       (gnus-summary-position-point))))
6425
6426 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6427 (make-obsolete
6428  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6429
6430 (defun gnus-summary-limit-to-unread (&optional all)
6431   "Limit the summary buffer to articles that are not marked as read.
6432 If ALL is non-nil, limit strictly to unread articles."
6433   (interactive "P")
6434   (if all
6435       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6436     (gnus-summary-limit-to-marks
6437      ;; Concat all the marks that say that an article is read and have
6438      ;; those removed.
6439      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6440            gnus-killed-mark gnus-kill-file-mark
6441            gnus-low-score-mark gnus-expirable-mark
6442            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6443            gnus-duplicate-mark gnus-souped-mark)
6444      'reverse)))
6445
6446 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6447 (make-obsolete 'gnus-summary-delete-marked-with
6448                'gnus-summary-limit-exlude-marks)
6449
6450 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6451   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6452 If REVERSE, limit the summary buffer to articles that are marked
6453 with MARKS.  MARKS can either be a string of marks or a list of marks.
6454 Returns how many articles were removed."
6455   (interactive "sMarks: ")
6456   (gnus-summary-limit-to-marks marks t))
6457
6458 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6459   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6460 If REVERSE (the prefix), limit the summary buffer to articles that are
6461 not marked with MARKS.  MARKS can either be a string of marks or a
6462 list of marks.
6463 Returns how many articles were removed."
6464   (interactive "sMarks: \nP")
6465   (prog1
6466       (let ((data gnus-newsgroup-data)
6467             (marks (if (listp marks) marks
6468                      (append marks nil))) ; Transform to list.
6469             articles)
6470         (while data
6471           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6472                   (memq (gnus-data-mark (car data)) marks))
6473             (push (gnus-data-number (car data)) articles))
6474           (setq data (cdr data)))
6475         (gnus-summary-limit articles))
6476     (gnus-summary-position-point)))
6477
6478 (defun gnus-summary-limit-to-score (&optional score)
6479   "Limit to articles with score at or above SCORE."
6480   (interactive "P")
6481   (setq score (if score
6482                   (prefix-numeric-value score)
6483                 (or gnus-summary-default-score 0)))
6484   (let ((data gnus-newsgroup-data)
6485         articles)
6486     (while data
6487       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6488                 score)
6489         (push (gnus-data-number (car data)) articles))
6490       (setq data (cdr data)))
6491     (prog1
6492         (gnus-summary-limit articles)
6493       (gnus-summary-position-point))))
6494
6495 (defun gnus-summary-limit-include-thread (id)
6496   "Display all the hidden articles that in the current thread."
6497   (interactive (list (mail-header-id (gnus-summary-article-header))))
6498   (let ((articles (gnus-articles-in-thread
6499                    (gnus-id-to-thread (gnus-root-id id)))))
6500     (prog1
6501         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6502       (gnus-summary-position-point))))
6503
6504 (defun gnus-summary-limit-include-dormant ()
6505   "Display all the hidden articles that are marked as dormant.
6506 Note that this command only works on a subset of the articles currently
6507 fetched for this group."
6508   (interactive)
6509   (unless gnus-newsgroup-dormant
6510     (error "There are no dormant articles in this group"))
6511   (prog1
6512       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6513     (gnus-summary-position-point)))
6514
6515 (defun gnus-summary-limit-exclude-dormant ()
6516   "Hide all dormant articles."
6517   (interactive)
6518   (prog1
6519       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6520     (gnus-summary-position-point)))
6521
6522 (defun gnus-summary-limit-exclude-childless-dormant ()
6523   "Hide all dormant articles that have no children."
6524   (interactive)
6525   (let ((data (gnus-data-list t))
6526         articles d children)
6527     ;; Find all articles that are either not dormant or have
6528     ;; children.
6529     (while (setq d (pop data))
6530       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6531                 (and (setq children
6532                            (gnus-article-children (gnus-data-number d)))
6533                      (let (found)
6534                        (while children
6535                          (when (memq (car children) articles)
6536                            (setq children nil
6537                                  found t))
6538                          (pop children))
6539                        found)))
6540         (push (gnus-data-number d) articles)))
6541     ;; Do the limiting.
6542     (prog1
6543         (gnus-summary-limit articles)
6544       (gnus-summary-position-point))))
6545
6546 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6547   "Mark all unread excluded articles as read.
6548 If ALL, mark even excluded ticked and dormants as read."
6549   (interactive "P")
6550   (let ((articles (gnus-sorted-complement
6551                    (sort
6552                     (mapcar (lambda (h) (mail-header-number h))
6553                             gnus-newsgroup-headers)
6554                     '<)
6555                    (sort gnus-newsgroup-limit '<)))
6556         article)
6557     (setq gnus-newsgroup-unreads
6558           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6559     (if all
6560         (setq gnus-newsgroup-dormant nil
6561               gnus-newsgroup-marked nil
6562               gnus-newsgroup-reads
6563               (nconc
6564                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6565                gnus-newsgroup-reads))
6566       (while (setq article (pop articles))
6567         (unless (or (memq article gnus-newsgroup-dormant)
6568                     (memq article gnus-newsgroup-marked))
6569           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6570
6571 (defun gnus-summary-limit (articles &optional pop)
6572   (if pop
6573       ;; We pop the previous limit off the stack and use that.
6574       (setq articles (car gnus-newsgroup-limits)
6575             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6576     ;; We use the new limit, so we push the old limit on the stack.
6577     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6578   ;; Set the limit.
6579   (setq gnus-newsgroup-limit articles)
6580   (let ((total (length gnus-newsgroup-data))
6581         (data (gnus-data-find-list (gnus-summary-article-number)))
6582         (gnus-summary-mark-below nil)   ; Inhibit this.
6583         found)
6584     ;; This will do all the work of generating the new summary buffer
6585     ;; according to the new limit.
6586     (gnus-summary-prepare)
6587     ;; Hide any threads, possibly.
6588     (and gnus-show-threads
6589          gnus-thread-hide-subtree
6590          (gnus-summary-hide-all-threads))
6591     ;; Try to return to the article you were at, or one in the
6592     ;; neighborhood.
6593     (when data
6594       ;; We try to find some article after the current one.
6595       (while data
6596         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6597           (setq data nil
6598                 found t))
6599         (setq data (cdr data))))
6600     (unless found
6601       ;; If there is no data, that means that we were after the last
6602       ;; article.  The same goes when we can't find any articles
6603       ;; after the current one.
6604       (goto-char (point-max))
6605       (gnus-summary-find-prev))
6606     (gnus-set-mode-line 'summary)
6607     ;; We return how many articles were removed from the summary
6608     ;; buffer as a result of the new limit.
6609     (- total (length gnus-newsgroup-data))))
6610
6611 (defsubst gnus-invisible-cut-children (threads)
6612   (let ((num 0))
6613     (while threads
6614       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6615         (incf num))
6616       (pop threads))
6617     (< num 2)))
6618
6619 (defsubst gnus-cut-thread (thread)
6620   "Go forwards in the thread until we find an article that we want to display."
6621   (when (or (eq gnus-fetch-old-headers 'some)
6622             (eq gnus-fetch-old-headers 'invisible)