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