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