Sync up with Gnus v5.6.9.
[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                     ;; We go to the dummy root by going to
3249                     ;; the first sub-"thread", and then one line up.
3250                     (gnus-summary-goto-article
3251                      (mail-header-number (caadr thread)))
3252                     (forward-line -1)
3253                     (gnus-delete-line)
3254                     (gnus-data-compute-positions))
3255                   (setq thread (cdr thread))
3256                   (while thread
3257                     (gnus-remove-thread-1 (car thread))
3258                     (setq thread (cdr thread))))
3259               (gnus-remove-thread-1 thread))))))))
3260
3261 (defun gnus-remove-thread-1 (thread)
3262   "Remove the thread THREAD recursively."
3263   (let ((number (mail-header-number (pop thread)))
3264         d)
3265     (setq thread (reverse thread))
3266     (while thread
3267       (gnus-remove-thread-1 (pop thread)))
3268     (when (setq d (gnus-data-find number))
3269       (goto-char (gnus-data-pos d))
3270       (gnus-data-remove
3271        number
3272        (- (gnus-point-at-bol)
3273           (prog1
3274               (1+ (gnus-point-at-eol))
3275             (gnus-delete-line)))))))
3276
3277 (defun gnus-sort-threads (threads)
3278   "Sort THREADS."
3279   (if (not gnus-thread-sort-functions)
3280       threads
3281     (gnus-message 7 "Sorting threads...")
3282     (prog1
3283         (sort threads (gnus-make-sort-function gnus-thread-sort-functions))
3284       (gnus-message 7 "Sorting threads...done"))))
3285
3286 (defun gnus-sort-articles (articles)
3287   "Sort ARTICLES."
3288   (when gnus-article-sort-functions
3289     (gnus-message 7 "Sorting articles...")
3290     (prog1
3291         (setq gnus-newsgroup-headers
3292               (sort articles (gnus-make-sort-function
3293                               gnus-article-sort-functions)))
3294       (gnus-message 7 "Sorting articles...done"))))
3295
3296 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3297 (defmacro gnus-thread-header (thread)
3298   ;; Return header of first article in THREAD.
3299   ;; Note that THREAD must never, ever be anything else than a variable -
3300   ;; using some other form will lead to serious barfage.
3301   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3302   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3303   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
3304         (vector thread) 2))
3305
3306 (defsubst gnus-article-sort-by-number (h1 h2)
3307   "Sort articles by article number."
3308   (< (mail-header-number h1)
3309      (mail-header-number h2)))
3310
3311 (defun gnus-thread-sort-by-number (h1 h2)
3312   "Sort threads by root article number."
3313   (gnus-article-sort-by-number
3314    (gnus-thread-header h1) (gnus-thread-header h2)))
3315
3316 (defsubst gnus-article-sort-by-lines (h1 h2)
3317   "Sort articles by article Lines header."
3318   (< (mail-header-lines h1)
3319      (mail-header-lines h2)))
3320
3321 (defun gnus-thread-sort-by-lines (h1 h2)
3322   "Sort threads by root article Lines header."
3323   (gnus-article-sort-by-lines
3324    (gnus-thread-header h1) (gnus-thread-header h2)))
3325
3326 (defsubst gnus-article-sort-by-author (h1 h2)
3327   "Sort articles by root author."
3328   (string-lessp
3329    (let ((extract (funcall
3330                    gnus-extract-address-components
3331                    (mail-header-from h1))))
3332      (or (car extract) (cadr extract) ""))
3333    (let ((extract (funcall
3334                    gnus-extract-address-components
3335                    (mail-header-from h2))))
3336      (or (car extract) (cadr extract) ""))))
3337
3338 (defun gnus-thread-sort-by-author (h1 h2)
3339   "Sort threads by root author."
3340   (gnus-article-sort-by-author
3341    (gnus-thread-header h1)  (gnus-thread-header h2)))
3342
3343 (defsubst gnus-article-sort-by-subject (h1 h2)
3344   "Sort articles by root subject."
3345   (string-lessp
3346    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3347    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3348
3349 (defun gnus-thread-sort-by-subject (h1 h2)
3350   "Sort threads by root subject."
3351   (gnus-article-sort-by-subject
3352    (gnus-thread-header h1) (gnus-thread-header h2)))
3353
3354 (defsubst gnus-article-sort-by-date (h1 h2)
3355   "Sort articles by root article date."
3356   (gnus-time-less
3357    (gnus-date-get-time (mail-header-date h1))
3358    (gnus-date-get-time (mail-header-date h2))))
3359
3360 (defun gnus-thread-sort-by-date (h1 h2)
3361   "Sort threads by root article date."
3362   (gnus-article-sort-by-date
3363    (gnus-thread-header h1) (gnus-thread-header h2)))
3364
3365 (defsubst gnus-article-sort-by-score (h1 h2)
3366   "Sort articles by root article score.
3367 Unscored articles will be counted as having a score of zero."
3368   (> (or (cdr (assq (mail-header-number h1)
3369                     gnus-newsgroup-scored))
3370          gnus-summary-default-score 0)
3371      (or (cdr (assq (mail-header-number h2)
3372                     gnus-newsgroup-scored))
3373          gnus-summary-default-score 0)))
3374
3375 (defun gnus-thread-sort-by-score (h1 h2)
3376   "Sort threads by root article score."
3377   (gnus-article-sort-by-score
3378    (gnus-thread-header h1) (gnus-thread-header h2)))
3379
3380 (defun gnus-thread-sort-by-total-score (h1 h2)
3381   "Sort threads by the sum of all scores in the thread.
3382 Unscored articles will be counted as having a score of zero."
3383   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3384
3385 (defun gnus-thread-total-score (thread)
3386   ;;  This function find the total score of THREAD.
3387   (cond ((null thread)
3388          0)
3389         ((consp thread)
3390          (if (stringp (car thread))
3391              (apply gnus-thread-score-function 0
3392                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3393            (gnus-thread-total-score-1 thread)))
3394         (t
3395          (gnus-thread-total-score-1 (list thread)))))
3396
3397 (defun gnus-thread-total-score-1 (root)
3398   ;; This function find the total score of the thread below ROOT.
3399   (setq root (car root))
3400   (apply gnus-thread-score-function
3401          (or (append
3402               (mapcar 'gnus-thread-total-score
3403                       (cdr (gnus-gethash (mail-header-id root)
3404                                          gnus-newsgroup-dependencies)))
3405               (when (> (mail-header-number root) 0)
3406                 (list (or (cdr (assq (mail-header-number root)
3407                                      gnus-newsgroup-scored))
3408                           gnus-summary-default-score 0))))
3409              (list gnus-summary-default-score)
3410              '(0))))
3411
3412 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3413 (defvar gnus-tmp-prev-subject nil)
3414 (defvar gnus-tmp-false-parent nil)
3415 (defvar gnus-tmp-root-expunged nil)
3416 (defvar gnus-tmp-dummy-line nil)
3417
3418 (defun gnus-summary-prepare-threads (threads)
3419   "Prepare summary buffer from THREADS and indentation LEVEL.
3420 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3421 or a straight list of headers."
3422   (gnus-message 7 "Generating summary...")
3423
3424   (setq gnus-newsgroup-threads threads)
3425   (beginning-of-line)
3426
3427   (let ((gnus-tmp-level 0)
3428         (default-score (or gnus-summary-default-score 0))
3429         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3430         thread number subject stack state gnus-tmp-gathered beg-match
3431         new-roots gnus-tmp-new-adopts thread-end
3432         gnus-tmp-header gnus-tmp-unread
3433         gnus-tmp-replied gnus-tmp-subject-or-nil
3434         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3435         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3436         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3437
3438     (setq gnus-tmp-prev-subject nil)
3439
3440     (if (vectorp (car threads))
3441         ;; If this is a straight (sic) list of headers, then a
3442         ;; threaded summary display isn't required, so we just create
3443         ;; an unthreaded one.
3444         (gnus-summary-prepare-unthreaded threads)
3445
3446       ;; Do the threaded display.
3447
3448       (while (or threads stack gnus-tmp-new-adopts new-roots)
3449
3450         (if (and (= gnus-tmp-level 0)
3451                  (or (not stack)
3452                      (= (caar stack) 0))
3453                  (not gnus-tmp-false-parent)
3454                  (or gnus-tmp-new-adopts new-roots))
3455             (if gnus-tmp-new-adopts
3456                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3457                       thread (list (car gnus-tmp-new-adopts))
3458                       gnus-tmp-header (caar thread)
3459                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3460               (when new-roots
3461                 (setq thread (list (car new-roots))
3462                       gnus-tmp-header (caar thread)
3463                       new-roots (cdr new-roots))))
3464
3465           (if threads
3466               ;; If there are some threads, we do them before the
3467               ;; threads on the stack.
3468               (setq thread threads
3469                     gnus-tmp-header (caar thread))
3470             ;; There were no current threads, so we pop something off
3471             ;; the stack.
3472             (setq state (car stack)
3473                   gnus-tmp-level (car state)
3474                   thread (cdr state)
3475                   stack (cdr stack)
3476                   gnus-tmp-header (caar thread))))
3477
3478         (setq gnus-tmp-false-parent nil)
3479         (setq gnus-tmp-root-expunged nil)
3480         (setq thread-end nil)
3481
3482         (if (stringp gnus-tmp-header)
3483             ;; The header is a dummy root.
3484             (cond
3485              ((eq gnus-summary-make-false-root 'adopt)
3486               ;; We let the first article adopt the rest.
3487               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3488                                                (cddar thread)))
3489               (setq gnus-tmp-gathered
3490                     (nconc (mapcar
3491                             (lambda (h) (mail-header-number (car h)))
3492                             (cddar thread))
3493                            gnus-tmp-gathered))
3494               (setq thread (cons (list (caar thread)
3495                                        (cadar thread))
3496                                  (cdr thread)))
3497               (setq gnus-tmp-level -1
3498                     gnus-tmp-false-parent t))
3499              ((eq gnus-summary-make-false-root 'empty)
3500               ;; We print adopted articles with empty subject fields.
3501               (setq gnus-tmp-gathered
3502                     (nconc (mapcar
3503                             (lambda (h) (mail-header-number (car h)))
3504                             (cddar thread))
3505                            gnus-tmp-gathered))
3506               (setq gnus-tmp-level -1))
3507              ((eq gnus-summary-make-false-root 'dummy)
3508               ;; We remember that we probably want to output a dummy
3509               ;; root.
3510               (setq gnus-tmp-dummy-line gnus-tmp-header)
3511               (setq gnus-tmp-prev-subject gnus-tmp-header))
3512              (t
3513               ;; We do not make a root for the gathered
3514               ;; sub-threads at all.
3515               (setq gnus-tmp-level -1)))
3516
3517           (setq number (mail-header-number gnus-tmp-header)
3518                 subject (mail-header-subject gnus-tmp-header))
3519
3520           (cond
3521            ;; If the thread has changed subject, we might want to make
3522            ;; this subthread into a root.
3523            ((and (null gnus-thread-ignore-subject)
3524                  (not (zerop gnus-tmp-level))
3525                  gnus-tmp-prev-subject
3526                  (not (inline
3527                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3528             (setq new-roots (nconc new-roots (list (car thread)))
3529                   thread-end t
3530                   gnus-tmp-header nil))
3531            ;; If the article lies outside the current limit,
3532            ;; then we do not display it.
3533            ((not (memq number gnus-newsgroup-limit))
3534             (setq gnus-tmp-gathered
3535                   (nconc (mapcar
3536                           (lambda (h) (mail-header-number (car h)))
3537                           (cdar thread))
3538                          gnus-tmp-gathered))
3539             (setq gnus-tmp-new-adopts (if (cdar thread)
3540                                           (append gnus-tmp-new-adopts
3541                                                   (cdar thread))
3542                                         gnus-tmp-new-adopts)
3543                   thread-end t
3544                   gnus-tmp-header nil)
3545             (when (zerop gnus-tmp-level)
3546               (setq gnus-tmp-root-expunged t)))
3547            ;; Perhaps this article is to be marked as read?
3548            ((and gnus-summary-mark-below
3549                  (< (or (cdr (assq number gnus-newsgroup-scored))
3550                         default-score)
3551                     gnus-summary-mark-below)
3552                  ;; Don't touch sparse articles.
3553                  (not (gnus-summary-article-sparse-p number))
3554                  (not (gnus-summary-article-ancient-p number)))
3555             (setq gnus-newsgroup-unreads
3556                   (delq number gnus-newsgroup-unreads))
3557             (if gnus-newsgroup-auto-expire
3558                 (push number gnus-newsgroup-expirable)
3559               (push (cons number gnus-low-score-mark)
3560                     gnus-newsgroup-reads))))
3561
3562           (when gnus-tmp-header
3563             ;; We may have an old dummy line to output before this
3564             ;; article.
3565             (when (and gnus-tmp-dummy-line
3566                        (gnus-subject-equal
3567                         gnus-tmp-dummy-line
3568                         (mail-header-subject gnus-tmp-header)))
3569               (gnus-summary-insert-dummy-line
3570                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3571               (setq gnus-tmp-dummy-line nil))
3572
3573             ;; Compute the mark.
3574             (setq gnus-tmp-unread (gnus-article-mark number))
3575
3576             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3577                                   gnus-tmp-header gnus-tmp-level)
3578                   gnus-newsgroup-data)
3579
3580             ;; Actually insert the line.
3581             (setq
3582              gnus-tmp-subject-or-nil
3583              (cond
3584               ((and gnus-thread-ignore-subject
3585                     gnus-tmp-prev-subject
3586                     (not (inline (gnus-subject-equal
3587                                   gnus-tmp-prev-subject subject))))
3588                subject)
3589               ((zerop gnus-tmp-level)
3590                (if (and (eq gnus-summary-make-false-root 'empty)
3591                         (memq number gnus-tmp-gathered)
3592                         gnus-tmp-prev-subject
3593                         (inline (gnus-subject-equal
3594                                  gnus-tmp-prev-subject subject)))
3595                    gnus-summary-same-subject
3596                  subject))
3597               (t gnus-summary-same-subject)))
3598             (if (and (eq gnus-summary-make-false-root 'adopt)
3599                      (= gnus-tmp-level 1)
3600                      (memq number gnus-tmp-gathered))
3601                 (setq gnus-tmp-opening-bracket ?\<
3602                       gnus-tmp-closing-bracket ?\>)
3603               (setq gnus-tmp-opening-bracket ?\[
3604                     gnus-tmp-closing-bracket ?\]))
3605             (setq
3606              gnus-tmp-indentation
3607              (aref gnus-thread-indent-array gnus-tmp-level)
3608              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
3609              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
3610                                 gnus-summary-default-score 0)
3611              gnus-tmp-score-char
3612              (if (or (null gnus-summary-default-score)
3613                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3614                          gnus-summary-zcore-fuzz))
3615                  ? 
3616                (if (< gnus-tmp-score gnus-summary-default-score)
3617                    gnus-score-below-mark gnus-score-over-mark))
3618              gnus-tmp-replied
3619              (cond ((memq number gnus-newsgroup-processable)
3620                     gnus-process-mark)
3621                    ((memq number gnus-newsgroup-cached)
3622                     gnus-cached-mark)
3623                    ((memq number gnus-newsgroup-replied)
3624                     gnus-replied-mark)
3625                    ((memq number gnus-newsgroup-saved)
3626                     gnus-saved-mark)
3627                    (t gnus-unread-mark))
3628              gnus-tmp-from (mail-header-from gnus-tmp-header)
3629              gnus-tmp-name
3630              (cond
3631               ((string-match "<[^>]+> *$" gnus-tmp-from)
3632                (setq beg-match (match-beginning 0))
3633                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
3634                         (substring gnus-tmp-from (1+ (match-beginning 0))
3635                                    (1- (match-end 0))))
3636                    (substring gnus-tmp-from 0 beg-match)))
3637               ((string-match "(.+)" gnus-tmp-from)
3638                (substring gnus-tmp-from
3639                           (1+ (match-beginning 0)) (1- (match-end 0))))
3640               (t gnus-tmp-from)))
3641             (when (string= gnus-tmp-name "")
3642               (setq gnus-tmp-name gnus-tmp-from))
3643             (unless (numberp gnus-tmp-lines)
3644               (setq gnus-tmp-lines 0))
3645             (gnus-put-text-property
3646              (point)
3647              (progn (eval gnus-summary-line-format-spec) (point))
3648              'gnus-number number)
3649             (when gnus-visual-p
3650               (forward-line -1)
3651               (gnus-run-hooks 'gnus-summary-update-hook)
3652               (forward-line 1))
3653
3654             (setq gnus-tmp-prev-subject subject)))
3655
3656         (when (nth 1 thread)
3657           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
3658         (incf gnus-tmp-level)
3659         (setq threads (if thread-end nil (cdar thread)))
3660         (unless threads
3661           (setq gnus-tmp-level 0)))))
3662   (gnus-message 7 "Generating summary...done"))
3663
3664 (defun gnus-summary-prepare-unthreaded (headers)
3665   "Generate an unthreaded summary buffer based on HEADERS."
3666   (let (header number mark)
3667
3668     (beginning-of-line)
3669
3670     (while headers
3671       ;; We may have to root out some bad articles...
3672       (when (memq (setq number (mail-header-number
3673                                 (setq header (pop headers))))
3674                   gnus-newsgroup-limit)
3675         ;; Mark article as read when it has a low score.
3676         (when (and gnus-summary-mark-below
3677                    (< (or (cdr (assq number gnus-newsgroup-scored))
3678                           gnus-summary-default-score 0)
3679                       gnus-summary-mark-below)
3680                    (not (gnus-summary-article-ancient-p number)))
3681           (setq gnus-newsgroup-unreads
3682                 (delq number gnus-newsgroup-unreads))
3683           (if gnus-newsgroup-auto-expire
3684               (push number gnus-newsgroup-expirable)
3685             (push (cons number gnus-low-score-mark)
3686                   gnus-newsgroup-reads)))
3687
3688         (setq mark (gnus-article-mark number))
3689         (push (gnus-data-make number mark (1+ (point)) header 0)
3690               gnus-newsgroup-data)
3691         (gnus-summary-insert-line
3692          header 0 number
3693          mark (memq number gnus-newsgroup-replied)
3694          (memq number gnus-newsgroup-expirable)
3695          (mail-header-subject header) nil
3696          (cdr (assq number gnus-newsgroup-scored))
3697          (memq number gnus-newsgroup-processable))))))
3698
3699 (defun gnus-select-newsgroup (group &optional read-all)
3700   "Select newsgroup GROUP.
3701 If READ-ALL is non-nil, all articles in the group are selected."
3702   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
3703          ;;!!! Dirty hack; should be removed.
3704          (gnus-summary-ignore-duplicates
3705           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
3706               t
3707             gnus-summary-ignore-duplicates))
3708          (info (nth 2 entry))
3709          articles fetched-articles cached)
3710
3711     (unless (gnus-check-server
3712              (setq gnus-current-select-method
3713                    (gnus-find-method-for-group group)))
3714       (error "Couldn't open server"))
3715
3716     (or (and entry (not (eq (car entry) t))) ; Either it's active...
3717         (gnus-activate-group group)     ; Or we can activate it...
3718         (progn                          ; Or we bug out.
3719           (when (equal major-mode 'gnus-summary-mode)
3720             (kill-buffer (current-buffer)))
3721           (error "Couldn't request group %s: %s"
3722                  group (gnus-status-message group))))
3723
3724     (unless (gnus-request-group group t)
3725       (when (equal major-mode 'gnus-summary-mode)
3726         (kill-buffer (current-buffer)))
3727       (error "Couldn't request group %s: %s"
3728              group (gnus-status-message group)))
3729
3730     (setq gnus-newsgroup-name group)
3731     (setq gnus-newsgroup-unselected nil)
3732     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
3733
3734     ;; Adjust and set lists of article marks.
3735     (when info
3736       (gnus-adjust-marked-articles info))
3737
3738     ;; Kludge to avoid having cached articles nixed out in virtual groups.
3739     (when (gnus-virtual-group-p group)
3740       (setq cached gnus-newsgroup-cached))
3741
3742     (setq gnus-newsgroup-unreads
3743           (gnus-set-difference
3744            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
3745            gnus-newsgroup-dormant))
3746
3747     (setq gnus-newsgroup-processable nil)
3748
3749     (gnus-update-read-articles group gnus-newsgroup-unreads)
3750     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
3751       (gnus-group-update-group group))
3752
3753     (setq articles (gnus-articles-to-read group read-all))
3754
3755     (cond
3756      ((null articles)
3757       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
3758       'quit)
3759      ((eq articles 0) nil)
3760      (t
3761       ;; Init the dependencies hash table.
3762       (setq gnus-newsgroup-dependencies
3763             (gnus-make-hashtable (length articles)))
3764       ;; Retrieve the headers and read them in.
3765       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
3766       (setq gnus-newsgroup-headers
3767             (if (eq 'nov
3768                     (setq gnus-headers-retrieved-by
3769                           (gnus-retrieve-headers
3770                            articles gnus-newsgroup-name
3771                            ;; We might want to fetch old headers, but
3772                            ;; not if there is only 1 article.
3773                            (and (or (and
3774                                      (not (eq gnus-fetch-old-headers 'some))
3775                                      (not (numberp gnus-fetch-old-headers)))
3776                                     (> (length articles) 1))
3777                                 gnus-fetch-old-headers))))
3778                 (gnus-get-newsgroup-headers-xover
3779                  articles nil nil gnus-newsgroup-name t)
3780               (gnus-get-newsgroup-headers)))
3781       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
3782
3783       ;; Kludge to avoid having cached articles nixed out in virtual groups.
3784       (when cached
3785         (setq gnus-newsgroup-cached cached))
3786
3787       ;; Suppress duplicates?
3788       (when gnus-suppress-duplicates
3789         (gnus-dup-suppress-articles))
3790
3791       ;; Set the initial limit.
3792       (setq gnus-newsgroup-limit (copy-sequence articles))
3793       ;; Remove canceled articles from the list of unread articles.
3794       (setq gnus-newsgroup-unreads
3795             (gnus-set-sorted-intersection
3796              gnus-newsgroup-unreads
3797              (setq fetched-articles
3798                    (mapcar (lambda (headers) (mail-header-number headers))
3799                            gnus-newsgroup-headers))))
3800       ;; Removed marked articles that do not exist.
3801       (gnus-update-missing-marks
3802        (gnus-sorted-complement fetched-articles articles))
3803       ;; Let the Gnus agent mark articles as read.
3804       (when gnus-agent
3805         (gnus-agent-get-undownloaded-list))
3806       ;; We might want to build some more threads first.
3807       (when (and gnus-fetch-old-headers
3808                  (eq gnus-headers-retrieved-by 'nov))
3809         (if (eq gnus-fetch-old-headers 'invisible)
3810             (gnus-build-all-threads)
3811           (gnus-build-old-threads)))
3812       ;; Check whether auto-expire is to be done in this group.
3813       (setq gnus-newsgroup-auto-expire
3814             (gnus-group-auto-expirable-p group))
3815       ;; Set up the article buffer now, if necessary.
3816       (unless gnus-single-article-buffer
3817         (gnus-article-setup-buffer))
3818       ;; First and last article in this newsgroup.
3819       (when gnus-newsgroup-headers
3820         (setq gnus-newsgroup-begin
3821               (mail-header-number (car gnus-newsgroup-headers))
3822               gnus-newsgroup-end
3823               (mail-header-number
3824                (gnus-last-element gnus-newsgroup-headers))))
3825       ;; GROUP is successfully selected.
3826       (or gnus-newsgroup-headers t)))))
3827
3828 (defun gnus-articles-to-read (group &optional read-all)
3829   ;; Find out what articles the user wants to read.
3830   (let* ((articles
3831           ;; Select all articles if `read-all' is non-nil, or if there
3832           ;; are no unread articles.
3833           (if (or read-all
3834                   (and (zerop (length gnus-newsgroup-marked))
3835                        (zerop (length gnus-newsgroup-unreads)))
3836                   (eq (gnus-group-find-parameter group 'display)
3837                       'all))
3838               (gnus-uncompress-range (gnus-active group))
3839             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
3840                           (copy-sequence gnus-newsgroup-unreads))
3841                   '<)))
3842          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
3843          (scored (length scored-list))
3844          (number (length articles))
3845          (marked (+ (length gnus-newsgroup-marked)
3846                     (length gnus-newsgroup-dormant)))
3847          (select
3848           (cond
3849            ((numberp read-all)
3850             read-all)
3851            (t
3852             (condition-case ()
3853                 (cond
3854                  ((and (or (<= scored marked) (= scored number))
3855                        (numberp gnus-large-newsgroup)
3856                        (> number gnus-large-newsgroup))
3857                   (let ((input
3858                          (read-string
3859                           (format
3860                            "How many articles from %s (default %d): "
3861                            (gnus-limit-string gnus-newsgroup-name 35)
3862                            number))))
3863                     (if (string-match "^[ \t]*$" input) number input)))
3864                  ((and (> scored marked) (< scored number)
3865                        (> (- scored number) 20))
3866                   (let ((input
3867                          (read-string
3868                           (format "%s %s (%d scored, %d total): "
3869                                   "How many articles from"
3870                                   group scored number))))
3871                     (if (string-match "^[ \t]*$" input)
3872                         number input)))
3873                  (t number))
3874               (quit nil))))))
3875     (setq select (if (stringp select) (string-to-number select) select))
3876     (if (or (null select) (zerop select))
3877         select
3878       (if (and (not (zerop scored)) (<= (abs select) scored))
3879           (progn
3880             (setq articles (sort scored-list '<))
3881             (setq number (length articles)))
3882         (setq articles (copy-sequence articles)))
3883
3884       (when (< (abs select) number)
3885         (if (< select 0)
3886             ;; Select the N oldest articles.
3887             (setcdr (nthcdr (1- (abs select)) articles) nil)
3888           ;; Select the N most recent articles.
3889           (setq articles (nthcdr (- number select) articles))))
3890       (setq gnus-newsgroup-unselected
3891             (gnus-sorted-intersection
3892              gnus-newsgroup-unreads
3893              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
3894       articles)))
3895
3896 (defun gnus-killed-articles (killed articles)
3897   (let (out)
3898     (while articles
3899       (when (inline (gnus-member-of-range (car articles) killed))
3900         (push (car articles) out))
3901       (setq articles (cdr articles)))
3902     out))
3903
3904 (defun gnus-uncompress-marks (marks)
3905   "Uncompress the mark ranges in MARKS."
3906   (let ((uncompressed '(score bookmark))
3907         out)
3908     (while marks
3909       (if (memq (caar marks) uncompressed)
3910           (push (car marks) out)
3911         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
3912       (setq marks (cdr marks)))
3913     out))
3914
3915 (defun gnus-adjust-marked-articles (info)
3916   "Set all article lists and remove all marks that are no longer legal."
3917   (let* ((marked-lists (gnus-info-marks info))
3918          (active (gnus-active (gnus-info-group info)))
3919          (min (car active))
3920          (max (cdr active))
3921          (types gnus-article-mark-lists)
3922          (uncompressed '(score bookmark killed))
3923          marks var articles article mark)
3924
3925     (while marked-lists
3926       (setq marks (pop marked-lists))
3927       (set (setq var (intern (format "gnus-newsgroup-%s"
3928                                      (car (rassq (setq mark (car marks))
3929                                                  types)))))
3930            (if (memq (car marks) uncompressed) (cdr marks)
3931              (gnus-uncompress-range (cdr marks))))
3932
3933       (setq articles (symbol-value var))
3934
3935       ;; All articles have to be subsets of the active articles.
3936       (cond
3937        ;; Adjust "simple" lists.
3938        ((memq mark '(tick dormant expire reply save))
3939         (while articles
3940           (when (or (< (setq article (pop articles)) min) (> article max))
3941             (set var (delq article (symbol-value var))))))
3942        ;; Adjust assocs.
3943        ((memq mark uncompressed)
3944         (when (not (listp (cdr (symbol-value var))))
3945           (set var (list (symbol-value var))))
3946         (when (not (listp (cdr articles)))
3947           (setq articles (list articles)))
3948         (while articles
3949           (when (or (not (consp (setq article (pop articles))))
3950                     (< (car article) min)
3951                     (> (car article) max))
3952             (set var (delq article (symbol-value var))))))))))
3953
3954 (defun gnus-update-missing-marks (missing)
3955   "Go through the list of MISSING articles and remove them from the mark lists."
3956   (when missing
3957     (let ((types gnus-article-mark-lists)
3958           var m)
3959       ;; Go through all types.
3960       (while types
3961         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
3962         (when (symbol-value var)
3963           ;; This list has articles.  So we delete all missing articles
3964           ;; from it.
3965           (setq m missing)
3966           (while m
3967             (set var (delq (pop m) (symbol-value var)))))))))
3968
3969 (defun gnus-update-marks ()
3970   "Enter the various lists of marked articles into the newsgroup info list."
3971   (let ((types gnus-article-mark-lists)
3972         (info (gnus-get-info gnus-newsgroup-name))
3973         (uncompressed '(score bookmark killed))
3974         type list newmarked symbol)
3975     (when info
3976       ;; Add all marks lists that are non-nil to the list of marks lists.
3977       (while (setq type (pop types))
3978         (when (setq list (symbol-value
3979                           (setq symbol
3980                                 (intern (format "gnus-newsgroup-%s"
3981                                                 (car type))))))
3982
3983           ;; Get rid of the entries of the articles that have the
3984           ;; default score.
3985           (when (and (eq (cdr type) 'score)
3986                      gnus-save-score
3987                      list)
3988             (let* ((arts list)
3989                    (prev (cons nil list))
3990                    (all prev))
3991               (while arts
3992                 (if (or (not (consp (car arts)))
3993                         (= (cdar arts) gnus-summary-default-score))
3994                     (setcdr prev (cdr arts))
3995                   (setq prev arts))
3996                 (setq arts (cdr arts)))
3997               (setq list (cdr all))))
3998
3999           (push (cons (cdr type)
4000                       (if (memq (cdr type) uncompressed) list
4001                         (gnus-compress-sequence
4002                          (set symbol (sort list '<)) t)))
4003                 newmarked)))
4004
4005       ;; Enter these new marks into the info of the group.
4006       (if (nthcdr 3 info)
4007           (setcar (nthcdr 3 info) newmarked)
4008         ;; Add the marks lists to the end of the info.
4009         (when newmarked
4010           (setcdr (nthcdr 2 info) (list newmarked))))
4011
4012       ;; Cut off the end of the info if there's nothing else there.
4013       (let ((i 5))
4014         (while (and (> i 2)
4015                     (not (nth i info)))
4016           (when (nthcdr (decf i) info)
4017             (setcdr (nthcdr i info) nil)))))))
4018
4019 (defun gnus-set-mode-line (where)
4020   "This function sets the mode line of the article or summary buffers.
4021 If WHERE is `summary', the summary mode line format will be used."
4022   ;; Is this mode line one we keep updated?
4023   (when (memq where gnus-updated-mode-lines)
4024     (let (mode-string)
4025       (save-excursion
4026         ;; We evaluate this in the summary buffer since these
4027         ;; variables are buffer-local to that buffer.
4028         (set-buffer gnus-summary-buffer)
4029         ;; We bind all these variables that are used in the `eval' form
4030         ;; below.
4031         (let* ((mformat (symbol-value
4032                          (intern
4033                           (format "gnus-%s-mode-line-format-spec" where))))
4034                (gnus-tmp-group-name gnus-newsgroup-name)
4035                (gnus-tmp-article-number (or gnus-current-article 0))
4036                (gnus-tmp-unread gnus-newsgroup-unreads)
4037                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4038                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4039                (gnus-tmp-unread-and-unselected
4040                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4041                             (zerop gnus-tmp-unselected))
4042                        "")
4043                       ((zerop gnus-tmp-unselected)
4044                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4045                       (t (format "{%d(+%d) more}"
4046                                  gnus-tmp-unread-and-unticked
4047                                  gnus-tmp-unselected))))
4048                (gnus-tmp-subject
4049                 (if (and gnus-current-headers
4050                          (vectorp gnus-current-headers))
4051                     (gnus-mode-string-quote
4052                      (mail-header-subject gnus-current-headers))
4053                   ""))
4054                bufname-length max-len
4055                gnus-tmp-header);; passed as argument to any user-format-funcs
4056           (setq mode-string (eval mformat))
4057           (setq bufname-length (if (string-match "%b" mode-string)
4058                                    (- (length
4059                                        (buffer-name
4060                                         (if (eq where 'summary)
4061                                             nil
4062                                           (get-buffer gnus-article-buffer))))
4063                                       2)
4064                                  0))
4065           (setq max-len (max 4 (if gnus-mode-non-string-length
4066                                    (- (window-width)
4067                                       gnus-mode-non-string-length
4068                                       bufname-length)
4069                                  (length mode-string))))
4070           ;; We might have to chop a bit of the string off...
4071           (when (> (length mode-string) max-len)
4072             (setq mode-string
4073                   (concat (gnus-truncate-string mode-string (- max-len 3))
4074                           "...")))
4075           ;; Pad the mode string a bit.
4076           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4077       ;; Update the mode line.
4078       (setq mode-line-buffer-identification
4079             (gnus-mode-line-buffer-identification (list mode-string)))
4080       (set-buffer-modified-p t))))
4081
4082 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4083   "Go through the HEADERS list and add all Xrefs to a hash table.
4084 The resulting hash table is returned, or nil if no Xrefs were found."
4085   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4086          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4087          (xref-hashtb (gnus-make-hashtable))
4088          start group entry number xrefs header)
4089     (while headers
4090       (setq header (pop headers))
4091       (when (and (setq xrefs (mail-header-xref header))
4092                  (not (memq (setq number (mail-header-number header))
4093                             unreads)))
4094         (setq start 0)
4095         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4096           (setq start (match-end 0))
4097           (setq group (if prefix
4098                           (concat prefix (substring xrefs (match-beginning 1)
4099                                                     (match-end 1)))
4100                         (substring xrefs (match-beginning 1) (match-end 1))))
4101           (setq number
4102                 (string-to-int (substring xrefs (match-beginning 2)
4103                                           (match-end 2))))
4104           (if (setq entry (gnus-gethash group xref-hashtb))
4105               (setcdr entry (cons number (cdr entry)))
4106             (gnus-sethash group (cons number nil) xref-hashtb)))))
4107     (and start xref-hashtb)))
4108
4109 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4110   "Look through all the headers and mark the Xrefs as read."
4111   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4112         name entry info xref-hashtb idlist method nth4)
4113     (save-excursion
4114       (set-buffer gnus-group-buffer)
4115       (when (setq xref-hashtb
4116                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4117         (mapatoms
4118          (lambda (group)
4119            (unless (string= from-newsgroup (setq name (symbol-name group)))
4120              (setq idlist (symbol-value group))
4121              ;; Dead groups are not updated.
4122              (and (prog1
4123                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4124                             info (nth 2 entry))
4125                     (when (stringp (setq nth4 (gnus-info-method info)))
4126                       (setq nth4 (gnus-server-to-method nth4))))
4127                   ;; Only do the xrefs if the group has the same
4128                   ;; select method as the group we have just read.
4129                   (or (gnus-methods-equal-p
4130                        nth4 (gnus-find-method-for-group from-newsgroup))
4131                       virtual
4132                       (equal nth4 (setq method (gnus-find-method-for-group
4133                                                 from-newsgroup)))
4134                       (and (equal (car nth4) (car method))
4135                            (equal (nth 1 nth4) (nth 1 method))))
4136                   gnus-use-cross-reference
4137                   (or (not (eq gnus-use-cross-reference t))
4138                       virtual
4139                       ;; Only do cross-references on subscribed
4140                       ;; groups, if that is what is wanted.
4141                       (<= (gnus-info-level info) gnus-level-subscribed))
4142                   (gnus-group-make-articles-read name idlist))))
4143          xref-hashtb)))))
4144
4145 (defun gnus-compute-read-articles (group articles)
4146   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4147          (info (nth 2 entry))
4148          (active (gnus-active group))
4149          ninfo)
4150     (when entry
4151       ;; First peel off all illegal article numbers.
4152       (when active
4153         (let ((ids articles)
4154               id first)
4155           (while (setq id (pop ids))
4156             (when (and first (> id (cdr active)))
4157               ;; We'll end up in this situation in one particular
4158               ;; obscure situation.  If you re-scan a group and get
4159               ;; a new article that is cross-posted to a different
4160               ;; group that has not been re-scanned, you might get
4161               ;; crossposted article that has a higher number than
4162               ;; Gnus believes possible.  So we re-activate this
4163               ;; group as well.  This might mean doing the
4164               ;; crossposting thingy will *increase* the number
4165               ;; of articles in some groups.  Tsk, tsk.
4166               (setq active (or (gnus-activate-group group) active)))
4167             (when (or (> id (cdr active))
4168                       (< id (car active)))
4169               (setq articles (delq id articles))))))
4170       ;; If the read list is nil, we init it.
4171       (if (and active
4172                (null (gnus-info-read info))
4173                (> (car active) 1))
4174           (setq ninfo (cons 1 (1- (car active))))
4175         (setq ninfo (gnus-info-read info)))
4176       ;; Then we add the read articles to the range.
4177       (gnus-add-to-range
4178        ninfo (setq articles (sort articles '<))))))
4179   
4180 (defun gnus-group-make-articles-read (group articles)
4181   "Update the info of GROUP to say that ARTICLES are read."
4182   (let* ((num 0)
4183          (entry (gnus-gethash group gnus-newsrc-hashtb))
4184          (info (nth 2 entry))
4185          (active (gnus-active group))
4186          range)
4187     (when entry
4188       (setq range (gnus-compute-read-articles group articles))
4189       (save-excursion
4190         (set-buffer gnus-group-buffer)
4191         (gnus-undo-register
4192           `(progn
4193              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4194              (gnus-info-set-read ',info ',(gnus-info-read info))
4195              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4196              (gnus-group-update-group ,group t))))
4197       ;; Add the read articles to the range.
4198       (gnus-info-set-read info range)
4199       ;; Then we have to re-compute how many unread
4200       ;; articles there are in this group.
4201       (when active
4202         (cond
4203          ((not range)
4204           (setq num (- (1+ (cdr active)) (car active))))
4205          ((not (listp (cdr range)))
4206           (setq num (- (cdr active) (- (1+ (cdr range))
4207                                        (car range)))))
4208          (t
4209           (while range
4210             (if (numberp (car range))
4211                 (setq num (1+ num))
4212               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4213             (setq range (cdr range)))
4214           (setq num (- (cdr active) num))))
4215         ;; Update the number of unread articles.
4216         (setcar entry num)
4217         ;; Update the group buffer.
4218         (gnus-group-update-group group t)))))
4219
4220 (defun gnus-methods-equal-p (m1 m2)
4221   (let ((m1 (or m1 gnus-select-method))
4222         (m2 (or m2 gnus-select-method)))
4223     (or (equal m1 m2)
4224         (and (eq (car m1) (car m2))
4225              (or (not (memq 'address (assoc (symbol-name (car m1))
4226                                             gnus-valid-select-methods)))
4227                  (equal (nth 1 m1) (nth 1 m2)))))))
4228
4229 (defvar gnus-newsgroup-none-id 0)
4230
4231 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4232   (let ((cur nntp-server-buffer)
4233         (dependencies
4234          (or dependencies
4235              (save-excursion (set-buffer gnus-summary-buffer)
4236                              gnus-newsgroup-dependencies)))
4237         headers id id-dep ref-dep end ref)
4238     (save-excursion
4239       (set-buffer nntp-server-buffer)
4240       ;; Translate all TAB characters into SPACE characters.
4241       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4242       (gnus-run-hooks 'gnus-parse-headers-hook)
4243       (let ((case-fold-search t)
4244             in-reply-to header p lines)
4245         (goto-char (point-min))
4246         ;; Search to the beginning of the next header.  Error messages
4247         ;; do not begin with 2 or 3.
4248         (while (re-search-forward "^[23][0-9]+ " nil t)
4249           (setq id nil
4250                 ref nil)
4251           ;; This implementation of this function, with nine
4252           ;; search-forwards instead of the one re-search-forward and
4253           ;; a case (which basically was the old function) is actually
4254           ;; about twice as fast, even though it looks messier.  You
4255           ;; can't have everything, I guess.  Speed and elegance
4256           ;; doesn't always go hand in hand.
4257           (setq
4258            header
4259            (vector
4260             ;; Number.
4261             (prog1
4262                 (read cur)
4263               (end-of-line)
4264               (setq p (point))
4265               (narrow-to-region (point)
4266                                 (or (and (search-forward "\n.\n" nil t)
4267                                          (- (point) 2))
4268                                     (point))))
4269             ;; Subject.
4270             (progn
4271               (goto-char p)
4272               (if (search-forward "\nsubject: " nil t)
4273                   (funcall
4274                    gnus-unstructured-field-decoder (nnheader-header-value))
4275                 "(none)"))
4276             ;; From.
4277             (progn
4278               (goto-char p)
4279               (if (search-forward "\nfrom: " nil t)
4280                   (funcall
4281                    gnus-structured-field-decoder (nnheader-header-value))
4282                 "(nobody)"))
4283             ;; Date.
4284             (progn
4285               (goto-char p)
4286               (if (search-forward "\ndate: " nil t)
4287                   (nnheader-header-value) ""))
4288             ;; Message-ID.
4289             (progn
4290               (goto-char p)
4291               (setq id (if (re-search-forward
4292                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4293                            ;; We do it this way to make sure the Message-ID
4294                            ;; is (somewhat) syntactically valid.
4295                            (buffer-substring (match-beginning 1)
4296                                              (match-end 1))
4297                          ;; If there was no message-id, we just fake one
4298                          ;; to make subsequent routines simpler.
4299                          (nnheader-generate-fake-message-id))))
4300             ;; References.
4301             (progn
4302               (goto-char p)
4303               (if (search-forward "\nreferences: " nil t)
4304                   (progn
4305                     (setq end (point))
4306                     (prog1
4307                         (nnheader-header-value)
4308                       (setq ref
4309                             (buffer-substring
4310                              (progn
4311                                (end-of-line)
4312                                (search-backward ">" end t)
4313                                (1+ (point)))
4314                              (progn
4315                                (search-backward "<" end t)
4316                                (point))))))
4317                 ;; Get the references from the in-reply-to header if there
4318                 ;; were no references and the in-reply-to header looks
4319                 ;; promising.
4320                 (if (and (search-forward "\nin-reply-to: " nil t)
4321                          (setq in-reply-to (nnheader-header-value))
4322                          (string-match "<[^>]+>" in-reply-to))
4323                     (let (ref2)
4324                       (setq ref (substring in-reply-to (match-beginning 0)
4325                                            (match-end 0)))
4326                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4327                         (setq ref2 (substring in-reply-to (match-beginning 0)
4328                                               (match-end 0)))
4329                         (when (> (length ref2) (length ref))
4330                           (setq ref ref2))))
4331                   (setq ref nil))))
4332             ;; Chars.
4333             0
4334             ;; Lines.
4335             (progn
4336               (goto-char p)
4337               (if (search-forward "\nlines: " nil t)
4338                   (if (numberp (setq lines (ignore-errors (read cur))))
4339                       lines 0)
4340                 0))
4341             ;; Xref.
4342             (progn
4343               (goto-char p)
4344               (and (search-forward "\nxref: " nil t)
4345                    (nnheader-header-value)))))
4346           (when (equal id ref)
4347             (setq ref nil))
4348
4349           (when gnus-alter-header-function
4350             (funcall gnus-alter-header-function header)
4351             (setq id (mail-header-id header)
4352                   ref (gnus-parent-id (mail-header-references header))))
4353     
4354           ;; We do the threading while we read the headers.  The
4355           ;; message-id and the last reference are both entered into
4356           ;; the same hash table.  Some tippy-toeing around has to be
4357           ;; done in case an article has arrived before the article
4358           ;; which it refers to.
4359           (if (boundp (setq id-dep (intern id dependencies)))
4360               (if (and (car (symbol-value id-dep))
4361                        (not force-new))
4362                   ;; An article with this Message-ID has already been seen.
4363                   (if gnus-summary-ignore-duplicates
4364                       ;; We ignore this one, except we add
4365                       ;; any additional Xrefs (in case the two articles
4366                       ;; came from different servers).
4367                       (progn
4368                         (mail-header-set-xref
4369                          (car (symbol-value id-dep))
4370                          (concat (or (mail-header-xref
4371                                       (car (symbol-value id-dep)))
4372                                      "")
4373                                  (or (mail-header-xref header) "")))
4374                         (setq header nil))
4375                     ;; We rename the Message-ID.
4376                     (set
4377                      (setq id-dep (intern (setq id (nnmail-message-id))
4378                                           dependencies))
4379                      (list header))
4380                     (mail-header-set-id header id))
4381                 (setcar (symbol-value id-dep) header))
4382             (set id-dep (list header)))
4383           (when header
4384             (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4385                 (setcdr (symbol-value ref-dep)
4386                         (nconc (cdr (symbol-value ref-dep))
4387                                (list (symbol-value id-dep))))
4388               (set ref-dep (list nil (symbol-value id-dep))))
4389             (push header headers))
4390           (goto-char (point-max))
4391           (widen))
4392         (nreverse headers)))))
4393
4394 ;; The following macros and functions were written by Felix Lee
4395 ;; <flee@cse.psu.edu>.
4396
4397 (defmacro gnus-nov-read-integer ()
4398   '(prog1
4399        (if (= (following-char) ?\t)
4400            0
4401          (let ((num (ignore-errors (read buffer))))
4402            (if (numberp num) num 0)))
4403      (unless (eobp)
4404        (search-forward "\t" eol 'move))))
4405
4406 (defmacro gnus-nov-skip-field ()
4407   '(search-forward "\t" eol 'move))
4408
4409 (defmacro gnus-nov-field ()
4410   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
4411
4412 ;; (defvar gnus-nov-none-counter 0)
4413
4414 ;; This function has to be called with point after the article number
4415 ;; on the beginning of the line.
4416 (defun gnus-nov-parse-line (number dependencies &optional force-new)
4417   (let ((eol (gnus-point-at-eol))
4418         (buffer (current-buffer))
4419         header ref id id-dep ref-dep)
4420
4421     ;; overview: [num subject from date id refs chars lines misc]
4422     (unwind-protect
4423         (progn
4424           (narrow-to-region (point) eol)
4425           (unless (eobp)
4426             (forward-char))
4427
4428           (setq header
4429                 (vector
4430                  number                 ; number
4431                  (funcall
4432                   gnus-unstructured-field-decoder (gnus-nov-field)) ; subject
4433                  (funcall
4434                   gnus-structured-field-decoder (gnus-nov-field)) ; from
4435                  (gnus-nov-field)       ; date
4436                  (setq id (or (gnus-nov-field)
4437                               (nnheader-generate-fake-message-id))) ; id
4438                  (progn
4439                    (let ((beg (point)))
4440                      (search-forward "\t" eol)
4441                      (if (search-backward ">" beg t)
4442                          (setq ref
4443                                (buffer-substring
4444                                 (1+ (point))
4445                                 (or (search-backward "<" beg t) beg)))
4446                        (setq ref nil))
4447                      (goto-char beg))
4448                    (gnus-nov-field))    ; refs
4449                  (gnus-nov-read-integer) ; chars
4450                  (gnus-nov-read-integer) ; lines
4451                  (if (= (following-char) ?\n)
4452                      nil
4453                    (gnus-nov-field))))) ; misc
4454
4455       (widen))
4456
4457     (when gnus-alter-header-function
4458       (funcall gnus-alter-header-function header)
4459       (setq id (mail-header-id header)
4460             ref (gnus-parent-id (mail-header-references header))))
4461     
4462     ;; We build the thread tree.
4463     (when (equal id ref)
4464       ;; This article refers back to itself.  Naughty, naughty.
4465       (setq ref nil))
4466     (if (boundp (setq id-dep (intern id dependencies)))
4467         (if (and (car (symbol-value id-dep))
4468                  (not force-new))
4469             ;; An article with this Message-ID has already been seen.
4470             (if gnus-summary-ignore-duplicates
4471                 ;; We ignore this one, except we add any additional
4472                 ;; Xrefs (in case the two articles came from different
4473                 ;; servers.
4474                 (progn
4475                   (mail-header-set-xref
4476                    (car (symbol-value id-dep))
4477                    (concat (or (mail-header-xref
4478                                 (car (symbol-value id-dep)))
4479                                "")
4480                            (or (mail-header-xref header) "")))
4481                   (setq header nil))
4482               ;; We rename the Message-ID.
4483               (set
4484                (setq id-dep (intern (setq id (nnmail-message-id))
4485                                     dependencies))
4486                (list header))
4487               (mail-header-set-id header id))
4488           (setcar (symbol-value id-dep) header))
4489       (set id-dep (list header)))
4490     (when header
4491       (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
4492           (setcdr (symbol-value ref-dep)
4493                   (nconc (cdr (symbol-value ref-dep))
4494                          (list (symbol-value id-dep))))
4495         (set ref-dep (list nil (symbol-value id-dep)))))
4496     header))
4497
4498 ;; Goes through the xover lines and returns a list of vectors
4499 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4500                                                   force-new dependencies
4501                                                   group also-fetch-heads)
4502   "Parse the news overview data in the server buffer, and return a
4503 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
4504   ;; Get the Xref when the users reads the articles since most/some
4505   ;; NNTP servers do not include Xrefs when using XOVER.
4506   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4507   (let ((cur nntp-server-buffer)
4508         (dependencies (or dependencies gnus-newsgroup-dependencies))
4509         number headers header)
4510     (save-excursion
4511       (set-buffer nntp-server-buffer)
4512       ;; Allow the user to mangle the headers before parsing them.
4513       (gnus-run-hooks 'gnus-parse-headers-hook)
4514       (goto-char (point-min))
4515       (while (not (eobp))
4516         (condition-case ()
4517             (while (and sequence (not (eobp)))
4518               (setq number (read cur))
4519               (while (and sequence
4520                           (< (car sequence) number))
4521                 (setq sequence (cdr sequence)))
4522               (and sequence
4523                    (eq number (car sequence))
4524                    (progn
4525                      (setq sequence (cdr sequence))
4526                      (setq header (inline
4527                                     (gnus-nov-parse-line
4528                                      number dependencies force-new))))
4529                    (push header headers))
4530               (forward-line 1))
4531           (error
4532            (gnus-error 4 "Strange nov line (%d)"
4533                        (count-lines (point-min) (point)))))
4534         (forward-line 1))
4535       ;; A common bug in inn is that if you have posted an article and
4536       ;; then retrieves the active file, it will answer correctly --
4537       ;; the new article is included.  However, a NOV entry for the
4538       ;; article may not have been generated yet, so this may fail.
4539       ;; We work around this problem by retrieving the last few
4540       ;; headers using HEAD.
4541       (if (or (not also-fetch-heads)
4542               (not sequence))
4543           ;; We (probably) got all the headers.
4544           (nreverse headers)
4545         (let ((gnus-nov-is-evil t))
4546           (nconc
4547            (nreverse headers)
4548            (when (gnus-retrieve-headers sequence group)
4549              (gnus-get-newsgroup-headers))))))))
4550
4551 (defun gnus-article-get-xrefs ()
4552   "Fill in the Xref value in `gnus-current-headers', if necessary.
4553 This is meant to be called in `gnus-article-internal-prepare-hook'."
4554   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4555                                  gnus-current-headers)))
4556     (or (not gnus-use-cross-reference)
4557         (not headers)
4558         (and (mail-header-xref headers)
4559              (not (string= (mail-header-xref headers) "")))
4560         (let ((case-fold-search t)
4561               xref)
4562           (save-restriction
4563             (nnheader-narrow-to-headers)
4564             (goto-char (point-min))
4565             (when (or (and (eq (downcase (following-char)) ?x)
4566                            (looking-at "Xref:"))
4567                       (search-forward "\nXref:" nil t))
4568               (goto-char (1+ (match-end 0)))
4569               (setq xref (buffer-substring (point)
4570                                            (progn (end-of-line) (point))))
4571               (mail-header-set-xref headers xref)))))))
4572
4573 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4574   "Find article ID and insert the summary line for that article."
4575   (let ((header (cond ((and old-header use-old-header)
4576                        old-header)
4577                       ((and (numberp id)
4578                             (gnus-number-to-header id))
4579                        (gnus-number-to-header id))
4580                       (t
4581                        (gnus-read-header id))))
4582         (number (and (numberp id) id))
4583         pos d)
4584     (when header
4585       ;; Rebuild the thread that this article is part of and go to the
4586       ;; article we have fetched.
4587       (when (and (not gnus-show-threads)
4588                  old-header)
4589         (when (and number
4590                    (setq d (gnus-data-find (mail-header-number old-header))))
4591           (goto-char (gnus-data-pos d))
4592           (gnus-data-remove
4593            number
4594            (- (gnus-point-at-bol)
4595               (prog1
4596                   (1+ (gnus-point-at-eol))
4597                 (gnus-delete-line))))))
4598       (when old-header
4599         (mail-header-set-number header (mail-header-number old-header)))
4600       (setq gnus-newsgroup-sparse
4601             (delq (setq number (mail-header-number header))
4602                   gnus-newsgroup-sparse))
4603       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4604       (push number gnus-newsgroup-limit)
4605       (gnus-rebuild-thread (mail-header-id header))
4606       (gnus-summary-goto-subject number nil t))
4607     (when (and (numberp number)
4608                (> number 0))
4609       ;; We have to update the boundaries even if we can't fetch the
4610       ;; article if ID is a number -- so that the next `P' or `N'
4611       ;; command will fetch the previous (or next) article even
4612       ;; if the one we tried to fetch this time has been canceled.
4613       (when (> number gnus-newsgroup-end)
4614         (setq gnus-newsgroup-end number))
4615       (when (< number gnus-newsgroup-begin)
4616         (setq gnus-newsgroup-begin number))
4617       (setq gnus-newsgroup-unselected
4618             (delq number gnus-newsgroup-unselected)))
4619     ;; Report back a success?
4620     (and header (mail-header-number header))))
4621
4622 ;;; Process/prefix in the summary buffer
4623
4624 (defun gnus-summary-work-articles (n)
4625   "Return a list of articles to be worked upon.  The prefix argument,
4626 the list of process marked articles, and the current article will be
4627 taken into consideration."
4628   (save-excursion
4629     (set-buffer gnus-summary-buffer)
4630     (cond
4631      (n
4632       ;; A numerical prefix has been given.
4633       (setq n (prefix-numeric-value n))
4634       (let ((backward (< n 0))
4635             (n (abs (prefix-numeric-value n)))
4636             articles article)
4637         (save-excursion
4638           (while
4639               (and (> n 0)
4640                    (push (setq article (gnus-summary-article-number))
4641                          articles)
4642                    (if backward
4643                        (gnus-summary-find-prev nil article)
4644                      (gnus-summary-find-next nil article)))
4645             (decf n)))
4646         (nreverse articles)))
4647      ((and (gnus-region-active-p) (mark))
4648       (message "region active")
4649       ;; Work on the region between point and mark.
4650       (let ((max (max (point) (mark)))
4651             articles article)
4652         (save-excursion
4653           (goto-char (min (point) (mark)))
4654           (while
4655               (and
4656                (push (setq article (gnus-summary-article-number)) articles)
4657                (gnus-summary-find-next nil article)
4658                (< (point) max)))
4659           (nreverse articles))))
4660      (gnus-newsgroup-processable
4661       ;; There are process-marked articles present.
4662       ;; Save current state.
4663       (gnus-summary-save-process-mark)
4664       ;; Return the list.
4665       (reverse gnus-newsgroup-processable))
4666      (t
4667       ;; Just return the current article.
4668       (list (gnus-summary-article-number))))))
4669
4670 (defun gnus-summary-save-process-mark ()
4671   "Push the current set of process marked articles on the stack."
4672   (interactive)
4673   (push (copy-sequence gnus-newsgroup-processable)
4674         gnus-newsgroup-process-stack))
4675
4676 (defun gnus-summary-kill-process-mark ()
4677   "Push the current set of process marked articles on the stack and unmark."
4678   (interactive)
4679   (gnus-summary-save-process-mark)
4680   (gnus-summary-unmark-all-processable))
4681
4682 (defun gnus-summary-yank-process-mark ()
4683   "Pop the last process mark state off the stack and restore it."
4684   (interactive)
4685   (unless gnus-newsgroup-process-stack
4686     (error "Empty mark stack"))
4687   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
4688
4689 (defun gnus-summary-process-mark-set (set)
4690   "Make SET into the current process marked articles."
4691   (gnus-summary-unmark-all-processable)
4692   (while set
4693     (gnus-summary-set-process-mark (pop set))))
4694
4695 ;;; Searching and stuff
4696
4697 (defun gnus-summary-search-group (&optional backward use-level)
4698   "Search for next unread newsgroup.
4699 If optional argument BACKWARD is non-nil, search backward instead."
4700   (save-excursion
4701     (set-buffer gnus-group-buffer)
4702     (when (gnus-group-search-forward
4703            backward nil (if use-level (gnus-group-group-level) nil))
4704       (gnus-group-group-name))))
4705
4706 (defun gnus-summary-best-group (&optional exclude-group)
4707   "Find the name of the best unread group.
4708 If EXCLUDE-GROUP, do not go to this group."
4709   (save-excursion
4710     (set-buffer gnus-group-buffer)
4711     (save-excursion
4712       (gnus-group-best-unread-group exclude-group))))
4713
4714 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
4715   (if backward (gnus-summary-find-prev)
4716     (let* ((dummy (gnus-summary-article-intangible-p))
4717            (article (or article (gnus-summary-article-number)))
4718            (arts (gnus-data-find-list article))
4719            result)
4720       (when (and (not dummy)
4721                  (or (not gnus-summary-check-current)
4722                      (not unread)
4723                      (not (gnus-data-unread-p (car arts)))))
4724         (setq arts (cdr arts)))
4725       (when (setq result
4726                   (if unread
4727                       (progn
4728                         (while arts
4729                           (when (or (and undownloaded
4730                                          (eq gnus-undownloaded-mark
4731                                              (gnus-data-mark (car arts))))
4732                                     (gnus-data-unread-p (car arts)))
4733                             (setq result (car arts)
4734                                   arts nil))
4735                           (setq arts (cdr arts)))
4736                         result)
4737                     (car arts)))
4738         (goto-char (gnus-data-pos result))
4739         (gnus-data-number result)))))
4740
4741 (defun gnus-summary-find-prev (&optional unread article)
4742   (let* ((eobp (eobp))
4743          (article (or article (gnus-summary-article-number)))
4744          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
4745          result)
4746     (when (and (not eobp)
4747                (or (not gnus-summary-check-current)
4748                    (not unread)
4749                    (not (gnus-data-unread-p (car arts)))))
4750       (setq arts (cdr arts)))
4751     (when (setq result
4752                 (if unread
4753                     (progn
4754                       (while arts
4755                         (when (gnus-data-unread-p (car arts))
4756                           (setq result (car arts)
4757                                 arts nil))
4758                         (setq arts (cdr arts)))
4759                       result)
4760                   (car arts)))
4761       (goto-char (gnus-data-pos result))
4762       (gnus-data-number result))))
4763
4764 (defun gnus-summary-find-subject (subject &optional unread backward article)
4765   (let* ((simp-subject (gnus-simplify-subject-fully subject))
4766          (article (or article (gnus-summary-article-number)))
4767          (articles (gnus-data-list backward))
4768          (arts (gnus-data-find-list article articles))
4769          result)
4770     (when (or (not gnus-summary-check-current)
4771               (not unread)
4772               (not (gnus-data-unread-p (car arts))))
4773       (setq arts (cdr arts)))
4774     (while arts
4775       (and (or (not unread)
4776                (gnus-data-unread-p (car arts)))
4777            (vectorp (gnus-data-header (car arts)))
4778            (gnus-subject-equal
4779             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
4780            (setq result (car arts)
4781                  arts nil))
4782       (setq arts (cdr arts)))
4783     (and result
4784          (goto-char (gnus-data-pos result))
4785          (gnus-data-number result))))
4786
4787 (defun gnus-summary-search-forward (&optional unread subject backward)
4788   "Search forward for an article.
4789 If UNREAD, look for unread articles.  If SUBJECT, look for
4790 articles with that subject.  If BACKWARD, search backward instead."
4791   (cond (subject (gnus-summary-find-subject subject unread backward))
4792         (backward (gnus-summary-find-prev unread))
4793         (t (gnus-summary-find-next unread))))
4794
4795 (defun gnus-recenter (&optional n)
4796   "Center point in window and redisplay frame.
4797 Also do horizontal recentering."
4798   (interactive "P")
4799   (when (and gnus-auto-center-summary
4800              (not (eq gnus-auto-center-summary 'vertical)))
4801     (gnus-horizontal-recenter))
4802   (recenter n))
4803
4804 (defun gnus-summary-recenter ()
4805   "Center point in the summary window.
4806 If `gnus-auto-center-summary' is nil, or the article buffer isn't
4807 displayed, no centering will be performed."
4808   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
4809   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
4810   (let* ((top (cond ((< (window-height) 4) 0)
4811                     ((< (window-height) 7) 1)
4812                     (t 2)))
4813          (height (1- (window-height)))
4814          (bottom (save-excursion (goto-char (point-max))
4815                                  (forward-line (- height))
4816                                  (point)))
4817          (window (get-buffer-window (current-buffer))))
4818     ;; The user has to want it.
4819     (when gnus-auto-center-summary
4820       (when (get-buffer-window gnus-article-buffer)
4821         ;; Only do recentering when the article buffer is displayed,
4822         ;; Set the window start to either `bottom', which is the biggest
4823         ;; possible valid number, or the second line from the top,
4824         ;; whichever is the least.
4825         (set-window-start
4826          window (min bottom (save-excursion
4827                               (forward-line (- top)) (point)))))
4828       ;; Do horizontal recentering while we're at it.
4829       (when (and (get-buffer-window (current-buffer) t)
4830                  (not (eq gnus-auto-center-summary 'vertical)))
4831         (let ((selected (selected-window)))
4832           (select-window (get-buffer-window (current-buffer) t))
4833           (gnus-summary-position-point)
4834           (gnus-horizontal-recenter)
4835           (select-window selected))))))
4836
4837 (defun gnus-summary-jump-to-group (newsgroup)
4838   "Move point to NEWSGROUP in group mode buffer."
4839   ;; Keep update point of group mode buffer if visible.
4840   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
4841       (save-window-excursion
4842         ;; Take care of tree window mode.
4843         (when (get-buffer-window gnus-group-buffer)
4844           (pop-to-buffer gnus-group-buffer))
4845         (gnus-group-jump-to-group newsgroup))
4846     (save-excursion
4847       ;; Take care of tree window mode.
4848       (if (get-buffer-window gnus-group-buffer)
4849           (pop-to-buffer gnus-group-buffer)
4850         (set-buffer gnus-group-buffer))
4851       (gnus-group-jump-to-group newsgroup))))
4852
4853 ;; This function returns a list of article numbers based on the
4854 ;; difference between the ranges of read articles in this group and
4855 ;; the range of active articles.
4856 (defun gnus-list-of-unread-articles (group)
4857   (let* ((read (gnus-info-read (gnus-get-info group)))
4858          (active (or (gnus-active group) (gnus-activate-group group)))
4859          (last (cdr active))
4860          first nlast unread)
4861     ;; If none are read, then all are unread.
4862     (if (not read)
4863         (setq first (car active))
4864       ;; If the range of read articles is a single range, then the
4865       ;; first unread article is the article after the last read
4866       ;; article.  Sounds logical, doesn't it?
4867       (if (not (listp (cdr read)))
4868           (setq first (1+ (cdr read)))
4869         ;; `read' is a list of ranges.
4870         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
4871                                   (caar read)))
4872                   1)
4873           (setq first 1))
4874         (while read
4875           (when first
4876             (while (< first nlast)
4877               (push first unread)
4878               (setq first (1+ first))))
4879           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
4880           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
4881           (setq read (cdr read)))))
4882     ;; And add the last unread articles.
4883     (while (<= first last)
4884       (push first unread)
4885       (setq first (1+ first)))
4886     ;; Return the list of unread articles.
4887     (delq 0 (nreverse unread))))
4888
4889 (defun gnus-list-of-read-articles (group)
4890   "Return a list of unread, unticked and non-dormant articles."
4891   (let* ((info (gnus-get-info group))
4892          (marked (gnus-info-marks info))
4893          (active (gnus-active group)))
4894     (and info active
4895          (gnus-set-difference
4896           (gnus-sorted-complement
4897            (gnus-uncompress-range active)
4898            (gnus-list-of-unread-articles group))
4899           (append
4900            (gnus-uncompress-range (cdr (assq 'dormant marked)))
4901            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
4902
4903 ;; Various summary commands
4904
4905 (defun gnus-summary-select-article-buffer ()
4906   "Reconfigure windows to show article buffer."
4907   (interactive)
4908   (if (not (gnus-buffer-live-p gnus-article-buffer))
4909       (error "There is no article buffer for this summary buffer")
4910     (gnus-configure-windows 'article)
4911     (select-window (get-buffer-window gnus-article-buffer))))
4912
4913 (defun gnus-summary-universal-argument (arg)
4914   "Perform any operation on all articles that are process/prefixed."
4915   (interactive "P")
4916   (let ((articles (gnus-summary-work-articles arg))
4917         func article)
4918     (if (eq
4919          (setq
4920           func
4921           (key-binding
4922            (read-key-sequence
4923             (substitute-command-keys
4924              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
4925              ))))
4926          'undefined)
4927         (gnus-error 1 "Undefined key")
4928       (save-excursion
4929         (while articles
4930           (gnus-summary-goto-subject (setq article (pop articles)))
4931           (let (gnus-newsgroup-processable)
4932             (command-execute func))
4933           (gnus-summary-remove-process-mark article)))))
4934   (gnus-summary-position-point))
4935
4936 (defun gnus-summary-toggle-truncation (&optional arg)
4937   "Toggle truncation of summary lines.
4938 With arg, turn line truncation on iff arg is positive."
4939   (interactive "P")
4940   (setq truncate-lines
4941         (if (null arg) (not truncate-lines)
4942           (> (prefix-numeric-value arg) 0)))
4943   (redraw-display))
4944
4945 (defun gnus-summary-reselect-current-group (&optional all rescan)
4946   "Exit and then reselect the current newsgroup.
4947 The prefix argument ALL means to select all articles."
4948   (interactive "P")
4949   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
4950     (error "Ephemeral groups can't be reselected"))
4951   (let ((current-subject (gnus-summary-article-number))
4952         (group gnus-newsgroup-name))
4953     (setq gnus-newsgroup-begin nil)
4954     (gnus-summary-exit)
4955     ;; We have to adjust the point of group mode buffer because
4956     ;; point was moved to the next unread newsgroup by exiting.
4957     (gnus-summary-jump-to-group group)
4958     (when rescan
4959       (save-excursion
4960         (gnus-group-get-new-news-this-group 1)))
4961     (gnus-group-read-group all t)
4962     (gnus-summary-goto-subject current-subject nil t)))
4963
4964 (defun gnus-summary-rescan-group (&optional all)
4965   "Exit the newsgroup, ask for new articles, and select the newsgroup."
4966   (interactive "P")
4967   (gnus-summary-reselect-current-group all t))
4968
4969 (defun gnus-summary-update-info (&optional non-destructive)
4970   (save-excursion
4971     (let ((group gnus-newsgroup-name))
4972       (when group
4973         (when gnus-newsgroup-kill-headers
4974           (setq gnus-newsgroup-killed
4975                 (gnus-compress-sequence
4976                  (nconc
4977                   (gnus-set-sorted-intersection
4978                    (gnus-uncompress-range gnus-newsgroup-killed)
4979                    (setq gnus-newsgroup-unselected
4980                          (sort gnus-newsgroup-unselected '<)))
4981                   (setq gnus-newsgroup-unreads
4982                         (sort gnus-newsgroup-unreads '<)))
4983                  t)))
4984         (unless (listp (cdr gnus-newsgroup-killed))
4985           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
4986         (let ((headers gnus-newsgroup-headers))
4987           ;; Set the new ranges of read articles.
4988           (save-excursion
4989             (set-buffer gnus-group-buffer)
4990             (gnus-undo-force-boundary))
4991           (gnus-update-read-articles
4992            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
4993           ;; Set the current article marks.
4994           (let ((gnus-newsgroup-scored 
4995                  (if (and (not gnus-save-score)
4996                           (not non-destructive))
4997                      nil
4998                    gnus-newsgroup-scored)))
4999             (save-excursion
5000               (gnus-update-marks)))
5001           ;; Do the cross-ref thing.
5002           (when gnus-use-cross-reference
5003             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5004           ;; Do not switch windows but change the buffer to work.
5005           (set-buffer gnus-group-buffer)
5006           (unless (gnus-ephemeral-group-p group)
5007             (gnus-group-update-group group)))))))
5008
5009 (defun gnus-summary-save-newsrc (&optional force)
5010   "Save the current number of read/marked articles in the dribble buffer.
5011 The dribble buffer will then be saved.
5012 If FORCE (the prefix), also save the .newsrc file(s)."
5013   (interactive "P")
5014   (gnus-summary-update-info t)
5015   (if force
5016       (gnus-save-newsrc-file)
5017     (gnus-dribble-save)))
5018
5019 (defun gnus-summary-exit (&optional temporary)
5020   "Exit reading current newsgroup, and then return to group selection mode.
5021 gnus-exit-group-hook is called with no arguments if that value is non-nil."
5022   (interactive)
5023   (gnus-set-global-variables)
5024   (gnus-kill-save-kill-buffer)
5025   (gnus-async-halt-prefetch)
5026   (let* ((group gnus-newsgroup-name)
5027          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5028          (mode major-mode)
5029          (group-point nil)
5030          (buf (current-buffer)))
5031     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5032     ;; If we have several article buffers, we kill them at exit.
5033     (unless gnus-single-article-buffer
5034       (gnus-kill-buffer gnus-original-article-buffer)
5035       (setq gnus-article-current nil))
5036     (when gnus-use-cache
5037       (gnus-cache-possibly-remove-articles)
5038       (gnus-cache-save-buffers))
5039     (gnus-async-prefetch-remove-group group)
5040     (when gnus-suppress-duplicates
5041       (gnus-dup-enter-articles))
5042     (when gnus-use-trees
5043       (gnus-tree-close group))
5044     ;; Remove entries for this group.
5045     (nnmail-purge-split-history (gnus-group-real-name group))
5046     ;; Make all changes in this group permanent.
5047     (unless quit-config
5048       (gnus-run-hooks 'gnus-exit-group-hook)
5049       (gnus-summary-update-info)
5050       ;; Do adaptive scoring, and possibly save score files.
5051       (when gnus-newsgroup-adaptive
5052         (gnus-score-adaptive))
5053       (when gnus-use-scoring
5054         (gnus-score-save)))
5055     (gnus-close-group group)
5056     ;; Make sure where we were, and go to next newsgroup.
5057     (set-buffer gnus-group-buffer)
5058     (unless quit-config
5059       (gnus-group-jump-to-group group))
5060     (gnus-run-hooks 'gnus-summary-exit-hook)
5061     (unless (or quit-config
5062                 ;; If this group has disappeared from the summary
5063                 ;; buffer, don't skip forwards.
5064                 (not (string= group (gnus-group-group-name))))
5065       (gnus-group-next-unread-group 1))
5066     (setq group-point (point))
5067     (if temporary
5068         nil                             ;Nothing to do.
5069       ;; If we have several article buffers, we kill them at exit.
5070       (unless gnus-single-article-buffer
5071         (gnus-kill-buffer gnus-article-buffer)
5072         (gnus-kill-buffer gnus-original-article-buffer)
5073         (setq gnus-article-current nil))
5074       (set-buffer buf)
5075       (if (not gnus-kill-summary-on-exit)
5076           (gnus-deaden-summary)
5077         ;; We set all buffer-local variables to nil.  It is unclear why
5078         ;; this is needed, but if we don't, buffer-local variables are
5079         ;; not garbage-collected, it seems.  This would the lead to en
5080         ;; ever-growing Emacs.
5081         (gnus-summary-clear-local-variables)
5082         (when (get-buffer gnus-article-buffer)
5083           (bury-buffer gnus-article-buffer))
5084         ;; We clear the global counterparts of the buffer-local
5085         ;; variables as well, just to be on the safe side.
5086         (set-buffer gnus-group-buffer)
5087         (gnus-summary-clear-local-variables)
5088         ;; Return to group mode buffer.
5089         (when (eq mode 'gnus-summary-mode)
5090           (gnus-kill-buffer buf)))
5091       (setq gnus-current-select-method gnus-select-method)
5092       (pop-to-buffer gnus-group-buffer)
5093       ;; Clear the current group name.
5094       (if (not quit-config)
5095           (progn
5096             (goto-char group-point)
5097             (gnus-configure-windows 'group 'force))
5098         (gnus-handle-ephemeral-exit quit-config))
5099       (unless quit-config
5100         (setq gnus-newsgroup-name nil)))))
5101
5102 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5103 (defun gnus-summary-exit-no-update (&optional no-questions)
5104   "Quit reading current newsgroup without updating read article info."
5105   (interactive)
5106   (let* ((group gnus-newsgroup-name)
5107          (quit-config (gnus-group-quit-config group)))
5108     (when (or no-questions
5109               gnus-expert-user
5110               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5111       (gnus-async-halt-prefetch)
5112       (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5113       ;; If we have several article buffers, we kill them at exit.
5114       (unless gnus-single-article-buffer
5115         (gnus-kill-buffer gnus-article-buffer)
5116         (gnus-kill-buffer gnus-original-article-buffer)
5117         (setq gnus-article-current nil))
5118       (if (not gnus-kill-summary-on-exit)
5119           (gnus-deaden-summary)
5120         (gnus-close-group group)
5121         (gnus-summary-clear-local-variables)
5122         (set-buffer gnus-group-buffer)
5123         (gnus-summary-clear-local-variables)
5124         (when (get-buffer gnus-summary-buffer)
5125           (kill-buffer gnus-summary-buffer)))
5126       (unless gnus-single-article-buffer
5127         (setq gnus-article-current nil))
5128       (when gnus-use-trees
5129         (gnus-tree-close group))
5130       (gnus-async-prefetch-remove-group group)
5131       (when (get-buffer gnus-article-buffer)
5132         (bury-buffer gnus-article-buffer))
5133       ;; Return to the group buffer.
5134       (gnus-configure-windows 'group 'force)
5135       ;; Clear the current group name.
5136       (setq gnus-newsgroup-name nil)
5137       (when (equal (gnus-group-group-name) group)
5138         (gnus-group-next-unread-group 1))
5139       (when quit-config
5140         (gnus-handle-ephemeral-exit quit-config)))))
5141
5142 (defun gnus-handle-ephemeral-exit (quit-config)
5143   "Handle movement when leaving an ephemeral group.  The state
5144 which existed when entering the ephemeral is reset."
5145   (if (not (buffer-name (car quit-config)))
5146       (gnus-configure-windows 'group 'force)
5147     (set-buffer (car quit-config))
5148     (cond ((eq major-mode 'gnus-summary-mode)
5149            (gnus-set-global-variables))
5150           ((eq major-mode 'gnus-article-mode)
5151            (save-excursion
5152              ;; The `gnus-summary-buffer' variable may point
5153              ;; to the old summary buffer when using a single
5154              ;; article buffer.
5155              (unless (gnus-buffer-live-p gnus-summary-buffer)
5156                (set-buffer gnus-group-buffer))
5157              (set-buffer gnus-summary-buffer)
5158              (gnus-set-global-variables))))
5159     (if (or (eq (cdr quit-config) 'article)
5160             (eq (cdr quit-config) 'pick))
5161         (progn
5162           ;; The current article may be from the ephemeral group
5163           ;; thus it is best that we reload this article
5164           (gnus-summary-show-article)
5165           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5166               (gnus-configure-windows 'pick 'force)
5167             (gnus-configure-windows (cdr quit-config) 'force)))
5168       (gnus-configure-windows (cdr quit-config) 'force))
5169     (when (eq major-mode 'gnus-summary-mode)
5170       (gnus-summary-next-subject 1 nil t)
5171       (gnus-summary-recenter)
5172       (gnus-summary-position-point))))
5173
5174 (defun gnus-summary-preview-mime-message (arg)
5175   "MIME decode and play this message."
5176   (interactive "P")
5177   (let ((gnus-break-pages nil))
5178     (gnus-summary-select-article t t)
5179     )
5180   (pop-to-buffer gnus-original-article-buffer t)
5181   (let (buffer-read-only)
5182     (if (text-property-any (point-min) (point-max) 'invisible t)
5183         (remove-text-properties (point-min) (point-max)
5184                                 gnus-hidden-properties)
5185       ))
5186   (mime-view-mode nil nil nil gnus-original-article-buffer
5187                   gnus-article-buffer)
5188   )
5189
5190 (defun gnus-summary-scroll-down ()
5191   "Scroll down one line current article."
5192   (interactive)
5193   (gnus-summary-scroll-up -1)
5194   )
5195
5196 ;;; Dead summaries.
5197
5198 (defvar gnus-dead-summary-mode-map nil)
5199
5200 (unless gnus-dead-summary-mode-map
5201   (setq gnus-dead-summary-mode-map (make-keymap))
5202   (suppress-keymap gnus-dead-summary-mode-map)
5203   (substitute-key-definition
5204    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5205   (let ((keys '("\C-d" "\r" "\177" [delete])))
5206     (while keys
5207       (define-key gnus-dead-summary-mode-map
5208         (pop keys) 'gnus-summary-wake-up-the-dead))))
5209
5210 (defvar gnus-dead-summary-mode nil
5211   "Minor mode for Gnus summary buffers.")
5212
5213 (defun gnus-dead-summary-mode (&optional arg)
5214   "Minor mode for Gnus summary buffers."
5215   (interactive "P")
5216   (when (eq major-mode 'gnus-summary-mode)
5217     (make-local-variable 'gnus-dead-summary-mode)
5218     (setq gnus-dead-summary-mode
5219           (if (null arg) (not gnus-dead-summary-mode)
5220             (> (prefix-numeric-value arg) 0)))
5221     (when gnus-dead-summary-mode
5222       (gnus-add-minor-mode
5223        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5224
5225 (defun gnus-deaden-summary ()
5226   "Make the current summary buffer into a dead summary buffer."
5227   ;; Kill any previous dead summary buffer.
5228   (when (and gnus-dead-summary
5229              (buffer-name gnus-dead-summary))
5230     (save-excursion
5231       (set-buffer gnus-dead-summary)
5232       (when gnus-dead-summary-mode
5233         (kill-buffer (current-buffer)))))
5234   ;; Make this the current dead summary.
5235   (setq gnus-dead-summary (current-buffer))
5236   (gnus-dead-summary-mode 1)
5237   (let ((name (buffer-name)))
5238     (when (string-match "Summary" name)
5239       (rename-buffer
5240        (concat (substring name 0 (match-beginning 0)) "Dead "
5241                (substring name (match-beginning 0)))
5242        t))))
5243
5244 (defun gnus-kill-or-deaden-summary (buffer)
5245   "Kill or deaden the summary BUFFER."
5246   (save-excursion
5247     (when (and (buffer-name buffer)
5248                (not gnus-single-article-buffer))
5249       (save-excursion
5250         (set-buffer buffer)
5251         (gnus-kill-buffer gnus-article-buffer)
5252         (gnus-kill-buffer gnus-original-article-buffer)))
5253     (cond (gnus-kill-summary-on-exit
5254            (when (and gnus-use-trees
5255                       (gnus-buffer-exists-p buffer))
5256              (save-excursion
5257                (set-buffer buffer)
5258                (gnus-tree-close gnus-newsgroup-name)))
5259            (gnus-kill-buffer buffer))
5260           ((gnus-buffer-exists-p buffer)
5261            (save-excursion
5262              (set-buffer buffer)
5263              (gnus-deaden-summary))))))
5264
5265 (defun gnus-summary-wake-up-the-dead (&rest args)
5266   "Wake up the dead summary buffer."
5267   (interactive)
5268   (gnus-dead-summary-mode -1)
5269   (let ((name (buffer-name)))
5270     (when (string-match "Dead " name)
5271       (rename-buffer
5272        (concat (substring name 0 (match-beginning 0))
5273                (substring name (match-end 0)))
5274        t)))
5275   (gnus-message 3 "This dead summary is now alive again"))
5276
5277 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5278 (defun gnus-summary-fetch-faq (&optional faq-dir)
5279   "Fetch the FAQ for the current group.
5280 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5281 in."
5282   (interactive
5283    (list
5284     (when current-prefix-arg
5285       (completing-read
5286        "Faq dir: " (and (listp gnus-group-faq-directory)
5287                         (mapcar (lambda (file) (list file))
5288                                 gnus-group-faq-directory))))))
5289   (let (gnus-faq-buffer)
5290     (when (setq gnus-faq-buffer
5291                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5292       (gnus-configure-windows 'summary-faq))))
5293
5294 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5295 (defun gnus-summary-describe-group (&optional force)
5296   "Describe the current newsgroup."
5297   (interactive "P")
5298   (gnus-group-describe-group force gnus-newsgroup-name))
5299
5300 (defun gnus-summary-describe-briefly ()
5301   "Describe summary mode commands briefly."
5302   (interactive)
5303   (gnus-message 6
5304                 (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")))
5305
5306 ;; Walking around group mode buffer from summary mode.
5307
5308 (defun gnus-summary-next-group (&optional no-article target-group backward)
5309   "Exit current newsgroup and then select next unread newsgroup.
5310 If prefix argument NO-ARTICLE is non-nil, no article is selected
5311 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5312 previous group instead."
5313   (interactive "P")
5314   ;; Stop pre-fetching.
5315   (gnus-async-halt-prefetch)
5316   (let ((current-group gnus-newsgroup-name)
5317         (current-buffer (current-buffer))
5318         entered)
5319     ;; First we semi-exit this group to update Xrefs and all variables.
5320     ;; We can't do a real exit, because the window conf must remain
5321     ;; the same in case the user is prompted for info, and we don't
5322     ;; want the window conf to change before that...
5323     (gnus-summary-exit t)
5324     (while (not entered)
5325       ;; Then we find what group we are supposed to enter.
5326       (set-buffer gnus-group-buffer)
5327       (gnus-group-jump-to-group current-group)
5328       (setq target-group
5329             (or target-group
5330                 (if (eq gnus-keep-same-level 'best)
5331                     (gnus-summary-best-group gnus-newsgroup-name)
5332                   (gnus-summary-search-group backward gnus-keep-same-level))))
5333       (if (not target-group)
5334           ;; There are no further groups, so we return to the group
5335           ;; buffer.
5336           (progn
5337             (gnus-message 5 "Returning to the group buffer")
5338             (setq entered t)
5339             (when (gnus-buffer-live-p current-buffer)
5340               (set-buffer current-buffer)
5341               (gnus-summary-exit))
5342             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5343         ;; We try to enter the target group.
5344         (gnus-group-jump-to-group target-group)
5345         (let ((unreads (gnus-group-group-unread)))
5346           (if (and (or (eq t unreads)
5347                        (and unreads (not (zerop unreads))))
5348                    (gnus-summary-read-group
5349                     target-group nil no-article
5350                     (and (buffer-name current-buffer) current-buffer)
5351                     nil backward))
5352               (setq entered t)
5353             (setq current-group target-group
5354                   target-group nil)))))))
5355
5356 (defun gnus-summary-prev-group (&optional no-article)
5357   "Exit current newsgroup and then select previous unread newsgroup.
5358 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5359   (interactive "P")
5360   (gnus-summary-next-group no-article nil t))
5361
5362 ;; Walking around summary lines.
5363
5364 (defun gnus-summary-first-subject (&optional unread undownloaded)
5365   "Go to the first unread subject.
5366 If UNREAD is non-nil, go to the first unread article.
5367 Returns the article selected or nil if there are no unread articles."
5368   (interactive "P")
5369   (prog1
5370       (cond
5371        ;; Empty summary.
5372        ((null gnus-newsgroup-data)
5373         (gnus-message 3 "No articles in the group")
5374         nil)
5375        ;; Pick the first article.
5376        ((not unread)
5377         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5378         (gnus-data-number (car gnus-newsgroup-data)))
5379        ;; No unread articles.
5380        ((null gnus-newsgroup-unreads)
5381         (gnus-message 3 "No more unread articles")
5382         nil)
5383        ;; Find the first unread article.
5384        (t
5385         (let ((data gnus-newsgroup-data))
5386           (while (and data
5387                       (and (not (and undownloaded
5388                                      (eq gnus-undownloaded-mark
5389                                          (gnus-data-mark (car data)))))
5390                            (not (gnus-data-unread-p (car data)))))
5391             (setq data (cdr data)))
5392           (when data
5393             (goto-char (gnus-data-pos (car data)))
5394             (gnus-data-number (car data))))))
5395     (gnus-summary-position-point)))
5396
5397 (defun gnus-summary-next-subject (n &optional unread dont-display)
5398   "Go to next N'th summary line.
5399 If N is negative, go to the previous N'th subject line.
5400 If UNREAD is non-nil, only unread articles are selected.
5401 The difference between N and the actual number of steps taken is
5402 returned."
5403   (interactive "p")
5404   (let ((backward (< n 0))
5405         (n (abs n)))
5406     (while (and (> n 0)
5407                 (if backward
5408                     (gnus-summary-find-prev unread)
5409                   (gnus-summary-find-next unread)))
5410       (gnus-summary-show-thread)
5411       (setq n (1- n)))
5412     (when (/= 0 n)
5413       (gnus-message 7 "No more%s articles"
5414                     (if unread " unread" "")))
5415     (unless dont-display
5416       (gnus-summary-recenter)
5417       (gnus-summary-position-point))
5418     n))
5419
5420 (defun gnus-summary-next-unread-subject (n)
5421   "Go to next N'th unread summary line."
5422   (interactive "p")
5423   (gnus-summary-next-subject n t))
5424
5425 (defun gnus-summary-prev-subject (n &optional unread)
5426   "Go to previous N'th summary line.
5427 If optional argument UNREAD is non-nil, only unread article is selected."
5428   (interactive "p")
5429   (gnus-summary-next-subject (- n) unread))
5430
5431 (defun gnus-summary-prev-unread-subject (n)
5432   "Go to previous N'th unread summary line."
5433   (interactive "p")
5434   (gnus-summary-next-subject (- n) t))
5435
5436 (defun gnus-summary-goto-subject (article &optional force silent)
5437   "Go the subject line of ARTICLE.
5438 If FORCE, also allow jumping to articles not currently shown."
5439   (interactive "nArticle number: ")
5440   (let ((b (point))
5441         (data (gnus-data-find article)))
5442     ;; We read in the article if we have to.
5443     (and (not data)
5444          force
5445          (gnus-summary-insert-subject article (and (vectorp force) force) t)
5446          (setq data (gnus-data-find article)))
5447     (goto-char b)
5448     (if (not data)
5449         (progn
5450           (unless silent
5451             (gnus-message 3 "Can't find article %d" article))
5452           nil)
5453       (goto-char (gnus-data-pos data))
5454       article)))
5455
5456 ;; Walking around summary lines with displaying articles.
5457
5458 (defun gnus-summary-expand-window (&optional arg)
5459   "Make the summary buffer take up the entire Emacs frame.
5460 Given a prefix, will force an `article' buffer configuration."
5461   (interactive "P")
5462   (if arg
5463       (gnus-configure-windows 'article 'force)
5464     (gnus-configure-windows 'summary 'force)))
5465
5466 (defun gnus-summary-display-article (article &optional all-header)
5467   "Display ARTICLE in article buffer."
5468   (gnus-set-global-variables)
5469   (if (null article)
5470       nil
5471     (prog1
5472         (if gnus-summary-display-article-function
5473             (funcall gnus-summary-display-article-function article all-header)
5474           (gnus-article-prepare article all-header))
5475       (gnus-run-hooks 'gnus-select-article-hook)
5476       (when (and gnus-current-article
5477                  (not (zerop gnus-current-article)))
5478         (gnus-summary-goto-subject gnus-current-article))
5479       (gnus-summary-recenter)
5480       (when (and gnus-use-trees gnus-show-threads)
5481         (gnus-possibly-generate-tree article)
5482         (gnus-highlight-selected-tree article))
5483       ;; Successfully display article.
5484       (gnus-article-set-window-start
5485        (cdr (assq article gnus-newsgroup-bookmarks))))))
5486
5487 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5488   "Select the current article.
5489 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5490 non-nil, the article will be re-fetched even if it already present in
5491 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5492 be displayed."
5493   ;; Make sure we are in the summary buffer to work around bbdb bug.
5494   (unless (eq major-mode 'gnus-summary-mode)
5495     (set-buffer gnus-summary-buffer))
5496   (let ((article (or article (gnus-summary-article-number)))
5497         (all-headers (not (not all-headers))) ;Must be T or NIL.
5498         gnus-summary-display-article-function
5499         did)
5500     (and (not pseudo)
5501          (gnus-summary-article-pseudo-p article)
5502          (error "This is a pseudo-article"))
5503     (prog1
5504         (save-excursion
5505           (set-buffer gnus-summary-buffer)
5506           (if (or (and gnus-single-article-buffer
5507                        (or (null gnus-current-article)
5508                            (null gnus-article-current)
5509                            (null (get-buffer gnus-article-buffer))
5510                            (not (eq article (cdr gnus-article-current)))
5511                            (not (equal (car gnus-article-current)
5512                                        gnus-newsgroup-name))))
5513                   (and (not gnus-single-article-buffer)
5514                        (or (null gnus-current-article)
5515                            (not (eq gnus-current-article article))))
5516                   force)
5517               ;; The requested article is different from the current article.
5518               (prog1
5519                   (gnus-summary-display-article article all-headers)
5520                 (setq did article))
5521             (when (or all-headers gnus-show-all-headers)
5522               (gnus-article-show-all-headers))
5523             'old))
5524       (when did
5525         (gnus-article-set-window-start
5526          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5527
5528 (defun gnus-summary-set-current-mark (&optional current-mark)
5529   "Obsolete function."
5530   nil)
5531
5532 (defun gnus-summary-next-article (&optional unread subject backward push)
5533   "Select the next article.
5534 If UNREAD, only unread articles are selected.
5535 If SUBJECT, only articles with SUBJECT are selected.
5536 If BACKWARD, the previous article is selected instead of the next."
5537   (interactive "P")
5538   (cond
5539    ;; Is there such an article?
5540    ((and (gnus-summary-search-forward unread subject backward)
5541          (or (gnus-summary-display-article (gnus-summary-article-number))
5542              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5543     (gnus-summary-position-point))
5544    ;; If not, we try the first unread, if that is wanted.
5545    ((and subject
5546          gnus-auto-select-same
5547          (gnus-summary-first-unread-article))
5548     (gnus-summary-position-point)
5549     (gnus-message 6 "Wrapped"))
5550    ;; Try to get next/previous article not displayed in this group.
5551    ((and gnus-auto-extend-newsgroup
5552          (not unread) (not subject))
5553     (gnus-summary-goto-article
5554      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5555      nil t))
5556    ;; Go to next/previous group.
5557    (t
5558     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5559       (gnus-summary-jump-to-group gnus-newsgroup-name))
5560     (let ((cmd last-command-char)
5561           (point
5562            (save-excursion
5563              (set-buffer gnus-group-buffer)
5564              (point)))
5565           (group
5566            (if (eq gnus-keep-same-level 'best)
5567                (gnus-summary-best-group gnus-newsgroup-name)
5568              (gnus-summary-search-group backward gnus-keep-same-level))))
5569       ;; For some reason, the group window gets selected.  We change
5570       ;; it back.
5571       (select-window (get-buffer-window (current-buffer)))
5572       ;; Select next unread newsgroup automagically.
5573       (cond
5574        ((or (not gnus-auto-select-next)
5575             (not cmd))
5576         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5577        ((or (eq gnus-auto-select-next 'quietly)
5578             (and (eq gnus-auto-select-next 'slightly-quietly)
5579                  push)
5580             (and (eq gnus-auto-select-next 'almost-quietly)
5581                  (gnus-summary-last-article-p)))
5582         ;; Select quietly.
5583         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5584             (gnus-summary-exit)
5585           (gnus-message 7 "No more%s articles (%s)..."
5586                         (if unread " unread" "")
5587                         (if group (concat "selecting " group)
5588                           "exiting"))
5589           (gnus-summary-next-group nil group backward)))
5590        (t
5591         (when (gnus-key-press-event-p last-input-event)
5592           (gnus-summary-walk-group-buffer
5593            gnus-newsgroup-name cmd unread backward point))))))))
5594
5595 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5596   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5597                       (?\C-p (gnus-group-prev-unread-group 1))))
5598         (cursor-in-echo-area t)
5599         keve key group ended)
5600     (save-excursion
5601       (set-buffer gnus-group-buffer)
5602       (goto-char start)
5603       (setq group
5604             (if (eq gnus-keep-same-level 'best)
5605                 (gnus-summary-best-group gnus-newsgroup-name)
5606               (gnus-summary-search-group backward gnus-keep-same-level))))
5607     (while (not ended)
5608       (gnus-message
5609        5 "No more%s articles%s" (if unread " unread" "")
5610        (if (and group
5611                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5612            (format " (Type %s for %s [%s])"
5613                    (single-key-description cmd) group
5614                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5615          (format " (Type %s to exit %s)"
5616                  (single-key-description cmd)
5617                  gnus-newsgroup-name)))
5618       ;; Confirm auto selection.
5619       (setq key (car (setq keve (gnus-read-event-char))))
5620       (setq ended t)
5621       (cond
5622        ((assq key keystrokes)
5623         (let ((obuf (current-buffer)))
5624           (switch-to-buffer gnus-group-buffer)
5625           (when group
5626             (gnus-group-jump-to-group group))
5627           (eval (cadr (assq key keystrokes)))
5628           (setq group (gnus-group-group-name))
5629           (switch-to-buffer obuf))
5630         (setq ended nil))
5631        ((equal key cmd)
5632         (if (or (not group)
5633                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5634             (gnus-summary-exit)
5635           (gnus-summary-next-group nil group backward)))
5636        (t
5637         (push (cdr keve) unread-command-events))))))
5638
5639 (defun gnus-summary-next-unread-article ()
5640   "Select unread article after current one."
5641   (interactive)
5642   (gnus-summary-next-article
5643    (or (not (eq gnus-summary-goto-unread 'never))
5644        (gnus-summary-last-article-p (gnus-summary-article-number)))
5645    (and gnus-auto-select-same
5646         (gnus-summary-article-subject))))
5647
5648 (defun gnus-summary-prev-article (&optional unread subject)
5649   "Select the article after the current one.
5650 If UNREAD is non-nil, only unread articles are selected."
5651   (interactive "P")
5652   (gnus-summary-next-article unread subject t))
5653
5654 (defun gnus-summary-prev-unread-article ()
5655   "Select unread article before current one."
5656   (interactive)
5657   (gnus-summary-prev-article
5658    (or (not (eq gnus-summary-goto-unread 'never))
5659        (gnus-summary-first-article-p (gnus-summary-article-number)))
5660    (and gnus-auto-select-same
5661         (gnus-summary-article-subject))))
5662
5663 (defun gnus-summary-next-page (&optional lines circular)
5664   "Show next page of the selected article.
5665 If at the end of the current article, select the next article.
5666 LINES says how many lines should be scrolled up.
5667
5668 If CIRCULAR is non-nil, go to the start of the article instead of
5669 selecting the next article when reaching the end of the current
5670 article."
5671   (interactive "P")
5672   (setq gnus-summary-buffer (current-buffer))
5673   (gnus-set-global-variables)
5674   (let ((article (gnus-summary-article-number))
5675         (article-window (get-buffer-window gnus-article-buffer t))
5676         endp)
5677     ;; If the buffer is empty, we have no article.
5678     (unless article
5679       (error "No article to select"))
5680     (gnus-configure-windows 'article)
5681     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5682         (if (and (eq gnus-summary-goto-unread 'never)
5683                  (not (gnus-summary-last-article-p article)))
5684             (gnus-summary-next-article)
5685           (gnus-summary-next-unread-article))
5686       (if (or (null gnus-current-article)
5687               (null gnus-article-current)
5688               (/= article (cdr gnus-article-current))
5689               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5690           ;; Selected subject is different from current article's.
5691           (gnus-summary-display-article article)
5692         (when article-window
5693           (gnus-eval-in-buffer-window gnus-article-buffer
5694             (setq endp (gnus-article-next-page lines)))
5695           (when endp
5696             (cond (circular
5697                    (gnus-summary-beginning-of-article))
5698                   (lines
5699                    (gnus-message 3 "End of message"))
5700                   ((null lines)
5701                    (if (and (eq gnus-summary-goto-unread 'never)
5702                             (not (gnus-summary-last-article-p article)))
5703                        (gnus-summary-next-article)
5704                      (gnus-summary-next-unread-article))))))))
5705     (gnus-summary-recenter)
5706     (gnus-summary-position-point)))
5707
5708 (defun gnus-summary-prev-page (&optional lines move)
5709   "Show previous page of selected article.
5710 Argument LINES specifies lines to be scrolled down.
5711 If MOVE, move to the previous unread article if point is at
5712 the beginning of the buffer."
5713   (interactive "P")
5714   (let ((article (gnus-summary-article-number))
5715         (article-window (get-buffer-window gnus-article-buffer t))
5716         endp)
5717     (gnus-configure-windows 'article)
5718     (if (or (null gnus-current-article)
5719             (null gnus-article-current)
5720             (/= article (cdr gnus-article-current))
5721             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5722         ;; Selected subject is different from current article's.
5723         (gnus-summary-display-article article)
5724       (gnus-summary-recenter)
5725       (when article-window
5726         (gnus-eval-in-buffer-window gnus-article-buffer
5727           (setq endp (gnus-article-prev-page lines)))
5728         (when (and move endp)
5729           (cond (lines
5730                  (gnus-message 3 "Beginning of message"))
5731                 ((null lines)
5732                  (if (and (eq gnus-summary-goto-unread 'never)
5733                           (not (gnus-summary-first-article-p article)))
5734                      (gnus-summary-prev-article)
5735                    (gnus-summary-prev-unread-article))))))))
5736   (gnus-summary-position-point))
5737
5738 (defun gnus-summary-prev-page-or-article (&optional lines)
5739   "Show previous page of selected article.
5740 Argument LINES specifies lines to be scrolled down.
5741 If at the beginning of the article, go to the next article."
5742   (interactive "P")
5743   (gnus-summary-prev-page lines t))
5744
5745 (defun gnus-summary-scroll-up (lines)
5746   "Scroll up (or down) one line current article.
5747 Argument LINES specifies lines to be scrolled up (or down if negative)."
5748   (interactive "p")
5749   (gnus-configure-windows 'article)
5750   (gnus-summary-show-thread)
5751   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5752     (gnus-eval-in-buffer-window gnus-article-buffer
5753       (cond ((> lines 0)
5754              (when (gnus-article-next-page lines)
5755                (gnus-message 3 "End of message")))
5756             ((< lines 0)
5757              (gnus-article-prev-page (- lines))))))
5758   (gnus-summary-recenter)
5759   (gnus-summary-position-point))
5760
5761 (defun gnus-summary-next-same-subject ()
5762   "Select next article which has the same subject as current one."
5763   (interactive)
5764   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5765
5766 (defun gnus-summary-prev-same-subject ()
5767   "Select previous article which has the same subject as current one."
5768   (interactive)
5769   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5770
5771 (defun gnus-summary-next-unread-same-subject ()
5772   "Select next unread article which has the same subject as current one."
5773   (interactive)
5774   (gnus-summary-next-article t (gnus-summary-article-subject)))
5775
5776 (defun gnus-summary-prev-unread-same-subject ()
5777   "Select previous unread article which has the same subject as current one."
5778   (interactive)
5779   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5780
5781 (defun gnus-summary-first-unread-article ()
5782   "Select the first unread article.
5783 Return nil if there are no unread articles."
5784   (interactive)
5785   (prog1
5786       (when (gnus-summary-first-subject t)
5787         (gnus-summary-show-thread)
5788         (gnus-summary-first-subject t)
5789         (gnus-summary-display-article (gnus-summary-article-number)))
5790     (gnus-summary-position-point)))
5791
5792 (defun gnus-summary-first-article ()
5793   "Select the first article.
5794 Return nil if there are no articles."
5795   (interactive)
5796   (prog1
5797       (when (gnus-summary-first-subject)
5798       (gnus-summary-show-thread)
5799       (gnus-summary-first-subject)
5800       (gnus-summary-display-article (gnus-summary-article-number)))
5801     (gnus-summary-position-point)))
5802
5803 (defun gnus-summary-best-unread-article ()
5804   "Select the unread article with the highest score."
5805   (interactive)
5806   (let ((best -1000000)
5807         (data gnus-newsgroup-data)
5808         article score)
5809     (while data
5810       (and (gnus-data-unread-p (car data))
5811            (> (setq score
5812                     (gnus-summary-article-score (gnus-data-number (car data))))
5813               best)
5814            (setq best score
5815                  article (gnus-data-number (car data))))
5816       (setq data (cdr data)))
5817     (prog1
5818         (if article
5819             (gnus-summary-goto-article article)
5820           (error "No unread articles"))
5821       (gnus-summary-position-point))))
5822
5823 (defun gnus-summary-last-subject ()
5824   "Go to the last displayed subject line in the group."
5825   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5826     (when article
5827       (gnus-summary-goto-subject article))))
5828
5829 (defun gnus-summary-goto-article (article &optional all-headers force)
5830   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5831 If ALL-HEADERS is non-nil, no header lines are hidden."
5832   (interactive
5833    (list
5834     (completing-read
5835      "Article number or Message-ID: "
5836      (mapcar (lambda (number) (list (int-to-string number)))
5837              gnus-newsgroup-limit))
5838     current-prefix-arg
5839     t))
5840   (prog1
5841       (if (and (stringp article)
5842                (string-match "@" article))
5843           (gnus-summary-refer-article article)
5844         (when (stringp article)
5845           (setq article (string-to-number article)))
5846         (if (gnus-summary-goto-subject article force)
5847             (gnus-summary-display-article article all-headers)
5848           (gnus-message 4 "Couldn't go to article %s" article) nil))
5849     (gnus-summary-position-point)))
5850
5851 (defun gnus-summary-goto-last-article ()
5852   "Go to the previously read article."
5853   (interactive)
5854   (prog1
5855       (when gnus-last-article
5856         (gnus-summary-goto-article gnus-last-article nil t))
5857     (gnus-summary-position-point)))
5858
5859 (defun gnus-summary-pop-article (number)
5860   "Pop one article off the history and go to the previous.
5861 NUMBER articles will be popped off."
5862   (interactive "p")
5863   (let (to)
5864     (setq gnus-newsgroup-history
5865           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5866     (if to
5867         (gnus-summary-goto-article (car to) nil t)
5868       (error "Article history empty")))
5869   (gnus-summary-position-point))
5870
5871 ;; Summary commands and functions for limiting the summary buffer.
5872
5873 (defun gnus-summary-limit-to-articles (n)
5874   "Limit the summary buffer to the next N articles.
5875 If not given a prefix, use the process marked articles instead."
5876   (interactive "P")
5877   (prog1
5878       (let ((articles (gnus-summary-work-articles n)))
5879         (setq gnus-newsgroup-processable nil)
5880         (gnus-summary-limit articles))
5881     (gnus-summary-position-point)))
5882
5883 (defun gnus-summary-pop-limit (&optional total)
5884   "Restore the previous limit.
5885 If given a prefix, remove all limits."
5886   (interactive "P")
5887   (when total
5888     (setq gnus-newsgroup-limits
5889           (list (mapcar (lambda (h) (mail-header-number h))
5890                         gnus-newsgroup-headers))))
5891   (unless gnus-newsgroup-limits
5892     (error "No limit to pop"))
5893   (prog1
5894       (gnus-summary-limit nil 'pop)
5895     (gnus-summary-position-point)))
5896
5897 (defun gnus-summary-limit-to-subject (subject &optional header)
5898   "Limit the summary buffer to articles that have subjects that match a regexp."
5899   (interactive "sLimit to subject (regexp): ")
5900   (unless header
5901     (setq header "subject"))
5902   (when (not (equal "" subject))
5903     (prog1
5904         (let ((articles (gnus-summary-find-matching
5905                          (or header "subject") subject 'all)))
5906           (unless articles
5907             (error "Found no matches for \"%s\"" subject))
5908           (gnus-summary-limit articles))
5909       (gnus-summary-position-point))))
5910
5911 (defun gnus-summary-limit-to-author (from)
5912   "Limit the summary buffer to articles that have authors that match a regexp."
5913   (interactive "sLimit to author (regexp): ")
5914   (gnus-summary-limit-to-subject from "from"))
5915
5916 (defun gnus-summary-limit-to-age (age &optional younger-p)
5917   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5918 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5919 articles that are younger than AGE days."
5920   (interactive "nTime in days: \nP")
5921   (prog1
5922       (let ((data gnus-newsgroup-data)
5923             (cutoff (nnmail-days-to-time age))
5924             articles d date is-younger)
5925         (while (setq d (pop data))
5926           (when (and (vectorp (gnus-data-header d))
5927                      (setq date (mail-header-date (gnus-data-header d))))
5928             (setq is-younger (nnmail-time-less
5929                               (nnmail-time-since (nnmail-date-to-time date))
5930                               cutoff))
5931             (when (if younger-p is-younger (not is-younger))
5932               (push (gnus-data-number d) articles))))
5933         (gnus-summary-limit (nreverse articles)))
5934     (gnus-summary-position-point)))
5935
5936 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5937 (make-obsolete
5938  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5939
5940 (defun gnus-summary-limit-to-unread (&optional all)
5941   "Limit the summary buffer to articles that are not marked as read.
5942 If ALL is non-nil, limit strictly to unread articles."
5943   (interactive "P")
5944   (if all
5945       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5946     (gnus-summary-limit-to-marks
5947      ;; Concat all the marks that say that an article is read and have
5948      ;; those removed.
5949      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5950            gnus-killed-mark gnus-kill-file-mark
5951            gnus-low-score-mark gnus-expirable-mark
5952            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5953            gnus-duplicate-mark gnus-souped-mark)
5954      'reverse)))
5955
5956 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5957 (make-obsolete 'gnus-summary-delete-marked-with
5958                'gnus-summary-limit-exlude-marks)
5959
5960 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5961   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5962 If REVERSE, limit the summary buffer to articles that are marked
5963 with MARKS.  MARKS can either be a string of marks or a list of marks.
5964 Returns how many articles were removed."
5965   (interactive "sMarks: ")
5966   (gnus-summary-limit-to-marks marks t))
5967
5968 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5969   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5970 If REVERSE (the prefix), limit the summary buffer to articles that are
5971 not marked with MARKS.  MARKS can either be a string of marks or a
5972 list of marks.
5973 Returns how many articles were removed."
5974   (interactive "sMarks: \nP")
5975   (prog1
5976       (let ((data gnus-newsgroup-data)
5977             (marks (if (listp marks) marks
5978                      (append marks nil))) ; Transform to list.
5979             articles)
5980         (while data
5981           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5982                   (memq (gnus-data-mark (car data)) marks))
5983             (push (gnus-data-number (car data)) articles))
5984           (setq data (cdr data)))
5985         (gnus-summary-limit articles))
5986     (gnus-summary-position-point)))
5987
5988 (defun gnus-summary-limit-to-score (&optional score)
5989   "Limit to articles with score at or above SCORE."
5990   (interactive "P")
5991   (setq score (if score
5992                   (prefix-numeric-value score)
5993                 (or gnus-summary-default-score 0)))
5994   (let ((data gnus-newsgroup-data)
5995         articles)
5996     (while data
5997       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5998                 score)
5999         (push (gnus-data-number (car data)) articles))
6000       (setq data (cdr data)))
6001     (prog1
6002         (gnus-summary-limit articles)
6003       (gnus-summary-position-point))))
6004
6005 (defun gnus-summary-limit-include-thread (id)
6006   "Display all the hidden articles that in the current thread."
6007   (interactive (list (mail-header-id (gnus-summary-article-header))))
6008   (let ((articles (gnus-articles-in-thread
6009                    (gnus-id-to-thread (gnus-root-id id)))))
6010     (prog1
6011         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6012       (gnus-summary-position-point))))
6013
6014 (defun gnus-summary-limit-include-dormant ()
6015   "Display all the hidden articles that are marked as dormant.
6016 Note that this command only works on a subset of the articles currently
6017 fetched for this group."
6018   (interactive)
6019   (unless gnus-newsgroup-dormant
6020     (error "There are no dormant articles in this group"))
6021   (prog1
6022       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6023     (gnus-summary-position-point)))
6024
6025 (defun gnus-summary-limit-exclude-dormant ()
6026   "Hide all dormant articles."
6027   (interactive)
6028   (prog1
6029       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6030     (gnus-summary-position-point)))
6031
6032 (defun gnus-summary-limit-exclude-childless-dormant ()
6033   "Hide all dormant articles that have no children."
6034   (interactive)
6035   (let ((data (gnus-data-list t))
6036         articles d children)
6037     ;; Find all articles that are either not dormant or have
6038     ;; children.
6039     (while (setq d (pop data))
6040       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6041                 (and (setq children
6042                            (gnus-article-children (gnus-data-number d)))
6043                      (let (found)
6044                        (while children
6045                          (when (memq (car children) articles)
6046                            (setq children nil
6047                                  found t))
6048                          (pop children))
6049                        found)))
6050         (push (gnus-data-number d) articles)))
6051     ;; Do the limiting.
6052     (prog1
6053         (gnus-summary-limit articles)
6054       (gnus-summary-position-point))))
6055
6056 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6057   "Mark all unread excluded articles as read.
6058 If ALL, mark even excluded ticked and dormants as read."
6059   (interactive "P")
6060   (let ((articles (gnus-sorted-complement
6061                    (sort
6062                     (mapcar (lambda (h) (mail-header-number h))
6063                             gnus-newsgroup-headers)
6064                     '<)
6065                    (sort gnus-newsgroup-limit '<)))
6066         article)
6067     (setq gnus-newsgroup-unreads gnus-newsgroup-limit)
6068     (if all
6069         (setq gnus-newsgroup-dormant nil
6070               gnus-newsgroup-marked nil
6071               gnus-newsgroup-reads
6072               (nconc
6073                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6074                gnus-newsgroup-reads))
6075       (while (setq article (pop articles))
6076         (unless (or (memq article gnus-newsgroup-dormant)
6077                     (memq article gnus-newsgroup-marked))
6078           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6079
6080 (defun gnus-summary-limit (articles &optional pop)
6081   (if pop
6082       ;; We pop the previous limit off the stack and use that.
6083       (setq articles (car gnus-newsgroup-limits)
6084             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6085     ;; We use the new limit, so we push the old limit on the stack.
6086     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6087   ;; Set the limit.
6088   (setq gnus-newsgroup-limit articles)
6089   (let ((total (length gnus-newsgroup-data))
6090         (data (gnus-data-find-list (gnus-summary-article-number)))
6091         (gnus-summary-mark-below nil)   ; Inhibit this.
6092         found)
6093     ;; This will do all the work of generating the new summary buffer
6094     ;; according to the new limit.
6095     (gnus-summary-prepare)
6096     ;; Hide any threads, possibly.
6097     (and gnus-show-threads
6098          gnus-thread-hide-subtree
6099          (gnus-summary-hide-all-threads))
6100     ;; Try to return to the article you were at, or one in the
6101     ;; neighborhood.
6102     (when data
6103       ;; We try to find some article after the current one.
6104       (while data
6105         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6106           (setq data nil
6107                 found t))
6108         (setq data (cdr data))))
6109     (unless found
6110       ;; If there is no data, that means that we were after the last
6111       ;; article.  The same goes when we can't find any articles
6112       ;; after the current one.
6113       (goto-char (point-max))
6114       (gnus-summary-find-prev))
6115     ;; We return how many articles were removed from the summary
6116     ;; buffer as a result of the new limit.
6117     (- total (length gnus-newsgroup-data))))
6118
6119 (defsubst gnus-invisible-cut-children (threads)
6120   (let ((num 0))
6121     (while threads
6122       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6123         (incf num))
6124       (pop threads))
6125     (< num 2)))
6126
6127 (defsubst gnus-cut-thread (thread)
6128   "Go forwards in the thread until we find an article that we want to display."
6129   (when (or (eq gnus-fetch-old-headers 'some)
6130             (eq gnus-fetch-old-headers 'invisible)          
6131             (eq gnus-build-sparse-threads 'some)
6132             (eq gnus-build-sparse-threads 'more))
6133     ;; Deal with old-fetched headers and sparse threads.
6134     (while (and
6135             thread
6136             (or
6137              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6138              (gnus-summary-article-ancient-p
6139               (mail-header-number (car thread))))
6140             (if (or (<= (length (cdr thread)) 1)
6141                     (eq gnus-fetch-old-headers 'invisible))
6142                 (setq gnus-newsgroup-limit
6143                       (delq (mail-header-number (car thread))
6144                             gnus-newsgroup-limit)
6145                       thread (cadr thread))
6146               (when (gnus-invisible-cut-children (cdr thread))
6147                 (let ((th (cdr thread)))
6148                   (while th
6149                     (if (memq (mail-header-number (caar th))
6150                               gnus-newsgroup-limit)
6151                         (setq thread (car th)
6152                               th nil)
6153                       (setq th (cdr th))))))))))
6154   thread)
6155
6156 (defun gnus-cut-threads (threads)
6157   "Cut off all uninteresting articles from the beginning of threads."
6158   (when (or (eq gnus-fetch-old-headers 'some)
6159             (eq gnus-fetch-old-headers 'invisible)
6160             (eq gnus-build-sparse-threads 'some)
6161             (eq gnus-build-sparse-threads 'more))
6162     (let ((th threads))
6163       (while th
6164         (setcar th (gnus-cut-thread (car th)))
6165         (setq th (cdr th)))))
6166   ;; Remove nixed out threads.
6167   (delq nil threads))
6168
6169 (defun gnus-summary-initial-limit (&optional show-if-empty)
6170   "Figure out what the initial limit is supposed to be on group entry.
6171 This entails weeding out unwanted dormants, low-scored articles,
6172 fetch-old-headers verbiage, and so on."
6173   ;; Most groups have nothing to remove.
6174   (if (or gnus-inhibit-limiting
6175           (and (null gnus-newsgroup-dormant)
6176                (not (eq gnus-fetch-old-headers 'some))
6177                (not (eq gnus-fetch-old-headers 'invisible))
6178                (null gnus-summary-expunge-below)
6179                (not (eq gnus-build-sparse-threads 'some))
6180                (not (eq gnus-build-sparse-threads 'more))
6181                (null gnus-thread-expunge-below)
6182                (not gnus-use-nocem)))
6183       ()                                ; Do nothing.
6184     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6185     (setq gnus-newsgroup-limit nil)
6186     (mapatoms
6187      (lambda (node)
6188        (unless (car (symbol-value node))
6189          ;; These threads have no parents -- they are roots.
6190          (let ((nodes (cdr (symbol-value node)))
6191                thread)
6192            (while nodes
6193              (if (and gnus-thread-expunge-below
6194                       (< (gnus-thread-total-score (car nodes))
6195                          gnus-thread-expunge-below))
6196                  (gnus-expunge-thread (pop nodes))
6197                (setq thread (pop nodes))
6198                (gnus-summary-limit-children thread))))))
6199      gnus-newsgroup-dependencies)
6200     ;; If this limitation resulted in an empty group, we might
6201     ;; pop the previous limit and use it instead.
6202     (when (and (not gnus-newsgroup-limit)
6203                show-if-empty)
6204       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6205     gnus-newsgroup-limit))
6206
6207 (defun gnus-summary-limit-children (thread)
6208   "Return 1 if this subthread is visible and 0 if it is not."
6209   ;; First we get the number of visible children to this thread.  This
6210   ;; is done by recursing down the thread using this function, so this
6211   ;; will really go down to a leaf article first, before slowly
6212   ;; working its way up towards the root.
6213   (when thread
6214     (let ((children
6215            (if (cdr thread)
6216                (apply '+ (mapcar 'gnus-summary-limit-children
6217                                  (cdr thread)))
6218              0))
6219           (number (mail-header-number (car thread)))
6220           score)
6221       (if (and
6222            (not (memq number gnus-newsgroup-marked))
6223            (or
6224             ;; If this article is dormant and has absolutely no visible
6225             ;; children, then this article isn't visible.
6226             (and (memq number gnus-newsgroup-dormant)
6227                  (zerop children))
6228             ;; If this is "fetch-old-headered" and there is no
6229             ;; visible children, then we don't want this article.
6230             (and (eq gnus-fetch-old-headers 'some)
6231                  (gnus-summary-article-ancient-p number)
6232                  (zerop children))
6233             ;; If this is "fetch-old-headered" and `invisible', then
6234             ;; we don't want this article.
6235             (and (eq gnus-fetch-old-headers 'invisible)
6236                  (gnus-summary-article-ancient-p number))
6237             ;; If this is a sparsely inserted article with no children,
6238             ;; we don't want it.
6239             (and (eq gnus-build-sparse-threads 'some)
6240                  (gnus-summary-article-sparse-p number)
6241                  (zerop children))
6242             ;; If we use expunging, and this article is really
6243             ;; low-scored, then we don't want this article.
6244             (when (and gnus-summary-expunge-below
6245                        (< (setq score
6246                                 (or (cdr (assq number gnus-newsgroup-scored))
6247                                     gnus-summary-default-score))
6248                           gnus-summary-expunge-below))
6249               ;; We increase the expunge-tally here, but that has
6250               ;; nothing to do with the limits, really.
6251               (incf gnus-newsgroup-expunged-tally)
6252               ;; We also mark as read here, if that's wanted.
6253               (when (and gnus-summary-mark-below
6254                          (< score gnus-summary-mark-below))
6255                 (setq gnus-newsgroup-unreads
6256                       (delq number gnus-newsgroup-unreads))
6257                 (if gnus-newsgroup-auto-expire
6258                     (push number gnus-newsgroup-expirable)
6259                   (push (cons number gnus-low-score-mark)
6260                         gnus-newsgroup-reads)))
6261               t)
6262             ;; Check NoCeM things.
6263             (if (and gnus-use-nocem
6264                      (gnus-nocem-unwanted-article-p
6265                       (mail-header-id (car thread))))
6266                 (progn
6267                   (setq gnus-newsgroup-unreads
6268                         (delq number gnus-newsgroup-unreads))
6269                   t))))
6270           ;; Nope, invisible article.
6271           0
6272         ;; Ok, this article is to be visible, so we add it to the limit
6273         ;; and return 1.
6274         (push number gnus-newsgroup-limit)
6275         1))))
6276
6277 (defun gnus-expunge-thread (thread)
6278   "Mark all articles in THREAD as read."
6279   (let* ((number (mail-header-number (car thread))))
6280     (incf gnus-newsgroup-expunged-tally)
6281     ;; We also mark as read here, if that's wanted.
6282     (setq gnus-newsgroup-unreads
6283           (delq number gnus-newsgroup-unreads))
6284     (if gnus-newsgroup-auto-expire
6285         (push number gnus-newsgroup-expirable)
6286       (push (cons number gnus-low-score-mark)
6287             gnus-newsgroup-reads)))
6288   ;; Go recursively through all subthreads.
6289   (mapcar 'gnus-expunge-thread (cdr thread)))
6290
6291 ;; Summary article oriented commands
6292
6293 (defun gnus-summary-refer-parent-article (n)
6294   "Refer parent article N times.
6295 If N is negative, go to ancestor -N instead.
6296 The difference between N and the number of articles fetched is returned."
6297   (interactive "p")
6298   (let ((skip 1)
6299         error header ref)
6300     (when (not (natnump n))
6301       (setq skip (abs n)
6302             n 1))
6303     (while (and (> n 0)
6304                 (not error))
6305       (setq header (gnus-summary-article-header))
6306       (if (and (eq (mail-header-number header)
6307                    (cdr gnus-article-current))
6308                (equal gnus-newsgroup-name
6309                       (car gnus-article-current)))
6310           ;; If we try to find the parent of the currently
6311           ;; displayed article, then we take a look at the actual
6312           ;; References header, since this is slightly more
6313           ;; reliable than the References field we got from the
6314           ;; server.
6315           (save-excursion
6316             (set-buffer gnus-original-article-buffer)
6317             (nnheader-narrow-to-headers)
6318             (unless (setq ref (message-fetch-field "references"))
6319               (setq ref (message-fetch-field "in-reply-to")))
6320             (widen))
6321         (setq ref
6322               ;; It's not the current article, so we take a bet on
6323               ;; the value we got from the server.
6324               (mail-header-references header)))
6325       (if (and ref
6326                (not (equal ref "")))
6327           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6328             (gnus-message 1 "Couldn't find parent"))
6329         (gnus-message 1 "No references in article %d"
6330                       (gnus-summary-article-number))
6331         (setq error t))
6332       (decf n))
6333     (gnus-summary-position-point)
6334     n))
6335
6336 (defun gnus-summary-refer-references ()
6337   "Fetch all articles mentioned in the References header.
6338 Return the number of articles fetched."
6339   (interactive)
6340   (let ((ref (mail-header-references (gnus-summary-article-header)))
6341         (current (gnus-summary-article-number))
6342         (n 0))
6343     (if (or (not ref)
6344             (equal ref ""))
6345         (error "No References in the current article")
6346       ;; For each Message-ID in the References header...
6347       (while (string-match "<[^>]*>" ref)
6348         (incf n)
6349         ;; ... fetch that article.
6350         (gnus-summary-refer-article
6351          (prog1 (match-string 0 ref)
6352            (setq ref (substring ref (match-end 0))))))
6353       (gnus-summary-goto-subject current)
6354       (gnus-summary-position-point)
6355       n)))
6356
6357 (defun gnus-summary-refer-thread (&optional limit)
6358   "Fetch all articles in the current thread.
6359 If LIMIT (the numerical prefix), fetch that many old headers instead
6360 of what's specified by the `gnus-refer-thread-limit' variable."
6361   (interactive "P")
6362   (let ((id (mail-header-id (gnus-summary-article-header)))
6363         (limit (if limit (prefix-numeric-value limit)
6364                  gnus-refer-thread-limit))
6365         fmethod root)
6366     ;; We want to fetch LIMIT *old* headers, but we also have to
6367     ;; re-fetch all the headers in the current buffer, because many of
6368     ;; them may be undisplayed.  So we adjust LIMIT.
6369     (when (numberp limit)
6370       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6371     (unless (eq gnus-fetch-old-headers 'invisible)
6372       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6373       ;; Retrieve the headers and read them in.
6374       (if (eq (gnus-retrieve-headers
6375                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6376               'nov)
6377           (gnus-build-all-threads)
6378         (error "Can't fetch thread from backends that don't support NOV"))
6379       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6380     (gnus-summary-limit-include-thread id)))
6381
6382 (defun gnus-summary-refer-article (message-id &optional arg)
6383   "Fetch an article specified by MESSAGE-ID.
6384 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6385 or `gnus-select-method', no matter what backend the article comes from."
6386   (interactive "sMessage-ID: \nP")
6387   (when (and (stringp message-id)
6388              (not (zerop (length message-id))))
6389     ;; Construct the correct Message-ID if necessary.
6390     ;; Suggested by tale@pawl.rpi.edu.
6391     (unless (string-match "^<" message-id)
6392       (setq message-id (concat "<" message-id)))
6393     (unless (string-match ">$" message-id)
6394       (setq message-id (concat message-id ">")))
6395     (let* ((header (gnus-id-to-header message-id))
6396            (sparse (and header
6397                         (gnus-summary-article-sparse-p
6398                          (mail-header-number header))
6399                         (memq (mail-header-number header)
6400                               gnus-newsgroup-limit)))
6401            h)
6402       (cond
6403        ;; If the article is present in the buffer we just go to it.
6404        ((and header
6405              (or (not (gnus-summary-article-sparse-p
6406                        (mail-header-number header)))
6407                  sparse))
6408         (prog1
6409             (gnus-summary-goto-article
6410              (mail-header-number header) nil t)
6411           (when sparse
6412             (gnus-summary-update-article (mail-header-number header)))))
6413        (t
6414         ;; We fetch the article
6415         (let ((gnus-override-method
6416                (cond ((gnus-news-group-p gnus-newsgroup-name)
6417                       gnus-refer-article-method)
6418                      (arg
6419                       (or gnus-refer-article-method gnus-select-method))
6420                      (t nil)))
6421               number)
6422           ;; Start the special refer-article method, if necessary.
6423           (when (and gnus-refer-article-method
6424                      (gnus-news-group-p gnus-newsgroup-name))
6425             (gnus-check-server gnus-refer-article-method))
6426           ;; Fetch the header, and display the article.
6427           (if (setq number (gnus-summary-insert-subject message-id))
6428               (gnus-summary-select-article nil nil nil number)
6429             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6430
6431 (defun gnus-summary-edit-parameters ()
6432   "Edit the group parameters of the current group."
6433   (interactive)
6434   (gnus-group-edit-group gnus-newsgroup-name 'params))
6435
6436 (defun gnus-summary-enter-digest-group (&optional force)
6437   "Enter an nndoc group based on the current article.
6438 If FORCE, force a digest interpretation.  If not, try
6439 to guess what the document format is."
6440   (interactive "P")
6441   (let ((conf gnus-current-window-configuration))
6442     (save-excursion
6443       (gnus-summary-select-article))
6444     (setq gnus-current-window-configuration conf)
6445     (let* ((name (format "%s-%d"
6446                          (gnus-group-prefixed-name
6447                           gnus-newsgroup-name (list 'nndoc ""))
6448                          (save-excursion
6449                            (set-buffer gnus-summary-buffer)
6450                            gnus-current-article)))
6451            (ogroup gnus-newsgroup-name)
6452            (params (append (gnus-info-params (gnus-get-info ogroup))
6453                            (list (cons 'to-group ogroup))
6454                            (list (cons 'save-article-group ogroup))))
6455            (case-fold-search t)
6456            (buf (current-buffer))
6457            dig)
6458       (save-excursion
6459         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6460         (insert-buffer-substring gnus-original-article-buffer)
6461         ;; Remove lines that may lead nndoc to misinterpret the
6462         ;; document type.
6463         (narrow-to-region
6464          (goto-char (point-min))
6465          (or (search-forward "\n\n" nil t) (point)))
6466         (goto-char (point-min))
6467         (delete-matching-lines "^\\(Path\\):\\|^From ")
6468         (widen))
6469       (unwind-protect
6470           (if (gnus-group-read-ephemeral-group
6471                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6472                             (nndoc-article-type
6473                              ,(if force 'digest 'guess))) t)
6474               ;; Make all postings to this group go to the parent group.
6475               (nconc (gnus-info-params (gnus-get-info name))
6476                      params)
6477             ;; Couldn't select this doc group.
6478             (switch-to-buffer buf)
6479             (gnus-set-global-variables)
6480             (gnus-configure-windows 'summary)
6481             (gnus-message 3 "Article couldn't be entered?"))
6482         (kill-buffer dig)))))
6483
6484 (defun gnus-summary-read-document (n)
6485   "Open a new group based on the current article(s).
6486 This will allow you to read digests and other similar
6487 documents as newsgroups.
6488 Obeys the standard process/prefix convention."
6489   (interactive "P")
6490   (let* ((articles (gnus-summary-work-articles n))
6491          (ogroup gnus-newsgroup-name)
6492          (params (append (gnus-info-params (gnus-get-info ogroup))
6493                          (list (cons 'to-group ogroup))))
6494          article group egroup groups vgroup)
6495     (while (setq article (pop articles))
6496       (setq group (format "%s-%d" gnus-newsgroup-name article))
6497       (gnus-summary-remove-process-mark article)
6498       (when (gnus-summary-display-article article)
6499         (save-excursion
6500           (nnheader-temp-write nil
6501             (insert-buffer-substring gnus-original-article-buffer)
6502             ;; Remove some headers that may lead nndoc to make
6503             ;; the wrong guess.
6504             (message-narrow-to-head)
6505             (goto-char (point-min))
6506             (delete-matching-lines "^\\(Path\\):\\|^From ")
6507             (widen)
6508             (if (setq egroup
6509                       (gnus-group-read-ephemeral-group
6510                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6511                                      (nndoc-article-type guess))
6512                        t nil t))
6513                 (progn
6514                   ;; Make all postings to this group go to the parent group.
6515                   (nconc (gnus-info-params (gnus-get-info egroup))
6516                          params)
6517                   (push egroup groups))
6518               ;; Couldn't select this doc group.
6519               (gnus-error 3 "Article couldn't be entered"))))))
6520     ;; Now we have selected all the documents.
6521     (cond
6522      ((not groups)
6523       (error "None of the articles could be interpreted as documents"))
6524      ((gnus-group-read-ephemeral-group
6525        (setq vgroup (format
6526                      "nnvirtual:%s-%s" gnus-newsgroup-name
6527                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6528        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6529        t
6530        (cons (current-buffer) 'summary)))
6531      (t
6532       (error "Couldn't select virtual nndoc group")))))
6533
6534 (defun gnus-summary-isearch-article (&optional regexp-p)
6535   "Do incremental search forward on the current article.
6536 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6537   (interactive "P")
6538   (gnus-summary-select-article)
6539   (gnus-configure-windows 'article)
6540   (gnus-eval-in-buffer-window gnus-article-buffer
6541     (save-restriction
6542       (widen)
6543       (isearch-forward regexp-p))))
6544
6545 (defun gnus-summary-search-article-forward (regexp &optional backward)
6546   "Search for an article containing REGEXP forward.
6547 If BACKWARD, search backward instead."
6548   (interactive
6549    (list (read-string
6550           (format "Search article %s (regexp%s): "
6551                   (if current-prefix-arg "backward" "forward")
6552                   (if gnus-last-search-regexp
6553                       (concat ", default " gnus-last-search-regexp)
6554                     "")))
6555          current-prefix-arg))
6556   (if (string-equal regexp "")
6557       (setq regexp (or gnus-last-search-regexp ""))
6558     (setq gnus-last-search-regexp regexp))
6559   (if (gnus-summary-search-article regexp backward)
6560       (gnus-summary-show-thread)
6561     (error "Search failed: \"%s\"" regexp)))
6562
6563 (defun gnus-summary-search-article-backward (regexp)
6564   "Search for an article containing REGEXP backward."
6565   (interactive
6566    (list (read-string
6567           (format "Search article backward (regexp%s): "
6568                   (if gnus-last-search-regexp
6569                       (concat ", default " gnus-last-search-regexp)
6570                     "")))))
6571   (gnus-summary-search-article-forward regexp 'backward))
6572
6573 (defun gnus-summary-search-article (regexp &optional backward)
6574   "Search for an article containing REGEXP.
6575 Optional argument BACKWARD means do search for backward.
6576 `gnus-select-article-hook' is not called during the search."
6577   ;; We have to require this here to make sure that the following
6578   ;; dynamic binding isn't shadowed by autoloading.
6579   (require 'gnus-async)
6580   (let ((gnus-select-article-hook nil)  ;Disable hook.
6581         (gnus-article-display-hook nil)
6582         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6583         (gnus-use-article-prefetch nil)
6584         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6585         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6586         (sum (current-buffer))
6587         (found nil)
6588         point)
6589     (gnus-save-hidden-threads
6590       (gnus-summary-select-article)
6591       (set-buffer gnus-article-buffer)
6592       (when backward
6593         (forward-line -1))
6594       (while (not found)
6595         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6596         (if (if backward
6597                 (re-search-backward regexp nil t)
6598               (re-search-forward regexp nil t))
6599             ;; We found the regexp.
6600             (progn
6601               (setq found 'found)
6602               (beginning-of-line)
6603               (set-window-start
6604                (get-buffer-window (current-buffer))
6605                (point))
6606               (forward-line 1)
6607               (set-buffer sum)
6608               (setq point (point)))
6609           ;; We didn't find it, so we go to the next article.
6610           (set-buffer sum)
6611           (setq found 'not)
6612           (while (eq found 'not)
6613             (if (not (if backward (gnus-summary-find-prev)
6614                        (gnus-summary-find-next)))
6615                 ;; No more articles.
6616                 (setq found t)
6617               ;; Select the next article and adjust point.
6618               (unless (gnus-summary-article-sparse-p
6619                        (gnus-summary-article-number))
6620                 (setq found nil)
6621                 (gnus-summary-select-article)
6622                 (set-buffer gnus-article-buffer)
6623                 (widen)
6624                 (goto-char (if backward (point-max) (point-min))))))))
6625       (gnus-message 7 ""))
6626     ;; Return whether we found the regexp.
6627     (when (eq found 'found)
6628       (goto-char point)
6629       (gnus-summary-show-thread)
6630       (gnus-summary-goto-subject gnus-current-article)
6631       (gnus-summary-position-point)
6632       t)))
6633
6634 (defun gnus-summary-find-matching (header regexp &optional backward unread
6635                                           not-case-fold)
6636   "Return a list of all articles that match REGEXP on HEADER.
6637 The search stars on the current article and goes forwards unless
6638 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6639 If UNREAD is non-nil, only unread articles will
6640 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6641 in the comparisons."
6642   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6643                 (gnus-data-find-list
6644                  (gnus-summary-article-number) (gnus-data-list backward))))
6645         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6646         (case-fold-search (not not-case-fold))
6647         articles d)
6648     (unless (fboundp (intern (concat "mail-header-" header)))
6649       (error "%s is not a valid header" header))
6650     (while data
6651       (setq d (car data))
6652       (and (or (not unread)             ; We want all articles...
6653                (gnus-data-unread-p d))  ; Or just unreads.
6654            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6655            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6656            (push (gnus-data-number d) articles)) ; Success!
6657       (setq data (cdr data)))
6658     (nreverse articles)))
6659
6660 (defun gnus-summary-execute-command (header regexp command &optional backward)
6661   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6662 If HEADER is an empty string (or nil), the match is done on the entire
6663 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6664   (interactive
6665    (list (let ((completion-ignore-case t))
6666            (completing-read
6667             "Header name: "
6668             (mapcar (lambda (string) (list string))
6669                     '("Number" "Subject" "From" "Lines" "Date"
6670                       "Message-ID" "Xref" "References" "Body"))
6671             nil 'require-match))
6672          (read-string "Regexp: ")
6673          (read-key-sequence "Command: ")
6674          current-prefix-arg))
6675   (when (equal header "Body")
6676     (setq header ""))
6677   ;; Hidden thread subtrees must be searched as well.
6678   (gnus-summary-show-all-threads)
6679   ;; We don't want to change current point nor window configuration.
6680   (save-excursion
6681     (save-window-excursion
6682       (gnus-message 6 "Executing %s..." (key-description command))
6683       ;; We'd like to execute COMMAND interactively so as to give arguments.
6684       (gnus-execute header regexp
6685                     `(call-interactively ',(key-binding command))
6686                     backward)
6687       (gnus-message 6 "Executing %s...done" (key-description command)))))
6688
6689 (defun gnus-summary-beginning-of-article ()
6690   "Scroll the article back to the beginning."
6691   (interactive)
6692   (gnus-summary-select-article)
6693   (gnus-configure-windows 'article)
6694   (gnus-eval-in-buffer-window gnus-article-buffer
6695     (widen)
6696     (goto-char (point-min))
6697     (when gnus-page-broken
6698       (gnus-narrow-to-page))))
6699
6700 (defun gnus-summary-end-of-article ()
6701   "Scroll to the end of the article."
6702   (interactive)
6703   (gnus-summary-select-article)
6704   (gnus-configure-windows 'article)
6705   (gnus-eval-in-buffer-window gnus-article-buffer
6706     (widen)
6707     (goto-char (point-max))
6708     (recenter -3)
6709     (when gnus-page-broken
6710       (gnus-narrow-to-page))))
6711
6712 (defun gnus-summary-print-article (&optional filename n)
6713   "Generate and print a PostScript image of the N next (mail) articles.
6714
6715 If N is negative, print the N previous articles.  If N is nil and articles
6716 have been marked with the process mark, print these instead.
6717
6718 If the optional second argument FILENAME is nil, send the image to the
6719 printer.  If FILENAME is a string, save the PostScript image in a file with
6720 that name.  If FILENAME is a number, prompt the user for the name of the file
6721 to save in."
6722   (interactive (list (ps-print-preprint current-prefix-arg)
6723                      current-prefix-arg))
6724   (dolist (article (gnus-summary-work-articles n))
6725     (gnus-summary-select-article nil nil 'pseudo article)
6726     (gnus-eval-in-buffer-window gnus-article-buffer
6727       (let ((buffer (generate-new-buffer " *print*")))
6728         (unwind-protect
6729             (progn
6730               (copy-to-buffer buffer (point-min) (point-max))
6731               (set-buffer buffer)
6732               (gnus-article-delete-invisible-text)
6733               (let ((ps-left-header
6734                      (list 
6735                       (concat "("
6736                               (mail-header-subject gnus-current-headers) ")")
6737                       (concat "("
6738                               (mail-header-from gnus-current-headers) ")")))
6739                     (ps-right-header 
6740                      (list 
6741                       "/pagenumberstring load" 
6742                       (concat "("
6743                               (mail-header-date gnus-current-headers) ")"))))
6744                 (gnus-run-hooks 'gnus-ps-print-hook)
6745                 (save-excursion
6746                   (ps-print-buffer-with-faces filename))))
6747           (kill-buffer buffer))))))
6748
6749 (defun gnus-summary-show-article (&optional arg)
6750   "Force re-fetching of the current article.
6751 If ARG (the prefix) is non-nil, show the raw article without any
6752 article massaging functions being run."
6753   (interactive "P")
6754   (if (not arg)
6755       ;; Select the article the normal way.
6756       (gnus-summary-select-article nil 'force)
6757     ;; Bind the article treatment functions to nil.
6758     (let ((gnus-have-all-headers t)
6759           gnus-article-display-hook
6760           gnus-article-prepare-hook
6761           gnus-break-pages
6762           gnus-show-mime
6763           gnus-visual)
6764       (gnus-summary-select-article nil 'force)))
6765   (gnus-summary-goto-subject gnus-current-article)
6766   (gnus-summary-position-point))
6767
6768 (defun gnus-summary-verbose-headers (&optional arg)
6769   "Toggle permanent full header display.
6770 If ARG is a positive number, turn header display on.
6771 If ARG is a negative number, turn header display off."
6772   (interactive "P")
6773   (setq gnus-show-all-headers
6774         (cond ((or (not (numberp arg))
6775                    (zerop arg))
6776                (not gnus-show-all-headers))
6777               ((natnump arg)
6778                t)))
6779   (gnus-summary-show-article))
6780
6781 (defun gnus-summary-toggle-header (&optional arg)
6782   "Show the headers if they are hidden, or hide them if they are shown.
6783 If ARG is a positive number, show the entire header.
6784 If ARG is a negative number, hide the unwanted header lines."
6785   (interactive "P")
6786   (save-excursion
6787     (set-buffer gnus-article-buffer)
6788     (let* ((buffer-read-only nil)
6789            (inhibit-point-motion-hooks t)
6790            (hidden (text-property-any
6791                     (goto-char (point-min)) (search-forward "\n\n")
6792                     'invisible t))
6793            e)
6794       (goto-char (point-min))
6795       (when (search-forward "\n\n" nil t)
6796         (delete-region (point-min) (1- (point))))
6797       (goto-char (point-min))
6798       (save-excursion
6799         (set-buffer gnus-original-article-buffer)
6800         (goto-char (point-min))
6801         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6802       (insert-buffer-substring gnus-original-article-buffer 1 e)
6803       (let ((article-inhibit-hiding t))
6804         (gnus-run-hooks 'gnus-article-display-hook))
6805       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6806         (gnus-article-hide-headers)))))
6807
6808 (defun gnus-summary-show-all-headers ()
6809   "Make all header lines visible."
6810   (interactive)
6811   (gnus-article-show-all-headers))
6812
6813 (defun gnus-summary-toggle-mime (&optional arg)
6814   "Toggle MIME processing.
6815 If ARG is a positive number, turn MIME processing on."
6816   (interactive "P")
6817   (setq gnus-show-mime
6818         (if (null arg) (not gnus-show-mime)
6819           (> (prefix-numeric-value arg) 0)))
6820   (gnus-summary-select-article t 'force))
6821
6822 (defun gnus-summary-caesar-message (&optional arg)
6823   "Caesar rotate the current article by 13.
6824 The numerical prefix specifies how many places to rotate each letter
6825 forward."
6826   (interactive "P")
6827   (gnus-summary-select-article)
6828   (let ((mail-header-separator ""))
6829     (gnus-eval-in-buffer-window gnus-article-buffer
6830       (save-restriction
6831         (widen)
6832         (let ((start (window-start))
6833               buffer-read-only)
6834           (message-caesar-buffer-body arg)
6835           (set-window-start (get-buffer-window (current-buffer)) start))))))
6836
6837 (defun gnus-summary-stop-page-breaking ()
6838   "Stop page breaking in the current article."
6839   (interactive)
6840   (gnus-summary-select-article)
6841   (gnus-eval-in-buffer-window gnus-article-buffer
6842     (widen)
6843     (when (gnus-visual-p 'page-marker)
6844       (let ((buffer-read-only nil))
6845         (gnus-remove-text-with-property 'gnus-prev)
6846         (gnus-remove-text-with-property 'gnus-next))
6847       (setq gnus-page-broken nil))))
6848
6849 (defun gnus-summary-move-article (&optional n to-newsgroup
6850                                             select-method action)
6851   "Move the current article to a different newsgroup.
6852 If N is a positive number, move the N next articles.
6853 If N is a negative number, move the N previous articles.
6854 If N is nil and any articles have been marked with the process mark,
6855 move those articles instead.
6856 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6857 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6858 re-spool using this method.
6859
6860 For this function to work, both the current newsgroup and the
6861 newsgroup that you want to move to have to support the `request-move'
6862 and `request-accept' functions."
6863   (interactive "P")
6864   (unless action
6865     (setq action 'move))
6866   ;; Disable marking as read.
6867   (let (gnus-mark-article-hook)
6868     (save-window-excursion
6869       (gnus-summary-select-article)))
6870   ;; Check whether the source group supports the required functions.
6871   (cond ((and (eq action 'move)
6872               (not (gnus-check-backend-function
6873                     'request-move-article gnus-newsgroup-name)))
6874          (error "The current group does not support article moving"))
6875         ((and (eq action 'crosspost)
6876               (not (gnus-check-backend-function
6877                     'request-replace-article gnus-newsgroup-name)))
6878          (error "The current group does not support article editing")))
6879   (let ((articles (gnus-summary-work-articles n))
6880         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6881         (names '((move "Move" "Moving")
6882                  (copy "Copy" "Copying")
6883                  (crosspost "Crosspost" "Crossposting")))
6884         (copy-buf (save-excursion
6885                     (nnheader-set-temp-buffer " *copy article*")))
6886         art-group to-method new-xref article to-groups)
6887     (unless (assq action names)
6888       (error "Unknown action %s" action))
6889     ;; Read the newsgroup name.
6890     (when (and (not to-newsgroup)
6891                (not select-method))
6892       (setq to-newsgroup
6893             (gnus-read-move-group-name
6894              (cadr (assq action names))
6895              (symbol-value (intern (format "gnus-current-%s-group" action)))
6896              articles prefix))
6897       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6898     (setq to-method (or select-method
6899                         (gnus-group-name-to-method to-newsgroup)))
6900     ;; Check the method we are to move this article to...
6901     (unless (gnus-check-backend-function
6902              'request-accept-article (car to-method))
6903       (error "%s does not support article copying" (car to-method)))
6904     (unless (gnus-check-server to-method)
6905       (error "Can't open server %s" (car to-method)))
6906     (gnus-message 6 "%s to %s: %s..."
6907                   (caddr (assq action names))
6908                   (or (car select-method) to-newsgroup) articles)
6909     (while articles
6910       (setq article (pop articles))
6911       (setq
6912        art-group
6913        (cond
6914         ;; Move the article.
6915         ((eq action 'move)
6916          ;; Remove this article from future suppression.
6917          (gnus-dup-unsuppress-article article)
6918          (gnus-request-move-article
6919           article                       ; Article to move
6920           gnus-newsgroup-name           ; From newsgroup
6921           (nth 1 (gnus-find-method-for-group
6922                   gnus-newsgroup-name)) ; Server
6923           (list 'gnus-request-accept-article
6924                 to-newsgroup (list 'quote select-method)
6925                 (not articles))         ; Accept form
6926           (not articles)))              ; Only save nov last time
6927         ;; Copy the article.
6928         ((eq action 'copy)
6929          (save-excursion
6930            (set-buffer copy-buf)
6931            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6932              (gnus-request-accept-article
6933               to-newsgroup select-method (not articles)))))
6934         ;; Crosspost the article.
6935         ((eq action 'crosspost)
6936          (let ((xref (message-tokenize-header
6937                       (mail-header-xref (gnus-summary-article-header article))
6938                       " ")))
6939            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6940                                   ":" article))
6941            (unless xref
6942              (setq xref (list (system-name))))
6943            (setq new-xref
6944                  (concat
6945                   (mapconcat 'identity
6946                              (delete "Xref:" (delete new-xref xref))
6947                              " ")
6948                   " " new-xref))
6949            (save-excursion
6950              (set-buffer copy-buf)
6951              ;; First put the article in the destination group.
6952              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6953              (when (consp (setq art-group
6954                                 (gnus-request-accept-article
6955                                  to-newsgroup select-method (not articles))))
6956                (setq new-xref (concat new-xref " " (car art-group)
6957                                       ":" (cdr art-group)))
6958                ;; Now we have the new Xrefs header, so we insert
6959                ;; it and replace the new article.
6960                (nnheader-replace-header "Xref" new-xref)
6961                (gnus-request-replace-article
6962                 (cdr art-group) to-newsgroup (current-buffer))
6963                art-group))))))
6964       (cond
6965        ((not art-group)
6966         (gnus-message 1 "Couldn't %s article %s"
6967                       (cadr (assq action names)) article))
6968        ((and (eq art-group 'junk)
6969              (eq action 'move))
6970         (gnus-summary-mark-article article gnus-canceled-mark)
6971         (gnus-message 4 "Deleted article %s" article))
6972        (t
6973         (let* ((entry
6974                 (or
6975                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6976                  (gnus-gethash
6977                   (gnus-group-prefixed-name
6978                    (car art-group)
6979                    (or select-method
6980                        (gnus-find-method-for-group to-newsgroup)))
6981                   gnus-newsrc-hashtb)))
6982                (info (nth 2 entry))
6983                (to-group (gnus-info-group info)))
6984           ;; Update the group that has been moved to.
6985           (when (and info
6986                      (memq action '(move copy)))
6987             (unless (member to-group to-groups)
6988               (push to-group to-groups))
6989
6990             (unless (memq article gnus-newsgroup-unreads)
6991               (gnus-info-set-read
6992                info (gnus-add-to-range (gnus-info-read info)
6993                                        (list (cdr art-group)))))
6994
6995             ;; Copy any marks over to the new group.
6996             (let ((marks gnus-article-mark-lists)
6997                   (to-article (cdr art-group)))
6998
6999               ;; See whether the article is to be put in the cache.
7000               (when gnus-use-cache
7001                 (gnus-cache-possibly-enter-article
7002                  to-group to-article
7003                  (let ((header (copy-sequence
7004                                 (gnus-summary-article-header article))))
7005                    (mail-header-set-number header to-article)
7006                    header)
7007                  (memq article gnus-newsgroup-marked)
7008                  (memq article gnus-newsgroup-dormant)
7009                  (memq article gnus-newsgroup-unreads)))
7010
7011               (when (and (equal to-group gnus-newsgroup-name)
7012                          (not (memq article gnus-newsgroup-unreads)))
7013                 ;; Mark this article as read in this group.
7014                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7015                 (setcdr (gnus-active to-group) to-article)
7016                 (setcdr gnus-newsgroup-active to-article))
7017
7018               (while marks
7019                 (when (memq article (symbol-value
7020                                      (intern (format "gnus-newsgroup-%s"
7021                                                      (caar marks)))))
7022                   ;; If the other group is the same as this group,
7023                   ;; then we have to add the mark to the list.
7024                   (when (equal to-group gnus-newsgroup-name)
7025                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7026                          (cons to-article
7027                                (symbol-value
7028                                 (intern (format "gnus-newsgroup-%s"
7029                                                 (caar marks)))))))
7030                   ;; Copy the marks to other group.
7031                   (gnus-add-marked-articles
7032                    to-group (cdar marks) (list to-article) info))
7033                 (setq marks (cdr marks)))
7034
7035               (gnus-dribble-enter
7036                (concat "(gnus-group-set-info '"
7037                        (gnus-prin1-to-string (gnus-get-info to-group))
7038                        ")"))))
7039
7040           ;; Update the Xref header in this article to point to
7041           ;; the new crossposted article we have just created.
7042           (when (eq action 'crosspost)
7043             (save-excursion
7044               (set-buffer copy-buf)
7045               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7046               (nnheader-replace-header "Xref" new-xref)
7047               (gnus-request-replace-article
7048                article gnus-newsgroup-name (current-buffer)))))
7049
7050         ;;;!!!Why is this necessary?
7051         (set-buffer gnus-summary-buffer)
7052         
7053         (gnus-summary-goto-subject article)
7054         (when (eq action 'move)
7055           (gnus-summary-mark-article article gnus-canceled-mark))))
7056       (gnus-summary-remove-process-mark article))
7057     ;; Re-activate all groups that have been moved to.
7058     (while to-groups
7059       (save-excursion
7060         (set-buffer gnus-group-buffer)
7061         (when (gnus-group-goto-group (car to-groups) t)
7062           (gnus-group-get-new-news-this-group 1 t))
7063         (pop to-groups)))
7064
7065     (gnus-kill-buffer copy-buf)
7066     (gnus-summary-position-point)
7067     (gnus-set-mode-line 'summary)))
7068
7069 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7070   "Move the current article to a different newsgroup.
7071 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7072 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7073 re-spool using this method."
7074   (interactive "P")
7075   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7076
7077 (defun gnus-summary-crosspost-article (&optional n)
7078   "Crosspost the current article to some other group."
7079   (interactive "P")
7080   (gnus-summary-move-article n nil nil 'crosspost))
7081
7082 (defcustom gnus-summary-respool-default-method nil
7083   "Default method for respooling an article.
7084 If nil, use to the current newsgroup method."
7085   :type `(choice (gnus-select-method :value (nnml ""))
7086                  (const nil))
7087   :group 'gnus-summary-mail)
7088
7089 (defun gnus-summary-respool-article (&optional n method)
7090   "Respool the current article.
7091 The article will be squeezed through the mail spooling process again,
7092 which means that it will be put in some mail newsgroup or other
7093 depending on `nnmail-split-methods'.
7094 If N is a positive number, respool the N next articles.
7095 If N is a negative number, respool the N previous articles.
7096 If N is nil and any articles have been marked with the process mark,
7097 respool those articles instead.
7098
7099 Respooling can be done both from mail groups and \"real\" newsgroups.
7100 In the former case, the articles in question will be moved from the
7101 current group into whatever groups they are destined to.  In the
7102 latter case, they will be copied into the relevant groups."
7103   (interactive
7104    (list current-prefix-arg
7105          (let* ((methods (gnus-methods-using 'respool))
7106                 (methname
7107                  (symbol-name (or gnus-summary-respool-default-method
7108                                   (car (gnus-find-method-for-group
7109                                         gnus-newsgroup-name)))))
7110                 (method
7111                  (gnus-completing-read
7112                   methname "What backend do you want to use when respooling?"
7113                   methods nil t nil 'gnus-mail-method-history))
7114                 ms)
7115            (cond
7116             ((zerop (length (setq ms (gnus-servers-using-backend
7117                                       (intern method)))))
7118              (list (intern method) ""))
7119             ((= 1 (length ms))
7120              (car ms))
7121             (t
7122              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7123                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7124                            ms-alist))))))))
7125   (unless method
7126     (error "No method given for respooling"))
7127   (if (assoc (symbol-name
7128               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7129              (gnus-methods-using 'respool))
7130       (gnus-summary-move-article n nil method)
7131     (gnus-summary-copy-article n nil method)))
7132
7133 (defun gnus-summary-import-article (file)
7134   "Import an arbitrary file into a mail newsgroup."
7135   (interactive "fImport file: ")
7136   (let ((group gnus-newsgroup-name)
7137         (now (current-time))
7138         atts lines)
7139     (unless (gnus-check-backend-function 'request-accept-article group)
7140       (error "%s does not support article importing" group))
7141     (or (file-readable-p file)
7142         (not (file-regular-p file))
7143         (error "Can't read %s" file))
7144     (save-excursion
7145       (set-buffer (get-buffer-create " *import file*"))
7146       (buffer-disable-undo (current-buffer))
7147       (erase-buffer)
7148       (nnheader-insert-file-contents file)
7149       (goto-char (point-min))
7150       (unless (nnheader-article-p)
7151         ;; This doesn't look like an article, so we fudge some headers.
7152         (setq atts (file-attributes file)
7153               lines (count-lines (point-min) (point-max)))
7154         (insert "From: " (read-string "From: ") "\n"
7155                 "Subject: " (read-string "Subject: ") "\n"
7156                 "Date: " (timezone-make-date-arpa-standard
7157                           (current-time-string (nth 5 atts))
7158                           (current-time-zone now)
7159                           (current-time-zone now))
7160                 "\n"
7161                 "Message-ID: " (message-make-message-id) "\n"
7162                 "Lines: " (int-to-string lines) "\n"
7163                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7164       (gnus-request-accept-article group nil t)
7165       (kill-buffer (current-buffer)))))
7166
7167 (defun gnus-summary-article-posted-p ()
7168   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7169 This will be the case if the article has both been mailed and posted."
7170   (interactive)
7171   (let ((id (mail-header-references (gnus-summary-article-header)))
7172         (gnus-override-method
7173          (or gnus-refer-article-method gnus-select-method)))
7174     (if (gnus-request-head id "")
7175         (gnus-message 2 "The current message was found on %s"
7176                       gnus-override-method)
7177       (gnus-message 2 "The current message couldn't be found on %s"
7178                     gnus-override-method)
7179       nil)))
7180
7181 (defun gnus-summary-expire-articles (&optional now)
7182   "Expire all articles that are marked as expirable in the current group."
7183   (interactive)
7184   (when (gnus-check-backend-function
7185          'request-expire-articles gnus-newsgroup-name)
7186     ;; This backend supports expiry.
7187     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7188            (expirable (if total
7189                           (progn
7190                             ;; We need to update the info for
7191                             ;; this group for `gnus-list-of-read-articles'
7192                             ;; to give us the right answer.
7193                             (gnus-run-hooks 'gnus-exit-group-hook)
7194                             (gnus-summary-update-info)
7195                             (gnus-list-of-read-articles gnus-newsgroup-name))
7196                         (setq gnus-newsgroup-expirable
7197                               (sort gnus-newsgroup-expirable '<))))
7198            (expiry-wait (if now 'immediate
7199                           (gnus-group-find-parameter
7200                            gnus-newsgroup-name 'expiry-wait)))
7201            es)
7202       (when expirable
7203         ;; There are expirable articles in this group, so we run them
7204         ;; through the expiry process.
7205         (gnus-message 6 "Expiring articles...")
7206         ;; The list of articles that weren't expired is returned.
7207         (save-excursion
7208           (if expiry-wait
7209               (let ((nnmail-expiry-wait-function nil)
7210                     (nnmail-expiry-wait expiry-wait))
7211                 (setq es (gnus-request-expire-articles
7212                           expirable gnus-newsgroup-name)))
7213             (setq es (gnus-request-expire-articles
7214                       expirable gnus-newsgroup-name))))
7215         (unless total
7216           (setq gnus-newsgroup-expirable es))
7217         ;; We go through the old list of expirable, and mark all
7218         ;; really expired articles as nonexistent.
7219         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7220           (let ((gnus-use-cache nil))
7221             (while expirable
7222               (unless (memq (car expirable) es)
7223                 (when (gnus-data-find (car expirable))
7224                   (gnus-summary-mark-article
7225                    (car expirable) gnus-canceled-mark)))
7226               (setq expirable (cdr expirable)))))
7227         (gnus-message 6 "Expiring articles...done")))))
7228
7229 (defun gnus-summary-expire-articles-now ()
7230   "Expunge all expirable articles in the current group.
7231 This means that *all* articles that are marked as expirable will be
7232 deleted forever, right now."
7233   (interactive)
7234   (or gnus-expert-user
7235       (gnus-yes-or-no-p
7236        "Are you really, really, really sure you want to delete all these messages? ")
7237       (error "Phew!"))
7238   (gnus-summary-expire-articles t))
7239
7240 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7241 (defun gnus-summary-delete-article (&optional n)
7242   "Delete the N next (mail) articles.
7243 This command actually deletes articles.  This is not a marking
7244 command.  The article will disappear forever from your life, never to
7245 return.
7246 If N is negative, delete backwards.
7247 If N is nil and articles have been marked with the process mark,
7248 delete these instead."
7249   (interactive "P")
7250   (unless (gnus-check-backend-function 'request-expire-articles
7251                                        gnus-newsgroup-name)
7252     (error "The current newsgroup does not support article deletion"))
7253   ;; Compute the list of articles to delete.
7254   (let ((articles (gnus-summary-work-articles n))
7255         not-deleted)
7256     (if (and gnus-novice-user
7257              (not (gnus-yes-or-no-p
7258                    (format "Do you really want to delete %s forever? "
7259                            (if (> (length articles) 1)
7260                                (format "these %s articles" (length articles))
7261                              "this article")))))
7262         ()
7263       ;; Delete the articles.
7264       (setq not-deleted (gnus-request-expire-articles
7265                          articles gnus-newsgroup-name 'force))
7266       (while articles
7267         (gnus-summary-remove-process-mark (car articles))
7268         ;; The backend might not have been able to delete the article
7269         ;; after all.
7270         (unless (memq (car articles) not-deleted)
7271           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7272         (setq articles (cdr articles)))
7273       (when not-deleted
7274         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7275     (gnus-summary-position-point)
7276     (gnus-set-mode-line 'summary)
7277     not-deleted))
7278
7279 (defun gnus-summary-edit-article (&optional force)
7280   "Edit the current article.
7281 This will have permanent effect only in mail groups.
7282 If FORCE is non-nil, allow editing of articles even in read-only
7283 groups."
7284   (interactive "P")
7285   (save-excursion
7286     (set-buffer gnus-summary-buffer)
7287     (gnus-set-global-variables)
7288     (when (and (not force)
7289                (gnus-group-read-only-p))
7290       (error "The current newsgroup does not support article editing"))
7291     ;; Select article if needed.
7292     (unless (eq (gnus-summary-article-number)
7293                 gnus-current-article)
7294       (gnus-summary-select-article t))
7295     (gnus-article-date-original)
7296     (gnus-article-edit-article
7297      `(lambda (no-highlight)
7298         (gnus-summary-edit-article-done
7299          ,(or (mail-header-references gnus-current-headers) "")
7300          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7301
7302 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7303
7304 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7305                                                  no-highlight)
7306   "Make edits to the current article permanent."
7307   (interactive)
7308   ;; Replace the article.
7309   (if (and (not read-only)
7310            (not (gnus-request-replace-article
7311                  (cdr gnus-article-current) (car gnus-article-current)
7312                  (current-buffer))))
7313       (error "Couldn't replace article")
7314     ;; Update the summary buffer.
7315     (if (and references
7316              (equal (message-tokenize-header references " ")
7317                     (message-tokenize-header
7318                      (or (message-fetch-field "references") "") " ")))
7319         ;; We only have to update this line.
7320         (save-excursion
7321           (save-restriction
7322             (message-narrow-to-head)
7323             (let ((head (buffer-string))
7324                   header)
7325               (nnheader-temp-write nil
7326                 (insert (format "211 %d Article retrieved.\n"
7327                                 (cdr gnus-article-current)))
7328                 (insert head)
7329                 (insert ".\n")
7330                 (let ((nntp-server-buffer (current-buffer)))
7331                   (setq header (car (gnus-get-newsgroup-headers
7332                                      (save-excursion
7333                                        (set-buffer gnus-summary-buffer)
7334                                        gnus-newsgroup-dependencies)
7335                                      t))))
7336                 (save-excursion
7337                   (set-buffer gnus-summary-buffer)
7338                   (gnus-data-set-header
7339                    (gnus-data-find (cdr gnus-article-current))
7340                    header)
7341                   (gnus-summary-update-article-line
7342                    (cdr gnus-article-current) header))))))
7343       ;; Update threads.
7344       (set-buffer (or buffer gnus-summary-buffer))
7345       (gnus-summary-update-article (cdr gnus-article-current)))
7346     ;; Prettify the article buffer again.
7347     (unless no-highlight
7348       (save-excursion
7349         (set-buffer gnus-article-buffer)
7350         (gnus-run-hooks 'gnus-article-display-hook)
7351         (set-buffer gnus-original-article-buffer)
7352         (gnus-request-article
7353          (cdr gnus-article-current)
7354          (car gnus-article-current) (current-buffer))))
7355     ;; Prettify the summary buffer line.
7356     (when (gnus-visual-p 'summary-highlight 'highlight)
7357       (gnus-run-hooks 'gnus-visual-mark-article-hook))))
7358
7359 (defun gnus-summary-edit-wash (key)
7360   "Perform editing command KEY in the article buffer."
7361   (interactive
7362    (list
7363     (progn
7364       (message "%s" (concat (this-command-keys) "- "))
7365       (read-char))))
7366   (message "")
7367   (gnus-summary-edit-article)
7368   (execute-kbd-macro (concat (this-command-keys) key))
7369   (gnus-article-edit-done))
7370
7371 ;;; Respooling
7372
7373 (defun gnus-summary-respool-query (&optional silent)
7374   "Query where the respool algorithm would put this article."
7375   (interactive)
7376   (let (gnus-mark-article-hook)
7377     (gnus-summary-select-article)
7378     (save-excursion
7379       (set-buffer gnus-original-article-buffer)
7380       (save-restriction
7381         (message-narrow-to-head)
7382         (let ((groups (nnmail-article-group 'identity)))
7383           (unless silent
7384             (if groups
7385                 (message "This message would go to %s"
7386                          (mapconcat 'car groups ", "))
7387               (message "This message would go to no groups"))
7388             groups))))))
7389
7390 ;; Summary marking commands.
7391
7392 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7393   "Mark articles which has the same subject as read, and then select the next.
7394 If UNMARK is positive, remove any kind of mark.
7395 If UNMARK is negative, tick articles."
7396   (interactive "P")
7397   (when unmark
7398     (setq unmark (prefix-numeric-value unmark)))
7399   (let ((count
7400          (gnus-summary-mark-same-subject
7401           (gnus-summary-article-subject) unmark)))
7402     ;; Select next unread article.  If auto-select-same mode, should
7403     ;; select the first unread article.
7404     (gnus-summary-next-article t (and gnus-auto-select-same
7405                                       (gnus-summary-article-subject)))
7406     (gnus-message 7 "%d article%s marked as %s"
7407                   count (if (= count 1) " is" "s are")
7408                   (if unmark "unread" "read"))))
7409
7410 (defun gnus-summary-kill-same-subject (&optional unmark)
7411   "Mark articles which has the same subject as read.
7412 If UNMARK is positive, remove any kind of mark.
7413 If UNMARK is negative, tick articles."
7414   (interactive "P")
7415   (when unmark
7416     (setq unmark (prefix-numeric-value unmark)))
7417   (let ((count
7418          (gnus-summary-mark-same-subject
7419           (gnus-summary-article-subject) unmark)))
7420     ;; If marked as read, go to next unread subject.
7421     (when (null unmark)
7422       ;; Go to next unread subject.
7423       (gnus-summary-next-subject 1 t))
7424     (gnus-message 7 "%d articles are marked as %s"
7425                   count (if unmark "unread" "read"))))
7426
7427 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7428   "Mark articles with same SUBJECT as read, and return marked number.
7429 If optional argument UNMARK is positive, remove any kinds of marks.
7430 If optional argument UNMARK is negative, mark articles as unread instead."
7431   (let ((count 1))
7432     (save-excursion
7433       (cond
7434        ((null unmark)                   ; Mark as read.
7435         (while (and
7436                 (progn
7437                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7438                   (gnus-summary-show-thread) t)
7439                 (gnus-summary-find-subject subject))
7440           (setq count (1+ count))))
7441        ((> unmark 0)                    ; Tick.
7442         (while (and
7443                 (progn
7444                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7445                   (gnus-summary-show-thread) t)
7446                 (gnus-summary-find-subject subject))
7447           (setq count (1+ count))))
7448        (t                               ; Mark as unread.
7449         (while (and
7450                 (progn
7451                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7452                   (gnus-summary-show-thread) t)
7453                 (gnus-summary-find-subject subject))
7454           (setq count (1+ count)))))
7455       (gnus-set-mode-line 'summary)
7456       ;; Return the number of marked articles.
7457       count)))
7458
7459 (defun gnus-summary-mark-as-processable (n &optional unmark)
7460   "Set the process mark on the next N articles.
7461 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7462 the process mark instead.  The difference between N and the actual
7463 number of articles marked is returned."
7464   (interactive "p")
7465   (let ((backward (< n 0))
7466         (n (abs n)))
7467     (while (and
7468             (> n 0)
7469             (if unmark
7470                 (gnus-summary-remove-process-mark
7471                  (gnus-summary-article-number))
7472               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7473             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7474       (setq n (1- n)))
7475     (when (/= 0 n)
7476       (gnus-message 7 "No more articles"))
7477     (gnus-summary-recenter)
7478     (gnus-summary-position-point)
7479     n))
7480
7481 (defun gnus-summary-unmark-as-processable (n)
7482   "Remove the process mark from the next N articles.
7483 If N is negative, unmark backward instead.  The difference between N and
7484 the actual number of articles unmarked is returned."
7485   (interactive "p")
7486   (gnus-summary-mark-as-processable n t))
7487
7488 (defun gnus-summary-unmark-all-processable ()
7489   "Remove the process mark from all articles."
7490   (interactive)
7491   (save-excursion
7492     (while gnus-newsgroup-processable
7493       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7494   (gnus-summary-position-point))
7495
7496 (defun gnus-summary-mark-as-expirable (n)
7497   "Mark N articles forward as expirable.
7498 If N is negative, mark backward instead.  The difference between N and
7499 the actual number of articles marked is returned."
7500   (interactive "p")
7501   (gnus-summary-mark-forward n gnus-expirable-mark))
7502
7503 (defun gnus-summary-mark-article-as-replied (article)
7504   "Mark ARTICLE replied and update the summary line."
7505   (push article gnus-newsgroup-replied)
7506   (let ((buffer-read-only nil))
7507     (when (gnus-summary-goto-subject article)
7508       (gnus-summary-update-secondary-mark article))))
7509
7510 (defun gnus-summary-set-bookmark (article)
7511   "Set a bookmark in current article."
7512   (interactive (list (gnus-summary-article-number)))
7513   (when (or (not (get-buffer gnus-article-buffer))
7514             (not gnus-current-article)
7515             (not gnus-article-current)
7516             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7517     (error "No current article selected"))
7518   ;; Remove old bookmark, if one exists.
7519   (let ((old (assq article gnus-newsgroup-bookmarks)))
7520     (when old
7521       (setq gnus-newsgroup-bookmarks
7522             (delq old gnus-newsgroup-bookmarks))))
7523   ;; Set the new bookmark, which is on the form
7524   ;; (article-number . line-number-in-body).
7525   (push
7526    (cons article
7527          (save-excursion
7528            (set-buffer gnus-article-buffer)
7529            (count-lines
7530             (min (point)
7531                  (save-excursion
7532                    (goto-char (point-min))
7533                    (search-forward "\n\n" nil t)
7534                    (point)))
7535             (point))))
7536    gnus-newsgroup-bookmarks)
7537   (gnus-message 6 "A bookmark has been added to the current article."))
7538
7539 (defun gnus-summary-remove-bookmark (article)
7540   "Remove the bookmark from the current article."
7541   (interactive (list (gnus-summary-article-number)))
7542   ;; Remove old bookmark, if one exists.
7543   (let ((old (assq article gnus-newsgroup-bookmarks)))
7544     (if old
7545         (progn
7546           (setq gnus-newsgroup-bookmarks
7547                 (delq old gnus-newsgroup-bookmarks))
7548           (gnus-message 6 "Removed bookmark."))
7549       (gnus-message 6 "No bookmark in current article."))))
7550
7551 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7552 (defun gnus-summary-mark-as-dormant (n)
7553   "Mark N articles forward as dormant.
7554 If N is negative, mark backward instead.  The difference between N and
7555 the actual number of articles marked is returned."
7556   (interactive "p")
7557   (gnus-summary-mark-forward n gnus-dormant-mark))
7558
7559 (defun gnus-summary-set-process-mark (article)
7560   "Set the process mark on ARTICLE and update the summary line."
7561   (setq gnus-newsgroup-processable
7562         (cons article
7563               (delq article gnus-newsgroup-processable)))
7564   (when (gnus-summary-goto-subject article)
7565     (gnus-summary-show-thread)
7566     (gnus-summary-update-secondary-mark article)))
7567
7568 (defun gnus-summary-remove-process-mark (article)
7569   "Remove the process mark from ARTICLE and update the summary line."
7570   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7571   (when (gnus-summary-goto-subject article)
7572     (gnus-summary-show-thread)
7573     (gnus-summary-update-secondary-mark article)))
7574
7575 (defun gnus-summary-set-saved-mark (article)
7576   "Set the process mark on ARTICLE and update the summary line."
7577   (push article gnus-newsgroup-saved)
7578   (when (gnus-summary-goto-subject article)
7579     (gnus-summary-update-secondary-mark article)))
7580
7581 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7582   "Mark N articles as read forwards.
7583 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7584 The difference between N and the actual number of articles marked is
7585 returned."
7586   (interactive "p")
7587   (let ((backward (< n 0))
7588         (gnus-summary-goto-unread
7589          (and gnus-summary-goto-unread
7590               (not (eq gnus-summary-goto-unread 'never))
7591               (not (memq mark (list gnus-unread-mark
7592                                     gnus-ticked-mark gnus-dormant-mark)))))
7593         (n (abs n))
7594         (mark (or mark gnus-del-mark)))
7595     (while (and (> n 0)
7596                 (gnus-summary-mark-article nil mark no-expire)
7597                 (zerop (gnus-summary-next-subject
7598                         (if backward -1 1)
7599                         (and gnus-summary-goto-unread
7600                              (not (eq gnus-summary-goto-unread 'never)))
7601                         t)))
7602       (setq n (1- n)))
7603     (when (/= 0 n)
7604       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7605     (gnus-summary-recenter)
7606     (gnus-summary-position-point)
7607     (gnus-set-mode-line 'summary)
7608     n))
7609
7610 (defun gnus-summary-mark-article-as-read (mark)
7611   "Mark the current article quickly as read with MARK."
7612   (let ((article (gnus-summary-article-number)))
7613     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7614     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7615     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7616     (push (cons article mark) gnus-newsgroup-reads)
7617     ;; Possibly remove from cache, if that is used.
7618     (when gnus-use-cache
7619       (gnus-cache-enter-remove-article article))
7620     ;; Allow the backend to change the mark.
7621     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7622     ;; Check for auto-expiry.
7623     (when (and gnus-newsgroup-auto-expire
7624                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7625                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7626                    (= mark gnus-ancient-mark)
7627                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7628                    (= mark gnus-duplicate-mark)))
7629       (setq mark gnus-expirable-mark)
7630       (push article gnus-newsgroup-expirable))
7631     ;; Set the mark in the buffer.
7632     (gnus-summary-update-mark mark 'unread)
7633     t))
7634
7635 (defun gnus-summary-mark-article-as-unread (mark)
7636   "Mark the current article quickly as unread with MARK."
7637   (let* ((article (gnus-summary-article-number))
7638          (old-mark (gnus-summary-article-mark article)))
7639     (if (eq mark old-mark)
7640         t
7641       (if (<= article 0)
7642           (progn
7643             (gnus-error 1 "Can't mark negative article numbers")
7644             nil)
7645         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7646         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7647         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7648         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7649         (cond ((= mark gnus-ticked-mark)
7650                (push article gnus-newsgroup-marked))
7651               ((= mark gnus-dormant-mark)
7652                (push article gnus-newsgroup-dormant))
7653               (t
7654                (push article gnus-newsgroup-unreads)))
7655         (setq gnus-newsgroup-reads
7656               (delq (assq article gnus-newsgroup-reads)
7657                     gnus-newsgroup-reads))
7658
7659         ;; See whether the article is to be put in the cache.
7660         (and gnus-use-cache
7661              (vectorp (gnus-summary-article-header article))
7662              (save-excursion
7663                (gnus-cache-possibly-enter-article
7664                 gnus-newsgroup-name article
7665                 (gnus-summary-article-header article)
7666                 (= mark gnus-ticked-mark)
7667                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7668
7669         ;; Fix the mark.
7670         (gnus-summary-update-mark mark 'unread)
7671         t))))
7672
7673 (defun gnus-summary-mark-article (&optional article mark no-expire)
7674   "Mark ARTICLE with MARK.  MARK can be any character.
7675 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7676 `??' (dormant) and `?E' (expirable).
7677 If MARK is nil, then the default character `?D' is used.
7678 If ARTICLE is nil, then the article on the current line will be
7679 marked."
7680   ;; The mark might be a string.
7681   (when (stringp mark)
7682     (setq mark (aref mark 0)))
7683   ;; If no mark is given, then we check auto-expiring.
7684   (and (not no-expire)
7685        gnus-newsgroup-auto-expire
7686        (or (not mark)
7687            (and (gnus-characterp mark)
7688                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7689                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7690                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7691                     (= mark gnus-duplicate-mark))))
7692        (setq mark gnus-expirable-mark))
7693   (let* ((mark (or mark gnus-del-mark))
7694          (article (or article (gnus-summary-article-number)))
7695          (old-mark (gnus-summary-article-mark article)))
7696     (if (eq mark old-mark)
7697         t
7698       (unless article
7699         (error "No article on current line"))
7700       (if (not (if (or (= mark gnus-unread-mark)
7701                        (= mark gnus-ticked-mark)
7702                        (= mark gnus-dormant-mark))
7703                    (gnus-mark-article-as-unread article mark)
7704                  (gnus-mark-article-as-read article mark)))
7705           t
7706         ;; See whether the article is to be put in the cache.
7707         (and gnus-use-cache
7708              (not (= mark gnus-canceled-mark))
7709              (vectorp (gnus-summary-article-header article))
7710              (save-excursion
7711                (gnus-cache-possibly-enter-article
7712                 gnus-newsgroup-name article
7713                 (gnus-summary-article-header article)
7714                 (= mark gnus-ticked-mark)
7715                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7716
7717         (when (gnus-summary-goto-subject article nil t)
7718           (let ((buffer-read-only nil))
7719             (gnus-summary-show-thread)
7720             ;; Fix the mark.
7721             (gnus-summary-update-mark mark 'unread)
7722             t))))))
7723
7724 (defun gnus-summary-update-secondary-mark (article)
7725   "Update the secondary (read, process, cache) mark."
7726   (gnus-summary-update-mark
7727    (cond ((memq article gnus-newsgroup-processable)
7728           gnus-process-mark)
7729          ((memq article gnus-newsgroup-cached)
7730           gnus-cached-mark)
7731          ((memq article gnus-newsgroup-replied)
7732           gnus-replied-mark)
7733          ((memq article gnus-newsgroup-saved)
7734           gnus-saved-mark)
7735          (t gnus-unread-mark))
7736    'replied)
7737   (when (gnus-visual-p 'summary-highlight 'highlight)
7738     (gnus-run-hooks 'gnus-summary-update-hook))
7739   t)
7740
7741 (defun gnus-summary-update-mark (mark type)
7742   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7743         (buffer-read-only nil))
7744     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7745     (when (looking-at "\r")
7746       (incf forward))
7747     (when (and forward
7748                (<= (+ forward (point)) (point-max)))
7749       ;; Go to the right position on the line.
7750       (goto-char (+ forward (point)))
7751       ;; Replace the old mark with the new mark.
7752       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7753       ;; Optionally update the marks by some user rule.
7754       (when (eq type 'unread)
7755         (gnus-data-set-mark
7756          (gnus-data-find (gnus-summary-article-number)) mark)
7757         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7758
7759 (defun gnus-mark-article-as-read (article &optional mark)
7760   "Enter ARTICLE in the pertinent lists and remove it from others."
7761   ;; Make the article expirable.
7762   (let ((mark (or mark gnus-del-mark)))
7763     (if (= mark gnus-expirable-mark)
7764         (push article gnus-newsgroup-expirable)
7765       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7766     ;; Remove from unread and marked lists.
7767     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7768     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7769     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7770     (push (cons article mark) gnus-newsgroup-reads)
7771     ;; Possibly remove from cache, if that is used.
7772     (when gnus-use-cache
7773       (gnus-cache-enter-remove-article article))
7774     t))
7775
7776 (defun gnus-mark-article-as-unread (article &optional mark)
7777   "Enter ARTICLE in the pertinent lists and remove it from others."
7778   (let ((mark (or mark gnus-ticked-mark)))
7779     (if (<= article 0)
7780         (progn
7781           (gnus-error 1 "Can't mark negative article numbers")
7782           nil)
7783       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7784             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7785             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7786             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7787
7788       ;; Unsuppress duplicates?
7789       (when gnus-suppress-duplicates
7790         (gnus-dup-unsuppress-article article))
7791
7792       (cond ((= mark gnus-ticked-mark)
7793              (push article gnus-newsgroup-marked))
7794             ((= mark gnus-dormant-mark)
7795              (push article gnus-newsgroup-dormant))
7796             (t
7797              (push article gnus-newsgroup-unreads)))
7798       (setq gnus-newsgroup-reads
7799             (delq (assq article gnus-newsgroup-reads)
7800                   gnus-newsgroup-reads))
7801       t)))
7802
7803 (defalias 'gnus-summary-mark-as-unread-forward
7804   'gnus-summary-tick-article-forward)
7805 (make-obsolete 'gnus-summary-mark-as-unread-forward
7806                'gnus-summary-tick-article-forward)
7807 (defun gnus-summary-tick-article-forward (n)
7808   "Tick N articles forwards.
7809 If N is negative, tick backwards instead.
7810 The difference between N and the number of articles ticked is returned."
7811   (interactive "p")
7812   (gnus-summary-mark-forward n gnus-ticked-mark))
7813
7814 (defalias 'gnus-summary-mark-as-unread-backward
7815   'gnus-summary-tick-article-backward)
7816 (make-obsolete 'gnus-summary-mark-as-unread-backward
7817                'gnus-summary-tick-article-backward)
7818 (defun gnus-summary-tick-article-backward (n)
7819   "Tick N articles backwards.
7820 The difference between N and the number of articles ticked is returned."
7821   (interactive "p")
7822   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7823
7824 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7825 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7826 (defun gnus-summary-tick-article (&optional article clear-mark)
7827   "Mark current article as unread.
7828 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7829 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7830   (interactive)
7831   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7832                                        gnus-ticked-mark)))
7833
7834 (defun gnus-summary-mark-as-read-forward (n)
7835   "Mark N articles as read forwards.
7836 If N is negative, mark backwards instead.
7837 The difference between N and the actual number of articles marked is
7838 returned."
7839   (interactive "p")
7840   (gnus-summary-mark-forward n gnus-del-mark t))
7841
7842 (defun gnus-summary-mark-as-read-backward (n)
7843   "Mark the N articles as read backwards.
7844 The difference between N and the actual number of articles marked is
7845 returned."
7846   (interactive "p")
7847   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7848
7849 (defun gnus-summary-mark-as-read (&optional article mark)
7850   "Mark current article as read.
7851 ARTICLE specifies the article to be marked as read.
7852 MARK specifies a string to be inserted at the beginning of the line."
7853   (gnus-summary-mark-article article mark))
7854
7855 (defun gnus-summary-clear-mark-forward (n)
7856   "Clear marks from N articles forward.
7857 If N is negative, clear backward instead.
7858 The difference between N and the number of marks cleared is returned."
7859   (interactive "p")
7860   (gnus-summary-mark-forward n gnus-unread-mark))
7861
7862 (defun gnus-summary-clear-mark-backward (n)
7863   "Clear marks from N articles backward.
7864 The difference between N and the number of marks cleared is returned."
7865   (interactive "p")
7866   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7867
7868 (defun gnus-summary-mark-unread-as-read ()
7869   "Intended to be used by `gnus-summary-mark-article-hook'."
7870   (when (memq gnus-current-article gnus-newsgroup-unreads)
7871     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7872
7873 (defun gnus-summary-mark-read-and-unread-as-read ()
7874   "Intended to be used by `gnus-summary-mark-article-hook'."
7875   (let ((mark (gnus-summary-article-mark)))
7876     (when (or (gnus-unread-mark-p mark)
7877               (gnus-read-mark-p mark))
7878       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7879
7880 (defun gnus-summary-mark-region-as-read (point mark all)
7881   "Mark all unread articles between point and mark as read.
7882 If given a prefix, mark all articles between point and mark as read,
7883 even ticked and dormant ones."
7884   (interactive "r\nP")
7885   (save-excursion
7886     (let (article)
7887       (goto-char point)
7888       (beginning-of-line)
7889       (while (and
7890               (< (point) mark)
7891               (progn
7892                 (when (or all
7893                           (memq (setq article (gnus-summary-article-number))
7894                                 gnus-newsgroup-unreads))
7895                   (gnus-summary-mark-article article gnus-del-mark))
7896                 t)
7897               (gnus-summary-find-next))))))
7898
7899 (defun gnus-summary-mark-below (score mark)
7900   "Mark articles with score less than SCORE with MARK."
7901   (interactive "P\ncMark: ")
7902   (setq score (if score
7903                   (prefix-numeric-value score)
7904                 (or gnus-summary-default-score 0)))
7905   (save-excursion
7906     (set-buffer gnus-summary-buffer)
7907     (goto-char (point-min))
7908     (while
7909         (progn
7910           (and (< (gnus-summary-article-score) score)
7911                (gnus-summary-mark-article nil mark))
7912           (gnus-summary-find-next)))))
7913
7914 (defun gnus-summary-kill-below (&optional score)
7915   "Mark articles with score below SCORE as read."
7916   (interactive "P")
7917   (gnus-summary-mark-below score gnus-killed-mark))
7918
7919 (defun gnus-summary-clear-above (&optional score)
7920   "Clear all marks from articles with score above SCORE."
7921   (interactive "P")
7922   (gnus-summary-mark-above score gnus-unread-mark))
7923
7924 (defun gnus-summary-tick-above (&optional score)
7925   "Tick all articles with score above SCORE."
7926   (interactive "P")
7927   (gnus-summary-mark-above score gnus-ticked-mark))
7928
7929 (defun gnus-summary-mark-above (score mark)
7930   "Mark articles with score over SCORE with MARK."
7931   (interactive "P\ncMark: ")
7932   (setq score (if score
7933                   (prefix-numeric-value score)
7934                 (or gnus-summary-default-score 0)))
7935   (save-excursion
7936     (set-buffer gnus-summary-buffer)
7937     (goto-char (point-min))
7938     (while (and (progn
7939                   (when (> (gnus-summary-article-score) score)
7940                     (gnus-summary-mark-article nil mark))
7941                   t)
7942                 (gnus-summary-find-next)))))
7943
7944 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7945 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7946 (defun gnus-summary-limit-include-expunged (&optional no-error)
7947   "Display all the hidden articles that were expunged for low scores."
7948   (interactive)
7949   (let ((buffer-read-only nil))
7950     (let ((scored gnus-newsgroup-scored)
7951           headers h)
7952       (while scored
7953         (unless (gnus-summary-goto-subject (caar scored))
7954           (and (setq h (gnus-summary-article-header (caar scored)))
7955                (< (cdar scored) gnus-summary-expunge-below)
7956                (push h headers)))
7957         (setq scored (cdr scored)))
7958       (if (not headers)
7959           (when (not no-error)
7960             (error "No expunged articles hidden"))
7961         (goto-char (point-min))
7962         (gnus-summary-prepare-unthreaded (nreverse headers))
7963         (goto-char (point-min))
7964         (gnus-summary-position-point)
7965         t))))
7966
7967 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7968   "Mark all unread articles in this newsgroup as read.
7969 If prefix argument ALL is non-nil, ticked and dormant articles will
7970 also be marked as read.
7971 If QUIETLY is non-nil, no questions will be asked.
7972 If TO-HERE is non-nil, it should be a point in the buffer.  All
7973 articles before this point will be marked as read.
7974 Note that this function will only catch up the unread article
7975 in the current summary buffer limitation.
7976 The number of articles marked as read is returned."
7977   (interactive "P")
7978   (prog1
7979       (save-excursion
7980         (when (or quietly
7981                   (not gnus-interactive-catchup) ;Without confirmation?
7982                   gnus-expert-user
7983                   (gnus-y-or-n-p
7984                    (if all
7985                        "Mark absolutely all articles as read? "
7986                      "Mark all unread articles as read? ")))
7987           (if (and not-mark
7988                    (not gnus-newsgroup-adaptive)
7989                    (not gnus-newsgroup-auto-expire)
7990                    (not gnus-suppress-duplicates)
7991                    (or (not gnus-use-cache)
7992                        (eq gnus-use-cache 'passive)))
7993               (progn
7994                 (when all
7995                   (setq gnus-newsgroup-marked nil
7996                         gnus-newsgroup-dormant nil))
7997                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
7998             ;; We actually mark all articles as canceled, which we
7999             ;; have to do when using auto-expiry or adaptive scoring.
8000             (gnus-summary-show-all-threads)
8001             (when (gnus-summary-first-subject (not all) t)
8002               (while (and
8003                       (if to-here (< (point) to-here) t)
8004                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8005                       (gnus-summary-find-next (not all) nil nil t))))
8006             (gnus-set-mode-line 'summary))
8007           t))
8008     (gnus-summary-position-point)))
8009
8010 (defun gnus-summary-catchup-to-here (&optional all)
8011   "Mark all unticked articles before the current one as read.
8012 If ALL is non-nil, also mark ticked and dormant articles as read."
8013   (interactive "P")
8014   (save-excursion
8015     (gnus-save-hidden-threads
8016       (let ((beg (point)))
8017         ;; We check that there are unread articles.
8018         (when (or all (gnus-summary-find-prev))
8019           (gnus-summary-catchup all t beg)))))
8020   (gnus-summary-position-point))
8021
8022 (defun gnus-summary-catchup-all (&optional quietly)
8023   "Mark all articles in this newsgroup as read."
8024   (interactive "P")
8025   (gnus-summary-catchup t quietly))
8026
8027 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8028   "Mark all articles not marked as unread in this newsgroup as read, then exit.
8029 If prefix argument ALL is non-nil, all articles are marked as read."
8030   (interactive "P")
8031   (when (gnus-summary-catchup all quietly nil 'fast)
8032     ;; Select next newsgroup or exit.
8033     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8034              (eq gnus-auto-select-next 'quietly))
8035         (gnus-summary-next-group nil)
8036       (gnus-summary-exit))))
8037
8038 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8039   "Mark all articles in this newsgroup as read, and then exit."
8040   (interactive "P")
8041   (gnus-summary-catchup-and-exit t quietly))
8042
8043 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8044 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8045   "Mark all articles in this group as read and select the next group.
8046 If given a prefix, mark all articles, unread as well as ticked, as
8047 read."
8048   (interactive "P")
8049   (save-excursion
8050     (gnus-summary-catchup all))
8051   (gnus-summary-next-article t nil nil t))
8052
8053 ;; Thread-based commands.
8054
8055 (defun gnus-summary-articles-in-thread (&optional article)
8056   "Return a list of all articles in the current thread.
8057 If ARTICLE is non-nil, return all articles in the thread that starts
8058 with that article."
8059   (let* ((article (or article (gnus-summary-article-number)))
8060          (data (gnus-data-find-list article))
8061          (top-level (gnus-data-level (car data)))
8062          (top-subject
8063           (cond ((null gnus-thread-operation-ignore-subject)
8064                  (gnus-simplify-subject-re
8065                   (mail-header-subject (gnus-data-header (car data)))))
8066                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8067                  (gnus-simplify-subject-fuzzy
8068                   (mail-header-subject (gnus-data-header (car data)))))
8069                 (t nil)))
8070          (end-point (save-excursion
8071                       (if (gnus-summary-go-to-next-thread)
8072                           (point) (point-max))))
8073          articles)
8074     (while (and data
8075                 (< (gnus-data-pos (car data)) end-point))
8076       (when (or (not top-subject)
8077                 (string= top-subject
8078                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8079                              (gnus-simplify-subject-fuzzy
8080                               (mail-header-subject
8081                                (gnus-data-header (car data))))
8082                            (gnus-simplify-subject-re
8083                             (mail-header-subject
8084                              (gnus-data-header (car data)))))))
8085         (push (gnus-data-number (car data)) articles))
8086       (unless (and (setq data (cdr data))
8087                    (> (gnus-data-level (car data)) top-level))
8088         (setq data nil)))
8089     ;; Return the list of articles.
8090     (nreverse articles)))
8091
8092 (defun gnus-summary-rethread-current ()
8093   "Rethread the thread the current article is part of."
8094   (interactive)
8095   (let* ((gnus-show-threads t)
8096          (article (gnus-summary-article-number))
8097          (id (mail-header-id (gnus-summary-article-header)))
8098          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8099     (unless id
8100       (error "No article on the current line"))
8101     (gnus-rebuild-thread id)
8102     (gnus-summary-goto-subject article)))
8103
8104 (defun gnus-summary-reparent-thread ()
8105   "Make the current article child of the marked (or previous) article.
8106
8107 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8108 is non-nil or the Subject: of both articles are the same."
8109   (interactive)
8110   (unless (not (gnus-group-read-only-p))
8111     (error "The current newsgroup does not support article editing"))
8112   (unless (<= (length gnus-newsgroup-processable) 1)
8113     (error "No more than one article may be marked"))
8114   (save-window-excursion
8115     (let ((gnus-article-buffer " *reparent*")
8116           (current-article (gnus-summary-article-number))
8117           ;; First grab the marked article, otherwise one line up.
8118           (parent-article (if (not (null gnus-newsgroup-processable))
8119                               (car gnus-newsgroup-processable)
8120                             (save-excursion
8121                               (if (eq (forward-line -1) 0)
8122                                   (gnus-summary-article-number)
8123                                 (error "Beginning of summary buffer"))))))
8124       (unless (not (eq current-article parent-article))
8125         (error "An article may not be self-referential"))
8126       (let ((message-id (mail-header-id
8127                          (gnus-summary-article-header parent-article))))
8128         (unless (and message-id (not (equal message-id "")))
8129           (error "No message-id in desired parent"))
8130         ;; We don't want the article to be marked as read.
8131         (let (gnus-mark-article-hook)
8132           (gnus-summary-select-article t t nil current-article))
8133         (set-buffer gnus-original-article-buffer)
8134         (let ((buf (format "%s" (buffer-string))))
8135           (nnheader-temp-write nil
8136             (insert buf)
8137             (goto-char (point-min))
8138             (if (re-search-forward "^References: " nil t)
8139                 (progn
8140                   (re-search-forward "^[^ \t]" nil t)
8141                   (forward-line -1)
8142                   (end-of-line)
8143                   (insert " " message-id))
8144               (insert "References: " message-id "\n"))
8145             (unless (gnus-request-replace-article
8146                      current-article (car gnus-article-current)
8147                      (current-buffer))
8148               (error "Couldn't replace article"))))
8149         (set-buffer gnus-summary-buffer)
8150         (gnus-summary-unmark-all-processable)
8151         (gnus-summary-update-article current-article)
8152         (gnus-summary-rethread-current)
8153         (gnus-message 3 "Article %d is now the child of article %d"
8154                       current-article parent-article)))))
8155
8156 (defun gnus-summary-toggle-threads (&optional arg)
8157   "Toggle showing conversation threads.
8158 If ARG is positive number, turn showing conversation threads on."
8159   (interactive "P")
8160   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8161     (setq gnus-show-threads
8162           (if (null arg) (not gnus-show-threads)
8163             (> (prefix-numeric-value arg) 0)))
8164     (gnus-summary-prepare)
8165     (gnus-summary-goto-subject current)
8166     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8167     (gnus-summary-position-point)))
8168
8169 (defun gnus-summary-show-all-threads ()
8170   "Show all threads."
8171   (interactive)
8172   (save-excursion
8173     (let ((buffer-read-only nil))
8174       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8175   (gnus-summary-position-point))
8176
8177 (defun gnus-summary-show-thread ()
8178   "Show thread subtrees.
8179 Returns nil if no thread was there to be shown."
8180   (interactive)
8181   (let ((buffer-read-only nil)
8182         (orig (point))
8183         ;; first goto end then to beg, to have point at beg after let
8184         (end (progn (end-of-line) (point)))
8185         (beg (progn (beginning-of-line) (point))))
8186     (prog1
8187         ;; Any hidden lines here?
8188         (search-forward "\r" end t)
8189       (subst-char-in-region beg end ?\^M ?\n t)
8190       (goto-char orig)
8191       (gnus-summary-position-point))))
8192
8193 (defun gnus-summary-hide-all-threads ()
8194   "Hide all thread subtrees."
8195   (interactive)
8196   (save-excursion
8197     (goto-char (point-min))
8198     (gnus-summary-hide-thread)
8199     (while (zerop (gnus-summary-next-thread 1 t))
8200       (gnus-summary-hide-thread)))
8201   (gnus-summary-position-point))
8202
8203 (defun gnus-summary-hide-thread ()
8204   "Hide thread subtrees.
8205 Returns nil if no threads were there to be hidden."
8206   (interactive)
8207   (let ((buffer-read-only nil)
8208         (start (point))
8209         (article (gnus-summary-article-number)))
8210     (goto-char start)
8211     ;; Go forward until either the buffer ends or the subthread
8212     ;; ends.
8213     (when (and (not (eobp))
8214                (or (zerop (gnus-summary-next-thread 1 t))
8215                    (goto-char (point-max))))
8216       (prog1
8217           (if (and (> (point) start)
8218                    (search-backward "\n" start t))
8219               (progn
8220                 (subst-char-in-region start (point) ?\n ?\^M)
8221                 (gnus-summary-goto-subject article))
8222             (goto-char start)
8223             nil)
8224         ;;(gnus-summary-position-point)
8225         ))))
8226
8227 (defun gnus-summary-go-to-next-thread (&optional previous)
8228   "Go to the same level (or less) next thread.
8229 If PREVIOUS is non-nil, go to previous thread instead.
8230 Return the article number moved to, or nil if moving was impossible."
8231   (let ((level (gnus-summary-thread-level))
8232         (way (if previous -1 1))
8233         (beg (point)))
8234     (forward-line way)
8235     (while (and (not (eobp))
8236                 (< level (gnus-summary-thread-level)))
8237       (forward-line way))
8238     (if (eobp)
8239         (progn
8240           (goto-char beg)
8241           nil)
8242       (setq beg (point))
8243       (prog1
8244           (gnus-summary-article-number)
8245         (goto-char beg)))))
8246
8247 (defun gnus-summary-next-thread (n &optional silent)
8248   "Go to the same level next N'th thread.
8249 If N is negative, search backward instead.
8250 Returns the difference between N and the number of skips actually
8251 done.
8252
8253 If SILENT, don't output messages."
8254   (interactive "p")
8255   (let ((backward (< n 0))
8256         (n (abs n)))
8257     (while (and (> n 0)
8258                 (gnus-summary-go-to-next-thread backward))
8259       (decf n))
8260     (unless silent
8261       (gnus-summary-position-point))
8262     (when (and (not silent) (/= 0 n))
8263       (gnus-message 7 "No more threads"))
8264     n))
8265
8266 (defun gnus-summary-prev-thread (n)
8267   "Go to the same level previous N'th thread.
8268 Returns the difference between N and the number of skips actually
8269 done."
8270   (interactive "p")
8271   (gnus-summary-next-thread (- n)))
8272
8273 (defun gnus-summary-go-down-thread ()
8274   "Go down one level in the current thread."
8275   (let ((children (gnus-summary-article-children)))
8276     (when children
8277       (gnus-summary-goto-subject (car children)))))
8278
8279 (defun gnus-summary-go-up-thread ()
8280   "Go up one level in the current thread."
8281   (let ((parent (gnus-summary-article-parent)))
8282     (when parent
8283       (gnus-summary-goto-subject parent))))
8284
8285 (defun gnus-summary-down-thread (n)
8286   "Go down thread N steps.
8287 If N is negative, go up instead.
8288 Returns the difference between N and how many steps down that were
8289 taken."
8290   (interactive "p")
8291   (let ((up (< n 0))
8292         (n (abs n)))
8293     (while (and (> n 0)
8294                 (if up (gnus-summary-go-up-thread)
8295                   (gnus-summary-go-down-thread)))
8296       (setq n (1- n)))
8297     (gnus-summary-position-point)
8298     (when (/= 0 n)
8299       (gnus-message 7 "Can't go further"))
8300     n))
8301
8302 (defun gnus-summary-up-thread (n)
8303   "Go up thread N steps.
8304 If N is negative, go up instead.
8305 Returns the difference between N and how many steps down that were
8306 taken."
8307   (interactive "p")
8308   (gnus-summary-down-thread (- n)))
8309
8310 (defun gnus-summary-top-thread ()
8311   "Go to the top of the thread."
8312   (interactive)
8313   (while (gnus-summary-go-up-thread))
8314   (gnus-summary-article-number))
8315
8316 (defun gnus-summary-kill-thread (&optional unmark)
8317   "Mark articles under current thread as read.
8318 If the prefix argument is positive, remove any kinds of marks.
8319 If the prefix argument is negative, tick articles instead."
8320   (interactive "P")
8321   (when unmark
8322     (setq unmark (prefix-numeric-value unmark)))
8323   (let ((articles (gnus-summary-articles-in-thread)))
8324     (save-excursion
8325       ;; Expand the thread.
8326       (gnus-summary-show-thread)
8327       ;; Mark all the articles.
8328       (while articles
8329         (gnus-summary-goto-subject (car articles))
8330         (cond ((null unmark)
8331                (gnus-summary-mark-article-as-read gnus-killed-mark))
8332               ((> unmark 0)
8333                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8334               (t
8335                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8336         (setq articles (cdr articles))))
8337     ;; Hide killed subtrees.
8338     (and (null unmark)
8339          gnus-thread-hide-killed
8340          (gnus-summary-hide-thread))
8341     ;; If marked as read, go to next unread subject.
8342     (when (null unmark)
8343       ;; Go to next unread subject.
8344       (gnus-summary-next-subject 1 t)))
8345   (gnus-set-mode-line 'summary))
8346
8347 ;; Summary sorting commands
8348
8349 (defun gnus-summary-sort-by-number (&optional reverse)
8350   "Sort the summary buffer by article number.
8351 Argument REVERSE means reverse order."
8352   (interactive "P")
8353   (gnus-summary-sort 'number reverse))
8354
8355 (defun gnus-summary-sort-by-author (&optional reverse)
8356   "Sort the summary buffer by author name alphabetically.
8357 If case-fold-search is non-nil, case of letters is ignored.
8358 Argument REVERSE means reverse order."
8359   (interactive "P")
8360   (gnus-summary-sort 'author reverse))
8361
8362 (defun gnus-summary-sort-by-subject (&optional reverse)
8363   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8364 If case-fold-search is non-nil, case of letters is ignored.
8365 Argument REVERSE means reverse order."
8366   (interactive "P")
8367   (gnus-summary-sort 'subject reverse))
8368
8369 (defun gnus-summary-sort-by-date (&optional reverse)
8370   "Sort the summary buffer by date.
8371 Argument REVERSE means reverse order."
8372   (interactive "P")
8373   (gnus-summary-sort 'date reverse))
8374
8375 (defun gnus-summary-sort-by-score (&optional reverse)
8376   "Sort the summary buffer by score.
8377 Argument REVERSE means reverse order."
8378   (interactive "P")
8379   (gnus-summary-sort 'score reverse))
8380
8381 (defun gnus-summary-sort-by-lines (&optional reverse)
8382   "Sort the summary buffer by article length.
8383 Argument REVERSE means reverse order."
8384   (interactive "P")
8385   (gnus-summary-sort 'lines reverse))
8386
8387 (defun gnus-summary-sort (predicate reverse)
8388   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8389   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8390          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8391          (gnus-thread-sort-functions
8392           (list
8393            (if (not reverse)
8394                thread
8395              `(lambda (t1 t2)
8396                 (,thread t2 t1)))))
8397          (gnus-article-sort-functions
8398           (list
8399            (if (not reverse)
8400                article
8401              `(lambda (t1 t2)
8402                 (,article t2 t1)))))
8403          (buffer-read-only)
8404          (gnus-summary-prepare-hook nil))
8405     ;; We do the sorting by regenerating the threads.
8406     (gnus-summary-prepare)
8407     ;; Hide subthreads if needed.
8408     (when (and gnus-show-threads gnus-thread-hide-subtree)
8409       (gnus-summary-hide-all-threads))))
8410
8411 ;; Summary saving commands.
8412
8413 (defun gnus-summary-save-article (&optional n not-saved)
8414   "Save the current article using the default saver function.
8415 If N is a positive number, save the N next articles.
8416 If N is a negative number, save the N previous articles.
8417 If N is nil and any articles have been marked with the process mark,
8418 save those articles instead.
8419 The variable `gnus-default-article-saver' specifies the saver function."
8420   (interactive "P")
8421   (let* ((articles (gnus-summary-work-articles n))
8422          (save-buffer (save-excursion
8423                         (nnheader-set-temp-buffer " *Gnus Save*")))
8424          (num (length articles))
8425          header article file)
8426     (while articles
8427       (setq header (gnus-summary-article-header
8428                     (setq article (pop articles))))
8429       (if (not (vectorp header))
8430           ;; This is a pseudo-article.
8431           (if (assq 'name header)
8432               (gnus-copy-file (cdr (assq 'name header)))
8433             (gnus-message 1 "Article %d is unsaveable" article))
8434         ;; This is a real article.
8435         (save-window-excursion
8436           (gnus-summary-select-article t nil nil article))
8437         (save-excursion
8438           (set-buffer save-buffer)
8439           (erase-buffer)
8440           (insert-buffer-substring gnus-original-article-buffer))
8441         (setq file (gnus-article-save save-buffer file num))
8442         (gnus-summary-remove-process-mark article)
8443         (unless not-saved
8444           (gnus-summary-set-saved-mark article))))
8445     (gnus-kill-buffer save-buffer)
8446     (gnus-summary-position-point)
8447     (gnus-set-mode-line 'summary)
8448     n))
8449
8450 (defun gnus-summary-pipe-output (&optional arg)
8451   "Pipe the current article to a subprocess.
8452 If N is a positive number, pipe the N next articles.
8453 If N is a negative number, pipe the N previous articles.
8454 If N is nil and any articles have been marked with the process mark,
8455 pipe those articles instead."
8456   (interactive "P")
8457   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8458     (gnus-summary-save-article arg t))
8459   (gnus-configure-windows 'pipe))
8460
8461 (defun gnus-summary-save-article-mail (&optional arg)
8462   "Append the current article to an mail file.
8463 If N is a positive number, save the N next articles.
8464 If N is a negative number, save the N previous articles.
8465 If N is nil and any articles have been marked with the process mark,
8466 save those articles instead."
8467   (interactive "P")
8468   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8469     (gnus-summary-save-article arg)))
8470
8471 (defun gnus-summary-save-article-rmail (&optional arg)
8472   "Append the current article to an rmail file.
8473 If N is a positive number, save the N next articles.
8474 If N is a negative number, save the N previous articles.
8475 If N is nil and any articles have been marked with the process mark,
8476 save those articles instead."
8477   (interactive "P")
8478   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8479     (gnus-summary-save-article arg)))
8480
8481 (defun gnus-summary-save-article-file (&optional arg)
8482   "Append the current article to a file.
8483 If N is a positive number, save the N next articles.
8484 If N is a negative number, save the N previous articles.
8485 If N is nil and any articles have been marked with the process mark,
8486 save those articles instead."
8487   (interactive "P")
8488   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8489     (gnus-summary-save-article arg)))
8490
8491 (defun gnus-summary-write-article-file (&optional arg)
8492   "Write the current article to a file, deleting the previous file.
8493 If N is a positive number, save the N next articles.
8494 If N is a negative number, save the N previous articles.
8495 If N is nil and any articles have been marked with the process mark,
8496 save those articles instead."
8497   (interactive "P")
8498   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8499     (gnus-summary-save-article arg)))
8500
8501 (defun gnus-summary-save-article-body-file (&optional arg)
8502   "Append the current article body to a file.
8503 If N is a positive number, save the N next articles.
8504 If N is a negative number, save the N previous articles.
8505 If N is nil and any articles have been marked with the process mark,
8506 save those articles instead."
8507   (interactive "P")
8508   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8509     (gnus-summary-save-article arg)))
8510
8511 (defun gnus-summary-pipe-message (program)
8512   "Pipe the current article through PROGRAM."
8513   (interactive "sProgram: ")
8514   (gnus-summary-select-article)
8515   (let ((mail-header-separator "")
8516         (art-buf (get-buffer gnus-article-buffer)))
8517     (gnus-eval-in-buffer-window gnus-article-buffer
8518       (save-restriction
8519         (widen)
8520         (let ((start (window-start))
8521               buffer-read-only)
8522           (message-pipe-buffer-body program)
8523           (set-window-start (get-buffer-window (current-buffer)) start))))))
8524
8525 (defun gnus-get-split-value (methods)
8526   "Return a value based on the split METHODS."
8527   (let (split-name method result match)
8528     (when methods
8529       (save-excursion
8530         (set-buffer gnus-original-article-buffer)
8531         (save-restriction
8532           (nnheader-narrow-to-headers)
8533           (while methods
8534             (goto-char (point-min))
8535             (setq method (pop methods))
8536             (setq match (car method))
8537             (when (cond
8538                    ((stringp match)
8539                     ;; Regular expression.
8540                     (ignore-errors
8541                       (re-search-forward match nil t)))
8542                    ((gnus-functionp match)
8543                     ;; Function.
8544                     (save-restriction
8545                       (widen)
8546                       (setq result (funcall match gnus-newsgroup-name))))
8547                    ((consp match)
8548                     ;; Form.
8549                     (save-restriction
8550                       (widen)
8551                       (setq result (eval match)))))
8552               (setq split-name (append (cdr method) split-name))
8553               (cond ((stringp result)
8554                      (push (expand-file-name
8555                             result gnus-article-save-directory)
8556                            split-name))
8557                     ((consp result)
8558                      (setq split-name (append result split-name)))))))))
8559     split-name))
8560
8561 (defun gnus-valid-move-group-p (group)
8562   (and (boundp group)
8563        (symbol-name group)
8564        (memq 'respool
8565              (assoc (symbol-name
8566                      (car (gnus-find-method-for-group
8567                            (symbol-name group))))
8568                     gnus-valid-select-methods))))
8569
8570 (defun gnus-read-move-group-name (prompt default articles prefix)
8571   "Read a group name."
8572   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8573          (minibuffer-confirm-incomplete nil) ; XEmacs
8574          (prom
8575           (format "%s %s to:"
8576                   prompt
8577                   (if (> (length articles) 1)
8578                       (format "these %d articles" (length articles))
8579                     "this article")))
8580          (to-newsgroup
8581           (cond
8582            ((null split-name)
8583             (gnus-completing-read default prom
8584                                   gnus-active-hashtb
8585                                   'gnus-valid-move-group-p
8586                                   nil prefix
8587                                   'gnus-group-history))
8588            ((= 1 (length split-name))
8589             (gnus-completing-read (car split-name) prom
8590                                   gnus-active-hashtb
8591                                   'gnus-valid-move-group-p
8592                                   nil nil
8593                                   'gnus-group-history))
8594            (t
8595             (gnus-completing-read nil prom
8596                                   (mapcar (lambda (el) (list el))
8597                                           (nreverse split-name))
8598                                   nil nil nil
8599                                   'gnus-group-history)))))
8600     (when to-newsgroup
8601       (if (or (string= to-newsgroup "")
8602               (string= to-newsgroup prefix))
8603           (setq to-newsgroup default))
8604       (unless to-newsgroup
8605         (error "No group name entered"))
8606       (or (gnus-active to-newsgroup)
8607           (gnus-activate-group to-newsgroup)
8608           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8609                                      to-newsgroup))
8610               (or (and (gnus-request-create-group
8611                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8612                        (gnus-activate-group to-newsgroup nil nil
8613                                             (gnus-group-name-to-method
8614                                              to-newsgroup)))
8615                   (error "Couldn't create group %s" to-newsgroup)))
8616           (error "No such group: %s" to-newsgroup)))
8617     to-newsgroup))
8618
8619 ;; Summary extract commands
8620
8621 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8622   (let ((buffer-read-only nil)
8623         (article (gnus-summary-article-number))
8624         after-article b e)
8625     (unless (gnus-summary-goto-subject article)
8626       (error "No such article: %d" article))
8627     (gnus-summary-position-point)
8628     ;; If all commands are to be bunched up on one line, we collect
8629     ;; them here.
8630     (unless gnus-view-pseudos-separately
8631       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8632             files action)
8633         (while ps
8634           (setq action (cdr (assq 'action (car ps))))
8635           (setq files (list (cdr (assq 'name (car ps)))))
8636           (while (and ps (cdr ps)
8637                       (string= (or action "1")
8638                                (or (cdr (assq 'action (cadr ps))) "2")))
8639             (push (cdr (assq 'name (cadr ps))) files)
8640             (setcdr ps (cddr ps)))
8641           (when files
8642             (when (not (string-match "%s" action))
8643               (push " " files))
8644             (push " " files)
8645             (when (assq 'execute (car ps))
8646               (setcdr (assq 'execute (car ps))
8647                       (funcall (if (string-match "%s" action)
8648                                    'format 'concat)
8649                                action
8650                                (mapconcat
8651                                 (lambda (f)
8652                                   (if (equal f " ")
8653                                       f
8654                                     (gnus-quote-arg-for-sh-or-csh f)))
8655                                 files " ")))))
8656           (setq ps (cdr ps)))))
8657     (if (and gnus-view-pseudos (not not-view))
8658         (while pslist
8659           (when (assq 'execute (car pslist))
8660             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8661                                   (eq gnus-view-pseudos 'not-confirm)))
8662           (setq pslist (cdr pslist)))
8663       (save-excursion
8664         (while pslist
8665           (setq after-article (or (cdr (assq 'article (car pslist)))
8666                                   (gnus-summary-article-number)))
8667           (gnus-summary-goto-subject after-article)
8668           (forward-line 1)
8669           (setq b (point))
8670           (insert "    " (file-name-nondirectory
8671                           (cdr (assq 'name (car pslist))))
8672                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8673           (setq e (point))
8674           (forward-line -1)             ; back to `b'
8675           (gnus-add-text-properties
8676            b (1- e) (list 'gnus-number gnus-reffed-article-number
8677                           gnus-mouse-face-prop gnus-mouse-face))
8678           (gnus-data-enter
8679            after-article gnus-reffed-article-number
8680            gnus-unread-mark b (car pslist) 0 (- e b))
8681           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8682           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8683           (setq pslist (cdr pslist)))))))
8684
8685 (defun gnus-pseudos< (p1 p2)
8686   (let ((c1 (cdr (assq 'action p1)))
8687         (c2 (cdr (assq 'action p2))))
8688     (and c1 c2 (string< c1 c2))))
8689
8690 (defun gnus-request-pseudo-article (props)
8691   (cond ((assq 'execute props)
8692          (gnus-execute-command (cdr (assq 'execute props)))))
8693   (let ((gnus-current-article (gnus-summary-article-number)))
8694     (gnus-run-hooks 'gnus-mark-article-hook)))
8695
8696 (defun gnus-execute-command (command &optional automatic)
8697   (save-excursion
8698     (gnus-article-setup-buffer)
8699     (set-buffer gnus-article-buffer)
8700     (setq buffer-read-only nil)
8701     (let ((command (if automatic command
8702                      (read-string "Command: " (cons command 0)))))
8703       (erase-buffer)
8704       (insert "$ " command "\n\n")
8705       (if gnus-view-pseudo-asynchronously
8706           (start-process "gnus-execute" (current-buffer) shell-file-name
8707                          shell-command-switch command)
8708         (call-process shell-file-name nil t nil
8709                       shell-command-switch command)))))
8710
8711 ;; Summary kill commands.
8712
8713 (defun gnus-summary-edit-global-kill (article)
8714   "Edit the \"global\" kill file."
8715   (interactive (list (gnus-summary-article-number)))
8716   (gnus-group-edit-global-kill article))
8717
8718 (defun gnus-summary-edit-local-kill ()
8719   "Edit a local kill file applied to the current newsgroup."
8720   (interactive)
8721   (setq gnus-current-headers (gnus-summary-article-header))
8722   (gnus-group-edit-local-kill
8723    (gnus-summary-article-number) gnus-newsgroup-name))
8724
8725 ;;; Header reading.
8726
8727 (defun gnus-read-header (id &optional header)
8728   "Read the headers of article ID and enter them into the Gnus system."
8729   (let ((group gnus-newsgroup-name)
8730         (gnus-override-method
8731          (and (gnus-news-group-p gnus-newsgroup-name)
8732               gnus-refer-article-method))
8733         where)
8734     ;; First we check to see whether the header in question is already
8735     ;; fetched.
8736     (if (stringp id)
8737         ;; This is a Message-ID.
8738         (setq header (or header (gnus-id-to-header id)))
8739       ;; This is an article number.
8740       (setq header (or header (gnus-summary-article-header id))))
8741     (if (and header
8742              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8743         ;; We have found the header.
8744         header
8745       ;; If this is a sparse article, we have to nix out its
8746       ;; previous entry in the thread hashtb.
8747       (when (and header
8748                  (gnus-summary-article-sparse-p (mail-header-number header)))
8749         (let* ((parent (gnus-parent-id (mail-header-references header)))
8750                (thread
8751                 (and parent
8752                      (gnus-gethash parent gnus-newsgroup-dependencies))))
8753           (when thread
8754             (delq (assq header thread) thread))))
8755       ;; We have to really fetch the header to this article.
8756       (save-excursion
8757         (set-buffer nntp-server-buffer)
8758         (when (setq where (gnus-request-head id group))
8759           (nnheader-fold-continuation-lines)
8760           (goto-char (point-max))
8761           (insert ".\n")
8762           (goto-char (point-min))
8763           (insert "211 ")
8764           (princ (cond
8765                   ((numberp id) id)
8766                   ((cdr where) (cdr where))
8767                   (header (mail-header-number header))
8768                   (t gnus-reffed-article-number))
8769                  (current-buffer))
8770           (insert " Article retrieved.\n"))
8771         (if (or (not where)
8772                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8773             ()                          ; Malformed head.
8774           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8775             (when (and (stringp id)
8776                        (not (string= (gnus-group-real-name group)
8777                                      (car where))))
8778               ;; If we fetched by Message-ID and the article came
8779               ;; from a different group, we fudge some bogus article
8780               ;; numbers for this article.
8781               (mail-header-set-number header gnus-reffed-article-number))
8782             (save-excursion
8783               (set-buffer gnus-summary-buffer)
8784               (decf gnus-reffed-article-number)
8785               (gnus-remove-header (mail-header-number header))
8786               (push header gnus-newsgroup-headers)
8787               (setq gnus-current-headers header)
8788               (push (mail-header-number header) gnus-newsgroup-limit)))
8789           header)))))
8790
8791 (defun gnus-remove-header (number)
8792   "Remove header NUMBER from `gnus-newsgroup-headers'."
8793   (if (and gnus-newsgroup-headers
8794            (= number (mail-header-number (car gnus-newsgroup-headers))))
8795       (pop gnus-newsgroup-headers)
8796     (let ((headers gnus-newsgroup-headers))
8797       (while (and (cdr headers)
8798                   (not (= number (mail-header-number (cadr headers)))))
8799         (pop headers))
8800       (when (cdr headers)
8801         (setcdr headers (cddr headers))))))
8802
8803 ;;;
8804 ;;; summary highlights
8805 ;;;
8806
8807 (defun gnus-highlight-selected-summary ()
8808   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8809   ;; Highlight selected article in summary buffer
8810   (when gnus-summary-selected-face
8811     (save-excursion
8812       (let* ((beg (progn (beginning-of-line) (point)))
8813              (end (progn (end-of-line) (point)))
8814              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8815              (from (if (get-text-property beg gnus-mouse-face-prop)
8816                        beg
8817                      (or (next-single-property-change
8818                           beg gnus-mouse-face-prop nil end)
8819                          beg)))
8820              (to
8821               (if (= from end)
8822                   (- from 2)
8823                 (or (next-single-property-change
8824                      from gnus-mouse-face-prop nil end)
8825                     end))))
8826         ;; If no mouse-face prop on line we will have to = from = end,
8827         ;; so we highlight the entire line instead.
8828         (when (= (+ to 2) from)
8829           (setq from beg)
8830           (setq to end))
8831         (if gnus-newsgroup-selected-overlay
8832             ;; Move old overlay.
8833             (gnus-move-overlay
8834              gnus-newsgroup-selected-overlay from to (current-buffer))
8835           ;; Create new overlay.
8836           (gnus-overlay-put
8837            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8838            'face gnus-summary-selected-face))))))
8839
8840 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8841 (defun gnus-summary-highlight-line ()
8842   "Highlight current line according to `gnus-summary-highlight'."
8843   (let* ((list gnus-summary-highlight)
8844          (p (point))
8845          (end (progn (end-of-line) (point)))
8846          ;; now find out where the line starts and leave point there.
8847          (beg (progn (beginning-of-line) (point)))
8848          (article (gnus-summary-article-number))
8849          (score (or (cdr (assq (or article gnus-current-article)
8850                                gnus-newsgroup-scored))
8851                     gnus-summary-default-score 0))
8852          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8853          (inhibit-read-only t))
8854     ;; Eval the cars of the lists until we find a match.
8855     (let ((default gnus-summary-default-score))
8856       (while (and list
8857                   (not (eval (caar list))))
8858         (setq list (cdr list))))
8859     (let ((face (cdar list)))
8860       (unless (eq face (get-text-property beg 'face))
8861         (gnus-put-text-property
8862          beg end 'face
8863          (setq face (if (boundp face) (symbol-value face) face)))
8864         (when gnus-summary-highlight-line-function
8865           (funcall gnus-summary-highlight-line-function article face))))
8866     (goto-char p)))
8867
8868 (defun gnus-update-read-articles (group unread &optional compute)
8869   "Update the list of read articles in GROUP."
8870   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8871          (entry (gnus-gethash group gnus-newsrc-hashtb))
8872          (info (nth 2 entry))
8873          (prev 1)
8874          (unread (sort (copy-sequence unread) '<))
8875          read)
8876     (if (or (not info) (not active))
8877         ;; There is no info on this group if it was, in fact,
8878         ;; killed.  Gnus stores no information on killed groups, so
8879         ;; there's nothing to be done.
8880         ;; One could store the information somewhere temporarily,
8881         ;; perhaps...  Hmmm...
8882         ()
8883       ;; Remove any negative articles numbers.
8884       (while (and unread (< (car unread) 0))
8885         (setq unread (cdr unread)))
8886       ;; Remove any expired article numbers
8887       (while (and unread (< (car unread) (car active)))
8888         (setq unread (cdr unread)))
8889       ;; Compute the ranges of read articles by looking at the list of
8890       ;; unread articles.
8891       (while unread
8892         (when (/= (car unread) prev)
8893           (push (if (= prev (1- (car unread))) prev
8894                   (cons prev (1- (car unread))))
8895                 read))
8896         (setq prev (1+ (car unread)))
8897         (setq unread (cdr unread)))
8898       (when (<= prev (cdr active))
8899         (push (cons prev (cdr active)) read))
8900       (if compute
8901           (if (> (length read) 1) (nreverse read) read)
8902         (save-excursion
8903           (set-buffer gnus-group-buffer)
8904           (gnus-undo-register
8905             `(progn
8906                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8907                (gnus-info-set-read ',info ',(gnus-info-read info))
8908                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8909                (gnus-group-update-group ,group t))))
8910         ;; Enter this list into the group info.
8911         (gnus-info-set-read
8912          info (if (> (length read) 1) (nreverse read) read))
8913         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8914         (gnus-get-unread-articles-in-group info (gnus-active group))
8915         t))))
8916
8917 (defun gnus-offer-save-summaries ()
8918   "Offer to save all active summary buffers."
8919   (save-excursion
8920     (let ((buflist (buffer-list))
8921           buffers bufname)
8922       ;; Go through all buffers and find all summaries.
8923       (while buflist
8924         (and (setq bufname (buffer-name (car buflist)))
8925              (string-match "Summary" bufname)
8926              (save-excursion
8927                (set-buffer bufname)
8928                ;; We check that this is, indeed, a summary buffer.
8929                (and (eq major-mode 'gnus-summary-mode)
8930                     ;; Also make sure this isn't bogus.
8931                     gnus-newsgroup-prepared
8932                     ;; Also make sure that this isn't a dead summary buffer.
8933                     (not gnus-dead-summary-mode)))
8934              (push bufname buffers))
8935         (setq buflist (cdr buflist)))
8936       ;; Go through all these summary buffers and offer to save them.
8937       (when buffers
8938         (map-y-or-n-p
8939          "Update summary buffer %s? "
8940          (lambda (buf)
8941            (switch-to-buffer buf)
8942            (gnus-summary-exit))
8943          buffers)))))
8944
8945
8946 ;;; @ for mime-partial
8947 ;;;
8948
8949 (defun gnus-mime-partial-preview-function ()
8950   (gnus-summary-preview-mime-message (gnus-summary-article-number))
8951   )
8952
8953 (autoload 'mime-method-to-combine-message/partial-pieces
8954   "mime-partial"
8955   "Internal method to combine message/partial messages automatically.")
8956
8957 (ctree-set-calist-strictly
8958  'mime-acting-condition
8959  '((type . message)(subtype . partial)
8960    (method . mime-method-to-combine-message/partial-pieces)
8961    (major-mode . gnus-original-article-mode)
8962    (summary-buffer-exp . gnus-summary-buffer)
8963    ))
8964
8965 (set-alist 'mime-view-partial-message-method-alist
8966            'gnus-original-article-mode
8967            'gnus-mime-partial-preview-function)
8968
8969
8970 ;;; @ end
8971 ;;;
8972
8973 (gnus-ems-redefine)
8974
8975 (provide 'gnus-sum)
8976
8977 (run-hooks 'gnus-sum-load-hook)
8978
8979 ;;; gnus-sum.el ends here