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