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