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