ad03a14876b45110041a56e759490107be53f47e
[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-parse-headers-hook nil
667   "*A hook called before parsing the headers."
668   :group 'gnus-various
669   :type 'hook)
670
671 (defcustom gnus-exit-group-hook nil
672   "*A hook called when exiting (not quitting) summary mode."
673   :group 'gnus-various
674   :type 'hook)
675
676 (defcustom gnus-summary-update-hook
677   (list 'gnus-summary-highlight-line)
678   "*A hook called when a summary line is changed.
679 The hook will not be called if `gnus-visual' is nil.
680
681 The default function `gnus-summary-highlight-line' will
682 highlight the line according to the `gnus-summary-highlight'
683 variable."
684   :group 'gnus-summary-visual
685   :type 'hook)
686
687 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
688   "*A hook called when an article is selected for the first time.
689 The hook is intended to mark an article as read (or unread)
690 automatically when it is selected."
691   :group 'gnus-summary-choose
692   :type 'hook)
693
694 (defcustom gnus-group-no-more-groups-hook nil
695   "*A hook run when returning to group mode having no more (unread) groups."
696   :group 'gnus-group-select
697   :type 'hook)
698
699 (defcustom gnus-ps-print-hook nil
700   "*A hook run before ps-printing something from Gnus."
701   :group 'gnus-summary
702   :type 'hook)
703
704 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
705   "Face used for highlighting the current article in the summary buffer."
706   :group 'gnus-summary-visual
707   :type 'face)
708
709 (defcustom gnus-summary-highlight
710   '(((= mark gnus-canceled-mark)
711      . gnus-summary-cancelled-face)
712     ((and (> score default)
713           (or (= mark gnus-dormant-mark)
714               (= mark gnus-ticked-mark)))
715      . gnus-summary-high-ticked-face)
716     ((and (< score default)
717           (or (= mark gnus-dormant-mark)
718               (= mark gnus-ticked-mark)))
719      . gnus-summary-low-ticked-face)
720     ((or (= mark gnus-dormant-mark)
721          (= mark gnus-ticked-mark))
722      . gnus-summary-normal-ticked-face)
723     ((and (> score default) (= mark gnus-ancient-mark))
724      . gnus-summary-high-ancient-face)
725     ((and (< score default) (= mark gnus-ancient-mark))
726      . gnus-summary-low-ancient-face)
727     ((= mark gnus-ancient-mark)
728      . gnus-summary-normal-ancient-face)
729     ((and (> score default) (= mark gnus-unread-mark))
730      . gnus-summary-high-unread-face)
731     ((and (< score default) (= mark gnus-unread-mark))
732      . gnus-summary-low-unread-face)
733     ((= mark gnus-unread-mark)
734      . gnus-summary-normal-unread-face)
735     ((and (> score default) (memq mark (list gnus-downloadable-mark
736                                              gnus-undownloaded-mark)))
737      . gnus-summary-high-unread-face)
738     ((and (< score default) (memq mark (list gnus-downloadable-mark
739                                              gnus-undownloaded-mark)))
740      . gnus-summary-low-unread-face)
741     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
742      . gnus-summary-normal-unread-face)
743     ((> score default)
744      . gnus-summary-high-read-face)
745     ((< score default)
746      . gnus-summary-low-read-face)
747     (t
748      . gnus-summary-normal-read-face))
749   "*Controls the highlighting of summary buffer lines.
750
751 A list of (FORM . FACE) pairs.  When deciding how a a particular
752 summary line should be displayed, each form is evaluated.  The content
753 of the face field after the first true form is used.  You can change
754 how those summary lines are displayed, by editing the face field.
755
756 You can use the following variables in the FORM field.
757
758 score:   The articles score
759 default: The default article score.
760 below:   The score below which articles are automatically marked as read.
761 mark:    The articles mark."
762   :group 'gnus-summary-visual
763   :type '(repeat (cons (sexp :tag "Form" nil)
764                        face)))
765
766 (defcustom gnus-alter-header-function nil
767   "Function called to allow alteration of article header structures.
768 The function is called with one parameter, the article header vector,
769 which it may alter in any way.")
770
771 ;;; Internal variables
772
773 (defvar gnus-scores-exclude-files nil)
774 (defvar gnus-page-broken nil)
775
776 (defvar gnus-original-article nil)
777 (defvar gnus-article-internal-prepare-hook nil)
778 (defvar gnus-newsgroup-process-stack nil)
779
780 (defvar gnus-thread-indent-array nil)
781 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
782
783 ;; Avoid highlighting in kill files.
784 (defvar gnus-summary-inhibit-highlight nil)
785 (defvar gnus-newsgroup-selected-overlay nil)
786 (defvar gnus-inhibit-limiting nil)
787 (defvar gnus-newsgroup-adaptive-score-file nil)
788 (defvar gnus-current-score-file nil)
789 (defvar gnus-current-move-group nil)
790 (defvar gnus-current-copy-group nil)
791 (defvar gnus-current-crosspost-group nil)
792
793 (defvar gnus-newsgroup-dependencies nil)
794 (defvar gnus-newsgroup-adaptive nil)
795 (defvar gnus-summary-display-article-function nil)
796 (defvar gnus-summary-highlight-line-function nil
797   "Function called after highlighting a summary line.")
798
799 (defvar gnus-summary-line-format-alist
800   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
801     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
802     (?s gnus-tmp-subject-or-nil ?s)
803     (?n gnus-tmp-name ?s)
804     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
805         ?s)
806     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
807             gnus-tmp-from) ?s)
808     (?F gnus-tmp-from ?s)
809     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
810     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
811     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
812     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
813     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
814     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
815     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
816     (?L gnus-tmp-lines ?d)
817     (?I gnus-tmp-indentation ?s)
818     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
819     (?R gnus-tmp-replied ?c)
820     (?\[ gnus-tmp-opening-bracket ?c)
821     (?\] gnus-tmp-closing-bracket ?c)
822     (?\> (make-string gnus-tmp-level ? ) ?s)
823     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
824     (?i gnus-tmp-score ?d)
825     (?z gnus-tmp-score-char ?c)
826     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
827     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
828     (?U gnus-tmp-unread ?c)
829     (?t (gnus-summary-number-of-articles-in-thread
830          (and (boundp 'thread) (car thread)) gnus-tmp-level)
831         ?d)
832     (?e (gnus-summary-number-of-articles-in-thread
833          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
834         ?c)
835     (?u gnus-tmp-user-defined ?s)
836     (?P (gnus-pick-line-number) ?d))
837   "An alist of format specifications that can appear in summary lines,
838 and what variables they correspond with, along with the type of the
839 variable (string, integer, character, etc).")
840
841 (defvar gnus-summary-dummy-line-format-alist
842   `((?S gnus-tmp-subject ?s)
843     (?N gnus-tmp-number ?d)
844     (?u gnus-tmp-user-defined ?s)))
845
846 (defvar gnus-summary-mode-line-format-alist
847   `((?G gnus-tmp-group-name ?s)
848     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
849     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
850     (?A gnus-tmp-article-number ?d)
851     (?Z gnus-tmp-unread-and-unselected ?s)
852     (?V gnus-version ?s)
853     (?U gnus-tmp-unread-and-unticked ?d)
854     (?S gnus-tmp-subject ?s)
855     (?e gnus-tmp-unselected ?d)
856     (?u gnus-tmp-user-defined ?s)
857     (?d (length gnus-newsgroup-dormant) ?d)
858     (?t (length gnus-newsgroup-marked) ?d)
859     (?r (length gnus-newsgroup-reads) ?d)
860     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
861     (?E gnus-newsgroup-expunged-tally ?d)
862     (?s (gnus-current-score-file-nondirectory) ?s)))
863
864 (defvar gnus-last-search-regexp nil
865   "Default regexp for article search command.")
866
867 (defvar gnus-last-shell-command nil
868   "Default shell command on article.")
869
870 (defvar gnus-newsgroup-begin nil)
871 (defvar gnus-newsgroup-end nil)
872 (defvar gnus-newsgroup-last-rmail nil)
873 (defvar gnus-newsgroup-last-mail nil)
874 (defvar gnus-newsgroup-last-folder nil)
875 (defvar gnus-newsgroup-last-file nil)
876 (defvar gnus-newsgroup-auto-expire nil)
877 (defvar gnus-newsgroup-active nil)
878
879 (defvar gnus-newsgroup-data nil)
880 (defvar gnus-newsgroup-data-reverse nil)
881 (defvar gnus-newsgroup-limit nil)
882 (defvar gnus-newsgroup-limits nil)
883
884 (defvar gnus-newsgroup-unreads nil
885   "List of unread articles in the current newsgroup.")
886
887 (defvar gnus-newsgroup-unselected nil
888   "List of unselected unread articles in the current newsgroup.")
889
890 (defvar gnus-newsgroup-reads nil
891   "Alist of read articles and article marks in the current newsgroup.")
892
893 (defvar gnus-newsgroup-expunged-tally nil)
894
895 (defvar gnus-newsgroup-marked nil
896   "List of ticked articles in the current newsgroup (a subset of unread art).")
897
898 (defvar gnus-newsgroup-killed nil
899   "List of ranges of articles that have been through the scoring process.")
900
901 (defvar gnus-newsgroup-cached nil
902   "List of articles that come from the article cache.")
903
904 (defvar gnus-newsgroup-saved nil
905   "List of articles that have been saved.")
906
907 (defvar gnus-newsgroup-kill-headers nil)
908
909 (defvar gnus-newsgroup-replied nil
910   "List of articles that have been replied to in the current newsgroup.")
911
912 (defvar gnus-newsgroup-expirable nil
913   "List of articles in the current newsgroup that can be expired.")
914
915 (defvar gnus-newsgroup-processable nil
916   "List of articles in the current newsgroup that can be processed.")
917
918 (defvar gnus-newsgroup-downloadable nil
919   "List of articles in the current newsgroup that can be processed.")
920
921 (defvar gnus-newsgroup-undownloaded nil
922   "List of articles in the current newsgroup that haven't been downloaded..")
923
924 (defvar gnus-newsgroup-unsendable nil
925   "List of articles in the current newsgroup that won't be sent.")
926
927 (defvar gnus-newsgroup-bookmarks nil
928   "List of articles in the current newsgroup that have bookmarks.")
929
930 (defvar gnus-newsgroup-dormant nil
931   "List of dormant articles in the current newsgroup.")
932
933 (defvar gnus-newsgroup-scored nil
934   "List of scored articles in the current newsgroup.")
935
936 (defvar gnus-newsgroup-headers nil
937   "List of article headers in the current newsgroup.")
938
939 (defvar gnus-newsgroup-threads nil)
940
941 (defvar gnus-newsgroup-prepared nil
942   "Whether the current group has been prepared properly.")
943
944 (defvar gnus-newsgroup-ancient nil
945   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
946
947 (defvar gnus-newsgroup-sparse nil)
948
949 (defvar gnus-current-article nil)
950 (defvar gnus-article-current nil)
951 (defvar gnus-current-headers nil)
952 (defvar gnus-have-all-headers nil)
953 (defvar gnus-last-article nil)
954 (defvar gnus-newsgroup-history nil)
955
956 (defconst gnus-summary-local-variables
957   '(gnus-newsgroup-name
958     gnus-newsgroup-begin gnus-newsgroup-end
959     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
960     gnus-newsgroup-last-folder gnus-newsgroup-last-file
961     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
962     gnus-newsgroup-unselected gnus-newsgroup-marked
963     gnus-newsgroup-reads gnus-newsgroup-saved
964     gnus-newsgroup-replied gnus-newsgroup-expirable
965     gnus-newsgroup-processable gnus-newsgroup-killed
966     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
967     gnus-newsgroup-unsendable
968     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
969     gnus-newsgroup-headers gnus-newsgroup-threads
970     gnus-newsgroup-prepared gnus-summary-highlight-line-function
971     gnus-current-article gnus-current-headers gnus-have-all-headers
972     gnus-last-article gnus-article-internal-prepare-hook
973     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
974     gnus-newsgroup-scored gnus-newsgroup-kill-headers
975     gnus-thread-expunge-below
976     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
977     (gnus-summary-mark-below . global)
978     gnus-newsgroup-active gnus-scores-exclude-files
979     gnus-newsgroup-history gnus-newsgroup-ancient
980     gnus-newsgroup-sparse gnus-newsgroup-process-stack
981     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
982     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
983     (gnus-newsgroup-expunged-tally . 0)
984     gnus-cache-removable-articles gnus-newsgroup-cached
985     gnus-newsgroup-data gnus-newsgroup-data-reverse
986     gnus-newsgroup-limit gnus-newsgroup-limits)
987   "Variables that are buffer-local to the summary buffers.")
988
989 ;; Byte-compiler warning.
990 (defvar gnus-article-mode-map)
991
992 ;; Subject simplification.
993
994 (defun gnus-simplify-whitespace (str)
995   "Remove excessive whitespace."
996   (let ((mystr str))
997     ;; Multiple spaces.
998     (while (string-match "[ \t][ \t]+" mystr)
999       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1000                           " "
1001                           (substring mystr (match-end 0)))))
1002     ;; Leading spaces.
1003     (when (string-match "^[ \t]+" mystr)
1004       (setq mystr (substring mystr (match-end 0))))
1005     ;; Trailing spaces.
1006     (when (string-match "[ \t]+$" mystr)
1007       (setq mystr (substring mystr 0 (match-beginning 0))))
1008     mystr))
1009
1010 (defsubst gnus-simplify-subject-re (subject)
1011   "Remove \"Re:\" from subject lines."
1012   (if (string-match "^[Rr][Ee]: *" subject)
1013       (substring subject (match-end 0))
1014     subject))
1015
1016 (defun gnus-simplify-subject (subject &optional re-only)
1017   "Remove `Re:' and words in parentheses.
1018 If RE-ONLY is non-nil, strip leading `Re:'s only."
1019   (let ((case-fold-search t))           ;Ignore case.
1020     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1021     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1022       (setq subject (substring subject (match-end 0))))
1023     ;; Remove uninteresting prefixes.
1024     (when (and (not re-only)
1025                gnus-simplify-ignored-prefixes
1026                (string-match gnus-simplify-ignored-prefixes subject))
1027       (setq subject (substring subject (match-end 0))))
1028     ;; Remove words in parentheses from end.
1029     (unless re-only
1030       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1031         (setq subject (substring subject 0 (match-beginning 0)))))
1032     ;; Return subject string.
1033     subject))
1034
1035 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1036 ;; all whitespace.
1037 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1038   (goto-char (point-min))
1039   (while (re-search-forward regexp nil t)
1040       (replace-match (or newtext ""))))
1041
1042 (defun gnus-simplify-buffer-fuzzy ()
1043   "Simplify string in the buffer fuzzily.
1044 The string in the accessible portion of the current buffer is simplified.
1045 It is assumed to be a single-line subject.
1046 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1047 matter is removed.  Additional things can be deleted by setting
1048 gnus-simplify-subject-fuzzy-regexp."
1049   (let ((case-fold-search t)
1050         (modified-tick))
1051     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1052
1053     (while (not (eq modified-tick (buffer-modified-tick)))
1054       (setq modified-tick (buffer-modified-tick))
1055       (cond
1056        ((listp gnus-simplify-subject-fuzzy-regexp)
1057         (mapcar 'gnus-simplify-buffer-fuzzy-step
1058                 gnus-simplify-subject-fuzzy-regexp))
1059        (gnus-simplify-subject-fuzzy-regexp
1060         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1061       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1062       (gnus-simplify-buffer-fuzzy-step
1063        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1064       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1065
1066     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1067     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1068     (gnus-simplify-buffer-fuzzy-step " $")
1069     (gnus-simplify-buffer-fuzzy-step "^ +")))
1070
1071 (defun gnus-simplify-subject-fuzzy (subject)
1072   "Simplify a subject string fuzzily.
1073 See `gnus-simplify-buffer-fuzzy' for details."
1074   (save-excursion
1075     (gnus-set-work-buffer)
1076     (let ((case-fold-search t))
1077       ;; Remove uninteresting prefixes.
1078       (when (and gnus-simplify-ignored-prefixes
1079                  (string-match gnus-simplify-ignored-prefixes subject))
1080         (setq subject (substring subject (match-end 0))))
1081       (insert subject)
1082       (inline (gnus-simplify-buffer-fuzzy))
1083       (buffer-string))))
1084
1085 (defsubst gnus-simplify-subject-fully (subject)
1086   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1087   (cond
1088    (gnus-simplify-subject-functions
1089     (gnus-map-function gnus-simplify-subject-functions subject))
1090    ((null gnus-summary-gather-subject-limit)
1091     (gnus-simplify-subject-re subject))
1092    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1093     (gnus-simplify-subject-fuzzy subject))
1094    ((numberp gnus-summary-gather-subject-limit)
1095     (gnus-limit-string (gnus-simplify-subject-re subject)
1096                        gnus-summary-gather-subject-limit))
1097    (t
1098     subject)))
1099
1100 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1101   "Check whether two subjects are equal.
1102 If optional argument simple-first is t, first argument is already
1103 simplified."
1104   (cond
1105    ((null simple-first)
1106     (equal (gnus-simplify-subject-fully s1)
1107            (gnus-simplify-subject-fully s2)))
1108    (t
1109     (equal s1
1110            (gnus-simplify-subject-fully s2)))))
1111
1112 (defun gnus-summary-bubble-group ()
1113   "Increase the score of the current group.
1114 This is a handy function to add to `gnus-summary-exit-hook' to
1115 increase the score of each group you read."
1116   (gnus-group-add-score gnus-newsgroup-name))
1117
1118 \f
1119 ;;;
1120 ;;; Gnus summary mode
1121 ;;;
1122
1123 (put 'gnus-summary-mode 'mode-class 'special)
1124
1125 (when t
1126   ;; Non-orthogonal keys
1127
1128   (gnus-define-keys gnus-summary-mode-map
1129     " " gnus-summary-next-page
1130     "\177" gnus-summary-prev-page
1131     [delete] gnus-summary-prev-page
1132     [backspace] gnus-summary-prev-page
1133     "\r" gnus-summary-scroll-up
1134     "\M-\r" gnus-summary-scroll-down
1135     "n" gnus-summary-next-unread-article
1136     "p" gnus-summary-prev-unread-article
1137     "N" gnus-summary-next-article
1138     "P" gnus-summary-prev-article
1139     "\M-\C-n" gnus-summary-next-same-subject
1140     "\M-\C-p" gnus-summary-prev-same-subject
1141     "\M-n" gnus-summary-next-unread-subject
1142     "\M-p" gnus-summary-prev-unread-subject
1143     "." gnus-summary-first-unread-article
1144     "," gnus-summary-best-unread-article
1145     "\M-s" gnus-summary-search-article-forward
1146     "\M-r" gnus-summary-search-article-backward
1147     "<" gnus-summary-beginning-of-article
1148     ">" gnus-summary-end-of-article
1149     "j" gnus-summary-goto-article
1150     "^" gnus-summary-refer-parent-article
1151     "\M-^" gnus-summary-refer-article
1152     "u" gnus-summary-tick-article-forward
1153     "!" gnus-summary-tick-article-forward
1154     "U" gnus-summary-tick-article-backward
1155     "d" gnus-summary-mark-as-read-forward
1156     "D" gnus-summary-mark-as-read-backward
1157     "E" gnus-summary-mark-as-expirable
1158     "\M-u" gnus-summary-clear-mark-forward
1159     "\M-U" gnus-summary-clear-mark-backward
1160     "k" gnus-summary-kill-same-subject-and-select
1161     "\C-k" gnus-summary-kill-same-subject
1162     "\M-\C-k" gnus-summary-kill-thread
1163     "\M-\C-l" gnus-summary-lower-thread
1164     "e" gnus-summary-edit-article
1165     "#" gnus-summary-mark-as-processable
1166     "\M-#" gnus-summary-unmark-as-processable
1167     "\M-\C-t" gnus-summary-toggle-threads
1168     "\M-\C-s" gnus-summary-show-thread
1169     "\M-\C-h" gnus-summary-hide-thread
1170     "\M-\C-f" gnus-summary-next-thread
1171     "\M-\C-b" gnus-summary-prev-thread
1172     "\M-\C-u" gnus-summary-up-thread
1173     "\M-\C-d" gnus-summary-down-thread
1174     "&" gnus-summary-execute-command
1175     "c" gnus-summary-catchup-and-exit
1176     "\C-w" gnus-summary-mark-region-as-read
1177     "\C-t" gnus-summary-toggle-truncation
1178     "?" gnus-summary-mark-as-dormant
1179     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1180     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1181     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1182     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1183     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1184     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1185     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1186     "=" gnus-summary-expand-window
1187     "\C-x\C-s" gnus-summary-reselect-current-group
1188     "\M-g" gnus-summary-rescan-group
1189     "w" gnus-summary-stop-page-breaking
1190     "\C-c\C-r" gnus-summary-caesar-message
1191     "\M-t" gnus-summary-toggle-mime
1192     "f" gnus-summary-followup
1193     "F" gnus-summary-followup-with-original
1194     "C" gnus-summary-cancel-article
1195     "r" gnus-summary-reply
1196     "R" gnus-summary-reply-with-original
1197     "\C-c\C-f" gnus-summary-mail-forward
1198     "o" gnus-summary-save-article
1199     "\C-o" gnus-summary-save-article-mail
1200     "|" gnus-summary-pipe-output
1201     "\M-k" gnus-summary-edit-local-kill
1202     "\M-K" gnus-summary-edit-global-kill
1203     ;; "V" gnus-version
1204     "\C-c\C-d" gnus-summary-describe-group
1205     "q" gnus-summary-exit
1206     "Q" gnus-summary-exit-no-update
1207     "\C-c\C-i" gnus-info-find-node
1208     gnus-mouse-2 gnus-mouse-pick-article
1209     "m" gnus-summary-mail-other-window
1210     "a" gnus-summary-post-news
1211     "x" gnus-summary-limit-to-unread
1212     "s" gnus-summary-isearch-article
1213     "t" gnus-article-hide-headers
1214     "g" gnus-summary-show-article
1215     "l" gnus-summary-goto-last-article
1216     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1217     "\C-d" gnus-summary-enter-digest-group
1218     "\M-\C-d" gnus-summary-read-document
1219     "\M-\C-e" gnus-summary-edit-parameters
1220     "\C-c\C-b" gnus-bug
1221     "*" gnus-cache-enter-article
1222     "\M-*" gnus-cache-remove-article
1223     "\M-&" gnus-summary-universal-argument
1224     "\C-l" gnus-recenter
1225     "I" gnus-summary-increase-score
1226     "L" gnus-summary-lower-score
1227     "\M-i" gnus-symbolic-argument
1228     "h" gnus-summary-select-article-buffer
1229     
1230     "V" gnus-summary-score-map
1231     "X" gnus-uu-extract-map
1232     "S" gnus-summary-send-map)
1233
1234   ;; Sort of orthogonal keymap
1235   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1236     "t" gnus-summary-tick-article-forward
1237     "!" gnus-summary-tick-article-forward
1238     "d" gnus-summary-mark-as-read-forward
1239     "r" gnus-summary-mark-as-read-forward
1240     "c" gnus-summary-clear-mark-forward
1241     " " gnus-summary-clear-mark-forward
1242     "e" gnus-summary-mark-as-expirable
1243     "x" gnus-summary-mark-as-expirable
1244     "?" gnus-summary-mark-as-dormant
1245     "b" gnus-summary-set-bookmark
1246     "B" gnus-summary-remove-bookmark
1247     "#" gnus-summary-mark-as-processable
1248     "\M-#" gnus-summary-unmark-as-processable
1249     "S" gnus-summary-limit-include-expunged
1250     "C" gnus-summary-catchup
1251     "H" gnus-summary-catchup-to-here
1252     "\C-c" gnus-summary-catchup-all
1253     "k" gnus-summary-kill-same-subject-and-select
1254     "K" gnus-summary-kill-same-subject
1255     "P" gnus-uu-mark-map)
1256
1257   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1258     "c" gnus-summary-clear-above
1259     "u" gnus-summary-tick-above
1260     "m" gnus-summary-mark-above
1261     "k" gnus-summary-kill-below)
1262
1263   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1264     "/" gnus-summary-limit-to-subject
1265     "n" gnus-summary-limit-to-articles
1266     "w" gnus-summary-pop-limit
1267     "s" gnus-summary-limit-to-subject
1268     "a" gnus-summary-limit-to-author
1269     "u" gnus-summary-limit-to-unread
1270     "m" gnus-summary-limit-to-marks
1271     "v" gnus-summary-limit-to-score
1272     "*" gnus-summary-limit-include-cached
1273     "D" gnus-summary-limit-include-dormant
1274     "T" gnus-summary-limit-include-thread
1275     "d" gnus-summary-limit-exclude-dormant
1276     "t" gnus-summary-limit-to-age
1277     "E" gnus-summary-limit-include-expunged
1278     "c" gnus-summary-limit-exclude-childless-dormant
1279     "C" gnus-summary-limit-mark-excluded-as-read)
1280
1281   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1282     "n" gnus-summary-next-unread-article
1283     "p" gnus-summary-prev-unread-article
1284     "N" gnus-summary-next-article
1285     "P" gnus-summary-prev-article
1286     "\C-n" gnus-summary-next-same-subject
1287     "\C-p" gnus-summary-prev-same-subject
1288     "\M-n" gnus-summary-next-unread-subject
1289     "\M-p" gnus-summary-prev-unread-subject
1290     "f" gnus-summary-first-unread-article
1291     "b" gnus-summary-best-unread-article
1292     "j" gnus-summary-goto-article
1293     "g" gnus-summary-goto-subject
1294     "l" gnus-summary-goto-last-article
1295     "o" gnus-summary-pop-article)
1296
1297   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1298     "k" gnus-summary-kill-thread
1299     "l" gnus-summary-lower-thread
1300     "i" gnus-summary-raise-thread
1301     "T" gnus-summary-toggle-threads
1302     "t" gnus-summary-rethread-current
1303     "^" gnus-summary-reparent-thread
1304     "s" gnus-summary-show-thread
1305     "S" gnus-summary-show-all-threads
1306     "h" gnus-summary-hide-thread
1307     "H" gnus-summary-hide-all-threads
1308     "n" gnus-summary-next-thread
1309     "p" gnus-summary-prev-thread
1310     "u" gnus-summary-up-thread
1311     "o" gnus-summary-top-thread
1312     "d" gnus-summary-down-thread
1313     "#" gnus-uu-mark-thread
1314     "\M-#" gnus-uu-unmark-thread)
1315
1316   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1317     "g" gnus-summary-prepare
1318     "c" gnus-summary-insert-cached-articles)
1319
1320   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1321     "c" gnus-summary-catchup-and-exit
1322     "C" gnus-summary-catchup-all-and-exit
1323     "E" gnus-summary-exit-no-update
1324     "Q" gnus-summary-exit
1325     "Z" gnus-summary-exit
1326     "n" gnus-summary-catchup-and-goto-next-group
1327     "R" gnus-summary-reselect-current-group
1328     "G" gnus-summary-rescan-group
1329     "N" gnus-summary-next-group
1330     "s" gnus-summary-save-newsrc
1331     "P" gnus-summary-prev-group)
1332
1333   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1334     " " gnus-summary-next-page
1335     "n" gnus-summary-next-page
1336     "\177" gnus-summary-prev-page
1337     [delete] gnus-summary-prev-page
1338     "p" gnus-summary-prev-page
1339     "\r" gnus-summary-scroll-up
1340     "\M-\r" gnus-summary-scroll-down
1341     "<" gnus-summary-beginning-of-article
1342     ">" gnus-summary-end-of-article
1343     "b" gnus-summary-beginning-of-article
1344     "e" gnus-summary-end-of-article
1345     "^" gnus-summary-refer-parent-article
1346     "r" gnus-summary-refer-parent-article
1347     "R" gnus-summary-refer-references
1348     "T" gnus-summary-refer-thread
1349     "g" gnus-summary-show-article
1350     "s" gnus-summary-isearch-article
1351     "P" gnus-summary-print-article)
1352
1353   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1354     "b" gnus-article-add-buttons
1355     "B" gnus-article-add-buttons-to-head
1356     "o" gnus-article-treat-overstrike
1357     "e" gnus-article-emphasize
1358     "w" gnus-article-fill-cited-article
1359     "c" gnus-article-remove-cr
1360     "q" gnus-article-de-quoted-unreadable
1361     "f" gnus-article-display-x-face
1362     "l" gnus-summary-stop-page-breaking
1363     "r" gnus-summary-caesar-message
1364     "t" gnus-article-hide-headers
1365     "v" gnus-summary-verbose-headers
1366     "m" gnus-summary-toggle-mime
1367     "h" gnus-article-treat-html
1368     "d" gnus-article-treat-dumbquotes)
1369
1370   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1371     "a" gnus-article-hide
1372     "h" gnus-article-hide-headers
1373     "b" gnus-article-hide-boring-headers
1374     "s" gnus-article-hide-signature
1375     "c" gnus-article-hide-citation
1376     "C" gnus-article-hide-citation-in-followups
1377     "p" gnus-article-hide-pgp
1378     "P" gnus-article-hide-pem
1379     "\C-c" gnus-article-hide-citation-maybe)
1380
1381   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1382     "a" gnus-article-highlight
1383     "h" gnus-article-highlight-headers
1384     "c" gnus-article-highlight-citation
1385     "s" gnus-article-highlight-signature)
1386
1387   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1388     "w" gnus-article-decode-mime-words)
1389
1390   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1391     "z" gnus-article-date-ut
1392     "u" gnus-article-date-ut
1393     "l" gnus-article-date-local
1394     "e" gnus-article-date-lapsed
1395     "o" gnus-article-date-original
1396     "i" gnus-article-date-iso8601
1397     "s" gnus-article-date-user)
1398
1399   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1400     "t" gnus-article-remove-trailing-blank-lines
1401     "l" gnus-article-strip-leading-blank-lines
1402     "m" gnus-article-strip-multiple-blank-lines
1403     "a" gnus-article-strip-blank-lines
1404     "A" gnus-article-strip-all-blank-lines
1405     "s" gnus-article-strip-leading-space)
1406
1407   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1408     "v" gnus-version
1409     "f" gnus-summary-fetch-faq
1410     "d" gnus-summary-describe-group
1411     "h" gnus-summary-describe-briefly
1412     "i" gnus-info-find-node)
1413
1414   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1415     "e" gnus-summary-expire-articles
1416     "\M-\C-e" gnus-summary-expire-articles-now
1417     "\177" gnus-summary-delete-article
1418     [delete] gnus-summary-delete-article
1419     "m" gnus-summary-move-article
1420     "r" gnus-summary-respool-article
1421     "w" gnus-summary-edit-article
1422     "c" gnus-summary-copy-article
1423     "B" gnus-summary-crosspost-article
1424     "q" gnus-summary-respool-query
1425     "t" gnus-summary-respool-trace
1426     "i" gnus-summary-import-article
1427     "p" gnus-summary-article-posted-p)
1428
1429   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1430     "o" gnus-summary-save-article
1431     "m" gnus-summary-save-article-mail
1432     "F" gnus-summary-write-article-file
1433     "r" gnus-summary-save-article-rmail
1434     "f" gnus-summary-save-article-file
1435     "b" gnus-summary-save-article-body-file
1436     "h" gnus-summary-save-article-folder
1437     "v" gnus-summary-save-article-vm
1438     "p" gnus-summary-pipe-output
1439     "s" gnus-soup-add-article))
1440
1441 (defun gnus-summary-make-menu-bar ()
1442   (gnus-turn-off-edit-menu 'summary)
1443
1444   (unless (boundp 'gnus-summary-misc-menu)
1445
1446     (easy-menu-define
1447      gnus-summary-kill-menu gnus-summary-mode-map ""
1448      (cons
1449       "Score"
1450       (nconc
1451        (list
1452         ["Enter score..." gnus-summary-score-entry t]
1453         ["Customize" gnus-score-customize t])
1454        (gnus-make-score-map 'increase)
1455        (gnus-make-score-map 'lower)
1456        '(("Mark"
1457           ["Kill below" gnus-summary-kill-below t]
1458           ["Mark above" gnus-summary-mark-above t]
1459           ["Tick above" gnus-summary-tick-above t]
1460           ["Clear above" gnus-summary-clear-above t])
1461          ["Current score" gnus-summary-current-score t]
1462          ["Set score" gnus-summary-set-score t]
1463          ["Switch current score file..." gnus-score-change-score-file t]
1464          ["Set mark below..." gnus-score-set-mark-below t]
1465          ["Set expunge below..." gnus-score-set-expunge-below t]
1466          ["Edit current score file" gnus-score-edit-current-scores t]
1467          ["Edit score file" gnus-score-edit-file t]
1468          ["Trace score" gnus-score-find-trace t]
1469          ["Find words" gnus-score-find-favourite-words t]
1470          ["Rescore buffer" gnus-summary-rescore t]
1471          ["Increase score..." gnus-summary-increase-score t]
1472          ["Lower score..." gnus-summary-lower-score t]))))
1473
1474     ;; Define both the Article menu in the summary buffer and the equivalent
1475     ;; Commands menu in the article buffer here for consistency.
1476     (let ((innards
1477            '(("Hide"
1478               ["All" gnus-article-hide t]
1479               ["Headers" gnus-article-hide-headers t]
1480               ["Signature" gnus-article-hide-signature t]
1481               ["Citation" gnus-article-hide-citation t]
1482               ["PGP" gnus-article-hide-pgp t]
1483               ["Boring headers" gnus-article-hide-boring-headers t])
1484              ("Highlight"
1485               ["All" gnus-article-highlight t]
1486               ["Headers" gnus-article-highlight-headers t]
1487               ["Signature" gnus-article-highlight-signature t]
1488               ["Citation" gnus-article-highlight-citation t])
1489              ("MIME"
1490               ["Words" gnus-article-decode-mime-words t]
1491               ["QP" gnus-article-de-quoted-unreadable t])
1492              ("Date"
1493               ["Local" gnus-article-date-local t]
1494               ["ISO8601" gnus-article-date-iso8601 t]
1495               ["UT" gnus-article-date-ut t]
1496               ["Original" gnus-article-date-original t]
1497               ["Lapsed" gnus-article-date-lapsed t]
1498               ["User-defined" gnus-article-date-user t])
1499              ("Washing"
1500               ("Remove Blanks"
1501                ["Leading" gnus-article-strip-leading-blank-lines t]
1502                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1503                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1504                ["All of the above" gnus-article-strip-blank-lines t]
1505                ["All" gnus-article-strip-all-blank-lines t]
1506                ["Leading space" gnus-article-strip-leading-space t])
1507               ["Overstrike" gnus-article-treat-overstrike t]
1508               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1509               ["Emphasis" gnus-article-emphasize t]
1510               ["Word wrap" gnus-article-fill-cited-article t]
1511               ["CR" gnus-article-remove-cr t]
1512               ["Show X-Face" gnus-article-display-x-face t]
1513               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1514               ["UnHTMLize" gnus-article-treat-html t]
1515               ["Rot 13" gnus-summary-caesar-message t]
1516               ["Unix pipe" gnus-summary-pipe-message t]
1517               ["Add buttons" gnus-article-add-buttons t]
1518               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1519               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1520               ["Toggle MIME" gnus-summary-toggle-mime t]
1521               ["Verbose header" gnus-summary-verbose-headers t]
1522               ["Toggle header" gnus-summary-toggle-header t])
1523              ("Output"
1524               ["Save in default format" gnus-summary-save-article t]
1525               ["Save in file" gnus-summary-save-article-file t]
1526               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1527               ["Save in MH folder" gnus-summary-save-article-folder t]
1528               ["Save in VM folder" gnus-summary-save-article-vm t]
1529               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1530               ["Save body in file" gnus-summary-save-article-body-file t]
1531               ["Pipe through a filter" gnus-summary-pipe-output t]
1532               ["Add to SOUP packet" gnus-soup-add-article t]
1533               ["Print" gnus-summary-print-article t])
1534              ("Backend"
1535               ["Respool article..." gnus-summary-respool-article t]
1536               ["Move article..." gnus-summary-move-article
1537                (gnus-check-backend-function
1538                 'request-move-article gnus-newsgroup-name)]
1539               ["Copy article..." gnus-summary-copy-article t]
1540               ["Crosspost article..." gnus-summary-crosspost-article
1541                (gnus-check-backend-function
1542                 'request-replace-article gnus-newsgroup-name)]
1543               ["Import file..." gnus-summary-import-article t]
1544               ["Check if posted" gnus-summary-article-posted-p t]
1545               ["Edit article" gnus-summary-edit-article
1546                (not (gnus-group-read-only-p))]
1547               ["Delete article" gnus-summary-delete-article
1548                (gnus-check-backend-function
1549                 'request-expire-articles gnus-newsgroup-name)]
1550               ["Query respool" gnus-summary-respool-query t]
1551               ["Trace respool" gnus-summary-respool-trace t]
1552               ["Delete expirable articles" gnus-summary-expire-articles-now
1553                (gnus-check-backend-function
1554                 'request-expire-articles gnus-newsgroup-name)])
1555              ("Extract"
1556               ["Uudecode" gnus-uu-decode-uu t]
1557               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1558               ["Unshar" gnus-uu-decode-unshar t]
1559               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1560               ["Save" gnus-uu-decode-save t]
1561               ["Binhex" gnus-uu-decode-binhex t]
1562               ["Postscript" gnus-uu-decode-postscript t])
1563              ("Cache"
1564               ["Enter article" gnus-cache-enter-article t]
1565               ["Remove article" gnus-cache-remove-article t])
1566              ["Select article buffer" gnus-summary-select-article-buffer t]
1567              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1568              ["Isearch article..." gnus-summary-isearch-article t]
1569              ["Beginning of the article" gnus-summary-beginning-of-article t]
1570              ["End of the article" gnus-summary-end-of-article t]
1571              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1572              ["Fetch referenced articles" gnus-summary-refer-references t]
1573              ["Fetch current thread" gnus-summary-refer-thread t]
1574              ["Fetch article with id..." gnus-summary-refer-article t]
1575              ["Redisplay" gnus-summary-show-article t])))
1576       (easy-menu-define
1577        gnus-summary-article-menu gnus-summary-mode-map ""
1578        (cons "Article" innards))
1579
1580       (easy-menu-define
1581        gnus-article-commands-menu gnus-article-mode-map ""
1582        (cons "Commands" innards)))
1583
1584     (easy-menu-define
1585      gnus-summary-thread-menu gnus-summary-mode-map ""
1586      '("Threads"
1587        ["Toggle threading" gnus-summary-toggle-threads t]
1588        ["Hide threads" gnus-summary-hide-all-threads t]
1589        ["Show threads" gnus-summary-show-all-threads t]
1590        ["Hide thread" gnus-summary-hide-thread t]
1591        ["Show thread" gnus-summary-show-thread t]
1592        ["Go to next thread" gnus-summary-next-thread t]
1593        ["Go to previous thread" gnus-summary-prev-thread t]
1594        ["Go down thread" gnus-summary-down-thread t]
1595        ["Go up thread" gnus-summary-up-thread t]
1596        ["Top of thread" gnus-summary-top-thread t]
1597        ["Mark thread as read" gnus-summary-kill-thread t]
1598        ["Lower thread score" gnus-summary-lower-thread t]
1599        ["Raise thread score" gnus-summary-raise-thread t]
1600        ["Rethread current" gnus-summary-rethread-current t]
1601        ))
1602
1603     (easy-menu-define
1604      gnus-summary-post-menu gnus-summary-mode-map ""
1605      '("Post"
1606        ["Post an article" gnus-summary-post-news t]
1607        ["Followup" gnus-summary-followup t]
1608        ["Followup and yank" gnus-summary-followup-with-original t]
1609        ["Supersede article" gnus-summary-supersede-article t]
1610        ["Cancel article" gnus-summary-cancel-article t]
1611        ["Reply" gnus-summary-reply t]
1612        ["Reply and yank" gnus-summary-reply-with-original t]
1613        ["Wide reply" gnus-summary-wide-reply t]
1614        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1615        ["Mail forward" gnus-summary-mail-forward t]
1616        ["Post forward" gnus-summary-post-forward t]
1617        ["Digest and mail" gnus-uu-digest-mail-forward t]
1618        ["Digest and post" gnus-uu-digest-post-forward t]
1619        ["Resend message" gnus-summary-resend-message t]
1620        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1621        ["Send a mail" gnus-summary-mail-other-window t]
1622        ["Uuencode and post" gnus-uu-post-news t]
1623        ["Followup via news" gnus-summary-followup-to-mail t]
1624        ["Followup via news and yank"
1625         gnus-summary-followup-to-mail-with-original t]
1626        ;;("Draft"
1627        ;;["Send" gnus-summary-send-draft t]
1628        ;;["Send bounced" gnus-resend-bounced-mail t])
1629        ))
1630
1631     (easy-menu-define
1632      gnus-summary-misc-menu gnus-summary-mode-map ""
1633      '("Misc"
1634        ("Mark Read"
1635         ["Mark as read" gnus-summary-mark-as-read-forward t]
1636         ["Mark same subject and select"
1637          gnus-summary-kill-same-subject-and-select t]
1638         ["Mark same subject" gnus-summary-kill-same-subject t]
1639         ["Catchup" gnus-summary-catchup t]
1640         ["Catchup all" gnus-summary-catchup-all t]
1641         ["Catchup to here" gnus-summary-catchup-to-here t]
1642         ["Catchup region" gnus-summary-mark-region-as-read t]
1643         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1644        ("Mark Various"
1645         ["Tick" gnus-summary-tick-article-forward t]
1646         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1647         ["Remove marks" gnus-summary-clear-mark-forward t]
1648         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1649         ["Set bookmark" gnus-summary-set-bookmark t]
1650         ["Remove bookmark" gnus-summary-remove-bookmark t])
1651        ("Mark Limit"
1652         ["Marks..." gnus-summary-limit-to-marks t]
1653         ["Subject..." gnus-summary-limit-to-subject t]
1654         ["Author..." gnus-summary-limit-to-author t]
1655         ["Age..." gnus-summary-limit-to-age t]
1656         ["Score" gnus-summary-limit-to-score t]
1657         ["Unread" gnus-summary-limit-to-unread t]
1658         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1659         ["Articles" gnus-summary-limit-to-articles t]
1660         ["Pop limit" gnus-summary-pop-limit t]
1661         ["Show dormant" gnus-summary-limit-include-dormant t]
1662         ["Hide childless dormant"
1663          gnus-summary-limit-exclude-childless-dormant t]
1664         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1665         ["Show expunged" gnus-summary-show-all-expunged t])
1666        ("Process Mark"
1667         ["Set mark" gnus-summary-mark-as-processable t]
1668         ["Remove mark" gnus-summary-unmark-as-processable t]
1669         ["Remove all marks" gnus-summary-unmark-all-processable t]
1670         ["Mark above" gnus-uu-mark-over t]
1671         ["Mark series" gnus-uu-mark-series t]
1672         ["Mark region" gnus-uu-mark-region t]
1673         ["Unmark region" gnus-uu-unmark-region t]
1674         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1675         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1676         ["Mark all" gnus-uu-mark-all t]
1677         ["Mark buffer" gnus-uu-mark-buffer t]
1678         ["Mark sparse" gnus-uu-mark-sparse t]
1679         ["Mark thread" gnus-uu-mark-thread t]
1680         ["Unmark thread" gnus-uu-unmark-thread t]
1681         ("Process Mark Sets"
1682          ["Kill" gnus-summary-kill-process-mark t]
1683          ["Yank" gnus-summary-yank-process-mark
1684           gnus-newsgroup-process-stack]
1685          ["Save" gnus-summary-save-process-mark t]))
1686        ("Scroll article"
1687         ["Page forward" gnus-summary-next-page t]
1688         ["Page backward" gnus-summary-prev-page t]
1689         ["Line forward" gnus-summary-scroll-up t])
1690        ("Move"
1691         ["Next unread article" gnus-summary-next-unread-article t]
1692         ["Previous unread article" gnus-summary-prev-unread-article t]
1693         ["Next article" gnus-summary-next-article t]
1694         ["Previous article" gnus-summary-prev-article t]
1695         ["Next unread subject" gnus-summary-next-unread-subject t]
1696         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1697         ["Next article same subject" gnus-summary-next-same-subject t]
1698         ["Previous article same subject" gnus-summary-prev-same-subject t]
1699         ["First unread article" gnus-summary-first-unread-article t]
1700         ["Best unread article" gnus-summary-best-unread-article t]
1701         ["Go to subject number..." gnus-summary-goto-subject t]
1702         ["Go to article number..." gnus-summary-goto-article t]
1703         ["Go to the last article" gnus-summary-goto-last-article t]
1704         ["Pop article off history" gnus-summary-pop-article t])
1705        ("Sort"
1706         ["Sort by number" gnus-summary-sort-by-number t]
1707         ["Sort by author" gnus-summary-sort-by-author t]
1708         ["Sort by subject" gnus-summary-sort-by-subject t]
1709         ["Sort by date" gnus-summary-sort-by-date t]
1710         ["Sort by score" gnus-summary-sort-by-score t]
1711         ["Sort by lines" gnus-summary-sort-by-lines t])
1712        ("Help"
1713         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1714         ["Describe group" gnus-summary-describe-group t]
1715         ["Read manual" gnus-info-find-node t])
1716        ("Modes"
1717         ["Pick and read" gnus-pick-mode t]
1718         ["Binary" gnus-binary-mode t])
1719        ("Regeneration"
1720         ["Regenerate" gnus-summary-prepare t]
1721         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1722         ["Toggle threading" gnus-summary-toggle-threads t])
1723        ["Filter articles..." gnus-summary-execute-command t]
1724        ["Run command on subjects..." gnus-summary-universal-argument t]
1725        ["Search articles forward..." gnus-summary-search-article-forward t]
1726        ["Search articles backward..." gnus-summary-search-article-backward t]
1727        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1728        ["Expand window" gnus-summary-expand-window t]
1729        ["Expire expirable articles" gnus-summary-expire-articles
1730         (gnus-check-backend-function
1731          'request-expire-articles gnus-newsgroup-name)]
1732        ["Edit local kill file" gnus-summary-edit-local-kill t]
1733        ["Edit main kill file" gnus-summary-edit-global-kill t]
1734        ["Edit group parameters" gnus-summary-edit-parameters t]
1735        ["Send a bug report" gnus-bug t]
1736        ("Exit"
1737         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1738         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1739         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1740         ["Exit group" gnus-summary-exit t]
1741         ["Exit group without updating" gnus-summary-exit-no-update t]
1742         ["Exit and goto next group" gnus-summary-next-group t]
1743         ["Exit and goto prev group" gnus-summary-prev-group t]
1744         ["Reselect group" gnus-summary-reselect-current-group t]
1745         ["Rescan group" gnus-summary-rescan-group t]
1746         ["Update dribble" gnus-summary-save-newsrc t])))
1747
1748     (gnus-run-hooks 'gnus-summary-menu-hook)))
1749
1750 (defun gnus-score-set-default (var value)
1751   "A version of set that updates the GNU Emacs menu-bar."
1752   (set var value)
1753   ;; It is the message that forces the active status to be updated.
1754   (message ""))
1755
1756 (defun gnus-make-score-map (type)
1757   "Make a summary score map of type TYPE."
1758   (if t
1759       nil
1760     (let ((headers '(("author" "from" string)
1761                      ("subject" "subject" string)
1762                      ("article body" "body" string)
1763                      ("article head" "head" string)
1764                      ("xref" "xref" string)
1765                      ("lines" "lines" number)
1766                      ("followups to author" "followup" string)))
1767           (types '((number ("less than" <)
1768                            ("greater than" >)
1769                            ("equal" =))
1770                    (string ("substring" s)
1771                            ("exact string" e)
1772                            ("fuzzy string" f)
1773                            ("regexp" r))))
1774           (perms '(("temporary" (current-time-string))
1775                    ("permanent" nil)
1776                    ("immediate" now)))
1777           header)
1778       (list
1779        (apply
1780         'nconc
1781         (list
1782          (if (eq type 'lower)
1783              "Lower score"
1784            "Increase score"))
1785         (let (outh)
1786           (while headers
1787             (setq header (car headers))
1788             (setq outh
1789                   (cons
1790                    (apply
1791                     'nconc
1792                     (list (car header))
1793                     (let ((ts (cdr (assoc (nth 2 header) types)))
1794                           outt)
1795                       (while ts
1796                         (setq outt
1797                               (cons
1798                                (apply
1799                                 'nconc
1800                                 (list (caar ts))
1801                                 (let ((ps perms)
1802                                       outp)
1803                                   (while ps
1804                                     (setq outp
1805                                           (cons
1806                                            (vector
1807                                             (caar ps)
1808                                             (list
1809                                              'gnus-summary-score-entry
1810                                              (nth 1 header)
1811                                              (if (or (string= (nth 1 header)
1812                                                               "head")
1813                                                      (string= (nth 1 header)
1814                                                               "body"))
1815                                                  ""
1816                                                (list 'gnus-summary-header
1817                                                      (nth 1 header)))
1818                                              (list 'quote (nth 1 (car ts)))
1819                                              (list 'gnus-score-default nil)
1820                                              (nth 1 (car ps))
1821                                              t)
1822                                             t)
1823                                            outp))
1824                                     (setq ps (cdr ps)))
1825                                   (list (nreverse outp))))
1826                                outt))
1827                         (setq ts (cdr ts)))
1828                       (list (nreverse outt))))
1829                    outh))
1830             (setq headers (cdr headers)))
1831           (list (nreverse outh))))))))
1832
1833 \f
1834
1835 (defun gnus-summary-mode (&optional group)
1836   "Major mode for reading articles.
1837
1838 All normal editing commands are switched off.
1839 \\<gnus-summary-mode-map>
1840 Each line in this buffer represents one article.  To read an
1841 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1842 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1843 respectively.
1844
1845 You can also post articles and send mail from this buffer.  To
1846 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1847 of an article, type `\\[gnus-summary-reply]'.
1848
1849 There are approx. one gazillion commands you can execute in this
1850 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1851
1852 The following commands are available:
1853
1854 \\{gnus-summary-mode-map}"
1855   (interactive)
1856   (when (gnus-visual-p 'summary-menu 'menu)
1857     (gnus-summary-make-menu-bar))
1858   (kill-all-local-variables)
1859   (gnus-summary-make-local-variables)
1860   (gnus-make-thread-indent-array)
1861   (gnus-simplify-mode-line)
1862   (setq major-mode 'gnus-summary-mode)
1863   (setq mode-name "Summary")
1864   (make-local-variable 'minor-mode-alist)
1865   (use-local-map gnus-summary-mode-map)
1866   (buffer-disable-undo (current-buffer))
1867   (setq buffer-read-only t)             ;Disable modification
1868   (setq truncate-lines t)
1869   (setq selective-display t)
1870   (setq selective-display-ellipses t)   ;Display `...'
1871   (gnus-summary-set-display-table)
1872   (gnus-set-default-directory)
1873   (setq gnus-newsgroup-name group)
1874   (make-local-variable 'gnus-summary-line-format)
1875   (make-local-variable 'gnus-summary-line-format-spec)
1876   (make-local-variable 'gnus-summary-dummy-line-format)
1877   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1878   (make-local-variable 'gnus-summary-mark-positions)
1879   (make-local-hook 'post-command-hook)
1880   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1881   (make-local-hook 'pre-command-hook)
1882   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1883   (gnus-run-hooks 'gnus-summary-mode-hook)
1884   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1885   (gnus-update-summary-mark-positions))
1886
1887 (defun gnus-summary-make-local-variables ()
1888   "Make all the local summary buffer variables."
1889   (let ((locals gnus-summary-local-variables)
1890         global local)
1891     (while (setq local (pop locals))
1892       (if (consp local)
1893           (progn
1894             (if (eq (cdr local) 'global)
1895                 ;; Copy the global value of the variable.
1896                 (setq global (symbol-value (car local)))
1897               ;; Use the value from the list.
1898               (setq global (eval (cdr local))))
1899             (make-local-variable (car local))
1900             (set (car local) global))
1901         ;; Simple nil-valued local variable.
1902         (make-local-variable local)
1903         (set local nil)))))
1904
1905 (defun gnus-summary-clear-local-variables ()
1906   (let ((locals gnus-summary-local-variables))
1907     (while locals
1908       (if (consp (car locals))
1909           (and (vectorp (caar locals))
1910                (set (caar locals) nil))
1911         (and (vectorp (car locals))
1912              (set (car locals) nil)))
1913       (setq locals (cdr locals)))))
1914
1915 ;; Summary data functions.
1916
1917 (defmacro gnus-data-number (data)
1918   `(car ,data))
1919
1920 (defmacro gnus-data-set-number (data number)
1921   `(setcar ,data ,number))
1922
1923 (defmacro gnus-data-mark (data)
1924   `(nth 1 ,data))
1925
1926 (defmacro gnus-data-set-mark (data mark)
1927   `(setcar (nthcdr 1 ,data) ,mark))
1928
1929 (defmacro gnus-data-pos (data)
1930   `(nth 2 ,data))
1931
1932 (defmacro gnus-data-set-pos (data pos)
1933   `(setcar (nthcdr 2 ,data) ,pos))
1934
1935 (defmacro gnus-data-header (data)
1936   `(nth 3 ,data))
1937
1938 (defmacro gnus-data-set-header (data header)
1939   `(setf (nth 3 ,data) ,header))
1940
1941 (defmacro gnus-data-level (data)
1942   `(nth 4 ,data))
1943
1944 (defmacro gnus-data-unread-p (data)
1945   `(= (nth 1 ,data) gnus-unread-mark))
1946
1947 (defmacro gnus-data-read-p (data)
1948   `(/= (nth 1 ,data) gnus-unread-mark))
1949
1950 (defmacro gnus-data-pseudo-p (data)
1951   `(consp (nth 3 ,data)))
1952
1953 (defmacro gnus-data-find (number)
1954   `(assq ,number gnus-newsgroup-data))
1955
1956 (defmacro gnus-data-find-list (number &optional data)
1957   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1958      (memq (assq ,number bdata)
1959            bdata)))
1960
1961 (defmacro gnus-data-make (number mark pos header level)
1962   `(list ,number ,mark ,pos ,header ,level))
1963
1964 (defun gnus-data-enter (after-article number mark pos header level offset)
1965   (let ((data (gnus-data-find-list after-article)))
1966     (unless data
1967       (error "No such article: %d" after-article))
1968     (setcdr data (cons (gnus-data-make number mark pos header level)
1969                        (cdr data)))
1970     (setq gnus-newsgroup-data-reverse nil)
1971     (gnus-data-update-list (cddr data) offset)))
1972
1973 (defun gnus-data-enter-list (after-article list &optional offset)
1974   (when list
1975     (let ((data (and after-article (gnus-data-find-list after-article)))
1976           (ilist list))
1977       (if (not (or data
1978                    after-article))
1979           (let ((odata gnus-newsgroup-data))
1980             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
1981             (when offset
1982               (gnus-data-update-list odata offset)))
1983         ;; Find the last element in the list to be spliced into the main
1984         ;; list.
1985         (while (cdr list)
1986           (setq list (cdr list)))
1987         (if (not data)
1988             (progn
1989               (setcdr list gnus-newsgroup-data)
1990               (setq gnus-newsgroup-data ilist)
1991               (when offset
1992                 (gnus-data-update-list (cdr list) offset)))
1993           (setcdr list (cdr data))
1994           (setcdr data ilist)
1995           (when offset
1996             (gnus-data-update-list (cdr list) offset))))
1997       (setq gnus-newsgroup-data-reverse nil))))
1998
1999 (defun gnus-data-remove (article &optional offset)
2000   (let ((data gnus-newsgroup-data))
2001     (if (= (gnus-data-number (car data)) article)
2002         (progn
2003           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2004                 gnus-newsgroup-data-reverse nil)
2005           (when offset
2006             (gnus-data-update-list gnus-newsgroup-data offset)))
2007       (while (cdr data)
2008         (when (= (gnus-data-number (cadr data)) article)
2009           (setcdr data (cddr data))
2010           (when offset
2011             (gnus-data-update-list (cdr data) offset))
2012           (setq data nil
2013                 gnus-newsgroup-data-reverse nil))
2014         (setq data (cdr data))))))
2015
2016 (defmacro gnus-data-list (backward)
2017   `(if ,backward
2018        (or gnus-newsgroup-data-reverse
2019            (setq gnus-newsgroup-data-reverse
2020                  (reverse gnus-newsgroup-data)))
2021      gnus-newsgroup-data))
2022
2023 (defun gnus-data-update-list (data offset)
2024   "Add OFFSET to the POS of all data entries in DATA."
2025   (setq gnus-newsgroup-data-reverse nil)
2026   (while data
2027     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2028     (setq data (cdr data))))
2029
2030 (defun gnus-data-compute-positions ()
2031   "Compute the positions of all articles."
2032   (setq gnus-newsgroup-data-reverse nil)
2033   (let ((data gnus-newsgroup-data))
2034     (save-excursion
2035       (gnus-save-hidden-threads
2036         (goto-char (point-min))
2037         (while data
2038           (while (get-text-property (point) 'gnus-intangible)
2039             (forward-line 1))
2040           (gnus-data-set-pos (car data) (+ (point) 3))
2041           (setq data (cdr data))
2042           (forward-line 1))))))
2043
2044 (defun gnus-summary-article-pseudo-p (article)
2045   "Say whether this article is a pseudo article or not."
2046   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2047
2048 (defmacro gnus-summary-article-sparse-p (article)
2049   "Say whether this article is a sparse article or not."
2050   `(memq ,article gnus-newsgroup-sparse))
2051
2052 (defmacro gnus-summary-article-ancient-p (article)
2053   "Say whether this article is a sparse article or not."
2054   `(memq ,article gnus-newsgroup-ancient))
2055
2056 (defun gnus-article-parent-p (number)
2057   "Say whether this article is a parent or not."
2058   (let ((data (gnus-data-find-list number)))
2059     (and (cdr data)                     ; There has to be an article after...
2060          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2061             (gnus-data-level (nth 1 data))))))
2062
2063 (defun gnus-article-children (number)
2064   "Return a list of all children to NUMBER."
2065   (let* ((data (gnus-data-find-list number))
2066          (level (gnus-data-level (car data)))
2067          children)
2068     (setq data (cdr data))
2069     (while (and data
2070                 (= (gnus-data-level (car data)) (1+ level)))
2071       (push (gnus-data-number (car data)) children)
2072       (setq data (cdr data)))
2073     children))
2074
2075 (defmacro gnus-summary-skip-intangible ()
2076   "If the current article is intangible, then jump to a different article."
2077   '(let ((to (get-text-property (point) 'gnus-intangible)))
2078      (and to (gnus-summary-goto-subject to))))
2079
2080 (defmacro gnus-summary-article-intangible-p ()
2081   "Say whether this article is intangible or not."
2082   '(get-text-property (point) 'gnus-intangible))
2083
2084 (defun gnus-article-read-p (article)
2085   "Say whether ARTICLE is read or not."
2086   (not (or (memq article gnus-newsgroup-marked)
2087            (memq article gnus-newsgroup-unreads)
2088            (memq article gnus-newsgroup-unselected)
2089            (memq article gnus-newsgroup-dormant))))
2090
2091 ;; Some summary mode macros.
2092
2093 (defmacro gnus-summary-article-number ()
2094   "The article number of the article on the current line.
2095 If there isn's an article number here, then we return the current
2096 article number."
2097   '(progn
2098      (gnus-summary-skip-intangible)
2099      (or (get-text-property (point) 'gnus-number)
2100          (gnus-summary-last-subject))))
2101
2102 (defmacro gnus-summary-article-header (&optional number)
2103   "Return the header of article NUMBER."
2104   `(gnus-data-header (gnus-data-find
2105                       ,(or number '(gnus-summary-article-number)))))
2106
2107 (defmacro gnus-summary-thread-level (&optional number)
2108   "Return the level of thread that starts with article NUMBER."
2109   `(if (and (eq gnus-summary-make-false-root 'dummy)
2110             (get-text-property (point) 'gnus-intangible))
2111        0
2112      (gnus-data-level (gnus-data-find
2113                        ,(or number '(gnus-summary-article-number))))))
2114
2115 (defmacro gnus-summary-article-mark (&optional number)
2116   "Return the mark of article NUMBER."
2117   `(gnus-data-mark (gnus-data-find
2118                     ,(or number '(gnus-summary-article-number)))))
2119
2120 (defmacro gnus-summary-article-pos (&optional number)
2121   "Return the position of the line of article NUMBER."
2122   `(gnus-data-pos (gnus-data-find
2123                    ,(or number '(gnus-summary-article-number)))))
2124
2125 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2126 (defmacro gnus-summary-article-subject (&optional number)
2127   "Return current subject string or nil if nothing."
2128   `(let ((headers
2129           ,(if number
2130                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2131              '(gnus-data-header (assq (gnus-summary-article-number)
2132                                       gnus-newsgroup-data)))))
2133      (and headers
2134           (vectorp headers)
2135           (mail-header-subject headers))))
2136
2137 (defmacro gnus-summary-article-score (&optional number)
2138   "Return current article score."
2139   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2140                   gnus-newsgroup-scored))
2141        gnus-summary-default-score 0))
2142
2143 (defun gnus-summary-article-children (&optional number)
2144   "Return a list of article numbers that are children of article NUMBER."
2145   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2146          (level (gnus-data-level (car data)))
2147          l children)
2148     (while (and (setq data (cdr data))
2149                 (> (setq l (gnus-data-level (car data))) level))
2150       (and (= (1+ level) l)
2151            (push (gnus-data-number (car data))
2152                  children)))
2153     (nreverse children)))
2154
2155 (defun gnus-summary-article-parent (&optional number)
2156   "Return the article number of the parent of article NUMBER."
2157   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2158                                     (gnus-data-list t)))
2159          (level (gnus-data-level (car data))))
2160     (if (zerop level)
2161         ()                              ; This is a root.
2162       ;; We search until we find an article with a level less than
2163       ;; this one.  That function has to be the parent.
2164       (while (and (setq data (cdr data))
2165                   (not (< (gnus-data-level (car data)) level))))
2166       (and data (gnus-data-number (car data))))))
2167
2168 (defun gnus-unread-mark-p (mark)
2169   "Say whether MARK is the unread mark."
2170   (= mark gnus-unread-mark))
2171
2172 (defun gnus-read-mark-p (mark)
2173   "Say whether MARK is one of the marks that mark as read.
2174 This is all marks except unread, ticked, dormant, and expirable."
2175   (not (or (= mark gnus-unread-mark)
2176            (= mark gnus-ticked-mark)
2177            (= mark gnus-dormant-mark)
2178            (= mark gnus-expirable-mark))))
2179
2180 (defmacro gnus-article-mark (number)
2181   "Return the MARK of article NUMBER.
2182 This macro should only be used when computing the mark the \"first\"
2183 time; i.e., when generating the summary lines.  After that,
2184 `gnus-summary-article-mark' should be used to examine the
2185 marks of articles."
2186   `(cond
2187     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2188     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2189     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2190     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2191     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2192     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2193     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2194     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2195            gnus-ancient-mark))))
2196
2197 ;; Saving hidden threads.
2198
2199 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2200 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2201
2202 (defmacro gnus-save-hidden-threads (&rest forms)
2203   "Save hidden threads, eval FORMS, and restore the hidden threads."
2204   (let ((config (make-symbol "config")))
2205     `(let ((,config (gnus-hidden-threads-configuration)))
2206        (unwind-protect
2207            (save-excursion
2208              ,@forms)
2209          (gnus-restore-hidden-threads-configuration ,config)))))
2210
2211 (defun gnus-hidden-threads-configuration ()
2212   "Return the current hidden threads configuration."
2213   (save-excursion
2214     (let (config)
2215       (goto-char (point-min))
2216       (while (search-forward "\r" nil t)
2217         (push (1- (point)) config))
2218       config)))
2219
2220 (defun gnus-restore-hidden-threads-configuration (config)
2221   "Restore hidden threads configuration from CONFIG."
2222   (let (point buffer-read-only)
2223     (while (setq point (pop config))
2224       (when (and (< point (point-max))
2225                  (goto-char point)
2226                  (= (following-char) ?\n))
2227         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2228
2229 ;; Various summary mode internalish functions.
2230
2231 (defun gnus-mouse-pick-article (e)
2232   (interactive "e")
2233   (mouse-set-point e)
2234   (gnus-summary-next-page nil t))
2235
2236 (defun gnus-summary-set-display-table ()
2237   ;; Change the display table.  Odd characters have a tendency to mess
2238   ;; up nicely formatted displays - we make all possible glyphs
2239   ;; display only a single character.
2240
2241   ;; We start from the standard display table, if any.
2242   (let ((table (or (copy-sequence standard-display-table)
2243                    (make-display-table)))
2244         (i 32))
2245     ;; Nix out all the control chars...
2246     (while (>= (setq i (1- i)) 0)
2247       (aset table i [??]))
2248     ;; ... but not newline and cr, of course.  (cr is necessary for the
2249     ;; selective display).
2250     (aset table ?\n nil)
2251     (aset table ?\r nil)
2252     ;; We keep TAB as well.
2253     (aset table ?\t nil)
2254     ;; We nix out any glyphs over 126 that are not set already.
2255     (let ((i 256))
2256       (while (>= (setq i (1- i)) 127)
2257         ;; Only modify if the entry is nil.
2258         (unless (aref table i)
2259           (aset table i [??]))))
2260     (setq buffer-display-table table)))
2261
2262 (defun gnus-summary-setup-buffer (group)
2263   "Initialize summary buffer."
2264   (let ((buffer (concat "*Summary " group "*")))
2265     (if (get-buffer buffer)
2266         (progn
2267           (set-buffer buffer)
2268           (setq gnus-summary-buffer (current-buffer))
2269           (not gnus-newsgroup-prepared))
2270       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2271       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2272       (gnus-summary-mode group)
2273       (when gnus-carpal
2274         (gnus-carpal-setup-buffer 'summary))
2275       (unless gnus-single-article-buffer
2276         (make-local-variable 'gnus-article-buffer)
2277         (make-local-variable 'gnus-article-current)
2278         (make-local-variable 'gnus-original-article-buffer))
2279       (setq gnus-newsgroup-name group)
2280       t)))
2281
2282 (defun gnus-set-global-variables ()
2283   ;; Set the global equivalents of the summary buffer-local variables
2284   ;; to the latest values they had.  These reflect the summary buffer
2285   ;; that was in action when the last article was fetched.
2286   (when (eq major-mode 'gnus-summary-mode)
2287     (setq gnus-summary-buffer (current-buffer))
2288     (let ((name gnus-newsgroup-name)
2289           (marked gnus-newsgroup-marked)
2290           (unread gnus-newsgroup-unreads)
2291           (headers gnus-current-headers)
2292           (data gnus-newsgroup-data)
2293           (summary gnus-summary-buffer)
2294           (article-buffer gnus-article-buffer)
2295           (original gnus-original-article-buffer)
2296           (gac gnus-article-current)
2297           (reffed gnus-reffed-article-number)
2298           (score-file gnus-current-score-file))
2299       (save-excursion
2300         (set-buffer gnus-group-buffer)
2301         (setq gnus-newsgroup-name name
2302               gnus-newsgroup-marked marked
2303               gnus-newsgroup-unreads unread
2304               gnus-current-headers headers
2305               gnus-newsgroup-data data
2306               gnus-article-current gac
2307               gnus-summary-buffer summary
2308               gnus-article-buffer article-buffer
2309               gnus-original-article-buffer original
2310               gnus-reffed-article-number reffed
2311               gnus-current-score-file score-file)
2312         ;; The article buffer also has local variables.
2313         (when (gnus-buffer-live-p gnus-article-buffer)
2314           (set-buffer gnus-article-buffer)
2315           (setq gnus-summary-buffer summary))))))
2316
2317 (defun gnus-summary-article-unread-p (article)
2318   "Say whether ARTICLE is unread or not."
2319   (memq article gnus-newsgroup-unreads))
2320
2321 (defun gnus-summary-first-article-p (&optional article)
2322   "Return whether ARTICLE is the first article in the buffer."
2323   (if (not (setq article (or article (gnus-summary-article-number))))
2324       nil
2325     (eq article (caar gnus-newsgroup-data))))
2326
2327 (defun gnus-summary-last-article-p (&optional article)
2328   "Return whether ARTICLE is the last article in the buffer."
2329   (if (not (setq article (or article (gnus-summary-article-number))))
2330       t         ; All non-existent numbers are the last article.  :-)
2331     (not (cdr (gnus-data-find-list article)))))
2332
2333 (defun gnus-make-thread-indent-array ()
2334   (let ((n 200))
2335     (unless (and gnus-thread-indent-array
2336                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2337       (setq gnus-thread-indent-array (make-vector 201 "")
2338             gnus-thread-indent-array-level gnus-thread-indent-level)
2339       (while (>= n 0)
2340         (aset gnus-thread-indent-array n
2341               (make-string (* n gnus-thread-indent-level) ? ))
2342         (setq n (1- n))))))
2343
2344 (defun gnus-update-summary-mark-positions ()
2345   "Compute where the summary marks are to go."
2346   (save-excursion
2347     (when (gnus-buffer-exists-p gnus-summary-buffer)
2348       (set-buffer gnus-summary-buffer))
2349     (let ((gnus-replied-mark 129)
2350           (gnus-score-below-mark 130)
2351           (gnus-score-over-mark 130)
2352           (gnus-download-mark 131)
2353           (spec gnus-summary-line-format-spec)
2354           gnus-visual pos)
2355       (save-excursion
2356         (gnus-set-work-buffer)
2357         (let ((gnus-summary-line-format-spec spec)
2358               (gnus-newsgroup-downloadable '((0 . t))))
2359           (gnus-summary-insert-line
2360            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2361           (goto-char (point-min))
2362           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2363                                              (- (point) 2)))))
2364           (goto-char (point-min))
2365           (push (cons 'replied (and (search-forward "\201" nil t)
2366                                     (- (point) 2)))
2367                 pos)
2368           (goto-char (point-min))
2369           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2370                 pos)
2371           (goto-char (point-min))
2372           (push (cons 'download
2373                       (and (search-forward "\203" nil t) (- (point) 2)))
2374                 pos)))
2375       (setq gnus-summary-mark-positions pos))))
2376
2377 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2378   "Insert a dummy root in the summary buffer."
2379   (beginning-of-line)
2380   (gnus-add-text-properties
2381    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2382    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2383
2384 (defun gnus-summary-insert-line (gnus-tmp-header
2385                                  gnus-tmp-level gnus-tmp-current
2386                                  gnus-tmp-unread gnus-tmp-replied
2387                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2388                                  &optional gnus-tmp-dummy gnus-tmp-score
2389                                  gnus-tmp-process)
2390   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2391          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2392          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2393          (gnus-tmp-score-char
2394           (if (or (null gnus-summary-default-score)
2395                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2396                       gnus-summary-zcore-fuzz))
2397               ? 
2398             (if (< gnus-tmp-score gnus-summary-default-score)
2399                 gnus-score-below-mark gnus-score-over-mark)))
2400          (gnus-tmp-replied
2401           (cond (gnus-tmp-process gnus-process-mark)
2402                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2403                  gnus-cached-mark)
2404                 (gnus-tmp-replied gnus-replied-mark)
2405                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2406                  gnus-saved-mark)
2407                 (t gnus-unread-mark)))
2408          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2409          (gnus-tmp-name
2410           (cond
2411            ((string-match "<[^>]+> *$" gnus-tmp-from)
2412             (let ((beg (match-beginning 0)))
2413               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2414                        (substring gnus-tmp-from (1+ (match-beginning 0))
2415                                   (1- (match-end 0))))
2416                   (substring gnus-tmp-from 0 beg))))
2417            ((string-match "(.+)" gnus-tmp-from)
2418             (substring gnus-tmp-from
2419                        (1+ (match-beginning 0)) (1- (match-end 0))))
2420            (t gnus-tmp-from)))
2421          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2422          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2423          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2424          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2425          (buffer-read-only nil))
2426     (when (string= gnus-tmp-name "")
2427       (setq gnus-tmp-name gnus-tmp-from))
2428     (unless (numberp gnus-tmp-lines)
2429       (setq gnus-tmp-lines 0))
2430     (gnus-put-text-property-excluding-characters-with-faces
2431      (point)
2432      (progn (eval gnus-summary-line-format-spec) (point))
2433      'gnus-number gnus-tmp-number)
2434     (when (gnus-visual-p 'summary-highlight 'highlight)
2435       (forward-line -1)
2436       (gnus-run-hooks 'gnus-summary-update-hook)
2437       (forward-line 1))))
2438
2439 (defun gnus-summary-update-line (&optional dont-update)
2440   ;; Update summary line after change.
2441   (when (and gnus-summary-default-score
2442              (not gnus-summary-inhibit-highlight))
2443     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2444            (article (gnus-summary-article-number))
2445            (score (gnus-summary-article-score article)))
2446       (unless dont-update
2447         (if (and gnus-summary-mark-below
2448                  (< (gnus-summary-article-score)
2449                     gnus-summary-mark-below))
2450             ;; This article has a low score, so we mark it as read.
2451             (when (memq article gnus-newsgroup-unreads)
2452               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2453           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2454             ;; This article was previously marked as read on account
2455             ;; of a low score, but now it has risen, so we mark it as
2456             ;; unread.
2457             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2458         (gnus-summary-update-mark
2459          (if (or (null gnus-summary-default-score)
2460                  (<= (abs (- score gnus-summary-default-score))
2461                      gnus-summary-zcore-fuzz))
2462              ? 
2463            (if (< score gnus-summary-default-score)
2464                gnus-score-below-mark gnus-score-over-mark))
2465          'score))
2466       ;; Do visual highlighting.
2467       (when (gnus-visual-p 'summary-highlight 'highlight)
2468         (gnus-run-hooks 'gnus-summary-update-hook)))))
2469
2470 (defvar gnus-tmp-new-adopts nil)
2471
2472 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2473   "Return the number of articles in THREAD.
2474 This may be 0 in some cases -- if none of the articles in
2475 the thread are to be displayed."
2476   (let* ((number
2477           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2478           (cond
2479            ((not (listp thread))
2480             1)
2481            ((and (consp thread) (cdr thread))
2482             (apply
2483              '+ 1 (mapcar
2484                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2485            ((null thread)
2486             1)
2487            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2488             1)
2489            (t 0))))
2490     (when (and level (zerop level) gnus-tmp-new-adopts)
2491       (incf number
2492             (apply '+ (mapcar
2493                        'gnus-summary-number-of-articles-in-thread
2494                        gnus-tmp-new-adopts))))
2495     (if char
2496         (if (> number 1) gnus-not-empty-thread-mark
2497           gnus-empty-thread-mark)
2498       number)))
2499
2500 (defun gnus-summary-set-local-parameters (group)
2501   "Go through the local params of GROUP and set all variable specs in that list."
2502   (let ((params (gnus-group-find-parameter group))
2503         elem)
2504     (while params
2505       (setq elem (car params)
2506             params (cdr params))
2507       (and (consp elem)                 ; Has to be a cons.
2508            (consp (cdr elem))           ; The cdr has to be a list.
2509            (symbolp (car elem))         ; Has to be a symbol in there.
2510            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2511            (ignore-errors               ; So we set it.
2512              (make-local-variable (car elem))
2513              (set (car elem) (eval (nth 1 elem))))))))
2514
2515 (defun gnus-summary-read-group (group &optional show-all no-article
2516                                       kill-buffer no-display backward
2517                                       select-articles)
2518   "Start reading news in newsgroup GROUP.
2519 If SHOW-ALL is non-nil, already read articles are also listed.
2520 If NO-ARTICLE is non-nil, no article is selected initially.
2521 If NO-DISPLAY, don't generate a summary buffer."
2522   (let (result)
2523     (while (and group
2524                 (null (setq result
2525                             (let ((gnus-auto-select-next nil))
2526                               (or (gnus-summary-read-group-1
2527                                    group show-all no-article
2528                                    kill-buffer no-display
2529                                    select-articles)
2530                                   (setq show-all nil
2531                                    select-articles nil)))))
2532                 (eq gnus-auto-select-next 'quietly))
2533       (set-buffer gnus-group-buffer)
2534       ;; The entry function called above goes to the next
2535       ;; group automatically, so we go two groups back
2536       ;; if we are searching for the previous group.
2537       (when backward
2538         (gnus-group-prev-unread-group 2))
2539       (if (not (equal group (gnus-group-group-name)))
2540           (setq group (gnus-group-group-name))
2541         (setq group nil)))
2542     result))
2543
2544 (defun gnus-summary-read-group-1 (group show-all no-article
2545                                         kill-buffer no-display
2546                                         &optional select-articles)
2547   ;; Killed foreign groups can't be entered.
2548   (when (and (not (gnus-group-native-p group))
2549              (not (gnus-gethash group gnus-newsrc-hashtb)))
2550     (error "Dead non-native groups can't be entered"))
2551   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2552   (let* ((new-group (gnus-summary-setup-buffer group))
2553          (quit-config (gnus-group-quit-config group))
2554          (did-select (and new-group (gnus-select-newsgroup
2555                                      group show-all select-articles))))
2556     (cond
2557      ;; This summary buffer exists already, so we just select it.
2558      ((not new-group)
2559       (gnus-set-global-variables)
2560       (when kill-buffer
2561         (gnus-kill-or-deaden-summary kill-buffer))
2562       (gnus-configure-windows 'summary 'force)
2563       (gnus-set-mode-line 'summary)
2564       (gnus-summary-position-point)
2565       (message "")
2566       t)
2567      ;; We couldn't select this group.
2568      ((null did-select)
2569       (when (and (eq major-mode 'gnus-summary-mode)
2570                  (not (equal (current-buffer) kill-buffer)))
2571         (kill-buffer (current-buffer))
2572         (if (not quit-config)
2573             (progn
2574               ;; Update the info -- marks might need to be removed,
2575               ;; for instance.
2576               (gnus-summary-update-info)
2577               (set-buffer gnus-group-buffer)
2578               (gnus-group-jump-to-group group)
2579               (gnus-group-next-unread-group 1))
2580           (gnus-handle-ephemeral-exit quit-config)))
2581       (gnus-message 3 "Can't select group")
2582       nil)
2583      ;; The user did a `C-g' while prompting for number of articles,
2584      ;; so we exit this group.
2585      ((eq did-select 'quit)
2586       (and (eq major-mode 'gnus-summary-mode)
2587            (not (equal (current-buffer) kill-buffer))
2588            (kill-buffer (current-buffer)))
2589       (when kill-buffer
2590         (gnus-kill-or-deaden-summary kill-buffer))
2591       (if (not quit-config)
2592           (progn
2593             (set-buffer gnus-group-buffer)
2594             (gnus-group-jump-to-group group)
2595             (gnus-group-next-unread-group 1)
2596             (gnus-configure-windows 'group 'force))
2597         (gnus-handle-ephemeral-exit quit-config))
2598       ;; Finally signal the quit.
2599       (signal 'quit nil))
2600      ;; The group was successfully selected.
2601      (t
2602       (gnus-set-global-variables)
2603       ;; Save the active value in effect when the group was entered.
2604       (setq gnus-newsgroup-active
2605             (gnus-copy-sequence
2606              (gnus-active gnus-newsgroup-name)))
2607       ;; You can change the summary buffer in some way with this hook.
2608       (gnus-run-hooks 'gnus-select-group-hook)
2609       ;; Set any local variables in the group parameters.
2610       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2611       (gnus-update-format-specifications
2612        nil 'summary 'summary-mode 'summary-dummy)
2613       ;; Do score processing.
2614       (when gnus-use-scoring
2615         (gnus-possibly-score-headers))
2616       ;; Check whether to fill in the gaps in the threads.
2617       (when gnus-build-sparse-threads
2618         (gnus-build-sparse-threads))
2619       ;; Find the initial limit.
2620       (if gnus-show-threads
2621           (if show-all
2622               (let ((gnus-newsgroup-dormant nil))
2623                 (gnus-summary-initial-limit show-all))
2624             (gnus-summary-initial-limit show-all))
2625         (setq gnus-newsgroup-limit
2626               (mapcar
2627                (lambda (header) (mail-header-number header))
2628                gnus-newsgroup-headers)))
2629       ;; Generate the summary buffer.
2630       (unless no-display
2631         (gnus-summary-prepare))
2632       (when gnus-use-trees
2633         (gnus-tree-open group)
2634         (setq gnus-summary-highlight-line-function
2635               'gnus-tree-highlight-article))
2636       ;; If the summary buffer is empty, but there are some low-scored
2637       ;; articles or some excluded dormants, we include these in the
2638       ;; buffer.
2639       (when (and (zerop (buffer-size))
2640                  (not no-display))
2641         (cond (gnus-newsgroup-dormant
2642                (gnus-summary-limit-include-dormant))
2643               ((and gnus-newsgroup-scored show-all)
2644                (gnus-summary-limit-include-expunged t))))
2645       ;; Function `gnus-apply-kill-file' must be called in this hook.
2646       (gnus-run-hooks 'gnus-apply-kill-hook)
2647       (if (and (zerop (buffer-size))
2648                (not no-display))
2649           (progn
2650             ;; This newsgroup is empty.
2651             (gnus-summary-catchup-and-exit nil t)
2652             (gnus-message 6 "No unread news")
2653             (when kill-buffer
2654               (gnus-kill-or-deaden-summary kill-buffer))
2655             ;; Return nil from this function.
2656             nil)
2657         ;; Hide conversation thread subtrees.  We cannot do this in
2658         ;; gnus-summary-prepare-hook since kill processing may not
2659         ;; work with hidden articles.
2660         (and gnus-show-threads
2661              gnus-thread-hide-subtree
2662              (gnus-summary-hide-all-threads))
2663         (when kill-buffer
2664           (gnus-kill-or-deaden-summary kill-buffer))
2665         ;; Show first unread article if requested.
2666         (if (and (not no-article)
2667                  (not no-display)
2668                  gnus-newsgroup-unreads
2669                  gnus-auto-select-first)
2670             (unless (if (eq gnus-auto-select-first 'best)
2671                         (gnus-summary-best-unread-article)
2672                       (gnus-summary-first-unread-article))
2673               (gnus-configure-windows 'summary))
2674           ;; Don't select any articles, just move point to the first
2675           ;; article in the group.
2676           (goto-char (point-min))
2677           (gnus-summary-position-point)
2678           (gnus-configure-windows 'summary 'force)
2679           (gnus-set-mode-line 'summary))        
2680         (when (get-buffer-window gnus-group-buffer t)
2681           ;; Gotta use windows, because recenter does weird stuff if
2682           ;; the current buffer ain't the displayed window.
2683           (let ((owin (selected-window)))
2684             (select-window (get-buffer-window gnus-group-buffer t))
2685             (when (gnus-group-goto-group group)
2686               (recenter))
2687             (select-window owin)))
2688         ;; Mark this buffer as "prepared".
2689         (setq gnus-newsgroup-prepared t)
2690         (gnus-run-hooks 'gnus-summary-prepared-hook)
2691         t)))))
2692
2693 (defun gnus-summary-prepare ()
2694   "Generate the summary buffer."
2695   (interactive)
2696   (let ((buffer-read-only nil))
2697     (erase-buffer)
2698     (setq gnus-newsgroup-data nil
2699           gnus-newsgroup-data-reverse nil)
2700     (gnus-run-hooks 'gnus-summary-generate-hook)
2701     ;; Generate the buffer, either with threads or without.
2702     (when gnus-newsgroup-headers
2703       (gnus-summary-prepare-threads
2704        (if gnus-show-threads
2705            (gnus-sort-gathered-threads
2706             (funcall gnus-summary-thread-gathering-function
2707                      (gnus-sort-threads
2708                       (gnus-cut-threads (gnus-make-threads)))))
2709          ;; Unthreaded display.
2710          (gnus-sort-articles gnus-newsgroup-headers))))
2711     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2712     ;; Call hooks for modifying summary buffer.
2713     (goto-char (point-min))
2714     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2715
2716 (defsubst gnus-general-simplify-subject (subject)
2717   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2718   (setq subject
2719         (cond
2720          ;; Truncate the subject.
2721          (gnus-simplify-subject-functions
2722           (gnus-map-function gnus-simplify-subject-functions subject))
2723          ((numberp gnus-summary-gather-subject-limit)
2724           (setq subject (gnus-simplify-subject-re subject))
2725           (if (> (length subject) gnus-summary-gather-subject-limit)
2726               (substring subject 0 gnus-summary-gather-subject-limit)
2727             subject))
2728          ;; Fuzzily simplify it.
2729          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2730           (gnus-simplify-subject-fuzzy subject))
2731          ;; Just remove the leading "Re:".
2732          (t
2733           (gnus-simplify-subject-re subject))))
2734
2735   (if (and gnus-summary-gather-exclude-subject
2736            (string-match gnus-summary-gather-exclude-subject subject))
2737       nil                               ; This article shouldn't be gathered
2738     subject))
2739
2740 (defun gnus-summary-simplify-subject-query ()
2741   "Query where the respool algorithm would put this article."
2742   (interactive)
2743   (gnus-summary-select-article)
2744   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2745
2746 (defun gnus-gather-threads-by-subject (threads)
2747   "Gather threads by looking at Subject headers."
2748   (if (not gnus-summary-make-false-root)
2749       threads
2750     (let ((hashtb (gnus-make-hashtable 1024))
2751           (prev threads)
2752           (result threads)
2753           subject hthread whole-subject)
2754       (while threads
2755         (setq subject (gnus-general-simplify-subject
2756                        (setq whole-subject (mail-header-subject
2757                                             (caar threads)))))
2758         (when subject
2759           (if (setq hthread (gnus-gethash subject hashtb))
2760               (progn
2761                 ;; We enter a dummy root into the thread, if we
2762                 ;; haven't done that already.
2763                 (unless (stringp (caar hthread))
2764                   (setcar hthread (list whole-subject (car hthread))))
2765                 ;; We add this new gathered thread to this gathered
2766                 ;; thread.
2767                 (setcdr (car hthread)
2768                         (nconc (cdar hthread) (list (car threads))))
2769                 ;; Remove it from the list of threads.
2770                 (setcdr prev (cdr threads))
2771                 (setq threads prev))
2772             ;; Enter this thread into the hash table.
2773             (gnus-sethash subject threads hashtb)))
2774         (setq prev threads)
2775         (setq threads (cdr threads)))
2776       result)))
2777
2778 (defun gnus-gather-threads-by-references (threads)
2779   "Gather threads by looking at References headers."
2780   (let ((idhashtb (gnus-make-hashtable 1024))
2781         (thhashtb (gnus-make-hashtable 1024))
2782         (prev threads)
2783         (result threads)
2784         ids references id gthread gid entered ref)
2785     (while threads
2786       (when (setq references (mail-header-references (caar threads)))
2787         (setq id (mail-header-id (caar threads))
2788               ids (gnus-split-references references)
2789               entered nil)
2790         (while (setq ref (pop ids))
2791           (setq ids (delete ref ids))
2792           (if (not (setq gid (gnus-gethash ref idhashtb)))
2793               (progn
2794                 (gnus-sethash ref id idhashtb)
2795                 (gnus-sethash id threads thhashtb))
2796             (setq gthread (gnus-gethash gid thhashtb))
2797             (unless entered
2798               ;; We enter a dummy root into the thread, if we
2799               ;; haven't done that already.
2800               (unless (stringp (caar gthread))
2801                 (setcar gthread (list (mail-header-subject (caar gthread))
2802                                       (car gthread))))
2803               ;; We add this new gathered thread to this gathered
2804               ;; thread.
2805               (setcdr (car gthread)
2806                       (nconc (cdar gthread) (list (car threads)))))
2807             ;; Add it into the thread hash table.
2808             (gnus-sethash id gthread thhashtb)
2809             (setq entered t)
2810             ;; Remove it from the list of threads.
2811             (setcdr prev (cdr threads))
2812             (setq threads prev))))
2813       (setq prev threads)
2814       (setq threads (cdr threads)))
2815     result))
2816
2817 (defun gnus-sort-gathered-threads (threads)
2818   "Sort subtreads inside each gathered thread by article number."
2819   (let ((result threads))
2820     (while threads
2821       (when (stringp (caar threads))
2822         (setcdr (car threads)
2823                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2824       (setq threads (cdr threads)))
2825     result))
2826
2827 (defun gnus-thread-loop-p (root thread)
2828   "Say whether ROOT is in THREAD."
2829   (let ((stack (list thread))
2830         (infloop 0)
2831         th)
2832     (while (setq thread (pop stack))
2833       (setq th (cdr thread))
2834       (while (and th
2835                   (not (eq (caar th) root)))
2836         (pop th))
2837       (if th
2838           ;; We have found a loop.
2839           (let (ref-dep)
2840             (setcdr thread (delq (car th) (cdr thread)))
2841             (if (boundp (setq ref-dep (intern "none"
2842                                               gnus-newsgroup-dependencies)))
2843                 (setcdr (symbol-value ref-dep)
2844                         (nconc (cdr (symbol-value ref-dep))
2845                                (list (car th))))
2846               (set ref-dep (list nil (car th))))
2847             (setq infloop 1
2848                   stack nil))
2849         ;; Push all the subthreads onto the stack.
2850         (push (cdr thread) stack)))
2851     infloop))
2852
2853 (defun gnus-make-threads ()
2854   "Go through the dependency hashtb and find the roots.  Return all threads."
2855   (let (threads)
2856     (while (catch 'infloop
2857              (mapatoms
2858               (lambda (refs)
2859                 ;; Deal with self-referencing References loops.
2860                 (when (and (car (symbol-value refs))
2861                            (not (zerop
2862                                  (apply
2863                                   '+
2864                                   (mapcar
2865                                    (lambda (thread)
2866                                      (gnus-thread-loop-p
2867                                       (car (symbol-value refs)) thread))
2868                                    (cdr (symbol-value refs)))))))
2869                   (setq threads nil)
2870                   (throw 'infloop t))
2871                 (unless (car (symbol-value refs))
2872                   ;; These threads do not refer back to any other articles,
2873                   ;; so they're roots.
2874                   (setq threads (append (cdr (symbol-value refs)) threads))))
2875               gnus-newsgroup-dependencies)))
2876     threads))
2877
2878 ;; Build the thread tree.
2879 (defun gnus-dependencies-add-header (header dependencies force-new)
2880   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2881
2882 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2883 if it was already present.
2884
2885 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2886 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2887 Message-IDs will be renamed be renamed to a unique Message-ID before
2888 being entered.
2889
2890 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2891   (let* ((id (mail-header-id header))
2892          (id-dep (and id (intern id dependencies)))
2893          ref ref-dep ref-header)
2894     ;; Enter this `header' in the `dependencies' table.
2895     (cond
2896      ((not id-dep)
2897       (setq header nil))
2898      ;; The first two cases do the normal part: enter a new `header'
2899      ;; in the `dependencies' table.
2900      ((not (boundp id-dep))
2901       (set id-dep (list header)))
2902      ((null (car (symbol-value id-dep)))
2903       (setcar (symbol-value id-dep) header))
2904
2905      ;; From here the `header' was already present in the
2906      ;; `dependencies' table.
2907      (force-new
2908       ;; Overrides an existing entry;
2909       ;; just set the header part of the entry.
2910       (setcar (symbol-value id-dep) header))
2911
2912      ;; Renames the existing `header' to a unique Message-ID.
2913      ((not gnus-summary-ignore-duplicates)
2914       ;; An article with this Message-ID has already been seen.
2915       ;; We rename the Message-ID.
2916       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
2917            (list header))
2918       (mail-header-set-id header id))
2919
2920      ;; The last case ignores an existing entry, except it adds any
2921      ;; additional Xrefs (in case the two articles came from different
2922      ;; servers.
2923      ;; Also sets `header' to `nil' meaning that the `dependencies'
2924      ;; table was *not* modified.
2925      (t
2926       (mail-header-set-xref
2927        (car (symbol-value id-dep))
2928        (concat (or (mail-header-xref (car (symbol-value id-dep)))
2929                    "")
2930                (or (mail-header-xref header) "")))
2931       (setq header nil)))
2932
2933     (when header
2934       ;; First check if that we are not creating a References loop.
2935       (setq ref (gnus-parent-id (mail-header-references header)))
2936       (while (and ref
2937                   (setq ref-dep (intern-soft ref dependencies))
2938                   (boundp ref-dep)
2939                   (setq ref-header (car (symbol-value ref-dep))))
2940         (if (string= id ref)
2941             ;; Yuk!  This is a reference loop.  Make the article be a
2942             ;; root article.
2943             (progn
2944               (mail-header-set-references (car (symbol-value id-dep)) "none")
2945               (setq ref nil))
2946           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
2947       (setq ref (gnus-parent-id (mail-header-references header)))
2948       (setq ref-dep (intern (or ref "none") dependencies))
2949       (if (boundp ref-dep)
2950           (setcdr (symbol-value ref-dep)
2951                   (nconc (cdr (symbol-value ref-dep))
2952                          (list (symbol-value id-dep))))
2953         (set ref-dep (list nil (symbol-value id-dep)))))
2954     header))
2955
2956 (defun gnus-build-sparse-threads ()
2957   (let ((headers gnus-newsgroup-headers)
2958         (gnus-summary-ignore-duplicates t)
2959         header references generation relations
2960         subject child end new-child date)
2961     ;; First we create an alist of generations/relations, where
2962     ;; generations is how much we trust the relation, and the relation
2963     ;; is parent/child.
2964     (gnus-message 7 "Making sparse threads...")
2965     (save-excursion
2966       (nnheader-set-temp-buffer " *gnus sparse threads*")
2967       (while (setq header (pop headers))
2968         (when (and (setq references (mail-header-references header))
2969                    (not (string= references "")))
2970           (insert references)
2971           (setq child (mail-header-id header)
2972                 subject (mail-header-subject header)
2973                 date (mail-header-date header)
2974                 generation 0)
2975           (while (search-backward ">" nil t)
2976             (setq end (1+ (point)))
2977             (when (search-backward "<" nil t)
2978               (setq new-child (buffer-substring (point) end))
2979               (push (list (incf generation)
2980                           child (setq child new-child)
2981                           subject date)
2982                     relations)))
2983           (when child
2984             (push (list (1+ generation) child nil subject) relations))
2985           (erase-buffer)))
2986       (kill-buffer (current-buffer)))
2987     ;; Sort over trustworthiness.
2988     (mapcar
2989      (lambda (relation)
2990        (when (gnus-dependencies-add-header
2991               (make-full-mail-header
2992                gnus-reffed-article-number
2993                (nth 3 relation) "" (or (nth 4 relation) "")
2994                (nth 1 relation)
2995                (or (nth 2 relation) "") 0 0 "")
2996               gnus-newsgroup-dependencies nil)
2997          (push gnus-reffed-article-number gnus-newsgroup-limit)
2998          (push gnus-reffed-article-number gnus-newsgroup-sparse)
2999          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3000                gnus-newsgroup-reads)
3001          (decf gnus-reffed-article-number)))
3002      (sort relations 'car-less-than-car))
3003     (gnus-message 7 "Making sparse threads...done")))
3004
3005 (defun gnus-build-old-threads ()
3006   ;; Look at all the articles that refer back to old articles, and
3007   ;; fetch the headers for the articles that aren't there.  This will
3008   ;; build complete threads - if the roots haven't been expired by the
3009   ;; server, that is.
3010   (let (id heads)
3011     (mapatoms
3012      (lambda (refs)
3013        (when (not (car (symbol-value refs)))
3014          (setq heads (cdr (symbol-value refs)))
3015          (while heads
3016            (if (memq (mail-header-number (caar heads))
3017                      gnus-newsgroup-dormant)
3018                (setq heads (cdr heads))
3019              (setq id (symbol-name refs))
3020              (while (and (setq id (gnus-build-get-header id))
3021                          (not (car (gnus-id-to-thread id)))))
3022              (setq heads nil)))))
3023      gnus-newsgroup-dependencies)))
3024
3025 ;; The following macros and functions were written by Felix Lee
3026 ;; <flee@cse.psu.edu>.
3027
3028 (defmacro gnus-nov-read-integer ()
3029   '(prog1
3030        (if (= (following-char) ?\t)
3031            0
3032          (let ((num (ignore-errors (read buffer))))
3033            (if (numberp num) num 0)))
3034      (unless (eobp)
3035        (search-forward "\t" eol 'move))))
3036
3037 (defmacro gnus-nov-skip-field ()
3038   '(search-forward "\t" eol 'move))
3039
3040 (defmacro gnus-nov-field ()
3041   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
3042
3043 ;; This function has to be called with point after the article number
3044 ;; on the beginning of the line.
3045 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3046   (let ((eol (gnus-point-at-eol))
3047         (buffer (current-buffer))
3048         header)
3049
3050     ;; overview: [num subject from date id refs chars lines misc]
3051     (unwind-protect
3052         (progn
3053           (narrow-to-region (point) eol)
3054           (unless (eobp)
3055             (forward-char))
3056
3057           (setq header
3058                 (make-full-mail-header
3059                  number                 ; number
3060                  (rfc2047-decode-string (gnus-nov-field)) ; subject
3061                  (rfc2047-decode-string (gnus-nov-field)) ; from
3062                  (gnus-nov-field)       ; date
3063                  (or (gnus-nov-field)
3064                      (nnheader-generate-fake-message-id)) ; id
3065                  (gnus-nov-field)       ; refs
3066                  (gnus-nov-read-integer) ; chars
3067                  (gnus-nov-read-integer) ; lines
3068                  (unless (= (following-char) ?\n)
3069                    (gnus-nov-field))))) ; misc
3070
3071       (widen))
3072
3073     (when gnus-alter-header-function
3074       (funcall gnus-alter-header-function header))
3075     (gnus-dependencies-add-header header dependencies force-new)))
3076
3077 (defun gnus-build-get-header (id)
3078   ;; Look through the buffer of NOV lines and find the header to
3079   ;; ID.  Enter this line into the dependencies hash table, and return
3080   ;; the id of the parent article (if any).
3081   (let ((deps gnus-newsgroup-dependencies)
3082         found header)
3083     (prog1
3084         (save-excursion
3085           (set-buffer nntp-server-buffer)
3086           (let ((case-fold-search nil))
3087             (goto-char (point-min))
3088             (while (and (not found)
3089                         (search-forward id nil t))
3090               (beginning-of-line)
3091               (setq found (looking-at
3092                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3093                                    (regexp-quote id))))
3094               (or found (beginning-of-line 2)))
3095             (when found
3096               (beginning-of-line)
3097               (and
3098                (setq header (gnus-nov-parse-line
3099                              (read (current-buffer)) deps))
3100                (gnus-parent-id (mail-header-references header))))))
3101       (when header
3102         (let ((number (mail-header-number header)))
3103           (push number gnus-newsgroup-limit)
3104           (push header gnus-newsgroup-headers)
3105           (if (memq number gnus-newsgroup-unselected)
3106               (progn
3107                 (push number gnus-newsgroup-unreads)
3108                 (setq gnus-newsgroup-unselected
3109                       (delq number gnus-newsgroup-unselected)))
3110             (push number gnus-newsgroup-ancient)))))))
3111
3112 (defun gnus-build-all-threads ()
3113   "Read all the headers."
3114   (let ((gnus-summary-ignore-duplicates t)
3115         (dependencies gnus-newsgroup-dependencies)
3116         header article)
3117     (save-excursion
3118       (set-buffer nntp-server-buffer)
3119       (let ((case-fold-search nil))
3120         (goto-char (point-min))
3121         (while (not (eobp))
3122           (ignore-errors
3123             (setq article (read (current-buffer))
3124                   header (gnus-nov-parse-line
3125                           article dependencies)))
3126           (when header
3127             (save-excursion
3128               (set-buffer gnus-summary-buffer)
3129               (push header gnus-newsgroup-headers)
3130               (if (memq (setq article (mail-header-number header))
3131                         gnus-newsgroup-unselected)
3132                   (progn
3133                     (push article gnus-newsgroup-unreads)
3134                     (setq gnus-newsgroup-unselected
3135                           (delq article gnus-newsgroup-unselected)))
3136                 (push article gnus-newsgroup-ancient)))
3137             (forward-line 1)))))))
3138
3139 (defun gnus-summary-update-article-line (article header)
3140   "Update the line for ARTICLE using HEADERS."
3141   (let* ((id (mail-header-id header))
3142          (thread (gnus-id-to-thread id)))
3143     (unless thread
3144       (error "Article in no thread"))
3145     ;; Update the thread.
3146     (setcar thread header)
3147     (gnus-summary-goto-subject article)
3148     (let* ((datal (gnus-data-find-list article))
3149            (data (car datal))
3150            (length (when (cdr datal)
3151                      (- (gnus-data-pos data)
3152                         (gnus-data-pos (cadr datal)))))
3153            (buffer-read-only nil)
3154            (level (gnus-summary-thread-level)))
3155       (gnus-delete-line)
3156       (gnus-summary-insert-line
3157        header level nil (gnus-article-mark article)
3158        (memq article gnus-newsgroup-replied)
3159        (memq article gnus-newsgroup-expirable)
3160        ;; Only insert the Subject string when it's different
3161        ;; from the previous Subject string.
3162        (if (gnus-subject-equal
3163             (condition-case ()
3164                 (mail-header-subject
3165                  (gnus-data-header
3166                   (cadr
3167                    (gnus-data-find-list
3168                     article
3169                     (gnus-data-list t)))))
3170               ;; Error on the side of excessive subjects.
3171               (error ""))
3172             (mail-header-subject header))
3173            ""
3174          (mail-header-subject header))
3175        nil (cdr (assq article gnus-newsgroup-scored))
3176        (memq article gnus-newsgroup-processable))
3177       (when length
3178         (gnus-data-update-list
3179          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3180
3181 (defun gnus-summary-update-article (article &optional iheader)
3182   "Update ARTICLE in the summary buffer."
3183   (set-buffer gnus-summary-buffer)
3184   (let* ((header (gnus-summary-article-header article))
3185          (id (mail-header-id header))
3186          (data (gnus-data-find article))
3187          (thread (gnus-id-to-thread id))
3188          (references (mail-header-references header))
3189          (parent
3190           (gnus-id-to-thread
3191            (or (gnus-parent-id
3192                 (when (and references
3193                            (not (equal "" references)))
3194                   references))
3195                "none")))
3196          (buffer-read-only nil)
3197          (old (car thread)))
3198     (when thread
3199       (unless iheader
3200         (setcar thread nil)
3201         (when parent
3202           (delq thread parent)))
3203       (if (gnus-summary-insert-subject id header)
3204           ;; Set the (possibly) new article number in the data structure.
3205           (gnus-data-set-number data (gnus-id-to-article id))
3206         (setcar thread old)
3207         nil))))
3208
3209 (defun gnus-rebuild-thread (id &optional line)
3210   "Rebuild the thread containing ID.
3211 If LINE, insert the rebuilt thread starting on line LINE."
3212   (let ((buffer-read-only nil)
3213         old-pos current thread data)
3214     (if (not gnus-show-threads)
3215         (setq thread (list (car (gnus-id-to-thread id))))
3216       ;; Get the thread this article is part of.
3217       (setq thread (gnus-remove-thread id)))
3218     (setq old-pos (gnus-point-at-bol))
3219     (setq current (save-excursion
3220                     (and (zerop (forward-line -1))
3221                          (gnus-summary-article-number))))
3222     ;; If this is a gathered thread, we have to go some re-gathering.
3223     (when (stringp (car thread))
3224       (let ((subject (car thread))
3225             roots thr)
3226         (setq thread (cdr thread))
3227         (while thread
3228           (unless (memq (setq thr (gnus-id-to-thread
3229                                    (gnus-root-id
3230                                     (mail-header-id (caar thread)))))
3231                         roots)
3232             (push thr roots))
3233           (setq thread (cdr thread)))
3234         ;; We now have all (unique) roots.
3235         (if (= (length roots) 1)
3236             ;; All the loose roots are now one solid root.
3237             (setq thread (car roots))
3238           (setq thread (cons subject (gnus-sort-threads roots))))))
3239     (let (threads)
3240       ;; We then insert this thread into the summary buffer.
3241       (when line
3242         (goto-char (point-min))
3243         (forward-line (1- line)))
3244       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3245         (if gnus-show-threads
3246             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3247           (gnus-summary-prepare-unthreaded thread))
3248         (setq data (nreverse gnus-newsgroup-data))
3249         (setq threads gnus-newsgroup-threads))
3250       ;; We splice the new data into the data structure.
3251       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3252       ;;!!! then we want to insert at the beginning of the buffer.
3253       ;;!!! That happens to be true with Gnus now, but that may
3254       ;;!!! change in the future.  Perhaps.
3255       (gnus-data-enter-list
3256        (if line nil current) data (- (point) old-pos))
3257       (setq gnus-newsgroup-threads
3258             (nconc threads gnus-newsgroup-threads))
3259       (gnus-data-compute-positions))))
3260
3261 (defun gnus-number-to-header (number)
3262   "Return the header for article NUMBER."
3263   (let ((headers gnus-newsgroup-headers))
3264     (while (and headers
3265                 (not (= number (mail-header-number (car headers)))))
3266       (pop headers))
3267     (when headers
3268       (car headers))))
3269
3270 (defun gnus-parent-headers (in-headers &optional generation)
3271   "Return the headers of the GENERATIONeth parent of HEADERS."
3272   (unless generation
3273     (setq generation 1))
3274   (let ((parent t)
3275         (headers in-headers)
3276         references)
3277     (while (and parent
3278                 (not (zerop generation))
3279                 (setq references (mail-header-references headers)))
3280       (setq headers (if (and references
3281                              (setq parent (gnus-parent-id references)))
3282                         (car (gnus-id-to-thread parent))
3283                       nil))
3284       (decf generation))
3285     (and (not (eq headers in-headers))
3286          headers)))
3287
3288 (defun gnus-id-to-thread (id)
3289   "Return the (sub-)thread where ID appears."
3290   (gnus-gethash id gnus-newsgroup-dependencies))
3291
3292 (defun gnus-id-to-article (id)
3293   "Return the article number of ID."
3294   (let ((thread (gnus-id-to-thread id)))
3295     (when (and thread
3296                (car thread))
3297       (mail-header-number (car thread)))))
3298
3299 (defun gnus-id-to-header (id)
3300   "Return the article headers of ID."
3301   (car (gnus-id-to-thread id)))
3302
3303 (defun gnus-article-displayed-root-p (article)
3304   "Say whether ARTICLE is a root(ish) article."
3305   (let ((level (gnus-summary-thread-level article))
3306         (refs (mail-header-references  (gnus-summary-article-header article)))
3307         particle)
3308     (cond
3309      ((null level) nil)
3310      ((zerop level) t)
3311      ((null refs) t)
3312      ((null (gnus-parent-id refs)) t)
3313      ((and (= 1 level)
3314            (null (setq particle (gnus-id-to-article
3315                                  (gnus-parent-id refs))))
3316            (null (gnus-summary-thread-level particle)))))))
3317
3318 (defun gnus-root-id (id)
3319   "Return the id of the root of the thread where ID appears."
3320   (let (last-id prev)
3321     (while (and id (setq prev (car (gnus-id-to-thread id))))
3322       (setq last-id id
3323             id (gnus-parent-id (mail-header-references prev))))
3324     last-id))
3325
3326 (defun gnus-articles-in-thread (thread)
3327   "Return the list of articles in THREAD."
3328   (cons (mail-header-number (car thread))
3329         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3330
3331 (defun gnus-remove-thread (id &optional dont-remove)
3332   "Remove the thread that has ID in it."
3333   (let (headers thread last-id)
3334     ;; First go up in this thread until we find the root.
3335     (setq last-id (gnus-root-id id)
3336           headers (message-flatten-list (gnus-id-to-thread last-id)))
3337     ;; We have now found the real root of this thread.  It might have
3338     ;; been gathered into some loose thread, so we have to search
3339     ;; through the threads to find the thread we wanted.
3340     (let ((threads gnus-newsgroup-threads)
3341           sub)
3342       (while threads
3343         (setq sub (car threads))
3344         (if (stringp (car sub))
3345             ;; This is a gathered thread, so we look at the roots
3346             ;; below it to find whether this article is in this
3347             ;; gathered root.
3348             (progn
3349               (setq sub (cdr sub))
3350               (while sub
3351                 (when (member (caar sub) headers)
3352                   (setq thread (car threads)
3353                         threads nil
3354                         sub nil))
3355                 (setq sub (cdr sub))))
3356           ;; It's an ordinary thread, so we check it.
3357           (when (eq (car sub) (car headers))
3358             (setq thread sub
3359                   threads nil)))
3360         (setq threads (cdr threads)))
3361       ;; If this article is in no thread, then it's a root.
3362       (if thread
3363           (unless dont-remove
3364             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3365         (setq thread (gnus-id-to-thread last-id)))
3366       (when thread
3367         (prog1
3368             thread                      ; We return this thread.
3369           (unless dont-remove
3370             (if (stringp (car thread))
3371                 (progn
3372                   ;; If we use dummy roots, then we have to remove the
3373                   ;; dummy root as well.
3374                   (when (eq gnus-summary-make-false-root 'dummy)
3375                     ;; We go to the dummy root by going to
3376                     ;; the first sub-"thread", and then one line up.
3377                     (gnus-summary-goto-article
3378                      (mail-header-number (caadr thread)))
3379                     (forward-line -1)
3380                     (gnus-delete-line)
3381                     (gnus-data-compute-positions))
3382                   (setq thread (cdr thread))
3383                   (while thread
3384                     (gnus-remove-thread-1 (car thread))
3385                     (setq thread (cdr thread))))
3386               (gnus-remove-thread-1 thread))))))))
3387
3388 (defun gnus-remove-thread-1 (thread)
3389   "Remove the thread THREAD recursively."
3390   (let ((number (mail-header-number (pop thread)))
3391         d)
3392     (setq thread (reverse thread))
3393     (while thread
3394       (gnus-remove-thread-1 (pop thread)))
3395     (when (setq d (gnus-data-find number))
3396       (goto-char (gnus-data-pos d))
3397       (gnus-data-remove
3398        number
3399        (- (gnus-point-at-bol)
3400           (prog1
3401               (1+ (gnus-point-at-eol))
3402             (gnus-delete-line)))))))
3403
3404 (defun gnus-sort-threads (threads)
3405   "Sort THREADS."
3406   (if (not gnus-thread-sort-functions)
3407       threads
3408     (gnus-message 8 "Sorting threads...")
3409     (prog1
3410         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3411       (gnus-message 8 "Sorting threads...done"))))
3412
3413 (defun gnus-sort-articles (articles)
3414   "Sort ARTICLES."
3415   (when gnus-article-sort-functions
3416     (gnus-message 7 "Sorting articles...")
3417     (prog1
3418         (setq gnus-newsgroup-headers
3419               (sort articles (gnus-make-sort-function
3420                               gnus-article-sort-functions)))
3421       (gnus-message 7 "Sorting articles...done"))))
3422
3423 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3424 (defmacro gnus-thread-header (thread)
3425   ;; Return header of first article in THREAD.
3426   ;; Note that THREAD must never, ever be anything else than a variable -
3427   ;; using some other form will lead to serious barfage.
3428   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3429   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3430   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3431         (vector thread) 2))
3432
3433 (defsubst gnus-article-sort-by-number (h1 h2)
3434   "Sort articles by article number."
3435   (< (mail-header-number h1)
3436      (mail-header-number h2)))
3437
3438 (defun gnus-thread-sort-by-number (h1 h2)
3439   "Sort threads by root article number."
3440   (gnus-article-sort-by-number
3441    (gnus-thread-header h1) (gnus-thread-header h2)))
3442
3443 (defsubst gnus-article-sort-by-lines (h1 h2)
3444   "Sort articles by article Lines header."
3445   (< (mail-header-lines h1)
3446      (mail-header-lines h2)))
3447
3448 (defun gnus-thread-sort-by-lines (h1 h2)
3449   "Sort threads by root article Lines header."
3450   (gnus-article-sort-by-lines
3451    (gnus-thread-header h1) (gnus-thread-header h2)))
3452
3453 (defsubst gnus-article-sort-by-author (h1 h2)
3454   "Sort articles by root author."
3455   (string-lessp
3456    (let ((extract (funcall
3457                    gnus-extract-address-components
3458                    (mail-header-from h1))))
3459      (or (car extract) (cadr extract) ""))
3460    (let ((extract (funcall
3461                    gnus-extract-address-components
3462                    (mail-header-from h2))))
3463      (or (car extract) (cadr extract) ""))))
3464
3465 (defun gnus-thread-sort-by-author (h1 h2)
3466   "Sort threads by root author."
3467   (gnus-article-sort-by-author
3468    (gnus-thread-header h1)  (gnus-thread-header h2)))
3469
3470 (defsubst gnus-article-sort-by-subject (h1 h2)
3471   "Sort articles by root subject."
3472   (string-lessp
3473    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3474    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3475
3476 (defun gnus-thread-sort-by-subject (h1 h2)
3477   "Sort threads by root subject."
3478   (gnus-article-sort-by-subject
3479    (gnus-thread-header h1) (gnus-thread-header h2)))
3480
3481 (defsubst gnus-article-sort-by-date (h1 h2)
3482   "Sort articles by root article date."
3483   (gnus-time-less
3484    (gnus-date-get-time (mail-header-date h1))
3485    (gnus-date-get-time (mail-header-date h2))))
3486
3487 (defun gnus-thread-sort-by-date (h1 h2)
3488   "Sort threads by root article date."
3489   (gnus-article-sort-by-date
3490    (gnus-thread-header h1) (gnus-thread-header h2)))
3491
3492 (defsubst gnus-article-sort-by-score (h1 h2)
3493   "Sort articles by root article score.
3494 Unscored articles will be counted as having a score of zero."
3495   (> (or (cdr (assq (mail-header-number h1)
3496                     gnus-newsgroup-scored))
3497          gnus-summary-default-score 0)
3498      (or (cdr (assq (mail-header-number h2)
3499                     gnus-newsgroup-scored))
3500          gnus-summary-default-score 0)))
3501
3502 (defun gnus-thread-sort-by-score (h1 h2)
3503   "Sort threads by root article score."
3504   (gnus-article-sort-by-score
3505    (gnus-thread-header h1) (gnus-thread-header h2)))
3506
3507 (defun gnus-thread-sort-by-total-score (h1 h2)
3508   "Sort threads by the sum of all scores in the thread.
3509 Unscored articles will be counted as having a score of zero."
3510   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3511
3512 (defun gnus-thread-total-score (thread)
3513   ;;  This function find the total score of THREAD.
3514   (cond ((null thread)
3515          0)
3516         ((consp thread)
3517          (if (stringp (car thread))
3518              (apply gnus-thread-score-function 0
3519                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3520            (gnus-thread-total-score-1 thread)))
3521         (t
3522          (gnus-thread-total-score-1 (list thread)))))
3523
3524 (defun gnus-thread-total-score-1 (root)
3525   ;; This function find the total score of the thread below ROOT.
3526   (setq root (car root))
3527   (apply gnus-thread-score-function
3528          (or (append
3529               (mapcar 'gnus-thread-total-score
3530                       (cdr (gnus-id-to-thread (mail-header-id root))))
3531               (when (> (mail-header-number root) 0)
3532                 (list (or (cdr (assq (mail-header-number root)
3533                                      gnus-newsgroup-scored))
3534                           gnus-summary-default-score 0))))
3535              (list gnus-summary-default-score)
3536              '(0))))
3537
3538 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3539 (defvar gnus-tmp-prev-subject nil)
3540 (defvar gnus-tmp-false-parent nil)
3541 (defvar gnus-tmp-root-expunged nil)
3542 (defvar gnus-tmp-dummy-line nil)
3543
3544 (defun gnus-summary-prepare-threads (threads)
3545   "Prepare summary buffer from THREADS and indentation LEVEL.
3546 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3547 or a straight list of headers."
3548   (gnus-message 7 "Generating summary...")
3549
3550   (setq gnus-newsgroup-threads threads)
3551   (beginning-of-line)
3552
3553   (let ((gnus-tmp-level 0)
3554         (default-score (or gnus-summary-default-score 0))
3555         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3556         thread number subject stack state gnus-tmp-gathered beg-match
3557         new-roots gnus-tmp-new-adopts thread-end
3558         gnus-tmp-header gnus-tmp-unread
3559         gnus-tmp-replied gnus-tmp-subject-or-nil
3560         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3561         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3562         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3563
3564     (setq gnus-tmp-prev-subject nil)
3565
3566     (if (vectorp (car threads))
3567         ;; If this is a straight (sic) list of headers, then a
3568         ;; threaded summary display isn't required, so we just create
3569         ;; an unthreaded one.
3570         (gnus-summary-prepare-unthreaded threads)
3571
3572       ;; Do the threaded display.
3573
3574       (while (or threads stack gnus-tmp-new-adopts new-roots)
3575
3576         (if (and (= gnus-tmp-level 0)
3577                  (or (not stack)
3578                      (= (caar stack) 0))
3579                  (not gnus-tmp-false-parent)
3580                  (or gnus-tmp-new-adopts new-roots))
3581             (if gnus-tmp-new-adopts
3582                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3583                       thread (list (car gnus-tmp-new-adopts))
3584                       gnus-tmp-header (caar thread)
3585                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3586               (when new-roots
3587                 (setq thread (list (car new-roots))
3588                       gnus-tmp-header (caar thread)
3589                       new-roots (cdr new-roots))))
3590
3591           (if threads
3592               ;; If there are some threads, we do them before the
3593               ;; threads on the stack.
3594               (setq thread threads
3595                     gnus-tmp-header (caar thread))
3596             ;; There were no current threads, so we pop something off
3597             ;; the stack.
3598             (setq state (car stack)
3599                   gnus-tmp-level (car state)
3600                   thread (cdr state)
3601                   stack (cdr stack)
3602                   gnus-tmp-header (caar thread))))
3603
3604         (setq gnus-tmp-false-parent nil)
3605         (setq gnus-tmp-root-expunged nil)
3606         (setq thread-end nil)
3607
3608         (if (stringp gnus-tmp-header)
3609             ;; The header is a dummy root.
3610             (cond
3611              ((eq gnus-summary-make-false-root 'adopt)
3612               ;; We let the first article adopt the rest.
3613               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3614                                                (cddar thread)))
3615               (setq gnus-tmp-gathered
3616                     (nconc (mapcar
3617                             (lambda (h) (mail-header-number (car h)))
3618                             (cddar thread))
3619                            gnus-tmp-gathered))
3620               (setq thread (cons (list (caar thread)
3621                                        (cadar thread))
3622                                  (cdr thread)))
3623               (setq gnus-tmp-level -1
3624                     gnus-tmp-false-parent t))
3625              ((eq gnus-summary-make-false-root 'empty)
3626               ;; We print adopted articles with empty subject fields.
3627               (setq gnus-tmp-gathered
3628                     (nconc (mapcar
3629                             (lambda (h) (mail-header-number (car h)))
3630                             (cddar thread))
3631                            gnus-tmp-gathered))
3632               (setq gnus-tmp-level -1))
3633              ((eq gnus-summary-make-false-root 'dummy)
3634               ;; We remember that we probably want to output a dummy
3635               ;; root.
3636               (setq gnus-tmp-dummy-line gnus-tmp-header)
3637               (setq gnus-tmp-prev-subject gnus-tmp-header))
3638              (t
3639               ;; We do not make a root for the gathered
3640               ;; sub-threads at all.
3641               (setq gnus-tmp-level -1)))
3642
3643           (setq number (mail-header-number gnus-tmp-header)
3644                 subject (mail-header-subject gnus-tmp-header))
3645
3646           (cond
3647            ;; If the thread has changed subject, we might want to make
3648            ;; this subthread into a root.
3649            ((and (null gnus-thread-ignore-subject)
3650                  (not (zerop gnus-tmp-level))
3651                  gnus-tmp-prev-subject
3652                  (not (inline
3653                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3654             (setq new-roots (nconc new-roots (list (car thread)))
3655                   thread-end t
3656                   gnus-tmp-header nil))
3657            ;; If the article lies outside the current limit,
3658            ;; then we do not display it.
3659            ((not (memq number gnus-newsgroup-limit))
3660             (setq gnus-tmp-gathered
3661                   (nconc (mapcar
3662                           (lambda (h) (mail-header-number (car h)))
3663                           (cdar thread))
3664                          gnus-tmp-gathered))
3665             (setq gnus-tmp-new-adopts (if (cdar thread)
3666                                           (append gnus-tmp-new-adopts
3667                                                   (cdar thread))
3668                                         gnus-tmp-new-adopts)
3669                   thread-end t
3670                   gnus-tmp-header nil)
3671             (when (zerop gnus-tmp-level)
3672               (setq gnus-tmp-root-expunged t)))
3673            ;; Perhaps this article is to be marked as read?
3674            ((and gnus-summary-mark-below
3675                  (< (or (cdr (assq number gnus-newsgroup-scored))
3676                         default-score)
3677                     gnus-summary-mark-below)
3678                  ;; Don't touch sparse articles.
3679                  (not (gnus-summary-article-sparse-p number))
3680                  (not (gnus-summary-article-ancient-p number)))
3681             (setq gnus-newsgroup-unreads
3682                   (delq number gnus-newsgroup-unreads))
3683             (if gnus-newsgroup-auto-expire
3684                 (push number gnus-newsgroup-expirable)
3685               (push (cons number gnus-low-score-mark)
3686                     gnus-newsgroup-reads))))
3687
3688           (when gnus-tmp-header
3689             ;; We may have an old dummy line to output before this
3690             ;; article.
3691             (when (and gnus-tmp-dummy-line
3692                        (gnus-subject-equal
3693                         gnus-tmp-dummy-line
3694                         (mail-header-subject gnus-tmp-header)))
3695               (gnus-summary-insert-dummy-line
3696                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3697               (setq gnus-tmp-dummy-line nil))
3698
3699             ;; Compute the mark.
3700             (setq gnus-tmp-unread (gnus-article-mark number))
3701
3702             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3703                                   gnus-tmp-header gnus-tmp-level)
3704                   gnus-newsgroup-data)
3705
3706             ;; Actually insert the line.
3707             (setq
3708              gnus-tmp-subject-or-nil
3709              (cond
3710               ((and gnus-thread-ignore-subject
3711                     gnus-tmp-prev-subject
3712                     (not (inline (gnus-subject-equal
3713                                   gnus-tmp-prev-subject subject))))
3714                subject)
3715               ((zerop gnus-tmp-level)
3716                (if (and (eq gnus-summary-make-false-root 'empty)
3717                         (memq number gnus-tmp-gathered)
3718                         gnus-tmp-prev-subject
3719                         (inline (gnus-subject-equal
3720                                  gnus-tmp-prev-subject subject)))
3721                    gnus-summary-same-subject
3722                  subject))
3723               (t gnus-summary-same-subject)))
3724             (if (and (eq gnus-summary-make-false-root 'adopt)
3725                      (= gnus-tmp-level 1)
3726                      (memq number gnus-tmp-gathered))
3727                 (setq gnus-tmp-opening-bracket ?\<
3728                       gnus-tmp-closing-bracket ?\>)
3729               (setq gnus-tmp-opening-bracket ?\[
3730                     gnus-tmp-closing-bracket ?\]))
3731             (setq
3732              gnus-tmp-indentation
3733              (aref gnus-thread-indent-array gnus-tmp-level)
3734              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3735              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3736                                 gnus-summary-default-score 0)
3737              gnus-tmp-score-char
3738              (if (or (null gnus-summary-default-score)
3739                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3740                          gnus-summary-zcore-fuzz))
3741                  ? 
3742                (if (< gnus-tmp-score gnus-summary-default-score)
3743                    gnus-score-below-mark gnus-score-over-mark))
3744              gnus-tmp-replied
3745              (cond ((memq number gnus-newsgroup-processable)
3746                     gnus-process-mark)
3747                    ((memq number gnus-newsgroup-cached)
3748                     gnus-cached-mark)
3749                    ((memq number gnus-newsgroup-replied)
3750                     gnus-replied-mark)
3751                    ((memq number gnus-newsgroup-saved)
3752                     gnus-saved-mark)
3753                    (t gnus-unread-mark))
3754              gnus-tmp-from (mail-header-from gnus-tmp-header)
3755              gnus-tmp-name
3756              (cond
3757               ((string-match "<[^>]+> *$" gnus-tmp-from)
3758                (setq beg-match (match-beginning 0))
3759                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3760                         (substring gnus-tmp-from (1+ (match-beginning 0))
3761                                    (1- (match-end 0))))
3762                    (substring gnus-tmp-from 0 beg-match)))
3763               ((string-match "(.+)" gnus-tmp-from)
3764                (substring gnus-tmp-from
3765                           (1+ (match-beginning 0)) (1- (match-end 0))))
3766               (t gnus-tmp-from)))
3767             (when (string= gnus-tmp-name "")
3768               (setq gnus-tmp-name gnus-tmp-from))
3769             (unless (numberp gnus-tmp-lines)
3770               (setq gnus-tmp-lines 0))
3771             (gnus-put-text-property-excluding-characters-with-faces
3772              (point)
3773              (progn (eval gnus-summary-line-format-spec) (point))
3774              'gnus-number number)
3775             (when gnus-visual-p
3776               (forward-line -1)
3777               (gnus-run-hooks 'gnus-summary-update-hook)
3778               (forward-line 1))
3779
3780             (setq gnus-tmp-prev-subject subject)))
3781
3782         (when (nth 1 thread)
3783           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3784         (incf gnus-tmp-level)
3785         (setq threads (if thread-end nil (cdar thread)))
3786         (unless threads
3787           (setq gnus-tmp-level 0)))))
3788   (gnus-message 7 "Generating summary...done"))
3789
3790 (defun gnus-summary-prepare-unthreaded (headers)
3791   "Generate an unthreaded summary buffer based on HEADERS."
3792   (let (header number mark)
3793
3794     (beginning-of-line)
3795
3796     (while headers
3797       ;; We may have to root out some bad articles...
3798       (when (memq (setq number (mail-header-number
3799                                 (setq header (pop headers))))
3800                   gnus-newsgroup-limit)
3801         ;; Mark article as read when it has a low score.
3802         (when (and gnus-summary-mark-below
3803                    (< (or (cdr (assq number gnus-newsgroup-scored))
3804                           gnus-summary-default-score 0)
3805                       gnus-summary-mark-below)
3806                    (not (gnus-summary-article-ancient-p number)))
3807           (setq gnus-newsgroup-unreads
3808                 (delq number gnus-newsgroup-unreads))
3809           (if gnus-newsgroup-auto-expire
3810               (push number gnus-newsgroup-expirable)
3811             (push (cons number gnus-low-score-mark)
3812                   gnus-newsgroup-reads)))
3813
3814         (setq mark (gnus-article-mark number))
3815         (push (gnus-data-make number mark (1+ (point)) header 0)
3816               gnus-newsgroup-data)
3817         (gnus-summary-insert-line
3818          header 0 number
3819          mark (memq number gnus-newsgroup-replied)
3820          (memq number gnus-newsgroup-expirable)
3821          (mail-header-subject header) nil
3822          (cdr (assq number gnus-newsgroup-scored))
3823          (memq number gnus-newsgroup-processable))))))
3824
3825 (defun gnus-select-newsgroup (group &optional read-all select-articles)
3826   "Select newsgroup GROUP.
3827 If READ-ALL is non-nil, all articles in the group are selected.
3828 If SELECT-ARTICLES, only select those articles from GROUP."
3829   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3830          ;;!!! Dirty hack; should be removed.
3831          (gnus-summary-ignore-duplicates
3832           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3833               t
3834             gnus-summary-ignore-duplicates))
3835          (info (nth 2 entry))
3836          articles fetched-articles cached)
3837
3838     (unless (gnus-check-server
3839              (setq gnus-current-select-method
3840                    (gnus-find-method-for-group group)))
3841       (error "Couldn't open server"))
3842
3843     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3844         (gnus-activate-group group)     ; Or we can activate it...
3845         (progn                          ; Or we bug out.
3846           (when (equal major-mode 'gnus-summary-mode)
3847             (kill-buffer (current-buffer)))
3848           (error "Couldn't request group %s: %s"
3849                  group (gnus-status-message group))))
3850
3851     (unless (gnus-request-group group t)
3852       (when (equal major-mode 'gnus-summary-mode)
3853         (kill-buffer (current-buffer)))
3854       (error "Couldn't request group %s: %s"
3855              group (gnus-status-message group)))
3856
3857     (setq gnus-newsgroup-name group)
3858     (setq gnus-newsgroup-unselected nil)
3859     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3860
3861     ;; Adjust and set lists of article marks.
3862     (when info
3863       (gnus-adjust-marked-articles info))
3864
3865     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3866     (when (gnus-virtual-group-p group)
3867       (setq cached gnus-newsgroup-cached))
3868
3869     (setq gnus-newsgroup-unreads
3870           (gnus-set-difference
3871            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3872            gnus-newsgroup-dormant))
3873
3874     (setq gnus-newsgroup-processable nil)
3875
3876     (gnus-update-read-articles group gnus-newsgroup-unreads)
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 (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 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                   (rfc2047-decode-string (nnheader-header-value))
4404                 "(none)"))
4405             ;; From.
4406             (progn
4407               (goto-char p)
4408               (if (search-forward "\nfrom: " nil t)
4409                   (rfc2047-decode-string (nnheader-header-value))
4410                 "(nobody)"))
4411             ;; Date.
4412             (progn
4413               (goto-char p)
4414               (if (search-forward "\ndate: " nil t)
4415                   (nnheader-header-value) ""))
4416             ;; Message-ID.
4417             (progn
4418               (goto-char p)
4419               (setq id (if (re-search-forward
4420                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4421                            ;; We do it this way to make sure the Message-ID
4422                            ;; is (somewhat) syntactically valid.
4423                            (buffer-substring (match-beginning 1)
4424                                              (match-end 1))
4425                          ;; If there was no message-id, we just fake one
4426                          ;; to make subsequent routines simpler.
4427                          (nnheader-generate-fake-message-id))))
4428             ;; References.
4429             (progn
4430               (goto-char p)
4431               (if (search-forward "\nreferences: " nil t)
4432                   (progn
4433                     (setq end (point))
4434                     (prog1
4435                         (nnheader-header-value)
4436                       (setq ref
4437                             (buffer-substring
4438                              (progn
4439                                (end-of-line)
4440                                (search-backward ">" end t)
4441                                (1+ (point)))
4442                              (progn
4443                                (search-backward "<" end t)
4444                                (point))))))
4445                 ;; Get the references from the in-reply-to header if there
4446                 ;; were no references and the in-reply-to header looks
4447                 ;; promising.
4448                 (if (and (search-forward "\nin-reply-to: " nil t)
4449                          (setq in-reply-to (nnheader-header-value))
4450                          (string-match "<[^>]+>" in-reply-to))
4451                     (let (ref2)
4452                       (setq ref (substring in-reply-to (match-beginning 0)
4453                                            (match-end 0)))
4454                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4455                         (setq ref2 (substring in-reply-to (match-beginning 0)
4456                                               (match-end 0)))
4457                         (when (> (length ref2) (length ref))
4458                           (setq ref ref2)))
4459                       ref)
4460                   (setq ref nil))))
4461             ;; Chars.
4462             (progn
4463               (goto-char p)
4464               (if (search-forward "\nchars: " nil t)
4465                   (if (numberp (setq chars (ignore-errors (read cur))))
4466                       chars 0)
4467                 0))
4468             ;; Lines.
4469             (progn
4470               (goto-char p)
4471               (if (search-forward "\nlines: " nil t)
4472                   (if (numberp (setq lines (ignore-errors (read cur))))
4473                       lines 0)
4474                 0))
4475             ;; Xref.
4476             (progn
4477               (goto-char p)
4478               (and (search-forward "\nxref: " nil t)
4479                    (nnheader-header-value)))))
4480           (when (equal id ref)
4481             (setq ref nil))
4482
4483           (when gnus-alter-header-function
4484             (funcall gnus-alter-header-function header)
4485             (setq id (mail-header-id header)
4486                   ref (gnus-parent-id (mail-header-references header))))
4487
4488           (when (setq header
4489                       (gnus-dependencies-add-header
4490                        header dependencies force-new))
4491             (push header headers))
4492           (goto-char (point-max))
4493           (widen))
4494         (nreverse headers)))))
4495
4496 ;; Goes through the xover lines and returns a list of vectors
4497 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4498                                                   force-new dependencies
4499                                                   group also-fetch-heads)
4500   "Parse the news overview data in the server buffer, and return a
4501 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4502   ;; Get the Xref when the users reads the articles since most/some
4503   ;; NNTP servers do not include Xrefs when using XOVER.
4504   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4505   (let ((cur nntp-server-buffer)
4506         (dependencies (or dependencies gnus-newsgroup-dependencies))
4507         number headers header)
4508     (save-excursion
4509       (set-buffer nntp-server-buffer)
4510       ;; Allow the user to mangle the headers before parsing them.
4511       (gnus-run-hooks 'gnus-parse-headers-hook)
4512       (goto-char (point-min))
4513       (while (not (eobp))
4514         (condition-case ()
4515             (while (and sequence (not (eobp)))
4516               (setq number (read cur))
4517               (while (and sequence
4518                           (< (car sequence) number))
4519                 (setq sequence (cdr sequence)))
4520               (and sequence
4521                    (eq number (car sequence))
4522                    (progn
4523                      (setq sequence (cdr sequence))
4524                      (setq header (inline
4525                                     (gnus-nov-parse-line
4526                                      number dependencies force-new))))
4527                    (push header headers))
4528               (forward-line 1))
4529           ;(error
4530           ; (gnus-error 4 "Strange nov line (%d)"
4531         ;              (count-lines (point-min) (point))))
4532           )
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         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-scroll-down (lines)
5760   "Scroll down (or up) one line current article.
5761 Argument LINES specifies lines to be scrolled down (or up if negative)."
5762   (interactive "p")
5763   (gnus-summary-scroll-up (- lines)))
5764
5765 (defun gnus-summary-next-same-subject ()
5766   "Select next article which has the same subject as current one."
5767   (interactive)
5768   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5769
5770 (defun gnus-summary-prev-same-subject ()
5771   "Select previous article which has the same subject as current one."
5772   (interactive)
5773   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5774
5775 (defun gnus-summary-next-unread-same-subject ()
5776   "Select next unread article which has the same subject as current one."
5777   (interactive)
5778   (gnus-summary-next-article t (gnus-summary-article-subject)))
5779
5780 (defun gnus-summary-prev-unread-same-subject ()
5781   "Select previous unread article which has the same subject as current one."
5782   (interactive)
5783   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5784
5785 (defun gnus-summary-first-unread-article ()
5786   "Select the first unread article.
5787 Return nil if there are no unread articles."
5788   (interactive)
5789   (prog1
5790       (when (gnus-summary-first-subject t)
5791         (gnus-summary-show-thread)
5792         (gnus-summary-first-subject t)
5793         (gnus-summary-display-article (gnus-summary-article-number)))
5794     (gnus-summary-position-point)))
5795
5796 (defun gnus-summary-first-article ()
5797   "Select the first article.
5798 Return nil if there are no articles."
5799   (interactive)
5800   (prog1
5801       (when (gnus-summary-first-subject)
5802       (gnus-summary-show-thread)
5803       (gnus-summary-first-subject)
5804       (gnus-summary-display-article (gnus-summary-article-number)))
5805     (gnus-summary-position-point)))
5806
5807 (defun gnus-summary-best-unread-article ()
5808   "Select the unread article with the highest score."
5809   (interactive)
5810   (let ((best -1000000)
5811         (data gnus-newsgroup-data)
5812         article score)
5813     (while data
5814       (and (gnus-data-unread-p (car data))
5815            (> (setq score
5816                     (gnus-summary-article-score (gnus-data-number (car data))))
5817               best)
5818            (setq best score
5819                  article (gnus-data-number (car data))))
5820       (setq data (cdr data)))
5821     (prog1
5822         (if article
5823             (gnus-summary-goto-article article)
5824           (error "No unread articles"))
5825       (gnus-summary-position-point))))
5826
5827 (defun gnus-summary-last-subject ()
5828   "Go to the last displayed subject line in the group."
5829   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5830     (when article
5831       (gnus-summary-goto-subject article))))
5832
5833 (defun gnus-summary-goto-article (article &optional all-headers force)
5834   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5835 If ALL-HEADERS is non-nil, no header lines are hidden.
5836 If FORCE, go to the article even if it isn't displayed.  If FORCE
5837 is a number, it is the line the article is to be displayed on."
5838   (interactive
5839    (list
5840     (completing-read
5841      "Article number or Message-ID: "
5842      (mapcar (lambda (number) (list (int-to-string number)))
5843              gnus-newsgroup-limit))
5844     current-prefix-arg
5845     t))
5846   (prog1
5847       (if (and (stringp article)
5848                (string-match "@" article))
5849           (gnus-summary-refer-article article)
5850         (when (stringp article)
5851           (setq article (string-to-number article)))
5852         (if (gnus-summary-goto-subject article force)
5853             (gnus-summary-display-article article all-headers)
5854           (gnus-message 4 "Couldn't go to article %s" article) nil))
5855     (gnus-summary-position-point)))
5856
5857 (defun gnus-summary-goto-last-article ()
5858   "Go to the previously read article."
5859   (interactive)
5860   (prog1
5861       (when gnus-last-article
5862         (gnus-summary-goto-article gnus-last-article nil t))
5863     (gnus-summary-position-point)))
5864
5865 (defun gnus-summary-pop-article (number)
5866   "Pop one article off the history and go to the previous.
5867 NUMBER articles will be popped off."
5868   (interactive "p")
5869   (let (to)
5870     (setq gnus-newsgroup-history
5871           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5872     (if to
5873         (gnus-summary-goto-article (car to) nil t)
5874       (error "Article history empty")))
5875   (gnus-summary-position-point))
5876
5877 ;; Summary commands and functions for limiting the summary buffer.
5878
5879 (defun gnus-summary-limit-to-articles (n)
5880   "Limit the summary buffer to the next N articles.
5881 If not given a prefix, use the process marked articles instead."
5882   (interactive "P")
5883   (prog1
5884       (let ((articles (gnus-summary-work-articles n)))
5885         (setq gnus-newsgroup-processable nil)
5886         (gnus-summary-limit articles))
5887     (gnus-summary-position-point)))
5888
5889 (defun gnus-summary-pop-limit (&optional total)
5890   "Restore the previous limit.
5891 If given a prefix, remove all limits."
5892   (interactive "P")
5893   (when total
5894     (setq gnus-newsgroup-limits
5895           (list (mapcar (lambda (h) (mail-header-number h))
5896                         gnus-newsgroup-headers))))
5897   (unless gnus-newsgroup-limits
5898     (error "No limit to pop"))
5899   (prog1
5900       (gnus-summary-limit nil 'pop)
5901     (gnus-summary-position-point)))
5902
5903 (defun gnus-summary-limit-to-subject (subject &optional header)
5904   "Limit the summary buffer to articles that have subjects that match a regexp."
5905   (interactive "sLimit to subject (regexp): ")
5906   (unless header
5907     (setq header "subject"))
5908   (when (not (equal "" subject))
5909     (prog1
5910         (let ((articles (gnus-summary-find-matching
5911                          (or header "subject") subject 'all)))
5912           (unless articles
5913             (error "Found no matches for \"%s\"" subject))
5914           (gnus-summary-limit articles))
5915       (gnus-summary-position-point))))
5916
5917 (defun gnus-summary-limit-to-author (from)
5918   "Limit the summary buffer to articles that have authors that match a regexp."
5919   (interactive "sLimit to author (regexp): ")
5920   (gnus-summary-limit-to-subject from "from"))
5921
5922 (defun gnus-summary-limit-to-age (age &optional younger-p)
5923   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5924 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5925 articles that are younger than AGE days."
5926   (interactive "nTime in days: \nP")
5927   (prog1
5928       (let ((data gnus-newsgroup-data)
5929             (cutoff (nnmail-days-to-time age))
5930             articles d date is-younger)
5931         (while (setq d (pop data))
5932           (when (and (vectorp (gnus-data-header d))
5933                      (setq date (mail-header-date (gnus-data-header d))))
5934             (setq is-younger (nnmail-time-less
5935                               (nnmail-time-since (nnmail-date-to-time date))
5936                               cutoff))
5937             (when (if younger-p
5938                       is-younger
5939                     (not is-younger))
5940               (push (gnus-data-number d) articles))))
5941         (gnus-summary-limit (nreverse articles)))
5942     (gnus-summary-position-point)))
5943
5944 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5945 (make-obsolete
5946  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5947
5948 (defun gnus-summary-limit-to-unread (&optional all)
5949   "Limit the summary buffer to articles that are not marked as read.
5950 If ALL is non-nil, limit strictly to unread articles."
5951   (interactive "P")
5952   (if all
5953       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5954     (gnus-summary-limit-to-marks
5955      ;; Concat all the marks that say that an article is read and have
5956      ;; those removed.
5957      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5958            gnus-killed-mark gnus-kill-file-mark
5959            gnus-low-score-mark gnus-expirable-mark
5960            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5961            gnus-duplicate-mark gnus-souped-mark)
5962      'reverse)))
5963
5964 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5965 (make-obsolete 'gnus-summary-delete-marked-with
5966                'gnus-summary-limit-exlude-marks)
5967
5968 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5969   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5970 If REVERSE, limit the summary buffer to articles that are marked
5971 with MARKS.  MARKS can either be a string of marks or a list of marks.
5972 Returns how many articles were removed."
5973   (interactive "sMarks: ")
5974   (gnus-summary-limit-to-marks marks t))
5975
5976 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5977   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5978 If REVERSE (the prefix), limit the summary buffer to articles that are
5979 not marked with MARKS.  MARKS can either be a string of marks or a
5980 list of marks.
5981 Returns how many articles were removed."
5982   (interactive "sMarks: \nP")
5983   (prog1
5984       (let ((data gnus-newsgroup-data)
5985             (marks (if (listp marks) marks
5986                      (append marks nil))) ; Transform to list.
5987             articles)
5988         (while data
5989           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5990                   (memq (gnus-data-mark (car data)) marks))
5991             (push (gnus-data-number (car data)) articles))
5992           (setq data (cdr data)))
5993         (gnus-summary-limit articles))
5994     (gnus-summary-position-point)))
5995
5996 (defun gnus-summary-limit-to-score (&optional score)
5997   "Limit to articles with score at or above SCORE."
5998   (interactive "P")
5999   (setq score (if score
6000                   (prefix-numeric-value score)
6001                 (or gnus-summary-default-score 0)))
6002   (let ((data gnus-newsgroup-data)
6003         articles)
6004     (while data
6005       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6006                 score)
6007         (push (gnus-data-number (car data)) articles))
6008       (setq data (cdr data)))
6009     (prog1
6010         (gnus-summary-limit articles)
6011       (gnus-summary-position-point))))
6012
6013 (defun gnus-summary-limit-include-thread (id)
6014   "Display all the hidden articles that in the current thread."
6015   (interactive (list (mail-header-id (gnus-summary-article-header))))
6016   (let ((articles (gnus-articles-in-thread
6017                    (gnus-id-to-thread (gnus-root-id id)))))
6018     (prog1
6019         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6020       (gnus-summary-position-point))))
6021
6022 (defun gnus-summary-limit-include-dormant ()
6023   "Display all the hidden articles that are marked as dormant.
6024 Note that this command only works on a subset of the articles currently
6025 fetched for this group."
6026   (interactive)
6027   (unless gnus-newsgroup-dormant
6028     (error "There are no dormant articles in this group"))
6029   (prog1
6030       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6031     (gnus-summary-position-point)))
6032
6033 (defun gnus-summary-limit-exclude-dormant ()
6034   "Hide all dormant articles."
6035   (interactive)
6036   (prog1
6037       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6038     (gnus-summary-position-point)))
6039
6040 (defun gnus-summary-limit-exclude-childless-dormant ()
6041   "Hide all dormant articles that have no children."
6042   (interactive)
6043   (let ((data (gnus-data-list t))
6044         articles d children)
6045     ;; Find all articles that are either not dormant or have
6046     ;; children.
6047     (while (setq d (pop data))
6048       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6049                 (and (setq children
6050                            (gnus-article-children (gnus-data-number d)))
6051                      (let (found)
6052                        (while children
6053                          (when (memq (car children) articles)
6054                            (setq children nil
6055                                  found t))
6056                          (pop children))
6057                        found)))
6058         (push (gnus-data-number d) articles)))
6059     ;; Do the limiting.
6060     (prog1
6061         (gnus-summary-limit articles)
6062       (gnus-summary-position-point))))
6063
6064 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6065   "Mark all unread excluded articles as read.
6066 If ALL, mark even excluded ticked and dormants as read."
6067   (interactive "P")
6068   (let ((articles (gnus-sorted-complement
6069                    (sort
6070                     (mapcar (lambda (h) (mail-header-number h))
6071                             gnus-newsgroup-headers)
6072                     '<)
6073                    (sort gnus-newsgroup-limit '<)))
6074         article)
6075     (setq gnus-newsgroup-unreads
6076           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6077     (if all
6078         (setq gnus-newsgroup-dormant nil
6079               gnus-newsgroup-marked nil
6080               gnus-newsgroup-reads
6081               (nconc
6082                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6083                gnus-newsgroup-reads))
6084       (while (setq article (pop articles))
6085         (unless (or (memq article gnus-newsgroup-dormant)
6086                     (memq article gnus-newsgroup-marked))
6087           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6088
6089 (defun gnus-summary-limit (articles &optional pop)
6090   (if pop
6091       ;; We pop the previous limit off the stack and use that.
6092       (setq articles (car gnus-newsgroup-limits)
6093             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6094     ;; We use the new limit, so we push the old limit on the stack.
6095     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6096   ;; Set the limit.
6097   (setq gnus-newsgroup-limit articles)
6098   (let ((total (length gnus-newsgroup-data))
6099         (data (gnus-data-find-list (gnus-summary-article-number)))
6100         (gnus-summary-mark-below nil)   ; Inhibit this.
6101         found)
6102     ;; This will do all the work of generating the new summary buffer
6103     ;; according to the new limit.
6104     (gnus-summary-prepare)
6105     ;; Hide any threads, possibly.
6106     (and gnus-show-threads
6107          gnus-thread-hide-subtree
6108          (gnus-summary-hide-all-threads))
6109     ;; Try to return to the article you were at, or one in the
6110     ;; neighborhood.
6111     (when data
6112       ;; We try to find some article after the current one.
6113       (while data
6114         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6115           (setq data nil
6116                 found t))
6117         (setq data (cdr data))))
6118     (unless found
6119       ;; If there is no data, that means that we were after the last
6120       ;; article.  The same goes when we can't find any articles
6121       ;; after the current one.
6122       (goto-char (point-max))
6123       (gnus-summary-find-prev))
6124     (gnus-set-mode-line 'summary)
6125     ;; We return how many articles were removed from the summary
6126     ;; buffer as a result of the new limit.
6127     (- total (length gnus-newsgroup-data))))
6128
6129 (defsubst gnus-invisible-cut-children (threads)
6130   (let ((num 0))
6131     (while threads
6132       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6133         (incf num))
6134       (pop threads))
6135     (< num 2)))
6136
6137 (defsubst gnus-cut-thread (thread)
6138   "Go forwards in the thread until we find an article that we want to display."
6139   (when (or (eq gnus-fetch-old-headers 'some)
6140             (eq gnus-fetch-old-headers 'invisible)          
6141             (eq gnus-build-sparse-threads 'some)
6142             (eq gnus-build-sparse-threads 'more))
6143     ;; Deal with old-fetched headers and sparse threads.
6144     (while (and
6145             thread
6146             (or
6147              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6148              (gnus-summary-article-ancient-p
6149               (mail-header-number (car thread))))
6150             (if (or (<= (length (cdr thread)) 1)
6151                     (eq gnus-fetch-old-headers 'invisible))
6152                 (setq gnus-newsgroup-limit
6153                       (delq (mail-header-number (car thread))
6154                             gnus-newsgroup-limit)
6155                       thread (cadr thread))
6156               (when (gnus-invisible-cut-children (cdr thread))
6157                 (let ((th (cdr thread)))
6158                   (while th
6159                     (if (memq (mail-header-number (caar th))
6160                               gnus-newsgroup-limit)
6161                         (setq thread (car th)
6162                               th nil)
6163                       (setq th (cdr th))))))))))
6164   thread)
6165
6166 (defun gnus-cut-threads (threads)
6167   "Cut off all uninteresting articles from the beginning of threads."
6168   (when (or (eq gnus-fetch-old-headers 'some)
6169             (eq gnus-fetch-old-headers 'invisible)
6170             (eq gnus-build-sparse-threads 'some)
6171             (eq gnus-build-sparse-threads 'more))
6172     (let ((th threads))
6173       (while th
6174         (setcar th (gnus-cut-thread (car th)))
6175         (setq th (cdr th)))))
6176   ;; Remove nixed out threads.
6177   (delq nil threads))
6178
6179 (defun gnus-summary-initial-limit (&optional show-if-empty)
6180   "Figure out what the initial limit is supposed to be on group entry.
6181 This entails weeding out unwanted dormants, low-scored articles,
6182 fetch-old-headers verbiage, and so on."
6183   ;; Most groups have nothing to remove.
6184   (if (or gnus-inhibit-limiting
6185           (and (null gnus-newsgroup-dormant)
6186                (not (eq gnus-fetch-old-headers 'some))
6187                (not (eq gnus-fetch-old-headers 'invisible))
6188                (null gnus-summary-expunge-below)
6189                (not (eq gnus-build-sparse-threads 'some))
6190                (not (eq gnus-build-sparse-threads 'more))
6191                (null gnus-thread-expunge-below)
6192                (not gnus-use-nocem)))
6193       ()                                ; Do nothing.
6194     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6195     (setq gnus-newsgroup-limit nil)
6196     (mapatoms
6197      (lambda (node)
6198        (unless (car (symbol-value node))
6199          ;; These threads have no parents -- they are roots.
6200          (let ((nodes (cdr (symbol-value node)))
6201                thread)
6202            (while nodes
6203              (if (and gnus-thread-expunge-below
6204                       (< (gnus-thread-total-score (car nodes))
6205                          gnus-thread-expunge-below))
6206                  (gnus-expunge-thread (pop nodes))
6207                (setq thread (pop nodes))
6208                (gnus-summary-limit-children thread))))))
6209      gnus-newsgroup-dependencies)
6210     ;; If this limitation resulted in an empty group, we might
6211     ;; pop the previous limit and use it instead.
6212     (when (and (not gnus-newsgroup-limit)
6213                show-if-empty)
6214       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6215     gnus-newsgroup-limit))
6216
6217 (defun gnus-summary-limit-children (thread)
6218   "Return 1 if this subthread is visible and 0 if it is not."
6219   ;; First we get the number of visible children to this thread.  This
6220   ;; is done by recursing down the thread using this function, so this
6221   ;; will really go down to a leaf article first, before slowly
6222   ;; working its way up towards the root.
6223   (when thread
6224     (let ((children
6225            (if (cdr thread)
6226                (apply '+ (mapcar 'gnus-summary-limit-children
6227                                  (cdr thread)))
6228              0))
6229           (number (mail-header-number (car thread)))
6230           score)
6231       (if (and
6232            (not (memq number gnus-newsgroup-marked))
6233            (or
6234             ;; If this article is dormant and has absolutely no visible
6235             ;; children, then this article isn't visible.
6236             (and (memq number gnus-newsgroup-dormant)
6237                  (zerop children))
6238             ;; If this is "fetch-old-headered" and there is no
6239             ;; visible children, then we don't want this article.
6240             (and (eq gnus-fetch-old-headers 'some)
6241                  (gnus-summary-article-ancient-p number)
6242                  (zerop children))
6243             ;; If this is "fetch-old-headered" and `invisible', then
6244             ;; we don't want this article.
6245             (and (eq gnus-fetch-old-headers 'invisible)
6246                  (gnus-summary-article-ancient-p number))
6247             ;; If this is a sparsely inserted article with no children,
6248             ;; we don't want it.
6249             (and (eq gnus-build-sparse-threads 'some)
6250                  (gnus-summary-article-sparse-p number)
6251                  (zerop children))
6252             ;; If we use expunging, and this article is really
6253             ;; low-scored, then we don't want this article.
6254             (when (and gnus-summary-expunge-below
6255                        (< (setq score
6256                                 (or (cdr (assq number gnus-newsgroup-scored))
6257                                     gnus-summary-default-score))
6258                           gnus-summary-expunge-below))
6259               ;; We increase the expunge-tally here, but that has
6260               ;; nothing to do with the limits, really.
6261               (incf gnus-newsgroup-expunged-tally)
6262               ;; We also mark as read here, if that's wanted.
6263               (when (and gnus-summary-mark-below
6264                          (< score gnus-summary-mark-below))
6265                 (setq gnus-newsgroup-unreads
6266                       (delq number gnus-newsgroup-unreads))
6267                 (if gnus-newsgroup-auto-expire
6268                     (push number gnus-newsgroup-expirable)
6269                   (push (cons number gnus-low-score-mark)
6270                         gnus-newsgroup-reads)))
6271               t)
6272             ;; Check NoCeM things.
6273             (if (and gnus-use-nocem
6274                      (gnus-nocem-unwanted-article-p
6275                       (mail-header-id (car thread))))
6276                 (progn
6277                   (setq gnus-newsgroup-unreads
6278                         (delq number gnus-newsgroup-unreads))
6279                   t))))
6280           ;; Nope, invisible article.
6281           0
6282         ;; Ok, this article is to be visible, so we add it to the limit
6283         ;; and return 1.
6284         (push number gnus-newsgroup-limit)
6285         1))))
6286
6287 (defun gnus-expunge-thread (thread)
6288   "Mark all articles in THREAD as read."
6289   (let* ((number (mail-header-number (car thread))))
6290     (incf gnus-newsgroup-expunged-tally)
6291     ;; We also mark as read here, if that's wanted.
6292     (setq gnus-newsgroup-unreads
6293           (delq number gnus-newsgroup-unreads))
6294     (if gnus-newsgroup-auto-expire
6295         (push number gnus-newsgroup-expirable)
6296       (push (cons number gnus-low-score-mark)
6297             gnus-newsgroup-reads)))
6298   ;; Go recursively through all subthreads.
6299   (mapcar 'gnus-expunge-thread (cdr thread)))
6300
6301 ;; Summary article oriented commands
6302
6303 (defun gnus-summary-refer-parent-article (n)
6304   "Refer parent article N times.
6305 If N is negative, go to ancestor -N instead.
6306 The difference between N and the number of articles fetched is returned."
6307   (interactive "p")
6308   (let ((skip 1)
6309         error header ref)
6310     (when (not (natnump n))
6311       (setq skip (abs n)
6312             n 1))
6313     (while (and (> n 0)
6314                 (not error))
6315       (setq header (gnus-summary-article-header))
6316       (if (and (eq (mail-header-number header)
6317                    (cdr gnus-article-current))
6318                (equal gnus-newsgroup-name
6319                       (car gnus-article-current)))
6320           ;; If we try to find the parent of the currently
6321           ;; displayed article, then we take a look at the actual
6322           ;; References header, since this is slightly more
6323           ;; reliable than the References field we got from the
6324           ;; server.
6325           (save-excursion
6326             (set-buffer gnus-original-article-buffer)
6327             (nnheader-narrow-to-headers)
6328             (unless (setq ref (message-fetch-field "references"))
6329               (setq ref (message-fetch-field "in-reply-to")))
6330             (widen))
6331         (setq ref
6332               ;; It's not the current article, so we take a bet on
6333               ;; the value we got from the server.
6334               (mail-header-references header)))
6335       (if (and ref
6336                (not (equal ref "")))
6337           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6338             (gnus-message 1 "Couldn't find parent"))
6339         (gnus-message 1 "No references in article %d"
6340                       (gnus-summary-article-number))
6341         (setq error t))
6342       (decf n))
6343     (gnus-summary-position-point)
6344     n))
6345
6346 (defun gnus-summary-refer-references ()
6347   "Fetch all articles mentioned in the References header.
6348 Return the number of articles fetched."
6349   (interactive)
6350   (let ((ref (mail-header-references (gnus-summary-article-header)))
6351         (current (gnus-summary-article-number))
6352         (n 0))
6353     (if (or (not ref)
6354             (equal ref ""))
6355         (error "No References in the current article")
6356       ;; For each Message-ID in the References header...
6357       (while (string-match "<[^>]*>" ref)
6358         (incf n)
6359         ;; ... fetch that article.
6360         (gnus-summary-refer-article
6361          (prog1 (match-string 0 ref)
6362            (setq ref (substring ref (match-end 0))))))
6363       (gnus-summary-goto-subject current)
6364       (gnus-summary-position-point)
6365       n)))
6366
6367 (defun gnus-summary-refer-thread (&optional limit)
6368   "Fetch all articles in the current thread.
6369 If LIMIT (the numerical prefix), fetch that many old headers instead
6370 of what's specified by the `gnus-refer-thread-limit' variable."
6371   (interactive "P")
6372   (let ((id (mail-header-id (gnus-summary-article-header)))
6373         (limit (if limit (prefix-numeric-value limit)
6374                  gnus-refer-thread-limit)))
6375     ;; We want to fetch LIMIT *old* headers, but we also have to
6376     ;; re-fetch all the headers in the current buffer, because many of
6377     ;; them may be undisplayed.  So we adjust LIMIT.
6378     (when (numberp limit)
6379       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6380     (unless (eq gnus-fetch-old-headers 'invisible)
6381       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6382       ;; Retrieve the headers and read them in.
6383       (if (eq (gnus-retrieve-headers
6384                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6385               'nov)
6386           (gnus-build-all-threads)
6387         (error "Can't fetch thread from backends that don't support NOV"))
6388       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6389     (gnus-summary-limit-include-thread id)))
6390
6391 (defun gnus-summary-refer-article (message-id &optional arg)
6392   "Fetch an article specified by MESSAGE-ID.
6393 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6394 or `gnus-select-method', no matter what backend the article comes from."
6395   (interactive "sMessage-ID: \nP")
6396   (when (and (stringp message-id)
6397              (not (zerop (length message-id))))
6398     ;; Construct the correct Message-ID if necessary.
6399     ;; Suggested by tale@pawl.rpi.edu.
6400     (unless (string-match "^<" message-id)
6401       (setq message-id (concat "<" message-id)))
6402     (unless (string-match ">$" message-id)
6403       (setq message-id (concat message-id ">")))
6404     (let* ((header (gnus-id-to-header message-id))
6405            (sparse (and header
6406                         (gnus-summary-article-sparse-p
6407                          (mail-header-number header))
6408                         (memq (mail-header-number header)
6409                               gnus-newsgroup-limit))))
6410       (cond
6411        ;; If the article is present in the buffer we just go to it.
6412        ((and header
6413              (or (not (gnus-summary-article-sparse-p
6414                        (mail-header-number header)))
6415                  sparse))
6416         (prog1
6417             (gnus-summary-goto-article
6418              (mail-header-number header) nil t)
6419           (when sparse
6420             (gnus-summary-update-article (mail-header-number header)))))
6421        (t
6422         ;; We fetch the article
6423         (let ((gnus-override-method
6424                (cond ((gnus-news-group-p gnus-newsgroup-name)
6425                       gnus-refer-article-method)
6426                      (arg
6427                       (or gnus-refer-article-method gnus-select-method))
6428                      (t nil)))
6429               number)
6430           ;; Start the special refer-article method, if necessary.
6431           (when (and gnus-refer-article-method
6432                      (gnus-news-group-p gnus-newsgroup-name))
6433             (gnus-check-server gnus-refer-article-method))
6434           ;; Fetch the header, and display the article.
6435           (if (setq number (gnus-summary-insert-subject message-id))
6436               (gnus-summary-select-article nil nil nil number)
6437             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6438
6439 (defun gnus-summary-edit-parameters ()
6440   "Edit the group parameters of the current group."
6441   (interactive)
6442   (gnus-group-edit-group gnus-newsgroup-name 'params))
6443
6444 (defun gnus-summary-enter-digest-group (&optional force)
6445   "Enter an nndoc group based on the current article.
6446 If FORCE, force a digest interpretation.  If not, try
6447 to guess what the document format is."
6448   (interactive "P")
6449   (let ((conf gnus-current-window-configuration))
6450     (save-excursion
6451       (gnus-summary-select-article))
6452     (setq gnus-current-window-configuration conf)
6453     (let* ((name (format "%s-%d"
6454                          (gnus-group-prefixed-name
6455                           gnus-newsgroup-name (list 'nndoc ""))
6456                          (save-excursion
6457                            (set-buffer gnus-summary-buffer)
6458                            gnus-current-article)))
6459            (ogroup gnus-newsgroup-name)
6460            (params (append (gnus-info-params (gnus-get-info ogroup))
6461                            (list (cons 'to-group ogroup))
6462                            (list (cons 'save-article-group ogroup))))
6463            (case-fold-search t)
6464            (buf (current-buffer))
6465            dig)
6466       (save-excursion
6467         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6468         (insert-buffer-substring gnus-original-article-buffer)
6469         ;; Remove lines that may lead nndoc to misinterpret the
6470         ;; document type.
6471         (narrow-to-region
6472          (goto-char (point-min))
6473          (or (search-forward "\n\n" nil t) (point)))
6474         (goto-char (point-min))
6475         (delete-matching-lines "^\\(Path\\):\\|^From ")
6476         (widen))
6477       (unwind-protect
6478           (if (gnus-group-read-ephemeral-group
6479                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6480                             (nndoc-article-type
6481                              ,(if force 'digest 'guess))) t)
6482               ;; Make all postings to this group go to the parent group.
6483               (nconc (gnus-info-params (gnus-get-info name))
6484                      params)
6485             ;; Couldn't select this doc group.
6486             (switch-to-buffer buf)
6487             (gnus-set-global-variables)
6488             (gnus-configure-windows 'summary)
6489             (gnus-message 3 "Article couldn't be entered?"))
6490         (kill-buffer dig)))))
6491
6492 (defun gnus-summary-read-document (n)
6493   "Open a new group based on the current article(s).
6494 This will allow you to read digests and other similar
6495 documents as newsgroups.
6496 Obeys the standard process/prefix convention."
6497   (interactive "P")
6498   (let* ((articles (gnus-summary-work-articles n))
6499          (ogroup gnus-newsgroup-name)
6500          (params (append (gnus-info-params (gnus-get-info ogroup))
6501                          (list (cons 'to-group ogroup))))
6502          article group egroup groups vgroup)
6503     (while (setq article (pop articles))
6504       (setq group (format "%s-%d" gnus-newsgroup-name article))
6505       (gnus-summary-remove-process-mark article)
6506       (when (gnus-summary-display-article article)
6507         (save-excursion
6508           (with-temp-buffer
6509             (insert-buffer-substring gnus-original-article-buffer)
6510             ;; Remove some headers that may lead nndoc to make
6511             ;; the wrong guess.
6512             (message-narrow-to-head)
6513             (goto-char (point-min))
6514             (delete-matching-lines "^\\(Path\\):\\|^From ")
6515             (widen)
6516             (if (setq egroup
6517                       (gnus-group-read-ephemeral-group
6518                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6519                                      (nndoc-article-type guess))
6520                        t nil t))
6521                 (progn
6522                   ;; Make all postings to this group go to the parent group.
6523                   (nconc (gnus-info-params (gnus-get-info egroup))
6524                          params)
6525                   (push egroup groups))
6526               ;; Couldn't select this doc group.
6527               (gnus-error 3 "Article couldn't be entered"))))))
6528     ;; Now we have selected all the documents.
6529     (cond
6530      ((not groups)
6531       (error "None of the articles could be interpreted as documents"))
6532      ((gnus-group-read-ephemeral-group
6533        (setq vgroup (format
6534                      "nnvirtual:%s-%s" gnus-newsgroup-name
6535                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6536        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6537        t
6538        (cons (current-buffer) 'summary)))
6539      (t
6540       (error "Couldn't select virtual nndoc group")))))
6541
6542 (defun gnus-summary-isearch-article (&optional regexp-p)
6543   "Do incremental search forward on the current article.
6544 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6545   (interactive "P")
6546   (gnus-summary-select-article)
6547   (gnus-configure-windows 'article)
6548   (gnus-eval-in-buffer-window gnus-article-buffer
6549     (save-restriction
6550       (widen)
6551       (isearch-forward regexp-p))))
6552
6553 (defun gnus-summary-search-article-forward (regexp &optional backward)
6554   "Search for an article containing REGEXP forward.
6555 If BACKWARD, search backward instead."
6556   (interactive
6557    (list (read-string
6558           (format "Search article %s (regexp%s): "
6559                   (if current-prefix-arg "backward" "forward")
6560                   (if gnus-last-search-regexp
6561                       (concat ", default " gnus-last-search-regexp)
6562                     "")))
6563          current-prefix-arg))
6564   (if (string-equal regexp "")
6565       (setq regexp (or gnus-last-search-regexp ""))
6566     (setq gnus-last-search-regexp regexp))
6567   (if (gnus-summary-search-article regexp backward)
6568       (gnus-summary-show-thread)
6569     (error "Search failed: \"%s\"" regexp)))
6570
6571 (defun gnus-summary-search-article-backward (regexp)
6572   "Search for an article containing REGEXP backward."
6573   (interactive
6574    (list (read-string
6575           (format "Search article backward (regexp%s): "
6576                   (if gnus-last-search-regexp
6577                       (concat ", default " gnus-last-search-regexp)
6578                     "")))))
6579   (gnus-summary-search-article-forward regexp 'backward))
6580
6581 (defun gnus-summary-search-article (regexp &optional backward)
6582   "Search for an article containing REGEXP.
6583 Optional argument BACKWARD means do search for backward.
6584 `gnus-select-article-hook' is not called during the search."
6585   ;; We have to require this here to make sure that the following
6586   ;; dynamic binding isn't shadowed by autoloading.
6587   (require 'gnus-async)
6588   (let ((gnus-select-article-hook nil)  ;Disable hook.
6589         (gnus-article-display-hook nil)
6590         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6591         (gnus-use-article-prefetch nil)
6592         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6593         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6594         (sum (current-buffer))
6595         (found nil)
6596         point)
6597     (gnus-save-hidden-threads
6598       (gnus-summary-select-article)
6599       (set-buffer gnus-article-buffer)
6600       (when backward
6601         (forward-line -1))
6602       (while (not found)
6603         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6604         (if (if backward
6605                 (re-search-backward regexp nil t)
6606               (re-search-forward regexp nil t))
6607             ;; We found the regexp.
6608             (progn
6609               (setq found 'found)
6610               (beginning-of-line)
6611               (set-window-start
6612                (get-buffer-window (current-buffer))
6613                (point))
6614               (forward-line 1)
6615               (set-buffer sum)
6616               (setq point (point)))
6617           ;; We didn't find it, so we go to the next article.
6618           (set-buffer sum)
6619           (setq found 'not)
6620           (while (eq found 'not)
6621             (if (not (if backward (gnus-summary-find-prev)
6622                        (gnus-summary-find-next)))
6623                 ;; No more articles.
6624                 (setq found t)
6625               ;; Select the next article and adjust point.
6626               (unless (gnus-summary-article-sparse-p
6627                        (gnus-summary-article-number))
6628                 (setq found nil)
6629                 (gnus-summary-select-article)
6630                 (set-buffer gnus-article-buffer)
6631                 (widen)
6632                 (goto-char (if backward (point-max) (point-min))))))))
6633       (gnus-message 7 ""))
6634     ;; Return whether we found the regexp.
6635     (when (eq found 'found)
6636       (goto-char point)
6637       (gnus-summary-show-thread)
6638       (gnus-summary-goto-subject gnus-current-article)
6639       (gnus-summary-position-point)
6640       t)))
6641
6642 (defun gnus-summary-find-matching (header regexp &optional backward unread
6643                                           not-case-fold)
6644   "Return a list of all articles that match REGEXP on HEADER.
6645 The search stars on the current article and goes forwards unless
6646 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6647 If UNREAD is non-nil, only unread articles will
6648 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6649 in the comparisons."
6650   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6651                 (gnus-data-find-list
6652                  (gnus-summary-article-number) (gnus-data-list backward))))
6653         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6654         (case-fold-search (not not-case-fold))
6655         articles d)
6656     (unless (fboundp (intern (concat "mail-header-" header)))
6657       (error "%s is not a valid header" header))
6658     (while data
6659       (setq d (car data))
6660       (and (or (not unread)             ; We want all articles...
6661                (gnus-data-unread-p d))  ; Or just unreads.
6662            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6663            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6664            (push (gnus-data-number d) articles)) ; Success!
6665       (setq data (cdr data)))
6666     (nreverse articles)))
6667
6668 (defun gnus-summary-execute-command (header regexp command &optional backward)
6669   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6670 If HEADER is an empty string (or nil), the match is done on the entire
6671 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6672   (interactive
6673    (list (let ((completion-ignore-case t))
6674            (completing-read
6675             "Header name: "
6676             (mapcar (lambda (string) (list string))
6677                     '("Number" "Subject" "From" "Lines" "Date"
6678                       "Message-ID" "Xref" "References" "Body"))
6679             nil 'require-match))
6680          (read-string "Regexp: ")
6681          (read-key-sequence "Command: ")
6682          current-prefix-arg))
6683   (when (equal header "Body")
6684     (setq header ""))
6685   ;; Hidden thread subtrees must be searched as well.
6686   (gnus-summary-show-all-threads)
6687   ;; We don't want to change current point nor window configuration.
6688   (save-excursion
6689     (save-window-excursion
6690       (gnus-message 6 "Executing %s..." (key-description command))
6691       ;; We'd like to execute COMMAND interactively so as to give arguments.
6692       (gnus-execute header regexp
6693                     `(call-interactively ',(key-binding command))
6694                     backward)
6695       (gnus-message 6 "Executing %s...done" (key-description command)))))
6696
6697 (defun gnus-summary-beginning-of-article ()
6698   "Scroll the article back to the beginning."
6699   (interactive)
6700   (gnus-summary-select-article)
6701   (gnus-configure-windows 'article)
6702   (gnus-eval-in-buffer-window gnus-article-buffer
6703     (widen)
6704     (goto-char (point-min))
6705     (when gnus-page-broken
6706       (gnus-narrow-to-page))))
6707
6708 (defun gnus-summary-end-of-article ()
6709   "Scroll to the end of the article."
6710   (interactive)
6711   (gnus-summary-select-article)
6712   (gnus-configure-windows 'article)
6713   (gnus-eval-in-buffer-window gnus-article-buffer
6714     (widen)
6715     (goto-char (point-max))
6716     (recenter -3)
6717     (when gnus-page-broken
6718       (gnus-narrow-to-page))))
6719
6720 (defun gnus-summary-print-article (&optional filename n)
6721   "Generate and print a PostScript image of the N next (mail) articles.
6722
6723 If N is negative, print the N previous articles.  If N is nil and articles
6724 have been marked with the process mark, print these instead.
6725
6726 If the optional second argument FILENAME is nil, send the image to the
6727 printer.  If FILENAME is a string, save the PostScript image in a file with
6728 that name.  If FILENAME is a number, prompt the user for the name of the file
6729 to save in."
6730   (interactive (list (ps-print-preprint current-prefix-arg)
6731                      current-prefix-arg))
6732   (dolist (article (gnus-summary-work-articles n))
6733     (gnus-summary-select-article nil nil 'pseudo article)
6734     (gnus-eval-in-buffer-window gnus-article-buffer
6735       (let ((buffer (generate-new-buffer " *print*")))
6736         (unwind-protect
6737             (progn
6738               (copy-to-buffer buffer (point-min) (point-max))
6739               (set-buffer buffer)
6740               (gnus-article-delete-invisible-text)
6741               (let ((ps-left-header
6742                      (list 
6743                       (concat "("
6744                               (mail-header-subject gnus-current-headers) ")")
6745                       (concat "("
6746                               (mail-header-from gnus-current-headers) ")")))
6747                     (ps-right-header 
6748                      (list 
6749                       "/pagenumberstring load" 
6750                       (concat "("
6751                               (mail-header-date gnus-current-headers) ")"))))
6752                 (gnus-run-hooks 'gnus-ps-print-hook)
6753                 (save-excursion
6754                   (ps-print-buffer-with-faces filename))))
6755           (kill-buffer buffer))))))
6756
6757 (defun gnus-summary-show-article (&optional arg)
6758   "Force re-fetching of the current article.
6759 If ARG (the prefix) is non-nil, show the raw article without any
6760 article massaging functions being run."
6761   (interactive "P")
6762   (if (not arg)
6763       ;; Select the article the normal way.
6764       (gnus-summary-select-article nil 'force)
6765     ;; Bind the article treatment functions to nil.
6766     (let ((gnus-have-all-headers t)
6767           gnus-article-display-hook
6768           gnus-article-prepare-hook
6769           gnus-break-pages
6770           gnus-show-mime
6771           gnus-visual)
6772       (gnus-summary-select-article nil 'force)))
6773   (gnus-summary-goto-subject gnus-current-article)
6774   (gnus-summary-position-point))
6775
6776 (defun gnus-summary-verbose-headers (&optional arg)
6777   "Toggle permanent full header display.
6778 If ARG is a positive number, turn header display on.
6779 If ARG is a negative number, turn header display off."
6780   (interactive "P")
6781   (setq gnus-show-all-headers
6782         (cond ((or (not (numberp arg))
6783                    (zerop arg))
6784                (not gnus-show-all-headers))
6785               ((natnump arg)
6786                t)))
6787   (gnus-summary-show-article))
6788
6789 (defun gnus-summary-toggle-header (&optional arg)
6790   "Show the headers if they are hidden, or hide them if they are shown.
6791 If ARG is a positive number, show the entire header.
6792 If ARG is a negative number, hide the unwanted header lines."
6793   (interactive "P")
6794   (save-excursion
6795     (set-buffer gnus-article-buffer)
6796     (let* ((buffer-read-only nil)
6797            (inhibit-point-motion-hooks t)
6798            (hidden (text-property-any
6799                     (goto-char (point-min)) (search-forward "\n\n")
6800                     'invisible t))
6801            e)
6802       (goto-char (point-min))
6803       (when (search-forward "\n\n" nil t)
6804         (delete-region (point-min) (1- (point))))
6805       (goto-char (point-min))
6806       (save-excursion
6807         (set-buffer gnus-original-article-buffer)
6808         (goto-char (point-min))
6809         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6810       (insert-buffer-substring gnus-original-article-buffer 1 e)
6811       (let ((article-inhibit-hiding t))
6812         (gnus-run-hooks 'gnus-article-display-hook))
6813       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6814         (gnus-article-hide-headers)))))
6815
6816 (defun gnus-summary-show-all-headers ()
6817   "Make all header lines visible."
6818   (interactive)
6819   (gnus-article-show-all-headers))
6820
6821 (defun gnus-summary-toggle-mime (&optional arg)
6822   "Toggle MIME processing.
6823 If ARG is a positive number, turn MIME processing on."
6824   (interactive "P")
6825   (setq gnus-show-mime
6826         (if (null arg) (not gnus-show-mime)
6827           (> (prefix-numeric-value arg) 0)))
6828   (gnus-summary-select-article t 'force))
6829
6830 (defun gnus-summary-caesar-message (&optional arg)
6831   "Caesar rotate the current article by 13.
6832 The numerical prefix specifies how many places to rotate each letter
6833 forward."
6834   (interactive "P")
6835   (gnus-summary-select-article)
6836   (let ((mail-header-separator ""))
6837     (gnus-eval-in-buffer-window gnus-article-buffer
6838       (save-restriction
6839         (widen)
6840         (let ((start (window-start))
6841               buffer-read-only)
6842           (message-caesar-buffer-body arg)
6843           (set-window-start (get-buffer-window (current-buffer)) start))))))
6844
6845 (defun gnus-summary-stop-page-breaking ()
6846   "Stop page breaking in the current article."
6847   (interactive)
6848   (gnus-summary-select-article)
6849   (gnus-eval-in-buffer-window gnus-article-buffer
6850     (widen)
6851     (when (gnus-visual-p 'page-marker)
6852       (let ((buffer-read-only nil))
6853         (gnus-remove-text-with-property 'gnus-prev)
6854         (gnus-remove-text-with-property 'gnus-next))
6855       (setq gnus-page-broken nil))))
6856
6857 (defun gnus-summary-move-article (&optional n to-newsgroup
6858                                             select-method action)
6859   "Move the current article to a different newsgroup.
6860 If N is a positive number, move the N next articles.
6861 If N is a negative number, move the N previous articles.
6862 If N is nil and any articles have been marked with the process mark,
6863 move those articles instead.
6864 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6865 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6866 re-spool using this method.
6867
6868 For this function to work, both the current newsgroup and the
6869 newsgroup that you want to move to have to support the `request-move'
6870 and `request-accept' functions."
6871   (interactive "P")
6872   (unless action
6873     (setq action 'move))
6874   ;; Disable marking as read.
6875   (let (gnus-mark-article-hook)
6876     (save-window-excursion
6877       (gnus-summary-select-article)))
6878   ;; Check whether the source group supports the required functions.
6879   (cond ((and (eq action 'move)
6880               (not (gnus-check-backend-function
6881                     'request-move-article gnus-newsgroup-name)))
6882          (error "The current group does not support article moving"))
6883         ((and (eq action 'crosspost)
6884               (not (gnus-check-backend-function
6885                     'request-replace-article gnus-newsgroup-name)))
6886          (error "The current group does not support article editing")))
6887   (let ((articles (gnus-summary-work-articles n))
6888         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6889         (names '((move "Move" "Moving")
6890                  (copy "Copy" "Copying")
6891                  (crosspost "Crosspost" "Crossposting")))
6892         (copy-buf (save-excursion
6893                     (nnheader-set-temp-buffer " *copy article*")))
6894         art-group to-method new-xref article to-groups)
6895     (unless (assq action names)
6896       (error "Unknown action %s" action))
6897     ;; Read the newsgroup name.
6898     (when (and (not to-newsgroup)
6899                (not select-method))
6900       (setq to-newsgroup
6901             (gnus-read-move-group-name
6902              (cadr (assq action names))
6903              (symbol-value (intern (format "gnus-current-%s-group" action)))
6904              articles prefix))
6905       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6906     (setq to-method (or select-method
6907                         (gnus-group-name-to-method to-newsgroup)))
6908     ;; Check the method we are to move this article to...
6909     (unless (gnus-check-backend-function
6910              'request-accept-article (car to-method))
6911       (error "%s does not support article copying" (car to-method)))
6912     (unless (gnus-check-server to-method)
6913       (error "Can't open server %s" (car to-method)))
6914     (gnus-message 6 "%s to %s: %s..."
6915                   (caddr (assq action names))
6916                   (or (car select-method) to-newsgroup) articles)
6917     (while articles
6918       (setq article (pop articles))
6919       (setq
6920        art-group
6921        (cond
6922         ;; Move the article.
6923         ((eq action 'move)
6924          ;; Remove this article from future suppression.
6925          (gnus-dup-unsuppress-article article)
6926          (gnus-request-move-article
6927           article                       ; Article to move
6928           gnus-newsgroup-name           ; From newsgroup
6929           (nth 1 (gnus-find-method-for-group
6930                   gnus-newsgroup-name)) ; Server
6931           (list 'gnus-request-accept-article
6932                 to-newsgroup (list 'quote select-method)
6933                 (not articles))         ; Accept form
6934           (not articles)))              ; Only save nov last time
6935         ;; Copy the article.
6936         ((eq action 'copy)
6937          (save-excursion
6938            (set-buffer copy-buf)
6939            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6940              (gnus-request-accept-article
6941               to-newsgroup select-method (not articles)))))
6942         ;; Crosspost the article.
6943         ((eq action 'crosspost)
6944          (let ((xref (message-tokenize-header
6945                       (mail-header-xref (gnus-summary-article-header article))
6946                       " ")))
6947            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6948                                   ":" article))
6949            (unless xref
6950              (setq xref (list (system-name))))
6951            (setq new-xref
6952                  (concat
6953                   (mapconcat 'identity
6954                              (delete "Xref:" (delete new-xref xref))
6955                              " ")
6956                   " " new-xref))
6957            (save-excursion
6958              (set-buffer copy-buf)
6959              ;; First put the article in the destination group.
6960              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6961              (when (consp (setq art-group
6962                                 (gnus-request-accept-article
6963                                  to-newsgroup select-method (not articles))))
6964                (setq new-xref (concat new-xref " " (car art-group)
6965                                       ":" (cdr art-group)))
6966                ;; Now we have the new Xrefs header, so we insert
6967                ;; it and replace the new article.
6968                (nnheader-replace-header "Xref" new-xref)
6969                (gnus-request-replace-article
6970                 (cdr art-group) to-newsgroup (current-buffer))
6971                art-group))))))
6972       (cond
6973        ((not art-group)
6974         (gnus-message 1 "Couldn't %s article %s"
6975                       (cadr (assq action names)) article))
6976        ((and (eq art-group 'junk)
6977              (eq action 'move))
6978         (gnus-summary-mark-article article gnus-canceled-mark)
6979         (gnus-message 4 "Deleted article %s" article))
6980        (t
6981         (let* ((pto-group (gnus-group-prefixed-name
6982                            (car art-group) to-method))
6983                (entry
6984                 (gnus-gethash pto-group gnus-newsrc-hashtb))
6985                (info (nth 2 entry))
6986                (to-group (gnus-info-group info)))
6987           ;; Update the group that has been moved to.
6988           (when (and info
6989                      (memq action '(move copy)))
6990             (unless (member to-group to-groups)
6991               (push to-group to-groups))
6992
6993             (unless (memq article gnus-newsgroup-unreads)
6994               (gnus-info-set-read
6995                info (gnus-add-to-range (gnus-info-read info)
6996                                        (list (cdr art-group)))))
6997
6998             ;; Copy any marks over to the new group.
6999             (let ((marks gnus-article-mark-lists)
7000                   (to-article (cdr art-group)))
7001
7002               ;; See whether the article is to be put in the cache.
7003               (when gnus-use-cache
7004                 (gnus-cache-possibly-enter-article
7005                  to-group to-article
7006                  (let ((header (copy-sequence
7007                                 (gnus-summary-article-header article))))
7008                    (mail-header-set-number header to-article)
7009                    header)
7010                  (memq article gnus-newsgroup-marked)
7011                  (memq article gnus-newsgroup-dormant)
7012                  (memq article gnus-newsgroup-unreads)))
7013
7014               (when (and (equal to-group gnus-newsgroup-name)
7015                          (not (memq article gnus-newsgroup-unreads)))
7016                 ;; Mark this article as read in this group.
7017                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7018                 (setcdr (gnus-active to-group) to-article)
7019                 (setcdr gnus-newsgroup-active to-article))
7020
7021               (while marks
7022                 (when (memq article (symbol-value
7023                                      (intern (format "gnus-newsgroup-%s"
7024                                                      (caar marks)))))
7025                   ;; If the other group is the same as this group,
7026                   ;; then we have to add the mark to the list.
7027                   (when (equal to-group gnus-newsgroup-name)
7028                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7029                          (cons to-article
7030                                (symbol-value
7031                                 (intern (format "gnus-newsgroup-%s"
7032                                                 (caar marks)))))))
7033                   ;; Copy the marks to other group.
7034                   (gnus-add-marked-articles
7035                    to-group (cdar marks) (list to-article) info))
7036                 (setq marks (cdr marks)))
7037
7038               (gnus-dribble-enter
7039                (concat "(gnus-group-set-info '"
7040                        (gnus-prin1-to-string (gnus-get-info to-group))
7041                        ")"))))
7042
7043           ;; Update the Xref header in this article to point to
7044           ;; the new crossposted article we have just created.
7045           (when (eq action 'crosspost)
7046             (save-excursion
7047               (set-buffer copy-buf)
7048               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7049               (nnheader-replace-header "Xref" new-xref)
7050               (gnus-request-replace-article
7051                article gnus-newsgroup-name (current-buffer)))))
7052
7053         ;;;!!!Why is this necessary?
7054         (set-buffer gnus-summary-buffer)
7055         
7056         (gnus-summary-goto-subject article)
7057         (when (eq action 'move)
7058           (gnus-summary-mark-article article gnus-canceled-mark))))
7059       (gnus-summary-remove-process-mark article))
7060     ;; Re-activate all groups that have been moved to.
7061     (while to-groups
7062       (save-excursion
7063         (set-buffer gnus-group-buffer)
7064         (when (gnus-group-goto-group (car to-groups) t)
7065           (gnus-group-get-new-news-this-group 1 t))
7066         (pop to-groups)))
7067
7068     (gnus-kill-buffer copy-buf)
7069     (gnus-summary-position-point)
7070     (gnus-set-mode-line 'summary)))
7071
7072 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7073   "Move the current article to a different newsgroup.
7074 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7075 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7076 re-spool using this method."
7077   (interactive "P")
7078   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7079
7080 (defun gnus-summary-crosspost-article (&optional n)
7081   "Crosspost the current article to some other group."
7082   (interactive "P")
7083   (gnus-summary-move-article n nil nil 'crosspost))
7084
7085 (defcustom gnus-summary-respool-default-method nil
7086   "Default method for respooling an article.
7087 If nil, use to the current newsgroup method."
7088   :type `(choice (gnus-select-method :value (nnml ""))
7089                  (const nil))
7090   :group 'gnus-summary-mail)
7091
7092 (defun gnus-summary-respool-article (&optional n method)
7093   "Respool the current article.
7094 The article will be squeezed through the mail spooling process again,
7095 which means that it will be put in some mail newsgroup or other
7096 depending on `nnmail-split-methods'.
7097 If N is a positive number, respool the N next articles.
7098 If N is a negative number, respool the N previous articles.
7099 If N is nil and any articles have been marked with the process mark,
7100 respool those articles instead.
7101
7102 Respooling can be done both from mail groups and \"real\" newsgroups.
7103 In the former case, the articles in question will be moved from the
7104 current group into whatever groups they are destined to.  In the
7105 latter case, they will be copied into the relevant groups."
7106   (interactive
7107    (list current-prefix-arg
7108          (let* ((methods (gnus-methods-using 'respool))
7109                 (methname
7110                  (symbol-name (or gnus-summary-respool-default-method
7111                                   (car (gnus-find-method-for-group
7112                                         gnus-newsgroup-name)))))
7113                 (method
7114                  (gnus-completing-read
7115                   methname "What backend do you want to use when respooling?"
7116                   methods nil t nil 'gnus-mail-method-history))
7117                 ms)
7118            (cond
7119             ((zerop (length (setq ms (gnus-servers-using-backend
7120                                       (intern method)))))
7121              (list (intern method) ""))
7122             ((= 1 (length ms))
7123              (car ms))
7124             (t
7125              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7126                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7127                            ms-alist))))))))
7128   (unless method
7129     (error "No method given for respooling"))
7130   (if (assoc (symbol-name
7131               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7132              (gnus-methods-using 'respool))
7133       (gnus-summary-move-article n nil method)
7134     (gnus-summary-copy-article n nil method)))
7135
7136 (defun gnus-summary-import-article (file)
7137   "Import an arbitrary file into a mail newsgroup."
7138   (interactive "fImport file: ")
7139   (let ((group gnus-newsgroup-name)
7140         (now (current-time))
7141         atts lines)
7142     (unless (gnus-check-backend-function 'request-accept-article group)
7143       (error "%s does not support article importing" group))
7144     (or (file-readable-p file)
7145         (not (file-regular-p file))
7146         (error "Can't read %s" file))
7147     (save-excursion
7148       (set-buffer (gnus-get-buffer-create " *import file*"))
7149       (buffer-disable-undo (current-buffer))
7150       (erase-buffer)
7151       (insert-file-contents file)
7152       (goto-char (point-min))
7153       (unless (nnheader-article-p)
7154         ;; This doesn't look like an article, so we fudge some headers.
7155         (setq atts (file-attributes file)
7156               lines (count-lines (point-min) (point-max)))
7157         (insert "From: " (read-string "From: ") "\n"
7158                 "Subject: " (read-string "Subject: ") "\n"
7159                 "Date: " (timezone-make-date-arpa-standard
7160                           (current-time-string (nth 5 atts))
7161                           (current-time-zone now)
7162                           (current-time-zone now))
7163                 "\n"
7164                 "Message-ID: " (message-make-message-id) "\n"
7165                 "Lines: " (int-to-string lines) "\n"
7166                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7167       (gnus-request-accept-article group nil t)
7168       (kill-buffer (current-buffer)))))
7169
7170 (defun gnus-summary-article-posted-p ()
7171   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7172 This will be the case if the article has both been mailed and posted."
7173   (interactive)
7174   (let ((id (mail-header-references (gnus-summary-article-header)))
7175         (gnus-override-method
7176          (or gnus-refer-article-method gnus-select-method)))
7177     (if (gnus-request-head id "")
7178         (gnus-message 2 "The current message was found on %s"
7179                       gnus-override-method)
7180       (gnus-message 2 "The current message couldn't be found on %s"
7181                     gnus-override-method)
7182       nil)))
7183
7184 (defun gnus-summary-expire-articles (&optional now)
7185   "Expire all articles that are marked as expirable in the current group."
7186   (interactive)
7187   (when (gnus-check-backend-function
7188          'request-expire-articles gnus-newsgroup-name)
7189     ;; This backend supports expiry.
7190     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7191            (expirable (if total
7192                           (progn
7193                             ;; We need to update the info for
7194                             ;; this group for `gnus-list-of-read-articles'
7195                             ;; to give us the right answer.
7196                             (gnus-run-hooks 'gnus-exit-group-hook)
7197                             (gnus-summary-update-info)
7198                             (gnus-list-of-read-articles gnus-newsgroup-name))
7199                         (setq gnus-newsgroup-expirable
7200                               (sort gnus-newsgroup-expirable '<))))
7201            (expiry-wait (if now 'immediate
7202                           (gnus-group-find-parameter
7203                            gnus-newsgroup-name 'expiry-wait)))
7204            es)
7205       (when expirable
7206         ;; There are expirable articles in this group, so we run them
7207         ;; through the expiry process.
7208         (gnus-message 6 "Expiring articles...")
7209         ;; The list of articles that weren't expired is returned.
7210         (save-excursion
7211           (if expiry-wait
7212               (let ((nnmail-expiry-wait-function nil)
7213                     (nnmail-expiry-wait expiry-wait))
7214                 (setq es (gnus-request-expire-articles
7215                           expirable gnus-newsgroup-name)))
7216             (setq es (gnus-request-expire-articles
7217                       expirable gnus-newsgroup-name))))
7218         (unless total
7219           (setq gnus-newsgroup-expirable es))
7220         ;; We go through the old list of expirable, and mark all
7221         ;; really expired articles as nonexistent.
7222         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7223           (let ((gnus-use-cache nil))
7224             (while expirable
7225               (unless (memq (car expirable) es)
7226                 (when (gnus-data-find (car expirable))
7227                   (gnus-summary-mark-article
7228                    (car expirable) gnus-canceled-mark)))
7229               (setq expirable (cdr expirable)))))
7230         (gnus-message 6 "Expiring articles...done")))))
7231
7232 (defun gnus-summary-expire-articles-now ()
7233   "Expunge all expirable articles in the current group.
7234 This means that *all* articles that are marked as expirable will be
7235 deleted forever, right now."
7236   (interactive)
7237   (or gnus-expert-user
7238       (gnus-yes-or-no-p
7239        "Are you really, really, really sure you want to delete all these messages? ")
7240       (error "Phew!"))
7241   (gnus-summary-expire-articles t))
7242
7243 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7244 (defun gnus-summary-delete-article (&optional n)
7245   "Delete the N next (mail) articles.
7246 This command actually deletes articles.  This is not a marking
7247 command.  The article will disappear forever from your life, never to
7248 return.
7249 If N is negative, delete backwards.
7250 If N is nil and articles have been marked with the process mark,
7251 delete these instead."
7252   (interactive "P")
7253   (unless (gnus-check-backend-function 'request-expire-articles
7254                                        gnus-newsgroup-name)
7255     (error "The current newsgroup does not support article deletion"))
7256   ;; Compute the list of articles to delete.
7257   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7258         not-deleted)
7259     (if (and gnus-novice-user
7260              (not (gnus-yes-or-no-p
7261                    (format "Do you really want to delete %s forever? "
7262                            (if (> (length articles) 1)
7263                                (format "these %s articles" (length articles))
7264                              "this article")))))
7265         ()
7266       ;; Delete the articles.
7267       (setq not-deleted (gnus-request-expire-articles
7268                          articles gnus-newsgroup-name 'force))
7269       (while articles
7270         (gnus-summary-remove-process-mark (car articles))
7271         ;; The backend might not have been able to delete the article
7272         ;; after all.
7273         (unless (memq (car articles) not-deleted)
7274           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7275         (setq articles (cdr articles)))
7276       (when not-deleted
7277         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7278     (gnus-summary-position-point)
7279     (gnus-set-mode-line 'summary)
7280     not-deleted))
7281
7282 (defun gnus-summary-edit-article (&optional force)
7283   "Edit the current article.
7284 This will have permanent effect only in mail groups.
7285 If FORCE is non-nil, allow editing of articles even in read-only
7286 groups."
7287   (interactive "P")
7288   (save-excursion
7289     (set-buffer gnus-summary-buffer)
7290     (gnus-set-global-variables)
7291     (when (and (not force)
7292                (gnus-group-read-only-p))
7293       (error "The current newsgroup does not support article editing"))
7294     ;; Select article if needed.
7295     (unless (eq (gnus-summary-article-number)
7296                 gnus-current-article)
7297       (gnus-summary-select-article t))
7298     (gnus-article-date-original)
7299     (gnus-article-edit-article
7300      `(lambda (no-highlight)
7301         (gnus-summary-edit-article-done
7302          ,(or (mail-header-references gnus-current-headers) "")
7303          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7304
7305 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7306
7307 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7308                                                  no-highlight)
7309   "Make edits to the current article permanent."
7310   (interactive)
7311   ;; Replace the article.
7312   (let ((buf (current-buffer)))
7313     (with-temp-buffer
7314       (insert-buffer buf)
7315       (if (and (not read-only)
7316                (not (gnus-request-replace-article
7317                      (cdr gnus-article-current) (car gnus-article-current)
7318                      (current-buffer))))
7319           (error "Couldn't replace article")
7320         ;; Update the summary buffer.
7321         (if (and references
7322                  (equal (message-tokenize-header references " ")
7323                         (message-tokenize-header
7324                          (or (message-fetch-field "references") "") " ")))
7325             ;; We only have to update this line.
7326             (save-excursion
7327               (save-restriction
7328                 (message-narrow-to-head)
7329                 (let ((head (buffer-string))
7330                       header)
7331                   (with-temp-buffer
7332                     (insert (format "211 %d Article retrieved.\n"
7333                                     (cdr gnus-article-current)))
7334                     (insert head)
7335                     (insert ".\n")
7336                     (let ((nntp-server-buffer (current-buffer)))
7337                       (setq header (car (gnus-get-newsgroup-headers
7338                                          (save-excursion
7339                                            (set-buffer gnus-summary-buffer)
7340                                            gnus-newsgroup-dependencies)
7341                                          t))))
7342                     (save-excursion
7343                       (set-buffer gnus-summary-buffer)
7344                       (gnus-data-set-header
7345                        (gnus-data-find (cdr gnus-article-current))
7346                        header)
7347                       (gnus-summary-update-article-line
7348                        (cdr gnus-article-current) header))))))
7349           ;; Update threads.
7350           (set-buffer (or buffer gnus-summary-buffer))
7351           (gnus-summary-update-article (cdr gnus-article-current)))
7352         ;; Prettify the article buffer again.
7353         (unless no-highlight
7354           (save-excursion
7355             (set-buffer gnus-article-buffer)
7356             (gnus-run-hooks 'gnus-article-display-hook)
7357             (set-buffer gnus-original-article-buffer)
7358             (gnus-request-article
7359              (cdr gnus-article-current)
7360              (car gnus-article-current) (current-buffer))))
7361         ;; Prettify the summary buffer line.
7362         (when (gnus-visual-p 'summary-highlight 'highlight)
7363           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7364
7365 (defun gnus-summary-edit-wash (key)
7366   "Perform editing command KEY in the article buffer."
7367   (interactive
7368    (list
7369     (progn
7370       (message "%s" (concat (this-command-keys) "- "))
7371       (read-char))))
7372   (message "")
7373   (gnus-summary-edit-article)
7374   (execute-kbd-macro (concat (this-command-keys) key))
7375   (gnus-article-edit-done))
7376
7377 ;;; Respooling
7378
7379 (defun gnus-summary-respool-query (&optional silent trace)
7380   "Query where the respool algorithm would put this article."
7381   (interactive)
7382   (let (gnus-mark-article-hook)
7383     (gnus-summary-select-article)
7384     (save-excursion
7385       (set-buffer gnus-original-article-buffer)
7386       (save-restriction
7387         (message-narrow-to-head)
7388         (let ((groups (nnmail-article-group 'identity trace)))
7389           (unless silent
7390             (if groups
7391                 (message "This message would go to %s"
7392                          (mapconcat 'car groups ", "))
7393               (message "This message would go to no groups"))
7394             groups))))))
7395
7396 (defun gnus-summary-respool-trace ()
7397   "Trace where the respool algorithm would put this article.
7398 Display a buffer showing all fancy splitting patterns which matched."
7399   (interactive)
7400   (gnus-summary-respool-query nil t))
7401
7402 ;; Summary marking commands.
7403
7404 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7405   "Mark articles which has the same subject as read, and then select the next.
7406 If UNMARK is positive, remove any kind of mark.
7407 If UNMARK is negative, tick articles."
7408   (interactive "P")
7409   (when unmark
7410     (setq unmark (prefix-numeric-value unmark)))
7411   (let ((count
7412          (gnus-summary-mark-same-subject
7413           (gnus-summary-article-subject) unmark)))
7414     ;; Select next unread article.  If auto-select-same mode, should
7415     ;; select the first unread article.
7416     (gnus-summary-next-article t (and gnus-auto-select-same
7417                                       (gnus-summary-article-subject)))
7418     (gnus-message 7 "%d article%s marked as %s"
7419                   count (if (= count 1) " is" "s are")
7420                   (if unmark "unread" "read"))))
7421
7422 (defun gnus-summary-kill-same-subject (&optional unmark)
7423   "Mark articles which has the same subject as read.
7424 If UNMARK is positive, remove any kind of mark.
7425 If UNMARK is negative, tick articles."
7426   (interactive "P")
7427   (when unmark
7428     (setq unmark (prefix-numeric-value unmark)))
7429   (let ((count
7430          (gnus-summary-mark-same-subject
7431           (gnus-summary-article-subject) unmark)))
7432     ;; If marked as read, go to next unread subject.
7433     (when (null unmark)
7434       ;; Go to next unread subject.
7435       (gnus-summary-next-subject 1 t))
7436     (gnus-message 7 "%d articles are marked as %s"
7437                   count (if unmark "unread" "read"))))
7438
7439 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7440   "Mark articles with same SUBJECT as read, and return marked number.
7441 If optional argument UNMARK is positive, remove any kinds of marks.
7442 If optional argument UNMARK is negative, mark articles as unread instead."
7443   (let ((count 1))
7444     (save-excursion
7445       (cond
7446        ((null unmark)                   ; Mark as read.
7447         (while (and
7448                 (progn
7449                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7450                   (gnus-summary-show-thread) t)
7451                 (gnus-summary-find-subject subject))
7452           (setq count (1+ count))))
7453        ((> unmark 0)                    ; Tick.
7454         (while (and
7455                 (progn
7456                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7457                   (gnus-summary-show-thread) t)
7458                 (gnus-summary-find-subject subject))
7459           (setq count (1+ count))))
7460        (t                               ; Mark as unread.
7461         (while (and
7462                 (progn
7463                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7464                   (gnus-summary-show-thread) t)
7465                 (gnus-summary-find-subject subject))
7466           (setq count (1+ count)))))
7467       (gnus-set-mode-line 'summary)
7468       ;; Return the number of marked articles.
7469       count)))
7470
7471 (defun gnus-summary-mark-as-processable (n &optional unmark)
7472   "Set the process mark on the next N articles.
7473 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7474 the process mark instead.  The difference between N and the actual
7475 number of articles marked is returned."
7476   (interactive "p")
7477   (let ((backward (< n 0))
7478         (n (abs n)))
7479     (while (and
7480             (> n 0)
7481             (if unmark
7482                 (gnus-summary-remove-process-mark
7483                  (gnus-summary-article-number))
7484               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7485             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7486       (setq n (1- n)))
7487     (when (/= 0 n)
7488       (gnus-message 7 "No more articles"))
7489     (gnus-summary-recenter)
7490     (gnus-summary-position-point)
7491     n))
7492
7493 (defun gnus-summary-unmark-as-processable (n)
7494   "Remove the process mark from the next N articles.
7495 If N is negative, unmark backward instead.  The difference between N and
7496 the actual number of articles unmarked is returned."
7497   (interactive "p")
7498   (gnus-summary-mark-as-processable n t))
7499
7500 (defun gnus-summary-unmark-all-processable ()
7501   "Remove the process mark from all articles."
7502   (interactive)
7503   (save-excursion
7504     (while gnus-newsgroup-processable
7505       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7506   (gnus-summary-position-point))
7507
7508 (defun gnus-summary-mark-as-expirable (n)
7509   "Mark N articles forward as expirable.
7510 If N is negative, mark backward instead.  The difference between N and
7511 the actual number of articles marked is returned."
7512   (interactive "p")
7513   (gnus-summary-mark-forward n gnus-expirable-mark))
7514
7515 (defun gnus-summary-mark-article-as-replied (article)
7516   "Mark ARTICLE replied and update the summary line."
7517   (push article gnus-newsgroup-replied)
7518   (let ((buffer-read-only nil))
7519     (when (gnus-summary-goto-subject article)
7520       (gnus-summary-update-secondary-mark article))))
7521
7522 (defun gnus-summary-set-bookmark (article)
7523   "Set a bookmark in current article."
7524   (interactive (list (gnus-summary-article-number)))
7525   (when (or (not (get-buffer gnus-article-buffer))
7526             (not gnus-current-article)
7527             (not gnus-article-current)
7528             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7529     (error "No current article selected"))
7530   ;; Remove old bookmark, if one exists.
7531   (let ((old (assq article gnus-newsgroup-bookmarks)))
7532     (when old
7533       (setq gnus-newsgroup-bookmarks
7534             (delq old gnus-newsgroup-bookmarks))))
7535   ;; Set the new bookmark, which is on the form
7536   ;; (article-number . line-number-in-body).
7537   (push
7538    (cons article
7539          (save-excursion
7540            (set-buffer gnus-article-buffer)
7541            (count-lines
7542             (min (point)
7543                  (save-excursion
7544                    (goto-char (point-min))
7545                    (search-forward "\n\n" nil t)
7546                    (point)))
7547             (point))))
7548    gnus-newsgroup-bookmarks)
7549   (gnus-message 6 "A bookmark has been added to the current article."))
7550
7551 (defun gnus-summary-remove-bookmark (article)
7552   "Remove the bookmark from the current article."
7553   (interactive (list (gnus-summary-article-number)))
7554   ;; Remove old bookmark, if one exists.
7555   (let ((old (assq article gnus-newsgroup-bookmarks)))
7556     (if old
7557         (progn
7558           (setq gnus-newsgroup-bookmarks
7559                 (delq old gnus-newsgroup-bookmarks))
7560           (gnus-message 6 "Removed bookmark."))
7561       (gnus-message 6 "No bookmark in current article."))))
7562
7563 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7564 (defun gnus-summary-mark-as-dormant (n)
7565   "Mark N articles forward as dormant.
7566 If N is negative, mark backward instead.  The difference between N and
7567 the actual number of articles marked is returned."
7568   (interactive "p")
7569   (gnus-summary-mark-forward n gnus-dormant-mark))
7570
7571 (defun gnus-summary-set-process-mark (article)
7572   "Set the process mark on ARTICLE and update the summary line."
7573   (setq gnus-newsgroup-processable
7574         (cons article
7575               (delq article gnus-newsgroup-processable)))
7576   (when (gnus-summary-goto-subject article)
7577     (gnus-summary-show-thread)
7578     (gnus-summary-goto-subject article)
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-goto-subject article)
7587     (gnus-summary-update-secondary-mark article)))
7588
7589 (defun gnus-summary-set-saved-mark (article)
7590   "Set the process mark on ARTICLE and update the summary line."
7591   (push article gnus-newsgroup-saved)
7592   (when (gnus-summary-goto-subject article)
7593     (gnus-summary-update-secondary-mark article)))
7594
7595 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7596   "Mark N articles as read forwards.
7597 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7598 The difference between N and the actual number of articles marked is
7599 returned."
7600   (interactive "p")
7601   (let ((backward (< n 0))
7602         (gnus-summary-goto-unread
7603          (and gnus-summary-goto-unread
7604               (not (eq gnus-summary-goto-unread 'never))
7605               (not (memq mark (list gnus-unread-mark
7606                                     gnus-ticked-mark gnus-dormant-mark)))))
7607         (n (abs n))
7608         (mark (or mark gnus-del-mark)))
7609     (while (and (> n 0)
7610                 (gnus-summary-mark-article nil mark no-expire)
7611                 (zerop (gnus-summary-next-subject
7612                         (if backward -1 1)
7613                         (and gnus-summary-goto-unread
7614                              (not (eq gnus-summary-goto-unread 'never)))
7615                         t)))
7616       (setq n (1- n)))
7617     (when (/= 0 n)
7618       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7619     (gnus-summary-recenter)
7620     (gnus-summary-position-point)
7621     (gnus-set-mode-line 'summary)
7622     n))
7623
7624 (defun gnus-summary-mark-article-as-read (mark)
7625   "Mark the current article quickly as read with MARK."
7626   (let ((article (gnus-summary-article-number)))
7627     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7628     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7629     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7630     (push (cons article mark) gnus-newsgroup-reads)
7631     ;; Possibly remove from cache, if that is used.
7632     (when gnus-use-cache
7633       (gnus-cache-enter-remove-article article))
7634     ;; Allow the backend to change the mark.
7635     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7636     ;; Check for auto-expiry.
7637     (when (and gnus-newsgroup-auto-expire
7638                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7639                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7640                    (= mark gnus-ancient-mark)
7641                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7642                    (= mark gnus-duplicate-mark)))
7643       (setq mark gnus-expirable-mark)
7644       (push article gnus-newsgroup-expirable))
7645     ;; Set the mark in the buffer.
7646     (gnus-summary-update-mark mark 'unread)
7647     t))
7648
7649 (defun gnus-summary-mark-article-as-unread (mark)
7650   "Mark the current article quickly as unread with MARK."
7651   (let* ((article (gnus-summary-article-number))
7652          (old-mark (gnus-summary-article-mark article)))
7653     (if (eq mark old-mark)
7654         t
7655       (if (<= article 0)
7656           (progn
7657             (gnus-error 1 "Can't mark negative article numbers")
7658             nil)
7659         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7660         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7661         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7662         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7663         (cond ((= mark gnus-ticked-mark)
7664                (push article gnus-newsgroup-marked))
7665               ((= mark gnus-dormant-mark)
7666                (push article gnus-newsgroup-dormant))
7667               (t
7668                (push article gnus-newsgroup-unreads)))
7669         (gnus-pull article gnus-newsgroup-reads)
7670
7671         ;; See whether the article is to be put in the cache.
7672         (and gnus-use-cache
7673              (vectorp (gnus-summary-article-header article))
7674              (save-excursion
7675                (gnus-cache-possibly-enter-article
7676                 gnus-newsgroup-name article
7677                 (gnus-summary-article-header article)
7678                 (= mark gnus-ticked-mark)
7679                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7680
7681         ;; Fix the mark.
7682         (gnus-summary-update-mark mark 'unread)
7683         t))))
7684
7685 (defun gnus-summary-mark-article (&optional article mark no-expire)
7686   "Mark ARTICLE with MARK.  MARK can be any character.
7687 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7688 `??' (dormant) and `?E' (expirable).
7689 If MARK is nil, then the default character `?D' is used.
7690 If ARTICLE is nil, then the article on the current line will be
7691 marked."
7692   ;; The mark might be a string.
7693   (when (stringp mark)
7694     (setq mark (aref mark 0)))
7695   ;; If no mark is given, then we check auto-expiring.
7696   (and (not no-expire)
7697        gnus-newsgroup-auto-expire
7698        (or (not mark)
7699            (and (gnus-characterp mark)
7700                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7701                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7702                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7703                     (= mark gnus-duplicate-mark))))
7704        (setq mark gnus-expirable-mark))
7705   (let* ((mark (or mark gnus-del-mark))
7706          (article (or article (gnus-summary-article-number)))
7707          (old-mark (gnus-summary-article-mark article)))
7708     (if (eq mark old-mark)
7709         t
7710       (unless article
7711         (error "No article on current line"))
7712       (if (not (if (or (= mark gnus-unread-mark)
7713                        (= mark gnus-ticked-mark)
7714                        (= mark gnus-dormant-mark))
7715                    (gnus-mark-article-as-unread article mark)
7716                  (gnus-mark-article-as-read article mark)))
7717           t
7718         ;; See whether the article is to be put in the cache.
7719         (and gnus-use-cache
7720              (not (= mark gnus-canceled-mark))
7721              (vectorp (gnus-summary-article-header article))
7722              (save-excursion
7723                (gnus-cache-possibly-enter-article
7724                 gnus-newsgroup-name article
7725                 (gnus-summary-article-header article)
7726                 (= mark gnus-ticked-mark)
7727                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7728
7729         (when (gnus-summary-goto-subject article nil t)
7730           (let ((buffer-read-only nil))
7731             (gnus-summary-show-thread)
7732             ;; Fix the mark.
7733             (gnus-summary-update-mark mark 'unread)
7734             t))))))
7735
7736 (defun gnus-summary-update-secondary-mark (article)
7737   "Update the secondary (read, process, cache) mark."
7738   (gnus-summary-update-mark
7739    (cond ((memq article gnus-newsgroup-processable)
7740           gnus-process-mark)
7741          ((memq article gnus-newsgroup-cached)
7742           gnus-cached-mark)
7743          ((memq article gnus-newsgroup-replied)
7744           gnus-replied-mark)
7745          ((memq article gnus-newsgroup-saved)
7746           gnus-saved-mark)
7747          (t gnus-unread-mark))
7748    'replied)
7749   (when (gnus-visual-p 'summary-highlight 'highlight)
7750     (gnus-run-hooks 'gnus-summary-update-hook))
7751   t)
7752
7753 (defun gnus-summary-update-mark (mark type)
7754   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7755         (buffer-read-only nil))
7756     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7757     (when (looking-at "\r")
7758       (incf forward))
7759     (when (and forward
7760                (<= (+ forward (point)) (point-max)))
7761       ;; Go to the right position on the line.
7762       (goto-char (+ forward (point)))
7763       ;; Replace the old mark with the new mark.
7764       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7765       ;; Optionally update the marks by some user rule.
7766       (when (eq type 'unread)
7767         (gnus-data-set-mark
7768          (gnus-data-find (gnus-summary-article-number)) mark)
7769         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7770
7771 (defun gnus-mark-article-as-read (article &optional mark)
7772   "Enter ARTICLE in the pertinent lists and remove it from others."
7773   ;; Make the article expirable.
7774   (let ((mark (or mark gnus-del-mark)))
7775     (if (= mark gnus-expirable-mark)
7776         (push article gnus-newsgroup-expirable)
7777       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7778     ;; Remove from unread and marked lists.
7779     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7780     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7781     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7782     (push (cons article mark) gnus-newsgroup-reads)
7783     ;; Possibly remove from cache, if that is used.
7784     (when gnus-use-cache
7785       (gnus-cache-enter-remove-article article))
7786     t))
7787
7788 (defun gnus-mark-article-as-unread (article &optional mark)
7789   "Enter ARTICLE in the pertinent lists and remove it from others."
7790   (let ((mark (or mark gnus-ticked-mark)))
7791     (if (<= article 0)
7792         (progn
7793           (gnus-error 1 "Can't mark negative article numbers")
7794           nil)
7795       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7796             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7797             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7798             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7799
7800       ;; Unsuppress duplicates?
7801       (when gnus-suppress-duplicates
7802         (gnus-dup-unsuppress-article article))
7803
7804       (cond ((= mark gnus-ticked-mark)
7805              (push article gnus-newsgroup-marked))
7806             ((= mark gnus-dormant-mark)
7807              (push article gnus-newsgroup-dormant))
7808             (t
7809              (push article gnus-newsgroup-unreads)))
7810       (gnus-pull article gnus-newsgroup-reads)
7811       t)))
7812
7813 (defalias 'gnus-summary-mark-as-unread-forward
7814   'gnus-summary-tick-article-forward)
7815 (make-obsolete 'gnus-summary-mark-as-unread-forward
7816                'gnus-summary-tick-article-forward)
7817 (defun gnus-summary-tick-article-forward (n)
7818   "Tick N articles forwards.
7819 If N is negative, tick backwards instead.
7820 The difference between N and the number of articles ticked is returned."
7821   (interactive "p")
7822   (gnus-summary-mark-forward n gnus-ticked-mark))
7823
7824 (defalias 'gnus-summary-mark-as-unread-backward
7825   'gnus-summary-tick-article-backward)
7826 (make-obsolete 'gnus-summary-mark-as-unread-backward
7827                'gnus-summary-tick-article-backward)
7828 (defun gnus-summary-tick-article-backward (n)
7829   "Tick N articles backwards.
7830 The difference between N and the number of articles ticked is returned."
7831   (interactive "p")
7832   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7833
7834 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7835 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7836 (defun gnus-summary-tick-article (&optional article clear-mark)
7837   "Mark current article as unread.
7838 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7839 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7840   (interactive)
7841   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7842                                        gnus-ticked-mark)))
7843
7844 (defun gnus-summary-mark-as-read-forward (n)
7845   "Mark N articles as read forwards.
7846 If N is negative, mark backwards instead.
7847 The difference between N and the actual number of articles marked is
7848 returned."
7849   (interactive "p")
7850   (gnus-summary-mark-forward n gnus-del-mark t))
7851
7852 (defun gnus-summary-mark-as-read-backward (n)
7853   "Mark the N articles as read backwards.
7854 The difference between N and the actual number of articles marked is
7855 returned."
7856   (interactive "p")
7857   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7858
7859 (defun gnus-summary-mark-as-read (&optional article mark)
7860   "Mark current article as read.
7861 ARTICLE specifies the article to be marked as read.
7862 MARK specifies a string to be inserted at the beginning of the line."
7863   (gnus-summary-mark-article article mark))
7864
7865 (defun gnus-summary-clear-mark-forward (n)
7866   "Clear marks from N articles forward.
7867 If N is negative, clear backward instead.
7868 The difference between N and the number of marks cleared is returned."
7869   (interactive "p")
7870   (gnus-summary-mark-forward n gnus-unread-mark))
7871
7872 (defun gnus-summary-clear-mark-backward (n)
7873   "Clear marks from N articles backward.
7874 The difference between N and the number of marks cleared is returned."
7875   (interactive "p")
7876   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7877
7878 (defun gnus-summary-mark-unread-as-read ()
7879   "Intended to be used by `gnus-summary-mark-article-hook'."
7880   (when (memq gnus-current-article gnus-newsgroup-unreads)
7881     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7882
7883 (defun gnus-summary-mark-read-and-unread-as-read ()
7884   "Intended to be used by `gnus-summary-mark-article-hook'."
7885   (let ((mark (gnus-summary-article-mark)))
7886     (when (or (gnus-unread-mark-p mark)
7887               (gnus-read-mark-p mark))
7888       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7889
7890 (defun gnus-summary-mark-region-as-read (point mark all)
7891   "Mark all unread articles between point and mark as read.
7892 If given a prefix, mark all articles between point and mark as read,
7893 even ticked and dormant ones."
7894   (interactive "r\nP")
7895   (save-excursion
7896     (let (article)
7897       (goto-char point)
7898       (beginning-of-line)
7899       (while (and
7900               (< (point) mark)
7901               (progn
7902                 (when (or all
7903                           (memq (setq article (gnus-summary-article-number))
7904                                 gnus-newsgroup-unreads))
7905                   (gnus-summary-mark-article article gnus-del-mark))
7906                 t)
7907               (gnus-summary-find-next))))))
7908
7909 (defun gnus-summary-mark-below (score mark)
7910   "Mark articles with score less than SCORE with MARK."
7911   (interactive "P\ncMark: ")
7912   (setq score (if score
7913                   (prefix-numeric-value score)
7914                 (or gnus-summary-default-score 0)))
7915   (save-excursion
7916     (set-buffer gnus-summary-buffer)
7917     (goto-char (point-min))
7918     (while
7919         (progn
7920           (and (< (gnus-summary-article-score) score)
7921                (gnus-summary-mark-article nil mark))
7922           (gnus-summary-find-next)))))
7923
7924 (defun gnus-summary-kill-below (&optional score)
7925   "Mark articles with score below SCORE as read."
7926   (interactive "P")
7927   (gnus-summary-mark-below score gnus-killed-mark))
7928
7929 (defun gnus-summary-clear-above (&optional score)
7930   "Clear all marks from articles with score above SCORE."
7931   (interactive "P")
7932   (gnus-summary-mark-above score gnus-unread-mark))
7933
7934 (defun gnus-summary-tick-above (&optional score)
7935   "Tick all articles with score above SCORE."
7936   (interactive "P")
7937   (gnus-summary-mark-above score gnus-ticked-mark))
7938
7939 (defun gnus-summary-mark-above (score mark)
7940   "Mark articles with score over SCORE with MARK."
7941   (interactive "P\ncMark: ")
7942   (setq score (if score
7943                   (prefix-numeric-value score)
7944                 (or gnus-summary-default-score 0)))
7945   (save-excursion
7946     (set-buffer gnus-summary-buffer)
7947     (goto-char (point-min))
7948     (while (and (progn
7949                   (when (> (gnus-summary-article-score) score)
7950                     (gnus-summary-mark-article nil mark))
7951                   t)
7952                 (gnus-summary-find-next)))))
7953
7954 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7955 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7956 (defun gnus-summary-limit-include-expunged (&optional no-error)
7957   "Display all the hidden articles that were expunged for low scores."
7958   (interactive)
7959   (let ((buffer-read-only nil))
7960     (let ((scored gnus-newsgroup-scored)
7961           headers h)
7962       (while scored
7963         (unless (gnus-summary-goto-subject (caar scored))
7964           (and (setq h (gnus-summary-article-header (caar scored)))
7965                (< (cdar scored) gnus-summary-expunge-below)
7966                (push h headers)))
7967         (setq scored (cdr scored)))
7968       (if (not headers)
7969           (when (not no-error)
7970             (error "No expunged articles hidden"))
7971         (goto-char (point-min))
7972         (gnus-summary-prepare-unthreaded (nreverse headers))
7973         (goto-char (point-min))
7974         (gnus-summary-position-point)
7975         t))))
7976
7977 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7978   "Mark all unread articles in this newsgroup as read.
7979 If prefix argument ALL is non-nil, ticked and dormant articles will
7980 also be marked as read.
7981 If QUIETLY is non-nil, no questions will be asked.
7982 If TO-HERE is non-nil, it should be a point in the buffer.  All
7983 articles before this point will be marked as read.
7984 Note that this function will only catch up the unread article
7985 in the current summary buffer limitation.
7986 The number of articles marked as read is returned."
7987   (interactive "P")
7988   (prog1
7989       (save-excursion
7990         (when (or quietly
7991                   (not gnus-interactive-catchup) ;Without confirmation?
7992                   gnus-expert-user
7993                   (gnus-y-or-n-p
7994                    (if all
7995                        "Mark absolutely all articles as read? "
7996                      "Mark all unread articles as read? ")))
7997           (if (and not-mark
7998                    (not gnus-newsgroup-adaptive)
7999                    (not gnus-newsgroup-auto-expire)
8000                    (not gnus-suppress-duplicates)
8001                    (or (not gnus-use-cache)
8002                        (eq gnus-use-cache 'passive)))
8003               (progn
8004                 (when all
8005                   (setq gnus-newsgroup-marked nil
8006                         gnus-newsgroup-dormant nil))
8007                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8008             ;; We actually mark all articles as canceled, which we
8009             ;; have to do when using auto-expiry or adaptive scoring.
8010             (gnus-summary-show-all-threads)
8011             (when (gnus-summary-first-subject (not all) t)
8012               (while (and
8013                       (if to-here (< (point) to-here) t)
8014                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8015                       (gnus-summary-find-next (not all) nil nil t))))
8016             (gnus-set-mode-line 'summary))
8017           t))
8018     (gnus-summary-position-point)))
8019
8020 (defun gnus-summary-catchup-to-here (&optional all)
8021   "Mark all unticked articles before the current one as read.
8022 If ALL is non-nil, also mark ticked and dormant articles as read."
8023   (interactive "P")
8024   (save-excursion
8025     (gnus-save-hidden-threads
8026       (let ((beg (point)))
8027         ;; We check that there are unread articles.
8028         (when (or all (gnus-summary-find-prev))
8029           (gnus-summary-catchup all t beg)))))
8030   (gnus-summary-position-point))
8031
8032 (defun gnus-summary-catchup-all (&optional quietly)
8033   "Mark all articles in this newsgroup as read."
8034   (interactive "P")
8035   (gnus-summary-catchup t quietly))
8036
8037 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8038   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8039 If prefix argument ALL is non-nil, all articles are marked as read."
8040   (interactive "P")
8041   (when (gnus-summary-catchup all quietly nil 'fast)
8042     ;; Select next newsgroup or exit.
8043     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8044              (eq gnus-auto-select-next 'quietly))
8045         (gnus-summary-next-group nil)
8046       (gnus-summary-exit))))
8047
8048 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8049   "Mark all articles in this newsgroup as read, and then exit."
8050   (interactive "P")
8051   (gnus-summary-catchup-and-exit t quietly))
8052
8053 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8054 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8055   "Mark all articles in this group as read and select the next group.
8056 If given a prefix, mark all articles, unread as well as ticked, as
8057 read."
8058   (interactive "P")
8059   (save-excursion
8060     (gnus-summary-catchup all))
8061   (gnus-summary-next-article t nil nil t))
8062
8063 ;; Thread-based commands.
8064
8065 (defun gnus-summary-articles-in-thread (&optional article)
8066   "Return a list of all articles in the current thread.
8067 If ARTICLE is non-nil, return all articles in the thread that starts
8068 with that article."
8069   (let* ((article (or article (gnus-summary-article-number)))
8070          (data (gnus-data-find-list article))
8071          (top-level (gnus-data-level (car data)))
8072          (top-subject
8073           (cond ((null gnus-thread-operation-ignore-subject)
8074                  (gnus-simplify-subject-re
8075                   (mail-header-subject (gnus-data-header (car data)))))
8076                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8077                  (gnus-simplify-subject-fuzzy
8078                   (mail-header-subject (gnus-data-header (car data)))))
8079                 (t nil)))
8080          (end-point (save-excursion
8081                       (if (gnus-summary-go-to-next-thread)
8082                           (point) (point-max))))
8083          articles)
8084     (while (and data
8085                 (< (gnus-data-pos (car data)) end-point))
8086       (when (or (not top-subject)
8087                 (string= top-subject
8088                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8089                              (gnus-simplify-subject-fuzzy
8090                               (mail-header-subject
8091                                (gnus-data-header (car data))))
8092                            (gnus-simplify-subject-re
8093                             (mail-header-subject
8094                              (gnus-data-header (car data)))))))
8095         (push (gnus-data-number (car data)) articles))
8096       (unless (and (setq data (cdr data))
8097                    (> (gnus-data-level (car data)) top-level))
8098         (setq data nil)))
8099     ;; Return the list of articles.
8100     (nreverse articles)))
8101
8102 (defun gnus-summary-rethread-current ()
8103   "Rethread the thread the current article is part of."
8104   (interactive)
8105   (let* ((gnus-show-threads t)
8106          (article (gnus-summary-article-number))
8107          (id (mail-header-id (gnus-summary-article-header)))
8108          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8109     (unless id
8110       (error "No article on the current line"))
8111     (gnus-rebuild-thread id)
8112     (gnus-summary-goto-subject article)))
8113
8114 (defun gnus-summary-reparent-thread ()
8115   "Make the current article child of the marked (or previous) article.
8116
8117 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8118 is non-nil or the Subject: of both articles are the same."
8119   (interactive)
8120   (unless (not (gnus-group-read-only-p))
8121     (error "The current newsgroup does not support article editing"))
8122   (unless (<= (length gnus-newsgroup-processable) 1)
8123     (error "No more than one article may be marked"))
8124   (save-window-excursion
8125     (let ((gnus-article-buffer " *reparent*")
8126           (current-article (gnus-summary-article-number))
8127           ;; First grab the marked article, otherwise one line up.
8128           (parent-article (if (not (null gnus-newsgroup-processable))
8129                               (car gnus-newsgroup-processable)
8130                             (save-excursion
8131                               (if (eq (forward-line -1) 0)
8132                                   (gnus-summary-article-number)
8133                                 (error "Beginning of summary buffer"))))))
8134       (unless (not (eq current-article parent-article))
8135         (error "An article may not be self-referential"))
8136       (let ((message-id (mail-header-id
8137                          (gnus-summary-article-header parent-article))))
8138         (unless (and message-id (not (equal message-id "")))
8139           (error "No message-id in desired parent"))
8140         ;; We don't want the article to be marked as read.
8141         (let (gnus-mark-article-hook)
8142           (gnus-summary-select-article t t nil current-article))
8143         (set-buffer gnus-original-article-buffer)
8144         (let ((buf (format "%s" (buffer-string))))
8145           (with-temp-buffer
8146             (insert buf)
8147             (goto-char (point-min))
8148             (if (re-search-forward "^References: " nil t)
8149                 (progn
8150                   (re-search-forward "^[^ \t]" nil t)
8151                   (forward-line -1)
8152                   (end-of-line)
8153                   (insert " " message-id))
8154               (insert "References: " message-id "\n"))
8155             (unless (gnus-request-replace-article
8156                      current-article (car gnus-article-current)
8157                      (current-buffer))
8158               (error "Couldn't replace article"))))
8159         (set-buffer gnus-summary-buffer)
8160         (gnus-summary-unmark-all-processable)
8161         (gnus-summary-update-article current-article)
8162         (gnus-summary-rethread-current)
8163         (gnus-message 3 "Article %d is now the child of article %d"
8164                       current-article parent-article)))))
8165
8166 (defun gnus-summary-toggle-threads (&optional arg)
8167   "Toggle showing conversation threads.
8168 If ARG is positive number, turn showing conversation threads on."
8169   (interactive "P")
8170   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8171     (setq gnus-show-threads
8172           (if (null arg) (not gnus-show-threads)
8173             (> (prefix-numeric-value arg) 0)))
8174     (gnus-summary-prepare)
8175     (gnus-summary-goto-subject current)
8176     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8177     (gnus-summary-position-point)))
8178
8179 (defun gnus-summary-show-all-threads ()
8180   "Show all threads."
8181   (interactive)
8182   (save-excursion
8183     (let ((buffer-read-only nil))
8184       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8185   (gnus-summary-position-point))
8186
8187 (defun gnus-summary-show-thread ()
8188   "Show thread subtrees.
8189 Returns nil if no thread was there to be shown."
8190   (interactive)
8191   (let ((buffer-read-only nil)
8192         (orig (point))
8193         ;; first goto end then to beg, to have point at beg after let
8194         (end (progn (end-of-line) (point)))
8195         (beg (progn (beginning-of-line) (point))))
8196     (prog1
8197         ;; Any hidden lines here?
8198         (search-forward "\r" end t)
8199       (subst-char-in-region beg end ?\^M ?\n t)
8200       (goto-char orig)
8201       (gnus-summary-position-point))))
8202
8203 (defun gnus-summary-hide-all-threads ()
8204   "Hide all thread subtrees."
8205   (interactive)
8206   (save-excursion
8207     (goto-char (point-min))
8208     (gnus-summary-hide-thread)
8209     (while (zerop (gnus-summary-next-thread 1 t))
8210       (gnus-summary-hide-thread)))
8211   (gnus-summary-position-point))
8212
8213 (defun gnus-summary-hide-thread ()
8214   "Hide thread subtrees.
8215 Returns nil if no threads were there to be hidden."
8216   (interactive)
8217   (let ((buffer-read-only nil)
8218         (start (point))
8219         (article (gnus-summary-article-number)))
8220     (goto-char start)
8221     ;; Go forward until either the buffer ends or the subthread
8222     ;; ends.
8223     (when (and (not (eobp))
8224                (or (zerop (gnus-summary-next-thread 1 t))
8225                    (goto-char (point-max))))
8226       (prog1
8227           (if (and (> (point) start)
8228                    (search-backward "\n" start t))
8229               (progn
8230                 (subst-char-in-region start (point) ?\n ?\^M)
8231                 (gnus-summary-goto-subject article))
8232             (goto-char start)
8233             nil)
8234         ;;(gnus-summary-position-point)
8235         ))))
8236
8237 (defun gnus-summary-go-to-next-thread (&optional previous)
8238   "Go to the same level (or less) next thread.
8239 If PREVIOUS is non-nil, go to previous thread instead.
8240 Return the article number moved to, or nil if moving was impossible."
8241   (let ((level (gnus-summary-thread-level))
8242         (way (if previous -1 1))
8243         (beg (point)))
8244     (forward-line way)
8245     (while (and (not (eobp))
8246                 (< level (gnus-summary-thread-level)))
8247       (forward-line way))
8248     (if (eobp)
8249         (progn
8250           (goto-char beg)
8251           nil)
8252       (setq beg (point))
8253       (prog1
8254           (gnus-summary-article-number)
8255         (goto-char beg)))))
8256
8257 (defun gnus-summary-next-thread (n &optional silent)
8258   "Go to the same level next N'th thread.
8259 If N is negative, search backward instead.
8260 Returns the difference between N and the number of skips actually
8261 done.
8262
8263 If SILENT, don't output messages."
8264   (interactive "p")
8265   (let ((backward (< n 0))
8266         (n (abs n)))
8267     (while (and (> n 0)
8268                 (gnus-summary-go-to-next-thread backward))
8269       (decf n))
8270     (unless silent
8271       (gnus-summary-position-point))
8272     (when (and (not silent) (/= 0 n))
8273       (gnus-message 7 "No more threads"))
8274     n))
8275
8276 (defun gnus-summary-prev-thread (n)
8277   "Go to the same level previous N'th thread.
8278 Returns the difference between N and the number of skips actually
8279 done."
8280   (interactive "p")
8281   (gnus-summary-next-thread (- n)))
8282
8283 (defun gnus-summary-go-down-thread ()
8284   "Go down one level in the current thread."
8285   (let ((children (gnus-summary-article-children)))
8286     (when children
8287       (gnus-summary-goto-subject (car children)))))
8288
8289 (defun gnus-summary-go-up-thread ()
8290   "Go up one level in the current thread."
8291   (let ((parent (gnus-summary-article-parent)))
8292     (when parent
8293       (gnus-summary-goto-subject parent))))
8294
8295 (defun gnus-summary-down-thread (n)
8296   "Go down thread N steps.
8297 If N is negative, go up instead.
8298 Returns the difference between N and how many steps down that were
8299 taken."
8300   (interactive "p")
8301   (let ((up (< n 0))
8302         (n (abs n)))
8303     (while (and (> n 0)
8304                 (if up (gnus-summary-go-up-thread)
8305                   (gnus-summary-go-down-thread)))
8306       (setq n (1- n)))
8307     (gnus-summary-position-point)
8308     (when (/= 0 n)
8309       (gnus-message 7 "Can't go further"))
8310     n))
8311
8312 (defun gnus-summary-up-thread (n)
8313   "Go up thread N steps.
8314 If N is negative, go up instead.
8315 Returns the difference between N and how many steps down that were
8316 taken."
8317   (interactive "p")
8318   (gnus-summary-down-thread (- n)))
8319
8320 (defun gnus-summary-top-thread ()
8321   "Go to the top of the thread."
8322   (interactive)
8323   (while (gnus-summary-go-up-thread))
8324   (gnus-summary-article-number))
8325
8326 (defun gnus-summary-kill-thread (&optional unmark)
8327   "Mark articles under current thread as read.
8328 If the prefix argument is positive, remove any kinds of marks.
8329 If the prefix argument is negative, tick articles instead."
8330   (interactive "P")
8331   (when unmark
8332     (setq unmark (prefix-numeric-value unmark)))
8333   (let ((articles (gnus-summary-articles-in-thread)))
8334     (save-excursion
8335       ;; Expand the thread.
8336       (gnus-summary-show-thread)
8337       ;; Mark all the articles.
8338       (while articles
8339         (gnus-summary-goto-subject (car articles))
8340         (cond ((null unmark)
8341                (gnus-summary-mark-article-as-read gnus-killed-mark))
8342               ((> unmark 0)
8343                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8344               (t
8345                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8346         (setq articles (cdr articles))))
8347     ;; Hide killed subtrees.
8348     (and (null unmark)
8349          gnus-thread-hide-killed
8350          (gnus-summary-hide-thread))
8351     ;; If marked as read, go to next unread subject.
8352     (when (null unmark)
8353       ;; Go to next unread subject.
8354       (gnus-summary-next-subject 1 t)))
8355   (gnus-set-mode-line 'summary))
8356
8357 ;; Summary sorting commands
8358
8359 (defun gnus-summary-sort-by-number (&optional reverse)
8360   "Sort the summary buffer by article number.
8361 Argument REVERSE means reverse order."
8362   (interactive "P")
8363   (gnus-summary-sort 'number reverse))
8364
8365 (defun gnus-summary-sort-by-author (&optional reverse)
8366   "Sort the summary buffer by author name alphabetically.
8367 If case-fold-search is non-nil, case of letters is ignored.
8368 Argument REVERSE means reverse order."
8369   (interactive "P")
8370   (gnus-summary-sort 'author reverse))
8371
8372 (defun gnus-summary-sort-by-subject (&optional reverse)
8373   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8374 If case-fold-search is non-nil, case of letters is ignored.
8375 Argument REVERSE means reverse order."
8376   (interactive "P")
8377   (gnus-summary-sort 'subject reverse))
8378
8379 (defun gnus-summary-sort-by-date (&optional reverse)
8380   "Sort the summary buffer by date.
8381 Argument REVERSE means reverse order."
8382   (interactive "P")
8383   (gnus-summary-sort 'date reverse))
8384
8385 (defun gnus-summary-sort-by-score (&optional reverse)
8386   "Sort the summary buffer by score.
8387 Argument REVERSE means reverse order."
8388   (interactive "P")
8389   (gnus-summary-sort 'score reverse))
8390
8391 (defun gnus-summary-sort-by-lines (&optional reverse)
8392   "Sort the summary buffer by article length.
8393 Argument REVERSE means reverse order."
8394   (interactive "P")
8395   (gnus-summary-sort 'lines reverse))
8396
8397 (defun gnus-summary-sort (predicate reverse)
8398   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8399   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8400          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8401          (gnus-thread-sort-functions
8402           (list
8403            (if (not reverse)
8404                thread
8405              `(lambda (t1 t2)
8406                 (,thread t2 t1)))))
8407          (gnus-article-sort-functions
8408           (list
8409            (if (not reverse)
8410                article
8411              `(lambda (t1 t2)
8412                 (,article t2 t1)))))
8413          (buffer-read-only)
8414          (gnus-summary-prepare-hook nil))
8415     ;; We do the sorting by regenerating the threads.
8416     (gnus-summary-prepare)
8417     ;; Hide subthreads if needed.
8418     (when (and gnus-show-threads gnus-thread-hide-subtree)
8419       (gnus-summary-hide-all-threads))))
8420
8421 ;; Summary saving commands.
8422
8423 (defun gnus-summary-save-article (&optional n not-saved)
8424   "Save the current article using the default saver function.
8425 If N is a positive number, save the N next articles.
8426 If N is a negative number, save the N previous articles.
8427 If N is nil and any articles have been marked with the process mark,
8428 save those articles instead.
8429 The variable `gnus-default-article-saver' specifies the saver function."
8430   (interactive "P")
8431   (let* ((articles (gnus-summary-work-articles n))
8432          (save-buffer (save-excursion
8433                         (nnheader-set-temp-buffer " *Gnus Save*")))
8434          (num (length articles))
8435          header article file)
8436     (while articles
8437       (setq header (gnus-summary-article-header
8438                     (setq article (pop articles))))
8439       (if (not (vectorp header))
8440           ;; This is a pseudo-article.
8441           (if (assq 'name header)
8442               (gnus-copy-file (cdr (assq 'name header)))
8443             (gnus-message 1 "Article %d is unsaveable" article))
8444         ;; This is a real article.
8445         (save-window-excursion
8446           (gnus-summary-select-article t nil nil article))
8447         (save-excursion
8448           (set-buffer save-buffer)
8449           (erase-buffer)
8450           (insert-buffer-substring gnus-original-article-buffer))
8451         (setq file (gnus-article-save save-buffer file num))
8452         (gnus-summary-remove-process-mark article)
8453         (unless not-saved
8454           (gnus-summary-set-saved-mark article))))
8455     (gnus-kill-buffer save-buffer)
8456     (gnus-summary-position-point)
8457     (gnus-set-mode-line 'summary)
8458     n))
8459
8460 (defun gnus-summary-pipe-output (&optional arg)
8461   "Pipe the current article to a subprocess.
8462 If N is a positive number, pipe the N next articles.
8463 If N is a negative number, pipe the N previous articles.
8464 If N is nil and any articles have been marked with the process mark,
8465 pipe those articles instead."
8466   (interactive "P")
8467   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8468     (gnus-summary-save-article arg t))
8469   (gnus-configure-windows 'pipe))
8470
8471 (defun gnus-summary-save-article-mail (&optional arg)
8472   "Append the current article to an mail file.
8473 If N is a positive number, save the N next articles.
8474 If N is a negative number, save the N previous articles.
8475 If N is nil and any articles have been marked with the process mark,
8476 save those articles instead."
8477   (interactive "P")
8478   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8479     (gnus-summary-save-article arg)))
8480
8481 (defun gnus-summary-save-article-rmail (&optional arg)
8482   "Append the current article to an rmail file.
8483 If N is a positive number, save the N next articles.
8484 If N is a negative number, save the N previous articles.
8485 If N is nil and any articles have been marked with the process mark,
8486 save those articles instead."
8487   (interactive "P")
8488   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8489     (gnus-summary-save-article arg)))
8490
8491 (defun gnus-summary-save-article-file (&optional arg)
8492   "Append the current article to a file.
8493 If N is a positive number, save the N next articles.
8494 If N is a negative number, save the N previous articles.
8495 If N is nil and any articles have been marked with the process mark,
8496 save those articles instead."
8497   (interactive "P")
8498   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8499     (gnus-summary-save-article arg)))
8500
8501 (defun gnus-summary-write-article-file (&optional arg)
8502   "Write the current article to a file, deleting the previous file.
8503 If N is a positive number, save the N next articles.
8504 If N is a negative number, save the N previous articles.
8505 If N is nil and any articles have been marked with the process mark,
8506 save those articles instead."
8507   (interactive "P")
8508   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8509     (gnus-summary-save-article arg)))
8510
8511 (defun gnus-summary-save-article-body-file (&optional arg)
8512   "Append the current article body to a file.
8513 If N is a positive number, save the N next articles.
8514 If N is a negative number, save the N previous articles.
8515 If N is nil and any articles have been marked with the process mark,
8516 save those articles instead."
8517   (interactive "P")
8518   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8519     (gnus-summary-save-article arg)))
8520
8521 (defun gnus-summary-pipe-message (program)
8522   "Pipe the current article through PROGRAM."
8523   (interactive "sProgram: ")
8524   (gnus-summary-select-article)
8525   (let ((mail-header-separator ""))
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