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