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