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