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