*** 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 (re-search-forward
4349                             "^message-id: *\\(<[^\n\t> ]>\\)" nil t)
4350                            ;; We do it this way to make sure the Message-ID
4351                            ;; is (somewhat) syntactically valid.
4352                            (buffer-substring (match-beginning 0)
4353                                              (match-end 0))
4354                          ;; If there was no message-id, we just fake one
4355                          ;; to make subsequent routines simpler.
4356                          (nnheader-generate-fake-message-id))))
4357             ;; References.
4358             (progn
4359               (goto-char p)
4360               (if (search-forward "\nreferences: " nil t)
4361                   (progn
4362                     (setq end (point))
4363                     (prog1
4364                         (nnheader-header-value)
4365                       (setq ref
4366                             (buffer-substring
4367                              (progn
4368                                (end-of-line)
4369                                (search-backward ">" end t)
4370                                (1+ (point)))
4371                              (progn
4372                                (search-backward "<" end t)
4373                                (point))))))
4374                 ;; Get the references from the in-reply-to header if there
4375                 ;; were no references and the in-reply-to header looks
4376                 ;; promising.
4377                 (if (and (search-forward "\nin-reply-to: " nil t)
4378                          (setq in-reply-to (nnheader-header-value))
4379                          (string-match "<[^>]+>" in-reply-to))
4380                     (let (ref2)
4381                       (setq ref (substring in-reply-to (match-beginning 0)
4382                                            (match-end 0)))
4383                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4384                         (setq ref2 (substring in-reply-to (match-beginning 0)
4385                                               (match-end 0)))
4386                         (when (> (length ref2) (length ref))
4387                           (setq ref ref2))))
4388                   (setq ref nil))))
4389             ;; Chars.
4390             0
4391             ;; Lines.
4392             (progn
4393               (goto-char p)
4394               (if (search-forward "\nlines: " nil t)
4395                   (if (numberp (setq lines (ignore-errors (read cur))))
4396                       lines 0)
4397                 0))
4398             ;; Xref.
4399             (progn
4400               (goto-char p)
4401               (and (search-forward "\nxref: " nil t)
4402                    (nnheader-header-value)))))
4403           (when (equal id ref)
4404             (setq ref nil))
4405
4406           (when gnus-alter-header-function
4407             (funcall gnus-alter-header-function header)
4408             (setq id (mail-header-id header)
4409                   ref (gnus-parent-id (mail-header-references header))))
4410     
4411           ;; We do the threading while we read the headers.  The
4412           ;; message-id and the last reference are both entered into
4413           ;; the same hash table.  Some tippy-toeing around has to be
4414           ;; done in case an article has arrived before the article
4415           ;; which it refers to.
4416           (if (boundp (setq id-dep (intern id dependencies)))
4417               (if (and (car (symbol-value id-dep))
4418                        (not force-new))
4419                   ;; An article with this Message-ID has already been seen.
4420                   (if gnus-summary-ignore-duplicates
4421                       ;; We ignore this one, except we add
4422                       ;; any additional Xrefs (in case the two articles
4423                       ;; came from different servers).
4424                       (progn
4425                         (mail-header-set-xref
4426                          (car (symbol-value id-dep))
4427                          (concat (or (mail-header-xref
4428                                       (car (symbol-value id-dep)))
4429                                      "")
4430                                  (or (mail-header-xref header) "")))
4431                         (setq header nil))
4432                     ;; We rename the Message-ID.
4433                     (set
4434                      (setq id-dep (intern (setq id (nnmail-message-id))
4435                                           dependencies))
4436                      (list header))
4437                     (mail-header-set-id header id))
4438                 (setcar (symbol-value id-dep) header))
4439             (set id-dep (list header)))
4440           (when header
4441             (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4442                 (setcdr (symbol-value ref-dep)
4443                         (nconc (cdr (symbol-value ref-dep))
4444                                (list (symbol-value id-dep))))
4445               (set ref-dep (list nil (symbol-value id-dep))))
4446             (push header headers))
4447           (goto-char (point-max))
4448           (widen))
4449         (nreverse headers)))))
4450
4451 ;; The following macros and functions were written by Felix Lee
4452 ;; <flee@cse.psu.edu>.
4453
4454 (defmacro gnus-nov-read-integer ()
4455   '(prog1
4456        (if (= (following-char) ?\t)
4457            0
4458          (let ((num (ignore-errors (read buffer))))
4459            (if (numberp num) num 0)))
4460      (unless (eobp)
4461        (search-forward "\t" eol 'move))))
4462
4463 (defmacro gnus-nov-skip-field ()
4464   '(search-forward "\t" eol 'move))
4465
4466 (defmacro gnus-nov-field ()
4467   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
4468
4469 ;; (defvar gnus-nov-none-counter 0)
4470
4471 ;; This function has to be called with point after the article number
4472 ;; on the beginning of the line.
4473 (defun gnus-nov-parse-line (number dependencies &optional force-new)
4474   (let ((eol (gnus-point-at-eol))
4475         (buffer (current-buffer))
4476         header ref id id-dep ref-dep)
4477
4478     ;; overview: [num subject from date id refs chars lines misc]
4479     (unwind-protect
4480         (progn
4481           (narrow-to-region (point) eol)
4482           (unless (eobp)
4483             (forward-char))
4484
4485           (setq header
4486                 (vector
4487                  number                 ; number
4488                  (funcall
4489                   gnus-unstructured-field-decoder (gnus-nov-field)) ; subject
4490                  (funcall
4491                   gnus-structured-field-decoder (gnus-nov-field)) ; from
4492                  (gnus-nov-field)       ; date
4493                  (setq id (or (gnus-nov-field)
4494                               (nnheader-generate-fake-message-id))) ; id
4495                  (progn
4496                    (let ((beg (point)))
4497                      (search-forward "\t" eol)
4498                      (if (search-backward ">" beg t)
4499                          (setq ref
4500                                (buffer-substring
4501                                 (1+ (point))
4502                                 (or (search-backward "<" beg t) beg)))
4503                        (setq ref nil))
4504                      (goto-char beg))
4505                    (gnus-nov-field))    ; refs
4506                  (gnus-nov-read-integer) ; chars
4507                  (gnus-nov-read-integer) ; lines
4508                  (if (= (following-char) ?\n)
4509                      nil
4510                    (gnus-nov-field))))) ; misc
4511
4512       (widen))
4513
4514     (when gnus-alter-header-function
4515       (funcall gnus-alter-header-function header)
4516       (setq id (mail-header-id header)
4517             ref (gnus-parent-id (mail-header-references header))))
4518     
4519     ;; We build the thread tree.
4520     (when (equal id ref)
4521       ;; This article refers back to itself.  Naughty, naughty.
4522       (setq ref nil))
4523     (if (boundp (setq id-dep (intern id dependencies)))
4524         (if (and (car (symbol-value id-dep))
4525                  (not force-new))
4526             ;; An article with this Message-ID has already been seen.
4527             (if gnus-summary-ignore-duplicates
4528                 ;; We ignore this one, except we add any additional
4529                 ;; Xrefs (in case the two articles came from different
4530                 ;; servers.
4531                 (progn
4532                   (mail-header-set-xref
4533                    (car (symbol-value id-dep))
4534                    (concat (or (mail-header-xref
4535                                 (car (symbol-value id-dep)))
4536                                "")
4537                            (or (mail-header-xref header) "")))
4538                   (setq header nil))
4539               ;; We rename the Message-ID.
4540               (set
4541                (setq id-dep (intern (setq id (nnmail-message-id))
4542                                     dependencies))
4543                (list header))
4544               (mail-header-set-id header id))
4545           (setcar (symbol-value id-dep) header))
4546       (set id-dep (list header)))
4547     (when header
4548       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4549           (setcdr (symbol-value ref-dep)
4550                   (nconc (cdr (symbol-value ref-dep))
4551                          (list (symbol-value id-dep))))
4552         (set ref-dep (list nil (symbol-value id-dep)))))
4553     header))
4554
4555 ;; Goes through the xover lines and returns a list of vectors
4556 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4557                                                   force-new dependencies
4558                                                   group also-fetch-heads)
4559   "Parse the news overview data in the server buffer, and return a
4560 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4561   ;; Get the Xref when the users reads the articles since most/some
4562   ;; NNTP servers do not include Xrefs when using XOVER.
4563   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4564   (let ((cur nntp-server-buffer)
4565         (dependencies (or dependencies gnus-newsgroup-dependencies))
4566         number headers header)
4567     (save-excursion
4568       (set-buffer nntp-server-buffer)
4569       ;; Allow the user to mangle the headers before parsing them.
4570       (gnus-run-hooks 'gnus-parse-headers-hook)
4571       (goto-char (point-min))
4572       (while (not (eobp))
4573         (condition-case ()
4574             (while (and sequence (not (eobp)))
4575               (setq number (read cur))
4576               (while (and sequence
4577                           (< (car sequence) number))
4578                 (setq sequence (cdr sequence)))
4579               (and sequence
4580                    (eq number (car sequence))
4581                    (progn
4582                      (setq sequence (cdr sequence))
4583                      (setq header (inline
4584                                     (gnus-nov-parse-line
4585                                      number dependencies force-new))))
4586                    (push header headers))
4587               (forward-line 1))
4588           (error
4589            (gnus-error 4 "Strange nov line (%d)"
4590                        (count-lines (point-min) (point)))))
4591         (forward-line 1))
4592       ;; A common bug in inn is that if you have posted an article and
4593       ;; then retrieves the active file, it will answer correctly --
4594       ;; the new article is included.  However, a NOV entry for the
4595       ;; article may not have been generated yet, so this may fail.
4596       ;; We work around this problem by retrieving the last few
4597       ;; headers using HEAD.
4598       (if (or (not also-fetch-heads)
4599               (not sequence))
4600           ;; We (probably) got all the headers.
4601           (nreverse headers)
4602         (let ((gnus-nov-is-evil t))
4603           (nconc
4604            (nreverse headers)
4605            (when (gnus-retrieve-headers sequence group)
4606              (gnus-get-newsgroup-headers))))))))
4607
4608 (defun gnus-article-get-xrefs ()
4609   "Fill in the Xref value in `gnus-current-headers', if necessary.
4610 This is meant to be called in `gnus-article-internal-prepare-hook'."
4611   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4612                                  gnus-current-headers)))
4613     (or (not gnus-use-cross-reference)
4614         (not headers)
4615         (and (mail-header-xref headers)
4616              (not (string= (mail-header-xref headers) "")))
4617         (let ((case-fold-search t)
4618               xref)
4619           (save-restriction
4620             (nnheader-narrow-to-headers)
4621             (goto-char (point-min))
4622             (when (or (and (eq (downcase (following-char)) ?x)
4623                            (looking-at "Xref:"))
4624                       (search-forward "\nXref:" nil t))
4625               (goto-char (1+ (match-end 0)))
4626               (setq xref (buffer-substring (point)
4627                                            (progn (end-of-line) (point))))
4628               (mail-header-set-xref headers xref)))))))
4629
4630 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4631   "Find article ID and insert the summary line for that article."
4632   (let ((header (cond ((and old-header use-old-header)
4633                        old-header)
4634                       ((and (numberp id)
4635                             (gnus-number-to-header id))
4636                        (gnus-number-to-header id))
4637                       (t
4638                        (gnus-read-header id))))
4639         (number (and (numberp id) id))
4640         pos d)
4641     (when header
4642       ;; Rebuild the thread that this article is part of and go to the
4643       ;; article we have fetched.
4644       (when (and (not gnus-show-threads)
4645                  old-header)
4646         (when (setq d (gnus-data-find (mail-header-number old-header)))
4647           (goto-char (gnus-data-pos d))
4648           (gnus-data-remove
4649            number
4650            (- (gnus-point-at-bol)
4651               (prog1
4652                   (1+ (gnus-point-at-eol))
4653                 (gnus-delete-line))))))
4654       (when old-header
4655         (mail-header-set-number header (mail-header-number old-header)))
4656       (setq gnus-newsgroup-sparse
4657             (delq (setq number (mail-header-number header))
4658                   gnus-newsgroup-sparse))
4659       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4660       (push number gnus-newsgroup-limit)
4661       (gnus-rebuild-thread (mail-header-id header))
4662       (gnus-summary-goto-subject number nil t))
4663     (when (and (numberp number)
4664                (> number 0))
4665       ;; We have to update the boundaries even if we can't fetch the
4666       ;; article if ID is a number -- so that the next `P' or `N'
4667       ;; command will fetch the previous (or next) article even
4668       ;; if the one we tried to fetch this time has been canceled.
4669       (when (> number gnus-newsgroup-end)
4670         (setq gnus-newsgroup-end number))
4671       (when (< number gnus-newsgroup-begin)
4672         (setq gnus-newsgroup-begin number))
4673       (setq gnus-newsgroup-unselected
4674             (delq number gnus-newsgroup-unselected)))
4675     ;; Report back a success?
4676     (and header (mail-header-number header))))
4677
4678 ;;; Process/prefix in the summary buffer
4679
4680 (defun gnus-summary-work-articles (n)
4681   "Return a list of articles to be worked upon.  The prefix argument,
4682 the list of process marked articles, and the current article will be
4683 taken into consideration."
4684   (save-excursion
4685     (set-buffer gnus-summary-buffer)
4686     (cond
4687      (n
4688       ;; A numerical prefix has been given.
4689       (setq n (prefix-numeric-value n))
4690       (let ((backward (< n 0))
4691             (n (abs (prefix-numeric-value n)))
4692             articles article)
4693         (save-excursion
4694           (while
4695               (and (> n 0)
4696                    (push (setq article (gnus-summary-article-number))
4697                          articles)
4698                    (if backward
4699                        (gnus-summary-find-prev nil article)
4700                      (gnus-summary-find-next nil article)))
4701             (decf n)))
4702         (nreverse articles)))
4703      ((and (gnus-region-active-p) (mark))
4704       (message "region active")
4705       ;; Work on the region between point and mark.
4706       (let ((max (max (point) (mark)))
4707             articles article)
4708         (save-excursion
4709           (goto-char (min (min (point) (mark))))
4710           (while
4711               (and
4712                (push (setq article (gnus-summary-article-number)) articles)
4713                (gnus-summary-find-next nil article)
4714                (< (point) max)))
4715           (nreverse articles))))
4716      (gnus-newsgroup-processable
4717       ;; There are process-marked articles present.
4718       ;; Save current state.
4719       (gnus-summary-save-process-mark)
4720       ;; Return the list.
4721       (reverse gnus-newsgroup-processable))
4722      (t
4723       ;; Just return the current article.
4724       (list (gnus-summary-article-number))))))
4725
4726 (defun gnus-summary-save-process-mark ()
4727   "Push the current set of process marked articles on the stack."
4728   (interactive)
4729   (push (copy-sequence gnus-newsgroup-processable)
4730         gnus-newsgroup-process-stack))
4731
4732 (defun gnus-summary-kill-process-mark ()
4733   "Push the current set of process marked articles on the stack and unmark."
4734   (interactive)
4735   (gnus-summary-save-process-mark)
4736   (gnus-summary-unmark-all-processable))
4737
4738 (defun gnus-summary-yank-process-mark ()
4739   "Pop the last process mark state off the stack and restore it."
4740   (interactive)
4741   (unless gnus-newsgroup-process-stack
4742     (error "Empty mark stack"))
4743   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4744
4745 (defun gnus-summary-process-mark-set (set)
4746   "Make SET into the current process marked articles."
4747   (gnus-summary-unmark-all-processable)
4748   (while set
4749     (gnus-summary-set-process-mark (pop set))))
4750
4751 ;;; Searching and stuff
4752
4753 (defun gnus-summary-search-group (&optional backward use-level)
4754   "Search for next unread newsgroup.
4755 If optional argument BACKWARD is non-nil, search backward instead."
4756   (save-excursion
4757     (set-buffer gnus-group-buffer)
4758     (when (gnus-group-search-forward
4759            backward nil (if use-level (gnus-group-group-level) nil))
4760       (gnus-group-group-name))))
4761
4762 (defun gnus-summary-best-group (&optional exclude-group)
4763   "Find the name of the best unread group.
4764 If EXCLUDE-GROUP, do not go to this group."
4765   (save-excursion
4766     (set-buffer gnus-group-buffer)
4767     (save-excursion
4768       (gnus-group-best-unread-group exclude-group))))
4769
4770 (defun gnus-summary-find-next (&optional unread article backward)
4771   (if backward (gnus-summary-find-prev)
4772     (let* ((dummy (gnus-summary-article-intangible-p))
4773            (article (or article (gnus-summary-article-number)))
4774            (arts (gnus-data-find-list article))
4775            result)
4776       (when (and (not dummy)
4777                  (or (not gnus-summary-check-current)
4778                      (not unread)
4779                      (not (gnus-data-unread-p (car arts)))))
4780         (setq arts (cdr arts)))
4781       (when (setq result
4782                   (if unread
4783                       (progn
4784                         (while arts
4785                           (when (gnus-data-unread-p (car arts))
4786                             (setq result (car arts)
4787                                   arts nil))
4788                           (setq arts (cdr arts)))
4789                         result)
4790                     (car arts)))
4791         (goto-char (gnus-data-pos result))
4792         (gnus-data-number result)))))
4793
4794 (defun gnus-summary-find-prev (&optional unread article)
4795   (let* ((eobp (eobp))
4796          (article (or article (gnus-summary-article-number)))
4797          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4798          result)
4799     (when (and (not eobp)
4800                (or (not gnus-summary-check-current)
4801                    (not unread)
4802                    (not (gnus-data-unread-p (car arts)))))
4803       (setq arts (cdr arts)))
4804     (when (setq result
4805                 (if unread
4806                     (progn
4807                       (while arts
4808                         (when (gnus-data-unread-p (car arts))
4809                           (setq result (car arts)
4810                                 arts nil))
4811                         (setq arts (cdr arts)))
4812                       result)
4813                   (car arts)))
4814       (goto-char (gnus-data-pos result))
4815       (gnus-data-number result))))
4816
4817 (defun gnus-summary-find-subject (subject &optional unread backward article)
4818   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4819          (article (or article (gnus-summary-article-number)))
4820          (articles (gnus-data-list backward))
4821          (arts (gnus-data-find-list article articles))
4822          result)
4823     (when (or (not gnus-summary-check-current)
4824               (not unread)
4825               (not (gnus-data-unread-p (car arts))))
4826       (setq arts (cdr arts)))
4827     (while arts
4828       (and (or (not unread)
4829                (gnus-data-unread-p (car arts)))
4830            (vectorp (gnus-data-header (car arts)))
4831            (gnus-subject-equal
4832             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4833            (setq result (car arts)
4834                  arts nil))
4835       (setq arts (cdr arts)))
4836     (and result
4837          (goto-char (gnus-data-pos result))
4838          (gnus-data-number result))))
4839
4840 (defun gnus-summary-search-forward (&optional unread subject backward)
4841   "Search forward for an article.
4842 If UNREAD, look for unread articles.  If SUBJECT, look for
4843 articles with that subject.  If BACKWARD, search backward instead."
4844   (cond (subject (gnus-summary-find-subject subject unread backward))
4845         (backward (gnus-summary-find-prev unread))
4846         (t (gnus-summary-find-next unread))))
4847
4848 (defun gnus-recenter (&optional n)
4849   "Center point in window and redisplay frame.
4850 Also do horizontal recentering."
4851   (interactive "P")
4852   (when (and gnus-auto-center-summary
4853              (not (eq gnus-auto-center-summary 'vertical)))
4854     (gnus-horizontal-recenter))
4855   (recenter n))
4856
4857 (defun gnus-summary-recenter ()
4858   "Center point in the summary window.
4859 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4860 displayed, no centering will be performed."
4861   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4862   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4863   (let* ((top (cond ((< (window-height) 4) 0)
4864                     ((< (window-height) 7) 1)
4865                     (t 2)))
4866          (height (1- (window-height)))
4867          (bottom (save-excursion (goto-char (point-max))
4868                                  (forward-line (- height))
4869                                  (point)))
4870          (window (get-buffer-window (current-buffer))))
4871     ;; The user has to want it.
4872     (when gnus-auto-center-summary
4873       (when (get-buffer-window gnus-article-buffer)
4874         ;; Only do recentering when the article buffer is displayed,
4875         ;; Set the window start to either `bottom', which is the biggest
4876         ;; possible valid number, or the second line from the top,
4877         ;; whichever is the least.
4878         (set-window-start
4879          window (min bottom (save-excursion
4880                               (forward-line (- top)) (point)))))
4881       ;; Do horizontal recentering while we're at it.
4882       (when (and (get-buffer-window (current-buffer) t)
4883                  (not (eq gnus-auto-center-summary 'vertical)))
4884         (let ((selected (selected-window)))
4885           (select-window (get-buffer-window (current-buffer) t))
4886           (gnus-summary-position-point)
4887           (gnus-horizontal-recenter)
4888           (select-window selected))))))
4889
4890 (defun gnus-summary-jump-to-group (newsgroup)
4891   "Move point to NEWSGROUP in group mode buffer."
4892   ;; Keep update point of group mode buffer if visible.
4893   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4894       (save-window-excursion
4895         ;; Take care of tree window mode.
4896         (when (get-buffer-window gnus-group-buffer)
4897           (pop-to-buffer gnus-group-buffer))
4898         (gnus-group-jump-to-group newsgroup))
4899     (save-excursion
4900       ;; Take care of tree window mode.
4901       (if (get-buffer-window gnus-group-buffer)
4902           (pop-to-buffer gnus-group-buffer)
4903         (set-buffer gnus-group-buffer))
4904       (gnus-group-jump-to-group newsgroup))))
4905
4906 ;; This function returns a list of article numbers based on the
4907 ;; difference between the ranges of read articles in this group and
4908 ;; the range of active articles.
4909 (defun gnus-list-of-unread-articles (group)
4910   (let* ((read (gnus-info-read (gnus-get-info group)))
4911          (active (or (gnus-active group) (gnus-activate-group group)))
4912          (last (cdr active))
4913          first nlast unread)
4914     ;; If none are read, then all are unread.
4915     (if (not read)
4916         (setq first (car active))
4917       ;; If the range of read articles is a single range, then the
4918       ;; first unread article is the article after the last read
4919       ;; article.  Sounds logical, doesn't it?
4920       (if (not (listp (cdr read)))
4921           (setq first (1+ (cdr read)))
4922         ;; `read' is a list of ranges.
4923         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4924                                   (caar read)))
4925                   1)
4926           (setq first 1))
4927         (while read
4928           (when first
4929             (while (< first nlast)
4930               (push first unread)
4931               (setq first (1+ first))))
4932           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4933           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4934           (setq read (cdr read)))))
4935     ;; And add the last unread articles.
4936     (while (<= first last)
4937       (push first unread)
4938       (setq first (1+ first)))
4939     ;; Return the list of unread articles.
4940     (delq 0 (nreverse unread))))
4941
4942 (defun gnus-list-of-read-articles (group)
4943   "Return a list of unread, unticked and non-dormant articles."
4944   (let* ((info (gnus-get-info group))
4945          (marked (gnus-info-marks info))
4946          (active (gnus-active group)))
4947     (and info active
4948          (gnus-set-difference
4949           (gnus-sorted-complement
4950            (gnus-uncompress-range active)
4951            (gnus-list-of-unread-articles group))
4952           (append
4953            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4954            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4955
4956 ;; Various summary commands
4957
4958 (defun gnus-summary-select-article-buffer ()
4959   "Reconfigure windows to show article buffer."
4960   (interactive)
4961   (if (not (gnus-buffer-live-p gnus-article-buffer))
4962       (error "There is no article buffer for this summary buffer")
4963     (gnus-configure-windows 'article)
4964     (select-window (get-buffer-window gnus-article-buffer))))
4965
4966 (defun gnus-summary-universal-argument (arg)
4967   "Perform any operation on all articles that are process/prefixed."
4968   (interactive "P")
4969   (let ((articles (gnus-summary-work-articles arg))
4970         func article)
4971     (if (eq
4972          (setq
4973           func
4974           (key-binding
4975            (read-key-sequence
4976             (substitute-command-keys
4977              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4978              ))))
4979          'undefined)
4980         (gnus-error 1 "Undefined key")
4981       (save-excursion
4982         (while articles
4983           (gnus-summary-goto-subject (setq article (pop articles)))
4984           (let (gnus-newsgroup-processable)
4985             (command-execute func))
4986           (gnus-summary-remove-process-mark article)))))
4987   (gnus-summary-position-point))
4988
4989 (defun gnus-summary-toggle-truncation (&optional arg)
4990   "Toggle truncation of summary lines.
4991 With arg, turn line truncation on iff arg is positive."
4992   (interactive "P")
4993   (setq truncate-lines
4994         (if (null arg) (not truncate-lines)
4995           (> (prefix-numeric-value arg) 0)))
4996   (redraw-display))
4997
4998 (defun gnus-summary-reselect-current-group (&optional all rescan)
4999   "Exit and then reselect the current newsgroup.
5000 The prefix argument ALL means to select all articles."
5001   (interactive "P")
5002   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5003     (error "Ephemeral groups can't be reselected"))
5004   (let ((current-subject (gnus-summary-article-number))
5005         (group gnus-newsgroup-name))
5006     (setq gnus-newsgroup-begin nil)
5007     (gnus-summary-exit)
5008     ;; We have to adjust the point of group mode buffer because
5009     ;; point was moved to the next unread newsgroup by exiting.
5010     (gnus-summary-jump-to-group group)
5011     (when rescan
5012       (save-excursion
5013         (gnus-group-get-new-news-this-group 1)))
5014     (gnus-group-read-group all t)
5015     (gnus-summary-goto-subject current-subject nil t)))
5016
5017 (defun gnus-summary-rescan-group (&optional all)
5018   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5019   (interactive "P")
5020   (gnus-summary-reselect-current-group all t))
5021
5022 (defun gnus-summary-update-info (&optional non-destructive)
5023   (save-excursion
5024     (let ((group gnus-newsgroup-name))
5025       (when group
5026         (when gnus-newsgroup-kill-headers
5027           (setq gnus-newsgroup-killed
5028                 (gnus-compress-sequence
5029                  (nconc
5030                   (gnus-set-sorted-intersection
5031                    (gnus-uncompress-range gnus-newsgroup-killed)
5032                    (setq gnus-newsgroup-unselected
5033                          (sort gnus-newsgroup-unselected '<)))
5034                   (setq gnus-newsgroup-unreads
5035                         (sort gnus-newsgroup-unreads '<)))
5036                  t)))
5037         (unless (listp (cdr gnus-newsgroup-killed))
5038           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5039         (let ((headers gnus-newsgroup-headers))
5040           (when (and (not gnus-save-score)
5041                      (not non-destructive))
5042             (setq gnus-newsgroup-scored nil))
5043           ;; Set the new ranges of read articles.
5044           (save-excursion
5045             (set-buffer gnus-group-buffer)
5046             (gnus-undo-force-boundary))
5047           (gnus-update-read-articles
5048            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5049           ;; Set the current article marks.
5050           (gnus-update-marks)
5051           ;; Do the cross-ref thing.
5052           (when gnus-use-cross-reference
5053             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5054           ;; Do not switch windows but change the buffer to work.
5055           (set-buffer gnus-group-buffer)
5056           (unless (gnus-ephemeral-group-p group)
5057             (gnus-group-update-group group)))))))
5058
5059 (defun gnus-summary-save-newsrc (&optional force)
5060   "Save the current number of read/marked articles in the dribble buffer.
5061 The dribble buffer will then be saved.
5062 If FORCE (the prefix), also save the .newsrc file(s)."
5063   (interactive "P")
5064   (gnus-summary-update-info t)
5065   (if force
5066       (gnus-save-newsrc-file)
5067     (gnus-dribble-save)))
5068
5069 (defun gnus-summary-exit (&optional temporary)
5070   "Exit reading current newsgroup, and then return to group selection mode.
5071 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5072   (interactive)
5073   (gnus-set-global-variables)
5074   (gnus-kill-save-kill-buffer)
5075   (let* ((group gnus-newsgroup-name)
5076          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5077          (mode major-mode)
5078          (group-point nil)
5079          (buf (current-buffer)))
5080     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5081     ;; If we have several article buffers, we kill them at exit.
5082     (unless gnus-single-article-buffer
5083       (gnus-kill-buffer gnus-original-article-buffer)
5084       (setq gnus-article-current nil))
5085     (when gnus-use-cache
5086       (gnus-cache-possibly-remove-articles)
5087       (gnus-cache-save-buffers))
5088     (gnus-async-prefetch-remove-group group)
5089     (when gnus-suppress-duplicates
5090       (gnus-dup-enter-articles))
5091     (when gnus-use-trees
5092       (gnus-tree-close group))
5093     ;; Remove entries for this group.
5094     (nnmail-purge-split-history (gnus-group-real-name group))
5095     ;; Make all changes in this group permanent.
5096     (unless quit-config
5097       (gnus-run-hooks 'gnus-exit-group-hook)
5098       (gnus-summary-update-info)
5099       ;; Do adaptive scoring, and possibly save score files.
5100       (when gnus-newsgroup-adaptive
5101         (gnus-score-adaptive))
5102       (when gnus-use-scoring
5103         (gnus-score-save)))
5104     (gnus-close-group group)
5105     ;; Make sure where we were, and go to next newsgroup.
5106     (set-buffer gnus-group-buffer)
5107     (unless quit-config
5108       (gnus-group-jump-to-group group))
5109     (gnus-run-hooks 'gnus-summary-exit-hook)
5110     (unless (or quit-config
5111                 ;; If this group has disappeared from the summary
5112                 ;; buffer, don't skip forwards.
5113                 (not (string= group (gnus-group-group-name))))
5114       (gnus-group-next-unread-group 1))
5115     (setq group-point (point))
5116     (if temporary
5117         nil                             ;Nothing to do.
5118       ;; If we have several article buffers, we kill them at exit.
5119       (unless gnus-single-article-buffer
5120         (gnus-kill-buffer gnus-article-buffer)
5121         (gnus-kill-buffer gnus-original-article-buffer)
5122         (setq gnus-article-current nil))
5123       (set-buffer buf)
5124       (if (not gnus-kill-summary-on-exit)
5125           (gnus-deaden-summary)
5126         ;; We set all buffer-local variables to nil.  It is unclear why
5127         ;; this is needed, but if we don't, buffer-local variables are
5128         ;; not garbage-collected, it seems.  This would the lead to en
5129         ;; ever-growing Emacs.
5130         (gnus-summary-clear-local-variables)
5131         (when (get-buffer gnus-article-buffer)
5132           (bury-buffer gnus-article-buffer))
5133         ;; We clear the global counterparts of the buffer-local
5134         ;; variables as well, just to be on the safe side.
5135         (set-buffer gnus-group-buffer)
5136         (gnus-summary-clear-local-variables)
5137         ;; Return to group mode buffer.
5138         (when (eq mode 'gnus-summary-mode)
5139           (gnus-kill-buffer buf)))
5140       (setq gnus-current-select-method gnus-select-method)
5141       (pop-to-buffer gnus-group-buffer)
5142       ;; Clear the current group name.
5143       (if (not quit-config)
5144           (progn
5145             (goto-char group-point)
5146             (gnus-configure-windows 'group 'force))
5147         (gnus-handle-ephemeral-exit quit-config))
5148       (unless quit-config
5149         (setq gnus-newsgroup-name nil)))))
5150
5151 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5152 (defun gnus-summary-exit-no-update (&optional no-questions)
5153   "Quit reading current newsgroup without updating read article info."
5154   (interactive)
5155   (let* ((group gnus-newsgroup-name)
5156          (quit-config (gnus-group-quit-config group)))
5157     (when (or no-questions
5158               gnus-expert-user
5159               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5160       ;; If we have several article buffers, we kill them at exit.
5161       (unless gnus-single-article-buffer
5162         (gnus-kill-buffer gnus-article-buffer)
5163         (gnus-kill-buffer gnus-original-article-buffer)
5164         (setq gnus-article-current nil))
5165       (if (not gnus-kill-summary-on-exit)
5166           (gnus-deaden-summary)
5167         (gnus-close-group group)
5168         (gnus-summary-clear-local-variables)
5169         (set-buffer gnus-group-buffer)
5170         (gnus-summary-clear-local-variables)
5171         (when (get-buffer gnus-summary-buffer)
5172           (kill-buffer gnus-summary-buffer)))
5173       (unless gnus-single-article-buffer
5174         (setq gnus-article-current nil))
5175       (when gnus-use-trees
5176         (gnus-tree-close group))
5177       (gnus-async-prefetch-remove-group group)
5178       (when (get-buffer gnus-article-buffer)
5179         (bury-buffer gnus-article-buffer))
5180       ;; Return to the group buffer.
5181       (gnus-configure-windows 'group 'force)
5182       ;; Clear the current group name.
5183       (setq gnus-newsgroup-name nil)
5184       (when (equal (gnus-group-group-name) group)
5185         (gnus-group-next-unread-group 1))
5186       (when quit-config
5187         (gnus-handle-ephemeral-exit quit-config)))))
5188
5189 (defun gnus-handle-ephemeral-exit (quit-config)
5190   "Handle movement when leaving an ephemeral group.  The state
5191 which existed when entering the ephemeral is reset."
5192   (if (not (buffer-name (car quit-config)))
5193       (gnus-configure-windows 'group 'force)
5194     (set-buffer (car quit-config))
5195     (cond ((eq major-mode 'gnus-summary-mode)
5196            (gnus-set-global-variables))
5197           ((eq major-mode 'gnus-article-mode)
5198            (save-excursion
5199              ;; The `gnus-summary-buffer' variable may point
5200              ;; to the old summary buffer when using a single
5201              ;; article buffer.
5202              (unless (gnus-buffer-live-p gnus-summary-buffer)
5203                (set-buffer gnus-group-buffer))
5204              (set-buffer gnus-summary-buffer)
5205              (gnus-set-global-variables))))
5206     (if (or (eq (cdr quit-config) 'article)
5207             (eq (cdr quit-config) 'pick))
5208         (progn
5209           ;; The current article may be from the ephemeral group
5210           ;; thus it is best that we reload this article
5211           (gnus-summary-show-article)
5212           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5213               (gnus-configure-windows 'pick 'force)
5214             (gnus-configure-windows (cdr quit-config) 'force)))
5215       (gnus-configure-windows (cdr quit-config) 'force))
5216     (when (eq major-mode 'gnus-summary-mode)
5217       (gnus-summary-next-subject 1 nil t)
5218       (gnus-summary-recenter)
5219       (gnus-summary-position-point))))
5220
5221 ;;; Dead summaries.
5222
5223 (defvar gnus-dead-summary-mode-map nil)
5224
5225 (unless gnus-dead-summary-mode-map
5226   (setq gnus-dead-summary-mode-map (make-keymap))
5227   (suppress-keymap gnus-dead-summary-mode-map)
5228   (substitute-key-definition
5229    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5230   (let ((keys '("\C-d" "\r" "\177" [delete])))
5231     (while keys
5232       (define-key gnus-dead-summary-mode-map
5233         (pop keys) 'gnus-summary-wake-up-the-dead))))
5234
5235 (defvar gnus-dead-summary-mode nil
5236   "Minor mode for Gnus summary buffers.")
5237
5238 (defun gnus-dead-summary-mode (&optional arg)
5239   "Minor mode for Gnus summary buffers."
5240   (interactive "P")
5241   (when (eq major-mode 'gnus-summary-mode)
5242     (make-local-variable 'gnus-dead-summary-mode)
5243     (setq gnus-dead-summary-mode
5244           (if (null arg) (not gnus-dead-summary-mode)
5245             (> (prefix-numeric-value arg) 0)))
5246     (when gnus-dead-summary-mode
5247       (gnus-add-minor-mode
5248        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5249
5250 (defun gnus-deaden-summary ()
5251   "Make the current summary buffer into a dead summary buffer."
5252   ;; Kill any previous dead summary buffer.
5253   (when (and gnus-dead-summary
5254              (buffer-name gnus-dead-summary))
5255     (save-excursion
5256       (set-buffer gnus-dead-summary)
5257       (when gnus-dead-summary-mode
5258         (kill-buffer (current-buffer)))))
5259   ;; Make this the current dead summary.
5260   (setq gnus-dead-summary (current-buffer))
5261   (gnus-dead-summary-mode 1)
5262   (let ((name (buffer-name)))
5263     (when (string-match "Summary" name)
5264       (rename-buffer
5265        (concat (substring name 0 (match-beginning 0)) "Dead "
5266                (substring name (match-beginning 0)))
5267        t))))
5268
5269 (defun gnus-kill-or-deaden-summary (buffer)
5270   "Kill or deaden the summary BUFFER."
5271   (save-excursion
5272     (when (and (buffer-name buffer)
5273                (not gnus-single-article-buffer))
5274       (save-excursion
5275         (set-buffer buffer)
5276         (gnus-kill-buffer gnus-article-buffer)
5277         (gnus-kill-buffer gnus-original-article-buffer)))
5278     (cond (gnus-kill-summary-on-exit
5279            (when (and gnus-use-trees
5280                       (and (get-buffer buffer)
5281                            (buffer-name (get-buffer buffer))))
5282              (save-excursion
5283                (set-buffer (get-buffer buffer))
5284                (gnus-tree-close gnus-newsgroup-name)))
5285            (gnus-kill-buffer buffer))
5286           ((and (get-buffer buffer)
5287                 (buffer-name (get-buffer buffer)))
5288            (save-excursion
5289              (set-buffer buffer)
5290              (gnus-deaden-summary))))))
5291
5292 (defun gnus-summary-wake-up-the-dead (&rest args)
5293   "Wake up the dead summary buffer."
5294   (interactive)
5295   (gnus-dead-summary-mode -1)
5296   (let ((name (buffer-name)))
5297     (when (string-match "Dead " name)
5298       (rename-buffer
5299        (concat (substring name 0 (match-beginning 0))
5300                (substring name (match-end 0)))
5301        t)))
5302   (gnus-message 3 "This dead summary is now alive again"))
5303
5304 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5305 (defun gnus-summary-fetch-faq (&optional faq-dir)
5306   "Fetch the FAQ for the current group.
5307 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5308 in."
5309   (interactive
5310    (list
5311     (when current-prefix-arg
5312       (completing-read
5313        "Faq dir: " (and (listp gnus-group-faq-directory)
5314                         (mapcar (lambda (file) (list file))
5315                                 gnus-group-faq-directory))))))
5316   (let (gnus-faq-buffer)
5317     (when (setq gnus-faq-buffer
5318                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5319       (gnus-configure-windows 'summary-faq))))
5320
5321 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5322 (defun gnus-summary-describe-group (&optional force)
5323   "Describe the current newsgroup."
5324   (interactive "P")
5325   (gnus-group-describe-group force gnus-newsgroup-name))
5326
5327 (defun gnus-summary-describe-briefly ()
5328   "Describe summary mode commands briefly."
5329   (interactive)
5330   (gnus-message 6
5331                 (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")))
5332
5333 ;; Walking around group mode buffer from summary mode.
5334
5335 (defun gnus-summary-next-group (&optional no-article target-group backward)
5336   "Exit current newsgroup and then select next unread newsgroup.
5337 If prefix argument NO-ARTICLE is non-nil, no article is selected
5338 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5339 previous group instead."
5340   (interactive "P")
5341   ;; Stop pre-fetching.
5342   (gnus-async-halt-prefetch)
5343   (let ((current-group gnus-newsgroup-name)
5344         (current-buffer (current-buffer))
5345         entered)
5346     ;; First we semi-exit this group to update Xrefs and all variables.
5347     ;; We can't do a real exit, because the window conf must remain
5348     ;; the same in case the user is prompted for info, and we don't
5349     ;; want the window conf to change before that...
5350     (gnus-summary-exit t)
5351     (while (not entered)
5352       ;; Then we find what group we are supposed to enter.
5353       (set-buffer gnus-group-buffer)
5354       (gnus-group-jump-to-group current-group)
5355       (setq target-group
5356             (or target-group
5357                 (if (eq gnus-keep-same-level 'best)
5358                     (gnus-summary-best-group gnus-newsgroup-name)
5359                   (gnus-summary-search-group backward gnus-keep-same-level))))
5360       (if (not target-group)
5361           ;; There are no further groups, so we return to the group
5362           ;; buffer.
5363           (progn
5364             (gnus-message 5 "Returning to the group buffer")
5365             (setq entered t)
5366             (when (gnus-buffer-live-p current-buffer)
5367               (set-buffer current-buffer)
5368               (gnus-summary-exit))
5369             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5370         ;; We try to enter the target group.
5371         (gnus-group-jump-to-group target-group)
5372         (let ((unreads (gnus-group-group-unread)))
5373           (if (and (or (eq t unreads)
5374                        (and unreads (not (zerop unreads))))
5375                    (gnus-summary-read-group
5376                     target-group nil no-article
5377                     (and (buffer-name current-buffer) current-buffer)))
5378               (setq entered t)
5379             (setq current-group target-group
5380                   target-group nil)))))))
5381
5382 (defun gnus-summary-prev-group (&optional no-article)
5383   "Exit current newsgroup and then select previous unread newsgroup.
5384 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5385   (interactive "P")
5386   (gnus-summary-next-group no-article nil t))
5387
5388 ;; Walking around summary lines.
5389
5390 (defun gnus-summary-first-subject (&optional unread)
5391   "Go to the first unread subject.
5392 If UNREAD is non-nil, go to the first unread article.
5393 Returns the article selected or nil if there are no unread articles."
5394   (interactive "P")
5395   (prog1
5396       (cond
5397        ;; Empty summary.
5398        ((null gnus-newsgroup-data)
5399         (gnus-message 3 "No articles in the group")
5400         nil)
5401        ;; Pick the first article.
5402        ((not unread)
5403         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5404         (gnus-data-number (car gnus-newsgroup-data)))
5405        ;; No unread articles.
5406        ((null gnus-newsgroup-unreads)
5407         (gnus-message 3 "No more unread articles")
5408         nil)
5409        ;; Find the first unread article.
5410        (t
5411         (let ((data gnus-newsgroup-data))
5412           (while (and data
5413                       (not (gnus-data-unread-p (car data))))
5414             (setq data (cdr data)))
5415           (when data
5416             (goto-char (gnus-data-pos (car data)))
5417             (gnus-data-number (car data))))))
5418     (gnus-summary-position-point)))
5419
5420 (defun gnus-summary-next-subject (n &optional unread dont-display)
5421   "Go to next N'th summary line.
5422 If N is negative, go to the previous N'th subject line.
5423 If UNREAD is non-nil, only unread articles are selected.
5424 The difference between N and the actual number of steps taken is
5425 returned."
5426   (interactive "p")
5427   (let ((backward (< n 0))
5428         (n (abs n)))
5429     (while (and (> n 0)
5430                 (if backward
5431                     (gnus-summary-find-prev unread)
5432                   (gnus-summary-find-next unread)))
5433       (setq n (1- n)))
5434     (when (/= 0 n)
5435       (gnus-message 7 "No more%s articles"
5436                     (if unread " unread" "")))
5437     (unless dont-display
5438       (gnus-summary-recenter)
5439       (gnus-summary-position-point))
5440     n))
5441
5442 (defun gnus-summary-next-unread-subject (n)
5443   "Go to next N'th unread summary line."
5444   (interactive "p")
5445   (gnus-summary-next-subject n t))
5446
5447 (defun gnus-summary-prev-subject (n &optional unread)
5448   "Go to previous N'th summary line.
5449 If optional argument UNREAD is non-nil, only unread article is selected."
5450   (interactive "p")
5451   (gnus-summary-next-subject (- n) unread))
5452
5453 (defun gnus-summary-prev-unread-subject (n)
5454   "Go to previous N'th unread summary line."
5455   (interactive "p")
5456   (gnus-summary-next-subject (- n) t))
5457
5458 (defun gnus-summary-goto-subject (article &optional force silent)
5459   "Go the subject line of ARTICLE.
5460 If FORCE, also allow jumping to articles not currently shown."
5461   (interactive "nArticle number: ")
5462   (let ((b (point))
5463         (data (gnus-data-find article)))
5464     ;; We read in the article if we have to.
5465     (and (not data)
5466          force
5467          (gnus-summary-insert-subject article (and (vectorp force) force) t)
5468          (setq data (gnus-data-find article)))
5469     (goto-char b)
5470     (if (not data)
5471         (progn
5472           (unless silent
5473             (gnus-message 3 "Can't find article %d" article))
5474           nil)
5475       (goto-char (gnus-data-pos data))
5476       article)))
5477
5478 ;; Walking around summary lines with displaying articles.
5479
5480 (defun gnus-summary-expand-window (&optional arg)
5481   "Make the summary buffer take up the entire Emacs frame.
5482 Given a prefix, will force an `article' buffer configuration."
5483   (interactive "P")
5484   (if arg
5485       (gnus-configure-windows 'article 'force)
5486     (gnus-configure-windows 'summary 'force)))
5487
5488 (defun gnus-summary-display-article (article &optional all-header)
5489   "Display ARTICLE in article buffer."
5490   (gnus-set-global-variables)
5491   (if (null article)
5492       nil
5493     (prog1
5494         (if gnus-summary-display-article-function
5495             (funcall gnus-summary-display-article-function article all-header)
5496           (gnus-article-prepare article all-header))
5497       (gnus-run-hooks 'gnus-select-article-hook)
5498       (when (and gnus-current-article
5499                  (not (zerop gnus-current-article)))
5500         (gnus-summary-goto-subject gnus-current-article))
5501       (gnus-summary-recenter)
5502       (when (and gnus-use-trees gnus-show-threads)
5503         (gnus-possibly-generate-tree article)
5504         (gnus-highlight-selected-tree article))
5505       ;; Successfully display article.
5506       (gnus-article-set-window-start
5507        (cdr (assq article gnus-newsgroup-bookmarks))))))
5508
5509 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5510   "Select the current article.
5511 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5512 non-nil, the article will be re-fetched even if it already present in
5513 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5514 be displayed."
5515   ;; Make sure we are in the summary buffer to work around bbdb bug.
5516   (unless (eq major-mode 'gnus-summary-mode)
5517     (set-buffer gnus-summary-buffer))
5518   (let ((article (or article (gnus-summary-article-number)))
5519         (all-headers (not (not all-headers))) ;Must be T or NIL.
5520         gnus-summary-display-article-function
5521         did)
5522     (and (not pseudo)
5523          (gnus-summary-article-pseudo-p article)
5524          (error "This is a pseudo-article"))
5525     (prog1
5526         (save-excursion
5527           (set-buffer gnus-summary-buffer)
5528           (if (or (and gnus-single-article-buffer
5529                        (or (null gnus-current-article)
5530                            (null gnus-article-current)
5531                            (null (get-buffer gnus-article-buffer))
5532                            (not (eq article (cdr gnus-article-current)))
5533                            (not (equal (car gnus-article-current)
5534                                        gnus-newsgroup-name))))
5535                   (and (not gnus-single-article-buffer)
5536                        (or (null gnus-current-article)
5537                            (not (eq gnus-current-article article))))
5538                   force)
5539               ;; The requested article is different from the current article.
5540               (prog1
5541                   (gnus-summary-display-article article all-headers)
5542                 (setq did article))
5543             (when (or all-headers gnus-show-all-headers)
5544               (gnus-article-show-all-headers))
5545             'old))
5546       (when did
5547         (gnus-article-set-window-start
5548          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5549
5550 (defun gnus-summary-set-current-mark (&optional current-mark)
5551   "Obsolete function."
5552   nil)
5553
5554 (defun gnus-summary-next-article (&optional unread subject backward push)
5555   "Select the next article.
5556 If UNREAD, only unread articles are selected.
5557 If SUBJECT, only articles with SUBJECT are selected.
5558 If BACKWARD, the previous article is selected instead of the next."
5559   (interactive "P")
5560   (cond
5561    ;; Is there such an article?
5562    ((and (gnus-summary-search-forward unread subject backward)
5563          (or (gnus-summary-display-article (gnus-summary-article-number))
5564              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5565     (gnus-summary-position-point))
5566    ;; If not, we try the first unread, if that is wanted.
5567    ((and subject
5568          gnus-auto-select-same
5569          (gnus-summary-first-unread-article))
5570     (gnus-summary-position-point)
5571     (gnus-message 6 "Wrapped"))
5572    ;; Try to get next/previous article not displayed in this group.
5573    ((and gnus-auto-extend-newsgroup
5574          (not unread) (not subject))
5575     (gnus-summary-goto-article
5576      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5577      nil t))
5578    ;; Go to next/previous group.
5579    (t
5580     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5581       (gnus-summary-jump-to-group gnus-newsgroup-name))
5582     (let ((cmd last-command-char)
5583           (point
5584            (save-excursion
5585              (set-buffer gnus-group-buffer)
5586              (point)))
5587           (group
5588            (if (eq gnus-keep-same-level 'best)
5589                (gnus-summary-best-group gnus-newsgroup-name)
5590              (gnus-summary-search-group backward gnus-keep-same-level))))
5591       ;; For some reason, the group window gets selected.  We change
5592       ;; it back.
5593       (select-window (get-buffer-window (current-buffer)))
5594       ;; Select next unread newsgroup automagically.
5595       (cond
5596        ((or (not gnus-auto-select-next)
5597             (not cmd))
5598         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5599        ((or (eq gnus-auto-select-next 'quietly)
5600             (and (eq gnus-auto-select-next 'slightly-quietly)
5601                  push)
5602             (and (eq gnus-auto-select-next 'almost-quietly)
5603                  (gnus-summary-last-article-p)))
5604         ;; Select quietly.
5605         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5606             (gnus-summary-exit)
5607           (gnus-message 7 "No more%s articles (%s)..."
5608                         (if unread " unread" "")
5609                         (if group (concat "selecting " group)
5610                           "exiting"))
5611           (gnus-summary-next-group nil group backward)))
5612        (t
5613         (when (gnus-key-press-event-p last-input-event)
5614           (gnus-summary-walk-group-buffer
5615            gnus-newsgroup-name cmd unread backward point))))))))
5616
5617 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5618   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5619                       (?\C-p (gnus-group-prev-unread-group 1))))
5620         (cursor-in-echo-area t)
5621         keve key group ended)
5622     (save-excursion
5623       (set-buffer gnus-group-buffer)
5624       (goto-char start)
5625       (setq group
5626             (if (eq gnus-keep-same-level 'best)
5627                 (gnus-summary-best-group gnus-newsgroup-name)
5628               (gnus-summary-search-group backward gnus-keep-same-level))))
5629     (while (not ended)
5630       (gnus-message
5631        5 "No more%s articles%s" (if unread " unread" "")
5632        (if (and group
5633                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5634            (format " (Type %s for %s [%s])"
5635                    (single-key-description cmd) group
5636                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5637          (format " (Type %s to exit %s)"
5638                  (single-key-description cmd)
5639                  gnus-newsgroup-name)))
5640       ;; Confirm auto selection.
5641       (setq key (car (setq keve (gnus-read-event-char))))
5642       (setq ended t)
5643       (cond
5644        ((assq key keystrokes)
5645         (let ((obuf (current-buffer)))
5646           (switch-to-buffer gnus-group-buffer)
5647           (when group
5648             (gnus-group-jump-to-group group))
5649           (eval (cadr (assq key keystrokes)))
5650           (setq group (gnus-group-group-name))
5651           (switch-to-buffer obuf))
5652         (setq ended nil))
5653        ((equal key cmd)
5654         (if (or (not group)
5655                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5656             (gnus-summary-exit)
5657           (gnus-summary-next-group nil group backward)))
5658        (t
5659         (push (cdr keve) unread-command-events))))))
5660
5661 (defun gnus-summary-next-unread-article ()
5662   "Select unread article after current one."
5663   (interactive)
5664   (gnus-summary-next-article
5665    (or (not (eq gnus-summary-goto-unread 'never))
5666        (gnus-summary-last-article-p (gnus-summary-article-number)))
5667    (and gnus-auto-select-same
5668         (gnus-summary-article-subject))))
5669
5670 (defun gnus-summary-prev-article (&optional unread subject)
5671   "Select the article after the current one.
5672 If UNREAD is non-nil, only unread articles are selected."
5673   (interactive "P")
5674   (gnus-summary-next-article unread subject t))
5675
5676 (defun gnus-summary-prev-unread-article ()
5677   "Select unread article before current one."
5678   (interactive)
5679   (gnus-summary-prev-article
5680    (or (not (eq gnus-summary-goto-unread 'never))
5681        (gnus-summary-first-article-p (gnus-summary-article-number)))
5682    (and gnus-auto-select-same
5683         (gnus-summary-article-subject))))
5684
5685 (defun gnus-summary-next-page (&optional lines circular)
5686   "Show next page of the selected article.
5687 If at the end of the current article, select the next article.
5688 LINES says how many lines should be scrolled up.
5689
5690 If CIRCULAR is non-nil, go to the start of the article instead of
5691 selecting the next article when reaching the end of the current
5692 article."
5693   (interactive "P")
5694   (setq gnus-summary-buffer (current-buffer))
5695   (gnus-set-global-variables)
5696   (let ((article (gnus-summary-article-number))
5697         (article-window (get-buffer-window gnus-article-buffer t))
5698         endp)
5699     (gnus-configure-windows 'article)
5700     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5701         (if (and (eq gnus-summary-goto-unread 'never)
5702                  (not (gnus-summary-last-article-p article)))
5703             (gnus-summary-next-article)
5704           (gnus-summary-next-unread-article))
5705       (if (or (null gnus-current-article)
5706               (null gnus-article-current)
5707               (/= article (cdr gnus-article-current))
5708               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5709           ;; Selected subject is different from current article's.
5710           (gnus-summary-display-article article)
5711         (when article-window
5712           (gnus-eval-in-buffer-window gnus-article-buffer
5713             (setq endp (gnus-article-next-page lines)))
5714           (when endp
5715             (cond (circular
5716                    (gnus-summary-beginning-of-article))
5717                   (lines
5718                    (gnus-message 3 "End of message"))
5719                   ((null lines)
5720                    (if (and (eq gnus-summary-goto-unread 'never)
5721                             (not (gnus-summary-last-article-p article)))
5722                        (gnus-summary-next-article)
5723                      (gnus-summary-next-unread-article))))))))
5724     (gnus-summary-recenter)
5725     (gnus-summary-position-point)))
5726
5727 (defun gnus-summary-prev-page (&optional lines move)
5728   "Show previous page of selected article.
5729 Argument LINES specifies lines to be scrolled down.
5730 If MOVE, move to the previous unread article if point is at
5731 the beginning of the buffer."
5732   (interactive "P")
5733   (let ((article (gnus-summary-article-number))
5734         (article-window (get-buffer-window gnus-article-buffer t))
5735         endp)
5736     (gnus-configure-windows 'article)
5737     (if (or (null gnus-current-article)
5738             (null gnus-article-current)
5739             (/= article (cdr gnus-article-current))
5740             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5741         ;; Selected subject is different from current article's.
5742         (gnus-summary-display-article article)
5743       (gnus-summary-recenter)
5744       (when article-window
5745         (gnus-eval-in-buffer-window gnus-article-buffer
5746           (setq endp (gnus-article-prev-page lines)))
5747         (when (and move endp)
5748           (cond (lines
5749                  (gnus-message 3 "Beginning of message"))
5750                 ((null lines)
5751                  (if (and (eq gnus-summary-goto-unread 'never)
5752                           (not (gnus-summary-first-article-p article)))
5753                      (gnus-summary-prev-article)
5754                    (gnus-summary-prev-unread-article))))))))
5755   (gnus-summary-position-point))
5756
5757 (defun gnus-summary-prev-page-or-article (&optional lines)
5758   "Show previous page of selected article.
5759 Argument LINES specifies lines to be scrolled down.
5760 If at the beginning of the article, go to the next article."
5761   (interactive "P")
5762   (gnus-summary-prev-page lines t))
5763
5764 (defun gnus-summary-scroll-up (lines)
5765   "Scroll up (or down) one line current article.
5766 Argument LINES specifies lines to be scrolled up (or down if negative)."
5767   (interactive "p")
5768   (gnus-configure-windows 'article)
5769   (gnus-summary-show-thread)
5770   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5771     (gnus-eval-in-buffer-window gnus-article-buffer
5772       (cond ((> lines 0)
5773              (when (gnus-article-next-page lines)
5774                (gnus-message 3 "End of message")))
5775             ((< lines 0)
5776              (gnus-article-prev-page (- lines))))))
5777   (gnus-summary-recenter)
5778   (gnus-summary-position-point))
5779
5780 (defun gnus-summary-next-same-subject ()
5781   "Select next article which has the same subject as current one."
5782   (interactive)
5783   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5784
5785 (defun gnus-summary-prev-same-subject ()
5786   "Select previous article which has the same subject as current one."
5787   (interactive)
5788   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5789
5790 (defun gnus-summary-next-unread-same-subject ()
5791   "Select next unread article which has the same subject as current one."
5792   (interactive)
5793   (gnus-summary-next-article t (gnus-summary-article-subject)))
5794
5795 (defun gnus-summary-prev-unread-same-subject ()
5796   "Select previous unread article which has the same subject as current one."
5797   (interactive)
5798   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5799
5800 (defun gnus-summary-first-unread-article ()
5801   "Select the first unread article.
5802 Return nil if there are no unread articles."
5803   (interactive)
5804   (prog1
5805       (when (gnus-summary-first-subject t)
5806         (gnus-summary-show-thread)
5807         (gnus-summary-first-subject t)
5808         (gnus-summary-display-article (gnus-summary-article-number)))
5809     (gnus-summary-position-point)))
5810
5811 (defun gnus-summary-first-article ()
5812   "Select the first article.
5813 Return nil if there are no articles."
5814   (interactive)
5815   (prog1
5816       (when (gnus-summary-first-subject)
5817       (gnus-summary-show-thread)
5818       (gnus-summary-first-subject)
5819       (gnus-summary-display-article (gnus-summary-article-number)))
5820     (gnus-summary-position-point)))
5821
5822 (defun gnus-summary-best-unread-article ()
5823   "Select the unread article with the highest score."
5824   (interactive)
5825   (let ((best -1000000)
5826         (data gnus-newsgroup-data)
5827         article score)
5828     (while data
5829       (and (gnus-data-unread-p (car data))
5830            (> (setq score
5831                     (gnus-summary-article-score (gnus-data-number (car data))))
5832               best)
5833            (setq best score
5834                  article (gnus-data-number (car data))))
5835       (setq data (cdr data)))
5836     (prog1
5837         (if article
5838             (gnus-summary-goto-article article)
5839           (error "No unread articles"))
5840       (gnus-summary-position-point))))
5841
5842 (defun gnus-summary-last-subject ()
5843   "Go to the last displayed subject line in the group."
5844   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5845     (when article
5846       (gnus-summary-goto-subject article))))
5847
5848 (defun gnus-summary-goto-article (article &optional all-headers force)
5849   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5850 If ALL-HEADERS is non-nil, no header lines are hidden."
5851   (interactive
5852    (list
5853     (completing-read
5854      "Article number or Message-ID: "
5855      (mapcar (lambda (number) (list (int-to-string number)))
5856              gnus-newsgroup-limit))
5857     current-prefix-arg
5858     t))
5859   (prog1
5860       (if (and (stringp article)
5861                (string-match "@" article))
5862           (gnus-summary-refer-article article)
5863         (when (stringp article)
5864           (setq article (string-to-number article)))
5865         (if (gnus-summary-goto-subject article force)
5866             (gnus-summary-display-article article all-headers)
5867           (gnus-message 4 "Couldn't go to article %s" article) nil))
5868     (gnus-summary-position-point)))
5869
5870 (defun gnus-summary-goto-last-article ()
5871   "Go to the previously read article."
5872   (interactive)
5873   (prog1
5874       (when gnus-last-article
5875         (gnus-summary-goto-article gnus-last-article nil t))
5876     (gnus-summary-position-point)))
5877
5878 (defun gnus-summary-pop-article (number)
5879   "Pop one article off the history and go to the previous.
5880 NUMBER articles will be popped off."
5881   (interactive "p")
5882   (let (to)
5883     (setq gnus-newsgroup-history
5884           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5885     (if to
5886         (gnus-summary-goto-article (car to) nil t)
5887       (error "Article history empty")))
5888   (gnus-summary-position-point))
5889
5890 ;; Summary commands and functions for limiting the summary buffer.
5891
5892 (defun gnus-summary-limit-to-articles (n)
5893   "Limit the summary buffer to the next N articles.
5894 If not given a prefix, use the process marked articles instead."
5895   (interactive "P")
5896   (prog1
5897       (let ((articles (gnus-summary-work-articles n)))
5898         (setq gnus-newsgroup-processable nil)
5899         (gnus-summary-limit articles))
5900     (gnus-summary-position-point)))
5901
5902 (defun gnus-summary-pop-limit (&optional total)
5903   "Restore the previous limit.
5904 If given a prefix, remove all limits."
5905   (interactive "P")
5906   (when total
5907     (setq gnus-newsgroup-limits
5908           (list (mapcar (lambda (h) (mail-header-number h))
5909                         gnus-newsgroup-headers))))
5910   (unless gnus-newsgroup-limits
5911     (error "No limit to pop"))
5912   (prog1
5913       (gnus-summary-limit nil 'pop)
5914     (gnus-summary-position-point)))
5915
5916 (defun gnus-summary-limit-to-subject (subject &optional header)
5917   "Limit the summary buffer to articles that have subjects that match a regexp."
5918   (interactive "sLimit to subject (regexp): ")
5919   (unless header
5920     (setq header "subject"))
5921   (when (not (equal "" subject))
5922     (prog1
5923         (let ((articles (gnus-summary-find-matching
5924                          (or header "subject") subject 'all)))
5925           (unless articles
5926             (error "Found no matches for \"%s\"" subject))
5927           (gnus-summary-limit articles))
5928       (gnus-summary-position-point))))
5929
5930 (defun gnus-summary-limit-to-author (from)
5931   "Limit the summary buffer to articles that have authors that match a regexp."
5932   (interactive "sLimit to author (regexp): ")
5933   (gnus-summary-limit-to-subject from "from"))
5934
5935 (defun gnus-summary-limit-to-age (age &optional younger-p)
5936   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5937 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5938 articles that are younger than AGE days."
5939   (interactive "nTime in days: \nP")
5940   (prog1
5941       (let ((data gnus-newsgroup-data)
5942             (cutoff (nnmail-days-to-time age))
5943             articles d date is-younger)
5944         (while (setq d (pop data))
5945           (when (and (vectorp (gnus-data-header d))
5946                      (setq date (mail-header-date (gnus-data-header d))))
5947             (setq is-younger (nnmail-time-less
5948                               (nnmail-time-since (nnmail-date-to-time date))
5949                               cutoff))
5950             (when (if younger-p is-younger (not is-younger))
5951               (push (gnus-data-number d) articles))))
5952         (gnus-summary-limit (nreverse articles)))
5953     (gnus-summary-position-point)))
5954
5955 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5956 (make-obsolete
5957  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5958
5959 (defun gnus-summary-limit-to-unread (&optional all)
5960   "Limit the summary buffer to articles that are not marked as read.
5961 If ALL is non-nil, limit strictly to unread articles."
5962   (interactive "P")
5963   (if all
5964       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5965     (gnus-summary-limit-to-marks
5966      ;; Concat all the marks that say that an article is read and have
5967      ;; those removed.
5968      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5969            gnus-killed-mark gnus-kill-file-mark
5970            gnus-low-score-mark gnus-expirable-mark
5971            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5972            gnus-duplicate-mark gnus-souped-mark)
5973      'reverse)))
5974
5975 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5976 (make-obsolete 'gnus-summary-delete-marked-with
5977                'gnus-summary-limit-exlude-marks)
5978
5979 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5980   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5981 If REVERSE, limit the summary buffer to articles that are marked
5982 with MARKS.  MARKS can either be a string of marks or a list of marks.
5983 Returns how many articles were removed."
5984   (interactive "sMarks: ")
5985   (gnus-summary-limit-to-marks marks t))
5986
5987 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5988   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5989 If REVERSE (the prefix), limit the summary buffer to articles that are
5990 not marked with MARKS.  MARKS can either be a string of marks or a
5991 list of marks.
5992 Returns how many articles were removed."
5993   (interactive "sMarks: \nP")
5994   (prog1
5995       (let ((data gnus-newsgroup-data)
5996             (marks (if (listp marks) marks
5997                      (append marks nil))) ; Transform to list.
5998             articles)
5999         (while data
6000           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6001                   (memq (gnus-data-mark (car data)) marks))
6002             (push (gnus-data-number (car data)) articles))
6003           (setq data (cdr data)))
6004         (gnus-summary-limit articles))
6005     (gnus-summary-position-point)))
6006
6007 (defun gnus-summary-limit-to-score (&optional score)
6008   "Limit to articles with score at or above SCORE."
6009   (interactive "P")
6010   (setq score (if score
6011                   (prefix-numeric-value score)
6012                 (or gnus-summary-default-score 0)))
6013   (let ((data gnus-newsgroup-data)
6014         articles)
6015     (while data
6016       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6017                 score)
6018         (push (gnus-data-number (car data)) articles))
6019       (setq data (cdr data)))
6020     (prog1
6021         (gnus-summary-limit articles)
6022       (gnus-summary-position-point))))
6023
6024 (defun gnus-summary-limit-include-thread (id)
6025   "Display all the hidden articles that in the current thread."
6026   (interactive (list (mail-header-id (gnus-summary-article-header))))
6027   (let ((articles (gnus-articles-in-thread
6028                    (gnus-id-to-thread (gnus-root-id id)))))
6029     (prog1
6030         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6031       (gnus-summary-position-point))))
6032
6033 (defun gnus-summary-limit-include-dormant ()
6034   "Display all the hidden articles that are marked as dormant.
6035 Note that this command only works on a subset of the articles currently
6036 fetched for this group."
6037   (interactive)
6038   (unless gnus-newsgroup-dormant
6039     (error "There are no dormant articles in this group"))
6040   (prog1
6041       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6042     (gnus-summary-position-point)))
6043
6044 (defun gnus-summary-limit-exclude-dormant ()
6045   "Hide all dormant articles."
6046   (interactive)
6047   (prog1
6048       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6049     (gnus-summary-position-point)))
6050
6051 (defun gnus-summary-limit-exclude-childless-dormant ()
6052   "Hide all dormant articles that have no children."
6053   (interactive)
6054   (let ((data (gnus-data-list t))
6055         articles d children)
6056     ;; Find all articles that are either not dormant or have
6057     ;; children.
6058     (while (setq d (pop data))
6059       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6060                 (and (setq children
6061                            (gnus-article-children (gnus-data-number d)))
6062                      (let (found)
6063                        (while children
6064                          (when (memq (car children) articles)
6065                            (setq children nil
6066                                  found t))
6067                          (pop children))
6068                        found)))
6069         (push (gnus-data-number d) articles)))
6070     ;; Do the limiting.
6071     (prog1
6072         (gnus-summary-limit articles)
6073       (gnus-summary-position-point))))
6074
6075 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6076   "Mark all unread excluded articles as read.
6077 If ALL, mark even excluded ticked and dormants as read."
6078   (interactive "P")
6079   (let ((articles (gnus-sorted-complement
6080                    (sort
6081                     (mapcar (lambda (h) (mail-header-number h))
6082                             gnus-newsgroup-headers)
6083                     '<)
6084                    (sort gnus-newsgroup-limit '<)))
6085         article)
6086     (setq gnus-newsgroup-unreads gnus-newsgroup-limit)
6087     (if all
6088         (setq gnus-newsgroup-dormant nil
6089               gnus-newsgroup-marked nil
6090               gnus-newsgroup-reads
6091               (nconc
6092                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6093                gnus-newsgroup-reads))
6094       (while (setq article (pop articles))
6095         (unless (or (memq article gnus-newsgroup-dormant)
6096                     (memq article gnus-newsgroup-marked))
6097           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6098
6099 (defun gnus-summary-limit (articles &optional pop)
6100   (if pop
6101       ;; We pop the previous limit off the stack and use that.
6102       (setq articles (car gnus-newsgroup-limits)
6103             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6104     ;; We use the new limit, so we push the old limit on the stack.
6105     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6106   ;; Set the limit.
6107   (setq gnus-newsgroup-limit articles)
6108   (let ((total (length gnus-newsgroup-data))
6109         (data (gnus-data-find-list (gnus-summary-article-number)))
6110         (gnus-summary-mark-below nil)   ; Inhibit this.
6111         found)
6112     ;; This will do all the work of generating the new summary buffer
6113     ;; according to the new limit.
6114     (gnus-summary-prepare)
6115     ;; Hide any threads, possibly.
6116     (and gnus-show-threads
6117          gnus-thread-hide-subtree
6118          (gnus-summary-hide-all-threads))
6119     ;; Try to return to the article you were at, or one in the
6120     ;; neighborhood.
6121     (when data
6122       ;; We try to find some article after the current one.
6123       (while data
6124         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6125           (setq data nil
6126                 found t))
6127         (setq data (cdr data))))
6128     (unless found
6129       ;; If there is no data, that means that we were after the last
6130       ;; article.  The same goes when we can't find any articles
6131       ;; after the current one.
6132       (goto-char (point-max))
6133       (gnus-summary-find-prev))
6134     ;; We return how many articles were removed from the summary
6135     ;; buffer as a result of the new limit.
6136     (- total (length gnus-newsgroup-data))))
6137
6138 (defsubst gnus-invisible-cut-children (threads)
6139   (let ((num 0))
6140     (while threads
6141       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6142         (incf num))
6143       (pop threads))
6144     (< num 2)))
6145
6146 (defsubst gnus-cut-thread (thread)
6147   "Go forwards in the thread until we find an article that we want to display."
6148   (when (or (eq gnus-fetch-old-headers 'some)
6149             (eq gnus-fetch-old-headers 'invisible)          
6150             (eq gnus-build-sparse-threads 'some)
6151             (eq gnus-build-sparse-threads 'more))
6152     ;; Deal with old-fetched headers and sparse threads.
6153     (while (and
6154             thread
6155             (or
6156              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6157              (gnus-summary-article-ancient-p
6158               (mail-header-number (car thread))))
6159             (if (or (<= (length (cdr thread)) 1)
6160                     (eq gnus-fetch-old-headers 'invisible))
6161                 (setq gnus-newsgroup-limit
6162                       (delq (mail-header-number (car thread))
6163                             gnus-newsgroup-limit)
6164                       thread (cadr thread))
6165               (when (gnus-invisible-cut-children (cdr thread))
6166                 (let ((th (cdr thread)))
6167                   (while th
6168                     (if (memq (mail-header-number (caar th))
6169                               gnus-newsgroup-limit)
6170                         (setq thread (car th)
6171                               th nil)
6172                       (setq th (cdr th))))))))))
6173   thread)
6174
6175 (defun gnus-cut-threads (threads)
6176   "Cut off all uninteresting articles from the beginning of threads."
6177   (when (or (eq gnus-fetch-old-headers 'some)
6178             (eq gnus-fetch-old-headers 'invisible)
6179             (eq gnus-build-sparse-threads 'some)
6180             (eq gnus-build-sparse-threads 'more))
6181     (let ((th threads))
6182       (while th
6183         (setcar th (gnus-cut-thread (car th)))
6184         (setq th (cdr th)))))
6185   ;; Remove nixed out threads.
6186   (delq nil threads))
6187
6188 (defun gnus-summary-initial-limit (&optional show-if-empty)
6189   "Figure out what the initial limit is supposed to be on group entry.
6190 This entails weeding out unwanted dormants, low-scored articles,
6191 fetch-old-headers verbiage, and so on."
6192   ;; Most groups have nothing to remove.
6193   (if (or gnus-inhibit-limiting
6194           (and (null gnus-newsgroup-dormant)
6195                (not (eq gnus-fetch-old-headers 'some))
6196                (not (eq gnus-fetch-old-headers 'invisible))
6197                (null gnus-summary-expunge-below)
6198                (not (eq gnus-build-sparse-threads 'some))
6199                (not (eq gnus-build-sparse-threads 'more))
6200                (null gnus-thread-expunge-below)
6201                (not gnus-use-nocem)))
6202       ()                                ; Do nothing.
6203     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6204     (setq gnus-newsgroup-limit nil)
6205     (mapatoms
6206      (lambda (node)
6207        (unless (car (symbol-value node))
6208          ;; These threads have no parents -- they are roots.
6209          (let ((nodes (cdr (symbol-value node)))
6210                thread)
6211            (while nodes
6212              (if (and gnus-thread-expunge-below
6213                       (< (gnus-thread-total-score (car nodes))
6214                          gnus-thread-expunge-below))
6215                  (gnus-expunge-thread (pop nodes))
6216                (setq thread (pop nodes))
6217                (gnus-summary-limit-children thread))))))
6218      gnus-newsgroup-dependencies)
6219     ;; If this limitation resulted in an empty group, we might
6220     ;; pop the previous limit and use it instead.
6221     (when (and (not gnus-newsgroup-limit)
6222                show-if-empty)
6223       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6224     gnus-newsgroup-limit))
6225
6226 (defun gnus-summary-limit-children (thread)
6227   "Return 1 if this subthread is visible and 0 if it is not."
6228   ;; First we get the number of visible children to this thread.  This
6229   ;; is done by recursing down the thread using this function, so this
6230   ;; will really go down to a leaf article first, before slowly
6231   ;; working its way up towards the root.
6232   (when thread
6233     (let ((children
6234            (if (cdr thread)
6235                (apply '+ (mapcar 'gnus-summary-limit-children
6236                                  (cdr thread)))
6237              0))
6238           (number (mail-header-number (car thread)))
6239           score)
6240       (if (and
6241            (not (memq number gnus-newsgroup-marked))
6242            (or
6243             ;; If this article is dormant and has absolutely no visible
6244             ;; children, then this article isn't visible.
6245             (and (memq number gnus-newsgroup-dormant)
6246                  (zerop children))
6247             ;; If this is "fetch-old-headered" and there is no
6248             ;; visible children, then we don't want this article.
6249             (and (eq gnus-fetch-old-headers 'some)
6250                  (gnus-summary-article-ancient-p number)
6251                  (zerop children))
6252             ;; If this is "fetch-old-headered" and `invisible', then
6253             ;; we don't want this article.
6254             (and (eq gnus-fetch-old-headers 'invisible)
6255                  (gnus-summary-article-ancient-p number))
6256             ;; If this is a sparsely inserted article with no children,
6257             ;; we don't want it.
6258             (and (eq gnus-build-sparse-threads 'some)
6259                  (gnus-summary-article-sparse-p number)
6260                  (zerop children))
6261             ;; If we use expunging, and this article is really
6262             ;; low-scored, then we don't want this article.
6263             (when (and gnus-summary-expunge-below
6264                        (< (setq score
6265                                 (or (cdr (assq number gnus-newsgroup-scored))
6266                                     gnus-summary-default-score))
6267                           gnus-summary-expunge-below))
6268               ;; We increase the expunge-tally here, but that has
6269               ;; nothing to do with the limits, really.
6270               (incf gnus-newsgroup-expunged-tally)
6271               ;; We also mark as read here, if that's wanted.
6272               (when (and gnus-summary-mark-below
6273                          (< score gnus-summary-mark-below))
6274                 (setq gnus-newsgroup-unreads
6275                       (delq number gnus-newsgroup-unreads))
6276                 (if gnus-newsgroup-auto-expire
6277                     (push number gnus-newsgroup-expirable)
6278                   (push (cons number gnus-low-score-mark)
6279                         gnus-newsgroup-reads)))
6280               t)
6281             ;; Check NoCeM things.
6282             (if (and gnus-use-nocem
6283                      (gnus-nocem-unwanted-article-p
6284                       (mail-header-id (car thread))))
6285                 (progn
6286                   (setq gnus-newsgroup-unreads
6287                         (delq number gnus-newsgroup-unreads))
6288                   t))))
6289           ;; Nope, invisible article.
6290           0
6291         ;; Ok, this article is to be visible, so we add it to the limit
6292         ;; and return 1.
6293         (push number gnus-newsgroup-limit)
6294         1))))
6295
6296 (defun gnus-expunge-thread (thread)
6297   "Mark all articles in THREAD as read."
6298   (let* ((number (mail-header-number (car thread))))
6299     (incf gnus-newsgroup-expunged-tally)
6300     ;; We also mark as read here, if that's wanted.
6301     (setq gnus-newsgroup-unreads
6302           (delq number gnus-newsgroup-unreads))
6303     (if gnus-newsgroup-auto-expire
6304         (push number gnus-newsgroup-expirable)
6305       (push (cons number gnus-low-score-mark)
6306             gnus-newsgroup-reads)))
6307   ;; Go recursively through all subthreads.
6308   (mapcar 'gnus-expunge-thread (cdr thread)))
6309
6310 ;; Summary article oriented commands
6311
6312 (defun gnus-summary-refer-parent-article (n)
6313   "Refer parent article N times.
6314 If N is negative, go to ancestor -N instead.
6315 The difference between N and the number of articles fetched is returned."
6316   (interactive "p")
6317   (let ((skip 1)
6318         error header ref)
6319     (when (not (natnump n))
6320       (setq skip (abs n)
6321             n 1))
6322     (while (and (> n 0)
6323                 (not error))
6324       (setq header (gnus-summary-article-header))
6325       (if (and (eq (mail-header-number header)
6326                    (cdr gnus-article-current))
6327                (equal gnus-newsgroup-name
6328                       (car gnus-article-current)))
6329           ;; If we try to find the parent of the currently
6330           ;; displayed article, then we take a look at the actual
6331           ;; References header, since this is slightly more
6332           ;; reliable than the References field we got from the
6333           ;; server.
6334           (save-excursion
6335             (set-buffer gnus-original-article-buffer)
6336             (nnheader-narrow-to-headers)
6337             (unless (setq ref (message-fetch-field "references"))
6338               (setq ref (message-fetch-field "in-reply-to")))
6339             (widen))
6340         (setq ref
6341               ;; It's not the current article, so we take a bet on
6342               ;; the value we got from the server.
6343               (mail-header-references header)))
6344       (if (and ref
6345                (not (equal ref "")))
6346           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6347             (gnus-message 1 "Couldn't find parent"))
6348         (gnus-message 1 "No references in article %d"
6349                       (gnus-summary-article-number))
6350         (setq error t))
6351       (decf n))
6352     (gnus-summary-position-point)
6353     n))
6354
6355 (defun gnus-summary-refer-references ()
6356   "Fetch all articles mentioned in the References header.
6357 Return the number of articles fetched."
6358   (interactive)
6359   (let ((ref (mail-header-references (gnus-summary-article-header)))
6360         (current (gnus-summary-article-number))
6361         (n 0))
6362     (if (or (not ref)
6363             (equal ref ""))
6364         (error "No References in the current article")
6365       ;; For each Message-ID in the References header...
6366       (while (string-match "<[^>]*>" ref)
6367         (incf n)
6368         ;; ... fetch that article.
6369         (gnus-summary-refer-article
6370          (prog1 (match-string 0 ref)
6371            (setq ref (substring ref (match-end 0))))))
6372       (gnus-summary-goto-subject current)
6373       (gnus-summary-position-point)
6374       n)))
6375
6376 (defun gnus-summary-refer-thread (&optional limit)
6377   "Fetch all articles in the current thread.
6378 If LIMIT (the numerical prefix), fetch that many old headers instead
6379 of what's specified by the `gnus-refer-thread-limit' variable."
6380   (interactive "P")
6381   (let ((id (mail-header-id (gnus-summary-article-header)))
6382         (limit (if limit (prefix-numeric-value limit)
6383                  gnus-refer-thread-limit))
6384         fmethod root)
6385     ;; We want to fetch LIMIT *old* headers, but we also have to
6386     ;; re-fetch all the headers in the current buffer, because many of
6387     ;; them may be undisplayed.  So we adjust LIMIT.
6388     (when (numberp limit)
6389       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6390     (unless (eq gnus-fetch-old-headers 'invisible)
6391       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6392       ;; Retrieve the headers and read them in.
6393       (if (eq (gnus-retrieve-headers
6394                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6395               'nov)
6396           (gnus-build-all-threads)
6397         (error "Can't fetch thread from backends that don't support NOV"))
6398       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6399     (gnus-summary-limit-include-thread id)))
6400
6401 (defun gnus-summary-refer-article (message-id &optional arg)
6402   "Fetch an article specified by MESSAGE-ID.
6403 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6404 or `gnus-select-method', no matter what backend the article comes from."
6405   (interactive "sMessage-ID: \nP")
6406   (when (and (stringp message-id)
6407              (not (zerop (length message-id))))
6408     ;; Construct the correct Message-ID if necessary.
6409     ;; Suggested by tale@pawl.rpi.edu.
6410     (unless (string-match "^<" message-id)
6411       (setq message-id (concat "<" message-id)))
6412     (unless (string-match ">$" message-id)
6413       (setq message-id (concat message-id ">")))
6414     (let* ((header (gnus-id-to-header message-id))
6415            (sparse (and header
6416                         (gnus-summary-article-sparse-p
6417                          (mail-header-number header))
6418                         (memq (mail-header-number header)
6419                               gnus-newsgroup-limit)))
6420            h)
6421       (cond
6422        ;; If the article is present in the buffer we just go to it.
6423        ((and header
6424              (or (not (gnus-summary-article-sparse-p
6425                        (mail-header-number header)))
6426                  sparse))
6427         (prog1
6428             (gnus-summary-goto-article
6429              (mail-header-number header) nil t)
6430           (when sparse
6431             (gnus-summary-update-article (mail-header-number header)))))
6432        (t
6433         ;; We fetch the article
6434         (let ((gnus-override-method
6435                (cond ((gnus-news-group-p gnus-newsgroup-name)
6436                       gnus-refer-article-method)
6437                      (arg
6438                       (or gnus-refer-article-method gnus-select-method))
6439                      (t nil)))
6440               number)
6441           ;; Start the special refer-article method, if necessary.
6442           (when (and gnus-refer-article-method
6443                      (gnus-news-group-p gnus-newsgroup-name))
6444             (gnus-check-server gnus-refer-article-method))
6445           ;; Fetch the header, and display the article.
6446           (if (setq number (gnus-summary-insert-subject message-id))
6447               (gnus-summary-select-article nil nil nil number)
6448             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6449
6450 (defun gnus-summary-edit-parameters ()
6451   "Edit the group parameters of the current group."
6452   (gnus-group-edit-group gnus-newsgroup-name 'params))
6453
6454 (defun gnus-summary-enter-digest-group (&optional force)
6455   "Enter an nndoc group based on the current article.
6456 If FORCE, force a digest interpretation.  If not, try
6457 to guess what the document format is."
6458   (interactive "P")
6459   (let ((conf gnus-current-window-configuration))
6460     (save-excursion
6461       (gnus-summary-select-article))
6462     (setq gnus-current-window-configuration conf)
6463     (let* ((name (format "%s-%d"
6464                          (gnus-group-prefixed-name
6465                           gnus-newsgroup-name (list 'nndoc ""))
6466                          (save-excursion
6467                            (set-buffer gnus-summary-buffer)
6468                            gnus-current-article)))
6469            (ogroup gnus-newsgroup-name)
6470            (params (append (gnus-info-params (gnus-get-info ogroup))
6471                            (list (cons 'to-group ogroup))
6472                            (list (cons 'save-article-group ogroup))))
6473            (case-fold-search t)
6474            (buf (current-buffer))
6475            dig)
6476       (save-excursion
6477         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6478         (insert-buffer-substring gnus-original-article-buffer)
6479         ;; Remove lines that may lead nndoc to misinterpret the
6480         ;; document type.
6481         (narrow-to-region
6482          (goto-char (point-min))
6483          (or (search-forward "\n\n" nil t) (point)))
6484         (goto-char (point-min))
6485         (delete-matching-lines "^\\(Path\\):\\|^From ")
6486         (widen))
6487       (unwind-protect
6488           (if (gnus-group-read-ephemeral-group
6489                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6490                             (nndoc-article-type
6491                              ,(if force 'digest 'guess))) t)
6492               ;; Make all postings to this group go to the parent group.
6493               (nconc (gnus-info-params (gnus-get-info name))
6494                      params)
6495             ;; Couldn't select this doc group.
6496             (switch-to-buffer buf)
6497             (gnus-set-global-variables)
6498             (gnus-configure-windows 'summary)
6499             (gnus-message 3 "Article couldn't be entered?"))
6500         (kill-buffer dig)))))
6501
6502 (defun gnus-summary-read-document (n)
6503   "Open a new group based on the current article(s).
6504 This will allow you to read digests and other similar
6505 documents as newsgroups.
6506 Obeys the standard process/prefix convention."
6507   (interactive "P")
6508   (let* ((articles (gnus-summary-work-articles n))
6509          (ogroup gnus-newsgroup-name)
6510          (params (append (gnus-info-params (gnus-get-info ogroup))
6511                          (list (cons 'to-group ogroup))))
6512          article group egroup groups vgroup)
6513     (while (setq article (pop articles))
6514       (setq group (format "%s-%d" gnus-newsgroup-name article))
6515       (gnus-summary-remove-process-mark article)
6516       (when (gnus-summary-display-article article)
6517         (save-excursion
6518           (nnheader-temp-write nil
6519             (insert-buffer-substring gnus-original-article-buffer)
6520             ;; Remove some headers that may lead nndoc to make
6521             ;; the wrong guess.
6522             (message-narrow-to-head)
6523             (goto-char (point-min))
6524             (delete-matching-lines "^\\(Path\\):\\|^From ")
6525             (widen)
6526             (if (setq egroup
6527                       (gnus-group-read-ephemeral-group
6528                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6529                                      (nndoc-article-type guess))
6530                        t nil t))
6531                 (progn
6532                   ;; Make all postings to this group go to the parent group.
6533                   (nconc (gnus-info-params (gnus-get-info egroup))
6534                          params)
6535                   (push egroup groups))
6536               ;; Couldn't select this doc group.
6537               (gnus-error 3 "Article couldn't be entered"))))))
6538     ;; Now we have selected all the documents.
6539     (cond
6540      ((not groups)
6541       (error "None of the articles could be interpreted as documents"))
6542      ((gnus-group-read-ephemeral-group
6543        (setq vgroup (format
6544                      "nnvirtual:%s-%s" gnus-newsgroup-name
6545                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6546        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6547        t
6548        (cons (current-buffer) 'summary)))
6549      (t
6550       (error "Couldn't select virtual nndoc group")))))
6551
6552 (defun gnus-summary-isearch-article (&optional regexp-p)
6553   "Do incremental search forward on the current article.
6554 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6555   (interactive "P")
6556   (gnus-summary-select-article)
6557   (gnus-configure-windows 'article)
6558   (gnus-eval-in-buffer-window gnus-article-buffer
6559     (save-restriction
6560       (widen)
6561       (isearch-forward regexp-p))))
6562
6563 (defun gnus-summary-search-article-forward (regexp &optional backward)
6564   "Search for an article containing REGEXP forward.
6565 If BACKWARD, search backward instead."
6566   (interactive
6567    (list (read-string
6568           (format "Search article %s (regexp%s): "
6569                   (if current-prefix-arg "backward" "forward")
6570                   (if gnus-last-search-regexp
6571                       (concat ", default " gnus-last-search-regexp)
6572                     "")))
6573          current-prefix-arg))
6574   (if (string-equal regexp "")
6575       (setq regexp (or gnus-last-search-regexp ""))
6576     (setq gnus-last-search-regexp regexp))
6577   (if (gnus-summary-search-article regexp backward)
6578       (gnus-summary-show-thread)
6579     (error "Search failed: \"%s\"" regexp)))
6580
6581 (defun gnus-summary-search-article-backward (regexp)
6582   "Search for an article containing REGEXP backward."
6583   (interactive
6584    (list (read-string
6585           (format "Search article backward (regexp%s): "
6586                   (if gnus-last-search-regexp
6587                       (concat ", default " gnus-last-search-regexp)
6588                     "")))))
6589   (gnus-summary-search-article-forward regexp 'backward))
6590
6591 (defun gnus-summary-search-article (regexp &optional backward)
6592   "Search for an article containing REGEXP.
6593 Optional argument BACKWARD means do search for backward.
6594 `gnus-select-article-hook' is not called during the search."
6595   ;; We have to require this here to make sure that the following
6596   ;; dynamic binding isn't shadowed by autoloading.
6597   (require 'gnus-async)
6598   (let ((gnus-select-article-hook nil)  ;Disable hook.
6599         (gnus-article-display-hook nil)
6600         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6601         (gnus-use-article-prefetch nil)
6602         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6603         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6604         (sum (current-buffer))
6605         (found nil)
6606         point)
6607     (gnus-save-hidden-threads
6608       (gnus-summary-select-article)
6609       (set-buffer gnus-article-buffer)
6610       (when backward
6611         (forward-line -1))
6612       (while (not found)
6613         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6614         (if (if backward
6615                 (re-search-backward regexp nil t)
6616               (re-search-forward regexp nil t))
6617             ;; We found the regexp.
6618             (progn
6619               (setq found 'found)
6620               (beginning-of-line)
6621               (set-window-start
6622                (get-buffer-window (current-buffer))
6623                (point))
6624               (forward-line 1)
6625               (set-buffer sum)
6626               (setq point (point)))
6627           ;; We didn't find it, so we go to the next article.
6628           (set-buffer sum)
6629           (setq found 'not)
6630           (while (eq found 'not)
6631             (if (not (if backward (gnus-summary-find-prev)
6632                        (gnus-summary-find-next)))
6633                 ;; No more articles.
6634                 (setq found t)
6635               ;; Select the next article and adjust point.
6636               (unless (gnus-summary-article-sparse-p
6637                        (gnus-summary-article-number))
6638                 (setq found nil)
6639                 (gnus-summary-select-article)
6640                 (set-buffer gnus-article-buffer)
6641                 (widen)
6642                 (goto-char (if backward (point-max) (point-min))))))))
6643       (gnus-message 7 ""))
6644     ;; Return whether we found the regexp.
6645     (when (eq found 'found)
6646       (goto-char point)
6647       (gnus-summary-show-thread)
6648       (gnus-summary-goto-subject gnus-current-article)
6649       (gnus-summary-position-point)
6650       t)))
6651
6652 (defun gnus-summary-find-matching (header regexp &optional backward unread
6653                                           not-case-fold)
6654   "Return a list of all articles that match REGEXP on HEADER.
6655 The search stars on the current article and goes forwards unless
6656 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6657 If UNREAD is non-nil, only unread articles will
6658 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6659 in the comparisons."
6660   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6661                 (gnus-data-find-list
6662                  (gnus-summary-article-number) (gnus-data-list backward))))
6663         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6664         (case-fold-search (not not-case-fold))
6665         articles d)
6666     (unless (fboundp (intern (concat "mail-header-" header)))
6667       (error "%s is not a valid header" header))
6668     (while data
6669       (setq d (car data))
6670       (and (or (not unread)             ; We want all articles...
6671                (gnus-data-unread-p d))  ; Or just unreads.
6672            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6673            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6674            (push (gnus-data-number d) articles)) ; Success!
6675       (setq data (cdr data)))
6676     (nreverse articles)))
6677
6678 (defun gnus-summary-execute-command (header regexp command &optional backward)
6679   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6680 If HEADER is an empty string (or nil), the match is done on the entire
6681 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6682   (interactive
6683    (list (let ((completion-ignore-case t))
6684            (completing-read
6685             "Header name: "
6686             (mapcar (lambda (string) (list string))
6687                     '("Number" "Subject" "From" "Lines" "Date"
6688                       "Message-ID" "Xref" "References" "Body"))
6689             nil 'require-match))
6690          (read-string "Regexp: ")
6691          (read-key-sequence "Command: ")
6692          current-prefix-arg))
6693   (when (equal header "Body")
6694     (setq header ""))
6695   ;; Hidden thread subtrees must be searched as well.
6696   (gnus-summary-show-all-threads)
6697   ;; We don't want to change current point nor window configuration.
6698   (save-excursion
6699     (save-window-excursion
6700       (gnus-message 6 "Executing %s..." (key-description command))
6701       ;; We'd like to execute COMMAND interactively so as to give arguments.
6702       (gnus-execute header regexp
6703                     `(call-interactively ',(key-binding command))
6704                     backward)
6705       (gnus-message 6 "Executing %s...done" (key-description command)))))
6706
6707 (defun gnus-summary-beginning-of-article ()
6708   "Scroll the article back to the beginning."
6709   (interactive)
6710   (gnus-summary-select-article)
6711   (gnus-configure-windows 'article)
6712   (gnus-eval-in-buffer-window gnus-article-buffer
6713     (widen)
6714     (goto-char (point-min))
6715     (when gnus-page-broken
6716       (gnus-narrow-to-page))))
6717
6718 (defun gnus-summary-end-of-article ()
6719   "Scroll to the end of the article."
6720   (interactive)
6721   (gnus-summary-select-article)
6722   (gnus-configure-windows 'article)
6723   (gnus-eval-in-buffer-window gnus-article-buffer
6724     (widen)
6725     (goto-char (point-max))
6726     (recenter -3)
6727     (when gnus-page-broken
6728       (gnus-narrow-to-page))))
6729
6730 (defun gnus-summary-print-article (&optional filename n)
6731   "Generate and print a PostScript image of the N next (mail) articles.
6732
6733 If N is negative, print the N previous articles.  If N is nil and articles
6734 have been marked with the process mark, print these instead.
6735
6736 If the optional second argument FILENAME is nil, send the image to the
6737 printer.  If FILENAME is a string, save the PostScript image in a file with
6738 that name.  If FILENAME is a number, prompt the user for the name of the file
6739 to save in."
6740   (interactive (list (ps-print-preprint current-prefix-arg)
6741                      current-prefix-arg))
6742   (dolist (article (gnus-summary-work-articles n))
6743     (gnus-summary-select-article nil nil 'pseudo article)
6744     (gnus-eval-in-buffer-window gnus-article-buffer
6745       (let ((buffer (generate-new-buffer " *print*")))
6746         (unwind-protect
6747             (progn
6748               (copy-to-buffer buffer (point-min) (point-max))
6749               (set-buffer buffer)
6750               (gnus-article-delete-invisible-text)
6751               (let ((ps-left-header
6752                      (list 
6753                       (concat "("
6754                               (mail-header-subject gnus-current-headers) ")")
6755                       (concat "("
6756                               (mail-header-from gnus-current-headers) ")")))
6757                     (ps-right-header 
6758                      (list 
6759                       "/pagenumberstring load" 
6760                       (concat "("
6761                               (mail-header-date gnus-current-headers) ")"))))
6762                 (gnus-run-hooks 'gnus-ps-print-hook)
6763                 (ps-print-buffer-with-faces filename)))
6764           (kill-buffer buffer))))))
6765
6766 (defun gnus-summary-show-article (&optional arg)
6767   "Force re-fetching of the current article.
6768 If ARG (the prefix) is non-nil, show the raw article without any
6769 article massaging functions being run."
6770   (interactive "P")
6771   (if (not arg)
6772       ;; Select the article the normal way.
6773       (gnus-summary-select-article nil 'force)
6774     ;; Bind the article treatment functions to nil.
6775     (let ((gnus-have-all-headers t)
6776           gnus-article-display-hook
6777           gnus-article-prepare-hook
6778           gnus-break-pages
6779           gnus-show-mime
6780           gnus-visual)
6781       (gnus-summary-select-article nil 'force)))
6782   (gnus-summary-goto-subject gnus-current-article)
6783   (gnus-summary-position-point))
6784
6785 (defun gnus-summary-verbose-headers (&optional arg)
6786   "Toggle permanent full header display.
6787 If ARG is a positive number, turn header display on.
6788 If ARG is a negative number, turn header display off."
6789   (interactive "P")
6790   (setq gnus-show-all-headers
6791         (cond ((or (not (numberp arg))
6792                    (zerop arg))
6793                (not gnus-show-all-headers))
6794               ((natnump arg)
6795                t)))
6796   (gnus-summary-show-article))
6797
6798 (defun gnus-summary-toggle-header (&optional arg)
6799   "Show the headers if they are hidden, or hide them if they are shown.
6800 If ARG is a positive number, show the entire header.
6801 If ARG is a negative number, hide the unwanted header lines."
6802   (interactive "P")
6803   (save-excursion
6804     (set-buffer gnus-article-buffer)
6805     (let* ((buffer-read-only nil)
6806            (inhibit-point-motion-hooks t)
6807            (hidden (text-property-any
6808                     (goto-char (point-min)) (search-forward "\n\n")
6809                     'invisible t))
6810            e)
6811       (goto-char (point-min))
6812       (when (search-forward "\n\n" nil t)
6813         (delete-region (point-min) (1- (point))))
6814       (goto-char (point-min))
6815       (save-excursion
6816         (set-buffer gnus-original-article-buffer)
6817         (goto-char (point-min))
6818         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6819       (insert-buffer-substring gnus-original-article-buffer 1 e)
6820       (let ((article-inhibit-hiding t))
6821         (gnus-run-hooks 'gnus-article-display-hook))
6822       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6823         (gnus-article-hide-headers)))))
6824
6825 (defun gnus-summary-show-all-headers ()
6826   "Make all header lines visible."
6827   (interactive)
6828   (gnus-article-show-all-headers))
6829
6830 (defun gnus-summary-toggle-mime (&optional arg)
6831   "Toggle MIME processing.
6832 If ARG is a positive number, turn MIME processing on."
6833   (interactive "P")
6834   (setq gnus-show-mime
6835         (if (null arg) (not gnus-show-mime)
6836           (> (prefix-numeric-value arg) 0)))
6837   (gnus-summary-select-article t 'force))
6838
6839 (defun gnus-summary-caesar-message (&optional arg)
6840   "Caesar rotate the current article by 13.
6841 The numerical prefix specifies how many places to rotate each letter
6842 forward."
6843   (interactive "P")
6844   (gnus-summary-select-article)
6845   (let ((mail-header-separator ""))
6846     (gnus-eval-in-buffer-window gnus-article-buffer
6847       (save-restriction
6848         (widen)
6849         (let ((start (window-start))
6850               buffer-read-only)
6851           (message-caesar-buffer-body arg)
6852           (set-window-start (get-buffer-window (current-buffer)) start))))))
6853
6854 (defun gnus-summary-stop-page-breaking ()
6855   "Stop page breaking in the current article."
6856   (interactive)
6857   (gnus-summary-select-article)
6858   (gnus-eval-in-buffer-window gnus-article-buffer
6859     (widen)
6860     (when (gnus-visual-p 'page-marker)
6861       (let ((buffer-read-only nil))
6862         (gnus-remove-text-with-property 'gnus-prev)
6863         (gnus-remove-text-with-property 'gnus-next))
6864       (setq gnus-page-broken nil))))
6865
6866 (defun gnus-summary-move-article (&optional n to-newsgroup
6867                                             select-method action)
6868   "Move the current article to a different newsgroup.
6869 If N is a positive number, move the N next articles.
6870 If N is a negative number, move the N previous articles.
6871 If N is nil and any articles have been marked with the process mark,
6872 move those articles instead.
6873 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6874 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6875 re-spool using this method.
6876
6877 For this function to work, both the current newsgroup and the
6878 newsgroup that you want to move to have to support the `request-move'
6879 and `request-accept' functions."
6880   (interactive "P")
6881   (unless action
6882     (setq action 'move))
6883   ;; Disable marking as read.
6884   (let (gnus-mark-article-hook)
6885     (save-window-excursion
6886       (gnus-summary-select-article)))
6887   ;; Check whether the source group supports the required functions.
6888   (cond ((and (eq action 'move)
6889               (not (gnus-check-backend-function
6890                     'request-move-article gnus-newsgroup-name)))
6891          (error "The current group does not support article moving"))
6892         ((and (eq action 'crosspost)
6893               (not (gnus-check-backend-function
6894                     'request-replace-article gnus-newsgroup-name)))
6895          (error "The current group does not support article editing")))
6896   (let ((articles (gnus-summary-work-articles n))
6897         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6898         (names '((move "Move" "Moving")
6899                  (copy "Copy" "Copying")
6900                  (crosspost "Crosspost" "Crossposting")))
6901         (copy-buf (save-excursion
6902                     (nnheader-set-temp-buffer " *copy article*")))
6903         art-group to-method new-xref article to-groups)
6904     (unless (assq action names)
6905       (error "Unknown action %s" action))
6906     ;; Read the newsgroup name.
6907     (when (and (not to-newsgroup)
6908                (not select-method))
6909       (setq to-newsgroup
6910             (gnus-read-move-group-name
6911              (cadr (assq action names))
6912              (symbol-value (intern (format "gnus-current-%s-group" action)))
6913              articles prefix))
6914       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6915     (setq to-method (or select-method
6916                         (gnus-group-name-to-method to-newsgroup)))
6917     ;; Check the method we are to move this article to...
6918     (unless (gnus-check-backend-function
6919              'request-accept-article (car to-method))
6920       (error "%s does not support article copying" (car to-method)))
6921     (unless (gnus-check-server to-method)
6922       (error "Can't open server %s" (car to-method)))
6923     (gnus-message 6 "%s to %s: %s..."
6924                   (caddr (assq action names))
6925                   (or (car select-method) to-newsgroup) articles)
6926     (while articles
6927       (setq article (pop articles))
6928       (setq
6929        art-group
6930        (cond
6931         ;; Move the article.
6932         ((eq action 'move)
6933          ;; Remove this article from future suppression.
6934          (gnus-dup-unsuppress-article article)
6935          (gnus-request-move-article
6936           article                       ; Article to move
6937           gnus-newsgroup-name           ; From newsgroup
6938           (nth 1 (gnus-find-method-for-group
6939                   gnus-newsgroup-name)) ; Server
6940           (list 'gnus-request-accept-article
6941                 to-newsgroup (list 'quote select-method)
6942                 (not articles))         ; Accept form
6943           (not articles)))              ; Only save nov last time
6944         ;; Copy the article.
6945         ((eq action 'copy)
6946          (save-excursion
6947            (set-buffer copy-buf)
6948            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6949              (gnus-request-accept-article
6950               to-newsgroup select-method (not articles)))))
6951         ;; Crosspost the article.
6952         ((eq action 'crosspost)
6953          (let ((xref (message-tokenize-header
6954                       (mail-header-xref (gnus-summary-article-header article))
6955                       " ")))
6956            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6957                                   ":" article))
6958            (unless xref
6959              (setq xref (list (system-name))))
6960            (setq new-xref
6961                  (concat
6962                   (mapconcat 'identity
6963                              (delete "Xref:" (delete new-xref xref))
6964                              " ")
6965                   " " new-xref))
6966            (save-excursion
6967              (set-buffer copy-buf)
6968              ;; First put the article in the destination group.
6969              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6970              (when (consp (setq art-group
6971                                 (gnus-request-accept-article
6972                                  to-newsgroup select-method (not articles))))
6973                (setq new-xref (concat new-xref " " (car art-group)
6974                                       ":" (cdr art-group)))
6975                ;; Now we have the new Xrefs header, so we insert
6976                ;; it and replace the new article.
6977                (nnheader-replace-header "Xref" new-xref)
6978                (gnus-request-replace-article
6979                 (cdr art-group) to-newsgroup (current-buffer))
6980                art-group))))))
6981       (cond
6982        ((not art-group)
6983         (gnus-message 1 "Couldn't %s article %s"
6984                       (cadr (assq action names)) article))
6985        ((and (eq art-group 'junk)
6986              (eq action 'move))
6987         (gnus-summary-mark-article article gnus-canceled-mark)
6988         (gnus-message 4 "Deleted article %s" article))
6989        (t
6990         (let* ((entry
6991                 (or
6992                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6993                  (gnus-gethash
6994                   (gnus-group-prefixed-name
6995                    (car art-group)
6996                    (or select-method
6997                        (gnus-find-method-for-group to-newsgroup)))
6998                   gnus-newsrc-hashtb)))
6999                (info (nth 2 entry))
7000                (to-group (gnus-info-group info)))
7001           ;; Update the group that has been moved to.
7002           (when (and info
7003                      (memq action '(move copy)))
7004             (unless (member to-group to-groups)
7005               (push to-group to-groups))
7006
7007             (unless (memq article gnus-newsgroup-unreads)
7008               (gnus-info-set-read
7009                info (gnus-add-to-range (gnus-info-read info)
7010                                        (list (cdr art-group)))))
7011
7012             ;; Copy any marks over to the new group.
7013             (let ((marks gnus-article-mark-lists)
7014                   (to-article (cdr art-group)))
7015
7016               ;; See whether the article is to be put in the cache.
7017               (when gnus-use-cache
7018                 (gnus-cache-possibly-enter-article
7019                  to-group to-article
7020                  (let ((header (copy-sequence
7021                                 (gnus-summary-article-header article))))
7022                    (mail-header-set-number header to-article)
7023                    header)
7024                  (memq article gnus-newsgroup-marked)
7025                  (memq article gnus-newsgroup-dormant)
7026                  (memq article gnus-newsgroup-unreads)))
7027
7028               (when (and (equal to-group gnus-newsgroup-name)
7029                          (not (memq article gnus-newsgroup-unreads)))
7030                 ;; Mark this article as read in this group.
7031                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7032                 (setcdr (gnus-active to-group) to-article)
7033                 (setcdr gnus-newsgroup-active to-article))
7034
7035               (while marks
7036                 (when (memq article (symbol-value
7037                                      (intern (format "gnus-newsgroup-%s"
7038                                                      (caar marks)))))
7039                   ;; If the other group is the same as this group,
7040                   ;; then we have to add the mark to the list.
7041                   (when (equal to-group gnus-newsgroup-name)
7042                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7043                          (cons to-article
7044                                (symbol-value
7045                                 (intern (format "gnus-newsgroup-%s"
7046                                                 (caar marks)))))))
7047                   ;; Copy the marks to other group.
7048                   (gnus-add-marked-articles
7049                    to-group (cdar marks) (list to-article) info))
7050                 (setq marks (cdr marks)))
7051
7052               (gnus-dribble-enter
7053                (concat "(gnus-group-set-info '"
7054                        (gnus-prin1-to-string (gnus-get-info to-group))
7055                        ")"))))
7056
7057           ;; Update the Xref header in this article to point to
7058           ;; the new crossposted article we have just created.
7059           (when (eq action 'crosspost)
7060             (save-excursion
7061               (set-buffer copy-buf)
7062               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7063               (nnheader-replace-header "Xref" new-xref)
7064               (gnus-request-replace-article
7065                article gnus-newsgroup-name (current-buffer)))))
7066
7067         ;;;!!!Why is this necessary?
7068         (set-buffer gnus-summary-buffer)
7069         
7070         (gnus-summary-goto-subject article)
7071         (when (eq action 'move)
7072           (gnus-summary-mark-article article gnus-canceled-mark))))
7073       (gnus-summary-remove-process-mark article))
7074     ;; Re-activate all groups that have been moved to.
7075     (while to-groups
7076       (save-excursion
7077         (set-buffer gnus-group-buffer)
7078         (when (gnus-group-goto-group (car to-groups) t)
7079           (gnus-group-get-new-news-this-group 1 t))
7080         (pop to-groups)))
7081
7082     (gnus-kill-buffer copy-buf)
7083     (gnus-summary-position-point)
7084     (gnus-set-mode-line 'summary)))
7085
7086 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7087   "Move the current article to a different newsgroup.
7088 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7089 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7090 re-spool using this method."
7091   (interactive "P")
7092   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7093
7094 (defun gnus-summary-crosspost-article (&optional n)
7095   "Crosspost the current article to some other group."
7096   (interactive "P")
7097   (gnus-summary-move-article n nil nil 'crosspost))
7098
7099 (defcustom gnus-summary-respool-default-method nil
7100   "*Default method for respooling an article.
7101 If nil, use to the current newsgroup method."
7102   :type `(choice (gnus-select-method :value (nnml ""))
7103                  (const nil))
7104   :group 'gnus-summary-mail)
7105
7106 (defun gnus-summary-respool-article (&optional n method)
7107   "Respool the current article.
7108 The article will be squeezed through the mail spooling process again,
7109 which means that it will be put in some mail newsgroup or other
7110 depending on `nnmail-split-methods'.
7111 If N is a positive number, respool the N next articles.
7112 If N is a negative number, respool the N previous articles.
7113 If N is nil and any articles have been marked with the process mark,
7114 respool those articles instead.
7115
7116 Respooling can be done both from mail groups and \"real\" newsgroups.
7117 In the former case, the articles in question will be moved from the
7118 current group into whatever groups they are destined to.  In the
7119 latter case, they will be copied into the relevant groups."
7120   (interactive
7121    (list current-prefix-arg
7122          (let* ((methods (gnus-methods-using 'respool))
7123                 (methname
7124                  (symbol-name (or gnus-summary-respool-default-method
7125                                   (car (gnus-find-method-for-group
7126                                         gnus-newsgroup-name)))))
7127                 (method
7128                  (gnus-completing-read
7129                   methname "What backend do you want to use when respooling?"
7130                   methods nil t nil 'gnus-mail-method-history))
7131                 ms)
7132            (cond
7133             ((zerop (length (setq ms (gnus-servers-using-backend
7134                                       (intern method)))))
7135              (list (intern method) ""))
7136             ((= 1 (length ms))
7137              (car ms))
7138             (t
7139              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7140                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7141                            ms-alist))))))))
7142   (unless method
7143     (error "No method given for respooling"))
7144   (if (assoc (symbol-name
7145               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7146              (gnus-methods-using 'respool))
7147       (gnus-summary-move-article n nil method)
7148     (gnus-summary-copy-article n nil method)))
7149
7150 (defun gnus-summary-import-article (file)
7151   "Import a random file into a mail newsgroup."
7152   (interactive "fImport file: ")
7153   (let ((group gnus-newsgroup-name)
7154         (now (current-time))
7155         atts lines)
7156     (unless (gnus-check-backend-function 'request-accept-article group)
7157       (error "%s does not support article importing" group))
7158     (or (file-readable-p file)
7159         (not (file-regular-p file))
7160         (error "Can't read %s" file))
7161     (save-excursion
7162       (set-buffer (get-buffer-create " *import file*"))
7163       (buffer-disable-undo (current-buffer))
7164       (erase-buffer)
7165       (insert-file-contents file)
7166       (goto-char (point-min))
7167       (unless (nnheader-article-p)
7168         ;; This doesn't look like an article, so we fudge some headers.
7169         (setq atts (file-attributes file)
7170               lines (count-lines (point-min) (point-max)))
7171         (insert "From: " (read-string "From: ") "\n"
7172                 "Subject: " (read-string "Subject: ") "\n"
7173                 "Date: " (timezone-make-date-arpa-standard
7174                           (current-time-string (nth 5 atts))
7175                           (current-time-zone now)
7176                           (current-time-zone now))
7177                 "\n"
7178                 "Message-ID: " (message-make-message-id) "\n"
7179                 "Lines: " (int-to-string lines) "\n"
7180                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7181       (gnus-request-accept-article group nil t)
7182       (kill-buffer (current-buffer)))))
7183
7184 (defun gnus-summary-article-posted-p ()
7185   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7186 This will be the case if the article has both been mailed and posted."
7187   (interactive)
7188   (let ((id (mail-header-references (gnus-summary-article-header)))
7189         (gnus-override-method
7190          (or gnus-refer-article-method gnus-select-method)))
7191     (if (gnus-request-head id "")
7192         (gnus-message 2 "The current message was found on %s"
7193                       gnus-override-method)
7194       (gnus-message 2 "The current message couldn't be found on %s"
7195                     gnus-override-method)
7196       nil)))
7197
7198 (defun gnus-summary-expire-articles (&optional now)
7199   "Expire all articles that are marked as expirable in the current group."
7200   (interactive)
7201   (when (gnus-check-backend-function
7202          'request-expire-articles gnus-newsgroup-name)
7203     ;; This backend supports expiry.
7204     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7205            (expirable (if total
7206                           (progn
7207                             ;; We need to update the info for
7208                             ;; this group for `gnus-list-of-read-articles'
7209                             ;; to give us the right answer.
7210                             (gnus-run-hooks 'gnus-exit-group-hook)
7211                             (gnus-summary-update-info)
7212                             (gnus-list-of-read-articles gnus-newsgroup-name))
7213                         (setq gnus-newsgroup-expirable
7214                               (sort gnus-newsgroup-expirable '<))))
7215            (expiry-wait (if now 'immediate
7216                           (gnus-group-find-parameter
7217                            gnus-newsgroup-name 'expiry-wait)))
7218            es)
7219       (when expirable
7220         ;; There are expirable articles in this group, so we run them
7221         ;; through the expiry process.
7222         (gnus-message 6 "Expiring articles...")
7223         ;; The list of articles that weren't expired is returned.
7224         (save-excursion
7225           (if expiry-wait
7226               (let ((nnmail-expiry-wait-function nil)
7227                     (nnmail-expiry-wait expiry-wait))
7228                 (setq es (gnus-request-expire-articles
7229                           expirable gnus-newsgroup-name)))
7230             (setq es (gnus-request-expire-articles
7231                       expirable gnus-newsgroup-name))))
7232         (unless total
7233           (setq gnus-newsgroup-expirable es))
7234         ;; We go through the old list of expirable, and mark all
7235         ;; really expired articles as nonexistent.
7236         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7237           (let ((gnus-use-cache nil))
7238             (while expirable
7239               (unless (memq (car expirable) es)
7240                 (when (gnus-data-find (car expirable))
7241                   (gnus-summary-mark-article
7242                    (car expirable) gnus-canceled-mark)))
7243               (setq expirable (cdr expirable)))))
7244         (gnus-message 6 "Expiring articles...done")))))
7245
7246 (defun gnus-summary-expire-articles-now ()
7247   "Expunge all expirable articles in the current group.
7248 This means that *all* articles that are marked as expirable will be
7249 deleted forever, right now."
7250   (interactive)
7251   (unless gnus-expert-user
7252     (gnus-yes-or-no-p
7253      "Are you really, really, really sure you want to delete all these messages? ")
7254     (error "Phew!"))
7255   (gnus-summary-expire-articles t))
7256
7257 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7258 (defun gnus-summary-delete-article (&optional n)
7259   "Delete the N next (mail) articles.
7260 This command actually deletes articles.  This is not a marking
7261 command.  The article will disappear forever from your life, never to
7262 return.
7263 If N is negative, delete backwards.
7264 If N is nil and articles have been marked with the process mark,
7265 delete these instead."
7266   (interactive "P")
7267   (unless (gnus-check-backend-function 'request-expire-articles
7268                                        gnus-newsgroup-name)
7269     (error "The current newsgroup does not support article deletion"))
7270   ;; Compute the list of articles to delete.
7271   (let ((articles (gnus-summary-work-articles n))
7272         not-deleted)
7273     (if (and gnus-novice-user
7274              (not (gnus-yes-or-no-p
7275                    (format "Do you really want to delete %s forever? "
7276                            (if (> (length articles) 1)
7277                                (format "these %s articles" (length articles))
7278                              "this article")))))
7279         ()
7280       ;; Delete the articles.
7281       (setq not-deleted (gnus-request-expire-articles
7282                          articles gnus-newsgroup-name 'force))
7283       (while articles
7284         (gnus-summary-remove-process-mark (car articles))
7285         ;; The backend might not have been able to delete the article
7286         ;; after all.
7287         (unless (memq (car articles) not-deleted)
7288           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7289         (setq articles (cdr articles)))
7290       (when not-deleted
7291         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7292     (gnus-summary-position-point)
7293     (gnus-set-mode-line 'summary)
7294     not-deleted))
7295
7296 (defun gnus-summary-edit-article (&optional force)
7297   "Edit the current article.
7298 This will have permanent effect only in mail groups.
7299 If FORCE is non-nil, allow editing of articles even in read-only
7300 groups."
7301   (interactive "P")
7302   (save-excursion
7303     (set-buffer gnus-summary-buffer)
7304     (gnus-set-global-variables)
7305     (when (and (not force)
7306                (gnus-group-read-only-p))
7307       (error "The current newsgroup does not support article editing"))
7308     ;; Select article if needed.
7309     (unless (eq (gnus-summary-article-number)
7310                 gnus-current-article)
7311       (gnus-summary-select-article t))
7312     (gnus-article-date-original)
7313     (gnus-article-edit-article
7314      `(lambda (no-highlight)
7315         (gnus-summary-edit-article-done
7316          ,(or (mail-header-references gnus-current-headers) "")
7317          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7318
7319 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7320
7321 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7322                                                  no-highlight)
7323   "Make edits to the current article permanent."
7324   (interactive)
7325   ;; Replace the article.
7326   (if (and (not read-only)
7327            (not (gnus-request-replace-article
7328                  (cdr gnus-article-current) (car gnus-article-current)
7329                  (current-buffer))))
7330       (error "Couldn't replace article")
7331     ;; Update the summary buffer.
7332     (if (and references
7333              (equal (message-tokenize-header references " ")
7334                     (message-tokenize-header
7335                      (or (message-fetch-field "references") "") " ")))
7336         ;; We only have to update this line.
7337         (save-excursion
7338           (save-restriction
7339             (message-narrow-to-head)
7340             (let ((head (buffer-string))
7341                   header)
7342               (nnheader-temp-write nil
7343                 (insert (format "211 %d Article retrieved.\n"
7344                                 (cdr gnus-article-current)))
7345                 (insert head)
7346                 (insert ".\n")
7347                 (let ((nntp-server-buffer (current-buffer)))
7348                   (setq header (car (gnus-get-newsgroup-headers
7349                                      (save-excursion
7350                                        (set-buffer gnus-summary-buffer)
7351                                        gnus-newsgroup-dependencies)
7352                                      t))))
7353                 (save-excursion
7354                   (set-buffer gnus-summary-buffer)
7355                   (gnus-data-set-header
7356                    (gnus-data-find (cdr gnus-article-current))
7357                    header)
7358                   (gnus-summary-update-article-line
7359                    (cdr gnus-article-current) header))))))
7360       ;; Update threads.
7361       (set-buffer (or buffer gnus-summary-buffer))
7362       (gnus-summary-update-article (cdr gnus-article-current)))
7363     ;; Prettify the article buffer again.
7364     (unless no-highlight
7365       (save-excursion
7366         (set-buffer gnus-article-buffer)
7367         (gnus-run-hooks 'gnus-article-display-hook)
7368         (set-buffer gnus-original-article-buffer)
7369         (gnus-request-article
7370          (cdr gnus-article-current)
7371          (car gnus-article-current) (current-buffer))))
7372     ;; Prettify the summary buffer line.
7373     (when (gnus-visual-p 'summary-highlight 'highlight)
7374       (gnus-run-hooks 'gnus-visual-mark-article-hook))))
7375
7376 (defun gnus-summary-edit-wash (key)
7377   "Perform editing command in the article buffer."
7378   (interactive
7379    (list
7380     (progn
7381       (message "%s" (concat (this-command-keys) "- "))
7382       (read-char))))
7383   (message "")
7384   (gnus-summary-edit-article)
7385   (execute-kbd-macro (concat (this-command-keys) key))
7386   (gnus-article-edit-done))
7387
7388 ;;; Respooling
7389
7390 (defun gnus-summary-respool-query (&optional silent)
7391   "Query where the respool algorithm would put this article."
7392   (interactive)
7393   (let (gnus-mark-article-hook)
7394     (gnus-summary-select-article)
7395     (save-excursion
7396       (set-buffer gnus-original-article-buffer)
7397       (save-restriction
7398         (message-narrow-to-head)
7399         (let ((groups (nnmail-article-group 'identity)))
7400           (unless silent
7401             (if groups
7402                 (message "This message would go to %s"
7403                          (mapconcat 'car groups ", "))
7404               (message "This message would go to no groups"))
7405             groups))))))
7406
7407 ;; Summary marking commands.
7408
7409 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7410   "Mark articles which has the same subject as read, and then select the next.
7411 If UNMARK is positive, remove any kind of mark.
7412 If UNMARK is negative, tick articles."
7413   (interactive "P")
7414   (when unmark
7415     (setq unmark (prefix-numeric-value unmark)))
7416   (let ((count
7417          (gnus-summary-mark-same-subject
7418           (gnus-summary-article-subject) unmark)))
7419     ;; Select next unread article.  If auto-select-same mode, should
7420     ;; select the first unread article.
7421     (gnus-summary-next-article t (and gnus-auto-select-same
7422                                       (gnus-summary-article-subject)))
7423     (gnus-message 7 "%d article%s marked as %s"
7424                   count (if (= count 1) " is" "s are")
7425                   (if unmark "unread" "read"))))
7426
7427 (defun gnus-summary-kill-same-subject (&optional unmark)
7428   "Mark articles which has the same subject as read.
7429 If UNMARK is positive, remove any kind of mark.
7430 If UNMARK is negative, tick articles."
7431   (interactive "P")
7432   (when unmark
7433     (setq unmark (prefix-numeric-value unmark)))
7434   (let ((count
7435          (gnus-summary-mark-same-subject
7436           (gnus-summary-article-subject) unmark)))
7437     ;; If marked as read, go to next unread subject.
7438     (when (null unmark)
7439       ;; Go to next unread subject.
7440       (gnus-summary-next-subject 1 t))
7441     (gnus-message 7 "%d articles are marked as %s"
7442                   count (if unmark "unread" "read"))))
7443
7444 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7445   "Mark articles with same SUBJECT as read, and return marked number.
7446 If optional argument UNMARK is positive, remove any kinds of marks.
7447 If optional argument UNMARK is negative, mark articles as unread instead."
7448   (let ((count 1))
7449     (save-excursion
7450       (cond
7451        ((null unmark)                   ; Mark as read.
7452         (while (and
7453                 (progn
7454                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7455                   (gnus-summary-show-thread) t)
7456                 (gnus-summary-find-subject subject))
7457           (setq count (1+ count))))
7458        ((> unmark 0)                    ; Tick.
7459         (while (and
7460                 (progn
7461                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7462                   (gnus-summary-show-thread) t)
7463                 (gnus-summary-find-subject subject))
7464           (setq count (1+ count))))
7465        (t                               ; Mark as unread.
7466         (while (and
7467                 (progn
7468                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7469                   (gnus-summary-show-thread) t)
7470                 (gnus-summary-find-subject subject))
7471           (setq count (1+ count)))))
7472       (gnus-set-mode-line 'summary)
7473       ;; Return the number of marked articles.
7474       count)))
7475
7476 (defun gnus-summary-mark-as-processable (n &optional unmark)
7477   "Set the process mark on the next N articles.
7478 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7479 the process mark instead.  The difference between N and the actual
7480 number of articles marked is returned."
7481   (interactive "p")
7482   (let ((backward (< n 0))
7483         (n (abs n)))
7484     (while (and
7485             (> n 0)
7486             (if unmark
7487                 (gnus-summary-remove-process-mark
7488                  (gnus-summary-article-number))
7489               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7490             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7491       (setq n (1- n)))
7492     (when (/= 0 n)
7493       (gnus-message 7 "No more articles"))
7494     (gnus-summary-recenter)
7495     (gnus-summary-position-point)
7496     n))
7497
7498 (defun gnus-summary-unmark-as-processable (n)
7499   "Remove the process mark from the next N articles.
7500 If N is negative, unmark backward instead.  The difference between N and
7501 the actual number of articles unmarked is returned."
7502   (interactive "p")
7503   (gnus-summary-mark-as-processable n t))
7504
7505 (defun gnus-summary-unmark-all-processable ()
7506   "Remove the process mark from all articles."
7507   (interactive)
7508   (save-excursion
7509     (while gnus-newsgroup-processable
7510       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7511   (gnus-summary-position-point))
7512
7513 (defun gnus-summary-mark-as-expirable (n)
7514   "Mark N articles forward as expirable.
7515 If N is negative, mark backward instead.  The difference between N and
7516 the actual number of articles marked is returned."
7517   (interactive "p")
7518   (gnus-summary-mark-forward n gnus-expirable-mark))
7519
7520 (defun gnus-summary-mark-article-as-replied (article)
7521   "Mark ARTICLE replied and update the summary line."
7522   (push article gnus-newsgroup-replied)
7523   (let ((buffer-read-only nil))
7524     (when (gnus-summary-goto-subject article)
7525       (gnus-summary-update-secondary-mark article))))
7526
7527 (defun gnus-summary-set-bookmark (article)
7528   "Set a bookmark in current article."
7529   (interactive (list (gnus-summary-article-number)))
7530   (when (or (not (get-buffer gnus-article-buffer))
7531             (not gnus-current-article)
7532             (not gnus-article-current)
7533             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7534     (error "No current article selected"))
7535   ;; Remove old bookmark, if one exists.
7536   (let ((old (assq article gnus-newsgroup-bookmarks)))
7537     (when old
7538       (setq gnus-newsgroup-bookmarks
7539             (delq old gnus-newsgroup-bookmarks))))
7540   ;; Set the new bookmark, which is on the form
7541   ;; (article-number . line-number-in-body).
7542   (push
7543    (cons article
7544          (save-excursion
7545            (set-buffer gnus-article-buffer)
7546            (count-lines
7547             (min (point)
7548                  (save-excursion
7549                    (goto-char (point-min))
7550                    (search-forward "\n\n" nil t)
7551                    (point)))
7552             (point))))
7553    gnus-newsgroup-bookmarks)
7554   (gnus-message 6 "A bookmark has been added to the current article."))
7555
7556 (defun gnus-summary-remove-bookmark (article)
7557   "Remove the bookmark from the current article."
7558   (interactive (list (gnus-summary-article-number)))
7559   ;; Remove old bookmark, if one exists.
7560   (let ((old (assq article gnus-newsgroup-bookmarks)))
7561     (if old
7562         (progn
7563           (setq gnus-newsgroup-bookmarks
7564                 (delq old gnus-newsgroup-bookmarks))
7565           (gnus-message 6 "Removed bookmark."))
7566       (gnus-message 6 "No bookmark in current article."))))
7567
7568 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7569 (defun gnus-summary-mark-as-dormant (n)
7570   "Mark N articles forward as dormant.
7571 If N is negative, mark backward instead.  The difference between N and
7572 the actual number of articles marked is returned."
7573   (interactive "p")
7574   (gnus-summary-mark-forward n gnus-dormant-mark))
7575
7576 (defun gnus-summary-set-process-mark (article)
7577   "Set the process mark on ARTICLE and update the summary line."
7578   (setq gnus-newsgroup-processable
7579         (cons article
7580               (delq article gnus-newsgroup-processable)))
7581   (when (gnus-summary-goto-subject article)
7582     (gnus-summary-show-thread)
7583     (gnus-summary-update-secondary-mark article)))
7584
7585 (defun gnus-summary-remove-process-mark (article)
7586   "Remove the process mark from ARTICLE and update the summary line."
7587   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7588   (when (gnus-summary-goto-subject article)
7589     (gnus-summary-show-thread)
7590     (gnus-summary-update-secondary-mark article)))
7591
7592 (defun gnus-summary-set-saved-mark (article)
7593   "Set the process mark on ARTICLE and update the summary line."
7594   (push article gnus-newsgroup-saved)
7595   (when (gnus-summary-goto-subject article)
7596     (gnus-summary-update-secondary-mark article)))
7597
7598 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7599   "Mark N articles as read forwards.
7600 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7601 The difference between N and the actual number of articles marked is
7602 returned."
7603   (interactive "p")
7604   (let ((backward (< n 0))
7605         (gnus-summary-goto-unread
7606          (and gnus-summary-goto-unread
7607               (not (eq gnus-summary-goto-unread 'never))
7608               (not (memq mark (list gnus-unread-mark
7609                                     gnus-ticked-mark gnus-dormant-mark)))))
7610         (n (abs n))
7611         (mark (or mark gnus-del-mark)))
7612     (while (and (> n 0)
7613                 (gnus-summary-mark-article nil mark no-expire)
7614                 (zerop (gnus-summary-next-subject
7615                         (if backward -1 1)
7616                         (and gnus-summary-goto-unread
7617                              (not (eq gnus-summary-goto-unread 'never)))
7618                         t)))
7619       (setq n (1- n)))
7620     (when (/= 0 n)
7621       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7622     (gnus-summary-recenter)
7623     (gnus-summary-position-point)
7624     (gnus-set-mode-line 'summary)
7625     n))
7626
7627 (defun gnus-summary-mark-article-as-read (mark)
7628   "Mark the current article quickly as read with MARK."
7629   (let ((article (gnus-summary-article-number)))
7630     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7631     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7632     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7633     (push (cons article mark) gnus-newsgroup-reads)
7634     ;; Possibly remove from cache, if that is used.
7635     (when gnus-use-cache
7636       (gnus-cache-enter-remove-article article))
7637     ;; Allow the backend to change the mark.
7638     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7639     ;; Check for auto-expiry.
7640     (when (and gnus-newsgroup-auto-expire
7641                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7642                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7643                    (= mark gnus-ancient-mark)
7644                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7645                    (= mark gnus-duplicate-mark)))
7646       (setq mark gnus-expirable-mark)
7647       (push article gnus-newsgroup-expirable))
7648     ;; Set the mark in the buffer.
7649     (gnus-summary-update-mark mark 'unread)
7650     t))
7651
7652 (defun gnus-summary-mark-article-as-unread (mark)
7653   "Mark the current article quickly as unread with MARK."
7654   (let* ((article (gnus-summary-article-number))
7655          (old-mark (gnus-summary-article-mark article)))
7656     (if (eq mark old-mark)
7657         t
7658       (if (<= article 0)
7659           (progn
7660             (gnus-error 1 "Can't mark negative article numbers")
7661             nil)
7662         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7663         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7664         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7665         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7666         (cond ((= mark gnus-ticked-mark)
7667                (push article gnus-newsgroup-marked))
7668               ((= mark gnus-dormant-mark)
7669                (push article gnus-newsgroup-dormant))
7670               (t
7671                (push article gnus-newsgroup-unreads)))
7672         (setq gnus-newsgroup-reads
7673               (delq (assq article gnus-newsgroup-reads)
7674                     gnus-newsgroup-reads))
7675
7676         ;; See whether the article is to be put in the cache.
7677         (and gnus-use-cache
7678              (vectorp (gnus-summary-article-header article))
7679              (save-excursion
7680                (gnus-cache-possibly-enter-article
7681                 gnus-newsgroup-name article
7682                 (gnus-summary-article-header article)
7683                 (= mark gnus-ticked-mark)
7684                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7685
7686         ;; Fix the mark.
7687         (gnus-summary-update-mark mark 'unread)
7688         t))))
7689
7690 (defun gnus-summary-mark-article (&optional article mark no-expire)
7691   "Mark ARTICLE with MARK.  MARK can be any character.
7692 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7693 `??' (dormant) and `?E' (expirable).
7694 If MARK is nil, then the default character `?D' is used.
7695 If ARTICLE is nil, then the article on the current line will be
7696 marked."
7697   ;; The mark might be a string.
7698   (when (stringp mark)
7699     (setq mark (aref mark 0)))
7700   ;; If no mark is given, then we check auto-expiring.
7701   (and (not no-expire)
7702        gnus-newsgroup-auto-expire
7703        (or (not mark)
7704            (and (gnus-characterp mark)
7705                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7706                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7707                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7708                     (= mark gnus-duplicate-mark))))
7709        (setq mark gnus-expirable-mark))
7710   (let* ((mark (or mark gnus-del-mark))
7711          (article (or article (gnus-summary-article-number)))
7712          (old-mark (gnus-summary-article-mark article)))
7713     (if (eq mark old-mark)
7714         t
7715       (unless article
7716         (error "No article on current line"))
7717       (if (not (if (or (= mark gnus-unread-mark)
7718                        (= mark gnus-ticked-mark)
7719                        (= mark gnus-dormant-mark))
7720                    (gnus-mark-article-as-unread article mark)
7721                  (gnus-mark-article-as-read article mark)))
7722           t
7723         ;; See whether the article is to be put in the cache.
7724         (and gnus-use-cache
7725              (not (= mark gnus-canceled-mark))
7726              (vectorp (gnus-summary-article-header article))
7727              (save-excursion
7728                (gnus-cache-possibly-enter-article
7729                 gnus-newsgroup-name article
7730                 (gnus-summary-article-header article)
7731                 (= mark gnus-ticked-mark)
7732                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7733
7734         (when (gnus-summary-goto-subject article nil t)
7735           (let ((buffer-read-only nil))
7736             (gnus-summary-show-thread)
7737             ;; Fix the mark.
7738             (gnus-summary-update-mark mark 'unread)
7739             t))))))
7740
7741 (defun gnus-summary-update-secondary-mark (article)
7742   "Update the secondary (read, process, cache) mark."
7743   (gnus-summary-update-mark
7744    (cond ((memq article gnus-newsgroup-processable)
7745           gnus-process-mark)
7746          ((memq article gnus-newsgroup-cached)
7747           gnus-cached-mark)
7748          ((memq article gnus-newsgroup-replied)
7749           gnus-replied-mark)
7750          ((memq article gnus-newsgroup-saved)
7751           gnus-saved-mark)
7752          (t gnus-unread-mark))
7753    'replied)
7754   (when (gnus-visual-p 'summary-highlight 'highlight)
7755     (gnus-run-hooks 'gnus-summary-update-hook))
7756   t)
7757
7758 (defun gnus-summary-update-mark (mark type)
7759   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7760         (buffer-read-only nil))
7761     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7762     (when (looking-at "\r")
7763       (incf forward))
7764     (when (and forward
7765                (<= (+ forward (point)) (point-max)))
7766       ;; Go to the right position on the line.
7767       (goto-char (+ forward (point)))
7768       ;; Replace the old mark with the new mark.
7769       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7770       ;; Optionally update the marks by some user rule.
7771       (when (eq type 'unread)
7772         (gnus-data-set-mark
7773          (gnus-data-find (gnus-summary-article-number)) mark)
7774         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7775
7776 (defun gnus-mark-article-as-read (article &optional mark)
7777   "Enter ARTICLE in the pertinent lists and remove it from others."
7778   ;; Make the article expirable.
7779   (let ((mark (or mark gnus-del-mark)))
7780     (if (= mark gnus-expirable-mark)
7781         (push article gnus-newsgroup-expirable)
7782       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7783     ;; Remove from unread and marked lists.
7784     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7785     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7786     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7787     (push (cons article mark) gnus-newsgroup-reads)
7788     ;; Possibly remove from cache, if that is used.
7789     (when gnus-use-cache
7790       (gnus-cache-enter-remove-article article))
7791     t))
7792
7793 (defun gnus-mark-article-as-unread (article &optional mark)
7794   "Enter ARTICLE in the pertinent lists and remove it from others."
7795   (let ((mark (or mark gnus-ticked-mark)))
7796     (if (<= article 0)
7797         (progn
7798           (gnus-error 1 "Can't mark negative article numbers")
7799           nil)
7800       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7801             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7802             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7803             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7804
7805       ;; Unsuppress duplicates?
7806       (when gnus-suppress-duplicates
7807         (gnus-dup-unsuppress-article article))
7808
7809       (cond ((= mark gnus-ticked-mark)
7810              (push article gnus-newsgroup-marked))
7811             ((= mark gnus-dormant-mark)
7812              (push article gnus-newsgroup-dormant))
7813             (t
7814              (push article gnus-newsgroup-unreads)))
7815       (setq gnus-newsgroup-reads
7816             (delq (assq article gnus-newsgroup-reads)
7817                   gnus-newsgroup-reads))
7818       t)))
7819
7820 (defalias 'gnus-summary-mark-as-unread-forward
7821   'gnus-summary-tick-article-forward)
7822 (make-obsolete 'gnus-summary-mark-as-unread-forward
7823                'gnus-summary-tick-article-forward)
7824 (defun gnus-summary-tick-article-forward (n)
7825   "Tick N articles forwards.
7826 If N is negative, tick backwards instead.
7827 The difference between N and the number of articles ticked is returned."
7828   (interactive "p")
7829   (gnus-summary-mark-forward n gnus-ticked-mark))
7830
7831 (defalias 'gnus-summary-mark-as-unread-backward
7832   'gnus-summary-tick-article-backward)
7833 (make-obsolete 'gnus-summary-mark-as-unread-backward
7834                'gnus-summary-tick-article-backward)
7835 (defun gnus-summary-tick-article-backward (n)
7836   "Tick N articles backwards.
7837 The difference between N and the number of articles ticked is returned."
7838   (interactive "p")
7839   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7840
7841 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7842 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7843 (defun gnus-summary-tick-article (&optional article clear-mark)
7844   "Mark current article as unread.
7845 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7846 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7847   (interactive)
7848   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7849                                        gnus-ticked-mark)))
7850
7851 (defun gnus-summary-mark-as-read-forward (n)
7852   "Mark N articles as read forwards.
7853 If N is negative, mark backwards instead.
7854 The difference between N and the actual number of articles marked is
7855 returned."
7856   (interactive "p")
7857   (gnus-summary-mark-forward n gnus-del-mark t))
7858
7859 (defun gnus-summary-mark-as-read-backward (n)
7860   "Mark the N articles as read backwards.
7861 The difference between N and the actual number of articles marked is
7862 returned."
7863   (interactive "p")
7864   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7865
7866 (defun gnus-summary-mark-as-read (&optional article mark)
7867   "Mark current article as read.
7868 ARTICLE specifies the article to be marked as read.
7869 MARK specifies a string to be inserted at the beginning of the line."
7870   (gnus-summary-mark-article article mark))
7871
7872 (defun gnus-summary-clear-mark-forward (n)
7873   "Clear marks from N articles forward.
7874 If N is negative, clear backward instead.
7875 The difference between N and the number of marks cleared is returned."
7876   (interactive "p")
7877   (gnus-summary-mark-forward n gnus-unread-mark))
7878
7879 (defun gnus-summary-clear-mark-backward (n)
7880   "Clear marks from N articles backward.
7881 The difference between N and the number of marks cleared is returned."
7882   (interactive "p")
7883   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7884
7885 (defun gnus-summary-mark-unread-as-read ()
7886   "Intended to be used by `gnus-summary-mark-article-hook'."
7887   (when (memq gnus-current-article gnus-newsgroup-unreads)
7888     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7889
7890 (defun gnus-summary-mark-read-and-unread-as-read ()
7891   "Intended to be used by `gnus-summary-mark-article-hook'."
7892   (let ((mark (gnus-summary-article-mark)))
7893     (when (or (gnus-unread-mark-p mark)
7894               (gnus-read-mark-p mark))
7895       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7896
7897 (defun gnus-summary-mark-region-as-read (point mark all)
7898   "Mark all unread articles between point and mark as read.
7899 If given a prefix, mark all articles between point and mark as read,
7900 even ticked and dormant ones."
7901   (interactive "r\nP")
7902   (save-excursion
7903     (let (article)
7904       (goto-char point)
7905       (beginning-of-line)
7906       (while (and
7907               (< (point) mark)
7908               (progn
7909                 (when (or all
7910                           (memq (setq article (gnus-summary-article-number))
7911                                 gnus-newsgroup-unreads))
7912                   (gnus-summary-mark-article article gnus-del-mark))
7913                 t)
7914               (gnus-summary-find-next))))))
7915
7916 (defun gnus-summary-mark-below (score mark)
7917   "Mark articles with score less than SCORE with MARK."
7918   (interactive "P\ncMark: ")
7919   (setq score (if score
7920                   (prefix-numeric-value score)
7921                 (or gnus-summary-default-score 0)))
7922   (save-excursion
7923     (set-buffer gnus-summary-buffer)
7924     (goto-char (point-min))
7925     (while
7926         (progn
7927           (and (< (gnus-summary-article-score) score)
7928                (gnus-summary-mark-article nil mark))
7929           (gnus-summary-find-next)))))
7930
7931 (defun gnus-summary-kill-below (&optional score)
7932   "Mark articles with score below SCORE as read."
7933   (interactive "P")
7934   (gnus-summary-mark-below score gnus-killed-mark))
7935
7936 (defun gnus-summary-clear-above (&optional score)
7937   "Clear all marks from articles with score above SCORE."
7938   (interactive "P")
7939   (gnus-summary-mark-above score gnus-unread-mark))
7940
7941 (defun gnus-summary-tick-above (&optional score)
7942   "Tick all articles with score above SCORE."
7943   (interactive "P")
7944   (gnus-summary-mark-above score gnus-ticked-mark))
7945
7946 (defun gnus-summary-mark-above (score mark)
7947   "Mark articles with score over SCORE with MARK."
7948   (interactive "P\ncMark: ")
7949   (setq score (if score
7950                   (prefix-numeric-value score)
7951                 (or gnus-summary-default-score 0)))
7952   (save-excursion
7953     (set-buffer gnus-summary-buffer)
7954     (goto-char (point-min))
7955     (while (and (progn
7956                   (when (> (gnus-summary-article-score) score)
7957                     (gnus-summary-mark-article nil mark))
7958                   t)
7959                 (gnus-summary-find-next)))))
7960
7961 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7962 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7963 (defun gnus-summary-limit-include-expunged (&optional no-error)
7964   "Display all the hidden articles that were expunged for low scores."
7965   (interactive)
7966   (let ((buffer-read-only nil))
7967     (let ((scored gnus-newsgroup-scored)
7968           headers h)
7969       (while scored
7970         (unless (gnus-summary-goto-subject (caar scored))
7971           (and (setq h (gnus-summary-article-header (caar scored)))
7972                (< (cdar scored) gnus-summary-expunge-below)
7973                (push h headers)))
7974         (setq scored (cdr scored)))
7975       (if (not headers)
7976           (when (not no-error)
7977             (error "No expunged articles hidden"))
7978         (goto-char (point-min))
7979         (gnus-summary-prepare-unthreaded (nreverse headers))
7980         (goto-char (point-min))
7981         (gnus-summary-position-point)
7982         t))))
7983
7984 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7985   "Mark all unread articles in this newsgroup as read.
7986 If prefix argument ALL is non-nil, ticked and dormant articles will
7987 also be marked as read.
7988 If QUIETLY is non-nil, no questions will be asked.
7989 If TO-HERE is non-nil, it should be a point in the buffer.  All
7990 articles before this point will be marked as read.
7991 Note that this function will only catch up the unread article
7992 in the current summary buffer limitation.
7993 The number of articles marked as read is returned."
7994   (interactive "P")
7995   (prog1
7996       (save-excursion
7997         (when (or quietly
7998                   (not gnus-interactive-catchup) ;Without confirmation?
7999                   gnus-expert-user
8000                   (gnus-y-or-n-p
8001                    (if all
8002                        "Mark absolutely all articles as read? "
8003                      "Mark all unread articles as read? ")))
8004           (if (and not-mark
8005                    (not gnus-newsgroup-adaptive)
8006                    (not gnus-newsgroup-auto-expire)
8007                    (not gnus-suppress-duplicates)
8008                    (or (not gnus-use-cache)
8009                        (eq gnus-use-cache 'passive)))
8010               (progn
8011                 (when all
8012                   (setq gnus-newsgroup-marked nil
8013                         gnus-newsgroup-dormant nil))
8014                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8015             ;; We actually mark all articles as canceled, which we
8016             ;; have to do when using auto-expiry or adaptive scoring.
8017             (gnus-summary-show-all-threads)
8018             (when (gnus-summary-first-subject (not all))
8019               (while (and
8020                       (if to-here (< (point) to-here) t)
8021                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8022                       (gnus-summary-find-next (not all)))))
8023             (gnus-set-mode-line 'summary))
8024           t))
8025     (gnus-summary-position-point)))
8026
8027 (defun gnus-summary-catchup-to-here (&optional all)
8028   "Mark all unticked articles before the current one as read.
8029 If ALL is non-nil, also mark ticked and dormant articles as read."
8030   (interactive "P")
8031   (save-excursion
8032     (gnus-save-hidden-threads
8033       (let ((beg (point)))
8034         ;; We check that there are unread articles.
8035         (when (or all (gnus-summary-find-prev))
8036           (gnus-summary-catchup all t beg)))))
8037   (gnus-summary-position-point))
8038
8039 (defun gnus-summary-catchup-all (&optional quietly)
8040   "Mark all articles in this newsgroup as read."
8041   (interactive "P")
8042   (gnus-summary-catchup t quietly))
8043
8044 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8045   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8046 If prefix argument ALL is non-nil, all articles are marked as read."
8047   (interactive "P")
8048   (when (gnus-summary-catchup all quietly nil 'fast)
8049     ;; Select next newsgroup or exit.
8050     (if (eq gnus-auto-select-next 'quietly)
8051         (gnus-summary-next-group nil)
8052       (gnus-summary-exit))))
8053
8054 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8055   "Mark all articles in this newsgroup as read, and then exit."
8056   (interactive "P")
8057   (gnus-summary-catchup-and-exit t quietly))
8058
8059 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8060 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8061   "Mark all articles in this group as read and select the next group.
8062 If given a prefix, mark all articles, unread as well as ticked, as
8063 read."
8064   (interactive "P")
8065   (save-excursion
8066     (gnus-summary-catchup all))
8067   (gnus-summary-next-article t nil nil t))
8068
8069 ;; Thread-based commands.
8070
8071 (defun gnus-summary-articles-in-thread (&optional article)
8072   "Return a list of all articles in the current thread.
8073 If ARTICLE is non-nil, return all articles in the thread that starts
8074 with that article."
8075   (let* ((article (or article (gnus-summary-article-number)))
8076          (data (gnus-data-find-list article))
8077          (top-level (gnus-data-level (car data)))
8078          (top-subject
8079           (cond ((null gnus-thread-operation-ignore-subject)
8080                  (gnus-simplify-subject-re
8081                   (mail-header-subject (gnus-data-header (car data)))))
8082                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8083                  (gnus-simplify-subject-fuzzy
8084                   (mail-header-subject (gnus-data-header (car data)))))
8085                 (t nil)))
8086          (end-point (save-excursion
8087                       (if (gnus-summary-go-to-next-thread)
8088                           (point) (point-max))))
8089          articles)
8090     (while (and data
8091                 (< (gnus-data-pos (car data)) end-point))
8092       (when (or (not top-subject)
8093                 (string= top-subject
8094                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8095                              (gnus-simplify-subject-fuzzy
8096                               (mail-header-subject
8097                                (gnus-data-header (car data))))
8098                            (gnus-simplify-subject-re
8099                             (mail-header-subject
8100                              (gnus-data-header (car data)))))))
8101         (push (gnus-data-number (car data)) articles))
8102       (unless (and (setq data (cdr data))
8103                    (> (gnus-data-level (car data)) top-level))
8104         (setq data nil)))
8105     ;; Return the list of articles.
8106     (nreverse articles)))
8107
8108 (defun gnus-summary-rethread-current ()
8109   "Rethread the thread the current article is part of."
8110   (interactive)
8111   (let* ((gnus-show-threads t)
8112          (article (gnus-summary-article-number))
8113          (id (mail-header-id (gnus-summary-article-header)))
8114          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8115     (unless id
8116       (error "No article on the current line"))
8117     (gnus-rebuild-thread id)
8118     (gnus-summary-goto-subject article)))
8119
8120 (defun gnus-summary-reparent-thread ()
8121   "Make the current article child of the marked (or previous) article.
8122
8123 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8124 is non-nil or the Subject: of both articles are the same."
8125   (interactive)
8126   (unless (not (gnus-group-read-only-p))
8127     (error "The current newsgroup does not support article editing"))
8128   (unless (<= (length gnus-newsgroup-processable) 1)
8129     (error "No more than one article may be marked"))
8130   (save-window-excursion
8131     (let ((gnus-article-buffer " *reparent*")
8132           (current-article (gnus-summary-article-number))
8133           ;; First grab the marked article, otherwise one line up.
8134           (parent-article (if (not (null gnus-newsgroup-processable))
8135                               (car gnus-newsgroup-processable)
8136                             (save-excursion
8137                               (if (eq (forward-line -1) 0)
8138                                   (gnus-summary-article-number)
8139                                 (error "Beginning of summary buffer"))))))
8140       (unless (not (eq current-article parent-article))
8141         (error "An article may not be self-referential"))
8142       (let ((message-id (mail-header-id
8143                          (gnus-summary-article-header parent-article))))
8144         (unless (and message-id (not (equal message-id "")))
8145           (error "No message-id in desired parent"))
8146         ;; We don't want the article to be marked as read.
8147         (let (gnus-mark-article-hook)
8148           (gnus-summary-select-article t t nil current-article))
8149         (set-buffer gnus-original-article-buffer)
8150         (let ((buf (format "%s" (buffer-string))))
8151           (nnheader-temp-write nil
8152             (insert buf)
8153             (goto-char (point-min))
8154             (if (re-search-forward "^References: " nil t)
8155                 (progn
8156                   (re-search-forward "^[^ \t]" nil t)
8157                   (forward-line -1)
8158                   (end-of-line)
8159                   (insert " " message-id))
8160               (insert "References: " message-id "\n"))
8161             (unless (gnus-request-replace-article
8162                      current-article (car gnus-article-current)
8163                      (current-buffer))
8164               (error "Couldn't replace article"))))
8165         (set-buffer gnus-summary-buffer)
8166         (gnus-summary-unmark-all-processable)
8167         (gnus-summary-update-article current-article)
8168         (gnus-summary-rethread-current)
8169         (gnus-message 3 "Article %d is now the child of article %d"
8170                       current-article parent-article)))))
8171
8172 (defun gnus-summary-toggle-threads (&optional arg)
8173   "Toggle showing conversation threads.
8174 If ARG is positive number, turn showing conversation threads on."
8175   (interactive "P")
8176   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8177     (setq gnus-show-threads
8178           (if (null arg) (not gnus-show-threads)
8179             (> (prefix-numeric-value arg) 0)))
8180     (gnus-summary-prepare)
8181     (gnus-summary-goto-subject current)
8182     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8183     (gnus-summary-position-point)))
8184
8185 (defun gnus-summary-show-all-threads ()
8186   "Show all threads."
8187   (interactive)
8188   (save-excursion
8189     (let ((buffer-read-only nil))
8190       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8191   (gnus-summary-position-point))
8192
8193 (defun gnus-summary-show-thread ()
8194   "Show thread subtrees.
8195 Returns nil if no thread was there to be shown."
8196   (interactive)
8197   (let ((buffer-read-only nil)
8198         (orig (point))
8199         ;; first goto end then to beg, to have point at beg after let
8200         (end (progn (end-of-line) (point)))
8201         (beg (progn (beginning-of-line) (point))))
8202     (prog1
8203         ;; Any hidden lines here?
8204         (search-forward "\r" end t)
8205       (subst-char-in-region beg end ?\^M ?\n t)
8206       (goto-char orig)
8207       (gnus-summary-position-point))))
8208
8209 (defun gnus-summary-hide-all-threads ()
8210   "Hide all thread subtrees."
8211   (interactive)
8212   (save-excursion
8213     (goto-char (point-min))
8214     (gnus-summary-hide-thread)
8215     (while (zerop (gnus-summary-next-thread 1 t))
8216       (gnus-summary-hide-thread)))
8217   (gnus-summary-position-point))
8218
8219 (defun gnus-summary-hide-thread ()
8220   "Hide thread subtrees.
8221 Returns nil if no threads were there to be hidden."
8222   (interactive)
8223   (let ((buffer-read-only nil)
8224         (start (point))
8225         (article (gnus-summary-article-number)))
8226     (goto-char start)
8227     ;; Go forward until either the buffer ends or the subthread
8228     ;; ends.
8229     (when (and (not (eobp))
8230                (or (zerop (gnus-summary-next-thread 1 t))
8231                    (goto-char (point-max))))
8232       (prog1
8233           (if (and (> (point) start)
8234                    (search-backward "\n" start t))
8235               (progn
8236                 (subst-char-in-region start (point) ?\n ?\^M)
8237                 (gnus-summary-goto-subject article))
8238             (goto-char start)
8239             nil)
8240         ;;(gnus-summary-position-point)
8241         ))))
8242
8243 (defun gnus-summary-go-to-next-thread (&optional previous)
8244   "Go to the same level (or less) next thread.
8245 If PREVIOUS is non-nil, go to previous thread instead.
8246 Return the article number moved to, or nil if moving was impossible."
8247   (let ((level (gnus-summary-thread-level))
8248         (way (if previous -1 1))
8249         (beg (point)))
8250     (forward-line way)
8251     (while (and (not (eobp))
8252                 (< level (gnus-summary-thread-level)))
8253       (forward-line way))
8254     (if (eobp)
8255         (progn
8256           (goto-char beg)
8257           nil)
8258       (setq beg (point))
8259       (prog1
8260           (gnus-summary-article-number)
8261         (goto-char beg)))))
8262
8263 (defun gnus-summary-next-thread (n &optional silent)
8264   "Go to the same level next N'th thread.
8265 If N is negative, search backward instead.
8266 Returns the difference between N and the number of skips actually
8267 done.
8268
8269 If SILENT, don't output messages."
8270   (interactive "p")
8271   (let ((backward (< n 0))
8272         (n (abs n)))
8273     (while (and (> n 0)
8274                 (gnus-summary-go-to-next-thread backward))
8275       (decf n))
8276     (unless silent
8277       (gnus-summary-position-point))
8278     (when (and (not silent) (/= 0 n))
8279       (gnus-message 7 "No more threads"))
8280     n))
8281
8282 (defun gnus-summary-prev-thread (n)
8283   "Go to the same level previous N'th thread.
8284 Returns the difference between N and the number of skips actually
8285 done."
8286   (interactive "p")
8287   (gnus-summary-next-thread (- n)))
8288
8289 (defun gnus-summary-go-down-thread ()
8290   "Go down one level in the current thread."
8291   (let ((children (gnus-summary-article-children)))
8292     (when children
8293       (gnus-summary-goto-subject (car children)))))
8294
8295 (defun gnus-summary-go-up-thread ()
8296   "Go up one level in the current thread."
8297   (let ((parent (gnus-summary-article-parent)))
8298     (when parent
8299       (gnus-summary-goto-subject parent))))
8300
8301 (defun gnus-summary-down-thread (n)
8302   "Go down thread N steps.
8303 If N is negative, go up instead.
8304 Returns the difference between N and how many steps down that were
8305 taken."
8306   (interactive "p")
8307   (let ((up (< n 0))
8308         (n (abs n)))
8309     (while (and (> n 0)
8310                 (if up (gnus-summary-go-up-thread)
8311                   (gnus-summary-go-down-thread)))
8312       (setq n (1- n)))
8313     (gnus-summary-position-point)
8314     (when (/= 0 n)
8315       (gnus-message 7 "Can't go further"))
8316     n))
8317
8318 (defun gnus-summary-up-thread (n)
8319   "Go up thread N steps.
8320 If N is negative, go up instead.
8321 Returns the difference between N and how many steps down that were
8322 taken."
8323   (interactive "p")
8324   (gnus-summary-down-thread (- n)))
8325
8326 (defun gnus-summary-top-thread ()
8327   "Go to the top of the thread."
8328   (interactive)
8329   (while (gnus-summary-go-up-thread))
8330   (gnus-summary-article-number))
8331
8332 (defun gnus-summary-kill-thread (&optional unmark)
8333   "Mark articles under current thread as read.
8334 If the prefix argument is positive, remove any kinds of marks.
8335 If the prefix argument is negative, tick articles instead."
8336   (interactive "P")
8337   (when unmark
8338     (setq unmark (prefix-numeric-value unmark)))
8339   (let ((articles (gnus-summary-articles-in-thread)))
8340     (save-excursion
8341       ;; Expand the thread.
8342       (gnus-summary-show-thread)
8343       ;; Mark all the articles.
8344       (while articles
8345         (gnus-summary-goto-subject (car articles))
8346         (cond ((null unmark)
8347                (gnus-summary-mark-article-as-read gnus-killed-mark))
8348               ((> unmark 0)
8349                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8350               (t
8351                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8352         (setq articles (cdr articles))))
8353     ;; Hide killed subtrees.
8354     (and (null unmark)
8355          gnus-thread-hide-killed
8356          (gnus-summary-hide-thread))
8357     ;; If marked as read, go to next unread subject.
8358     (when (null unmark)
8359       ;; Go to next unread subject.
8360       (gnus-summary-next-subject 1 t)))
8361   (gnus-set-mode-line 'summary))
8362
8363 ;; Summary sorting commands
8364
8365 (defun gnus-summary-sort-by-number (&optional reverse)
8366   "Sort the summary buffer by article number.
8367 Argument REVERSE means reverse order."
8368   (interactive "P")
8369   (gnus-summary-sort 'number reverse))
8370
8371 (defun gnus-summary-sort-by-author (&optional reverse)
8372   "Sort the summary buffer by author name alphabetically.
8373 If case-fold-search is non-nil, case of letters is ignored.
8374 Argument REVERSE means reverse order."
8375   (interactive "P")
8376   (gnus-summary-sort 'author reverse))
8377
8378 (defun gnus-summary-sort-by-subject (&optional reverse)
8379   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8380 If case-fold-search is non-nil, case of letters is ignored.
8381 Argument REVERSE means reverse order."
8382   (interactive "P")
8383   (gnus-summary-sort 'subject reverse))
8384
8385 (defun gnus-summary-sort-by-date (&optional reverse)
8386   "Sort the summary buffer by date.
8387 Argument REVERSE means reverse order."
8388   (interactive "P")
8389   (gnus-summary-sort 'date reverse))
8390
8391 (defun gnus-summary-sort-by-score (&optional reverse)
8392   "Sort the summary buffer by score.
8393 Argument REVERSE means reverse order."
8394   (interactive "P")
8395   (gnus-summary-sort 'score reverse))
8396
8397 (defun gnus-summary-sort-by-lines (&optional reverse)
8398   "Sort the summary buffer by article length.
8399 Argument REVERSE means reverse order."
8400   (interactive "P")
8401   (gnus-summary-sort 'lines reverse))
8402
8403 (defun gnus-summary-sort (predicate reverse)
8404   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8405   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8406          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8407          (gnus-thread-sort-functions
8408           (list
8409            (if (not reverse)
8410                thread
8411              `(lambda (t1 t2)
8412                 (,thread t2 t1)))))
8413          (gnus-article-sort-functions
8414           (list
8415            (if (not reverse)
8416                article
8417              `(lambda (t1 t2)
8418                 (,article t2 t1)))))
8419          (buffer-read-only)
8420          (gnus-summary-prepare-hook nil))
8421     ;; We do the sorting by regenerating the threads.
8422     (gnus-summary-prepare)
8423     ;; Hide subthreads if needed.
8424     (when (and gnus-show-threads gnus-thread-hide-subtree)
8425       (gnus-summary-hide-all-threads))))
8426
8427 ;; Summary saving commands.
8428
8429 (defun gnus-summary-save-article (&optional n not-saved)
8430   "Save the current article using the default saver function.
8431 If N is a positive number, save the N next articles.
8432 If N is a negative number, save the N previous articles.
8433 If N is nil and any articles have been marked with the process mark,
8434 save those articles instead.
8435 The variable `gnus-default-article-saver' specifies the saver function."
8436   (interactive "P")
8437   (let* ((articles (gnus-summary-work-articles n))
8438          (save-buffer (save-excursion
8439                         (nnheader-set-temp-buffer " *Gnus Save*")))
8440          (num (length articles))
8441          header article file)
8442     (while articles
8443       (setq header (gnus-summary-article-header
8444                     (setq article (pop articles))))
8445       (if (not (vectorp header))
8446           ;; This is a pseudo-article.
8447           (if (assq 'name header)
8448               (gnus-copy-file (cdr (assq 'name header)))
8449             (gnus-message 1 "Article %d is unsaveable" article))
8450         ;; This is a real article.
8451         (save-window-excursion
8452           (gnus-summary-select-article t nil nil article))
8453         (save-excursion
8454           (set-buffer save-buffer)
8455           (erase-buffer)
8456           (insert-buffer-substring gnus-original-article-buffer))
8457         (setq file (gnus-article-save save-buffer file num))
8458         (gnus-summary-remove-process-mark article)
8459         (unless not-saved
8460           (gnus-summary-set-saved-mark article))))
8461     (gnus-kill-buffer save-buffer)
8462     (gnus-summary-position-point)
8463     (gnus-set-mode-line 'summary)
8464     n))
8465
8466 (defun gnus-summary-pipe-output (&optional arg)
8467   "Pipe the current article to a subprocess.
8468 If N is a positive number, pipe the N next articles.
8469 If N is a negative number, pipe the N previous articles.
8470 If N is nil and any articles have been marked with the process mark,
8471 pipe those articles instead."
8472   (interactive "P")
8473   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8474     (gnus-summary-save-article arg t))
8475   (gnus-configure-windows 'pipe))
8476
8477 (defun gnus-summary-save-article-mail (&optional arg)
8478   "Append the current article to an mail file.
8479 If N is a positive number, save the N next articles.
8480 If N is a negative number, save the N previous articles.
8481 If N is nil and any articles have been marked with the process mark,
8482 save those articles instead."
8483   (interactive "P")
8484   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8485     (gnus-summary-save-article arg)))
8486
8487 (defun gnus-summary-save-article-rmail (&optional arg)
8488   "Append the current article to an rmail file.
8489 If N is a positive number, save the N next articles.
8490 If N is a negative number, save the N previous articles.
8491 If N is nil and any articles have been marked with the process mark,
8492 save those articles instead."
8493   (interactive "P")
8494   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8495     (gnus-summary-save-article arg)))
8496
8497 (defun gnus-summary-save-article-file (&optional arg)
8498   "Append the current article to a file.
8499 If N is a positive number, save the N next articles.
8500 If N is a negative number, save the N previous articles.
8501 If N is nil and any articles have been marked with the process mark,
8502 save those articles instead."
8503   (interactive "P")
8504   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8505     (gnus-summary-save-article arg)))
8506
8507 (defun gnus-summary-write-article-file (&optional arg)
8508   "Write the current article to a file, deleting the previous file.
8509 If N is a positive number, save the N next articles.
8510 If N is a negative number, save the N previous articles.
8511 If N is nil and any articles have been marked with the process mark,
8512 save those articles instead."
8513   (interactive "P")
8514   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8515     (gnus-summary-save-article arg)))
8516
8517 (defun gnus-summary-save-article-body-file (&optional arg)
8518   "Append the current article body to a file.
8519 If N is a positive number, save the N next articles.
8520 If N is a negative number, save the N previous articles.
8521 If N is nil and any articles have been marked with the process mark,
8522 save those articles instead."
8523   (interactive "P")
8524   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8525     (gnus-summary-save-article arg)))
8526
8527 (defun gnus-summary-pipe-message (program)
8528   "Pipe the current article through PROGRAM."
8529   (interactive "sProgram: ")
8530   (gnus-summary-select-article)
8531   (let ((mail-header-separator "")
8532         (art-buf (get-buffer gnus-article-buffer)))
8533     (gnus-eval-in-buffer-window gnus-article-buffer
8534       (save-restriction
8535         (widen)
8536         (let ((start (window-start))
8537               buffer-read-only)
8538           (message-pipe-buffer-body program)
8539           (set-window-start (get-buffer-window (current-buffer)) start))))))
8540
8541 (defun gnus-get-split-value (methods)
8542   "Return a value based on the split METHODS."
8543   (let (split-name method result match)
8544     (when methods
8545       (save-excursion
8546         (set-buffer gnus-original-article-buffer)
8547         (save-restriction
8548           (nnheader-narrow-to-headers)
8549           (while methods
8550             (goto-char (point-min))
8551             (setq method (pop methods))
8552             (setq match (car method))
8553             (when (cond
8554                    ((stringp match)
8555                     ;; Regular expression.
8556                     (ignore-errors
8557                       (re-search-forward match nil t)))
8558                    ((gnus-functionp match)
8559                     ;; Function.
8560                     (save-restriction
8561                       (widen)
8562                       (setq result (funcall match gnus-newsgroup-name))))
8563                    ((consp match)
8564                     ;; Form.
8565                     (save-restriction
8566                       (widen)
8567                       (setq result (eval match)))))
8568               (setq split-name (append (cdr method) split-name))
8569               (cond ((stringp result)
8570                      (push (expand-file-name
8571                             result gnus-article-save-directory)
8572                            split-name))
8573                     ((consp result)
8574                      (setq split-name (append result split-name)))))))))
8575     split-name))
8576
8577 (defun gnus-valid-move-group-p (group)
8578   (and (boundp group)
8579        (symbol-name group)
8580        (memq 'respool
8581              (assoc (symbol-name
8582                      (car (gnus-find-method-for-group
8583                            (symbol-name group))))
8584                     gnus-valid-select-methods))))
8585
8586 (defun gnus-read-move-group-name (prompt default articles prefix)
8587   "Read a group name."
8588   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8589          (minibuffer-confirm-incomplete nil) ; XEmacs
8590          (prom
8591           (format "%s %s to:"
8592                   prompt
8593                   (if (> (length articles) 1)
8594                       (format "these %d articles" (length articles))
8595                     "this article")))
8596          (to-newsgroup
8597           (cond
8598            ((null split-name)
8599             (gnus-completing-read default prom
8600                                   gnus-active-hashtb
8601                                   'gnus-valid-move-group-p
8602                                   nil prefix
8603                                   'gnus-group-history))
8604            ((= 1 (length split-name))
8605             (gnus-completing-read (car split-name) prom
8606                                   gnus-active-hashtb
8607                                   'gnus-valid-move-group-p
8608                                   nil nil
8609                                   'gnus-group-history))
8610            (t
8611             (gnus-completing-read nil prom
8612                                   (mapcar (lambda (el) (list el))
8613                                           (nreverse split-name))
8614                                   nil nil nil
8615                                   'gnus-group-history)))))
8616     (when to-newsgroup
8617       (if (or (string= to-newsgroup "")
8618               (string= to-newsgroup prefix))
8619           (setq to-newsgroup default))
8620       (unless to-newsgroup
8621         (error "No group name entered"))
8622       (or (gnus-active to-newsgroup)
8623           (gnus-activate-group to-newsgroup)
8624           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8625                                      to-newsgroup))
8626               (or (and (gnus-request-create-group
8627                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8628                        (gnus-activate-group to-newsgroup nil nil
8629                                             (gnus-group-name-to-method
8630                                              to-newsgroup)))
8631                   (error "Couldn't create group %s" to-newsgroup)))
8632           (error "No such group: %s" to-newsgroup)))
8633     to-newsgroup))
8634
8635 ;; Summary extract commands
8636
8637 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8638   (let ((buffer-read-only nil)
8639         (article (gnus-summary-article-number))
8640         after-article b e)
8641     (unless (gnus-summary-goto-subject article)
8642       (error "No such article: %d" article))
8643     (gnus-summary-position-point)
8644     ;; If all commands are to be bunched up on one line, we collect
8645     ;; them here.
8646     (unless gnus-view-pseudos-separately
8647       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8648             files action)
8649         (while ps
8650           (setq action (cdr (assq 'action (car ps))))
8651           (setq files (list (cdr (assq 'name (car ps)))))
8652           (while (and ps (cdr ps)
8653                       (string= (or action "1")
8654                                (or (cdr (assq 'action (cadr ps))) "2")))
8655             (push (cdr (assq 'name (cadr ps))) files)
8656             (setcdr ps (cddr ps)))
8657           (when files
8658             (when (not (string-match "%s" action))
8659               (push " " files))
8660             (push " " files)
8661             (when (assq 'execute (car ps))
8662               (setcdr (assq 'execute (car ps))
8663                       (funcall (if (string-match "%s" action)
8664                                    'format 'concat)
8665                                action
8666                                (mapconcat
8667                                 (lambda (f)
8668                                   (if (equal f " ")
8669                                       f
8670                                     (gnus-quote-arg-for-sh-or-csh f)))
8671                                 files " ")))))
8672           (setq ps (cdr ps)))))
8673     (if (and gnus-view-pseudos (not not-view))
8674         (while pslist
8675           (when (assq 'execute (car pslist))
8676             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8677                                   (eq gnus-view-pseudos 'not-confirm)))
8678           (setq pslist (cdr pslist)))
8679       (save-excursion
8680         (while pslist
8681           (setq after-article (or (cdr (assq 'article (car pslist)))
8682                                   (gnus-summary-article-number)))
8683           (gnus-summary-goto-subject after-article)
8684           (forward-line 1)
8685           (setq b (point))
8686           (insert "    " (file-name-nondirectory
8687                           (cdr (assq 'name (car pslist))))
8688                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8689           (setq e (point))
8690           (forward-line -1)             ; back to `b'
8691           (gnus-add-text-properties
8692            b (1- e) (list 'gnus-number gnus-reffed-article-number
8693                           gnus-mouse-face-prop gnus-mouse-face))
8694           (gnus-data-enter
8695            after-article gnus-reffed-article-number
8696            gnus-unread-mark b (car pslist) 0 (- e b))
8697           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8698           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8699           (setq pslist (cdr pslist)))))))
8700
8701 (defun gnus-pseudos< (p1 p2)
8702   (let ((c1 (cdr (assq 'action p1)))
8703         (c2 (cdr (assq 'action p2))))
8704     (and c1 c2 (string< c1 c2))))
8705
8706 (defun gnus-request-pseudo-article (props)
8707   (cond ((assq 'execute props)
8708          (gnus-execute-command (cdr (assq 'execute props)))))
8709   (let ((gnus-current-article (gnus-summary-article-number)))
8710     (gnus-run-hooks 'gnus-mark-article-hook)))
8711
8712 (defun gnus-execute-command (command &optional automatic)
8713   (save-excursion
8714     (gnus-article-setup-buffer)
8715     (set-buffer gnus-article-buffer)
8716     (setq buffer-read-only nil)
8717     (let ((command (if automatic command
8718                      (read-string "Command: " (cons command 0)))))
8719       (erase-buffer)
8720       (insert "$ " command "\n\n")
8721       (if gnus-view-pseudo-asynchronously
8722           (start-process "gnus-execute" (current-buffer) shell-file-name
8723                          shell-command-switch command)
8724         (call-process shell-file-name nil t nil
8725                       shell-command-switch command)))))
8726
8727 ;; Summary kill commands.
8728
8729 (defun gnus-summary-edit-global-kill (article)
8730   "Edit the \"global\" kill file."
8731   (interactive (list (gnus-summary-article-number)))
8732   (gnus-group-edit-global-kill article))
8733
8734 (defun gnus-summary-edit-local-kill ()
8735   "Edit a local kill file applied to the current newsgroup."
8736   (interactive)
8737   (setq gnus-current-headers (gnus-summary-article-header))
8738   (gnus-group-edit-local-kill
8739    (gnus-summary-article-number) gnus-newsgroup-name))
8740
8741 ;;; Header reading.
8742
8743 (defun gnus-read-header (id &optional header)
8744   "Read the headers of article ID and enter them into the Gnus system."
8745   (let ((group gnus-newsgroup-name)
8746         (gnus-override-method
8747          (and (gnus-news-group-p gnus-newsgroup-name)
8748               gnus-refer-article-method))
8749         where)
8750     ;; First we check to see whether the header in question is already
8751     ;; fetched.
8752     (if (stringp id)
8753         ;; This is a Message-ID.
8754         (setq header (or header (gnus-id-to-header id)))
8755       ;; This is an article number.
8756       (setq header (or header (gnus-summary-article-header id))))
8757     (if (and header
8758              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8759         ;; We have found the header.
8760         header
8761       ;; If this is a sparse article, we have to nix out its
8762       ;; previous entry in the thread hashtb.
8763       (when (and header
8764                  (gnus-summary-article-sparse-p (mail-header-number header)))
8765         (let* ((parent (gnus-parent-id (mail-header-references header)))
8766                (thread
8767                 (and parent
8768                      (gnus-gethash parent gnus-newsgroup-dependencies))))
8769           (when thread
8770             (delq (assq header thread) thread))))
8771       ;; We have to really fetch the header to this article.
8772       (save-excursion
8773         (set-buffer nntp-server-buffer)
8774         (when (setq where (gnus-request-head id group))
8775           (nnheader-fold-continuation-lines)
8776           (goto-char (point-max))
8777           (insert ".\n")
8778           (goto-char (point-min))
8779           (insert "211 ")
8780           (princ (cond
8781                   ((numberp id) id)
8782                   ((cdr where) (cdr where))
8783                   (header (mail-header-number header))
8784                   (t gnus-reffed-article-number))
8785                  (current-buffer))
8786           (insert " Article retrieved.\n"))
8787         (if (or (not where)
8788                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8789             ()                          ; Malformed head.
8790           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8791             (when (and (stringp id)
8792                        (not (string= (gnus-group-real-name group)
8793                                      (car where))))
8794               ;; If we fetched by Message-ID and the article came
8795               ;; from a different group, we fudge some bogus article
8796               ;; numbers for this article.
8797               (mail-header-set-number header gnus-reffed-article-number))
8798             (save-excursion
8799               (set-buffer gnus-summary-buffer)
8800               (decf gnus-reffed-article-number)
8801               (gnus-remove-header (mail-header-number header))
8802               (push header gnus-newsgroup-headers)
8803               (setq gnus-current-headers header)
8804               (push (mail-header-number header) gnus-newsgroup-limit)))
8805           header)))))
8806
8807 (defun gnus-remove-header (number)
8808   "Remove header NUMBER from `gnus-newsgroup-headers'."
8809   (if (and gnus-newsgroup-headers
8810            (= number (mail-header-number (car gnus-newsgroup-headers))))
8811       (pop gnus-newsgroup-headers)
8812     (let ((headers gnus-newsgroup-headers))
8813       (while (and (cdr headers)
8814                   (not (= number (mail-header-number (cadr headers)))))
8815         (pop headers))
8816       (when (cdr headers)
8817         (setcdr headers (cddr headers))))))
8818
8819 ;;;
8820 ;;; summary highlights
8821 ;;;
8822
8823 (defun gnus-highlight-selected-summary ()
8824   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8825   ;; Highlight selected article in summary buffer
8826   (when gnus-summary-selected-face
8827     (save-excursion
8828       (let* ((beg (progn (beginning-of-line) (point)))
8829              (end (progn (end-of-line) (point)))
8830              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8831              (from (if (get-text-property beg gnus-mouse-face-prop)
8832                        beg
8833                      (or (next-single-property-change
8834                           beg gnus-mouse-face-prop nil end)
8835                          beg)))
8836              (to
8837               (if (= from end)
8838                   (- from 2)
8839                 (or (next-single-property-change
8840                      from gnus-mouse-face-prop nil end)
8841                     end))))
8842         ;; If no mouse-face prop on line we will have to = from = end,
8843         ;; so we highlight the entire line instead.
8844         (when (= (+ to 2) from)
8845           (setq from beg)
8846           (setq to end))
8847         (if gnus-newsgroup-selected-overlay
8848             ;; Move old overlay.
8849             (gnus-move-overlay
8850              gnus-newsgroup-selected-overlay from to (current-buffer))
8851           ;; Create new overlay.
8852           (gnus-overlay-put
8853            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8854            'face gnus-summary-selected-face))))))
8855
8856 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8857 (defun gnus-summary-highlight-line ()
8858   "Highlight current line according to `gnus-summary-highlight'."
8859   (let* ((list gnus-summary-highlight)
8860          (p (point))
8861          (end (progn (end-of-line) (point)))
8862          ;; now find out where the line starts and leave point there.
8863          (beg (progn (beginning-of-line) (point)))
8864          (article (gnus-summary-article-number))
8865          (score (or (cdr (assq (or article gnus-current-article)
8866                                gnus-newsgroup-scored))
8867                     gnus-summary-default-score 0))
8868          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8869          (inhibit-read-only t))
8870     ;; Eval the cars of the lists until we find a match.
8871     (let ((default gnus-summary-default-score))
8872       (while (and list
8873                   (not (eval (caar list))))
8874         (setq list (cdr list))))
8875     (let ((face (cdar list)))
8876       (unless (eq face (get-text-property beg 'face))
8877         (gnus-put-text-property
8878          beg end 'face
8879          (setq face (if (boundp face) (symbol-value face) face)))
8880         (when gnus-summary-highlight-line-function
8881           (funcall gnus-summary-highlight-line-function article face))))
8882     (goto-char p)))
8883
8884 (defun gnus-update-read-articles (group unread &optional compute)
8885   "Update the list of read articles in GROUP."
8886   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8887          (entry (gnus-gethash group gnus-newsrc-hashtb))
8888          (info (nth 2 entry))
8889          (prev 1)
8890          (unread (sort (copy-sequence unread) '<))
8891          read)
8892     (if (or (not info) (not active))
8893         ;; There is no info on this group if it was, in fact,
8894         ;; killed.  Gnus stores no information on killed groups, so
8895         ;; there's nothing to be done.
8896         ;; One could store the information somewhere temporarily,
8897         ;; perhaps...  Hmmm...
8898         ()
8899       ;; Remove any negative articles numbers.
8900       (while (and unread (< (car unread) 0))
8901         (setq unread (cdr unread)))
8902       ;; Remove any expired article numbers
8903       (while (and unread (< (car unread) (car active)))
8904         (setq unread (cdr unread)))
8905       ;; Compute the ranges of read articles by looking at the list of
8906       ;; unread articles.
8907       (while unread
8908         (when (/= (car unread) prev)
8909           (push (if (= prev (1- (car unread))) prev
8910                   (cons prev (1- (car unread))))
8911                 read))
8912         (setq prev (1+ (car unread)))
8913         (setq unread (cdr unread)))
8914       (when (<= prev (cdr active))
8915         (push (cons prev (cdr active)) read))
8916       (if compute
8917           (if (> (length read) 1) (nreverse read) read)
8918         (save-excursion
8919           (set-buffer gnus-group-buffer)
8920           (gnus-undo-register
8921             `(progn
8922                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8923                (gnus-info-set-read ',info ',(gnus-info-read info))
8924                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8925                (gnus-group-update-group ,group t))))
8926         ;; Enter this list into the group info.
8927         (gnus-info-set-read
8928          info (if (> (length read) 1) (nreverse read) read))
8929         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8930         (gnus-get-unread-articles-in-group info (gnus-active group))
8931         t))))
8932
8933 (defun gnus-offer-save-summaries ()
8934   "Offer to save all active summary buffers."
8935   (save-excursion
8936     (let ((buflist (buffer-list))
8937           buffers bufname)
8938       ;; Go through all buffers and find all summaries.
8939       (while buflist
8940         (and (setq bufname (buffer-name (car buflist)))
8941              (string-match "Summary" bufname)
8942              (save-excursion
8943                (set-buffer bufname)
8944                ;; We check that this is, indeed, a summary buffer.
8945                (and (eq major-mode 'gnus-summary-mode)
8946                     ;; Also make sure this isn't bogus.
8947                     gnus-newsgroup-prepared
8948                     ;; Also make sure that this isn't a dead summary buffer.
8949                     (not gnus-dead-summary-mode)))
8950              (push bufname buffers))
8951         (setq buflist (cdr buflist)))
8952       ;; Go through all these summary buffers and offer to save them.
8953       (when buffers
8954         (map-y-or-n-p
8955          "Update summary buffer %s? "
8956          (lambda (buf)
8957            (switch-to-buffer buf)
8958            (gnus-summary-exit))
8959          buffers)))))
8960
8961 (gnus-ems-redefine)
8962
8963 (provide 'gnus-sum)
8964
8965 (run-hooks 'gnus-sum-load-hook)
8966
8967 ;;; gnus-sum.el ends here