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