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