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