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