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