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