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