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