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