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