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