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