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