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