117ea8c5ed22f62914e0b55212c4db68373e5b92
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (defvar tool-bar-map))
34
35 (require 'gnus)
36 (require 'gnus-group)
37 (require 'gnus-spec)
38 (require 'gnus-range)
39 (require 'gnus-int)
40 (require 'gnus-undo)
41 (require 'gnus-util)
42 (require 'nnoo)
43
44 (eval-when-compile
45   (require 'mime-play)
46   (require 'static))
47
48 (eval-and-compile
49   (autoload 'pgg-decrypt-region "pgg" nil t)
50   (autoload 'pgg-verify-region "pgg" nil t))
51
52 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
53 (autoload 'gnus-cache-write-active "gnus-cache")
54 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
55 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
56 (autoload 'gnus-pick-line-number "gnus-salt" nil t)
57 (autoload 'mm-uu-dissect "mm-uu")
58 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
59   "Deuglify broken Outlook (Express) articles and redisplay."
60   t)
61 (autoload 'gnus-article-outlook-unwrap-lines "deuglify" nil t)
62 (autoload 'gnus-article-outlook-repair-attribution "deuglify" nil t)
63 (autoload 'gnus-article-outlook-rearrange-citation "deuglify" nil t)
64
65 (defcustom gnus-kill-summary-on-exit t
66   "*If non-nil, kill the summary buffer when you exit from it.
67 If nil, the summary will become a \"*Dead Summary*\" buffer, and
68 it will be killed sometime later."
69   :group 'gnus-summary-exit
70   :type 'boolean)
71
72 (defcustom gnus-fetch-old-headers nil
73   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
74 If an unread article in the group refers to an older, already read (or
75 just marked as read) article, the old article will not normally be
76 displayed in the Summary buffer.  If this variable is non-nil, Gnus
77 will attempt to grab the headers to the old articles, and thereby
78 build complete threads.  If it has the value `some', only enough
79 headers to connect otherwise loose threads will be displayed.  This
80 variable can also be a number.  In that case, no more than that number
81 of old headers will be fetched.  If it has the value `invisible', all
82 old headers will be fetched, but none will be displayed.
83
84 The server has to support NOV for any of this to work."
85   :group 'gnus-thread
86   :type '(choice (const :tag "off" nil)
87                  (const some)
88                  number
89                  (sexp :menu-tag "other" t)))
90
91 (defcustom gnus-refer-thread-limit 200
92   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
93 If t, fetch all the available old headers."
94   :group 'gnus-thread
95   :type '(choice number
96                  (sexp :menu-tag "other" t)))
97
98 (defcustom gnus-summary-make-false-root 'adopt
99   "*nil means that Gnus won't gather loose threads.
100 If the root of a thread has expired or been read in a previous
101 session, the information necessary to build a complete thread has been
102 lost.  Instead of having many small sub-threads from this original thread
103 scattered all over the summary buffer, Gnus can gather them.
104
105 If non-nil, Gnus will try to gather all loose sub-threads from an
106 original thread into one large thread.
107
108 If this variable is non-nil, it should be one of `none', `adopt',
109 `dummy' or `empty'.
110
111 If this variable is `none', Gnus will not make a false root, but just
112 present the sub-threads after another.
113 If this variable is `dummy', Gnus will create a dummy root that will
114 have all the sub-threads as children.
115 If this variable is `adopt', Gnus will make one of the \"children\"
116 the parent and mark all the step-children as such.
117 If this variable is `empty', the \"children\" are printed with empty
118 subject fields.  (Or rather, they will be printed with a string
119 given by the `gnus-summary-same-subject' variable.)"
120   :group 'gnus-thread
121   :type '(choice (const :tag "off" nil)
122                  (const none)
123                  (const dummy)
124                  (const adopt)
125                  (const empty)))
126
127 (defcustom gnus-summary-make-false-root-always nil
128   "Always make a false dummy root."
129   :group 'gnus-thread
130   :type 'boolean)
131
132 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
133   "*A regexp to match subjects to be excluded from loose thread gathering.
134 As loose thread gathering is done on subjects only, that means that
135 there can be many false gatherings performed.  By rooting out certain
136 common subjects, gathering might become saner."
137   :group 'gnus-thread
138   :type 'regexp)
139
140 (defcustom gnus-summary-gather-subject-limit nil
141   "*Maximum length of subject comparisons when gathering loose threads.
142 Use nil to compare full subjects.  Setting this variable to a low
143 number will help gather threads that have been corrupted by
144 newsreaders chopping off subject lines, but it might also mean that
145 unrelated articles that have subject that happen to begin with the
146 same few characters will be incorrectly gathered.
147
148 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
149 comparing subjects."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  (const fuzzy)
153                  (sexp :menu-tag "on" t)))
154
155 (defcustom gnus-simplify-subject-functions nil
156   "List of functions taking a string argument that simplify subjects.
157 The functions are applied recursively.
158
159 Useful functions to put in this list include:
160 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
161 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
162   :group 'gnus-thread
163   :type '(repeat function))
164
165 (defcustom gnus-simplify-ignored-prefixes nil
166   "*Remove matches for this regexp from subject lines when simplifying fuzzily."
167   :group 'gnus-thread
168   :type '(choice (const :tag "off" nil)
169                  regexp))
170
171 (defcustom gnus-build-sparse-threads nil
172   "*If non-nil, fill in the gaps in threads.
173 If `some', only fill in the gaps that are needed to tie loose threads
174 together.  If `more', fill in all leaf nodes that Gnus can find.  If
175 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
176   :group 'gnus-thread
177   :type '(choice (const :tag "off" nil)
178                  (const some)
179                  (const more)
180                  (sexp :menu-tag "all" t)))
181
182 (defcustom gnus-summary-thread-gathering-function
183   'gnus-gather-threads-by-subject
184   "*Function used for gathering loose threads.
185 There are two pre-defined functions: `gnus-gather-threads-by-subject',
186 which only takes Subjects into consideration; and
187 `gnus-gather-threads-by-references', which compared the References
188 headers of the articles to find matches."
189   :group 'gnus-thread
190   :type '(radio (function-item gnus-gather-threads-by-subject)
191                 (function-item gnus-gather-threads-by-references)
192                 (function :tag "other")))
193
194 (defcustom gnus-summary-same-subject ""
195   "*String indicating that the current article has the same subject as the previous.
196 This variable will only be used if the value of
197 `gnus-summary-make-false-root' is `empty'."
198   :group 'gnus-summary-format
199   :type 'string)
200
201 (defcustom gnus-summary-goto-unread t
202   "*If t, many commands will go to the next unread article.
203 This applies to marking commands as well as other commands that
204 \"naturally\" select the next article, like, for instance, `SPC' at
205 the end of an article.
206
207 If nil, the marking commands do NOT go to the next unread article
208 \(they go to the next article instead).  If `never', commands that
209 usually go to the next unread article, will go to the next article,
210 whether it is read or not."
211   :group 'gnus-summary-marks
212   :link '(custom-manual "(gnus)Setting Marks")
213   :type '(choice (const :tag "off" nil)
214                  (const never)
215                  (sexp :menu-tag "on" t)))
216
217 (defcustom gnus-summary-default-score 0
218   "*Default article score level.
219 All scores generated by the score files will be added to this score.
220 If this variable is nil, scoring will be disabled."
221   :group 'gnus-score-default
222   :type '(choice (const :tag "disable")
223                  integer))
224
225 (defcustom gnus-summary-default-high-score 0
226   "*Default threshold for a high scored article.
227 An article will be highlighted as high scored if its score is greater
228 than this score."
229   :group 'gnus-score-default
230   :type 'integer)
231
232 (defcustom gnus-summary-default-low-score 0
233   "*Default threshold for a low scored article.
234 An article will be highlighted as low scored if its score is smaller
235 than this score."
236   :group 'gnus-score-default
237   :type 'integer)
238
239 (defcustom gnus-summary-zcore-fuzz 0
240   "*Fuzziness factor for the zcore in the summary buffer.
241 Articles with scores closer than this to `gnus-summary-default-score'
242 will not be marked."
243   :group 'gnus-summary-format
244   :type 'integer)
245
246 (defcustom gnus-simplify-subject-fuzzy-regexp nil
247   "*Strings to be removed when doing fuzzy matches.
248 This can either be a regular expression or list of regular expressions
249 that will be removed from subject strings if fuzzy subject
250 simplification is selected."
251   :group 'gnus-thread
252   :type '(repeat regexp))
253
254 (defcustom gnus-show-threads t
255   "*If non-nil, display threads in summary mode."
256   :group 'gnus-thread
257   :type 'boolean)
258
259 (defcustom gnus-thread-hide-subtree nil
260   "*If non-nil, hide all threads initially.
261 This can be a predicate specifier which says which threads to hide.
262 If threads are hidden, you have to run the command
263 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
264 to expose hidden threads."
265   :group 'gnus-thread
266   :type '(radio (sexp :format "Non-nil\n"
267                       :match (lambda (widget value)
268                                (not (or (consp value) (functionp value))))
269                       :value t)
270                 (const nil)
271                 (sexp :tag "Predicate specifier" :size 0)))
272
273 (defcustom gnus-thread-hide-killed t
274   "*If non-nil, hide killed threads automatically."
275   :group 'gnus-thread
276   :type 'boolean)
277
278 (defcustom gnus-thread-ignore-subject t
279   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
280 If nil, articles that have different subjects from their parents will
281 start separate threads."
282   :group 'gnus-thread
283   :type 'boolean)
284
285 (defcustom gnus-thread-operation-ignore-subject t
286   "*If non-nil, subjects will be ignored when doing thread commands.
287 This affects commands like `gnus-summary-kill-thread' and
288 `gnus-summary-lower-thread'.
289
290 If this variable is nil, articles in the same thread with different
291 subjects will not be included in the operation in question.  If this
292 variable is `fuzzy', only articles that have subjects that are fuzzily
293 equal will be included."
294   :group 'gnus-thread
295   :type '(choice (const :tag "off" nil)
296                  (const fuzzy)
297                  (sexp :tag "on" t)))
298
299 (defcustom gnus-thread-indent-level 4
300   "*Number that says how much each sub-thread should be indented."
301   :group 'gnus-thread
302   :type 'integer)
303
304 (defcustom gnus-auto-extend-newsgroup t
305   "*If non-nil, extend newsgroup forward and backward when requested."
306   :group 'gnus-summary-choose
307   :type 'boolean)
308
309 (defcustom gnus-auto-select-first t
310   "*If non-nil, select the article under point.
311 Which article this is is controlled by the `gnus-auto-select-subject'
312 variable.
313
314 If you want to prevent automatic selection of articles in some
315 newsgroups, set the variable to nil in `gnus-select-group-hook'."
316   :group 'gnus-group-select
317   :type '(choice (const :tag "none" nil)
318                  (sexp :menu-tag "first" t)))
319
320 (defcustom gnus-auto-select-subject 'unread
321   "*Says what subject to place under point when entering a group.
322
323 This variable can either be the symbols `first' (place point on the
324 first subject), `unread' (place point on the subject line of the first
325 unread article), `best' (place point on the subject line of the
326 higest-scored article), `unseen' (place point on the subject line of
327 the first unseen article), 'unseen-or-unread' (place point on the subject
328 line of the first unseen article or, if all article have been seen, on the
329 subject line of the first unread article), or a function to be called to
330 place point on some subject line."
331   :group 'gnus-group-select
332   :type '(choice (const best)
333                  (const unread)
334                  (const first)
335                  (const unseen)
336                  (const unseen-or-unread)))
337
338 (defcustom gnus-dont-select-after-jump-to-other-group nil
339   "If non-nil, don't select the first unread article after entering the
340 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
341 it is depend on the value of `gnus-auto-select-first' whether to select
342 or not."
343   :group 'gnus-group-select
344   :type 'boolean)
345
346 (defcustom gnus-auto-select-next t
347   "*If non-nil, offer to go to the next group from the end of the previous.
348 If the value is t and the next newsgroup is empty, Gnus will exit
349 summary mode and go back to group mode.  If the value is neither nil
350 nor t, Gnus will select the following unread newsgroup.  In
351 particular, if the value is the symbol `quietly', the next unread
352 newsgroup will be selected without any confirmation, and if it is
353 `almost-quietly', the next group will be selected without any
354 confirmation if you are located on the last article in the group.
355 Finally, if this variable is `slightly-quietly', the `\\<gnus-summary-mode-map>\\[gnus-summary-catchup-and-goto-next-group]' command
356 will go to the next group without confirmation."
357   :group 'gnus-summary-maneuvering
358   :type '(choice (const :tag "off" nil)
359                  (const quietly)
360                  (const almost-quietly)
361                  (const slightly-quietly)
362                  (sexp :menu-tag "on" t)))
363
364 (defcustom gnus-auto-select-same nil
365   "*If non-nil, select the next article with the same subject.
366 If there are no more articles with the same subject, go to
367 the first unread article."
368   :group 'gnus-summary-maneuvering
369   :type 'boolean)
370
371 (defcustom gnus-auto-goto-ignores 'unfetched
372   "*Says how to handle unfetched articles when maneuvering.
373
374 This variable can either be the symbols nil (maneuver to any
375 article), `undownloaded' (maneuvering while unplugged ignores articles
376 that have not been fetched), `always-undownloaded' (maneuvering always
377 ignores articles that have not been fetched), `unfetched' (maneuvering
378 ignores articles whose headers have not been fetched).
379
380 NOTE: The list of unfetched articles will always be nil when plugged
381 and, when unplugged, a subset of the undownloaded article list."
382   :group 'gnus-summary-maneuvering
383   :type '(choice (const :tag "None" nil)
384                  (const :tag "Undownloaded when unplugged" undownloaded)
385                  (const :tag "Undownloaded" always-undownloaded)
386                  (const :tag "Unfetched" unfetched)))
387
388 (defcustom gnus-summary-check-current nil
389   "*If non-nil, consider the current article when moving.
390 The \"unread\" movement commands will stay on the same line if the
391 current article is unread."
392   :group 'gnus-summary-maneuvering
393   :type 'boolean)
394
395 (defcustom gnus-auto-center-summary t
396   "*If non-nil, always center the current summary buffer.
397 In particular, if `vertical' do only vertical recentering.  If non-nil
398 and non-`vertical', do both horizontal and vertical recentering."
399   :group 'gnus-summary-maneuvering
400   :type '(choice (const :tag "none" nil)
401                  (const vertical)
402                  (integer :tag "height")
403                  (sexp :menu-tag "both" t)))
404
405 (defvar gnus-auto-center-group t
406   "*If non-nil, always center the group buffer.")
407
408 (defcustom gnus-show-all-headers nil
409   "*If non-nil, don't hide any headers."
410   :group 'gnus-article-hiding
411   :group 'gnus-article-headers
412   :type 'boolean)
413
414 (defcustom gnus-summary-ignore-duplicates nil
415   "*If non-nil, ignore articles with identical Message-ID headers."
416   :group 'gnus-summary
417   :type 'boolean)
418
419 (defcustom gnus-single-article-buffer t
420   "*If non-nil, display all articles in the same buffer.
421 If nil, each group will get its own article buffer."
422   :group 'gnus-article-various
423   :type 'boolean)
424
425 (defcustom gnus-break-pages t
426   "*If non-nil, do page breaking on articles.
427 The page delimiter is specified by the `gnus-page-delimiter'
428 variable."
429   :group 'gnus-article-various
430   :type 'boolean)
431
432 (defcustom gnus-show-mime t
433   "*If non-nil, do mime processing of articles.
434 The articles will simply be fed to the function given by
435 `gnus-article-display-method-for-mime'."
436   :group 'gnus-article-mime
437   :type 'boolean)
438
439 (defcustom gnus-move-split-methods nil
440   "*Variable used to suggest where articles are to be moved to.
441 It uses the same syntax as the `gnus-split-methods' variable.
442 However, whereas `gnus-split-methods' specifies file names as targets,
443 this variable specifies group names."
444   :group 'gnus-summary-mail
445   :type '(repeat (choice (list :value (fun) function)
446                          (cons :value ("" "") regexp (repeat string))
447                          (sexp :value nil))))
448
449 (defcustom gnus-unread-mark ?\ ;;;Whitespace
450   "*Mark used for unread articles."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-ticked-mark ?!
455   "*Mark used for ticked articles."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-dormant-mark ??
460   "*Mark used for dormant articles."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-del-mark ?r
465   "*Mark used for del'd articles."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-read-mark ?R
470   "*Mark used for read articles."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-expirable-mark ?E
475   "*Mark used for expirable articles."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-killed-mark ?K
480   "*Mark used for killed articles."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-spam-mark ?$
485   "*Mark used for spam articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-souped-mark ?F
490   "*Mark used for souped articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-kill-file-mark ?X
495   "*Mark used for articles killed by kill files."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-low-score-mark ?Y
500   "*Mark used for articles with a low score."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-catchup-mark ?C
505   "*Mark used for articles that are caught up."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-replied-mark ?A
510   "*Mark used for articles that have been replied to."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-forwarded-mark ?F
515   "*Mark used for articles that have been forwarded."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-recent-mark ?N
520   "*Mark used for articles that are recent."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-cached-mark ?*
525   "*Mark used for articles that are in the cache."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-saved-mark ?S
530   "*Mark used for articles that have been saved."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-unseen-mark ?.
535   "*Mark used for articles that haven't been seen."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-no-mark ?\ ;;;Whitespace
540   "*Mark used for articles that have no other secondary mark."
541   :group 'gnus-summary-marks
542   :type 'character)
543
544 (defcustom gnus-ancient-mark ?O
545   "*Mark used for ancient articles."
546   :group 'gnus-summary-marks
547   :type 'character)
548
549 (defcustom gnus-sparse-mark ?Q
550   "*Mark used for sparsely reffed articles."
551   :group 'gnus-summary-marks
552   :type 'character)
553
554 (defcustom gnus-canceled-mark ?G
555   "*Mark used for canceled articles."
556   :group 'gnus-summary-marks
557   :type 'character)
558
559 (defcustom gnus-duplicate-mark ?M
560   "*Mark used for duplicate articles."
561   :group 'gnus-summary-marks
562   :type 'character)
563
564 (defcustom gnus-undownloaded-mark ?-
565   "*Mark used for articles that weren't downloaded."
566   :group 'gnus-summary-marks
567   :type 'character)
568
569 (defcustom gnus-downloaded-mark ?+
570   "*Mark used for articles that were downloaded."
571   :group 'gnus-summary-marks
572   :type 'character)
573
574 (defcustom gnus-downloadable-mark ?%
575   "*Mark used for articles that are to be downloaded."
576   :group 'gnus-summary-marks
577   :type 'character)
578
579 (defcustom gnus-unsendable-mark ?=
580   "*Mark used for articles that won't be sent."
581   :group 'gnus-summary-marks
582   :type 'character)
583
584 (defcustom gnus-score-over-mark ?+
585   "*Score mark used for articles with high scores."
586   :group 'gnus-summary-marks
587   :type 'character)
588
589 (defcustom gnus-score-below-mark ?-
590   "*Score mark used for articles with low scores."
591   :group 'gnus-summary-marks
592   :type 'character)
593
594 (defcustom gnus-empty-thread-mark ?\ ;;;Whitespace
595   "*There is no thread under the article."
596   :group 'gnus-summary-marks
597   :type 'character)
598
599 (defcustom gnus-not-empty-thread-mark ?=
600   "*There is a thread under the article."
601   :group 'gnus-summary-marks
602   :type 'character)
603
604 (defcustom gnus-view-pseudo-asynchronously nil
605   "*If non-nil, Gnus will view pseudo-articles asynchronously."
606   :group 'gnus-extract-view
607   :type 'boolean)
608
609 (defcustom gnus-auto-expirable-marks
610   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
611         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
612         gnus-souped-mark gnus-duplicate-mark)
613   "*The list of marks converted into expiration if a group is auto-expirable."
614   :version "21.1"
615   :group 'gnus-summary
616   :type '(repeat character))
617
618 (defcustom gnus-inhibit-user-auto-expire t
619   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
620   :version "21.1"
621   :group 'gnus-summary
622   :type 'boolean)
623
624 (defcustom gnus-view-pseudos nil
625   "*If `automatic', pseudo-articles will be viewed automatically.
626 If `not-confirm', pseudos will be viewed automatically, and the user
627 will not be asked to confirm the command."
628   :group 'gnus-extract-view
629   :type '(choice (const :tag "off" nil)
630                  (const automatic)
631                  (const not-confirm)))
632
633 (defcustom gnus-view-pseudos-separately t
634   "*If non-nil, one pseudo-article will be created for each file to be viewed.
635 If nil, all files that use the same viewing command will be given as a
636 list of parameters to that command."
637   :group 'gnus-extract-view
638   :type 'boolean)
639
640 (defcustom gnus-insert-pseudo-articles t
641   "*If non-nil, insert pseudo-articles when decoding articles."
642   :group 'gnus-extract-view
643   :type 'boolean)
644
645 (defcustom gnus-summary-dummy-line-format
646   "   %(:                             :%) %S\n"
647   "*The format specification for the dummy roots in the summary buffer.
648 It works along the same lines as a normal formatting string,
649 with some simple extensions.
650
651 %S  The subject
652
653 General format specifiers can also be used.
654 See `(gnus)Formatting Variables'."
655   :link '(custom-manual "(gnus)Formatting Variables")
656   :group 'gnus-threading
657   :type 'string)
658
659 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
660   "*The format specification for the summary mode line.
661 It works along the same lines as a normal formatting string,
662 with some simple extensions:
663
664 %G  Group name
665 %p  Unprefixed group name
666 %A  Current article number
667 %z  Current article score
668 %V  Gnus version
669 %U  Number of unread articles in the group
670 %e  Number of unselected articles in the group
671 %Z  A string with unread/unselected article counts
672 %g  Shortish group name
673 %S  Subject of the current article
674 %u  User-defined spec
675 %s  Current score file name
676 %d  Number of dormant articles
677 %r  Number of articles that have been marked as read in this session
678 %E  Number of articles expunged by the score files"
679   :group 'gnus-summary-format
680   :type 'string)
681
682 (defcustom gnus-list-identifiers nil
683   "Regexp that matches list identifiers to be removed from subject.
684 This can also be a list of regexps."
685   :version "21.1"
686   :group 'gnus-summary-format
687   :group 'gnus-article-hiding
688   :type '(choice (const :tag "none" nil)
689                  (regexp :value ".*")
690                  (repeat :value (".*") regexp)))
691
692 (defcustom gnus-summary-mark-below 0
693   "*Mark all articles with a score below this variable as read.
694 This variable is local to each summary buffer and usually set by the
695 score file."
696   :group 'gnus-score-default
697   :type 'integer)
698
699 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
700   "*List of functions used for sorting articles in the summary buffer.
701
702 Each function takes two articles and returns non-nil if the first
703 article should be sorted before the other.  If you use more than one
704 function, the primary sort function should be the last.  You should
705 probably always include `gnus-article-sort-by-number' in the list of
706 sorting functions -- preferably first.  Also note that sorting by date
707 is often much slower than sorting by number, and the sorting order is
708 very similar.  (Sorting by date means sorting by the time the message
709 was sent, sorting by number means sorting by arrival time.)
710
711 Ready-made functions include `gnus-article-sort-by-number',
712 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
713 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
714 and `gnus-article-sort-by-score'.
715
716 When threading is turned on, the variable `gnus-thread-sort-functions'
717 controls how articles are sorted."
718   :group 'gnus-summary-sort
719   :type '(repeat (choice (function-item gnus-article-sort-by-number)
720                          (function-item gnus-article-sort-by-author)
721                          (function-item gnus-article-sort-by-subject)
722                          (function-item gnus-article-sort-by-date)
723                          (function-item gnus-article-sort-by-score)
724                          (function-item gnus-article-sort-by-random)
725                          (function :tag "other"))))
726
727 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
728   "*List of functions used for sorting threads in the summary buffer.
729 By default, threads are sorted by article number.
730
731 Each function takes two threads and returns non-nil if the first
732 thread should be sorted before the other.  If you use more than one
733 function, the primary sort function should be the last.  You should
734 probably always include `gnus-thread-sort-by-number' in the list of
735 sorting functions -- preferably first.  Also note that sorting by date
736 is often much slower than sorting by number, and the sorting order is
737 very similar.  (Sorting by date means sorting by the time the message
738 was sent, sorting by number means sorting by arrival time.)
739
740 Ready-made functions include `gnus-thread-sort-by-number',
741 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
742 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
743 `gnus-thread-sort-by-most-recent-number',
744 `gnus-thread-sort-by-most-recent-date',
745 `gnus-thread-sort-by-random', and
746 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
747
748 When threading is turned off, the variable
749 `gnus-article-sort-functions' controls how articles are sorted."
750   :group 'gnus-summary-sort
751   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
752                          (function-item gnus-thread-sort-by-author)
753                          (function-item gnus-thread-sort-by-subject)
754                          (function-item gnus-thread-sort-by-date)
755                          (function-item gnus-thread-sort-by-score)
756                          (function-item gnus-thread-sort-by-total-score)
757                          (function-item gnus-thread-sort-by-random)
758                          (function :tag "other"))))
759
760 (defcustom gnus-thread-score-function '+
761   "*Function used for calculating the total score of a thread.
762
763 The function is called with the scores of the article and each
764 subthread and should then return the score of the thread.
765
766 Some functions you can use are `+', `max', or `min'."
767   :group 'gnus-summary-sort
768   :type 'function)
769
770 (defcustom gnus-summary-expunge-below nil
771   "All articles that have a score less than this variable will be expunged.
772 This variable is local to the summary buffers."
773   :group 'gnus-score-default
774   :type '(choice (const :tag "off" nil)
775                  integer))
776
777 (defcustom gnus-thread-expunge-below nil
778   "All threads that have a total score less than this variable will be expunged.
779 See `gnus-thread-score-function' for en explanation of what a
780 \"thread score\" is.
781
782 This variable is local to the summary buffers."
783   :group 'gnus-threading
784   :group 'gnus-score-default
785   :type '(choice (const :tag "off" nil)
786                  integer))
787
788 (defcustom gnus-summary-mode-hook nil
789   "*A hook for Gnus summary mode.
790 This hook is run before any variables are set in the summary buffer."
791   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
792   :group 'gnus-summary-various
793   :type 'hook)
794
795 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
796 (when (featurep 'xemacs)
797   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
798   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
799   (add-hook 'gnus-summary-mode-hook
800             'gnus-xmas-switch-horizontal-scrollbar-off))
801
802 (defcustom gnus-summary-menu-hook nil
803   "*Hook run after the creation of the summary mode menu."
804   :group 'gnus-summary-visual
805   :type 'hook)
806
807 (defcustom gnus-summary-exit-hook nil
808   "*A hook called on exit from the summary buffer.
809 It will be called with point in the group buffer."
810   :group 'gnus-summary-exit
811   :type 'hook)
812
813 (defcustom gnus-summary-prepare-hook nil
814   "*A hook called after the summary buffer has been generated.
815 If you want to modify the summary buffer, you can use this hook."
816   :group 'gnus-summary-various
817   :type 'hook)
818
819 (defcustom gnus-summary-prepared-hook nil
820   "*A hook called as the last thing after the summary buffer has been generated."
821   :group 'gnus-summary-various
822   :type 'hook)
823
824 (defcustom gnus-summary-generate-hook nil
825   "*A hook run just before generating the summary buffer.
826 This hook is commonly used to customize threading variables and the
827 like."
828   :group 'gnus-summary-various
829   :type 'hook)
830
831 (defcustom gnus-select-group-hook nil
832   "*A hook called when a newsgroup is selected.
833
834 If you'd like to simplify subjects like the
835 `gnus-summary-next-same-subject' command does, you can use the
836 following hook:
837
838  (add-hook gnus-select-group-hook
839            (lambda ()
840              (mapcar (lambda (header)
841                        (mail-header-set-subject
842                         header
843                         (gnus-simplify-subject
844                          (mail-header-subject header) 're-only)))
845                      gnus-newsgroup-headers)))"
846   :group 'gnus-group-select
847   :type 'hook)
848
849 (defcustom gnus-select-article-hook nil
850   "*A hook called when an article is selected."
851   :group 'gnus-summary-choose
852   :options '(gnus-agent-fetch-selected-article)
853   :type 'hook)
854
855 (defcustom gnus-visual-mark-article-hook
856   (list 'gnus-highlight-selected-summary)
857   "*Hook run after selecting an article in the summary buffer.
858 It is meant to be used for highlighting the article in some way.  It
859 is not run if `gnus-visual' is nil."
860   :group 'gnus-summary-visual
861   :type 'hook)
862
863 (defcustom gnus-parse-headers-hook '(gnus-summary-inherit-default-charset)
864   "*A hook called before parsing the headers."
865   :group 'gnus-various
866   :type 'hook)
867
868 (defcustom gnus-exit-group-hook nil
869   "*A hook called when exiting summary mode.
870 This hook is not called from the non-updating exit commands like `Q'."
871   :group 'gnus-various
872   :type 'hook)
873
874 (defcustom gnus-summary-update-hook
875   (list 'gnus-summary-highlight-line)
876   "*A hook called when a summary line is changed.
877 The hook will not be called if `gnus-visual' is nil.
878
879 The default function `gnus-summary-highlight-line' will
880 highlight the line according to the `gnus-summary-highlight'
881 variable."
882   :group 'gnus-summary-visual
883   :type 'hook)
884
885 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
886   "*A hook called when an article is selected for the first time.
887 The hook is intended to mark an article as read (or unread)
888 automatically when it is selected."
889   :group 'gnus-summary-choose
890   :type 'hook)
891
892 (defcustom gnus-group-no-more-groups-hook nil
893   "*A hook run when returning to group mode having no more (unread) groups."
894   :group 'gnus-group-select
895   :type 'hook)
896
897 (defcustom gnus-ps-print-hook nil
898   "*A hook run before ps-printing something from Gnus."
899   :group 'gnus-summary
900   :type 'hook)
901
902 (defcustom gnus-summary-article-move-hook nil
903   "*A hook called after an article is moved, copied, respooled, or crossposted."
904   :group 'gnus-summary
905   :type 'hook)
906
907 (defcustom gnus-summary-article-delete-hook nil
908   "*A hook called after an article is deleted."
909   :group 'gnus-summary
910   :type 'hook)
911
912 (defcustom gnus-summary-article-expire-hook nil
913   "*A hook called after an article is expired."
914   :group 'gnus-summary
915   :type 'hook)
916
917 (defcustom gnus-summary-display-arrow
918   (and (fboundp 'display-graphic-p)
919        (display-graphic-p))
920   "*If non-nil, display an arrow highlighting the current article."
921   :version "21.1"
922   :group 'gnus-summary
923   :type 'boolean)
924
925 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
926   "Face used for highlighting the current article in the summary buffer."
927   :group 'gnus-summary-visual
928   :type 'face)
929
930 (defvar gnus-tmp-downloaded nil)
931
932 (defcustom gnus-summary-highlight
933   '(((eq mark gnus-canceled-mark)
934      . gnus-summary-cancelled-face)
935     ((and uncached (> score default-high))
936      . gnus-summary-high-undownloaded-face)
937     ((and uncached (< score default-low))
938      . gnus-summary-low-undownloaded-face)
939     (uncached
940      . gnus-summary-normal-undownloaded-face)
941     ((and (> score default-high)
942           (or (eq mark gnus-dormant-mark)
943               (eq mark gnus-ticked-mark)))
944      . gnus-summary-high-ticked-face)
945     ((and (< score default-low)
946           (or (eq mark gnus-dormant-mark)
947               (eq mark gnus-ticked-mark)))
948      . gnus-summary-low-ticked-face)
949     ((or (eq mark gnus-dormant-mark)
950          (eq mark gnus-ticked-mark))
951      . gnus-summary-normal-ticked-face)
952     ((and (> score default-high) (eq mark gnus-ancient-mark))
953      . gnus-summary-high-ancient-face)
954     ((and (< score default-low) (eq mark gnus-ancient-mark))
955      . gnus-summary-low-ancient-face)
956     ((eq mark gnus-ancient-mark)
957      . gnus-summary-normal-ancient-face)
958     ((and (> score default-high) (eq mark gnus-unread-mark))
959      . gnus-summary-high-unread-face)
960     ((and (< score default-low) (eq mark gnus-unread-mark))
961      . gnus-summary-low-unread-face)
962     ((eq mark gnus-unread-mark)
963      . gnus-summary-normal-unread-face)
964     ((> score default-high)
965      . gnus-summary-high-read-face)
966     ((< score default-low)
967      . gnus-summary-low-read-face)
968     (t
969      . gnus-summary-normal-read-face))
970   "*Controls the highlighting of summary buffer lines.
971
972 A list of (FORM . FACE) pairs.  When deciding how a a particular
973 summary line should be displayed, each form is evaluated.  The content
974 of the face field after the first true form is used.  You can change
975 how those summary lines are displayed, by editing the face field.
976
977 You can use the following variables in the FORM field.
978
979 score:        The article's score
980 default:      The default article score.
981 default-high: The default score for high scored articles.
982 default-low:  The default score for low scored articles.
983 below:        The score below which articles are automatically marked as read.
984 mark:         The article's mark.
985 uncached:     Non-nil if the article is uncached."
986   :group 'gnus-summary-visual
987   :type '(repeat (cons (sexp :tag "Form" nil)
988                        face)))
989
990 (defcustom gnus-alter-header-function nil
991   "Function called to allow alteration of article header structures.
992 The function is called with one parameter, the article header vector,
993 which it may alter in any way."
994   :type '(choice (const :tag "None" nil)
995                  function)
996   :group 'gnus-summary)
997
998 (defvar gnus-decode-encoded-word-function
999   (mime-find-field-decoder 'From 'nov)
1000   "Variable that says which function should be used to decode a string with encoded words.")
1001
1002 (defcustom gnus-extra-headers '(To Newsgroups)
1003   "*Extra headers to parse."
1004   :version "21.1"
1005   :group 'gnus-summary
1006   :type '(repeat symbol))
1007
1008 (defcustom gnus-ignored-from-addresses
1009   (and user-mail-address (regexp-quote user-mail-address))
1010   "*Regexp of From headers that may be suppressed in favor of To headers."
1011   :version "21.1"
1012   :group 'gnus-summary
1013   :type 'regexp)
1014
1015 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
1016   "List of charsets that should be ignored.
1017 When these charsets are used in the \"charset\" parameter, the
1018 default charset will be used instead."
1019   :version "21.1"
1020   :type '(repeat symbol)
1021   :group 'gnus-charset)
1022
1023 (gnus-define-group-parameter
1024  ignored-charsets
1025  :type list
1026  :function-document
1027  "Return the ignored charsets of GROUP."
1028  :variable gnus-group-ignored-charsets-alist
1029  :variable-default
1030  '(("alt\\.chinese\\.text" iso-8859-1))
1031  :variable-document
1032  "Alist of regexps (to match group names) and charsets that should be ignored.
1033 When these charsets are used in the \"charset\" parameter, the
1034 default charset will be used instead."
1035  :variable-group gnus-charset
1036  :variable-type '(repeat (cons (regexp :tag "Group")
1037                                (repeat symbol)))
1038  :parameter-type '(choice :tag "Ignored charsets"
1039                           :value nil
1040                           (repeat (symbol)))
1041  :parameter-document       "\
1042 List of charsets that should be ignored.
1043
1044 When these charsets are used in the \"charset\" parameter, the
1045 default charset will be used instead.")
1046
1047 (defcustom gnus-group-highlight-words-alist nil
1048   "Alist of group regexps and highlight regexps.
1049 This variable uses the same syntax as `gnus-emphasis-alist'."
1050   :version "21.1"
1051   :type '(repeat (cons (regexp :tag "Group")
1052                        (repeat (list (regexp :tag "Highlight regexp")
1053                                      (number :tag "Group for entire word" 0)
1054                                      (number :tag "Group for displayed part" 0)
1055                                      (symbol :tag "Face"
1056                                              gnus-emphasis-highlight-words)))))
1057   :group 'gnus-summary-visual)
1058
1059 (defcustom gnus-use-wheel nil
1060   "Use Intelli-mouse on summary movement"
1061   :type 'boolean
1062   :group 'gnus-summary-maneuvering)
1063
1064 (defcustom gnus-wheel-scroll-amount '(5 . 1)
1065   "Amount to scroll messages by spinning the mouse wheel.
1066 This is actually a cons cell, where the first item is the amount to scroll
1067 on a normal wheel event, and the second is the amount to scroll when the
1068 wheel is moved with the shift key depressed."
1069   :type '(cons (integer :tag "Shift") integer)
1070   :group 'gnus-summary-maneuvering)
1071
1072 (defcustom gnus-wheel-edge-resistance 2
1073   "How hard it should be to change the current article
1074 by moving the mouse over the edge of the article window."
1075   :type 'integer
1076   :group 'gnus-summary-maneuvering)
1077
1078 (defcustom gnus-summary-show-article-charset-alist
1079   nil
1080   "Alist of number and charset.
1081 The article will be shown with the charset corresponding to the
1082 numbered argument.
1083 For example: ((1 . cn-gb-2312) (2 . big5))."
1084   :version "21.1"
1085   :type '(repeat (cons (number :tag "Argument" 1)
1086                        (symbol :tag "Charset")))
1087   :group 'gnus-charset)
1088
1089 (defcustom gnus-preserve-marks t
1090   "Whether marks are preserved when moving, copying and respooling messages."
1091   :version "21.1"
1092   :type 'boolean
1093   :group 'gnus-summary-marks)
1094
1095 (defcustom gnus-alter-articles-to-read-function nil
1096   "Function to be called to alter the list of articles to be selected."
1097   :type '(choice (const nil) function)
1098   :group 'gnus-summary)
1099
1100 (defcustom gnus-orphan-score nil
1101   "*All orphans get this score added.  Set in the score file."
1102   :group 'gnus-score-default
1103   :type '(choice (const nil)
1104                  integer))
1105
1106 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1107   "*A regexp to match MIME parts when saving multiple parts of a
1108 message with `gnus-summary-save-parts' (\\<gnus-summary-mode-map>\\[gnus-summary-save-parts]).
1109 This regexp will be used by default when prompting the user for which
1110 type of files to save."
1111   :group 'gnus-summary
1112   :type 'regexp)
1113
1114 (defcustom gnus-read-all-available-headers nil
1115   "Whether Gnus should parse all headers made available to it.
1116 This is mostly relevant for slow back ends where the user may
1117 wish to widen the summary buffer to include all headers
1118 that were fetched.  Say, for nnultimate groups."
1119   :group 'gnus-summary
1120   :type '(choice boolean regexp))
1121
1122 (defcustom gnus-summary-muttprint-program "muttprint"
1123   "Command (and optional arguments) used to run Muttprint."
1124   :version "21.3"
1125   :group 'gnus-summary
1126   :type 'string)
1127
1128 (defcustom gnus-article-loose-mime nil
1129   "If non-nil, don't require MIME-Version header.
1130 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1131 supply the MIME-Version header or deliberately strip it From the mail.
1132 Set it to non-nil, Gnus will treat some articles as MIME even if
1133 the MIME-Version header is missed."
1134   :version "21.3"
1135   :type 'boolean
1136   :group 'gnus-article-mime)
1137
1138 (defcustom gnus-article-emulate-mime t
1139   "If non-nil, use MIME emulation for uuencode and the like.
1140 This means that Gnus will search message bodies for text that look
1141 like uuencoded bits, yEncoded bits, and so on, and present that using
1142 the normal Gnus MIME machinery."
1143   :type 'boolean
1144   :group 'gnus-article-mime)
1145
1146 ;;; Internal variables
1147
1148 (defvar gnus-summary-display-cache nil)
1149 (defvar gnus-article-mime-handles nil)
1150 (defvar gnus-article-decoded-p nil)
1151 (defvar gnus-article-charset nil)
1152 (defvar gnus-article-ignored-charsets nil)
1153 (defvar gnus-scores-exclude-files nil)
1154 (defvar gnus-page-broken nil)
1155
1156 (defvar gnus-original-article nil)
1157 (defvar gnus-article-internal-prepare-hook nil)
1158 (defvar gnus-newsgroup-process-stack nil)
1159
1160 (defvar gnus-thread-indent-array nil)
1161 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1162 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1163   "Function called to sort the articles within a thread after it has been gathered together.")
1164
1165 (defvar gnus-summary-save-parts-type-history nil)
1166 (defvar gnus-summary-save-parts-last-directory nil)
1167
1168 ;; Avoid highlighting in kill files.
1169 (defvar gnus-summary-inhibit-highlight nil)
1170 (defvar gnus-newsgroup-selected-overlay nil)
1171 (defvar gnus-inhibit-limiting nil)
1172 (defvar gnus-newsgroup-adaptive-score-file nil)
1173 (defvar gnus-current-score-file nil)
1174 (defvar gnus-current-move-group nil)
1175 (defvar gnus-current-copy-group nil)
1176 (defvar gnus-current-crosspost-group nil)
1177 (defvar gnus-newsgroup-display nil)
1178
1179 (defvar gnus-newsgroup-dependencies nil)
1180 (defvar gnus-newsgroup-adaptive nil)
1181 (defvar gnus-summary-display-article-function nil)
1182 (defvar gnus-summary-highlight-line-function nil
1183   "Function called after highlighting a summary line.")
1184
1185 (defvar gnus-summary-line-format-alist
1186   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1187     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1188     (?s gnus-tmp-subject-or-nil ?s)
1189     (?n gnus-tmp-name ?s)
1190     (?A (std11-address-string
1191          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
1192     (?a (or (std11-full-name-string
1193              (car (mime-entity-read-field gnus-tmp-header 'From)))
1194             gnus-tmp-from) ?s)
1195     (?F gnus-tmp-from ?s)
1196     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1197     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1198     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1199     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1200     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1201     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1202     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1203     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1204     (?L gnus-tmp-lines ?s)
1205     (?O gnus-tmp-downloaded ?c)
1206     (?I gnus-tmp-indentation ?s)
1207     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1208     (?R gnus-tmp-replied ?c)
1209     (?\[ gnus-tmp-opening-bracket ?c)
1210     (?\] gnus-tmp-closing-bracket ?c)
1211     (?\> (make-string gnus-tmp-level ? ) ?s)
1212     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1213     (?i gnus-tmp-score ?d)
1214     (?z gnus-tmp-score-char ?c)
1215     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1216     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1217     (?U gnus-tmp-unread ?c)
1218     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1219         ?s)
1220     (?t (gnus-summary-number-of-articles-in-thread
1221          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1222         ?d)
1223     (?e (gnus-summary-number-of-articles-in-thread
1224          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1225         ?c)
1226     (?u gnus-tmp-user-defined ?s)
1227     (?P (gnus-pick-line-number) ?d)
1228     (?B gnus-tmp-thread-tree-header-string ?s)
1229     (user-date (gnus-user-date
1230                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1231   "An alist of format specifications that can appear in summary lines.
1232 These are paired with what variables they correspond with, along with
1233 the type of the variable (string, integer, character, etc).")
1234
1235 (defvar gnus-summary-dummy-line-format-alist
1236   `((?S gnus-tmp-subject ?s)
1237     (?N gnus-tmp-number ?d)
1238     (?u gnus-tmp-user-defined ?s)))
1239
1240 (defvar gnus-summary-mode-line-format-alist
1241   `((?G gnus-tmp-group-name ?s)
1242     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1243     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1244     (?A gnus-tmp-article-number ?d)
1245     (?Z gnus-tmp-unread-and-unselected ?s)
1246     (?V gnus-version ?s)
1247     (?U gnus-tmp-unread-and-unticked ?d)
1248     (?S gnus-tmp-subject ?s)
1249     (?e gnus-tmp-unselected ?d)
1250     (?u gnus-tmp-user-defined ?s)
1251     (?d (length gnus-newsgroup-dormant) ?d)
1252     (?t (length gnus-newsgroup-marked) ?d)
1253     (?h (length gnus-newsgroup-spam-marked) ?d)
1254     (?r (length gnus-newsgroup-reads) ?d)
1255     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1256     (?E gnus-newsgroup-expunged-tally ?d)
1257     (?s (gnus-current-score-file-nondirectory) ?s)))
1258
1259 (defvar gnus-last-search-regexp nil
1260   "Default regexp for article search command.")
1261
1262 (defvar gnus-summary-search-article-matched-data nil
1263   "Last matched data of article search command.  It is the local variable
1264 in `gnus-article-buffer' which consists of the list of start position,
1265 end position and text.")
1266
1267 (defvar gnus-last-shell-command nil
1268   "Default shell command on article.")
1269
1270 (defvar gnus-newsgroup-agentized nil
1271   "Locally bound in each summary buffer to indicate whether the server has been agentized.")
1272 (defvar gnus-newsgroup-begin nil)
1273 (defvar gnus-newsgroup-end nil)
1274 (defvar gnus-newsgroup-last-rmail nil)
1275 (defvar gnus-newsgroup-last-mail nil)
1276 (defvar gnus-newsgroup-last-folder nil)
1277 (defvar gnus-newsgroup-last-file nil)
1278 (defvar gnus-newsgroup-auto-expire nil)
1279 (defvar gnus-newsgroup-active nil)
1280
1281 (defvar gnus-newsgroup-data nil)
1282 (defvar gnus-newsgroup-data-reverse nil)
1283 (defvar gnus-newsgroup-limit nil)
1284 (defvar gnus-newsgroup-limits nil)
1285 (defvar gnus-summary-use-undownloaded-faces nil)
1286
1287 (defvar gnus-newsgroup-unreads nil
1288   "Sorted list of unread articles in the current newsgroup.")
1289
1290 (defvar gnus-newsgroup-unselected nil
1291   "Sorted list of unselected unread articles in the current newsgroup.")
1292
1293 (defvar gnus-newsgroup-reads nil
1294   "Alist of read articles and article marks in the current newsgroup.")
1295
1296 (defvar gnus-newsgroup-expunged-tally nil)
1297
1298 (defvar gnus-newsgroup-marked nil
1299   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1300
1301 (defvar gnus-newsgroup-spam-marked nil
1302   "List of ranges of articles that have been marked as spam.")
1303
1304 (defvar gnus-newsgroup-killed nil
1305   "List of ranges of articles that have been through the scoring process.")
1306
1307 (defvar gnus-newsgroup-cached nil
1308   "Sorted list of articles that come from the article cache.")
1309
1310 (defvar gnus-newsgroup-saved nil
1311   "List of articles that have been saved.")
1312
1313 (defvar gnus-newsgroup-kill-headers nil)
1314
1315 (defvar gnus-newsgroup-replied nil
1316   "List of articles that have been replied to in the current newsgroup.")
1317
1318 (defvar gnus-newsgroup-forwarded nil
1319   "List of articles that have been forwarded in the current newsgroup.")
1320
1321 (defvar gnus-newsgroup-recent nil
1322   "List of articles that have are recent in the current newsgroup.")
1323
1324 (defvar gnus-newsgroup-expirable nil
1325   "Sorted list of articles in the current newsgroup that can be expired.")
1326
1327 (defvar gnus-newsgroup-processable nil
1328   "List of articles in the current newsgroup that can be processed.")
1329
1330 (defvar gnus-newsgroup-downloadable nil
1331   "Sorted list of articles in the current newsgroup that can be processed.")
1332
1333 (defvar gnus-newsgroup-unfetched nil
1334   "Sorted list of articles in the current newsgroup whose headers have
1335 not been fetched into the agent.
1336
1337 This list will always be a subset of gnus-newsgroup-undownloaded.")
1338
1339 (defvar gnus-newsgroup-undownloaded nil
1340   "List of articles in the current newsgroup that haven't been downloaded.")
1341
1342 (defvar gnus-newsgroup-unsendable nil
1343   "List of articles in the current newsgroup that won't be sent.")
1344
1345 (defvar gnus-newsgroup-bookmarks nil
1346   "List of articles in the current newsgroup that have bookmarks.")
1347
1348 (defvar gnus-newsgroup-dormant nil
1349   "Sorted list of dormant articles in the current newsgroup.")
1350
1351 (defvar gnus-newsgroup-unseen nil
1352   "List of unseen articles in the current newsgroup.")
1353
1354 (defvar gnus-newsgroup-seen nil
1355   "Range of seen articles in the current newsgroup.")
1356
1357 (defvar gnus-newsgroup-articles nil
1358   "List of articles in the current newsgroup.")
1359
1360 (defvar gnus-newsgroup-scored nil
1361   "List of scored articles in the current newsgroup.")
1362
1363 (defvar gnus-newsgroup-incorporated nil
1364   "List of incorporated articles in the current newsgroup.")
1365
1366 (defvar gnus-newsgroup-headers nil
1367   "List of article headers in the current newsgroup.")
1368
1369 (defvar gnus-newsgroup-threads nil)
1370
1371 (defvar gnus-newsgroup-prepared nil
1372   "Whether the current group has been prepared properly.")
1373
1374 (defvar gnus-newsgroup-ancient nil
1375   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1376
1377 (defvar gnus-newsgroup-sparse nil)
1378
1379 (defvar gnus-current-article nil)
1380 (defvar gnus-article-current nil)
1381 (defvar gnus-current-headers nil)
1382 (defvar gnus-have-all-headers nil)
1383 (defvar gnus-last-article nil)
1384 (defvar gnus-newsgroup-history nil)
1385 (defvar gnus-newsgroup-charset nil)
1386 (defvar gnus-newsgroup-ephemeral-charset nil)
1387 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1388
1389 (defvar gnus-article-before-search nil)
1390
1391 (defvar gnus-summary-local-variables
1392   '(gnus-newsgroup-name
1393     gnus-newsgroup-begin gnus-newsgroup-end
1394     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1395     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1396     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1397     gnus-newsgroup-unselected gnus-newsgroup-marked
1398     gnus-newsgroup-spam-marked
1399     gnus-newsgroup-reads gnus-newsgroup-saved
1400     gnus-newsgroup-replied gnus-newsgroup-forwarded
1401     gnus-newsgroup-recent
1402     gnus-newsgroup-expirable
1403     gnus-newsgroup-processable gnus-newsgroup-killed
1404     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1405     gnus-newsgroup-unfetched
1406     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1407     gnus-newsgroup-seen gnus-newsgroup-articles
1408     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1409     gnus-newsgroup-headers gnus-newsgroup-threads
1410     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1411     gnus-current-article gnus-current-headers gnus-have-all-headers
1412     gnus-last-article gnus-article-internal-prepare-hook
1413     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1414     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1415     gnus-thread-expunge-below
1416     gnus-score-alist gnus-current-score-file
1417     (gnus-summary-expunge-below . global)
1418     (gnus-summary-mark-below . global)
1419     (gnus-orphan-score . global)
1420     gnus-newsgroup-active gnus-scores-exclude-files
1421     gnus-newsgroup-history gnus-newsgroup-ancient
1422     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1423     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1424     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1425     (gnus-newsgroup-expunged-tally . 0)
1426     gnus-cache-removable-articles gnus-newsgroup-cached
1427     gnus-newsgroup-data gnus-newsgroup-data-reverse
1428     gnus-newsgroup-limit gnus-newsgroup-limits
1429     gnus-newsgroup-charset gnus-newsgroup-display
1430     gnus-summary-use-undownloaded-faces
1431     gnus-newsgroup-incorporated)
1432   "Variables that are buffer-local to the summary buffers.")
1433
1434 (defvar gnus-newsgroup-variables nil
1435   "A list of variables that have separate values in different newsgroups.
1436 A list of newsgroup (summary buffer) local variables, or cons of
1437 variables and their default values (when the default values are not
1438 nil), that should be made global while the summary buffer is active.
1439 These variables can be used to set variables in the group parameters
1440 while still allowing them to affect operations done in other
1441 buffers. For example:
1442
1443 \(setq gnus-newsgroup-variables
1444      '(message-use-followup-to
1445        (gnus-visible-headers .
1446          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1447 ")
1448
1449 ;; Byte-compiler warning.
1450 (eval-when-compile
1451   ;; Bind features so that require will believe that gnus-sum has
1452   ;; already been loaded (avoids infinite recursion)
1453   (let ((features (cons 'gnus-sum features)))
1454     ;; Several of the declarations in gnus-sum are needed to load the
1455     ;; following files. Right now, these definitions have been
1456     ;; compiled but not defined (evaluated).  We could either do a
1457     ;; eval-and-compile about all of the declarations or evaluate the
1458     ;; source file.
1459     (if (boundp 'gnus-newsgroup-variables)
1460         nil
1461       (load "gnus-sum.el" t t t))
1462     (require 'gnus)
1463     (require 'gnus-agent)
1464     (require 'gnus-art)))
1465
1466 ;; Subject simplification.
1467
1468 (defun gnus-simplify-whitespace (str)
1469   "Remove excessive whitespace from STR."
1470   ;; Multiple spaces.
1471   (while (string-match "[ \t][ \t]+" str)
1472     (setq str (concat (substring str 0 (match-beginning 0))
1473                         " "
1474                         (substring str (match-end 0)))))
1475   ;; Leading spaces.
1476   (when (string-match "^[ \t]+" str)
1477     (setq str (substring str (match-end 0))))
1478   ;; Trailing spaces.
1479   (when (string-match "[ \t]+$" str)
1480     (setq str (substring str 0 (match-beginning 0))))
1481   str)
1482
1483 (defun gnus-simplify-all-whitespace (str)
1484   "Remove all whitespace from STR."
1485   (while (string-match "[ \t\n]+" str)
1486     (setq str (replace-match "" nil nil str)))
1487   str)
1488
1489 (defsubst gnus-simplify-subject-re (subject)
1490   "Remove \"Re:\" from subject lines."
1491   (if (string-match message-subject-re-regexp subject)
1492       (substring subject (match-end 0))
1493     subject))
1494
1495 (defun gnus-simplify-subject (subject &optional re-only)
1496   "Remove `Re:' and words in parentheses.
1497 If RE-ONLY is non-nil, strip leading `Re:'s only."
1498   (let ((case-fold-search t))           ;Ignore case.
1499     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1500     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1501       (setq subject (substring subject (match-end 0))))
1502     ;; Remove uninteresting prefixes.
1503     (when (and (not re-only)
1504                gnus-simplify-ignored-prefixes
1505                (string-match gnus-simplify-ignored-prefixes subject))
1506       (setq subject (substring subject (match-end 0))))
1507     ;; Remove words in parentheses from end.
1508     (unless re-only
1509       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1510         (setq subject (substring subject 0 (match-beginning 0)))))
1511     ;; Return subject string.
1512     subject))
1513
1514 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1515 ;; all whitespace.
1516 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1517   (goto-char (point-min))
1518   (while (re-search-forward regexp nil t)
1519     (replace-match (or newtext ""))))
1520
1521 (defun gnus-simplify-buffer-fuzzy ()
1522   "Simplify string in the buffer fuzzily.
1523 The string in the accessible portion of the current buffer is simplified.
1524 It is assumed to be a single-line subject.
1525 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1526 matter is removed.  Additional things can be deleted by setting
1527 `gnus-simplify-subject-fuzzy-regexp'."
1528   (let ((case-fold-search t)
1529         (modified-tick))
1530     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1531
1532     (while (not (eq modified-tick (buffer-modified-tick)))
1533       (setq modified-tick (buffer-modified-tick))
1534       (cond
1535        ((listp gnus-simplify-subject-fuzzy-regexp)
1536         (mapcar 'gnus-simplify-buffer-fuzzy-step
1537                 gnus-simplify-subject-fuzzy-regexp))
1538        (gnus-simplify-subject-fuzzy-regexp
1539         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1540       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1541       (gnus-simplify-buffer-fuzzy-step
1542        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1543       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1544
1545     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1546     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1547     (gnus-simplify-buffer-fuzzy-step " $")
1548     (gnus-simplify-buffer-fuzzy-step "^ +")))
1549
1550 (defun gnus-simplify-subject-fuzzy (subject)
1551   "Simplify a subject string fuzzily.
1552 See `gnus-simplify-buffer-fuzzy' for details."
1553   (save-excursion
1554     (gnus-set-work-buffer)
1555     (let ((case-fold-search t))
1556       ;; Remove uninteresting prefixes.
1557       (when (and gnus-simplify-ignored-prefixes
1558                  (string-match gnus-simplify-ignored-prefixes subject))
1559         (setq subject (substring subject (match-end 0))))
1560       (insert subject)
1561       (inline (gnus-simplify-buffer-fuzzy))
1562       (buffer-string))))
1563
1564 (defsubst gnus-simplify-subject-fully (subject)
1565   "Simplify a subject string according to `gnus-summary-gather-subject-limit'."
1566   (cond
1567    (gnus-simplify-subject-functions
1568     (gnus-map-function gnus-simplify-subject-functions subject))
1569    ((null gnus-summary-gather-subject-limit)
1570     (gnus-simplify-subject-re subject))
1571    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1572     (gnus-simplify-subject-fuzzy subject))
1573    ((numberp gnus-summary-gather-subject-limit)
1574     (gnus-limit-string (gnus-simplify-subject-re subject)
1575                        gnus-summary-gather-subject-limit))
1576    (t
1577     subject)))
1578
1579 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1580   "Check whether two subjects are equal.
1581 If optional argument SIMPLE-FIRST is t, first argument is already
1582 simplified."
1583   (cond
1584    ((null simple-first)
1585     (equal (gnus-simplify-subject-fully s1)
1586            (gnus-simplify-subject-fully s2)))
1587    (t
1588     (equal s1
1589            (gnus-simplify-subject-fully s2)))))
1590
1591 (defun gnus-summary-bubble-group ()
1592   "Increase the score of the current group.
1593 This is a handy function to add to `gnus-summary-exit-hook' to
1594 increase the score of each group you read."
1595   (gnus-group-add-score gnus-newsgroup-name))
1596
1597 \f
1598 ;;;
1599 ;;; Gnus summary mode
1600 ;;;
1601
1602 (put 'gnus-summary-mode 'mode-class 'special)
1603
1604 (defvar gnus-article-commands-menu)
1605
1606 ;; Non-orthogonal keys
1607
1608 (gnus-define-keys gnus-summary-mode-map
1609   " " gnus-summary-next-page
1610   "\177" gnus-summary-prev-page
1611   [delete] gnus-summary-prev-page
1612   [backspace] gnus-summary-prev-page
1613   "\r" gnus-summary-scroll-up
1614   "\M-\r" gnus-summary-scroll-down
1615   "n" gnus-summary-next-unread-article
1616   "p" gnus-summary-prev-unread-article
1617   "N" gnus-summary-next-article
1618   "P" gnus-summary-prev-article
1619   "\M-\C-n" gnus-summary-next-same-subject
1620   "\M-\C-p" gnus-summary-prev-same-subject
1621   "\M-n" gnus-summary-next-unread-subject
1622   "\M-p" gnus-summary-prev-unread-subject
1623   "." gnus-summary-first-unread-article
1624   "," gnus-summary-best-unread-article
1625   "\M-s" gnus-summary-search-article-forward
1626   "\M-r" gnus-summary-search-article-backward
1627   "<" gnus-summary-beginning-of-article
1628   ">" gnus-summary-end-of-article
1629   "j" gnus-summary-goto-article
1630   "^" gnus-summary-refer-parent-article
1631   "\M-^" gnus-summary-refer-article
1632   "u" gnus-summary-tick-article-forward
1633   "!" gnus-summary-tick-article-forward
1634   "U" gnus-summary-tick-article-backward
1635   "d" gnus-summary-mark-as-read-forward
1636   "D" gnus-summary-mark-as-read-backward
1637   "E" gnus-summary-mark-as-expirable
1638   "\M-u" gnus-summary-clear-mark-forward
1639   "\M-U" gnus-summary-clear-mark-backward
1640   "k" gnus-summary-kill-same-subject-and-select
1641   "\C-k" gnus-summary-kill-same-subject
1642   "\M-\C-k" gnus-summary-kill-thread
1643   "\M-\C-l" gnus-summary-lower-thread
1644   "e" gnus-summary-edit-article
1645   "#" gnus-summary-mark-as-processable
1646   "\M-#" gnus-summary-unmark-as-processable
1647   "\M-\C-t" gnus-summary-toggle-threads
1648   "\M-\C-s" gnus-summary-show-thread
1649   "\M-\C-h" gnus-summary-hide-thread
1650   "\M-\C-f" gnus-summary-next-thread
1651   "\M-\C-b" gnus-summary-prev-thread
1652   [(meta down)] gnus-summary-next-thread
1653   [(meta up)] gnus-summary-prev-thread
1654   "\M-\C-u" gnus-summary-up-thread
1655   "\M-\C-d" gnus-summary-down-thread
1656   "&" gnus-summary-execute-command
1657   "c" gnus-summary-catchup-and-exit
1658   "\C-w" gnus-summary-mark-region-as-read
1659   "\C-t" gnus-summary-toggle-truncation
1660   "?" gnus-summary-mark-as-dormant
1661   "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1662   "\C-c\C-s\C-n" gnus-summary-sort-by-number
1663   "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1664   "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1665   "\C-c\C-s\C-a" gnus-summary-sort-by-author
1666   "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1667   "\C-c\C-s\C-d" gnus-summary-sort-by-date
1668   "\C-c\C-s\C-i" gnus-summary-sort-by-score
1669   "\C-c\C-s\C-o" gnus-summary-sort-by-original
1670   "\C-c\C-s\C-r" gnus-summary-sort-by-random
1671   "=" gnus-summary-expand-window
1672   "\C-x\C-s" gnus-summary-reselect-current-group
1673   "\M-g" gnus-summary-rescan-group
1674   "w" gnus-summary-stop-page-breaking
1675   "\C-c\C-r" gnus-summary-caesar-message
1676   "\M-t" gnus-summary-toggle-mime
1677   "f" gnus-summary-followup
1678   "F" gnus-summary-followup-with-original
1679   "C" gnus-summary-cancel-article
1680   "r" gnus-summary-reply
1681   "R" gnus-summary-reply-with-original
1682   "\C-c\C-f" gnus-summary-mail-forward
1683   "o" gnus-summary-save-article
1684   "\C-o" gnus-summary-save-article-mail
1685   "|" gnus-summary-pipe-output
1686   "\M-k" gnus-summary-edit-local-kill
1687   "\M-K" gnus-summary-edit-global-kill
1688   ;; "V" gnus-version
1689   "\C-c\C-d" gnus-summary-describe-group
1690   "q" gnus-summary-exit
1691   "Q" gnus-summary-exit-no-update
1692   "\C-c\C-i" gnus-info-find-node
1693   gnus-mouse-2 gnus-mouse-pick-article
1694   "m" gnus-summary-mail-other-window
1695   "a" gnus-summary-post-news
1696   "i" gnus-summary-news-other-window
1697   "x" gnus-summary-limit-to-unread
1698   "s" gnus-summary-isearch-article
1699   "t" gnus-summary-toggle-header
1700   "g" gnus-summary-show-article
1701   "l" gnus-summary-goto-last-article
1702   "v" gnus-summary-preview-mime-message
1703   "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1704   "\C-d" gnus-summary-enter-digest-group
1705   "\M-\C-d" gnus-summary-read-document
1706   "\M-\C-e" gnus-summary-edit-parameters
1707   "\M-\C-a" gnus-summary-customize-parameters
1708   "\C-c\C-b" gnus-bug
1709   "\C-c\C-n" gnus-namazu-search
1710   "*" gnus-cache-enter-article
1711   "\M-*" gnus-cache-remove-article
1712   "\M-&" gnus-summary-universal-argument
1713   "\C-l" gnus-recenter
1714   "I" gnus-summary-increase-score
1715   "L" gnus-summary-lower-score
1716   "\M-i" gnus-symbolic-argument
1717   "h" gnus-summary-select-article-buffer
1718
1719   "V" gnus-summary-score-map
1720   "X" gnus-uu-extract-map
1721   "S" gnus-summary-send-map)
1722
1723   ;; Sort of orthogonal keymap
1724 (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1725   "t" gnus-summary-tick-article-forward
1726   "!" gnus-summary-tick-article-forward
1727   "d" gnus-summary-mark-as-read-forward
1728   "r" gnus-summary-mark-as-read-forward
1729   "c" gnus-summary-clear-mark-forward
1730   " " gnus-summary-clear-mark-forward
1731   "e" gnus-summary-mark-as-expirable
1732   "x" gnus-summary-mark-as-expirable
1733   "?" gnus-summary-mark-as-dormant
1734   "b" gnus-summary-set-bookmark
1735   "B" gnus-summary-remove-bookmark
1736   "#" gnus-summary-mark-as-processable
1737   "\M-#" gnus-summary-unmark-as-processable
1738   "S" gnus-summary-limit-include-expunged
1739   "C" gnus-summary-catchup
1740   "H" gnus-summary-catchup-to-here
1741   "h" gnus-summary-catchup-from-here
1742   "\C-c" gnus-summary-catchup-all
1743   "k" gnus-summary-kill-same-subject-and-select
1744   "K" gnus-summary-kill-same-subject
1745   "P" gnus-uu-mark-map)
1746
1747 (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1748   "c" gnus-summary-clear-above
1749   "u" gnus-summary-tick-above
1750   "m" gnus-summary-mark-above
1751   "k" gnus-summary-kill-below)
1752
1753 (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1754   "/" gnus-summary-limit-to-subject
1755   "n" gnus-summary-limit-to-articles
1756   "w" gnus-summary-pop-limit
1757   "s" gnus-summary-limit-to-subject
1758   "a" gnus-summary-limit-to-author
1759   "u" gnus-summary-limit-to-unread
1760   "m" gnus-summary-limit-to-marks
1761   "M" gnus-summary-limit-exclude-marks
1762   "v" gnus-summary-limit-to-score
1763   "*" gnus-summary-limit-include-cached
1764   "D" gnus-summary-limit-include-dormant
1765   "T" gnus-summary-limit-include-thread
1766   "d" gnus-summary-limit-exclude-dormant
1767   "t" gnus-summary-limit-to-age
1768   "." gnus-summary-limit-to-unseen
1769   "x" gnus-summary-limit-to-extra
1770   "p" gnus-summary-limit-to-display-predicate
1771   "E" gnus-summary-limit-include-expunged
1772   "c" gnus-summary-limit-exclude-childless-dormant
1773   "C" gnus-summary-limit-mark-excluded-as-read
1774   "o" gnus-summary-insert-old-articles
1775   "N" gnus-summary-insert-new-articles
1776   "r" gnus-summary-limit-to-replied)
1777
1778 (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1779   "n" gnus-summary-next-unread-article
1780   "p" gnus-summary-prev-unread-article
1781   "N" gnus-summary-next-article
1782   "P" gnus-summary-prev-article
1783   "\C-n" gnus-summary-next-same-subject
1784   "\C-p" gnus-summary-prev-same-subject
1785   "\M-n" gnus-summary-next-unread-subject
1786   "\M-p" gnus-summary-prev-unread-subject
1787   "f" gnus-summary-first-unread-article
1788   "b" gnus-summary-best-unread-article
1789   "j" gnus-summary-goto-article
1790   "g" gnus-summary-goto-subject
1791   "l" gnus-summary-goto-last-article
1792   "o" gnus-summary-pop-article)
1793
1794 (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1795   "k" gnus-summary-kill-thread
1796   "l" gnus-summary-lower-thread
1797   "i" gnus-summary-raise-thread
1798   "T" gnus-summary-toggle-threads
1799   "t" gnus-summary-rethread-current
1800   "^" gnus-summary-reparent-thread
1801   "s" gnus-summary-show-thread
1802   "S" gnus-summary-show-all-threads
1803   "h" gnus-summary-hide-thread
1804   "H" gnus-summary-hide-all-threads
1805   "n" gnus-summary-next-thread
1806   "p" gnus-summary-prev-thread
1807   "u" gnus-summary-up-thread
1808   "o" gnus-summary-top-thread
1809   "d" gnus-summary-down-thread
1810   "#" gnus-uu-mark-thread
1811   "\M-#" gnus-uu-unmark-thread)
1812
1813 (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1814   "g" gnus-summary-prepare
1815   "c" gnus-summary-insert-cached-articles
1816   "d" gnus-summary-insert-dormant-articles)
1817
1818 (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1819   "c" gnus-summary-catchup-and-exit
1820   "C" gnus-summary-catchup-all-and-exit
1821   "E" gnus-summary-exit-no-update
1822   "J" gnus-summary-jump-to-other-group
1823   "Q" gnus-summary-exit
1824   "Z" gnus-summary-exit
1825   "n" gnus-summary-catchup-and-goto-next-group
1826   "R" gnus-summary-reselect-current-group
1827   "G" gnus-summary-rescan-group
1828   "N" gnus-summary-next-group
1829   "s" gnus-summary-save-newsrc
1830   "P" gnus-summary-prev-group)
1831
1832 (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1833   " " gnus-summary-next-page
1834   "n" gnus-summary-next-page
1835   "\177" gnus-summary-prev-page
1836   [delete] gnus-summary-prev-page
1837   "p" gnus-summary-prev-page
1838   "\r" gnus-summary-scroll-up
1839   "\M-\r" gnus-summary-scroll-down
1840   "<" gnus-summary-beginning-of-article
1841   ">" gnus-summary-end-of-article
1842   "b" gnus-summary-beginning-of-article
1843   "e" gnus-summary-end-of-article
1844   "^" gnus-summary-refer-parent-article
1845   "r" gnus-summary-refer-parent-article
1846   "D" gnus-summary-enter-digest-group
1847   "R" gnus-summary-refer-references
1848   "T" gnus-summary-refer-thread
1849   "g" gnus-summary-show-article
1850   "s" gnus-summary-isearch-article
1851   "P" gnus-summary-print-article
1852   "M" gnus-mailing-list-insinuate
1853   "t" gnus-article-babel)
1854
1855 (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1856   "b" gnus-article-add-buttons
1857   "B" gnus-article-add-buttons-to-head
1858   "o" gnus-article-treat-overstrike
1859   "e" gnus-article-emphasize
1860   "w" gnus-article-fill-cited-article
1861   "Q" gnus-article-fill-long-lines
1862   "C" gnus-article-capitalize-sentences
1863   "c" gnus-article-remove-cr
1864   "Z" gnus-article-decode-HZ
1865   "A" gnus-article-treat-ansi-sequences
1866   "h" gnus-article-wash-html
1867   "u" gnus-article-unsplit-urls
1868   "f" gnus-article-display-x-face
1869   "l" gnus-summary-stop-page-breaking
1870   "r" gnus-summary-caesar-message
1871   "m" gnus-summary-morse-message
1872   "t" gnus-summary-toggle-header
1873   "g" gnus-treat-smiley
1874   "v" gnus-summary-verbose-headers
1875   "m" gnus-summary-toggle-mime
1876   "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1877   "p" gnus-article-verify-x-pgp-sig
1878   "d" gnus-article-treat-dumbquotes)
1879
1880 (gnus-define-keys (gnus-summary-wash-deuglify-map "Y" gnus-summary-wash-map)
1881   ;; mnemonic: deuglif*Y*
1882   "u" gnus-article-outlook-unwrap-lines
1883   "a" gnus-article-outlook-repair-attribution
1884   "c" gnus-article-outlook-rearrange-citation
1885   "f" gnus-article-outlook-deuglify-article) ;; mnemonic: full deuglify
1886
1887 (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1888   "a" gnus-article-hide
1889   "h" gnus-article-hide-headers
1890   "b" gnus-article-hide-boring-headers
1891   "s" gnus-article-hide-signature
1892   "c" gnus-article-hide-citation
1893   "C" gnus-article-hide-citation-in-followups
1894   "l" gnus-article-hide-list-identifiers
1895   "B" gnus-article-strip-banner
1896   "P" gnus-article-hide-pem
1897   "\C-c" gnus-article-hide-citation-maybe)
1898
1899 (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1900   "a" gnus-article-highlight
1901   "h" gnus-article-highlight-headers
1902   "c" gnus-article-highlight-citation
1903   "s" gnus-article-highlight-signature)
1904
1905 (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1906   "f" gnus-article-treat-fold-headers
1907   "u" gnus-article-treat-unfold-headers
1908   "n" gnus-article-treat-fold-newsgroups)
1909
1910 (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1911   "x" gnus-article-display-x-face
1912   "d" gnus-article-display-face
1913   "s" gnus-treat-smiley
1914   "D" gnus-article-remove-images
1915   "f" gnus-treat-from-picon
1916   "m" gnus-treat-mail-picon
1917   "n" gnus-treat-newsgroups-picon)
1918
1919 (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1920   "z" gnus-article-date-ut
1921   "u" gnus-article-date-ut
1922   "l" gnus-article-date-local
1923   "p" gnus-article-date-english
1924   "e" gnus-article-date-lapsed
1925   "o" gnus-article-date-original
1926   "i" gnus-article-date-iso8601
1927   "s" gnus-article-date-user)
1928
1929 (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1930   "t" gnus-article-remove-trailing-blank-lines
1931   "l" gnus-article-strip-leading-blank-lines
1932   "m" gnus-article-strip-multiple-blank-lines
1933   "a" gnus-article-strip-blank-lines
1934   "A" gnus-article-strip-all-blank-lines
1935   "s" gnus-article-strip-leading-space
1936   "e" gnus-article-strip-trailing-space
1937   "w" gnus-article-remove-leading-whitespace)
1938
1939 (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1940   "v" gnus-version
1941   "f" gnus-summary-fetch-faq
1942   "d" gnus-summary-describe-group
1943   "h" gnus-summary-describe-briefly
1944   "i" gnus-info-find-node
1945   "c" gnus-group-fetch-charter
1946   "C" gnus-group-fetch-control)
1947
1948 (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1949   "e" gnus-summary-expire-articles
1950   "\M-\C-e" gnus-summary-expire-articles-now
1951   "\177" gnus-summary-delete-article
1952   [delete] gnus-summary-delete-article
1953   [backspace] gnus-summary-delete-article
1954   "m" gnus-summary-move-article
1955   "r" gnus-summary-respool-article
1956   "w" gnus-summary-edit-article
1957   "c" gnus-summary-copy-article
1958   "B" gnus-summary-crosspost-article
1959   "q" gnus-summary-respool-query
1960   "t" gnus-summary-respool-trace
1961   "i" gnus-summary-import-article
1962   "I" gnus-summary-create-article
1963   "p" gnus-summary-article-posted-p)
1964
1965 (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1966   "o" gnus-summary-save-article
1967   "m" gnus-summary-save-article-mail
1968   "F" gnus-summary-write-article-file
1969   "r" gnus-summary-save-article-rmail
1970   "f" gnus-summary-save-article-file
1971   "b" gnus-summary-save-article-body-file
1972   "h" gnus-summary-save-article-folder
1973   "v" gnus-summary-save-article-vm
1974   "p" gnus-summary-pipe-output
1975   "P" gnus-summary-muttprint
1976   "s" gnus-soup-add-article)
1977
1978 (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1979   "b" gnus-summary-display-buttonized
1980   "m" gnus-summary-repair-multipart
1981   "v" gnus-article-view-part
1982   "o" gnus-article-save-part
1983   "c" gnus-article-copy-part
1984   "C" gnus-article-view-part-as-charset
1985   "e" gnus-article-view-part-externally
1986   "E" gnus-article-encrypt-body
1987   "i" gnus-article-inline-part
1988   "|" gnus-article-pipe-part)
1989
1990 (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1991   "p" gnus-summary-mark-as-processable
1992   "u" gnus-summary-unmark-as-processable
1993   "U" gnus-summary-unmark-all-processable
1994   "v" gnus-uu-mark-over
1995   "s" gnus-uu-mark-series
1996   "r" gnus-uu-mark-region
1997   "g" gnus-uu-unmark-region
1998   "R" gnus-uu-mark-by-regexp
1999   "G" gnus-uu-unmark-by-regexp
2000   "t" gnus-uu-mark-thread
2001   "T" gnus-uu-unmark-thread
2002   "a" gnus-uu-mark-all
2003   "b" gnus-uu-mark-buffer
2004   "S" gnus-uu-mark-sparse
2005   "k" gnus-summary-kill-process-mark
2006   "y" gnus-summary-yank-process-mark
2007   "w" gnus-summary-save-process-mark
2008   "i" gnus-uu-invert-processable)
2009
2010 (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
2011   ;;"x" gnus-uu-extract-any
2012   "m" gnus-summary-save-parts
2013   "u" gnus-uu-decode-uu
2014   "U" gnus-uu-decode-uu-and-save
2015   "s" gnus-uu-decode-unshar
2016   "S" gnus-uu-decode-unshar-and-save
2017   "o" gnus-uu-decode-save
2018   "O" gnus-uu-decode-save
2019   "b" gnus-uu-decode-binhex
2020   "B" gnus-uu-decode-binhex
2021   "p" gnus-uu-decode-postscript
2022   "P" gnus-uu-decode-postscript-and-save)
2023
2024 (gnus-define-keys
2025     (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
2026   "u" gnus-uu-decode-uu-view
2027   "U" gnus-uu-decode-uu-and-save-view
2028   "s" gnus-uu-decode-unshar-view
2029   "S" gnus-uu-decode-unshar-and-save-view
2030   "o" gnus-uu-decode-save-view
2031   "O" gnus-uu-decode-save-view
2032   "b" gnus-uu-decode-binhex-view
2033   "B" gnus-uu-decode-binhex-view
2034   "p" gnus-uu-decode-postscript-view
2035   "P" gnus-uu-decode-postscript-and-save-view)
2036
2037 (defvar gnus-article-post-menu nil)
2038
2039 (defconst gnus-summary-menu-maxlen 20)
2040
2041 (defun gnus-summary-menu-split (menu)
2042   ;; If we have lots of elements, divide them into groups of 20
2043   ;; and make a pane (or submenu) for each one.
2044   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
2045       (let ((menu menu) sublists next
2046             (i 1))
2047         (while menu
2048           ;; Pull off the next gnus-summary-menu-maxlen elements
2049           ;; and make them the next element of sublist.
2050           (setq next (nthcdr gnus-summary-menu-maxlen menu))
2051           (if next
2052               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
2053                       nil))
2054           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
2055                                              (aref (car (last menu)) 0)) menu)
2056                                sublists))
2057           (setq i (1+ i))
2058           (setq menu next))
2059         (nreverse sublists))
2060     ;; Few elements--put them all in one pane.
2061     menu))
2062
2063 (defun gnus-summary-make-menu-bar ()
2064   (gnus-turn-off-edit-menu 'summary)
2065
2066   (unless (boundp 'gnus-summary-misc-menu)
2067
2068     (easy-menu-define
2069      gnus-summary-kill-menu gnus-summary-mode-map ""
2070      (cons
2071       "Score"
2072       (nconc
2073        (list
2074         ["Customize" gnus-score-customize t])
2075        (gnus-make-score-map 'increase)
2076        (gnus-make-score-map 'lower)
2077        '(("Mark"
2078           ["Kill below" gnus-summary-kill-below t]
2079           ["Mark above" gnus-summary-mark-above t]
2080           ["Tick above" gnus-summary-tick-above t]
2081           ["Clear above" gnus-summary-clear-above t])
2082          ["Current score" gnus-summary-current-score t]
2083          ["Set score" gnus-summary-set-score t]
2084          ["Switch current score file..." gnus-score-change-score-file t]
2085          ["Set mark below..." gnus-score-set-mark-below t]
2086          ["Set expunge below..." gnus-score-set-expunge-below t]
2087          ["Edit current score file" gnus-score-edit-current-scores t]
2088          ["Edit score file" gnus-score-edit-file t]
2089          ["Trace score" gnus-score-find-trace t]
2090          ["Find words" gnus-score-find-favourite-words t]
2091          ["Rescore buffer" gnus-summary-rescore t]
2092          ["Increase score..." gnus-summary-increase-score t]
2093          ["Lower score..." gnus-summary-lower-score t]))))
2094
2095     ;; Define both the Article menu in the summary buffer and the
2096     ;; equivalent Commands menu in the article buffer here for
2097     ;; consistency.
2098     (let ((innards
2099            `(("Hide"
2100               ["All" gnus-article-hide t]
2101               ["Headers" gnus-article-hide-headers t]
2102               ["Signature" gnus-article-hide-signature t]
2103               ["Citation" gnus-article-hide-citation t]
2104               ["List identifiers" gnus-article-hide-list-identifiers t]
2105               ["Banner" gnus-article-strip-banner t]
2106               ["Boring headers" gnus-article-hide-boring-headers t])
2107              ("Highlight"
2108               ["All" gnus-article-highlight t]
2109               ["Headers" gnus-article-highlight-headers t]
2110               ["Signature" gnus-article-highlight-signature t]
2111               ["Citation" gnus-article-highlight-citation t])
2112              ("Date"
2113               ["Local" gnus-article-date-local t]
2114               ["ISO8601" gnus-article-date-iso8601 t]
2115               ["UT" gnus-article-date-ut t]
2116               ["Original" gnus-article-date-original t]
2117               ["Lapsed" gnus-article-date-lapsed t]
2118               ["User-defined" gnus-article-date-user t])
2119              ("Display"
2120               ["Remove images" gnus-article-remove-images t]
2121               ["Toggle smiley" gnus-treat-smiley t]
2122               ["Show X-Face" gnus-article-display-x-face t]
2123               ["Show picons in From" gnus-treat-from-picon t]
2124               ["Show picons in mail headers" gnus-treat-mail-picon t]
2125               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2126               ("View as different encoding"
2127                ,@(gnus-summary-menu-split
2128                   (mapcar
2129                    (lambda (cs)
2130                      ;; Since easymenu under Emacs doesn't allow
2131                      ;; lambda forms for menu commands, we should
2132                      ;; provide intern'ed function symbols.
2133                      (let ((command (intern (format "\
2134 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2135                        (fset command
2136                              `(lambda ()
2137                                 (interactive)
2138                                 (let ((gnus-summary-show-article-charset-alist
2139                                        '((1 . ,cs))))
2140                                   (gnus-summary-show-article 1))))
2141                        `[,(symbol-name cs) ,command t]))
2142                    (sort (if (fboundp 'coding-system-list)
2143                              (coding-system-list)
2144                            ;;(mapcar 'car mm-mime-mule-charset-alist)
2145                            )
2146                          'string<)))))
2147              ("Washing"
2148               ("Remove Blanks"
2149                ["Leading" gnus-article-strip-leading-blank-lines t]
2150                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2151                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2152                ["All of the above" gnus-article-strip-blank-lines t]
2153                ["All" gnus-article-strip-all-blank-lines t]
2154                ["Leading space" gnus-article-strip-leading-space t]
2155                ["Trailing space" gnus-article-strip-trailing-space t]
2156                ["Leading space in headers"
2157                 gnus-article-remove-leading-whitespace t])
2158               ["Overstrike" gnus-article-treat-overstrike t]
2159               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2160               ["Emphasis" gnus-article-emphasize t]
2161               ["Word wrap" gnus-article-fill-cited-article t]
2162               ["Fill long lines" gnus-article-fill-long-lines t]
2163               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2164               ["Remove CR" gnus-article-remove-cr t]
2165               ["Rot 13" gnus-summary-caesar-message
2166                ,@(if (featurep 'xemacs) '(t)
2167                    '(:help "\"Caesar rotate\" article by 13"))]
2168               ["Morse decode" gnus-summary-morse-message t]
2169               ["Unix pipe..." gnus-summary-pipe-message t]
2170               ["Add buttons" gnus-article-add-buttons t]
2171               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2172               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2173               ["Toggle MIME" gnus-summary-toggle-mime t]
2174               ["Verbose header" gnus-summary-verbose-headers t]
2175               ["Toggle header" gnus-summary-toggle-header t]
2176               ["Unfold headers" gnus-article-treat-unfold-headers t]
2177               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2178               ["Html" gnus-article-wash-html t]
2179               ["Unsplit URLs" gnus-article-unsplit-urls t]
2180               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2181               ["Decode HZ" gnus-article-decode-HZ t]
2182               ["ANSI sequences" gnus-article-treat-ansi-sequences t]
2183               ("(Outlook) Deuglify"
2184                ["Unwrap lines" gnus-article-outlook-unwrap-lines t]
2185                ["Repair attribution" gnus-article-outlook-repair-attribution t]
2186                ["Rearrange citation" gnus-article-outlook-rearrange-citation t]
2187                ["Full (Outlook) deuglify"
2188                 gnus-article-outlook-deuglify-article t])
2189               )
2190              ("Output"
2191               ["Save in default format..." gnus-summary-save-article
2192                ,@(if (featurep 'xemacs) '(t)
2193                    '(:help "Save article using default method"))]
2194               ["Save in file..." gnus-summary-save-article-file
2195                ,@(if (featurep 'xemacs) '(t)
2196                    '(:help "Save article in file"))]
2197               ["Save in Unix mail format..." gnus-summary-save-article-mail t]
2198               ["Save in MH folder..." gnus-summary-save-article-folder t]
2199               ["Save in VM folder..." gnus-summary-save-article-vm t]
2200               ["Save in RMAIL mbox..." gnus-summary-save-article-rmail t]
2201               ["Save body in file..." gnus-summary-save-article-body-file t]
2202               ["Pipe through a filter..." gnus-summary-pipe-output t]
2203               ["Add to SOUP packet" gnus-soup-add-article t]
2204               ["Print with Muttprint..." gnus-summary-muttprint t]
2205               ["Print" gnus-summary-print-article t])
2206              ("Backend"
2207               ["Respool article..." gnus-summary-respool-article t]
2208               ["Move article..." gnus-summary-move-article
2209                (gnus-check-backend-function
2210                 'request-move-article gnus-newsgroup-name)]
2211               ["Copy article..." gnus-summary-copy-article t]
2212               ["Crosspost article..." gnus-summary-crosspost-article
2213                (gnus-check-backend-function
2214                 'request-replace-article gnus-newsgroup-name)]
2215               ["Import file..." gnus-summary-import-article
2216                (gnus-check-backend-function
2217                 'request-accept-article gnus-newsgroup-name)]
2218               ["Create article..." gnus-summary-create-article
2219                (gnus-check-backend-function
2220                 'request-accept-article gnus-newsgroup-name)]
2221               ["Check if posted" gnus-summary-article-posted-p t]
2222               ["Edit article" gnus-summary-edit-article
2223                (not (gnus-group-read-only-p))]
2224               ["Delete article" gnus-summary-delete-article
2225                (gnus-check-backend-function
2226                 'request-expire-articles gnus-newsgroup-name)]
2227               ["Query respool" gnus-summary-respool-query t]
2228               ["Trace respool" gnus-summary-respool-trace t]
2229               ["Delete expirable articles" gnus-summary-expire-articles-now
2230                (gnus-check-backend-function
2231                 'request-expire-articles gnus-newsgroup-name)])
2232              ("Extract"
2233               ["Uudecode" gnus-uu-decode-uu
2234                ,@(if (featurep 'xemacs) '(t)
2235                    '(:help "Decode uuencoded article(s)"))]
2236               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2237               ["Unshar" gnus-uu-decode-unshar t]
2238               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2239               ["Save" gnus-uu-decode-save t]
2240               ["Binhex" gnus-uu-decode-binhex t]
2241               ["Postscript" gnus-uu-decode-postscript t])
2242              ("Cache"
2243               ["Enter article" gnus-cache-enter-article t]
2244               ["Remove article" gnus-cache-remove-article t])
2245              ["Translate" gnus-article-babel t]
2246              ["Select article buffer" gnus-summary-select-article-buffer t]
2247              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2248              ["Isearch article..." gnus-summary-isearch-article t]
2249              ["Beginning of the article" gnus-summary-beginning-of-article t]
2250              ["End of the article" gnus-summary-end-of-article t]
2251              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2252              ["Fetch referenced articles" gnus-summary-refer-references t]
2253              ["Fetch current thread" gnus-summary-refer-thread t]
2254              ["Fetch article with id..." gnus-summary-refer-article t]
2255              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2256              ["Redisplay" gnus-summary-show-article t]
2257              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2258       (easy-menu-define
2259        gnus-summary-article-menu gnus-summary-mode-map ""
2260        (cons "Article" innards))
2261
2262       (if (not (keymapp gnus-summary-article-menu))
2263           (easy-menu-define
2264            gnus-article-commands-menu gnus-article-mode-map ""
2265            (cons "Commands" innards))
2266         ;; in Emacs, don't share menu.
2267         (setq gnus-article-commands-menu
2268               (copy-keymap gnus-summary-article-menu))
2269         (define-key gnus-article-mode-map [menu-bar commands]
2270           (cons "Commands" gnus-article-commands-menu))))
2271
2272     (easy-menu-define
2273      gnus-summary-thread-menu gnus-summary-mode-map ""
2274      '("Threads"
2275        ["Find all messages in thread" gnus-summary-refer-thread t]
2276        ["Toggle threading" gnus-summary-toggle-threads t]
2277        ["Hide threads" gnus-summary-hide-all-threads t]
2278        ["Show threads" gnus-summary-show-all-threads t]
2279        ["Hide thread" gnus-summary-hide-thread t]
2280        ["Show thread" gnus-summary-show-thread t]
2281        ["Go to next thread" gnus-summary-next-thread t]
2282        ["Go to previous thread" gnus-summary-prev-thread t]
2283        ["Go down thread" gnus-summary-down-thread t]
2284        ["Go up thread" gnus-summary-up-thread t]
2285        ["Top of thread" gnus-summary-top-thread t]
2286        ["Mark thread as read" gnus-summary-kill-thread t]
2287        ["Lower thread score" gnus-summary-lower-thread t]
2288        ["Raise thread score" gnus-summary-raise-thread t]
2289        ["Rethread current" gnus-summary-rethread-current t]))
2290
2291     (easy-menu-define
2292      gnus-summary-post-menu gnus-summary-mode-map ""
2293      `("Post"
2294        ["Send a message (mail or news)" gnus-summary-post-news
2295         ,@(if (featurep 'xemacs) '(t)
2296             '(:help "Post an article"))]
2297        ["Followup" gnus-summary-followup
2298         ,@(if (featurep 'xemacs) '(t)
2299             '(:help "Post followup to this article"))]
2300        ["Followup and yank" gnus-summary-followup-with-original
2301         ,@(if (featurep 'xemacs) '(t)
2302             '(:help "Post followup to this article, quoting its contents"))]
2303        ["Supersede article" gnus-summary-supersede-article t]
2304        ["Cancel article" gnus-summary-cancel-article
2305         ,@(if (featurep 'xemacs) '(t)
2306             '(:help "Cancel an article you posted"))]
2307        ["Reply" gnus-summary-reply t]
2308        ["Reply and yank" gnus-summary-reply-with-original t]
2309        ["Wide reply" gnus-summary-wide-reply t]
2310        ["Wide reply and yank" gnus-summary-wide-reply-with-original
2311         ,@(if (featurep 'xemacs) '(t)
2312             '(:help "Mail a reply, quoting this article"))]
2313        ["Very wide reply" gnus-summary-very-wide-reply t]
2314        ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2315         ,@(if (featurep 'xemacs) '(t)
2316             '(:help "Mail a very wide reply, quoting this article"))]
2317        ["Mail forward" gnus-summary-mail-forward t]
2318        ["Post forward" gnus-summary-post-forward t]
2319        ["Digest and mail" gnus-summary-digest-mail-forward t]
2320        ["Digest and post" gnus-summary-digest-post-forward t]
2321        ["Resend message" gnus-summary-resend-message t]
2322        ["Resend message edit" gnus-summary-resend-message-edit t]
2323        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2324        ["Send a mail" gnus-summary-mail-other-window t]
2325        ["Create a local message" gnus-summary-news-other-window t]
2326        ["Uuencode and post" gnus-uu-post-news
2327         ,@(if (featurep 'xemacs) '(t)
2328             '(:help "Post a uuencoded article"))]
2329        ["Followup via news" gnus-summary-followup-to-mail t]
2330        ["Followup via news and yank"
2331         gnus-summary-followup-to-mail-with-original t]
2332        ;;("Draft"
2333        ;;["Send" gnus-summary-send-draft t]
2334        ;;["Send bounced" gnus-resend-bounced-mail t])
2335        ))
2336
2337     (cond
2338      ((not (keymapp gnus-summary-post-menu))
2339       (setq gnus-article-post-menu gnus-summary-post-menu))
2340      ((not gnus-article-post-menu)
2341       ;; Don't share post menu.
2342       (setq gnus-article-post-menu
2343             (copy-keymap gnus-summary-post-menu))))
2344     (define-key gnus-article-mode-map [menu-bar post]
2345       (cons "Post" gnus-article-post-menu))
2346
2347     (easy-menu-define
2348      gnus-summary-misc-menu gnus-summary-mode-map ""
2349      `("Gnus"
2350        ("Mark Read"
2351         ["Mark as read" gnus-summary-mark-as-read-forward t]
2352         ["Mark same subject and select"
2353          gnus-summary-kill-same-subject-and-select t]
2354         ["Mark same subject" gnus-summary-kill-same-subject t]
2355         ["Catchup" gnus-summary-catchup
2356          ,@(if (featurep 'xemacs) '(t)
2357              '(:help "Mark unread articles in this group as read"))]
2358         ["Catchup all" gnus-summary-catchup-all t]
2359         ["Catchup to here" gnus-summary-catchup-to-here t]
2360         ["Catchup from here" gnus-summary-catchup-from-here t]
2361         ["Catchup region" gnus-summary-mark-region-as-read
2362          (gnus-mark-active-p)]
2363         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2364        ("Mark Various"
2365         ["Tick" gnus-summary-tick-article-forward t]
2366         ["Mark as dormant" gnus-summary-mark-as-dormant t]
2367         ["Remove marks" gnus-summary-clear-mark-forward t]
2368         ["Set expirable mark" gnus-summary-mark-as-expirable t]
2369         ["Set bookmark" gnus-summary-set-bookmark t]
2370         ["Remove bookmark" gnus-summary-remove-bookmark t])
2371        ("Limit to"
2372         ["Marks..." gnus-summary-limit-to-marks t]
2373         ["Subject..." gnus-summary-limit-to-subject t]
2374         ["Author..." gnus-summary-limit-to-author t]
2375         ["Age..." gnus-summary-limit-to-age t]
2376         ["Extra..." gnus-summary-limit-to-extra t]
2377         ["Score..." gnus-summary-limit-to-score t]
2378         ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2379         ["Unread" gnus-summary-limit-to-unread t]
2380         ["Unseen" gnus-summary-limit-to-unseen t]
2381         ["Replied" gnus-summary-limit-to-replied t]
2382         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2383         ["Next articles" gnus-summary-limit-to-articles t]
2384         ["Pop limit" gnus-summary-pop-limit t]
2385         ["Show dormant" gnus-summary-limit-include-dormant t]
2386         ["Hide childless dormant"
2387          gnus-summary-limit-exclude-childless-dormant t]
2388         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2389         ["Hide marked" gnus-summary-limit-exclude-marks t]
2390         ["Show expunged" gnus-summary-limit-include-expunged t])
2391        ("Process Mark"
2392         ["Set mark" gnus-summary-mark-as-processable t]
2393         ["Remove mark" gnus-summary-unmark-as-processable t]
2394         ["Remove all marks" gnus-summary-unmark-all-processable t]
2395         ["Mark above" gnus-uu-mark-over t]
2396         ["Mark series" gnus-uu-mark-series t]
2397         ["Mark region" gnus-uu-mark-region (gnus-mark-active-p)]
2398         ["Unmark region" gnus-uu-unmark-region (gnus-mark-active-p)]
2399         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2400         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2401         ["Mark all" gnus-uu-mark-all t]
2402         ["Mark buffer" gnus-uu-mark-buffer t]
2403         ["Mark sparse" gnus-uu-mark-sparse t]
2404         ["Mark thread" gnus-uu-mark-thread t]
2405         ["Unmark thread" gnus-uu-unmark-thread t]
2406         ("Process Mark Sets"
2407          ["Kill" gnus-summary-kill-process-mark t]
2408          ["Yank" gnus-summary-yank-process-mark
2409           gnus-newsgroup-process-stack]
2410          ["Save" gnus-summary-save-process-mark t]
2411          ["Run command on marked..." gnus-summary-universal-argument t]))
2412        ("Scroll article"
2413         ["Page forward" gnus-summary-next-page
2414          ,@(if (featurep 'xemacs) '(t)
2415              '(:help "Show next page of article"))]
2416         ["Page backward" gnus-summary-prev-page
2417          ,@(if (featurep 'xemacs) '(t)
2418              '(:help "Show previous page of article"))]
2419         ["Line forward" gnus-summary-scroll-up t])
2420        ("Move"
2421         ["Next unread article" gnus-summary-next-unread-article t]
2422         ["Previous unread article" gnus-summary-prev-unread-article t]
2423         ["Next article" gnus-summary-next-article t]
2424         ["Previous article" gnus-summary-prev-article t]
2425         ["Next unread subject" gnus-summary-next-unread-subject t]
2426         ["Previous unread subject" gnus-summary-prev-unread-subject t]
2427         ["Next article same subject" gnus-summary-next-same-subject t]
2428         ["Previous article same subject" gnus-summary-prev-same-subject t]
2429         ["First unread article" gnus-summary-first-unread-article t]
2430         ["Best unread article" gnus-summary-best-unread-article t]
2431         ["Go to subject number..." gnus-summary-goto-subject t]
2432         ["Go to article number..." gnus-summary-goto-article t]
2433         ["Go to the last article" gnus-summary-goto-last-article t]
2434         ["Pop article off history" gnus-summary-pop-article t])
2435        ("Sort"
2436         ["Sort by number" gnus-summary-sort-by-number t]
2437         ["Sort by author" gnus-summary-sort-by-author t]
2438         ["Sort by subject" gnus-summary-sort-by-subject t]
2439         ["Sort by date" gnus-summary-sort-by-date t]
2440         ["Sort by score" gnus-summary-sort-by-score t]
2441         ["Sort by lines" gnus-summary-sort-by-lines t]
2442         ["Sort by characters" gnus-summary-sort-by-chars t]
2443         ["Randomize" gnus-summary-sort-by-random t]
2444         ["Original sort" gnus-summary-sort-by-original t])
2445        ("Help"
2446         ["Fetch group FAQ" gnus-summary-fetch-faq t]
2447         ["Describe group" gnus-summary-describe-group t]
2448         ["Fetch charter" gnus-group-fetch-charter
2449          ,@(if (featurep 'xemacs) nil
2450              '(:help "Display the charter of the current group"))]
2451         ["Fetch control message" gnus-group-fetch-control
2452          ,@(if (featurep 'xemacs) nil
2453              '(:help "Display the archived control message for the current group"))]
2454         ["Read manual" gnus-info-find-node t])
2455        ("Modes"
2456         ["Pick and read" gnus-pick-mode t]
2457         ["Binary" gnus-binary-mode t])
2458        ("Regeneration"
2459         ["Regenerate" gnus-summary-prepare t]
2460         ["Insert cached articles" gnus-summary-insert-cached-articles t]
2461         ["Insert dormant articles" gnus-summary-insert-dormant-articles t]
2462         ["Toggle threading" gnus-summary-toggle-threads t])
2463        ["See old articles" gnus-summary-insert-old-articles t]
2464        ["See new articles" gnus-summary-insert-new-articles t]
2465        ["Filter articles..." gnus-summary-execute-command t]
2466        ["Run command on articles..." gnus-summary-universal-argument t]
2467        ["Search articles forward..." gnus-summary-search-article-forward t]
2468        ["Search articles backward..." gnus-summary-search-article-backward t]
2469        ["Toggle line truncation" gnus-summary-toggle-truncation t]
2470        ["Expand window" gnus-summary-expand-window t]
2471        ["Expire expirable articles" gnus-summary-expire-articles
2472         (gnus-check-backend-function
2473          'request-expire-articles gnus-newsgroup-name)]
2474        ["Edit local kill file" gnus-summary-edit-local-kill t]
2475        ["Edit main kill file" gnus-summary-edit-global-kill t]
2476        ["Edit group parameters" gnus-summary-edit-parameters t]
2477        ["Customize group parameters" gnus-summary-customize-parameters t]
2478        ["Send a bug report" gnus-bug t]
2479        ("Exit"
2480         ["Catchup and exit" gnus-summary-catchup-and-exit
2481          ,@(if (featurep 'xemacs) '(t)
2482              '(:help "Mark unread articles in this group as read, then exit"))]
2483         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2484         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2485         ["Exit group" gnus-summary-exit
2486          ,@(if (featurep 'xemacs) '(t)
2487              '(:help "Exit current group, return to group selection mode"))]
2488         ["Exit group without updating" gnus-summary-exit-no-update t]
2489         ["Exit and goto next group" gnus-summary-next-group t]
2490         ["Exit and goto prev group" gnus-summary-prev-group t]
2491         ["Reselect group" gnus-summary-reselect-current-group t]
2492         ["Rescan group" gnus-summary-rescan-group t]
2493         ["Update dribble" gnus-summary-save-newsrc t])))
2494
2495     (gnus-run-hooks 'gnus-summary-menu-hook)))
2496
2497 (defvar gnus-summary-tool-bar-map nil)
2498
2499 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2500 (defun gnus-summary-make-tool-bar ()
2501   (if (and (fboundp 'tool-bar-add-item-from-menu)
2502            (default-value 'tool-bar-mode)
2503            (not gnus-summary-tool-bar-map))
2504       (setq gnus-summary-tool-bar-map
2505             (let ((tool-bar-map (make-sparse-keymap))
2506                   (load-path (mm-image-load-path)))
2507               (tool-bar-add-item-from-menu
2508                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2509               (tool-bar-add-item-from-menu
2510                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2511               (tool-bar-add-item-from-menu
2512                'gnus-summary-post-news "post" gnus-summary-mode-map)
2513               (tool-bar-add-item-from-menu
2514                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2515               (tool-bar-add-item-from-menu
2516                'gnus-summary-followup "followup" gnus-summary-mode-map)
2517               (tool-bar-add-item-from-menu
2518                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2519               (tool-bar-add-item-from-menu
2520                'gnus-summary-reply "reply" gnus-summary-mode-map)
2521               (tool-bar-add-item-from-menu
2522                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2523               (tool-bar-add-item-from-menu
2524                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2525               (tool-bar-add-item-from-menu
2526                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2527               (tool-bar-add-item-from-menu
2528                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2529               (tool-bar-add-item-from-menu
2530                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2531               (tool-bar-add-item-from-menu
2532                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2533               (tool-bar-add-item-from-menu
2534                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2535               (tool-bar-add-item-from-menu
2536                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2537               tool-bar-map)))
2538   (if gnus-summary-tool-bar-map
2539       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2540
2541 (defun gnus-score-set-default (var value)
2542   "A version of set that updates the GNU Emacs menu-bar."
2543   (set var value)
2544   ;; It is the message that forces the active status to be updated.
2545   (message ""))
2546
2547 (defun gnus-make-score-map (type)
2548   "Make a summary score map of type TYPE."
2549   (if t
2550       nil
2551     (let ((headers '(("author" "from" string)
2552                      ("subject" "subject" string)
2553                      ("article body" "body" string)
2554                      ("article head" "head" string)
2555                      ("xref" "xref" string)
2556                      ("extra header" "extra" string)
2557                      ("lines" "lines" number)
2558                      ("followups to author" "followup" string)))
2559           (types '((number ("less than" <)
2560                            ("greater than" >)
2561                            ("equal" =))
2562                    (string ("substring" s)
2563                            ("exact string" e)
2564                            ("fuzzy string" f)
2565                            ("regexp" r))))
2566           (perms '(("temporary" (current-time-string))
2567                    ("permanent" nil)
2568                    ("immediate" now)))
2569           header)
2570       (list
2571        (apply
2572         'nconc
2573         (list
2574          (if (eq type 'lower)
2575              "Lower score"
2576            "Increase score"))
2577         (let (outh)
2578           (while headers
2579             (setq header (car headers))
2580             (setq outh
2581                   (cons
2582                    (apply
2583                     'nconc
2584                     (list (car header))
2585                     (let ((ts (cdr (assoc (nth 2 header) types)))
2586                           outt)
2587                       (while ts
2588                         (setq outt
2589                               (cons
2590                                (apply
2591                                 'nconc
2592                                 (list (caar ts))
2593                                 (let ((ps perms)
2594                                       outp)
2595                                   (while ps
2596                                     (setq outp
2597                                           (cons
2598                                            (vector
2599                                             (caar ps)
2600                                             (list
2601                                              'gnus-summary-score-entry
2602                                              (nth 1 header)
2603                                              (if (or (string= (nth 1 header)
2604                                                               "head")
2605                                                      (string= (nth 1 header)
2606                                                               "body"))
2607                                                  ""
2608                                                (list 'gnus-summary-header
2609                                                      (nth 1 header)))
2610                                              (list 'quote (nth 1 (car ts)))
2611                                              (list 'gnus-score-delta-default
2612                                                    nil)
2613                                              (nth 1 (car ps))
2614                                              t)
2615                                             t)
2616                                            outp))
2617                                     (setq ps (cdr ps)))
2618                                   (list (nreverse outp))))
2619                                outt))
2620                         (setq ts (cdr ts)))
2621                       (list (nreverse outt))))
2622                    outh))
2623             (setq headers (cdr headers)))
2624           (list (nreverse outh))))))))
2625
2626 \f
2627
2628 (defun gnus-summary-mode (&optional group)
2629   "Major mode for reading articles.
2630
2631 All normal editing commands are switched off.
2632 \\<gnus-summary-mode-map>
2633 Each line in this buffer represents one article.  To read an
2634 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2635 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2636 respectively.
2637
2638 You can also post articles and send mail from this buffer.  To
2639 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2640 of an article, type `\\[gnus-summary-reply]'.
2641
2642 There are approx. one gazillion commands you can execute in this
2643 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2644
2645 The following commands are available:
2646
2647 \\{gnus-summary-mode-map}"
2648   (interactive)
2649   (kill-all-local-variables)
2650   (when (gnus-visual-p 'summary-menu 'menu)
2651     (gnus-summary-make-menu-bar)
2652     (gnus-summary-make-tool-bar))
2653   (gnus-summary-make-local-variables)
2654   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2655     (gnus-summary-make-local-variables))
2656   (gnus-make-thread-indent-array)
2657   (gnus-simplify-mode-line)
2658   (setq major-mode 'gnus-summary-mode)
2659   (setq mode-name "Summary")
2660   (make-local-variable 'minor-mode-alist)
2661   (use-local-map gnus-summary-mode-map)
2662   (buffer-disable-undo)
2663   (setq buffer-read-only t)             ;Disable modification
2664   (setq truncate-lines t)
2665   (setq selective-display t)
2666   (setq selective-display-ellipses t)   ;Display `...'
2667   (gnus-summary-set-display-table)
2668   (gnus-set-default-directory)
2669   (setq gnus-newsgroup-name group)
2670   (unless (gnus-news-group-p group)
2671     (setq gnus-newsgroup-incorporated
2672           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2673   (make-local-variable 'gnus-summary-line-format)
2674   (make-local-variable 'gnus-summary-line-format-spec)
2675   (make-local-variable 'gnus-summary-dummy-line-format)
2676   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2677   (make-local-variable 'gnus-summary-mark-positions)
2678   (gnus-make-local-hook 'pre-command-hook)
2679   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2680   (gnus-run-hooks 'gnus-summary-mode-hook)
2681   (turn-on-gnus-mailing-list-mode)
2682   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2683   (gnus-update-summary-mark-positions))
2684
2685 (defun gnus-summary-make-local-variables ()
2686   "Make all the local summary buffer variables."
2687   (let (global)
2688     (dolist (local gnus-summary-local-variables)
2689       (if (consp local)
2690           (progn
2691             (if (eq (cdr local) 'global)
2692                 ;; Copy the global value of the variable.
2693                 (setq global (symbol-value (car local)))
2694               ;; Use the value from the list.
2695               (setq global (eval (cdr local))))
2696             (set (make-local-variable (car local)) global))
2697         ;; Simple nil-valued local variable.
2698         (set (make-local-variable local) nil)))))
2699
2700 (defun gnus-summary-clear-local-variables ()
2701   (let ((locals gnus-summary-local-variables))
2702     (while locals
2703       (if (consp (car locals))
2704           (and (vectorp (caar locals))
2705                (set (caar locals) nil))
2706         (and (vectorp (car locals))
2707              (set (car locals) nil)))
2708       (setq locals (cdr locals)))))
2709
2710 ;; Summary data functions.
2711
2712 (defmacro gnus-data-number (data)
2713   `(car ,data))
2714
2715 (defmacro gnus-data-set-number (data number)
2716   `(setcar ,data ,number))
2717
2718 (defmacro gnus-data-mark (data)
2719   `(nth 1 ,data))
2720
2721 (defmacro gnus-data-set-mark (data mark)
2722   `(setcar (nthcdr 1 ,data) ,mark))
2723
2724 (defmacro gnus-data-pos (data)
2725   `(nth 2 ,data))
2726
2727 (defmacro gnus-data-set-pos (data pos)
2728   `(setcar (nthcdr 2 ,data) ,pos))
2729
2730 (defmacro gnus-data-header (data)
2731   `(nth 3 ,data))
2732
2733 (defmacro gnus-data-set-header (data header)
2734   `(setcar (nthcdr 3 ,data) ,header))
2735
2736 (defmacro gnus-data-level (data)
2737   `(nth 4 ,data))
2738
2739 (defmacro gnus-data-unread-p (data)
2740   `(= (nth 1 ,data) gnus-unread-mark))
2741
2742 (defmacro gnus-data-read-p (data)
2743   `(/= (nth 1 ,data) gnus-unread-mark))
2744
2745 (defmacro gnus-data-pseudo-p (data)
2746   `(consp (nth 3 ,data)))
2747
2748 (defmacro gnus-data-find (number)
2749   `(assq ,number gnus-newsgroup-data))
2750
2751 (defmacro gnus-data-find-list (number &optional data)
2752   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2753      (memq (assq ,number bdata)
2754            bdata)))
2755
2756 (defmacro gnus-data-make (number mark pos header level)
2757   `(list ,number ,mark ,pos ,header ,level))
2758
2759 (defun gnus-data-enter (after-article number mark pos header level offset)
2760   (let ((data (gnus-data-find-list after-article)))
2761     (unless data
2762       (error "No such article: %d" after-article))
2763     (setcdr data (cons (gnus-data-make number mark pos header level)
2764                        (cdr data)))
2765     (setq gnus-newsgroup-data-reverse nil)
2766     (gnus-data-update-list (cddr data) offset)))
2767
2768 (defun gnus-data-enter-list (after-article list &optional offset)
2769   (when list
2770     (let ((data (and after-article (gnus-data-find-list after-article)))
2771           (ilist list))
2772       (if (not (or data
2773                    after-article))
2774           (let ((odata gnus-newsgroup-data))
2775             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2776             (when offset
2777               (gnus-data-update-list odata offset)))
2778         ;; Find the last element in the list to be spliced into the main
2779         ;; list.
2780         (while (cdr list)
2781           (setq list (cdr list)))
2782         (if (not data)
2783             (progn
2784               (setcdr list gnus-newsgroup-data)
2785               (setq gnus-newsgroup-data ilist)
2786               (when offset
2787                 (gnus-data-update-list (cdr list) offset)))
2788           (setcdr list (cdr data))
2789           (setcdr data ilist)
2790           (when offset
2791             (gnus-data-update-list (cdr list) offset))))
2792       (setq gnus-newsgroup-data-reverse nil))))
2793
2794 (defun gnus-data-remove (article &optional offset)
2795   (let ((data gnus-newsgroup-data))
2796     (if (= (gnus-data-number (car data)) article)
2797         (progn
2798           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2799                 gnus-newsgroup-data-reverse nil)
2800           (when offset
2801             (gnus-data-update-list gnus-newsgroup-data offset)))
2802       (while (cdr data)
2803         (when (= (gnus-data-number (cadr data)) article)
2804           (setcdr data (cddr data))
2805           (when offset
2806             (gnus-data-update-list (cdr data) offset))
2807           (setq data nil
2808                 gnus-newsgroup-data-reverse nil))
2809         (setq data (cdr data))))))
2810
2811 (defmacro gnus-data-list (backward)
2812   `(if ,backward
2813        (or gnus-newsgroup-data-reverse
2814            (setq gnus-newsgroup-data-reverse
2815                  (reverse gnus-newsgroup-data)))
2816      gnus-newsgroup-data))
2817
2818 (defun gnus-data-update-list (data offset)
2819   "Add OFFSET to the POS of all data entries in DATA."
2820   (setq gnus-newsgroup-data-reverse nil)
2821   (while data
2822     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2823     (setq data (cdr data))))
2824
2825 (defun gnus-summary-article-pseudo-p (article)
2826   "Say whether this article is a pseudo article or not."
2827   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2828
2829 (defmacro gnus-summary-article-sparse-p (article)
2830   "Say whether this article is a sparse article or not."
2831   `(memq ,article gnus-newsgroup-sparse))
2832
2833 (defmacro gnus-summary-article-ancient-p (article)
2834   "Say whether this article is a sparse article or not."
2835   `(memq ,article gnus-newsgroup-ancient))
2836
2837 (defun gnus-article-parent-p (number)
2838   "Say whether this article is a parent or not."
2839   (let ((data (gnus-data-find-list number)))
2840     (and (cdr data)                     ; There has to be an article after...
2841          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2842             (gnus-data-level (nth 1 data))))))
2843
2844 (defun gnus-article-children (number)
2845   "Return a list of all children to NUMBER."
2846   (let* ((data (gnus-data-find-list number))
2847          (level (gnus-data-level (car data)))
2848          children)
2849     (setq data (cdr data))
2850     (while (and data
2851                 (= (gnus-data-level (car data)) (1+ level)))
2852       (push (gnus-data-number (car data)) children)
2853       (setq data (cdr data)))
2854     children))
2855
2856 (defmacro gnus-summary-skip-intangible ()
2857   "If the current article is intangible, then jump to a different article."
2858   '(let ((to (get-text-property (point) 'gnus-intangible)))
2859      (and to (gnus-summary-goto-subject to))))
2860
2861 (defmacro gnus-summary-article-intangible-p ()
2862   "Say whether this article is intangible or not."
2863   '(get-text-property (point) 'gnus-intangible))
2864
2865 (defun gnus-article-read-p (article)
2866   "Say whether ARTICLE is read or not."
2867   (not (or (memq article gnus-newsgroup-marked)
2868            (memq article gnus-newsgroup-spam-marked)
2869            (memq article gnus-newsgroup-unreads)
2870            (memq article gnus-newsgroup-unselected)
2871            (memq article gnus-newsgroup-dormant))))
2872
2873 ;; Some summary mode macros.
2874
2875 (defmacro gnus-summary-article-number ()
2876   "The article number of the article on the current line.
2877 If there isn't an article number here, then we return the current
2878 article number."
2879   '(progn
2880      (gnus-summary-skip-intangible)
2881      (or (get-text-property (point) 'gnus-number)
2882          (gnus-summary-last-subject))))
2883
2884 (defmacro gnus-summary-article-header (&optional number)
2885   "Return the header of article NUMBER."
2886   `(gnus-data-header (gnus-data-find
2887                       ,(or number '(gnus-summary-article-number)))))
2888
2889 (defmacro gnus-summary-thread-level (&optional number)
2890   "Return the level of thread that starts with article NUMBER."
2891   `(if (and (eq gnus-summary-make-false-root 'dummy)
2892             (get-text-property (point) 'gnus-intangible))
2893        0
2894      (gnus-data-level (gnus-data-find
2895                        ,(or number '(gnus-summary-article-number))))))
2896
2897 (defmacro gnus-summary-article-mark (&optional number)
2898   "Return the mark of article NUMBER."
2899   `(gnus-data-mark (gnus-data-find
2900                     ,(or number '(gnus-summary-article-number)))))
2901
2902 (defmacro gnus-summary-article-pos (&optional number)
2903   "Return the position of the line of article NUMBER."
2904   `(gnus-data-pos (gnus-data-find
2905                    ,(or number '(gnus-summary-article-number)))))
2906
2907 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2908 (defmacro gnus-summary-article-subject (&optional number)
2909   "Return current subject string or nil if nothing."
2910   `(let ((headers
2911           ,(if number
2912                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2913              '(gnus-data-header (assq (gnus-summary-article-number)
2914                                       gnus-newsgroup-data)))))
2915      (and headers
2916           (vectorp headers)
2917           (mail-header-subject headers))))
2918
2919 (defmacro gnus-summary-article-score (&optional number)
2920   "Return current article score."
2921   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2922                   gnus-newsgroup-scored))
2923        gnus-summary-default-score 0))
2924
2925 (defun gnus-summary-article-children (&optional number)
2926   "Return a list of article numbers that are children of article NUMBER."
2927   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2928          (level (gnus-data-level (car data)))
2929          l children)
2930     (while (and (setq data (cdr data))
2931                 (> (setq l (gnus-data-level (car data))) level))
2932       (and (= (1+ level) l)
2933            (push (gnus-data-number (car data))
2934                  children)))
2935     (nreverse children)))
2936
2937 (defun gnus-summary-article-parent (&optional number)
2938   "Return the article number of the parent of article NUMBER."
2939   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2940                                     (gnus-data-list t)))
2941          (level (gnus-data-level (car data))))
2942     (if (zerop level)
2943         ()                              ; This is a root.
2944       ;; We search until we find an article with a level less than
2945       ;; this one.  That function has to be the parent.
2946       (while (and (setq data (cdr data))
2947                   (not (< (gnus-data-level (car data)) level))))
2948       (and data (gnus-data-number (car data))))))
2949
2950 (defun gnus-unread-mark-p (mark)
2951   "Say whether MARK is the unread mark."
2952   (= mark gnus-unread-mark))
2953
2954 (defun gnus-read-mark-p (mark)
2955   "Say whether MARK is one of the marks that mark as read.
2956 This is all marks except unread, ticked, dormant, and expirable."
2957   (not (or (= mark gnus-unread-mark)
2958            (= mark gnus-ticked-mark)
2959            (= mark gnus-spam-mark)
2960            (= mark gnus-dormant-mark)
2961            (= mark gnus-expirable-mark))))
2962
2963 (defmacro gnus-article-mark (number)
2964   "Return the MARK of article NUMBER.
2965 This macro should only be used when computing the mark the \"first\"
2966 time; i.e., when generating the summary lines.  After that,
2967 `gnus-summary-article-mark' should be used to examine the
2968 marks of articles."
2969   `(cond
2970     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2971     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2972     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2973     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2974     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2975     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2976     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2977     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2978            gnus-ancient-mark))))
2979
2980 ;; Saving hidden threads.
2981
2982 (defmacro gnus-save-hidden-threads (&rest forms)
2983   "Save hidden threads, eval FORMS, and restore the hidden threads."
2984   (let ((config (make-symbol "config")))
2985     `(let ((,config (gnus-hidden-threads-configuration)))
2986        (unwind-protect
2987            (save-excursion
2988              ,@forms)
2989          (gnus-restore-hidden-threads-configuration ,config)))))
2990 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2991 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2992
2993 (defun gnus-data-compute-positions ()
2994   "Compute the positions of all articles."
2995   (setq gnus-newsgroup-data-reverse nil)
2996   (let ((data gnus-newsgroup-data))
2997     (save-excursion
2998       (gnus-save-hidden-threads
2999         (gnus-summary-show-all-threads)
3000         (goto-char (point-min))
3001         (while data
3002           (while (get-text-property (point) 'gnus-intangible)
3003             (forward-line 1))
3004           (gnus-data-set-pos (car data) (+ (point) 3))
3005           (setq data (cdr data))
3006           (forward-line 1))))))
3007
3008 (defun gnus-hidden-threads-configuration ()
3009   "Return the current hidden threads configuration."
3010   (save-excursion
3011     (let (config)
3012       (goto-char (point-min))
3013       (while (search-forward "\r" nil t)
3014         (push (1- (point)) config))
3015       config)))
3016
3017 (defun gnus-restore-hidden-threads-configuration (config)
3018   "Restore hidden threads configuration from CONFIG."
3019   (save-excursion
3020     (let (point buffer-read-only)
3021       (while (setq point (pop config))
3022         (when (and (< point (point-max))
3023                    (goto-char point)
3024                    (eq (char-after) ?\n))
3025           (subst-char-in-region point (1+ point) ?\n ?\r))))))
3026
3027 ;; Various summary mode internalish functions.
3028
3029 (defun gnus-mouse-pick-article (e)
3030   (interactive "e")
3031   (mouse-set-point e)
3032   (gnus-summary-next-page nil t))
3033
3034 (defun gnus-summary-set-display-table ()
3035   "Change the display table.
3036 Odd characters have a tendency to mess
3037 up nicely formatted displays - we make all possible glyphs
3038 display only a single character."
3039
3040   ;; We start from the standard display table, if any.
3041   (let ((table (or (copy-sequence standard-display-table)
3042                    (make-display-table)))
3043         (i 32))
3044     ;; Nix out all the control chars...
3045     (while (>= (setq i (1- i)) 0)
3046       (aset table i [??]))
3047     ;; ... but not newline and cr, of course.  (cr is necessary for the
3048     ;; selective display).
3049     (aset table ?\n nil)
3050     (aset table ?\r nil)
3051     ;; We keep TAB as well.
3052     (aset table ?\t nil)
3053     ;; We nix out any glyphs over 126 that are not set already.
3054     (let ((i 256))
3055       (while (>= (setq i (1- i)) 127)
3056         ;; Only modify if the entry is nil.
3057         (unless (aref table i)
3058           (aset table i [??]))))
3059     (setq buffer-display-table table)))
3060
3061 (defun gnus-summary-set-article-display-arrow (pos)
3062   "Update the overlay arrow to point to line at position POS."
3063   (when (and gnus-summary-display-arrow
3064              (boundp 'overlay-arrow-position)
3065              (boundp 'overlay-arrow-string))
3066     (save-excursion
3067       (goto-char pos)
3068       (beginning-of-line)
3069       (unless overlay-arrow-position
3070         (setq overlay-arrow-position (make-marker)))
3071       (setq overlay-arrow-string "=>"
3072             overlay-arrow-position (set-marker overlay-arrow-position
3073                                                (point)
3074                                                (current-buffer))))))
3075
3076 (defun gnus-summary-setup-buffer (group)
3077   "Initialize summary buffer."
3078   (let ((buffer (gnus-summary-buffer-name group))
3079         (dead-name (concat "*Dead Summary "
3080                            (gnus-group-decoded-name group) "*")))
3081     ;; If a dead summary buffer exists, we kill it.
3082     (when (gnus-buffer-live-p dead-name)
3083       (gnus-kill-buffer dead-name))
3084     (if (get-buffer buffer)
3085         (progn
3086           (set-buffer buffer)
3087           (setq gnus-summary-buffer (current-buffer))
3088           (not gnus-newsgroup-prepared))
3089       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
3090       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
3091       (gnus-summary-mode group)
3092       (when gnus-carpal
3093         (gnus-carpal-setup-buffer 'summary))
3094       (unless gnus-single-article-buffer
3095         (make-local-variable 'gnus-article-buffer)
3096         (make-local-variable 'gnus-article-current)
3097         (make-local-variable 'gnus-original-article-buffer))
3098       (setq gnus-newsgroup-name group)
3099       ;; Set any local variables in the group parameters.
3100       (gnus-summary-set-local-parameters gnus-newsgroup-name)
3101       t)))
3102
3103 (defun gnus-set-global-variables ()
3104   "Set the global equivalents of the buffer-local variables.
3105 They are set to the latest values they had.  These reflect the summary
3106 buffer that was in action when the last article was fetched."
3107   (when (eq major-mode 'gnus-summary-mode)
3108     (setq gnus-summary-buffer (current-buffer))
3109     (let ((name gnus-newsgroup-name)
3110           (marked gnus-newsgroup-marked)
3111           (spam gnus-newsgroup-spam-marked)
3112           (unread gnus-newsgroup-unreads)
3113           (headers gnus-current-headers)
3114           (data gnus-newsgroup-data)
3115           (summary gnus-summary-buffer)
3116           (article-buffer gnus-article-buffer)
3117           (original gnus-original-article-buffer)
3118           (gac gnus-article-current)
3119           (reffed gnus-reffed-article-number)
3120           (score-file gnus-current-score-file)
3121           (default-charset gnus-newsgroup-charset)
3122           vlist)
3123       (let ((locals gnus-newsgroup-variables))
3124         (while locals
3125           (if (consp (car locals))
3126               (push (eval (caar locals)) vlist)
3127             (push (eval (car locals)) vlist))
3128           (setq locals (cdr locals)))
3129         (setq vlist (nreverse vlist)))
3130       (save-excursion
3131         (set-buffer gnus-group-buffer)
3132         (setq gnus-newsgroup-name name
3133               gnus-newsgroup-marked marked
3134               gnus-newsgroup-spam-marked spam
3135               gnus-newsgroup-unreads unread
3136               gnus-current-headers headers
3137               gnus-newsgroup-data data
3138               gnus-article-current gac
3139               gnus-summary-buffer summary
3140               gnus-article-buffer article-buffer
3141               gnus-original-article-buffer original
3142               gnus-reffed-article-number reffed
3143               gnus-current-score-file score-file
3144               gnus-newsgroup-charset default-charset)
3145         (let ((locals gnus-newsgroup-variables))
3146           (while locals
3147             (if (consp (car locals))
3148                 (set (caar locals) (pop vlist))
3149               (set (car locals) (pop vlist)))
3150             (setq locals (cdr locals))))
3151         ;; The article buffer also has local variables.
3152         (when (gnus-buffer-live-p gnus-article-buffer)
3153           (set-buffer gnus-article-buffer)
3154           (setq gnus-summary-buffer summary))))))
3155
3156 (defun gnus-summary-article-unread-p (article)
3157   "Say whether ARTICLE is unread or not."
3158   (memq article gnus-newsgroup-unreads))
3159
3160 (defun gnus-summary-first-article-p (&optional article)
3161   "Return whether ARTICLE is the first article in the buffer."
3162   (if (not (setq article (or article (gnus-summary-article-number))))
3163       nil
3164     (eq article (caar gnus-newsgroup-data))))
3165
3166 (defun gnus-summary-last-article-p (&optional article)
3167   "Return whether ARTICLE is the last article in the buffer."
3168   (if (not (setq article (or article (gnus-summary-article-number))))
3169       ;; All non-existent numbers are the last article.  :-)
3170       t
3171     (not (cdr (gnus-data-find-list article)))))
3172
3173 (defun gnus-make-thread-indent-array ()
3174   (let ((n 200))
3175     (unless (and gnus-thread-indent-array
3176                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3177       (setq gnus-thread-indent-array (make-vector 201 "")
3178             gnus-thread-indent-array-level gnus-thread-indent-level)
3179       (while (>= n 0)
3180         (aset gnus-thread-indent-array n
3181               (make-string (* n gnus-thread-indent-level) ? ))
3182         (setq n (1- n))))))
3183
3184 (defun gnus-update-summary-mark-positions ()
3185   "Compute where the summary marks are to go."
3186   (save-excursion
3187     (when (gnus-buffer-exists-p gnus-summary-buffer)
3188       (set-buffer gnus-summary-buffer))
3189     (let ((gnus-replied-mark 129)
3190           (gnus-score-below-mark 130)
3191           (gnus-score-over-mark 130)
3192           (gnus-undownloaded-mark 131)
3193           (spec gnus-summary-line-format-spec)
3194           gnus-visual pos)
3195       (save-excursion
3196         (gnus-set-work-buffer)
3197         (let ((gnus-summary-line-format-spec spec)
3198               (gnus-newsgroup-downloadable '(0)))
3199           (gnus-summary-insert-line
3200            (make-full-mail-header 0 "" "nobody"
3201                                   "05 Apr 2001 23:33:09 +0400"
3202                                   "" "" 0 0 "" nil)
3203            0 nil t 128 t nil "" nil 1)
3204           (goto-char (point-min))
3205           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3206                                              (- (point) (point-min) 1)))))
3207           (goto-char (point-min))
3208           (push (cons 'replied (and (search-forward "\201" nil t)
3209                                     (- (point) (point-min) 1)))
3210                 pos)
3211           (goto-char (point-min))
3212           (push (cons 'score (and (search-forward "\202" nil t)
3213                                   (- (point) (point-min) 1)))
3214                 pos)
3215           (goto-char (point-min))
3216           (push (cons 'download
3217                       (and (search-forward "\203" nil t)
3218                            (- (point) (point-min) 1)))
3219                 pos)))
3220       (setq gnus-summary-mark-positions pos))))
3221
3222 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3223   "Insert a dummy root in the summary buffer."
3224   (beginning-of-line)
3225   (gnus-add-text-properties
3226    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3227    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3228
3229 (defun gnus-summary-extract-address-component (from)
3230   (or (car (funcall gnus-extract-address-components from))
3231       from))
3232
3233 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3234   (let ((default-mime-charset (with-current-buffer gnus-summary-buffer
3235                                 default-mime-charset)))
3236     ;; Is it really necessary to do this next part for each summary line?
3237     ;; Luckily, doesn't seem to slow things down much.
3238     (or
3239      (and gnus-ignored-from-addresses
3240           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3241           (let ((extra-headers (mail-header-extra header))
3242                 to
3243                 newsgroups)
3244             (cond
3245              ((setq to (cdr (assq 'To extra-headers)))
3246               (concat "-> "
3247                       (inline
3248                         (gnus-summary-extract-address-component
3249                          (funcall gnus-decode-encoded-word-function to)))))
3250              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3251               (concat "=> " newsgroups)))))
3252      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3253
3254 (defun gnus-summary-insert-line (gnus-tmp-header
3255                                  gnus-tmp-level gnus-tmp-current
3256                                  undownloaded gnus-tmp-unread gnus-tmp-replied
3257                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3258                                  &optional gnus-tmp-dummy gnus-tmp-score
3259                                  gnus-tmp-process)
3260   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3261          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3262          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3263          (gnus-tmp-score-char
3264           (if (or (null gnus-summary-default-score)
3265                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3266                       gnus-summary-zcore-fuzz))
3267               ?\ ;;;Whitespace
3268             (if (< gnus-tmp-score gnus-summary-default-score)
3269                 gnus-score-below-mark gnus-score-over-mark)))
3270          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3271          (gnus-tmp-replied
3272           (cond (gnus-tmp-process gnus-process-mark)
3273                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3274                  gnus-cached-mark)
3275                 (gnus-tmp-replied gnus-replied-mark)
3276                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3277                  gnus-forwarded-mark)
3278                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3279                  gnus-saved-mark)
3280                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3281                  gnus-recent-mark)
3282                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3283                  gnus-unseen-mark)
3284                 (t gnus-no-mark)))
3285          (gnus-tmp-downloaded
3286           (cond (undownloaded
3287                  gnus-undownloaded-mark)
3288                 (gnus-newsgroup-agentized
3289                  gnus-downloaded-mark)
3290                 (t
3291                  gnus-no-mark)))
3292          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3293          (gnus-tmp-name
3294           (cond
3295            ((string-match "<[^>]+> *$" gnus-tmp-from)
3296             (let ((beg (match-beginning 0)))
3297               (or (and (string-match "^\".+\"" gnus-tmp-from)
3298                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3299                   (substring gnus-tmp-from 0 beg))))
3300            ((string-match "(.+)" gnus-tmp-from)
3301             (substring gnus-tmp-from
3302                        (1+ (match-beginning 0)) (1- (match-end 0))))
3303            (t gnus-tmp-from)))
3304          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3305          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3306          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3307          (buffer-read-only nil))
3308     (when (string= gnus-tmp-name "")
3309       (setq gnus-tmp-name gnus-tmp-from))
3310     (unless (numberp gnus-tmp-lines)
3311       (setq gnus-tmp-lines -1))
3312     (if (= gnus-tmp-lines -1)
3313         (setq gnus-tmp-lines "?")
3314       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3315     (gnus-put-text-property-excluding-characters-with-faces
3316      (point)
3317      (progn (eval gnus-summary-line-format-spec) (point))
3318      'gnus-number gnus-tmp-number)
3319     (when (gnus-visual-p 'summary-highlight 'highlight)
3320       (forward-line -1)
3321       (gnus-run-hooks 'gnus-summary-update-hook)
3322       (forward-line 1))))
3323
3324 (defun gnus-summary-update-line (&optional dont-update)
3325   "Update summary line after change."
3326   (when (and gnus-summary-default-score
3327              (not gnus-summary-inhibit-highlight))
3328     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3329            (article (gnus-summary-article-number))
3330            (score (gnus-summary-article-score article)))
3331       (unless dont-update
3332         (if (and gnus-summary-mark-below
3333                  (< (gnus-summary-article-score)
3334                     gnus-summary-mark-below))
3335             ;; This article has a low score, so we mark it as read.
3336             (when (memq article gnus-newsgroup-unreads)
3337               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3338           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3339             ;; This article was previously marked as read on account
3340             ;; of a low score, but now it has risen, so we mark it as
3341             ;; unread.
3342             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3343         (gnus-summary-update-mark
3344          (if (or (null gnus-summary-default-score)
3345                  (<= (abs (- score gnus-summary-default-score))
3346                      gnus-summary-zcore-fuzz))
3347              ?\ ;;;Whitespace
3348            (if (< score gnus-summary-default-score)
3349                gnus-score-below-mark gnus-score-over-mark))
3350          'score))
3351       ;; Do visual highlighting.
3352       (when (gnus-visual-p 'summary-highlight 'highlight)
3353         (gnus-run-hooks 'gnus-summary-update-hook)))))
3354
3355 (defvar gnus-tmp-new-adopts nil)
3356
3357 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3358   "Return the number of articles in THREAD.
3359 This may be 0 in some cases -- if none of the articles in
3360 the thread are to be displayed."
3361   (let* ((number
3362           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3363           (cond
3364            ((not (listp thread))
3365             1)
3366            ((and (consp thread) (cdr thread))
3367             (apply
3368              '+ 1 (mapcar
3369                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3370            ((null thread)
3371             1)
3372            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3373             1)
3374            (t 0))))
3375     (when (and level (zerop level) gnus-tmp-new-adopts)
3376       (incf number
3377             (apply '+ (mapcar
3378                        'gnus-summary-number-of-articles-in-thread
3379                        gnus-tmp-new-adopts))))
3380     (if char
3381         (if (> number 1) gnus-not-empty-thread-mark
3382           gnus-empty-thread-mark)
3383       number)))
3384
3385 (defsubst gnus-summary-line-message-size (head)
3386   "Return pretty-printed version of message size.
3387 This function is intended to be used in
3388 `gnus-summary-line-format-alist'."
3389   (let ((c (or (mail-header-chars head) -1)))
3390     (cond ((< c 0) "n/a")               ; chars not available
3391           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3392           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3393           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3394           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3395
3396
3397 (defun gnus-summary-set-local-parameters (group)
3398   "Go through the local params of GROUP and set all variable specs in that list."
3399   (let ((params (gnus-group-find-parameter group))
3400         (vars '(quit-config))           ; Ignore quit-config.
3401         elem)
3402     (while params
3403       (setq elem (car params)
3404             params (cdr params))
3405       (and (consp elem)                 ; Has to be a cons.
3406            (consp (cdr elem))           ; The cdr has to be a list.
3407            (symbolp (car elem))         ; Has to be a symbol in there.
3408            (not (memq (car elem) vars))
3409            (ignore-errors               ; So we set it.
3410              (push (car elem) vars)
3411              (make-local-variable (car elem))
3412              (set (car elem) (eval (nth 1 elem))))))))
3413
3414 (defun gnus-summary-read-group (group &optional show-all no-article
3415                                       kill-buffer no-display backward
3416                                       select-articles)
3417   "Start reading news in newsgroup GROUP.
3418 If SHOW-ALL is non-nil, already read articles are also listed.
3419 If NO-ARTICLE is non-nil, no article is selected initially.
3420 If NO-DISPLAY, don't generate a summary buffer."
3421   (let (result)
3422     (while (and group
3423                 (null (setq result
3424                             (let ((gnus-auto-select-next nil))
3425                               (or (gnus-summary-read-group-1
3426                                    group show-all no-article
3427                                    kill-buffer no-display
3428                                    select-articles)
3429                                   (setq show-all nil
3430                                         select-articles nil)))))
3431                 (eq gnus-auto-select-next 'quietly))
3432       (set-buffer gnus-group-buffer)
3433       ;; The entry function called above goes to the next
3434       ;; group automatically, so we go two groups back
3435       ;; if we are searching for the previous group.
3436       (when backward
3437         (gnus-group-prev-unread-group 2))
3438       (if (not (equal group (gnus-group-group-name)))
3439           (setq group (gnus-group-group-name))
3440         (setq group nil)))
3441     result))
3442
3443 (defun gnus-summary-jump-to-other-group (group &optional show-all)
3444   "Directly jump to the other GROUP from summary buffer.
3445 If SHOW-ALL is non-nil, already read articles are also listed."
3446   (interactive
3447    (if (eq gnus-summary-buffer (current-buffer))
3448        (list (completing-read
3449               "Group: " gnus-active-hashtb nil t
3450               (when (and gnus-newsgroup-name
3451                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
3452                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
3453               'gnus-group-history)
3454              current-prefix-arg)
3455      (error "%s must be invoked from a gnus summary buffer." this-command)))
3456   (unless (or (zerop (length group))
3457               (and gnus-newsgroup-name
3458                    (string-equal gnus-newsgroup-name group)))
3459     (gnus-summary-exit)
3460     (gnus-summary-read-group group show-all
3461                              gnus-dont-select-after-jump-to-other-group)))
3462
3463 (defun gnus-summary-read-group-1 (group show-all no-article
3464                                         kill-buffer no-display
3465                                         &optional select-articles)
3466   ;; Killed foreign groups can't be entered.
3467   ;;  (when (and (not (gnus-group-native-p group))
3468   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3469   ;;    (error "Dead non-native groups can't be entered"))
3470   (gnus-message 5 "Retrieving newsgroup: %s..."
3471                 (gnus-group-decoded-name group))
3472   (let* ((new-group (gnus-summary-setup-buffer group))
3473          (quit-config (gnus-group-quit-config group))
3474          (did-select (and new-group (gnus-select-newsgroup
3475                                      group show-all select-articles))))
3476     (cond
3477      ;; This summary buffer exists already, so we just select it.
3478      ((not new-group)
3479       (gnus-set-global-variables)
3480       (when kill-buffer
3481         (gnus-kill-or-deaden-summary kill-buffer))
3482       (gnus-configure-windows 'summary 'force)
3483       (gnus-set-mode-line 'summary)
3484       (gnus-summary-position-point)
3485       (message "")
3486       t)
3487      ;; We couldn't select this group.
3488      ((null did-select)
3489       (when (and (eq major-mode 'gnus-summary-mode)
3490                  (not (equal (current-buffer) kill-buffer)))
3491         (kill-buffer (current-buffer))
3492         (if (not quit-config)
3493             (progn
3494               ;; Update the info -- marks might need to be removed,
3495               ;; for instance.
3496               (gnus-summary-update-info)
3497               (set-buffer gnus-group-buffer)
3498               (gnus-group-jump-to-group group)
3499               (gnus-group-next-unread-group 1))
3500           (gnus-handle-ephemeral-exit quit-config)))
3501       (let ((grpinfo (gnus-get-info group)))
3502         (if (null (gnus-info-read grpinfo))
3503             (gnus-message 3 "Group %s contains no messages"
3504                           (gnus-group-decoded-name group))
3505           (gnus-message 3 "Can't select group")))
3506       nil)
3507      ;; The user did a `C-g' while prompting for number of articles,
3508      ;; so we exit this group.
3509      ((eq did-select 'quit)
3510       (and (eq major-mode 'gnus-summary-mode)
3511            (not (equal (current-buffer) kill-buffer))
3512            (kill-buffer (current-buffer)))
3513       (when kill-buffer
3514         (gnus-kill-or-deaden-summary kill-buffer))
3515       (if (not quit-config)
3516           (progn
3517             (set-buffer gnus-group-buffer)
3518             (gnus-group-jump-to-group group)
3519             (gnus-group-next-unread-group 1)
3520             (gnus-configure-windows 'group 'force))
3521         (gnus-handle-ephemeral-exit quit-config))
3522       ;; Finally signal the quit.
3523       (signal 'quit nil))
3524      ;; The group was successfully selected.
3525      (t
3526       (gnus-set-global-variables)
3527       ;; Save the active value in effect when the group was entered.
3528       (setq gnus-newsgroup-active
3529             (gnus-copy-sequence
3530              (gnus-active gnus-newsgroup-name)))
3531       ;; You can change the summary buffer in some way with this hook.
3532       (gnus-run-hooks 'gnus-select-group-hook)
3533       (gnus-update-format-specifications
3534        nil 'summary 'summary-mode 'summary-dummy)
3535       (gnus-update-summary-mark-positions)
3536       ;; Do score processing.
3537       (when gnus-use-scoring
3538         (gnus-possibly-score-headers))
3539       ;; Check whether to fill in the gaps in the threads.
3540       (when gnus-build-sparse-threads
3541         (gnus-build-sparse-threads))
3542       ;; Find the initial limit.
3543       (if gnus-show-threads
3544           (if show-all
3545               (let ((gnus-newsgroup-dormant nil))
3546                 (gnus-summary-initial-limit show-all))
3547             (gnus-summary-initial-limit show-all))
3548         ;; When unthreaded, all articles are always shown.
3549         (setq gnus-newsgroup-limit
3550               (mapcar
3551                (lambda (header) (mail-header-number header))
3552                gnus-newsgroup-headers)))
3553       ;; Generate the summary buffer.
3554       (unless no-display
3555         (gnus-summary-prepare))
3556       (when gnus-use-trees
3557         (gnus-tree-open group)
3558         (setq gnus-summary-highlight-line-function
3559               'gnus-tree-highlight-article))
3560       ;; If the summary buffer is empty, but there are some low-scored
3561       ;; articles or some excluded dormants, we include these in the
3562       ;; buffer.
3563       (when (and (zerop (buffer-size))
3564                  (not no-display))
3565         (cond (gnus-newsgroup-dormant
3566                (gnus-summary-limit-include-dormant))
3567               ((and gnus-newsgroup-scored show-all)
3568                (gnus-summary-limit-include-expunged t))))
3569       ;; Function `gnus-apply-kill-file' must be called in this hook.
3570       (gnus-run-hooks 'gnus-apply-kill-hook)
3571       (if (and (zerop (buffer-size))
3572                (not no-display))
3573           (progn
3574             ;; This newsgroup is empty.
3575             (gnus-summary-catchup-and-exit nil t)
3576             (gnus-message 6 "No unread news")
3577             (when kill-buffer
3578               (gnus-kill-or-deaden-summary kill-buffer))
3579             ;; Return nil from this function.
3580             nil)
3581         ;; Hide conversation thread subtrees.  We cannot do this in
3582         ;; gnus-summary-prepare-hook since kill processing may not
3583         ;; work with hidden articles.
3584         (gnus-summary-maybe-hide-threads)
3585         (when kill-buffer
3586           (gnus-kill-or-deaden-summary kill-buffer))
3587         (gnus-summary-auto-select-subject)
3588         ;; Show first unread article if requested.
3589         (if (and (not no-article)
3590                  (not no-display)
3591                  gnus-newsgroup-unreads
3592                  gnus-auto-select-first)
3593             (progn
3594               (gnus-configure-windows 'summary)
3595               (let ((art (gnus-summary-article-number)))
3596                 (unless (and (not gnus-plugged)
3597                              (or (memq art gnus-newsgroup-undownloaded)
3598                                  (memq art gnus-newsgroup-downloadable)))
3599                   (gnus-summary-goto-article art))))
3600           ;; Don't select any articles.
3601           (gnus-summary-position-point)
3602           (gnus-configure-windows 'summary 'force)
3603           (gnus-set-mode-line 'summary))
3604         (when (and gnus-auto-center-group
3605                    (get-buffer-window gnus-group-buffer t))
3606           ;; Gotta use windows, because recenter does weird stuff if
3607           ;; the current buffer ain't the displayed window.
3608           (let ((owin (selected-window)))
3609             (select-window (get-buffer-window gnus-group-buffer t))
3610             (when (gnus-group-goto-group group)
3611               (recenter))
3612             (select-window owin)))
3613         ;; Mark this buffer as "prepared".
3614         (setq gnus-newsgroup-prepared t)
3615         (gnus-run-hooks 'gnus-summary-prepared-hook)
3616         (unless (gnus-ephemeral-group-p group)
3617           (gnus-group-update-group group))
3618         t)))))
3619
3620 (defun gnus-summary-auto-select-subject ()
3621   "Select the subject line on initial group entry."
3622   (goto-char (point-min))
3623   (cond
3624    ((eq gnus-auto-select-subject 'best)
3625     (gnus-summary-best-unread-subject))
3626    ((eq gnus-auto-select-subject 'unread)
3627     (gnus-summary-first-unread-subject))
3628    ((eq gnus-auto-select-subject 'unseen)
3629     (gnus-summary-first-unseen-subject))
3630    ((eq gnus-auto-select-subject 'unseen-or-unread)
3631     (gnus-summary-first-unseen-or-unread-subject))
3632    ((eq gnus-auto-select-subject 'first)
3633     ;; Do nothing.
3634     )
3635    ((functionp gnus-auto-select-subject)
3636     (funcall gnus-auto-select-subject))))
3637
3638 (defun gnus-summary-prepare ()
3639   "Generate the summary buffer."
3640   (interactive)
3641   (let ((buffer-read-only nil))
3642     (erase-buffer)
3643     (setq gnus-newsgroup-data nil
3644           gnus-newsgroup-data-reverse nil)
3645     (gnus-run-hooks 'gnus-summary-generate-hook)
3646     ;; Generate the buffer, either with threads or without.
3647     (when gnus-newsgroup-headers
3648       (gnus-summary-prepare-threads
3649        (if gnus-show-threads
3650            (gnus-sort-gathered-threads
3651             (funcall gnus-summary-thread-gathering-function
3652                      (gnus-sort-threads
3653                       (gnus-cut-threads (gnus-make-threads)))))
3654          ;; Unthreaded display.
3655          (gnus-sort-articles gnus-newsgroup-headers))))
3656     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3657     ;; Call hooks for modifying summary buffer.
3658     (goto-char (point-min))
3659     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3660
3661 (defsubst gnus-general-simplify-subject (subject)
3662   "Simplify subject by the same rules as `gnus-gather-threads-by-subject'."
3663   (setq subject
3664         (cond
3665          ;; Truncate the subject.
3666          (gnus-simplify-subject-functions
3667           (gnus-map-function gnus-simplify-subject-functions subject))
3668          ((numberp gnus-summary-gather-subject-limit)
3669           (setq subject (gnus-simplify-subject-re subject))
3670           (if (> (length subject) gnus-summary-gather-subject-limit)
3671               (substring subject 0 gnus-summary-gather-subject-limit)
3672             subject))
3673          ;; Fuzzily simplify it.
3674          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3675           (gnus-simplify-subject-fuzzy subject))
3676          ;; Just remove the leading "Re:".
3677          (t
3678           (gnus-simplify-subject-re subject))))
3679
3680   (if (and gnus-summary-gather-exclude-subject
3681            (string-match gnus-summary-gather-exclude-subject subject))
3682       nil                               ; This article shouldn't be gathered
3683     subject))
3684
3685 (defun gnus-summary-simplify-subject-query ()
3686   "Query where the respool algorithm would put this article."
3687   (interactive)
3688   (gnus-summary-select-article)
3689   (message "%s"
3690            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3691
3692 (defun gnus-gather-threads-by-subject (threads)
3693   "Gather threads by looking at Subject headers."
3694   (if (not gnus-summary-make-false-root)
3695       threads
3696     (let ((hashtb (gnus-make-hashtable 1024))
3697           (prev threads)
3698           (result threads)
3699           subject hthread whole-subject)
3700       (while threads
3701         (setq subject (gnus-general-simplify-subject
3702                        (setq whole-subject (mail-header-subject
3703                                             (caar threads)))))
3704         (when subject
3705           (if (setq hthread (gnus-gethash subject hashtb))
3706               (progn
3707                 ;; We enter a dummy root into the thread, if we
3708                 ;; haven't done that already.
3709                 (unless (stringp (caar hthread))
3710                   (setcar hthread (list whole-subject (car hthread))))
3711                 ;; We add this new gathered thread to this gathered
3712                 ;; thread.
3713                 (setcdr (car hthread)
3714                         (nconc (cdar hthread) (list (car threads))))
3715                 ;; Remove it from the list of threads.
3716                 (setcdr prev (cdr threads))
3717                 (setq threads prev))
3718             ;; Enter this thread into the hash table.
3719             (gnus-sethash subject
3720                           (if gnus-summary-make-false-root-always
3721                               (progn
3722                                 ;; If you want a dummy root above all
3723                                 ;; threads...
3724                                 (setcar threads (list whole-subject
3725                                                       (car threads)))
3726                                 threads)
3727                             threads)
3728                           hashtb)))
3729         (setq prev threads)
3730         (setq threads (cdr threads)))
3731       result)))
3732
3733 (defun gnus-gather-threads-by-references (threads)
3734   "Gather threads by looking at References headers."
3735   (let ((idhashtb (gnus-make-hashtable 1024))
3736         (thhashtb (gnus-make-hashtable 1024))
3737         (prev threads)
3738         (result threads)
3739         ids references id gthread gid entered ref)
3740     (while threads
3741       (when (setq references (mail-header-references (caar threads)))
3742         (setq id (mail-header-id (caar threads))
3743               ids (inline (gnus-split-references references))
3744               entered nil)
3745         (while (setq ref (pop ids))
3746           (setq ids (delete ref ids))
3747           (if (not (setq gid (gnus-gethash ref idhashtb)))
3748               (progn
3749                 (gnus-sethash ref id idhashtb)
3750                 (gnus-sethash id threads thhashtb))
3751             (setq gthread (gnus-gethash gid thhashtb))
3752             (unless entered
3753               ;; We enter a dummy root into the thread, if we
3754               ;; haven't done that already.
3755               (unless (stringp (caar gthread))
3756                 (setcar gthread (list (mail-header-subject (caar gthread))
3757                                       (car gthread))))
3758               ;; We add this new gathered thread to this gathered
3759               ;; thread.
3760               (setcdr (car gthread)
3761                       (nconc (cdar gthread) (list (car threads)))))
3762             ;; Add it into the thread hash table.
3763             (gnus-sethash id gthread thhashtb)
3764             (setq entered t)
3765             ;; Remove it from the list of threads.
3766             (setcdr prev (cdr threads))
3767             (setq threads prev))))
3768       (setq prev threads)
3769       (setq threads (cdr threads)))
3770     result))
3771
3772 (defun gnus-sort-gathered-threads (threads)
3773   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3774   (let ((result threads))
3775     (while threads
3776       (when (stringp (caar threads))
3777         (setcdr (car threads)
3778                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3779       (setq threads (cdr threads)))
3780     result))
3781
3782 (defun gnus-thread-loop-p (root thread)
3783   "Say whether ROOT is in THREAD."
3784   (let ((stack (list thread))
3785         (infloop 0)
3786         th)
3787     (while (setq thread (pop stack))
3788       (setq th (cdr thread))
3789       (while (and th
3790                   (not (eq (caar th) root)))
3791         (pop th))
3792       (if th
3793           ;; We have found a loop.
3794           (let (ref-dep)
3795             (setcdr thread (delq (car th) (cdr thread)))
3796             (if (boundp (setq ref-dep (intern "none"
3797                                               gnus-newsgroup-dependencies)))
3798                 (setcdr (symbol-value ref-dep)
3799                         (nconc (cdr (symbol-value ref-dep))
3800                                (list (car th))))
3801               (set ref-dep (list nil (car th))))
3802             (setq infloop 1
3803                   stack nil))
3804         ;; Push all the subthreads onto the stack.
3805         (push (cdr thread) stack)))
3806     infloop))
3807
3808 (defun gnus-make-threads ()
3809   "Go through the dependency hashtb and find the roots.  Return all threads."
3810   (let (threads)
3811     (while (catch 'infloop
3812              (mapatoms
3813               (lambda (refs)
3814                 ;; Deal with self-referencing References loops.
3815                 (when (and (car (symbol-value refs))
3816                            (not (zerop
3817                                  (apply
3818                                   '+
3819                                   (mapcar
3820                                    (lambda (thread)
3821                                      (gnus-thread-loop-p
3822                                       (car (symbol-value refs)) thread))
3823                                    (cdr (symbol-value refs)))))))
3824                   (setq threads nil)
3825                   (throw 'infloop t))
3826                 (unless (car (symbol-value refs))
3827                   ;; These threads do not refer back to any other articles,
3828                   ;; so they're roots.
3829                   (setq threads (append (cdr (symbol-value refs)) threads))))
3830               gnus-newsgroup-dependencies)))
3831     threads))
3832
3833 ;; Build the thread tree.
3834 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3835   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3836
3837 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3838 if it was already present.
3839
3840 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3841 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3842 Message-IDs will be renamed to a unique Message-ID before being
3843 entered.
3844
3845 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3846   (let* ((id (mail-header-id header))
3847          (id-dep (and id (intern id dependencies)))
3848          parent-id ref ref-dep ref-header replaced)
3849     ;; Enter this `header' in the `dependencies' table.
3850     (cond
3851      ((not id-dep)
3852       (setq header nil))
3853      ;; The first two cases do the normal part: enter a new `header'
3854      ;; in the `dependencies' table.
3855      ((not (boundp id-dep))
3856       (set id-dep (list header)))
3857      ((null (car (symbol-value id-dep)))
3858       (setcar (symbol-value id-dep) header))
3859
3860      ;; From here the `header' was already present in the
3861      ;; `dependencies' table.
3862      (force-new
3863       ;; Overrides an existing entry;
3864       ;; just set the header part of the entry.
3865       (setcar (symbol-value id-dep) header)
3866       (setq replaced t))
3867
3868      ;; Renames the existing `header' to a unique Message-ID.
3869      ((not gnus-summary-ignore-duplicates)
3870       ;; An article with this Message-ID has already been seen.
3871       ;; We rename the Message-ID.
3872       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3873            (list header))
3874       (mail-header-set-id header id))
3875
3876      ;; The last case ignores an existing entry, except it adds any
3877      ;; additional Xrefs (in case the two articles came from different
3878      ;; servers.
3879      ;; Also sets `header' to `nil' meaning that the `dependencies'
3880      ;; table was *not* modified.
3881      (t
3882       (mail-header-set-xref
3883        (car (symbol-value id-dep))
3884        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3885                    "")
3886                (or (mail-header-xref header) "")))
3887       (setq header nil)))
3888
3889     (when (and header (not replaced))
3890       ;; First check that we are not creating a References loop.
3891       (setq parent-id (gnus-parent-id (mail-header-references header)))
3892       (setq ref parent-id)
3893       (while (and ref
3894                   (setq ref-dep (intern-soft ref dependencies))
3895                   (boundp ref-dep)
3896                   (setq ref-header (car (symbol-value ref-dep))))
3897         (if (string= id ref)
3898             ;; Yuk!  This is a reference loop.  Make the article be a
3899             ;; root article.
3900             (progn
3901               (mail-header-set-references (car (symbol-value id-dep)) "none")
3902               (setq ref nil)
3903               (setq parent-id nil))
3904           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3905       (setq ref-dep (intern (or parent-id "none") dependencies))
3906       (if (boundp ref-dep)
3907           (setcdr (symbol-value ref-dep)
3908                   (nconc (cdr (symbol-value ref-dep))
3909                          (list (symbol-value id-dep))))
3910         (set ref-dep (list nil (symbol-value id-dep)))))
3911     header))
3912
3913 (defun gnus-extract-message-id-from-in-reply-to (string)
3914   (if (string-match "<[^>]+>" string)
3915       (substring string (match-beginning 0) (match-end 0))
3916     nil))
3917
3918 (defun gnus-build-sparse-threads ()
3919   (let ((headers gnus-newsgroup-headers)
3920         (mail-parse-charset gnus-newsgroup-charset)
3921         (gnus-summary-ignore-duplicates t)
3922         header references generation relations
3923         subject child end new-child date)
3924     ;; First we create an alist of generations/relations, where
3925     ;; generations is how much we trust the relation, and the relation
3926     ;; is parent/child.
3927     (gnus-message 7 "Making sparse threads...")
3928     (save-excursion
3929       (nnheader-set-temp-buffer " *gnus sparse threads*")
3930       (while (setq header (pop headers))
3931         (when (and (setq references (mail-header-references header))
3932                    (not (string= references "")))
3933           (insert references)
3934           (setq child (mail-header-id header)
3935                 subject (mail-header-subject header)
3936                 date (mail-header-date header)
3937                 generation 0)
3938           (while (search-backward ">" nil t)
3939             (setq end (1+ (point)))
3940             (when (search-backward "<" nil t)
3941               (setq new-child (buffer-substring (point) end))
3942               (push (list (incf generation)
3943                           child (setq child new-child)
3944                           subject date)
3945                     relations)))
3946           (when child
3947             (push (list (1+ generation) child nil subject) relations))
3948           (erase-buffer)))
3949       (kill-buffer (current-buffer)))
3950     ;; Sort over trustworthiness.
3951     (mapcar
3952      (lambda (relation)
3953        (when (gnus-dependencies-add-header
3954               (make-full-mail-header-from-decoded-header
3955                gnus-reffed-article-number
3956                (nth 3 relation) "" (or (nth 4 relation) "")
3957                (nth 1 relation)
3958                (or (nth 2 relation) "") 0 0 "")
3959               gnus-newsgroup-dependencies nil)
3960          (push gnus-reffed-article-number gnus-newsgroup-limit)
3961          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3962          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3963                gnus-newsgroup-reads)
3964          (decf gnus-reffed-article-number)))
3965      (sort relations 'car-less-than-car))
3966     (gnus-message 7 "Making sparse threads...done")))
3967
3968 (defun gnus-build-old-threads ()
3969   ;; Look at all the articles that refer back to old articles, and
3970   ;; fetch the headers for the articles that aren't there.  This will
3971   ;; build complete threads - if the roots haven't been expired by the
3972   ;; server, that is.
3973   (let ((mail-parse-charset gnus-newsgroup-charset)
3974         id heads)
3975     (mapatoms
3976      (lambda (refs)
3977        (when (not (car (symbol-value refs)))
3978          (setq heads (cdr (symbol-value refs)))
3979          (while heads
3980            (if (memq (mail-header-number (caar heads))
3981                      gnus-newsgroup-dormant)
3982                (setq heads (cdr heads))
3983              (setq id (symbol-name refs))
3984              (while (and (setq id (gnus-build-get-header id))
3985                          (not (car (gnus-id-to-thread id)))))
3986              (setq heads nil)))))
3987      gnus-newsgroup-dependencies)))
3988
3989 (defsubst gnus-remove-odd-characters (string)
3990   "Translate STRING into something that doesn't contain weird characters."
3991   (mm-subst-char-in-string
3992    ?\r ?\-
3993    (mm-subst-char-in-string ?\n ?\- string t) t))
3994
3995 ;; This function has to be called with point after the article number
3996 ;; on the beginning of the line.
3997 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3998   (let ((eol (point-at-eol))
3999         (buffer (current-buffer))
4000         header references in-reply-to)
4001
4002     ;; overview: [num subject from date id refs chars lines misc]
4003     (unwind-protect
4004         (progn
4005           (narrow-to-region (point) eol)
4006           (unless (eobp)
4007             (forward-char))
4008
4009           (setq header
4010                 (make-full-mail-header
4011                  number                         ; number
4012                  (nnheader-nov-field)           ; subject
4013                  (nnheader-nov-field)           ; from
4014                  (nnheader-nov-field)           ; date
4015                  (nnheader-nov-read-message-id) ; id
4016                  (nnheader-nov-field)           ; refs
4017                  (nnheader-nov-read-integer)    ; chars
4018                  (nnheader-nov-read-integer)    ; lines
4019                  (unless (eobp)
4020                    (if (looking-at "Xref: ")
4021                        (goto-char (match-end 0)))
4022                    (nnheader-nov-field))        ; Xref
4023                  (nnheader-nov-parse-extra))))  ; extra
4024
4025       (widen))
4026
4027     (when (and (string= references "")
4028                (setq in-reply-to (mail-header-extra header))
4029                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
4030       (mail-header-set-references
4031        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
4032
4033     (when gnus-alter-header-function
4034       (funcall gnus-alter-header-function header))
4035     (gnus-dependencies-add-header header dependencies force-new)))
4036
4037 (defun gnus-build-get-header (id)
4038   "Look through the buffer of NOV lines and find the header to ID.
4039 Enter this line into the dependencies hash table, and return
4040 the id of the parent article (if any)."
4041   (let ((deps gnus-newsgroup-dependencies)
4042         found header)
4043     (prog1
4044         (save-excursion
4045           (set-buffer nntp-server-buffer)
4046           (let ((case-fold-search nil))
4047             (goto-char (point-min))
4048             (while (and (not found)
4049                         (search-forward id nil t))
4050               (beginning-of-line)
4051               (setq found (looking-at
4052                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
4053                                    (regexp-quote id))))
4054               (or found (beginning-of-line 2)))
4055             (when found
4056               (beginning-of-line)
4057               (and
4058                (setq header (gnus-nov-parse-line
4059                              (read (current-buffer)) deps))
4060                (gnus-parent-id (mail-header-references header))))))
4061       (when header
4062         (let ((number (mail-header-number header)))
4063           (push number gnus-newsgroup-limit)
4064           (push header gnus-newsgroup-headers)
4065           (if (memq number gnus-newsgroup-unselected)
4066               (progn
4067                 (setq gnus-newsgroup-unreads
4068                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
4069                                                number))
4070                 (setq gnus-newsgroup-unselected
4071                       (delq number gnus-newsgroup-unselected)))
4072             (push number gnus-newsgroup-ancient)))))))
4073
4074 (defun gnus-build-all-threads ()
4075   "Read all the headers."
4076   (let ((gnus-summary-ignore-duplicates t)
4077         (mail-parse-charset gnus-newsgroup-charset)
4078         (dependencies gnus-newsgroup-dependencies)
4079         header article)
4080     (save-excursion
4081       (set-buffer nntp-server-buffer)
4082       (let ((case-fold-search nil))
4083         (goto-char (point-min))
4084         (while (not (eobp))
4085           (ignore-errors
4086             (setq article (read (current-buffer))
4087                   header (gnus-nov-parse-line article dependencies)))
4088           (when header
4089             (save-excursion
4090               (set-buffer gnus-summary-buffer)
4091               (push header gnus-newsgroup-headers)
4092               (if (memq (setq article (mail-header-number header))
4093                         gnus-newsgroup-unselected)
4094                   (progn
4095                     (setq gnus-newsgroup-unreads
4096                           (gnus-add-to-sorted-list
4097                            gnus-newsgroup-unreads article))
4098                     (setq gnus-newsgroup-unselected
4099                           (delq article gnus-newsgroup-unselected)))
4100                 (push article gnus-newsgroup-ancient)))
4101             (forward-line 1)))))))
4102
4103 (defun gnus-summary-update-article-line (article header)
4104   "Update the line for ARTICLE using HEADER."
4105   (let* ((id (mail-header-id header))
4106          (thread (gnus-id-to-thread id)))
4107     (unless thread
4108       (error "Article in no thread"))
4109     ;; Update the thread.
4110     (setcar thread header)
4111     (gnus-summary-goto-subject article)
4112     (let* ((datal (gnus-data-find-list article))
4113            (data (car datal))
4114            (buffer-read-only nil)
4115            (level (gnus-summary-thread-level)))
4116       (gnus-delete-line)
4117       (let ((inserted (- (point)
4118                          (progn
4119                            (gnus-summary-insert-line
4120                             header level nil
4121                             (memq article gnus-newsgroup-undownloaded)
4122                             (gnus-article-mark article)
4123                             (memq article gnus-newsgroup-replied)
4124                             (memq article gnus-newsgroup-expirable)
4125                             ;; Only insert the Subject string when it's different
4126                             ;; from the previous Subject string.
4127                             (if (and
4128                                  gnus-show-threads
4129                                  (gnus-subject-equal
4130                                   (condition-case ()
4131                                       (mail-header-subject
4132                                        (gnus-data-header
4133                                         (cadr
4134                                          (gnus-data-find-list
4135                                           article
4136                                           (gnus-data-list t)))))
4137                                     ;; Error on the side of excessive subjects.
4138                                     (error ""))
4139                                   (mail-header-subject header)))
4140                                 ""
4141                               (mail-header-subject header))
4142                             nil (cdr (assq article gnus-newsgroup-scored))
4143                             (memq article gnus-newsgroup-processable))
4144                            (point)))))
4145         (when (cdr datal)
4146           (gnus-data-update-list
4147            (cdr datal)
4148            (- (gnus-data-pos data) (gnus-data-pos (cadr datal)) inserted)))))))
4149
4150 (defun gnus-summary-update-article (article &optional iheader)
4151   "Update ARTICLE in the summary buffer."
4152   (set-buffer gnus-summary-buffer)
4153   (let* ((header (gnus-summary-article-header article))
4154          (id (mail-header-id header))
4155          (data (gnus-data-find article))
4156          (thread (gnus-id-to-thread id))
4157          (references (mail-header-references header))
4158          (parent
4159           (gnus-id-to-thread
4160            (or (gnus-parent-id
4161                 (when (and references
4162                            (not (equal "" references)))
4163                   references))
4164                "none")))
4165          (buffer-read-only nil)
4166          (old (car thread)))
4167     (when thread
4168       (unless iheader
4169         (setcar thread nil)
4170         (when parent
4171           (delq thread parent)))
4172       (if (gnus-summary-insert-subject id header)
4173           ;; Set the (possibly) new article number in the data structure.
4174           (gnus-data-set-number data (gnus-id-to-article id))
4175         (setcar thread old)
4176         nil))))
4177
4178 (defun gnus-rebuild-thread (id &optional line)
4179   "Rebuild the thread containing ID.
4180 If LINE, insert the rebuilt thread starting on line LINE."
4181   (let ((buffer-read-only nil)
4182         old-pos current thread data)
4183     (if (not gnus-show-threads)
4184         (setq thread (list (car (gnus-id-to-thread id))))
4185       ;; Get the thread this article is part of.
4186       (setq thread (gnus-remove-thread id)))
4187     (setq old-pos (point-at-bol))
4188     (setq current (save-excursion
4189                     (and (re-search-backward "[\r\n]" nil t)
4190                          (gnus-summary-article-number))))
4191     ;; If this is a gathered thread, we have to go some re-gathering.
4192     (when (stringp (car thread))
4193       (let ((subject (car thread))
4194             roots thr)
4195         (setq thread (cdr thread))
4196         (while thread
4197           (unless (memq (setq thr (gnus-id-to-thread
4198                                    (gnus-root-id
4199                                     (mail-header-id (caar thread)))))
4200                         roots)
4201             (push thr roots))
4202           (setq thread (cdr thread)))
4203         ;; We now have all (unique) roots.
4204         (if (= (length roots) 1)
4205             ;; All the loose roots are now one solid root.
4206             (setq thread (car roots))
4207           (setq thread (cons subject (gnus-sort-threads roots))))))
4208     (let (threads)
4209       ;; We then insert this thread into the summary buffer.
4210       (when line
4211         (goto-char (point-min))
4212         (forward-line (1- line)))
4213       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4214         (if gnus-show-threads
4215             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4216           (gnus-summary-prepare-unthreaded thread))
4217         (setq data (nreverse gnus-newsgroup-data))
4218         (setq threads gnus-newsgroup-threads))
4219       ;; We splice the new data into the data structure.
4220       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4221       ;;!!! then we want to insert at the beginning of the buffer.
4222       ;;!!! That happens to be true with Gnus now, but that may
4223       ;;!!! change in the future.  Perhaps.
4224       (gnus-data-enter-list
4225        (if line nil current) data (- (point) old-pos))
4226       (setq gnus-newsgroup-threads
4227             (nconc threads gnus-newsgroup-threads))
4228       (gnus-data-compute-positions))))
4229
4230 (defun gnus-number-to-header (number)
4231   "Return the header for article NUMBER."
4232   (let ((headers gnus-newsgroup-headers))
4233     (while (and headers
4234                 (not (= number (mail-header-number (car headers)))))
4235       (pop headers))
4236     (when headers
4237       (car headers))))
4238
4239 (defun gnus-parent-headers (in-headers &optional generation)
4240   "Return the headers of the GENERATIONeth parent of HEADERS."
4241   (unless generation
4242     (setq generation 1))
4243   (let ((parent t)
4244         (headers in-headers)
4245         references)
4246     (while (and parent
4247                 (not (zerop generation))
4248                 (setq references (mail-header-references headers)))
4249       (setq headers (if (and references
4250                              (setq parent (gnus-parent-id references)))
4251                         (car (gnus-id-to-thread parent))
4252                       nil))
4253       (decf generation))
4254     (and (not (eq headers in-headers))
4255          headers)))
4256
4257 (defun gnus-id-to-thread (id)
4258   "Return the (sub-)thread where ID appears."
4259   (gnus-gethash id gnus-newsgroup-dependencies))
4260
4261 (defun gnus-id-to-article (id)
4262   "Return the article number of ID."
4263   (let ((thread (gnus-id-to-thread id)))
4264     (when (and thread
4265                (car thread))
4266       (mail-header-number (car thread)))))
4267
4268 (defun gnus-id-to-header (id)
4269   "Return the article headers of ID."
4270   (car (gnus-id-to-thread id)))
4271
4272 (defun gnus-article-displayed-root-p (article)
4273   "Say whether ARTICLE is a root(ish) article."
4274   (let ((level (gnus-summary-thread-level article))
4275         (refs (mail-header-references  (gnus-summary-article-header article)))
4276         particle)
4277     (cond
4278      ((null level) nil)
4279      ((zerop level) t)
4280      ((null refs) t)
4281      ((null (gnus-parent-id refs)) t)
4282      ((and (= 1 level)
4283            (null (setq particle (gnus-id-to-article
4284                                  (gnus-parent-id refs))))
4285            (null (gnus-summary-thread-level particle)))))))
4286
4287 (defun gnus-root-id (id)
4288   "Return the id of the root of the thread where ID appears."
4289   (let (last-id prev)
4290     (while (and id (setq prev (car (gnus-id-to-thread id))))
4291       (setq last-id id
4292             id (gnus-parent-id (mail-header-references prev))))
4293     last-id))
4294
4295 (defun gnus-articles-in-thread (thread)
4296   "Return the list of articles in THREAD."
4297   (cons (mail-header-number (car thread))
4298         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4299
4300 (defun gnus-remove-thread (id &optional dont-remove)
4301   "Remove the thread that has ID in it."
4302   (let (headers thread last-id)
4303     ;; First go up in this thread until we find the root.
4304     (setq last-id (gnus-root-id id)
4305           headers (message-flatten-list (gnus-id-to-thread last-id)))
4306     ;; We have now found the real root of this thread.  It might have
4307     ;; been gathered into some loose thread, so we have to search
4308     ;; through the threads to find the thread we wanted.
4309     (let ((threads gnus-newsgroup-threads)
4310           sub)
4311       (while threads
4312         (setq sub (car threads))
4313         (if (stringp (car sub))
4314             ;; This is a gathered thread, so we look at the roots
4315             ;; below it to find whether this article is in this
4316             ;; gathered root.
4317             (progn
4318               (setq sub (cdr sub))
4319               (while sub
4320                 (when (member (caar sub) headers)
4321                   (setq thread (car threads)
4322                         threads nil
4323                         sub nil))
4324                 (setq sub (cdr sub))))
4325           ;; It's an ordinary thread, so we check it.
4326           (when (eq (car sub) (car headers))
4327             (setq thread sub
4328                   threads nil)))
4329         (setq threads (cdr threads)))
4330       ;; If this article is in no thread, then it's a root.
4331       (if thread
4332           (unless dont-remove
4333             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4334         (setq thread (gnus-id-to-thread last-id)))
4335       (when thread
4336         (prog1
4337             thread                      ; We return this thread.
4338           (unless dont-remove
4339             (if (stringp (car thread))
4340                 (progn
4341                   ;; If we use dummy roots, then we have to remove the
4342                   ;; dummy root as well.
4343                   (when (eq gnus-summary-make-false-root 'dummy)
4344                     ;; We go to the dummy root by going to
4345                     ;; the first sub-"thread", and then one line up.
4346                     (gnus-summary-goto-article
4347                      (mail-header-number (caadr thread)))
4348                     (forward-line -1)
4349                     (gnus-delete-line)
4350                     (gnus-data-compute-positions))
4351                   (setq thread (cdr thread))
4352                   (while thread
4353                     (gnus-remove-thread-1 (car thread))
4354                     (setq thread (cdr thread))))
4355               (gnus-remove-thread-1 thread))))))))
4356
4357 (defun gnus-remove-thread-1 (thread)
4358   "Remove the thread THREAD recursively."
4359   (let ((number (mail-header-number (pop thread)))
4360         d)
4361     (setq thread (reverse thread))
4362     (while thread
4363       (gnus-remove-thread-1 (pop thread)))
4364     (when (setq d (gnus-data-find number))
4365       (goto-char (gnus-data-pos d))
4366       (gnus-summary-show-thread)
4367       (gnus-data-remove
4368        number
4369        (- (point-at-bol)
4370           (prog1
4371               (1+ (point-at-eol))
4372             (gnus-delete-line)))))))
4373
4374 (defun gnus-sort-threads-1 (threads func)
4375   (sort (mapcar (lambda (thread)
4376                   (cons (car thread)
4377                         (and (cdr thread)
4378                              (gnus-sort-threads-1 (cdr thread) func))))
4379                 threads) func))
4380
4381 (defun gnus-sort-threads (threads)
4382   "Sort THREADS."
4383   (if (not gnus-thread-sort-functions)
4384       threads
4385     (gnus-message 8 "Sorting threads...")
4386     (let ((max-lisp-eval-depth 5000))
4387       (prog1 (gnus-sort-threads-1
4388          threads
4389          (gnus-make-sort-function gnus-thread-sort-functions))
4390         (gnus-message 8 "Sorting threads...done")))))
4391
4392 (defun gnus-sort-articles (articles)
4393   "Sort ARTICLES."
4394   (when gnus-article-sort-functions
4395     (gnus-message 7 "Sorting articles...")
4396     (prog1
4397         (setq gnus-newsgroup-headers
4398               (sort articles (gnus-make-sort-function
4399                               gnus-article-sort-functions)))
4400       (gnus-message 7 "Sorting articles...done"))))
4401
4402 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4403 (defmacro gnus-thread-header (thread)
4404   "Return header of first article in THREAD.
4405 Note that THREAD must never, ever be anything else than a variable -
4406 using some other form will lead to serious barfage."
4407   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4408   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4409   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4410         (vector thread) 2))
4411
4412 (defsubst gnus-article-sort-by-number (h1 h2)
4413   "Sort articles by article number."
4414   (< (mail-header-number h1)
4415      (mail-header-number h2)))
4416
4417 (defun gnus-thread-sort-by-number (h1 h2)
4418   "Sort threads by root article number."
4419   (gnus-article-sort-by-number
4420    (gnus-thread-header h1) (gnus-thread-header h2)))
4421
4422 (defsubst gnus-article-sort-by-random (h1 h2)
4423   "Sort articles by article number."
4424   (zerop (random 2)))
4425
4426 (defun gnus-thread-sort-by-random (h1 h2)
4427   "Sort threads by root article number."
4428   (gnus-article-sort-by-random
4429    (gnus-thread-header h1) (gnus-thread-header h2)))
4430
4431 (defsubst gnus-article-sort-by-lines (h1 h2)
4432   "Sort articles by article Lines header."
4433   (< (mail-header-lines h1)
4434      (mail-header-lines h2)))
4435
4436 (defun gnus-thread-sort-by-lines (h1 h2)
4437   "Sort threads by root article Lines header."
4438   (gnus-article-sort-by-lines
4439    (gnus-thread-header h1) (gnus-thread-header h2)))
4440
4441 (defsubst gnus-article-sort-by-chars (h1 h2)
4442   "Sort articles by octet length."
4443   (< (mail-header-chars h1)
4444      (mail-header-chars h2)))
4445
4446 (defun gnus-thread-sort-by-chars (h1 h2)
4447   "Sort threads by root article octet length."
4448   (gnus-article-sort-by-chars
4449    (gnus-thread-header h1) (gnus-thread-header h2)))
4450
4451 (defsubst gnus-article-sort-by-author (h1 h2)
4452   "Sort articles by root author."
4453   (string-lessp
4454    (let ((addr (car (mime-entity-read-field h1 'From))))
4455      (or (std11-full-name-string addr)
4456          (std11-address-string addr)
4457          ""))
4458    (let ((addr (car (mime-entity-read-field h2 'From))))
4459      (or (std11-full-name-string addr)
4460          (std11-address-string addr)
4461          ""))
4462    ))
4463
4464 (defun gnus-thread-sort-by-author (h1 h2)
4465   "Sort threads by root author."
4466   (gnus-article-sort-by-author
4467    (gnus-thread-header h1)  (gnus-thread-header h2)))
4468
4469 (defsubst gnus-article-sort-by-subject (h1 h2)
4470   "Sort articles by root subject."
4471   (string-lessp
4472    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4473    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4474
4475 (defun gnus-thread-sort-by-subject (h1 h2)
4476   "Sort threads by root subject."
4477   (gnus-article-sort-by-subject
4478    (gnus-thread-header h1) (gnus-thread-header h2)))
4479
4480 (defsubst gnus-article-sort-by-date (h1 h2)
4481   "Sort articles by root article date."
4482   (time-less-p
4483    (gnus-date-get-time (mail-header-date h1))
4484    (gnus-date-get-time (mail-header-date h2))))
4485
4486 (defun gnus-thread-sort-by-date (h1 h2)
4487   "Sort threads by root article date."
4488   (gnus-article-sort-by-date
4489    (gnus-thread-header h1) (gnus-thread-header h2)))
4490
4491 (defsubst gnus-article-sort-by-score (h1 h2)
4492   "Sort articles by root article score.
4493 Unscored articles will be counted as having a score of zero."
4494   (> (or (cdr (assq (mail-header-number h1)
4495                     gnus-newsgroup-scored))
4496          gnus-summary-default-score 0)
4497      (or (cdr (assq (mail-header-number h2)
4498                     gnus-newsgroup-scored))
4499          gnus-summary-default-score 0)))
4500
4501 (defun gnus-thread-sort-by-score (h1 h2)
4502   "Sort threads by root article score."
4503   (gnus-article-sort-by-score
4504    (gnus-thread-header h1) (gnus-thread-header h2)))
4505
4506 (defun gnus-thread-sort-by-total-score (h1 h2)
4507   "Sort threads by the sum of all scores in the thread.
4508 Unscored articles will be counted as having a score of zero."
4509   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4510
4511 (defun gnus-thread-total-score (thread)
4512   ;; This function find the total score of THREAD.
4513   (cond
4514    ((null thread)
4515     0)
4516    ((consp thread)
4517     (if (stringp (car thread))
4518         (apply gnus-thread-score-function 0
4519                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4520       (gnus-thread-total-score-1 thread)))
4521    (t
4522     (gnus-thread-total-score-1 (list thread)))))
4523
4524 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4525   "Sort threads such that the thread with the most recently arrived article comes first."
4526   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4527
4528 (defun gnus-thread-highest-number (thread)
4529   "Return the highest article number in THREAD."
4530   (apply 'max (mapcar (lambda (header)
4531                         (mail-header-number header))
4532                       (message-flatten-list thread))))
4533
4534 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4535   "Sort threads such that the thread with the most recently dated article comes first."
4536   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4537
4538 (defun gnus-thread-latest-date (thread)
4539   "Return the highest article date in THREAD."
4540   (let ((previous-time 0))
4541     (apply 'max
4542            (mapcar
4543             (lambda (header)
4544               (setq previous-time
4545                     (condition-case ()
4546                         (time-to-seconds (mail-header-parse-date
4547                                           (mail-header-date header)))
4548                       (error previous-time))))
4549             (sort
4550              (message-flatten-list thread)
4551              (lambda (h1 h2)
4552                (< (mail-header-number h1)
4553                   (mail-header-number h2))))))))
4554
4555 (defun gnus-thread-total-score-1 (root)
4556   ;; This function find the total score of the thread below ROOT.
4557   (setq root (car root))
4558   (apply gnus-thread-score-function
4559          (or (append
4560               (mapcar 'gnus-thread-total-score
4561                       (cdr (gnus-id-to-thread (mail-header-id root))))
4562               (when (> (mail-header-number root) 0)
4563                 (list (or (cdr (assq (mail-header-number root)
4564                                      gnus-newsgroup-scored))
4565                           gnus-summary-default-score 0))))
4566              (list gnus-summary-default-score)
4567              '(0))))
4568
4569 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4570 (defvar gnus-tmp-prev-subject nil)
4571 (defvar gnus-tmp-false-parent nil)
4572 (defvar gnus-tmp-root-expunged nil)
4573 (defvar gnus-tmp-dummy-line nil)
4574
4575 (eval-when-compile (defvar gnus-tmp-header))
4576 (defun gnus-extra-header (type &optional header)
4577   "Return the extra header of TYPE."
4578   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4579       ""))
4580
4581 (defvar gnus-tmp-thread-tree-header-string "")
4582
4583 (defcustom gnus-sum-thread-tree-root "> "
4584   "With %B spec, used for the root of a thread.
4585 If nil, use subject instead."
4586   :type '(radio (const :format "%v  " nil) (string :size 0))
4587   :group 'gnus-thread)
4588 (defcustom gnus-sum-thread-tree-false-root "> "
4589   "With %B spec, used for a false root of a thread.
4590 If nil, use subject instead."
4591   :type '(radio (const :format "%v  " nil) (string :size 0))
4592   :group 'gnus-thread)
4593 (defcustom gnus-sum-thread-tree-single-indent ""
4594   "With %B spec, used for a thread with just one message.
4595 If nil, use subject instead."
4596   :type '(radio (const :format "%v  " nil) (string :size 0))
4597   :group 'gnus-thread)
4598 (defcustom gnus-sum-thread-tree-vertical "| "
4599   "With %B spec, used for drawing a vertical line."
4600   :type 'string
4601   :group 'gnus-thread)
4602 (defcustom gnus-sum-thread-tree-indent "  "
4603   "With %B spec, used for indenting."
4604   :type 'string
4605   :group 'gnus-thread)
4606 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4607   "With %B spec, used for a leaf with brothers."
4608   :type 'string
4609   :group 'gnus-thread)
4610 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4611   "With %B spec, used for a leaf without brothers."
4612   :type 'string
4613   :group 'gnus-thread)
4614
4615 (defun gnus-summary-prepare-threads (threads)
4616   "Prepare summary buffer from THREADS and indentation LEVEL.
4617 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4618 or a straight list of headers."
4619   (gnus-message 7 "Generating summary...")
4620
4621   (setq gnus-newsgroup-threads threads)
4622   (beginning-of-line)
4623
4624   (let ((gnus-tmp-level 0)
4625         (default-score (or gnus-summary-default-score 0))
4626         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4627         (building-line-count gnus-summary-display-while-building)
4628         (building-count (integerp gnus-summary-display-while-building))
4629         thread number subject stack state gnus-tmp-gathered beg-match
4630         new-roots gnus-tmp-new-adopts thread-end simp-subject
4631         gnus-tmp-header gnus-tmp-unread gnus-tmp-downloaded
4632         gnus-tmp-replied gnus-tmp-subject-or-nil
4633         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4634         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4635         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4636         tree-stack)
4637
4638     (setq gnus-tmp-prev-subject nil
4639           gnus-tmp-thread-tree-header-string "")
4640
4641     (if (vectorp (car threads))
4642         ;; If this is a straight (sic) list of headers, then a
4643         ;; threaded summary display isn't required, so we just create
4644         ;; an unthreaded one.
4645         (gnus-summary-prepare-unthreaded threads)
4646
4647       ;; Do the threaded display.
4648
4649       (if gnus-summary-display-while-building
4650           (switch-to-buffer (buffer-name)))
4651       (while (or threads stack gnus-tmp-new-adopts new-roots)
4652
4653         (if (and (= gnus-tmp-level 0)
4654                  (or (not stack)
4655                      (= (caar stack) 0))
4656                  (not gnus-tmp-false-parent)
4657                  (or gnus-tmp-new-adopts new-roots))
4658             (if gnus-tmp-new-adopts
4659                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4660                       thread (list (car gnus-tmp-new-adopts))
4661                       gnus-tmp-header (caar thread)
4662                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4663               (when new-roots
4664                 (setq thread (list (car new-roots))
4665                       gnus-tmp-header (caar thread)
4666                       new-roots (cdr new-roots))))
4667
4668           (if threads
4669               ;; If there are some threads, we do them before the
4670               ;; threads on the stack.
4671               (setq thread threads
4672                     gnus-tmp-header (caar thread))
4673             ;; There were no current threads, so we pop something off
4674             ;; the stack.
4675             (setq state (car stack)
4676                   gnus-tmp-level (car state)
4677                   tree-stack (cadr state)
4678                   thread (caddr state)
4679                   stack (cdr stack)
4680                   gnus-tmp-header (caar thread))))
4681
4682         (setq gnus-tmp-false-parent nil)
4683         (setq gnus-tmp-root-expunged nil)
4684         (setq thread-end nil)
4685
4686         (if (stringp gnus-tmp-header)
4687             ;; The header is a dummy root.
4688             (cond
4689              ((eq gnus-summary-make-false-root 'adopt)
4690               ;; We let the first article adopt the rest.
4691               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4692                                                (cddar thread)))
4693               (setq gnus-tmp-gathered
4694                     (nconc (mapcar
4695                             (lambda (h) (mail-header-number (car h)))
4696                             (cddar thread))
4697                            gnus-tmp-gathered))
4698               (setq thread (cons (list (caar thread)
4699                                        (cadar thread))
4700                                  (cdr thread)))
4701               (setq gnus-tmp-level -1
4702                     gnus-tmp-false-parent t))
4703              ((eq gnus-summary-make-false-root 'empty)
4704               ;; We print adopted articles with empty subject fields.
4705               (setq gnus-tmp-gathered
4706                     (nconc (mapcar
4707                             (lambda (h) (mail-header-number (car h)))
4708                             (cddar thread))
4709                            gnus-tmp-gathered))
4710               (setq gnus-tmp-level -1))
4711              ((eq gnus-summary-make-false-root 'dummy)
4712               ;; We remember that we probably want to output a dummy
4713               ;; root.
4714               (setq gnus-tmp-dummy-line gnus-tmp-header)
4715               (setq gnus-tmp-prev-subject gnus-tmp-header))
4716              (t
4717               ;; We do not make a root for the gathered
4718               ;; sub-threads at all.
4719               (setq gnus-tmp-level -1)))
4720
4721           (setq number (mail-header-number gnus-tmp-header)
4722                 subject (mail-header-subject gnus-tmp-header)
4723                 simp-subject (gnus-simplify-subject-fully subject))
4724
4725           (cond
4726            ;; If the thread has changed subject, we might want to make
4727            ;; this subthread into a root.
4728            ((and (null gnus-thread-ignore-subject)
4729                  (not (zerop gnus-tmp-level))
4730                  gnus-tmp-prev-subject
4731                  (not (string= gnus-tmp-prev-subject simp-subject)))
4732             (setq new-roots (nconc new-roots (list (car thread)))
4733                   thread-end t
4734                   gnus-tmp-header nil))
4735            ;; If the article lies outside the current limit,
4736            ;; then we do not display it.
4737            ((not (memq number gnus-newsgroup-limit))
4738             (setq gnus-tmp-gathered
4739                   (nconc (mapcar
4740                           (lambda (h) (mail-header-number (car h)))
4741                           (cdar thread))
4742                          gnus-tmp-gathered))
4743             (setq gnus-tmp-new-adopts (if (cdar thread)
4744                                           (append gnus-tmp-new-adopts
4745                                                   (cdar thread))
4746                                         gnus-tmp-new-adopts)
4747                   thread-end t
4748                   gnus-tmp-header nil)
4749             (when (zerop gnus-tmp-level)
4750               (setq gnus-tmp-root-expunged t)))
4751            ;; Perhaps this article is to be marked as read?
4752            ((and gnus-summary-mark-below
4753                  (< (or (cdr (assq number gnus-newsgroup-scored))
4754                         default-score)
4755                     gnus-summary-mark-below)
4756                  ;; Don't touch sparse articles.
4757                  (not (gnus-summary-article-sparse-p number))
4758                  (not (gnus-summary-article-ancient-p number)))
4759             (setq gnus-newsgroup-unreads
4760                   (delq number gnus-newsgroup-unreads))
4761             (if gnus-newsgroup-auto-expire
4762                 (setq gnus-newsgroup-expirable
4763                       (gnus-add-to-sorted-list
4764                        gnus-newsgroup-expirable number))
4765               (push (cons number gnus-low-score-mark)
4766                     gnus-newsgroup-reads))))
4767
4768           (when gnus-tmp-header
4769             ;; We may have an old dummy line to output before this
4770             ;; article.
4771             (when (and gnus-tmp-dummy-line
4772                        (gnus-subject-equal
4773                         gnus-tmp-dummy-line
4774                         (mail-header-subject gnus-tmp-header)))
4775               (gnus-summary-insert-dummy-line
4776                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4777               (setq gnus-tmp-dummy-line nil))
4778
4779             ;; Compute the mark.
4780             (setq gnus-tmp-unread (gnus-article-mark number))
4781
4782             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4783                                   gnus-tmp-header gnus-tmp-level)
4784                   gnus-newsgroup-data)
4785
4786             ;; Actually insert the line.
4787             (setq
4788              gnus-tmp-subject-or-nil
4789              (cond
4790               ((and gnus-thread-ignore-subject
4791                     gnus-tmp-prev-subject
4792                     (not (string= gnus-tmp-prev-subject simp-subject)))
4793                subject)
4794               ((zerop gnus-tmp-level)
4795                (if (and (eq gnus-summary-make-false-root 'empty)
4796                         (memq number gnus-tmp-gathered)
4797                         gnus-tmp-prev-subject
4798                         (string= gnus-tmp-prev-subject simp-subject))
4799                    gnus-summary-same-subject
4800                  subject))
4801               (t gnus-summary-same-subject)))
4802             (if (and (eq gnus-summary-make-false-root 'adopt)
4803                      (= gnus-tmp-level 1)
4804                      (memq number gnus-tmp-gathered))
4805                 (setq gnus-tmp-opening-bracket ?\<
4806                       gnus-tmp-closing-bracket ?\>)
4807               (setq gnus-tmp-opening-bracket ?\[
4808                     gnus-tmp-closing-bracket ?\]))
4809             (setq
4810              gnus-tmp-indentation
4811              (aref gnus-thread-indent-array gnus-tmp-level)
4812              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4813              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4814                                 gnus-summary-default-score 0)
4815              gnus-tmp-score-char
4816              (if (or (null gnus-summary-default-score)
4817                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4818                          gnus-summary-zcore-fuzz))
4819                  ?\ ;;;Whitespace
4820                (if (< gnus-tmp-score gnus-summary-default-score)
4821                    gnus-score-below-mark gnus-score-over-mark))
4822              gnus-tmp-replied
4823              (cond ((memq number gnus-newsgroup-processable)
4824                     gnus-process-mark)
4825                    ((memq number gnus-newsgroup-cached)
4826                     gnus-cached-mark)
4827                    ((memq number gnus-newsgroup-replied)
4828                     gnus-replied-mark)
4829                    ((memq number gnus-newsgroup-forwarded)
4830                     gnus-forwarded-mark)
4831                    ((memq number gnus-newsgroup-saved)
4832                     gnus-saved-mark)
4833                    ((memq number gnus-newsgroup-recent)
4834                     gnus-recent-mark)
4835                    ((memq number gnus-newsgroup-unseen)
4836                     gnus-unseen-mark)
4837                    (t gnus-no-mark))
4838              gnus-tmp-downloaded
4839              (cond ((memq number gnus-newsgroup-undownloaded)
4840                     gnus-undownloaded-mark)
4841                    (gnus-newsgroup-agentized
4842                     gnus-downloaded-mark)
4843                    (t
4844                     gnus-no-mark))
4845              gnus-tmp-from (mail-header-from gnus-tmp-header)
4846              gnus-tmp-name
4847              (cond
4848               ((string-match "<[^>]+> *$" gnus-tmp-from)
4849                (setq beg-match (match-beginning 0))
4850                (or (and (string-match "^\".+\"" gnus-tmp-from)
4851                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4852                    (substring gnus-tmp-from 0 beg-match)))
4853               ((string-match "(.+)" gnus-tmp-from)
4854                (substring gnus-tmp-from
4855                           (1+ (match-beginning 0)) (1- (match-end 0))))
4856               (t gnus-tmp-from))
4857
4858              ;; Do the %B string
4859              gnus-tmp-thread-tree-header-string
4860              (cond
4861               ((not gnus-show-threads) "")
4862               ((zerop gnus-tmp-level)
4863                (cond ((cdar thread)
4864                       (or gnus-sum-thread-tree-root subject))
4865                      (gnus-tmp-new-adopts
4866                       (or gnus-sum-thread-tree-false-root subject))
4867                      (t
4868                       (or gnus-sum-thread-tree-single-indent subject))))
4869               (t
4870                (concat (apply 'concat
4871                               (mapcar (lambda (item)
4872                                         (if (= item 1)
4873                                             gnus-sum-thread-tree-vertical
4874                                           gnus-sum-thread-tree-indent))
4875                                       (cdr (reverse tree-stack))))
4876                        (if (nth 1 thread)
4877                            gnus-sum-thread-tree-leaf-with-other
4878                          gnus-sum-thread-tree-single-leaf)))))
4879             (when (string= gnus-tmp-name "")
4880               (setq gnus-tmp-name gnus-tmp-from))
4881             (unless (numberp gnus-tmp-lines)
4882               (setq gnus-tmp-lines -1))
4883             (if (= gnus-tmp-lines -1)
4884                 (setq gnus-tmp-lines "?")
4885               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4886               (gnus-put-text-property
4887              (point)
4888              (progn (eval gnus-summary-line-format-spec) (point))
4889                'gnus-number number)
4890             (when gnus-visual-p
4891               (forward-line -1)
4892               (gnus-run-hooks 'gnus-summary-update-hook)
4893               (forward-line 1))
4894
4895             (setq gnus-tmp-prev-subject simp-subject)))
4896
4897         (when (nth 1 thread)
4898           (push (list (max 0 gnus-tmp-level)
4899                       (copy-sequence tree-stack)
4900                       (nthcdr 1 thread))
4901                 stack))
4902         (push (if (nth 1 thread) 1 0) tree-stack)
4903         (incf gnus-tmp-level)
4904         (setq threads (if thread-end nil (cdar thread)))
4905         (if gnus-summary-display-while-building
4906             (if building-count
4907                 (progn
4908                   ;; use a set frequency
4909                   (setq building-line-count (1- building-line-count))
4910                   (when (= building-line-count 0)
4911                     (sit-for 0)
4912                     (setq building-line-count
4913                           gnus-summary-display-while-building)))
4914               ;; always
4915               (sit-for 0)))
4916         (unless threads
4917           (setq gnus-tmp-level 0)))))
4918   (gnus-message 7 "Generating summary...done"))
4919
4920 (defun gnus-summary-prepare-unthreaded (headers)
4921   "Generate an unthreaded summary buffer based on HEADERS."
4922   (let (header number mark)
4923
4924     (beginning-of-line)
4925
4926     (while headers
4927       ;; We may have to root out some bad articles...
4928       (when (memq (setq number (mail-header-number
4929                                 (setq header (pop headers))))
4930                   gnus-newsgroup-limit)
4931         ;; Mark article as read when it has a low score.
4932         (when (and gnus-summary-mark-below
4933                    (< (or (cdr (assq number gnus-newsgroup-scored))
4934                           gnus-summary-default-score 0)
4935                       gnus-summary-mark-below)
4936                    (not (gnus-summary-article-ancient-p number)))
4937           (setq gnus-newsgroup-unreads
4938                 (delq number gnus-newsgroup-unreads))
4939           (if gnus-newsgroup-auto-expire
4940               (push number gnus-newsgroup-expirable)
4941             (push (cons number gnus-low-score-mark)
4942                   gnus-newsgroup-reads)))
4943
4944         (setq mark (gnus-article-mark number))
4945         (push (gnus-data-make number mark (1+ (point)) header 0)
4946               gnus-newsgroup-data)
4947         (gnus-summary-insert-line
4948          header 0 number
4949          (memq number gnus-newsgroup-undownloaded)
4950          mark (memq number gnus-newsgroup-replied)
4951          (memq number gnus-newsgroup-expirable)
4952          (mail-header-subject header) nil
4953          (cdr (assq number gnus-newsgroup-scored))
4954          (memq number gnus-newsgroup-processable))))))
4955
4956 (defun gnus-summary-remove-list-identifiers ()
4957   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4958   (let ((regexp (if (consp gnus-list-identifiers)
4959                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4960                   gnus-list-identifiers))
4961         changed subject)
4962     (when regexp
4963       (setq regexp (concat "^\\(?:R[Ee]: +\\)*\\(" regexp " *\\)"))
4964       (dolist (header gnus-newsgroup-headers)
4965         (setq subject (mail-header-subject header)
4966               changed nil)
4967         (while (string-match regexp subject)
4968           (setq subject
4969                 (concat (substring subject 0 (match-beginning 1))
4970                         (substring subject (match-end 0)))
4971                 changed t))
4972         (when changed
4973           (when (string-match "^\\(\\(?:R[Ee]: +\\)+\\)R[Ee]: +" subject)
4974             (setq subject
4975                   (concat (substring subject 0 (match-beginning 1))
4976                           (substring subject (match-end 1)))))
4977           (mail-header-set-subject header subject))))))
4978
4979 (defun gnus-fetch-headers (articles)
4980   "Fetch headers of ARTICLES."
4981   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4982     (gnus-message 5 "Fetching headers for %s..." name)
4983     (prog1
4984         (if (eq 'nov
4985                 (setq gnus-headers-retrieved-by
4986                       (gnus-retrieve-headers
4987                        articles gnus-newsgroup-name
4988                        ;; We might want to fetch old headers, but
4989                        ;; not if there is only 1 article.
4990                        (and (or (and
4991                                  (not (eq gnus-fetch-old-headers 'some))
4992                                  (not (numberp gnus-fetch-old-headers)))
4993                                 (> (length articles) 1))
4994                             gnus-fetch-old-headers))))
4995             (gnus-get-newsgroup-headers-xover
4996              articles nil nil gnus-newsgroup-name t)
4997           (gnus-get-newsgroup-headers))
4998       (gnus-message 5 "Fetching headers for %s...done" name))))
4999
5000 (defun gnus-select-newsgroup (group &optional read-all select-articles)
5001   "Select newsgroup GROUP.
5002 If READ-ALL is non-nil, all articles in the group are selected.
5003 If SELECT-ARTICLES, only select those articles from GROUP."
5004   (let* ((entry (gnus-group-entry group))
5005          ;;!!! Dirty hack; should be removed.
5006          (gnus-summary-ignore-duplicates
5007           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
5008               t
5009             gnus-summary-ignore-duplicates))
5010          (info (nth 2 entry))
5011          articles fetched-articles cached)
5012
5013     (unless (gnus-check-server
5014              (set (make-local-variable 'gnus-current-select-method)
5015                   (gnus-find-method-for-group group)))
5016       (error "Couldn't open server"))
5017
5018     (or (and entry (not (eq (car entry) t))) ; Either it's active...
5019         (gnus-activate-group group)     ; Or we can activate it...
5020         (progn                          ; Or we bug out.
5021           (when (equal major-mode 'gnus-summary-mode)
5022             (gnus-kill-buffer (current-buffer)))
5023           (error "Couldn't activate group %s: %s"
5024                  group (gnus-status-message group))))
5025
5026     (unless (gnus-request-group group t)
5027       (when (equal major-mode 'gnus-summary-mode)
5028         (gnus-kill-buffer (current-buffer)))
5029       (error "Couldn't request group %s: %s"
5030              group (gnus-status-message group)))
5031
5032     (when gnus-agent
5033       (gnus-agent-possibly-alter-active group (gnus-active group) info)
5034       
5035       (setq gnus-summary-use-undownloaded-faces
5036             (gnus-agent-find-parameter
5037              group
5038              'agent-enable-undownloaded-faces)))
5039
5040     (setq gnus-newsgroup-name group
5041           gnus-newsgroup-unselected nil
5042           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
5043
5044     (let ((display (gnus-group-find-parameter group 'display)))
5045       (setq gnus-newsgroup-display
5046             (cond
5047              ((not (zerop (or (car-safe read-all) 0)))
5048               ;; The user entered the group with C-u SPC/RET, let's show
5049               ;; all articles.
5050               'gnus-not-ignore)
5051              ((eq display 'all)
5052               'gnus-not-ignore)
5053              ((arrayp display)
5054               (gnus-summary-display-make-predicate (mapcar 'identity display)))
5055              ((numberp display)
5056               ;; The following is probably the "correct" solution, but
5057               ;; it makes Gnus fetch all headers and then limit the
5058               ;; articles (which is slow), so instead we hack the
5059               ;; select-articles parameter instead. -- Simon Josefsson
5060               ;; <jas@kth.se>
5061               ;;
5062               ;; (gnus-byte-compile
5063               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
5064               ;;                         display)))))
5065               (setq select-articles
5066                     (gnus-uncompress-range
5067                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
5068                              (if (> tmp 0)
5069                                  tmp
5070                                1))
5071                            (cdr (gnus-active group)))))
5072               nil)
5073              (t
5074               nil))))
5075
5076     (gnus-summary-setup-default-charset)
5077
5078     ;; Kludge to avoid having cached articles nixed out in virtual groups.
5079     (when (gnus-virtual-group-p group)
5080       (setq cached gnus-newsgroup-cached))
5081
5082     (setq gnus-newsgroup-unreads
5083           (gnus-sorted-ndifference
5084            (gnus-sorted-ndifference gnus-newsgroup-unreads
5085                                     gnus-newsgroup-marked)
5086            gnus-newsgroup-dormant))
5087
5088     (setq gnus-newsgroup-processable nil)
5089
5090     (gnus-update-read-articles group gnus-newsgroup-unreads)
5091
5092     ;; Adjust and set lists of article marks.
5093     (when info
5094       (gnus-adjust-marked-articles info))
5095     (if (setq articles select-articles)
5096         (setq gnus-newsgroup-unselected
5097               (gnus-sorted-difference gnus-newsgroup-unreads articles))
5098       (setq articles (gnus-articles-to-read group read-all)))
5099
5100     (cond
5101      ((null articles)
5102       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
5103       'quit)
5104      ((eq articles 0) nil)
5105      (t
5106       ;; Init the dependencies hash table.
5107       (setq gnus-newsgroup-dependencies
5108             (gnus-make-hashtable (length articles)))
5109       (gnus-set-global-variables)
5110       ;; Retrieve the headers and read them in.
5111
5112       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
5113
5114       ;; Kludge to avoid having cached articles nixed out in virtual groups.
5115       (when cached
5116         (setq gnus-newsgroup-cached cached))
5117
5118       ;; Suppress duplicates?
5119       (when gnus-suppress-duplicates
5120         (gnus-dup-suppress-articles))
5121
5122       ;; Set the initial limit.
5123       (setq gnus-newsgroup-limit (copy-sequence articles))
5124       ;; Remove canceled articles from the list of unread articles.
5125       (setq fetched-articles
5126             (mapcar (lambda (headers) (mail-header-number headers))
5127                     gnus-newsgroup-headers))
5128       (setq gnus-newsgroup-articles fetched-articles)
5129       (setq gnus-newsgroup-unreads
5130             (gnus-sorted-nintersection
5131              gnus-newsgroup-unreads fetched-articles))
5132       (gnus-compute-unseen-list)
5133
5134       ;; Removed marked articles that do not exist.
5135       (gnus-update-missing-marks
5136        (gnus-sorted-difference articles fetched-articles))
5137       ;; We might want to build some more threads first.
5138       (when (and gnus-fetch-old-headers
5139                  (eq gnus-headers-retrieved-by 'nov))
5140         (if (eq gnus-fetch-old-headers 'invisible)
5141             (gnus-build-all-threads)
5142           (gnus-build-old-threads)))
5143       ;; Let the Gnus agent mark articles as read.
5144       (when gnus-agent
5145         (gnus-agent-get-undownloaded-list))
5146       ;; Remove list identifiers from subject
5147       (when gnus-list-identifiers
5148         (gnus-summary-remove-list-identifiers))
5149       ;; Check whether auto-expire is to be done in this group.
5150       (setq gnus-newsgroup-auto-expire
5151             (gnus-group-auto-expirable-p group))
5152       ;; Set up the article buffer now, if necessary.
5153       (unless gnus-single-article-buffer
5154         (gnus-article-setup-buffer))
5155       ;; First and last article in this newsgroup.
5156       (when gnus-newsgroup-headers
5157         (setq gnus-newsgroup-begin
5158               (mail-header-number (car gnus-newsgroup-headers))
5159               gnus-newsgroup-end
5160               (mail-header-number
5161                (gnus-last-element gnus-newsgroup-headers))))
5162       ;; GROUP is successfully selected.
5163       (or gnus-newsgroup-headers t)))))
5164
5165 (defun gnus-compute-unseen-list ()
5166   ;; The `seen' marks are treated specially.
5167   (if (not gnus-newsgroup-seen)
5168       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
5169     (setq gnus-newsgroup-unseen
5170           (gnus-inverse-list-range-intersection
5171            gnus-newsgroup-articles gnus-newsgroup-seen))))
5172
5173 (defun gnus-summary-display-make-predicate (display)
5174   (require 'gnus-agent)
5175   (when (= (length display) 1)
5176     (setq display (car display)))
5177   (unless gnus-summary-display-cache
5178     (dolist (elem (append '((unread . unread)
5179                             (read . read)
5180                             (unseen . unseen))
5181                           gnus-article-mark-lists))
5182       (push (cons (cdr elem)
5183                   (gnus-byte-compile
5184                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
5185             gnus-summary-display-cache)))
5186   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
5187         (gnus-category-predicate-cache gnus-summary-display-cache))
5188     (gnus-get-predicate display)))
5189
5190 ;; Uses the dynamically bound `number' variable.
5191 (eval-when-compile
5192   (defvar number))
5193 (defun gnus-article-marked-p (type &optional article)
5194   (let ((article (or article number)))
5195     (cond
5196      ((eq type 'tick)
5197       (memq article gnus-newsgroup-marked))
5198      ((eq type 'spam)
5199       (memq article gnus-newsgroup-spam-marked))
5200      ((eq type 'unsend)
5201       (memq article gnus-newsgroup-unsendable))
5202      ((eq type 'undownload)
5203       (memq article gnus-newsgroup-undownloaded))
5204      ((eq type 'download)
5205       (memq article gnus-newsgroup-downloadable))
5206      ((eq type 'unread)
5207       (memq article gnus-newsgroup-unreads))
5208      ((eq type 'read)
5209       (memq article gnus-newsgroup-reads))
5210      ((eq type 'dormant)
5211       (memq article gnus-newsgroup-dormant) )
5212      ((eq type 'expire)
5213       (memq article gnus-newsgroup-expirable))
5214      ((eq type 'reply)
5215       (memq article gnus-newsgroup-replied))
5216      ((eq type 'killed)
5217       (memq article gnus-newsgroup-killed))
5218      ((eq type 'bookmark)
5219       (assq article gnus-newsgroup-bookmarks))
5220      ((eq type 'score)
5221       (assq article gnus-newsgroup-scored))
5222      ((eq type 'save)
5223       (memq article gnus-newsgroup-saved))
5224      ((eq type 'cache)
5225       (memq article gnus-newsgroup-cached))
5226      ((eq type 'forward)
5227       (memq article gnus-newsgroup-forwarded))
5228      ((eq type 'seen)
5229       (not (memq article gnus-newsgroup-unseen)))
5230      ((eq type 'recent)
5231       (memq article gnus-newsgroup-recent))
5232      (t t))))
5233
5234 (defun gnus-articles-to-read (group &optional read-all)
5235   "Find out what articles the user wants to read."
5236   (let* ((display (gnus-group-find-parameter group 'display))
5237          (articles
5238           ;; Select all articles if `read-all' is non-nil, or if there
5239           ;; are no unread articles.
5240           (if (or read-all
5241                   (and (zerop (length gnus-newsgroup-marked))
5242                        (zerop (length gnus-newsgroup-unreads)))
5243                   ;; Fetch all if the predicate is non-nil.
5244                   gnus-newsgroup-display)
5245               ;; We want to select the headers for all the articles in
5246               ;; the group, so we select either all the active
5247               ;; articles in the group, or (if that's nil), the
5248               ;; articles in the cache.
5249               (or
5250                (gnus-uncompress-range (gnus-active group))
5251                (gnus-cache-articles-in-group group))
5252             ;; Select only the "normal" subset of articles.
5253             (gnus-sorted-nunion
5254              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5255              gnus-newsgroup-unreads)))
5256          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5257          (scored (length scored-list))
5258          (number (length articles))
5259          (marked (+ (length gnus-newsgroup-marked)
5260                     (length gnus-newsgroup-dormant)))
5261          (select
5262           (cond
5263            ((numberp read-all)
5264             read-all)
5265            ((numberp gnus-newsgroup-display)
5266             gnus-newsgroup-display)
5267            (t
5268             (condition-case ()
5269                 (cond
5270                  ((and (or (<= scored marked) (= scored number))
5271                        (numberp gnus-large-newsgroup)
5272                        (> number gnus-large-newsgroup))
5273                   (let* ((cursor-in-echo-area nil)
5274                          (initial (gnus-parameter-large-newsgroup-initial
5275                                    gnus-newsgroup-name))
5276                          (input
5277                           (read-string
5278                            (format
5279                             "How many articles from %s (%s %d): "
5280                             (gnus-limit-string
5281                              (gnus-group-decoded-name gnus-newsgroup-name)
5282                              35)
5283                             (if initial "max" "default")
5284                             number)
5285                            (if initial
5286                                (cons (number-to-string initial)
5287                                      0)))))
5288                     (if (string-match "^[ \t]*$" input) number input)))
5289                  ((and (> scored marked) (< scored number)
5290                        (> (- scored number) 20))
5291                   (let ((input
5292                          (read-string
5293                           (format "%s %s (%d scored, %d total): "
5294                                   "How many articles from"
5295                                   (gnus-group-decoded-name group)
5296                                   scored number))))
5297                     (if (string-match "^[ \t]*$" input)
5298                         number input)))
5299                  (t number))
5300               (quit
5301                (message "Quit getting the articles to read")
5302                nil))))))
5303     (setq select (if (stringp select) (string-to-number select) select))
5304     (if (or (null select) (zerop select))
5305         select
5306       (if (and (not (zerop scored)) (<= (abs select) scored))
5307           (progn
5308             (setq articles (sort scored-list '<))
5309             (setq number (length articles)))
5310         (setq articles (copy-sequence articles)))
5311
5312       (when (< (abs select) number)
5313         (if (< select 0)
5314             ;; Select the N oldest articles.
5315             (setcdr (nthcdr (1- (abs select)) articles) nil)
5316           ;; Select the N most recent articles.
5317           (setq articles (nthcdr (- number select) articles))))
5318       (setq gnus-newsgroup-unselected
5319             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5320       (when gnus-alter-articles-to-read-function
5321         (setq articles
5322               (sort
5323                (funcall gnus-alter-articles-to-read-function
5324                         gnus-newsgroup-name articles)
5325                '<)))
5326       articles)))
5327
5328 (defun gnus-killed-articles (killed articles)
5329   (let (out)
5330     (while articles
5331       (when (inline (gnus-member-of-range (car articles) killed))
5332         (push (car articles) out))
5333       (setq articles (cdr articles)))
5334     out))
5335
5336 (defun gnus-uncompress-marks (marks)
5337   "Uncompress the mark ranges in MARKS."
5338   (let ((uncompressed '(score bookmark))
5339         out)
5340     (while marks
5341       (if (memq (caar marks) uncompressed)
5342           (push (car marks) out)
5343         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5344       (setq marks (cdr marks)))
5345     out))
5346
5347 (defun gnus-article-mark-to-type (mark)
5348   "Return the type of MARK."
5349   (or (cadr (assq mark gnus-article-special-mark-lists))
5350       'list))
5351
5352 (defun gnus-article-unpropagatable-p (mark)
5353   "Return whether MARK should be propagated to back end."
5354   (memq mark gnus-article-unpropagated-mark-lists))
5355
5356 (defun gnus-adjust-marked-articles (info)
5357   "Set all article lists and remove all marks that are no longer valid."
5358   (let* ((marked-lists (gnus-info-marks info))
5359          (active (gnus-active (gnus-info-group info)))
5360          (min (car active))
5361          (max (cdr active))
5362          (types gnus-article-mark-lists)
5363          marks var articles article mark mark-type
5364          bgn end)
5365
5366     (dolist (marks marked-lists)
5367       (setq mark (car marks)
5368             mark-type (gnus-article-mark-to-type mark)
5369             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5370
5371       ;; We set the variable according to the type of the marks list,
5372       ;; and then adjust the marks to a subset of the active articles.
5373       (cond
5374        ;; Adjust "simple" lists - compressed yet unsorted
5375        ((eq mark-type 'list)
5376         ;; Simultaneously uncompress and clip to active range
5377         ;; See gnus-uncompress-range for a description of possible marks
5378         (let (l lh)
5379           (if (not (cadr marks))
5380               (set var nil)
5381             (setq articles (if (numberp (cddr marks))
5382                                (list (cdr marks))
5383                              (cdr marks))
5384                   lh (cons nil nil)
5385                   l lh)
5386
5387             (while (setq article (pop articles))
5388               (cond ((consp article)
5389                      (setq bgn (max (car article) min)
5390                            end (min (cdr article) max))
5391                      (while (<= bgn end)
5392                        (setq l (setcdr l (cons bgn nil))
5393                              bgn (1+ bgn))))
5394                     ((and (<= min article)
5395                           (>= max article))
5396                      (setq l (setcdr l (cons article nil))))))
5397             (set var (cdr lh)))))
5398        ;; Adjust assocs.
5399        ((eq mark-type 'tuple)
5400         (set var (setq articles (cdr marks)))
5401         (when (not (listp (cdr (symbol-value var))))
5402           (set var (list (symbol-value var))))
5403         (when (not (listp (cdr articles)))
5404           (setq articles (list articles)))
5405         (while articles
5406           (when (or (not (consp (setq article (pop articles))))
5407                     (< (car article) min)
5408                     (> (car article) max))
5409             (set var (delq article (symbol-value var))))))
5410        ;; Adjust ranges (sloppily).
5411        ((eq mark-type 'range)
5412         (cond
5413          ((eq mark 'seen)
5414           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5415           ;; It should be (seen (NUM1 . NUM2)).
5416           (when (numberp (cddr marks))
5417             (setcdr marks (list (cdr marks))))
5418           (setq articles (cdr marks))
5419           (while (and articles
5420                       (or (and (consp (car articles))
5421                                (> min (cdar articles)))
5422                           (and (numberp (car articles))
5423                                (> min (car articles)))))
5424             (pop articles))
5425           (set var articles))))))))
5426
5427 (defun gnus-update-missing-marks (missing)
5428   "Go through the list of MISSING articles and remove them from the mark lists."
5429   (when missing
5430     (let (var m)
5431       ;; Go through all types.
5432       (dolist (elem gnus-article-mark-lists)
5433         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5434           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5435           (when (symbol-value var)
5436             ;; This list has articles.  So we delete all missing
5437             ;; articles from it.
5438             (setq m missing)
5439             (while m
5440               (set var (delq (pop m) (symbol-value var))))))))))
5441
5442 (defun gnus-update-marks ()
5443   "Enter the various lists of marked articles into the newsgroup info list."
5444   (let ((types gnus-article-mark-lists)
5445         (info (gnus-get-info gnus-newsgroup-name))
5446         type list newmarked symbol delta-marks)
5447     (when info
5448       ;; Add all marks lists to the list of marks lists.
5449       (while (setq type (pop types))
5450         (setq list (symbol-value
5451                     (setq symbol
5452                           (intern (format "gnus-newsgroup-%s" (car type))))))
5453
5454         (when list
5455           ;; Get rid of the entries of the articles that have the
5456           ;; default score.
5457           (when (and (eq (cdr type) 'score)
5458                      gnus-save-score
5459                      list)
5460             (let* ((arts list)
5461                    (prev (cons nil list))
5462                    (all prev))
5463               (while arts
5464                 (if (or (not (consp (car arts)))
5465                         (= (cdar arts) gnus-summary-default-score))
5466                     (setcdr prev (cdr arts))
5467                   (setq prev arts))
5468                 (setq arts (cdr arts)))
5469               (setq list (cdr all)))))
5470
5471         (when (eq (cdr type) 'seen)
5472           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5473
5474         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5475           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5476
5477         (when (and (gnus-check-backend-function
5478                     'request-set-mark gnus-newsgroup-name)
5479                    (not (gnus-article-unpropagatable-p (cdr type))))
5480           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5481                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5482                  (add (gnus-remove-from-range
5483                        (gnus-copy-sequence list) old)))
5484             (when add
5485               (push (list add 'add (list (cdr type))) delta-marks))
5486             (when del
5487               (push (list del 'del (list (cdr type))) delta-marks))))
5488
5489         (when list
5490           (push (cons (cdr type) list) newmarked)))
5491
5492       (when delta-marks
5493         (unless (gnus-check-group gnus-newsgroup-name)
5494           (error "Can't open server for %s" gnus-newsgroup-name))
5495         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5496
5497       ;; Enter these new marks into the info of the group.
5498       (if (nthcdr 3 info)
5499           (setcar (nthcdr 3 info) newmarked)
5500         ;; Add the marks lists to the end of the info.
5501         (when newmarked
5502           (setcdr (nthcdr 2 info) (list newmarked))))
5503
5504       ;; Cut off the end of the info if there's nothing else there.
5505       (let ((i 5))
5506         (while (and (> i 2)
5507                     (not (nth i info)))
5508           (when (nthcdr (decf i) info)
5509             (setcdr (nthcdr i info) nil)))))))
5510
5511 (defun gnus-set-mode-line (where)
5512   "Set the mode line of the article or summary buffers.
5513 If WHERE is `summary', the summary mode line format will be used."
5514   ;; Is this mode line one we keep updated?
5515   (when (and (memq where gnus-updated-mode-lines)
5516              (symbol-value
5517               (intern (format "gnus-%s-mode-line-format-spec" where))))
5518     (let (mode-string)
5519       (save-excursion
5520         ;; We evaluate this in the summary buffer since these
5521         ;; variables are buffer-local to that buffer.
5522         (set-buffer gnus-summary-buffer)
5523         ;; We bind all these variables that are used in the `eval' form
5524         ;; below.
5525         (let* ((mformat (symbol-value
5526                          (intern
5527                           (format "gnus-%s-mode-line-format-spec" where))))
5528                (gnus-tmp-group-name (gnus-group-decoded-name
5529                                      gnus-newsgroup-name))
5530                (gnus-tmp-article-number (or gnus-current-article 0))
5531                (gnus-tmp-unread gnus-newsgroup-unreads)
5532                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5533                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5534                (gnus-tmp-unread-and-unselected
5535                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5536                             (zerop gnus-tmp-unselected))
5537                        "")
5538                       ((zerop gnus-tmp-unselected)
5539                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5540                       (t (format "{%d(+%d) more}"
5541                                  gnus-tmp-unread-and-unticked
5542                                  gnus-tmp-unselected))))
5543                (gnus-tmp-subject
5544                 (if (and gnus-current-headers
5545                          (vectorp gnus-current-headers))
5546                     (gnus-mode-string-quote
5547                      (mail-header-subject gnus-current-headers))
5548                   ""))
5549                bufname-length max-len
5550                gnus-tmp-header);; passed as argument to any user-format-funcs
5551           (setq mode-string (eval mformat))
5552           (setq bufname-length (if (string-match "%b" mode-string)
5553                                    (- (length
5554                                        (buffer-name
5555                                         (if (eq where 'summary)
5556                                             nil
5557                                           (get-buffer gnus-article-buffer))))
5558                                       2)
5559                                  0))
5560           (setq max-len (max 4 (if gnus-mode-non-string-length
5561                                    (- (window-width)
5562                                       gnus-mode-non-string-length
5563                                       bufname-length)
5564                                  (length mode-string))))
5565           ;; We might have to chop a bit of the string off...
5566           (when (> (length mode-string) max-len)
5567             (setq mode-string
5568                   (concat (gnus-truncate-string mode-string (- max-len 3))
5569                           "...")))
5570           ;; Pad the mode string a bit.
5571           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5572       ;; Update the mode line.
5573       (setq mode-line-buffer-identification
5574             (gnus-mode-line-buffer-identification (list mode-string)))
5575       (set-buffer-modified-p t))))
5576
5577 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5578   "Go through the HEADERS list and add all Xrefs to a hash table.
5579 The resulting hash table is returned, or nil if no Xrefs were found."
5580   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5581          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5582          (xref-hashtb (gnus-make-hashtable))
5583          start group entry number xrefs header)
5584     (while headers
5585       (setq header (pop headers))
5586       (when (and (setq xrefs (mail-header-xref header))
5587                  (not (memq (setq number (mail-header-number header))
5588                             unreads)))
5589         (setq start 0)
5590         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5591           (setq start (match-end 0))
5592           (setq group (if prefix
5593                           (concat prefix (substring xrefs (match-beginning 1)
5594                                                     (match-end 1)))
5595                         (substring xrefs (match-beginning 1) (match-end 1))))
5596           (setq number
5597                 (string-to-int (substring xrefs (match-beginning 2)
5598                                           (match-end 2))))
5599           (if (setq entry (gnus-gethash group xref-hashtb))
5600               (setcdr entry (cons number (cdr entry)))
5601             (gnus-sethash group (cons number nil) xref-hashtb)))))
5602     (and start xref-hashtb)))
5603
5604 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5605   "Look through all the headers and mark the Xrefs as read."
5606   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5607         name info xref-hashtb idlist method nth4)
5608     (save-excursion
5609       (set-buffer gnus-group-buffer)
5610       (when (setq xref-hashtb
5611                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5612         (mapatoms
5613          (lambda (group)
5614            (unless (string= from-newsgroup (setq name (symbol-name group)))
5615              (setq idlist (symbol-value group))
5616              ;; Dead groups are not updated.
5617              (and (prog1
5618                       (setq info (gnus-get-info name))
5619                     (when (stringp (setq nth4 (gnus-info-method info)))
5620                       (setq nth4 (gnus-server-to-method nth4))))
5621                   ;; Only do the xrefs if the group has the same
5622                   ;; select method as the group we have just read.
5623                   (or (gnus-methods-equal-p
5624                        nth4 (gnus-find-method-for-group from-newsgroup))
5625                       virtual
5626                       (equal nth4 (setq method (gnus-find-method-for-group
5627                                                 from-newsgroup)))
5628                       (and (equal (car nth4) (car method))
5629                            (equal (nth 1 nth4) (nth 1 method))))
5630                   gnus-use-cross-reference
5631                   (or (not (eq gnus-use-cross-reference t))
5632                       virtual
5633                       ;; Only do cross-references on subscribed
5634                       ;; groups, if that is what is wanted.
5635                       (<= (gnus-info-level info) gnus-level-subscribed))
5636                   (gnus-group-make-articles-read name idlist))))
5637          xref-hashtb)))))
5638
5639 (defun gnus-compute-read-articles (group articles)
5640   (let* ((entry (gnus-group-entry group))
5641          (info (nth 2 entry))
5642          (active (gnus-active group))
5643          ninfo)
5644     (when entry
5645       ;; First peel off all invalid article numbers.
5646       (when active
5647         (let ((ids articles)
5648               id first)
5649           (while (setq id (pop ids))
5650             (when (and first (> id (cdr active)))
5651               ;; We'll end up in this situation in one particular
5652               ;; obscure situation.  If you re-scan a group and get
5653               ;; a new article that is cross-posted to a different
5654               ;; group that has not been re-scanned, you might get
5655               ;; crossposted article that has a higher number than
5656               ;; Gnus believes possible.  So we re-activate this
5657               ;; group as well.  This might mean doing the
5658               ;; crossposting thingy will *increase* the number
5659               ;; of articles in some groups.  Tsk, tsk.
5660               (setq active (or (gnus-activate-group group) active)))
5661             (when (or (> id (cdr active))
5662                       (< id (car active)))
5663               (setq articles (delq id articles))))))
5664       ;; If the read list is nil, we init it.
5665       (if (and active
5666                (null (gnus-info-read info))
5667                (> (car active) 1))
5668           (setq ninfo (cons 1 (1- (car active))))
5669         (setq ninfo (gnus-info-read info)))
5670       ;; Then we add the read articles to the range.
5671       (gnus-add-to-range
5672        ninfo (setq articles (sort articles '<))))))
5673
5674 (defun gnus-group-make-articles-read (group articles)
5675   "Update the info of GROUP to say that ARTICLES are read."
5676   (let* ((num 0)
5677          (entry (gnus-group-entry group))
5678          (info (nth 2 entry))
5679          (active (gnus-active group))
5680          range)
5681     (when entry
5682       (setq range (gnus-compute-read-articles group articles))
5683       (with-current-buffer gnus-group-buffer
5684         (gnus-undo-register
5685           `(progn
5686              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5687              (gnus-info-set-read ',info ',(gnus-info-read info))
5688              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5689              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5690              (gnus-group-update-group ,group t))))
5691       ;; Add the read articles to the range.
5692       (gnus-info-set-read info range)
5693       (gnus-request-set-mark group (list (list range 'add '(read))))
5694       ;; Then we have to re-compute how many unread
5695       ;; articles there are in this group.
5696       (when active
5697         (cond
5698          ((not range)
5699           (setq num (- (1+ (cdr active)) (car active))))
5700          ((not (listp (cdr range)))
5701           (setq num (- (cdr active) (- (1+ (cdr range))
5702                                        (car range)))))
5703          (t
5704           (while range
5705             (if (numberp (car range))
5706                 (setq num (1+ num))
5707               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5708             (setq range (cdr range)))
5709           (setq num (- (cdr active) num))))
5710         ;; Update the number of unread articles.
5711         (setcar entry num)
5712         ;; Update the group buffer.
5713         (unless (gnus-ephemeral-group-p group)
5714           (gnus-group-update-group group t))))))
5715
5716 (defvar gnus-newsgroup-none-id 0)
5717
5718 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5719   (let ((cur nntp-server-buffer)
5720         (dependencies
5721          (or dependencies
5722              (save-excursion (set-buffer gnus-summary-buffer)
5723                              gnus-newsgroup-dependencies)))
5724         headers id end ref
5725         (mail-parse-charset gnus-newsgroup-charset)
5726         (mail-parse-ignored-charsets
5727          (save-excursion (condition-case nil
5728                              (set-buffer gnus-summary-buffer)
5729                            (error))
5730                          gnus-newsgroup-ignored-charsets)))
5731     (save-excursion
5732       (set-buffer nntp-server-buffer)
5733       ;; Translate all TAB characters into SPACE characters.
5734       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5735       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5736       (ietf-drums-unfold-fws)
5737       (gnus-run-hooks 'gnus-parse-headers-hook)
5738       (let ((case-fold-search t)
5739             in-reply-to header p lines chars ctype)
5740         (goto-char (point-min))
5741         ;; Search to the beginning of the next header.  Error messages
5742         ;; do not begin with 2 or 3.
5743         (while (re-search-forward "^[23][0-9]+ " nil t)
5744           (setq id nil
5745                 ref nil)
5746           ;; This implementation of this function, with nine
5747           ;; search-forwards instead of the one re-search-forward and
5748           ;; a case (which basically was the old function) is actually
5749           ;; about twice as fast, even though it looks messier.  You
5750           ;; can't have everything, I guess.  Speed and elegance
5751           ;; doesn't always go hand in hand.
5752           (setq
5753            header
5754            (make-full-mail-header
5755             ;; Number.
5756             (prog1
5757                 (read cur)
5758               (end-of-line)
5759               (setq p (point))
5760               (narrow-to-region (point)
5761                                 (or (and (search-forward "\n.\n" nil t)
5762                                          (- (point) 2))
5763                                     (point))))
5764             ;; Subject.
5765             (progn
5766               (goto-char p)
5767               (if (search-forward "\nsubject:" nil t)
5768                   (nnheader-header-value)
5769                 "(none)"))
5770             ;; From.
5771             (progn
5772               (goto-char p)
5773               (if (search-forward "\nfrom:" nil t)
5774                   (nnheader-header-value)
5775                 "(nobody)"))
5776             ;; Date.
5777             (progn
5778               (goto-char p)
5779               (if (search-forward "\ndate:" nil t)
5780                   (nnheader-header-value) ""))
5781             ;; Message-ID.
5782             (progn
5783               (goto-char p)
5784               (setq id (if (re-search-forward
5785                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5786                            ;; We do it this way to make sure the Message-ID
5787                            ;; is (somewhat) syntactically valid.
5788                            (buffer-substring (match-beginning 1)
5789                                              (match-end 1))
5790                          ;; If there was no message-id, we just fake one
5791                          ;; to make subsequent routines simpler.
5792                          (nnheader-generate-fake-message-id))))
5793             ;; References.
5794             (progn
5795               (goto-char p)
5796               (if (search-forward "\nreferences:" nil t)
5797                   (progn
5798                     (setq end (point))
5799                     (prog1
5800                         (nnheader-header-value)
5801                       (setq ref
5802                             (buffer-substring
5803                              (progn
5804                                ;; (end-of-line)
5805                                (search-backward ">" end t)
5806                                (1+ (point)))
5807                              (progn
5808                                (search-backward "<" end t)
5809                                (point))))))
5810                 ;; Get the references from the in-reply-to header if there
5811                 ;; were no references and the in-reply-to header looks
5812                 ;; promising.
5813                 (if (and (search-forward "\nin-reply-to:" nil t)
5814                          (setq in-reply-to (nnheader-header-value))
5815                          (string-match "<[^>]+>" in-reply-to))
5816                     (let (ref2)
5817                       (setq ref (substring in-reply-to (match-beginning 0)
5818                                            (match-end 0)))
5819                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5820                         (setq ref2 (substring in-reply-to (match-beginning 0)
5821                                               (match-end 0)))
5822                         (when (> (length ref2) (length ref))
5823                           (setq ref ref2)))
5824                       ref)
5825                   (setq ref nil))))
5826             ;; Chars.
5827             (progn
5828               (goto-char p)
5829               (if (search-forward "\nchars: " nil t)
5830                   (if (numberp (setq chars (ignore-errors (read cur))))
5831                       chars -1)
5832                 -1))
5833             ;; Lines.
5834             (progn
5835               (goto-char p)
5836               (if (search-forward "\nlines: " nil t)
5837                   (if (numberp (setq lines (ignore-errors (read cur))))
5838                       lines -1)
5839                 -1))
5840             ;; Xref.
5841             (progn
5842               (goto-char p)
5843               (and (search-forward "\nxref:" nil t)
5844                    (nnheader-header-value)))
5845             ;; Extra.
5846             (when gnus-extra-headers
5847               (let ((extra gnus-extra-headers)
5848                     out)
5849                 (while extra
5850                   (goto-char p)
5851                   (when (search-forward
5852                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5853                     (push (cons (car extra) (nnheader-header-value)) out))
5854                   (pop extra))
5855                 out))))
5856           (goto-char p)
5857           (if (and (search-forward "\ncontent-type: " nil t)
5858                    (setq ctype (nnheader-header-value)))
5859               (mime-entity-set-content-type-internal
5860                header (mime-parse-Content-Type ctype)))
5861           (when (equal id ref)
5862             (setq ref nil))
5863
5864           (when gnus-alter-header-function
5865             (funcall gnus-alter-header-function header)
5866             (setq id (mail-header-id header)
5867                   ref (gnus-parent-id (mail-header-references header))))
5868
5869           (when (setq header
5870                       (gnus-dependencies-add-header
5871                        header dependencies force-new))
5872             (push header headers))
5873           (goto-char (point-max))
5874           (widen))
5875         (nreverse headers)))))
5876
5877 ;; Goes through the xover lines and returns a list of vectors
5878 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5879                                                   force-new dependencies
5880                                                   group also-fetch-heads)
5881   "Parse the news overview data in the server buffer.
5882 Return a list of headers that match SEQUENCE (see
5883 `nntp-retrieve-headers')."
5884   ;; Get the Xref when the users reads the articles since most/some
5885   ;; NNTP servers do not include Xrefs when using XOVER.
5886   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5887   (let ((mail-parse-charset gnus-newsgroup-charset)
5888         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5889         (cur nntp-server-buffer)
5890         (dependencies (or dependencies gnus-newsgroup-dependencies))
5891         (allp (cond
5892                ((eq gnus-read-all-available-headers t)
5893                 t)
5894                ((stringp gnus-read-all-available-headers)
5895                 (string-match gnus-read-all-available-headers group))
5896                (t
5897                 nil)))
5898         number headers header)
5899     (save-excursion
5900       (set-buffer nntp-server-buffer)
5901       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5902       ;; Allow the user to mangle the headers before parsing them.
5903       (gnus-run-hooks 'gnus-parse-headers-hook)
5904       (goto-char (point-min))
5905       (gnus-parse-without-error
5906         (while (and (or sequence allp)
5907                     (not (eobp)))
5908           (setq number (read cur))
5909           (when (not allp)
5910             (while (and sequence
5911                         (< (car sequence) number))
5912               (setq sequence (cdr sequence))))
5913           (when (and (or allp
5914                          (and sequence
5915                               (eq number (car sequence))))
5916                      (progn
5917                        (setq sequence (cdr sequence))
5918                        (setq header (inline
5919                                       (gnus-nov-parse-line
5920                                        number dependencies force-new)))))
5921             (push header headers))
5922           (forward-line 1)))
5923       ;; A common bug in inn is that if you have posted an article and
5924       ;; then retrieves the active file, it will answer correctly --
5925       ;; the new article is included.  However, a NOV entry for the
5926       ;; article may not have been generated yet, so this may fail.
5927       ;; We work around this problem by retrieving the last few
5928       ;; headers using HEAD.
5929       (if (or (not also-fetch-heads)
5930               (not sequence))
5931           ;; We (probably) got all the headers.
5932           (nreverse headers)
5933         (let ((gnus-nov-is-evil t))
5934           (nconc
5935            (nreverse headers)
5936            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5937              (gnus-get-newsgroup-headers))))))))
5938
5939 (defun gnus-article-get-xrefs ()
5940   "Fill in the Xref value in `gnus-current-headers', if necessary.
5941 This is meant to be called in `gnus-article-internal-prepare-hook'."
5942   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5943                                  gnus-current-headers)))
5944     (or (not gnus-use-cross-reference)
5945         (not headers)
5946         (and (mail-header-xref headers)
5947              (not (string= (mail-header-xref headers) "")))
5948         (let ((case-fold-search t)
5949               xref)
5950           (save-restriction
5951             (nnheader-narrow-to-headers)
5952             (goto-char (point-min))
5953             (when (or (and (not (eobp))
5954                            (eq (downcase (char-after)) ?x)
5955                            (looking-at "Xref:"))
5956                       (search-forward "\nXref:" nil t))
5957               (goto-char (1+ (match-end 0)))
5958               (setq xref (buffer-substring (point) (point-at-eol)))
5959               (mail-header-set-xref headers xref)))))))
5960
5961 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5962   "Find article ID and insert the summary line for that article.
5963 OLD-HEADER can either be a header or a line number to insert
5964 the subject line on."
5965   (let* ((line (and (numberp old-header) old-header))
5966          (old-header (and (vectorp old-header) old-header))
5967          (header (cond ((and old-header use-old-header)
5968                         old-header)
5969                        ((and (numberp id)
5970                              (gnus-number-to-header id))
5971                         (gnus-number-to-header id))
5972                        (t
5973                         (gnus-read-header id))))
5974          (number (and (numberp id) id))
5975          d)
5976     (when header
5977       ;; Rebuild the thread that this article is part of and go to the
5978       ;; article we have fetched.
5979       (when (and (not gnus-show-threads)
5980                  old-header)
5981         (when (and number
5982                    (setq d (gnus-data-find (mail-header-number old-header))))
5983           (goto-char (gnus-data-pos d))
5984           (gnus-data-remove
5985            number
5986            (- (point-at-bol)
5987               (prog1
5988                   (1+ (point-at-eol))
5989                 (gnus-delete-line))))))
5990       (when old-header
5991         (mail-header-set-number header (mail-header-number old-header)))
5992       (setq gnus-newsgroup-sparse
5993             (delq (setq number (mail-header-number header))
5994                   gnus-newsgroup-sparse))
5995       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5996       (push number gnus-newsgroup-limit)
5997       (gnus-rebuild-thread (mail-header-id header) line)
5998       (gnus-summary-goto-subject number nil t))
5999     (when (and (numberp number)
6000                (> number 0))
6001       ;; We have to update the boundaries even if we can't fetch the
6002       ;; article if ID is a number -- so that the next `P' or `N'
6003       ;; command will fetch the previous (or next) article even
6004       ;; if the one we tried to fetch this time has been canceled.
6005       (when (> number gnus-newsgroup-end)
6006         (setq gnus-newsgroup-end number))
6007       (when (< number gnus-newsgroup-begin)
6008         (setq gnus-newsgroup-begin number))
6009       (setq gnus-newsgroup-unselected
6010             (delq number gnus-newsgroup-unselected)))
6011     ;; Report back a success?
6012     (and header (mail-header-number header))))
6013
6014 ;;; Process/prefix in the summary buffer
6015
6016 (defun gnus-summary-work-articles (n)
6017   "Return a list of articles to be worked upon.
6018 The prefix argument, the list of process marked articles, and the
6019 current article will be taken into consideration."
6020   (save-excursion
6021     (set-buffer gnus-summary-buffer)
6022     (cond
6023      (n
6024       ;; A numerical prefix has been given.
6025       (setq n (prefix-numeric-value n))
6026       (let ((backward (< n 0))
6027             (n (abs (prefix-numeric-value n)))
6028             articles article)
6029         (save-excursion
6030           (while
6031               (and (> n 0)
6032                    (push (setq article (gnus-summary-article-number))
6033                          articles)
6034                    (if backward
6035                        (gnus-summary-find-prev nil article)
6036                      (gnus-summary-find-next nil article)))
6037             (decf n)))
6038         (nreverse articles)))
6039      ((and (gnus-region-active-p) (mark))
6040       (message "region active")
6041       ;; Work on the region between point and mark.
6042       (let ((max (max (point) (mark)))
6043             articles article)
6044         (save-excursion
6045           (goto-char (min (point) (mark)))
6046           (while
6047               (and
6048                (push (setq article (gnus-summary-article-number)) articles)
6049                (gnus-summary-find-next nil article)
6050                (< (point) max)))
6051           (nreverse articles))))
6052      (gnus-newsgroup-processable
6053       ;; There are process-marked articles present.
6054       ;; Save current state.
6055       (gnus-summary-save-process-mark)
6056       ;; Return the list.
6057       (reverse gnus-newsgroup-processable))
6058      (t
6059       ;; Just return the current article.
6060       (list (gnus-summary-article-number))))))
6061
6062 (defmacro gnus-summary-iterate (arg &rest forms)
6063   "Iterate over the process/prefixed articles and do FORMS.
6064 ARG is the interactive prefix given to the command.  FORMS will be
6065 executed with point over the summary line of the articles."
6066   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
6067     `(let ((,articles (gnus-summary-work-articles ,arg)))
6068        (while ,articles
6069          (gnus-summary-goto-subject (car ,articles))
6070          ,@forms
6071          (pop ,articles)))))
6072
6073 (put 'gnus-summary-iterate 'lisp-indent-function 1)
6074 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
6075
6076 (defun gnus-summary-save-process-mark ()
6077   "Push the current set of process marked articles on the stack."
6078   (interactive)
6079   (push (copy-sequence gnus-newsgroup-processable)
6080         gnus-newsgroup-process-stack))
6081
6082 (defun gnus-summary-kill-process-mark ()
6083   "Push the current set of process marked articles on the stack and unmark."
6084   (interactive)
6085   (gnus-summary-save-process-mark)
6086   (gnus-summary-unmark-all-processable))
6087
6088 (defun gnus-summary-yank-process-mark ()
6089   "Pop the last process mark state off the stack and restore it."
6090   (interactive)
6091   (unless gnus-newsgroup-process-stack
6092     (error "Empty mark stack"))
6093   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
6094
6095 (defun gnus-summary-process-mark-set (set)
6096   "Make SET into the current process marked articles."
6097   (gnus-summary-unmark-all-processable)
6098   (while set
6099     (gnus-summary-set-process-mark (pop set))))
6100
6101 ;;; Searching and stuff
6102
6103 (defun gnus-summary-search-group (&optional backward use-level)
6104   "Search for next unread newsgroup.
6105 If optional argument BACKWARD is non-nil, search backward instead."
6106   (save-excursion
6107     (set-buffer gnus-group-buffer)
6108     (when (gnus-group-search-forward
6109            backward nil (if use-level (gnus-group-group-level) nil))
6110       (gnus-group-group-name))))
6111
6112 (defun gnus-summary-best-group (&optional exclude-group)
6113   "Find the name of the best unread group.
6114 If EXCLUDE-GROUP, do not go to this group."
6115   (save-excursion
6116     (set-buffer gnus-group-buffer)
6117     (save-excursion
6118       (gnus-group-best-unread-group exclude-group))))
6119
6120 (defun gnus-summary-find-next (&optional unread article backward)
6121   (if backward
6122       (gnus-summary-find-prev unread article)
6123     (let* ((dummy (gnus-summary-article-intangible-p))
6124            (article (or article (gnus-summary-article-number)))
6125            (data (gnus-data-find-list article))
6126            result)
6127       (when (and (not dummy)
6128                  (or (not gnus-summary-check-current)
6129                      (not unread)
6130                      (not (gnus-data-unread-p (car data)))))
6131         (setq data (cdr data)))
6132       (when (setq result
6133                   (if unread
6134                       (progn
6135                         (while data
6136                           (unless (memq (gnus-data-number (car data))
6137                                         (cond
6138                                          ((eq gnus-auto-goto-ignores
6139                                               'always-undownloaded)
6140                                           gnus-newsgroup-undownloaded)
6141                                          (gnus-plugged
6142                                           nil)
6143                                          ((eq gnus-auto-goto-ignores
6144                                               'unfetched)
6145                                           gnus-newsgroup-unfetched)
6146                                          ((eq gnus-auto-goto-ignores
6147                                               'undownloaded)
6148                                           gnus-newsgroup-undownloaded)))
6149                             (when (gnus-data-unread-p (car data))
6150                               (setq result (car data)
6151                                     data nil)))
6152                           (setq data (cdr data)))
6153                         result)
6154                     (car data)))
6155         (goto-char (gnus-data-pos result))
6156         (gnus-data-number result)))))
6157
6158 (defun gnus-summary-find-prev (&optional unread article)
6159   (let* ((eobp (eobp))
6160          (article (or article (gnus-summary-article-number)))
6161          (data (gnus-data-find-list article (gnus-data-list 'rev)))
6162          result)
6163     (when (and (not eobp)
6164                (or (not gnus-summary-check-current)
6165                    (not unread)
6166                    (not (gnus-data-unread-p (car data)))))
6167       (setq data (cdr data)))
6168     (when (setq result
6169                 (if unread
6170                     (progn
6171                       (while data
6172                         (unless (memq (gnus-data-number (car data))
6173                                       (cond
6174                                        ((eq gnus-auto-goto-ignores
6175                                             'always-undownloaded)
6176                                         gnus-newsgroup-undownloaded)
6177                                        (gnus-plugged
6178                                         nil)
6179                                        ((eq gnus-auto-goto-ignores
6180                                             'unfetched)
6181                                         gnus-newsgroup-unfetched)
6182                                        ((eq gnus-auto-goto-ignores
6183                                             'undownloaded)
6184                                         gnus-newsgroup-undownloaded)))
6185                           (when (gnus-data-unread-p (car data))
6186                             (setq result (car data)
6187                                   data nil)))
6188                         (setq data (cdr data)))
6189                       result)
6190                   (car data)))
6191       (goto-char (gnus-data-pos result))
6192       (gnus-data-number result))))
6193
6194 (defun gnus-summary-find-subject (subject &optional unread backward article)
6195   (let* ((simp-subject (gnus-simplify-subject-fully subject))
6196          (article (or article (gnus-summary-article-number)))
6197          (articles (gnus-data-list backward))
6198          (arts (gnus-data-find-list article articles))
6199          result)
6200     (when (or (not gnus-summary-check-current)
6201               (not unread)
6202               (not (gnus-data-unread-p (car arts))))
6203       (setq arts (cdr arts)))
6204     (while arts
6205       (and (or (not unread)
6206                (gnus-data-unread-p (car arts)))
6207            (vectorp (gnus-data-header (car arts)))
6208            (gnus-subject-equal
6209             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
6210            (setq result (car arts)
6211                  arts nil))
6212       (setq arts (cdr arts)))
6213     (and result
6214          (goto-char (gnus-data-pos result))
6215          (gnus-data-number result))))
6216
6217 (defun gnus-summary-search-forward (&optional unread subject backward)
6218   "Search forward for an article.
6219 If UNREAD, look for unread articles.  If SUBJECT, look for
6220 articles with that subject.  If BACKWARD, search backward instead."
6221   (cond (subject (gnus-summary-find-subject subject unread backward))
6222         (backward (gnus-summary-find-prev unread))
6223         (t (gnus-summary-find-next unread))))
6224
6225 (defun gnus-recenter (&optional n)
6226   "Center point in window and redisplay frame.
6227 Also do horizontal recentering."
6228   (interactive "P")
6229   (when (and gnus-auto-center-summary
6230              (not (eq gnus-auto-center-summary 'vertical)))
6231     (gnus-horizontal-recenter))
6232   (recenter n))
6233
6234 (defun gnus-summary-recenter ()
6235   "Center point in the summary window.
6236 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6237 displayed, no centering will be performed."
6238   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6239   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6240   (interactive)
6241   ;; The user has to want it.
6242   (when gnus-auto-center-summary
6243     (let* ((top (cond ((< (window-height) 4) 0)
6244                       ((< (window-height) 7) 1)
6245                       (t (if (numberp gnus-auto-center-summary)
6246                              gnus-auto-center-summary
6247                            2))))
6248            (height (1- (window-height)))
6249            (bottom (save-excursion (goto-char (point-max))
6250                                    (forward-line (- height))
6251                                    (point)))
6252            (window (get-buffer-window (current-buffer))))
6253       (when (get-buffer-window gnus-article-buffer)
6254         ;; Only do recentering when the article buffer is displayed,
6255         ;; Set the window start to either `bottom', which is the biggest
6256         ;; possible valid number, or the second line from the top,
6257         ;; whichever is the least.
6258         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6259           (if (> bottom top-pos)
6260               ;; Keep the second line from the top visible
6261               (set-window-start window top-pos t)
6262             ;; Try to keep the bottom line visible; if it's partially
6263             ;; obscured, either scroll one more line to make it fully
6264             ;; visible, or revert to using TOP-POS.
6265             (save-excursion
6266               (goto-char (point-max))
6267               (forward-line -1)
6268               (let ((last-line-start (point)))
6269                 (goto-char bottom)
6270                 (set-window-start window (point) t)
6271                 (when (not (pos-visible-in-window-p last-line-start window))
6272                   (forward-line 1)
6273                   (set-window-start window (min (point) top-pos) t)))))))
6274       ;; Do horizontal recentering while we're at it.
6275       (when (and (get-buffer-window (current-buffer) t)
6276                  (not (eq gnus-auto-center-summary 'vertical)))
6277         (let ((selected (selected-window)))
6278           (select-window (get-buffer-window (current-buffer) t))
6279           (gnus-summary-position-point)
6280           (gnus-horizontal-recenter)
6281           (select-window selected))))))
6282
6283 (defun gnus-summary-jump-to-group (newsgroup)
6284   "Move point to NEWSGROUP in group mode buffer."
6285   ;; Keep update point of group mode buffer if visible.
6286   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6287       (save-window-excursion
6288         ;; Take care of tree window mode.
6289         (when (get-buffer-window gnus-group-buffer)
6290           (pop-to-buffer gnus-group-buffer))
6291         (gnus-group-jump-to-group newsgroup))
6292     (save-excursion
6293       ;; Take care of tree window mode.
6294       (if (get-buffer-window gnus-group-buffer)
6295           (pop-to-buffer gnus-group-buffer)
6296         (set-buffer gnus-group-buffer))
6297       (gnus-group-jump-to-group newsgroup))))
6298
6299 ;; This function returns a list of article numbers based on the
6300 ;; difference between the ranges of read articles in this group and
6301 ;; the range of active articles.
6302 (defun gnus-list-of-unread-articles (group)
6303   (let* ((read (gnus-info-read (gnus-get-info group)))
6304          (active (or (gnus-active group) (gnus-activate-group group)))
6305          (last (cdr active))
6306          first nlast unread)
6307     ;; If none are read, then all are unread.
6308     (if (not read)
6309         (setq first (car active))
6310       ;; If the range of read articles is a single range, then the
6311       ;; first unread article is the article after the last read
6312       ;; article.  Sounds logical, doesn't it?
6313       (if (and (not (listp (cdr read)))
6314                (or (< (car read) (car active))
6315                    (progn (setq read (list read))
6316                           nil)))
6317           (setq first (max (car active) (1+ (cdr read))))
6318         ;; `read' is a list of ranges.
6319         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6320                                   (caar read)))
6321                   1)
6322           (setq first (car active)))
6323         (while read
6324           (when first
6325             (while (< first nlast)
6326               (setq unread (cons first unread)
6327                     first (1+ first))))
6328           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6329           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6330           (setq read (cdr read)))))
6331     ;; And add the last unread articles.
6332     (while (<= first last)
6333       (setq unread (cons first unread)
6334             first (1+ first)))
6335     ;; Return the list of unread articles.
6336     (delq 0 (nreverse unread))))
6337
6338 (defun gnus-list-of-read-articles (group)
6339   "Return a list of unread, unticked and non-dormant articles."
6340   (let* ((info (gnus-get-info group))
6341          (marked (gnus-info-marks info))
6342          (active (gnus-active group)))
6343     (and info active
6344          (gnus-list-range-difference
6345           (gnus-list-range-difference
6346            (gnus-sorted-complement
6347             (gnus-uncompress-range active)
6348             (gnus-list-of-unread-articles group))
6349            (cdr (assq 'dormant marked)))
6350           (cdr (assq 'tick marked))))))
6351
6352 ;; This function returns a sequence of article numbers based on the
6353 ;; difference between the ranges of read articles in this group and
6354 ;; the range of active articles.
6355 (defun gnus-sequence-of-unread-articles (group)
6356   (let* ((read (gnus-info-read (gnus-get-info group)))
6357          (active (or (gnus-active group) (gnus-activate-group group)))
6358          (last (cdr active))
6359          first nlast unread)
6360     ;; If none are read, then all are unread.
6361     (if (not read)
6362         (setq first (car active))
6363       ;; If the range of read articles is a single range, then the
6364       ;; first unread article is the article after the last read
6365       ;; article.  Sounds logical, doesn't it?
6366       (if (and (not (listp (cdr read)))
6367                (or (< (car read) (car active))
6368                    (progn (setq read (list read))
6369                           nil)))
6370           (setq first (max (car active) (1+ (cdr read))))
6371         ;; `read' is a list of ranges.
6372         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6373                                   (caar read)))
6374                   1)
6375           (setq first (car active)))
6376         (while read
6377           (when first
6378             (push (cons first nlast) unread))
6379           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6380           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6381           (setq read (cdr read)))))
6382     ;; And add the last unread articles.
6383     (cond ((< first last)
6384            (push (cons first last) unread))
6385           ((= first last)
6386            (push first unread)))
6387     ;; Return the sequence of unread articles.
6388     (delq 0 (nreverse unread))))
6389
6390 ;; Various summary commands
6391
6392 (defun gnus-summary-select-article-buffer ()
6393   "Reconfigure windows to show article buffer."
6394   (interactive)
6395   (if (not (gnus-buffer-live-p gnus-article-buffer))
6396       (error "There is no article buffer for this summary buffer")
6397     (gnus-configure-windows 'article)
6398     (select-window (get-buffer-window gnus-article-buffer))))
6399
6400 (defun gnus-summary-universal-argument (arg)
6401   "Perform any operation on all articles that are process/prefixed."
6402   (interactive "P")
6403   (let ((articles (gnus-summary-work-articles arg))
6404         func article)
6405     (if (eq
6406          (setq
6407           func
6408           (key-binding
6409            (read-key-sequence
6410             (substitute-command-keys
6411              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6412          'undefined)
6413         (gnus-error 1 "Undefined key")
6414       (save-excursion
6415         (while articles
6416           (gnus-summary-goto-subject (setq article (pop articles)))
6417           (let (gnus-newsgroup-processable)
6418             (command-execute func))
6419           (gnus-summary-remove-process-mark article)))))
6420   (gnus-summary-position-point))
6421
6422 (defun gnus-summary-toggle-truncation (&optional arg)
6423   "Toggle truncation of summary lines.
6424 With ARG, turn line truncation on if ARG is positive."
6425   (interactive "P")
6426   (setq truncate-lines
6427         (if (null arg) (not truncate-lines)
6428           (> (prefix-numeric-value arg) 0)))
6429   (redraw-display))
6430
6431 (defun gnus-summary-find-for-reselect ()
6432   "Return the number of an article to stay on across a reselect.
6433 The current article is considered, then following articles, then previous
6434 articles.  An article is sought which is not cancelled and isn't a temporary
6435 insertion from another group.  If there's no such then return a dummy 0."
6436   (let (found)
6437     (dolist (rev '(nil t))
6438       (unless found      ; don't demand the reverse list if we don't need it
6439         (let ((data (gnus-data-find-list
6440                      (gnus-summary-article-number) (gnus-data-list rev))))
6441           (while (and data (not found))
6442             (if (and (< 0 (gnus-data-number (car data)))
6443                      (not (eq gnus-canceled-mark (gnus-data-mark (car data)))))
6444                 (setq found (gnus-data-number (car data))))
6445             (setq data (cdr data))))))
6446     (or found 0)))
6447
6448 (defun gnus-summary-reselect-current-group (&optional all rescan)
6449   "Exit and then reselect the current newsgroup.
6450 The prefix argument ALL means to select all articles."
6451   (interactive "P")
6452   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6453     (error "Ephemeral groups can't be reselected"))
6454   (let ((current-subject (gnus-summary-find-for-reselect))
6455         (group gnus-newsgroup-name))
6456     (setq gnus-newsgroup-begin nil)
6457     (gnus-summary-exit nil 'leave-hidden)
6458     ;; We have to adjust the point of group mode buffer because
6459     ;; point was moved to the next unread newsgroup by exiting.
6460     (gnus-summary-jump-to-group group)
6461     (when rescan
6462       (save-excursion
6463         (save-window-excursion
6464           ;; Don't show group contents.
6465           (set-window-start (selected-window) (point-max))
6466           (gnus-group-get-new-news-this-group 1))))
6467     (gnus-group-read-group all t)
6468     (gnus-summary-goto-subject current-subject nil t)))
6469
6470 (defun gnus-summary-rescan-group (&optional all)
6471   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6472   (interactive "P")
6473   (gnus-summary-reselect-current-group all t))
6474
6475 (defun gnus-summary-update-info (&optional non-destructive)
6476   (save-excursion
6477     (let ((group gnus-newsgroup-name))
6478       (when group
6479         (when gnus-newsgroup-kill-headers
6480           (setq gnus-newsgroup-killed
6481                 (gnus-compress-sequence
6482                  (gnus-sorted-union
6483                   (gnus-list-range-intersection
6484                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6485                   gnus-newsgroup-unreads)
6486                  t)))
6487         (unless (listp (cdr gnus-newsgroup-killed))
6488           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6489         (let ((headers gnus-newsgroup-headers))
6490           ;; Set the new ranges of read articles.
6491           (save-excursion
6492             (set-buffer gnus-group-buffer)
6493             (gnus-undo-force-boundary))
6494           (gnus-update-read-articles
6495            group (gnus-sorted-union
6496                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6497           ;; Set the current article marks.
6498           (let ((gnus-newsgroup-scored
6499                  (if (and (not gnus-save-score)
6500                           (not non-destructive))
6501                      nil
6502                    gnus-newsgroup-scored)))
6503             (save-excursion
6504               (gnus-update-marks)))
6505           ;; Do the cross-ref thing.
6506           (when gnus-use-cross-reference
6507             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6508           ;; Do not switch windows but change the buffer to work.
6509           (set-buffer gnus-group-buffer)
6510           (unless (gnus-ephemeral-group-p group)
6511             (gnus-group-update-group group)))))))
6512
6513 (defun gnus-summary-save-newsrc (&optional force)
6514   "Save the current number of read/marked articles in the dribble buffer.
6515 The dribble buffer will then be saved.
6516 If FORCE (the prefix), also save the .newsrc file(s)."
6517   (interactive "P")
6518   (gnus-summary-update-info t)
6519   (if force
6520       (gnus-save-newsrc-file)
6521     (gnus-dribble-save)))
6522
6523 (defun gnus-summary-exit (&optional temporary leave-hidden)
6524   "Exit reading current newsgroup, and then return to group selection mode.
6525 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6526   (interactive)
6527   (gnus-set-global-variables)
6528   (gnus-kill-save-kill-buffer)
6529   (gnus-async-halt-prefetch)
6530   (let* ((group gnus-newsgroup-name)
6531          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6532          (gnus-group-is-exiting-p t)
6533          (mode major-mode)
6534          (group-point nil)
6535          (buf (current-buffer)))
6536     (unless quit-config
6537       ;; Do adaptive scoring, and possibly save score files.
6538       (when gnus-newsgroup-adaptive
6539         (gnus-score-adaptive))
6540       (when gnus-use-scoring
6541         (gnus-score-save)))
6542     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6543     ;; If we have several article buffers, we kill them at exit.
6544     (unless gnus-single-article-buffer
6545       (gnus-kill-buffer gnus-original-article-buffer)
6546       (setq gnus-article-current nil))
6547     (when gnus-use-cache
6548       (gnus-cache-possibly-remove-articles)
6549       (gnus-cache-save-buffers))
6550     (gnus-async-prefetch-remove-group group)
6551     (when gnus-suppress-duplicates
6552       (gnus-dup-enter-articles))
6553     (when gnus-use-trees
6554       (gnus-tree-close group))
6555     (when gnus-use-cache
6556       (gnus-cache-write-active))
6557     ;; Remove entries for this group.
6558     (nnmail-purge-split-history (gnus-group-real-name group))
6559     ;; Make all changes in this group permanent.
6560     (unless quit-config
6561       (gnus-run-hooks 'gnus-exit-group-hook)
6562       (gnus-summary-update-info))
6563     (gnus-close-group group)
6564     ;; Make sure where we were, and go to next newsgroup.
6565     (set-buffer gnus-group-buffer)
6566     (unless quit-config
6567       (gnus-group-jump-to-group group))
6568     (gnus-run-hooks 'gnus-summary-exit-hook)
6569     (unless (or quit-config
6570                 ;; If this group has disappeared from the summary
6571                 ;; buffer, don't skip forwards.
6572                 (not (string= group (gnus-group-group-name))))
6573       (gnus-group-next-unread-group 1))
6574     (setq group-point (point))
6575     (if temporary
6576         nil                             ;Nothing to do.
6577       ;; If we have several article buffers, we kill them at exit.
6578       (unless gnus-single-article-buffer
6579         (gnus-kill-buffer gnus-article-buffer)
6580         (gnus-kill-buffer gnus-original-article-buffer)
6581         (setq gnus-article-current nil))
6582       (set-buffer buf)
6583       (if (not gnus-kill-summary-on-exit)
6584           (progn
6585             (gnus-deaden-summary)
6586             (setq mode nil))
6587         ;; We set all buffer-local variables to nil.  It is unclear why
6588         ;; this is needed, but if we don't, buffer-local variables are
6589         ;; not garbage-collected, it seems.  This would the lead to en
6590         ;; ever-growing Emacs.
6591         (gnus-summary-clear-local-variables)
6592         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6593           (gnus-summary-clear-local-variables))
6594         (when (get-buffer gnus-article-buffer)
6595           (bury-buffer gnus-article-buffer))
6596         ;; We clear the global counterparts of the buffer-local
6597         ;; variables as well, just to be on the safe side.
6598         (set-buffer gnus-group-buffer)
6599         (gnus-summary-clear-local-variables)
6600         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6601           (gnus-summary-clear-local-variables))
6602         ;; Return to group mode buffer.
6603         (when (eq mode 'gnus-summary-mode)
6604           (gnus-kill-buffer buf)))
6605       (setq gnus-current-select-method gnus-select-method)
6606       (if leave-hidden
6607           (set-buffer gnus-group-buffer)
6608         (pop-to-buffer gnus-group-buffer))
6609       (if (not quit-config)
6610           (progn
6611             (goto-char group-point)
6612             (unless leave-hidden
6613               (gnus-configure-windows 'group 'force))
6614             (unless (pos-visible-in-window-p)
6615               (forward-line (/ (static-if (featurep 'xemacs)
6616                                    (window-displayed-height)
6617                                  (1- (window-height)))
6618                                -2))
6619               (set-window-start (selected-window) (point))
6620               (goto-char group-point)))
6621         (gnus-handle-ephemeral-exit quit-config))
6622       ;; Clear the current group name.
6623       (unless quit-config
6624         (setq gnus-newsgroup-name nil)))))
6625
6626 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6627 (defun gnus-summary-exit-no-update (&optional no-questions)
6628   "Quit reading current newsgroup without updating read article info."
6629   (interactive)
6630   (let* ((group gnus-newsgroup-name)
6631          (gnus-group-is-exiting-p t)
6632          (gnus-group-is-exiting-without-update-p t)
6633          (quit-config (gnus-group-quit-config group)))
6634     (when (or no-questions
6635               gnus-expert-user
6636               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6637       (gnus-async-halt-prefetch)
6638       (run-hooks 'gnus-summary-prepare-exit-hook)
6639       ;; If we have several article buffers, we kill them at exit.
6640       (unless gnus-single-article-buffer
6641         (gnus-kill-buffer gnus-article-buffer)
6642         (gnus-kill-buffer gnus-original-article-buffer)
6643         (setq gnus-article-current nil))
6644       (if (not gnus-kill-summary-on-exit)
6645           (gnus-deaden-summary)
6646         (gnus-close-group group)
6647         (gnus-summary-clear-local-variables)
6648         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6649           (gnus-summary-clear-local-variables))
6650         (set-buffer gnus-group-buffer)
6651         (gnus-summary-clear-local-variables)
6652         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6653           (gnus-summary-clear-local-variables))
6654         (gnus-kill-buffer gnus-summary-buffer))
6655       (unless gnus-single-article-buffer
6656         (setq gnus-article-current nil))
6657       (when gnus-use-trees
6658         (gnus-tree-close group))
6659       (gnus-async-prefetch-remove-group group)
6660       (when (get-buffer gnus-article-buffer)
6661         (bury-buffer gnus-article-buffer))
6662       ;; Return to the group buffer.
6663       (gnus-configure-windows 'group 'force)
6664       ;; Clear the current group name.
6665       (setq gnus-newsgroup-name nil)
6666       (unless (gnus-ephemeral-group-p group)
6667         (gnus-group-update-group group))
6668       (when (equal (gnus-group-group-name) group)
6669         (gnus-group-next-unread-group 1))
6670       (when quit-config
6671         (gnus-handle-ephemeral-exit quit-config)))))
6672
6673 (defun gnus-handle-ephemeral-exit (quit-config)
6674   "Handle movement when leaving an ephemeral group.
6675 The state which existed when entering the ephemeral is reset."
6676   (if (not (buffer-name (car quit-config)))
6677       (gnus-configure-windows 'group 'force)
6678     (set-buffer (car quit-config))
6679     (cond ((eq major-mode 'gnus-summary-mode)
6680            (gnus-set-global-variables))
6681           ((eq major-mode 'gnus-article-mode)
6682            (save-excursion
6683              ;; The `gnus-summary-buffer' variable may point
6684              ;; to the old summary buffer when using a single
6685              ;; article buffer.
6686              (unless (gnus-buffer-live-p gnus-summary-buffer)
6687                (set-buffer gnus-group-buffer))
6688              (set-buffer gnus-summary-buffer)
6689              (gnus-set-global-variables))))
6690     (if (or (eq (cdr quit-config) 'article)
6691             (eq (cdr quit-config) 'pick))
6692         (progn
6693           ;; The current article may be from the ephemeral group
6694           ;; thus it is best that we reload this article
6695           ;;
6696           ;; If we're exiting from a large digest, this can be
6697           ;; extremely slow.  So, it's better not to reload it. -- jh.
6698           ;;(gnus-summary-show-article)
6699           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6700               (gnus-configure-windows 'pick 'force)
6701             (gnus-configure-windows (cdr quit-config) 'force)))
6702       (gnus-configure-windows (cdr quit-config) 'force))
6703     (when (eq major-mode 'gnus-summary-mode)
6704       (gnus-summary-next-subject 1 nil t)
6705       (gnus-summary-recenter)
6706       (gnus-summary-position-point))))
6707
6708 (defun gnus-summary-preview-mime-message ()
6709   "MIME decode and play this message."
6710   (interactive)
6711   (let ((gnus-break-pages nil)
6712         (gnus-show-mime t))
6713     (gnus-summary-select-article gnus-show-all-headers t))
6714   (let ((w (get-buffer-window gnus-article-buffer)))
6715     (when w
6716       (select-window (get-buffer-window gnus-article-buffer)))))
6717
6718 ;;; Dead summaries.
6719
6720 (defvar gnus-dead-summary-mode-map nil)
6721
6722 (unless gnus-dead-summary-mode-map
6723   (setq gnus-dead-summary-mode-map (make-keymap))
6724   (suppress-keymap gnus-dead-summary-mode-map)
6725   (substitute-key-definition
6726    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6727   (dolist (key '("\C-d" "\r" "\177" [delete]))
6728     (define-key gnus-dead-summary-mode-map
6729       key 'gnus-summary-wake-up-the-dead))
6730   (dolist (key '("q" "Q"))
6731     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6732
6733 (defvar gnus-dead-summary-mode nil
6734   "Minor mode for Gnus summary buffers.")
6735
6736 (defun gnus-dead-summary-mode (&optional arg)
6737   "Minor mode for Gnus summary buffers."
6738   (interactive "P")
6739   (when (eq major-mode 'gnus-summary-mode)
6740     (make-local-variable 'gnus-dead-summary-mode)
6741     (setq gnus-dead-summary-mode
6742           (if (null arg) (not gnus-dead-summary-mode)
6743             (> (prefix-numeric-value arg) 0)))
6744     (when gnus-dead-summary-mode
6745       (add-minor-mode
6746        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6747
6748 (defun gnus-deaden-summary ()
6749   "Make the current summary buffer into a dead summary buffer."
6750   ;; Kill any previous dead summary buffer.
6751   (when (and gnus-dead-summary
6752              (buffer-name gnus-dead-summary))
6753     (save-excursion
6754       (set-buffer gnus-dead-summary)
6755       (when gnus-dead-summary-mode
6756         (kill-buffer (current-buffer)))))
6757   ;; Make this the current dead summary.
6758   (setq gnus-dead-summary (current-buffer))
6759   (gnus-dead-summary-mode 1)
6760   (let ((name (buffer-name)))
6761     (when (string-match "Summary" name)
6762       (rename-buffer
6763        (concat (substring name 0 (match-beginning 0)) "Dead "
6764                (substring name (match-beginning 0)))
6765        t)
6766       (bury-buffer))))
6767
6768 (defun gnus-kill-or-deaden-summary (buffer)
6769   "Kill or deaden the summary BUFFER."
6770   (save-excursion
6771     (when (and (buffer-name buffer)
6772                (not gnus-single-article-buffer))
6773       (save-excursion
6774         (set-buffer buffer)
6775         (gnus-kill-buffer gnus-article-buffer)
6776         (gnus-kill-buffer gnus-original-article-buffer)))
6777     (cond
6778      ;; Kill the buffer.
6779      (gnus-kill-summary-on-exit
6780       (when (and gnus-use-trees
6781                  (gnus-buffer-exists-p buffer))
6782         (save-excursion
6783           (set-buffer buffer)
6784           (gnus-tree-close gnus-newsgroup-name)))
6785       (gnus-kill-buffer buffer))
6786      ;; Deaden the buffer.
6787      ((gnus-buffer-exists-p buffer)
6788       (save-excursion
6789         (set-buffer buffer)
6790         (gnus-deaden-summary))))))
6791
6792 (defun gnus-summary-wake-up-the-dead (&rest args)
6793   "Wake up the dead summary buffer."
6794   (interactive)
6795   (gnus-dead-summary-mode -1)
6796   (let ((name (buffer-name)))
6797     (when (string-match "Dead " name)
6798       (rename-buffer
6799        (concat (substring name 0 (match-beginning 0))
6800                (substring name (match-end 0)))
6801        t)))
6802   (gnus-message 3 "This dead summary is now alive again"))
6803
6804 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6805 (defun gnus-summary-fetch-faq (&optional faq-dir)
6806   "Fetch the FAQ for the current group.
6807 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6808 in."
6809   (interactive
6810    (list
6811     (when current-prefix-arg
6812       (completing-read
6813        "FAQ dir: " (and (listp gnus-group-faq-directory)
6814                         (mapcar (lambda (file) (list file))
6815                                 gnus-group-faq-directory))))))
6816   (let (gnus-faq-buffer)
6817     (when (setq gnus-faq-buffer
6818                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6819       (gnus-configure-windows 'summary-faq))))
6820
6821 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6822 (defun gnus-summary-describe-group (&optional force)
6823   "Describe the current newsgroup."
6824   (interactive "P")
6825   (gnus-group-describe-group force gnus-newsgroup-name))
6826
6827 (defun gnus-summary-describe-briefly ()
6828   "Describe summary mode commands briefly."
6829   (interactive)
6830   (gnus-message 6 (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")))
6831
6832 ;; Walking around group mode buffer from summary mode.
6833
6834 (defun gnus-summary-next-group (&optional no-article target-group backward)
6835   "Exit current newsgroup and then select next unread newsgroup.
6836 If prefix argument NO-ARTICLE is non-nil, no article is selected
6837 initially.  If TARGET-GROUP, go to this group.  If BACKWARD, go to
6838 previous group instead."
6839   (interactive "P")
6840   ;; Stop pre-fetching.
6841   (gnus-async-halt-prefetch)
6842   (let ((current-group gnus-newsgroup-name)
6843         (current-buffer (current-buffer))
6844         entered)
6845     ;; First we semi-exit this group to update Xrefs and all variables.
6846     ;; We can't do a real exit, because the window conf must remain
6847     ;; the same in case the user is prompted for info, and we don't
6848     ;; want the window conf to change before that...
6849     (gnus-summary-exit t)
6850     (while (not entered)
6851       ;; Then we find what group we are supposed to enter.
6852       (set-buffer gnus-group-buffer)
6853       (gnus-group-jump-to-group current-group)
6854       (setq target-group
6855             (or target-group
6856                 (if (eq gnus-keep-same-level 'best)
6857                     (gnus-summary-best-group gnus-newsgroup-name)
6858                   (gnus-summary-search-group backward gnus-keep-same-level))))
6859       (if (not target-group)
6860           ;; There are no further groups, so we return to the group
6861           ;; buffer.
6862           (progn
6863             (gnus-message 5 "Returning to the group buffer")
6864             (setq entered t)
6865             (when (gnus-buffer-live-p current-buffer)
6866               (set-buffer current-buffer)
6867               (gnus-summary-exit))
6868             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6869         ;; We try to enter the target group.
6870         (gnus-group-jump-to-group target-group)
6871         (let ((unreads (gnus-group-group-unread)))
6872           (if (and (or (eq t unreads)
6873                        (and unreads (not (zerop unreads))))
6874                    (gnus-summary-read-group
6875                     target-group nil no-article
6876                     (and (buffer-name current-buffer) current-buffer)
6877                     nil backward))
6878               (setq entered t)
6879             (setq current-group target-group
6880                   target-group nil)))))))
6881
6882 (defun gnus-summary-prev-group (&optional no-article)
6883   "Exit current newsgroup and then select previous unread newsgroup.
6884 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6885   (interactive "P")
6886   (gnus-summary-next-group no-article nil t))
6887
6888 ;; Walking around summary lines.
6889
6890 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6891   "Go to the first subject satisfying any non-nil constraint.
6892 If UNREAD is non-nil, the article should be unread.
6893 If UNDOWNLOADED is non-nil, the article should be undownloaded.
6894 If UNSEEN is non-nil, the article should be unseen.
6895 Returns the article selected or nil if there are no matching articles."
6896   (interactive "P")
6897   (cond
6898    ;; Empty summary.
6899    ((null gnus-newsgroup-data)
6900     (gnus-message 3 "No articles in the group")
6901     nil)
6902    ;; Pick the first article.
6903    ((not (or unread undownloaded unseen))
6904     (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6905     (gnus-data-number (car gnus-newsgroup-data)))
6906    ;; Find the first unread article.
6907    (t
6908     (let ((data gnus-newsgroup-data))
6909       (while (and data
6910                   (let ((num (gnus-data-number (car data))))
6911                     (or (memq num gnus-newsgroup-unfetched)
6912                         (not (or (and unread
6913                                       (memq num gnus-newsgroup-unreads))
6914                                  (and undownloaded
6915                                       (memq num gnus-newsgroup-undownloaded))
6916                                  (and unseen
6917                                       (memq num gnus-newsgroup-unseen)))))))
6918         (setq data (cdr data)))
6919       (prog1
6920           (if data
6921               (progn
6922                 (goto-char (gnus-data-pos (car data)))
6923                 (gnus-data-number (car data)))
6924             (gnus-message 3 "No more%s articles"
6925                           (let* ((r (when unread " unread"))
6926                                  (d (when undownloaded " undownloaded"))
6927                                  (s (when unseen " unseen"))
6928                                  (l (delq nil (list r d s))))
6929                             (cond ((= 3 (length l))
6930                                    (concat r "," d ", or" s))
6931                                   ((= 2 (length l))
6932                                    (concat (car l) ", or" (cadr l)))
6933                                   ((= 1 (length l))
6934                                    (car l))
6935                                   (t
6936                                    ""))))
6937             nil
6938             )
6939         (gnus-summary-position-point))))))
6940
6941 (defun gnus-summary-next-subject (n &optional unread dont-display)
6942   "Go to next N'th summary line.
6943 If N is negative, go to the previous N'th subject line.
6944 If UNREAD is non-nil, only unread articles are selected.
6945 The difference between N and the actual number of steps taken is
6946 returned."
6947   (interactive "p")
6948   (let ((backward (< n 0))
6949         (n (abs n)))
6950     (while (and (> n 0)
6951                 (if backward
6952                     (gnus-summary-find-prev unread)
6953                   (gnus-summary-find-next unread)))
6954       (unless (zerop (setq n (1- n)))
6955         (gnus-summary-show-thread)))
6956     (when (/= 0 n)
6957       (gnus-message 7 "No more%s articles"
6958                     (if unread " unread" "")))
6959     (unless dont-display
6960       (gnus-summary-recenter)
6961       (gnus-summary-position-point))
6962     n))
6963
6964 (defun gnus-summary-next-unread-subject (n)
6965   "Go to next N'th unread summary line."
6966   (interactive "p")
6967   (gnus-summary-next-subject n t))
6968
6969 (defun gnus-summary-prev-subject (n &optional unread)
6970   "Go to previous N'th summary line.
6971 If optional argument UNREAD is non-nil, only unread article is selected."
6972   (interactive "p")
6973   (gnus-summary-next-subject (- n) unread))
6974
6975 (defun gnus-summary-prev-unread-subject (n)
6976   "Go to previous N'th unread summary line."
6977   (interactive "p")
6978   (gnus-summary-next-subject (- n) t))
6979
6980 (defun gnus-summary-goto-subjects (articles)
6981   "Insert the subject header for ARTICLES in the current buffer."
6982   (save-excursion
6983     (dolist (article articles)
6984       (gnus-summary-goto-subject article t)))
6985   (gnus-summary-limit (append articles gnus-newsgroup-limit))
6986   (gnus-summary-position-point))
6987  
6988 (defun gnus-summary-goto-subject (article &optional force silent)
6989   "Go the subject line of ARTICLE.
6990 If FORCE, also allow jumping to articles not currently shown."
6991   (interactive "nArticle number: ")
6992   (unless (numberp article)
6993     (error "Article %s is not a number" article))
6994   (let ((b (point))
6995         (data (gnus-data-find article)))
6996     ;; We read in the article if we have to.
6997     (and (not data)
6998          force
6999          (gnus-summary-insert-subject
7000           article
7001           (if (or (numberp force) (vectorp force)) force)
7002           t)
7003          (setq data (gnus-data-find article)))
7004     (goto-char b)
7005     (if (not data)
7006         (progn
7007           (unless silent
7008             (gnus-message 3 "Can't find article %d" article))
7009           nil)
7010       (let ((pt (gnus-data-pos data)))
7011         (goto-char pt)
7012         (gnus-summary-set-article-display-arrow pt))
7013       (gnus-summary-position-point)
7014       article)))
7015
7016 ;; Walking around summary lines with displaying articles.
7017
7018 (defun gnus-summary-expand-window (&optional arg)
7019   "Make the summary buffer take up the entire Emacs frame.
7020 Given a prefix, will force an `article' buffer configuration."
7021   (interactive "P")
7022   (if arg
7023       (gnus-configure-windows 'article 'force)
7024     (gnus-configure-windows 'summary 'force)))
7025
7026 (defun gnus-summary-display-article (article &optional all-header)
7027   "Display ARTICLE in article buffer."
7028   (when (gnus-buffer-live-p gnus-article-buffer)
7029     (with-current-buffer gnus-article-buffer
7030       (set-buffer-multibyte t)))
7031   (gnus-set-global-variables)
7032   (when (gnus-buffer-live-p gnus-article-buffer)
7033     (with-current-buffer gnus-article-buffer
7034       (setq gnus-article-charset gnus-newsgroup-charset)
7035       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
7036       (set-buffer-multibyte t)))
7037   (if (null article)
7038       nil
7039     (prog1
7040         (if gnus-summary-display-article-function
7041             (funcall gnus-summary-display-article-function article all-header)
7042           (gnus-article-prepare article all-header))
7043       (with-current-buffer gnus-article-buffer
7044         (set (make-local-variable 'gnus-summary-search-article-matched-data)
7045              nil))
7046       (gnus-run-hooks 'gnus-select-article-hook)
7047       (when (and gnus-current-article
7048                  (not (zerop gnus-current-article)))
7049         (gnus-summary-goto-subject gnus-current-article))
7050       (gnus-summary-recenter)
7051       (when (and gnus-use-trees gnus-show-threads)
7052         (gnus-possibly-generate-tree article)
7053         (gnus-highlight-selected-tree article))
7054       ;; Successfully display article.
7055       (gnus-article-set-window-start
7056        (cdr (assq article gnus-newsgroup-bookmarks))))))
7057
7058 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
7059   "Select the current article.
7060 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
7061 non-nil, the article will be re-fetched even if it already present in
7062 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
7063 be displayed."
7064   ;; Make sure we are in the summary buffer to work around bbdb bug.
7065   (unless (eq major-mode 'gnus-summary-mode)
7066     (set-buffer gnus-summary-buffer))
7067   (let ((article (or article (gnus-summary-article-number)))
7068         (all-headers (not (not all-headers))) ;Must be T or NIL.
7069         gnus-summary-display-article-function)
7070     (and (not pseudo)
7071          (gnus-summary-article-pseudo-p article)
7072          (error "This is a pseudo-article"))
7073     (save-excursion
7074       (set-buffer gnus-summary-buffer)
7075       (if (or (and gnus-single-article-buffer
7076                    (or (null gnus-current-article)
7077                        (null gnus-article-current)
7078                        (null (get-buffer gnus-article-buffer))
7079                        (not (eq article (cdr gnus-article-current)))
7080                        (not (equal (car gnus-article-current)
7081                                    gnus-newsgroup-name))))
7082               (and (not gnus-single-article-buffer)
7083                    (or (null gnus-current-article)
7084                        (not (eq gnus-current-article article))))
7085               force)
7086           ;; The requested article is different from the current article.
7087           (progn
7088             (gnus-summary-display-article article all-headers)
7089             (gnus-article-set-window-start
7090              (cdr (assq article gnus-newsgroup-bookmarks)))
7091             article)
7092         'old))))
7093
7094 (defun gnus-summary-force-verify-and-decrypt ()
7095   "Display buttons for signed/encrypted parts and verify/decrypt them."
7096   (interactive)
7097   (let ((mm-verify-option 'known)
7098         (mm-decrypt-option 'known)
7099         (gnus-article-emulate-mime t)
7100         (gnus-buttonized-mime-types (append (list "multipart/signed"
7101                                                   "multipart/encrypted")
7102                                             gnus-buttonized-mime-types)))
7103     (gnus-summary-select-article nil 'force)))
7104
7105 (defun gnus-summary-set-current-mark (&optional current-mark)
7106   "Obsolete function."
7107   nil)
7108
7109 (defun gnus-summary-next-article (&optional unread subject backward push)
7110   "Select the next article.
7111 If UNREAD, only unread articles are selected.
7112 If SUBJECT, only articles with SUBJECT are selected.
7113 If BACKWARD, the previous article is selected instead of the next."
7114   (interactive "P")
7115   (cond
7116    ;; Is there such an article?
7117    ((and (gnus-summary-search-forward unread subject backward)
7118          (or (gnus-summary-display-article (gnus-summary-article-number))
7119              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
7120     (gnus-summary-position-point))
7121    ;; If not, we try the first unread, if that is wanted.
7122    ((and subject
7123          gnus-auto-select-same
7124          (gnus-summary-first-unread-article))
7125     (gnus-summary-position-point)
7126     (gnus-message 6 "Wrapped"))
7127    ;; Try to get next/previous article not displayed in this group.
7128    ((and gnus-auto-extend-newsgroup
7129          (not unread) (not subject))
7130     (gnus-summary-goto-article
7131      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
7132      nil (count-lines (point-min) (point))))
7133    ;; Go to next/previous group.
7134    (t
7135     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
7136       (gnus-summary-jump-to-group gnus-newsgroup-name))
7137     (let ((cmd last-command-char)
7138           (point
7139            (save-excursion
7140              (set-buffer gnus-group-buffer)
7141              (point)))
7142           (group
7143            (if (eq gnus-keep-same-level 'best)
7144                (gnus-summary-best-group gnus-newsgroup-name)
7145              (gnus-summary-search-group backward gnus-keep-same-level))))
7146       ;; For some reason, the group window gets selected.  We change
7147       ;; it back.
7148       (select-window (get-buffer-window (current-buffer)))
7149       ;; Select next unread newsgroup automagically.
7150       (cond
7151        ((or (not gnus-auto-select-next)
7152             (not cmd))
7153         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
7154        ((or (eq gnus-auto-select-next 'quietly)
7155             (and (eq gnus-auto-select-next 'slightly-quietly)
7156                  push)
7157             (and (eq gnus-auto-select-next 'almost-quietly)
7158                  (gnus-summary-last-article-p)))
7159         ;; Select quietly.
7160         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
7161             (gnus-summary-exit)
7162           (gnus-message 7 "No more%s articles (%s)..."
7163                         (if unread " unread" "")
7164                         (if group (concat "selecting " group)
7165                           "exiting"))
7166           (gnus-summary-next-group nil group backward)))
7167        (t
7168         (when (gnus-key-press-event-p last-input-event)
7169           (gnus-summary-walk-group-buffer
7170            gnus-newsgroup-name cmd unread backward point))))))))
7171
7172 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
7173   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
7174                       (?\C-p (gnus-group-prev-unread-group 1))))
7175         (cursor-in-echo-area t)
7176         keve key group ended prompt)
7177     (save-excursion
7178       (set-buffer gnus-group-buffer)
7179       (goto-char start)
7180       (setq group
7181             (if (eq gnus-keep-same-level 'best)
7182                 (gnus-summary-best-group gnus-newsgroup-name)
7183               (gnus-summary-search-group backward gnus-keep-same-level))))
7184     (while (not ended)
7185       (setq prompt
7186             (format
7187              "No more%s articles%s " (if unread " unread" "")
7188              (if (and group
7189                       (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
7190                  (format " (Type %s for %s [%s])"
7191                          (single-key-description cmd) group
7192                          (gnus-group-unread group))
7193                (format " (Type %s to exit %s)"
7194                        (single-key-description cmd)
7195                        gnus-newsgroup-name))))
7196       ;; Confirm auto selection.
7197       (setq key (car (setq keve (gnus-read-event-char prompt)))
7198             ended t)
7199       (cond
7200        ((assq key keystrokes)
7201         (let ((obuf (current-buffer)))
7202           (switch-to-buffer gnus-group-buffer)
7203           (when group
7204             (gnus-group-jump-to-group group))
7205           (eval (cadr (assq key keystrokes)))
7206           (setq group (gnus-group-group-name))
7207           (switch-to-buffer obuf))
7208         (setq ended nil))
7209        ((equal key cmd)
7210         (if (or (not group)
7211                 (gnus-ephemeral-group-p gnus-newsgroup-name))
7212             (gnus-summary-exit)
7213           (gnus-summary-next-group nil group backward)))
7214        (t
7215         (push (cdr keve) unread-command-events))))))
7216
7217 (defun gnus-summary-next-unread-article ()
7218   "Select unread article after current one."
7219   (interactive)
7220   (gnus-summary-next-article
7221    (or (not (eq gnus-summary-goto-unread 'never))
7222        (gnus-summary-last-article-p (gnus-summary-article-number)))
7223    (and gnus-auto-select-same
7224         (gnus-summary-article-subject))))
7225
7226 (defun gnus-summary-prev-article (&optional unread subject)
7227   "Select the article after the current one.
7228 If UNREAD is non-nil, only unread articles are selected."
7229   (interactive "P")
7230   (gnus-summary-next-article unread subject t))
7231
7232 (defun gnus-summary-prev-unread-article ()
7233   "Select unread article before current one."
7234   (interactive)
7235   (gnus-summary-prev-article
7236    (or (not (eq gnus-summary-goto-unread 'never))
7237        (gnus-summary-first-article-p (gnus-summary-article-number)))
7238    (and gnus-auto-select-same
7239         (gnus-summary-article-subject))))
7240
7241 (defun gnus-summary-next-page (&optional lines circular stop)
7242   "Show next page of the selected article.
7243 If at the end of the current article, select the next article.
7244 LINES says how many lines should be scrolled up.
7245
7246 If CIRCULAR is non-nil, go to the start of the article instead of
7247 selecting the next article when reaching the end of the current
7248 article.
7249
7250 If STOP is non-nil, just stop when reaching the end of the message.
7251
7252 Also see the variable `gnus-article-skip-boring'."
7253   (interactive "P")
7254   (setq gnus-summary-buffer (current-buffer))
7255   (gnus-set-global-variables)
7256   (let ((article (gnus-summary-article-number))
7257         (article-window (get-buffer-window gnus-article-buffer t))
7258         endp)
7259     ;; If the buffer is empty, we have no article.
7260     (unless article
7261       (error "No article to select"))
7262     (gnus-configure-windows 'article)
7263     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
7264         (if (and (eq gnus-summary-goto-unread 'never)
7265                  (not (gnus-summary-last-article-p article)))
7266             (gnus-summary-next-article)
7267           (gnus-summary-next-unread-article))
7268       (if (or (null gnus-current-article)
7269               (null gnus-article-current)
7270               (/= article (cdr gnus-article-current))
7271               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7272           ;; Selected subject is different from current article's.
7273           (gnus-summary-display-article article)
7274         (when article-window
7275           (gnus-eval-in-buffer-window gnus-article-buffer
7276             (setq endp (or (gnus-article-next-page lines)
7277                            (gnus-article-only-boring-p))))
7278           (when endp
7279             (cond (stop
7280                    (gnus-message 3 "End of message"))
7281                   (circular
7282                    (gnus-summary-beginning-of-article))
7283                   (lines
7284                    (gnus-message 3 "End of message"))
7285                   ((null lines)
7286                    (if (and (eq gnus-summary-goto-unread 'never)
7287                             (not (gnus-summary-last-article-p article)))
7288                        (gnus-summary-next-article)
7289                      (gnus-summary-next-unread-article))))))))
7290     (gnus-summary-recenter)
7291     (gnus-summary-position-point)))
7292
7293 (defun gnus-summary-prev-page (&optional lines move)
7294   "Show previous page of selected article.
7295 Argument LINES specifies lines to be scrolled down.
7296 If MOVE, move to the previous unread article if point is at
7297 the beginning of the buffer."
7298   (interactive "P")
7299   (let ((article (gnus-summary-article-number))
7300         (article-window (get-buffer-window gnus-article-buffer t))
7301         endp)
7302     (gnus-configure-windows 'article)
7303     (if (or (null gnus-current-article)
7304             (null gnus-article-current)
7305             (/= article (cdr gnus-article-current))
7306             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
7307         ;; Selected subject is different from current article's.
7308         (gnus-summary-display-article article)
7309       (gnus-summary-recenter)
7310       (when article-window
7311         (gnus-eval-in-buffer-window gnus-article-buffer
7312           (setq endp (gnus-article-prev-page lines)))
7313         (when (and move endp)
7314           (cond (lines
7315                  (gnus-message 3 "Beginning of message"))
7316                 ((null lines)
7317                  (if (and (eq gnus-summary-goto-unread 'never)
7318                           (not (gnus-summary-first-article-p article)))
7319                      (gnus-summary-prev-article)
7320                    (gnus-summary-prev-unread-article))))))))
7321   (gnus-summary-position-point))
7322
7323 (defun gnus-summary-prev-page-or-article (&optional lines)
7324   "Show previous page of selected article.
7325 Argument LINES specifies lines to be scrolled down.
7326 If at the beginning of the article, go to the next article."
7327   (interactive "P")
7328   (gnus-summary-prev-page lines t))
7329
7330 (defun gnus-summary-scroll-up (lines)
7331   "Scroll up (or down) one line current article.
7332 Argument LINES specifies lines to be scrolled up (or down if negative)."
7333   (interactive "p")
7334   (gnus-configure-windows 'article)
7335   (gnus-summary-show-thread)
7336   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7337     (gnus-eval-in-buffer-window gnus-article-buffer
7338       (cond ((> lines 0)
7339              (when (gnus-article-next-page lines)
7340                (gnus-message 3 "End of message")))
7341             ((< lines 0)
7342              (gnus-article-prev-page (- lines))))))
7343   (gnus-summary-recenter)
7344   (gnus-summary-position-point))
7345
7346 (defun gnus-summary-scroll-down (lines)
7347   "Scroll down (or up) one line current article.
7348 Argument LINES specifies lines to be scrolled down (or up if negative)."
7349   (interactive "p")
7350   (gnus-summary-scroll-up (- lines)))
7351
7352 (defun gnus-summary-next-same-subject ()
7353   "Select next article which has the same subject as current one."
7354   (interactive)
7355   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7356
7357 (defun gnus-summary-prev-same-subject ()
7358   "Select previous article which has the same subject as current one."
7359   (interactive)
7360   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7361
7362 (defun gnus-summary-next-unread-same-subject ()
7363   "Select next unread article which has the same subject as current one."
7364   (interactive)
7365   (gnus-summary-next-article t (gnus-summary-article-subject)))
7366
7367 (defun gnus-summary-prev-unread-same-subject ()
7368   "Select previous unread article which has the same subject as current one."
7369   (interactive)
7370   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7371
7372 (defun gnus-summary-first-unread-article ()
7373   "Select the first unread article.
7374 Return nil if there are no unread articles."
7375   (interactive)
7376   (prog1
7377       (when (gnus-summary-first-subject t)
7378         (gnus-summary-show-thread)
7379         (gnus-summary-first-subject t)
7380         (gnus-summary-display-article (gnus-summary-article-number)))
7381     (gnus-summary-position-point)))
7382
7383 (defun gnus-summary-first-unread-subject ()
7384   "Place the point on the subject line of the first unread article.
7385 Return nil if there are no unread articles."
7386   (interactive)
7387   (prog1
7388       (when (gnus-summary-first-subject t)
7389         (gnus-summary-show-thread)
7390         (gnus-summary-first-subject t))
7391     (gnus-summary-position-point)))
7392
7393 (defun gnus-summary-first-unseen-subject ()
7394   "Place the point on the subject line of the first unseen article.
7395 Return nil if there are no unseen articles."
7396   (interactive)
7397   (prog1
7398       (when (gnus-summary-first-subject nil nil t)
7399         (gnus-summary-show-thread)
7400         (gnus-summary-first-subject nil nil t))
7401     (gnus-summary-position-point)))
7402
7403 (defun gnus-summary-first-unseen-or-unread-subject ()
7404   "Place the point on the subject line of the first unseen article or,
7405 if all article have been seen, on the subject line of the first unread
7406 article."
7407   (interactive)
7408   (prog1
7409       (unless (when (gnus-summary-first-subject nil nil t)
7410                 (gnus-summary-show-thread)
7411                 (gnus-summary-first-subject nil nil t))
7412         (when (gnus-summary-first-subject t)
7413           (gnus-summary-show-thread)
7414           (gnus-summary-first-subject t)))
7415     (gnus-summary-position-point)))
7416
7417 (defun gnus-summary-first-article ()
7418   "Select the first article.
7419 Return nil if there are no articles."
7420   (interactive)
7421   (prog1
7422       (when (gnus-summary-first-subject)
7423         (gnus-summary-show-thread)
7424         (gnus-summary-first-subject)
7425         (gnus-summary-display-article (gnus-summary-article-number)))
7426     (gnus-summary-position-point)))
7427
7428 (defun gnus-summary-best-unread-article (&optional arg)
7429   "Select the unread article with the highest score.
7430 If given a prefix argument, select the next unread article that has a
7431 score higher than the default score."
7432   (interactive "P")
7433   (let ((article (if arg
7434                      (gnus-summary-better-unread-subject)
7435                    (gnus-summary-best-unread-subject))))
7436     (if article
7437         (gnus-summary-goto-article article)
7438       (error "No unread articles"))))
7439
7440 (defun gnus-summary-best-unread-subject ()
7441   "Select the unread subject with the highest score."
7442   (interactive)
7443   (let ((best -1000000)
7444         (data gnus-newsgroup-data)
7445         article score)
7446     (while data
7447       (and (gnus-data-unread-p (car data))
7448            (> (setq score
7449                     (gnus-summary-article-score (gnus-data-number (car data))))
7450               best)
7451            (setq best score
7452                  article (gnus-data-number (car data))))
7453       (setq data (cdr data)))
7454     (when article
7455       (gnus-summary-goto-subject article))
7456     (gnus-summary-position-point)
7457     article))
7458
7459 (defun gnus-summary-better-unread-subject ()
7460   "Select the first unread subject that has a score over the default score."
7461   (interactive)
7462   (let ((data gnus-newsgroup-data)
7463         article score)
7464     (while (and (setq article (gnus-data-number (car data)))
7465                 (or (gnus-data-read-p (car data))
7466                     (not (> (gnus-summary-article-score article)
7467                             gnus-summary-default-score))))
7468       (setq data (cdr data)))
7469     (when article
7470       (gnus-summary-goto-subject article))
7471     (gnus-summary-position-point)
7472     article))
7473
7474 (defun gnus-summary-last-subject ()
7475   "Go to the last displayed subject line in the group."
7476   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7477     (when article
7478       (gnus-summary-goto-subject article))))
7479
7480 (defun gnus-summary-goto-article (article &optional all-headers force)
7481   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7482 If ALL-HEADERS is non-nil, no header lines are hidden.
7483 If FORCE, go to the article even if it isn't displayed.  If FORCE
7484 is a number, it is the line the article is to be displayed on."
7485   (interactive
7486    (list
7487     (completing-read
7488      "Article number or Message-ID: "
7489      (mapcar (lambda (number) (list (int-to-string number)))
7490              gnus-newsgroup-limit))
7491     current-prefix-arg
7492     t))
7493   (prog1
7494       (if (and (stringp article)
7495                (string-match "@\\|%40" article))
7496           (gnus-summary-refer-article article)
7497         (when (stringp article)
7498           (setq article (string-to-number article)))
7499         (if (gnus-summary-goto-subject article force)
7500             (gnus-summary-display-article article all-headers)
7501           (gnus-message 4 "Couldn't go to article %s" article) nil))
7502     (gnus-summary-position-point)))
7503
7504 (defun gnus-summary-goto-last-article ()
7505   "Go to the previously read article."
7506   (interactive)
7507   (prog1
7508       (when gnus-last-article
7509         (gnus-summary-goto-article gnus-last-article nil t))
7510     (gnus-summary-position-point)))
7511
7512 (defun gnus-summary-pop-article (number)
7513   "Pop one article off the history and go to the previous.
7514 NUMBER articles will be popped off."
7515   (interactive "p")
7516   (let (to)
7517     (setq gnus-newsgroup-history
7518           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7519     (if to
7520         (gnus-summary-goto-article (car to) nil t)
7521       (error "Article history empty")))
7522   (gnus-summary-position-point))
7523
7524 ;; Summary commands and functions for limiting the summary buffer.
7525
7526 (defun gnus-summary-limit-to-articles (n)
7527   "Limit the summary buffer to the next N articles.
7528 If not given a prefix, use the process marked articles instead."
7529   (interactive "P")
7530   (prog1
7531       (let ((articles (gnus-summary-work-articles n)))
7532         (setq gnus-newsgroup-processable nil)
7533         (gnus-summary-limit articles))
7534     (gnus-summary-position-point)))
7535
7536 (defun gnus-summary-pop-limit (&optional total)
7537   "Restore the previous limit.
7538 If given a prefix, remove all limits."
7539   (interactive "P")
7540   (when total
7541     (setq gnus-newsgroup-limits
7542           (list (mapcar (lambda (h) (mail-header-number h))
7543                         gnus-newsgroup-headers))))
7544   (unless gnus-newsgroup-limits
7545     (error "No limit to pop"))
7546   (prog1
7547       (gnus-summary-limit nil 'pop)
7548     (gnus-summary-position-point)))
7549
7550 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7551   "Limit the summary buffer to articles that have subjects that match a regexp.
7552 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7553   (interactive
7554    (list (read-string (if current-prefix-arg
7555                           "Exclude subject (regexp): "
7556                         "Limit to subject (regexp): "))
7557          nil current-prefix-arg))
7558   (unless header
7559     (setq header "subject"))
7560   (when (not (equal "" subject))
7561     (prog1
7562         (let ((articles (gnus-summary-find-matching
7563                          (or header "subject") subject 'all nil nil
7564                          not-matching)))
7565           (unless articles
7566             (error "Found no matches for \"%s\"" subject))
7567           (gnus-summary-limit articles))
7568       (gnus-summary-position-point))))
7569
7570 (defun gnus-summary-limit-to-author (from &optional not-matching)
7571   "Limit the summary buffer to articles that have authors that match a regexp.
7572 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7573   (interactive
7574    (list (read-string (if current-prefix-arg
7575                           "Exclude author (regexp): "
7576                         "Limit to author (regexp): "))
7577          current-prefix-arg))
7578   (gnus-summary-limit-to-subject from "from" not-matching))
7579
7580 (defun gnus-summary-limit-to-age (age &optional younger-p)
7581   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7582 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7583 articles that are younger than AGE days."
7584   (interactive
7585    (let ((younger current-prefix-arg)
7586          (days-got nil)
7587          days)
7588      (while (not days-got)
7589        (setq days (if younger
7590                       (read-string "Limit to articles younger than (in days, older when negative): ")
7591                     (read-string
7592                      "Limit to articles older than (in days, younger when negative): ")))
7593        (when (> (length days) 0)
7594          (setq days (read days)))
7595        (if (numberp days)
7596            (progn
7597              (setq days-got t)
7598              (if (< days 0)
7599                  (progn
7600                    (setq younger (not younger))
7601                    (setq days (* days -1)))))
7602          (message "Please enter a number.")
7603          (sleep-for 1)))
7604      (list days younger)))
7605   (prog1
7606       (let ((data gnus-newsgroup-data)
7607             (cutoff (days-to-time age))
7608             articles d date is-younger)
7609         (while (setq d (pop data))
7610           (when (and (vectorp (gnus-data-header d))
7611                      (setq date (mail-header-date (gnus-data-header d))))
7612             (setq is-younger (time-less-p
7613                               (time-since (condition-case ()
7614                                               (date-to-time date)
7615                                             (error '(0 0))))
7616                               cutoff))
7617             (when (if younger-p
7618                       is-younger
7619                     (not is-younger))
7620               (push (gnus-data-number d) articles))))
7621         (gnus-summary-limit (nreverse articles)))
7622     (gnus-summary-position-point)))
7623
7624 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7625   "Limit the summary buffer to articles that match an 'extra' header."
7626   (interactive
7627    (let ((header
7628           (intern
7629            (gnus-completing-read-with-default
7630             (symbol-name (car gnus-extra-headers))
7631             (if current-prefix-arg
7632                 "Exclude extra header:"
7633               "Limit extra header:")
7634             (mapcar (lambda (x)
7635                       (cons (symbol-name x) x))
7636                     gnus-extra-headers)
7637             nil
7638             t))))
7639      (list header
7640            (read-string (format "%s header %s (regexp): "
7641                                 (if current-prefix-arg "Exclude" "Limit to")
7642                                 header))
7643            current-prefix-arg)))
7644   (when (not (equal "" regexp))
7645     (prog1
7646         (let ((articles (gnus-summary-find-matching
7647                          (cons 'extra header) regexp 'all nil nil
7648                          not-matching)))
7649           (unless articles
7650             (error "Found no matches for \"%s\"" regexp))
7651           (gnus-summary-limit articles))
7652       (gnus-summary-position-point))))
7653
7654 (defun gnus-summary-limit-to-display-predicate ()
7655   "Limit the summary buffer to the predicated in the `display' group parameter."
7656   (interactive)
7657   (unless gnus-newsgroup-display
7658     (error "There is no `display' group parameter"))
7659   (let (articles)
7660     (dolist (number gnus-newsgroup-articles)
7661       (when (funcall gnus-newsgroup-display)
7662         (push number articles)))
7663     (gnus-summary-limit articles))
7664   (gnus-summary-position-point))
7665
7666 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7667 (make-obsolete
7668  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7669
7670 (defun gnus-summary-limit-to-unread (&optional all)
7671   "Limit the summary buffer to articles that are not marked as read.
7672 If ALL is non-nil, limit strictly to unread articles."
7673   (interactive "P")
7674   (if all
7675       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7676     (gnus-summary-limit-to-marks
7677      ;; Concat all the marks that say that an article is read and have
7678      ;; those removed.
7679      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7680            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7681            gnus-low-score-mark gnus-expirable-mark
7682            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7683            gnus-duplicate-mark gnus-souped-mark)
7684      'reverse)))
7685
7686 (defun gnus-summary-limit-to-replied (&optional unreplied)
7687   "Limit the summary buffer to replied articles.
7688 If UNREPLIED (the prefix), limit to unreplied articles."
7689   (interactive "P")
7690   (if unreplied
7691       (gnus-summary-limit
7692        (gnus-set-difference gnus-newsgroup-articles
7693         gnus-newsgroup-replied))
7694     (gnus-summary-limit gnus-newsgroup-replied))
7695   (gnus-summary-position-point))
7696
7697 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7698 (make-obsolete 'gnus-summary-delete-marked-with
7699                'gnus-summary-limit-exclude-marks)
7700
7701 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7702   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7703 If REVERSE, limit the summary buffer to articles that are marked
7704 with MARKS.  MARKS can either be a string of marks or a list of marks.
7705 Returns how many articles were removed."
7706   (interactive "sMarks: ")
7707   (gnus-summary-limit-to-marks marks t))
7708
7709 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7710   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7711 If REVERSE (the prefix), limit the summary buffer to articles that are
7712 not marked with MARKS.  MARKS can either be a string of marks or a
7713 list of marks.
7714 Returns how many articles were removed."
7715   (interactive "sMarks: \nP")
7716   (prog1
7717       (let ((data gnus-newsgroup-data)
7718             (marks (if (listp marks) marks
7719                      (append marks nil))) ; Transform to list.
7720             articles)
7721         (while data
7722           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7723                   (memq (gnus-data-mark (car data)) marks))
7724             (push (gnus-data-number (car data)) articles))
7725           (setq data (cdr data)))
7726         (gnus-summary-limit articles))
7727     (gnus-summary-position-point)))
7728
7729 (defun gnus-summary-limit-to-score (score)
7730   "Limit to articles with score at or above SCORE."
7731   (interactive "NLimit to articles with score of at least: ")
7732   (let ((data gnus-newsgroup-data)
7733         articles)
7734     (while data
7735       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7736                 score)
7737         (push (gnus-data-number (car data)) articles))
7738       (setq data (cdr data)))
7739     (prog1
7740         (gnus-summary-limit articles)
7741       (gnus-summary-position-point))))
7742
7743 (defun gnus-summary-limit-to-unseen ()
7744   "Limit to unseen articles."
7745   (interactive)
7746   (prog1
7747       (gnus-summary-limit gnus-newsgroup-unseen)
7748     (gnus-summary-position-point)))
7749
7750 (defun gnus-summary-limit-include-thread (id)
7751   "Display all the hidden articles that is in the thread with ID in it.
7752 When called interactively, ID is the Message-ID of the current
7753 article."
7754   (interactive (list (mail-header-id (gnus-summary-article-header))))
7755   (let ((articles (gnus-articles-in-thread
7756                    (gnus-id-to-thread (gnus-root-id id)))))
7757     (prog1
7758         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7759       (gnus-summary-limit-include-matching-articles
7760        "subject"
7761        (regexp-quote (gnus-simplify-subject-re
7762                       (mail-header-subject (gnus-id-to-header id)))))
7763       (gnus-summary-position-point))))
7764
7765 (defun gnus-summary-limit-include-matching-articles (header regexp)
7766   "Display all the hidden articles that have HEADERs that match REGEXP."
7767   (interactive (list (read-string "Match on header: ")
7768                      (read-string "Regexp: ")))
7769   (let ((articles (gnus-find-matching-articles header regexp)))
7770     (prog1
7771         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7772       (gnus-summary-position-point))))
7773
7774 (defun gnus-summary-insert-dormant-articles ()
7775   "Insert all the dormant articles for this group into the current buffer."
7776   (interactive)
7777   (let ((gnus-verbose (max 6 gnus-verbose)))
7778     (if (not gnus-newsgroup-dormant)
7779         (gnus-message 3 "No cached articles for this group")
7780       (gnus-summary-goto-subjects gnus-newsgroup-dormant))))
7781
7782 (defun gnus-summary-limit-include-dormant ()
7783   "Display all the hidden articles that are marked as dormant.
7784 Note that this command only works on a subset of the articles currently
7785 fetched for this group."
7786   (interactive)
7787   (unless gnus-newsgroup-dormant
7788     (error "There are no dormant articles in this group"))
7789   (prog1
7790       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7791     (gnus-summary-position-point)))
7792
7793 (defun gnus-summary-limit-exclude-dormant ()
7794   "Hide all dormant articles."
7795   (interactive)
7796   (prog1
7797       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7798     (gnus-summary-position-point)))
7799
7800 (defun gnus-summary-limit-exclude-childless-dormant ()
7801   "Hide all dormant articles that have no children."
7802   (interactive)
7803   (let ((data (gnus-data-list t))
7804         articles d children)
7805     ;; Find all articles that are either not dormant or have
7806     ;; children.
7807     (while (setq d (pop data))
7808       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7809                 (and (setq children
7810                            (gnus-article-children (gnus-data-number d)))
7811                      (let (found)
7812                        (while children
7813                          (when (memq (car children) articles)
7814                            (setq children nil
7815                                  found t))
7816                          (pop children))
7817                        found)))
7818         (push (gnus-data-number d) articles)))
7819     ;; Do the limiting.
7820     (prog1
7821         (gnus-summary-limit articles)
7822       (gnus-summary-position-point))))
7823
7824 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7825   "Mark all unread excluded articles as read.
7826 If ALL, mark even excluded ticked and dormants as read."
7827   (interactive "P")
7828   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7829   (let ((articles (gnus-sorted-ndifference
7830                    (sort
7831                     (mapcar (lambda (h) (mail-header-number h))
7832                             gnus-newsgroup-headers)
7833                     '<)
7834                    gnus-newsgroup-limit))
7835         article)
7836     (setq gnus-newsgroup-unreads
7837           (gnus-sorted-intersection gnus-newsgroup-unreads
7838                                     gnus-newsgroup-limit))
7839     (if all
7840         (setq gnus-newsgroup-dormant nil
7841               gnus-newsgroup-marked nil
7842               gnus-newsgroup-reads
7843               (nconc
7844                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7845                gnus-newsgroup-reads))
7846       (while (setq article (pop articles))
7847         (unless (or (memq article gnus-newsgroup-dormant)
7848                     (memq article gnus-newsgroup-marked))
7849           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7850
7851 (defun gnus-summary-limit (articles &optional pop)
7852   (if pop
7853       ;; We pop the previous limit off the stack and use that.
7854       (setq articles (car gnus-newsgroup-limits)
7855             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7856     ;; We use the new limit, so we push the old limit on the stack.
7857     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7858   ;; Set the limit.
7859   (setq gnus-newsgroup-limit articles)
7860   (let ((total (length gnus-newsgroup-data))
7861         (data (gnus-data-find-list (gnus-summary-article-number)))
7862         (gnus-summary-mark-below nil)   ; Inhibit this.
7863         found)
7864     ;; This will do all the work of generating the new summary buffer
7865     ;; according to the new limit.
7866     (gnus-summary-prepare)
7867     ;; Hide any threads, possibly.
7868     (gnus-summary-maybe-hide-threads)
7869     ;; Try to return to the article you were at, or one in the
7870     ;; neighborhood.
7871     (when data
7872       ;; We try to find some article after the current one.
7873       (while data
7874         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7875           (setq data nil
7876                 found t))
7877         (setq data (cdr data))))
7878     (unless found
7879       ;; If there is no data, that means that we were after the last
7880       ;; article.  The same goes when we can't find any articles
7881       ;; after the current one.
7882       (goto-char (point-max))
7883       (gnus-summary-find-prev))
7884     (gnus-set-mode-line 'summary)
7885     ;; We return how many articles were removed from the summary
7886     ;; buffer as a result of the new limit.
7887     (- total (length gnus-newsgroup-data))))
7888
7889 (defsubst gnus-invisible-cut-children (threads)
7890   (let ((num 0))
7891     (while threads
7892       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7893         (incf num))
7894       (pop threads))
7895     (< num 2)))
7896
7897 (defsubst gnus-cut-thread (thread)
7898   "Go forwards in the thread until we find an article that we want to display."
7899   (when (or (eq gnus-fetch-old-headers 'some)
7900             (eq gnus-fetch-old-headers 'invisible)
7901             (numberp gnus-fetch-old-headers)
7902             (eq gnus-build-sparse-threads 'some)
7903             (eq gnus-build-sparse-threads 'more))
7904     ;; Deal with old-fetched headers and sparse threads.
7905     (while (and
7906             thread
7907             (or
7908              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7909              (gnus-summary-article-ancient-p
7910               (mail-header-number (car thread))))
7911             (if (or (<= (length (cdr thread)) 1)
7912                     (eq gnus-fetch-old-headers 'invisible))
7913                 (setq gnus-newsgroup-limit
7914                       (delq (mail-header-number (car thread))
7915                             gnus-newsgroup-limit)
7916                       thread (cadr thread))
7917               (when (gnus-invisible-cut-children (cdr thread))
7918                 (let ((th (cdr thread)))
7919                   (while th
7920                     (if (memq (mail-header-number (caar th))
7921                               gnus-newsgroup-limit)
7922                         (setq thread (car th)
7923                               th nil)
7924                       (setq th (cdr th))))))))))
7925   thread)
7926
7927 (defun gnus-cut-threads (threads)
7928   "Cut off all uninteresting articles from the beginning of THREADS."
7929   (when (or (eq gnus-fetch-old-headers 'some)
7930             (eq gnus-fetch-old-headers 'invisible)
7931             (numberp gnus-fetch-old-headers)
7932             (eq gnus-build-sparse-threads 'some)
7933             (eq gnus-build-sparse-threads 'more))
7934     (let ((th threads))
7935       (while th
7936         (setcar th (gnus-cut-thread (car th)))
7937         (setq th (cdr th)))))
7938   ;; Remove nixed out threads.
7939   (delq nil threads))
7940
7941 (defun gnus-summary-initial-limit (&optional show-if-empty)
7942   "Figure out what the initial limit is supposed to be on group entry.
7943 This entails weeding out unwanted dormants, low-scored articles,
7944 fetch-old-headers verbiage, and so on."
7945   ;; Most groups have nothing to remove.
7946   (if (or gnus-inhibit-limiting
7947           (and (null gnus-newsgroup-dormant)
7948                (eq gnus-newsgroup-display 'gnus-not-ignore)
7949                (not (eq gnus-fetch-old-headers 'some))
7950                (not (numberp gnus-fetch-old-headers))
7951                (not (eq gnus-fetch-old-headers 'invisible))
7952                (null gnus-summary-expunge-below)
7953                (not (eq gnus-build-sparse-threads 'some))
7954                (not (eq gnus-build-sparse-threads 'more))
7955                (null gnus-thread-expunge-below)
7956                (not gnus-use-nocem)))
7957       ()                                ; Do nothing.
7958     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7959     (setq gnus-newsgroup-limit nil)
7960     (mapatoms
7961      (lambda (node)
7962        (unless (car (symbol-value node))
7963          ;; These threads have no parents -- they are roots.
7964          (let ((nodes (cdr (symbol-value node)))
7965                thread)
7966            (while nodes
7967              (if (and gnus-thread-expunge-below
7968                       (< (gnus-thread-total-score (car nodes))
7969                          gnus-thread-expunge-below))
7970                  (gnus-expunge-thread (pop nodes))
7971                (setq thread (pop nodes))
7972                (gnus-summary-limit-children thread))))))
7973      gnus-newsgroup-dependencies)
7974     ;; If this limitation resulted in an empty group, we might
7975     ;; pop the previous limit and use it instead.
7976     (when (and (not gnus-newsgroup-limit)
7977                show-if-empty)
7978       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7979     gnus-newsgroup-limit))
7980
7981 (defun gnus-summary-limit-children (thread)
7982   "Return 1 if this subthread is visible and 0 if it is not."
7983   ;; First we get the number of visible children to this thread.  This
7984   ;; is done by recursing down the thread using this function, so this
7985   ;; will really go down to a leaf article first, before slowly
7986   ;; working its way up towards the root.
7987   (when thread
7988     (let* ((max-lisp-eval-depth 5000)
7989            (children
7990            (if (cdr thread)
7991                (apply '+ (mapcar 'gnus-summary-limit-children
7992                                  (cdr thread)))
7993              0))
7994           (number (mail-header-number (car thread)))
7995           score)
7996       (if (and
7997            (not (memq number gnus-newsgroup-marked))
7998            (or
7999             ;; If this article is dormant and has absolutely no visible
8000             ;; children, then this article isn't visible.
8001             (and (memq number gnus-newsgroup-dormant)
8002                  (zerop children))
8003             ;; If this is "fetch-old-headered" and there is no
8004             ;; visible children, then we don't want this article.
8005             (and (or (eq gnus-fetch-old-headers 'some)
8006                      (numberp gnus-fetch-old-headers))
8007                  (gnus-summary-article-ancient-p number)
8008                  (zerop children))
8009             ;; If this is "fetch-old-headered" and `invisible', then
8010             ;; we don't want this article.
8011             (and (eq gnus-fetch-old-headers 'invisible)
8012                  (gnus-summary-article-ancient-p number))
8013             ;; If this is a sparsely inserted article with no children,
8014             ;; we don't want it.
8015             (and (eq gnus-build-sparse-threads 'some)
8016                  (gnus-summary-article-sparse-p number)
8017                  (zerop children))
8018             ;; If we use expunging, and this article is really
8019             ;; low-scored, then we don't want this article.
8020             (when (and gnus-summary-expunge-below
8021                        (< (setq score
8022                                 (or (cdr (assq number gnus-newsgroup-scored))
8023                                     gnus-summary-default-score))
8024                           gnus-summary-expunge-below))
8025               ;; We increase the expunge-tally here, but that has
8026               ;; nothing to do with the limits, really.
8027               (incf gnus-newsgroup-expunged-tally)
8028               ;; We also mark as read here, if that's wanted.
8029               (when (and gnus-summary-mark-below
8030                          (< score gnus-summary-mark-below))
8031                 (setq gnus-newsgroup-unreads
8032                       (delq number gnus-newsgroup-unreads))
8033                 (if gnus-newsgroup-auto-expire
8034                     (push number gnus-newsgroup-expirable)
8035                   (push (cons number gnus-low-score-mark)
8036                         gnus-newsgroup-reads)))
8037               t)
8038             ;; Do the `display' group parameter.
8039             (and gnus-newsgroup-display
8040                  (not (funcall gnus-newsgroup-display)))
8041             ;; Check NoCeM things.
8042             (if (and gnus-use-nocem
8043                      (gnus-nocem-unwanted-article-p
8044                       (mail-header-id (car thread))))
8045                 (progn
8046                   (setq gnus-newsgroup-unreads
8047                         (delq number gnus-newsgroup-unreads))
8048                   t))))
8049           ;; Nope, invisible article.
8050           0
8051         ;; Ok, this article is to be visible, so we add it to the limit
8052         ;; and return 1.
8053         (push number gnus-newsgroup-limit)
8054         1))))
8055
8056 (defun gnus-expunge-thread (thread)
8057   "Mark all articles in THREAD as read."
8058   (let* ((number (mail-header-number (car thread))))
8059     (incf gnus-newsgroup-expunged-tally)
8060     ;; We also mark as read here, if that's wanted.
8061     (setq gnus-newsgroup-unreads
8062           (delq number gnus-newsgroup-unreads))
8063     (if gnus-newsgroup-auto-expire
8064         (push number gnus-newsgroup-expirable)
8065       (push (cons number gnus-low-score-mark)
8066             gnus-newsgroup-reads)))
8067   ;; Go recursively through all subthreads.
8068   (mapcar 'gnus-expunge-thread (cdr thread)))
8069
8070 ;; Summary article oriented commands
8071
8072 (defun gnus-summary-refer-parent-article (n)
8073   "Refer parent article N times.
8074 If N is negative, go to ancestor -N instead.
8075 The difference between N and the number of articles fetched is returned."
8076   (interactive "p")
8077   (let ((skip 1)
8078         error header ref)
8079     (when (not (natnump n))
8080       (setq skip (abs n)
8081             n 1))
8082     (while (and (> n 0)
8083                 (not error))
8084       (setq header (gnus-summary-article-header))
8085       (if (and (eq (mail-header-number header)
8086                    (cdr gnus-article-current))
8087                (equal gnus-newsgroup-name
8088                       (car gnus-article-current)))
8089           ;; If we try to find the parent of the currently
8090           ;; displayed article, then we take a look at the actual
8091           ;; References header, since this is slightly more
8092           ;; reliable than the References field we got from the
8093           ;; server.
8094           (save-excursion
8095             (set-buffer gnus-original-article-buffer)
8096             (nnheader-narrow-to-headers)
8097             (unless (setq ref (message-fetch-field "references"))
8098               (when (setq ref (message-fetch-field "in-reply-to"))
8099                 (setq ref (gnus-extract-message-id-from-in-reply-to ref))))
8100             (widen))
8101         (setq ref
8102               ;; It's not the current article, so we take a bet on
8103               ;; the value we got from the server.
8104               (mail-header-references header)))
8105       (if (and ref
8106                (not (equal ref "")))
8107           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
8108             (gnus-message 1 "Couldn't find parent"))
8109         (gnus-message 1 "No references in article %d"
8110                       (gnus-summary-article-number))
8111         (setq error t))
8112       (decf n))
8113     (gnus-summary-position-point)
8114     n))
8115
8116 (defun gnus-summary-refer-references ()
8117   "Fetch all articles mentioned in the References header.
8118 Return the number of articles fetched."
8119   (interactive)
8120   (let ((ref (mail-header-references (gnus-summary-article-header)))
8121         (current (gnus-summary-article-number))
8122         (n 0))
8123     (if (or (not ref)
8124             (equal ref ""))
8125         (error "No References in the current article")
8126       ;; For each Message-ID in the References header...
8127       (while (string-match "<[^>]*>" ref)
8128         (incf n)
8129         ;; ... fetch that article.
8130         (gnus-summary-refer-article
8131          (prog1 (match-string 0 ref)
8132            (setq ref (substring ref (match-end 0))))))
8133       (gnus-summary-goto-subject current)
8134       (gnus-summary-position-point)
8135       n)))
8136
8137 (defun gnus-summary-refer-thread (&optional limit)
8138   "Fetch all articles in the current thread.
8139 If LIMIT (the numerical prefix), fetch that many old headers instead
8140 of what's specified by the `gnus-refer-thread-limit' variable."
8141   (interactive "P")
8142   (let ((id (mail-header-id (gnus-summary-article-header)))
8143         (limit (if limit (prefix-numeric-value limit)
8144                  gnus-refer-thread-limit)))
8145     (unless (eq gnus-fetch-old-headers 'invisible)
8146       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
8147       ;; Retrieve the headers and read them in.
8148       (if (eq (if (numberp limit)
8149                   (gnus-retrieve-headers
8150                    (list (min
8151                           (+ (mail-header-number
8152                               (gnus-summary-article-header))
8153                              limit)
8154                           gnus-newsgroup-end))
8155                    gnus-newsgroup-name (* limit 2))
8156                 ;; gnus-refer-thread-limit is t, i.e. fetch _all_
8157                 ;; headers.
8158                 (gnus-retrieve-headers (list gnus-newsgroup-end)
8159                                        gnus-newsgroup-name limit))
8160               'nov)
8161           (gnus-build-all-threads)
8162         (error "Can't fetch thread from back ends that don't support NOV"))
8163       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
8164     (gnus-summary-limit-include-thread id)))
8165
8166 (defun gnus-summary-refer-article (message-id)
8167   "Fetch an article specified by MESSAGE-ID."
8168   (interactive "sMessage-ID: ")
8169   (when (and (stringp message-id)
8170              (not (zerop (length message-id))))
8171     (setq message-id (gnus-replace-in-string message-id " " ""))
8172     ;; Construct the correct Message-ID if necessary.
8173     ;; Suggested by tale@pawl.rpi.edu.
8174     (unless (string-match "^<" message-id)
8175       (setq message-id (concat "<" message-id)))
8176     (unless (string-match ">$" message-id)
8177       (setq message-id (concat message-id ">")))
8178     ;; People often post MIDs from URLs, so unhex it:
8179     (unless (string-match "@" message-id)
8180       (setq message-id (gnus-url-unhex-string message-id)))
8181     (let* ((header (gnus-id-to-header message-id))
8182            (sparse (and header
8183                         (gnus-summary-article-sparse-p
8184                          (mail-header-number header))
8185                         (memq (mail-header-number header)
8186                               gnus-newsgroup-limit)))
8187            number)
8188       (cond
8189        ;; If the article is present in the buffer we just go to it.
8190        ((and header
8191              (or (not (gnus-summary-article-sparse-p
8192                        (mail-header-number header)))
8193                  sparse))
8194         (prog1
8195             (gnus-summary-goto-article
8196              (mail-header-number header) nil t)
8197           (when sparse
8198             (gnus-summary-update-article (mail-header-number header)))))
8199        (t
8200         ;; We fetch the article.
8201         (catch 'found
8202           (dolist (gnus-override-method (gnus-refer-article-methods))
8203             (when (and (gnus-check-server gnus-override-method)
8204                        ;; Fetch the header,
8205                        (setq number (gnus-summary-insert-subject message-id)))
8206               ;; and display the article.
8207               (gnus-summary-select-article nil nil nil number)
8208               (throw 'found t)))
8209           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
8210
8211 (defun gnus-refer-article-methods ()
8212   "Return a list of referable methods."
8213   (cond
8214    ;; No method, so we default to current and native.
8215    ((null gnus-refer-article-method)
8216     (list gnus-current-select-method gnus-select-method))
8217    ;; Current.
8218    ((eq 'current gnus-refer-article-method)
8219     (list gnus-current-select-method))
8220    ;; List of select methods.
8221    ((not (and (symbolp (car gnus-refer-article-method))
8222               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
8223     (let (out)
8224       (dolist (method gnus-refer-article-method)
8225         (push (if (eq 'current method)
8226                   gnus-current-select-method
8227                 method)
8228               out))
8229       (nreverse out)))
8230    ;; One single select method.
8231    (t
8232     (list gnus-refer-article-method))))
8233
8234 (defun gnus-summary-edit-parameters ()
8235   "Edit the group parameters of the current group."
8236   (interactive)
8237   (gnus-group-edit-group gnus-newsgroup-name 'params))
8238
8239 (defun gnus-summary-customize-parameters ()
8240   "Customize the group parameters of the current group."
8241   (interactive)
8242   (gnus-group-customize gnus-newsgroup-name))
8243
8244 (defun gnus-summary-enter-digest-group (&optional force)
8245   "Enter an nndoc group based on the current article.
8246 If FORCE, force a digest interpretation.  If not, try
8247 to guess what the document format is."
8248   (interactive "P")
8249   (let ((conf gnus-current-window-configuration))
8250     (save-window-excursion
8251       (save-excursion
8252         (let (gnus-article-prepare-hook
8253               gnus-display-mime-function
8254               gnus-break-pages)
8255           (gnus-summary-select-article))))
8256     (setq gnus-current-window-configuration conf)
8257     (let* ((name (format "%s-%d"
8258                          (gnus-group-prefixed-name
8259                           gnus-newsgroup-name (list 'nndoc ""))
8260                          (save-excursion
8261                            (set-buffer gnus-summary-buffer)
8262                            gnus-current-article)))
8263            (ogroup gnus-newsgroup-name)
8264            (params (append (gnus-info-params (gnus-get-info ogroup))
8265                            (list (cons 'to-group ogroup))
8266                            (list (cons 'parent-group ogroup))
8267                            (list (cons 'save-article-group ogroup))))
8268            (case-fold-search t)
8269            (buf (current-buffer))
8270            dig to-address)
8271       (save-excursion
8272         (set-buffer gnus-original-article-buffer)
8273         ;; Have the digest group inherit the main mail address of
8274         ;; the parent article.
8275         (when (setq to-address (or (gnus-fetch-field "reply-to")
8276                                    (gnus-fetch-field "from")))
8277           (setq params (append
8278                         (list (cons 'to-address
8279                                     (funcall gnus-decode-encoded-word-function
8280                                              to-address))))))
8281         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
8282         (insert-buffer-substring gnus-original-article-buffer)
8283         ;; Remove lines that may lead nndoc to misinterpret the
8284         ;; document type.
8285         (narrow-to-region
8286          (goto-char (point-min))
8287          (or (search-forward "\n\n" nil t) (point)))
8288         (goto-char (point-min))
8289         (delete-matching-lines "^Path:\\|^From ")
8290         (widen))
8291       (unwind-protect
8292           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
8293                     (gnus-newsgroup-ephemeral-ignored-charsets
8294                      gnus-newsgroup-ignored-charsets))
8295                 (gnus-group-read-ephemeral-group
8296                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
8297                               (nndoc-article-type
8298                                ,(if force 'mbox 'guess)))
8299                  t nil nil nil
8300                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
8301                                                         "ADAPT")))))
8302               ;; Make all postings to this group go to the parent group.
8303               (nconc (gnus-info-params (gnus-get-info name))
8304                      params)
8305             ;; Couldn't select this doc group.
8306             (switch-to-buffer buf)
8307             (gnus-set-global-variables)
8308             (gnus-configure-windows 'summary)
8309             (gnus-message 3 "Article couldn't be entered?"))
8310         (kill-buffer dig)))))
8311
8312 (defun gnus-summary-read-document (n)
8313   "Open a new group based on the current article(s).
8314 This will allow you to read digests and other similar
8315 documents as newsgroups.
8316 Obeys the standard process/prefix convention."
8317   (interactive "P")
8318   (let* ((articles (gnus-summary-work-articles n))
8319          (ogroup gnus-newsgroup-name)
8320          (params (append (gnus-info-params (gnus-get-info ogroup))
8321                          (list (cons 'to-group ogroup))))
8322          article group egroup groups vgroup)
8323     (while (setq article (pop articles))
8324       (setq group (format "%s-%d" gnus-newsgroup-name article))
8325       (gnus-summary-remove-process-mark article)
8326       (when (gnus-summary-display-article article)
8327         (save-excursion
8328           (with-temp-buffer
8329             (insert-buffer-substring gnus-original-article-buffer)
8330             ;; Remove some headers that may lead nndoc to make
8331             ;; the wrong guess.
8332             (message-narrow-to-head)
8333             (goto-char (point-min))
8334             (delete-matching-lines "^Path:\\|^From ")
8335             (widen)
8336             (if (setq egroup
8337                       (gnus-group-read-ephemeral-group
8338                        group `(nndoc ,group (nndoc-address ,(current-buffer))
8339                                      (nndoc-article-type guess))
8340                        t nil t))
8341                 (progn
8342                   ;; Make all postings to this group go to the parent group.
8343                   (nconc (gnus-info-params (gnus-get-info egroup))
8344                          params)
8345                   (push egroup groups))
8346               ;; Couldn't select this doc group.
8347               (gnus-error 3 "Article couldn't be entered"))))))
8348     ;; Now we have selected all the documents.
8349     (cond
8350      ((not groups)
8351       (error "None of the articles could be interpreted as documents"))
8352      ((gnus-group-read-ephemeral-group
8353        (setq vgroup (format
8354                      "nnvirtual:%s-%s" gnus-newsgroup-name
8355                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
8356        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
8357        t
8358        (cons (current-buffer) 'summary)))
8359      (t
8360       (error "Couldn't select virtual nndoc group")))))
8361
8362 (defun gnus-summary-isearch-article (&optional regexp-p)
8363   "Do incremental search forward on the current article.
8364 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
8365   (interactive "P")
8366   (let* ((gnus-inhibit-treatment t)
8367          (old (gnus-summary-select-article)))
8368     (gnus-configure-windows 'article)
8369     (gnus-eval-in-buffer-window gnus-article-buffer
8370       (save-restriction
8371         (widen)
8372         (when (eq 'old old)
8373           (gnus-article-show-all-headers))
8374         (goto-char (point-min))
8375         (isearch-forward regexp-p)))))
8376
8377 (defun gnus-summary-search-article-forward (regexp &optional backward)
8378   "Search for an article containing REGEXP forward.
8379 If BACKWARD, search backward instead."
8380   (interactive
8381    (list (read-string
8382           (format "Search article %s (regexp%s): "
8383                   (if current-prefix-arg "backward" "forward")
8384                   (if gnus-last-search-regexp
8385                       (concat ", default " gnus-last-search-regexp)
8386                     "")))
8387          current-prefix-arg))
8388   (if (string-equal regexp "")
8389       (setq regexp (or gnus-last-search-regexp ""))
8390     (setq gnus-last-search-regexp regexp)
8391     (setq gnus-article-before-search gnus-current-article))
8392   ;; Intentionally set gnus-last-article.
8393   (setq gnus-last-article gnus-article-before-search)
8394   (let ((gnus-last-article gnus-last-article))
8395     (if (gnus-summary-search-article regexp backward)
8396         (gnus-summary-show-thread)
8397       (error "Search failed: \"%s\"" regexp))))
8398
8399 (defun gnus-summary-search-article-backward (regexp)
8400   "Search for an article containing REGEXP backward."
8401   (interactive
8402    (list (read-string
8403           (format "Search article backward (regexp%s): "
8404                   (if gnus-last-search-regexp
8405                       (concat ", default " gnus-last-search-regexp)
8406                     "")))))
8407   (gnus-summary-search-article-forward regexp 'backward))
8408
8409 (eval-when-compile
8410   (defmacro gnus-summary-search-article-position-point (regexp backward)
8411     "Dehighlight the last matched text and goto the beginning position."
8412     (` (if (and gnus-summary-search-article-matched-data
8413                 (let ((text (caddr gnus-summary-search-article-matched-data))
8414                       (inhibit-read-only t)
8415                       buffer-read-only)
8416                   (delete-region
8417                    (goto-char (car gnus-summary-search-article-matched-data))
8418                    (cadr gnus-summary-search-article-matched-data))
8419                   (insert text)
8420                   (string-match (, regexp) text)))
8421            (if (, backward) (beginning-of-line) (end-of-line))
8422          (goto-char (if (, backward) (point-max) (point-min))))))
8423
8424   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
8425     "Place point where X-Face image is displayed."
8426     (if (featurep 'xemacs)
8427         (` (let ((end (if (search-forward "\n\n" nil t)
8428                           (goto-char (1- (point)))
8429                         (point-min)))
8430                  extent)
8431              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
8432              (unless (and (re-search-forward "^From:" end t)
8433                           (setq extent (extent-at (point)))
8434                           (extent-begin-glyph extent))
8435                (goto-char (, opoint)))))
8436       (` (let ((end (if (search-forward "\n\n" nil t)
8437                         (goto-char (1- (point)))
8438                       (point-min)))
8439                (start (or (search-backward "\n\n" nil t) (point-min))))
8440            (goto-char
8441             (or (text-property-any start end 'x-face-image t);; x-face-e21
8442                 (text-property-any start end 'x-face-mule-bitmap-image t)
8443                 (, opoint)))))))
8444
8445   (defmacro gnus-summary-search-article-highlight-matched-text
8446     (backward treated x-face)
8447     "Highlight matched text in the function `gnus-summary-search-article'."
8448     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
8449              (end (set-marker (make-marker) (match-end 0)))
8450              (inhibit-read-only t)
8451              buffer-read-only)
8452          (unless treated
8453            (let ((,@
8454                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
8455                     (mapcar
8456                      (lambda (item) (setq items (delq item items)))
8457                      '(gnus-treat-buttonize
8458                        gnus-treat-fill-article
8459                        gnus-treat-fill-long-lines
8460                        gnus-treat-emphasize
8461                        gnus-treat-highlight-headers
8462                        gnus-treat-highlight-citation
8463                        gnus-treat-highlight-signature
8464                        gnus-treat-overstrike
8465                        gnus-treat-display-x-face
8466                        gnus-treat-buttonize-head
8467                        gnus-treat-decode-article-as-default-mime-charset))
8468                     (static-if (featurep 'xemacs)
8469                         items
8470                       (cons '(x-face-mule-delete-x-face-field
8471                               (quote never))
8472                             items))))
8473                  (gnus-treat-display-x-face
8474                   (when (, x-face) gnus-treat-display-x-face)))
8475              (gnus-article-prepare-mime-display)))
8476          (goto-char (if (, backward) start end))
8477          (when (, x-face)
8478            (gnus-summary-search-article-highlight-goto-x-face (point)))
8479          (setq gnus-summary-search-article-matched-data
8480                (list start end (buffer-substring start end)))
8481          (unless (eq start end);; matched text has been deleted. :-<
8482            (put-text-property start end 'face
8483                               (or (find-face 'isearch)
8484                                   'secondary-selection))))))
8485   )
8486
8487 (defun gnus-summary-search-article (regexp &optional backward)
8488   "Search for an article containing REGEXP.
8489 Optional argument BACKWARD means do search for backward.
8490 `gnus-select-article-hook' is not called during the search."
8491   ;; We have to require this here to make sure that the following
8492   ;; dynamic binding isn't shadowed by autoloading.
8493   (require 'gnus-async)
8494   (require 'gnus-art)
8495   (let ((gnus-select-article-hook nil)  ;Disable hook.
8496         (gnus-article-prepare-hook nil)
8497         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8498         (gnus-use-article-prefetch nil)
8499         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8500         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8501         (gnus-visual nil)
8502         (gnus-keep-backlog nil)
8503         (gnus-break-pages nil)
8504         (gnus-summary-display-arrow nil)
8505         (gnus-updated-mode-lines nil)
8506         (gnus-auto-center-summary nil)
8507         (sum (current-buffer))
8508         (found nil)
8509         point treated)
8510     (gnus-save-hidden-threads
8511       (static-if (featurep 'xemacs)
8512           (let ((gnus-inhibit-treatment t))
8513             (setq treated (eq 'old (gnus-summary-select-article)))
8514             (when (and treated
8515                        (not (and (gnus-buffer-live-p gnus-article-buffer)
8516                                  (window-live-p (get-buffer-window
8517                                                  gnus-article-buffer t)))))
8518               (gnus-summary-select-article nil t)
8519               (setq treated nil)))
8520         (let ((gnus-inhibit-treatment t)
8521               (x-face-mule-delete-x-face-field 'never))
8522           (setq treated (eq 'old (gnus-summary-select-article)))
8523           (when (and treated
8524                      (not
8525                       (and (gnus-buffer-live-p gnus-article-buffer)
8526                            (window-live-p (get-buffer-window
8527                                            gnus-article-buffer t))
8528                            (or (not (string-match "^\\^X-Face:" regexp))
8529                                (with-current-buffer gnus-article-buffer
8530                                  gnus-summary-search-article-matched-data)))))
8531             (gnus-summary-select-article nil t)
8532             (setq treated nil))))
8533       (set-buffer gnus-article-buffer)
8534       (widen)
8535       (if treated
8536           (progn
8537             (gnus-article-show-all-headers)
8538             (gnus-summary-search-article-position-point regexp backward))
8539         (goto-char (if backward (point-max) (point-min))))
8540       (while (not found)
8541         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8542         (if (if backward
8543                 (re-search-backward regexp nil t)
8544               (re-search-forward regexp nil t))
8545             ;; We found the regexp.
8546             (progn
8547               (gnus-summary-search-article-highlight-matched-text
8548                backward treated (string-match "^\\^X-Face:" regexp))
8549               (setq found 'found)
8550               (forward-line
8551                (/ (- 2 (window-height
8552                         (get-buffer-window gnus-article-buffer t)))
8553                   2))
8554               (set-window-start
8555                (get-buffer-window (current-buffer))
8556                (point))
8557               (set-buffer sum)
8558               (setq point (point)))
8559           ;; We didn't find it, so we go to the next article.
8560           (set-buffer sum)
8561           (setq found 'not)
8562           (while (eq found 'not)
8563             (if (not (if backward (gnus-summary-find-prev)
8564                        (gnus-summary-find-next)))
8565                 ;; No more articles.
8566                 (setq found t)
8567               ;; Select the next article and adjust point.
8568               (unless (gnus-summary-article-sparse-p
8569                        (gnus-summary-article-number))
8570                 (setq found nil)
8571                 (let ((gnus-inhibit-treatment t))
8572                   (gnus-summary-select-article))
8573                 (setq treated nil)
8574                 (set-buffer gnus-article-buffer)
8575                 (widen)
8576                 (goto-char (if backward (point-max) (point-min))))))))
8577       (gnus-message 7 ""))
8578     ;; Return whether we found the regexp.
8579     (when (eq found 'found)
8580       (goto-char point)
8581       (gnus-summary-show-thread)
8582       (gnus-summary-goto-subject gnus-current-article)
8583       (gnus-summary-position-point)
8584       t)))
8585
8586 (defun gnus-find-matching-articles (header regexp)
8587   "Return a list of all articles that match REGEXP on HEADER.
8588 This search includes all articles in the current group that Gnus has
8589 fetched headers for, whether they are displayed or not."
8590   (let ((articles nil)
8591         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8592         (case-fold-search t))
8593     (dolist (header gnus-newsgroup-headers)
8594       (when (string-match regexp (funcall func header))
8595         (push (mail-header-number header) articles)))
8596     (nreverse articles)))
8597
8598 (defun gnus-summary-find-matching (header regexp &optional backward unread
8599                                           not-case-fold not-matching)
8600   "Return a list of all articles that match REGEXP on HEADER.
8601 The search stars on the current article and goes forwards unless
8602 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8603 If UNREAD is non-nil, only unread articles will
8604 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8605 in the comparisons. If NOT-MATCHING, return a list of all articles that
8606 not match REGEXP on HEADER."
8607   (let ((case-fold-search (not not-case-fold))
8608         articles d func)
8609     (if (consp header)
8610         (if (eq (car header) 'extra)
8611             (setq func
8612                   `(lambda (h)
8613                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8614                          "")))
8615           (error "%s is an invalid header" header))
8616       (unless (fboundp (intern (concat "mail-header-" header)))
8617         (error "%s is not a valid header" header))
8618       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8619     (dolist (d (if (eq backward 'all)
8620                    gnus-newsgroup-data
8621                  (gnus-data-find-list
8622                   (gnus-summary-article-number)
8623                   (gnus-data-list backward))))
8624       (when (and (or (not unread)       ; We want all articles...
8625                      (gnus-data-unread-p d)) ; Or just unreads.
8626                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8627                  (if not-matching
8628                      (not (string-match
8629                            regexp
8630                            (funcall func (gnus-data-header d))))
8631                    (string-match regexp
8632                                  (funcall func (gnus-data-header d)))))
8633         (push (gnus-data-number d) articles))) ; Success!
8634     (nreverse articles)))
8635
8636 (defun gnus-summary-execute-command (header regexp command &optional backward)
8637   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8638 If HEADER is an empty string (or nil), the match is done on the entire
8639 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8640   (interactive
8641    (list (let ((completion-ignore-case t))
8642            (completing-read
8643             "Header name: "
8644             (mapcar (lambda (header) (list (format "%s" header)))
8645                     (append
8646                      '("Number" "Subject" "From" "Lines" "Date"
8647                        "Message-ID" "Xref" "References" "Body")
8648                      gnus-extra-headers))
8649             nil 'require-match))
8650          (read-string "Regexp: ")
8651          (read-key-sequence "Command: ")
8652          current-prefix-arg))
8653   (when (equal header "Body")
8654     (setq header ""))
8655   ;; Hidden thread subtrees must be searched as well.
8656   (gnus-summary-show-all-threads)
8657   ;; We don't want to change current point nor window configuration.
8658   (save-excursion
8659     (save-window-excursion
8660       (let (gnus-visual
8661             gnus-treat-strip-trailing-blank-lines
8662             gnus-treat-strip-leading-blank-lines
8663             gnus-treat-strip-multiple-blank-lines
8664             gnus-treat-hide-boring-headers
8665             gnus-treat-fold-newsgroups
8666             gnus-article-prepare-hook)
8667         (gnus-message 6 "Executing %s..." (key-description command))
8668         ;; We'd like to execute COMMAND interactively so as to give arguments.
8669         (gnus-execute header regexp
8670                       `(call-interactively ',(key-binding command))
8671                       backward)
8672         (gnus-message 6 "Executing %s...done" (key-description command))))))
8673
8674 (defun gnus-summary-beginning-of-article ()
8675   "Scroll the article back to the beginning."
8676   (interactive)
8677   (gnus-summary-select-article)
8678   (gnus-configure-windows 'article)
8679   (gnus-eval-in-buffer-window gnus-article-buffer
8680     (widen)
8681     (goto-char (point-min))
8682     (when gnus-break-pages
8683       (gnus-narrow-to-page))))
8684
8685 (defun gnus-summary-end-of-article ()
8686   "Scroll to the end of the article."
8687   (interactive)
8688   (gnus-summary-select-article)
8689   (gnus-configure-windows 'article)
8690   (gnus-eval-in-buffer-window gnus-article-buffer
8691     (widen)
8692     (goto-char (point-max))
8693     (recenter -3)
8694     (when gnus-break-pages
8695       (when (re-search-backward page-delimiter nil t)
8696         (narrow-to-region (match-end 0) (point-max)))
8697       (gnus-narrow-to-page))))
8698
8699 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8700   "Truncate to LEN and quote all \"(\"'s in STRING."
8701   (gnus-replace-in-string (if (and len (> (length string) len))
8702                               (substring string 0 len)
8703                             string)
8704                           "[()]" "\\\\\\&"))
8705
8706 (defun gnus-summary-print-article (&optional filename n)
8707   "Generate and print a PostScript image of the process-marked (mail) articles.
8708
8709 If used interactively, print the current article if none are
8710 process-marked.  With prefix arg, prompt the user for the name of the
8711 file to save in.
8712
8713 When used from Lisp, accept two optional args FILENAME and N.  N means
8714 to print the next N articles.  If N is negative, print the N previous
8715 articles.  If N is nil and articles have been marked with the process
8716 mark, print these instead.
8717
8718 If the optional first argument FILENAME is nil, send the image to the
8719 printer.  If FILENAME is a string, save the PostScript image in a file with
8720 that name.  If FILENAME is a number, prompt the user for the name of the file
8721 to save in."
8722   (interactive (list (ps-print-preprint current-prefix-arg)))
8723   (dolist (article (gnus-summary-work-articles n))
8724     (gnus-summary-select-article nil nil 'pseudo article)
8725     (gnus-eval-in-buffer-window gnus-article-buffer
8726       (gnus-print-buffer))
8727     (gnus-summary-remove-process-mark article))
8728   (ps-despool filename))
8729
8730 (defun gnus-print-buffer ()
8731   (let ((buffer (generate-new-buffer " *print*")))
8732     (unwind-protect
8733         (progn
8734           (copy-to-buffer buffer (point-min) (point-max))
8735           (set-buffer buffer)
8736           (gnus-remove-text-with-property 'gnus-decoration)
8737           (when (gnus-visual-p 'article-highlight 'highlight)
8738             ;; Copy-to-buffer doesn't copy overlay.  So redo
8739             ;; highlight.
8740             (let ((gnus-article-buffer buffer))
8741               (gnus-article-highlight-citation t)
8742               (gnus-article-highlight-signature)
8743               (gnus-article-emphasize)
8744               (gnus-article-delete-invisible-text)))
8745           (let ((ps-left-header
8746                  (list
8747                   (concat "("
8748                           (gnus-summary-print-truncate-and-quote
8749                            (mail-header-subject gnus-current-headers)
8750                            66) ")")
8751                   (concat "("
8752                           (gnus-summary-print-truncate-and-quote
8753                            (mail-header-from gnus-current-headers)
8754                            45) ")")))
8755                 (ps-right-header
8756                  (list
8757                   "/pagenumberstring load"
8758                   (concat "("
8759                           (mail-header-date gnus-current-headers) ")"))))
8760             (gnus-run-hooks 'gnus-ps-print-hook)
8761             (save-excursion
8762               (if window-system
8763                   (ps-spool-buffer-with-faces)
8764                 (ps-spool-buffer)))))
8765       (kill-buffer buffer))))
8766
8767 (defun gnus-summary-show-article (&optional arg)
8768   "Force redisplaying of the current article.
8769 If ARG (the prefix) is a number, show the article with the charset
8770 defined in `gnus-summary-show-article-charset-alist', or the charset
8771 input.
8772 If ARG (the prefix) is non-nil and not a number, show the raw article
8773 without any article massaging functions being run.  Normally, the key
8774 strokes are `C-u g'."
8775   (interactive "P")
8776   (cond
8777    ((numberp arg)
8778     (gnus-summary-show-article t)
8779     (let* ((gnus-newsgroup-charset
8780             (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8781                 (mm-read-coding-system
8782                  "View as charset: " ;; actually it is coding system.
8783                  (save-excursion
8784                    (set-buffer gnus-article-buffer)
8785                    (mm-detect-coding-region (point) (point-max))))))
8786            (default-mime-charset gnus-newsgroup-charset)
8787            (gnus-newsgroup-ignored-charsets 'gnus-all))
8788       (gnus-summary-select-article nil 'force)
8789       (let ((deps gnus-newsgroup-dependencies)
8790             head header lines)
8791         (save-excursion
8792           (set-buffer gnus-original-article-buffer)
8793           (save-restriction
8794             (message-narrow-to-head)
8795             (setq head (buffer-string))
8796             (goto-char (point-min))
8797             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8798               (goto-char (point-max))
8799               (widen)
8800               (setq lines (1- (count-lines (point) (point-max))))))
8801           (with-temp-buffer
8802             (insert (format "211 %d Article retrieved.\n"
8803                             (cdr gnus-article-current)))
8804             (insert head)
8805             (if lines (insert (format "Lines: %d\n" lines)))
8806             (insert ".\n")
8807             (let ((nntp-server-buffer (current-buffer)))
8808               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8809         (gnus-data-set-header
8810          (gnus-data-find (cdr gnus-article-current))
8811          header)
8812         (gnus-summary-update-article-line
8813          (cdr gnus-article-current) header)
8814         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8815           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8816    ((not arg)
8817     ;; Select the article the normal way.
8818     (gnus-summary-select-article nil 'force))
8819    (t
8820     ;; We have to require this here to make sure that the following
8821     ;; dynamic binding isn't shadowed by autoloading.
8822     (require 'gnus-async)
8823     (require 'gnus-art)
8824     ;; Bind the article treatment functions to nil.
8825     (let ((gnus-have-all-headers t)
8826           gnus-article-prepare-hook
8827           gnus-article-decode-hook
8828           gnus-break-pages
8829           gnus-show-mime
8830           (gnus-inhibit-treatment t))
8831       (gnus-summary-select-article nil 'force))))
8832   (gnus-summary-goto-subject gnus-current-article)
8833   (gnus-summary-position-point))
8834
8835 (defun gnus-summary-show-raw-article ()
8836   "Show the raw article without any article massaging functions being run."
8837   (interactive)
8838   (gnus-summary-show-article t))
8839
8840 (defun gnus-summary-verbose-headers (&optional arg)
8841   "Toggle permanent full header display.
8842 If ARG is a positive number, turn header display on.
8843 If ARG is a negative number, turn header display off."
8844   (interactive "P")
8845   (setq gnus-show-all-headers
8846         (cond ((or (not (numberp arg))
8847                    (zerop arg))
8848                (not gnus-show-all-headers))
8849               ((natnump arg)
8850                t)))
8851   (gnus-summary-show-article))
8852
8853 (defun gnus-summary-toggle-header (&optional arg)
8854   "Show the headers if they are hidden, or hide them if they are shown.
8855 If ARG is a positive number, show the entire header.
8856 If ARG is a negative number, hide the unwanted header lines."
8857   (interactive "P")
8858   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8859                      (get-buffer-window gnus-article-buffer t))))
8860     (with-current-buffer gnus-article-buffer
8861       (widen)
8862       (article-narrow-to-head)
8863       (let* ((buffer-read-only nil)
8864              (inhibit-point-motion-hooks t)
8865              (hidden (if (numberp arg)
8866                          (>= arg 0)
8867                        (gnus-article-hidden-text-p 'headers)))
8868              s e)
8869         (delete-region (point-min) (point-max))
8870         (with-current-buffer gnus-original-article-buffer
8871           (goto-char (setq s (point-min)))
8872           (setq e (if (search-forward "\n\n" nil t)
8873                       (1- (point))
8874                     (point-max))))
8875         (insert-buffer-substring gnus-original-article-buffer s e)
8876         ;; In T-gnus, gnus-article-decode-hook doesn't contain
8877         ;; article-decode-encoded-words by default.
8878         (article-decode-encoded-words)
8879         (run-hooks 'gnus-article-decode-hook)
8880         (if hidden
8881             (let ((gnus-treat-hide-headers nil)
8882                   (gnus-treat-hide-boring-headers nil))
8883               (gnus-delete-wash-type 'headers)
8884               (gnus-treat-article 'head))
8885           (gnus-treat-article 'head))
8886         (widen)
8887         (if window
8888             (set-window-start window (goto-char (point-min))))
8889         (if gnus-break-pages
8890             (gnus-narrow-to-page)
8891           (when (gnus-visual-p 'page-marker)
8892             (let ((buffer-read-only nil))
8893               (gnus-remove-text-with-property 'gnus-prev)
8894               (gnus-remove-text-with-property 'gnus-next))))
8895         (gnus-set-mode-line 'article)))))
8896
8897 (defun gnus-summary-show-all-headers ()
8898   "Make all header lines visible."
8899   (interactive)
8900   (gnus-summary-toggle-header 1))
8901
8902 (defun gnus-summary-toggle-mime (&optional arg)
8903   "Toggle MIME processing.
8904 If ARG is a positive number, turn MIME processing on."
8905   (interactive "P")
8906   (setq gnus-show-mime
8907         (if (null arg)
8908             (not gnus-show-mime)
8909           (> (prefix-numeric-value arg) 0)))
8910   (gnus-summary-select-article t 'force))
8911
8912 (defun gnus-summary-caesar-message (&optional arg)
8913   "Caesar rotate the current article by 13.
8914 The numerical prefix specifies how many places to rotate each letter
8915 forward."
8916   (interactive "P")
8917   (gnus-summary-select-article)
8918   (let ((mail-header-separator ""))
8919     (gnus-eval-in-buffer-window gnus-article-buffer
8920       (save-restriction
8921         (widen)
8922         (let ((start (window-start))
8923               buffer-read-only)
8924           (message-caesar-buffer-body arg)
8925           (set-window-start (get-buffer-window (current-buffer)) start))))))
8926
8927 (autoload 'unmorse-region "morse"
8928   "Convert morse coded text in region to ordinary ASCII text."
8929   t)
8930
8931 (defun gnus-summary-morse-message (&optional arg)
8932   "Morse decode the current article."
8933   (interactive "P")
8934   (gnus-summary-select-article)
8935   (let ((mail-header-separator ""))
8936     (gnus-eval-in-buffer-window gnus-article-buffer
8937       (save-excursion
8938         (save-restriction
8939           (widen)
8940           (let ((pos (window-start))
8941                 buffer-read-only)
8942             (goto-char (point-min))
8943             (when (message-goto-body)
8944               (gnus-narrow-to-body))
8945             (goto-char (point-min))
8946             (while (re-search-forward "·" (point-max) t)
8947               (replace-match "."))
8948             (unmorse-region (point-min) (point-max))
8949             (widen)
8950             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8951
8952 (defun gnus-summary-stop-page-breaking ()
8953   "Stop page breaking in the current article."
8954   (interactive)
8955   (gnus-summary-select-article)
8956   (gnus-eval-in-buffer-window gnus-article-buffer
8957     (widen)
8958     (when (gnus-visual-p 'page-marker)
8959       (let ((buffer-read-only nil))
8960         (gnus-remove-text-with-property 'gnus-prev)
8961         (gnus-remove-text-with-property 'gnus-next))
8962       (setq gnus-page-broken nil))))
8963
8964 (defun gnus-summary-move-article (&optional n to-newsgroup
8965                                             select-method action)
8966   "Move the current article to a different newsgroup.
8967 If N is a positive number, move the N next articles.
8968 If N is a negative number, move the N previous articles.
8969 If N is nil and any articles have been marked with the process mark,
8970 move those articles instead.
8971 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8972 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8973 re-spool using this method.
8974
8975 When called interactively with TO-NEWSGROUP being nil, the value of
8976 the variable `gnus-move-split-methods' is used for finding a default
8977 for the target newsgroup.
8978
8979 For this function to work, both the current newsgroup and the
8980 newsgroup that you want to move to have to support the `request-move'
8981 and `request-accept' functions.
8982
8983 ACTION can be either `move' (the default), `crosspost' or `copy'."
8984   (interactive "P")
8985   (unless action
8986     (setq action 'move))
8987   ;; Check whether the source group supports the required functions.
8988   (cond ((and (eq action 'move)
8989               (not (gnus-check-backend-function
8990                     'request-move-article gnus-newsgroup-name)))
8991          (error "The current group does not support article moving"))
8992         ((and (eq action 'crosspost)
8993               (not (gnus-check-backend-function
8994                     'request-replace-article gnus-newsgroup-name)))
8995          (error "The current group does not support article editing")))
8996   (let ((articles (gnus-summary-work-articles n))
8997         (prefix (if (gnus-check-backend-function
8998                      'request-move-article gnus-newsgroup-name)
8999                     (gnus-group-real-prefix gnus-newsgroup-name)
9000                   ""))
9001         (names '((move "Move" "Moving")
9002                  (copy "Copy" "Copying")
9003                  (crosspost "Crosspost" "Crossposting")))
9004         (copy-buf (save-excursion
9005                     (nnheader-set-temp-buffer " *copy article*")))
9006         art-group to-method new-xref article to-groups)
9007     (unless (assq action names)
9008       (error "Unknown action %s" action))
9009     ;; Read the newsgroup name.
9010     (when (and (not to-newsgroup)
9011                (not select-method))
9012       (if (and gnus-move-split-methods
9013                (not
9014                 (and (memq gnus-current-article articles)
9015                      (gnus-buffer-live-p gnus-original-article-buffer))))
9016           ;; When `gnus-move-split-methods' is non-nil, we have to
9017           ;; select an article to give `gnus-read-move-group-name' an
9018           ;; opportunity to suggest an appropriate default.  However,
9019           ;; we needn't render or mark the article.
9020           (let ((gnus-display-mime-function nil)
9021                 (gnus-article-prepare-hook nil)
9022                 (gnus-mark-article-hook nil))
9023             (gnus-summary-select-article nil nil nil (car articles))))
9024       (setq to-newsgroup
9025             (gnus-read-move-group-name
9026              (cadr (assq action names))
9027              (symbol-value (intern (format "gnus-current-%s-group" action)))
9028              articles prefix))
9029       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
9030     (setq to-method (or select-method
9031                         (gnus-server-to-method
9032                          (gnus-group-method to-newsgroup))))
9033     ;; Check the method we are to move this article to...
9034     (unless (gnus-check-backend-function
9035              'request-accept-article (car to-method))
9036       (error "%s does not support article copying" (car to-method)))
9037     (unless (gnus-check-server to-method)
9038       (error "Can't open server %s" (car to-method)))
9039     (gnus-message 6 "%s to %s: %s..."
9040                   (caddr (assq action names))
9041                   (or (car select-method) to-newsgroup) articles)
9042     (while articles
9043       (setq article (pop articles))
9044       (setq
9045        art-group
9046        (cond
9047         ;; Move the article.
9048         ((eq action 'move)
9049          ;; Remove this article from future suppression.
9050          (gnus-dup-unsuppress-article article)
9051          (gnus-request-move-article
9052           article                       ; Article to move
9053           gnus-newsgroup-name           ; From newsgroup
9054           (nth 1 (gnus-find-method-for-group
9055                   gnus-newsgroup-name)) ; Server
9056           (list 'gnus-request-accept-article
9057                 to-newsgroup (list 'quote select-method)
9058                 (not articles) t)       ; Accept form
9059           (not articles)))              ; Only save nov last time
9060         ;; Copy the article.
9061         ((eq action 'copy)
9062          (save-excursion
9063            (set-buffer copy-buf)
9064            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
9065              (gnus-request-accept-article
9066               to-newsgroup select-method (not articles) t))))
9067         ;; Crosspost the article.
9068         ((eq action 'crosspost)
9069          (let ((xref (message-tokenize-header
9070                       (mail-header-xref (gnus-summary-article-header article))
9071                       " ")))
9072            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
9073                                   ":" (number-to-string article)))
9074            (unless xref
9075              (setq xref (list (system-name))))
9076            (setq new-xref
9077                  (concat
9078                   (mapconcat 'identity
9079                              (delete "Xref:" (delete new-xref xref))
9080                              " ")
9081                   " " new-xref))
9082            (save-excursion
9083              (set-buffer copy-buf)
9084              ;; First put the article in the destination group.
9085              (gnus-request-article-this-buffer article gnus-newsgroup-name)
9086              (when (consp (setq art-group
9087                                 (gnus-request-accept-article
9088                                  to-newsgroup select-method (not articles))))
9089                (setq new-xref (concat new-xref " " (car art-group)
9090                                       ":"
9091                                       (number-to-string (cdr art-group))))
9092                ;; Now we have the new Xrefs header, so we insert
9093                ;; it and replace the new article.
9094                (nnheader-replace-header "Xref" new-xref)
9095                (gnus-request-replace-article
9096                 (cdr art-group) to-newsgroup (current-buffer))
9097                art-group))))))
9098       (cond
9099        ((not art-group)
9100         (gnus-message 1 "Couldn't %s article %s: %s"
9101                       (cadr (assq action names)) article
9102                       (nnheader-get-report (car to-method))))
9103        ((eq art-group 'junk)
9104         (when (eq action 'move)
9105           (gnus-summary-mark-article article gnus-canceled-mark)
9106           (gnus-message 4 "Deleted article %s" article)
9107           ;; run the delete hook
9108           (run-hook-with-args 'gnus-summary-article-delete-hook
9109                               action
9110                               (gnus-data-header
9111                                (assoc article (gnus-data-list nil)))
9112                               gnus-newsgroup-name nil
9113                               select-method)))
9114        (t
9115         (let* ((pto-group (gnus-group-prefixed-name
9116                            (car art-group) to-method))
9117                (info (gnus-get-info pto-group))
9118                (to-group (gnus-info-group info))
9119                to-marks)
9120           ;; Update the group that has been moved to.
9121           (when (and info
9122                      (memq action '(move copy)))
9123             (unless (member to-group to-groups)
9124               (push to-group to-groups))
9125
9126             (unless (memq article gnus-newsgroup-unreads)
9127               (push 'read to-marks)
9128               (gnus-info-set-read
9129                info (gnus-add-to-range (gnus-info-read info)
9130                                        (list (cdr art-group)))))
9131
9132             ;; See whether the article is to be put in the cache.
9133             (let ((marks (if (gnus-group-auto-expirable-p to-group)
9134                              gnus-article-mark-lists
9135                            (delete '(expirable . expire)
9136                                    (copy-sequence gnus-article-mark-lists))))
9137                   (to-article (cdr art-group)))
9138
9139               ;; Enter the article into the cache in the new group,
9140               ;; if that is required.
9141               (when gnus-use-cache
9142                 (gnus-cache-possibly-enter-article
9143                  to-group to-article
9144                  (let ((header (copy-sequence
9145                                 (gnus-summary-article-header article))))
9146                    (mail-header-set-number header to-article)
9147                    header)
9148                  (memq article gnus-newsgroup-marked)
9149                  (memq article gnus-newsgroup-dormant)
9150                  (memq article gnus-newsgroup-unreads)))
9151
9152               (when gnus-preserve-marks
9153                 ;; Copy any marks over to the new group.
9154                 (when (and (equal to-group gnus-newsgroup-name)
9155                            (not (memq article gnus-newsgroup-unreads)))
9156                   ;; Mark this article as read in this group.
9157                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
9158                   (setcdr (gnus-active to-group) to-article)
9159                   (setcdr gnus-newsgroup-active to-article))
9160
9161                 (while marks
9162                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
9163                     (when (memq article (symbol-value
9164                                          (intern (format "gnus-newsgroup-%s"
9165                                                          (caar marks)))))
9166                       (push (cdar marks) to-marks)
9167                       ;; If the other group is the same as this group,
9168                       ;; then we have to add the mark to the list.
9169                       (when (equal to-group gnus-newsgroup-name)
9170                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
9171                              (cons to-article
9172                                    (symbol-value
9173                                     (intern (format "gnus-newsgroup-%s"
9174                                                     (caar marks)))))))
9175                       ;; Copy the marks to other group.
9176                       (gnus-add-marked-articles
9177                        to-group (cdar marks) (list to-article) info)))
9178                   (setq marks (cdr marks)))
9179
9180                 (gnus-request-set-mark
9181                  to-group (list (list (list to-article) 'add to-marks))))
9182
9183               (gnus-dribble-enter
9184                (concat "(gnus-group-set-info '"
9185                        (gnus-prin1-to-string (gnus-get-info to-group))
9186                        ")"))))
9187
9188           ;; Update the Xref header in this article to point to
9189           ;; the new crossposted article we have just created.
9190           (when (eq action 'crosspost)
9191             (save-excursion
9192               (set-buffer copy-buf)
9193               (gnus-request-article-this-buffer article gnus-newsgroup-name)
9194               (nnheader-replace-header "Xref" new-xref)
9195               (gnus-request-replace-article
9196                article gnus-newsgroup-name (current-buffer))))
9197
9198           ;; run the move/copy/crosspost/respool hook
9199           (run-hook-with-args 'gnus-summary-article-move-hook
9200                               action
9201                               (gnus-data-header
9202                                (assoc article (gnus-data-list nil)))
9203                               gnus-newsgroup-name
9204                               to-newsgroup
9205                               select-method))
9206
9207         ;;;!!!Why is this necessary?
9208         (set-buffer gnus-summary-buffer)
9209         
9210         (gnus-summary-goto-subject article)
9211         (when (eq action 'move)
9212           (gnus-summary-mark-article article gnus-canceled-mark))))
9213       (gnus-summary-remove-process-mark article))
9214     ;; Re-activate all groups that have been moved to.
9215     (save-excursion
9216       (set-buffer gnus-group-buffer)
9217       (let ((gnus-group-marked to-groups))
9218         (gnus-group-get-new-news-this-group nil t)))
9219
9220     (gnus-kill-buffer copy-buf)
9221     (gnus-summary-position-point)
9222     (gnus-set-mode-line 'summary)))
9223
9224 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
9225   "Move the current article to a different newsgroup.
9226 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
9227 When called interactively, if TO-NEWSGROUP is nil, use the value of
9228 the variable `gnus-move-split-methods' for finding a default target
9229 newsgroup.
9230 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
9231 re-spool using this method."
9232   (interactive "P")
9233   (gnus-summary-move-article n to-newsgroup select-method 'copy))
9234
9235 (defun gnus-summary-crosspost-article (&optional n)
9236   "Crosspost the current article to some other group."
9237   (interactive "P")
9238   (gnus-summary-move-article n nil nil 'crosspost))
9239
9240 (defcustom gnus-summary-respool-default-method nil
9241   "Default method type for respooling an article.
9242 If nil, use to the current newsgroup method."
9243   :type 'symbol
9244   :group 'gnus-summary-mail)
9245
9246 (defcustom gnus-summary-display-while-building nil
9247   "If non-nil, show and update the summary buffer as it's being built.
9248 If the value is t, update the buffer after every line is inserted.  If
9249 the value is an integer (N), update the display every N lines."
9250   :group 'gnus-thread
9251   :type '(choice (const :tag "off" nil)
9252                  number
9253                  (const :tag "frequently" t)))
9254
9255 (defun gnus-summary-respool-article (&optional n method)
9256   "Respool the current article.
9257 The article will be squeezed through the mail spooling process again,
9258 which means that it will be put in some mail newsgroup or other
9259 depending on `nnmail-split-methods'.
9260 If N is a positive number, respool the N next articles.
9261 If N is a negative number, respool the N previous articles.
9262 If N is nil and any articles have been marked with the process mark,
9263 respool those articles instead.
9264
9265 Respooling can be done both from mail groups and \"real\" newsgroups.
9266 In the former case, the articles in question will be moved from the
9267 current group into whatever groups they are destined to.  In the
9268 latter case, they will be copied into the relevant groups."
9269   (interactive
9270    (list current-prefix-arg
9271          (let* ((methods (gnus-methods-using 'respool))
9272                 (methname
9273                  (symbol-name (or gnus-summary-respool-default-method
9274                                   (car (gnus-find-method-for-group
9275                                         gnus-newsgroup-name)))))
9276                 (method
9277                  (gnus-completing-read-with-default
9278                   methname "What backend do you want to use when respooling?"
9279                   methods nil t nil 'gnus-mail-method-history))
9280                 ms)
9281            (cond
9282             ((zerop (length (setq ms (gnus-servers-using-backend
9283                                       (intern method)))))
9284              (list (intern method) ""))
9285             ((= 1 (length ms))
9286              (car ms))
9287             (t
9288              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
9289                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
9290                            ms-alist))))))))
9291   (unless method
9292     (error "No method given for respooling"))
9293   (if (assoc (symbol-name
9294               (car (gnus-find-method-for-group gnus-newsgroup-name)))
9295              (gnus-methods-using 'respool))
9296       (gnus-summary-move-article n nil method)
9297     (gnus-summary-copy-article n nil method)))
9298
9299 (defun gnus-summary-import-article (file &optional edit)
9300   "Import an arbitrary file into a mail newsgroup."
9301   (interactive "fImport file: \nP")
9302   (let ((group gnus-newsgroup-name)
9303         (now (current-time))
9304         atts lines group-art)
9305     (unless (gnus-check-backend-function 'request-accept-article group)
9306       (error "%s does not support article importing" group))
9307     (or (file-readable-p file)
9308         (not (file-regular-p file))
9309         (error "Can't read %s" file))
9310     (save-excursion
9311       (set-buffer (gnus-get-buffer-create " *import file*"))
9312       (erase-buffer)
9313       (nnheader-insert-file-contents file)
9314       (goto-char (point-min))
9315       (if (nnheader-article-p)
9316           (save-restriction
9317             (goto-char (point-min))
9318             (search-forward "\n\n" nil t)
9319             (narrow-to-region (point-min) (1- (point)))
9320             (goto-char (point-min))
9321             (unless (re-search-forward "^date:" nil t)
9322               (goto-char (point-max))
9323               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
9324         ;; This doesn't look like an article, so we fudge some headers.
9325         (setq atts (file-attributes file)
9326               lines (count-lines (point-min) (point-max)))
9327         (insert "From: " (read-string "From: ") "\n"
9328                 "Subject: " (read-string "Subject: ") "\n"
9329                 "Date: " (message-make-date (nth 5 atts)) "\n"
9330                 "Message-ID: " (message-make-message-id) "\n"
9331                 "Lines: " (int-to-string lines) "\n"
9332                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
9333       (setq group-art (gnus-request-accept-article group nil t))
9334       (kill-buffer (current-buffer)))
9335     (setq gnus-newsgroup-active (gnus-activate-group group))
9336     (forward-line 1)
9337     (gnus-summary-goto-article (cdr group-art) nil t)
9338     (when edit
9339       (gnus-summary-edit-article))))
9340
9341 (defun gnus-summary-create-article ()
9342   "Create an article in a mail newsgroup."
9343   (interactive)
9344   (let ((group gnus-newsgroup-name)
9345         (now (current-time))
9346         group-art)
9347     (unless (gnus-check-backend-function 'request-accept-article group)
9348       (error "%s does not support article importing" group))
9349     (save-excursion
9350       (set-buffer (gnus-get-buffer-create " *import file*"))
9351       (erase-buffer)
9352       (goto-char (point-min))
9353       ;; This doesn't look like an article, so we fudge some headers.
9354       (insert "From: " (read-string "From: ") "\n"
9355               "Subject: " (read-string "Subject: ") "\n"
9356               "Date: " (message-make-date now) "\n"
9357               "Message-ID: " (message-make-message-id) "\n")
9358       (setq group-art (gnus-request-accept-article group nil t))
9359       (kill-buffer (current-buffer)))
9360     (setq gnus-newsgroup-active (gnus-activate-group group))
9361     (forward-line 1)
9362     (gnus-summary-goto-article (cdr group-art) nil t)
9363     (gnus-summary-edit-article)))
9364
9365 (defun gnus-summary-article-posted-p ()
9366   "Say whether the current (mail) article is available from news as well.
9367 This will be the case if the article has both been mailed and posted."
9368   (interactive)
9369   (let ((id (mail-header-references (gnus-summary-article-header)))
9370         (gnus-override-method (car (gnus-refer-article-methods))))
9371     (if (gnus-request-head id "")
9372         (gnus-message 2 "The current message was found on %s"
9373                       gnus-override-method)
9374       (gnus-message 2 "The current message couldn't be found on %s"
9375                     gnus-override-method)
9376       nil)))
9377
9378 (defun gnus-summary-expire-articles (&optional now)
9379   "Expire all articles that are marked as expirable in the current group."
9380   (interactive)
9381   (when (and (not gnus-group-is-exiting-without-update-p)
9382              (gnus-check-backend-function
9383               'request-expire-articles gnus-newsgroup-name))
9384     ;; This backend supports expiry.
9385     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
9386            (expirable (if total
9387                           (progn
9388                             ;; We need to update the info for
9389                             ;; this group for `gnus-list-of-read-articles'
9390                             ;; to give us the right answer.
9391                             (gnus-run-hooks 'gnus-exit-group-hook)
9392                             (gnus-summary-update-info)
9393                             (gnus-list-of-read-articles gnus-newsgroup-name))
9394                         (setq gnus-newsgroup-expirable
9395                               (sort gnus-newsgroup-expirable '<))))
9396            (expiry-wait (if now 'immediate
9397                           (gnus-group-find-parameter
9398                            gnus-newsgroup-name 'expiry-wait)))
9399            (nnmail-expiry-target
9400             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
9401                 nnmail-expiry-target))
9402            es)
9403       (when expirable
9404         ;; There are expirable articles in this group, so we run them
9405         ;; through the expiry process.
9406         (gnus-message 6 "Expiring articles...")
9407         (unless (gnus-check-group gnus-newsgroup-name)
9408           (error "Can't open server for %s" gnus-newsgroup-name))
9409         ;; The list of articles that weren't expired is returned.
9410         (save-excursion
9411           (if expiry-wait
9412               (let ((nnmail-expiry-wait-function nil)
9413                     (nnmail-expiry-wait expiry-wait))
9414                 (setq es (gnus-request-expire-articles
9415                           expirable gnus-newsgroup-name)))
9416             (setq es (gnus-request-expire-articles
9417                       expirable gnus-newsgroup-name)))
9418           (unless total
9419             (setq gnus-newsgroup-expirable es))
9420           ;; We go through the old list of expirable, and mark all
9421           ;; really expired articles as nonexistent.
9422           (unless (eq es expirable)     ;If nothing was expired, we don't mark.
9423             (let ((gnus-use-cache nil))
9424               (dolist (article expirable)
9425                 (when (and (not (memq article es))
9426                            (gnus-data-find article))
9427                   (gnus-summary-mark-article article gnus-canceled-mark)
9428                   (run-hook-with-args 'gnus-summary-article-expire-hook
9429                                       'delete
9430                                       (gnus-data-header
9431                                        (assoc article (gnus-data-list nil)))
9432                                       gnus-newsgroup-name
9433                                       nil
9434                                       nil))))))
9435         (gnus-message 6 "Expiring articles...done")))))
9436
9437 (defun gnus-summary-expire-articles-now ()
9438   "Expunge all expirable articles in the current group.
9439 This means that *all* articles that are marked as expirable will be
9440 deleted forever, right now."
9441   (interactive)
9442   (or gnus-expert-user
9443       (gnus-yes-or-no-p
9444        "Are you really, really, really sure you want to delete all these messages? ")
9445       (error "Phew!"))
9446   (gnus-summary-expire-articles t))
9447
9448 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
9449 (defun gnus-summary-delete-article (&optional n)
9450   "Delete the N next (mail) articles.
9451 This command actually deletes articles.  This is not a marking
9452 command.  The article will disappear forever from your life, never to
9453 return.
9454
9455 If N is negative, delete backwards.
9456 If N is nil and articles have been marked with the process mark,
9457 delete these instead.
9458
9459 If `gnus-novice-user' is non-nil you will be asked for
9460 confirmation before the articles are deleted."
9461   (interactive "P")
9462   (unless (gnus-check-backend-function 'request-expire-articles
9463                                        gnus-newsgroup-name)
9464     (error "The current newsgroup does not support article deletion"))
9465   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
9466     (error "Couldn't open server"))
9467   ;; Compute the list of articles to delete.
9468   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
9469         (nnmail-expiry-target 'delete)
9470         not-deleted)
9471     (if (and gnus-novice-user
9472              (not (gnus-yes-or-no-p
9473                    (format "Do you really want to delete %s forever? "
9474                            (if (> (length articles) 1)
9475                                (format "these %s articles" (length articles))
9476                              "this article")))))
9477         ()
9478       ;; Delete the articles.
9479       (setq not-deleted (gnus-request-expire-articles
9480                          articles gnus-newsgroup-name 'force))
9481       (while articles
9482         (gnus-summary-remove-process-mark (car articles))
9483         ;; The backend might not have been able to delete the article
9484         ;; after all.
9485         (unless (memq (car articles) not-deleted)
9486           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
9487         (let* ((article (car articles))
9488                (id (mail-header-id (gnus-data-header
9489                                     (assoc article (gnus-data-list nil))))))
9490           (run-hook-with-args 'gnus-summary-article-delete-hook
9491                               'delete id gnus-newsgroup-name nil
9492                               nil))
9493         (setq articles (cdr articles)))
9494       (when not-deleted
9495         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
9496     (gnus-summary-position-point)
9497     (gnus-set-mode-line 'summary)
9498     not-deleted))
9499
9500 (defun gnus-summary-edit-article (&optional force)
9501   "Edit the current article.
9502 This will have permanent effect only in mail groups.
9503 If FORCE is non-nil, allow editing of articles even in read-only
9504 groups."
9505   (interactive "P")
9506   (save-excursion
9507     (set-buffer gnus-summary-buffer)
9508     (let ((mail-parse-charset gnus-newsgroup-charset)
9509           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
9510       (gnus-set-global-variables)
9511       (when (and (not force)
9512                  (gnus-group-read-only-p))
9513         (error "The current newsgroup does not support article editing"))
9514       (gnus-summary-show-article t)
9515       (gnus-article-edit-article
9516        'ignore
9517        `(lambda (no-highlight)
9518           (let ((mail-parse-charset ',gnus-newsgroup-charset)
9519                 (message-options message-options)
9520                 (message-options-set-recipient)
9521                 (mail-parse-ignored-charsets
9522                  ',gnus-newsgroup-ignored-charsets))
9523             (gnus-summary-edit-article-done
9524              ,(or (mail-header-references gnus-current-headers) "")
9525              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
9526
9527 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9528
9529 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9530                                                  no-highlight)
9531   "Make edits to the current article permanent."
9532   (interactive)
9533   (save-excursion
9534     ;; The buffer restriction contains the entire article if it exists.
9535     (when (article-goto-body)
9536       (let ((lines (count-lines (point) (point-max)))
9537             (length (- (point-max) (point)))
9538             (case-fold-search t)
9539             (body (copy-marker (point))))
9540         (goto-char (point-min))
9541         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9542           (delete-region (match-beginning 1) (match-end 1))
9543           (insert (number-to-string length)))
9544         (goto-char (point-min))
9545         (when (re-search-forward
9546                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9547           (delete-region (match-beginning 1) (match-end 1))
9548           (insert (number-to-string length)))
9549         (goto-char (point-min))
9550         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9551           (delete-region (match-beginning 1) (match-end 1))
9552           (insert (number-to-string lines))))))
9553   ;; Replace the article.
9554   (let ((buf (current-buffer)))
9555     (with-temp-buffer
9556       (insert-buffer-substring buf)
9557
9558       (if (and (not read-only)
9559                (not (gnus-request-replace-article
9560                      (cdr gnus-article-current) (car gnus-article-current)
9561                      (current-buffer) t)))
9562           (error "Couldn't replace article")
9563         ;; Update the summary buffer.
9564         (if (and references
9565                  (equal (message-tokenize-header references " ")
9566                         (message-tokenize-header
9567                          (or (message-fetch-field "references") "") " ")))
9568             ;; We only have to update this line.
9569             (save-excursion
9570               (save-restriction
9571                 (message-narrow-to-head)
9572                 (let ((head (buffer-string))
9573                       header)
9574                   (with-temp-buffer
9575                     (insert (format "211 %d Article retrieved.\n"
9576                                     (cdr gnus-article-current)))
9577                     (insert head)
9578                     (insert ".\n")
9579                     (let ((nntp-server-buffer (current-buffer)))
9580                       (setq header (car (gnus-get-newsgroup-headers
9581                                          nil t))))
9582                     (save-excursion
9583                       (set-buffer gnus-summary-buffer)
9584                       (gnus-data-set-header
9585                        (gnus-data-find (cdr gnus-article-current))
9586                        header)
9587                       (gnus-summary-update-article-line
9588                        (cdr gnus-article-current) header)
9589                       (if (gnus-summary-goto-subject
9590                            (cdr gnus-article-current) nil t)
9591                           (gnus-summary-update-secondary-mark
9592                            (cdr gnus-article-current))))))))
9593           ;; Update threads.
9594           (set-buffer (or buffer gnus-summary-buffer))
9595           (gnus-summary-update-article (cdr gnus-article-current))
9596           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9597               (gnus-summary-update-secondary-mark
9598                (cdr gnus-article-current))))
9599         ;; Prettify the article buffer again.
9600         (unless no-highlight
9601           (save-excursion
9602             (set-buffer gnus-article-buffer)
9603             ;;;!!! Fix this -- article should be rehighlighted.
9604             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9605             (set-buffer gnus-original-article-buffer)
9606             (gnus-request-article
9607              (cdr gnus-article-current)
9608              (car gnus-article-current) (current-buffer))))
9609         ;; Prettify the summary buffer line.
9610         (when (gnus-visual-p 'summary-highlight 'highlight)
9611           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9612
9613 (defun gnus-summary-edit-wash (key)
9614   "Perform editing command KEY in the article buffer."
9615   (interactive
9616    (list
9617     (progn
9618       (message "%s" (concat (this-command-keys) "- "))
9619       (read-char))))
9620   (message "")
9621   (gnus-summary-edit-article)
9622   (execute-kbd-macro (concat (this-command-keys) key))
9623   (gnus-article-edit-done))
9624
9625 ;;; Respooling
9626
9627 (defun gnus-summary-respool-query (&optional silent trace)
9628   "Query where the respool algorithm would put this article."
9629   (interactive)
9630   (let (gnus-mark-article-hook)
9631     (gnus-summary-select-article)
9632     (save-excursion
9633       (set-buffer gnus-original-article-buffer)
9634       (let ((groups (nnmail-article-group 'identity trace)))
9635         (unless silent
9636           (if groups
9637               (message "This message would go to %s"
9638                        (mapconcat 'car groups ", "))
9639             (message "This message would go to no groups"))
9640           groups)))))
9641
9642 (defun gnus-summary-respool-trace ()
9643   "Trace where the respool algorithm would put this article.
9644 Display a buffer showing all fancy splitting patterns which matched."
9645   (interactive)
9646   (gnus-summary-respool-query nil t))
9647
9648 ;; Summary marking commands.
9649
9650 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9651   "Mark articles which has the same subject as read, and then select the next.
9652 If UNMARK is positive, remove any kind of mark.
9653 If UNMARK is negative, tick articles."
9654   (interactive "P")
9655   (when unmark
9656     (setq unmark (prefix-numeric-value unmark)))
9657   (let ((count
9658          (gnus-summary-mark-same-subject
9659           (gnus-summary-article-subject) unmark)))
9660     ;; Select next unread article.  If auto-select-same mode, should
9661     ;; select the first unread article.
9662     (gnus-summary-next-article t (and gnus-auto-select-same
9663                                       (gnus-summary-article-subject)))
9664     (gnus-message 7 "%d article%s marked as %s"
9665                   count (if (= count 1) " is" "s are")
9666                   (if unmark "unread" "read"))))
9667
9668 (defun gnus-summary-kill-same-subject (&optional unmark)
9669   "Mark articles which has the same subject as read.
9670 If UNMARK is positive, remove any kind of mark.
9671 If UNMARK is negative, tick articles."
9672   (interactive "P")
9673   (when unmark
9674     (setq unmark (prefix-numeric-value unmark)))
9675   (let ((count
9676          (gnus-summary-mark-same-subject
9677           (gnus-summary-article-subject) unmark)))
9678     ;; If marked as read, go to next unread subject.
9679     (when (null unmark)
9680       ;; Go to next unread subject.
9681       (gnus-summary-next-subject 1 t))
9682     (gnus-message 7 "%d articles are marked as %s"
9683                   count (if unmark "unread" "read"))))
9684
9685 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9686   "Mark articles with same SUBJECT as read, and return marked number.
9687 If optional argument UNMARK is positive, remove any kinds of marks.
9688 If optional argument UNMARK is negative, mark articles as unread instead."
9689   (let ((count 1))
9690     (save-excursion
9691       (cond
9692        ((null unmark)                   ; Mark as read.
9693         (while (and
9694                 (progn
9695                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9696                   (gnus-summary-show-thread) t)
9697                 (gnus-summary-find-subject subject))
9698           (setq count (1+ count))))
9699        ((> unmark 0)                    ; Tick.
9700         (while (and
9701                 (progn
9702                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9703                   (gnus-summary-show-thread) t)
9704                 (gnus-summary-find-subject subject))
9705           (setq count (1+ count))))
9706        (t                               ; Mark as unread.
9707         (while (and
9708                 (progn
9709                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9710                   (gnus-summary-show-thread) t)
9711                 (gnus-summary-find-subject subject))
9712           (setq count (1+ count)))))
9713       (gnus-set-mode-line 'summary)
9714       ;; Return the number of marked articles.
9715       count)))
9716
9717 (defun gnus-summary-mark-as-processable (n &optional unmark)
9718   "Set the process mark on the next N articles.
9719 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9720 the process mark instead.  The difference between N and the actual
9721 number of articles marked is returned."
9722   (interactive "P")
9723   (if (and (null n) (gnus-region-active-p))
9724       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9725     (setq n (prefix-numeric-value n))
9726     (let ((backward (< n 0))
9727           (n (abs n)))
9728       (while (and
9729               (> n 0)
9730               (if unmark
9731                   (gnus-summary-remove-process-mark
9732                    (gnus-summary-article-number))
9733                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9734               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9735         (setq n (1- n)))
9736       (when (/= 0 n)
9737         (gnus-message 7 "No more articles"))
9738       (gnus-summary-recenter)
9739       (gnus-summary-position-point)
9740       n)))
9741
9742 (defun gnus-summary-unmark-as-processable (n)
9743   "Remove the process mark from the next N articles.
9744 If N is negative, unmark backward instead.  The difference between N and
9745 the actual number of articles unmarked is returned."
9746   (interactive "P")
9747   (gnus-summary-mark-as-processable n t))
9748
9749 (defun gnus-summary-unmark-all-processable ()
9750   "Remove the process mark from all articles."
9751   (interactive)
9752   (save-excursion
9753     (while gnus-newsgroup-processable
9754       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9755   (gnus-summary-position-point))
9756
9757 (defun gnus-summary-add-mark (article type)
9758   "Mark ARTICLE with a mark of TYPE."
9759   (let ((vtype (car (assq type gnus-article-mark-lists)))
9760         var)
9761     (if (not vtype)
9762         (error "No such mark type: %s" type)
9763       (setq var (intern (format "gnus-newsgroup-%s" type)))
9764       (set var (cons article (symbol-value var)))
9765       (if (memq type '(processable cached replied forwarded recent saved))
9766           (gnus-summary-update-secondary-mark article)
9767         ;;; !!! This is bogus.  We should find out what primary
9768         ;;; !!! mark we want to set.
9769         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9770
9771 (defun gnus-summary-mark-as-expirable (n)
9772   "Mark N articles forward as expirable.
9773 If N is negative, mark backward instead.  The difference between N and
9774 the actual number of articles marked is returned."
9775   (interactive "p")
9776   (gnus-summary-mark-forward n gnus-expirable-mark))
9777
9778 (defun gnus-summary-mark-as-spam (n)
9779   "Mark N articles forward as spam.
9780 If N is negative, mark backward instead.  The difference between N and
9781 the actual number of articles marked is returned."
9782   (interactive "p")
9783   (gnus-summary-mark-forward n gnus-spam-mark))
9784
9785 (defun gnus-summary-mark-article-as-replied (article)
9786   "Mark ARTICLE as replied to and update the summary line.
9787 ARTICLE can also be a list of articles."
9788   (interactive (list (gnus-summary-article-number)))
9789   (let ((articles (if (listp article) article (list article))))
9790     (dolist (article articles)
9791       (unless (numberp article)
9792         (error "%s is not a number" article))
9793       (push article gnus-newsgroup-replied)
9794       (let ((buffer-read-only nil))
9795         (when (gnus-summary-goto-subject article nil t)
9796           (gnus-summary-update-secondary-mark article))))))
9797
9798 (defun gnus-summary-mark-article-as-forwarded (article)
9799   "Mark ARTICLE as forwarded and update the summary line.
9800 ARTICLE can also be a list of articles."
9801   (let ((articles (if (listp article) article (list article))))
9802     (dolist (article articles)
9803       (push article gnus-newsgroup-forwarded)
9804       (let ((buffer-read-only nil))
9805         (when (gnus-summary-goto-subject article nil t)
9806           (gnus-summary-update-secondary-mark article))))))
9807
9808 (defun gnus-summary-set-bookmark (article)
9809   "Set a bookmark in current article."
9810   (interactive (list (gnus-summary-article-number)))
9811   (when (or (not (get-buffer gnus-article-buffer))
9812             (not gnus-current-article)
9813             (not gnus-article-current)
9814             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9815     (error "No current article selected"))
9816   ;; Remove old bookmark, if one exists.
9817   (gnus-pull article gnus-newsgroup-bookmarks)
9818   ;; Set the new bookmark, which is on the form
9819   ;; (article-number . line-number-in-body).
9820   (push
9821    (cons article
9822          (save-excursion
9823            (set-buffer gnus-article-buffer)
9824            (count-lines
9825             (min (point)
9826                  (save-excursion
9827                    (article-goto-body)
9828                    (point)))
9829             (point))))
9830    gnus-newsgroup-bookmarks)
9831   (gnus-message 6 "A bookmark has been added to the current article."))
9832
9833 (defun gnus-summary-remove-bookmark (article)
9834   "Remove the bookmark from the current article."
9835   (interactive (list (gnus-summary-article-number)))
9836   ;; Remove old bookmark, if one exists.
9837   (if (not (assq article gnus-newsgroup-bookmarks))
9838       (gnus-message 6 "No bookmark in current article.")
9839     (gnus-pull article gnus-newsgroup-bookmarks)
9840     (gnus-message 6 "Removed bookmark.")))
9841
9842 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9843 (defun gnus-summary-mark-as-dormant (n)
9844   "Mark N articles forward as dormant.
9845 If N is negative, mark backward instead.  The difference between N and
9846 the actual number of articles marked is returned."
9847   (interactive "p")
9848   (gnus-summary-mark-forward n gnus-dormant-mark))
9849
9850 (defun gnus-summary-set-process-mark (article)
9851   "Set the process mark on ARTICLE and update the summary line."
9852   (setq gnus-newsgroup-processable
9853         (cons article
9854               (delq article gnus-newsgroup-processable)))
9855   (when (gnus-summary-goto-subject article)
9856     (gnus-summary-show-thread)
9857     (gnus-summary-goto-subject article)
9858     (gnus-summary-update-secondary-mark article)))
9859
9860 (defun gnus-summary-remove-process-mark (article)
9861   "Remove the process mark from ARTICLE and update the summary line."
9862   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9863   (when (gnus-summary-goto-subject article)
9864     (gnus-summary-show-thread)
9865     (gnus-summary-goto-subject article)
9866     (gnus-summary-update-secondary-mark article)))
9867
9868 (defun gnus-summary-set-saved-mark (article)
9869   "Set the process mark on ARTICLE and update the summary line."
9870   (push article gnus-newsgroup-saved)
9871   (when (gnus-summary-goto-subject article)
9872     (gnus-summary-update-secondary-mark article)))
9873
9874 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9875   "Mark N articles as read forwards.
9876 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9877 The difference between N and the actual number of articles marked is
9878 returned.
9879 If NO-EXPIRE, auto-expiry will be inhibited."
9880   (interactive "p")
9881   (gnus-summary-show-thread)
9882   (let ((backward (< n 0))
9883         (gnus-summary-goto-unread
9884          (and gnus-summary-goto-unread
9885               (not (eq gnus-summary-goto-unread 'never))
9886               (not (memq mark (list gnus-unread-mark gnus-spam-mark
9887                                     gnus-ticked-mark gnus-dormant-mark)))))
9888         (n (abs n))
9889         (mark (or mark gnus-del-mark)))
9890     (while (and (> n 0)
9891                 (gnus-summary-mark-article nil mark no-expire)
9892                 (zerop (gnus-summary-next-subject
9893                         (if backward -1 1)
9894                         (and gnus-summary-goto-unread
9895                              (not (eq gnus-summary-goto-unread 'never)))
9896                         t)))
9897       (setq n (1- n)))
9898     (when (/= 0 n)
9899       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9900     (gnus-summary-recenter)
9901     (gnus-summary-position-point)
9902     (gnus-set-mode-line 'summary)
9903     n))
9904
9905 (defun gnus-summary-mark-article-as-read (mark)
9906   "Mark the current article quickly as read with MARK."
9907   (let ((article (gnus-summary-article-number)))
9908     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9909     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9910     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9911     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9912     (push (cons article mark) gnus-newsgroup-reads)
9913     ;; Possibly remove from cache, if that is used.
9914     (when gnus-use-cache
9915       (gnus-cache-enter-remove-article article))
9916     ;; Allow the backend to change the mark.
9917     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9918     ;; Check for auto-expiry.
9919     (when (and gnus-newsgroup-auto-expire
9920                (memq mark gnus-auto-expirable-marks))
9921       (setq mark gnus-expirable-mark)
9922       ;; Let the backend know about the mark change.
9923       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9924       (push article gnus-newsgroup-expirable))
9925     ;; Set the mark in the buffer.
9926     (gnus-summary-update-mark mark 'unread)
9927     t))
9928
9929 (defun gnus-summary-mark-article-as-unread (mark)
9930   "Mark the current article quickly as unread with MARK."
9931   (let* ((article (gnus-summary-article-number))
9932          (old-mark (gnus-summary-article-mark article)))
9933     ;; Allow the backend to change the mark.
9934     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9935     (if (eq mark old-mark)
9936         t
9937       (if (<= article 0)
9938           (progn
9939             (gnus-error 1 "Can't mark negative article numbers")
9940             nil)
9941         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9942         (setq gnus-newsgroup-spam-marked
9943               (delq article gnus-newsgroup-spam-marked))
9944         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9945         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9946         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9947         (cond ((= mark gnus-ticked-mark)
9948                (setq gnus-newsgroup-marked
9949                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9950                                               article)))
9951               ((= mark gnus-spam-mark)
9952                (setq gnus-newsgroup-spam-marked
9953                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9954                                               article)))
9955               ((= mark gnus-dormant-mark)
9956                (setq gnus-newsgroup-dormant
9957                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9958                                               article)))
9959               (t
9960                (setq gnus-newsgroup-unreads
9961                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9962                                               article))))
9963         (gnus-pull article gnus-newsgroup-reads)
9964
9965         ;; See whether the article is to be put in the cache.
9966         (and gnus-use-cache
9967              (vectorp (gnus-summary-article-header article))
9968              (save-excursion
9969                (gnus-cache-possibly-enter-article
9970                 gnus-newsgroup-name article
9971                 (gnus-summary-article-header article)
9972                 (= mark gnus-ticked-mark)
9973                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9974
9975         ;; Fix the mark.
9976         (gnus-summary-update-mark mark 'unread)
9977         t))))
9978
9979 (defun gnus-summary-mark-article (&optional article mark no-expire)
9980   "Mark ARTICLE with MARK.  MARK can be any character.
9981 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9982 `??' (dormant) and `?E' (expirable).
9983 If MARK is nil, then the default character `?r' is used.
9984 If ARTICLE is nil, then the article on the current line will be
9985 marked.
9986 If NO-EXPIRE, auto-expiry will be inhibited."
9987   ;; The mark might be a string.
9988   (when (stringp mark)
9989     (setq mark (aref mark 0)))
9990   ;; If no mark is given, then we check auto-expiring.
9991   (when (null mark)
9992     (setq mark gnus-del-mark))
9993   (when (and (not no-expire)
9994              gnus-newsgroup-auto-expire
9995              (memq mark gnus-auto-expirable-marks))
9996     (setq mark gnus-expirable-mark))
9997   (let ((article (or article (gnus-summary-article-number)))
9998         (old-mark (gnus-summary-article-mark article)))
9999     ;; Allow the backend to change the mark.
10000     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
10001     (if (eq mark old-mark)
10002         t
10003       (unless article
10004         (error "No article on current line"))
10005       (if (not (if (or (= mark gnus-unread-mark)
10006                        (= mark gnus-ticked-mark)
10007                        (= mark gnus-spam-mark)
10008                        (= mark gnus-dormant-mark))
10009                    (gnus-mark-article-as-unread article mark)
10010                  (gnus-mark-article-as-read article mark)))
10011           t
10012         ;; See whether the article is to be put in the cache.
10013         (and gnus-use-cache
10014              (not (= mark gnus-canceled-mark))
10015              (vectorp (gnus-summary-article-header article))
10016              (save-excursion
10017                (gnus-cache-possibly-enter-article
10018                 gnus-newsgroup-name article
10019                 (gnus-summary-article-header article)
10020                 (= mark gnus-ticked-mark)
10021                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
10022
10023         (when (gnus-summary-goto-subject article nil t)
10024           (let ((buffer-read-only nil))
10025             (gnus-summary-show-thread)
10026             ;; Fix the mark.
10027             (gnus-summary-update-mark mark 'unread)
10028             t))))))
10029
10030 (defun gnus-summary-update-secondary-mark (article)
10031   "Update the secondary (read, process, cache) mark."
10032   (gnus-summary-update-mark
10033    (cond ((memq article gnus-newsgroup-processable)
10034           gnus-process-mark)
10035          ((memq article gnus-newsgroup-cached)
10036           gnus-cached-mark)
10037          ((memq article gnus-newsgroup-replied)
10038           gnus-replied-mark)
10039          ((memq article gnus-newsgroup-forwarded)
10040           gnus-forwarded-mark)
10041          ((memq article gnus-newsgroup-saved)
10042           gnus-saved-mark)
10043          ((memq article gnus-newsgroup-recent)
10044           gnus-recent-mark)
10045          ((memq article gnus-newsgroup-unseen)
10046           gnus-unseen-mark)
10047          (t gnus-no-mark))
10048    'replied)
10049   (when (gnus-visual-p 'summary-highlight 'highlight)
10050     (gnus-run-hooks 'gnus-summary-update-hook))
10051   t)
10052
10053 (defun gnus-summary-update-download-mark (article)
10054   "Update the download mark."
10055   (gnus-summary-update-mark
10056    (cond ((memq article gnus-newsgroup-undownloaded)
10057           gnus-undownloaded-mark)
10058          (gnus-newsgroup-agentized
10059           gnus-downloaded-mark)
10060          (t
10061           gnus-no-mark))
10062    'download)
10063   (gnus-summary-update-line t)
10064   t)
10065
10066 (defun gnus-summary-update-mark (mark type)
10067   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
10068         (buffer-read-only nil))
10069     (re-search-backward "[\n\r]" (point-at-bol) 'move-to-limit)
10070     (when forward
10071       (when (looking-at "\r")
10072         (incf forward))
10073       (when (<= (+ forward (point)) (point-max))
10074         ;; Go to the right position on the line.
10075         (goto-char (+ forward (point)))
10076         ;; Replace the old mark with the new mark.
10077         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
10078         ;; Optionally update the marks by some user rule.
10079         (when (eq type 'unread)
10080           (gnus-data-set-mark
10081            (gnus-data-find (gnus-summary-article-number)) mark)
10082           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
10083
10084 (defun gnus-mark-article-as-read (article &optional mark)
10085   "Enter ARTICLE in the pertinent lists and remove it from others."
10086   ;; Make the article expirable.
10087   (let ((mark (or mark gnus-del-mark)))
10088     (setq gnus-newsgroup-expirable
10089           (if (= mark gnus-expirable-mark)
10090               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
10091             (delq article gnus-newsgroup-expirable)))
10092     ;; Remove from unread and marked lists.
10093     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10094     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
10095     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
10096     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
10097     (push (cons article mark) gnus-newsgroup-reads)
10098     ;; Possibly remove from cache, if that is used.
10099     (when gnus-use-cache
10100       (gnus-cache-enter-remove-article article))
10101     t))
10102
10103 (defun gnus-mark-article-as-unread (article &optional mark)
10104   "Enter ARTICLE in the pertinent lists and remove it from others."
10105   (let ((mark (or mark gnus-ticked-mark)))
10106     (if (<= article 0)
10107         (progn
10108           (gnus-error 1 "Can't mark negative article numbers")
10109           nil)
10110       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
10111             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
10112             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
10113             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
10114             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
10115
10116       ;; Unsuppress duplicates?
10117       (when gnus-suppress-duplicates
10118         (gnus-dup-unsuppress-article article))
10119
10120       (cond ((= mark gnus-ticked-mark)
10121              (setq gnus-newsgroup-marked
10122                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
10123             ((= mark gnus-spam-mark)
10124              (setq gnus-newsgroup-spam-marked
10125                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
10126                                             article)))
10127             ((= mark gnus-dormant-mark)
10128              (setq gnus-newsgroup-dormant
10129                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
10130             (t
10131              (setq gnus-newsgroup-unreads
10132                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
10133       (gnus-pull article gnus-newsgroup-reads)
10134       t)))
10135
10136 (defalias 'gnus-summary-mark-as-unread-forward
10137   'gnus-summary-tick-article-forward)
10138 (make-obsolete 'gnus-summary-mark-as-unread-forward
10139                'gnus-summary-tick-article-forward)
10140 (defun gnus-summary-tick-article-forward (n)
10141   "Tick N articles forwards.
10142 If N is negative, tick backwards instead.
10143 The difference between N and the number of articles ticked is returned."
10144   (interactive "p")
10145   (gnus-summary-mark-forward n gnus-ticked-mark))
10146
10147 (defalias 'gnus-summary-mark-as-unread-backward
10148   'gnus-summary-tick-article-backward)
10149 (make-obsolete 'gnus-summary-mark-as-unread-backward
10150                'gnus-summary-tick-article-backward)
10151 (defun gnus-summary-tick-article-backward (n)
10152   "Tick N articles backwards.
10153 The difference between N and the number of articles ticked is returned."
10154   (interactive "p")
10155   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
10156
10157 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10158 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
10159 (defun gnus-summary-tick-article (&optional article clear-mark)
10160   "Mark current article as unread.
10161 Optional 1st argument ARTICLE specifies article number to be marked as unread.
10162 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
10163   (interactive)
10164   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
10165                                        gnus-ticked-mark)))
10166
10167 (defun gnus-summary-mark-as-read-forward (n)
10168   "Mark N articles as read forwards.
10169 If N is negative, mark backwards instead.
10170 The difference between N and the actual number of articles marked is
10171 returned."
10172   (interactive "p")
10173   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
10174
10175 (defun gnus-summary-mark-as-read-backward (n)
10176   "Mark the N articles as read backwards.
10177 The difference between N and the actual number of articles marked is
10178 returned."
10179   (interactive "p")
10180   (gnus-summary-mark-forward
10181    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
10182
10183 (defun gnus-summary-mark-as-read (&optional article mark)
10184   "Mark current article as read.
10185 ARTICLE specifies the article to be marked as read.
10186 MARK specifies a string to be inserted at the beginning of the line."
10187   (gnus-summary-mark-article article mark))
10188
10189 (defun gnus-summary-clear-mark-forward (n)
10190   "Clear marks from N articles forward.
10191 If N is negative, clear backward instead.
10192 The difference between N and the number of marks cleared is returned."
10193   (interactive "p")
10194   (gnus-summary-mark-forward n gnus-unread-mark))
10195
10196 (defun gnus-summary-clear-mark-backward (n)
10197   "Clear marks from N articles backward.
10198 The difference between N and the number of marks cleared is returned."
10199   (interactive "p")
10200   (gnus-summary-mark-forward (- n) gnus-unread-mark))
10201
10202 (defun gnus-summary-mark-unread-as-read ()
10203   "Intended to be used by `gnus-summary-mark-article-hook'."
10204   (when (memq gnus-current-article gnus-newsgroup-unreads)
10205     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
10206
10207 (defun gnus-summary-mark-read-and-unread-as-read (&optional new-mark)
10208   "Intended to be used by `gnus-summary-mark-article-hook'."
10209   (let ((mark (gnus-summary-article-mark)))
10210     (when (or (gnus-unread-mark-p mark)
10211               (gnus-read-mark-p mark))
10212       (gnus-summary-mark-article gnus-current-article
10213                                  (or new-mark gnus-read-mark)))))
10214
10215 (defun gnus-summary-mark-current-read-and-unread-as-read (&optional new-mark)
10216   "Intended to be used by `gnus-summary-mark-article-hook'."
10217   (let ((mark (gnus-summary-article-mark)))
10218     (when (or (gnus-unread-mark-p mark)
10219               (gnus-read-mark-p mark))
10220       (gnus-summary-mark-article (gnus-summary-article-number)
10221                                  (or new-mark gnus-read-mark)))))
10222
10223 (defun gnus-summary-mark-unread-as-ticked ()
10224   "Intended to be used by `gnus-summary-mark-article-hook'."
10225   (when (memq gnus-current-article gnus-newsgroup-unreads)
10226     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
10227
10228 (defun gnus-summary-mark-region-as-read (point mark all)
10229   "Mark all unread articles between point and mark as read.
10230 If given a prefix, mark all articles between point and mark as read,
10231 even ticked and dormant ones."
10232   (interactive "r\nP")
10233   (save-excursion
10234     (let (article)
10235       (goto-char point)
10236       (beginning-of-line)
10237       (while (and
10238               (< (point) mark)
10239               (progn
10240                 (when (or all
10241                           (memq (setq article (gnus-summary-article-number))
10242                                 gnus-newsgroup-unreads))
10243                   (gnus-summary-mark-article article gnus-del-mark))
10244                 t)
10245               (gnus-summary-find-next))))))
10246
10247 (defun gnus-summary-mark-below (score mark)
10248   "Mark articles with score less than SCORE with MARK."
10249   (interactive "P\ncMark: ")
10250   (setq score (if score
10251                   (prefix-numeric-value score)
10252                 (or gnus-summary-default-score 0)))
10253   (save-excursion
10254     (set-buffer gnus-summary-buffer)
10255     (goto-char (point-min))
10256     (while
10257         (progn
10258           (and (< (gnus-summary-article-score) score)
10259                (gnus-summary-mark-article nil mark))
10260           (gnus-summary-find-next)))))
10261
10262 (defun gnus-summary-kill-below (&optional score)
10263   "Mark articles with score below SCORE as read."
10264   (interactive "P")
10265   (gnus-summary-mark-below score gnus-killed-mark))
10266
10267 (defun gnus-summary-clear-above (&optional score)
10268   "Clear all marks from articles with score above SCORE."
10269   (interactive "P")
10270   (gnus-summary-mark-above score gnus-unread-mark))
10271
10272 (defun gnus-summary-tick-above (&optional score)
10273   "Tick all articles with score above SCORE."
10274   (interactive "P")
10275   (gnus-summary-mark-above score gnus-ticked-mark))
10276
10277 (defun gnus-summary-mark-above (score mark)
10278   "Mark articles with score over SCORE with MARK."
10279   (interactive "P\ncMark: ")
10280   (setq score (if score
10281                   (prefix-numeric-value score)
10282                 (or gnus-summary-default-score 0)))
10283   (save-excursion
10284     (set-buffer gnus-summary-buffer)
10285     (goto-char (point-min))
10286     (while (and (progn
10287                   (when (> (gnus-summary-article-score) score)
10288                     (gnus-summary-mark-article nil mark))
10289                   t)
10290                 (gnus-summary-find-next)))))
10291
10292 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
10293 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
10294 (defun gnus-summary-limit-include-expunged (&optional no-error)
10295   "Display all the hidden articles that were expunged for low scores."
10296   (interactive)
10297   (let ((buffer-read-only nil))
10298     (let ((scored gnus-newsgroup-scored)
10299           headers h)
10300       (while scored
10301         (unless (gnus-summary-article-header (caar scored))
10302           (and (setq h (gnus-number-to-header (caar scored)))
10303                (< (cdar scored) gnus-summary-expunge-below)
10304                (push h headers)))
10305         (setq scored (cdr scored)))
10306       (if (not headers)
10307           (when (not no-error)
10308             (error "No expunged articles hidden"))
10309         (goto-char (point-min))
10310         (push gnus-newsgroup-limit gnus-newsgroup-limits)
10311         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
10312         (mapcar (lambda (x) (push (mail-header-number x)
10313                                   gnus-newsgroup-limit))
10314                 headers)
10315         (gnus-summary-prepare-unthreaded (nreverse headers))
10316         (goto-char (point-min))
10317         (gnus-summary-position-point)
10318         t))))
10319
10320 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
10321   "Mark all unread articles in this newsgroup as read.
10322 If prefix argument ALL is non-nil, ticked and dormant articles will
10323 also be marked as read.
10324 If QUIETLY is non-nil, no questions will be asked.
10325
10326 If TO-HERE is non-nil, it should be a point in the buffer.  All
10327 articles before (after, if REVERSE is set) this point will be marked
10328 as read.
10329
10330 Note that this function will only catch up the unread article
10331 in the current summary buffer limitation.
10332
10333 The number of articles marked as read is returned."
10334   (interactive "P")
10335   (prog1
10336       (save-excursion
10337         (when (or quietly
10338                   (not gnus-interactive-catchup) ;Without confirmation?
10339                   gnus-expert-user
10340                   (gnus-y-or-n-p
10341                    (if all
10342                        "Mark absolutely all articles as read? "
10343                      "Mark all unread articles as read? ")))
10344           (if (and not-mark
10345                    (not gnus-newsgroup-adaptive)
10346                    (not gnus-newsgroup-auto-expire)
10347                    (not gnus-suppress-duplicates)
10348                    (or (not gnus-use-cache)
10349                        (eq gnus-use-cache 'passive)))
10350               (progn
10351                 (when all
10352                   (setq gnus-newsgroup-marked nil
10353                         gnus-newsgroup-spam-marked nil
10354                         gnus-newsgroup-dormant nil))
10355                 (setq gnus-newsgroup-unreads
10356                       (gnus-sorted-nunion
10357                        (gnus-intersection gnus-newsgroup-unreads
10358                                           gnus-newsgroup-downloadable)
10359                        gnus-newsgroup-unfetched)))
10360             ;; We actually mark all articles as canceled, which we
10361             ;; have to do when using auto-expiry or adaptive scoring.
10362             (gnus-summary-show-all-threads)
10363             (if (and to-here reverse)
10364                 (progn
10365                   (goto-char to-here)
10366                   (gnus-summary-mark-current-read-and-unread-as-read
10367                    gnus-catchup-mark)
10368                   (while (gnus-summary-find-next (not all))
10369                     (gnus-summary-mark-article-as-read gnus-catchup-mark)))
10370               (when (gnus-summary-first-subject (not all))
10371                 (while (and
10372                         (if to-here (< (point) to-here) t)
10373                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
10374                         (gnus-summary-find-next (not all))))))
10375             (gnus-set-mode-line 'summary))
10376           t))
10377     (gnus-summary-position-point)))
10378
10379 (defun gnus-summary-catchup-to-here (&optional all)
10380   "Mark all unticked articles before the current one as read.
10381 If ALL is non-nil, also mark ticked and dormant articles as read."
10382   (interactive "P")
10383   (save-excursion
10384     (gnus-save-hidden-threads
10385       (let ((beg (point)))
10386         ;; We check that there are unread articles.
10387         (when (or all (gnus-summary-find-prev))
10388           (gnus-summary-catchup all t beg)))))
10389   (gnus-summary-position-point))
10390
10391 (defun gnus-summary-catchup-from-here (&optional all)
10392   "Mark all unticked articles after (and including) the current one as read.
10393 If ALL is non-nil, also mark ticked and dormant articles as read."
10394   (interactive "P")
10395   (save-excursion
10396     (gnus-save-hidden-threads
10397       (let ((beg (point)))
10398         ;; We check that there are unread articles.
10399         (when (or all (gnus-summary-find-next))
10400           (gnus-summary-catchup all t beg nil t)))))
10401   (gnus-summary-position-point))
10402
10403 (defun gnus-summary-catchup-all (&optional quietly)
10404   "Mark all articles in this newsgroup as read.
10405 This command is dangerous.  Normally, you want \\[gnus-summary-catchup]
10406 instead, which marks only unread articles as read."
10407   (interactive "P")
10408   (gnus-summary-catchup t quietly))
10409
10410 (defun gnus-summary-catchup-and-exit (&optional all quietly)
10411   "Mark all unread articles in this group as read, then exit.
10412 If prefix argument ALL is non-nil, all articles are marked as read.
10413 If QUIETLY is non-nil, no questions will be asked."
10414   (interactive "P")
10415   (when (gnus-summary-catchup all quietly nil 'fast)
10416     ;; Select next newsgroup or exit.
10417     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
10418              (eq gnus-auto-select-next 'quietly))
10419         (gnus-summary-next-group nil)
10420       (gnus-summary-exit))))
10421
10422 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
10423   "Mark all articles in this newsgroup as read, and then exit.
10424 This command is dangerous.  Normally, you want \\[gnus-summary-catchup-and-exit]
10425 instead, which marks only unread articles as read."
10426   (interactive "P")
10427   (gnus-summary-catchup-and-exit t quietly))
10428
10429 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
10430   "Mark all articles in this group as read and select the next group.
10431 If given a prefix, mark all articles, unread as well as ticked, as
10432 read."
10433   (interactive "P")
10434   (save-excursion
10435     (gnus-summary-catchup all))
10436   (gnus-summary-next-group))
10437
10438 ;;;
10439 ;;; with article
10440 ;;;
10441
10442 (defmacro gnus-with-article (article &rest forms)
10443   "Select ARTICLE and perform FORMS in the original article buffer.
10444 Then replace the article with the result."
10445   `(progn
10446      ;; We don't want the article to be marked as read.
10447      (let (gnus-mark-article-hook)
10448        (gnus-summary-select-article t t nil ,article))
10449      (set-buffer gnus-original-article-buffer)
10450      ,@forms
10451      (if (not (gnus-check-backend-function
10452                'request-replace-article (car gnus-article-current)))
10453          (gnus-message 5 "Read-only group; not replacing")
10454        (unless (gnus-request-replace-article
10455                 ,article (car gnus-article-current)
10456                 (current-buffer) t)
10457          (error "Couldn't replace article")))
10458      ;; The cache and backlog have to be flushed somewhat.
10459      (when gnus-keep-backlog
10460        (gnus-backlog-remove-article
10461         (car gnus-article-current) (cdr gnus-article-current)))
10462      (when gnus-use-cache
10463        (gnus-cache-update-article
10464         (car gnus-article-current) (cdr gnus-article-current)))))
10465
10466 (put 'gnus-with-article 'lisp-indent-function 1)
10467 (put 'gnus-with-article 'edebug-form-spec '(form body))
10468
10469 ;; Thread-based commands.
10470
10471 (defun gnus-summary-articles-in-thread (&optional article)
10472   "Return a list of all articles in the current thread.
10473 If ARTICLE is non-nil, return all articles in the thread that starts
10474 with that article."
10475   (let* ((article (or article (gnus-summary-article-number)))
10476          (data (gnus-data-find-list article))
10477          (top-level (gnus-data-level (car data)))
10478          (top-subject
10479           (cond ((null gnus-thread-operation-ignore-subject)
10480                  (gnus-simplify-subject-re
10481                   (mail-header-subject (gnus-data-header (car data)))))
10482                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
10483                  (gnus-simplify-subject-fuzzy
10484                   (mail-header-subject (gnus-data-header (car data)))))
10485                 (t nil)))
10486          (end-point (save-excursion
10487                       (if (gnus-summary-go-to-next-thread)
10488                           (point) (point-max))))
10489          articles)
10490     (while (and data
10491                 (< (gnus-data-pos (car data)) end-point))
10492       (when (or (not top-subject)
10493                 (string= top-subject
10494                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
10495                              (gnus-simplify-subject-fuzzy
10496                               (mail-header-subject
10497                                (gnus-data-header (car data))))
10498                            (gnus-simplify-subject-re
10499                             (mail-header-subject
10500                              (gnus-data-header (car data)))))))
10501         (push (gnus-data-number (car data)) articles))
10502       (unless (and (setq data (cdr data))
10503                    (> (gnus-data-level (car data)) top-level))
10504         (setq data nil)))
10505     ;; Return the list of articles.
10506     (nreverse articles)))
10507
10508 (defun gnus-summary-rethread-current ()
10509   "Rethread the thread the current article is part of."
10510   (interactive)
10511   (let* ((gnus-show-threads t)
10512          (article (gnus-summary-article-number))
10513          (id (mail-header-id (gnus-summary-article-header)))
10514          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
10515     (unless id
10516       (error "No article on the current line"))
10517     (gnus-rebuild-thread id)
10518     (gnus-summary-goto-subject article)))
10519
10520 (defun gnus-summary-reparent-thread ()
10521   "Make the current article child of the marked (or previous) article.
10522
10523 Note that the re-threading will only work if `gnus-thread-ignore-subject'
10524 is non-nil or the Subject: of both articles are the same."
10525   (interactive)
10526   (unless (not (gnus-group-read-only-p))
10527     (error "The current newsgroup does not support article editing"))
10528   (unless (<= (length gnus-newsgroup-processable) 1)
10529     (error "No more than one article may be marked"))
10530   (save-window-excursion
10531     (let ((gnus-article-buffer " *reparent*")
10532           (current-article (gnus-summary-article-number))
10533           ;; First grab the marked article, otherwise one line up.
10534           (parent-article (if (not (null gnus-newsgroup-processable))
10535                               (car gnus-newsgroup-processable)
10536                             (save-excursion
10537                               (if (eq (forward-line -1) 0)
10538                                   (gnus-summary-article-number)
10539                                 (error "Beginning of summary buffer"))))))
10540       (unless (not (eq current-article parent-article))
10541         (error "An article may not be self-referential"))
10542       (let ((message-id (mail-header-id
10543                          (gnus-summary-article-header parent-article))))
10544         (unless (and message-id (not (equal message-id "")))
10545           (error "No message-id in desired parent"))
10546         (gnus-with-article current-article
10547           (save-restriction
10548             (goto-char (point-min))
10549             (message-narrow-to-head)
10550             (if (re-search-forward "^References: " nil t)
10551                 (progn
10552                   (re-search-forward "^[^ \t]" nil t)
10553                   (forward-line -1)
10554                   (end-of-line)
10555                   (insert " " message-id))
10556               (insert "References: " message-id "\n"))))
10557         (set-buffer gnus-summary-buffer)
10558         (gnus-summary-unmark-all-processable)
10559         (gnus-summary-update-article current-article)
10560         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10561             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10562         (gnus-summary-rethread-current)
10563         (gnus-message 3 "Article %d is now the child of article %d"
10564                       current-article parent-article)))))
10565
10566 (defun gnus-summary-toggle-threads (&optional arg)
10567   "Toggle showing conversation threads.
10568 If ARG is positive number, turn showing conversation threads on."
10569   (interactive "P")
10570   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10571     (setq gnus-show-threads
10572           (if (null arg) (not gnus-show-threads)
10573             (> (prefix-numeric-value arg) 0)))
10574     (gnus-summary-prepare)
10575     (gnus-summary-goto-subject current)
10576     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10577     (gnus-summary-position-point)))
10578
10579 (defun gnus-summary-show-all-threads ()
10580   "Show all threads."
10581   (interactive)
10582   (save-excursion
10583     (let ((buffer-read-only nil))
10584       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10585   (gnus-summary-position-point))
10586
10587 (defun gnus-summary-show-thread ()
10588   "Show thread subtrees.
10589 Returns nil if no thread was there to be shown."
10590   (interactive)
10591   (let ((buffer-read-only nil)
10592         (orig (point))
10593         (end (point-at-eol))
10594         ;; Leave point at bol
10595         (beg (progn (beginning-of-line) (point))))
10596     (prog1
10597         ;; Any hidden lines here?
10598         (search-forward "\r" end t)
10599       (subst-char-in-region beg end ?\^M ?\n t)
10600       (goto-char orig)
10601       (gnus-summary-position-point))))
10602
10603 (defun gnus-summary-maybe-hide-threads ()
10604   "If requested, hide the threads that should be hidden."
10605   (when (and gnus-show-threads
10606              gnus-thread-hide-subtree)
10607     (gnus-summary-hide-all-threads
10608      (if (or (consp gnus-thread-hide-subtree)
10609              (functionp gnus-thread-hide-subtree))
10610          (gnus-make-predicate gnus-thread-hide-subtree)
10611        nil))))
10612
10613 ;;; Hiding predicates.
10614
10615 (defun gnus-article-unread-p (header)
10616   (memq (mail-header-number header) gnus-newsgroup-unreads))
10617
10618 (defun gnus-article-unseen-p (header)
10619   (memq (mail-header-number header) gnus-newsgroup-unseen))
10620
10621 (defun gnus-map-articles (predicate articles)
10622   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10623   (apply 'gnus-or (mapcar predicate
10624                           (mapcar 'gnus-summary-article-header articles))))
10625
10626 (defun gnus-summary-hide-all-threads (&optional predicate)
10627   "Hide all thread subtrees.
10628 If PREDICATE is supplied, threads that satisfy this predicate
10629 will not be hidden."
10630   (interactive)
10631   (save-excursion
10632     (goto-char (point-min))
10633     (let ((end nil))
10634       (while (not end)
10635         (when (or (not predicate)
10636                   (gnus-map-articles
10637                    predicate (gnus-summary-article-children)))
10638             (gnus-summary-hide-thread))
10639         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10640   (gnus-summary-position-point))
10641
10642 (defun gnus-summary-hide-thread ()
10643   "Hide thread subtrees.
10644 If PREDICATE is supplied, threads that satisfy this predicate
10645 will not be hidden.
10646 Returns nil if no threads were there to be hidden."
10647   (interactive)
10648   (let ((buffer-read-only nil)
10649         (start (point))
10650         (article (gnus-summary-article-number)))
10651     (goto-char start)
10652     ;; Go forward until either the buffer ends or the subthread
10653     ;; ends.
10654     (when (and (not (eobp))
10655                (or (zerop (gnus-summary-next-thread 1 t))
10656                    (goto-char (point-max))))
10657       (prog1
10658           (if (and (> (point) start)
10659                    (search-backward "\n" start t))
10660               (progn
10661                 (subst-char-in-region start (point) ?\n ?\^M)
10662                 (gnus-summary-goto-subject article))
10663             (goto-char start)
10664             nil)))))
10665
10666 (defun gnus-summary-go-to-next-thread (&optional previous)
10667   "Go to the same level (or less) next thread.
10668 If PREVIOUS is non-nil, go to previous thread instead.
10669 Return the article number moved to, or nil if moving was impossible."
10670   (let ((level (gnus-summary-thread-level))
10671         (way (if previous -1 1))
10672         (beg (point)))
10673     (forward-line way)
10674     (while (and (not (eobp))
10675                 (< level (gnus-summary-thread-level)))
10676       (forward-line way))
10677     (if (eobp)
10678         (progn
10679           (goto-char beg)
10680           nil)
10681       (setq beg (point))
10682       (prog1
10683           (gnus-summary-article-number)
10684         (goto-char beg)))))
10685
10686 (defun gnus-summary-next-thread (n &optional silent)
10687   "Go to the same level next N'th thread.
10688 If N is negative, search backward instead.
10689 Returns the difference between N and the number of skips actually
10690 done.
10691
10692 If SILENT, don't output messages."
10693   (interactive "p")
10694   (let ((backward (< n 0))
10695         (n (abs n)))
10696     (while (and (> n 0)
10697                 (gnus-summary-go-to-next-thread backward))
10698       (decf n))
10699     (unless silent
10700       (gnus-summary-position-point))
10701     (when (and (not silent) (/= 0 n))
10702       (gnus-message 7 "No more threads"))
10703     n))
10704
10705 (defun gnus-summary-prev-thread (n)
10706   "Go to the same level previous N'th thread.
10707 Returns the difference between N and the number of skips actually
10708 done."
10709   (interactive "p")
10710   (gnus-summary-next-thread (- n)))
10711
10712 (defun gnus-summary-go-down-thread ()
10713   "Go down one level in the current thread."
10714   (let ((children (gnus-summary-article-children)))
10715     (when children
10716       (gnus-summary-goto-subject (car children)))))
10717
10718 (defun gnus-summary-go-up-thread ()
10719   "Go up one level in the current thread."
10720   (let ((parent (gnus-summary-article-parent)))
10721     (when parent
10722       (gnus-summary-goto-subject parent))))
10723
10724 (defun gnus-summary-down-thread (n)
10725   "Go down thread N steps.
10726 If N is negative, go up instead.
10727 Returns the difference between N and how many steps down that were
10728 taken."
10729   (interactive "p")
10730   (let ((up (< n 0))
10731         (n (abs n)))
10732     (while (and (> n 0)
10733                 (if up (gnus-summary-go-up-thread)
10734                   (gnus-summary-go-down-thread)))
10735       (setq n (1- n)))
10736     (gnus-summary-position-point)
10737     (when (/= 0 n)
10738       (gnus-message 7 "Can't go further"))
10739     n))
10740
10741 (defun gnus-summary-up-thread (n)
10742   "Go up thread N steps.
10743 If N is negative, go down instead.
10744 Returns the difference between N and how many steps down that were
10745 taken."
10746   (interactive "p")
10747   (gnus-summary-down-thread (- n)))
10748
10749 (defun gnus-summary-top-thread ()
10750   "Go to the top of the thread."
10751   (interactive)
10752   (while (gnus-summary-go-up-thread))
10753   (gnus-summary-article-number))
10754
10755 (defun gnus-summary-kill-thread (&optional unmark)
10756   "Mark articles under current thread as read.
10757 If the prefix argument is positive, remove any kinds of marks.
10758 If the prefix argument is negative, tick articles instead."
10759   (interactive "P")
10760   (when unmark
10761     (setq unmark (prefix-numeric-value unmark)))
10762   (let ((articles (gnus-summary-articles-in-thread)))
10763     (save-excursion
10764       ;; Expand the thread.
10765       (gnus-summary-show-thread)
10766       ;; Mark all the articles.
10767       (while articles
10768         (gnus-summary-goto-subject (car articles))
10769         (cond ((null unmark)
10770                (gnus-summary-mark-article-as-read gnus-killed-mark))
10771               ((> unmark 0)
10772                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10773               (t
10774                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10775         (setq articles (cdr articles))))
10776     ;; Hide killed subtrees.
10777     (and (null unmark)
10778          gnus-thread-hide-killed
10779          (gnus-summary-hide-thread))
10780     ;; If marked as read, go to next unread subject.
10781     (when (null unmark)
10782       ;; Go to next unread subject.
10783       (gnus-summary-next-subject 1 t)))
10784   (gnus-set-mode-line 'summary))
10785
10786 ;; Summary sorting commands
10787
10788 (defun gnus-summary-sort-by-number (&optional reverse)
10789   "Sort the summary buffer by article number.
10790 Argument REVERSE means reverse order."
10791   (interactive "P")
10792   (gnus-summary-sort 'number reverse))
10793
10794 (defun gnus-summary-sort-by-random (&optional reverse)
10795   "Randomize the order in the summary buffer.
10796 Argument REVERSE means to randomize in reverse order."
10797   (interactive "P")
10798   (gnus-summary-sort 'random reverse))
10799
10800 (defun gnus-summary-sort-by-author (&optional reverse)
10801   "Sort the summary buffer by author name alphabetically.
10802 If `case-fold-search' is non-nil, case of letters is ignored.
10803 Argument REVERSE means reverse order."
10804   (interactive "P")
10805   (gnus-summary-sort 'author reverse))
10806
10807 (defun gnus-summary-sort-by-subject (&optional reverse)
10808   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10809 If `case-fold-search' is non-nil, case of letters is ignored.
10810 Argument REVERSE means reverse order."
10811   (interactive "P")
10812   (gnus-summary-sort 'subject reverse))
10813
10814 (defun gnus-summary-sort-by-date (&optional reverse)
10815   "Sort the summary buffer by date.
10816 Argument REVERSE means reverse order."
10817   (interactive "P")
10818   (gnus-summary-sort 'date reverse))
10819
10820 (defun gnus-summary-sort-by-score (&optional reverse)
10821   "Sort the summary buffer by score.
10822 Argument REVERSE means reverse order."
10823   (interactive "P")
10824   (gnus-summary-sort 'score reverse))
10825
10826 (defun gnus-summary-sort-by-lines (&optional reverse)
10827   "Sort the summary buffer by the number of lines.
10828 Argument REVERSE means reverse order."
10829   (interactive "P")
10830   (gnus-summary-sort 'lines reverse))
10831
10832 (defun gnus-summary-sort-by-chars (&optional reverse)
10833   "Sort the summary buffer by article length.
10834 Argument REVERSE means reverse order."
10835   (interactive "P")
10836   (gnus-summary-sort 'chars reverse))
10837
10838 (defun gnus-summary-sort-by-original (&optional reverse)
10839   "Sort the summary buffer using the default sorting method.
10840 Argument REVERSE means reverse order."
10841   (interactive "P")
10842   (let* ((buffer-read-only)
10843          (gnus-summary-prepare-hook nil))
10844     ;; We do the sorting by regenerating the threads.
10845     (gnus-summary-prepare)
10846     ;; Hide subthreads if needed.
10847     (gnus-summary-maybe-hide-threads)))
10848
10849 (defun gnus-summary-sort (predicate reverse)
10850   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10851   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10852          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10853          (gnus-thread-sort-functions
10854           (if (not reverse)
10855               thread
10856             `(lambda (t1 t2)
10857                (,thread t2 t1))))
10858          (gnus-sort-gathered-threads-function
10859           gnus-thread-sort-functions)
10860          (gnus-article-sort-functions
10861           (if (not reverse)
10862               article
10863             `(lambda (t1 t2)
10864                (,article t2 t1))))
10865          (buffer-read-only)
10866          (gnus-summary-prepare-hook nil))
10867     ;; We do the sorting by regenerating the threads.
10868     (gnus-summary-prepare)
10869     ;; Hide subthreads if needed.
10870     (gnus-summary-maybe-hide-threads)))
10871
10872 ;; Summary saving commands.
10873
10874 (defun gnus-summary-save-article (&optional n not-saved)
10875   "Save the current article using the default saver function.
10876 If N is a positive number, save the N next articles.
10877 If N is a negative number, save the N previous articles.
10878 If N is nil and any articles have been marked with the process mark,
10879 save those articles instead.
10880 The variable `gnus-default-article-saver' specifies the saver function."
10881   (interactive "P")
10882   (let* ((articles (gnus-summary-work-articles n))
10883          (save-buffer (save-excursion
10884                         (nnheader-set-temp-buffer " *Gnus Save*")))
10885          (num (length articles))
10886          header file)
10887     (dolist (article articles)
10888       (setq header (gnus-summary-article-header article))
10889       (if (not (vectorp header))
10890           ;; This is a pseudo-article.
10891           (if (assq 'name header)
10892               (gnus-copy-file (cdr (assq 'name header)))
10893             (gnus-message 1 "Article %d is unsaveable" article))
10894         ;; This is a real article.
10895         (save-window-excursion
10896           (let ((gnus-display-mime-function nil)
10897                 (gnus-article-prepare-hook nil))
10898             (gnus-summary-select-article t nil nil article)))
10899         (save-excursion
10900           (set-buffer save-buffer)
10901           (erase-buffer)
10902           (insert-buffer-substring gnus-original-article-buffer))
10903         (setq file (gnus-article-save save-buffer file num))
10904         (gnus-summary-remove-process-mark article)
10905         (unless not-saved
10906           (gnus-summary-set-saved-mark article))))
10907     (gnus-kill-buffer save-buffer)
10908     (gnus-summary-position-point)
10909     (gnus-set-mode-line 'summary)
10910     n))
10911
10912 (defun gnus-summary-pipe-output (&optional arg headers)
10913   "Pipe the current article to a subprocess.
10914 If N is a positive number, pipe the N next articles.
10915 If N is a negative number, pipe the N previous articles.
10916 If N is nil and any articles have been marked with the process mark,
10917 pipe those articles instead.
10918 If HEADERS (the symbolic prefix), include the headers, too."
10919   (interactive (gnus-interactive "P\ny"))
10920   (require 'gnus-art)
10921   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe)
10922         (gnus-save-all-headers (or headers gnus-save-all-headers)))
10923     (gnus-summary-save-article arg t))
10924   (let ((buffer (get-buffer "*Shell Command Output*")))
10925     (when (and buffer
10926                (not (zerop (buffer-size buffer))))
10927       (gnus-configure-windows 'pipe))))
10928
10929 (defun gnus-summary-save-article-mail (&optional arg)
10930   "Append the current article to an mail file.
10931 If N is a positive number, save the N next articles.
10932 If N is a negative number, save the N previous articles.
10933 If N is nil and any articles have been marked with the process mark,
10934 save those articles instead."
10935   (interactive "P")
10936   (require 'gnus-art)
10937   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10938     (gnus-summary-save-article arg)))
10939
10940 (defun gnus-summary-save-article-rmail (&optional arg)
10941   "Append the current article to an rmail file.
10942 If N is a positive number, save the N next articles.
10943 If N is a negative number, save the N previous articles.
10944 If N is nil and any articles have been marked with the process mark,
10945 save those articles instead."
10946   (interactive "P")
10947   (require 'gnus-art)
10948   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10949     (gnus-summary-save-article arg)))
10950
10951 (defun gnus-summary-save-article-file (&optional arg)
10952   "Append the current article to a file.
10953 If N is a positive number, save the N next articles.
10954 If N is a negative number, save the N previous articles.
10955 If N is nil and any articles have been marked with the process mark,
10956 save those articles instead."
10957   (interactive "P")
10958   (require 'gnus-art)
10959   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10960     (gnus-summary-save-article arg)))
10961
10962 (defun gnus-summary-write-article-file (&optional arg)
10963   "Write the current article to a file, deleting the previous file.
10964 If N is a positive number, save the N next articles.
10965 If N is a negative number, save the N previous articles.
10966 If N is nil and any articles have been marked with the process mark,
10967 save those articles instead."
10968   (interactive "P")
10969   (require 'gnus-art)
10970   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10971     (gnus-summary-save-article arg)))
10972
10973 (defun gnus-summary-save-article-body-file (&optional arg)
10974   "Append the current article body to a file.
10975 If N is a positive number, save the N next articles.
10976 If N is a negative number, save the N previous articles.
10977 If N is nil and any articles have been marked with the process mark,
10978 save those articles instead."
10979   (interactive "P")
10980   (require 'gnus-art)
10981   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10982     (gnus-summary-save-article arg)))
10983
10984 (defun gnus-summary-muttprint (&optional arg)
10985   "Print the current article using Muttprint.
10986 If N is a positive number, save the N next articles.
10987 If N is a negative number, save the N previous articles.
10988 If N is nil and any articles have been marked with the process mark,
10989 save those articles instead."
10990   (interactive "P")
10991   (require 'gnus-art)
10992   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10993     (gnus-summary-save-article arg t)))
10994
10995 (defun gnus-summary-pipe-message (program)
10996   "Pipe the current article through PROGRAM."
10997   (interactive "sProgram: ")
10998   (gnus-summary-select-article)
10999   (let ((mail-header-separator ""))
11000     (gnus-eval-in-buffer-window gnus-article-buffer
11001       (save-restriction
11002         (widen)
11003         (let ((start (window-start))
11004               buffer-read-only)
11005           (message-pipe-buffer-body program)
11006           (set-window-start (get-buffer-window (current-buffer)) start))))))
11007
11008 (defun gnus-get-split-value (methods)
11009   "Return a value based on the split METHODS."
11010   (let (split-name method result match)
11011     (when methods
11012       (save-excursion
11013         (set-buffer gnus-original-article-buffer)
11014         (save-restriction
11015           (nnheader-narrow-to-headers)
11016           (while (and methods (not split-name))
11017             (goto-char (point-min))
11018             (setq method (pop methods))
11019             (setq match (car method))
11020             (when (cond
11021                    ((stringp match)
11022                     ;; Regular expression.
11023                     (ignore-errors
11024                       (re-search-forward match nil t)))
11025                    ((functionp match)
11026                     ;; Function.
11027                     (save-restriction
11028                       (widen)
11029                       (setq result (funcall match gnus-newsgroup-name))))
11030                    ((consp match)
11031                     ;; Form.
11032                     (save-restriction
11033                       (widen)
11034                       (setq result (eval match)))))
11035               (setq split-name (cdr method))
11036               (cond ((stringp result)
11037                      (push (expand-file-name
11038                             result gnus-article-save-directory)
11039                            split-name))
11040                     ((consp result)
11041                      (setq split-name (append result split-name)))))))))
11042     (nreverse split-name)))
11043
11044 (defun gnus-valid-move-group-p (group)
11045   (and (boundp group)
11046        (symbol-name group)
11047        (symbol-value group)
11048        (gnus-get-function (gnus-find-method-for-group
11049                            (symbol-name group)) 'request-accept-article t)))
11050
11051 (defun gnus-read-move-group-name (prompt default articles prefix)
11052   "Read a group name."
11053   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
11054          (minibuffer-confirm-incomplete nil) ; XEmacs
11055          (prom
11056           (format "%s %s to:"
11057                   prompt
11058                   (if (> (length articles) 1)
11059                       (format "these %d articles" (length articles))
11060                     "this article")))
11061          (to-newsgroup
11062           (cond
11063            ((null split-name)
11064             (gnus-completing-read-with-default
11065              default prom
11066              gnus-active-hashtb
11067              'gnus-valid-move-group-p
11068              nil prefix
11069              'gnus-group-history))
11070            ((= 1 (length split-name))
11071             (gnus-completing-read-with-default
11072              (car split-name) prom
11073              gnus-active-hashtb
11074              'gnus-valid-move-group-p
11075              nil nil
11076              'gnus-group-history))
11077            (t
11078             (gnus-completing-read-with-default
11079              nil prom
11080              (mapcar (lambda (el) (list el))
11081                      (nreverse split-name))
11082              nil nil nil
11083              'gnus-group-history))))
11084          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
11085     (when to-newsgroup
11086       (if (or (string= to-newsgroup "")
11087               (string= to-newsgroup prefix))
11088           (setq to-newsgroup default))
11089       (unless to-newsgroup
11090         (error "No group name entered"))
11091       (or (gnus-active to-newsgroup)
11092           (gnus-activate-group to-newsgroup nil nil to-method)
11093           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
11094                                      to-newsgroup))
11095               (or (and (gnus-request-create-group to-newsgroup to-method)
11096                        (gnus-activate-group
11097                         to-newsgroup nil nil to-method)
11098                        (gnus-subscribe-group to-newsgroup))
11099                   (error "Couldn't create group %s" to-newsgroup)))
11100           (error "No such group: %s" to-newsgroup)))
11101     to-newsgroup))
11102
11103 (defun gnus-summary-save-parts (type dir n &optional reverse)
11104   "Save parts matching TYPE to DIR.
11105 If REVERSE, save parts that do not match TYPE."
11106   (interactive
11107    (list (read-string "Save parts of type: "
11108                       (or (car gnus-summary-save-parts-type-history)
11109                           gnus-summary-save-parts-default-mime)
11110                       'gnus-summary-save-parts-type-history)
11111          (setq gnus-summary-save-parts-last-directory
11112                (read-file-name "Save to directory: "
11113                                gnus-summary-save-parts-last-directory
11114                                nil t))
11115          current-prefix-arg))
11116   (gnus-summary-iterate n
11117     (let ((gnus-display-mime-function nil)
11118           (gnus-inhibit-treatment t))
11119       (gnus-summary-select-article))
11120     (save-excursion
11121       (set-buffer gnus-article-buffer)
11122       (let ((handles (or gnus-article-mime-handles
11123                          (mm-dissect-buffer nil gnus-article-loose-mime)
11124                          (and gnus-article-emulate-mime
11125                               (mm-uu-dissect)))))
11126         (when handles
11127           (gnus-summary-save-parts-1 type dir handles reverse)
11128           (unless gnus-article-mime-handles ;; Don't destroy this case.
11129             (mm-destroy-parts handles)))))))
11130
11131 (defun gnus-summary-save-parts-1 (type dir handle reverse)
11132   (if (stringp (car handle))
11133       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
11134               (cdr handle))
11135     (when (if reverse
11136               (not (string-match type (mm-handle-media-type handle)))
11137             (string-match type (mm-handle-media-type handle)))
11138       (let ((file (expand-file-name
11139                    (gnus-map-function
11140                     mm-file-name-rewrite-functions
11141                     (file-name-nondirectory
11142                      (or
11143                       (mail-content-type-get
11144                        (mm-handle-disposition handle) 'filename)
11145                       (mail-content-type-get
11146                        (mm-handle-type handle) 'name)
11147                       (concat gnus-newsgroup-name
11148                               "." (number-to-string
11149                                    (cdr gnus-article-current))))))
11150                    dir)))
11151         (unless (file-exists-p file)
11152           (mm-save-part-to-file handle file))))))
11153
11154 ;; Summary extract commands
11155
11156 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
11157   (let ((buffer-read-only nil)
11158         (article (gnus-summary-article-number))
11159         after-article b e)
11160     (unless (gnus-summary-goto-subject article)
11161       (error "No such article: %d" article))
11162     (gnus-summary-position-point)
11163     ;; If all commands are to be bunched up on one line, we collect
11164     ;; them here.
11165     (unless gnus-view-pseudos-separately
11166       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
11167             files action)
11168         (while ps
11169           (setq action (cdr (assq 'action (car ps))))
11170           (setq files (list (cdr (assq 'name (car ps)))))
11171           (while (and ps (cdr ps)
11172                       (string= (or action "1")
11173                                (or (cdr (assq 'action (cadr ps))) "2")))
11174             (push (cdr (assq 'name (cadr ps))) files)
11175             (setcdr ps (cddr ps)))
11176           (when files
11177             (when (not (string-match "%s" action))
11178               (push " " files))
11179             (push " " files)
11180             (when (assq 'execute (car ps))
11181               (setcdr (assq 'execute (car ps))
11182                       (funcall (if (string-match "%s" action)
11183                                    'format 'concat)
11184                                action
11185                                (mapconcat
11186                                 (lambda (f)
11187                                   (if (equal f " ")
11188                                       f
11189                                     (shell-quote-argument f)))
11190                                 files " ")))))
11191           (setq ps (cdr ps)))))
11192     (if (and gnus-view-pseudos (not not-view))
11193         (while pslist
11194           (when (assq 'execute (car pslist))
11195             (gnus-execute-command (cdr (assq 'execute (car pslist)))
11196                                   (eq gnus-view-pseudos 'not-confirm)))
11197           (setq pslist (cdr pslist)))
11198       (save-excursion
11199         (while pslist
11200           (setq after-article (or (cdr (assq 'article (car pslist)))
11201                                   (gnus-summary-article-number)))
11202           (gnus-summary-goto-subject after-article)
11203           (forward-line 1)
11204           (setq b (point))
11205           (insert "    " (file-name-nondirectory
11206                           (cdr (assq 'name (car pslist))))
11207                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
11208           (setq e (point))
11209           (forward-line -1)             ; back to `b'
11210           (gnus-add-text-properties
11211            b (1- e) (list 'gnus-number gnus-reffed-article-number
11212                           gnus-mouse-face-prop gnus-mouse-face))
11213           (gnus-data-enter
11214            after-article gnus-reffed-article-number
11215            gnus-unread-mark b (car pslist) 0 (- e b))
11216           (setq gnus-newsgroup-unreads
11217                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
11218                                          gnus-reffed-article-number))
11219           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
11220           (setq pslist (cdr pslist)))))))
11221
11222 (defun gnus-pseudos< (p1 p2)
11223   (let ((c1 (cdr (assq 'action p1)))
11224         (c2 (cdr (assq 'action p2))))
11225     (and c1 c2 (string< c1 c2))))
11226
11227 (defun gnus-request-pseudo-article (props)
11228   (cond ((assq 'execute props)
11229          (gnus-execute-command (cdr (assq 'execute props)))))
11230   (let ((gnus-current-article (gnus-summary-article-number)))
11231     (gnus-run-hooks 'gnus-mark-article-hook)))
11232
11233 (defun gnus-execute-command (command &optional automatic)
11234   (save-excursion
11235     (gnus-article-setup-buffer)
11236     (set-buffer gnus-article-buffer)
11237     (setq buffer-read-only nil)
11238     (let ((command (if automatic command
11239                      (read-string "Command: " (cons command 0)))))
11240       (erase-buffer)
11241       (insert "$ " command "\n\n")
11242       (if gnus-view-pseudo-asynchronously
11243           (start-process "gnus-execute" (current-buffer) shell-file-name
11244                          shell-command-switch command)
11245         (call-process shell-file-name nil t nil
11246                       shell-command-switch command)))))
11247
11248 ;; Summary kill commands.
11249
11250 (defun gnus-summary-edit-global-kill (article)
11251   "Edit the \"global\" kill file."
11252   (interactive (list (gnus-summary-article-number)))
11253   (gnus-group-edit-global-kill article))
11254
11255 (defun gnus-summary-edit-local-kill ()
11256   "Edit a local kill file applied to the current newsgroup."
11257   (interactive)
11258   (setq gnus-current-headers (gnus-summary-article-header))
11259   (gnus-group-edit-local-kill
11260    (gnus-summary-article-number) gnus-newsgroup-name))
11261
11262 ;;; Header reading.
11263
11264 (defun gnus-read-header (id &optional header)
11265   "Read the headers of article ID and enter them into the Gnus system."
11266   (let ((group gnus-newsgroup-name)
11267         (gnus-override-method
11268          (or
11269           gnus-override-method
11270           (and (gnus-news-group-p gnus-newsgroup-name)
11271                (car (gnus-refer-article-methods)))))
11272         where)
11273     ;; First we check to see whether the header in question is already
11274     ;; fetched.
11275     (if (stringp id)
11276         ;; This is a Message-ID.
11277         (setq header (or header (gnus-id-to-header id)))
11278       ;; This is an article number.
11279       (setq header (or header (gnus-summary-article-header id))))
11280     (if (and header
11281              (not (gnus-summary-article-sparse-p (mail-header-number header))))
11282         ;; We have found the header.
11283         header
11284       ;; If this is a sparse article, we have to nix out its
11285       ;; previous entry in the thread hashtb.
11286       (when (and header
11287                  (gnus-summary-article-sparse-p (mail-header-number header)))
11288         (let* ((parent (gnus-parent-id (mail-header-references header)))
11289                (thread (and parent (gnus-id-to-thread parent))))
11290           (when thread
11291             (delq (assq header thread) thread))))
11292       ;; We have to really fetch the header to this article.
11293       (save-excursion
11294         (set-buffer nntp-server-buffer)
11295         (when (setq where (gnus-request-head id group))
11296           (nnheader-fold-continuation-lines)
11297           (goto-char (point-max))
11298           (insert ".\n")
11299           (goto-char (point-min))
11300           (insert "211 ")
11301           (princ (cond
11302                   ((numberp id) id)
11303                   ((cdr where) (cdr where))
11304                   (header (mail-header-number header))
11305                   (t gnus-reffed-article-number))
11306                  (current-buffer))
11307           (insert " Article retrieved.\n"))
11308         (if (or (not where)
11309                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
11310             ()                          ; Malformed head.
11311           (unless (gnus-summary-article-sparse-p (mail-header-number header))
11312             (when (and (stringp id)
11313                        (not (string= (gnus-group-real-name group)
11314                                      (car where))))
11315               ;; If we fetched by Message-ID and the article came
11316               ;; from a different group, we fudge some bogus article
11317               ;; numbers for this article.
11318               (mail-header-set-number header gnus-reffed-article-number))
11319             (save-excursion
11320               (set-buffer gnus-summary-buffer)
11321               (decf gnus-reffed-article-number)
11322               (gnus-remove-header (mail-header-number header))
11323               (push header gnus-newsgroup-headers)
11324               (setq gnus-current-headers header)
11325               (push (mail-header-number header) gnus-newsgroup-limit)))
11326           header)))))
11327
11328 (defun gnus-remove-header (number)
11329   "Remove header NUMBER from `gnus-newsgroup-headers'."
11330   (if (and gnus-newsgroup-headers
11331            (= number (mail-header-number (car gnus-newsgroup-headers))))
11332       (pop gnus-newsgroup-headers)
11333     (let ((headers gnus-newsgroup-headers))
11334       (while (and (cdr headers)
11335                   (not (= number (mail-header-number (cadr headers)))))
11336         (pop headers))
11337       (when (cdr headers)
11338         (setcdr headers (cddr headers))))))
11339
11340 ;;;
11341 ;;; summary highlights
11342 ;;;
11343
11344 (defun gnus-highlight-selected-summary ()
11345   "Highlight selected article in summary buffer."
11346   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
11347   (when gnus-summary-selected-face
11348     (save-excursion
11349       (let* ((beg (point-at-bol))
11350              (end (point-at-eol))
11351              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
11352              (from (if (get-text-property beg gnus-mouse-face-prop)
11353                        beg
11354                      (or (next-single-property-change
11355                           beg gnus-mouse-face-prop nil end)
11356                          beg)))
11357              (to
11358               (if (= from end)
11359                   (- from 2)
11360                 (or (next-single-property-change
11361                      from gnus-mouse-face-prop nil end)
11362                     end))))
11363         ;; If no mouse-face prop on line we will have to = from = end,
11364         ;; so we highlight the entire line instead.
11365         (when (= (+ to 2) from)
11366           (setq from beg)
11367           (setq to end))
11368         (if gnus-newsgroup-selected-overlay
11369             ;; Move old overlay.
11370             (gnus-move-overlay
11371              gnus-newsgroup-selected-overlay from to (current-buffer))
11372           ;; Create new overlay.
11373           (gnus-overlay-put
11374            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
11375            'face gnus-summary-selected-face))))))
11376
11377 (defvar gnus-summary-highlight-line-cached nil)
11378 (defvar gnus-summary-highlight-line-trigger nil)
11379
11380 (defun gnus-summary-highlight-line-0 ()
11381   (if (and (eq gnus-summary-highlight-line-trigger
11382                gnus-summary-highlight)
11383            gnus-summary-highlight-line-cached)
11384       gnus-summary-highlight-line-cached
11385     (setq gnus-summary-highlight-line-trigger gnus-summary-highlight
11386           gnus-summary-highlight-line-cached
11387           (let* ((cond (list 'cond))
11388                  (c cond)
11389                  (list gnus-summary-highlight))
11390             (while list
11391               (setcdr c (cons (list (caar list) (list 'quote (cdar list)))
11392                               nil))
11393               (setq c (cdr c)
11394                     list (cdr list)))
11395             (gnus-byte-compile (list 'lambda nil cond))))))
11396
11397 (defun gnus-summary-highlight-line ()
11398   "Highlight current line according to `gnus-summary-highlight'."
11399   (let* ((beg (point-at-bol))
11400          (article (or (gnus-summary-article-number) gnus-current-article))
11401          (score (or (cdr (assq article
11402                                gnus-newsgroup-scored))
11403                     gnus-summary-default-score 0))
11404          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
11405          (inhibit-read-only t)
11406          (default gnus-summary-default-score)
11407          (default-high gnus-summary-default-high-score)
11408          (default-low gnus-summary-default-low-score)
11409          (uncached (and gnus-summary-use-undownloaded-faces
11410                         (memq article gnus-newsgroup-undownloaded))))
11411     (let ((face (funcall (gnus-summary-highlight-line-0))))
11412       (unless (eq face (get-text-property beg 'face))
11413         (gnus-put-text-property-excluding-characters-with-faces
11414          beg (point-at-eol) 'face
11415          (setq face (if (boundp face) (symbol-value face) face)))
11416         (when gnus-summary-highlight-line-function
11417           (funcall gnus-summary-highlight-line-function article face))))))
11418
11419 (defun gnus-update-read-articles (group unread &optional compute)
11420   "Update the list of read articles in GROUP.
11421 UNREAD is a sorted list."
11422   (let ((active (or gnus-newsgroup-active (gnus-active group)))
11423         (info (gnus-get-info group))
11424         (prev 1)
11425         read)
11426     (if (or (not info) (not active))
11427         ;; There is no info on this group if it was, in fact,
11428         ;; killed.  Gnus stores no information on killed groups, so
11429         ;; there's nothing to be done.
11430         ;; One could store the information somewhere temporarily,
11431         ;; perhaps...  Hmmm...
11432         ()
11433       ;; Remove any negative articles numbers.
11434       (while (and unread (< (car unread) 0))
11435         (setq unread (cdr unread)))
11436       ;; Remove any expired article numbers
11437       (while (and unread (< (car unread) (car active)))
11438         (setq unread (cdr unread)))
11439       ;; Compute the ranges of read articles by looking at the list of
11440       ;; unread articles.
11441       (while unread
11442         (when (/= (car unread) prev)
11443           (push (if (= prev (1- (car unread))) prev
11444                   (cons prev (1- (car unread))))
11445                 read))
11446         (setq prev (1+ (car unread)))
11447         (setq unread (cdr unread)))
11448       (when (<= prev (cdr active))
11449         (push (cons prev (cdr active)) read))
11450       (setq read (if (> (length read) 1) (nreverse read) read))
11451       (if compute
11452           read
11453         (save-excursion
11454           (let (setmarkundo)
11455             ;; Propagate the read marks to the backend.
11456             (when (gnus-check-backend-function 'request-set-mark group)
11457               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
11458                     (add (gnus-remove-from-range read (gnus-info-read info))))
11459                 (when (or add del)
11460                   (unless (gnus-check-group group)
11461                     (error "Can't open server for %s" group))
11462                   (gnus-request-set-mark
11463                    group (delq nil (list (if add (list add 'add '(read)))
11464                                          (if del (list del 'del '(read))))))
11465                   (setq setmarkundo
11466                         `(gnus-request-set-mark
11467                           ,group
11468                           ',(delq nil (list
11469                                        (if del (list del 'add '(read)))
11470                                        (if add (list add 'del '(read))))))))))
11471             (set-buffer gnus-group-buffer)
11472             (gnus-undo-register
11473               `(progn
11474                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
11475                  (gnus-info-set-read ',info ',(gnus-info-read info))
11476                  (gnus-get-unread-articles-in-group ',info
11477                                                     (gnus-active ,group))
11478                  (gnus-group-update-group ,group t)
11479                  ,setmarkundo))))
11480         ;; Enter this list into the group info.
11481         (gnus-info-set-read info read)
11482         ;; Set the number of unread articles in gnus-newsrc-hashtb.
11483         (gnus-get-unread-articles-in-group info (gnus-active group))
11484         t))))
11485
11486 (defun gnus-offer-save-summaries ()
11487   "Offer to save all active summary buffers."
11488   (let (buffers)
11489     ;; Go through all buffers and find all summaries.
11490     (dolist (buffer (buffer-list))
11491       (when (and (setq buffer (buffer-name buffer))
11492                  (string-match "Summary" buffer)
11493                  (save-excursion
11494                    (set-buffer buffer)
11495                    ;; We check that this is, indeed, a summary buffer.
11496                    (and (eq major-mode 'gnus-summary-mode)
11497                         ;; Also make sure this isn't bogus.
11498                         gnus-newsgroup-prepared
11499                         ;; Also make sure that this isn't a
11500                         ;; dead summary buffer.
11501                         (not gnus-dead-summary-mode))))
11502         (push buffer buffers)))
11503     ;; Go through all these summary buffers and offer to save them.
11504     (when buffers
11505       (save-excursion
11506         (map-y-or-n-p
11507          "Update summary buffer %s? "
11508          (lambda (buf)
11509            (switch-to-buffer buf)
11510            (gnus-summary-exit))
11511          buffers)))))
11512
11513
11514 ;;; @ for mime-partial
11515 ;;;
11516
11517 (defun gnus-request-partial-message ()
11518   (save-excursion
11519     (let ((number (gnus-summary-article-number))
11520           (group gnus-newsgroup-name)
11521           (mother gnus-article-buffer))
11522       (set-buffer (get-buffer-create " *Partial Article*"))
11523       (erase-buffer)
11524       (setq mime-preview-buffer mother)
11525       (gnus-request-article-this-buffer number group)
11526       (mime-parse-buffer)
11527       )))
11528
11529 (autoload 'mime-combine-message/partial-pieces-automatically
11530   "mime-partial"
11531   "Internal method to combine message/partial messages automatically.")
11532
11533 (mime-add-condition
11534  'action '((type . message)(subtype . partial)
11535            (major-mode . gnus-original-article-mode)
11536            (method . mime-combine-message/partial-pieces-automatically)
11537            (summary-buffer-exp . gnus-summary-buffer)
11538            (request-partial-message-method . gnus-request-partial-message)
11539            ))
11540
11541
11542 ;;; @ for message/rfc822
11543 ;;;
11544
11545 (defun gnus-mime-extract-message/rfc822 (entity situation)
11546   "Burst a forwarded article."
11547   (save-excursion
11548     (set-buffer gnus-summary-buffer)
11549     (let* ((group (completing-read "Group: " gnus-active-hashtb nil t
11550                                    gnus-newsgroup-name 'gnus-group-history))
11551            (gnus-group-marked (list group))
11552            article info)
11553       (with-temp-buffer
11554         (mime-insert-entity-content entity)
11555         (setq article (gnus-request-accept-article group)))
11556       (when (and (consp article)
11557                  (numberp (setq article (cdr article))))
11558         (setq info (gnus-get-info group))
11559         (gnus-info-set-read info
11560                             (gnus-remove-from-range (gnus-info-read info)
11561                                                     (list article)))
11562         (when (string-equal group gnus-newsgroup-name)
11563           (forward-line 1)
11564           (let (gnus-show-threads)
11565             (gnus-summary-goto-subject article t))
11566           (gnus-summary-clear-mark-forward 1))
11567         (set-buffer gnus-group-buffer)
11568         (gnus-group-get-new-news-this-group nil t)))))
11569
11570 (mime-add-condition
11571  'action '((type . message)(subtype . rfc822)
11572            (major-mode . gnus-original-article-mode)
11573            (method . gnus-mime-extract-message/rfc822)
11574            (mode . "extract")
11575            ))
11576
11577 (mime-add-condition
11578  'action '((type . message)(subtype . news)
11579            (major-mode . gnus-original-article-mode)
11580            (method . gnus-mime-extract-message/rfc822)
11581            (mode . "extract")
11582            ))
11583
11584 (defun gnus-mime-extract-multipart (entity situation)
11585   (let ((children (mime-entity-children entity))
11586         mime-acting-situation-to-override
11587         f)
11588     (while children
11589       (mime-play-entity (car children)
11590                         (cons (assq 'mode situation)
11591                               mime-acting-situation-to-override))
11592       (setq children (cdr children)))
11593     (if (setq f (cdr (assq 'after-method
11594                            mime-acting-situation-to-override)))
11595         (eval f)
11596       )))
11597
11598 (mime-add-condition
11599  'action '((type . multipart)
11600            (method . gnus-mime-extract-multipart)
11601            (mode . "extract")
11602            )
11603  'with-default)
11604
11605
11606 ;;; @ end
11607 ;;;
11608
11609 (defun gnus-summary-inherit-default-charset ()
11610   "Import `default-mime-charset' from summary buffer.
11611 Also take care of `default-mime-charset-unlimited' if the LIMIT version
11612 of FLIM is used."
11613   (if (buffer-live-p gnus-summary-buffer)
11614       (let (d-m-c d-m-c-u)
11615         (with-current-buffer gnus-summary-buffer
11616           (setq d-m-c (if (local-variable-p 'default-mime-charset
11617                                             gnus-summary-buffer)
11618                           default-mime-charset
11619                         t)
11620                 ;; LIMIT
11621                 d-m-c-u (if (local-variable-p 'default-mime-charset-unlimited
11622                                               gnus-summary-buffer)
11623                             (symbol-value 'default-mime-charset-unlimited)
11624                           t)))
11625         (if (eq t d-m-c)
11626             (kill-local-variable 'default-mime-charset)
11627           (set (make-local-variable 'default-mime-charset) d-m-c))
11628         (if (eq t d-m-c-u)
11629             (kill-local-variable 'default-mime-charset-unlimited)
11630           (set (make-local-variable 'default-mime-charset-unlimited)
11631                d-m-c-u)))))
11632
11633 (defun gnus-summary-setup-default-charset ()
11634   "Setup newsgroup default charset."
11635   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
11636       (progn
11637         (setq gnus-newsgroup-charset nil)
11638         (set (make-local-variable 'default-mime-charset) nil)
11639         (when (boundp 'default-mime-charset-unlimited);; LIMIT
11640           (set (make-local-variable 'default-mime-charset-unlimited) nil)))
11641     (let ((ignored-charsets
11642            (or gnus-newsgroup-ephemeral-ignored-charsets
11643                (append
11644                 (and gnus-newsgroup-name
11645                      (gnus-parameter-ignored-charsets gnus-newsgroup-name))
11646                 gnus-newsgroup-ignored-charsets)))
11647           charset)
11648       (setq gnus-newsgroup-charset
11649             (or gnus-newsgroup-ephemeral-charset
11650                 (when (and gnus-newsgroup-name
11651                            (setq charset (gnus-parameter-charset
11652                                           gnus-newsgroup-name)))
11653                   (make-local-variable 'default-mime-charset)
11654                   (setq default-mime-charset charset))
11655                 gnus-default-charset))
11656       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
11657            ignored-charsets))))
11658
11659 ;;;
11660 ;;; Mime Commands
11661 ;;;
11662
11663 (defun gnus-summary-display-buttonized (&optional show-all-parts)
11664   "Display the current article buffer fully MIME-buttonized.
11665 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
11666 treated as multipart/mixed."
11667   (interactive "P")
11668   (require 'gnus-art)
11669   (let ((gnus-unbuttonized-mime-types nil)
11670         (gnus-mime-display-multipart-as-mixed show-all-parts))
11671     (gnus-summary-show-article)))
11672
11673 (defun gnus-summary-repair-multipart (article)
11674   "Add a Content-Type header to a multipart article without one."
11675   (interactive (list (gnus-summary-article-number)))
11676   (gnus-with-article article
11677     (message-narrow-to-head)
11678     (message-remove-header "Mime-Version")
11679     (goto-char (point-max))
11680     (insert "Mime-Version: 1.0\n")
11681     (widen)
11682     (when (search-forward "\n--" nil t)
11683       (let ((separator (buffer-substring (point) (point-at-eol))))
11684         (message-narrow-to-head)
11685         (message-remove-header "Content-Type")
11686         (goto-char (point-max))
11687         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11688                         separator))
11689         (widen))))
11690   (let (gnus-mark-article-hook)
11691     (gnus-summary-select-article t t nil article)))
11692
11693 (defun gnus-summary-toggle-display-buttonized ()
11694   "Toggle the buttonizing of the article buffer."
11695   (interactive)
11696   (require 'gnus-art)
11697   (if (setq gnus-inhibit-mime-unbuttonizing
11698             (not gnus-inhibit-mime-unbuttonizing))
11699       (let ((gnus-unbuttonized-mime-types nil))
11700         (gnus-summary-show-article))
11701     (gnus-summary-show-article)))
11702
11703 ;;;
11704 ;;; Intelli-mouse commmands
11705 ;;;
11706
11707 (defun gnus-wheel-summary-scroll (event)
11708   (interactive "e")
11709   (let ((amount (if (memq 'shift (event-modifiers event))
11710                     (car gnus-wheel-scroll-amount)
11711                   (cdr gnus-wheel-scroll-amount)))
11712         (direction (- (* (static-if (featurep 'xemacs)
11713                              (event-button event)
11714                            (cond ((eq 'mouse-4 (event-basic-type event))
11715                                   4)
11716                                  ((eq 'mouse-5 (event-basic-type event))
11717                                   5)))
11718                          2) 9))
11719         edge)
11720     (gnus-summary-scroll-up (* amount direction))
11721     (when (gnus-eval-in-buffer-window gnus-article-buffer
11722             (save-restriction
11723               (widen)
11724               (and (if (< 0 direction)
11725                        (gnus-article-next-page 0)
11726                      (gnus-article-prev-page 0)
11727                      (bobp))
11728                    (if (setq edge (get-text-property
11729                                    (point-min) 'gnus-wheel-edge))
11730                        (setq edge (* edge direction))
11731                      (setq edge -1))
11732                    (or (plusp edge)
11733                        (let ((buffer-read-only nil)
11734                              (inhibit-read-only t))
11735                          (put-text-property (point-min) (point-max)
11736                                             'gnus-wheel-edge direction)
11737                          nil))
11738                    (or (> edge gnus-wheel-edge-resistance)
11739                        (let ((buffer-read-only nil)
11740                              (inhibit-read-only t))
11741                          (put-text-property (point-min) (point-max)
11742                                             'gnus-wheel-edge
11743                                             (* (1+ edge) direction))
11744                          nil))
11745                    (eq last-command 'gnus-wheel-summary-scroll))))
11746       (gnus-summary-next-article nil nil (minusp direction)))))
11747
11748 (defun gnus-wheel-install ()
11749   "Enable mouse wheel support on summary window."
11750   (when gnus-use-wheel
11751     (let ((keys
11752            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
11753       (dolist (key keys)
11754         (define-key gnus-summary-mode-map key
11755           'gnus-wheel-summary-scroll)))))
11756
11757 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
11758
11759 ;;;
11760 ;;; Traditional PGP commmands
11761 ;;;
11762
11763 (defun gnus-summary-decrypt-article (&optional force)
11764   "Decrypt the current article in traditional PGP way.
11765 This will have permanent effect only in mail groups.
11766 If FORCE is non-nil, allow editing of articles even in read-only
11767 groups."
11768   (interactive "P")
11769   (gnus-summary-select-article t)
11770   (gnus-eval-in-buffer-window gnus-article-buffer
11771     (save-excursion
11772       (save-restriction
11773         (widen)
11774         (goto-char (point-min))
11775         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
11776           (error "Not a traditional PGP message!"))
11777         (let ((armor-start (match-beginning 0)))
11778           (if (and (pgg-decrypt-region armor-start (point-max))
11779                    (or force (not (gnus-group-read-only-p))))
11780               (let ((inhibit-read-only t)
11781                     buffer-read-only)
11782                 (delete-region armor-start
11783                                (progn
11784                                  (re-search-forward "^-+END PGP" nil t)
11785                                  (beginning-of-line 2)
11786                                  (point)))
11787                 (insert-buffer-substring pgg-output-buffer))))))))
11788
11789 (defun gnus-summary-verify-article ()
11790   "Verify the current article in traditional PGP way."
11791   (interactive)
11792   (save-excursion
11793     (set-buffer gnus-original-article-buffer)
11794     (goto-char (point-min))
11795     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
11796       (error "Not a traditional PGP message!"))
11797     (re-search-forward "^-+END PGP" nil t)
11798     (beginning-of-line 2)
11799     (call-interactively (function pgg-verify-region))))
11800
11801 ;;;
11802 ;;; Generic summary marking commands
11803 ;;;
11804
11805 (defvar gnus-summary-marking-alist
11806   '((read gnus-del-mark "d")
11807     (unread gnus-unread-mark "u")
11808     (ticked gnus-ticked-mark "!")
11809     (dormant gnus-dormant-mark "?")
11810     (expirable gnus-expirable-mark "e"))
11811   "An alist of names/marks/keystrokes.")
11812
11813 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11814 (defvar gnus-summary-mark-map)
11815
11816 (defun gnus-summary-make-all-marking-commands ()
11817   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11818   (dolist (elem gnus-summary-marking-alist)
11819     (apply 'gnus-summary-make-marking-command elem)))
11820
11821 (defun gnus-summary-make-marking-command (name mark keystroke)
11822   (let ((map (make-sparse-keymap)))
11823     (define-key gnus-summary-generic-mark-map keystroke map)
11824     (dolist (lway `((next "next" next nil "n")
11825                     (next-unread "next unread" next t "N")
11826                     (prev "previous" prev nil "p")
11827                     (prev-unread "previous unread" prev t "P")
11828                     (nomove "" nil nil ,keystroke)))
11829       (let ((func (gnus-summary-make-marking-command-1
11830                    mark (car lway) lway name)))
11831         (setq func (eval func))
11832         (define-key map (nth 4 lway) func)))))
11833
11834 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11835   `(defun ,(intern
11836             (format "gnus-summary-put-mark-as-%s%s"
11837                     name (if (eq way 'nomove)
11838                              ""
11839                            (concat "-" (symbol-name way)))))
11840      (n)
11841      ,(format
11842        "Mark the current article as %s%s.
11843 If N, the prefix, then repeat N times.
11844 If N is negative, move in reverse order.
11845 The difference between N and the actual number of articles marked is
11846 returned."
11847        name (car (cdr lway)))
11848      (interactive "p")
11849      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11850
11851 (defun gnus-summary-generic-mark (n mark move unread)
11852   "Mark N articles with MARK."
11853   (unless (eq major-mode 'gnus-summary-mode)
11854     (error "This command can only be used in the summary buffer"))
11855   (gnus-summary-show-thread)
11856   (let ((nummove
11857          (cond
11858           ((eq move 'next) 1)
11859           ((eq move 'prev) -1)
11860           (t 0))))
11861     (if (zerop nummove)
11862         (setq n 1)
11863       (when (< n 0)
11864         (setq n (abs n)
11865               nummove (* -1 nummove))))
11866     (while (and (> n 0)
11867                 (gnus-summary-mark-article nil mark)
11868                 (zerop (gnus-summary-next-subject nummove unread t)))
11869       (setq n (1- n)))
11870     (when (/= 0 n)
11871       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11872     (gnus-summary-recenter)
11873     (gnus-summary-position-point)
11874     (gnus-set-mode-line 'summary)
11875     n))
11876
11877 (defun gnus-summary-insert-articles (articles)
11878   (when (setq articles
11879               (gnus-sorted-difference articles
11880                                       (mapcar (lambda (h)
11881                                                 (mail-header-number h))
11882                                               gnus-newsgroup-headers)))
11883     (setq gnus-newsgroup-headers
11884           (gnus-merge 'list
11885                       gnus-newsgroup-headers
11886                       (gnus-fetch-headers articles)
11887                       'gnus-article-sort-by-number))
11888     ;; Suppress duplicates?
11889     (when gnus-suppress-duplicates
11890       (gnus-dup-suppress-articles))
11891
11892     ;; We might want to build some more threads first.
11893     (when (and gnus-fetch-old-headers
11894                (eq gnus-headers-retrieved-by 'nov))
11895       (if (eq gnus-fetch-old-headers 'invisible)
11896           (gnus-build-all-threads)
11897         (gnus-build-old-threads)))
11898     ;; Let the Gnus agent mark articles as read.
11899     (when gnus-agent
11900       (gnus-agent-get-undownloaded-list))
11901     ;; Remove list identifiers from subject
11902     (when gnus-list-identifiers
11903       (gnus-summary-remove-list-identifiers))
11904     ;; First and last article in this newsgroup.
11905     (when gnus-newsgroup-headers
11906       (setq gnus-newsgroup-begin
11907             (mail-header-number (car gnus-newsgroup-headers))
11908             gnus-newsgroup-end
11909             (mail-header-number
11910              (gnus-last-element gnus-newsgroup-headers))))
11911     (when gnus-use-scoring
11912       (gnus-possibly-score-headers))))
11913
11914 (defun gnus-summary-insert-old-articles (&optional all)
11915   "Insert all old articles in this group.
11916 If ALL is non-nil, already read articles become readable.
11917 If ALL is a number, fetch this number of articles."
11918   (interactive "P")
11919   (prog1
11920       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11921             older len)
11922         (setq older
11923               ;; Some nntp servers lie about their active range.  When
11924               ;; this happens, the active range can be in the millions.
11925               ;; Use a compressed range to avoid creating a huge list.
11926               (gnus-range-difference (list gnus-newsgroup-active) old))
11927         (setq len (gnus-range-length older))
11928         (cond
11929          ((null older) nil)
11930          ((numberp all)
11931           (if (< all len)
11932               (let ((older-range (nreverse older)))
11933                 (setq older nil)
11934
11935                 (while (> all 0)
11936                   (let* ((r (pop older-range))
11937                          (min (if (numberp r) r (car r)))
11938                          (max (if (numberp r) r (cdr r))))
11939                     (while (and (<= min max)
11940                                 (> all 0))
11941                       (push max older)
11942                       (setq all (1- all)
11943                             max (1- max))))))
11944             (setq older (gnus-uncompress-range older))))
11945          (all
11946           (setq older (gnus-uncompress-range older)))
11947          (t
11948           (when (and (numberp gnus-large-newsgroup)
11949                    (> len gnus-large-newsgroup))
11950               (let* ((cursor-in-echo-area nil)
11951                      (initial (gnus-parameter-large-newsgroup-initial
11952                                gnus-newsgroup-name))
11953                      (input
11954                       (read-string
11955                        (format
11956                         "How many articles from %s (%s %d): "
11957                         (gnus-limit-string
11958                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11959                         (if initial "max" "default")
11960                         len)
11961                        (if initial
11962                            (cons (number-to-string initial)
11963                                  0)))))
11964                 (unless (string-match "^[ \t]*$" input)
11965                   (setq all (string-to-number input))
11966                   (if (< all len)
11967                       (let ((older-range (nreverse older)))
11968                         (setq older nil)
11969
11970                         (while (> all 0)
11971                           (let* ((r (pop older-range))
11972                                  (min (if (numberp r) r (car r)))
11973                                  (max (if (numberp r) r (cdr r))))
11974                             (while (and (<= min max)
11975                                         (> all 0))
11976                               (push max older)
11977                               (setq all (1- all)
11978                                     max (1- max))))))))))
11979           (setq older (gnus-uncompress-range older))))
11980         (if (not older)
11981             (message "No old news.")
11982           (gnus-summary-insert-articles older)
11983           (gnus-summary-limit (gnus-sorted-nunion old older))))
11984     (gnus-summary-position-point)))
11985
11986 (defun gnus-summary-insert-new-articles ()
11987   "Insert all new articles in this group."
11988   (interactive)
11989   (prog1
11990       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11991             (old-active gnus-newsgroup-active)
11992             (nnmail-fetched-sources (list t))
11993             i new)
11994         (setq gnus-newsgroup-active
11995               (gnus-activate-group gnus-newsgroup-name 'scan))
11996         (setq i (cdr gnus-newsgroup-active))
11997         (while (> i (cdr old-active))
11998           (push i new)
11999           (decf i))
12000         (if (not new)
12001             (message "No gnus is bad news")
12002           (gnus-summary-insert-articles new)
12003           (setq gnus-newsgroup-unreads
12004                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
12005           (gnus-summary-limit (gnus-sorted-nunion old new))))
12006     (gnus-summary-position-point)))
12007
12008 (gnus-summary-make-all-marking-commands)
12009
12010 (gnus-ems-redefine)
12011
12012 (provide 'gnus-sum)
12013
12014 (run-hooks 'gnus-sum-load-hook)
12015
12016 ;; Local Variables:
12017 ;; coding: iso-8859-1
12018 ;; End:
12019
12020 ;;; gnus-sum.el ends here