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