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