*** empty log message ***
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-group)
32 (require 'gnus-spec)
33 (require 'gnus-range)
34 (require 'gnus-int)
35 (require 'gnus-undo)
36 (require 'gnus-util)
37 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
38
39 (defcustom gnus-kill-summary-on-exit t
40   "*If non-nil, kill the summary buffer when you exit from it.
41 If nil, the summary will become a \"*Dead Summary*\" buffer, and
42 it will be killed sometime later."
43   :group 'gnus-summary-exit
44   :type 'boolean)
45
46 (defcustom gnus-fetch-old-headers nil
47   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
48 If an unread article in the group refers to an older, already read (or
49 just marked as read) article, the old article will not normally be
50 displayed in the Summary buffer.  If this variable is non-nil, Gnus
51 will attempt to grab the headers to the old articles, and thereby
52 build complete threads.  If it has the value `some', only enough
53 headers to connect otherwise loose threads will be displayed.  This
54 variable can also be a number.  In that case, no more than that number
55 of old headers will be fetched.  If it has the value `invisible', all
56 old headers will be fetched, but none will be displayed.
57
58 The server has to support NOV for any of this to work."
59   :group 'gnus-thread
60   :type '(choice (const :tag "off" nil)
61                  (const some)
62                  number
63                  (sexp :menu-tag "other" t)))
64
65 (defcustom gnus-refer-thread-limit 200
66   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
67 If t, fetch all the available old headers."
68   :group 'gnus-thread
69   :type '(choice number
70                  (sexp :menu-tag "other" t)))
71
72 (defcustom gnus-summary-make-false-root 'adopt
73   "*nil means that Gnus won't gather loose threads.
74 If the root of a thread has expired or been read in a previous
75 session, the information necessary to build a complete thread has been
76 lost.  Instead of having many small sub-threads from this original thread
77 scattered all over the summary buffer, Gnus can gather them.
78
79 If non-nil, Gnus will try to gather all loose sub-threads from an
80 original thread into one large thread.
81
82 If this variable is non-nil, it should be one of `none', `adopt',
83 `dummy' or `empty'.
84
85 If this variable is `none', Gnus will not make a false root, but just
86 present the sub-threads after another.
87 If this variable is `dummy', Gnus will create a dummy root that will
88 have all the sub-threads as children.
89 If this variable is `adopt', Gnus will make one of the \"children\"
90 the parent and mark all the step-children as such.
91 If this variable is `empty', the \"children\" are printed with empty
92 subject fields.  (Or rather, they will be printed with a string
93 given by the `gnus-summary-same-subject' variable.)"
94   :group 'gnus-thread
95   :type '(choice (const :tag "off" nil)
96                  (const none)
97                  (const dummy)
98                  (const adopt)
99                  (const empty)))
100
101 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
102   "*A regexp to match subjects to be excluded from loose thread gathering.
103 As loose thread gathering is done on subjects only, that means that
104 there can be many false gatherings performed.  By rooting out certain
105 common subjects, gathering might become saner."
106   :group 'gnus-thread
107   :type 'regexp)
108
109 (defcustom gnus-summary-gather-subject-limit nil
110   "*Maximum length of subject comparisons when gathering loose threads.
111 Use nil to compare full subjects.  Setting this variable to a low
112 number will help gather threads that have been corrupted by
113 newsreaders chopping off subject lines, but it might also mean that
114 unrelated articles that have subject that happen to begin with the
115 same few characters will be incorrectly gathered.
116
117 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
118 comparing subjects."
119   :group 'gnus-thread
120   :type '(choice (const :tag "off" nil)
121                  (const fuzzy)
122                  (sexp :menu-tag "on" t)))
123
124 (defcustom gnus-simplify-subject-functions nil
125   "List of functions taking a string argument that simplify subjects.
126 The functions are applied recursively.
127
128 Useful functions to put in this list include: `gnus-simplify-subject-re',
129 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
130   :group 'gnus-thread
131   :type '(repeat function))
132
133 (defcustom gnus-simplify-ignored-prefixes nil
134   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
135   :group 'gnus-thread
136   :type '(choice (const :tag "off" nil)
137                  regexp))
138
139 (defcustom gnus-build-sparse-threads nil
140   "*If non-nil, fill in the gaps in threads.
141 If `some', only fill in the gaps that are needed to tie loose threads
142 together.  If `more', fill in all leaf nodes that Gnus can find.  If
143 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  (const some)
147                  (const more)
148                  (sexp :menu-tag "all" t)))
149
150 (defcustom gnus-summary-thread-gathering-function
151   'gnus-gather-threads-by-subject
152   "*Function used for gathering loose threads.
153 There are two pre-defined functions: `gnus-gather-threads-by-subject',
154 which only takes Subjects into consideration; and
155 `gnus-gather-threads-by-references', which compared the References
156 headers of the articles to find matches."
157   :group 'gnus-thread
158   :type '(radio (function-item gnus-gather-threads-by-subject)
159                 (function-item gnus-gather-threads-by-references)
160                 (function :tag "other")))
161
162 (defcustom gnus-summary-same-subject ""
163   "*String indicating that the current article has the same subject as the previous.
164 This variable will only be used if the value of
165 `gnus-summary-make-false-root' is `empty'."
166   :group 'gnus-summary-format
167   :type 'string)
168
169 (defcustom gnus-summary-goto-unread t
170   "*If t, marking commands will go to the next unread article.
171 If `never', commands that usually go to the next unread article, will
172 go to the next article, whether it is read or not.
173 If nil, only the marking commands will go to the next (un)read article."
174   :group 'gnus-summary-marks
175   :link '(custom-manual "(gnus)Setting Marks")
176   :type '(choice (const :tag "off" nil)
177                  (const never)
178                  (sexp :menu-tag "on" t)))
179
180 (defcustom gnus-summary-default-score 0
181   "*Default article score level.
182 All scores generated by the score files will be added to this score.
183 If this variable is nil, scoring will be disabled."
184   :group 'gnus-score-default
185   :type '(choice (const :tag "disable")
186                  integer))
187
188 (defcustom gnus-summary-zcore-fuzz 0
189   "*Fuzziness factor for the zcore in the summary buffer.
190 Articles with scores closer than this to `gnus-summary-default-score'
191 will not be marked."
192   :group 'gnus-summary-format
193   :type 'integer)
194
195 (defcustom gnus-simplify-subject-fuzzy-regexp nil
196   "*Strings to be removed when doing fuzzy matches.
197 This can either be a regular expression or list of regular expressions
198 that will be removed from subject strings if fuzzy subject
199 simplification is selected."
200   :group 'gnus-thread
201   :type '(repeat regexp))
202
203 (defcustom gnus-show-threads t
204   "*If non-nil, display threads in summary mode."
205   :group 'gnus-thread
206   :type 'boolean)
207
208 (defcustom gnus-thread-hide-subtree nil
209   "*If non-nil, hide all threads initially.
210 If threads are hidden, you have to run the command
211 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
212 to expose hidden threads."
213   :group 'gnus-thread
214   :type 'boolean)
215
216 (defcustom gnus-thread-hide-killed t
217   "*If non-nil, hide killed threads automatically."
218   :group 'gnus-thread
219   :type 'boolean)
220
221 (defcustom gnus-thread-ignore-subject nil
222   "*If non-nil, ignore subjects and do all threading based on the Reference header.
223 If nil, which is the default, articles that have different subjects
224 from their parents will start separate threads."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-operation-ignore-subject t
229   "*If non-nil, subjects will be ignored when doing thread commands.
230 This affects commands like `gnus-summary-kill-thread' and
231 `gnus-summary-lower-thread'.
232
233 If this variable is nil, articles in the same thread with different
234 subjects will not be included in the operation in question.  If this
235 variable is `fuzzy', only articles that have subjects that are fuzzily
236 equal will be included."
237   :group 'gnus-thread
238   :type '(choice (const :tag "off" nil)
239                  (const fuzzy)
240                  (sexp :tag "on" t)))
241
242 (defcustom gnus-thread-indent-level 4
243   "*Number that says how much each sub-thread should be indented."
244   :group 'gnus-thread
245   :type 'integer)
246
247 (defcustom gnus-auto-extend-newsgroup t
248   "*If non-nil, extend newsgroup forward and backward when requested."
249   :group 'gnus-summary-choose
250   :type 'boolean)
251
252 (defcustom gnus-auto-select-first t
253   "*If nil, don't select the first unread article when entering a group.
254 If this variable is `best', select the highest-scored unread article
255 in the group.  If neither nil nor `best', select the first unread
256 article.
257
258 If you want to prevent automatic selection of the first unread article
259 in some newsgroups, set the variable to nil in
260 `gnus-select-group-hook'."
261   :group 'gnus-group-select
262   :type '(choice (const :tag "none" nil)
263                  (const best)
264                  (sexp :menu-tag "first" t)))
265
266 (defcustom gnus-auto-select-next t
267   "*If non-nil, offer to go to the next group from the end of the previous.
268 If the value is t and the next newsgroup is empty, Gnus will exit
269 summary mode and go back to group mode.  If the value is neither nil
270 nor t, Gnus will select the following unread newsgroup.  In
271 particular, if the value is the symbol `quietly', the next unread
272 newsgroup will be selected without any confirmation, and if it is
273 `almost-quietly', the next group will be selected without any
274 confirmation if you are located on the last article in the group.
275 Finally, if this variable is `slightly-quietly', the `Z n' command
276 will go to the next group without confirmation."
277   :group 'gnus-summary-maneuvering
278   :type '(choice (const :tag "off" nil)
279                  (const quietly)
280                  (const almost-quietly)
281                  (const slightly-quietly)
282                  (sexp :menu-tag "on" t)))
283
284 (defcustom gnus-auto-select-same nil
285   "*If non-nil, select the next article with the same subject."
286   :group 'gnus-summary-maneuvering
287   :type 'boolean)
288
289 (defcustom gnus-summary-check-current nil
290   "*If non-nil, consider the current article when moving.
291 The \"unread\" movement commands will stay on the same line if the
292 current article is unread."
293   :group 'gnus-summary-maneuvering
294   :type 'boolean)
295
296 (defcustom gnus-auto-center-summary t
297   "*If non-nil, always center the current summary buffer.
298 In particular, if `vertical' do only vertical recentering.  If non-nil
299 and non-`vertical', do both horizontal and vertical recentering."
300   :group 'gnus-summary-maneuvering
301   :type '(choice (const :tag "none" nil)
302                  (const vertical)
303                  (sexp :menu-tag "both" t)))
304
305 (defcustom gnus-show-all-headers nil
306   "*If non-nil, don't hide any headers."
307   :group 'gnus-article-hiding
308   :group 'gnus-article-headers
309   :type 'boolean)
310
311 (defcustom gnus-summary-ignore-duplicates nil
312   "*If non-nil, ignore articles with identical Message-ID headers."
313   :group 'gnus-summary
314   :type 'boolean)
315   
316 (defcustom gnus-single-article-buffer t
317   "*If non-nil, display all articles in the same buffer.
318 If nil, each group will get its own article buffer."
319   :group 'gnus-article-various
320   :type 'boolean)
321
322 (defcustom gnus-break-pages t
323   "*If non-nil, do page breaking on articles.
324 The page delimiter is specified by the `gnus-page-delimiter'
325 variable."
326   :group 'gnus-article-various
327   :type 'boolean)
328
329 (defcustom gnus-show-mime nil
330   "*If non-nil, do mime processing of articles.
331 The articles will simply be fed to the function given by
332 `gnus-show-mime-method'."
333   :group 'gnus-article-mime
334   :type 'boolean)
335
336 (defcustom gnus-move-split-methods nil
337   "*Variable used to suggest where articles are to be moved to.
338 It uses the same syntax as the `gnus-split-methods' variable."
339   :group 'gnus-summary-mail
340   :type '(repeat (choice (list :value (fun) function)
341                          (cons :value ("" "") regexp (repeat string))
342                          (sexp :value nil))))
343
344 (defcustom gnus-unread-mark ? 
345   "*Mark used for unread articles."
346   :group 'gnus-summary-marks
347   :type 'character)
348
349 (defcustom gnus-ticked-mark ?!
350   "*Mark used for ticked articles."
351   :group 'gnus-summary-marks
352   :type 'character)
353
354 (defcustom gnus-dormant-mark ??
355   "*Mark used for dormant articles."
356   :group 'gnus-summary-marks
357   :type 'character)
358
359 (defcustom gnus-del-mark ?r
360   "*Mark used for del'd articles."
361   :group 'gnus-summary-marks
362   :type 'character)
363
364 (defcustom gnus-read-mark ?R
365   "*Mark used for read articles."
366   :group 'gnus-summary-marks
367   :type 'character)
368
369 (defcustom gnus-expirable-mark ?E
370   "*Mark used for expirable articles."
371   :group 'gnus-summary-marks
372   :type 'character)
373
374 (defcustom gnus-killed-mark ?K
375   "*Mark used for killed articles."
376   :group 'gnus-summary-marks
377   :type 'character)
378
379 (defcustom gnus-souped-mark ?F
380   "*Mark used for killed articles."
381   :group 'gnus-summary-marks
382   :type 'character)
383
384 (defcustom gnus-kill-file-mark ?X
385   "*Mark used for articles killed by kill files."
386   :group 'gnus-summary-marks
387   :type 'character)
388
389 (defcustom gnus-low-score-mark ?Y
390   "*Mark used for articles with a low score."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-catchup-mark ?C
395   "*Mark used for articles that are caught up."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-replied-mark ?A
400   "*Mark used for articles that have been replied to."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-cached-mark ?*
405   "*Mark used for articles that are in the cache."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-saved-mark ?S
410   "*Mark used for articles that have been saved to."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-ancient-mark ?O
415   "*Mark used for ancient articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-sparse-mark ?Q
420   "*Mark used for sparsely reffed articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-canceled-mark ?G
425   "*Mark used for canceled articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-duplicate-mark ?M
430   "*Mark used for duplicate articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-undownloaded-mark ?@
435   "*Mark used for articles that weren't downloaded."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-downloadable-mark ?%
440   "*Mark used for articles that are to be downloaded."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-unsendable-mark ?=
445   "*Mark used for articles that won't be sent."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-score-over-mark ?+
450   "*Score mark used for articles with high scores."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-score-below-mark ?-
455   "*Score mark used for articles with low scores."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-empty-thread-mark ? 
460   "*There is no thread under the article."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-not-empty-thread-mark ?=
465   "*There is a thread under the article."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-view-pseudo-asynchronously nil
470   "*If non-nil, Gnus will view pseudo-articles asynchronously."
471   :group 'gnus-extract-view
472   :type 'boolean)
473
474 (defcustom gnus-view-pseudos nil
475   "*If `automatic', pseudo-articles will be viewed automatically.
476 If `not-confirm', pseudos will be viewed automatically, and the user
477 will not be asked to confirm the command."
478   :group 'gnus-extract-view
479   :type '(choice (const :tag "off" nil)
480                  (const automatic)
481                  (const not-confirm)))
482
483 (defcustom gnus-view-pseudos-separately t
484   "*If non-nil, one pseudo-article will be created for each file to be viewed.
485 If nil, all files that use the same viewing command will be given as a
486 list of parameters to that command."
487   :group 'gnus-extract-view
488   :type 'boolean)
489
490 (defcustom gnus-insert-pseudo-articles t
491   "*If non-nil, insert pseudo-articles when decoding articles."
492   :group 'gnus-extract-view
493   :type 'boolean)
494
495 (defcustom gnus-summary-dummy-line-format
496   "  %(:                          :%) %S\n"
497   "*The format specification for the dummy roots in the summary buffer.
498 It works along the same lines as a normal formatting string,
499 with some simple extensions.
500
501 %S  The subject"
502   :group 'gnus-threading
503   :type 'string)
504
505 (defcustom gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
506   "*The format specification for the summary mode line.
507 It works along the same lines as a normal formatting string,
508 with some simple extensions:
509
510 %G  Group name
511 %p  Unprefixed group name
512 %A  Current article number
513 %z  Current article score
514 %V  Gnus version
515 %U  Number of unread articles in the group
516 %e  Number of unselected articles in the group
517 %Z  A string with unread/unselected article counts
518 %g  Shortish group name
519 %S  Subject of the current article
520 %u  User-defined spec
521 %s  Current score file name
522 %d  Number of dormant articles
523 %r  Number of articles that have been marked as read in this session
524 %E  Number of articles expunged by the score files"
525   :group 'gnus-summary-format
526   :type 'string)
527
528 (defcustom gnus-summary-mark-below 0
529   "*Mark all articles with a score below this variable as read.
530 This variable is local to each summary buffer and usually set by the
531 score file."
532   :group 'gnus-score-default
533   :type 'integer)
534
535 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
536   "*List of functions used for sorting articles in the summary buffer.
537 This variable is only used when not using a threaded display."
538   :group 'gnus-summary-sort
539   :type '(repeat (choice (function-item gnus-article-sort-by-number)
540                          (function-item gnus-article-sort-by-author)
541                          (function-item gnus-article-sort-by-subject)
542                          (function-item gnus-article-sort-by-date)
543                          (function-item gnus-article-sort-by-score)
544                          (function :tag "other"))))
545
546 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
547   "*List of functions used for sorting threads in the summary buffer.
548 By default, threads are sorted by article number.
549
550 Each function takes two threads and return non-nil if the first thread
551 should be sorted before the other.  If you use more than one function,
552 the primary sort function should be the last.  You should probably
553 always include `gnus-thread-sort-by-number' in the list of sorting
554 functions -- preferably first.
555
556 Ready-made functions include `gnus-thread-sort-by-number',
557 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
558 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
559 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
560   :group 'gnus-summary-sort
561   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
562                          (function-item gnus-thread-sort-by-author)
563                          (function-item gnus-thread-sort-by-subject)
564                          (function-item gnus-thread-sort-by-date)
565                          (function-item gnus-thread-sort-by-score)
566                          (function-item gnus-thread-sort-by-total-score)
567                          (function :tag "other"))))
568
569 (defcustom gnus-thread-score-function '+
570   "*Function used for calculating the total score of a thread.
571
572 The function is called with the scores of the article and each
573 subthread and should then return the score of the thread.
574
575 Some functions you can use are `+', `max', or `min'."
576   :group 'gnus-summary-sort
577   :type 'function)
578
579 (defcustom gnus-summary-expunge-below nil
580   "All articles that have a score less than this variable will be expunged.
581 This variable is local to the summary buffers."
582   :group 'gnus-score-default
583   :type '(choice (const :tag "off" nil)
584                  integer))
585
586 (defcustom gnus-thread-expunge-below nil
587   "All threads that have a total score less than this variable will be expunged.
588 See `gnus-thread-score-function' for en explanation of what a
589 \"thread score\" is.
590
591 This variable is local to the summary buffers."
592   :group 'gnus-treading
593   :group 'gnus-score-default
594   :type '(choice (const :tag "off" nil)
595                  integer))
596
597 (defcustom gnus-summary-mode-hook nil
598   "*A hook for Gnus summary mode.
599 This hook is run before any variables are set in the summary buffer."
600   :group 'gnus-summary-various
601   :type 'hook)
602
603 (defcustom gnus-summary-menu-hook nil
604   "*Hook run after the creation of the summary mode menu."
605   :group 'gnus-summary-visual
606   :type 'hook)
607
608 (defcustom gnus-summary-exit-hook nil
609   "*A hook called on exit from the summary buffer.
610 It will be called with point in the group buffer."
611   :group 'gnus-summary-exit
612   :type 'hook)
613
614 (defcustom gnus-summary-prepare-hook nil
615   "*A hook called after the summary buffer has been generated.
616 If you want to modify the summary buffer, you can use this hook."
617   :group 'gnus-summary-various
618   :type 'hook)
619
620 (defcustom gnus-summary-prepared-hook nil
621   "*A hook called as the last thing after the summary buffer has been generated."
622   :group 'gnus-summary-various
623   :type 'hook)
624
625 (defcustom gnus-summary-generate-hook nil
626   "*A hook run just before generating the summary buffer.
627 This hook is commonly used to customize threading variables and the
628 like."
629   :group 'gnus-summary-various
630   :type 'hook)
631
632 (defcustom gnus-select-group-hook nil
633   "*A hook called when a newsgroup is selected.
634
635 If you'd like to simplify subjects like the
636 `gnus-summary-next-same-subject' command does, you can use the
637 following hook:
638
639  (setq gnus-select-group-hook
640       (list
641         (lambda ()
642           (mapcar (lambda (header)
643                      (mail-header-set-subject
644                       header
645                       (gnus-simplify-subject
646                        (mail-header-subject header) 're-only)))
647                   gnus-newsgroup-headers))))"
648   :group 'gnus-group-select
649   :type 'hook)
650
651 (defcustom gnus-select-article-hook nil
652   "*A hook called when an article is selected."
653   :group 'gnus-summary-choose
654   :type 'hook)
655
656 (defcustom gnus-visual-mark-article-hook
657   (list 'gnus-highlight-selected-summary)
658   "*Hook run after selecting an article in the summary buffer.
659 It is meant to be used for highlighting the article in some way.  It
660 is not run if `gnus-visual' is nil."
661   :group 'gnus-summary-visual
662   :type 'hook)
663
664 (defcustom gnus-structured-field-decoder 'identity
665   "Function to decode non-ASCII characters in structured field for summary."
666   :group 'gnus-various
667   :type 'function)
668
669 (defcustom gnus-unstructured-field-decoder 'identity
670   "Function to decode non-ASCII characters in unstructured field for summary."
671   :group 'gnus-various
672   :type 'function)
673
674 (defcustom gnus-parse-headers-hook
675   (list 'gnus-hack-decode-rfc1522 'gnus-decode-rfc1522)
676   "*A hook called before parsing the headers."
677   :group 'gnus-various
678   :type 'hook)
679
680 (defcustom gnus-exit-group-hook nil
681   "*A hook called when exiting (not quitting) summary mode."
682   :group 'gnus-various
683   :type 'hook)
684
685 (defcustom gnus-summary-update-hook
686   (list 'gnus-summary-highlight-line)
687   "*A hook called when a summary line is changed.
688 The hook will not be called if `gnus-visual' is nil.
689
690 The default function `gnus-summary-highlight-line' will
691 highlight the line according to the `gnus-summary-highlight'
692 variable."
693   :group 'gnus-summary-visual
694   :type 'hook)
695
696 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
697   "*A hook called when an article is selected for the first time.
698 The hook is intended to mark an article as read (or unread)
699 automatically when it is selected."
700   :group 'gnus-summary-choose
701   :type 'hook)
702
703 (defcustom gnus-group-no-more-groups-hook nil
704   "*A hook run when returning to group mode having no more (unread) groups."
705   :group 'gnus-group-select
706   :type 'hook)
707
708 (defcustom gnus-ps-print-hook nil
709   "*A hook run before ps-printing something from Gnus."
710   :group 'gnus-summary
711   :type 'hook)
712
713 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
714   "Face used for highlighting the current article in the summary buffer."
715   :group 'gnus-summary-visual
716   :type 'face)
717
718 (defcustom gnus-summary-highlight
719   '(((= mark gnus-canceled-mark)
720      . gnus-summary-cancelled-face)
721     ((and (> score default)
722           (or (= mark gnus-dormant-mark)
723               (= mark gnus-ticked-mark)))
724      . gnus-summary-high-ticked-face)
725     ((and (< score default)
726           (or (= mark gnus-dormant-mark)
727               (= mark gnus-ticked-mark)))
728      . gnus-summary-low-ticked-face)
729     ((or (= mark gnus-dormant-mark)
730          (= mark gnus-ticked-mark))
731      . gnus-summary-normal-ticked-face)
732     ((and (> score default) (= mark gnus-ancient-mark))
733      . gnus-summary-high-ancient-face)
734     ((and (< score default) (= mark gnus-ancient-mark))
735      . gnus-summary-low-ancient-face)
736     ((= mark gnus-ancient-mark)
737      . gnus-summary-normal-ancient-face)
738     ((and (> score default) (= mark gnus-unread-mark))
739      . gnus-summary-high-unread-face)
740     ((and (< score default) (= mark gnus-unread-mark))
741      . gnus-summary-low-unread-face)
742     ((= mark gnus-unread-mark)
743      . gnus-summary-normal-unread-face)
744     ((and (> score default) (memq mark (list gnus-downloadable-mark
745                                              gnus-undownloaded-mark)))
746      . gnus-summary-high-unread-face)
747     ((and (< score default) (memq mark (list gnus-downloadable-mark
748                                              gnus-undownloaded-mark)))
749      . gnus-summary-low-unread-face)
750     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
751      . gnus-summary-normal-unread-face)
752     ((> score default)
753      . gnus-summary-high-read-face)
754     ((< score default)
755      . gnus-summary-low-read-face)
756     (t
757      . gnus-summary-normal-read-face))
758   "*Controls the highlighting of summary buffer lines.
759
760 A list of (FORM . FACE) pairs.  When deciding how a a particular
761 summary line should be displayed, each form is evaluated.  The content
762 of the face field after the first true form is used.  You can change
763 how those summary lines are displayed, by editing the face field.
764
765 You can use the following variables in the FORM field.
766
767 score:   The articles score
768 default: The default article score.
769 below:   The score below which articles are automatically marked as read.
770 mark:    The articles mark."
771   :group 'gnus-summary-visual
772   :type '(repeat (cons (sexp :tag "Form" nil)
773                        face)))
774
775 (defcustom gnus-alter-header-function nil
776   "Function called to allow alteration of article header structures.
777 The function is called with one parameter, the article header vector,
778 which it may alter in any way.")
779
780 ;;; Internal variables
781
782 (defvar gnus-scores-exclude-files nil)
783 (defvar gnus-page-broken nil)
784
785 (defvar gnus-original-article nil)
786 (defvar gnus-article-internal-prepare-hook nil)
787 (defvar gnus-newsgroup-process-stack nil)
788
789 (defvar gnus-thread-indent-array nil)
790 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
791
792 ;; Avoid highlighting in kill files.
793 (defvar gnus-summary-inhibit-highlight nil)
794 (defvar gnus-newsgroup-selected-overlay nil)
795 (defvar gnus-inhibit-limiting nil)
796 (defvar gnus-newsgroup-adaptive-score-file nil)
797 (defvar gnus-current-score-file nil)
798 (defvar gnus-current-move-group nil)
799 (defvar gnus-current-copy-group nil)
800 (defvar gnus-current-crosspost-group nil)
801
802 (defvar gnus-newsgroup-dependencies nil)
803 (defvar gnus-newsgroup-adaptive nil)
804 (defvar gnus-summary-display-article-function nil)
805 (defvar gnus-summary-highlight-line-function nil
806   "Function called after highlighting a summary line.")
807
808 (defvar gnus-summary-line-format-alist
809   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
810     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
811     (?s gnus-tmp-subject-or-nil ?s)
812     (?n gnus-tmp-name ?s)
813     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
814         ?s)
815     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
816             gnus-tmp-from) ?s)
817     (?F gnus-tmp-from ?s)
818     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
819     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
820     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
821     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
822     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
823     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
824     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
825     (?L gnus-tmp-lines ?d)
826     (?I gnus-tmp-indentation ?s)
827     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
828     (?R gnus-tmp-replied ?c)
829     (?\[ gnus-tmp-opening-bracket ?c)
830     (?\] gnus-tmp-closing-bracket ?c)
831     (?\> (make-string gnus-tmp-level ? ) ?s)
832     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
833     (?i gnus-tmp-score ?d)
834     (?z gnus-tmp-score-char ?c)
835     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
836     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
837     (?U gnus-tmp-unread ?c)
838     (?t (gnus-summary-number-of-articles-in-thread
839          (and (boundp 'thread) (car thread)) gnus-tmp-level)
840         ?d)
841     (?e (gnus-summary-number-of-articles-in-thread
842          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
843         ?c)
844     (?u gnus-tmp-user-defined ?s)
845     (?P (gnus-pick-line-number) ?d))
846   "An alist of format specifications that can appear in summary lines,
847 and what variables they correspond with, along with the type of the
848 variable (string, integer, character, etc).")
849
850 (defvar gnus-summary-dummy-line-format-alist
851   `((?S gnus-tmp-subject ?s)
852     (?N gnus-tmp-number ?d)
853     (?u gnus-tmp-user-defined ?s)))
854
855 (defvar gnus-summary-mode-line-format-alist
856   `((?G gnus-tmp-group-name ?s)
857     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
858     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
859     (?A gnus-tmp-article-number ?d)
860     (?Z gnus-tmp-unread-and-unselected ?s)
861     (?V gnus-version ?s)
862     (?U gnus-tmp-unread-and-unticked ?d)
863     (?S gnus-tmp-subject ?s)
864     (?e gnus-tmp-unselected ?d)
865     (?u gnus-tmp-user-defined ?s)
866     (?d (length gnus-newsgroup-dormant) ?d)
867     (?t (length gnus-newsgroup-marked) ?d)
868     (?r (length gnus-newsgroup-reads) ?d)
869     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
870     (?E gnus-newsgroup-expunged-tally ?d)
871     (?s (gnus-current-score-file-nondirectory) ?s)))
872
873 (defvar gnus-last-search-regexp nil
874   "Default regexp for article search command.")
875
876 (defvar gnus-last-shell-command nil
877   "Default shell command on article.")
878
879 (defvar gnus-newsgroup-begin nil)
880 (defvar gnus-newsgroup-end nil)
881 (defvar gnus-newsgroup-last-rmail nil)
882 (defvar gnus-newsgroup-last-mail nil)
883 (defvar gnus-newsgroup-last-folder nil)
884 (defvar gnus-newsgroup-last-file nil)
885 (defvar gnus-newsgroup-auto-expire nil)
886 (defvar gnus-newsgroup-active nil)
887
888 (defvar gnus-newsgroup-data nil)
889 (defvar gnus-newsgroup-data-reverse nil)
890 (defvar gnus-newsgroup-limit nil)
891 (defvar gnus-newsgroup-limits nil)
892
893 (defvar gnus-newsgroup-unreads nil
894   "List of unread articles in the current newsgroup.")
895
896 (defvar gnus-newsgroup-unselected nil
897   "List of unselected unread articles in the current newsgroup.")
898
899 (defvar gnus-newsgroup-reads nil
900   "Alist of read articles and article marks in the current newsgroup.")
901
902 (defvar gnus-newsgroup-expunged-tally nil)
903
904 (defvar gnus-newsgroup-marked nil
905   "List of ticked articles in the current newsgroup (a subset of unread art).")
906
907 (defvar gnus-newsgroup-killed nil
908   "List of ranges of articles that have been through the scoring process.")
909
910 (defvar gnus-newsgroup-cached nil
911   "List of articles that come from the article cache.")
912
913 (defvar gnus-newsgroup-saved nil
914   "List of articles that have been saved.")
915
916 (defvar gnus-newsgroup-kill-headers nil)
917
918 (defvar gnus-newsgroup-replied nil
919   "List of articles that have been replied to in the current newsgroup.")
920
921 (defvar gnus-newsgroup-expirable nil
922   "List of articles in the current newsgroup that can be expired.")
923
924 (defvar gnus-newsgroup-processable nil
925   "List of articles in the current newsgroup that can be processed.")
926
927 (defvar gnus-newsgroup-downloadable nil
928   "List of articles in the current newsgroup that can be processed.")
929
930 (defvar gnus-newsgroup-undownloaded nil
931   "List of articles in the current newsgroup that haven't been downloaded..")
932
933 (defvar gnus-newsgroup-unsendable nil
934   "List of articles in the current newsgroup that won't be sent.")
935
936 (defvar gnus-newsgroup-bookmarks nil
937   "List of articles in the current newsgroup that have bookmarks.")
938
939 (defvar gnus-newsgroup-dormant nil
940   "List of dormant articles in the current newsgroup.")
941
942 (defvar gnus-newsgroup-scored nil
943   "List of scored articles in the current newsgroup.")
944
945 (defvar gnus-newsgroup-headers nil
946   "List of article headers in the current newsgroup.")
947
948 (defvar gnus-newsgroup-threads nil)
949
950 (defvar gnus-newsgroup-prepared nil
951   "Whether the current group has been prepared properly.")
952
953 (defvar gnus-newsgroup-ancient nil
954   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
955
956 (defvar gnus-newsgroup-sparse nil)
957
958 (defvar gnus-current-article nil)
959 (defvar gnus-article-current nil)
960 (defvar gnus-current-headers nil)
961 (defvar gnus-have-all-headers nil)
962 (defvar gnus-last-article nil)
963 (defvar gnus-newsgroup-history nil)
964
965 (defconst gnus-summary-local-variables
966   '(gnus-newsgroup-name
967     gnus-newsgroup-begin gnus-newsgroup-end
968     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
969     gnus-newsgroup-last-folder gnus-newsgroup-last-file
970     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
971     gnus-newsgroup-unselected gnus-newsgroup-marked
972     gnus-newsgroup-reads gnus-newsgroup-saved
973     gnus-newsgroup-replied gnus-newsgroup-expirable
974     gnus-newsgroup-processable gnus-newsgroup-killed
975     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
976     gnus-newsgroup-unsendable
977     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
978     gnus-newsgroup-headers gnus-newsgroup-threads
979     gnus-newsgroup-prepared gnus-summary-highlight-line-function
980     gnus-current-article gnus-current-headers gnus-have-all-headers
981     gnus-last-article gnus-article-internal-prepare-hook
982     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
983     gnus-newsgroup-scored gnus-newsgroup-kill-headers
984     gnus-thread-expunge-below
985     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
986     (gnus-summary-mark-below . global)
987     gnus-newsgroup-active gnus-scores-exclude-files
988     gnus-newsgroup-history gnus-newsgroup-ancient
989     gnus-newsgroup-sparse gnus-newsgroup-process-stack
990     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
991     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
992     (gnus-newsgroup-expunged-tally . 0)
993     gnus-cache-removable-articles gnus-newsgroup-cached
994     gnus-newsgroup-data gnus-newsgroup-data-reverse
995     gnus-newsgroup-limit gnus-newsgroup-limits)
996   "Variables that are buffer-local to the summary buffers.")
997
998 ;; Byte-compiler warning.
999 (defvar gnus-article-mode-map)
1000
1001 ;; Subject simplification.
1002
1003 (defun gnus-simplify-whitespace (str)
1004   "Remove excessive whitespace."
1005   (let ((mystr str))
1006     ;; Multiple spaces.
1007     (while (string-match "[ \t][ \t]+" mystr)
1008       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1009                           " "
1010                           (substring mystr (match-end 0)))))
1011     ;; Leading spaces.
1012     (when (string-match "^[ \t]+" mystr)
1013       (setq mystr (substring mystr (match-end 0))))
1014     ;; Trailing spaces.
1015     (when (string-match "[ \t]+$" mystr)
1016       (setq mystr (substring mystr 0 (match-beginning 0))))
1017     mystr))
1018
1019 (defsubst gnus-simplify-subject-re (subject)
1020   "Remove \"Re:\" from subject lines."
1021   (if (string-match "^[Rr][Ee]: *" subject)
1022       (substring subject (match-end 0))
1023     subject))
1024
1025 (defun gnus-simplify-subject (subject &optional re-only)
1026   "Remove `Re:' and words in parentheses.
1027 If RE-ONLY is non-nil, strip leading `Re:'s only."
1028   (let ((case-fold-search t))           ;Ignore case.
1029     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1030     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1031       (setq subject (substring subject (match-end 0))))
1032     ;; Remove uninteresting prefixes.
1033     (when (and (not re-only)
1034                gnus-simplify-ignored-prefixes
1035                (string-match gnus-simplify-ignored-prefixes subject))
1036       (setq subject (substring subject (match-end 0))))
1037     ;; Remove words in parentheses from end.
1038     (unless re-only
1039       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1040         (setq subject (substring subject 0 (match-beginning 0)))))
1041     ;; Return subject string.
1042     subject))
1043
1044 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1045 ;; all whitespace.
1046 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1047   (goto-char (point-min))
1048   (while (re-search-forward regexp nil t)
1049       (replace-match (or newtext ""))))
1050
1051 (defun gnus-simplify-buffer-fuzzy ()
1052   "Simplify string in the buffer fuzzily.
1053 The string in the accessible portion of the current buffer is simplified.
1054 It is assumed to be a single-line subject.
1055 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1056 matter is removed.  Additional things can be deleted by setting
1057 gnus-simplify-subject-fuzzy-regexp."
1058   (let ((case-fold-search t)
1059         (modified-tick))
1060     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1061
1062     (while (not (eq modified-tick (buffer-modified-tick)))
1063       (setq modified-tick (buffer-modified-tick))
1064       (cond
1065        ((listp gnus-simplify-subject-fuzzy-regexp)
1066         (mapcar 'gnus-simplify-buffer-fuzzy-step
1067                 gnus-simplify-subject-fuzzy-regexp))
1068        (gnus-simplify-subject-fuzzy-regexp
1069         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1070       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1071       (gnus-simplify-buffer-fuzzy-step
1072        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1073       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1074
1075     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1076     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1077     (gnus-simplify-buffer-fuzzy-step " $")
1078     (gnus-simplify-buffer-fuzzy-step "^ +")))
1079
1080 (defun gnus-simplify-subject-fuzzy (subject)
1081   "Simplify a subject string fuzzily.
1082 See `gnus-simplify-buffer-fuzzy' for details."
1083   (save-excursion
1084     (gnus-set-work-buffer)
1085     (let ((case-fold-search t))
1086       ;; Remove uninteresting prefixes.
1087       (when (and gnus-simplify-ignored-prefixes
1088                  (string-match gnus-simplify-ignored-prefixes subject))
1089         (setq subject (substring subject (match-end 0))))
1090       (insert subject)
1091       (inline (gnus-simplify-buffer-fuzzy))
1092       (buffer-string))))
1093
1094 (defsubst gnus-simplify-subject-fully (subject)
1095   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1096   (cond
1097    (gnus-simplify-subject-functions
1098     (gnus-map-function gnus-simplify-subject-functions subject))
1099    ((null gnus-summary-gather-subject-limit)
1100     (gnus-simplify-subject-re subject))
1101    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1102     (gnus-simplify-subject-fuzzy subject))
1103    ((numberp gnus-summary-gather-subject-limit)
1104     (gnus-limit-string (gnus-simplify-subject-re subject)
1105                        gnus-summary-gather-subject-limit))
1106    (t
1107     subject)))
1108
1109 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1110   "Check whether two subjects are equal.
1111 If optional argument simple-first is t, first argument is already
1112 simplified."
1113   (cond
1114    ((null simple-first)
1115     (equal (gnus-simplify-subject-fully s1)
1116            (gnus-simplify-subject-fully s2)))
1117    (t
1118     (equal s1
1119            (gnus-simplify-subject-fully s2)))))
1120
1121 (defun gnus-summary-bubble-group ()
1122   "Increase the score of the current group.
1123 This is a handy function to add to `gnus-summary-exit-hook' to
1124 increase the score of each group you read."
1125   (gnus-group-add-score gnus-newsgroup-name))
1126
1127 \f
1128 ;;;
1129 ;;; Gnus summary mode
1130 ;;;
1131
1132 (put 'gnus-summary-mode 'mode-class 'special)
1133
1134 (when t
1135   ;; Non-orthogonal keys
1136
1137   (gnus-define-keys gnus-summary-mode-map
1138     " " gnus-summary-next-page
1139     "\177" gnus-summary-prev-page
1140     [delete] gnus-summary-prev-page
1141     [backspace] gnus-summary-prev-page
1142     "\r" gnus-summary-scroll-up
1143     "n" gnus-summary-next-unread-article
1144     "p" gnus-summary-prev-unread-article
1145     "N" gnus-summary-next-article
1146     "P" gnus-summary-prev-article
1147     "\M-\C-n" gnus-summary-next-same-subject
1148     "\M-\C-p" gnus-summary-prev-same-subject
1149     "\M-n" gnus-summary-next-unread-subject
1150     "\M-p" gnus-summary-prev-unread-subject
1151     "." gnus-summary-first-unread-article
1152     "," gnus-summary-best-unread-article
1153     "\M-s" gnus-summary-search-article-forward
1154     "\M-r" gnus-summary-search-article-backward
1155     "<" gnus-summary-beginning-of-article
1156     ">" gnus-summary-end-of-article
1157     "j" gnus-summary-goto-article
1158     "^" gnus-summary-refer-parent-article
1159     "\M-^" gnus-summary-refer-article
1160     "u" gnus-summary-tick-article-forward
1161     "!" gnus-summary-tick-article-forward
1162     "U" gnus-summary-tick-article-backward
1163     "d" gnus-summary-mark-as-read-forward
1164     "D" gnus-summary-mark-as-read-backward
1165     "E" gnus-summary-mark-as-expirable
1166     "\M-u" gnus-summary-clear-mark-forward
1167     "\M-U" gnus-summary-clear-mark-backward
1168     "k" gnus-summary-kill-same-subject-and-select
1169     "\C-k" gnus-summary-kill-same-subject
1170     "\M-\C-k" gnus-summary-kill-thread
1171     "\M-\C-l" gnus-summary-lower-thread
1172     "e" gnus-summary-edit-article
1173     "#" gnus-summary-mark-as-processable
1174     "\M-#" gnus-summary-unmark-as-processable
1175     "\M-\C-t" gnus-summary-toggle-threads
1176     "\M-\C-s" gnus-summary-show-thread
1177     "\M-\C-h" gnus-summary-hide-thread
1178     "\M-\C-f" gnus-summary-next-thread
1179     "\M-\C-b" gnus-summary-prev-thread
1180     "\M-\C-u" gnus-summary-up-thread
1181     "\M-\C-d" gnus-summary-down-thread
1182     "&" gnus-summary-execute-command
1183     "c" gnus-summary-catchup-and-exit
1184     "\C-w" gnus-summary-mark-region-as-read
1185     "\C-t" gnus-summary-toggle-truncation
1186     "?" gnus-summary-mark-as-dormant
1187     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1188     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1189     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1190     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1191     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1192     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1193     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1194     "=" gnus-summary-expand-window
1195     "\C-x\C-s" gnus-summary-reselect-current-group
1196     "\M-g" gnus-summary-rescan-group
1197     "w" gnus-summary-stop-page-breaking
1198     "\C-c\C-r" gnus-summary-caesar-message
1199     "\M-t" gnus-summary-toggle-mime
1200     "f" gnus-summary-followup
1201     "F" gnus-summary-followup-with-original
1202     "C" gnus-summary-cancel-article
1203     "r" gnus-summary-reply
1204     "R" gnus-summary-reply-with-original
1205     "\C-c\C-f" gnus-summary-mail-forward
1206     "o" gnus-summary-save-article
1207     "\C-o" gnus-summary-save-article-mail
1208     "|" gnus-summary-pipe-output
1209     "\M-k" gnus-summary-edit-local-kill
1210     "\M-K" gnus-summary-edit-global-kill
1211     ;; "V" gnus-version
1212     "\C-c\C-d" gnus-summary-describe-group
1213     "q" gnus-summary-exit
1214     "Q" gnus-summary-exit-no-update
1215     "\C-c\C-i" gnus-info-find-node
1216     gnus-mouse-2 gnus-mouse-pick-article
1217     "m" gnus-summary-mail-other-window
1218     "a" gnus-summary-post-news
1219     "x" gnus-summary-limit-to-unread
1220     "s" gnus-summary-isearch-article
1221     "t" gnus-article-hide-headers
1222     "g" gnus-summary-show-article
1223     "l" gnus-summary-goto-last-article
1224     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1225     "\C-d" gnus-summary-enter-digest-group
1226     "\M-\C-d" gnus-summary-read-document
1227     "\M-\C-e" gnus-summary-edit-parameters
1228     "\C-c\C-b" gnus-bug
1229     "*" gnus-cache-enter-article
1230     "\M-*" gnus-cache-remove-article
1231     "\M-&" gnus-summary-universal-argument
1232     "\C-l" gnus-recenter
1233     "I" gnus-summary-increase-score
1234     "L" gnus-summary-lower-score
1235     "\M-i" gnus-symbolic-argument
1236     "h" gnus-summary-select-article-buffer
1237     
1238     "V" gnus-summary-score-map
1239     "X" gnus-uu-extract-map
1240     "S" gnus-summary-send-map)
1241
1242   ;; Sort of orthogonal keymap
1243   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1244     "t" gnus-summary-tick-article-forward
1245     "!" gnus-summary-tick-article-forward
1246     "d" gnus-summary-mark-as-read-forward
1247     "r" gnus-summary-mark-as-read-forward
1248     "c" gnus-summary-clear-mark-forward
1249     " " gnus-summary-clear-mark-forward
1250     "e" gnus-summary-mark-as-expirable
1251     "x" gnus-summary-mark-as-expirable
1252     "?" gnus-summary-mark-as-dormant
1253     "b" gnus-summary-set-bookmark
1254     "B" gnus-summary-remove-bookmark
1255     "#" gnus-summary-mark-as-processable
1256     "\M-#" gnus-summary-unmark-as-processable
1257     "S" gnus-summary-limit-include-expunged
1258     "C" gnus-summary-catchup
1259     "H" gnus-summary-catchup-to-here
1260     "\C-c" gnus-summary-catchup-all
1261     "k" gnus-summary-kill-same-subject-and-select
1262     "K" gnus-summary-kill-same-subject
1263     "P" gnus-uu-mark-map)
1264
1265   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1266     "c" gnus-summary-clear-above
1267     "u" gnus-summary-tick-above
1268     "m" gnus-summary-mark-above
1269     "k" gnus-summary-kill-below)
1270
1271   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1272     "/" gnus-summary-limit-to-subject
1273     "n" gnus-summary-limit-to-articles
1274     "w" gnus-summary-pop-limit
1275     "s" gnus-summary-limit-to-subject
1276     "a" gnus-summary-limit-to-author
1277     "u" gnus-summary-limit-to-unread
1278     "m" gnus-summary-limit-to-marks
1279     "v" gnus-summary-limit-to-score
1280     "*" gnus-summary-limit-include-cached
1281     "D" gnus-summary-limit-include-dormant
1282     "T" gnus-summary-limit-include-thread
1283     "d" gnus-summary-limit-exclude-dormant
1284     "t" gnus-summary-limit-to-age
1285     "E" gnus-summary-limit-include-expunged
1286     "c" gnus-summary-limit-exclude-childless-dormant
1287     "C" gnus-summary-limit-mark-excluded-as-read)
1288
1289   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1290     "n" gnus-summary-next-unread-article
1291     "p" gnus-summary-prev-unread-article
1292     "N" gnus-summary-next-article
1293     "P" gnus-summary-prev-article
1294     "\C-n" gnus-summary-next-same-subject
1295     "\C-p" gnus-summary-prev-same-subject
1296     "\M-n" gnus-summary-next-unread-subject
1297     "\M-p" gnus-summary-prev-unread-subject
1298     "f" gnus-summary-first-unread-article
1299     "b" gnus-summary-best-unread-article
1300     "j" gnus-summary-goto-article
1301     "g" gnus-summary-goto-subject
1302     "l" gnus-summary-goto-last-article
1303     "o" gnus-summary-pop-article)
1304
1305   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1306     "k" gnus-summary-kill-thread
1307     "l" gnus-summary-lower-thread
1308     "i" gnus-summary-raise-thread
1309     "T" gnus-summary-toggle-threads
1310     "t" gnus-summary-rethread-current
1311     "^" gnus-summary-reparent-thread
1312     "s" gnus-summary-show-thread
1313     "S" gnus-summary-show-all-threads
1314     "h" gnus-summary-hide-thread
1315     "H" gnus-summary-hide-all-threads
1316     "n" gnus-summary-next-thread
1317     "p" gnus-summary-prev-thread
1318     "u" gnus-summary-up-thread
1319     "o" gnus-summary-top-thread
1320     "d" gnus-summary-down-thread
1321     "#" gnus-uu-mark-thread
1322     "\M-#" gnus-uu-unmark-thread)
1323
1324   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1325     "g" gnus-summary-prepare
1326     "c" gnus-summary-insert-cached-articles)
1327
1328   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1329     "c" gnus-summary-catchup-and-exit
1330     "C" gnus-summary-catchup-all-and-exit
1331     "E" gnus-summary-exit-no-update
1332     "Q" gnus-summary-exit
1333     "Z" gnus-summary-exit
1334     "n" gnus-summary-catchup-and-goto-next-group
1335     "R" gnus-summary-reselect-current-group
1336     "G" gnus-summary-rescan-group
1337     "N" gnus-summary-next-group
1338     "s" gnus-summary-save-newsrc
1339     "P" gnus-summary-prev-group)
1340
1341   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1342     " " gnus-summary-next-page
1343     "n" gnus-summary-next-page
1344     "\177" gnus-summary-prev-page
1345     [delete] gnus-summary-prev-page
1346     "p" gnus-summary-prev-page
1347     "\r" gnus-summary-scroll-up
1348     "<" gnus-summary-beginning-of-article
1349     ">" gnus-summary-end-of-article
1350     "b" gnus-summary-beginning-of-article
1351     "e" gnus-summary-end-of-article
1352     "^" gnus-summary-refer-parent-article
1353     "r" gnus-summary-refer-parent-article
1354     "R" gnus-summary-refer-references
1355     "T" gnus-summary-refer-thread
1356     "g" gnus-summary-show-article
1357     "s" gnus-summary-isearch-article
1358     "P" gnus-summary-print-article)
1359
1360   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1361     "b" gnus-article-add-buttons
1362     "B" gnus-article-add-buttons-to-head
1363     "o" gnus-article-treat-overstrike
1364     "e" gnus-article-emphasize
1365     "w" gnus-article-fill-cited-article
1366     "c" gnus-article-remove-cr
1367     "q" gnus-article-de-quoted-unreadable
1368     "f" gnus-article-display-x-face
1369     "l" gnus-summary-stop-page-breaking
1370     "r" gnus-summary-caesar-message
1371     "t" gnus-article-hide-headers
1372     "v" gnus-summary-verbose-headers
1373     "m" gnus-summary-toggle-mime
1374     "h" gnus-article-treat-html
1375     "d" gnus-article-treat-dumbquotes)
1376
1377   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1378     "a" gnus-article-hide
1379     "h" gnus-article-hide-headers
1380     "b" gnus-article-hide-boring-headers
1381     "s" gnus-article-hide-signature
1382     "c" gnus-article-hide-citation
1383     "p" gnus-article-hide-pgp
1384     "P" gnus-article-hide-pem
1385     "\C-c" gnus-article-hide-citation-maybe)
1386
1387   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1388     "a" gnus-article-highlight
1389     "h" gnus-article-highlight-headers
1390     "c" gnus-article-highlight-citation
1391     "s" gnus-article-highlight-signature)
1392
1393   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1394     "z" gnus-article-date-ut
1395     "u" gnus-article-date-ut
1396     "l" gnus-article-date-local
1397     "e" gnus-article-date-lapsed
1398     "o" gnus-article-date-original
1399     "i" gnus-article-date-iso8601
1400     "s" gnus-article-date-user)
1401
1402   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1403     "t" gnus-article-remove-trailing-blank-lines
1404     "l" gnus-article-strip-leading-blank-lines
1405     "m" gnus-article-strip-multiple-blank-lines
1406     "a" gnus-article-strip-blank-lines
1407     "A" gnus-article-strip-all-blank-lines
1408     "s" gnus-article-strip-leading-space)
1409
1410   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1411     "v" gnus-version
1412     "f" gnus-summary-fetch-faq
1413     "d" gnus-summary-describe-group
1414     "h" gnus-summary-describe-briefly
1415     "i" gnus-info-find-node)
1416
1417   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1418     "e" gnus-summary-expire-articles
1419     "\M-\C-e" gnus-summary-expire-articles-now
1420     "\177" gnus-summary-delete-article
1421     [delete] gnus-summary-delete-article
1422     "m" gnus-summary-move-article
1423     "r" gnus-summary-respool-article
1424     "w" gnus-summary-edit-article
1425     "c" gnus-summary-copy-article
1426     "B" gnus-summary-crosspost-article
1427     "q" gnus-summary-respool-query
1428     "i" gnus-summary-import-article
1429     "p" gnus-summary-article-posted-p)
1430
1431   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1432     "o" gnus-summary-save-article
1433     "m" gnus-summary-save-article-mail
1434     "F" gnus-summary-write-article-file
1435     "r" gnus-summary-save-article-rmail
1436     "f" gnus-summary-save-article-file
1437     "b" gnus-summary-save-article-body-file
1438     "h" gnus-summary-save-article-folder
1439     "v" gnus-summary-save-article-vm
1440     "p" gnus-summary-pipe-output
1441     "s" gnus-soup-add-article))
1442
1443 (defun gnus-summary-make-menu-bar ()
1444   (gnus-turn-off-edit-menu 'summary)
1445
1446   (unless (boundp 'gnus-summary-misc-menu)
1447
1448     (easy-menu-define
1449      gnus-summary-kill-menu gnus-summary-mode-map ""
1450      (cons
1451       "Score"
1452       (nconc
1453        (list
1454         ["Enter score..." gnus-summary-score-entry t]
1455         ["Customize" gnus-score-customize t])
1456        (gnus-make-score-map 'increase)
1457        (gnus-make-score-map 'lower)
1458        '(("Mark"
1459           ["Kill below" gnus-summary-kill-below t]
1460           ["Mark above" gnus-summary-mark-above t]
1461           ["Tick above" gnus-summary-tick-above t]
1462           ["Clear above" gnus-summary-clear-above t])
1463          ["Current score" gnus-summary-current-score t]
1464          ["Set score" gnus-summary-set-score t]
1465          ["Switch current score file..." gnus-score-change-score-file t]
1466          ["Set mark below..." gnus-score-set-mark-below t]
1467          ["Set expunge below..." gnus-score-set-expunge-below t]
1468          ["Edit current score file" gnus-score-edit-current-scores t]
1469          ["Edit score file" gnus-score-edit-file t]
1470          ["Trace score" gnus-score-find-trace t]
1471          ["Find words" gnus-score-find-favourite-words t]
1472          ["Rescore buffer" gnus-summary-rescore t]
1473          ["Increase score..." gnus-summary-increase-score t]
1474          ["Lower score..." gnus-summary-lower-score t]))))
1475
1476     ;; Define both the Article menu in the summary buffer and the equivalent
1477     ;; Commands menu in the article buffer here for consistency.
1478     (let ((innards
1479            '(("Hide"
1480               ["All" gnus-article-hide t]
1481               ["Headers" gnus-article-hide-headers t]
1482               ["Signature" gnus-article-hide-signature t]
1483               ["Citation" gnus-article-hide-citation t]
1484               ["PGP" gnus-article-hide-pgp t]
1485               ["Boring headers" gnus-article-hide-boring-headers t])
1486              ("Highlight"
1487               ["All" gnus-article-highlight t]
1488               ["Headers" gnus-article-highlight-headers t]
1489               ["Signature" gnus-article-highlight-signature t]
1490               ["Citation" gnus-article-highlight-citation t])
1491              ("Date"
1492               ["Local" gnus-article-date-local t]
1493               ["ISO8601" gnus-article-date-iso8601 t]
1494               ["UT" gnus-article-date-ut t]
1495               ["Original" gnus-article-date-original t]
1496               ["Lapsed" gnus-article-date-lapsed t]
1497               ["User-defined" gnus-article-date-user t])
1498              ("Washing"
1499               ("Remove Blanks"
1500                ["Leading" gnus-article-strip-leading-blank-lines t]
1501                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1502                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1503                ["All of the above" gnus-article-strip-blank-lines t]
1504                ["All" gnus-article-strip-all-blank-lines t]
1505                ["Leading space" gnus-article-strip-leading-space t])
1506               ["Overstrike" gnus-article-treat-overstrike t]
1507               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1508               ["Emphasis" gnus-article-emphasize t]
1509               ["Word wrap" gnus-article-fill-cited-article t]
1510               ["CR" gnus-article-remove-cr t]
1511               ["Show X-Face" gnus-article-display-x-face t]
1512               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1513               ["UnHTMLize" gnus-article-treat-html t]
1514               ["Rot 13" gnus-summary-caesar-message t]
1515               ["Unix pipe" gnus-summary-pipe-message t]
1516               ["Add buttons" gnus-article-add-buttons t]
1517               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1518               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1519               ["Toggle MIME" gnus-summary-toggle-mime t]
1520               ["Verbose header" gnus-summary-verbose-headers t]
1521               ["Toggle header" gnus-summary-toggle-header t])
1522              ("Output"
1523               ["Save in default format" gnus-summary-save-article t]
1524               ["Save in file" gnus-summary-save-article-file t]
1525               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1526               ["Save in MH folder" gnus-summary-save-article-folder t]
1527               ["Save in VM folder" gnus-summary-save-article-vm t]
1528               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1529               ["Save body in file" gnus-summary-save-article-body-file t]
1530               ["Pipe through a filter" gnus-summary-pipe-output t]
1531               ["Add to SOUP packet" gnus-soup-add-article t]
1532               ["Print" gnus-summary-print-article t])
1533              ("Backend"
1534               ["Respool article..." gnus-summary-respool-article t]
1535               ["Move article..." gnus-summary-move-article
1536                (gnus-check-backend-function
1537                 'request-move-article gnus-newsgroup-name)]
1538               ["Copy article..." gnus-summary-copy-article t]
1539               ["Crosspost article..." gnus-summary-crosspost-article
1540                (gnus-check-backend-function
1541                 'request-replace-article gnus-newsgroup-name)]
1542               ["Import file..." gnus-summary-import-article t]
1543               ["Check if posted" gnus-summary-article-posted-p t]
1544               ["Edit article" gnus-summary-edit-article
1545                (not (gnus-group-read-only-p))]
1546               ["Delete article" gnus-summary-delete-article
1547                (gnus-check-backend-function
1548                 'request-expire-articles gnus-newsgroup-name)]
1549               ["Query respool" gnus-summary-respool-query t]
1550               ["Delete expirable articles" gnus-summary-expire-articles-now
1551                (gnus-check-backend-function
1552                 'request-expire-articles gnus-newsgroup-name)])
1553              ("Extract"
1554               ["Uudecode" gnus-uu-decode-uu t]
1555               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1556               ["Unshar" gnus-uu-decode-unshar t]
1557               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1558               ["Save" gnus-uu-decode-save t]
1559               ["Binhex" gnus-uu-decode-binhex t]
1560               ["Postscript" gnus-uu-decode-postscript t])
1561              ("Cache"
1562               ["Enter article" gnus-cache-enter-article t]
1563               ["Remove article" gnus-cache-remove-article t])
1564              ["Select article buffer" gnus-summary-select-article-buffer t]
1565              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1566              ["Isearch article..." gnus-summary-isearch-article t]
1567              ["Beginning of the article" gnus-summary-beginning-of-article t]
1568              ["End of the article" gnus-summary-end-of-article t]
1569              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1570              ["Fetch referenced articles" gnus-summary-refer-references t]
1571              ["Fetch current thread" gnus-summary-refer-thread t]
1572              ["Fetch article with id..." gnus-summary-refer-article t]
1573              ["Redisplay" gnus-summary-show-article t])))
1574       (easy-menu-define
1575        gnus-summary-article-menu gnus-summary-mode-map ""
1576        (cons "Article" innards))
1577
1578       (easy-menu-define
1579        gnus-article-commands-menu gnus-article-mode-map ""
1580        (cons "Commands" innards)))
1581
1582     (easy-menu-define
1583      gnus-summary-thread-menu gnus-summary-mode-map ""
1584      '("Threads"
1585        ["Toggle threading" gnus-summary-toggle-threads t]
1586        ["Hide threads" gnus-summary-hide-all-threads t]
1587        ["Show threads" gnus-summary-show-all-threads t]
1588        ["Hide thread" gnus-summary-hide-thread t]
1589        ["Show thread" gnus-summary-show-thread t]
1590        ["Go to next thread" gnus-summary-next-thread t]
1591        ["Go to previous thread" gnus-summary-prev-thread t]
1592        ["Go down thread" gnus-summary-down-thread t]
1593        ["Go up thread" gnus-summary-up-thread t]
1594        ["Top of thread" gnus-summary-top-thread t]
1595        ["Mark thread as read" gnus-summary-kill-thread t]
1596        ["Lower thread score" gnus-summary-lower-thread t]
1597        ["Raise thread score" gnus-summary-raise-thread t]
1598        ["Rethread current" gnus-summary-rethread-current t]
1599        ))
1600
1601     (easy-menu-define
1602      gnus-summary-post-menu gnus-summary-mode-map ""
1603      '("Post"
1604        ["Post an article" gnus-summary-post-news t]
1605        ["Followup" gnus-summary-followup t]
1606        ["Followup and yank" gnus-summary-followup-with-original t]
1607        ["Supersede article" gnus-summary-supersede-article t]
1608        ["Cancel article" gnus-summary-cancel-article t]
1609        ["Reply" gnus-summary-reply t]
1610        ["Reply and yank" gnus-summary-reply-with-original t]
1611        ["Wide reply" gnus-summary-wide-reply t]
1612        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1613        ["Mail forward" gnus-summary-mail-forward t]
1614        ["Post forward" gnus-summary-post-forward t]
1615        ["Digest and mail" gnus-uu-digest-mail-forward t]
1616        ["Digest and post" gnus-uu-digest-post-forward t]
1617        ["Resend message" gnus-summary-resend-message t]
1618        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1619        ["Send a mail" gnus-summary-mail-other-window t]
1620        ["Uuencode and post" gnus-uu-post-news t]
1621        ["Followup via news" gnus-summary-followup-to-mail t]
1622        ["Followup via news and yank"
1623         gnus-summary-followup-to-mail-with-original t]
1624        ;;("Draft"
1625        ;;["Send" gnus-summary-send-draft t]
1626        ;;["Send bounced" gnus-resend-bounced-mail t])
1627        ))
1628
1629     (easy-menu-define
1630      gnus-summary-misc-menu gnus-summary-mode-map ""
1631      '("Misc"
1632        ("Mark Read"
1633         ["Mark as read" gnus-summary-mark-as-read-forward t]
1634         ["Mark same subject and select"
1635          gnus-summary-kill-same-subject-and-select t]
1636         ["Mark same subject" gnus-summary-kill-same-subject t]
1637         ["Catchup" gnus-summary-catchup t]
1638         ["Catchup all" gnus-summary-catchup-all t]
1639         ["Catchup to here" gnus-summary-catchup-to-here t]
1640         ["Catchup region" gnus-summary-mark-region-as-read t]
1641         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1642        ("Mark Various"
1643         ["Tick" gnus-summary-tick-article-forward t]
1644         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1645         ["Remove marks" gnus-summary-clear-mark-forward t]
1646         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1647         ["Set bookmark" gnus-summary-set-bookmark t]
1648         ["Remove bookmark" gnus-summary-remove-bookmark t])
1649        ("Mark Limit"
1650         ["Marks..." gnus-summary-limit-to-marks t]
1651         ["Subject..." gnus-summary-limit-to-subject t]
1652         ["Author..." gnus-summary-limit-to-author t]
1653         ["Age..." gnus-summary-limit-to-age t]
1654         ["Score" gnus-summary-limit-to-score t]
1655         ["Unread" gnus-summary-limit-to-unread t]
1656         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1657         ["Articles" gnus-summary-limit-to-articles t]
1658         ["Pop limit" gnus-summary-pop-limit t]
1659         ["Show dormant" gnus-summary-limit-include-dormant t]
1660         ["Hide childless dormant"
1661          gnus-summary-limit-exclude-childless-dormant t]
1662         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1663         ["Show expunged" gnus-summary-show-all-expunged t])
1664        ("Process Mark"
1665         ["Set mark" gnus-summary-mark-as-processable t]
1666         ["Remove mark" gnus-summary-unmark-as-processable t]
1667         ["Remove all marks" gnus-summary-unmark-all-processable t]
1668         ["Mark above" gnus-uu-mark-over t]
1669         ["Mark series" gnus-uu-mark-series t]
1670         ["Mark region" gnus-uu-mark-region t]
1671         ["Unmark region" gnus-uu-unmark-region t]
1672         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1673         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1674         ["Mark all" gnus-uu-mark-all t]
1675         ["Mark buffer" gnus-uu-mark-buffer t]
1676         ["Mark sparse" gnus-uu-mark-sparse t]
1677         ["Mark thread" gnus-uu-mark-thread t]
1678         ["Unmark thread" gnus-uu-unmark-thread t]
1679         ("Process Mark Sets"
1680          ["Kill" gnus-summary-kill-process-mark t]
1681          ["Yank" gnus-summary-yank-process-mark
1682           gnus-newsgroup-process-stack]
1683          ["Save" gnus-summary-save-process-mark t]))
1684        ("Scroll article"
1685         ["Page forward" gnus-summary-next-page t]
1686         ["Page backward" gnus-summary-prev-page t]
1687         ["Line forward" gnus-summary-scroll-up t])
1688        ("Move"
1689         ["Next unread article" gnus-summary-next-unread-article t]
1690         ["Previous unread article" gnus-summary-prev-unread-article t]
1691         ["Next article" gnus-summary-next-article t]
1692         ["Previous article" gnus-summary-prev-article t]
1693         ["Next unread subject" gnus-summary-next-unread-subject t]
1694         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1695         ["Next article same subject" gnus-summary-next-same-subject t]
1696         ["Previous article same subject" gnus-summary-prev-same-subject t]
1697         ["First unread article" gnus-summary-first-unread-article t]
1698         ["Best unread article" gnus-summary-best-unread-article t]
1699         ["Go to subject number..." gnus-summary-goto-subject t]
1700         ["Go to article number..." gnus-summary-goto-article t]
1701         ["Go to the last article" gnus-summary-goto-last-article t]
1702         ["Pop article off history" gnus-summary-pop-article t])
1703        ("Sort"
1704         ["Sort by number" gnus-summary-sort-by-number t]
1705         ["Sort by author" gnus-summary-sort-by-author t]
1706         ["Sort by subject" gnus-summary-sort-by-subject t]
1707         ["Sort by date" gnus-summary-sort-by-date t]
1708         ["Sort by score" gnus-summary-sort-by-score t]
1709         ["Sort by lines" gnus-summary-sort-by-lines t])
1710        ("Help"
1711         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1712         ["Describe group" gnus-summary-describe-group t]
1713         ["Read manual" gnus-info-find-node t])
1714        ("Modes"
1715         ["Pick and read" gnus-pick-mode t]
1716         ["Binary" gnus-binary-mode t])
1717        ("Regeneration"
1718         ["Regenerate" gnus-summary-prepare t]
1719         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1720         ["Toggle threading" gnus-summary-toggle-threads t])
1721        ["Filter articles..." gnus-summary-execute-command t]
1722        ["Run command on subjects..." gnus-summary-universal-argument t]
1723        ["Search articles forward..." gnus-summary-search-article-forward t]
1724        ["Search articles backward..." gnus-summary-search-article-backward t]
1725        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1726        ["Expand window" gnus-summary-expand-window t]
1727        ["Expire expirable articles" gnus-summary-expire-articles
1728         (gnus-check-backend-function
1729          'request-expire-articles gnus-newsgroup-name)]
1730        ["Edit local kill file" gnus-summary-edit-local-kill t]
1731        ["Edit main kill file" gnus-summary-edit-global-kill t]
1732        ["Edit group parameters" gnus-summary-edit-parameters t]
1733        ("Exit"
1734         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1735         ["Catchup all and exit" gnus-summary-catchup-and-exit t]
1736         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1737         ["Exit group" gnus-summary-exit t]
1738         ["Exit group without updating" gnus-summary-exit-no-update t]
1739         ["Exit and goto next group" gnus-summary-next-group t]
1740         ["Exit and goto prev group" gnus-summary-prev-group t]
1741         ["Reselect group" gnus-summary-reselect-current-group t]
1742         ["Rescan group" gnus-summary-rescan-group t]
1743         ["Update dribble" gnus-summary-save-newsrc t])))
1744
1745     (gnus-run-hooks 'gnus-summary-menu-hook)))
1746
1747 (defun gnus-score-set-default (var value)
1748   "A version of set that updates the GNU Emacs menu-bar."
1749   (set var value)
1750   ;; It is the message that forces the active status to be updated.
1751   (message ""))
1752
1753 (defun gnus-make-score-map (type)
1754   "Make a summary score map of type TYPE."
1755   (if t
1756       nil
1757     (let ((headers '(("author" "from" string)
1758                      ("subject" "subject" string)
1759                      ("article body" "body" string)
1760                      ("article head" "head" string)
1761                      ("xref" "xref" string)
1762                      ("lines" "lines" number)
1763                      ("followups to author" "followup" string)))
1764           (types '((number ("less than" <)
1765                            ("greater than" >)
1766                            ("equal" =))
1767                    (string ("substring" s)
1768                            ("exact string" e)
1769                            ("fuzzy string" f)
1770                            ("regexp" r))))
1771           (perms '(("temporary" (current-time-string))
1772                    ("permanent" nil)
1773                    ("immediate" now)))
1774           header)
1775       (list
1776        (apply
1777         'nconc
1778         (list
1779          (if (eq type 'lower)
1780              "Lower score"
1781            "Increase score"))
1782         (let (outh)
1783           (while headers
1784             (setq header (car headers))
1785             (setq outh
1786                   (cons
1787                    (apply
1788                     'nconc
1789                     (list (car header))
1790                     (let ((ts (cdr (assoc (nth 2 header) types)))
1791                           outt)
1792                       (while ts
1793                         (setq outt
1794                               (cons
1795                                (apply
1796                                 'nconc
1797                                 (list (caar ts))
1798                                 (let ((ps perms)
1799                                       outp)
1800                                   (while ps
1801                                     (setq outp
1802                                           (cons
1803                                            (vector
1804                                             (caar ps)
1805                                             (list
1806                                              'gnus-summary-score-entry
1807                                              (nth 1 header)
1808                                              (if (or (string= (nth 1 header)
1809                                                               "head")
1810                                                      (string= (nth 1 header)
1811                                                               "body"))
1812                                                  ""
1813                                                (list 'gnus-summary-header
1814                                                      (nth 1 header)))
1815                                              (list 'quote (nth 1 (car ts)))
1816                                              (list 'gnus-score-default nil)
1817                                              (nth 1 (car ps))
1818                                              t)
1819                                             t)
1820                                            outp))
1821                                     (setq ps (cdr ps)))
1822                                   (list (nreverse outp))))
1823                                outt))
1824                         (setq ts (cdr ts)))
1825                       (list (nreverse outt))))
1826                    outh))
1827             (setq headers (cdr headers)))
1828           (list (nreverse outh))))))))
1829
1830 \f
1831
1832 (defun gnus-summary-mode (&optional group)
1833   "Major mode for reading articles.
1834
1835 All normal editing commands are switched off.
1836 \\<gnus-summary-mode-map>
1837 Each line in this buffer represents one article.  To read an
1838 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
1839 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
1840 respectively.
1841
1842 You can also post articles and send mail from this buffer.  To
1843 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
1844 of an article, type `\\[gnus-summary-reply]'.
1845
1846 There are approx. one gazillion commands you can execute in this
1847 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
1848
1849 The following commands are available:
1850
1851 \\{gnus-summary-mode-map}"
1852   (interactive)
1853   (when (gnus-visual-p 'summary-menu 'menu)
1854     (gnus-summary-make-menu-bar))
1855   (kill-all-local-variables)
1856   (gnus-summary-make-local-variables)
1857   (gnus-make-thread-indent-array)
1858   (gnus-simplify-mode-line)
1859   (setq major-mode 'gnus-summary-mode)
1860   (setq mode-name "Summary")
1861   (make-local-variable 'minor-mode-alist)
1862   (use-local-map gnus-summary-mode-map)
1863   (buffer-disable-undo (current-buffer))
1864   (setq buffer-read-only t)             ;Disable modification
1865   (setq truncate-lines t)
1866   (setq selective-display t)
1867   (setq selective-display-ellipses t)   ;Display `...'
1868   (gnus-summary-set-display-table)
1869   (gnus-set-default-directory)
1870   (setq gnus-newsgroup-name group)
1871   (make-local-variable 'gnus-summary-line-format)
1872   (make-local-variable 'gnus-summary-line-format-spec)
1873   (make-local-variable 'gnus-summary-dummy-line-format)
1874   (make-local-variable 'gnus-summary-dummy-line-format-spec)
1875   (make-local-variable 'gnus-summary-mark-positions)
1876   (make-local-hook 'post-command-hook)
1877   (add-hook 'post-command-hook 'gnus-clear-inboxes-moved nil t)
1878   (make-local-hook 'pre-command-hook)
1879   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
1880   (gnus-run-hooks 'gnus-summary-mode-hook)
1881   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
1882   (gnus-update-summary-mark-positions))
1883
1884 (defun gnus-summary-make-local-variables ()
1885   "Make all the local summary buffer variables."
1886   (let ((locals gnus-summary-local-variables)
1887         global local)
1888     (while (setq local (pop locals))
1889       (if (consp local)
1890           (progn
1891             (if (eq (cdr local) 'global)
1892                 ;; Copy the global value of the variable.
1893                 (setq global (symbol-value (car local)))
1894               ;; Use the value from the list.
1895               (setq global (eval (cdr local))))
1896             (make-local-variable (car local))
1897             (set (car local) global))
1898         ;; Simple nil-valued local variable.
1899         (make-local-variable local)
1900         (set local nil)))))
1901
1902 (defun gnus-summary-clear-local-variables ()
1903   (let ((locals gnus-summary-local-variables))
1904     (while locals
1905       (if (consp (car locals))
1906           (and (vectorp (caar locals))
1907                (set (caar locals) nil))
1908         (and (vectorp (car locals))
1909              (set (car locals) nil)))
1910       (setq locals (cdr locals)))))
1911
1912 ;; Summary data functions.
1913
1914 (defmacro gnus-data-number (data)
1915   `(car ,data))
1916
1917 (defmacro gnus-data-set-number (data number)
1918   `(setcar ,data ,number))
1919
1920 (defmacro gnus-data-mark (data)
1921   `(nth 1 ,data))
1922
1923 (defmacro gnus-data-set-mark (data mark)
1924   `(setcar (nthcdr 1 ,data) ,mark))
1925
1926 (defmacro gnus-data-pos (data)
1927   `(nth 2 ,data))
1928
1929 (defmacro gnus-data-set-pos (data pos)
1930   `(setcar (nthcdr 2 ,data) ,pos))
1931
1932 (defmacro gnus-data-header (data)
1933   `(nth 3 ,data))
1934
1935 (defmacro gnus-data-set-header (data header)
1936   `(setf (nth 3 ,data) ,header))
1937
1938 (defmacro gnus-data-level (data)
1939   `(nth 4 ,data))
1940
1941 (defmacro gnus-data-unread-p (data)
1942   `(= (nth 1 ,data) gnus-unread-mark))
1943
1944 (defmacro gnus-data-read-p (data)
1945   `(/= (nth 1 ,data) gnus-unread-mark))
1946
1947 (defmacro gnus-data-pseudo-p (data)
1948   `(consp (nth 3 ,data)))
1949
1950 (defmacro gnus-data-find (number)
1951   `(assq ,number gnus-newsgroup-data))
1952
1953 (defmacro gnus-data-find-list (number &optional data)
1954   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
1955      (memq (assq ,number bdata)
1956            bdata)))
1957
1958 (defmacro gnus-data-make (number mark pos header level)
1959   `(list ,number ,mark ,pos ,header ,level))
1960
1961 (defun gnus-data-enter (after-article number mark pos header level offset)
1962   (let ((data (gnus-data-find-list after-article)))
1963     (unless data
1964       (error "No such article: %d" after-article))
1965     (setcdr data (cons (gnus-data-make number mark pos header level)
1966                        (cdr data)))
1967     (setq gnus-newsgroup-data-reverse nil)
1968     (gnus-data-update-list (cddr data) offset)))
1969
1970 (defun gnus-data-enter-list (after-article list &optional offset)
1971   (when list
1972     (let ((data (and after-article (gnus-data-find-list after-article)))
1973           (ilist list))
1974       (or data (not after-article) (error "No such article: %d" after-article))
1975       ;; Find the last element in the list to be spliced into the main
1976       ;; list.
1977       (while (cdr list)
1978         (setq list (cdr list)))
1979       (if (not data)
1980           (progn
1981             (setcdr list gnus-newsgroup-data)
1982             (setq gnus-newsgroup-data ilist)
1983             (when offset
1984               (gnus-data-update-list (cdr list) offset)))
1985         (setcdr list (cdr data))
1986         (setcdr data ilist)
1987         (when offset
1988           (gnus-data-update-list (cdr list) offset)))
1989       (setq gnus-newsgroup-data-reverse nil))))
1990
1991 (defun gnus-data-remove (article &optional offset)
1992   (let ((data gnus-newsgroup-data))
1993     (if (= (gnus-data-number (car data)) article)
1994         (progn
1995           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
1996                 gnus-newsgroup-data-reverse nil)
1997           (when offset
1998             (gnus-data-update-list gnus-newsgroup-data offset)))
1999       (while (cdr data)
2000         (when (= (gnus-data-number (cadr data)) article)
2001           (setcdr data (cddr data))
2002           (when offset
2003             (gnus-data-update-list (cdr data) offset))
2004           (setq data nil
2005                 gnus-newsgroup-data-reverse nil))
2006         (setq data (cdr data))))))
2007
2008 (defmacro gnus-data-list (backward)
2009   `(if ,backward
2010        (or gnus-newsgroup-data-reverse
2011            (setq gnus-newsgroup-data-reverse
2012                  (reverse gnus-newsgroup-data)))
2013      gnus-newsgroup-data))
2014
2015 (defun gnus-data-update-list (data offset)
2016   "Add OFFSET to the POS of all data entries in DATA."
2017   (while data
2018     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2019     (setq data (cdr data))))
2020
2021 (defun gnus-data-compute-positions ()
2022   "Compute the positions of all articles."
2023   (let ((data gnus-newsgroup-data)
2024         pos)
2025     (while data
2026       (when (setq pos (text-property-any
2027                        (point-min) (point-max)
2028                        'gnus-number (gnus-data-number (car data))))
2029         (gnus-data-set-pos (car data) (+ pos 3)))
2030       (setq data (cdr data)))))
2031
2032 (defun gnus-summary-article-pseudo-p (article)
2033   "Say whether this article is a pseudo article or not."
2034   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2035
2036 (defmacro gnus-summary-article-sparse-p (article)
2037   "Say whether this article is a sparse article or not."
2038   `(memq ,article gnus-newsgroup-sparse))
2039
2040 (defmacro gnus-summary-article-ancient-p (article)
2041   "Say whether this article is a sparse article or not."
2042   `(memq ,article gnus-newsgroup-ancient))
2043
2044 (defun gnus-article-parent-p (number)
2045   "Say whether this article is a parent or not."
2046   (let ((data (gnus-data-find-list number)))
2047     (and (cdr data)                     ; There has to be an article after...
2048          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2049             (gnus-data-level (nth 1 data))))))
2050
2051 (defun gnus-article-children (number)
2052   "Return a list of all children to NUMBER."
2053   (let* ((data (gnus-data-find-list number))
2054          (level (gnus-data-level (car data)))
2055          children)
2056     (setq data (cdr data))
2057     (while (and data
2058                 (= (gnus-data-level (car data)) (1+ level)))
2059       (push (gnus-data-number (car data)) children)
2060       (setq data (cdr data)))
2061     children))
2062
2063 (defmacro gnus-summary-skip-intangible ()
2064   "If the current article is intangible, then jump to a different article."
2065   '(let ((to (get-text-property (point) 'gnus-intangible)))
2066      (and to (gnus-summary-goto-subject to))))
2067
2068 (defmacro gnus-summary-article-intangible-p ()
2069   "Say whether this article is intangible or not."
2070   '(get-text-property (point) 'gnus-intangible))
2071
2072 (defun gnus-article-read-p (article)
2073   "Say whether ARTICLE is read or not."
2074   (not (or (memq article gnus-newsgroup-marked)
2075            (memq article gnus-newsgroup-unreads)
2076            (memq article gnus-newsgroup-unselected)
2077            (memq article gnus-newsgroup-dormant))))
2078
2079 ;; Some summary mode macros.
2080
2081 (defmacro gnus-summary-article-number ()
2082   "The article number of the article on the current line.
2083 If there isn's an article number here, then we return the current
2084 article number."
2085   '(progn
2086      (gnus-summary-skip-intangible)
2087      (or (get-text-property (point) 'gnus-number)
2088          (gnus-summary-last-subject))))
2089
2090 (defmacro gnus-summary-article-header (&optional number)
2091   "Return the header of article NUMBER."
2092   `(gnus-data-header (gnus-data-find
2093                       ,(or number '(gnus-summary-article-number)))))
2094
2095 (defmacro gnus-summary-thread-level (&optional number)
2096   "Return the level of thread that starts with article NUMBER."
2097   `(if (and (eq gnus-summary-make-false-root 'dummy)
2098             (get-text-property (point) 'gnus-intangible))
2099        0
2100      (gnus-data-level (gnus-data-find
2101                        ,(or number '(gnus-summary-article-number))))))
2102
2103 (defmacro gnus-summary-article-mark (&optional number)
2104   "Return the mark of article NUMBER."
2105   `(gnus-data-mark (gnus-data-find
2106                     ,(or number '(gnus-summary-article-number)))))
2107
2108 (defmacro gnus-summary-article-pos (&optional number)
2109   "Return the position of the line of article NUMBER."
2110   `(gnus-data-pos (gnus-data-find
2111                    ,(or number '(gnus-summary-article-number)))))
2112
2113 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2114 (defmacro gnus-summary-article-subject (&optional number)
2115   "Return current subject string or nil if nothing."
2116   `(let ((headers
2117           ,(if number
2118                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2119              '(gnus-data-header (assq (gnus-summary-article-number)
2120                                       gnus-newsgroup-data)))))
2121      (and headers
2122           (vectorp headers)
2123           (mail-header-subject headers))))
2124
2125 (defmacro gnus-summary-article-score (&optional number)
2126   "Return current article score."
2127   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2128                   gnus-newsgroup-scored))
2129        gnus-summary-default-score 0))
2130
2131 (defun gnus-summary-article-children (&optional number)
2132   "Return a list of article numbers that are children of article NUMBER."
2133   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2134          (level (gnus-data-level (car data)))
2135          l children)
2136     (while (and (setq data (cdr data))
2137                 (> (setq l (gnus-data-level (car data))) level))
2138       (and (= (1+ level) l)
2139            (push (gnus-data-number (car data))
2140                  children)))
2141     (nreverse children)))
2142
2143 (defun gnus-summary-article-parent (&optional number)
2144   "Return the article number of the parent of article NUMBER."
2145   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2146                                     (gnus-data-list t)))
2147          (level (gnus-data-level (car data))))
2148     (if (zerop level)
2149         ()                              ; This is a root.
2150       ;; We search until we find an article with a level less than
2151       ;; this one.  That function has to be the parent.
2152       (while (and (setq data (cdr data))
2153                   (not (< (gnus-data-level (car data)) level))))
2154       (and data (gnus-data-number (car data))))))
2155
2156 (defun gnus-unread-mark-p (mark)
2157   "Say whether MARK is the unread mark."
2158   (= mark gnus-unread-mark))
2159
2160 (defun gnus-read-mark-p (mark)
2161   "Say whether MARK is one of the marks that mark as read.
2162 This is all marks except unread, ticked, dormant, and expirable."
2163   (not (or (= mark gnus-unread-mark)
2164            (= mark gnus-ticked-mark)
2165            (= mark gnus-dormant-mark)
2166            (= mark gnus-expirable-mark))))
2167
2168 (defmacro gnus-article-mark (number)
2169   "Return the MARK of article NUMBER.
2170 This macro should only be used when computing the mark the \"first\"
2171 time; i.e., when generating the summary lines.  After that,
2172 `gnus-summary-article-mark' should be used to examine the
2173 marks of articles."
2174   `(cond
2175     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2176     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2177     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2178     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2179     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2180     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2181     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2182     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2183            gnus-ancient-mark))))
2184
2185 ;; Saving hidden threads.
2186
2187 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2188 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2189
2190 (defmacro gnus-save-hidden-threads (&rest forms)
2191   "Save hidden threads, eval FORMS, and restore the hidden threads."
2192   (let ((config (make-symbol "config")))
2193     `(let ((,config (gnus-hidden-threads-configuration)))
2194        (unwind-protect
2195            (save-excursion
2196              ,@forms)
2197          (gnus-restore-hidden-threads-configuration ,config)))))
2198
2199 (defun gnus-hidden-threads-configuration ()
2200   "Return the current hidden threads configuration."
2201   (save-excursion
2202     (let (config)
2203       (goto-char (point-min))
2204       (while (search-forward "\r" nil t)
2205         (push (1- (point)) config))
2206       config)))
2207
2208 (defun gnus-restore-hidden-threads-configuration (config)
2209   "Restore hidden threads configuration from CONFIG."
2210   (let (point buffer-read-only)
2211     (while (setq point (pop config))
2212       (when (and (< point (point-max))
2213                  (goto-char point)
2214                  (= (following-char) ?\n))
2215         (subst-char-in-region point (1+ point) ?\n ?\r)))))
2216
2217 ;; Various summary mode internalish functions.
2218
2219 (defun gnus-mouse-pick-article (e)
2220   (interactive "e")
2221   (mouse-set-point e)
2222   (gnus-summary-next-page nil t))
2223
2224 (defun gnus-summary-set-display-table ()
2225   ;; Change the display table.  Odd characters have a tendency to mess
2226   ;; up nicely formatted displays - we make all possible glyphs
2227   ;; display only a single character.
2228
2229   ;; We start from the standard display table, if any.
2230   (let ((table (or (copy-sequence standard-display-table)
2231                    (make-display-table)))
2232         (i 32))
2233     ;; Nix out all the control chars...
2234     (while (>= (setq i (1- i)) 0)
2235       (aset table i [??]))
2236     ;; ... but not newline and cr, of course.  (cr is necessary for the
2237     ;; selective display).
2238     (aset table ?\n nil)
2239     (aset table ?\r nil)
2240     ;; We keep TAB as well.
2241     (aset table ?\t nil)
2242     ;; We nix out any glyphs over 126 that are not set already.
2243     (let ((i 256))
2244       (while (>= (setq i (1- i)) 127)
2245         ;; Only modify if the entry is nil.
2246         (unless (aref table i)
2247           (aset table i [??]))))
2248     (setq buffer-display-table table)))
2249
2250 (defun gnus-summary-setup-buffer (group)
2251   "Initialize summary buffer."
2252   (let ((buffer (concat "*Summary " group "*")))
2253     (if (get-buffer buffer)
2254         (progn
2255           (set-buffer buffer)
2256           (setq gnus-summary-buffer (current-buffer))
2257           (not gnus-newsgroup-prepared))
2258       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2259       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
2260       (gnus-add-current-to-buffer-list)
2261       (gnus-summary-mode group)
2262       (when gnus-carpal
2263         (gnus-carpal-setup-buffer 'summary))
2264       (unless gnus-single-article-buffer
2265         (make-local-variable 'gnus-article-buffer)
2266         (make-local-variable 'gnus-article-current)
2267         (make-local-variable 'gnus-original-article-buffer))
2268       (setq gnus-newsgroup-name group)
2269       t)))
2270
2271 (defun gnus-set-global-variables ()
2272   ;; Set the global equivalents of the summary buffer-local variables
2273   ;; to the latest values they had.  These reflect the summary buffer
2274   ;; that was in action when the last article was fetched.
2275   (when (eq major-mode 'gnus-summary-mode)
2276     (setq gnus-summary-buffer (current-buffer))
2277     (let ((name gnus-newsgroup-name)
2278           (marked gnus-newsgroup-marked)
2279           (unread gnus-newsgroup-unreads)
2280           (headers gnus-current-headers)
2281           (data gnus-newsgroup-data)
2282           (summary gnus-summary-buffer)
2283           (article-buffer gnus-article-buffer)
2284           (original gnus-original-article-buffer)
2285           (gac gnus-article-current)
2286           (reffed gnus-reffed-article-number)
2287           (score-file gnus-current-score-file))
2288       (save-excursion
2289         (set-buffer gnus-group-buffer)
2290         (setq gnus-newsgroup-name name
2291               gnus-newsgroup-marked marked
2292               gnus-newsgroup-unreads unread
2293               gnus-current-headers headers
2294               gnus-newsgroup-data data
2295               gnus-article-current gac
2296               gnus-summary-buffer summary
2297               gnus-article-buffer article-buffer
2298               gnus-original-article-buffer original
2299               gnus-reffed-article-number reffed
2300               gnus-current-score-file score-file)
2301         ;; The article buffer also has local variables.
2302         (when (gnus-buffer-live-p gnus-article-buffer)
2303           (set-buffer gnus-article-buffer)
2304           (setq gnus-summary-buffer summary))))))
2305
2306 (defun gnus-summary-article-unread-p (article)
2307   "Say whether ARTICLE is unread or not."
2308   (memq article gnus-newsgroup-unreads))
2309
2310 (defun gnus-summary-first-article-p (&optional article)
2311   "Return whether ARTICLE is the first article in the buffer."
2312   (if (not (setq article (or article (gnus-summary-article-number))))
2313       nil
2314     (eq article (caar gnus-newsgroup-data))))
2315
2316 (defun gnus-summary-last-article-p (&optional article)
2317   "Return whether ARTICLE is the last article in the buffer."
2318   (if (not (setq article (or article (gnus-summary-article-number))))
2319       t         ; All non-existent numbers are the last article.  :-)
2320     (not (cdr (gnus-data-find-list article)))))
2321
2322 (defun gnus-make-thread-indent-array ()
2323   (let ((n 200))
2324     (unless (and gnus-thread-indent-array
2325                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2326       (setq gnus-thread-indent-array (make-vector 201 "")
2327             gnus-thread-indent-array-level gnus-thread-indent-level)
2328       (while (>= n 0)
2329         (aset gnus-thread-indent-array n
2330               (make-string (* n gnus-thread-indent-level) ? ))
2331         (setq n (1- n))))))
2332
2333 (defun gnus-update-summary-mark-positions ()
2334   "Compute where the summary marks are to go."
2335   (save-excursion
2336     (when (gnus-buffer-exists-p gnus-summary-buffer)
2337       (set-buffer gnus-summary-buffer))
2338     (let ((gnus-replied-mark 129)
2339           (gnus-score-below-mark 130)
2340           (gnus-score-over-mark 130)
2341           (gnus-download-mark 131)
2342           (spec gnus-summary-line-format-spec)
2343           thread gnus-visual pos)
2344       (save-excursion
2345         (gnus-set-work-buffer)
2346         (let ((gnus-summary-line-format-spec spec)
2347               (gnus-newsgroup-downloadable '((0 . t))))
2348           (gnus-summary-insert-line
2349            [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2350           (goto-char (point-min))
2351           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2352                                              (- (point) 2)))))
2353           (goto-char (point-min))
2354           (push (cons 'replied (and (search-forward "\201" nil t)
2355                                     (- (point) 2)))
2356                 pos)
2357           (goto-char (point-min))
2358           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2359                 pos)
2360           (goto-char (point-min))
2361           (push (cons 'download
2362                       (and (search-forward "\203" nil t) (- (point) 2)))
2363                 pos)))
2364       (setq gnus-summary-mark-positions pos))))
2365
2366 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2367   "Insert a dummy root in the summary buffer."
2368   (beginning-of-line)
2369   (gnus-add-text-properties
2370    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2371    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2372
2373 (defun gnus-summary-insert-line (gnus-tmp-header
2374                                  gnus-tmp-level gnus-tmp-current
2375                                  gnus-tmp-unread gnus-tmp-replied
2376                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2377                                  &optional gnus-tmp-dummy gnus-tmp-score
2378                                  gnus-tmp-process)
2379   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2380          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2381          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2382          (gnus-tmp-score-char
2383           (if (or (null gnus-summary-default-score)
2384                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2385                       gnus-summary-zcore-fuzz))
2386               ? 
2387             (if (< gnus-tmp-score gnus-summary-default-score)
2388                 gnus-score-below-mark gnus-score-over-mark)))
2389          (gnus-tmp-replied
2390           (cond (gnus-tmp-process gnus-process-mark)
2391                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2392                  gnus-cached-mark)
2393                 (gnus-tmp-replied gnus-replied-mark)
2394                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2395                  gnus-saved-mark)
2396                 (t gnus-unread-mark)))
2397          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2398          (gnus-tmp-name
2399           (cond
2400            ((string-match "<[^>]+> *$" gnus-tmp-from)
2401             (let ((beg (match-beginning 0)))
2402               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2403                        (substring gnus-tmp-from (1+ (match-beginning 0))
2404                                   (1- (match-end 0))))
2405                   (substring gnus-tmp-from 0 beg))))
2406            ((string-match "(.+)" gnus-tmp-from)
2407             (substring gnus-tmp-from
2408                        (1+ (match-beginning 0)) (1- (match-end 0))))
2409            (t gnus-tmp-from)))
2410          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2411          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2412          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2413          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2414          (buffer-read-only nil))
2415     (when (string= gnus-tmp-name "")
2416       (setq gnus-tmp-name gnus-tmp-from))
2417     (unless (numberp gnus-tmp-lines)
2418       (setq gnus-tmp-lines 0))
2419     (gnus-put-text-property-excluding-characters-with-faces
2420      (point)
2421      (progn (eval gnus-summary-line-format-spec) (point))
2422      'gnus-number gnus-tmp-number)
2423     (when (gnus-visual-p 'summary-highlight 'highlight)
2424       (forward-line -1)
2425       (gnus-run-hooks 'gnus-summary-update-hook)
2426       (forward-line 1))))
2427
2428 (defun gnus-summary-update-line (&optional dont-update)
2429   ;; Update summary line after change.
2430   (when (and gnus-summary-default-score
2431              (not gnus-summary-inhibit-highlight))
2432     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2433            (article (gnus-summary-article-number))
2434            (score (gnus-summary-article-score article)))
2435       (unless dont-update
2436         (if (and gnus-summary-mark-below
2437                  (< (gnus-summary-article-score)
2438                     gnus-summary-mark-below))
2439             ;; This article has a low score, so we mark it as read.
2440             (when (memq article gnus-newsgroup-unreads)
2441               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2442           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2443             ;; This article was previously marked as read on account
2444             ;; of a low score, but now it has risen, so we mark it as
2445             ;; unread.
2446             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2447         (gnus-summary-update-mark
2448          (if (or (null gnus-summary-default-score)
2449                  (<= (abs (- score gnus-summary-default-score))
2450                      gnus-summary-zcore-fuzz))
2451              ? 
2452            (if (< score gnus-summary-default-score)
2453                gnus-score-below-mark gnus-score-over-mark))
2454          'score))
2455       ;; Do visual highlighting.
2456       (when (gnus-visual-p 'summary-highlight 'highlight)
2457         (gnus-run-hooks 'gnus-summary-update-hook)))))
2458
2459 (defvar gnus-tmp-new-adopts nil)
2460
2461 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2462   "Return the number of articles in THREAD.
2463 This may be 0 in some cases -- if none of the articles in
2464 the thread are to be displayed."
2465   (let* ((number
2466           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2467           (cond
2468            ((not (listp thread))
2469             1)
2470            ((and (consp thread) (cdr thread))
2471             (apply
2472              '+ 1 (mapcar
2473                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2474            ((null thread)
2475             1)
2476            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2477             1)
2478            (t 0))))
2479     (when (and level (zerop level) gnus-tmp-new-adopts)
2480       (incf number
2481             (apply '+ (mapcar
2482                        'gnus-summary-number-of-articles-in-thread
2483                        gnus-tmp-new-adopts))))
2484     (if char
2485         (if (> number 1) gnus-not-empty-thread-mark
2486           gnus-empty-thread-mark)
2487       number)))
2488
2489 (defun gnus-summary-set-local-parameters (group)
2490   "Go through the local params of GROUP and set all variable specs in that list."
2491   (let ((params (gnus-group-find-parameter group))
2492         elem)
2493     (while params
2494       (setq elem (car params)
2495             params (cdr params))
2496       (and (consp elem)                 ; Has to be a cons.
2497            (consp (cdr elem))           ; The cdr has to be a list.
2498            (symbolp (car elem))         ; Has to be a symbol in there.
2499            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2500            (ignore-errors               ; So we set it.
2501              (make-local-variable (car elem))
2502              (set (car elem) (eval (nth 1 elem))))))))
2503
2504 (defun gnus-summary-read-group (group &optional show-all no-article
2505                                       kill-buffer no-display backward)
2506   "Start reading news in newsgroup GROUP.
2507 If SHOW-ALL is non-nil, already read articles are also listed.
2508 If NO-ARTICLE is non-nil, no article is selected initially.
2509 If NO-DISPLAY, don't generate a summary buffer."
2510   (let (result)
2511     (while (and group
2512                 (null (setq result
2513                             (let ((gnus-auto-select-next nil))
2514                               (or (gnus-summary-read-group-1
2515                                    group show-all no-article
2516                                    kill-buffer no-display)
2517                                   (setq show-all nil)))))
2518                 (eq gnus-auto-select-next 'quietly))
2519       (set-buffer gnus-group-buffer)
2520       ;; The entry function called above goes to the next
2521       ;; group automatically, so we go two groups back
2522       ;; if we are searching for the previous group.
2523       (when backward
2524         (gnus-group-prev-unread-group 2))
2525       (if (not (equal group (gnus-group-group-name)))
2526           (setq group (gnus-group-group-name))
2527         (setq group nil)))
2528     result))
2529
2530 (defun gnus-summary-read-group-1 (group show-all no-article
2531                                         kill-buffer no-display)
2532   ;; Killed foreign groups can't be entered.
2533   (when (and (not (gnus-group-native-p group))
2534              (not (gnus-gethash group gnus-newsrc-hashtb)))
2535     (error "Dead non-native groups can't be entered"))
2536   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2537   (let* ((new-group (gnus-summary-setup-buffer group))
2538          (quit-config (gnus-group-quit-config group))
2539          (did-select (and new-group (gnus-select-newsgroup group show-all))))
2540     (cond
2541      ;; This summary buffer exists already, so we just select it.
2542      ((not new-group)
2543       (gnus-set-global-variables)
2544       (when kill-buffer
2545         (gnus-kill-or-deaden-summary kill-buffer))
2546       (gnus-configure-windows 'summary 'force)
2547       (gnus-set-mode-line 'summary)
2548       (gnus-summary-position-point)
2549       (message "")
2550       t)
2551      ;; We couldn't select this group.
2552      ((null did-select)
2553       (when (and (eq major-mode 'gnus-summary-mode)
2554                  (not (equal (current-buffer) kill-buffer)))
2555         (kill-buffer (current-buffer))
2556         (if (not quit-config)
2557             (progn
2558               ;; Update the info -- marks might need to be removed,
2559               ;; for instance.
2560               (gnus-summary-update-info)
2561               (set-buffer gnus-group-buffer)
2562               (gnus-group-jump-to-group group)
2563               (gnus-group-next-unread-group 1))
2564           (gnus-handle-ephemeral-exit quit-config)))
2565       (gnus-message 3 "Can't select group")
2566       nil)
2567      ;; The user did a `C-g' while prompting for number of articles,
2568      ;; so we exit this group.
2569      ((eq did-select 'quit)
2570       (and (eq major-mode 'gnus-summary-mode)
2571            (not (equal (current-buffer) kill-buffer))
2572            (kill-buffer (current-buffer)))
2573       (when kill-buffer
2574         (gnus-kill-or-deaden-summary kill-buffer))
2575       (if (not quit-config)
2576           (progn
2577             (set-buffer gnus-group-buffer)
2578             (gnus-group-jump-to-group group)
2579             (gnus-group-next-unread-group 1)
2580             (gnus-configure-windows 'group 'force))
2581         (gnus-handle-ephemeral-exit quit-config))
2582       ;; Finally signal the quit.
2583       (signal 'quit nil))
2584      ;; The group was successfully selected.
2585      (t
2586       (gnus-set-global-variables)
2587       ;; Save the active value in effect when the group was entered.
2588       (setq gnus-newsgroup-active
2589             (gnus-copy-sequence
2590              (gnus-active gnus-newsgroup-name)))
2591       ;; You can change the summary buffer in some way with this hook.
2592       (gnus-run-hooks 'gnus-select-group-hook)
2593       ;; Set any local variables in the group parameters.
2594       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2595       (gnus-update-format-specifications
2596        nil 'summary 'summary-mode 'summary-dummy)
2597       ;; Do score processing.
2598       (when gnus-use-scoring
2599         (gnus-possibly-score-headers))
2600       ;; Check whether to fill in the gaps in the threads.
2601       (when gnus-build-sparse-threads
2602         (gnus-build-sparse-threads))
2603       ;; Find the initial limit.
2604       (if gnus-show-threads
2605           (if show-all
2606               (let ((gnus-newsgroup-dormant nil))
2607                 (gnus-summary-initial-limit show-all))
2608             (gnus-summary-initial-limit show-all))
2609         (setq gnus-newsgroup-limit
2610               (mapcar
2611                (lambda (header) (mail-header-number header))
2612                gnus-newsgroup-headers)))
2613       ;; Generate the summary buffer.
2614       (unless no-display
2615         (gnus-summary-prepare))
2616       (when gnus-use-trees
2617         (gnus-tree-open group)
2618         (setq gnus-summary-highlight-line-function
2619               'gnus-tree-highlight-article))
2620       ;; If the summary buffer is empty, but there are some low-scored
2621       ;; articles or some excluded dormants, we include these in the
2622       ;; buffer.
2623       (when (and (zerop (buffer-size))
2624                  (not no-display))
2625         (cond (gnus-newsgroup-dormant
2626                (gnus-summary-limit-include-dormant))
2627               ((and gnus-newsgroup-scored show-all)
2628                (gnus-summary-limit-include-expunged t))))
2629       ;; Function `gnus-apply-kill-file' must be called in this hook.
2630       (gnus-run-hooks 'gnus-apply-kill-hook)
2631       (if (and (zerop (buffer-size))
2632                (not no-display))
2633           (progn
2634             ;; This newsgroup is empty.
2635             (gnus-summary-catchup-and-exit nil t)
2636             (gnus-message 6 "No unread news")
2637             (when kill-buffer
2638               (gnus-kill-or-deaden-summary kill-buffer))
2639             ;; Return nil from this function.
2640             nil)
2641         ;; Hide conversation thread subtrees.  We cannot do this in
2642         ;; gnus-summary-prepare-hook since kill processing may not
2643         ;; work with hidden articles.
2644         (and gnus-show-threads
2645              gnus-thread-hide-subtree
2646              (gnus-summary-hide-all-threads))
2647         (when kill-buffer
2648           (gnus-kill-or-deaden-summary kill-buffer))
2649         ;; Show first unread article if requested.
2650         (if (and (not no-article)
2651                  (not no-display)
2652                  gnus-newsgroup-unreads
2653                  gnus-auto-select-first)
2654             (unless (if (eq gnus-auto-select-first 'best)
2655                         (gnus-summary-best-unread-article)
2656                       (gnus-summary-first-unread-article))
2657               (gnus-configure-windows 'summary))
2658           ;; Don't select any articles, just move point to the first
2659           ;; article in the group.
2660           (goto-char (point-min))
2661           (gnus-summary-position-point)
2662           (gnus-configure-windows 'summary 'force)
2663           (gnus-set-mode-line 'summary))        
2664         (when (get-buffer-window gnus-group-buffer t)
2665           ;; Gotta use windows, because recenter does weird stuff if
2666           ;; the current buffer ain't the displayed window.
2667           (let ((owin (selected-window)))
2668             (select-window (get-buffer-window gnus-group-buffer t))
2669             (when (gnus-group-goto-group group)
2670               (recenter))
2671             (select-window owin)))
2672         ;; Mark this buffer as "prepared".
2673         (setq gnus-newsgroup-prepared t)
2674         (gnus-run-hooks 'gnus-summary-prepared-hook)
2675         t)))))
2676
2677 (defun gnus-summary-prepare ()
2678   "Generate the summary buffer."
2679   (interactive)
2680   (let ((buffer-read-only nil))
2681     (erase-buffer)
2682     (setq gnus-newsgroup-data nil
2683           gnus-newsgroup-data-reverse nil)
2684     (gnus-run-hooks 'gnus-summary-generate-hook)
2685     ;; Generate the buffer, either with threads or without.
2686     (when gnus-newsgroup-headers
2687       (gnus-summary-prepare-threads
2688        (if gnus-show-threads
2689            (gnus-sort-gathered-threads
2690             (funcall gnus-summary-thread-gathering-function
2691                      (gnus-sort-threads
2692                       (gnus-cut-threads (gnus-make-threads)))))
2693          ;; Unthreaded display.
2694          (gnus-sort-articles gnus-newsgroup-headers))))
2695     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2696     ;; Call hooks for modifying summary buffer.
2697     (goto-char (point-min))
2698     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2699
2700 (defsubst gnus-general-simplify-subject (subject)
2701   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2702   (setq subject
2703         (cond
2704          ;; Truncate the subject.
2705          (gnus-simplify-subject-functions
2706           (gnus-map-function gnus-simplify-subject-functions subject))
2707          ((numberp gnus-summary-gather-subject-limit)
2708           (setq subject (gnus-simplify-subject-re subject))
2709           (if (> (length subject) gnus-summary-gather-subject-limit)
2710               (substring subject 0 gnus-summary-gather-subject-limit)
2711             subject))
2712          ;; Fuzzily simplify it.
2713          ((eq 'fuzzy gnus-summary-gather-subject-limit)
2714           (gnus-simplify-subject-fuzzy subject))
2715          ;; Just remove the leading "Re:".
2716          (t
2717           (gnus-simplify-subject-re subject))))
2718
2719   (if (and gnus-summary-gather-exclude-subject
2720            (string-match gnus-summary-gather-exclude-subject subject))
2721       nil                               ; This article shouldn't be gathered
2722     subject))
2723
2724 (defun gnus-summary-simplify-subject-query ()
2725   "Query where the respool algorithm would put this article."
2726   (interactive)
2727   (gnus-summary-select-article)
2728   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
2729
2730 (defun gnus-gather-threads-by-subject (threads)
2731   "Gather threads by looking at Subject headers."
2732   (if (not gnus-summary-make-false-root)
2733       threads
2734     (let ((hashtb (gnus-make-hashtable 1024))
2735           (prev threads)
2736           (result threads)
2737           subject hthread whole-subject)
2738       (while threads
2739         (setq subject (gnus-general-simplify-subject
2740                        (setq whole-subject (mail-header-subject
2741                                             (caar threads)))))
2742         (when subject
2743           (if (setq hthread (gnus-gethash subject hashtb))
2744               (progn
2745                 ;; We enter a dummy root into the thread, if we
2746                 ;; haven't done that already.
2747                 (unless (stringp (caar hthread))
2748                   (setcar hthread (list whole-subject (car hthread))))
2749                 ;; We add this new gathered thread to this gathered
2750                 ;; thread.
2751                 (setcdr (car hthread)
2752                         (nconc (cdar hthread) (list (car threads))))
2753                 ;; Remove it from the list of threads.
2754                 (setcdr prev (cdr threads))
2755                 (setq threads prev))
2756             ;; Enter this thread into the hash table.
2757             (gnus-sethash subject threads hashtb)))
2758         (setq prev threads)
2759         (setq threads (cdr threads)))
2760       result)))
2761
2762 (defun gnus-gather-threads-by-references (threads)
2763   "Gather threads by looking at References headers."
2764   (let ((idhashtb (gnus-make-hashtable 1024))
2765         (thhashtb (gnus-make-hashtable 1024))
2766         (prev threads)
2767         (result threads)
2768         ids references id gthread gid entered ref)
2769     (while threads
2770       (when (setq references (mail-header-references (caar threads)))
2771         (setq id (mail-header-id (caar threads))
2772               ids (gnus-split-references references)
2773               entered nil)
2774         (while (setq ref (pop ids))
2775           (setq ids (delete ref ids))
2776           (if (not (setq gid (gnus-gethash ref idhashtb)))
2777               (progn
2778                 (gnus-sethash ref id idhashtb)
2779                 (gnus-sethash id threads thhashtb))
2780             (setq gthread (gnus-gethash gid thhashtb))
2781             (unless entered
2782               ;; We enter a dummy root into the thread, if we
2783               ;; haven't done that already.
2784               (unless (stringp (caar gthread))
2785                 (setcar gthread (list (mail-header-subject (caar gthread))
2786                                       (car gthread))))
2787               ;; We add this new gathered thread to this gathered
2788               ;; thread.
2789               (setcdr (car gthread)
2790                       (nconc (cdar gthread) (list (car threads)))))
2791             ;; Add it into the thread hash table.
2792             (gnus-sethash id gthread thhashtb)
2793             (setq entered t)
2794             ;; Remove it from the list of threads.
2795             (setcdr prev (cdr threads))
2796             (setq threads prev))))
2797       (setq prev threads)
2798       (setq threads (cdr threads)))
2799     result))
2800
2801 (defun gnus-sort-gathered-threads (threads)
2802   "Sort subtreads inside each gathered thread by article number."
2803   (let ((result threads))
2804     (while threads
2805       (when (stringp (caar threads))
2806         (setcdr (car threads)
2807                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
2808       (setq threads (cdr threads)))
2809     result))
2810
2811 (defun gnus-thread-loop-p (root thread)
2812   "Say whether ROOT is in THREAD."
2813   (let ((stack (list thread))
2814         (infloop 0)
2815         th)
2816     (while (setq thread (pop stack))
2817       (setq th (cdr thread))
2818       (while (and th
2819                   (not (eq (caar th) root)))
2820         (pop th))
2821       (if th
2822           ;; We have found a loop.
2823           (let (ref-dep)
2824             (setcdr thread (delq (car th) (cdr thread)))
2825             (if (boundp (setq ref-dep (intern "none"
2826                                               gnus-newsgroup-dependencies)))
2827                 (setcdr (symbol-value ref-dep)
2828                         (nconc (cdr (symbol-value ref-dep))
2829                                (list (car th))))
2830               (set ref-dep (list nil (car th))))
2831             (setq infloop 1
2832                   stack nil))
2833         ;; Push all the subthreads onto the stack.
2834         (push (cdr thread) stack)))
2835     infloop))
2836
2837 (defun gnus-make-threads ()
2838   "Go through the dependency hashtb and find the roots.  Return all threads."
2839   (let (threads)
2840     (while (catch 'infloop
2841              (mapatoms
2842               (lambda (refs)
2843                 ;; Deal with self-referencing References loops.
2844                 (when (and (car (symbol-value refs))
2845                            (not (zerop
2846                                  (apply
2847                                   '+
2848                                   (mapcar
2849                                    (lambda (thread)
2850                                      (gnus-thread-loop-p
2851                                       (car (symbol-value refs)) thread))
2852                                    (cdr (symbol-value refs)))))))
2853                   (setq threads nil)
2854                   (throw 'infloop t))
2855                 (unless (car (symbol-value refs))
2856                   ;; These threads do not refer back to any other articles,
2857                   ;; so they're roots.
2858                   (setq threads (append (cdr (symbol-value refs)) threads))))
2859               gnus-newsgroup-dependencies)))
2860     threads))
2861
2862 ;; Build the thread tree.
2863 (defun gnus-dependencies-add-header (header dependencies force-new)
2864   "Enter HEADER into the DEPENDENCIES table if it is not already there.
2865
2866 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
2867 if it was already present.
2868
2869 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
2870 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
2871 Message-IDs will be renamed be renamed to a unique Message-ID before
2872 being entered.
2873
2874 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
2875   (let* ((id (mail-header-id header))
2876          (id-dep (and id (intern id dependencies)))
2877          ref ref-dep ref-header)
2878     ;; Enter this `header' in the `dependencies' table.
2879     (cond
2880      ((not id-dep)
2881       (setq header nil))
2882      ;; The first two cases do the normal part: enter a new `header'
2883      ;; in the `dependencies' table.
2884      ((not (boundp id-dep))
2885       (set id-dep (list header)))
2886      ((null (car (symbol-value id-dep)))
2887       (setcar (symbol-value id-dep) header))
2888
2889      ;; From here the `header' was already present in the
2890      ;; `dependencies' table.
2891      (force-new
2892       ;; Overrides an existing entry;
2893       ;; just set the header part of the entry.
2894       (setcar (symbol-value id-dep) header))
2895
2896      ;; Renames the existing `header' to a unique Message-ID.
2897      ((not gnus-summary-ignore-duplicates)
2898       ;; An article with this Message-ID has already been seen.
2899       ;; We rename the Message-ID.
2900       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
2901            (list header))
2902       (mail-header-set-id header id))
2903
2904      ;; The last case ignores an existing entry, except it adds any
2905      ;; additional Xrefs (in case the two articles came from different
2906      ;; servers.
2907      ;; Also sets `header' to `nil' meaning that the `dependencies'
2908      ;; table was *not* modified.
2909      (t
2910       (mail-header-set-xref
2911        (car (symbol-value id-dep))
2912        (concat (or (mail-header-xref (car (symbol-value id-dep)))
2913                    "")
2914                (or (mail-header-xref header) "")))
2915       (setq header nil)))
2916
2917     (when header
2918       ;; First check if that we are not creating a References loop.
2919       (setq ref (gnus-parent-id (mail-header-references header)))
2920       (while (and ref
2921                   (setq ref-dep (intern-soft ref dependencies))
2922                   (boundp ref-dep)
2923                   (setq ref-header (car (symbol-value ref-dep))))
2924         (if (string= id ref)
2925             ;; Yuk!  This is a reference loop.  Make the article be a
2926             ;; root article.
2927             (progn
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                 (not (zerop generation))
3262                 (setq references (mail-header-references headers)))
3263       (setq headers (if (and references
3264                              (setq parent (gnus-parent-id references)))
3265                         (car (gnus-id-to-thread parent))
3266                       nil))
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-excluding-characters-with-faces
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       (gnus-summary-position-point)
5419       article)))
5420
5421 ;; Walking around summary lines with displaying articles.
5422
5423 (defun gnus-summary-expand-window (&optional arg)
5424   "Make the summary buffer take up the entire Emacs frame.
5425 Given a prefix, will force an `article' buffer configuration."
5426   (interactive "P")
5427   (if arg
5428       (gnus-configure-windows 'article 'force)
5429     (gnus-configure-windows 'summary 'force)))
5430
5431 (defun gnus-summary-display-article (article &optional all-header)
5432   "Display ARTICLE in article buffer."
5433   (gnus-set-global-variables)
5434   (if (null article)
5435       nil
5436     (prog1
5437         (if gnus-summary-display-article-function
5438             (funcall gnus-summary-display-article-function article all-header)
5439           (gnus-article-prepare article all-header))
5440       (gnus-run-hooks 'gnus-select-article-hook)
5441       (when (and gnus-current-article
5442                  (not (zerop gnus-current-article)))
5443         (gnus-summary-goto-subject gnus-current-article))
5444       (gnus-summary-recenter)
5445       (when (and gnus-use-trees gnus-show-threads)
5446         (gnus-possibly-generate-tree article)
5447         (gnus-highlight-selected-tree article))
5448       ;; Successfully display article.
5449       (gnus-article-set-window-start
5450        (cdr (assq article gnus-newsgroup-bookmarks))))))
5451
5452 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5453   "Select the current article.
5454 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5455 non-nil, the article will be re-fetched even if it already present in
5456 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5457 be displayed."
5458   ;; Make sure we are in the summary buffer to work around bbdb bug.
5459   (unless (eq major-mode 'gnus-summary-mode)
5460     (set-buffer gnus-summary-buffer))
5461   (let ((article (or article (gnus-summary-article-number)))
5462         (all-headers (not (not all-headers))) ;Must be T or NIL.
5463         gnus-summary-display-article-function
5464         did)
5465     (and (not pseudo)
5466          (gnus-summary-article-pseudo-p article)
5467          (error "This is a pseudo-article"))
5468     (prog1
5469         (save-excursion
5470           (set-buffer gnus-summary-buffer)
5471           (if (or (and gnus-single-article-buffer
5472                        (or (null gnus-current-article)
5473                            (null gnus-article-current)
5474                            (null (get-buffer gnus-article-buffer))
5475                            (not (eq article (cdr gnus-article-current)))
5476                            (not (equal (car gnus-article-current)
5477                                        gnus-newsgroup-name))))
5478                   (and (not gnus-single-article-buffer)
5479                        (or (null gnus-current-article)
5480                            (not (eq gnus-current-article article))))
5481                   force)
5482               ;; The requested article is different from the current article.
5483               (prog1
5484                   (gnus-summary-display-article article all-headers)
5485                 (setq did article))
5486             (when (or all-headers gnus-show-all-headers)
5487               (gnus-article-show-all-headers))
5488             'old))
5489       (when did
5490         (gnus-article-set-window-start
5491          (cdr (assq article gnus-newsgroup-bookmarks)))))))
5492
5493 (defun gnus-summary-set-current-mark (&optional current-mark)
5494   "Obsolete function."
5495   nil)
5496
5497 (defun gnus-summary-next-article (&optional unread subject backward push)
5498   "Select the next article.
5499 If UNREAD, only unread articles are selected.
5500 If SUBJECT, only articles with SUBJECT are selected.
5501 If BACKWARD, the previous article is selected instead of the next."
5502   (interactive "P")
5503   (cond
5504    ;; Is there such an article?
5505    ((and (gnus-summary-search-forward unread subject backward)
5506          (or (gnus-summary-display-article (gnus-summary-article-number))
5507              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5508     (gnus-summary-position-point))
5509    ;; If not, we try the first unread, if that is wanted.
5510    ((and subject
5511          gnus-auto-select-same
5512          (gnus-summary-first-unread-article))
5513     (gnus-summary-position-point)
5514     (gnus-message 6 "Wrapped"))
5515    ;; Try to get next/previous article not displayed in this group.
5516    ((and gnus-auto-extend-newsgroup
5517          (not unread) (not subject))
5518     (gnus-summary-goto-article
5519      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5520      nil (count-lines (point-min) (point))))
5521    ;; Go to next/previous group.
5522    (t
5523     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5524       (gnus-summary-jump-to-group gnus-newsgroup-name))
5525     (let ((cmd last-command-char)
5526           (point
5527            (save-excursion
5528              (set-buffer gnus-group-buffer)
5529              (point)))
5530           (group
5531            (if (eq gnus-keep-same-level 'best)
5532                (gnus-summary-best-group gnus-newsgroup-name)
5533              (gnus-summary-search-group backward gnus-keep-same-level))))
5534       ;; For some reason, the group window gets selected.  We change
5535       ;; it back.
5536       (select-window (get-buffer-window (current-buffer)))
5537       ;; Select next unread newsgroup automagically.
5538       (cond
5539        ((or (not gnus-auto-select-next)
5540             (not cmd))
5541         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5542        ((or (eq gnus-auto-select-next 'quietly)
5543             (and (eq gnus-auto-select-next 'slightly-quietly)
5544                  push)
5545             (and (eq gnus-auto-select-next 'almost-quietly)
5546                  (gnus-summary-last-article-p)))
5547         ;; Select quietly.
5548         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5549             (gnus-summary-exit)
5550           (gnus-message 7 "No more%s articles (%s)..."
5551                         (if unread " unread" "")
5552                         (if group (concat "selecting " group)
5553                           "exiting"))
5554           (gnus-summary-next-group nil group backward)))
5555        (t
5556         (when (gnus-key-press-event-p last-input-event)
5557           (gnus-summary-walk-group-buffer
5558            gnus-newsgroup-name cmd unread backward point))))))))
5559
5560 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
5561   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
5562                       (?\C-p (gnus-group-prev-unread-group 1))))
5563         (cursor-in-echo-area t)
5564         keve key group ended)
5565     (save-excursion
5566       (set-buffer gnus-group-buffer)
5567       (goto-char start)
5568       (setq group
5569             (if (eq gnus-keep-same-level 'best)
5570                 (gnus-summary-best-group gnus-newsgroup-name)
5571               (gnus-summary-search-group backward gnus-keep-same-level))))
5572     (while (not ended)
5573       (gnus-message
5574        5 "No more%s articles%s" (if unread " unread" "")
5575        (if (and group
5576                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
5577            (format " (Type %s for %s [%s])"
5578                    (single-key-description cmd) group
5579                    (car (gnus-gethash group gnus-newsrc-hashtb)))
5580          (format " (Type %s to exit %s)"
5581                  (single-key-description cmd)
5582                  gnus-newsgroup-name)))
5583       ;; Confirm auto selection.
5584       (setq key (car (setq keve (gnus-read-event-char))))
5585       (setq ended t)
5586       (cond
5587        ((assq key keystrokes)
5588         (let ((obuf (current-buffer)))
5589           (switch-to-buffer gnus-group-buffer)
5590           (when group
5591             (gnus-group-jump-to-group group))
5592           (eval (cadr (assq key keystrokes)))
5593           (setq group (gnus-group-group-name))
5594           (switch-to-buffer obuf))
5595         (setq ended nil))
5596        ((equal key cmd)
5597         (if (or (not group)
5598                 (gnus-ephemeral-group-p gnus-newsgroup-name))
5599             (gnus-summary-exit)
5600           (gnus-summary-next-group nil group backward)))
5601        (t
5602         (push (cdr keve) unread-command-events))))))
5603
5604 (defun gnus-summary-next-unread-article ()
5605   "Select unread article after current one."
5606   (interactive)
5607   (gnus-summary-next-article
5608    (or (not (eq gnus-summary-goto-unread 'never))
5609        (gnus-summary-last-article-p (gnus-summary-article-number)))
5610    (and gnus-auto-select-same
5611         (gnus-summary-article-subject))))
5612
5613 (defun gnus-summary-prev-article (&optional unread subject)
5614   "Select the article after the current one.
5615 If UNREAD is non-nil, only unread articles are selected."
5616   (interactive "P")
5617   (gnus-summary-next-article unread subject t))
5618
5619 (defun gnus-summary-prev-unread-article ()
5620   "Select unread article before current one."
5621   (interactive)
5622   (gnus-summary-prev-article
5623    (or (not (eq gnus-summary-goto-unread 'never))
5624        (gnus-summary-first-article-p (gnus-summary-article-number)))
5625    (and gnus-auto-select-same
5626         (gnus-summary-article-subject))))
5627
5628 (defun gnus-summary-next-page (&optional lines circular)
5629   "Show next page of the selected article.
5630 If at the end of the current article, select the next article.
5631 LINES says how many lines should be scrolled up.
5632
5633 If CIRCULAR is non-nil, go to the start of the article instead of
5634 selecting the next article when reaching the end of the current
5635 article."
5636   (interactive "P")
5637   (setq gnus-summary-buffer (current-buffer))
5638   (gnus-set-global-variables)
5639   (let ((article (gnus-summary-article-number))
5640         (article-window (get-buffer-window gnus-article-buffer t))
5641         endp)
5642     ;; If the buffer is empty, we have no article.
5643     (unless article
5644       (error "No article to select"))
5645     (gnus-configure-windows 'article)
5646     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
5647         (if (and (eq gnus-summary-goto-unread 'never)
5648                  (not (gnus-summary-last-article-p article)))
5649             (gnus-summary-next-article)
5650           (gnus-summary-next-unread-article))
5651       (if (or (null gnus-current-article)
5652               (null gnus-article-current)
5653               (/= article (cdr gnus-article-current))
5654               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5655           ;; Selected subject is different from current article's.
5656           (gnus-summary-display-article article)
5657         (when article-window
5658           (gnus-eval-in-buffer-window gnus-article-buffer
5659             (setq endp (gnus-article-next-page lines)))
5660           (when endp
5661             (cond (circular
5662                    (gnus-summary-beginning-of-article))
5663                   (lines
5664                    (gnus-message 3 "End of message"))
5665                   ((null lines)
5666                    (if (and (eq gnus-summary-goto-unread 'never)
5667                             (not (gnus-summary-last-article-p article)))
5668                        (gnus-summary-next-article)
5669                      (gnus-summary-next-unread-article))))))))
5670     (gnus-summary-recenter)
5671     (gnus-summary-position-point)))
5672
5673 (defun gnus-summary-prev-page (&optional lines move)
5674   "Show previous page of selected article.
5675 Argument LINES specifies lines to be scrolled down.
5676 If MOVE, move to the previous unread article if point is at
5677 the beginning of the buffer."
5678   (interactive "P")
5679   (let ((article (gnus-summary-article-number))
5680         (article-window (get-buffer-window gnus-article-buffer t))
5681         endp)
5682     (gnus-configure-windows 'article)
5683     (if (or (null gnus-current-article)
5684             (null gnus-article-current)
5685             (/= article (cdr gnus-article-current))
5686             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
5687         ;; Selected subject is different from current article's.
5688         (gnus-summary-display-article article)
5689       (gnus-summary-recenter)
5690       (when article-window
5691         (gnus-eval-in-buffer-window gnus-article-buffer
5692           (setq endp (gnus-article-prev-page lines)))
5693         (when (and move endp)
5694           (cond (lines
5695                  (gnus-message 3 "Beginning of message"))
5696                 ((null lines)
5697                  (if (and (eq gnus-summary-goto-unread 'never)
5698                           (not (gnus-summary-first-article-p article)))
5699                      (gnus-summary-prev-article)
5700                    (gnus-summary-prev-unread-article))))))))
5701   (gnus-summary-position-point))
5702
5703 (defun gnus-summary-prev-page-or-article (&optional lines)
5704   "Show previous page of selected article.
5705 Argument LINES specifies lines to be scrolled down.
5706 If at the beginning of the article, go to the next article."
5707   (interactive "P")
5708   (gnus-summary-prev-page lines t))
5709
5710 (defun gnus-summary-scroll-up (lines)
5711   "Scroll up (or down) one line current article.
5712 Argument LINES specifies lines to be scrolled up (or down if negative)."
5713   (interactive "p")
5714   (gnus-configure-windows 'article)
5715   (gnus-summary-show-thread)
5716   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
5717     (gnus-eval-in-buffer-window gnus-article-buffer
5718       (cond ((> lines 0)
5719              (when (gnus-article-next-page lines)
5720                (gnus-message 3 "End of message")))
5721             ((< lines 0)
5722              (gnus-article-prev-page (- lines))))))
5723   (gnus-summary-recenter)
5724   (gnus-summary-position-point))
5725
5726 (defun gnus-summary-next-same-subject ()
5727   "Select next article which has the same subject as current one."
5728   (interactive)
5729   (gnus-summary-next-article nil (gnus-summary-article-subject)))
5730
5731 (defun gnus-summary-prev-same-subject ()
5732   "Select previous article which has the same subject as current one."
5733   (interactive)
5734   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
5735
5736 (defun gnus-summary-next-unread-same-subject ()
5737   "Select next unread article which has the same subject as current one."
5738   (interactive)
5739   (gnus-summary-next-article t (gnus-summary-article-subject)))
5740
5741 (defun gnus-summary-prev-unread-same-subject ()
5742   "Select previous unread article which has the same subject as current one."
5743   (interactive)
5744   (gnus-summary-prev-article t (gnus-summary-article-subject)))
5745
5746 (defun gnus-summary-first-unread-article ()
5747   "Select the first unread article.
5748 Return nil if there are no unread articles."
5749   (interactive)
5750   (prog1
5751       (when (gnus-summary-first-subject t)
5752         (gnus-summary-show-thread)
5753         (gnus-summary-first-subject t)
5754         (gnus-summary-display-article (gnus-summary-article-number)))
5755     (gnus-summary-position-point)))
5756
5757 (defun gnus-summary-first-article ()
5758   "Select the first article.
5759 Return nil if there are no articles."
5760   (interactive)
5761   (prog1
5762       (when (gnus-summary-first-subject)
5763       (gnus-summary-show-thread)
5764       (gnus-summary-first-subject)
5765       (gnus-summary-display-article (gnus-summary-article-number)))
5766     (gnus-summary-position-point)))
5767
5768 (defun gnus-summary-best-unread-article ()
5769   "Select the unread article with the highest score."
5770   (interactive)
5771   (let ((best -1000000)
5772         (data gnus-newsgroup-data)
5773         article score)
5774     (while data
5775       (and (gnus-data-unread-p (car data))
5776            (> (setq score
5777                     (gnus-summary-article-score (gnus-data-number (car data))))
5778               best)
5779            (setq best score
5780                  article (gnus-data-number (car data))))
5781       (setq data (cdr data)))
5782     (prog1
5783         (if article
5784             (gnus-summary-goto-article article)
5785           (error "No unread articles"))
5786       (gnus-summary-position-point))))
5787
5788 (defun gnus-summary-last-subject ()
5789   "Go to the last displayed subject line in the group."
5790   (let ((article (gnus-data-number (car (gnus-data-list t)))))
5791     (when article
5792       (gnus-summary-goto-subject article))))
5793
5794 (defun gnus-summary-goto-article (article &optional all-headers force)
5795   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
5796 If ALL-HEADERS is non-nil, no header lines are hidden.
5797 If FORCE, go to the article even if it isn't displayed.  If FORCE
5798 is a number, it is the line the article is to be displayed on."
5799   (interactive
5800    (list
5801     (completing-read
5802      "Article number or Message-ID: "
5803      (mapcar (lambda (number) (list (int-to-string number)))
5804              gnus-newsgroup-limit))
5805     current-prefix-arg
5806     t))
5807   (prog1
5808       (if (and (stringp article)
5809                (string-match "@" article))
5810           (gnus-summary-refer-article article)
5811         (when (stringp article)
5812           (setq article (string-to-number article)))
5813         (if (gnus-summary-goto-subject article force)
5814             (gnus-summary-display-article article all-headers)
5815           (gnus-message 4 "Couldn't go to article %s" article) nil))
5816     (gnus-summary-position-point)))
5817
5818 (defun gnus-summary-goto-last-article ()
5819   "Go to the previously read article."
5820   (interactive)
5821   (prog1
5822       (when gnus-last-article
5823         (gnus-summary-goto-article gnus-last-article nil t))
5824     (gnus-summary-position-point)))
5825
5826 (defun gnus-summary-pop-article (number)
5827   "Pop one article off the history and go to the previous.
5828 NUMBER articles will be popped off."
5829   (interactive "p")
5830   (let (to)
5831     (setq gnus-newsgroup-history
5832           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
5833     (if to
5834         (gnus-summary-goto-article (car to) nil t)
5835       (error "Article history empty")))
5836   (gnus-summary-position-point))
5837
5838 ;; Summary commands and functions for limiting the summary buffer.
5839
5840 (defun gnus-summary-limit-to-articles (n)
5841   "Limit the summary buffer to the next N articles.
5842 If not given a prefix, use the process marked articles instead."
5843   (interactive "P")
5844   (prog1
5845       (let ((articles (gnus-summary-work-articles n)))
5846         (setq gnus-newsgroup-processable nil)
5847         (gnus-summary-limit articles))
5848     (gnus-summary-position-point)))
5849
5850 (defun gnus-summary-pop-limit (&optional total)
5851   "Restore the previous limit.
5852 If given a prefix, remove all limits."
5853   (interactive "P")
5854   (when total
5855     (setq gnus-newsgroup-limits
5856           (list (mapcar (lambda (h) (mail-header-number h))
5857                         gnus-newsgroup-headers))))
5858   (unless gnus-newsgroup-limits
5859     (error "No limit to pop"))
5860   (prog1
5861       (gnus-summary-limit nil 'pop)
5862     (gnus-summary-position-point)))
5863
5864 (defun gnus-summary-limit-to-subject (subject &optional header)
5865   "Limit the summary buffer to articles that have subjects that match a regexp."
5866   (interactive "sLimit to subject (regexp): ")
5867   (unless header
5868     (setq header "subject"))
5869   (when (not (equal "" subject))
5870     (prog1
5871         (let ((articles (gnus-summary-find-matching
5872                          (or header "subject") subject 'all)))
5873           (unless articles
5874             (error "Found no matches for \"%s\"" subject))
5875           (gnus-summary-limit articles))
5876       (gnus-summary-position-point))))
5877
5878 (defun gnus-summary-limit-to-author (from)
5879   "Limit the summary buffer to articles that have authors that match a regexp."
5880   (interactive "sLimit to author (regexp): ")
5881   (gnus-summary-limit-to-subject from "from"))
5882
5883 (defun gnus-summary-limit-to-age (age &optional younger-p)
5884   "Limit the summary buffer to articles that are older than (or equal) AGE days.
5885 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
5886 articles that are younger than AGE days."
5887   (interactive "nTime in days: \nP")
5888   (prog1
5889       (let ((data gnus-newsgroup-data)
5890             (cutoff (nnmail-days-to-time age))
5891             articles d date is-younger)
5892         (while (setq d (pop data))
5893           (when (and (vectorp (gnus-data-header d))
5894                      (setq date (mail-header-date (gnus-data-header d))))
5895             (setq is-younger (nnmail-time-less
5896                               (nnmail-time-since (nnmail-date-to-time date))
5897                               cutoff))
5898             (when (if younger-p
5899                       is-younger
5900                     (not is-younger))
5901               (push (gnus-data-number d) articles))))
5902         (gnus-summary-limit (nreverse articles)))
5903     (gnus-summary-position-point)))
5904
5905 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5906 (make-obsolete
5907  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
5908
5909 (defun gnus-summary-limit-to-unread (&optional all)
5910   "Limit the summary buffer to articles that are not marked as read.
5911 If ALL is non-nil, limit strictly to unread articles."
5912   (interactive "P")
5913   (if all
5914       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
5915     (gnus-summary-limit-to-marks
5916      ;; Concat all the marks that say that an article is read and have
5917      ;; those removed.
5918      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
5919            gnus-killed-mark gnus-kill-file-mark
5920            gnus-low-score-mark gnus-expirable-mark
5921            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
5922            gnus-duplicate-mark gnus-souped-mark)
5923      'reverse)))
5924
5925 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
5926 (make-obsolete 'gnus-summary-delete-marked-with
5927                'gnus-summary-limit-exlude-marks)
5928
5929 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
5930   "Exclude articles that are marked with MARKS (e.g. \"DK\").
5931 If REVERSE, limit the summary buffer to articles that are marked
5932 with MARKS.  MARKS can either be a string of marks or a list of marks.
5933 Returns how many articles were removed."
5934   (interactive "sMarks: ")
5935   (gnus-summary-limit-to-marks marks t))
5936
5937 (defun gnus-summary-limit-to-marks (marks &optional reverse)
5938   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
5939 If REVERSE (the prefix), limit the summary buffer to articles that are
5940 not marked with MARKS.  MARKS can either be a string of marks or a
5941 list of marks.
5942 Returns how many articles were removed."
5943   (interactive "sMarks: \nP")
5944   (prog1
5945       (let ((data gnus-newsgroup-data)
5946             (marks (if (listp marks) marks
5947                      (append marks nil))) ; Transform to list.
5948             articles)
5949         (while data
5950           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
5951                   (memq (gnus-data-mark (car data)) marks))
5952             (push (gnus-data-number (car data)) articles))
5953           (setq data (cdr data)))
5954         (gnus-summary-limit articles))
5955     (gnus-summary-position-point)))
5956
5957 (defun gnus-summary-limit-to-score (&optional score)
5958   "Limit to articles with score at or above SCORE."
5959   (interactive "P")
5960   (setq score (if score
5961                   (prefix-numeric-value score)
5962                 (or gnus-summary-default-score 0)))
5963   (let ((data gnus-newsgroup-data)
5964         articles)
5965     (while data
5966       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
5967                 score)
5968         (push (gnus-data-number (car data)) articles))
5969       (setq data (cdr data)))
5970     (prog1
5971         (gnus-summary-limit articles)
5972       (gnus-summary-position-point))))
5973
5974 (defun gnus-summary-limit-include-thread (id)
5975   "Display all the hidden articles that in the current thread."
5976   (interactive (list (mail-header-id (gnus-summary-article-header))))
5977   (let ((articles (gnus-articles-in-thread
5978                    (gnus-id-to-thread (gnus-root-id id)))))
5979     (prog1
5980         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
5981       (gnus-summary-position-point))))
5982
5983 (defun gnus-summary-limit-include-dormant ()
5984   "Display all the hidden articles that are marked as dormant.
5985 Note that this command only works on a subset of the articles currently
5986 fetched for this group."
5987   (interactive)
5988   (unless gnus-newsgroup-dormant
5989     (error "There are no dormant articles in this group"))
5990   (prog1
5991       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
5992     (gnus-summary-position-point)))
5993
5994 (defun gnus-summary-limit-exclude-dormant ()
5995   "Hide all dormant articles."
5996   (interactive)
5997   (prog1
5998       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
5999     (gnus-summary-position-point)))
6000
6001 (defun gnus-summary-limit-exclude-childless-dormant ()
6002   "Hide all dormant articles that have no children."
6003   (interactive)
6004   (let ((data (gnus-data-list t))
6005         articles d children)
6006     ;; Find all articles that are either not dormant or have
6007     ;; children.
6008     (while (setq d (pop data))
6009       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6010                 (and (setq children
6011                            (gnus-article-children (gnus-data-number d)))
6012                      (let (found)
6013                        (while children
6014                          (when (memq (car children) articles)
6015                            (setq children nil
6016                                  found t))
6017                          (pop children))
6018                        found)))
6019         (push (gnus-data-number d) articles)))
6020     ;; Do the limiting.
6021     (prog1
6022         (gnus-summary-limit articles)
6023       (gnus-summary-position-point))))
6024
6025 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6026   "Mark all unread excluded articles as read.
6027 If ALL, mark even excluded ticked and dormants as read."
6028   (interactive "P")
6029   (let ((articles (gnus-sorted-complement
6030                    (sort
6031                     (mapcar (lambda (h) (mail-header-number h))
6032                             gnus-newsgroup-headers)
6033                     '<)
6034                    (sort gnus-newsgroup-limit '<)))
6035         article)
6036     (setq gnus-newsgroup-unreads gnus-newsgroup-limit)
6037     (if all
6038         (setq gnus-newsgroup-dormant nil
6039               gnus-newsgroup-marked nil
6040               gnus-newsgroup-reads
6041               (nconc
6042                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6043                gnus-newsgroup-reads))
6044       (while (setq article (pop articles))
6045         (unless (or (memq article gnus-newsgroup-dormant)
6046                     (memq article gnus-newsgroup-marked))
6047           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6048
6049 (defun gnus-summary-limit (articles &optional pop)
6050   (if pop
6051       ;; We pop the previous limit off the stack and use that.
6052       (setq articles (car gnus-newsgroup-limits)
6053             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6054     ;; We use the new limit, so we push the old limit on the stack.
6055     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6056   ;; Set the limit.
6057   (setq gnus-newsgroup-limit articles)
6058   (let ((total (length gnus-newsgroup-data))
6059         (data (gnus-data-find-list (gnus-summary-article-number)))
6060         (gnus-summary-mark-below nil)   ; Inhibit this.
6061         found)
6062     ;; This will do all the work of generating the new summary buffer
6063     ;; according to the new limit.
6064     (gnus-summary-prepare)
6065     ;; Hide any threads, possibly.
6066     (and gnus-show-threads
6067          gnus-thread-hide-subtree
6068          (gnus-summary-hide-all-threads))
6069     ;; Try to return to the article you were at, or one in the
6070     ;; neighborhood.
6071     (when data
6072       ;; We try to find some article after the current one.
6073       (while data
6074         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6075           (setq data nil
6076                 found t))
6077         (setq data (cdr data))))
6078     (unless found
6079       ;; If there is no data, that means that we were after the last
6080       ;; article.  The same goes when we can't find any articles
6081       ;; after the current one.
6082       (goto-char (point-max))
6083       (gnus-summary-find-prev))
6084     ;; We return how many articles were removed from the summary
6085     ;; buffer as a result of the new limit.
6086     (- total (length gnus-newsgroup-data))))
6087
6088 (defsubst gnus-invisible-cut-children (threads)
6089   (let ((num 0))
6090     (while threads
6091       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6092         (incf num))
6093       (pop threads))
6094     (< num 2)))
6095
6096 (defsubst gnus-cut-thread (thread)
6097   "Go forwards in the thread until we find an article that we want to display."
6098   (when (or (eq gnus-fetch-old-headers 'some)
6099             (eq gnus-fetch-old-headers 'invisible)          
6100             (eq gnus-build-sparse-threads 'some)
6101             (eq gnus-build-sparse-threads 'more))
6102     ;; Deal with old-fetched headers and sparse threads.
6103     (while (and
6104             thread
6105             (or
6106              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6107              (gnus-summary-article-ancient-p
6108               (mail-header-number (car thread))))
6109             (if (or (<= (length (cdr thread)) 1)
6110                     (eq gnus-fetch-old-headers 'invisible))
6111                 (setq gnus-newsgroup-limit
6112                       (delq (mail-header-number (car thread))
6113                             gnus-newsgroup-limit)
6114                       thread (cadr thread))
6115               (when (gnus-invisible-cut-children (cdr thread))
6116                 (let ((th (cdr thread)))
6117                   (while th
6118                     (if (memq (mail-header-number (caar th))
6119                               gnus-newsgroup-limit)
6120                         (setq thread (car th)
6121                               th nil)
6122                       (setq th (cdr th))))))))))
6123   thread)
6124
6125 (defun gnus-cut-threads (threads)
6126   "Cut off all uninteresting articles from the beginning of threads."
6127   (when (or (eq gnus-fetch-old-headers 'some)
6128             (eq gnus-fetch-old-headers 'invisible)
6129             (eq gnus-build-sparse-threads 'some)
6130             (eq gnus-build-sparse-threads 'more))
6131     (let ((th threads))
6132       (while th
6133         (setcar th (gnus-cut-thread (car th)))
6134         (setq th (cdr th)))))
6135   ;; Remove nixed out threads.
6136   (delq nil threads))
6137
6138 (defun gnus-summary-initial-limit (&optional show-if-empty)
6139   "Figure out what the initial limit is supposed to be on group entry.
6140 This entails weeding out unwanted dormants, low-scored articles,
6141 fetch-old-headers verbiage, and so on."
6142   ;; Most groups have nothing to remove.
6143   (if (or gnus-inhibit-limiting
6144           (and (null gnus-newsgroup-dormant)
6145                (not (eq gnus-fetch-old-headers 'some))
6146                (not (eq gnus-fetch-old-headers 'invisible))
6147                (null gnus-summary-expunge-below)
6148                (not (eq gnus-build-sparse-threads 'some))
6149                (not (eq gnus-build-sparse-threads 'more))
6150                (null gnus-thread-expunge-below)
6151                (not gnus-use-nocem)))
6152       ()                                ; Do nothing.
6153     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6154     (setq gnus-newsgroup-limit nil)
6155     (mapatoms
6156      (lambda (node)
6157        (unless (car (symbol-value node))
6158          ;; These threads have no parents -- they are roots.
6159          (let ((nodes (cdr (symbol-value node)))
6160                thread)
6161            (while nodes
6162              (if (and gnus-thread-expunge-below
6163                       (< (gnus-thread-total-score (car nodes))
6164                          gnus-thread-expunge-below))
6165                  (gnus-expunge-thread (pop nodes))
6166                (setq thread (pop nodes))
6167                (gnus-summary-limit-children thread))))))
6168      gnus-newsgroup-dependencies)
6169     ;; If this limitation resulted in an empty group, we might
6170     ;; pop the previous limit and use it instead.
6171     (when (and (not gnus-newsgroup-limit)
6172                show-if-empty)
6173       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6174     gnus-newsgroup-limit))
6175
6176 (defun gnus-summary-limit-children (thread)
6177   "Return 1 if this subthread is visible and 0 if it is not."
6178   ;; First we get the number of visible children to this thread.  This
6179   ;; is done by recursing down the thread using this function, so this
6180   ;; will really go down to a leaf article first, before slowly
6181   ;; working its way up towards the root.
6182   (when thread
6183     (let ((children
6184            (if (cdr thread)
6185                (apply '+ (mapcar 'gnus-summary-limit-children
6186                                  (cdr thread)))
6187              0))
6188           (number (mail-header-number (car thread)))
6189           score)
6190       (if (and
6191            (not (memq number gnus-newsgroup-marked))
6192            (or
6193             ;; If this article is dormant and has absolutely no visible
6194             ;; children, then this article isn't visible.
6195             (and (memq number gnus-newsgroup-dormant)
6196                  (zerop children))
6197             ;; If this is "fetch-old-headered" and there is no
6198             ;; visible children, then we don't want this article.
6199             (and (eq gnus-fetch-old-headers 'some)
6200                  (gnus-summary-article-ancient-p number)
6201                  (zerop children))
6202             ;; If this is "fetch-old-headered" and `invisible', then
6203             ;; we don't want this article.
6204             (and (eq gnus-fetch-old-headers 'invisible)
6205                  (gnus-summary-article-ancient-p number))
6206             ;; If this is a sparsely inserted article with no children,
6207             ;; we don't want it.
6208             (and (eq gnus-build-sparse-threads 'some)
6209                  (gnus-summary-article-sparse-p number)
6210                  (zerop children))
6211             ;; If we use expunging, and this article is really
6212             ;; low-scored, then we don't want this article.
6213             (when (and gnus-summary-expunge-below
6214                        (< (setq score
6215                                 (or (cdr (assq number gnus-newsgroup-scored))
6216                                     gnus-summary-default-score))
6217                           gnus-summary-expunge-below))
6218               ;; We increase the expunge-tally here, but that has
6219               ;; nothing to do with the limits, really.
6220               (incf gnus-newsgroup-expunged-tally)
6221               ;; We also mark as read here, if that's wanted.
6222               (when (and gnus-summary-mark-below
6223                          (< score gnus-summary-mark-below))
6224                 (setq gnus-newsgroup-unreads
6225                       (delq number gnus-newsgroup-unreads))
6226                 (if gnus-newsgroup-auto-expire
6227                     (push number gnus-newsgroup-expirable)
6228                   (push (cons number gnus-low-score-mark)
6229                         gnus-newsgroup-reads)))
6230               t)
6231             ;; Check NoCeM things.
6232             (if (and gnus-use-nocem
6233                      (gnus-nocem-unwanted-article-p
6234                       (mail-header-id (car thread))))
6235                 (progn
6236                   (setq gnus-newsgroup-unreads
6237                         (delq number gnus-newsgroup-unreads))
6238                   t))))
6239           ;; Nope, invisible article.
6240           0
6241         ;; Ok, this article is to be visible, so we add it to the limit
6242         ;; and return 1.
6243         (push number gnus-newsgroup-limit)
6244         1))))
6245
6246 (defun gnus-expunge-thread (thread)
6247   "Mark all articles in THREAD as read."
6248   (let* ((number (mail-header-number (car thread))))
6249     (incf gnus-newsgroup-expunged-tally)
6250     ;; We also mark as read here, if that's wanted.
6251     (setq gnus-newsgroup-unreads
6252           (delq number gnus-newsgroup-unreads))
6253     (if gnus-newsgroup-auto-expire
6254         (push number gnus-newsgroup-expirable)
6255       (push (cons number gnus-low-score-mark)
6256             gnus-newsgroup-reads)))
6257   ;; Go recursively through all subthreads.
6258   (mapcar 'gnus-expunge-thread (cdr thread)))
6259
6260 ;; Summary article oriented commands
6261
6262 (defun gnus-summary-refer-parent-article (n)
6263   "Refer parent article N times.
6264 If N is negative, go to ancestor -N instead.
6265 The difference between N and the number of articles fetched is returned."
6266   (interactive "p")
6267   (let ((skip 1)
6268         error header ref)
6269     (when (not (natnump n))
6270       (setq skip (abs n)
6271             n 1))
6272     (while (and (> n 0)
6273                 (not error))
6274       (setq header (gnus-summary-article-header))
6275       (if (and (eq (mail-header-number header)
6276                    (cdr gnus-article-current))
6277                (equal gnus-newsgroup-name
6278                       (car gnus-article-current)))
6279           ;; If we try to find the parent of the currently
6280           ;; displayed article, then we take a look at the actual
6281           ;; References header, since this is slightly more
6282           ;; reliable than the References field we got from the
6283           ;; server.
6284           (save-excursion
6285             (set-buffer gnus-original-article-buffer)
6286             (nnheader-narrow-to-headers)
6287             (unless (setq ref (message-fetch-field "references"))
6288               (setq ref (message-fetch-field "in-reply-to")))
6289             (widen))
6290         (setq ref
6291               ;; It's not the current article, so we take a bet on
6292               ;; the value we got from the server.
6293               (mail-header-references header)))
6294       (if (and ref
6295                (not (equal ref "")))
6296           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6297             (gnus-message 1 "Couldn't find parent"))
6298         (gnus-message 1 "No references in article %d"
6299                       (gnus-summary-article-number))
6300         (setq error t))
6301       (decf n))
6302     (gnus-summary-position-point)
6303     n))
6304
6305 (defun gnus-summary-refer-references ()
6306   "Fetch all articles mentioned in the References header.
6307 Return the number of articles fetched."
6308   (interactive)
6309   (let ((ref (mail-header-references (gnus-summary-article-header)))
6310         (current (gnus-summary-article-number))
6311         (n 0))
6312     (if (or (not ref)
6313             (equal ref ""))
6314         (error "No References in the current article")
6315       ;; For each Message-ID in the References header...
6316       (while (string-match "<[^>]*>" ref)
6317         (incf n)
6318         ;; ... fetch that article.
6319         (gnus-summary-refer-article
6320          (prog1 (match-string 0 ref)
6321            (setq ref (substring ref (match-end 0))))))
6322       (gnus-summary-goto-subject current)
6323       (gnus-summary-position-point)
6324       n)))
6325
6326 (defun gnus-summary-refer-thread (&optional limit)
6327   "Fetch all articles in the current thread.
6328 If LIMIT (the numerical prefix), fetch that many old headers instead
6329 of what's specified by the `gnus-refer-thread-limit' variable."
6330   (interactive "P")
6331   (let ((id (mail-header-id (gnus-summary-article-header)))
6332         (limit (if limit (prefix-numeric-value limit)
6333                  gnus-refer-thread-limit))
6334         fmethod root)
6335     ;; We want to fetch LIMIT *old* headers, but we also have to
6336     ;; re-fetch all the headers in the current buffer, because many of
6337     ;; them may be undisplayed.  So we adjust LIMIT.
6338     (when (numberp limit)
6339       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6340     (unless (eq gnus-fetch-old-headers 'invisible)
6341       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6342       ;; Retrieve the headers and read them in.
6343       (if (eq (gnus-retrieve-headers
6344                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6345               'nov)
6346           (gnus-build-all-threads)
6347         (error "Can't fetch thread from backends that don't support NOV"))
6348       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6349     (gnus-summary-limit-include-thread id)))
6350
6351 (defun gnus-summary-refer-article (message-id &optional arg)
6352   "Fetch an article specified by MESSAGE-ID.
6353 If ARG (the prefix), fetch the article using `gnus-refer-article-method'
6354 or `gnus-select-method', no matter what backend the article comes from."
6355   (interactive "sMessage-ID: \nP")
6356   (when (and (stringp message-id)
6357              (not (zerop (length message-id))))
6358     ;; Construct the correct Message-ID if necessary.
6359     ;; Suggested by tale@pawl.rpi.edu.
6360     (unless (string-match "^<" message-id)
6361       (setq message-id (concat "<" message-id)))
6362     (unless (string-match ">$" message-id)
6363       (setq message-id (concat message-id ">")))
6364     (let* ((header (gnus-id-to-header message-id))
6365            (sparse (and header
6366                         (gnus-summary-article-sparse-p
6367                          (mail-header-number header))
6368                         (memq (mail-header-number header)
6369                               gnus-newsgroup-limit)))
6370            h)
6371       (cond
6372        ;; If the article is present in the buffer we just go to it.
6373        ((and header
6374              (or (not (gnus-summary-article-sparse-p
6375                        (mail-header-number header)))
6376                  sparse))
6377         (prog1
6378             (gnus-summary-goto-article
6379              (mail-header-number header) nil t)
6380           (when sparse
6381             (gnus-summary-update-article (mail-header-number header)))))
6382        (t
6383         ;; We fetch the article
6384         (let ((gnus-override-method
6385                (cond ((gnus-news-group-p gnus-newsgroup-name)
6386                       gnus-refer-article-method)
6387                      (arg
6388                       (or gnus-refer-article-method gnus-select-method))
6389                      (t nil)))
6390               number)
6391           ;; Start the special refer-article method, if necessary.
6392           (when (and gnus-refer-article-method
6393                      (gnus-news-group-p gnus-newsgroup-name))
6394             (gnus-check-server gnus-refer-article-method))
6395           ;; Fetch the header, and display the article.
6396           (if (setq number (gnus-summary-insert-subject message-id))
6397               (gnus-summary-select-article nil nil nil number)
6398             (gnus-message 3 "Couldn't fetch article %s" message-id))))))))
6399
6400 (defun gnus-summary-edit-parameters ()
6401   "Edit the group parameters of the current group."
6402   (interactive)
6403   (gnus-group-edit-group gnus-newsgroup-name 'params))
6404
6405 (defun gnus-summary-enter-digest-group (&optional force)
6406   "Enter an nndoc group based on the current article.
6407 If FORCE, force a digest interpretation.  If not, try
6408 to guess what the document format is."
6409   (interactive "P")
6410   (let ((conf gnus-current-window-configuration))
6411     (save-excursion
6412       (gnus-summary-select-article))
6413     (setq gnus-current-window-configuration conf)
6414     (let* ((name (format "%s-%d"
6415                          (gnus-group-prefixed-name
6416                           gnus-newsgroup-name (list 'nndoc ""))
6417                          (save-excursion
6418                            (set-buffer gnus-summary-buffer)
6419                            gnus-current-article)))
6420            (ogroup gnus-newsgroup-name)
6421            (params (append (gnus-info-params (gnus-get-info ogroup))
6422                            (list (cons 'to-group ogroup))
6423                            (list (cons 'save-article-group ogroup))))
6424            (case-fold-search t)
6425            (buf (current-buffer))
6426            dig)
6427       (save-excursion
6428         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6429         (insert-buffer-substring gnus-original-article-buffer)
6430         ;; Remove lines that may lead nndoc to misinterpret the
6431         ;; document type.
6432         (narrow-to-region
6433          (goto-char (point-min))
6434          (or (search-forward "\n\n" nil t) (point)))
6435         (goto-char (point-min))
6436         (delete-matching-lines "^\\(Path\\):\\|^From ")
6437         (widen))
6438       (unwind-protect
6439           (if (gnus-group-read-ephemeral-group
6440                name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6441                             (nndoc-article-type
6442                              ,(if force 'digest 'guess))) t)
6443               ;; Make all postings to this group go to the parent group.
6444               (nconc (gnus-info-params (gnus-get-info name))
6445                      params)
6446             ;; Couldn't select this doc group.
6447             (switch-to-buffer buf)
6448             (gnus-set-global-variables)
6449             (gnus-configure-windows 'summary)
6450             (gnus-message 3 "Article couldn't be entered?"))
6451         (kill-buffer dig)))))
6452
6453 (defun gnus-summary-read-document (n)
6454   "Open a new group based on the current article(s).
6455 This will allow you to read digests and other similar
6456 documents as newsgroups.
6457 Obeys the standard process/prefix convention."
6458   (interactive "P")
6459   (let* ((articles (gnus-summary-work-articles n))
6460          (ogroup gnus-newsgroup-name)
6461          (params (append (gnus-info-params (gnus-get-info ogroup))
6462                          (list (cons 'to-group ogroup))))
6463          article group egroup groups vgroup)
6464     (while (setq article (pop articles))
6465       (setq group (format "%s-%d" gnus-newsgroup-name article))
6466       (gnus-summary-remove-process-mark article)
6467       (when (gnus-summary-display-article article)
6468         (save-excursion
6469           (nnheader-temp-write nil
6470             (insert-buffer-substring gnus-original-article-buffer)
6471             ;; Remove some headers that may lead nndoc to make
6472             ;; the wrong guess.
6473             (message-narrow-to-head)
6474             (goto-char (point-min))
6475             (delete-matching-lines "^\\(Path\\):\\|^From ")
6476             (widen)
6477             (if (setq egroup
6478                       (gnus-group-read-ephemeral-group
6479                        group `(nndoc ,group (nndoc-address ,(current-buffer))
6480                                      (nndoc-article-type guess))
6481                        t nil t))
6482                 (progn
6483                   ;; Make all postings to this group go to the parent group.
6484                   (nconc (gnus-info-params (gnus-get-info egroup))
6485                          params)
6486                   (push egroup groups))
6487               ;; Couldn't select this doc group.
6488               (gnus-error 3 "Article couldn't be entered"))))))
6489     ;; Now we have selected all the documents.
6490     (cond
6491      ((not groups)
6492       (error "None of the articles could be interpreted as documents"))
6493      ((gnus-group-read-ephemeral-group
6494        (setq vgroup (format
6495                      "nnvirtual:%s-%s" gnus-newsgroup-name
6496                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
6497        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
6498        t
6499        (cons (current-buffer) 'summary)))
6500      (t
6501       (error "Couldn't select virtual nndoc group")))))
6502
6503 (defun gnus-summary-isearch-article (&optional regexp-p)
6504   "Do incremental search forward on the current article.
6505 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
6506   (interactive "P")
6507   (gnus-summary-select-article)
6508   (gnus-configure-windows 'article)
6509   (gnus-eval-in-buffer-window gnus-article-buffer
6510     (save-restriction
6511       (widen)
6512       (isearch-forward regexp-p))))
6513
6514 (defun gnus-summary-search-article-forward (regexp &optional backward)
6515   "Search for an article containing REGEXP forward.
6516 If BACKWARD, search backward instead."
6517   (interactive
6518    (list (read-string
6519           (format "Search article %s (regexp%s): "
6520                   (if current-prefix-arg "backward" "forward")
6521                   (if gnus-last-search-regexp
6522                       (concat ", default " gnus-last-search-regexp)
6523                     "")))
6524          current-prefix-arg))
6525   (if (string-equal regexp "")
6526       (setq regexp (or gnus-last-search-regexp ""))
6527     (setq gnus-last-search-regexp regexp))
6528   (if (gnus-summary-search-article regexp backward)
6529       (gnus-summary-show-thread)
6530     (error "Search failed: \"%s\"" regexp)))
6531
6532 (defun gnus-summary-search-article-backward (regexp)
6533   "Search for an article containing REGEXP backward."
6534   (interactive
6535    (list (read-string
6536           (format "Search article backward (regexp%s): "
6537                   (if gnus-last-search-regexp
6538                       (concat ", default " gnus-last-search-regexp)
6539                     "")))))
6540   (gnus-summary-search-article-forward regexp 'backward))
6541
6542 (defun gnus-summary-search-article (regexp &optional backward)
6543   "Search for an article containing REGEXP.
6544 Optional argument BACKWARD means do search for backward.
6545 `gnus-select-article-hook' is not called during the search."
6546   ;; We have to require this here to make sure that the following
6547   ;; dynamic binding isn't shadowed by autoloading.
6548   (require 'gnus-async)
6549   (let ((gnus-select-article-hook nil)  ;Disable hook.
6550         (gnus-article-display-hook nil)
6551         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
6552         (gnus-use-article-prefetch nil)
6553         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
6554         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
6555         (sum (current-buffer))
6556         (found nil)
6557         point)
6558     (gnus-save-hidden-threads
6559       (gnus-summary-select-article)
6560       (set-buffer gnus-article-buffer)
6561       (when backward
6562         (forward-line -1))
6563       (while (not found)
6564         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
6565         (if (if backward
6566                 (re-search-backward regexp nil t)
6567               (re-search-forward regexp nil t))
6568             ;; We found the regexp.
6569             (progn
6570               (setq found 'found)
6571               (beginning-of-line)
6572               (set-window-start
6573                (get-buffer-window (current-buffer))
6574                (point))
6575               (forward-line 1)
6576               (set-buffer sum)
6577               (setq point (point)))
6578           ;; We didn't find it, so we go to the next article.
6579           (set-buffer sum)
6580           (setq found 'not)
6581           (while (eq found 'not)
6582             (if (not (if backward (gnus-summary-find-prev)
6583                        (gnus-summary-find-next)))
6584                 ;; No more articles.
6585                 (setq found t)
6586               ;; Select the next article and adjust point.
6587               (unless (gnus-summary-article-sparse-p
6588                        (gnus-summary-article-number))
6589                 (setq found nil)
6590                 (gnus-summary-select-article)
6591                 (set-buffer gnus-article-buffer)
6592                 (widen)
6593                 (goto-char (if backward (point-max) (point-min))))))))
6594       (gnus-message 7 ""))
6595     ;; Return whether we found the regexp.
6596     (when (eq found 'found)
6597       (goto-char point)
6598       (gnus-summary-show-thread)
6599       (gnus-summary-goto-subject gnus-current-article)
6600       (gnus-summary-position-point)
6601       t)))
6602
6603 (defun gnus-summary-find-matching (header regexp &optional backward unread
6604                                           not-case-fold)
6605   "Return a list of all articles that match REGEXP on HEADER.
6606 The search stars on the current article and goes forwards unless
6607 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
6608 If UNREAD is non-nil, only unread articles will
6609 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
6610 in the comparisons."
6611   (let ((data (if (eq backward 'all) gnus-newsgroup-data
6612                 (gnus-data-find-list
6613                  (gnus-summary-article-number) (gnus-data-list backward))))
6614         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
6615         (case-fold-search (not not-case-fold))
6616         articles d)
6617     (unless (fboundp (intern (concat "mail-header-" header)))
6618       (error "%s is not a valid header" header))
6619     (while data
6620       (setq d (car data))
6621       (and (or (not unread)             ; We want all articles...
6622                (gnus-data-unread-p d))  ; Or just unreads.
6623            (vectorp (gnus-data-header d)) ; It's not a pseudo.
6624            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
6625            (push (gnus-data-number d) articles)) ; Success!
6626       (setq data (cdr data)))
6627     (nreverse articles)))
6628
6629 (defun gnus-summary-execute-command (header regexp command &optional backward)
6630   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
6631 If HEADER is an empty string (or nil), the match is done on the entire
6632 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
6633   (interactive
6634    (list (let ((completion-ignore-case t))
6635            (completing-read
6636             "Header name: "
6637             (mapcar (lambda (string) (list string))
6638                     '("Number" "Subject" "From" "Lines" "Date"
6639                       "Message-ID" "Xref" "References" "Body"))
6640             nil 'require-match))
6641          (read-string "Regexp: ")
6642          (read-key-sequence "Command: ")
6643          current-prefix-arg))
6644   (when (equal header "Body")
6645     (setq header ""))
6646   ;; Hidden thread subtrees must be searched as well.
6647   (gnus-summary-show-all-threads)
6648   ;; We don't want to change current point nor window configuration.
6649   (save-excursion
6650     (save-window-excursion
6651       (gnus-message 6 "Executing %s..." (key-description command))
6652       ;; We'd like to execute COMMAND interactively so as to give arguments.
6653       (gnus-execute header regexp
6654                     `(call-interactively ',(key-binding command))
6655                     backward)
6656       (gnus-message 6 "Executing %s...done" (key-description command)))))
6657
6658 (defun gnus-summary-beginning-of-article ()
6659   "Scroll the article back to the beginning."
6660   (interactive)
6661   (gnus-summary-select-article)
6662   (gnus-configure-windows 'article)
6663   (gnus-eval-in-buffer-window gnus-article-buffer
6664     (widen)
6665     (goto-char (point-min))
6666     (when gnus-page-broken
6667       (gnus-narrow-to-page))))
6668
6669 (defun gnus-summary-end-of-article ()
6670   "Scroll to the end of the article."
6671   (interactive)
6672   (gnus-summary-select-article)
6673   (gnus-configure-windows 'article)
6674   (gnus-eval-in-buffer-window gnus-article-buffer
6675     (widen)
6676     (goto-char (point-max))
6677     (recenter -3)
6678     (when gnus-page-broken
6679       (gnus-narrow-to-page))))
6680
6681 (defun gnus-summary-print-article (&optional filename n)
6682   "Generate and print a PostScript image of the N next (mail) articles.
6683
6684 If N is negative, print the N previous articles.  If N is nil and articles
6685 have been marked with the process mark, print these instead.
6686
6687 If the optional second argument FILENAME is nil, send the image to the
6688 printer.  If FILENAME is a string, save the PostScript image in a file with
6689 that name.  If FILENAME is a number, prompt the user for the name of the file
6690 to save in."
6691   (interactive (list (ps-print-preprint current-prefix-arg)
6692                      current-prefix-arg))
6693   (dolist (article (gnus-summary-work-articles n))
6694     (gnus-summary-select-article nil nil 'pseudo article)
6695     (gnus-eval-in-buffer-window gnus-article-buffer
6696       (let ((buffer (generate-new-buffer " *print*")))
6697         (unwind-protect
6698             (progn
6699               (copy-to-buffer buffer (point-min) (point-max))
6700               (set-buffer buffer)
6701               (gnus-article-delete-invisible-text)
6702               (let ((ps-left-header
6703                      (list 
6704                       (concat "("
6705                               (mail-header-subject gnus-current-headers) ")")
6706                       (concat "("
6707                               (mail-header-from gnus-current-headers) ")")))
6708                     (ps-right-header 
6709                      (list 
6710                       "/pagenumberstring load" 
6711                       (concat "("
6712                               (mail-header-date gnus-current-headers) ")"))))
6713                 (gnus-run-hooks 'gnus-ps-print-hook)
6714                 (save-excursion
6715                   (ps-print-buffer-with-faces filename))))
6716           (kill-buffer buffer))))))
6717
6718 (defun gnus-summary-show-article (&optional arg)
6719   "Force re-fetching of the current article.
6720 If ARG (the prefix) is non-nil, show the raw article without any
6721 article massaging functions being run."
6722   (interactive "P")
6723   (if (not arg)
6724       ;; Select the article the normal way.
6725       (gnus-summary-select-article nil 'force)
6726     ;; Bind the article treatment functions to nil.
6727     (let ((gnus-have-all-headers t)
6728           gnus-article-display-hook
6729           gnus-article-prepare-hook
6730           gnus-break-pages
6731           gnus-show-mime
6732           gnus-visual)
6733       (gnus-summary-select-article nil 'force)))
6734   (gnus-summary-goto-subject gnus-current-article)
6735   (gnus-summary-position-point))
6736
6737 (defun gnus-summary-verbose-headers (&optional arg)
6738   "Toggle permanent full header display.
6739 If ARG is a positive number, turn header display on.
6740 If ARG is a negative number, turn header display off."
6741   (interactive "P")
6742   (setq gnus-show-all-headers
6743         (cond ((or (not (numberp arg))
6744                    (zerop arg))
6745                (not gnus-show-all-headers))
6746               ((natnump arg)
6747                t)))
6748   (gnus-summary-show-article))
6749
6750 (defun gnus-summary-toggle-header (&optional arg)
6751   "Show the headers if they are hidden, or hide them if they are shown.
6752 If ARG is a positive number, show the entire header.
6753 If ARG is a negative number, hide the unwanted header lines."
6754   (interactive "P")
6755   (save-excursion
6756     (set-buffer gnus-article-buffer)
6757     (let* ((buffer-read-only nil)
6758            (inhibit-point-motion-hooks t)
6759            (hidden (text-property-any
6760                     (goto-char (point-min)) (search-forward "\n\n")
6761                     'invisible t))
6762            e)
6763       (goto-char (point-min))
6764       (when (search-forward "\n\n" nil t)
6765         (delete-region (point-min) (1- (point))))
6766       (goto-char (point-min))
6767       (save-excursion
6768         (set-buffer gnus-original-article-buffer)
6769         (goto-char (point-min))
6770         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
6771       (insert-buffer-substring gnus-original-article-buffer 1 e)
6772       (let ((article-inhibit-hiding t))
6773         (gnus-run-hooks 'gnus-article-display-hook))
6774       (when (or (not hidden) (and (numberp arg) (< arg 0)))
6775         (gnus-article-hide-headers)))))
6776
6777 (defun gnus-summary-show-all-headers ()
6778   "Make all header lines visible."
6779   (interactive)
6780   (gnus-article-show-all-headers))
6781
6782 (defun gnus-summary-toggle-mime (&optional arg)
6783   "Toggle MIME processing.
6784 If ARG is a positive number, turn MIME processing on."
6785   (interactive "P")
6786   (setq gnus-show-mime
6787         (if (null arg) (not gnus-show-mime)
6788           (> (prefix-numeric-value arg) 0)))
6789   (gnus-summary-select-article t 'force))
6790
6791 (defun gnus-summary-caesar-message (&optional arg)
6792   "Caesar rotate the current article by 13.
6793 The numerical prefix specifies how many places to rotate each letter
6794 forward."
6795   (interactive "P")
6796   (gnus-summary-select-article)
6797   (let ((mail-header-separator ""))
6798     (gnus-eval-in-buffer-window gnus-article-buffer
6799       (save-restriction
6800         (widen)
6801         (let ((start (window-start))
6802               buffer-read-only)
6803           (message-caesar-buffer-body arg)
6804           (set-window-start (get-buffer-window (current-buffer)) start))))))
6805
6806 (defun gnus-summary-stop-page-breaking ()
6807   "Stop page breaking in the current article."
6808   (interactive)
6809   (gnus-summary-select-article)
6810   (gnus-eval-in-buffer-window gnus-article-buffer
6811     (widen)
6812     (when (gnus-visual-p 'page-marker)
6813       (let ((buffer-read-only nil))
6814         (gnus-remove-text-with-property 'gnus-prev)
6815         (gnus-remove-text-with-property 'gnus-next))
6816       (setq gnus-page-broken nil))))
6817
6818 (defun gnus-summary-move-article (&optional n to-newsgroup
6819                                             select-method action)
6820   "Move the current article to a different newsgroup.
6821 If N is a positive number, move the N next articles.
6822 If N is a negative number, move the N previous articles.
6823 If N is nil and any articles have been marked with the process mark,
6824 move those articles instead.
6825 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
6826 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
6827 re-spool using this method.
6828
6829 For this function to work, both the current newsgroup and the
6830 newsgroup that you want to move to have to support the `request-move'
6831 and `request-accept' functions."
6832   (interactive "P")
6833   (unless action
6834     (setq action 'move))
6835   ;; Disable marking as read.
6836   (let (gnus-mark-article-hook)
6837     (save-window-excursion
6838       (gnus-summary-select-article)))
6839   ;; Check whether the source group supports the required functions.
6840   (cond ((and (eq action 'move)
6841               (not (gnus-check-backend-function
6842                     'request-move-article gnus-newsgroup-name)))
6843          (error "The current group does not support article moving"))
6844         ((and (eq action 'crosspost)
6845               (not (gnus-check-backend-function
6846                     'request-replace-article gnus-newsgroup-name)))
6847          (error "The current group does not support article editing")))
6848   (let ((articles (gnus-summary-work-articles n))
6849         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
6850         (names '((move "Move" "Moving")
6851                  (copy "Copy" "Copying")
6852                  (crosspost "Crosspost" "Crossposting")))
6853         (copy-buf (save-excursion
6854                     (nnheader-set-temp-buffer " *copy article*")))
6855         art-group to-method new-xref article to-groups)
6856     (unless (assq action names)
6857       (error "Unknown action %s" action))
6858     ;; Read the newsgroup name.
6859     (when (and (not to-newsgroup)
6860                (not select-method))
6861       (setq to-newsgroup
6862             (gnus-read-move-group-name
6863              (cadr (assq action names))
6864              (symbol-value (intern (format "gnus-current-%s-group" action)))
6865              articles prefix))
6866       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
6867     (setq to-method (or select-method
6868                         (gnus-group-name-to-method to-newsgroup)))
6869     ;; Check the method we are to move this article to...
6870     (unless (gnus-check-backend-function
6871              'request-accept-article (car to-method))
6872       (error "%s does not support article copying" (car to-method)))
6873     (unless (gnus-check-server to-method)
6874       (error "Can't open server %s" (car to-method)))
6875     (gnus-message 6 "%s to %s: %s..."
6876                   (caddr (assq action names))
6877                   (or (car select-method) to-newsgroup) articles)
6878     (while articles
6879       (setq article (pop articles))
6880       (setq
6881        art-group
6882        (cond
6883         ;; Move the article.
6884         ((eq action 'move)
6885          ;; Remove this article from future suppression.
6886          (gnus-dup-unsuppress-article article)
6887          (gnus-request-move-article
6888           article                       ; Article to move
6889           gnus-newsgroup-name           ; From newsgroup
6890           (nth 1 (gnus-find-method-for-group
6891                   gnus-newsgroup-name)) ; Server
6892           (list 'gnus-request-accept-article
6893                 to-newsgroup (list 'quote select-method)
6894                 (not articles))         ; Accept form
6895           (not articles)))              ; Only save nov last time
6896         ;; Copy the article.
6897         ((eq action 'copy)
6898          (save-excursion
6899            (set-buffer copy-buf)
6900            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
6901              (gnus-request-accept-article
6902               to-newsgroup select-method (not articles)))))
6903         ;; Crosspost the article.
6904         ((eq action 'crosspost)
6905          (let ((xref (message-tokenize-header
6906                       (mail-header-xref (gnus-summary-article-header article))
6907                       " ")))
6908            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
6909                                   ":" article))
6910            (unless xref
6911              (setq xref (list (system-name))))
6912            (setq new-xref
6913                  (concat
6914                   (mapconcat 'identity
6915                              (delete "Xref:" (delete new-xref xref))
6916                              " ")
6917                   " " new-xref))
6918            (save-excursion
6919              (set-buffer copy-buf)
6920              ;; First put the article in the destination group.
6921              (gnus-request-article-this-buffer article gnus-newsgroup-name)
6922              (when (consp (setq art-group
6923                                 (gnus-request-accept-article
6924                                  to-newsgroup select-method (not articles))))
6925                (setq new-xref (concat new-xref " " (car art-group)
6926                                       ":" (cdr art-group)))
6927                ;; Now we have the new Xrefs header, so we insert
6928                ;; it and replace the new article.
6929                (nnheader-replace-header "Xref" new-xref)
6930                (gnus-request-replace-article
6931                 (cdr art-group) to-newsgroup (current-buffer))
6932                art-group))))))
6933       (cond
6934        ((not art-group)
6935         (gnus-message 1 "Couldn't %s article %s"
6936                       (cadr (assq action names)) article))
6937        ((and (eq art-group 'junk)
6938              (eq action 'move))
6939         (gnus-summary-mark-article article gnus-canceled-mark)
6940         (gnus-message 4 "Deleted article %s" article))
6941        (t
6942         (let* ((entry
6943                 (or
6944                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
6945                  (gnus-gethash
6946                   (gnus-group-prefixed-name
6947                    (car art-group)
6948                    (or select-method
6949                        (gnus-find-method-for-group to-newsgroup)))
6950                   gnus-newsrc-hashtb)))
6951                (info (nth 2 entry))
6952                (to-group (gnus-info-group info)))
6953           ;; Update the group that has been moved to.
6954           (when (and info
6955                      (memq action '(move copy)))
6956             (unless (member to-group to-groups)
6957               (push to-group to-groups))
6958
6959             (unless (memq article gnus-newsgroup-unreads)
6960               (gnus-info-set-read
6961                info (gnus-add-to-range (gnus-info-read info)
6962                                        (list (cdr art-group)))))
6963
6964             ;; Copy any marks over to the new group.
6965             (let ((marks gnus-article-mark-lists)
6966                   (to-article (cdr art-group)))
6967
6968               ;; See whether the article is to be put in the cache.
6969               (when gnus-use-cache
6970                 (gnus-cache-possibly-enter-article
6971                  to-group to-article
6972                  (let ((header (copy-sequence
6973                                 (gnus-summary-article-header article))))
6974                    (mail-header-set-number header to-article)
6975                    header)
6976                  (memq article gnus-newsgroup-marked)
6977                  (memq article gnus-newsgroup-dormant)
6978                  (memq article gnus-newsgroup-unreads)))
6979
6980               (when (and (equal to-group gnus-newsgroup-name)
6981                          (not (memq article gnus-newsgroup-unreads)))
6982                 ;; Mark this article as read in this group.
6983                 (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
6984                 (setcdr (gnus-active to-group) to-article)
6985                 (setcdr gnus-newsgroup-active to-article))
6986
6987               (while marks
6988                 (when (memq article (symbol-value
6989                                      (intern (format "gnus-newsgroup-%s"
6990                                                      (caar marks)))))
6991                   ;; If the other group is the same as this group,
6992                   ;; then we have to add the mark to the list.
6993                   (when (equal to-group gnus-newsgroup-name)
6994                     (set (intern (format "gnus-newsgroup-%s" (caar marks)))
6995                          (cons to-article
6996                                (symbol-value
6997                                 (intern (format "gnus-newsgroup-%s"
6998                                                 (caar marks)))))))
6999                   ;; Copy the marks to other group.
7000                   (gnus-add-marked-articles
7001                    to-group (cdar marks) (list to-article) info))
7002                 (setq marks (cdr marks)))
7003
7004               (gnus-dribble-enter
7005                (concat "(gnus-group-set-info '"
7006                        (gnus-prin1-to-string (gnus-get-info to-group))
7007                        ")"))))
7008
7009           ;; Update the Xref header in this article to point to
7010           ;; the new crossposted article we have just created.
7011           (when (eq action 'crosspost)
7012             (save-excursion
7013               (set-buffer copy-buf)
7014               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7015               (nnheader-replace-header "Xref" new-xref)
7016               (gnus-request-replace-article
7017                article gnus-newsgroup-name (current-buffer)))))
7018
7019         ;;;!!!Why is this necessary?
7020         (set-buffer gnus-summary-buffer)
7021         
7022         (gnus-summary-goto-subject article)
7023         (when (eq action 'move)
7024           (gnus-summary-mark-article article gnus-canceled-mark))))
7025       (gnus-summary-remove-process-mark article))
7026     ;; Re-activate all groups that have been moved to.
7027     (while to-groups
7028       (save-excursion
7029         (set-buffer gnus-group-buffer)
7030         (when (gnus-group-goto-group (car to-groups) t)
7031           (gnus-group-get-new-news-this-group 1 t))
7032         (pop to-groups)))
7033
7034     (gnus-kill-buffer copy-buf)
7035     (gnus-summary-position-point)
7036     (gnus-set-mode-line 'summary)))
7037
7038 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7039   "Move the current article to a different newsgroup.
7040 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7041 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7042 re-spool using this method."
7043   (interactive "P")
7044   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7045
7046 (defun gnus-summary-crosspost-article (&optional n)
7047   "Crosspost the current article to some other group."
7048   (interactive "P")
7049   (gnus-summary-move-article n nil nil 'crosspost))
7050
7051 (defcustom gnus-summary-respool-default-method nil
7052   "Default method for respooling an article.
7053 If nil, use to the current newsgroup method."
7054   :type `(choice (gnus-select-method :value (nnml ""))
7055                  (const nil))
7056   :group 'gnus-summary-mail)
7057
7058 (defun gnus-summary-respool-article (&optional n method)
7059   "Respool the current article.
7060 The article will be squeezed through the mail spooling process again,
7061 which means that it will be put in some mail newsgroup or other
7062 depending on `nnmail-split-methods'.
7063 If N is a positive number, respool the N next articles.
7064 If N is a negative number, respool the N previous articles.
7065 If N is nil and any articles have been marked with the process mark,
7066 respool those articles instead.
7067
7068 Respooling can be done both from mail groups and \"real\" newsgroups.
7069 In the former case, the articles in question will be moved from the
7070 current group into whatever groups they are destined to.  In the
7071 latter case, they will be copied into the relevant groups."
7072   (interactive
7073    (list current-prefix-arg
7074          (let* ((methods (gnus-methods-using 'respool))
7075                 (methname
7076                  (symbol-name (or gnus-summary-respool-default-method
7077                                   (car (gnus-find-method-for-group
7078                                         gnus-newsgroup-name)))))
7079                 (method
7080                  (gnus-completing-read
7081                   methname "What backend do you want to use when respooling?"
7082                   methods nil t nil 'gnus-mail-method-history))
7083                 ms)
7084            (cond
7085             ((zerop (length (setq ms (gnus-servers-using-backend
7086                                       (intern method)))))
7087              (list (intern method) ""))
7088             ((= 1 (length ms))
7089              (car ms))
7090             (t
7091              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7092                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7093                            ms-alist))))))))
7094   (unless method
7095     (error "No method given for respooling"))
7096   (if (assoc (symbol-name
7097               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7098              (gnus-methods-using 'respool))
7099       (gnus-summary-move-article n nil method)
7100     (gnus-summary-copy-article n nil method)))
7101
7102 (defun gnus-summary-import-article (file)
7103   "Import an arbitrary file into a mail newsgroup."
7104   (interactive "fImport file: ")
7105   (let ((group gnus-newsgroup-name)
7106         (now (current-time))
7107         atts lines)
7108     (unless (gnus-check-backend-function 'request-accept-article group)
7109       (error "%s does not support article importing" group))
7110     (or (file-readable-p file)
7111         (not (file-regular-p file))
7112         (error "Can't read %s" file))
7113     (save-excursion
7114       (set-buffer (get-buffer-create " *import file*"))
7115       (buffer-disable-undo (current-buffer))
7116       (erase-buffer)
7117       (insert-file-contents file)
7118       (goto-char (point-min))
7119       (unless (nnheader-article-p)
7120         ;; This doesn't look like an article, so we fudge some headers.
7121         (setq atts (file-attributes file)
7122               lines (count-lines (point-min) (point-max)))
7123         (insert "From: " (read-string "From: ") "\n"
7124                 "Subject: " (read-string "Subject: ") "\n"
7125                 "Date: " (timezone-make-date-arpa-standard
7126                           (current-time-string (nth 5 atts))
7127                           (current-time-zone now)
7128                           (current-time-zone now))
7129                 "\n"
7130                 "Message-ID: " (message-make-message-id) "\n"
7131                 "Lines: " (int-to-string lines) "\n"
7132                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7133       (gnus-request-accept-article group nil t)
7134       (kill-buffer (current-buffer)))))
7135
7136 (defun gnus-summary-article-posted-p ()
7137   "Say whether the current (mail) article is available from `gnus-select-method' as well.
7138 This will be the case if the article has both been mailed and posted."
7139   (interactive)
7140   (let ((id (mail-header-references (gnus-summary-article-header)))
7141         (gnus-override-method
7142          (or gnus-refer-article-method gnus-select-method)))
7143     (if (gnus-request-head id "")
7144         (gnus-message 2 "The current message was found on %s"
7145                       gnus-override-method)
7146       (gnus-message 2 "The current message couldn't be found on %s"
7147                     gnus-override-method)
7148       nil)))
7149
7150 (defun gnus-summary-expire-articles (&optional now)
7151   "Expire all articles that are marked as expirable in the current group."
7152   (interactive)
7153   (when (gnus-check-backend-function
7154          'request-expire-articles gnus-newsgroup-name)
7155     ;; This backend supports expiry.
7156     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7157            (expirable (if total
7158                           (progn
7159                             ;; We need to update the info for
7160                             ;; this group for `gnus-list-of-read-articles'
7161                             ;; to give us the right answer.
7162                             (gnus-run-hooks 'gnus-exit-group-hook)
7163                             (gnus-summary-update-info)
7164                             (gnus-list-of-read-articles gnus-newsgroup-name))
7165                         (setq gnus-newsgroup-expirable
7166                               (sort gnus-newsgroup-expirable '<))))
7167            (expiry-wait (if now 'immediate
7168                           (gnus-group-find-parameter
7169                            gnus-newsgroup-name 'expiry-wait)))
7170            es)
7171       (when expirable
7172         ;; There are expirable articles in this group, so we run them
7173         ;; through the expiry process.
7174         (gnus-message 6 "Expiring articles...")
7175         ;; The list of articles that weren't expired is returned.
7176         (save-excursion
7177           (if expiry-wait
7178               (let ((nnmail-expiry-wait-function nil)
7179                     (nnmail-expiry-wait expiry-wait))
7180                 (setq es (gnus-request-expire-articles
7181                           expirable gnus-newsgroup-name)))
7182             (setq es (gnus-request-expire-articles
7183                       expirable gnus-newsgroup-name))))
7184         (unless total
7185           (setq gnus-newsgroup-expirable es))
7186         ;; We go through the old list of expirable, and mark all
7187         ;; really expired articles as nonexistent.
7188         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7189           (let ((gnus-use-cache nil))
7190             (while expirable
7191               (unless (memq (car expirable) es)
7192                 (when (gnus-data-find (car expirable))
7193                   (gnus-summary-mark-article
7194                    (car expirable) gnus-canceled-mark)))
7195               (setq expirable (cdr expirable)))))
7196         (gnus-message 6 "Expiring articles...done")))))
7197
7198 (defun gnus-summary-expire-articles-now ()
7199   "Expunge all expirable articles in the current group.
7200 This means that *all* articles that are marked as expirable will be
7201 deleted forever, right now."
7202   (interactive)
7203   (or gnus-expert-user
7204       (gnus-yes-or-no-p
7205        "Are you really, really, really sure you want to delete all these messages? ")
7206       (error "Phew!"))
7207   (gnus-summary-expire-articles t))
7208
7209 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7210 (defun gnus-summary-delete-article (&optional n)
7211   "Delete the N next (mail) articles.
7212 This command actually deletes articles.  This is not a marking
7213 command.  The article will disappear forever from your life, never to
7214 return.
7215 If N is negative, delete backwards.
7216 If N is nil and articles have been marked with the process mark,
7217 delete these instead."
7218   (interactive "P")
7219   (unless (gnus-check-backend-function 'request-expire-articles
7220                                        gnus-newsgroup-name)
7221     (error "The current newsgroup does not support article deletion"))
7222   ;; Compute the list of articles to delete.
7223   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7224         not-deleted)
7225     (if (and gnus-novice-user
7226              (not (gnus-yes-or-no-p
7227                    (format "Do you really want to delete %s forever? "
7228                            (if (> (length articles) 1)
7229                                (format "these %s articles" (length articles))
7230                              "this article")))))
7231         ()
7232       ;; Delete the articles.
7233       (setq not-deleted (gnus-request-expire-articles
7234                          articles gnus-newsgroup-name 'force))
7235       (while articles
7236         (gnus-summary-remove-process-mark (car articles))
7237         ;; The backend might not have been able to delete the article
7238         ;; after all.
7239         (unless (memq (car articles) not-deleted)
7240           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7241         (setq articles (cdr articles)))
7242       (when not-deleted
7243         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7244     (gnus-summary-position-point)
7245     (gnus-set-mode-line 'summary)
7246     not-deleted))
7247
7248 (defun gnus-summary-edit-article (&optional force)
7249   "Edit the current article.
7250 This will have permanent effect only in mail groups.
7251 If FORCE is non-nil, allow editing of articles even in read-only
7252 groups."
7253   (interactive "P")
7254   (save-excursion
7255     (set-buffer gnus-summary-buffer)
7256     (gnus-set-global-variables)
7257     (when (and (not force)
7258                (gnus-group-read-only-p))
7259       (error "The current newsgroup does not support article editing"))
7260     ;; Select article if needed.
7261     (unless (eq (gnus-summary-article-number)
7262                 gnus-current-article)
7263       (gnus-summary-select-article t))
7264     (gnus-article-date-original)
7265     (gnus-article-edit-article
7266      `(lambda (no-highlight)
7267         (gnus-summary-edit-article-done
7268          ,(or (mail-header-references gnus-current-headers) "")
7269          ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))
7270
7271 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7272
7273 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7274                                                  no-highlight)
7275   "Make edits to the current article permanent."
7276   (interactive)
7277   ;; Replace the article.
7278   (let ((buf (current-buffer)))
7279     (nnheader-temp-write nil
7280       (insert-buffer buf)
7281       (if (and (not read-only)
7282                (not (gnus-request-replace-article
7283                      (cdr gnus-article-current) (car gnus-article-current)
7284                      (current-buffer))))
7285           (error "Couldn't replace article")
7286         ;; Update the summary buffer.
7287         (if (and references
7288                  (equal (message-tokenize-header references " ")
7289                         (message-tokenize-header
7290                          (or (message-fetch-field "references") "") " ")))
7291             ;; We only have to update this line.
7292             (save-excursion
7293               (save-restriction
7294                 (message-narrow-to-head)
7295                 (let ((head (buffer-string))
7296                       header)
7297                   (nnheader-temp-write nil
7298                     (insert (format "211 %d Article retrieved.\n"
7299                                     (cdr gnus-article-current)))
7300                     (insert head)
7301                     (insert ".\n")
7302                     (let ((nntp-server-buffer (current-buffer)))
7303                       (setq header (car (gnus-get-newsgroup-headers
7304                                          (save-excursion
7305                                            (set-buffer gnus-summary-buffer)
7306                                            gnus-newsgroup-dependencies)
7307                                          t))))
7308                     (save-excursion
7309                       (set-buffer gnus-summary-buffer)
7310                       (gnus-data-set-header
7311                        (gnus-data-find (cdr gnus-article-current))
7312                        header)
7313                       (gnus-summary-update-article-line
7314                        (cdr gnus-article-current) header))))))
7315           ;; Update threads.
7316           (set-buffer (or buffer gnus-summary-buffer))
7317           (gnus-summary-update-article (cdr gnus-article-current)))
7318         ;; Prettify the article buffer again.
7319         (unless no-highlight
7320           (save-excursion
7321             (set-buffer gnus-article-buffer)
7322             (gnus-run-hooks 'gnus-article-display-hook)
7323             (set-buffer gnus-original-article-buffer)
7324             (gnus-request-article
7325              (cdr gnus-article-current)
7326              (car gnus-article-current) (current-buffer))))
7327         ;; Prettify the summary buffer line.
7328         (when (gnus-visual-p 'summary-highlight 'highlight)
7329           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
7330
7331 (defun gnus-summary-edit-wash (key)
7332   "Perform editing command KEY in the article buffer."
7333   (interactive
7334    (list
7335     (progn
7336       (message "%s" (concat (this-command-keys) "- "))
7337       (read-char))))
7338   (message "")
7339   (gnus-summary-edit-article)
7340   (execute-kbd-macro (concat (this-command-keys) key))
7341   (gnus-article-edit-done))
7342
7343 ;;; Respooling
7344
7345 (defun gnus-summary-respool-query (&optional silent)
7346   "Query where the respool algorithm would put this article."
7347   (interactive)
7348   (let (gnus-mark-article-hook)
7349     (gnus-summary-select-article)
7350     (save-excursion
7351       (set-buffer gnus-original-article-buffer)
7352       (save-restriction
7353         (message-narrow-to-head)
7354         (let ((groups (nnmail-article-group 'identity)))
7355           (unless silent
7356             (if groups
7357                 (message "This message would go to %s"
7358                          (mapconcat 'car groups ", "))
7359               (message "This message would go to no groups"))
7360             groups))))))
7361
7362 ;; Summary marking commands.
7363
7364 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
7365   "Mark articles which has the same subject as read, and then select the next.
7366 If UNMARK is positive, remove any kind of mark.
7367 If UNMARK is negative, tick articles."
7368   (interactive "P")
7369   (when unmark
7370     (setq unmark (prefix-numeric-value unmark)))
7371   (let ((count
7372          (gnus-summary-mark-same-subject
7373           (gnus-summary-article-subject) unmark)))
7374     ;; Select next unread article.  If auto-select-same mode, should
7375     ;; select the first unread article.
7376     (gnus-summary-next-article t (and gnus-auto-select-same
7377                                       (gnus-summary-article-subject)))
7378     (gnus-message 7 "%d article%s marked as %s"
7379                   count (if (= count 1) " is" "s are")
7380                   (if unmark "unread" "read"))))
7381
7382 (defun gnus-summary-kill-same-subject (&optional unmark)
7383   "Mark articles which has the same subject as read.
7384 If UNMARK is positive, remove any kind of mark.
7385 If UNMARK is negative, tick articles."
7386   (interactive "P")
7387   (when unmark
7388     (setq unmark (prefix-numeric-value unmark)))
7389   (let ((count
7390          (gnus-summary-mark-same-subject
7391           (gnus-summary-article-subject) unmark)))
7392     ;; If marked as read, go to next unread subject.
7393     (when (null unmark)
7394       ;; Go to next unread subject.
7395       (gnus-summary-next-subject 1 t))
7396     (gnus-message 7 "%d articles are marked as %s"
7397                   count (if unmark "unread" "read"))))
7398
7399 (defun gnus-summary-mark-same-subject (subject &optional unmark)
7400   "Mark articles with same SUBJECT as read, and return marked number.
7401 If optional argument UNMARK is positive, remove any kinds of marks.
7402 If optional argument UNMARK is negative, mark articles as unread instead."
7403   (let ((count 1))
7404     (save-excursion
7405       (cond
7406        ((null unmark)                   ; Mark as read.
7407         (while (and
7408                 (progn
7409                   (gnus-summary-mark-article-as-read gnus-killed-mark)
7410                   (gnus-summary-show-thread) t)
7411                 (gnus-summary-find-subject subject))
7412           (setq count (1+ count))))
7413        ((> unmark 0)                    ; Tick.
7414         (while (and
7415                 (progn
7416                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
7417                   (gnus-summary-show-thread) t)
7418                 (gnus-summary-find-subject subject))
7419           (setq count (1+ count))))
7420        (t                               ; Mark as unread.
7421         (while (and
7422                 (progn
7423                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
7424                   (gnus-summary-show-thread) t)
7425                 (gnus-summary-find-subject subject))
7426           (setq count (1+ count)))))
7427       (gnus-set-mode-line 'summary)
7428       ;; Return the number of marked articles.
7429       count)))
7430
7431 (defun gnus-summary-mark-as-processable (n &optional unmark)
7432   "Set the process mark on the next N articles.
7433 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
7434 the process mark instead.  The difference between N and the actual
7435 number of articles marked is returned."
7436   (interactive "p")
7437   (let ((backward (< n 0))
7438         (n (abs n)))
7439     (while (and
7440             (> n 0)
7441             (if unmark
7442                 (gnus-summary-remove-process-mark
7443                  (gnus-summary-article-number))
7444               (gnus-summary-set-process-mark (gnus-summary-article-number)))
7445             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
7446       (setq n (1- n)))
7447     (when (/= 0 n)
7448       (gnus-message 7 "No more articles"))
7449     (gnus-summary-recenter)
7450     (gnus-summary-position-point)
7451     n))
7452
7453 (defun gnus-summary-unmark-as-processable (n)
7454   "Remove the process mark from the next N articles.
7455 If N is negative, unmark backward instead.  The difference between N and
7456 the actual number of articles unmarked is returned."
7457   (interactive "p")
7458   (gnus-summary-mark-as-processable n t))
7459
7460 (defun gnus-summary-unmark-all-processable ()
7461   "Remove the process mark from all articles."
7462   (interactive)
7463   (save-excursion
7464     (while gnus-newsgroup-processable
7465       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
7466   (gnus-summary-position-point))
7467
7468 (defun gnus-summary-mark-as-expirable (n)
7469   "Mark N articles forward as expirable.
7470 If N is negative, mark backward instead.  The difference between N and
7471 the actual number of articles marked is returned."
7472   (interactive "p")
7473   (gnus-summary-mark-forward n gnus-expirable-mark))
7474
7475 (defun gnus-summary-mark-article-as-replied (article)
7476   "Mark ARTICLE replied and update the summary line."
7477   (push article gnus-newsgroup-replied)
7478   (let ((buffer-read-only nil))
7479     (when (gnus-summary-goto-subject article)
7480       (gnus-summary-update-secondary-mark article))))
7481
7482 (defun gnus-summary-set-bookmark (article)
7483   "Set a bookmark in current article."
7484   (interactive (list (gnus-summary-article-number)))
7485   (when (or (not (get-buffer gnus-article-buffer))
7486             (not gnus-current-article)
7487             (not gnus-article-current)
7488             (not (equal gnus-newsgroup-name (car gnus-article-current))))
7489     (error "No current article selected"))
7490   ;; Remove old bookmark, if one exists.
7491   (let ((old (assq article gnus-newsgroup-bookmarks)))
7492     (when old
7493       (setq gnus-newsgroup-bookmarks
7494             (delq old gnus-newsgroup-bookmarks))))
7495   ;; Set the new bookmark, which is on the form
7496   ;; (article-number . line-number-in-body).
7497   (push
7498    (cons article
7499          (save-excursion
7500            (set-buffer gnus-article-buffer)
7501            (count-lines
7502             (min (point)
7503                  (save-excursion
7504                    (goto-char (point-min))
7505                    (search-forward "\n\n" nil t)
7506                    (point)))
7507             (point))))
7508    gnus-newsgroup-bookmarks)
7509   (gnus-message 6 "A bookmark has been added to the current article."))
7510
7511 (defun gnus-summary-remove-bookmark (article)
7512   "Remove the bookmark from the current article."
7513   (interactive (list (gnus-summary-article-number)))
7514   ;; Remove old bookmark, if one exists.
7515   (let ((old (assq article gnus-newsgroup-bookmarks)))
7516     (if old
7517         (progn
7518           (setq gnus-newsgroup-bookmarks
7519                 (delq old gnus-newsgroup-bookmarks))
7520           (gnus-message 6 "Removed bookmark."))
7521       (gnus-message 6 "No bookmark in current article."))))
7522
7523 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7524 (defun gnus-summary-mark-as-dormant (n)
7525   "Mark N articles forward as dormant.
7526 If N is negative, mark backward instead.  The difference between N and
7527 the actual number of articles marked is returned."
7528   (interactive "p")
7529   (gnus-summary-mark-forward n gnus-dormant-mark))
7530
7531 (defun gnus-summary-set-process-mark (article)
7532   "Set the process mark on ARTICLE and update the summary line."
7533   (setq gnus-newsgroup-processable
7534         (cons article
7535               (delq article gnus-newsgroup-processable)))
7536   (when (gnus-summary-goto-subject article)
7537     (gnus-summary-show-thread)
7538     (gnus-summary-update-secondary-mark article)))
7539
7540 (defun gnus-summary-remove-process-mark (article)
7541   "Remove the process mark from ARTICLE and update the summary line."
7542   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
7543   (when (gnus-summary-goto-subject article)
7544     (gnus-summary-show-thread)
7545     (gnus-summary-update-secondary-mark article)))
7546
7547 (defun gnus-summary-set-saved-mark (article)
7548   "Set the process mark on ARTICLE and update the summary line."
7549   (push article gnus-newsgroup-saved)
7550   (when (gnus-summary-goto-subject article)
7551     (gnus-summary-update-secondary-mark article)))
7552
7553 (defun gnus-summary-mark-forward (n &optional mark no-expire)
7554   "Mark N articles as read forwards.
7555 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
7556 The difference between N and the actual number of articles marked is
7557 returned."
7558   (interactive "p")
7559   (let ((backward (< n 0))
7560         (gnus-summary-goto-unread
7561          (and gnus-summary-goto-unread
7562               (not (eq gnus-summary-goto-unread 'never))
7563               (not (memq mark (list gnus-unread-mark
7564                                     gnus-ticked-mark gnus-dormant-mark)))))
7565         (n (abs n))
7566         (mark (or mark gnus-del-mark)))
7567     (while (and (> n 0)
7568                 (gnus-summary-mark-article nil mark no-expire)
7569                 (zerop (gnus-summary-next-subject
7570                         (if backward -1 1)
7571                         (and gnus-summary-goto-unread
7572                              (not (eq gnus-summary-goto-unread 'never)))
7573                         t)))
7574       (setq n (1- n)))
7575     (when (/= 0 n)
7576       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
7577     (gnus-summary-recenter)
7578     (gnus-summary-position-point)
7579     (gnus-set-mode-line 'summary)
7580     n))
7581
7582 (defun gnus-summary-mark-article-as-read (mark)
7583   "Mark the current article quickly as read with MARK."
7584   (let ((article (gnus-summary-article-number)))
7585     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7586     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7587     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7588     (push (cons article mark) gnus-newsgroup-reads)
7589     ;; Possibly remove from cache, if that is used.
7590     (when gnus-use-cache
7591       (gnus-cache-enter-remove-article article))
7592     ;; Allow the backend to change the mark.
7593     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
7594     ;; Check for auto-expiry.
7595     (when (and gnus-newsgroup-auto-expire
7596                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7597                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7598                    (= mark gnus-ancient-mark)
7599                    (= mark gnus-read-mark) (= mark gnus-souped-mark)
7600                    (= mark gnus-duplicate-mark)))
7601       (setq mark gnus-expirable-mark)
7602       (push article gnus-newsgroup-expirable))
7603     ;; Set the mark in the buffer.
7604     (gnus-summary-update-mark mark 'unread)
7605     t))
7606
7607 (defun gnus-summary-mark-article-as-unread (mark)
7608   "Mark the current article quickly as unread with MARK."
7609   (let* ((article (gnus-summary-article-number))
7610          (old-mark (gnus-summary-article-mark article)))
7611     (if (eq mark old-mark)
7612         t
7613       (if (<= article 0)
7614           (progn
7615             (gnus-error 1 "Can't mark negative article numbers")
7616             nil)
7617         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7618         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7619         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
7620         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
7621         (cond ((= mark gnus-ticked-mark)
7622                (push article gnus-newsgroup-marked))
7623               ((= mark gnus-dormant-mark)
7624                (push article gnus-newsgroup-dormant))
7625               (t
7626                (push article gnus-newsgroup-unreads)))
7627         (gnus-pull article gnus-newsgroup-reads)
7628
7629         ;; See whether the article is to be put in the cache.
7630         (and gnus-use-cache
7631              (vectorp (gnus-summary-article-header article))
7632              (save-excursion
7633                (gnus-cache-possibly-enter-article
7634                 gnus-newsgroup-name article
7635                 (gnus-summary-article-header article)
7636                 (= mark gnus-ticked-mark)
7637                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7638
7639         ;; Fix the mark.
7640         (gnus-summary-update-mark mark 'unread)
7641         t))))
7642
7643 (defun gnus-summary-mark-article (&optional article mark no-expire)
7644   "Mark ARTICLE with MARK.  MARK can be any character.
7645 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
7646 `??' (dormant) and `?E' (expirable).
7647 If MARK is nil, then the default character `?D' is used.
7648 If ARTICLE is nil, then the article on the current line will be
7649 marked."
7650   ;; The mark might be a string.
7651   (when (stringp mark)
7652     (setq mark (aref mark 0)))
7653   ;; If no mark is given, then we check auto-expiring.
7654   (and (not no-expire)
7655        gnus-newsgroup-auto-expire
7656        (or (not mark)
7657            (and (gnus-characterp mark)
7658                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
7659                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
7660                     (= mark gnus-read-mark) (= mark gnus-souped-mark)
7661                     (= mark gnus-duplicate-mark))))
7662        (setq mark gnus-expirable-mark))
7663   (let* ((mark (or mark gnus-del-mark))
7664          (article (or article (gnus-summary-article-number)))
7665          (old-mark (gnus-summary-article-mark article)))
7666     (if (eq mark old-mark)
7667         t
7668       (unless article
7669         (error "No article on current line"))
7670       (if (not (if (or (= mark gnus-unread-mark)
7671                        (= mark gnus-ticked-mark)
7672                        (= mark gnus-dormant-mark))
7673                    (gnus-mark-article-as-unread article mark)
7674                  (gnus-mark-article-as-read article mark)))
7675           t
7676         ;; See whether the article is to be put in the cache.
7677         (and gnus-use-cache
7678              (not (= mark gnus-canceled-mark))
7679              (vectorp (gnus-summary-article-header article))
7680              (save-excursion
7681                (gnus-cache-possibly-enter-article
7682                 gnus-newsgroup-name article
7683                 (gnus-summary-article-header article)
7684                 (= mark gnus-ticked-mark)
7685                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
7686
7687         (when (gnus-summary-goto-subject article nil t)
7688           (let ((buffer-read-only nil))
7689             (gnus-summary-show-thread)
7690             ;; Fix the mark.
7691             (gnus-summary-update-mark mark 'unread)
7692             t))))))
7693
7694 (defun gnus-summary-update-secondary-mark (article)
7695   "Update the secondary (read, process, cache) mark."
7696   (gnus-summary-update-mark
7697    (cond ((memq article gnus-newsgroup-processable)
7698           gnus-process-mark)
7699          ((memq article gnus-newsgroup-cached)
7700           gnus-cached-mark)
7701          ((memq article gnus-newsgroup-replied)
7702           gnus-replied-mark)
7703          ((memq article gnus-newsgroup-saved)
7704           gnus-saved-mark)
7705          (t gnus-unread-mark))
7706    'replied)
7707   (when (gnus-visual-p 'summary-highlight 'highlight)
7708     (gnus-run-hooks 'gnus-summary-update-hook))
7709   t)
7710
7711 (defun gnus-summary-update-mark (mark type)
7712   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
7713         (buffer-read-only nil))
7714     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
7715     (when (looking-at "\r")
7716       (incf forward))
7717     (when (and forward
7718                (<= (+ forward (point)) (point-max)))
7719       ;; Go to the right position on the line.
7720       (goto-char (+ forward (point)))
7721       ;; Replace the old mark with the new mark.
7722       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
7723       ;; Optionally update the marks by some user rule.
7724       (when (eq type 'unread)
7725         (gnus-data-set-mark
7726          (gnus-data-find (gnus-summary-article-number)) mark)
7727         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
7728
7729 (defun gnus-mark-article-as-read (article &optional mark)
7730   "Enter ARTICLE in the pertinent lists and remove it from others."
7731   ;; Make the article expirable.
7732   (let ((mark (or mark gnus-del-mark)))
7733     (if (= mark gnus-expirable-mark)
7734         (push article gnus-newsgroup-expirable)
7735       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
7736     ;; Remove from unread and marked lists.
7737     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7738     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
7739     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
7740     (push (cons article mark) gnus-newsgroup-reads)
7741     ;; Possibly remove from cache, if that is used.
7742     (when gnus-use-cache
7743       (gnus-cache-enter-remove-article article))
7744     t))
7745
7746 (defun gnus-mark-article-as-unread (article &optional mark)
7747   "Enter ARTICLE in the pertinent lists and remove it from others."
7748   (let ((mark (or mark gnus-ticked-mark)))
7749     (if (<= article 0)
7750         (progn
7751           (gnus-error 1 "Can't mark negative article numbers")
7752           nil)
7753       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
7754             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
7755             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
7756             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
7757
7758       ;; Unsuppress duplicates?
7759       (when gnus-suppress-duplicates
7760         (gnus-dup-unsuppress-article article))
7761
7762       (cond ((= mark gnus-ticked-mark)
7763              (push article gnus-newsgroup-marked))
7764             ((= mark gnus-dormant-mark)
7765              (push article gnus-newsgroup-dormant))
7766             (t
7767              (push article gnus-newsgroup-unreads)))
7768       (gnus-pull article gnus-newsgroup-reads)
7769       t)))
7770
7771 (defalias 'gnus-summary-mark-as-unread-forward
7772   'gnus-summary-tick-article-forward)
7773 (make-obsolete 'gnus-summary-mark-as-unread-forward
7774                'gnus-summary-tick-article-forward)
7775 (defun gnus-summary-tick-article-forward (n)
7776   "Tick N articles forwards.
7777 If N is negative, tick backwards instead.
7778 The difference between N and the number of articles ticked is returned."
7779   (interactive "p")
7780   (gnus-summary-mark-forward n gnus-ticked-mark))
7781
7782 (defalias 'gnus-summary-mark-as-unread-backward
7783   'gnus-summary-tick-article-backward)
7784 (make-obsolete 'gnus-summary-mark-as-unread-backward
7785                'gnus-summary-tick-article-backward)
7786 (defun gnus-summary-tick-article-backward (n)
7787   "Tick N articles backwards.
7788 The difference between N and the number of articles ticked is returned."
7789   (interactive "p")
7790   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
7791
7792 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7793 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
7794 (defun gnus-summary-tick-article (&optional article clear-mark)
7795   "Mark current article as unread.
7796 Optional 1st argument ARTICLE specifies article number to be marked as unread.
7797 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
7798   (interactive)
7799   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
7800                                        gnus-ticked-mark)))
7801
7802 (defun gnus-summary-mark-as-read-forward (n)
7803   "Mark N articles as read forwards.
7804 If N is negative, mark backwards instead.
7805 The difference between N and the actual number of articles marked is
7806 returned."
7807   (interactive "p")
7808   (gnus-summary-mark-forward n gnus-del-mark t))
7809
7810 (defun gnus-summary-mark-as-read-backward (n)
7811   "Mark the N articles as read backwards.
7812 The difference between N and the actual number of articles marked is
7813 returned."
7814   (interactive "p")
7815   (gnus-summary-mark-forward (- n) gnus-del-mark t))
7816
7817 (defun gnus-summary-mark-as-read (&optional article mark)
7818   "Mark current article as read.
7819 ARTICLE specifies the article to be marked as read.
7820 MARK specifies a string to be inserted at the beginning of the line."
7821   (gnus-summary-mark-article article mark))
7822
7823 (defun gnus-summary-clear-mark-forward (n)
7824   "Clear marks from N articles forward.
7825 If N is negative, clear backward instead.
7826 The difference between N and the number of marks cleared is returned."
7827   (interactive "p")
7828   (gnus-summary-mark-forward n gnus-unread-mark))
7829
7830 (defun gnus-summary-clear-mark-backward (n)
7831   "Clear marks from N articles backward.
7832 The difference between N and the number of marks cleared is returned."
7833   (interactive "p")
7834   (gnus-summary-mark-forward (- n) gnus-unread-mark))
7835
7836 (defun gnus-summary-mark-unread-as-read ()
7837   "Intended to be used by `gnus-summary-mark-article-hook'."
7838   (when (memq gnus-current-article gnus-newsgroup-unreads)
7839     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
7840
7841 (defun gnus-summary-mark-read-and-unread-as-read ()
7842   "Intended to be used by `gnus-summary-mark-article-hook'."
7843   (let ((mark (gnus-summary-article-mark)))
7844     (when (or (gnus-unread-mark-p mark)
7845               (gnus-read-mark-p mark))
7846       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
7847
7848 (defun gnus-summary-mark-region-as-read (point mark all)
7849   "Mark all unread articles between point and mark as read.
7850 If given a prefix, mark all articles between point and mark as read,
7851 even ticked and dormant ones."
7852   (interactive "r\nP")
7853   (save-excursion
7854     (let (article)
7855       (goto-char point)
7856       (beginning-of-line)
7857       (while (and
7858               (< (point) mark)
7859               (progn
7860                 (when (or all
7861                           (memq (setq article (gnus-summary-article-number))
7862                                 gnus-newsgroup-unreads))
7863                   (gnus-summary-mark-article article gnus-del-mark))
7864                 t)
7865               (gnus-summary-find-next))))))
7866
7867 (defun gnus-summary-mark-below (score mark)
7868   "Mark articles with score less than SCORE with MARK."
7869   (interactive "P\ncMark: ")
7870   (setq score (if score
7871                   (prefix-numeric-value score)
7872                 (or gnus-summary-default-score 0)))
7873   (save-excursion
7874     (set-buffer gnus-summary-buffer)
7875     (goto-char (point-min))
7876     (while
7877         (progn
7878           (and (< (gnus-summary-article-score) score)
7879                (gnus-summary-mark-article nil mark))
7880           (gnus-summary-find-next)))))
7881
7882 (defun gnus-summary-kill-below (&optional score)
7883   "Mark articles with score below SCORE as read."
7884   (interactive "P")
7885   (gnus-summary-mark-below score gnus-killed-mark))
7886
7887 (defun gnus-summary-clear-above (&optional score)
7888   "Clear all marks from articles with score above SCORE."
7889   (interactive "P")
7890   (gnus-summary-mark-above score gnus-unread-mark))
7891
7892 (defun gnus-summary-tick-above (&optional score)
7893   "Tick all articles with score above SCORE."
7894   (interactive "P")
7895   (gnus-summary-mark-above score gnus-ticked-mark))
7896
7897 (defun gnus-summary-mark-above (score mark)
7898   "Mark articles with score over SCORE with MARK."
7899   (interactive "P\ncMark: ")
7900   (setq score (if score
7901                   (prefix-numeric-value score)
7902                 (or gnus-summary-default-score 0)))
7903   (save-excursion
7904     (set-buffer gnus-summary-buffer)
7905     (goto-char (point-min))
7906     (while (and (progn
7907                   (when (> (gnus-summary-article-score) score)
7908                     (gnus-summary-mark-article nil mark))
7909                   t)
7910                 (gnus-summary-find-next)))))
7911
7912 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
7913 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
7914 (defun gnus-summary-limit-include-expunged (&optional no-error)
7915   "Display all the hidden articles that were expunged for low scores."
7916   (interactive)
7917   (let ((buffer-read-only nil))
7918     (let ((scored gnus-newsgroup-scored)
7919           headers h)
7920       (while scored
7921         (unless (gnus-summary-goto-subject (caar scored))
7922           (and (setq h (gnus-summary-article-header (caar scored)))
7923                (< (cdar scored) gnus-summary-expunge-below)
7924                (push h headers)))
7925         (setq scored (cdr scored)))
7926       (if (not headers)
7927           (when (not no-error)
7928             (error "No expunged articles hidden"))
7929         (goto-char (point-min))
7930         (gnus-summary-prepare-unthreaded (nreverse headers))
7931         (goto-char (point-min))
7932         (gnus-summary-position-point)
7933         t))))
7934
7935 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
7936   "Mark all unread articles in this newsgroup as read.
7937 If prefix argument ALL is non-nil, ticked and dormant articles will
7938 also be marked as read.
7939 If QUIETLY is non-nil, no questions will be asked.
7940 If TO-HERE is non-nil, it should be a point in the buffer.  All
7941 articles before this point will be marked as read.
7942 Note that this function will only catch up the unread article
7943 in the current summary buffer limitation.
7944 The number of articles marked as read is returned."
7945   (interactive "P")
7946   (prog1
7947       (save-excursion
7948         (when (or quietly
7949                   (not gnus-interactive-catchup) ;Without confirmation?
7950                   gnus-expert-user
7951                   (gnus-y-or-n-p
7952                    (if all
7953                        "Mark absolutely all articles as read? "
7954                      "Mark all unread articles as read? ")))
7955           (if (and not-mark
7956                    (not gnus-newsgroup-adaptive)
7957                    (not gnus-newsgroup-auto-expire)
7958                    (not gnus-suppress-duplicates)
7959                    (or (not gnus-use-cache)
7960                        (eq gnus-use-cache 'passive)))
7961               (progn
7962                 (when all
7963                   (setq gnus-newsgroup-marked nil
7964                         gnus-newsgroup-dormant nil))
7965                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
7966             ;; We actually mark all articles as canceled, which we
7967             ;; have to do when using auto-expiry or adaptive scoring.
7968             (gnus-summary-show-all-threads)
7969             (when (gnus-summary-first-subject (not all) t)
7970               (while (and
7971                       (if to-here (< (point) to-here) t)
7972                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
7973                       (gnus-summary-find-next (not all) nil nil t))))
7974             (gnus-set-mode-line 'summary))
7975           t))
7976     (gnus-summary-position-point)))
7977
7978 (defun gnus-summary-catchup-to-here (&optional all)
7979   "Mark all unticked articles before the current one as read.
7980 If ALL is non-nil, also mark ticked and dormant articles as read."
7981   (interactive "P")
7982   (save-excursion
7983     (gnus-save-hidden-threads
7984       (let ((beg (point)))
7985         ;; We check that there are unread articles.
7986         (when (or all (gnus-summary-find-prev))
7987           (gnus-summary-catchup all t beg)))))
7988   (gnus-summary-position-point))
7989
7990 (defun gnus-summary-catchup-all (&optional quietly)
7991   "Mark all articles in this newsgroup as read."
7992   (interactive "P")
7993   (gnus-summary-catchup t quietly))
7994
7995 (defun gnus-summary-catchup-and-exit (&optional all quietly)
7996   "Mark all articles not marked as unread in this newsgroup as read, then exit.
7997 If prefix argument ALL is non-nil, all articles are marked as read."
7998   (interactive "P")
7999   (when (gnus-summary-catchup all quietly nil 'fast)
8000     ;; Select next newsgroup or exit.
8001     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8002              (eq gnus-auto-select-next 'quietly))
8003         (gnus-summary-next-group nil)
8004       (gnus-summary-exit))))
8005
8006 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8007   "Mark all articles in this newsgroup as read, and then exit."
8008   (interactive "P")
8009   (gnus-summary-catchup-and-exit t quietly))
8010
8011 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
8012 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8013   "Mark all articles in this group as read and select the next group.
8014 If given a prefix, mark all articles, unread as well as ticked, as
8015 read."
8016   (interactive "P")
8017   (save-excursion
8018     (gnus-summary-catchup all))
8019   (gnus-summary-next-article t nil nil t))
8020
8021 ;; Thread-based commands.
8022
8023 (defun gnus-summary-articles-in-thread (&optional article)
8024   "Return a list of all articles in the current thread.
8025 If ARTICLE is non-nil, return all articles in the thread that starts
8026 with that article."
8027   (let* ((article (or article (gnus-summary-article-number)))
8028          (data (gnus-data-find-list article))
8029          (top-level (gnus-data-level (car data)))
8030          (top-subject
8031           (cond ((null gnus-thread-operation-ignore-subject)
8032                  (gnus-simplify-subject-re
8033                   (mail-header-subject (gnus-data-header (car data)))))
8034                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8035                  (gnus-simplify-subject-fuzzy
8036                   (mail-header-subject (gnus-data-header (car data)))))
8037                 (t nil)))
8038          (end-point (save-excursion
8039                       (if (gnus-summary-go-to-next-thread)
8040                           (point) (point-max))))
8041          articles)
8042     (while (and data
8043                 (< (gnus-data-pos (car data)) end-point))
8044       (when (or (not top-subject)
8045                 (string= top-subject
8046                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8047                              (gnus-simplify-subject-fuzzy
8048                               (mail-header-subject
8049                                (gnus-data-header (car data))))
8050                            (gnus-simplify-subject-re
8051                             (mail-header-subject
8052                              (gnus-data-header (car data)))))))
8053         (push (gnus-data-number (car data)) articles))
8054       (unless (and (setq data (cdr data))
8055                    (> (gnus-data-level (car data)) top-level))
8056         (setq data nil)))
8057     ;; Return the list of articles.
8058     (nreverse articles)))
8059
8060 (defun gnus-summary-rethread-current ()
8061   "Rethread the thread the current article is part of."
8062   (interactive)
8063   (let* ((gnus-show-threads t)
8064          (article (gnus-summary-article-number))
8065          (id (mail-header-id (gnus-summary-article-header)))
8066          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8067     (unless id
8068       (error "No article on the current line"))
8069     (gnus-rebuild-thread id)
8070     (gnus-summary-goto-subject article)))
8071
8072 (defun gnus-summary-reparent-thread ()
8073   "Make the current article child of the marked (or previous) article.
8074
8075 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8076 is non-nil or the Subject: of both articles are the same."
8077   (interactive)
8078   (unless (not (gnus-group-read-only-p))
8079     (error "The current newsgroup does not support article editing"))
8080   (unless (<= (length gnus-newsgroup-processable) 1)
8081     (error "No more than one article may be marked"))
8082   (save-window-excursion
8083     (let ((gnus-article-buffer " *reparent*")
8084           (current-article (gnus-summary-article-number))
8085           ;; First grab the marked article, otherwise one line up.
8086           (parent-article (if (not (null gnus-newsgroup-processable))
8087                               (car gnus-newsgroup-processable)
8088                             (save-excursion
8089                               (if (eq (forward-line -1) 0)
8090                                   (gnus-summary-article-number)
8091                                 (error "Beginning of summary buffer"))))))
8092       (unless (not (eq current-article parent-article))
8093         (error "An article may not be self-referential"))
8094       (let ((message-id (mail-header-id
8095                          (gnus-summary-article-header parent-article))))
8096         (unless (and message-id (not (equal message-id "")))
8097           (error "No message-id in desired parent"))
8098         ;; We don't want the article to be marked as read.
8099         (let (gnus-mark-article-hook)
8100           (gnus-summary-select-article t t nil current-article))
8101         (set-buffer gnus-original-article-buffer)
8102         (let ((buf (format "%s" (buffer-string))))
8103           (nnheader-temp-write nil
8104             (insert buf)
8105             (goto-char (point-min))
8106             (if (re-search-forward "^References: " nil t)
8107                 (progn
8108                   (re-search-forward "^[^ \t]" nil t)
8109                   (forward-line -1)
8110                   (end-of-line)
8111                   (insert " " message-id))
8112               (insert "References: " message-id "\n"))
8113             (unless (gnus-request-replace-article
8114                      current-article (car gnus-article-current)
8115                      (current-buffer))
8116               (error "Couldn't replace article"))))
8117         (set-buffer gnus-summary-buffer)
8118         (gnus-summary-unmark-all-processable)
8119         (gnus-summary-update-article current-article)
8120         (gnus-summary-rethread-current)
8121         (gnus-message 3 "Article %d is now the child of article %d"
8122                       current-article parent-article)))))
8123
8124 (defun gnus-summary-toggle-threads (&optional arg)
8125   "Toggle showing conversation threads.
8126 If ARG is positive number, turn showing conversation threads on."
8127   (interactive "P")
8128   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8129     (setq gnus-show-threads
8130           (if (null arg) (not gnus-show-threads)
8131             (> (prefix-numeric-value arg) 0)))
8132     (gnus-summary-prepare)
8133     (gnus-summary-goto-subject current)
8134     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8135     (gnus-summary-position-point)))
8136
8137 (defun gnus-summary-show-all-threads ()
8138   "Show all threads."
8139   (interactive)
8140   (save-excursion
8141     (let ((buffer-read-only nil))
8142       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8143   (gnus-summary-position-point))
8144
8145 (defun gnus-summary-show-thread ()
8146   "Show thread subtrees.
8147 Returns nil if no thread was there to be shown."
8148   (interactive)
8149   (let ((buffer-read-only nil)
8150         (orig (point))
8151         ;; first goto end then to beg, to have point at beg after let
8152         (end (progn (end-of-line) (point)))
8153         (beg (progn (beginning-of-line) (point))))
8154     (prog1
8155         ;; Any hidden lines here?
8156         (search-forward "\r" end t)
8157       (subst-char-in-region beg end ?\^M ?\n t)
8158       (goto-char orig)
8159       (gnus-summary-position-point))))
8160
8161 (defun gnus-summary-hide-all-threads ()
8162   "Hide all thread subtrees."
8163   (interactive)
8164   (save-excursion
8165     (goto-char (point-min))
8166     (gnus-summary-hide-thread)
8167     (while (zerop (gnus-summary-next-thread 1 t))
8168       (gnus-summary-hide-thread)))
8169   (gnus-summary-position-point))
8170
8171 (defun gnus-summary-hide-thread ()
8172   "Hide thread subtrees.
8173 Returns nil if no threads were there to be hidden."
8174   (interactive)
8175   (let ((buffer-read-only nil)
8176         (start (point))
8177         (article (gnus-summary-article-number)))
8178     (goto-char start)
8179     ;; Go forward until either the buffer ends or the subthread
8180     ;; ends.
8181     (when (and (not (eobp))
8182                (or (zerop (gnus-summary-next-thread 1 t))
8183                    (goto-char (point-max))))
8184       (prog1
8185           (if (and (> (point) start)
8186                    (search-backward "\n" start t))
8187               (progn
8188                 (subst-char-in-region start (point) ?\n ?\^M)
8189                 (gnus-summary-goto-subject article))
8190             (goto-char start)
8191             nil)
8192         ;;(gnus-summary-position-point)
8193         ))))
8194
8195 (defun gnus-summary-go-to-next-thread (&optional previous)
8196   "Go to the same level (or less) next thread.
8197 If PREVIOUS is non-nil, go to previous thread instead.
8198 Return the article number moved to, or nil if moving was impossible."
8199   (let ((level (gnus-summary-thread-level))
8200         (way (if previous -1 1))
8201         (beg (point)))
8202     (forward-line way)
8203     (while (and (not (eobp))
8204                 (< level (gnus-summary-thread-level)))
8205       (forward-line way))
8206     (if (eobp)
8207         (progn
8208           (goto-char beg)
8209           nil)
8210       (setq beg (point))
8211       (prog1
8212           (gnus-summary-article-number)
8213         (goto-char beg)))))
8214
8215 (defun gnus-summary-next-thread (n &optional silent)
8216   "Go to the same level next N'th thread.
8217 If N is negative, search backward instead.
8218 Returns the difference between N and the number of skips actually
8219 done.
8220
8221 If SILENT, don't output messages."
8222   (interactive "p")
8223   (let ((backward (< n 0))
8224         (n (abs n)))
8225     (while (and (> n 0)
8226                 (gnus-summary-go-to-next-thread backward))
8227       (decf n))
8228     (unless silent
8229       (gnus-summary-position-point))
8230     (when (and (not silent) (/= 0 n))
8231       (gnus-message 7 "No more threads"))
8232     n))
8233
8234 (defun gnus-summary-prev-thread (n)
8235   "Go to the same level previous N'th thread.
8236 Returns the difference between N and the number of skips actually
8237 done."
8238   (interactive "p")
8239   (gnus-summary-next-thread (- n)))
8240
8241 (defun gnus-summary-go-down-thread ()
8242   "Go down one level in the current thread."
8243   (let ((children (gnus-summary-article-children)))
8244     (when children
8245       (gnus-summary-goto-subject (car children)))))
8246
8247 (defun gnus-summary-go-up-thread ()
8248   "Go up one level in the current thread."
8249   (let ((parent (gnus-summary-article-parent)))
8250     (when parent
8251       (gnus-summary-goto-subject parent))))
8252
8253 (defun gnus-summary-down-thread (n)
8254   "Go down thread N steps.
8255 If N is negative, go up instead.
8256 Returns the difference between N and how many steps down that were
8257 taken."
8258   (interactive "p")
8259   (let ((up (< n 0))
8260         (n (abs n)))
8261     (while (and (> n 0)
8262                 (if up (gnus-summary-go-up-thread)
8263                   (gnus-summary-go-down-thread)))
8264       (setq n (1- n)))
8265     (gnus-summary-position-point)
8266     (when (/= 0 n)
8267       (gnus-message 7 "Can't go further"))
8268     n))
8269
8270 (defun gnus-summary-up-thread (n)
8271   "Go up thread N steps.
8272 If N is negative, go up instead.
8273 Returns the difference between N and how many steps down that were
8274 taken."
8275   (interactive "p")
8276   (gnus-summary-down-thread (- n)))
8277
8278 (defun gnus-summary-top-thread ()
8279   "Go to the top of the thread."
8280   (interactive)
8281   (while (gnus-summary-go-up-thread))
8282   (gnus-summary-article-number))
8283
8284 (defun gnus-summary-kill-thread (&optional unmark)
8285   "Mark articles under current thread as read.
8286 If the prefix argument is positive, remove any kinds of marks.
8287 If the prefix argument is negative, tick articles instead."
8288   (interactive "P")
8289   (when unmark
8290     (setq unmark (prefix-numeric-value unmark)))
8291   (let ((articles (gnus-summary-articles-in-thread)))
8292     (save-excursion
8293       ;; Expand the thread.
8294       (gnus-summary-show-thread)
8295       ;; Mark all the articles.
8296       (while articles
8297         (gnus-summary-goto-subject (car articles))
8298         (cond ((null unmark)
8299                (gnus-summary-mark-article-as-read gnus-killed-mark))
8300               ((> unmark 0)
8301                (gnus-summary-mark-article-as-unread gnus-unread-mark))
8302               (t
8303                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
8304         (setq articles (cdr articles))))
8305     ;; Hide killed subtrees.
8306     (and (null unmark)
8307          gnus-thread-hide-killed
8308          (gnus-summary-hide-thread))
8309     ;; If marked as read, go to next unread subject.
8310     (when (null unmark)
8311       ;; Go to next unread subject.
8312       (gnus-summary-next-subject 1 t)))
8313   (gnus-set-mode-line 'summary))
8314
8315 ;; Summary sorting commands
8316
8317 (defun gnus-summary-sort-by-number (&optional reverse)
8318   "Sort the summary buffer by article number.
8319 Argument REVERSE means reverse order."
8320   (interactive "P")
8321   (gnus-summary-sort 'number reverse))
8322
8323 (defun gnus-summary-sort-by-author (&optional reverse)
8324   "Sort the summary buffer by author name alphabetically.
8325 If case-fold-search is non-nil, case of letters is ignored.
8326 Argument REVERSE means reverse order."
8327   (interactive "P")
8328   (gnus-summary-sort 'author reverse))
8329
8330 (defun gnus-summary-sort-by-subject (&optional reverse)
8331   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
8332 If case-fold-search is non-nil, case of letters is ignored.
8333 Argument REVERSE means reverse order."
8334   (interactive "P")
8335   (gnus-summary-sort 'subject reverse))
8336
8337 (defun gnus-summary-sort-by-date (&optional reverse)
8338   "Sort the summary buffer by date.
8339 Argument REVERSE means reverse order."
8340   (interactive "P")
8341   (gnus-summary-sort 'date reverse))
8342
8343 (defun gnus-summary-sort-by-score (&optional reverse)
8344   "Sort the summary buffer by score.
8345 Argument REVERSE means reverse order."
8346   (interactive "P")
8347   (gnus-summary-sort 'score reverse))
8348
8349 (defun gnus-summary-sort-by-lines (&optional reverse)
8350   "Sort the summary buffer by article length.
8351 Argument REVERSE means reverse order."
8352   (interactive "P")
8353   (gnus-summary-sort 'lines reverse))
8354
8355 (defun gnus-summary-sort (predicate reverse)
8356   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
8357   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
8358          (article (intern (format "gnus-article-sort-by-%s" predicate)))
8359          (gnus-thread-sort-functions
8360           (list
8361            (if (not reverse)
8362                thread
8363              `(lambda (t1 t2)
8364                 (,thread t2 t1)))))
8365          (gnus-article-sort-functions
8366           (list
8367            (if (not reverse)
8368                article
8369              `(lambda (t1 t2)
8370                 (,article t2 t1)))))
8371          (buffer-read-only)
8372          (gnus-summary-prepare-hook nil))
8373     ;; We do the sorting by regenerating the threads.
8374     (gnus-summary-prepare)
8375     ;; Hide subthreads if needed.
8376     (when (and gnus-show-threads gnus-thread-hide-subtree)
8377       (gnus-summary-hide-all-threads))))
8378
8379 ;; Summary saving commands.
8380
8381 (defun gnus-summary-save-article (&optional n not-saved)
8382   "Save the current article using the default saver function.
8383 If N is a positive number, save the N next articles.
8384 If N is a negative number, save the N previous articles.
8385 If N is nil and any articles have been marked with the process mark,
8386 save those articles instead.
8387 The variable `gnus-default-article-saver' specifies the saver function."
8388   (interactive "P")
8389   (let* ((articles (gnus-summary-work-articles n))
8390          (save-buffer (save-excursion
8391                         (nnheader-set-temp-buffer " *Gnus Save*")))
8392          (num (length articles))
8393          header article file)
8394     (while articles
8395       (setq header (gnus-summary-article-header
8396                     (setq article (pop articles))))
8397       (if (not (vectorp header))
8398           ;; This is a pseudo-article.
8399           (if (assq 'name header)
8400               (gnus-copy-file (cdr (assq 'name header)))
8401             (gnus-message 1 "Article %d is unsaveable" article))
8402         ;; This is a real article.
8403         (save-window-excursion
8404           (gnus-summary-select-article t nil nil article))
8405         (save-excursion
8406           (set-buffer save-buffer)
8407           (erase-buffer)
8408           (insert-buffer-substring gnus-original-article-buffer))
8409         (setq file (gnus-article-save save-buffer file num))
8410         (gnus-summary-remove-process-mark article)
8411         (unless not-saved
8412           (gnus-summary-set-saved-mark article))))
8413     (gnus-kill-buffer save-buffer)
8414     (gnus-summary-position-point)
8415     (gnus-set-mode-line 'summary)
8416     n))
8417
8418 (defun gnus-summary-pipe-output (&optional arg)
8419   "Pipe the current article to a subprocess.
8420 If N is a positive number, pipe the N next articles.
8421 If N is a negative number, pipe the N previous articles.
8422 If N is nil and any articles have been marked with the process mark,
8423 pipe those articles instead."
8424   (interactive "P")
8425   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
8426     (gnus-summary-save-article arg t))
8427   (gnus-configure-windows 'pipe))
8428
8429 (defun gnus-summary-save-article-mail (&optional arg)
8430   "Append the current article to an mail file.
8431 If N is a positive number, save the N next articles.
8432 If N is a negative number, save the N previous articles.
8433 If N is nil and any articles have been marked with the process mark,
8434 save those articles instead."
8435   (interactive "P")
8436   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
8437     (gnus-summary-save-article arg)))
8438
8439 (defun gnus-summary-save-article-rmail (&optional arg)
8440   "Append the current article to an rmail file.
8441 If N is a positive number, save the N next articles.
8442 If N is a negative number, save the N previous articles.
8443 If N is nil and any articles have been marked with the process mark,
8444 save those articles instead."
8445   (interactive "P")
8446   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
8447     (gnus-summary-save-article arg)))
8448
8449 (defun gnus-summary-save-article-file (&optional arg)
8450   "Append the current article to a file.
8451 If N is a positive number, save the N next articles.
8452 If N is a negative number, save the N previous articles.
8453 If N is nil and any articles have been marked with the process mark,
8454 save those articles instead."
8455   (interactive "P")
8456   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
8457     (gnus-summary-save-article arg)))
8458
8459 (defun gnus-summary-write-article-file (&optional arg)
8460   "Write the current article to a file, deleting the previous file.
8461 If N is a positive number, save the N next articles.
8462 If N is a negative number, save the N previous articles.
8463 If N is nil and any articles have been marked with the process mark,
8464 save those articles instead."
8465   (interactive "P")
8466   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
8467     (gnus-summary-save-article arg)))
8468
8469 (defun gnus-summary-save-article-body-file (&optional arg)
8470   "Append the current article body to a file.
8471 If N is a positive number, save the N next articles.
8472 If N is a negative number, save the N previous articles.
8473 If N is nil and any articles have been marked with the process mark,
8474 save those articles instead."
8475   (interactive "P")
8476   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
8477     (gnus-summary-save-article arg)))
8478
8479 (defun gnus-summary-pipe-message (program)
8480   "Pipe the current article through PROGRAM."
8481   (interactive "sProgram: ")
8482   (gnus-summary-select-article)
8483   (let ((mail-header-separator "")
8484         (art-buf (get-buffer gnus-article-buffer)))
8485     (gnus-eval-in-buffer-window gnus-article-buffer
8486       (save-restriction
8487         (widen)
8488         (let ((start (window-start))
8489               buffer-read-only)
8490           (message-pipe-buffer-body program)
8491           (set-window-start (get-buffer-window (current-buffer)) start))))))
8492
8493 (defun gnus-get-split-value (methods)
8494   "Return a value based on the split METHODS."
8495   (let (split-name method result match)
8496     (when methods
8497       (save-excursion
8498         (set-buffer gnus-original-article-buffer)
8499         (save-restriction
8500           (nnheader-narrow-to-headers)
8501           (while methods
8502             (goto-char (point-min))
8503             (setq method (pop methods))
8504             (setq match (car method))
8505             (when (cond
8506                    ((stringp match)
8507                     ;; Regular expression.
8508                     (ignore-errors
8509                       (re-search-forward match nil t)))
8510                    ((gnus-functionp match)
8511                     ;; Function.
8512                     (save-restriction
8513                       (widen)
8514                       (setq result (funcall match gnus-newsgroup-name))))
8515                    ((consp match)
8516                     ;; Form.
8517                     (save-restriction
8518                       (widen)
8519                       (setq result (eval match)))))
8520               (setq split-name (append (cdr method) split-name))
8521               (cond ((stringp result)
8522                      (push (expand-file-name
8523                             result gnus-article-save-directory)
8524                            split-name))
8525                     ((consp result)
8526                      (setq split-name (append result split-name)))))))))
8527     split-name))
8528
8529 (defun gnus-valid-move-group-p (group)
8530   (and (boundp group)
8531        (symbol-name group)
8532        (memq 'respool
8533              (assoc (symbol-name
8534                      (car (gnus-find-method-for-group
8535                            (symbol-name group))))
8536                     gnus-valid-select-methods))))
8537
8538 (defun gnus-read-move-group-name (prompt default articles prefix)
8539   "Read a group name."
8540   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
8541          (minibuffer-confirm-incomplete nil) ; XEmacs
8542          (prom
8543           (format "%s %s to:"
8544                   prompt
8545                   (if (> (length articles) 1)
8546                       (format "these %d articles" (length articles))
8547                     "this article")))
8548          (to-newsgroup
8549           (cond
8550            ((null split-name)
8551             (gnus-completing-read default prom
8552                                   gnus-active-hashtb
8553                                   'gnus-valid-move-group-p
8554                                   nil prefix
8555                                   'gnus-group-history))
8556            ((= 1 (length split-name))
8557             (gnus-completing-read (car split-name) prom
8558                                   gnus-active-hashtb
8559                                   'gnus-valid-move-group-p
8560                                   nil nil
8561                                   'gnus-group-history))
8562            (t
8563             (gnus-completing-read nil prom
8564                                   (mapcar (lambda (el) (list el))
8565                                           (nreverse split-name))
8566                                   nil nil nil
8567                                   'gnus-group-history)))))
8568     (when to-newsgroup
8569       (if (or (string= to-newsgroup "")
8570               (string= to-newsgroup prefix))
8571           (setq to-newsgroup default))
8572       (unless to-newsgroup
8573         (error "No group name entered"))
8574       (or (gnus-active to-newsgroup)
8575           (gnus-activate-group to-newsgroup)
8576           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
8577                                      to-newsgroup))
8578               (or (and (gnus-request-create-group
8579                         to-newsgroup (gnus-group-name-to-method to-newsgroup))
8580                        (gnus-activate-group to-newsgroup nil nil
8581                                             (gnus-group-name-to-method
8582                                              to-newsgroup)))
8583                   (error "Couldn't create group %s" to-newsgroup)))
8584           (error "No such group: %s" to-newsgroup)))
8585     to-newsgroup))
8586
8587 ;; Summary extract commands
8588
8589 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
8590   (let ((buffer-read-only nil)
8591         (article (gnus-summary-article-number))
8592         after-article b e)
8593     (unless (gnus-summary-goto-subject article)
8594       (error "No such article: %d" article))
8595     (gnus-summary-position-point)
8596     ;; If all commands are to be bunched up on one line, we collect
8597     ;; them here.
8598     (unless gnus-view-pseudos-separately
8599       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
8600             files action)
8601         (while ps
8602           (setq action (cdr (assq 'action (car ps))))
8603           (setq files (list (cdr (assq 'name (car ps)))))
8604           (while (and ps (cdr ps)
8605                       (string= (or action "1")
8606                                (or (cdr (assq 'action (cadr ps))) "2")))
8607             (push (cdr (assq 'name (cadr ps))) files)
8608             (setcdr ps (cddr ps)))
8609           (when files
8610             (when (not (string-match "%s" action))
8611               (push " " files))
8612             (push " " files)
8613             (when (assq 'execute (car ps))
8614               (setcdr (assq 'execute (car ps))
8615                       (funcall (if (string-match "%s" action)
8616                                    'format 'concat)
8617                                action
8618                                (mapconcat
8619                                 (lambda (f)
8620                                   (if (equal f " ")
8621                                       f
8622                                     (gnus-quote-arg-for-sh-or-csh f)))
8623                                 files " ")))))
8624           (setq ps (cdr ps)))))
8625     (if (and gnus-view-pseudos (not not-view))
8626         (while pslist
8627           (when (assq 'execute (car pslist))
8628             (gnus-execute-command (cdr (assq 'execute (car pslist)))
8629                                   (eq gnus-view-pseudos 'not-confirm)))
8630           (setq pslist (cdr pslist)))
8631       (save-excursion
8632         (while pslist
8633           (setq after-article (or (cdr (assq 'article (car pslist)))
8634                                   (gnus-summary-article-number)))
8635           (gnus-summary-goto-subject after-article)
8636           (forward-line 1)
8637           (setq b (point))
8638           (insert "    " (file-name-nondirectory
8639                           (cdr (assq 'name (car pslist))))
8640                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
8641           (setq e (point))
8642           (forward-line -1)             ; back to `b'
8643           (gnus-add-text-properties
8644            b (1- e) (list 'gnus-number gnus-reffed-article-number
8645                           gnus-mouse-face-prop gnus-mouse-face))
8646           (gnus-data-enter
8647            after-article gnus-reffed-article-number
8648            gnus-unread-mark b (car pslist) 0 (- e b))
8649           (push gnus-reffed-article-number gnus-newsgroup-unreads)
8650           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
8651           (setq pslist (cdr pslist)))))))
8652
8653 (defun gnus-pseudos< (p1 p2)
8654   (let ((c1 (cdr (assq 'action p1)))
8655         (c2 (cdr (assq 'action p2))))
8656     (and c1 c2 (string< c1 c2))))
8657
8658 (defun gnus-request-pseudo-article (props)
8659   (cond ((assq 'execute props)
8660          (gnus-execute-command (cdr (assq 'execute props)))))
8661   (let ((gnus-current-article (gnus-summary-article-number)))
8662     (gnus-run-hooks 'gnus-mark-article-hook)))
8663
8664 (defun gnus-execute-command (command &optional automatic)
8665   (save-excursion
8666     (gnus-article-setup-buffer)
8667     (set-buffer gnus-article-buffer)
8668     (setq buffer-read-only nil)
8669     (let ((command (if automatic command
8670                      (read-string "Command: " (cons command 0)))))
8671       (erase-buffer)
8672       (insert "$ " command "\n\n")
8673       (if gnus-view-pseudo-asynchronously
8674           (start-process "gnus-execute" (current-buffer) shell-file-name
8675                          shell-command-switch command)
8676         (call-process shell-file-name nil t nil
8677                       shell-command-switch command)))))
8678
8679 ;; Summary kill commands.
8680
8681 (defun gnus-summary-edit-global-kill (article)
8682   "Edit the \"global\" kill file."
8683   (interactive (list (gnus-summary-article-number)))
8684   (gnus-group-edit-global-kill article))
8685
8686 (defun gnus-summary-edit-local-kill ()
8687   "Edit a local kill file applied to the current newsgroup."
8688   (interactive)
8689   (setq gnus-current-headers (gnus-summary-article-header))
8690   (gnus-group-edit-local-kill
8691    (gnus-summary-article-number) gnus-newsgroup-name))
8692
8693 ;;; Header reading.
8694
8695 (defun gnus-read-header (id &optional header)
8696   "Read the headers of article ID and enter them into the Gnus system."
8697   (let ((group gnus-newsgroup-name)
8698         (gnus-override-method
8699          (and (gnus-news-group-p gnus-newsgroup-name)
8700               gnus-refer-article-method))
8701         where)
8702     ;; First we check to see whether the header in question is already
8703     ;; fetched.
8704     (if (stringp id)
8705         ;; This is a Message-ID.
8706         (setq header (or header (gnus-id-to-header id)))
8707       ;; This is an article number.
8708       (setq header (or header (gnus-summary-article-header id))))
8709     (if (and header
8710              (not (gnus-summary-article-sparse-p (mail-header-number header))))
8711         ;; We have found the header.
8712         header
8713       ;; If this is a sparse article, we have to nix out its
8714       ;; previous entry in the thread hashtb.
8715       (when (and header
8716                  (gnus-summary-article-sparse-p (mail-header-number header)))
8717         (let* ((parent (gnus-parent-id (mail-header-references header)))
8718                (thread (and parent (gnus-id-to-thread parent))))
8719           (when thread
8720             (delq (assq header thread) thread))))
8721       ;; We have to really fetch the header to this article.
8722       (save-excursion
8723         (set-buffer nntp-server-buffer)
8724         (when (setq where (gnus-request-head id group))
8725           (nnheader-fold-continuation-lines)
8726           (goto-char (point-max))
8727           (insert ".\n")
8728           (goto-char (point-min))
8729           (insert "211 ")
8730           (princ (cond
8731                   ((numberp id) id)
8732                   ((cdr where) (cdr where))
8733                   (header (mail-header-number header))
8734                   (t gnus-reffed-article-number))
8735                  (current-buffer))
8736           (insert " Article retrieved.\n"))
8737         (if (or (not where)
8738                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
8739             ()                          ; Malformed head.
8740           (unless (gnus-summary-article-sparse-p (mail-header-number header))
8741             (when (and (stringp id)
8742                        (not (string= (gnus-group-real-name group)
8743                                      (car where))))
8744               ;; If we fetched by Message-ID and the article came
8745               ;; from a different group, we fudge some bogus article
8746               ;; numbers for this article.
8747               (mail-header-set-number header gnus-reffed-article-number))
8748             (save-excursion
8749               (set-buffer gnus-summary-buffer)
8750               (decf gnus-reffed-article-number)
8751               (gnus-remove-header (mail-header-number header))
8752               (push header gnus-newsgroup-headers)
8753               (setq gnus-current-headers header)
8754               (push (mail-header-number header) gnus-newsgroup-limit)))
8755           header)))))
8756
8757 (defun gnus-remove-header (number)
8758   "Remove header NUMBER from `gnus-newsgroup-headers'."
8759   (if (and gnus-newsgroup-headers
8760            (= number (mail-header-number (car gnus-newsgroup-headers))))
8761       (pop gnus-newsgroup-headers)
8762     (let ((headers gnus-newsgroup-headers))
8763       (while (and (cdr headers)
8764                   (not (= number (mail-header-number (cadr headers)))))
8765         (pop headers))
8766       (when (cdr headers)
8767         (setcdr headers (cddr headers))))))
8768
8769 ;;;
8770 ;;; summary highlights
8771 ;;;
8772
8773 (defun gnus-highlight-selected-summary ()
8774   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8775   ;; Highlight selected article in summary buffer
8776   (when gnus-summary-selected-face
8777     (save-excursion
8778       (let* ((beg (progn (beginning-of-line) (point)))
8779              (end (progn (end-of-line) (point)))
8780              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
8781              (from (if (get-text-property beg gnus-mouse-face-prop)
8782                        beg
8783                      (or (next-single-property-change
8784                           beg gnus-mouse-face-prop nil end)
8785                          beg)))
8786              (to
8787               (if (= from end)
8788                   (- from 2)
8789                 (or (next-single-property-change
8790                      from gnus-mouse-face-prop nil end)
8791                     end))))
8792         ;; If no mouse-face prop on line we will have to = from = end,
8793         ;; so we highlight the entire line instead.
8794         (when (= (+ to 2) from)
8795           (setq from beg)
8796           (setq to end))
8797         (if gnus-newsgroup-selected-overlay
8798             ;; Move old overlay.
8799             (gnus-move-overlay
8800              gnus-newsgroup-selected-overlay from to (current-buffer))
8801           ;; Create new overlay.
8802           (gnus-overlay-put
8803            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
8804            'face gnus-summary-selected-face))))))
8805
8806 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
8807 (defun gnus-summary-highlight-line ()
8808   "Highlight current line according to `gnus-summary-highlight'."
8809   (let* ((list gnus-summary-highlight)
8810          (p (point))
8811          (end (progn (end-of-line) (point)))
8812          ;; now find out where the line starts and leave point there.
8813          (beg (progn (beginning-of-line) (point)))
8814          (article (gnus-summary-article-number))
8815          (score (or (cdr (assq (or article gnus-current-article)
8816                                gnus-newsgroup-scored))
8817                     gnus-summary-default-score 0))
8818          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
8819          (inhibit-read-only t))
8820     ;; Eval the cars of the lists until we find a match.
8821     (let ((default gnus-summary-default-score))
8822       (while (and list
8823                   (not (eval (caar list))))
8824         (setq list (cdr list))))
8825     (let ((face (cdar list)))
8826       (unless (eq face (get-text-property beg 'face))
8827         (gnus-put-text-property-excluding-characters-with-faces
8828          beg end 'face
8829          (setq face (if (boundp face) (symbol-value face) face)))
8830         (when gnus-summary-highlight-line-function
8831           (funcall gnus-summary-highlight-line-function article face))))
8832     (goto-char p)))
8833
8834 (defun gnus-update-read-articles (group unread &optional compute)
8835   "Update the list of read articles in GROUP."
8836   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
8837          (entry (gnus-gethash group gnus-newsrc-hashtb))
8838          (info (nth 2 entry))
8839          (prev 1)
8840          (unread (sort (copy-sequence unread) '<))
8841          read)
8842     (if (or (not info) (not active))
8843         ;; There is no info on this group if it was, in fact,
8844         ;; killed.  Gnus stores no information on killed groups, so
8845         ;; there's nothing to be done.
8846         ;; One could store the information somewhere temporarily,
8847         ;; perhaps...  Hmmm...
8848         ()
8849       ;; Remove any negative articles numbers.
8850       (while (and unread (< (car unread) 0))
8851         (setq unread (cdr unread)))
8852       ;; Remove any expired article numbers
8853       (while (and unread (< (car unread) (car active)))
8854         (setq unread (cdr unread)))
8855       ;; Compute the ranges of read articles by looking at the list of
8856       ;; unread articles.
8857       (while unread
8858         (when (/= (car unread) prev)
8859           (push (if (= prev (1- (car unread))) prev
8860                   (cons prev (1- (car unread))))
8861                 read))
8862         (setq prev (1+ (car unread)))
8863         (setq unread (cdr unread)))
8864       (when (<= prev (cdr active))
8865         (push (cons prev (cdr active)) read))
8866       (if compute
8867           (if (> (length read) 1) (nreverse read) read)
8868         (save-excursion
8869           (set-buffer gnus-group-buffer)
8870           (gnus-undo-register
8871             `(progn
8872                (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
8873                (gnus-info-set-read ',info ',(gnus-info-read info))
8874                (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
8875                (gnus-group-update-group ,group t))))
8876         ;; Enter this list into the group info.
8877         (gnus-info-set-read
8878          info (if (> (length read) 1) (nreverse read) read))
8879         ;; Set the number of unread articles in gnus-newsrc-hashtb.
8880         (gnus-get-unread-articles-in-group info (gnus-active group))
8881         t))))
8882
8883 (defun gnus-offer-save-summaries ()
8884   "Offer to save all active summary buffers."
8885   (save-excursion
8886     (let ((buflist (buffer-list))
8887           buffers bufname)
8888       ;; Go through all buffers and find all summaries.
8889       (while buflist
8890         (and (setq bufname (buffer-name (car buflist)))
8891              (string-match "Summary" bufname)
8892              (save-excursion
8893                (set-buffer bufname)
8894                ;; We check that this is, indeed, a summary buffer.
8895                (and (eq major-mode 'gnus-summary-mode)
8896                     ;; Also make sure this isn't bogus.
8897                     gnus-newsgroup-prepared
8898                     ;; Also make sure that this isn't a dead summary buffer.
8899                     (not gnus-dead-summary-mode)))
8900              (push bufname buffers))
8901         (setq buflist (cdr buflist)))
8902       ;; Go through all these summary buffers and offer to save them.
8903       (when buffers
8904         (map-y-or-n-p
8905          "Update summary buffer %s? "
8906          (lambda (buf)
8907            (switch-to-buffer buf)
8908            (gnus-summary-exit))
8909          buffers)))))
8910
8911 (gnus-ems-redefine)
8912
8913 (provide 'gnus-sum)
8914
8915 (run-hooks 'gnus-sum-load-hook)
8916
8917 ;;; gnus-sum.el ends here