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