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