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