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