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