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