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