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