012cabcd828dd8346d0fad7e41ba2ff909827016
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval-when-compile (require 'cl))
30
31 (require 'gnus)
32 (require 'gnus-group)
33 (require 'gnus-spec)
34 (require 'gnus-range)
35 (require 'gnus-int)
36 (require 'gnus-undo)
37 (require 'gnus-util)
38 (require 'mm-decode)
39 (require 'nnoo)
40
41 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
42 (autoload 'gnus-cache-write-active "gnus-cache")
43 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
44 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
45 (autoload 'mm-uu-dissect "mm-uu")
46 (autoload 'gnus-article-outlook-deuglify-article "deuglify"
47   "Deuglify broken Outlook (Express) articles and redisplay."
48   t)
49
50 (defcustom gnus-kill-summary-on-exit t
51   "*If non-nil, kill the summary buffer when you exit from it.
52 If nil, the summary will become a \"*Dead Summary*\" buffer, and
53 it will be killed sometime later."
54   :group 'gnus-summary-exit
55   :type 'boolean)
56
57 (defcustom gnus-fetch-old-headers nil
58   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
59 If an unread article in the group refers to an older, already read (or
60 just marked as read) article, the old article will not normally be
61 displayed in the Summary buffer.  If this variable is non-nil, Gnus
62 will attempt to grab the headers to the old articles, and thereby
63 build complete threads.  If it has the value `some', only enough
64 headers to connect otherwise loose threads will be displayed.  This
65 variable can also be a number.  In that case, no more than that number
66 of old headers will be fetched.  If it has the value `invisible', all
67 old headers will be fetched, but none will be displayed.
68
69 The server has to support NOV for any of this to work."
70   :group 'gnus-thread
71   :type '(choice (const :tag "off" nil)
72                  (const some)
73                  number
74                  (sexp :menu-tag "other" t)))
75
76 (defcustom gnus-refer-thread-limit 200
77   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
78 If t, fetch all the available old headers."
79   :group 'gnus-thread
80   :type '(choice number
81                  (sexp :menu-tag "other" t)))
82
83 (defcustom gnus-summary-make-false-root 'adopt
84   "*nil means that Gnus won't gather loose threads.
85 If the root of a thread has expired or been read in a previous
86 session, the information necessary to build a complete thread has been
87 lost.  Instead of having many small sub-threads from this original thread
88 scattered all over the summary buffer, Gnus can gather them.
89
90 If non-nil, Gnus will try to gather all loose sub-threads from an
91 original thread into one large thread.
92
93 If this variable is non-nil, it should be one of `none', `adopt',
94 `dummy' or `empty'.
95
96 If this variable is `none', Gnus will not make a false root, but just
97 present the sub-threads after another.
98 If this variable is `dummy', Gnus will create a dummy root that will
99 have all the sub-threads as children.
100 If this variable is `adopt', Gnus will make one of the \"children\"
101 the parent and mark all the step-children as such.
102 If this variable is `empty', the \"children\" are printed with empty
103 subject fields.  (Or rather, they will be printed with a string
104 given by the `gnus-summary-same-subject' variable.)"
105   :group 'gnus-thread
106   :type '(choice (const :tag "off" nil)
107                  (const none)
108                  (const dummy)
109                  (const adopt)
110                  (const empty)))
111
112 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
113   "*A regexp to match subjects to be excluded from loose thread gathering.
114 As loose thread gathering is done on subjects only, that means that
115 there can be many false gatherings performed.  By rooting out certain
116 common subjects, gathering might become saner."
117   :group 'gnus-thread
118   :type 'regexp)
119
120 (defcustom gnus-summary-gather-subject-limit nil
121   "*Maximum length of subject comparisons when gathering loose threads.
122 Use nil to compare full subjects.  Setting this variable to a low
123 number will help gather threads that have been corrupted by
124 newsreaders chopping off subject lines, but it might also mean that
125 unrelated articles that have subject that happen to begin with the
126 same few characters will be incorrectly gathered.
127
128 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
129 comparing subjects."
130   :group 'gnus-thread
131   :type '(choice (const :tag "off" nil)
132                  (const fuzzy)
133                  (sexp :menu-tag "on" t)))
134
135 (defcustom gnus-simplify-subject-functions nil
136   "List of functions taking a string argument that simplify subjects.
137 The functions are applied recursively.
138
139 Useful functions to put in this list include:
140 `gnus-simplify-subject-re', `gnus-simplify-subject-fuzzy',
141 `gnus-simplify-whitespace', and `gnus-simplify-all-whitespace'."
142   :group 'gnus-thread
143   :type '(repeat function))
144
145 (defcustom gnus-simplify-ignored-prefixes nil
146   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
147   :group 'gnus-thread
148   :type '(choice (const :tag "off" nil)
149                  regexp))
150
151 (defcustom gnus-build-sparse-threads nil
152   "*If non-nil, fill in the gaps in threads.
153 If `some', only fill in the gaps that are needed to tie loose threads
154 together.  If `more', fill in all leaf nodes that Gnus can find.  If
155 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
156   :group 'gnus-thread
157   :type '(choice (const :tag "off" nil)
158                  (const some)
159                  (const more)
160                  (sexp :menu-tag "all" t)))
161
162 (defcustom gnus-summary-thread-gathering-function
163   'gnus-gather-threads-by-subject
164   "*Function used for gathering loose threads.
165 There are two pre-defined functions: `gnus-gather-threads-by-subject',
166 which only takes Subjects into consideration; and
167 `gnus-gather-threads-by-references', which compared the References
168 headers of the articles to find matches."
169   :group 'gnus-thread
170   :type '(radio (function-item gnus-gather-threads-by-subject)
171                 (function-item gnus-gather-threads-by-references)
172                 (function :tag "other")))
173
174 (defcustom gnus-summary-same-subject ""
175   "*String indicating that the current article has the same subject as the previous.
176 This variable will only be used if the value of
177 `gnus-summary-make-false-root' is `empty'."
178   :group 'gnus-summary-format
179   :type 'string)
180
181 (defcustom gnus-summary-goto-unread t
182   "*If t, many commands will go to the next unread article.
183 This applies to marking commands as well as other commands that
184 \"naturally\" select the next article, like, for instance, `SPC' at
185 the end of an article.
186
187 If nil, the marking commands do NOT go to the next unread article
188 \(they go to the next article instead).  If `never', commands that
189 usually go to the next unread article, will go to the next article,
190 whether it is read or not."
191   :group 'gnus-summary-marks
192   :link '(custom-manual "(gnus)Setting Marks")
193   :type '(choice (const :tag "off" nil)
194                  (const never)
195                  (sexp :menu-tag "on" t)))
196
197 (defcustom gnus-summary-default-score 0
198   "*Default article score level.
199 All scores generated by the score files will be added to this score.
200 If this variable is nil, scoring will be disabled."
201   :group 'gnus-score-default
202   :type '(choice (const :tag "disable")
203                  integer))
204
205 (defcustom gnus-summary-default-high-score 0
206   "*Default threshold for a high scored article.
207 An article will be highlighted as high scored if its score is greater
208 than this score."
209   :group 'gnus-score-default
210   :type 'integer)
211
212 (defcustom gnus-summary-default-low-score 0
213   "*Default threshold for a low scored article.
214 An article will be highlighted as low scored if its score is smaller
215 than this score."
216   :group 'gnus-score-default
217   :type 'integer)
218
219 (defcustom gnus-summary-zcore-fuzz 0
220   "*Fuzziness factor for the zcore in the summary buffer.
221 Articles with scores closer than this to `gnus-summary-default-score'
222 will not be marked."
223   :group 'gnus-summary-format
224   :type 'integer)
225
226 (defcustom gnus-simplify-subject-fuzzy-regexp nil
227   "*Strings to be removed when doing fuzzy matches.
228 This can either be a regular expression or list of regular expressions
229 that will be removed from subject strings if fuzzy subject
230 simplification is selected."
231   :group 'gnus-thread
232   :type '(repeat regexp))
233
234 (defcustom gnus-show-threads t
235   "*If non-nil, display threads in summary mode."
236   :group 'gnus-thread
237   :type 'boolean)
238
239 (defcustom gnus-thread-hide-subtree nil
240   "*If non-nil, hide all threads initially.
241 This can be a predicate specifier which says which threads to hide.
242 If threads are hidden, you have to run the command
243 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
244 to expose hidden threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-hide-killed t
249   "*If non-nil, hide killed threads automatically."
250   :group 'gnus-thread
251   :type 'boolean)
252
253 (defcustom gnus-thread-ignore-subject t
254   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
255 If nil, articles that have different subjects from their parents will
256 start separate threads."
257   :group 'gnus-thread
258   :type 'boolean)
259
260 (defcustom gnus-thread-operation-ignore-subject t
261   "*If non-nil, subjects will be ignored when doing thread commands.
262 This affects commands like `gnus-summary-kill-thread' and
263 `gnus-summary-lower-thread'.
264
265 If this variable is nil, articles in the same thread with different
266 subjects will not be included in the operation in question.  If this
267 variable is `fuzzy', only articles that have subjects that are fuzzily
268 equal will be included."
269   :group 'gnus-thread
270   :type '(choice (const :tag "off" nil)
271                  (const fuzzy)
272                  (sexp :tag "on" t)))
273
274 (defcustom gnus-thread-indent-level 4
275   "*Number that says how much each sub-thread should be indented."
276   :group 'gnus-thread
277   :type 'integer)
278
279 (defcustom gnus-auto-extend-newsgroup t
280   "*If non-nil, extend newsgroup forward and backward when requested."
281   :group 'gnus-summary-choose
282   :type 'boolean)
283
284 (defcustom gnus-auto-select-first t
285   "*If non-nil, select the article under point.
286 Which article this is is controlled by the `gnus-auto-select-subject'
287 variable.
288
289 If you want to prevent automatic selection of articles in some
290 newsgroups, set the variable to nil in `gnus-select-group-hook'."
291   :group 'gnus-group-select
292   :type '(choice (const :tag "none" nil)
293                  (sexp :menu-tag "first" t)))
294
295 (defcustom gnus-auto-select-subject 'unread
296   "*Says what subject to place under point when entering a group.
297
298 This variable can either be the symbols `first' (place point on the
299 first subject), `unread' (place point on the subject line of the first
300 unread article), `best' (place point on the subject line of the
301 higest-scored article), `unseen' (place point on the subject line of
302 the first unseen article), 'unseen-or-unread' (place point on the subject
303 line of the first unseen article or, if all article have been seen, on the
304 subject line of the first unread article), or a function to be called to
305 place point on some subject line."
306   :group 'gnus-group-select
307   :type '(choice (const best)
308                  (const unread)
309                  (const first)
310                  (const unseen)
311                  (const unseen-or-unread)))
312
313 (defcustom gnus-auto-select-next t
314   "*If non-nil, offer to go to the next group from the end of the previous.
315 If the value is t and the next newsgroup is empty, Gnus will exit
316 summary mode and go back to group mode.  If the value is neither nil
317 nor t, Gnus will select the following unread newsgroup.  In
318 particular, if the value is the symbol `quietly', the next unread
319 newsgroup will be selected without any confirmation, and if it is
320 `almost-quietly', the next group will be selected without any
321 confirmation if you are located on the last article in the group.
322 Finally, if this variable is `slightly-quietly', the `Z n' command
323 will go to the next group without confirmation."
324   :group 'gnus-summary-maneuvering
325   :type '(choice (const :tag "off" nil)
326                  (const quietly)
327                  (const almost-quietly)
328                  (const slightly-quietly)
329                  (sexp :menu-tag "on" t)))
330
331 (defcustom gnus-auto-select-same nil
332   "*If non-nil, select the next article with the same subject.
333 If there are no more articles with the same subject, go to
334 the first unread article."
335   :group 'gnus-summary-maneuvering
336   :type 'boolean)
337
338 (defcustom gnus-summary-check-current nil
339   "*If non-nil, consider the current article when moving.
340 The \"unread\" movement commands will stay on the same line if the
341 current article is unread."
342   :group 'gnus-summary-maneuvering
343   :type 'boolean)
344
345 (defcustom gnus-auto-center-summary t
346   "*If non-nil, always center the current summary buffer.
347 In particular, if `vertical' do only vertical recentering.  If non-nil
348 and non-`vertical', do both horizontal and vertical recentering."
349   :group 'gnus-summary-maneuvering
350   :type '(choice (const :tag "none" nil)
351                  (const vertical)
352                  (integer :tag "height")
353                  (sexp :menu-tag "both" t)))
354
355 (defcustom gnus-show-all-headers nil
356   "*If non-nil, don't hide any headers."
357   :group 'gnus-article-hiding
358   :group 'gnus-article-headers
359   :type 'boolean)
360
361 (defcustom gnus-summary-ignore-duplicates nil
362   "*If non-nil, ignore articles with identical Message-ID headers."
363   :group 'gnus-summary
364   :type 'boolean)
365
366 (defcustom gnus-single-article-buffer t
367   "*If non-nil, display all articles in the same buffer.
368 If nil, each group will get its own article buffer."
369   :group 'gnus-article-various
370   :type 'boolean)
371
372 (defcustom gnus-break-pages t
373   "*If non-nil, do page breaking on articles.
374 The page delimiter is specified by the `gnus-page-delimiter'
375 variable."
376   :group 'gnus-article-various
377   :type 'boolean)
378
379 (defcustom gnus-move-split-methods nil
380   "*Variable used to suggest where articles are to be moved to.
381 It uses the same syntax as the `gnus-split-methods' variable.
382 However, whereas `gnus-split-methods' specifies file names as targets,
383 this variable specifies group names."
384   :group 'gnus-summary-mail
385   :type '(repeat (choice (list :value (fun) function)
386                          (cons :value ("" "") regexp (repeat string))
387                          (sexp :value nil))))
388
389 (defcustom gnus-unread-mark ?           ;Whitespace
390   "*Mark used for unread articles."
391   :group 'gnus-summary-marks
392   :type 'character)
393
394 (defcustom gnus-ticked-mark ?!
395   "*Mark used for ticked articles."
396   :group 'gnus-summary-marks
397   :type 'character)
398
399 (defcustom gnus-dormant-mark ??
400   "*Mark used for dormant articles."
401   :group 'gnus-summary-marks
402   :type 'character)
403
404 (defcustom gnus-del-mark ?r
405   "*Mark used for del'd articles."
406   :group 'gnus-summary-marks
407   :type 'character)
408
409 (defcustom gnus-read-mark ?R
410   "*Mark used for read articles."
411   :group 'gnus-summary-marks
412   :type 'character)
413
414 (defcustom gnus-expirable-mark ?E
415   "*Mark used for expirable articles."
416   :group 'gnus-summary-marks
417   :type 'character)
418
419 (defcustom gnus-killed-mark ?K
420   "*Mark used for killed articles."
421   :group 'gnus-summary-marks
422   :type 'character)
423
424 (defcustom gnus-spam-mark ?H
425   "*Mark used for spam articles."
426   :group 'gnus-summary-marks
427   :type 'character)
428
429 (defcustom gnus-souped-mark ?F
430   "*Mark used for souped articles."
431   :group 'gnus-summary-marks
432   :type 'character)
433
434 (defcustom gnus-kill-file-mark ?X
435   "*Mark used for articles killed by kill files."
436   :group 'gnus-summary-marks
437   :type 'character)
438
439 (defcustom gnus-low-score-mark ?Y
440   "*Mark used for articles with a low score."
441   :group 'gnus-summary-marks
442   :type 'character)
443
444 (defcustom gnus-catchup-mark ?C
445   "*Mark used for articles that are caught up."
446   :group 'gnus-summary-marks
447   :type 'character)
448
449 (defcustom gnus-replied-mark ?A
450   "*Mark used for articles that have been replied to."
451   :group 'gnus-summary-marks
452   :type 'character)
453
454 (defcustom gnus-forwarded-mark ?F
455   "*Mark used for articles that have been forwarded."
456   :group 'gnus-summary-marks
457   :type 'character)
458
459 (defcustom gnus-recent-mark ?N
460   "*Mark used for articles that are recent."
461   :group 'gnus-summary-marks
462   :type 'character)
463
464 (defcustom gnus-cached-mark ?*
465   "*Mark used for articles that are in the cache."
466   :group 'gnus-summary-marks
467   :type 'character)
468
469 (defcustom gnus-saved-mark ?S
470   "*Mark used for articles that have been saved."
471   :group 'gnus-summary-marks
472   :type 'character)
473
474 (defcustom gnus-unseen-mark ?.
475   "*Mark used for articles that haven't been seen."
476   :group 'gnus-summary-marks
477   :type 'character)
478
479 (defcustom gnus-no-mark ?               ;Whitespace
480   "*Mark used for articles that have no other secondary mark."
481   :group 'gnus-summary-marks
482   :type 'character)
483
484 (defcustom gnus-ancient-mark ?O
485   "*Mark used for ancient articles."
486   :group 'gnus-summary-marks
487   :type 'character)
488
489 (defcustom gnus-sparse-mark ?Q
490   "*Mark used for sparsely reffed articles."
491   :group 'gnus-summary-marks
492   :type 'character)
493
494 (defcustom gnus-canceled-mark ?G
495   "*Mark used for canceled articles."
496   :group 'gnus-summary-marks
497   :type 'character)
498
499 (defcustom gnus-duplicate-mark ?M
500   "*Mark used for duplicate articles."
501   :group 'gnus-summary-marks
502   :type 'character)
503
504 (defcustom gnus-undownloaded-mark ?@
505   "*Mark used for articles that weren't downloaded."
506   :group 'gnus-summary-marks
507   :type 'character)
508
509 (defcustom gnus-downloadable-mark ?%
510   "*Mark used for articles that are to be downloaded."
511   :group 'gnus-summary-marks
512   :type 'character)
513
514 (defcustom gnus-unsendable-mark ?=
515   "*Mark used for articles that won't be sent."
516   :group 'gnus-summary-marks
517   :type 'character)
518
519 (defcustom gnus-score-over-mark ?+
520   "*Score mark used for articles with high scores."
521   :group 'gnus-summary-marks
522   :type 'character)
523
524 (defcustom gnus-score-below-mark ?-
525   "*Score mark used for articles with low scores."
526   :group 'gnus-summary-marks
527   :type 'character)
528
529 (defcustom gnus-empty-thread-mark ?     ;Whitespace
530   "*There is no thread under the article."
531   :group 'gnus-summary-marks
532   :type 'character)
533
534 (defcustom gnus-not-empty-thread-mark ?=
535   "*There is a thread under the article."
536   :group 'gnus-summary-marks
537   :type 'character)
538
539 (defcustom gnus-view-pseudo-asynchronously nil
540   "*If non-nil, Gnus will view pseudo-articles asynchronously."
541   :group 'gnus-extract-view
542   :type 'boolean)
543
544 (defcustom gnus-auto-expirable-marks
545   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
546         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
547         gnus-souped-mark gnus-duplicate-mark)
548   "*The list of marks converted into expiration if a group is auto-expirable."
549   :version "21.1"
550   :group 'gnus-summary
551   :type '(repeat character))
552
553 (defcustom gnus-inhibit-user-auto-expire t
554   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
555   :version "21.1"
556   :group 'gnus-summary
557   :type 'boolean)
558
559 (defcustom gnus-view-pseudos nil
560   "*If `automatic', pseudo-articles will be viewed automatically.
561 If `not-confirm', pseudos will be viewed automatically, and the user
562 will not be asked to confirm the command."
563   :group 'gnus-extract-view
564   :type '(choice (const :tag "off" nil)
565                  (const automatic)
566                  (const not-confirm)))
567
568 (defcustom gnus-view-pseudos-separately t
569   "*If non-nil, one pseudo-article will be created for each file to be viewed.
570 If nil, all files that use the same viewing command will be given as a
571 list of parameters to that command."
572   :group 'gnus-extract-view
573   :type 'boolean)
574
575 (defcustom gnus-insert-pseudo-articles t
576   "*If non-nil, insert pseudo-articles when decoding articles."
577   :group 'gnus-extract-view
578   :type 'boolean)
579
580 (defcustom gnus-summary-dummy-line-format
581   "  %(:                          :%) %S\n"
582   "*The format specification for the dummy roots in the summary buffer.
583 It works along the same lines as a normal formatting string,
584 with some simple extensions.
585
586 %S  The subject
587
588 General format specifiers can also be used.
589 See `(gnus)Formatting Variables'."
590   :link '(custom-manual "(gnus)Formatting Variables")
591   :group 'gnus-threading
592   :type 'string)
593
594 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
595   "*The format specification for the summary mode line.
596 It works along the same lines as a normal formatting string,
597 with some simple extensions:
598
599 %G  Group name
600 %p  Unprefixed group name
601 %A  Current article number
602 %z  Current article score
603 %V  Gnus version
604 %U  Number of unread articles in the group
605 %e  Number of unselected articles in the group
606 %Z  A string with unread/unselected article counts
607 %g  Shortish group name
608 %S  Subject of the current article
609 %u  User-defined spec
610 %s  Current score file name
611 %d  Number of dormant articles
612 %r  Number of articles that have been marked as read in this session
613 %E  Number of articles expunged by the score files"
614   :group 'gnus-summary-format
615   :type 'string)
616
617 (defcustom gnus-list-identifiers nil
618   "Regexp that matches list identifiers to be removed from subject.
619 This can also be a list of regexps."
620   :version "21.1"
621   :group 'gnus-summary-format
622   :group 'gnus-article-hiding
623   :type '(choice (const :tag "none" nil)
624                  (regexp :value ".*")
625                  (repeat :value (".*") regexp)))
626
627 (defcustom gnus-summary-mark-below 0
628   "*Mark all articles with a score below this variable as read.
629 This variable is local to each summary buffer and usually set by the
630 score file."
631   :group 'gnus-score-default
632   :type 'integer)
633
634 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
635   "*List of functions used for sorting articles in the summary buffer.
636
637 Each function takes two articles and returns non-nil if the first
638 article should be sorted before the other.  If you use more than one
639 function, the primary sort function should be the last.  You should
640 probably always include `gnus-article-sort-by-number' in the list of
641 sorting functions -- preferably first.  Also note that sorting by date
642 is often much slower than sorting by number, and the sorting order is
643 very similar.  (Sorting by date means sorting by the time the message
644 was sent, sorting by number means sorting by arrival time.)
645
646 Ready-made functions include `gnus-article-sort-by-number',
647 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
648 `gnus-article-sort-by-date', `gnus-article-sort-by-random'
649 and `gnus-article-sort-by-score'.
650
651 When threading is turned on, the variable `gnus-thread-sort-functions'
652 controls how articles are sorted."
653   :group 'gnus-summary-sort
654   :type '(repeat (choice (function-item gnus-article-sort-by-number)
655                          (function-item gnus-article-sort-by-author)
656                          (function-item gnus-article-sort-by-subject)
657                          (function-item gnus-article-sort-by-date)
658                          (function-item gnus-article-sort-by-score)
659                          (function-item gnus-article-sort-by-random)
660                          (function :tag "other"))))
661
662 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
663   "*List of functions used for sorting threads in the summary buffer.
664 By default, threads are sorted by article number.
665
666 Each function takes two threads and returns non-nil if the first
667 thread should be sorted before the other.  If you use more than one
668 function, the primary sort function should be the last.  You should
669 probably always include `gnus-thread-sort-by-number' in the list of
670 sorting functions -- preferably first.  Also note that sorting by date
671 is often much slower than sorting by number, and the sorting order is
672 very similar.  (Sorting by date means sorting by the time the message
673 was sent, sorting by number means sorting by arrival time.)
674
675 Ready-made functions include `gnus-thread-sort-by-number',
676 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
677 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score',
678 `gnus-thread-sort-by-most-recent-number',
679 `gnus-thread-sort-by-most-recent-date',
680 `gnus-thread-sort-by-random', and
681 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
682
683 When threading is turned off, the variable
684 `gnus-article-sort-functions' controls how articles are sorted."
685   :group 'gnus-summary-sort
686   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
687                          (function-item gnus-thread-sort-by-author)
688                          (function-item gnus-thread-sort-by-subject)
689                          (function-item gnus-thread-sort-by-date)
690                          (function-item gnus-thread-sort-by-score)
691                          (function-item gnus-thread-sort-by-total-score)
692                          (function-item gnus-thread-sort-by-random)
693                          (function :tag "other"))))
694
695 (defcustom gnus-thread-score-function '+
696   "*Function used for calculating the total score of a thread.
697
698 The function is called with the scores of the article and each
699 subthread and should then return the score of the thread.
700
701 Some functions you can use are `+', `max', or `min'."
702   :group 'gnus-summary-sort
703   :type 'function)
704
705 (defcustom gnus-summary-expunge-below nil
706   "All articles that have a score less than this variable will be expunged.
707 This variable is local to the summary buffers."
708   :group 'gnus-score-default
709   :type '(choice (const :tag "off" nil)
710                  integer))
711
712 (defcustom gnus-thread-expunge-below nil
713   "All threads that have a total score less than this variable will be expunged.
714 See `gnus-thread-score-function' for en explanation of what a
715 \"thread score\" is.
716
717 This variable is local to the summary buffers."
718   :group 'gnus-threading
719   :group 'gnus-score-default
720   :type '(choice (const :tag "off" nil)
721                  integer))
722
723 (defcustom gnus-summary-mode-hook nil
724   "*A hook for Gnus summary mode.
725 This hook is run before any variables are set in the summary buffer."
726   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
727   :group 'gnus-summary-various
728   :type 'hook)
729
730 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
731 (when (featurep 'xemacs)
732   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
733   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
734   (add-hook 'gnus-summary-mode-hook
735             'gnus-xmas-switch-horizontal-scrollbar-off))
736
737 (defcustom gnus-summary-menu-hook nil
738   "*Hook run after the creation of the summary mode menu."
739   :group 'gnus-summary-visual
740   :type 'hook)
741
742 (defcustom gnus-summary-exit-hook nil
743   "*A hook called on exit from the summary buffer.
744 It will be called with point in the group buffer."
745   :group 'gnus-summary-exit
746   :type 'hook)
747
748 (defcustom gnus-summary-prepare-hook nil
749   "*A hook called after the summary buffer has been generated.
750 If you want to modify the summary buffer, you can use this hook."
751   :group 'gnus-summary-various
752   :type 'hook)
753
754 (defcustom gnus-summary-prepared-hook nil
755   "*A hook called as the last thing after the summary buffer has been generated."
756   :group 'gnus-summary-various
757   :type 'hook)
758
759 (defcustom gnus-summary-generate-hook nil
760   "*A hook run just before generating the summary buffer.
761 This hook is commonly used to customize threading variables and the
762 like."
763   :group 'gnus-summary-various
764   :type 'hook)
765
766 (defcustom gnus-select-group-hook nil
767   "*A hook called when a newsgroup is selected.
768
769 If you'd like to simplify subjects like the
770 `gnus-summary-next-same-subject' command does, you can use the
771 following hook:
772
773  (add-hook gnus-select-group-hook
774            (lambda ()
775              (mapcar (lambda (header)
776                        (mail-header-set-subject
777                         header
778                         (gnus-simplify-subject
779                          (mail-header-subject header) 're-only)))
780                      gnus-newsgroup-headers)))"
781   :group 'gnus-group-select
782   :type 'hook)
783
784 (defcustom gnus-select-article-hook nil
785   "*A hook called when an article is selected."
786   :group 'gnus-summary-choose
787   :type 'hook)
788
789 (defcustom gnus-visual-mark-article-hook
790   (list 'gnus-highlight-selected-summary)
791   "*Hook run after selecting an article in the summary buffer.
792 It is meant to be used for highlighting the article in some way.  It
793 is not run if `gnus-visual' is nil."
794   :group 'gnus-summary-visual
795   :type 'hook)
796
797 (defcustom gnus-parse-headers-hook nil
798   "*A hook called before parsing the headers."
799   :group 'gnus-various
800   :type 'hook)
801
802 (defcustom gnus-exit-group-hook nil
803   "*A hook called when exiting summary mode.
804 This hook is not called from the non-updating exit commands like `Q'."
805   :group 'gnus-various
806   :type 'hook)
807
808 (defcustom gnus-summary-update-hook
809   (list 'gnus-summary-highlight-line)
810   "*A hook called when a summary line is changed.
811 The hook will not be called if `gnus-visual' is nil.
812
813 The default function `gnus-summary-highlight-line' will
814 highlight the line according to the `gnus-summary-highlight'
815 variable."
816   :group 'gnus-summary-visual
817   :type 'hook)
818
819 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
820   "*A hook called when an article is selected for the first time.
821 The hook is intended to mark an article as read (or unread)
822 automatically when it is selected."
823   :group 'gnus-summary-choose
824   :type 'hook)
825
826 (defcustom gnus-group-no-more-groups-hook nil
827   "*A hook run when returning to group mode having no more (unread) groups."
828   :group 'gnus-group-select
829   :type 'hook)
830
831 (defcustom gnus-ps-print-hook nil
832   "*A hook run before ps-printing something from Gnus."
833   :group 'gnus-summary
834   :type 'hook)
835
836 (defcustom gnus-summary-display-arrow
837   (and (fboundp 'display-graphic-p)
838        (display-graphic-p))
839   "*If non-nil, display an arrow highlighting the current article."
840   :version "21.1"
841   :group 'gnus-summary
842   :type 'boolean)
843
844 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
845   "Face used for highlighting the current article in the summary buffer."
846   :group 'gnus-summary-visual
847   :type 'face)
848
849 (defcustom gnus-summary-highlight
850   '(((eq mark gnus-canceled-mark)
851      . gnus-summary-cancelled-face)
852     ((and (> score default-high)
853           (or (eq mark gnus-dormant-mark)
854               (eq mark gnus-ticked-mark)))
855      . gnus-summary-high-ticked-face)
856     ((and (< score default-low)
857           (or (eq mark gnus-dormant-mark)
858               (eq mark gnus-ticked-mark)))
859      . gnus-summary-low-ticked-face)
860     ((or (eq mark gnus-dormant-mark)
861          (eq mark gnus-ticked-mark))
862      . gnus-summary-normal-ticked-face)
863     ((and (> score default-high) (eq mark gnus-ancient-mark))
864      . gnus-summary-high-ancient-face)
865     ((and (< score default-low) (eq mark gnus-ancient-mark))
866      . gnus-summary-low-ancient-face)
867     ((eq mark gnus-ancient-mark)
868      . gnus-summary-normal-ancient-face)
869     ((and (> score default-high) (eq mark gnus-unread-mark))
870      . gnus-summary-high-unread-face)
871     ((and (< score default-low) (eq mark gnus-unread-mark))
872      . gnus-summary-low-unread-face)
873     ((eq mark gnus-unread-mark)
874      . gnus-summary-normal-unread-face)
875     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
876                                                   gnus-undownloaded-mark)))
877      . gnus-summary-high-unread-face)
878     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
879                                                  gnus-undownloaded-mark)))
880      . gnus-summary-low-unread-face)
881     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
882           (memq article gnus-newsgroup-unreads))
883      . gnus-summary-normal-unread-face)
884     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
885      . gnus-summary-normal-read-face)
886     ((> score default-high)
887      . gnus-summary-high-read-face)
888     ((< score default-low)
889      . gnus-summary-low-read-face)
890     (t
891      . gnus-summary-normal-read-face))
892   "*Controls the highlighting of summary buffer lines.
893
894 A list of (FORM . FACE) pairs.  When deciding how a a particular
895 summary line should be displayed, each form is evaluated.  The content
896 of the face field after the first true form is used.  You can change
897 how those summary lines are displayed, by editing the face field.
898
899 You can use the following variables in the FORM field.
900
901 score:        The article's score
902 default:      The default article score.
903 default-high: The default score for high scored articles.
904 default-low:  The default score for low scored articles.
905 below:        The score below which articles are automatically marked as read.
906 mark:         The articles mark."
907   :group 'gnus-summary-visual
908   :type '(repeat (cons (sexp :tag "Form" nil)
909                        face)))
910
911 (defcustom gnus-alter-header-function nil
912   "Function called to allow alteration of article header structures.
913 The function is called with one parameter, the article header vector,
914 which it may alter in any way.")
915
916 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
917   "Variable that says which function should be used to decode a string with encoded words.")
918
919 (defcustom gnus-extra-headers '(To Newsgroups)
920   "*Extra headers to parse."
921   :version "21.1"
922   :group 'gnus-summary
923   :type '(repeat symbol))
924
925 (defcustom gnus-ignored-from-addresses
926   (and user-mail-address (regexp-quote user-mail-address))
927   "*Regexp of From headers that may be suppressed in favor of To headers."
928   :version "21.1"
929   :group 'gnus-summary
930   :type 'regexp)
931
932 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
933   "List of charsets that should be ignored.
934 When these charsets are used in the \"charset\" parameter, the
935 default charset will be used instead."
936   :version "21.1"
937   :type '(repeat symbol)
938   :group 'gnus-charset)
939
940 (gnus-define-group-parameter
941  ignored-charsets
942  :type list
943  :function-document
944  "Return the ignored charsets of GROUP."
945  :variable gnus-group-ignored-charsets-alist
946  :variable-default
947  '(("alt\\.chinese\\.text" iso-8859-1))
948  :variable-document
949  "Alist of regexps (to match group names) and charsets that should be ignored.
950 When these charsets are used in the \"charset\" parameter, the
951 default charset will be used instead."
952  :variable-group gnus-charset
953  :variable-type '(repeat (cons (regexp :tag "Group")
954                                (repeat symbol)))
955  :parameter-type '(choice :tag "Ignored charsets"
956                           :value nil
957                           (repeat (symbol)))
958  :parameter-document       "\
959 List of charsets that should be ignored.
960
961 When these charsets are used in the \"charset\" parameter, the
962 default charset will be used instead.")
963
964 (defcustom gnus-group-highlight-words-alist nil
965   "Alist of group regexps and highlight regexps.
966 This variable uses the same syntax as `gnus-emphasis-alist'."
967   :version "21.1"
968   :type '(repeat (cons (regexp :tag "Group")
969                        (repeat (list (regexp :tag "Highlight regexp")
970                                      (number :tag "Group for entire word" 0)
971                                      (number :tag "Group for displayed part" 0)
972                                      (symbol :tag "Face"
973                                              gnus-emphasis-highlight-words)))))
974   :group 'gnus-summary-visual)
975
976 (defcustom gnus-summary-show-article-charset-alist
977   nil
978   "Alist of number and charset.
979 The article will be shown with the charset corresponding to the
980 numbered argument.
981 For example: ((1 . cn-gb-2312) (2 . big5))."
982   :version "21.1"
983   :type '(repeat (cons (number :tag "Argument" 1)
984                        (symbol :tag "Charset")))
985   :group 'gnus-charset)
986
987 (defcustom gnus-preserve-marks t
988   "Whether marks are preserved when moving, copying and respooling messages."
989   :version "21.1"
990   :type 'boolean
991   :group 'gnus-summary-marks)
992
993 (defcustom gnus-alter-articles-to-read-function nil
994   "Function to be called to alter the list of articles to be selected."
995   :type '(choice (const nil) function)
996   :group 'gnus-summary)
997
998 (defcustom gnus-orphan-score nil
999   "*All orphans get this score added.  Set in the score file."
1000   :group 'gnus-score-default
1001   :type '(choice (const nil)
1002                  integer))
1003
1004 (defcustom gnus-summary-save-parts-default-mime "image/.*"
1005   "*A regexp to match MIME parts when saving multiple parts of a message
1006 with gnus-summary-save-parts (X m). This regexp will be used by default
1007 when prompting the user for which type of files to save."
1008   :group 'gnus-summary
1009   :type 'regexp)
1010
1011 (defcustom gnus-read-all-available-headers nil
1012   "Whether Gnus should parse all headers made available to it.
1013 This is mostly relevant for slow backends where the user may
1014 wish to widen the summary buffer to include all headers
1015 that were fetched.  Say, for nnultimate groups."
1016   :group 'gnus-summary
1017   :type '(choice boolean regexp))
1018
1019 (defcustom gnus-summary-muttprint-program "muttprint"
1020   "Command (and optional arguments) used to run Muttprint."
1021   :version "21.3"
1022   :group 'gnus-summary
1023   :type 'string)
1024
1025 (defcustom gnus-article-loose-mime nil
1026   "If non-nil, don't require MIME-Version header.
1027 Some brain-damaged MUA/MTA, e.g. Lotus Domino 5.0.6 clients, does not
1028 supply the MIME-Version header or deliberately strip it From the mail.
1029 Set it to non-nil, Gnus will treat some articles as MIME even if
1030 the MIME-Version header is missed."
1031   :version "21.3"
1032   :type 'boolean
1033   :group 'gnus-article)
1034
1035 ;;; Internal variables
1036
1037 (defvar gnus-summary-display-cache nil)
1038 (defvar gnus-article-mime-handles nil)
1039 (defvar gnus-article-decoded-p nil)
1040 (defvar gnus-article-charset nil)
1041 (defvar gnus-article-ignored-charsets nil)
1042 (defvar gnus-scores-exclude-files nil)
1043 (defvar gnus-page-broken nil)
1044 (defvar gnus-inhibit-mime-unbuttonizing nil)
1045
1046 (defvar gnus-original-article nil)
1047 (defvar gnus-article-internal-prepare-hook nil)
1048 (defvar gnus-newsgroup-process-stack nil)
1049
1050 (defvar gnus-thread-indent-array nil)
1051 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1052 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1053   "Function called to sort the articles within a thread after it has been gathered together.")
1054
1055 (defvar gnus-summary-save-parts-type-history nil)
1056 (defvar gnus-summary-save-parts-last-directory nil)
1057
1058 ;; Avoid highlighting in kill files.
1059 (defvar gnus-summary-inhibit-highlight nil)
1060 (defvar gnus-newsgroup-selected-overlay nil)
1061 (defvar gnus-inhibit-limiting nil)
1062 (defvar gnus-newsgroup-adaptive-score-file nil)
1063 (defvar gnus-current-score-file nil)
1064 (defvar gnus-current-move-group nil)
1065 (defvar gnus-current-copy-group nil)
1066 (defvar gnus-current-crosspost-group nil)
1067 (defvar gnus-newsgroup-display nil)
1068
1069 (defvar gnus-newsgroup-dependencies nil)
1070 (defvar gnus-newsgroup-adaptive nil)
1071 (defvar gnus-summary-display-article-function nil)
1072 (defvar gnus-summary-highlight-line-function nil
1073   "Function called after highlighting a summary line.")
1074
1075 (defvar gnus-summary-line-format-alist
1076   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1077     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1078     (?s gnus-tmp-subject-or-nil ?s)
1079     (?n gnus-tmp-name ?s)
1080     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1081         ?s)
1082     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1083             gnus-tmp-from) ?s)
1084     (?F gnus-tmp-from ?s)
1085     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1086     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1087     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1088     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1089     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1090     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1091     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1092     (?k (gnus-summary-line-message-size gnus-tmp-header) ?s)
1093     (?L gnus-tmp-lines ?s)
1094     (?I gnus-tmp-indentation ?s)
1095     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1096     (?R gnus-tmp-replied ?c)
1097     (?\[ gnus-tmp-opening-bracket ?c)
1098     (?\] gnus-tmp-closing-bracket ?c)
1099     (?\> (make-string gnus-tmp-level ? ) ?s)
1100     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1101     (?i gnus-tmp-score ?d)
1102     (?z gnus-tmp-score-char ?c)
1103     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1104     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1105     (?U gnus-tmp-unread ?c)
1106     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header gnus-tmp-from)
1107         ?s)
1108     (?t (gnus-summary-number-of-articles-in-thread
1109          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1110         ?d)
1111     (?e (gnus-summary-number-of-articles-in-thread
1112          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1113         ?c)
1114     (?u gnus-tmp-user-defined ?s)
1115     (?P (gnus-pick-line-number) ?d)
1116     (?B gnus-tmp-thread-tree-header-string ?s)
1117     (user-date (gnus-user-date
1118                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1119   "An alist of format specifications that can appear in summary lines.
1120 These are paired with what variables they correspond with, along with
1121 the type of the variable (string, integer, character, etc).")
1122
1123 (defvar gnus-summary-dummy-line-format-alist
1124   `((?S gnus-tmp-subject ?s)
1125     (?N gnus-tmp-number ?d)
1126     (?u gnus-tmp-user-defined ?s)))
1127
1128 (defvar gnus-summary-mode-line-format-alist
1129   `((?G gnus-tmp-group-name ?s)
1130     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1131     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1132     (?A gnus-tmp-article-number ?d)
1133     (?Z gnus-tmp-unread-and-unselected ?s)
1134     (?V gnus-version ?s)
1135     (?U gnus-tmp-unread-and-unticked ?d)
1136     (?S gnus-tmp-subject ?s)
1137     (?e gnus-tmp-unselected ?d)
1138     (?u gnus-tmp-user-defined ?s)
1139     (?d (length gnus-newsgroup-dormant) ?d)
1140     (?t (length gnus-newsgroup-marked) ?d)
1141     (?h (length gnus-newsgroup-spam-marked) ?d)
1142     (?r (length gnus-newsgroup-reads) ?d)
1143     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1144     (?E gnus-newsgroup-expunged-tally ?d)
1145     (?s (gnus-current-score-file-nondirectory) ?s)))
1146
1147 (defvar gnus-last-search-regexp nil
1148   "Default regexp for article search command.")
1149
1150 (defvar gnus-last-shell-command nil
1151   "Default shell command on article.")
1152
1153 (defvar gnus-newsgroup-begin nil)
1154 (defvar gnus-newsgroup-end nil)
1155 (defvar gnus-newsgroup-last-rmail nil)
1156 (defvar gnus-newsgroup-last-mail nil)
1157 (defvar gnus-newsgroup-last-folder nil)
1158 (defvar gnus-newsgroup-last-file nil)
1159 (defvar gnus-newsgroup-auto-expire nil)
1160 (defvar gnus-newsgroup-active nil)
1161
1162 (defvar gnus-newsgroup-data nil)
1163 (defvar gnus-newsgroup-data-reverse nil)
1164 (defvar gnus-newsgroup-limit nil)
1165 (defvar gnus-newsgroup-limits nil)
1166
1167 (defvar gnus-newsgroup-unreads nil
1168   "Sorted list of unread articles in the current newsgroup.")
1169
1170 (defvar gnus-newsgroup-unselected nil
1171   "Sorted list of unselected unread articles in the current newsgroup.")
1172
1173 (defvar gnus-newsgroup-reads nil
1174   "Alist of read articles and article marks in the current newsgroup.")
1175
1176 (defvar gnus-newsgroup-expunged-tally nil)
1177
1178 (defvar gnus-newsgroup-marked nil
1179   "Sorted list of ticked articles in the current newsgroup (a subset of unread art).")
1180
1181 (defvar gnus-newsgroup-spam-marked nil
1182   "List of ranges of articles that have been marked as spam.")
1183
1184 (defvar gnus-newsgroup-killed nil
1185   "List of ranges of articles that have been through the scoring process.")
1186
1187 (defvar gnus-newsgroup-cached nil
1188   "Sorted list of articles that come from the article cache.")
1189
1190 (defvar gnus-newsgroup-saved nil
1191   "List of articles that have been saved.")
1192
1193 (defvar gnus-newsgroup-kill-headers nil)
1194
1195 (defvar gnus-newsgroup-replied nil
1196   "List of articles that have been replied to in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-forwarded nil
1199   "List of articles that have been forwarded in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-recent nil
1202   "List of articles that have are recent in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-expirable nil
1205   "Sorted list of articles in the current newsgroup that can be expired.")
1206
1207 (defvar gnus-newsgroup-processable nil
1208   "List of articles in the current newsgroup that can be processed.")
1209
1210 (defvar gnus-newsgroup-downloadable nil
1211   "Sorted list of articles in the current newsgroup that can be processed.")
1212
1213 (defvar gnus-newsgroup-undownloaded nil
1214   "List of articles in the current newsgroup that haven't been downloaded..")
1215
1216 (defvar gnus-newsgroup-unsendable nil
1217   "List of articles in the current newsgroup that won't be sent.")
1218
1219 (defvar gnus-newsgroup-bookmarks nil
1220   "List of articles in the current newsgroup that have bookmarks.")
1221
1222 (defvar gnus-newsgroup-dormant nil
1223   "Sorted list of dormant articles in the current newsgroup.")
1224
1225 (defvar gnus-newsgroup-unseen nil
1226   "List of unseen articles in the current newsgroup.")
1227
1228 (defvar gnus-newsgroup-seen nil
1229   "Range of seen articles in the current newsgroup.")
1230
1231 (defvar gnus-newsgroup-articles nil
1232   "List of articles in the current newsgroup.")
1233
1234 (defvar gnus-newsgroup-scored nil
1235   "List of scored articles in the current newsgroup.")
1236
1237 (defvar gnus-newsgroup-headers nil
1238   "List of article headers in the current newsgroup.")
1239
1240 (defvar gnus-newsgroup-threads nil)
1241
1242 (defvar gnus-newsgroup-prepared nil
1243   "Whether the current group has been prepared properly.")
1244
1245 (defvar gnus-newsgroup-ancient nil
1246   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1247
1248 (defvar gnus-newsgroup-sparse nil)
1249
1250 (defvar gnus-current-article nil)
1251 (defvar gnus-article-current nil)
1252 (defvar gnus-current-headers nil)
1253 (defvar gnus-have-all-headers nil)
1254 (defvar gnus-last-article nil)
1255 (defvar gnus-newsgroup-history nil)
1256 (defvar gnus-newsgroup-charset nil)
1257 (defvar gnus-newsgroup-ephemeral-charset nil)
1258 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1259
1260 (defvar gnus-article-before-search nil)
1261
1262 (defconst gnus-summary-local-variables
1263   '(gnus-newsgroup-name
1264     gnus-newsgroup-begin gnus-newsgroup-end
1265     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1266     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1267     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1268     gnus-newsgroup-unselected gnus-newsgroup-marked
1269     gnus-newsgroup-spam-marked
1270     gnus-newsgroup-reads gnus-newsgroup-saved
1271     gnus-newsgroup-replied gnus-newsgroup-forwarded
1272     gnus-newsgroup-recent
1273     gnus-newsgroup-expirable
1274     gnus-newsgroup-processable gnus-newsgroup-killed
1275     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1276     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1277     gnus-newsgroup-seen gnus-newsgroup-articles
1278     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1279     gnus-newsgroup-headers gnus-newsgroup-threads
1280     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1281     gnus-current-article gnus-current-headers gnus-have-all-headers
1282     gnus-last-article gnus-article-internal-prepare-hook
1283     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1284     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1285     gnus-thread-expunge-below
1286     gnus-score-alist gnus-current-score-file
1287     (gnus-summary-expunge-below . global)
1288     (gnus-summary-mark-below . global)
1289     (gnus-orphan-score . global)
1290     gnus-newsgroup-active gnus-scores-exclude-files
1291     gnus-newsgroup-history gnus-newsgroup-ancient
1292     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1293     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1294     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1295     (gnus-newsgroup-expunged-tally . 0)
1296     gnus-cache-removable-articles gnus-newsgroup-cached
1297     gnus-newsgroup-data gnus-newsgroup-data-reverse
1298     gnus-newsgroup-limit gnus-newsgroup-limits
1299     gnus-newsgroup-charset gnus-newsgroup-display)
1300   "Variables that are buffer-local to the summary buffers.")
1301
1302 (defvar gnus-newsgroup-variables nil
1303   "A list of variables that have separate values in different newsgroups.
1304 A list of newsgroup (summary buffer) local variables, or cons of
1305 variables and their default values (when the default values are not
1306 nil), that should be made global while the summary buffer is active.
1307 These variables can be used to set variables in the group parameters
1308 while still allowing them to affect operations done in other
1309 buffers. For example:
1310
1311 \(setq gnus-newsgroup-variables
1312      '(message-use-followup-to
1313        (gnus-visible-headers .
1314          \"^From:\\\\|^Newsgroups:\\\\|^Subject:\\\\|^Date:\\\\|^To:\")))
1315 ")
1316
1317 ;; Byte-compiler warning.
1318 (eval-when-compile (defvar gnus-article-mode-map))
1319
1320 ;; MIME stuff.
1321
1322 (defvar gnus-decode-encoded-word-methods
1323   '(mail-decode-encoded-word-string)
1324   "List of methods used to decode encoded words.
1325
1326 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
1327 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1328 \(REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1329 whose names match REGEXP.
1330
1331 For example:
1332 \((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1333  mail-decode-encoded-word-string
1334  (\"chinese\" . rfc1843-decode-string))")
1335
1336 (defvar gnus-decode-encoded-word-methods-cache nil)
1337
1338 (defun gnus-multi-decode-encoded-word-string (string)
1339   "Apply the functions from `gnus-encoded-word-methods' that match."
1340   (unless (and gnus-decode-encoded-word-methods-cache
1341                (eq gnus-newsgroup-name
1342                    (car gnus-decode-encoded-word-methods-cache)))
1343     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1344     (mapcar (lambda (x)
1345               (if (symbolp x)
1346                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1347                 (if (and gnus-newsgroup-name
1348                          (string-match (car x) gnus-newsgroup-name))
1349                     (nconc gnus-decode-encoded-word-methods-cache
1350                            (list (cdr x))))))
1351             gnus-decode-encoded-word-methods))
1352   (let ((xlist gnus-decode-encoded-word-methods-cache))
1353     (pop xlist)
1354     (while xlist
1355       (setq string (funcall (pop xlist) string))))
1356   string)
1357
1358 ;; Subject simplification.
1359
1360 (defun gnus-simplify-whitespace (str)
1361   "Remove excessive whitespace from STR."
1362   (let ((mystr str))
1363     ;; Multiple spaces.
1364     (while (string-match "[ \t][ \t]+" mystr)
1365       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1366                           " "
1367                           (substring mystr (match-end 0)))))
1368     ;; Leading spaces.
1369     (when (string-match "^[ \t]+" mystr)
1370       (setq mystr (substring mystr (match-end 0))))
1371     ;; Trailing spaces.
1372     (when (string-match "[ \t]+$" mystr)
1373       (setq mystr (substring mystr 0 (match-beginning 0))))
1374     mystr))
1375
1376 (defun gnus-simplify-all-whitespace (str)
1377   "Remove all whitespace from STR."
1378   (let ((mystr str))
1379     (while (string-match "[ \t\n]+" mystr)
1380       (setq mystr (replace-match "" nil nil mystr)))
1381     mystr))
1382
1383 (defsubst gnus-simplify-subject-re (subject)
1384   "Remove \"Re:\" from subject lines."
1385   (if (string-match message-subject-re-regexp subject)
1386       (substring subject (match-end 0))
1387     subject))
1388
1389 (defun gnus-simplify-subject (subject &optional re-only)
1390   "Remove `Re:' and words in parentheses.
1391 If RE-ONLY is non-nil, strip leading `Re:'s only."
1392   (let ((case-fold-search t))           ;Ignore case.
1393     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1394     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1395       (setq subject (substring subject (match-end 0))))
1396     ;; Remove uninteresting prefixes.
1397     (when (and (not re-only)
1398                gnus-simplify-ignored-prefixes
1399                (string-match gnus-simplify-ignored-prefixes subject))
1400       (setq subject (substring subject (match-end 0))))
1401     ;; Remove words in parentheses from end.
1402     (unless re-only
1403       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1404         (setq subject (substring subject 0 (match-beginning 0)))))
1405     ;; Return subject string.
1406     subject))
1407
1408 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1409 ;; all whitespace.
1410 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1411   (goto-char (point-min))
1412   (while (re-search-forward regexp nil t)
1413     (replace-match (or newtext ""))))
1414
1415 (defun gnus-simplify-buffer-fuzzy ()
1416   "Simplify string in the buffer fuzzily.
1417 The string in the accessible portion of the current buffer is simplified.
1418 It is assumed to be a single-line subject.
1419 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1420 matter is removed.  Additional things can be deleted by setting
1421 `gnus-simplify-subject-fuzzy-regexp'."
1422   (let ((case-fold-search t)
1423         (modified-tick))
1424     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1425
1426     (while (not (eq modified-tick (buffer-modified-tick)))
1427       (setq modified-tick (buffer-modified-tick))
1428       (cond
1429        ((listp gnus-simplify-subject-fuzzy-regexp)
1430         (mapcar 'gnus-simplify-buffer-fuzzy-step
1431                 gnus-simplify-subject-fuzzy-regexp))
1432        (gnus-simplify-subject-fuzzy-regexp
1433         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1434       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1435       (gnus-simplify-buffer-fuzzy-step
1436        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1437       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1438
1439     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1440     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1441     (gnus-simplify-buffer-fuzzy-step " $")
1442     (gnus-simplify-buffer-fuzzy-step "^ +")))
1443
1444 (defun gnus-simplify-subject-fuzzy (subject)
1445   "Simplify a subject string fuzzily.
1446 See `gnus-simplify-buffer-fuzzy' for details."
1447   (save-excursion
1448     (gnus-set-work-buffer)
1449     (let ((case-fold-search t))
1450       ;; Remove uninteresting prefixes.
1451       (when (and gnus-simplify-ignored-prefixes
1452                  (string-match gnus-simplify-ignored-prefixes subject))
1453         (setq subject (substring subject (match-end 0))))
1454       (insert subject)
1455       (inline (gnus-simplify-buffer-fuzzy))
1456       (buffer-string))))
1457
1458 (defsubst gnus-simplify-subject-fully (subject)
1459   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1460   (cond
1461    (gnus-simplify-subject-functions
1462     (gnus-map-function gnus-simplify-subject-functions subject))
1463    ((null gnus-summary-gather-subject-limit)
1464     (gnus-simplify-subject-re subject))
1465    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1466     (gnus-simplify-subject-fuzzy subject))
1467    ((numberp gnus-summary-gather-subject-limit)
1468     (gnus-limit-string (gnus-simplify-subject-re subject)
1469                        gnus-summary-gather-subject-limit))
1470    (t
1471     subject)))
1472
1473 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1474   "Check whether two subjects are equal.
1475 If optional argument simple-first is t, first argument is already
1476 simplified."
1477   (cond
1478    ((null simple-first)
1479     (equal (gnus-simplify-subject-fully s1)
1480            (gnus-simplify-subject-fully s2)))
1481    (t
1482     (equal s1
1483            (gnus-simplify-subject-fully s2)))))
1484
1485 (defun gnus-summary-bubble-group ()
1486   "Increase the score of the current group.
1487 This is a handy function to add to `gnus-summary-exit-hook' to
1488 increase the score of each group you read."
1489   (gnus-group-add-score gnus-newsgroup-name))
1490
1491 \f
1492 ;;;
1493 ;;; Gnus summary mode
1494 ;;;
1495
1496 (put 'gnus-summary-mode 'mode-class 'special)
1497
1498 (defvar gnus-article-commands-menu)
1499
1500 (when t
1501   ;; Non-orthogonal keys
1502
1503   (gnus-define-keys gnus-summary-mode-map
1504     " " gnus-summary-next-page
1505     "\177" gnus-summary-prev-page
1506     [delete] gnus-summary-prev-page
1507     [backspace] gnus-summary-prev-page
1508     "\r" gnus-summary-scroll-up
1509     "\M-\r" gnus-summary-scroll-down
1510     "n" gnus-summary-next-unread-article
1511     "p" gnus-summary-prev-unread-article
1512     "N" gnus-summary-next-article
1513     "P" gnus-summary-prev-article
1514     "\M-\C-n" gnus-summary-next-same-subject
1515     "\M-\C-p" gnus-summary-prev-same-subject
1516     "\M-n" gnus-summary-next-unread-subject
1517     "\M-p" gnus-summary-prev-unread-subject
1518     "." gnus-summary-first-unread-article
1519     "," gnus-summary-best-unread-article
1520     "\M-s" gnus-summary-search-article-forward
1521     "\M-r" gnus-summary-search-article-backward
1522     "<" gnus-summary-beginning-of-article
1523     ">" gnus-summary-end-of-article
1524     "j" gnus-summary-goto-article
1525     "^" gnus-summary-refer-parent-article
1526     "\M-^" gnus-summary-refer-article
1527     "u" gnus-summary-tick-article-forward
1528     "!" gnus-summary-tick-article-forward
1529     "U" gnus-summary-tick-article-backward
1530     "d" gnus-summary-mark-as-read-forward
1531     "D" gnus-summary-mark-as-read-backward
1532     "E" gnus-summary-mark-as-expirable
1533     "\M-u" gnus-summary-clear-mark-forward
1534     "\M-U" gnus-summary-clear-mark-backward
1535     "k" gnus-summary-kill-same-subject-and-select
1536     "\C-k" gnus-summary-kill-same-subject
1537     "\M-\C-k" gnus-summary-kill-thread
1538     "\M-\C-l" gnus-summary-lower-thread
1539     "e" gnus-summary-edit-article
1540     "#" gnus-summary-mark-as-processable
1541     "\M-#" gnus-summary-unmark-as-processable
1542     "\M-\C-t" gnus-summary-toggle-threads
1543     "\M-\C-s" gnus-summary-show-thread
1544     "\M-\C-h" gnus-summary-hide-thread
1545     "\M-\C-f" gnus-summary-next-thread
1546     "\M-\C-b" gnus-summary-prev-thread
1547     [(meta down)] gnus-summary-next-thread
1548     [(meta up)] gnus-summary-prev-thread
1549     "\M-\C-u" gnus-summary-up-thread
1550     "\M-\C-d" gnus-summary-down-thread
1551     "&" gnus-summary-execute-command
1552     "c" gnus-summary-catchup-and-exit
1553     "\C-w" gnus-summary-mark-region-as-read
1554     "\C-t" gnus-summary-toggle-truncation
1555     "?" gnus-summary-mark-as-dormant
1556     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1557     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1558     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1559     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1560     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1561     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1562     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1563     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1564     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1565     "\C-c\C-s\C-r" gnus-summary-sort-by-random
1566     "=" gnus-summary-expand-window
1567     "\C-x\C-s" gnus-summary-reselect-current-group
1568     "\M-g" gnus-summary-rescan-group
1569     "w" gnus-summary-stop-page-breaking
1570     "\C-c\C-r" gnus-summary-caesar-message
1571     "f" gnus-summary-followup
1572     "F" gnus-summary-followup-with-original
1573     "C" gnus-summary-cancel-article
1574     "r" gnus-summary-reply
1575     "R" gnus-summary-reply-with-original
1576     "\C-c\C-f" gnus-summary-mail-forward
1577     "o" gnus-summary-save-article
1578     "\C-o" gnus-summary-save-article-mail
1579     "|" gnus-summary-pipe-output
1580     "\M-k" gnus-summary-edit-local-kill
1581     "\M-K" gnus-summary-edit-global-kill
1582     ;; "V" gnus-version
1583     "\C-c\C-d" gnus-summary-describe-group
1584     "q" gnus-summary-exit
1585     "Q" gnus-summary-exit-no-update
1586     "\C-c\C-i" gnus-info-find-node
1587     gnus-mouse-2 gnus-mouse-pick-article
1588     "m" gnus-summary-mail-other-window
1589     "a" gnus-summary-post-news
1590     "i" gnus-summary-news-other-window
1591     "x" gnus-summary-limit-to-unread
1592     "s" gnus-summary-isearch-article
1593     "t" gnus-summary-toggle-header
1594     "g" gnus-summary-show-article
1595     "l" gnus-summary-goto-last-article
1596     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1597     "\C-d" gnus-summary-enter-digest-group
1598     "\M-\C-d" gnus-summary-read-document
1599     "\M-\C-e" gnus-summary-edit-parameters
1600     "\M-\C-a" gnus-summary-customize-parameters
1601     "\C-c\C-b" gnus-bug
1602     "*" gnus-cache-enter-article
1603     "\M-*" gnus-cache-remove-article
1604     "\M-&" gnus-summary-universal-argument
1605     "\C-l" gnus-recenter
1606     "I" gnus-summary-increase-score
1607     "L" gnus-summary-lower-score
1608     "\M-i" gnus-symbolic-argument
1609     "h" gnus-summary-select-article-buffer
1610
1611     "b" gnus-article-view-part
1612     "\M-t" gnus-summary-toggle-display-buttonized
1613
1614     "V" gnus-summary-score-map
1615     "X" gnus-uu-extract-map
1616     "S" gnus-summary-send-map)
1617
1618   ;; Sort of orthogonal keymap
1619   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1620     "t" gnus-summary-tick-article-forward
1621     "!" gnus-summary-tick-article-forward
1622     "d" gnus-summary-mark-as-read-forward
1623     "r" gnus-summary-mark-as-read-forward
1624     "c" gnus-summary-clear-mark-forward
1625     " " gnus-summary-clear-mark-forward
1626     "e" gnus-summary-mark-as-expirable
1627     "x" gnus-summary-mark-as-expirable
1628     "?" gnus-summary-mark-as-dormant
1629     "b" gnus-summary-set-bookmark
1630     "B" gnus-summary-remove-bookmark
1631     "#" gnus-summary-mark-as-processable
1632     "\M-#" gnus-summary-unmark-as-processable
1633     "S" gnus-summary-limit-include-expunged
1634     "C" gnus-summary-catchup
1635     "H" gnus-summary-catchup-to-here
1636     "h" gnus-summary-catchup-from-here
1637     "\C-c" gnus-summary-catchup-all
1638     "k" gnus-summary-kill-same-subject-and-select
1639     "K" gnus-summary-kill-same-subject
1640     "P" gnus-uu-mark-map)
1641
1642   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1643     "c" gnus-summary-clear-above
1644     "u" gnus-summary-tick-above
1645     "m" gnus-summary-mark-above
1646     "k" gnus-summary-kill-below)
1647
1648   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1649     "/" gnus-summary-limit-to-subject
1650     "n" gnus-summary-limit-to-articles
1651     "w" gnus-summary-pop-limit
1652     "s" gnus-summary-limit-to-subject
1653     "a" gnus-summary-limit-to-author
1654     "u" gnus-summary-limit-to-unread
1655     "m" gnus-summary-limit-to-marks
1656     "M" gnus-summary-limit-exclude-marks
1657     "v" gnus-summary-limit-to-score
1658     "*" gnus-summary-limit-include-cached
1659     "D" gnus-summary-limit-include-dormant
1660     "T" gnus-summary-limit-include-thread
1661     "d" gnus-summary-limit-exclude-dormant
1662     "t" gnus-summary-limit-to-age
1663     "." gnus-summary-limit-to-unseen
1664     "x" gnus-summary-limit-to-extra
1665     "p" gnus-summary-limit-to-display-predicate
1666     "E" gnus-summary-limit-include-expunged
1667     "c" gnus-summary-limit-exclude-childless-dormant
1668     "C" gnus-summary-limit-mark-excluded-as-read
1669     "o" gnus-summary-insert-old-articles
1670     "N" gnus-summary-insert-new-articles)
1671
1672   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1673     "n" gnus-summary-next-unread-article
1674     "p" gnus-summary-prev-unread-article
1675     "N" gnus-summary-next-article
1676     "P" gnus-summary-prev-article
1677     "\C-n" gnus-summary-next-same-subject
1678     "\C-p" gnus-summary-prev-same-subject
1679     "\M-n" gnus-summary-next-unread-subject
1680     "\M-p" gnus-summary-prev-unread-subject
1681     "f" gnus-summary-first-unread-article
1682     "b" gnus-summary-best-unread-article
1683     "j" gnus-summary-goto-article
1684     "g" gnus-summary-goto-subject
1685     "l" gnus-summary-goto-last-article
1686     "o" gnus-summary-pop-article)
1687
1688   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1689     "k" gnus-summary-kill-thread
1690     "l" gnus-summary-lower-thread
1691     "i" gnus-summary-raise-thread
1692     "T" gnus-summary-toggle-threads
1693     "t" gnus-summary-rethread-current
1694     "^" gnus-summary-reparent-thread
1695     "s" gnus-summary-show-thread
1696     "S" gnus-summary-show-all-threads
1697     "h" gnus-summary-hide-thread
1698     "H" gnus-summary-hide-all-threads
1699     "n" gnus-summary-next-thread
1700     "p" gnus-summary-prev-thread
1701     "u" gnus-summary-up-thread
1702     "o" gnus-summary-top-thread
1703     "d" gnus-summary-down-thread
1704     "#" gnus-uu-mark-thread
1705     "\M-#" gnus-uu-unmark-thread)
1706
1707   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1708     "g" gnus-summary-prepare
1709     "c" gnus-summary-insert-cached-articles)
1710
1711   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1712     "c" gnus-summary-catchup-and-exit
1713     "C" gnus-summary-catchup-all-and-exit
1714     "E" gnus-summary-exit-no-update
1715     "Q" gnus-summary-exit
1716     "Z" gnus-summary-exit
1717     "n" gnus-summary-catchup-and-goto-next-group
1718     "R" gnus-summary-reselect-current-group
1719     "G" gnus-summary-rescan-group
1720     "N" gnus-summary-next-group
1721     "s" gnus-summary-save-newsrc
1722     "P" gnus-summary-prev-group)
1723
1724   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1725     " " gnus-summary-next-page
1726     "n" gnus-summary-next-page
1727     "\177" gnus-summary-prev-page
1728     [delete] gnus-summary-prev-page
1729     "p" gnus-summary-prev-page
1730     "\r" gnus-summary-scroll-up
1731     "\M-\r" gnus-summary-scroll-down
1732     "<" gnus-summary-beginning-of-article
1733     ">" gnus-summary-end-of-article
1734     "b" gnus-summary-beginning-of-article
1735     "e" gnus-summary-end-of-article
1736     "^" gnus-summary-refer-parent-article
1737     "r" gnus-summary-refer-parent-article
1738     "D" gnus-summary-enter-digest-group
1739     "R" gnus-summary-refer-references
1740     "T" gnus-summary-refer-thread
1741     "g" gnus-summary-show-article
1742     "s" gnus-summary-isearch-article
1743     "P" gnus-summary-print-article
1744     "M" gnus-mailing-list-insinuate
1745     "t" gnus-article-babel)
1746
1747   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1748     "b" gnus-article-add-buttons
1749     "B" gnus-article-add-buttons-to-head
1750     "o" gnus-article-treat-overstrike
1751     "e" gnus-article-emphasize
1752     "w" gnus-article-fill-cited-article
1753     "Q" gnus-article-fill-long-lines
1754     "C" gnus-article-capitalize-sentences
1755     "c" gnus-article-remove-cr
1756     "q" gnus-article-de-quoted-unreadable
1757     "6" gnus-article-de-base64-unreadable
1758     "Z" gnus-article-decode-HZ
1759     "h" gnus-article-wash-html
1760     "u" gnus-article-unsplit-urls
1761     "s" gnus-summary-force-verify-and-decrypt
1762     "f" gnus-article-display-x-face
1763     "l" gnus-summary-stop-page-breaking
1764     "r" gnus-summary-caesar-message
1765     "m" gnus-summary-morse-message
1766     "t" gnus-summary-toggle-header
1767     "g" gnus-treat-smiley
1768     "v" gnus-summary-verbose-headers
1769     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1770     "p" gnus-article-verify-x-pgp-sig
1771     "d" gnus-article-treat-dumbquotes
1772     "k" gnus-article-outlook-deuglify-article)
1773
1774   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1775     "a" gnus-article-hide
1776     "h" gnus-article-hide-headers
1777     "b" gnus-article-hide-boring-headers
1778     "s" gnus-article-hide-signature
1779     "c" gnus-article-hide-citation
1780     "C" gnus-article-hide-citation-in-followups
1781     "l" gnus-article-hide-list-identifiers
1782     "p" gnus-article-hide-pgp
1783     "B" gnus-article-strip-banner
1784     "P" gnus-article-hide-pem
1785     "\C-c" gnus-article-hide-citation-maybe)
1786
1787   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1788     "a" gnus-article-highlight
1789     "h" gnus-article-highlight-headers
1790     "c" gnus-article-highlight-citation
1791     "s" gnus-article-highlight-signature)
1792
1793   (gnus-define-keys (gnus-summary-wash-header-map "G" gnus-summary-wash-map)
1794     "f" gnus-article-treat-fold-headers
1795     "u" gnus-article-treat-unfold-headers
1796     "n" gnus-article-treat-fold-newsgroups)
1797
1798   (gnus-define-keys (gnus-summary-wash-display-map "D" gnus-summary-wash-map)
1799     "x" gnus-article-display-x-face
1800     "s" gnus-treat-smiley
1801     "D" gnus-article-remove-images
1802     "f" gnus-treat-from-picon
1803     "m" gnus-treat-mail-picon
1804     "n" gnus-treat-newsgroups-picon)
1805
1806   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1807     "w" gnus-article-decode-mime-words
1808     "c" gnus-article-decode-charset
1809     "v" gnus-mime-view-all-parts
1810     "b" gnus-article-view-part)
1811
1812   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1813     "z" gnus-article-date-ut
1814     "u" gnus-article-date-ut
1815     "l" gnus-article-date-local
1816     "p" gnus-article-date-english
1817     "e" gnus-article-date-lapsed
1818     "o" gnus-article-date-original
1819     "i" gnus-article-date-iso8601
1820     "s" gnus-article-date-user)
1821
1822   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1823     "t" gnus-article-remove-trailing-blank-lines
1824     "l" gnus-article-strip-leading-blank-lines
1825     "m" gnus-article-strip-multiple-blank-lines
1826     "a" gnus-article-strip-blank-lines
1827     "A" gnus-article-strip-all-blank-lines
1828     "s" gnus-article-strip-leading-space
1829     "e" gnus-article-strip-trailing-space
1830     "w" gnus-article-remove-leading-whitespace)
1831
1832   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1833     "v" gnus-version
1834     "f" gnus-summary-fetch-faq
1835     "d" gnus-summary-describe-group
1836     "h" gnus-summary-describe-briefly
1837     "i" gnus-info-find-node
1838     "c" gnus-group-fetch-charter
1839     "C" gnus-group-fetch-control)
1840
1841   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1842     "e" gnus-summary-expire-articles
1843     "\M-\C-e" gnus-summary-expire-articles-now
1844     "\177" gnus-summary-delete-article
1845     [delete] gnus-summary-delete-article
1846     [backspace] gnus-summary-delete-article
1847     "m" gnus-summary-move-article
1848     "r" gnus-summary-respool-article
1849     "w" gnus-summary-edit-article
1850     "c" gnus-summary-copy-article
1851     "B" gnus-summary-crosspost-article
1852     "q" gnus-summary-respool-query
1853     "t" gnus-summary-respool-trace
1854     "i" gnus-summary-import-article
1855     "I" gnus-summary-create-article
1856     "p" gnus-summary-article-posted-p)
1857
1858   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1859     "o" gnus-summary-save-article
1860     "m" gnus-summary-save-article-mail
1861     "F" gnus-summary-write-article-file
1862     "r" gnus-summary-save-article-rmail
1863     "f" gnus-summary-save-article-file
1864     "b" gnus-summary-save-article-body-file
1865     "h" gnus-summary-save-article-folder
1866     "v" gnus-summary-save-article-vm
1867     "p" gnus-summary-pipe-output
1868     "P" gnus-summary-muttprint
1869     "s" gnus-soup-add-article)
1870
1871   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1872     "b" gnus-summary-display-buttonized
1873     "m" gnus-summary-repair-multipart
1874     "v" gnus-article-view-part
1875     "o" gnus-article-save-part
1876     "c" gnus-article-copy-part
1877     "C" gnus-article-view-part-as-charset
1878     "e" gnus-article-view-part-externally
1879     "E" gnus-article-encrypt-body
1880     "i" gnus-article-inline-part
1881     "|" gnus-article-pipe-part)
1882
1883   (gnus-define-keys (gnus-uu-mark-map "P" gnus-summary-mark-map)
1884     "p" gnus-summary-mark-as-processable
1885     "u" gnus-summary-unmark-as-processable
1886     "U" gnus-summary-unmark-all-processable
1887     "v" gnus-uu-mark-over
1888     "s" gnus-uu-mark-series
1889     "r" gnus-uu-mark-region
1890     "g" gnus-uu-unmark-region
1891     "R" gnus-uu-mark-by-regexp
1892     "G" gnus-uu-unmark-by-regexp
1893     "t" gnus-uu-mark-thread
1894     "T" gnus-uu-unmark-thread
1895     "a" gnus-uu-mark-all
1896     "b" gnus-uu-mark-buffer
1897     "S" gnus-uu-mark-sparse
1898     "k" gnus-summary-kill-process-mark
1899     "y" gnus-summary-yank-process-mark
1900     "w" gnus-summary-save-process-mark
1901     "i" gnus-uu-invert-processable)
1902
1903   (gnus-define-keys (gnus-uu-extract-map "X" gnus-summary-mode-map)
1904     ;;"x" gnus-uu-extract-any
1905     "m" gnus-summary-save-parts
1906     "u" gnus-uu-decode-uu
1907     "U" gnus-uu-decode-uu-and-save
1908     "s" gnus-uu-decode-unshar
1909     "S" gnus-uu-decode-unshar-and-save
1910     "o" gnus-uu-decode-save
1911     "O" gnus-uu-decode-save
1912     "b" gnus-uu-decode-binhex
1913     "B" gnus-uu-decode-binhex
1914     "p" gnus-uu-decode-postscript
1915     "P" gnus-uu-decode-postscript-and-save)
1916
1917   (gnus-define-keys
1918       (gnus-uu-extract-view-map "v" gnus-uu-extract-map)
1919     "u" gnus-uu-decode-uu-view
1920     "U" gnus-uu-decode-uu-and-save-view
1921     "s" gnus-uu-decode-unshar-view
1922     "S" gnus-uu-decode-unshar-and-save-view
1923     "o" gnus-uu-decode-save-view
1924     "O" gnus-uu-decode-save-view
1925     "b" gnus-uu-decode-binhex-view
1926     "B" gnus-uu-decode-binhex-view
1927     "p" gnus-uu-decode-postscript-view
1928     "P" gnus-uu-decode-postscript-and-save-view))
1929
1930 (defvar gnus-article-post-menu nil)
1931
1932 (defconst gnus-summary-menu-maxlen 20)
1933
1934 (defun gnus-summary-menu-split (menu)
1935   ;; If we have lots of elements, divide them into groups of 20
1936   ;; and make a pane (or submenu) for each one.
1937   (if (> (length menu) (/ (* gnus-summary-menu-maxlen 3) 2))
1938       (let ((menu menu) sublists next
1939             (i 1))
1940         (while menu
1941           ;; Pull off the next gnus-summary-menu-maxlen elements
1942           ;; and make them the next element of sublist.
1943           (setq next (nthcdr gnus-summary-menu-maxlen menu))
1944           (if next
1945               (setcdr (nthcdr (1- gnus-summary-menu-maxlen) menu)
1946                       nil))
1947           (setq sublists (cons (cons (format "%s ... %s" (aref (car menu) 0)
1948                                              (aref (car (last menu)) 0)) menu)
1949                                sublists))
1950           (setq i (1+ i))
1951           (setq menu next))
1952         (nreverse sublists))
1953     ;; Few elements--put them all in one pane.
1954     menu))
1955
1956 (defun gnus-summary-make-menu-bar ()
1957   (gnus-turn-off-edit-menu 'summary)
1958
1959   (unless (boundp 'gnus-summary-misc-menu)
1960
1961     (easy-menu-define
1962       gnus-summary-kill-menu gnus-summary-mode-map ""
1963       (cons
1964        "Score"
1965        (nconc
1966         (list
1967          ["Customize" gnus-score-customize t])
1968         (gnus-make-score-map 'increase)
1969         (gnus-make-score-map 'lower)
1970         '(("Mark"
1971            ["Kill below" gnus-summary-kill-below t]
1972            ["Mark above" gnus-summary-mark-above t]
1973            ["Tick above" gnus-summary-tick-above t]
1974            ["Clear above" gnus-summary-clear-above t])
1975           ["Current score" gnus-summary-current-score t]
1976           ["Set score" gnus-summary-set-score t]
1977           ["Switch current score file..." gnus-score-change-score-file t]
1978           ["Set mark below..." gnus-score-set-mark-below t]
1979           ["Set expunge below..." gnus-score-set-expunge-below t]
1980           ["Edit current score file" gnus-score-edit-current-scores t]
1981           ["Edit score file" gnus-score-edit-file t]
1982           ["Trace score" gnus-score-find-trace t]
1983           ["Find words" gnus-score-find-favourite-words t]
1984           ["Rescore buffer" gnus-summary-rescore t]
1985           ["Increase score..." gnus-summary-increase-score t]
1986           ["Lower score..." gnus-summary-lower-score t]))))
1987
1988     ;; Define both the Article menu in the summary buffer and the
1989     ;; equivalent Commands menu in the article buffer here for
1990     ;; consistency.
1991     (let ((innards
1992            `(("Hide"
1993               ["All" gnus-article-hide t]
1994               ["Headers" gnus-article-hide-headers t]
1995               ["Signature" gnus-article-hide-signature t]
1996               ["Citation" gnus-article-hide-citation t]
1997               ["List identifiers" gnus-article-hide-list-identifiers t]
1998               ["PGP" gnus-article-hide-pgp t]
1999               ["Banner" gnus-article-strip-banner t]
2000               ["Boring headers" gnus-article-hide-boring-headers t])
2001              ("Highlight"
2002               ["All" gnus-article-highlight t]
2003               ["Headers" gnus-article-highlight-headers t]
2004               ["Signature" gnus-article-highlight-signature t]
2005               ["Citation" gnus-article-highlight-citation t])
2006              ("MIME"
2007               ["Words" gnus-article-decode-mime-words t]
2008               ["Charset" gnus-article-decode-charset t]
2009               ["QP" gnus-article-de-quoted-unreadable t]
2010               ["Base64" gnus-article-de-base64-unreadable t]
2011               ["View MIME buttons" gnus-summary-display-buttonized t]
2012               ["View all" gnus-mime-view-all-parts t]
2013               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
2014               ["Encrypt body" gnus-article-encrypt-body t]
2015               ["Extract all parts" gnus-summary-save-parts t])
2016              ("Date"
2017               ["Local" gnus-article-date-local t]
2018               ["ISO8601" gnus-article-date-iso8601 t]
2019               ["UT" gnus-article-date-ut t]
2020               ["Original" gnus-article-date-original t]
2021               ["Lapsed" gnus-article-date-lapsed t]
2022               ["User-defined" gnus-article-date-user t])
2023              ("Display"
2024               ["Remove images" gnus-article-remove-images t]
2025               ["Toggle smiley" gnus-treat-smiley t]
2026               ["Show X-Face" gnus-article-display-x-face t]
2027               ["Show picons in From" gnus-treat-from-picon t]
2028               ["Show picons in mail headers" gnus-treat-mail-picon t]
2029               ["Show picons in news headers" gnus-treat-newsgroups-picon t]
2030               ("View as different encoding"
2031                ,@(gnus-summary-menu-split
2032                   (mapcar
2033                    (lambda (cs)
2034                      ;; Since easymenu under FSF Emacs doesn't allow lambda
2035                      ;; forms for menu commands, we should provide intern'ed
2036                      ;; function symbols.
2037                      (let ((command (intern (format "\
2038 gnus-summary-show-article-from-menu-as-charset-%s" cs))))
2039                        (fset command
2040                              `(lambda ()
2041                                 (interactive)
2042                                 (let ((gnus-summary-show-article-charset-alist
2043                                        '((1 . ,cs))))
2044                                   (gnus-summary-show-article 1))))
2045                        `[,(symbol-name cs) ,command t]))
2046                    (sort (if (fboundp 'coding-system-list)
2047                              (coding-system-list)
2048                            (mapcar 'car mm-mime-mule-charset-alist))
2049                          'string<)))))
2050              ("Washing"
2051               ("Remove Blanks"
2052                ["Leading" gnus-article-strip-leading-blank-lines t]
2053                ["Multiple" gnus-article-strip-multiple-blank-lines t]
2054                ["Trailing" gnus-article-remove-trailing-blank-lines t]
2055                ["All of the above" gnus-article-strip-blank-lines t]
2056                ["All" gnus-article-strip-all-blank-lines t]
2057                ["Leading space" gnus-article-strip-leading-space t]
2058                ["Trailing space" gnus-article-strip-trailing-space t]
2059                ["Leading space in headers"
2060                 gnus-article-remove-leading-whitespace t])
2061               ["Overstrike" gnus-article-treat-overstrike t]
2062               ["Dumb quotes" gnus-article-treat-dumbquotes t]
2063               ["Emphasis" gnus-article-emphasize t]
2064               ["Word wrap" gnus-article-fill-cited-article t]
2065               ["Fill long lines" gnus-article-fill-long-lines t]
2066               ["Capitalize sentences" gnus-article-capitalize-sentences t]
2067               ["CR" gnus-article-remove-cr t]
2068               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
2069               ["Base64" gnus-article-de-base64-unreadable t]
2070               ["Rot 13" gnus-summary-caesar-message
2071                ,@(if (featurep 'xemacs) '(t)
2072                    '(:help "\"Caesar rotate\" article by 13"))]
2073               ["Morse decode" gnus-summary-morse-message t]
2074               ["Unix pipe..." gnus-summary-pipe-message t]
2075               ["Add buttons" gnus-article-add-buttons t]
2076               ["Add buttons to head" gnus-article-add-buttons-to-head t]
2077               ["Stop page breaking" gnus-summary-stop-page-breaking t]
2078               ["Verbose header" gnus-summary-verbose-headers t]
2079               ["Toggle header" gnus-summary-toggle-header t]
2080               ["Unfold headers" gnus-article-treat-unfold-headers t]
2081               ["Fold newsgroups" gnus-article-treat-fold-newsgroups t]
2082               ["Html" gnus-article-wash-html t]
2083               ["URLs" gnus-article-unsplit-urls t]
2084               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
2085               ["HZ" gnus-article-decode-HZ t]
2086               ["OutlooK deuglify" gnus-article-outlook-deuglify-article t]
2087               )
2088              ("Output"
2089               ["Save in default format" gnus-summary-save-article
2090                ,@(if (featurep 'xemacs) '(t)
2091                    '(:help "Save article using default method"))]
2092               ["Save in file" gnus-summary-save-article-file
2093                ,@(if (featurep 'xemacs) '(t)
2094                    '(:help "Save article in file"))]
2095               ["Save in Unix mail format" gnus-summary-save-article-mail t]
2096               ["Save in MH folder" gnus-summary-save-article-folder t]
2097               ["Save in VM folder" gnus-summary-save-article-vm t]
2098               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
2099               ["Save body in file" gnus-summary-save-article-body-file t]
2100               ["Pipe through a filter" gnus-summary-pipe-output t]
2101               ["Add to SOUP packet" gnus-soup-add-article t]
2102               ["Print with Muttprint" gnus-summary-muttprint t]
2103               ["Print" gnus-summary-print-article t])
2104              ("Backend"
2105               ["Respool article..." gnus-summary-respool-article t]
2106               ["Move article..." gnus-summary-move-article
2107                (gnus-check-backend-function
2108                 'request-move-article gnus-newsgroup-name)]
2109               ["Copy article..." gnus-summary-copy-article t]
2110               ["Crosspost article..." gnus-summary-crosspost-article
2111                (gnus-check-backend-function
2112                 'request-replace-article gnus-newsgroup-name)]
2113               ["Import file..." gnus-summary-import-article t]
2114               ["Create article..." gnus-summary-create-article t]
2115               ["Check if posted" gnus-summary-article-posted-p t]
2116               ["Edit article" gnus-summary-edit-article
2117                (not (gnus-group-read-only-p))]
2118               ["Delete article" gnus-summary-delete-article
2119                (gnus-check-backend-function
2120                 'request-expire-articles gnus-newsgroup-name)]
2121               ["Query respool" gnus-summary-respool-query t]
2122               ["Trace respool" gnus-summary-respool-trace t]
2123               ["Delete expirable articles" gnus-summary-expire-articles-now
2124                (gnus-check-backend-function
2125                 'request-expire-articles gnus-newsgroup-name)])
2126              ("Extract"
2127               ["Uudecode" gnus-uu-decode-uu
2128                ,@(if (featurep 'xemacs) '(t)
2129                    '(:help "Decode uuencoded article(s)"))]
2130               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
2131               ["Unshar" gnus-uu-decode-unshar t]
2132               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
2133               ["Save" gnus-uu-decode-save t]
2134               ["Binhex" gnus-uu-decode-binhex t]
2135               ["Postscript" gnus-uu-decode-postscript t]
2136               ["All MIME parts" gnus-summary-save-parts t])
2137              ("Cache"
2138               ["Enter article" gnus-cache-enter-article t]
2139               ["Remove article" gnus-cache-remove-article t])
2140              ["Translate" gnus-article-babel t]
2141              ["Select article buffer" gnus-summary-select-article-buffer t]
2142              ["Enter digest buffer" gnus-summary-enter-digest-group t]
2143              ["Isearch article..." gnus-summary-isearch-article t]
2144              ["Beginning of the article" gnus-summary-beginning-of-article t]
2145              ["End of the article" gnus-summary-end-of-article t]
2146              ["Fetch parent of article" gnus-summary-refer-parent-article t]
2147              ["Fetch referenced articles" gnus-summary-refer-references t]
2148              ["Fetch current thread" gnus-summary-refer-thread t]
2149              ["Fetch article with id..." gnus-summary-refer-article t]
2150              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
2151              ["Redisplay" gnus-summary-show-article t]
2152              ["Raw article" gnus-summary-show-raw-article :keys "C-u g"])))
2153       (easy-menu-define
2154         gnus-summary-article-menu gnus-summary-mode-map ""
2155         (cons "Article" innards))
2156
2157       (if (not (keymapp gnus-summary-article-menu))
2158           (easy-menu-define
2159             gnus-article-commands-menu gnus-article-mode-map ""
2160             (cons "Commands" innards))
2161         ;; in Emacs, don't share menu.
2162         (setq gnus-article-commands-menu
2163               (copy-keymap gnus-summary-article-menu))
2164         (define-key gnus-article-mode-map [menu-bar commands]
2165           (cons "Commands" gnus-article-commands-menu))))
2166
2167     (easy-menu-define
2168       gnus-summary-thread-menu gnus-summary-mode-map ""
2169       '("Threads"
2170         ["Toggle threading" gnus-summary-toggle-threads t]
2171         ["Hide threads" gnus-summary-hide-all-threads t]
2172         ["Show threads" gnus-summary-show-all-threads t]
2173         ["Hide thread" gnus-summary-hide-thread t]
2174         ["Show thread" gnus-summary-show-thread t]
2175         ["Go to next thread" gnus-summary-next-thread t]
2176         ["Go to previous thread" gnus-summary-prev-thread t]
2177         ["Go down thread" gnus-summary-down-thread t]
2178         ["Go up thread" gnus-summary-up-thread t]
2179         ["Top of thread" gnus-summary-top-thread t]
2180         ["Mark thread as read" gnus-summary-kill-thread t]
2181         ["Lower thread score" gnus-summary-lower-thread t]
2182         ["Raise thread score" gnus-summary-raise-thread t]
2183         ["Rethread current" gnus-summary-rethread-current t]))
2184
2185     (easy-menu-define
2186       gnus-summary-post-menu gnus-summary-mode-map ""
2187       `("Post"
2188         ["Send a message (mail or news)" gnus-summary-post-news
2189          ,@(if (featurep 'xemacs) '(t)
2190              '(:help "Post an article"))]
2191         ["Followup" gnus-summary-followup
2192          ,@(if (featurep 'xemacs) '(t)
2193              '(:help "Post followup to this article"))]
2194         ["Followup and yank" gnus-summary-followup-with-original
2195          ,@(if (featurep 'xemacs) '(t)
2196              '(:help "Post followup to this article, quoting its contents"))]
2197         ["Supersede article" gnus-summary-supersede-article t]
2198         ["Cancel article" gnus-summary-cancel-article
2199          ,@(if (featurep 'xemacs) '(t)
2200              '(:help "Cancel an article you posted"))]
2201         ["Reply" gnus-summary-reply t]
2202         ["Reply and yank" gnus-summary-reply-with-original t]
2203         ["Wide reply" gnus-summary-wide-reply t]
2204         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2205          ,@(if (featurep 'xemacs) '(t)
2206              '(:help "Mail a reply, quoting this article"))]
2207         ["Very wide reply" gnus-summary-very-wide-reply t]
2208         ["Very wide reply and yank" gnus-summary-very-wide-reply-with-original
2209          ,@(if (featurep 'xemacs) '(t)
2210              '(:help "Mail a very wide reply, quoting this article"))]
2211         ["Mail forward" gnus-summary-mail-forward t]
2212         ["Post forward" gnus-summary-post-forward t]
2213         ["Digest and mail" gnus-uu-digest-mail-forward t]
2214         ["Digest and post" gnus-uu-digest-post-forward t]
2215         ["Resend message" gnus-summary-resend-message t]
2216         ["Resend message edit" gnus-summary-resend-message-edit t]
2217         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2218         ["Send a mail" gnus-summary-mail-other-window t]
2219         ["Create a local message" gnus-summary-news-other-window t]
2220         ["Uuencode and post" gnus-uu-post-news
2221          ,@(if (featurep 'xemacs) '(t)
2222              '(:help "Post a uuencoded article"))]
2223         ["Followup via news" gnus-summary-followup-to-mail t]
2224         ["Followup via news and yank"
2225          gnus-summary-followup-to-mail-with-original t]
2226         ;;("Draft"
2227         ;;["Send" gnus-summary-send-draft t]
2228         ;;["Send bounced" gnus-resend-bounced-mail t])
2229         ))
2230
2231     (cond
2232      ((not (keymapp gnus-summary-post-menu))
2233       (setq gnus-article-post-menu gnus-summary-post-menu))
2234      ((not gnus-article-post-menu)
2235       ;; Don't share post menu.
2236       (setq gnus-article-post-menu
2237             (copy-keymap gnus-summary-post-menu))))
2238     (define-key gnus-article-mode-map [menu-bar post]
2239       (cons "Post" gnus-article-post-menu))
2240
2241     (easy-menu-define
2242       gnus-summary-misc-menu gnus-summary-mode-map ""
2243       `("Gnus"
2244         ("Mark Read"
2245          ["Mark as read" gnus-summary-mark-as-read-forward t]
2246          ["Mark same subject and select"
2247           gnus-summary-kill-same-subject-and-select t]
2248          ["Mark same subject" gnus-summary-kill-same-subject t]
2249          ["Catchup" gnus-summary-catchup
2250           ,@(if (featurep 'xemacs) '(t)
2251               '(:help "Mark unread articles in this group as read"))]
2252          ["Catchup all" gnus-summary-catchup-all t]
2253          ["Catchup to here" gnus-summary-catchup-to-here t]
2254          ["Catchup from here" gnus-summary-catchup-from-here t]
2255          ["Catchup region" gnus-summary-mark-region-as-read t]
2256          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2257         ("Mark Various"
2258          ["Tick" gnus-summary-tick-article-forward t]
2259          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2260          ["Remove marks" gnus-summary-clear-mark-forward t]
2261          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2262          ["Set bookmark" gnus-summary-set-bookmark t]
2263          ["Remove bookmark" gnus-summary-remove-bookmark t])
2264         ("Limit to"
2265          ["Marks..." gnus-summary-limit-to-marks t]
2266          ["Subject..." gnus-summary-limit-to-subject t]
2267          ["Author..." gnus-summary-limit-to-author t]
2268          ["Age..." gnus-summary-limit-to-age t]
2269          ["Extra..." gnus-summary-limit-to-extra t]
2270          ["Score..." gnus-summary-limit-to-score t]
2271          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2272          ["Unread" gnus-summary-limit-to-unread t]
2273          ["Unseen" gnus-summary-limit-to-unseen t]
2274          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2275          ["Articles" gnus-summary-limit-to-articles t]
2276          ["Pop limit" gnus-summary-pop-limit t]
2277          ["Show dormant" gnus-summary-limit-include-dormant t]
2278          ["Hide childless dormant"
2279           gnus-summary-limit-exclude-childless-dormant t]
2280          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2281          ["Hide marked" gnus-summary-limit-exclude-marks t]
2282          ["Show expunged" gnus-summary-limit-include-expunged t])
2283         ("Process Mark"
2284          ["Set mark" gnus-summary-mark-as-processable t]
2285          ["Remove mark" gnus-summary-unmark-as-processable t]
2286          ["Remove all marks" gnus-summary-unmark-all-processable t]
2287          ["Mark above" gnus-uu-mark-over t]
2288          ["Mark series" gnus-uu-mark-series t]
2289          ["Mark region" gnus-uu-mark-region t]
2290          ["Unmark region" gnus-uu-unmark-region t]
2291          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2292          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2293          ["Mark all" gnus-uu-mark-all t]
2294          ["Mark buffer" gnus-uu-mark-buffer t]
2295          ["Mark sparse" gnus-uu-mark-sparse t]
2296          ["Mark thread" gnus-uu-mark-thread t]
2297          ["Unmark thread" gnus-uu-unmark-thread t]
2298          ("Process Mark Sets"
2299           ["Kill" gnus-summary-kill-process-mark t]
2300           ["Yank" gnus-summary-yank-process-mark
2301            gnus-newsgroup-process-stack]
2302           ["Save" gnus-summary-save-process-mark t]))
2303         ("Scroll article"
2304          ["Page forward" gnus-summary-next-page
2305           ,@(if (featurep 'xemacs) '(t)
2306               '(:help "Show next page of article"))]
2307          ["Page backward" gnus-summary-prev-page
2308           ,@(if (featurep 'xemacs) '(t)
2309               '(:help "Show previous page of article"))]
2310          ["Line forward" gnus-summary-scroll-up t])
2311         ("Move"
2312          ["Next unread article" gnus-summary-next-unread-article t]
2313          ["Previous unread article" gnus-summary-prev-unread-article t]
2314          ["Next article" gnus-summary-next-article t]
2315          ["Previous article" gnus-summary-prev-article t]
2316          ["Next unread subject" gnus-summary-next-unread-subject t]
2317          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2318          ["Next article same subject" gnus-summary-next-same-subject t]
2319          ["Previous article same subject" gnus-summary-prev-same-subject t]
2320          ["First unread article" gnus-summary-first-unread-article t]
2321          ["Best unread article" gnus-summary-best-unread-article t]
2322          ["Go to subject number..." gnus-summary-goto-subject t]
2323          ["Go to article number..." gnus-summary-goto-article t]
2324          ["Go to the last article" gnus-summary-goto-last-article t]
2325          ["Pop article off history" gnus-summary-pop-article t])
2326         ("Sort"
2327          ["Sort by number" gnus-summary-sort-by-number t]
2328          ["Sort by author" gnus-summary-sort-by-author t]
2329          ["Sort by subject" gnus-summary-sort-by-subject t]
2330          ["Sort by date" gnus-summary-sort-by-date t]
2331          ["Sort by score" gnus-summary-sort-by-score t]
2332          ["Sort by lines" gnus-summary-sort-by-lines t]
2333          ["Sort by characters" gnus-summary-sort-by-chars t]
2334          ["Randomize" gnus-summary-sort-by-random t]
2335          ["Original sort" gnus-summary-sort-by-original t])
2336         ("Help"
2337          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2338          ["Describe group" gnus-summary-describe-group t]
2339          ["Fetch charter" gnus-group-fetch-charter
2340           ,@(if (featurep 'xemacs) nil
2341               '(:help "Display the charter of the current group"))]
2342          ["Fetch control message" gnus-group-fetch-control
2343           ,@(if (featurep 'xemacs) nil
2344               '(:help "Display the archived control message for the current group"))]
2345          ["Read manual" gnus-info-find-node t])
2346         ("Modes"
2347          ["Pick and read" gnus-pick-mode t]
2348          ["Binary" gnus-binary-mode t])
2349         ("Regeneration"
2350          ["Regenerate" gnus-summary-prepare t]
2351          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2352          ["Toggle threading" gnus-summary-toggle-threads t])
2353         ["See old articles" gnus-summary-insert-old-articles t]
2354         ["See new articles" gnus-summary-insert-new-articles t]
2355         ["Filter articles..." gnus-summary-execute-command t]
2356         ["Run command on subjects..." gnus-summary-universal-argument t]
2357         ["Search articles forward..." gnus-summary-search-article-forward t]
2358         ["Search articles backward..." gnus-summary-search-article-backward t]
2359         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2360         ["Expand window" gnus-summary-expand-window t]
2361         ["Expire expirable articles" gnus-summary-expire-articles
2362          (gnus-check-backend-function
2363           'request-expire-articles gnus-newsgroup-name)]
2364         ["Edit local kill file" gnus-summary-edit-local-kill t]
2365         ["Edit main kill file" gnus-summary-edit-global-kill t]
2366         ["Edit group parameters" gnus-summary-edit-parameters t]
2367         ["Customize group parameters" gnus-summary-customize-parameters t]
2368         ["Send a bug report" gnus-bug t]
2369         ("Exit"
2370          ["Catchup and exit" gnus-summary-catchup-and-exit
2371           ,@(if (featurep 'xemacs) '(t)
2372               '(:help "Mark unread articles in this group as read, then exit"))]
2373          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2374          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2375          ["Exit group" gnus-summary-exit
2376           ,@(if (featurep 'xemacs) '(t)
2377               '(:help "Exit current group, return to group selection mode"))]
2378          ["Exit group without updating" gnus-summary-exit-no-update t]
2379          ["Exit and goto next group" gnus-summary-next-group t]
2380          ["Exit and goto prev group" gnus-summary-prev-group t]
2381          ["Reselect group" gnus-summary-reselect-current-group t]
2382          ["Rescan group" gnus-summary-rescan-group t]
2383          ["Update dribble" gnus-summary-save-newsrc t])))
2384
2385     (gnus-run-hooks 'gnus-summary-menu-hook)))
2386
2387 (defvar gnus-summary-tool-bar-map nil)
2388
2389 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2390 (defun gnus-summary-make-tool-bar ()
2391   (if (and (fboundp 'tool-bar-add-item-from-menu)
2392            (default-value 'tool-bar-mode)
2393            (not gnus-summary-tool-bar-map))
2394       (setq gnus-summary-tool-bar-map
2395             (let ((tool-bar-map (make-sparse-keymap))
2396                   (load-path (mm-image-load-path)))
2397               (tool-bar-add-item-from-menu
2398                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2399               (tool-bar-add-item-from-menu
2400                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2401               (tool-bar-add-item-from-menu
2402                'gnus-summary-post-news "post" gnus-summary-mode-map)
2403               (tool-bar-add-item-from-menu
2404                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2405               (tool-bar-add-item-from-menu
2406                'gnus-summary-followup "followup" gnus-summary-mode-map)
2407               (tool-bar-add-item-from-menu
2408                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2409               (tool-bar-add-item-from-menu
2410                'gnus-summary-reply "reply" gnus-summary-mode-map)
2411               (tool-bar-add-item-from-menu
2412                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2413               (tool-bar-add-item-from-menu
2414                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2415               (tool-bar-add-item-from-menu
2416                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2417               (tool-bar-add-item-from-menu
2418                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2419               (tool-bar-add-item-from-menu
2420                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2421               (tool-bar-add-item-from-menu
2422                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2423               (tool-bar-add-item-from-menu
2424                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2425               (tool-bar-add-item-from-menu
2426                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2427               tool-bar-map)))
2428   (if gnus-summary-tool-bar-map
2429       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2430
2431 (defun gnus-score-set-default (var value)
2432   "A version of set that updates the GNU Emacs menu-bar."
2433   (set var value)
2434   ;; It is the message that forces the active status to be updated.
2435   (message ""))
2436
2437 (defun gnus-make-score-map (type)
2438   "Make a summary score map of type TYPE."
2439   (if t
2440       nil
2441     (let ((headers '(("author" "from" string)
2442                      ("subject" "subject" string)
2443                      ("article body" "body" string)
2444                      ("article head" "head" string)
2445                      ("xref" "xref" string)
2446                      ("extra header" "extra" string)
2447                      ("lines" "lines" number)
2448                      ("followups to author" "followup" string)))
2449           (types '((number ("less than" <)
2450                            ("greater than" >)
2451                            ("equal" =))
2452                    (string ("substring" s)
2453                            ("exact string" e)
2454                            ("fuzzy string" f)
2455                            ("regexp" r))))
2456           (perms '(("temporary" (current-time-string))
2457                    ("permanent" nil)
2458                    ("immediate" now)))
2459           header)
2460       (list
2461        (apply
2462         'nconc
2463         (list
2464          (if (eq type 'lower)
2465              "Lower score"
2466            "Increase score"))
2467         (let (outh)
2468           (while headers
2469             (setq header (car headers))
2470             (setq outh
2471                   (cons
2472                    (apply
2473                     'nconc
2474                     (list (car header))
2475                     (let ((ts (cdr (assoc (nth 2 header) types)))
2476                           outt)
2477                       (while ts
2478                         (setq outt
2479                               (cons
2480                                (apply
2481                                 'nconc
2482                                 (list (caar ts))
2483                                 (let ((ps perms)
2484                                       outp)
2485                                   (while ps
2486                                     (setq outp
2487                                           (cons
2488                                            (vector
2489                                             (caar ps)
2490                                             (list
2491                                              'gnus-summary-score-entry
2492                                              (nth 1 header)
2493                                              (if (or (string= (nth 1 header)
2494                                                               "head")
2495                                                      (string= (nth 1 header)
2496                                                               "body"))
2497                                                  ""
2498                                                (list 'gnus-summary-header
2499                                                      (nth 1 header)))
2500                                              (list 'quote (nth 1 (car ts)))
2501                                              (list 'gnus-score-delta-default
2502                                                    nil)
2503                                              (nth 1 (car ps))
2504                                              t)
2505                                             t)
2506                                            outp))
2507                                     (setq ps (cdr ps)))
2508                                   (list (nreverse outp))))
2509                                outt))
2510                         (setq ts (cdr ts)))
2511                       (list (nreverse outt))))
2512                    outh))
2513             (setq headers (cdr headers)))
2514           (list (nreverse outh))))))))
2515
2516 \f
2517
2518 (defun gnus-summary-mode (&optional group)
2519   "Major mode for reading articles.
2520
2521 All normal editing commands are switched off.
2522 \\<gnus-summary-mode-map>
2523 Each line in this buffer represents one article.  To read an
2524 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2525 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2526 respectively.
2527
2528 You can also post articles and send mail from this buffer.  To
2529 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2530 of an article, type `\\[gnus-summary-reply]'.
2531
2532 There are approx. one gazillion commands you can execute in this
2533 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2534
2535 The following commands are available:
2536
2537 \\{gnus-summary-mode-map}"
2538   (interactive)
2539   (kill-all-local-variables)
2540   (when (gnus-visual-p 'summary-menu 'menu)
2541     (gnus-summary-make-menu-bar)
2542     (gnus-summary-make-tool-bar))
2543   (gnus-summary-make-local-variables)
2544   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2545     (gnus-summary-make-local-variables))
2546   (gnus-make-thread-indent-array)
2547   (gnus-simplify-mode-line)
2548   (setq major-mode 'gnus-summary-mode)
2549   (setq mode-name "Summary")
2550   (make-local-variable 'minor-mode-alist)
2551   (use-local-map gnus-summary-mode-map)
2552   (buffer-disable-undo)
2553   (setq buffer-read-only t)             ;Disable modification
2554   (setq truncate-lines t)
2555   (setq selective-display t)
2556   (setq selective-display-ellipses t)   ;Display `...'
2557   (gnus-summary-set-display-table)
2558   (gnus-set-default-directory)
2559   (setq gnus-newsgroup-name group)
2560   (make-local-variable 'gnus-summary-line-format)
2561   (make-local-variable 'gnus-summary-line-format-spec)
2562   (make-local-variable 'gnus-summary-dummy-line-format)
2563   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2564   (make-local-variable 'gnus-summary-mark-positions)
2565   (make-local-hook 'pre-command-hook)
2566   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2567   (gnus-run-hooks 'gnus-summary-mode-hook)
2568   (turn-on-gnus-mailing-list-mode)
2569   (mm-enable-multibyte)
2570   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2571   (gnus-update-summary-mark-positions))
2572
2573 (defun gnus-summary-make-local-variables ()
2574   "Make all the local summary buffer variables."
2575   (let (global)
2576     (dolist (local gnus-summary-local-variables)
2577       (if (consp local)
2578           (progn
2579             (if (eq (cdr local) 'global)
2580                 ;; Copy the global value of the variable.
2581                 (setq global (symbol-value (car local)))
2582               ;; Use the value from the list.
2583               (setq global (eval (cdr local))))
2584             (set (make-local-variable (car local)) global))
2585         ;; Simple nil-valued local variable.
2586         (set (make-local-variable local) nil)))))
2587
2588 (defun gnus-summary-clear-local-variables ()
2589   (let ((locals gnus-summary-local-variables))
2590     (while locals
2591       (if (consp (car locals))
2592           (and (vectorp (caar locals))
2593                (set (caar locals) nil))
2594         (and (vectorp (car locals))
2595              (set (car locals) nil)))
2596       (setq locals (cdr locals)))))
2597
2598 ;; Summary data functions.
2599
2600 (defmacro gnus-data-number (data)
2601   `(car ,data))
2602
2603 (defmacro gnus-data-set-number (data number)
2604   `(setcar ,data ,number))
2605
2606 (defmacro gnus-data-mark (data)
2607   `(nth 1 ,data))
2608
2609 (defmacro gnus-data-set-mark (data mark)
2610   `(setcar (nthcdr 1 ,data) ,mark))
2611
2612 (defmacro gnus-data-pos (data)
2613   `(nth 2 ,data))
2614
2615 (defmacro gnus-data-set-pos (data pos)
2616   `(setcar (nthcdr 2 ,data) ,pos))
2617
2618 (defmacro gnus-data-header (data)
2619   `(nth 3 ,data))
2620
2621 (defmacro gnus-data-set-header (data header)
2622   `(setf (nth 3 ,data) ,header))
2623
2624 (defmacro gnus-data-level (data)
2625   `(nth 4 ,data))
2626
2627 (defmacro gnus-data-unread-p (data)
2628   `(= (nth 1 ,data) gnus-unread-mark))
2629
2630 (defmacro gnus-data-read-p (data)
2631   `(/= (nth 1 ,data) gnus-unread-mark))
2632
2633 (defmacro gnus-data-pseudo-p (data)
2634   `(consp (nth 3 ,data)))
2635
2636 (defmacro gnus-data-find (number)
2637   `(assq ,number gnus-newsgroup-data))
2638
2639 (defmacro gnus-data-find-list (number &optional data)
2640   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2641      (memq (assq ,number bdata)
2642            bdata)))
2643
2644 (defmacro gnus-data-make (number mark pos header level)
2645   `(list ,number ,mark ,pos ,header ,level))
2646
2647 (defun gnus-data-enter (after-article number mark pos header level offset)
2648   (let ((data (gnus-data-find-list after-article)))
2649     (unless data
2650       (error "No such article: %d" after-article))
2651     (setcdr data (cons (gnus-data-make number mark pos header level)
2652                        (cdr data)))
2653     (setq gnus-newsgroup-data-reverse nil)
2654     (gnus-data-update-list (cddr data) offset)))
2655
2656 (defun gnus-data-enter-list (after-article list &optional offset)
2657   (when list
2658     (let ((data (and after-article (gnus-data-find-list after-article)))
2659           (ilist list))
2660       (if (not (or data
2661                    after-article))
2662           (let ((odata gnus-newsgroup-data))
2663             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2664             (when offset
2665               (gnus-data-update-list odata offset)))
2666       ;; Find the last element in the list to be spliced into the main
2667         ;; list.
2668         (while (cdr list)
2669           (setq list (cdr list)))
2670         (if (not data)
2671             (progn
2672               (setcdr list gnus-newsgroup-data)
2673               (setq gnus-newsgroup-data ilist)
2674               (when offset
2675                 (gnus-data-update-list (cdr list) offset)))
2676           (setcdr list (cdr data))
2677           (setcdr data ilist)
2678           (when offset
2679             (gnus-data-update-list (cdr list) offset))))
2680       (setq gnus-newsgroup-data-reverse nil))))
2681
2682 (defun gnus-data-remove (article &optional offset)
2683   (let ((data gnus-newsgroup-data))
2684     (if (= (gnus-data-number (car data)) article)
2685         (progn
2686           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2687                 gnus-newsgroup-data-reverse nil)
2688           (when offset
2689             (gnus-data-update-list gnus-newsgroup-data offset)))
2690       (while (cdr data)
2691         (when (= (gnus-data-number (cadr data)) article)
2692           (setcdr data (cddr data))
2693           (when offset
2694             (gnus-data-update-list (cdr data) offset))
2695           (setq data nil
2696                 gnus-newsgroup-data-reverse nil))
2697         (setq data (cdr data))))))
2698
2699 (defmacro gnus-data-list (backward)
2700   `(if ,backward
2701        (or gnus-newsgroup-data-reverse
2702            (setq gnus-newsgroup-data-reverse
2703                  (reverse gnus-newsgroup-data)))
2704      gnus-newsgroup-data))
2705
2706 (defun gnus-data-update-list (data offset)
2707   "Add OFFSET to the POS of all data entries in DATA."
2708   (setq gnus-newsgroup-data-reverse nil)
2709   (while data
2710     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2711     (setq data (cdr data))))
2712
2713 (defun gnus-summary-article-pseudo-p (article)
2714   "Say whether this article is a pseudo article or not."
2715   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2716
2717 (defmacro gnus-summary-article-sparse-p (article)
2718   "Say whether this article is a sparse article or not."
2719   `(memq ,article gnus-newsgroup-sparse))
2720
2721 (defmacro gnus-summary-article-ancient-p (article)
2722   "Say whether this article is a sparse article or not."
2723   `(memq ,article gnus-newsgroup-ancient))
2724
2725 (defun gnus-article-parent-p (number)
2726   "Say whether this article is a parent or not."
2727   (let ((data (gnus-data-find-list number)))
2728     (and (cdr data)              ; There has to be an article after...
2729          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2730             (gnus-data-level (nth 1 data))))))
2731
2732 (defun gnus-article-children (number)
2733   "Return a list of all children to NUMBER."
2734   (let* ((data (gnus-data-find-list number))
2735          (level (gnus-data-level (car data)))
2736          children)
2737     (setq data (cdr data))
2738     (while (and data
2739                 (= (gnus-data-level (car data)) (1+ level)))
2740       (push (gnus-data-number (car data)) children)
2741       (setq data (cdr data)))
2742     children))
2743
2744 (defmacro gnus-summary-skip-intangible ()
2745   "If the current article is intangible, then jump to a different article."
2746   '(let ((to (get-text-property (point) 'gnus-intangible)))
2747      (and to (gnus-summary-goto-subject to))))
2748
2749 (defmacro gnus-summary-article-intangible-p ()
2750   "Say whether this article is intangible or not."
2751   '(get-text-property (point) 'gnus-intangible))
2752
2753 (defun gnus-article-read-p (article)
2754   "Say whether ARTICLE is read or not."
2755   (not (or (memq article gnus-newsgroup-marked)
2756            (memq article gnus-newsgroup-spam-marked)
2757            (memq article gnus-newsgroup-unreads)
2758            (memq article gnus-newsgroup-unselected)
2759            (memq article gnus-newsgroup-dormant))))
2760
2761 ;; Some summary mode macros.
2762
2763 (defmacro gnus-summary-article-number ()
2764   "The article number of the article on the current line.
2765 If there isn's an article number here, then we return the current
2766 article number."
2767   '(progn
2768      (gnus-summary-skip-intangible)
2769      (or (get-text-property (point) 'gnus-number)
2770          (gnus-summary-last-subject))))
2771
2772 (defmacro gnus-summary-article-header (&optional number)
2773   "Return the header of article NUMBER."
2774   `(gnus-data-header (gnus-data-find
2775                       ,(or number '(gnus-summary-article-number)))))
2776
2777 (defmacro gnus-summary-thread-level (&optional number)
2778   "Return the level of thread that starts with article NUMBER."
2779   `(if (and (eq gnus-summary-make-false-root 'dummy)
2780             (get-text-property (point) 'gnus-intangible))
2781        0
2782      (gnus-data-level (gnus-data-find
2783                        ,(or number '(gnus-summary-article-number))))))
2784
2785 (defmacro gnus-summary-article-mark (&optional number)
2786   "Return the mark of article NUMBER."
2787   `(gnus-data-mark (gnus-data-find
2788                     ,(or number '(gnus-summary-article-number)))))
2789
2790 (defmacro gnus-summary-article-pos (&optional number)
2791   "Return the position of the line of article NUMBER."
2792   `(gnus-data-pos (gnus-data-find
2793                    ,(or number '(gnus-summary-article-number)))))
2794
2795 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2796 (defmacro gnus-summary-article-subject (&optional number)
2797   "Return current subject string or nil if nothing."
2798   `(let ((headers
2799           ,(if number
2800                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2801              '(gnus-data-header (assq (gnus-summary-article-number)
2802                                       gnus-newsgroup-data)))))
2803      (and headers
2804           (vectorp headers)
2805           (mail-header-subject headers))))
2806
2807 (defmacro gnus-summary-article-score (&optional number)
2808   "Return current article score."
2809   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2810                   gnus-newsgroup-scored))
2811        gnus-summary-default-score 0))
2812
2813 (defun gnus-summary-article-children (&optional number)
2814   "Return a list of article numbers that are children of article NUMBER."
2815   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2816          (level (gnus-data-level (car data)))
2817          l children)
2818     (while (and (setq data (cdr data))
2819                 (> (setq l (gnus-data-level (car data))) level))
2820       (and (= (1+ level) l)
2821            (push (gnus-data-number (car data))
2822                  children)))
2823     (nreverse children)))
2824
2825 (defun gnus-summary-article-parent (&optional number)
2826   "Return the article number of the parent of article NUMBER."
2827   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2828                                     (gnus-data-list t)))
2829          (level (gnus-data-level (car data))))
2830     (if (zerop level)
2831         ()                              ; This is a root.
2832       ;; We search until we find an article with a level less than
2833       ;; this one.  That function has to be the parent.
2834       (while (and (setq data (cdr data))
2835                   (not (< (gnus-data-level (car data)) level))))
2836       (and data (gnus-data-number (car data))))))
2837
2838 (defun gnus-unread-mark-p (mark)
2839   "Say whether MARK is the unread mark."
2840   (= mark gnus-unread-mark))
2841
2842 (defun gnus-read-mark-p (mark)
2843   "Say whether MARK is one of the marks that mark as read.
2844 This is all marks except unread, ticked, dormant, and expirable."
2845   (not (or (= mark gnus-unread-mark)
2846            (= mark gnus-ticked-mark)
2847            (= mark gnus-dormant-mark)
2848            (= mark gnus-expirable-mark))))
2849
2850 (defmacro gnus-article-mark (number)
2851   "Return the MARK of article NUMBER.
2852 This macro should only be used when computing the mark the \"first\"
2853 time; i.e., when generating the summary lines.  After that,
2854 `gnus-summary-article-mark' should be used to examine the
2855 marks of articles."
2856   `(cond
2857     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2858     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2859     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2860     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2861     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2862     ((memq ,number gnus-newsgroup-spam-marked) gnus-spam-mark)
2863     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2864     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2865     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2866            gnus-ancient-mark))))
2867
2868 ;; Saving hidden threads.
2869
2870 (defmacro gnus-save-hidden-threads (&rest forms)
2871   "Save hidden threads, eval FORMS, and restore the hidden threads."
2872   (let ((config (make-symbol "config")))
2873     `(let ((,config (gnus-hidden-threads-configuration)))
2874        (unwind-protect
2875            (save-excursion
2876              ,@forms)
2877          (gnus-restore-hidden-threads-configuration ,config)))))
2878 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2879 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2880
2881 (defun gnus-data-compute-positions ()
2882   "Compute the positions of all articles."
2883   (setq gnus-newsgroup-data-reverse nil)
2884   (let ((data gnus-newsgroup-data))
2885     (save-excursion
2886       (gnus-save-hidden-threads
2887         (gnus-summary-show-all-threads)
2888         (goto-char (point-min))
2889         (while data
2890           (while (get-text-property (point) 'gnus-intangible)
2891             (forward-line 1))
2892           (gnus-data-set-pos (car data) (+ (point) 3))
2893           (setq data (cdr data))
2894           (forward-line 1))))))
2895
2896 (defun gnus-hidden-threads-configuration ()
2897   "Return the current hidden threads configuration."
2898   (save-excursion
2899     (let (config)
2900       (goto-char (point-min))
2901       (while (search-forward "\r" nil t)
2902         (push (1- (point)) config))
2903       config)))
2904
2905 (defun gnus-restore-hidden-threads-configuration (config)
2906   "Restore hidden threads configuration from CONFIG."
2907   (save-excursion
2908     (let (point buffer-read-only)
2909       (while (setq point (pop config))
2910         (when (and (< point (point-max))
2911                    (goto-char point)
2912                    (eq (char-after) ?\n))
2913           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2914
2915 ;; Various summary mode internalish functions.
2916
2917 (defun gnus-mouse-pick-article (e)
2918   (interactive "e")
2919   (mouse-set-point e)
2920   (gnus-summary-next-page nil t))
2921
2922 (defun gnus-summary-set-display-table ()
2923   "Change the display table.
2924 Odd characters have a tendency to mess
2925 up nicely formatted displays - we make all possible glyphs
2926 display only a single character."
2927
2928   ;; We start from the standard display table, if any.
2929   (let ((table (or (copy-sequence standard-display-table)
2930                    (make-display-table)))
2931         (i 32))
2932     ;; Nix out all the control chars...
2933     (while (>= (setq i (1- i)) 0)
2934       (aset table i [??]))
2935    ;; ... but not newline and cr, of course.  (cr is necessary for the
2936     ;; selective display).
2937     (aset table ?\n nil)
2938     (aset table ?\r nil)
2939     ;; We keep TAB as well.
2940     (aset table ?\t nil)
2941     ;; We nix out any glyphs over 126 that are not set already.
2942     (let ((i 256))
2943       (while (>= (setq i (1- i)) 127)
2944         ;; Only modify if the entry is nil.
2945         (unless (aref table i)
2946           (aset table i [??]))))
2947     (setq buffer-display-table table)))
2948
2949 (defun gnus-summary-set-article-display-arrow (pos)
2950   "Update the overlay arrow to point to line at position POS."
2951   (when (and gnus-summary-display-arrow
2952              (boundp 'overlay-arrow-position)
2953              (boundp 'overlay-arrow-string))
2954     (save-excursion
2955       (goto-char pos)
2956       (beginning-of-line)
2957       (unless overlay-arrow-position
2958         (setq overlay-arrow-position (make-marker)))
2959       (setq overlay-arrow-string "=>"
2960             overlay-arrow-position (set-marker overlay-arrow-position
2961                                                (point)
2962                                                (current-buffer))))))
2963
2964 (defun gnus-summary-buffer-name (group)
2965   "Return the summary buffer name of GROUP."
2966   (concat "*Summary " (gnus-group-decoded-name group) "*"))
2967
2968 (defun gnus-summary-setup-buffer (group)
2969   "Initialize summary buffer."
2970   (let ((buffer (gnus-summary-buffer-name group))
2971         (dead-name (concat "*Dead Summary "
2972                            (gnus-group-decoded-name group) "*")))
2973     ;; If a dead summary buffer exists, we kill it.
2974     (when (gnus-buffer-live-p dead-name)
2975       (gnus-kill-buffer dead-name))
2976     (if (get-buffer buffer)
2977         (progn
2978           (set-buffer buffer)
2979           (setq gnus-summary-buffer (current-buffer))
2980           (not gnus-newsgroup-prepared))
2981       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2982       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2983       (gnus-summary-mode group)
2984       (when gnus-carpal
2985         (gnus-carpal-setup-buffer 'summary))
2986       (unless gnus-single-article-buffer
2987         (make-local-variable 'gnus-article-buffer)
2988         (make-local-variable 'gnus-article-current)
2989         (make-local-variable 'gnus-original-article-buffer))
2990       (setq gnus-newsgroup-name group)
2991       ;; Set any local variables in the group parameters.
2992       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2993       t)))
2994
2995 (defun gnus-set-global-variables ()
2996   "Set the global equivalents of the buffer-local variables.
2997 They are set to the latest values they had.  These reflect the summary
2998 buffer that was in action when the last article was fetched."
2999   (when (eq major-mode 'gnus-summary-mode)
3000     (setq gnus-summary-buffer (current-buffer))
3001     (let ((name gnus-newsgroup-name)
3002           (marked gnus-newsgroup-marked)
3003           (spam gnus-newsgroup-spam-marked)
3004           (unread gnus-newsgroup-unreads)
3005           (headers gnus-current-headers)
3006           (data gnus-newsgroup-data)
3007           (summary gnus-summary-buffer)
3008           (article-buffer gnus-article-buffer)
3009           (original gnus-original-article-buffer)
3010           (gac gnus-article-current)
3011           (reffed gnus-reffed-article-number)
3012           (score-file gnus-current-score-file)
3013           (default-charset gnus-newsgroup-charset)
3014           vlist)
3015       (let ((locals gnus-newsgroup-variables))
3016         (while locals
3017           (if (consp (car locals))
3018               (push (eval (caar locals)) vlist)
3019             (push (eval (car locals)) vlist))
3020           (setq locals (cdr locals)))
3021         (setq vlist (nreverse vlist)))
3022       (save-excursion
3023         (set-buffer gnus-group-buffer)
3024         (setq gnus-newsgroup-name name
3025               gnus-newsgroup-marked marked
3026               gnus-newsgroup-spam-marked spam
3027               gnus-newsgroup-unreads unread
3028               gnus-current-headers headers
3029               gnus-newsgroup-data data
3030               gnus-article-current gac
3031               gnus-summary-buffer summary
3032               gnus-article-buffer article-buffer
3033               gnus-original-article-buffer original
3034               gnus-reffed-article-number reffed
3035               gnus-current-score-file score-file
3036               gnus-newsgroup-charset default-charset)
3037         (let ((locals gnus-newsgroup-variables))
3038           (while locals
3039             (if (consp (car locals))
3040                 (set (caar locals) (pop vlist))
3041               (set (car locals) (pop vlist)))
3042             (setq locals (cdr locals))))
3043         ;; The article buffer also has local variables.
3044         (when (gnus-buffer-live-p gnus-article-buffer)
3045           (set-buffer gnus-article-buffer)
3046           (setq gnus-summary-buffer summary))))))
3047
3048 (defun gnus-summary-article-unread-p (article)
3049   "Say whether ARTICLE is unread or not."
3050   (memq article gnus-newsgroup-unreads))
3051
3052 (defun gnus-summary-first-article-p (&optional article)
3053   "Return whether ARTICLE is the first article in the buffer."
3054   (if (not (setq article (or article (gnus-summary-article-number))))
3055       nil
3056     (eq article (caar gnus-newsgroup-data))))
3057
3058 (defun gnus-summary-last-article-p (&optional article)
3059   "Return whether ARTICLE is the last article in the buffer."
3060   (if (not (setq article (or article (gnus-summary-article-number))))
3061       ;; All non-existent numbers are the last article.  :-)
3062       t
3063     (not (cdr (gnus-data-find-list article)))))
3064
3065 (defun gnus-make-thread-indent-array ()
3066   (let ((n 200))
3067     (unless (and gnus-thread-indent-array
3068                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
3069       (setq gnus-thread-indent-array (make-vector 201 "")
3070             gnus-thread-indent-array-level gnus-thread-indent-level)
3071       (while (>= n 0)
3072         (aset gnus-thread-indent-array n
3073               (make-string (* n gnus-thread-indent-level) ? ))
3074         (setq n (1- n))))))
3075
3076 (defun gnus-update-summary-mark-positions ()
3077   "Compute where the summary marks are to go."
3078   (save-excursion
3079     (when (gnus-buffer-exists-p gnus-summary-buffer)
3080       (set-buffer gnus-summary-buffer))
3081     (let ((gnus-replied-mark 129)
3082           (gnus-score-below-mark 130)
3083           (gnus-score-over-mark 130)
3084           (gnus-download-mark 131)
3085           (spec gnus-summary-line-format-spec)
3086           gnus-visual pos)
3087       (save-excursion
3088         (gnus-set-work-buffer)
3089         (let ((gnus-summary-line-format-spec spec)
3090               (gnus-newsgroup-downloadable '((0 . t))))
3091           (gnus-summary-insert-line
3092            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
3093            0 nil 128 t nil "" nil 1)
3094           (goto-char (point-min))
3095           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
3096                                              (- (point) (point-min) 1)))))
3097           (goto-char (point-min))
3098           (push (cons 'replied (and (search-forward "\201" nil t)
3099                                     (- (point) (point-min) 1)))
3100                 pos)
3101           (goto-char (point-min))
3102           (push (cons 'score (and (search-forward "\202" nil t)
3103                                   (- (point) (point-min) 1)))
3104                 pos)
3105           (goto-char (point-min))
3106           (push (cons 'download
3107                       (and (search-forward "\203" nil t)
3108                            (- (point) (point-min) 1)))
3109                 pos)))
3110       (setq gnus-summary-mark-positions pos))))
3111
3112 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
3113   "Insert a dummy root in the summary buffer."
3114   (beginning-of-line)
3115   (gnus-add-text-properties
3116    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
3117    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
3118
3119 (defun gnus-summary-extract-address-component (from)
3120   (or (car (funcall gnus-extract-address-components from))
3121       from))
3122
3123 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
3124   (let ((mail-parse-charset gnus-newsgroup-charset)
3125         ; Is it really necessary to do this next part for each summary line?
3126         ; Luckily, doesn't seem to slow things down much.
3127         (mail-parse-ignored-charsets
3128          (save-excursion (set-buffer gnus-summary-buffer)
3129                          gnus-newsgroup-ignored-charsets)))
3130     (or
3131      (and gnus-ignored-from-addresses
3132           (string-match gnus-ignored-from-addresses gnus-tmp-from)
3133           (let ((extra-headers (mail-header-extra header))
3134                 to
3135                 newsgroups)
3136             (cond
3137              ((setq to (cdr (assq 'To extra-headers)))
3138               (concat "-> "
3139                       (inline
3140                         (gnus-summary-extract-address-component
3141                          (funcall gnus-decode-encoded-word-function to)))))
3142              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
3143               (concat "=> " newsgroups)))))
3144      (inline (gnus-summary-extract-address-component gnus-tmp-from)))))
3145
3146 (defun gnus-summary-insert-line (gnus-tmp-header
3147                                  gnus-tmp-level gnus-tmp-current
3148                                  gnus-tmp-unread gnus-tmp-replied
3149                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
3150                                  &optional gnus-tmp-dummy gnus-tmp-score
3151                                  gnus-tmp-process)
3152   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
3153          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
3154          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
3155          (gnus-tmp-score-char
3156           (if (or (null gnus-summary-default-score)
3157                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
3158                       gnus-summary-zcore-fuzz))
3159               ?                         ;Whitespace
3160             (if (< gnus-tmp-score gnus-summary-default-score)
3161                 gnus-score-below-mark gnus-score-over-mark)))
3162          (gnus-tmp-number (mail-header-number gnus-tmp-header))
3163          (gnus-tmp-replied
3164           (cond (gnus-tmp-process gnus-process-mark)
3165                 ((memq gnus-tmp-current gnus-newsgroup-cached)
3166                  gnus-cached-mark)
3167                 (gnus-tmp-replied gnus-replied-mark)
3168                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
3169                  gnus-forwarded-mark)
3170                 ((memq gnus-tmp-current gnus-newsgroup-saved)
3171                  gnus-saved-mark)
3172                 ((memq gnus-tmp-number gnus-newsgroup-recent)
3173                  gnus-recent-mark)
3174                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
3175                  gnus-unseen-mark)
3176                 (t gnus-no-mark)))
3177          (gnus-tmp-from (mail-header-from gnus-tmp-header))
3178          (gnus-tmp-name
3179           (cond
3180            ((string-match "<[^>]+> *$" gnus-tmp-from)
3181             (let ((beg (match-beginning 0)))
3182               (or (and (string-match "^\".+\"" gnus-tmp-from)
3183                        (substring gnus-tmp-from 1 (1- (match-end 0))))
3184                   (substring gnus-tmp-from 0 beg))))
3185            ((string-match "(.+)" gnus-tmp-from)
3186             (substring gnus-tmp-from
3187                        (1+ (match-beginning 0)) (1- (match-end 0))))
3188            (t gnus-tmp-from)))
3189          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
3190          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
3191          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
3192          (buffer-read-only nil))
3193     (when (string= gnus-tmp-name "")
3194       (setq gnus-tmp-name gnus-tmp-from))
3195     (unless (numberp gnus-tmp-lines)
3196       (setq gnus-tmp-lines -1))
3197     (if (= gnus-tmp-lines -1)
3198         (setq gnus-tmp-lines "?")
3199       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
3200     (gnus-put-text-property
3201      (point)
3202      (progn (eval gnus-summary-line-format-spec) (point))
3203      'gnus-number gnus-tmp-number)
3204     (when (gnus-visual-p 'summary-highlight 'highlight)
3205       (forward-line -1)
3206       (gnus-run-hooks 'gnus-summary-update-hook)
3207       (forward-line 1))))
3208
3209 (defun gnus-summary-update-line (&optional dont-update)
3210   "Update summary line after change."
3211   (when (and gnus-summary-default-score
3212              (not gnus-summary-inhibit-highlight))
3213     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3214            (article (gnus-summary-article-number))
3215            (score (gnus-summary-article-score article)))
3216       (unless dont-update
3217         (if (and gnus-summary-mark-below
3218                  (< (gnus-summary-article-score)
3219                     gnus-summary-mark-below))
3220             ;; This article has a low score, so we mark it as read.
3221             (when (memq article gnus-newsgroup-unreads)
3222               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3223           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3224             ;; This article was previously marked as read on account
3225             ;; of a low score, but now it has risen, so we mark it as
3226             ;; unread.
3227             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3228         (gnus-summary-update-mark
3229          (if (or (null gnus-summary-default-score)
3230                  (<= (abs (- score gnus-summary-default-score))
3231                      gnus-summary-zcore-fuzz))
3232              ?                          ;Whitespace
3233            (if (< score gnus-summary-default-score)
3234                gnus-score-below-mark gnus-score-over-mark))
3235          'score))
3236       ;; Do visual highlighting.
3237       (when (gnus-visual-p 'summary-highlight 'highlight)
3238         (gnus-run-hooks 'gnus-summary-update-hook)))))
3239
3240 (defvar gnus-tmp-new-adopts nil)
3241
3242 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3243   "Return the number of articles in THREAD.
3244 This may be 0 in some cases -- if none of the articles in
3245 the thread are to be displayed."
3246   (let* ((number
3247          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3248           (cond
3249            ((not (listp thread))
3250             1)
3251            ((and (consp thread) (cdr thread))
3252             (apply
3253              '+ 1 (mapcar
3254                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3255            ((null thread)
3256             1)
3257            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3258             1)
3259            (t 0))))
3260     (when (and level (zerop level) gnus-tmp-new-adopts)
3261       (incf number
3262             (apply '+ (mapcar
3263                        'gnus-summary-number-of-articles-in-thread
3264                        gnus-tmp-new-adopts))))
3265     (if char
3266         (if (> number 1) gnus-not-empty-thread-mark
3267           gnus-empty-thread-mark)
3268       number)))
3269
3270 (defsubst gnus-summary-line-message-size (head)
3271   "Return pretty-printed version of message size.
3272 This function is intended to be used in
3273 `gnus-summary-line-format-alist', which see."
3274   (let ((c (or (mail-header-chars head) -1)))
3275     (cond ((< c 0) "n/a")               ; chars not available
3276           ((< c (* 1000 10)) (format "%1.1fk" (/ c 1024.0)))
3277           ((< c (* 1000 100)) (format "%dk" (/ c 1024.0)))
3278           ((< c (* 1000 10000)) (format "%1.1fM" (/ c (* 1024.0 1024))))
3279           (t (format "%dM" (/ c (* 1024.0 1024)))))))
3280
3281
3282 (defun gnus-summary-set-local-parameters (group)
3283   "Go through the local params of GROUP and set all variable specs in that list."
3284   (let ((params (gnus-group-find-parameter group))
3285         (vars '(quit-config))           ; Ignore quit-config.
3286         elem)
3287     (while params
3288       (setq elem (car params)
3289             params (cdr params))
3290       (and (consp elem)                 ; Has to be a cons.
3291            (consp (cdr elem))           ; The cdr has to be a list.
3292            (symbolp (car elem))         ; Has to be a symbol in there.
3293            (not (memq (car elem) vars))
3294            (ignore-errors               ; So we set it.
3295              (push (car elem) vars)
3296              (make-local-variable (car elem))
3297              (set (car elem) (eval (nth 1 elem))))))))
3298
3299 (defun gnus-summary-read-group (group &optional show-all no-article
3300                                       kill-buffer no-display backward
3301                                       select-articles)
3302   "Start reading news in newsgroup GROUP.
3303 If SHOW-ALL is non-nil, already read articles are also listed.
3304 If NO-ARTICLE is non-nil, no article is selected initially.
3305 If NO-DISPLAY, don't generate a summary buffer."
3306   (let (result)
3307     (while (and group
3308                 (null (setq result
3309                             (let ((gnus-auto-select-next nil))
3310                               (or (gnus-summary-read-group-1
3311                                    group show-all no-article
3312                                    kill-buffer no-display
3313                                    select-articles)
3314                                   (setq show-all nil
3315                                         select-articles nil)))))
3316                 (eq gnus-auto-select-next 'quietly))
3317       (set-buffer gnus-group-buffer)
3318       ;; The entry function called above goes to the next
3319       ;; group automatically, so we go two groups back
3320       ;; if we are searching for the previous group.
3321       (when backward
3322         (gnus-group-prev-unread-group 2))
3323       (if (not (equal group (gnus-group-group-name)))
3324           (setq group (gnus-group-group-name))
3325         (setq group nil)))
3326     result))
3327
3328 (defun gnus-summary-read-group-1 (group show-all no-article
3329                                         kill-buffer no-display
3330                                         &optional select-articles)
3331   ;; Killed foreign groups can't be entered.
3332   ;;  (when (and (not (gnus-group-native-p group))
3333   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3334   ;;    (error "Dead non-native groups can't be entered"))
3335   (gnus-message 5 "Retrieving newsgroup: %s..."
3336                 (gnus-group-decoded-name group))
3337   (let* ((new-group (gnus-summary-setup-buffer group))
3338          (quit-config (gnus-group-quit-config group))
3339          (did-select (and new-group (gnus-select-newsgroup
3340                                      group show-all select-articles))))
3341     (cond
3342      ;; This summary buffer exists already, so we just select it.
3343      ((not new-group)
3344       (gnus-set-global-variables)
3345       (when kill-buffer
3346         (gnus-kill-or-deaden-summary kill-buffer))
3347       (gnus-configure-windows 'summary 'force)
3348       (gnus-set-mode-line 'summary)
3349       (gnus-summary-position-point)
3350       (message "")
3351       t)
3352      ;; We couldn't select this group.
3353      ((null did-select)
3354       (when (and (eq major-mode 'gnus-summary-mode)
3355                  (not (equal (current-buffer) kill-buffer)))
3356         (kill-buffer (current-buffer))
3357         (if (not quit-config)
3358             (progn
3359               ;; Update the info -- marks might need to be removed,
3360               ;; for instance.
3361               (gnus-summary-update-info)
3362               (set-buffer gnus-group-buffer)
3363               (gnus-group-jump-to-group group)
3364               (gnus-group-next-unread-group 1))
3365           (gnus-handle-ephemeral-exit quit-config)))
3366       (let ((grpinfo (gnus-get-info group)))
3367         (if (null (gnus-info-read grpinfo))
3368             (gnus-message 3 "Group %s contains no messages"
3369                           (gnus-group-decoded-name group))
3370           (gnus-message 3 "Can't select group")))
3371       nil)
3372      ;; The user did a `C-g' while prompting for number of articles,
3373      ;; so we exit this group.
3374      ((eq did-select 'quit)
3375       (and (eq major-mode 'gnus-summary-mode)
3376            (not (equal (current-buffer) kill-buffer))
3377            (kill-buffer (current-buffer)))
3378       (when kill-buffer
3379         (gnus-kill-or-deaden-summary kill-buffer))
3380       (if (not quit-config)
3381           (progn
3382             (set-buffer gnus-group-buffer)
3383             (gnus-group-jump-to-group group)
3384             (gnus-group-next-unread-group 1)
3385             (gnus-configure-windows 'group 'force))
3386         (gnus-handle-ephemeral-exit quit-config))
3387       ;; Finally signal the quit.
3388       (signal 'quit nil))
3389      ;; The group was successfully selected.
3390      (t
3391       (gnus-set-global-variables)
3392       ;; Save the active value in effect when the group was entered.
3393       (setq gnus-newsgroup-active
3394             (gnus-copy-sequence
3395              (gnus-active gnus-newsgroup-name)))
3396       ;; You can change the summary buffer in some way with this hook.
3397       (gnus-run-hooks 'gnus-select-group-hook)
3398       (gnus-update-format-specifications
3399        nil 'summary 'summary-mode 'summary-dummy)
3400       (gnus-update-summary-mark-positions)
3401       ;; Do score processing.
3402       (when gnus-use-scoring
3403         (gnus-possibly-score-headers))
3404       ;; Check whether to fill in the gaps in the threads.
3405       (when gnus-build-sparse-threads
3406         (gnus-build-sparse-threads))
3407       ;; Find the initial limit.
3408       (if gnus-show-threads
3409           (if show-all
3410               (let ((gnus-newsgroup-dormant nil))
3411                 (gnus-summary-initial-limit show-all))
3412             (gnus-summary-initial-limit show-all))
3413         ;; When untreaded, all articles are always shown.
3414         (setq gnus-newsgroup-limit
3415               (mapcar
3416                (lambda (header) (mail-header-number header))
3417                gnus-newsgroup-headers)))
3418       ;; Generate the summary buffer.
3419       (unless no-display
3420         (gnus-summary-prepare))
3421       (when gnus-use-trees
3422         (gnus-tree-open group)
3423         (setq gnus-summary-highlight-line-function
3424               'gnus-tree-highlight-article))
3425       ;; If the summary buffer is empty, but there are some low-scored
3426       ;; articles or some excluded dormants, we include these in the
3427       ;; buffer.
3428       (when (and (zerop (buffer-size))
3429                  (not no-display))
3430         (cond (gnus-newsgroup-dormant
3431                (gnus-summary-limit-include-dormant))
3432               ((and gnus-newsgroup-scored show-all)
3433                (gnus-summary-limit-include-expunged t))))
3434       ;; Function `gnus-apply-kill-file' must be called in this hook.
3435       (gnus-run-hooks 'gnus-apply-kill-hook)
3436       (if (and (zerop (buffer-size))
3437                (not no-display))
3438           (progn
3439             ;; This newsgroup is empty.
3440             (gnus-summary-catchup-and-exit nil t)
3441             (gnus-message 6 "No unread news")
3442             (when kill-buffer
3443               (gnus-kill-or-deaden-summary kill-buffer))
3444             ;; Return nil from this function.
3445             nil)
3446         ;; Hide conversation thread subtrees.  We cannot do this in
3447         ;; gnus-summary-prepare-hook since kill processing may not
3448         ;; work with hidden articles.
3449         (gnus-summary-maybe-hide-threads)
3450         (when kill-buffer
3451           (gnus-kill-or-deaden-summary kill-buffer))
3452         (gnus-summary-auto-select-subject)
3453         ;; Show first unread article if requested.
3454         (if (and (not no-article)
3455                  (not no-display)
3456                  gnus-newsgroup-unreads
3457                  gnus-auto-select-first)
3458             (progn
3459               (gnus-configure-windows 'summary)
3460               (let ((art (gnus-summary-article-number)))
3461                 (unless (or (memq art gnus-newsgroup-undownloaded)
3462                             (memq art gnus-newsgroup-downloadable))
3463                   (gnus-summary-goto-article art))))
3464           ;; Don't select any articles.
3465           (gnus-summary-position-point)
3466           (gnus-configure-windows 'summary 'force)
3467           (gnus-set-mode-line 'summary))
3468         (when (get-buffer-window gnus-group-buffer t)
3469           ;; Gotta use windows, because recenter does weird stuff if
3470           ;; the current buffer ain't the displayed window.
3471           (let ((owin (selected-window)))
3472             (select-window (get-buffer-window gnus-group-buffer t))
3473             (when (gnus-group-goto-group group)
3474               (recenter))
3475             (select-window owin)))
3476         ;; Mark this buffer as "prepared".
3477         (setq gnus-newsgroup-prepared t)
3478         (gnus-run-hooks 'gnus-summary-prepared-hook)
3479         t)))))
3480
3481 (defun gnus-summary-auto-select-subject ()
3482   "Select the subject line on initial group entry."
3483   (goto-char (point-min))
3484   (cond
3485    ((eq gnus-auto-select-subject 'best)
3486     (gnus-summary-best-unread-subject))
3487    ((eq gnus-auto-select-subject 'unread)
3488     (gnus-summary-first-unread-subject))
3489    ((eq gnus-auto-select-subject 'unseen)
3490     (gnus-summary-first-unseen-subject))
3491    ((eq gnus-auto-select-subject 'unseen-or-unread)
3492     (gnus-summary-first-unseen-or-unread-subject))
3493    ((eq gnus-auto-select-subject 'first)
3494     ;; Do nothing.
3495     )
3496    ((gnus-functionp gnus-auto-select-subject)
3497     (funcall gnus-auto-select-subject))))
3498
3499 (defun gnus-summary-prepare ()
3500   "Generate the summary buffer."
3501   (interactive)
3502   (let ((buffer-read-only nil))
3503     (erase-buffer)
3504     (setq gnus-newsgroup-data nil
3505           gnus-newsgroup-data-reverse nil)
3506     (gnus-run-hooks 'gnus-summary-generate-hook)
3507     ;; Generate the buffer, either with threads or without.
3508     (when gnus-newsgroup-headers
3509       (gnus-summary-prepare-threads
3510        (if gnus-show-threads
3511            (gnus-sort-gathered-threads
3512             (funcall gnus-summary-thread-gathering-function
3513                      (gnus-sort-threads
3514                       (gnus-cut-threads (gnus-make-threads)))))
3515          ;; Unthreaded display.
3516          (gnus-sort-articles gnus-newsgroup-headers))))
3517     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3518     ;; Call hooks for modifying summary buffer.
3519     (goto-char (point-min))
3520     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3521
3522 (defsubst gnus-general-simplify-subject (subject)
3523   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3524   (setq subject
3525         (cond
3526          ;; Truncate the subject.
3527          (gnus-simplify-subject-functions
3528           (gnus-map-function gnus-simplify-subject-functions subject))
3529          ((numberp gnus-summary-gather-subject-limit)
3530           (setq subject (gnus-simplify-subject-re subject))
3531           (if (> (length subject) gnus-summary-gather-subject-limit)
3532               (substring subject 0 gnus-summary-gather-subject-limit)
3533             subject))
3534          ;; Fuzzily simplify it.
3535          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3536           (gnus-simplify-subject-fuzzy subject))
3537          ;; Just remove the leading "Re:".
3538          (t
3539           (gnus-simplify-subject-re subject))))
3540
3541   (if (and gnus-summary-gather-exclude-subject
3542            (string-match gnus-summary-gather-exclude-subject subject))
3543       nil                         ; This article shouldn't be gathered
3544     subject))
3545
3546 (defun gnus-summary-simplify-subject-query ()
3547   "Query where the respool algorithm would put this article."
3548   (interactive)
3549   (gnus-summary-select-article)
3550   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3551
3552 (defun gnus-gather-threads-by-subject (threads)
3553   "Gather threads by looking at Subject headers."
3554   (if (not gnus-summary-make-false-root)
3555       threads
3556     (let ((hashtb (gnus-make-hashtable 1024))
3557           (prev threads)
3558           (result threads)
3559           subject hthread whole-subject)
3560       (while threads
3561         (setq subject (gnus-general-simplify-subject
3562                        (setq whole-subject (mail-header-subject
3563                                             (caar threads)))))
3564         (when subject
3565           (if (setq hthread (gnus-gethash subject hashtb))
3566               (progn
3567                 ;; We enter a dummy root into the thread, if we
3568                 ;; haven't done that already.
3569                 (unless (stringp (caar hthread))
3570                   (setcar hthread (list whole-subject (car hthread))))
3571                 ;; We add this new gathered thread to this gathered
3572                 ;; thread.
3573                 (setcdr (car hthread)
3574                         (nconc (cdar hthread) (list (car threads))))
3575                 ;; Remove it from the list of threads.
3576                 (setcdr prev (cdr threads))
3577                 (setq threads prev))
3578             ;; Enter this thread into the hash table.
3579             (gnus-sethash subject threads hashtb)))
3580         (setq prev threads)
3581         (setq threads (cdr threads)))
3582       result)))
3583
3584 (defun gnus-gather-threads-by-references (threads)
3585   "Gather threads by looking at References headers."
3586   (let ((idhashtb (gnus-make-hashtable 1024))
3587         (thhashtb (gnus-make-hashtable 1024))
3588         (prev threads)
3589         (result threads)
3590         ids references id gthread gid entered ref)
3591     (while threads
3592       (when (setq references (mail-header-references (caar threads)))
3593         (setq id (mail-header-id (caar threads))
3594               ids (inline (gnus-split-references references))
3595               entered nil)
3596         (while (setq ref (pop ids))
3597           (setq ids (delete ref ids))
3598           (if (not (setq gid (gnus-gethash ref idhashtb)))
3599               (progn
3600                 (gnus-sethash ref id idhashtb)
3601                 (gnus-sethash id threads thhashtb))
3602             (setq gthread (gnus-gethash gid thhashtb))
3603             (unless entered
3604               ;; We enter a dummy root into the thread, if we
3605               ;; haven't done that already.
3606               (unless (stringp (caar gthread))
3607                 (setcar gthread (list (mail-header-subject (caar gthread))
3608                                       (car gthread))))
3609               ;; We add this new gathered thread to this gathered
3610               ;; thread.
3611               (setcdr (car gthread)
3612                       (nconc (cdar gthread) (list (car threads)))))
3613             ;; Add it into the thread hash table.
3614             (gnus-sethash id gthread thhashtb)
3615             (setq entered t)
3616             ;; Remove it from the list of threads.
3617             (setcdr prev (cdr threads))
3618             (setq threads prev))))
3619       (setq prev threads)
3620       (setq threads (cdr threads)))
3621     result))
3622
3623 (defun gnus-sort-gathered-threads (threads)
3624   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3625   (let ((result threads))
3626     (while threads
3627       (when (stringp (caar threads))
3628         (setcdr (car threads)
3629                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3630       (setq threads (cdr threads)))
3631     result))
3632
3633 (defun gnus-thread-loop-p (root thread)
3634   "Say whether ROOT is in THREAD."
3635   (let ((stack (list thread))
3636         (infloop 0)
3637         th)
3638     (while (setq thread (pop stack))
3639       (setq th (cdr thread))
3640       (while (and th
3641                   (not (eq (caar th) root)))
3642         (pop th))
3643       (if th
3644           ;; We have found a loop.
3645           (let (ref-dep)
3646             (setcdr thread (delq (car th) (cdr thread)))
3647             (if (boundp (setq ref-dep (intern "none"
3648                                               gnus-newsgroup-dependencies)))
3649                 (setcdr (symbol-value ref-dep)
3650                         (nconc (cdr (symbol-value ref-dep))
3651                                (list (car th))))
3652               (set ref-dep (list nil (car th))))
3653             (setq infloop 1
3654                   stack nil))
3655         ;; Push all the subthreads onto the stack.
3656         (push (cdr thread) stack)))
3657     infloop))
3658
3659 (defun gnus-make-threads ()
3660   "Go through the dependency hashtb and find the roots.  Return all threads."
3661   (let (threads)
3662     (while (catch 'infloop
3663              (mapatoms
3664               (lambda (refs)
3665                 ;; Deal with self-referencing References loops.
3666                 (when (and (car (symbol-value refs))
3667                            (not (zerop
3668                                  (apply
3669                                   '+
3670                                   (mapcar
3671                                    (lambda (thread)
3672                                      (gnus-thread-loop-p
3673                                       (car (symbol-value refs)) thread))
3674                                    (cdr (symbol-value refs)))))))
3675                   (setq threads nil)
3676                   (throw 'infloop t))
3677                 (unless (car (symbol-value refs))
3678                   ;; These threads do not refer back to any other
3679                   ;; articles, so they're roots.
3680                   (setq threads (append (cdr (symbol-value refs)) threads))))
3681               gnus-newsgroup-dependencies)))
3682     threads))
3683
3684 ;; Build the thread tree.
3685 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3686   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3687
3688 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3689 if it was already present.
3690
3691 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3692 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3693 Message-IDs will be renamed to a unique Message-ID before being
3694 entered.
3695
3696 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3697   (let* ((id (mail-header-id header))
3698          (id-dep (and id (intern id dependencies)))
3699          parent-id ref ref-dep ref-header replaced)
3700     ;; Enter this `header' in the `dependencies' table.
3701     (cond
3702      ((not id-dep)
3703       (setq header nil))
3704      ;; The first two cases do the normal part: enter a new `header'
3705      ;; in the `dependencies' table.
3706      ((not (boundp id-dep))
3707       (set id-dep (list header)))
3708      ((null (car (symbol-value id-dep)))
3709       (setcar (symbol-value id-dep) header))
3710
3711      ;; From here the `header' was already present in the
3712      ;; `dependencies' table.
3713      (force-new
3714       ;; Overrides an existing entry;
3715       ;; just set the header part of the entry.
3716       (setcar (symbol-value id-dep) header)
3717       (setq replaced t))
3718
3719      ;; Renames the existing `header' to a unique Message-ID.
3720      ((not gnus-summary-ignore-duplicates)
3721       ;; An article with this Message-ID has already been seen.
3722       ;; We rename the Message-ID.
3723       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3724            (list header))
3725       (mail-header-set-id header id))
3726
3727      ;; The last case ignores an existing entry, except it adds any
3728      ;; additional Xrefs (in case the two articles came from different
3729      ;; servers.
3730      ;; Also sets `header' to `nil' meaning that the `dependencies'
3731      ;; table was *not* modified.
3732      (t
3733       (mail-header-set-xref
3734        (car (symbol-value id-dep))
3735        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3736                    "")
3737                (or (mail-header-xref header) "")))
3738       (setq header nil)))
3739
3740     (when (and header (not replaced))
3741       ;; First check that we are not creating a References loop.
3742       (setq parent-id (gnus-parent-id (mail-header-references header)))
3743       (setq ref parent-id)
3744       (while (and ref
3745                   (setq ref-dep (intern-soft ref dependencies))
3746                   (boundp ref-dep)
3747                   (setq ref-header (car (symbol-value ref-dep))))
3748         (if (string= id ref)
3749             ;; Yuk!  This is a reference loop.  Make the article be a
3750             ;; root article.
3751             (progn
3752               (mail-header-set-references (car (symbol-value id-dep)) "none")
3753               (setq ref nil)
3754               (setq parent-id nil))
3755           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3756       (setq ref-dep (intern (or parent-id "none") dependencies))
3757       (if (boundp ref-dep)
3758           (setcdr (symbol-value ref-dep)
3759                   (nconc (cdr (symbol-value ref-dep))
3760                          (list (symbol-value id-dep))))
3761         (set ref-dep (list nil (symbol-value id-dep)))))
3762     header))
3763
3764 (defun gnus-extract-message-id-from-in-reply-to (string)
3765   (if (string-match "<[^>]+>" string)
3766       (substring string (match-beginning 0) (match-end 0))
3767     nil))
3768
3769 (defun gnus-build-sparse-threads ()
3770   (let ((headers gnus-newsgroup-headers)
3771         (mail-parse-charset gnus-newsgroup-charset)
3772         (gnus-summary-ignore-duplicates t)
3773         header references generation relations
3774         subject child end new-child date)
3775     ;; First we create an alist of generations/relations, where
3776     ;; generations is how much we trust the relation, and the relation
3777     ;; is parent/child.
3778     (gnus-message 7 "Making sparse threads...")
3779     (save-excursion
3780       (nnheader-set-temp-buffer " *gnus sparse threads*")
3781       (while (setq header (pop headers))
3782         (when (and (setq references (mail-header-references header))
3783                    (not (string= references "")))
3784           (insert references)
3785           (setq child (mail-header-id header)
3786                 subject (mail-header-subject header)
3787                 date (mail-header-date header)
3788                 generation 0)
3789           (while (search-backward ">" nil t)
3790             (setq end (1+ (point)))
3791             (when (search-backward "<" nil t)
3792               (setq new-child (buffer-substring (point) end))
3793               (push (list (incf generation)
3794                           child (setq child new-child)
3795                           subject date)
3796                     relations)))
3797           (when child
3798             (push (list (1+ generation) child nil subject) relations))
3799           (erase-buffer)))
3800       (kill-buffer (current-buffer)))
3801     ;; Sort over trustworthiness.
3802     (mapcar
3803      (lambda (relation)
3804        (when (gnus-dependencies-add-header
3805               (make-full-mail-header
3806                gnus-reffed-article-number
3807                (nth 3 relation) "" (or (nth 4 relation) "")
3808                (nth 1 relation)
3809                (or (nth 2 relation) "") 0 0 "")
3810               gnus-newsgroup-dependencies nil)
3811          (push gnus-reffed-article-number gnus-newsgroup-limit)
3812          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3813          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3814                gnus-newsgroup-reads)
3815          (decf gnus-reffed-article-number)))
3816      (sort relations 'car-less-than-car))
3817     (gnus-message 7 "Making sparse threads...done")))
3818
3819 (defun gnus-build-old-threads ()
3820   ;; Look at all the articles that refer back to old articles, and
3821   ;; fetch the headers for the articles that aren't there.  This will
3822   ;; build complete threads - if the roots haven't been expired by the
3823   ;; server, that is.
3824   (let ((mail-parse-charset gnus-newsgroup-charset)
3825         id heads)
3826     (mapatoms
3827      (lambda (refs)
3828        (when (not (car (symbol-value refs)))
3829          (setq heads (cdr (symbol-value refs)))
3830          (while heads
3831            (if (memq (mail-header-number (caar heads))
3832                      gnus-newsgroup-dormant)
3833                (setq heads (cdr heads))
3834              (setq id (symbol-name refs))
3835              (while (and (setq id (gnus-build-get-header id))
3836                          (not (car (gnus-id-to-thread id)))))
3837              (setq heads nil)))))
3838      gnus-newsgroup-dependencies)))
3839
3840 ;; This function has to be called with point after the article number
3841 ;; on the beginning of the line.
3842 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3843   (let ((eol (gnus-point-at-eol))
3844         (buffer (current-buffer))
3845         header references in-reply-to)
3846
3847     ;; overview: [num subject from date id refs chars lines misc]
3848     (unwind-protect
3849         (let (x)
3850           (narrow-to-region (point) eol)
3851           (unless (eobp)
3852             (forward-char))
3853
3854           (setq header
3855                 (make-full-mail-header
3856                  number                 ; number
3857                  (condition-case ()     ; subject
3858                      (funcall gnus-decode-encoded-word-function
3859                               (setq x (nnheader-nov-field)))
3860                    (error x))
3861                  (condition-case ()     ; from
3862                      (funcall gnus-decode-encoded-word-function
3863                               (setq x (nnheader-nov-field)))
3864                    (error x))
3865                  (nnheader-nov-field)   ; date
3866                  (nnheader-nov-read-message-id) ; id
3867                  (setq references (nnheader-nov-field)) ; refs
3868                  (nnheader-nov-read-integer) ; chars
3869                  (nnheader-nov-read-integer) ; lines
3870                  (unless (eobp)
3871                    (if (looking-at "Xref: ")
3872                        (goto-char (match-end 0)))
3873                    (nnheader-nov-field)) ; Xref
3874                  (nnheader-nov-parse-extra)))) ; extra
3875
3876       (widen))
3877
3878     (when (and (string= references "")
3879                (setq in-reply-to (mail-header-extra header))
3880                (setq in-reply-to (cdr (assq 'In-Reply-To in-reply-to))))
3881       (mail-header-set-references
3882        header (gnus-extract-message-id-from-in-reply-to in-reply-to)))
3883
3884     (when gnus-alter-header-function
3885       (funcall gnus-alter-header-function header))
3886     (gnus-dependencies-add-header header dependencies force-new)))
3887
3888 (defun gnus-build-get-header (id)
3889   "Look through the buffer of NOV lines and find the header to ID.
3890 Enter this line into the dependencies hash table, and return
3891 the id of the parent article (if any)."
3892   (let ((deps gnus-newsgroup-dependencies)
3893         found header)
3894     (prog1
3895         (save-excursion
3896           (set-buffer nntp-server-buffer)
3897           (let ((case-fold-search nil))
3898             (goto-char (point-min))
3899             (while (and (not found)
3900                         (search-forward id nil t))
3901               (beginning-of-line)
3902               (setq found (looking-at
3903                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3904                                    (regexp-quote id))))
3905               (or found (beginning-of-line 2)))
3906             (when found
3907               (beginning-of-line)
3908               (and
3909                (setq header (gnus-nov-parse-line
3910                              (read (current-buffer)) deps))
3911                (gnus-parent-id (mail-header-references header))))))
3912       (when header
3913         (let ((number (mail-header-number header)))
3914           (push number gnus-newsgroup-limit)
3915           (push header gnus-newsgroup-headers)
3916           (if (memq number gnus-newsgroup-unselected)
3917               (progn
3918                 (setq gnus-newsgroup-unreads
3919                       (gnus-add-to-sorted-list gnus-newsgroup-unreads
3920                                                number))
3921                 (setq gnus-newsgroup-unselected
3922                       (delq number gnus-newsgroup-unselected)))
3923             (push number gnus-newsgroup-ancient)))))))
3924
3925 (defun gnus-build-all-threads ()
3926   "Read all the headers."
3927   (let ((gnus-summary-ignore-duplicates t)
3928         (mail-parse-charset gnus-newsgroup-charset)
3929         (dependencies gnus-newsgroup-dependencies)
3930         header article)
3931     (save-excursion
3932       (set-buffer nntp-server-buffer)
3933       (let ((case-fold-search nil))
3934         (goto-char (point-min))
3935         (while (not (eobp))
3936           (ignore-errors
3937             (setq article (read (current-buffer))
3938                   header (gnus-nov-parse-line article dependencies)))
3939           (when header
3940             (save-excursion
3941               (set-buffer gnus-summary-buffer)
3942               (push header gnus-newsgroup-headers)
3943               (if (memq (setq article (mail-header-number header))
3944                         gnus-newsgroup-unselected)
3945                   (progn
3946                     (setq gnus-newsgroup-unreads
3947                           (gnus-add-to-sorted-list
3948                            gnus-newsgroup-unreads article))
3949                     (setq gnus-newsgroup-unselected
3950                           (delq article gnus-newsgroup-unselected)))
3951                 (push article gnus-newsgroup-ancient)))
3952             (forward-line 1)))))))
3953
3954 (defun gnus-summary-update-article-line (article header)
3955   "Update the line for ARTICLE using HEADERS."
3956   (let* ((id (mail-header-id header))
3957          (thread (gnus-id-to-thread id)))
3958     (unless thread
3959       (error "Article in no thread"))
3960     ;; Update the thread.
3961     (setcar thread header)
3962     (gnus-summary-goto-subject article)
3963     (let* ((datal (gnus-data-find-list article))
3964            (data (car datal))
3965            (length (when (cdr datal)
3966                      (- (gnus-data-pos data)
3967                         (gnus-data-pos (cadr datal)))))
3968            (buffer-read-only nil)
3969            (level (gnus-summary-thread-level)))
3970       (gnus-delete-line)
3971       (gnus-summary-insert-line
3972        header level nil (gnus-article-mark article)
3973        (memq article gnus-newsgroup-replied)
3974        (memq article gnus-newsgroup-expirable)
3975        ;; Only insert the Subject string when it's different
3976        ;; from the previous Subject string.
3977        (if (and
3978             gnus-show-threads
3979             (gnus-subject-equal
3980              (condition-case ()
3981                  (mail-header-subject
3982                   (gnus-data-header
3983                    (cadr
3984                     (gnus-data-find-list
3985                      article
3986                      (gnus-data-list t)))))
3987                ;; Error on the side of excessive subjects.
3988                (error ""))
3989              (mail-header-subject header)))
3990            ""
3991          (mail-header-subject header))
3992        nil (cdr (assq article gnus-newsgroup-scored))
3993        (memq article gnus-newsgroup-processable))
3994       (when length
3995         (gnus-data-update-list
3996          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3997
3998 (defun gnus-summary-update-article (article &optional iheader)
3999   "Update ARTICLE in the summary buffer."
4000   (set-buffer gnus-summary-buffer)
4001   (let* ((header (gnus-summary-article-header article))
4002          (id (mail-header-id header))
4003          (data (gnus-data-find article))
4004          (thread (gnus-id-to-thread id))
4005          (references (mail-header-references header))
4006          (parent
4007           (gnus-id-to-thread
4008            (or (gnus-parent-id
4009                 (when (and references
4010                            (not (equal "" references)))
4011                   references))
4012                "none")))
4013          (buffer-read-only nil)
4014          (old (car thread)))
4015     (when thread
4016       (unless iheader
4017         (setcar thread nil)
4018         (when parent
4019           (delq thread parent)))
4020       (if (gnus-summary-insert-subject id header)
4021        ;; Set the (possibly) new article number in the data structure.
4022           (gnus-data-set-number data (gnus-id-to-article id))
4023         (setcar thread old)
4024         nil))))
4025
4026 (defun gnus-rebuild-thread (id &optional line)
4027   "Rebuild the thread containing ID.
4028 If LINE, insert the rebuilt thread starting on line LINE."
4029   (let ((buffer-read-only nil)
4030         old-pos current thread data)
4031     (if (not gnus-show-threads)
4032         (setq thread (list (car (gnus-id-to-thread id))))
4033       ;; Get the thread this article is part of.
4034       (setq thread (gnus-remove-thread id)))
4035     (setq old-pos (gnus-point-at-bol))
4036     (setq current (save-excursion
4037                     (and (re-search-backward "[\r\n]" nil t)
4038                          (gnus-summary-article-number))))
4039     ;; If this is a gathered thread, we have to go some re-gathering.
4040     (when (stringp (car thread))
4041       (let ((subject (car thread))
4042             roots thr)
4043         (setq thread (cdr thread))
4044         (while thread
4045           (unless (memq (setq thr (gnus-id-to-thread
4046                                    (gnus-root-id
4047                                     (mail-header-id (caar thread)))))
4048                         roots)
4049             (push thr roots))
4050           (setq thread (cdr thread)))
4051         ;; We now have all (unique) roots.
4052         (if (= (length roots) 1)
4053             ;; All the loose roots are now one solid root.
4054             (setq thread (car roots))
4055           (setq thread (cons subject (gnus-sort-threads roots))))))
4056     (let (threads)
4057       ;; We then insert this thread into the summary buffer.
4058       (when line
4059         (goto-char (point-min))
4060         (forward-line (1- line)))
4061       (let (gnus-newsgroup-data gnus-newsgroup-threads)
4062         (if gnus-show-threads
4063             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
4064           (gnus-summary-prepare-unthreaded thread))
4065         (setq data (nreverse gnus-newsgroup-data))
4066         (setq threads gnus-newsgroup-threads))
4067       ;; We splice the new data into the data structure.
4068       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
4069       ;;!!! then we want to insert at the beginning of the buffer.
4070       ;;!!! That happens to be true with Gnus now, but that may
4071       ;;!!! change in the future.  Perhaps.
4072       (gnus-data-enter-list
4073        (if line nil current) data (- (point) old-pos))
4074       (setq gnus-newsgroup-threads
4075             (nconc threads gnus-newsgroup-threads))
4076       (gnus-data-compute-positions))))
4077
4078 (defun gnus-number-to-header (number)
4079   "Return the header for article NUMBER."
4080   (let ((headers gnus-newsgroup-headers))
4081     (while (and headers
4082                 (not (= number (mail-header-number (car headers)))))
4083       (pop headers))
4084     (when headers
4085       (car headers))))
4086
4087 (defun gnus-parent-headers (in-headers &optional generation)
4088   "Return the headers of the GENERATIONeth parent of HEADERS."
4089   (unless generation
4090     (setq generation 1))
4091   (let ((parent t)
4092         (headers in-headers)
4093         references)
4094     (while (and parent
4095                 (not (zerop generation))
4096                 (setq references (mail-header-references headers)))
4097       (setq headers (if (and references
4098                              (setq parent (gnus-parent-id references)))
4099                         (car (gnus-id-to-thread parent))
4100                       nil))
4101       (decf generation))
4102     (and (not (eq headers in-headers))
4103          headers)))
4104
4105 (defun gnus-id-to-thread (id)
4106   "Return the (sub-)thread where ID appears."
4107   (gnus-gethash id gnus-newsgroup-dependencies))
4108
4109 (defun gnus-id-to-article (id)
4110   "Return the article number of ID."
4111   (let ((thread (gnus-id-to-thread id)))
4112     (when (and thread
4113                (car thread))
4114       (mail-header-number (car thread)))))
4115
4116 (defun gnus-id-to-header (id)
4117   "Return the article headers of ID."
4118   (car (gnus-id-to-thread id)))
4119
4120 (defun gnus-article-displayed-root-p (article)
4121   "Say whether ARTICLE is a root(ish) article."
4122   (let ((level (gnus-summary-thread-level article))
4123         (refs (mail-header-references  (gnus-summary-article-header article)))
4124         particle)
4125     (cond
4126      ((null level) nil)
4127      ((zerop level) t)
4128      ((null refs) t)
4129      ((null (gnus-parent-id refs)) t)
4130      ((and (= 1 level)
4131            (null (setq particle (gnus-id-to-article
4132                                  (gnus-parent-id refs))))
4133            (null (gnus-summary-thread-level particle)))))))
4134
4135 (defun gnus-root-id (id)
4136   "Return the id of the root of the thread where ID appears."
4137   (let (last-id prev)
4138     (while (and id (setq prev (car (gnus-id-to-thread id))))
4139       (setq last-id id
4140             id (gnus-parent-id (mail-header-references prev))))
4141     last-id))
4142
4143 (defun gnus-articles-in-thread (thread)
4144   "Return the list of articles in THREAD."
4145   (cons (mail-header-number (car thread))
4146         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
4147
4148 (defun gnus-remove-thread (id &optional dont-remove)
4149   "Remove the thread that has ID in it."
4150   (let (headers thread last-id)
4151     ;; First go up in this thread until we find the root.
4152     (setq last-id (gnus-root-id id)
4153           headers (message-flatten-list (gnus-id-to-thread last-id)))
4154     ;; We have now found the real root of this thread.  It might have
4155     ;; been gathered into some loose thread, so we have to search
4156     ;; through the threads to find the thread we wanted.
4157     (let ((threads gnus-newsgroup-threads)
4158           sub)
4159       (while threads
4160         (setq sub (car threads))
4161         (if (stringp (car sub))
4162             ;; This is a gathered thread, so we look at the roots
4163             ;; below it to find whether this article is in this
4164             ;; gathered root.
4165             (progn
4166               (setq sub (cdr sub))
4167               (while sub
4168                 (when (member (caar sub) headers)
4169                   (setq thread (car threads)
4170                         threads nil
4171                         sub nil))
4172                 (setq sub (cdr sub))))
4173           ;; It's an ordinary thread, so we check it.
4174           (when (eq (car sub) (car headers))
4175             (setq thread sub
4176                   threads nil)))
4177         (setq threads (cdr threads)))
4178       ;; If this article is in no thread, then it's a root.
4179       (if thread
4180           (unless dont-remove
4181             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
4182         (setq thread (gnus-id-to-thread last-id)))
4183       (when thread
4184         (prog1
4185             thread                      ; We return this thread.
4186           (unless dont-remove
4187             (if (stringp (car thread))
4188                 (progn
4189                   ;; If we use dummy roots, then we have to remove the
4190                   ;; dummy root as well.
4191                   (when (eq gnus-summary-make-false-root 'dummy)
4192                     ;; We go to the dummy root by going to
4193                     ;; the first sub-"thread", and then one line up.
4194                     (gnus-summary-goto-article
4195                      (mail-header-number (caadr thread)))
4196                     (forward-line -1)
4197                     (gnus-delete-line)
4198                     (gnus-data-compute-positions))
4199                   (setq thread (cdr thread))
4200                   (while thread
4201                     (gnus-remove-thread-1 (car thread))
4202                     (setq thread (cdr thread))))
4203               (gnus-remove-thread-1 thread))))))))
4204
4205 (defun gnus-remove-thread-1 (thread)
4206   "Remove the thread THREAD recursively."
4207   (let ((number (mail-header-number (pop thread)))
4208         d)
4209     (setq thread (reverse thread))
4210     (while thread
4211       (gnus-remove-thread-1 (pop thread)))
4212     (when (setq d (gnus-data-find number))
4213       (goto-char (gnus-data-pos d))
4214       (gnus-summary-show-thread)
4215       (gnus-data-remove
4216        number
4217        (- (gnus-point-at-bol)
4218           (prog1
4219               (1+ (gnus-point-at-eol))
4220             (gnus-delete-line)))))))
4221
4222 (defun gnus-sort-threads-1 (threads func)
4223   (sort (mapcar (lambda (thread)
4224                   (cons (car thread)
4225                         (and (cdr thread)
4226                              (gnus-sort-threads-1 (cdr thread) func))))
4227                 threads) func))
4228
4229 (defun gnus-sort-threads (threads)
4230   "Sort THREADS."
4231   (if (not gnus-thread-sort-functions)
4232       threads
4233     (gnus-message 8 "Sorting threads...")
4234     (prog1
4235         (gnus-sort-threads-1
4236          threads
4237          (gnus-make-sort-function gnus-thread-sort-functions))
4238       (gnus-message 8 "Sorting threads...done"))))
4239
4240 (defun gnus-sort-articles (articles)
4241   "Sort ARTICLES."
4242   (when gnus-article-sort-functions
4243     (gnus-message 7 "Sorting articles...")
4244     (prog1
4245         (setq gnus-newsgroup-headers
4246               (sort articles (gnus-make-sort-function
4247                               gnus-article-sort-functions)))
4248       (gnus-message 7 "Sorting articles...done"))))
4249
4250 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
4251 (defmacro gnus-thread-header (thread)
4252   "Return header of first article in THREAD.
4253 Note that THREAD must never, ever be anything else than a variable -
4254 using some other form will lead to serious barfage."
4255   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4256   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4257   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4258         (vector thread) 2))
4259
4260 (defsubst gnus-article-sort-by-number (h1 h2)
4261   "Sort articles by article number."
4262   (< (mail-header-number h1)
4263      (mail-header-number h2)))
4264
4265 (defun gnus-thread-sort-by-number (h1 h2)
4266   "Sort threads by root article number."
4267   (gnus-article-sort-by-number
4268    (gnus-thread-header h1) (gnus-thread-header h2)))
4269
4270 (defsubst gnus-article-sort-by-random (h1 h2)
4271   "Sort articles by article number."
4272   (zerop (random 2)))
4273
4274 (defun gnus-thread-sort-by-random (h1 h2)
4275   "Sort threads by root article number."
4276   (gnus-article-sort-by-random
4277    (gnus-thread-header h1) (gnus-thread-header h2)))
4278
4279 (defsubst gnus-article-sort-by-lines (h1 h2)
4280   "Sort articles by article Lines header."
4281   (< (mail-header-lines h1)
4282      (mail-header-lines h2)))
4283
4284 (defun gnus-thread-sort-by-lines (h1 h2)
4285   "Sort threads by root article Lines header."
4286   (gnus-article-sort-by-lines
4287    (gnus-thread-header h1) (gnus-thread-header h2)))
4288
4289 (defsubst gnus-article-sort-by-chars (h1 h2)
4290   "Sort articles by octet length."
4291   (< (mail-header-chars h1)
4292      (mail-header-chars h2)))
4293
4294 (defun gnus-thread-sort-by-chars (h1 h2)
4295   "Sort threads by root article octet length."
4296   (gnus-article-sort-by-chars
4297    (gnus-thread-header h1) (gnus-thread-header h2)))
4298
4299 (defsubst gnus-article-sort-by-author (h1 h2)
4300   "Sort articles by root author."
4301   (string-lessp
4302    (let ((extract (funcall
4303                    gnus-extract-address-components
4304                    (mail-header-from h1))))
4305      (or (car extract) (cadr extract) ""))
4306    (let ((extract (funcall
4307                    gnus-extract-address-components
4308                    (mail-header-from h2))))
4309      (or (car extract) (cadr extract) ""))))
4310
4311 (defun gnus-thread-sort-by-author (h1 h2)
4312   "Sort threads by root author."
4313   (gnus-article-sort-by-author
4314    (gnus-thread-header h1)  (gnus-thread-header h2)))
4315
4316 (defsubst gnus-article-sort-by-subject (h1 h2)
4317   "Sort articles by root subject."
4318   (string-lessp
4319    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4320    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4321
4322 (defun gnus-thread-sort-by-subject (h1 h2)
4323   "Sort threads by root subject."
4324   (gnus-article-sort-by-subject
4325    (gnus-thread-header h1) (gnus-thread-header h2)))
4326
4327 (defsubst gnus-article-sort-by-date (h1 h2)
4328   "Sort articles by root article date."
4329   (time-less-p
4330    (gnus-date-get-time (mail-header-date h1))
4331    (gnus-date-get-time (mail-header-date h2))))
4332
4333 (defun gnus-thread-sort-by-date (h1 h2)
4334   "Sort threads by root article date."
4335   (gnus-article-sort-by-date
4336    (gnus-thread-header h1) (gnus-thread-header h2)))
4337
4338 (defsubst gnus-article-sort-by-score (h1 h2)
4339   "Sort articles by root article score.
4340 Unscored articles will be counted as having a score of zero."
4341   (> (or (cdr (assq (mail-header-number h1)
4342                     gnus-newsgroup-scored))
4343          gnus-summary-default-score 0)
4344      (or (cdr (assq (mail-header-number h2)
4345                     gnus-newsgroup-scored))
4346          gnus-summary-default-score 0)))
4347
4348 (defun gnus-thread-sort-by-score (h1 h2)
4349   "Sort threads by root article score."
4350   (gnus-article-sort-by-score
4351    (gnus-thread-header h1) (gnus-thread-header h2)))
4352
4353 (defun gnus-thread-sort-by-total-score (h1 h2)
4354   "Sort threads by the sum of all scores in the thread.
4355 Unscored articles will be counted as having a score of zero."
4356   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4357
4358 (defun gnus-thread-total-score (thread)
4359   ;; This function find the total score of THREAD.
4360   (cond
4361    ((null thread)
4362     0)
4363    ((consp thread)
4364     (if (stringp (car thread))
4365         (apply gnus-thread-score-function 0
4366                (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4367       (gnus-thread-total-score-1 thread)))
4368    (t
4369     (gnus-thread-total-score-1 (list thread)))))
4370
4371 (defun gnus-thread-sort-by-most-recent-number (h1 h2)
4372   "Sort threads such that the thread with the most recently arrived article comes first."
4373   (> (gnus-thread-highest-number h1) (gnus-thread-highest-number h2)))
4374
4375 (defun gnus-thread-highest-number (thread)
4376   "Return the highest article number in THREAD."
4377   (apply 'max (mapcar (lambda (header)
4378                         (mail-header-number header))
4379                       (message-flatten-list thread))))
4380
4381 (defun gnus-thread-sort-by-most-recent-date (h1 h2)
4382   "Sort threads such that the thread with the most recently dated article comes first."
4383   (> (gnus-thread-latest-date h1) (gnus-thread-latest-date h2)))
4384
4385 (defun gnus-thread-latest-date (thread)
4386   "Return the highest article date in THREAD."
4387   (let ((previous-time 0))
4388     (apply 'max (mapcar
4389                  (lambda (header)
4390                    (setq previous-time
4391                          (time-to-seconds
4392                           (mail-header-parse-date
4393                            (condition-case ()
4394                                (mail-header-date header)
4395                              (error previous-time))))))
4396                  (sort
4397                   (message-flatten-list thread)
4398                   (lambda (h1 h2)
4399                     (< (mail-header-number h1)
4400                        (mail-header-number h2))))))))
4401
4402 (defun gnus-thread-total-score-1 (root)
4403   ;; This function find the total score of the thread below ROOT.
4404   (setq root (car root))
4405   (apply gnus-thread-score-function
4406          (or (append
4407               (mapcar 'gnus-thread-total-score
4408                       (cdr (gnus-id-to-thread (mail-header-id root))))
4409               (when (> (mail-header-number root) 0)
4410                 (list (or (cdr (assq (mail-header-number root)
4411                                      gnus-newsgroup-scored))
4412                           gnus-summary-default-score 0))))
4413              (list gnus-summary-default-score)
4414              '(0))))
4415
4416 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4417 (defvar gnus-tmp-prev-subject nil)
4418 (defvar gnus-tmp-false-parent nil)
4419 (defvar gnus-tmp-root-expunged nil)
4420 (defvar gnus-tmp-dummy-line nil)
4421
4422 (eval-when-compile (defvar gnus-tmp-header))
4423 (defun gnus-extra-header (type &optional header)
4424   "Return the extra header of TYPE."
4425   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4426       ""))
4427
4428 (defvar gnus-tmp-thread-tree-header-string "")
4429
4430 (defcustom gnus-sum-thread-tree-root "> "
4431   "With %B spec, used for the root of a thread.
4432 If nil, use subject instead."
4433   :type 'string
4434   :group 'gnus-thread)
4435 (defcustom gnus-sum-thread-tree-single-indent ""
4436   "With %B spec, used for a thread with just one message.
4437 If nil, use subject instead."
4438   :type 'string
4439   :group 'gnus-thread)
4440 (defcustom gnus-sum-thread-tree-vertical "| "
4441   "With %B spec, used for drawing a vertical line."
4442   :type 'string
4443   :group 'gnus-thread)
4444 (defcustom gnus-sum-thread-tree-indent "  "
4445   "With %B spec, used for indenting."
4446   :type 'string
4447   :group 'gnus-thread)
4448 (defcustom gnus-sum-thread-tree-leaf-with-other "+-> "
4449   "With %B spec, used for a leaf with brothers."
4450   :type 'string
4451   :group 'gnus-thread)
4452 (defcustom gnus-sum-thread-tree-single-leaf "\\-> "
4453   "With %B spec, used for a leaf without brothers."
4454   :type 'string
4455   :group 'gnus-thread)
4456
4457 (defun gnus-summary-prepare-threads (threads)
4458   "Prepare summary buffer from THREADS and indentation LEVEL.
4459 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4460 or a straight list of headers."
4461   (gnus-message 7 "Generating summary...")
4462
4463   (setq gnus-newsgroup-threads threads)
4464   (beginning-of-line)
4465
4466   (let ((gnus-tmp-level 0)
4467         (default-score (or gnus-summary-default-score 0))
4468         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4469         thread number subject stack state gnus-tmp-gathered beg-match
4470         new-roots gnus-tmp-new-adopts thread-end simp-subject
4471         gnus-tmp-header gnus-tmp-unread
4472         gnus-tmp-replied gnus-tmp-subject-or-nil
4473         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4474         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4475         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4476         tree-stack)
4477
4478     (setq gnus-tmp-prev-subject nil)
4479
4480     (if (vectorp (car threads))
4481         ;; If this is a straight (sic) list of headers, then a
4482         ;; threaded summary display isn't required, so we just create
4483         ;; an unthreaded one.
4484         (gnus-summary-prepare-unthreaded threads)
4485
4486       ;; Do the threaded display.
4487
4488       (while (or threads stack gnus-tmp-new-adopts new-roots)
4489
4490         (if (and (= gnus-tmp-level 0)
4491                  (or (not stack)
4492                      (= (caar stack) 0))
4493                  (not gnus-tmp-false-parent)
4494                  (or gnus-tmp-new-adopts new-roots))
4495             (if gnus-tmp-new-adopts
4496                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4497                       thread (list (car gnus-tmp-new-adopts))
4498                       gnus-tmp-header (caar thread)
4499                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4500               (when new-roots
4501                 (setq thread (list (car new-roots))
4502                       gnus-tmp-header (caar thread)
4503                       new-roots (cdr new-roots))))
4504
4505           (if threads
4506               ;; If there are some threads, we do them before the
4507               ;; threads on the stack.
4508               (setq thread threads
4509                     gnus-tmp-header (caar thread))
4510             ;; There were no current threads, so we pop something off
4511             ;; the stack.
4512             (setq state (car stack)
4513                   gnus-tmp-level (car state)
4514                   tree-stack (cadr state)
4515                   thread (caddr state)
4516                   stack (cdr stack)
4517                   gnus-tmp-header (caar thread))))
4518
4519         (setq gnus-tmp-false-parent nil)
4520         (setq gnus-tmp-root-expunged nil)
4521         (setq thread-end nil)
4522
4523         (if (stringp gnus-tmp-header)
4524             ;; The header is a dummy root.
4525             (cond
4526              ((eq gnus-summary-make-false-root 'adopt)
4527               ;; We let the first article adopt the rest.
4528               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4529                                                (cddar thread)))
4530               (setq gnus-tmp-gathered
4531                     (nconc (mapcar
4532                             (lambda (h) (mail-header-number (car h)))
4533                             (cddar thread))
4534                            gnus-tmp-gathered))
4535               (setq thread (cons (list (caar thread)
4536                                        (cadar thread))
4537                                  (cdr thread)))
4538               (setq gnus-tmp-level -1
4539                     gnus-tmp-false-parent t))
4540              ((eq gnus-summary-make-false-root 'empty)
4541               ;; We print adopted articles with empty subject fields.
4542               (setq gnus-tmp-gathered
4543                     (nconc (mapcar
4544                             (lambda (h) (mail-header-number (car h)))
4545                             (cddar thread))
4546                            gnus-tmp-gathered))
4547               (setq gnus-tmp-level -1))
4548              ((eq gnus-summary-make-false-root 'dummy)
4549               ;; We remember that we probably want to output a dummy
4550               ;; root.
4551               (setq gnus-tmp-dummy-line gnus-tmp-header)
4552               (setq gnus-tmp-prev-subject gnus-tmp-header))
4553              (t
4554               ;; We do not make a root for the gathered
4555               ;; sub-threads at all.
4556               (setq gnus-tmp-level -1)))
4557
4558           (setq number (mail-header-number gnus-tmp-header)
4559                 subject (mail-header-subject gnus-tmp-header)
4560                 simp-subject (gnus-simplify-subject-fully subject))
4561
4562           (cond
4563            ;; If the thread has changed subject, we might want to make
4564            ;; this subthread into a root.
4565            ((and (null gnus-thread-ignore-subject)
4566                  (not (zerop gnus-tmp-level))
4567                  gnus-tmp-prev-subject
4568                  (not (string= gnus-tmp-prev-subject simp-subject)))
4569             (setq new-roots (nconc new-roots (list (car thread)))
4570                   thread-end t
4571                   gnus-tmp-header nil))
4572            ;; If the article lies outside the current limit,
4573            ;; then we do not display it.
4574            ((not (memq number gnus-newsgroup-limit))
4575             (setq gnus-tmp-gathered
4576                   (nconc (mapcar
4577                           (lambda (h) (mail-header-number (car h)))
4578                           (cdar thread))
4579                          gnus-tmp-gathered))
4580             (setq gnus-tmp-new-adopts (if (cdar thread)
4581                                           (append gnus-tmp-new-adopts
4582                                                   (cdar thread))
4583                                         gnus-tmp-new-adopts)
4584                   thread-end t
4585                   gnus-tmp-header nil)
4586             (when (zerop gnus-tmp-level)
4587               (setq gnus-tmp-root-expunged t)))
4588            ;; Perhaps this article is to be marked as read?
4589            ((and gnus-summary-mark-below
4590                  (< (or (cdr (assq number gnus-newsgroup-scored))
4591                         default-score)
4592                     gnus-summary-mark-below)
4593                  ;; Don't touch sparse articles.
4594                  (not (gnus-summary-article-sparse-p number))
4595                  (not (gnus-summary-article-ancient-p number)))
4596             (setq gnus-newsgroup-unreads
4597                   (delq number gnus-newsgroup-unreads))
4598             (if gnus-newsgroup-auto-expire
4599                 (setq gnus-newsgroup-expirable
4600                       (gnus-add-to-sorted-list
4601                        gnus-newsgroup-expirable number))
4602               (push (cons number gnus-low-score-mark)
4603                     gnus-newsgroup-reads))))
4604
4605           (when gnus-tmp-header
4606             ;; We may have an old dummy line to output before this
4607             ;; article.
4608             (when (and gnus-tmp-dummy-line
4609                        (gnus-subject-equal
4610                         gnus-tmp-dummy-line
4611                         (mail-header-subject gnus-tmp-header)))
4612               (gnus-summary-insert-dummy-line
4613                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4614               (setq gnus-tmp-dummy-line nil))
4615
4616             ;; Compute the mark.
4617             (setq gnus-tmp-unread (gnus-article-mark number))
4618
4619             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4620                                   gnus-tmp-header gnus-tmp-level)
4621                   gnus-newsgroup-data)
4622
4623             ;; Actually insert the line.
4624             (setq
4625              gnus-tmp-subject-or-nil
4626              (cond
4627               ((and gnus-thread-ignore-subject
4628                     gnus-tmp-prev-subject
4629                     (not (string= gnus-tmp-prev-subject simp-subject)))
4630                subject)
4631               ((zerop gnus-tmp-level)
4632                (if (and (eq gnus-summary-make-false-root 'empty)
4633                         (memq number gnus-tmp-gathered)
4634                         gnus-tmp-prev-subject
4635                         (string= gnus-tmp-prev-subject simp-subject))
4636                    gnus-summary-same-subject
4637                  subject))
4638               (t gnus-summary-same-subject)))
4639             (if (and (eq gnus-summary-make-false-root 'adopt)
4640                      (= gnus-tmp-level 1)
4641                      (memq number gnus-tmp-gathered))
4642                 (setq gnus-tmp-opening-bracket ?\<
4643                       gnus-tmp-closing-bracket ?\>)
4644               (setq gnus-tmp-opening-bracket ?\[
4645                     gnus-tmp-closing-bracket ?\]))
4646             (setq
4647              gnus-tmp-indentation
4648              (aref gnus-thread-indent-array gnus-tmp-level)
4649              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4650              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4651                                 gnus-summary-default-score 0)
4652              gnus-tmp-score-char
4653              (if (or (null gnus-summary-default-score)
4654                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4655                          gnus-summary-zcore-fuzz))
4656                  ?                      ;Whitespace
4657                (if (< gnus-tmp-score gnus-summary-default-score)
4658                    gnus-score-below-mark gnus-score-over-mark))
4659              gnus-tmp-replied
4660              (cond ((memq number gnus-newsgroup-processable)
4661                     gnus-process-mark)
4662                    ((memq number gnus-newsgroup-cached)
4663                     gnus-cached-mark)
4664                    ((memq number gnus-newsgroup-replied)
4665                     gnus-replied-mark)
4666                    ((memq number gnus-newsgroup-forwarded)
4667                     gnus-forwarded-mark)
4668                    ((memq number gnus-newsgroup-saved)
4669                     gnus-saved-mark)
4670                    ((memq number gnus-newsgroup-recent)
4671                     gnus-recent-mark)
4672                    ((memq number gnus-newsgroup-unseen)
4673                     gnus-unseen-mark)
4674                    (t gnus-no-mark))
4675              gnus-tmp-from (mail-header-from gnus-tmp-header)
4676              gnus-tmp-name
4677              (cond
4678               ((string-match "<[^>]+> *$" gnus-tmp-from)
4679                (setq beg-match (match-beginning 0))
4680                (or (and (string-match "^\".+\"" gnus-tmp-from)
4681                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4682                    (substring gnus-tmp-from 0 beg-match)))
4683               ((string-match "(.+)" gnus-tmp-from)
4684                (substring gnus-tmp-from
4685                           (1+ (match-beginning 0)) (1- (match-end 0))))
4686               (t gnus-tmp-from))
4687              gnus-tmp-thread-tree-header-string
4688              (cond
4689               ((not gnus-show-threads) "")
4690               ((zerop gnus-tmp-level)
4691                (if (cdar thread)
4692                    (or gnus-sum-thread-tree-root subject)
4693                  (or gnus-sum-thread-tree-single-indent subject)))
4694               (t
4695                (concat (apply 'concat
4696                               (mapcar (lambda (item)
4697                                         (if (= item 1)
4698                                             gnus-sum-thread-tree-vertical
4699                                           gnus-sum-thread-tree-indent))
4700                                       (cdr (reverse tree-stack))))
4701                        (if (nth 1 thread)
4702                            gnus-sum-thread-tree-leaf-with-other
4703                          gnus-sum-thread-tree-single-leaf)))))
4704             (when (string= gnus-tmp-name "")
4705               (setq gnus-tmp-name gnus-tmp-from))
4706             (unless (numberp gnus-tmp-lines)
4707               (setq gnus-tmp-lines -1))
4708             (if (= gnus-tmp-lines -1)
4709                 (setq gnus-tmp-lines "?")
4710               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4711             (gnus-put-text-property
4712              (point)
4713              (progn (eval gnus-summary-line-format-spec) (point))
4714              'gnus-number number)
4715             (when gnus-visual-p
4716               (forward-line -1)
4717               (gnus-run-hooks 'gnus-summary-update-hook)
4718               (forward-line 1))
4719
4720             (setq gnus-tmp-prev-subject simp-subject)))
4721
4722         (when (nth 1 thread)
4723           (push (list (max 0 gnus-tmp-level)
4724                       (copy-list tree-stack)
4725                       (nthcdr 1 thread))
4726                 stack))
4727         (push (if (nth 1 thread) 1 0) tree-stack)
4728         (incf gnus-tmp-level)
4729         (setq threads (if thread-end nil (cdar thread)))
4730         (unless threads
4731           (setq gnus-tmp-level 0)))))
4732   (gnus-message 7 "Generating summary...done"))
4733
4734 (defun gnus-summary-prepare-unthreaded (headers)
4735   "Generate an unthreaded summary buffer based on HEADERS."
4736   (let (header number mark)
4737
4738     (beginning-of-line)
4739
4740     (while headers
4741       ;; We may have to root out some bad articles...
4742       (when (memq (setq number (mail-header-number
4743                                 (setq header (pop headers))))
4744                   gnus-newsgroup-limit)
4745         ;; Mark article as read when it has a low score.
4746         (when (and gnus-summary-mark-below
4747                    (< (or (cdr (assq number gnus-newsgroup-scored))
4748                           gnus-summary-default-score 0)
4749                       gnus-summary-mark-below)
4750                    (not (gnus-summary-article-ancient-p number)))
4751           (setq gnus-newsgroup-unreads
4752                 (delq number gnus-newsgroup-unreads))
4753           (if gnus-newsgroup-auto-expire
4754               (push number gnus-newsgroup-expirable)
4755             (push (cons number gnus-low-score-mark)
4756                   gnus-newsgroup-reads)))
4757
4758         (setq mark (gnus-article-mark number))
4759         (push (gnus-data-make number mark (1+ (point)) header 0)
4760               gnus-newsgroup-data)
4761         (gnus-summary-insert-line
4762          header 0 number
4763          mark (memq number gnus-newsgroup-replied)
4764          (memq number gnus-newsgroup-expirable)
4765          (mail-header-subject header) nil
4766          (cdr (assq number gnus-newsgroup-scored))
4767          (memq number gnus-newsgroup-processable))))))
4768
4769 (defun gnus-summary-remove-list-identifiers ()
4770   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4771   (let ((regexp (if (consp gnus-list-identifiers)
4772                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4773                   gnus-list-identifiers))
4774         changed subject)
4775     (when regexp
4776       (dolist (header gnus-newsgroup-headers)
4777         (setq subject (mail-header-subject header)
4778               changed nil)
4779         (while (string-match
4780                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4781                 subject)
4782           (setq subject
4783                 (concat (substring subject 0 (match-beginning 2))
4784                         (substring subject (match-end 0)))
4785                 changed t))
4786         (when (and changed
4787                    (string-match
4788                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4789           (setq subject
4790                 (concat (substring subject 0 (match-beginning 1))
4791                         (substring subject (match-end 1)))))
4792         (when changed
4793           (mail-header-set-subject header subject))))))
4794
4795 (defun gnus-fetch-headers (articles)
4796   "Fetch headers of ARTICLES."
4797   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4798     (gnus-message 5 "Fetching headers for %s..." name)
4799     (prog1
4800         (if (eq 'nov
4801                 (setq gnus-headers-retrieved-by
4802                       (gnus-retrieve-headers
4803                        articles gnus-newsgroup-name
4804                        ;; We might want to fetch old headers, but
4805                        ;; not if there is only 1 article.
4806                        (and (or (and
4807                                  (not (eq gnus-fetch-old-headers 'some))
4808                                  (not (numberp gnus-fetch-old-headers)))
4809                                 (> (length articles) 1))
4810                             gnus-fetch-old-headers))))
4811             (gnus-get-newsgroup-headers-xover
4812              articles nil nil gnus-newsgroup-name t)
4813           (gnus-get-newsgroup-headers))
4814       (gnus-message 5 "Fetching headers for %s...done" name))))
4815
4816 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4817   "Select newsgroup GROUP.
4818 If READ-ALL is non-nil, all articles in the group are selected.
4819 If SELECT-ARTICLES, only select those articles from GROUP."
4820   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4821          ;;!!! Dirty hack; should be removed.
4822          (gnus-summary-ignore-duplicates
4823           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4824               t
4825             gnus-summary-ignore-duplicates))
4826          (info (nth 2 entry))
4827          articles fetched-articles cached)
4828
4829     (unless (gnus-check-server
4830              (set (make-local-variable 'gnus-current-select-method)
4831                   (gnus-find-method-for-group group)))
4832       (error "Couldn't open server"))
4833
4834     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4835         (gnus-activate-group group)     ; Or we can activate it...
4836         (progn                          ; Or we bug out.
4837           (when (equal major-mode 'gnus-summary-mode)
4838             (kill-buffer (current-buffer)))
4839           (error "Couldn't activate group %s: %s"
4840                  group (gnus-status-message group))))
4841
4842     (unless (gnus-request-group group t)
4843       (when (equal major-mode 'gnus-summary-mode)
4844         (kill-buffer (current-buffer)))
4845       (error "Couldn't request group %s: %s"
4846              group (gnus-status-message group)))
4847
4848     (setq gnus-newsgroup-name group
4849           gnus-newsgroup-unselected nil
4850           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4851
4852     (let ((display (gnus-group-find-parameter group 'display)))
4853       (setq gnus-newsgroup-display
4854             (cond
4855              ((not (zerop (or (car-safe read-all) 0)))
4856               ;; The user entered the group with C-u SPC/RET, let's show
4857               ;; all articles.
4858               'gnus-not-ignore)
4859              ((eq display 'all)
4860               'gnus-not-ignore)
4861              ((arrayp display)
4862               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4863              ((numberp display)
4864               ;; The following is probably the "correct" solution, but
4865               ;; it makes Gnus fetch all headers and then limit the
4866               ;; articles (which is slow), so instead we hack the
4867               ;; select-articles parameter instead. -- Simon Josefsson
4868               ;; <jas@kth.se>
4869               ;;
4870               ;; (gnus-byte-compile
4871               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4872               ;;                         display)))))
4873               (setq select-articles
4874                     (gnus-uncompress-range
4875                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4876                              (if (> tmp 0)
4877                                  tmp
4878                                1))
4879                            (cdr (gnus-active group)))))
4880               nil)
4881              (t
4882               nil))))
4883
4884     (gnus-summary-setup-default-charset)
4885
4886     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4887     (when (gnus-virtual-group-p group)
4888       (setq cached gnus-newsgroup-cached))
4889
4890     (setq gnus-newsgroup-unreads
4891           (gnus-sorted-ndifference
4892            (gnus-sorted-ndifference gnus-newsgroup-unreads
4893                                     gnus-newsgroup-marked)
4894            gnus-newsgroup-dormant))
4895
4896     (setq gnus-newsgroup-processable nil)
4897
4898     (gnus-update-read-articles group gnus-newsgroup-unreads)
4899
4900     ;; Adjust and set lists of article marks.
4901     (when info
4902       (gnus-adjust-marked-articles info))
4903
4904     (if (setq articles select-articles)
4905         (setq gnus-newsgroup-unselected
4906               (gnus-sorted-difference gnus-newsgroup-unreads articles))
4907       (setq articles (gnus-articles-to-read group read-all)))
4908
4909     (cond
4910      ((null articles)
4911       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4912       'quit)
4913      ((eq articles 0) nil)
4914      (t
4915       ;; Init the dependencies hash table.
4916       (setq gnus-newsgroup-dependencies
4917             (gnus-make-hashtable (length articles)))
4918       (gnus-set-global-variables)
4919       ;; Retrieve the headers and read them in.
4920       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4921
4922       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4923       (when cached
4924         (setq gnus-newsgroup-cached cached))
4925
4926       ;; Suppress duplicates?
4927       (when gnus-suppress-duplicates
4928         (gnus-dup-suppress-articles))
4929
4930       ;; Set the initial limit.
4931       (setq gnus-newsgroup-limit (copy-sequence articles))
4932       ;; Remove canceled articles from the list of unread articles.
4933       (setq fetched-articles
4934             (mapcar (lambda (headers) (mail-header-number headers))
4935                     gnus-newsgroup-headers))
4936       (setq gnus-newsgroup-articles fetched-articles)
4937       (setq gnus-newsgroup-unreads
4938             (gnus-sorted-nintersection
4939              gnus-newsgroup-unreads fetched-articles))
4940       (gnus-compute-unseen-list)
4941
4942       ;; Removed marked articles that do not exist.
4943       (gnus-update-missing-marks
4944        (gnus-sorted-difference articles fetched-articles))
4945       ;; We might want to build some more threads first.
4946       (when (and gnus-fetch-old-headers
4947                  (eq gnus-headers-retrieved-by 'nov))
4948         (if (eq gnus-fetch-old-headers 'invisible)
4949             (gnus-build-all-threads)
4950           (gnus-build-old-threads)))
4951       ;; Let the Gnus agent mark articles as read.
4952       (when gnus-agent
4953         (gnus-agent-get-undownloaded-list))
4954       ;; Remove list identifiers from subject
4955       (when gnus-list-identifiers
4956         (gnus-summary-remove-list-identifiers))
4957       ;; Check whether auto-expire is to be done in this group.
4958       (setq gnus-newsgroup-auto-expire
4959             (gnus-group-auto-expirable-p group))
4960       ;; Set up the article buffer now, if necessary.
4961       (unless gnus-single-article-buffer
4962         (gnus-article-setup-buffer))
4963       ;; First and last article in this newsgroup.
4964       (when gnus-newsgroup-headers
4965         (setq gnus-newsgroup-begin
4966               (mail-header-number (car gnus-newsgroup-headers))
4967               gnus-newsgroup-end
4968               (mail-header-number
4969                (gnus-last-element gnus-newsgroup-headers))))
4970       ;; GROUP is successfully selected.
4971       (or gnus-newsgroup-headers t)))))
4972
4973 (defun gnus-compute-unseen-list ()
4974   ;; The `seen' marks are treated specially.
4975   (if (not gnus-newsgroup-seen)
4976       (setq gnus-newsgroup-unseen gnus-newsgroup-articles)
4977     (setq gnus-newsgroup-unseen
4978           (gnus-inverse-list-range-intersection
4979            gnus-newsgroup-articles gnus-newsgroup-seen))))
4980
4981 (defun gnus-summary-display-make-predicate (display)
4982   (require 'gnus-agent)
4983   (when (= (length display) 1)
4984     (setq display (car display)))
4985   (unless gnus-summary-display-cache
4986     (dolist (elem (append '((unread . unread)
4987                             (read . read)
4988                             (unseen . unseen))
4989                           gnus-article-mark-lists))
4990       (push (cons (cdr elem)
4991                   (gnus-byte-compile
4992                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4993             gnus-summary-display-cache)))
4994   (let ((gnus-category-predicate-alist gnus-summary-display-cache)
4995         (gnus-category-predicate-cache gnus-summary-display-cache))
4996     (gnus-get-predicate display)))
4997
4998 ;; Uses the dynamically bound `number' variable.
4999 (defvar number)
5000 (defun gnus-article-marked-p (type &optional article)
5001   (let ((article (or article number)))
5002     (cond
5003      ((eq type 'tick)
5004       (memq article gnus-newsgroup-marked))
5005      ((eq type 'spam)
5006       (memq article gnus-newsgroup-spam-marked))
5007      ((eq type 'unsend)
5008       (memq article gnus-newsgroup-unsendable))
5009      ((eq type 'undownload)
5010       (memq article gnus-newsgroup-undownloaded))
5011      ((eq type 'download)
5012       (memq article gnus-newsgroup-downloadable))
5013      ((eq type 'unread)
5014       (memq article gnus-newsgroup-unreads))
5015      ((eq type 'read)
5016       (memq article gnus-newsgroup-reads))
5017      ((eq type 'dormant)
5018       (memq article gnus-newsgroup-dormant) )
5019      ((eq type 'expire)
5020       (memq article gnus-newsgroup-expirable))
5021      ((eq type 'reply)
5022       (memq article gnus-newsgroup-replied))
5023      ((eq type 'killed)
5024       (memq article gnus-newsgroup-killed))
5025      ((eq type 'bookmark)
5026       (assq article gnus-newsgroup-bookmarks))
5027      ((eq type 'score)
5028       (assq article gnus-newsgroup-scored))
5029      ((eq type 'save)
5030       (memq article gnus-newsgroup-saved))
5031      ((eq type 'cache)
5032       (memq article gnus-newsgroup-cached))
5033      ((eq type 'forward)
5034       (memq article gnus-newsgroup-forwarded))
5035      ((eq type 'seen)
5036       (not (memq article gnus-newsgroup-unseen)))
5037      ((eq type 'recent)
5038       (memq article gnus-newsgroup-recent))
5039      (t t))))
5040
5041 (defun gnus-articles-to-read (group &optional read-all)
5042   "Find out what articles the user wants to read."
5043   (let* ((articles
5044           ;; Select all articles if `read-all' is non-nil, or if there
5045           ;; are no unread articles.
5046           (if (or read-all
5047                   (and (zerop (length gnus-newsgroup-marked))
5048                        (zerop (length gnus-newsgroup-unreads)))
5049                   ;; Fetch all if the predicate is non-nil.
5050                   gnus-newsgroup-display)
5051               ;; We want to select the headers for all the articles in
5052               ;; the group, so we select either all the active
5053               ;; articles in the group, or (if that's nil), the
5054               ;; articles in the cache.
5055               (or
5056                (gnus-uncompress-range (gnus-active group))
5057                (gnus-cache-articles-in-group group))
5058             ;; Select only the "normal" subset of articles.
5059             (gnus-sorted-nunion
5060              (gnus-sorted-union gnus-newsgroup-dormant gnus-newsgroup-marked)
5061              gnus-newsgroup-unreads)))
5062          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
5063          (scored (length scored-list))
5064          (number (length articles))
5065          (marked (+ (length gnus-newsgroup-marked)
5066                     (length gnus-newsgroup-dormant)))
5067          (select
5068           (cond
5069            ((numberp read-all)
5070             read-all)
5071            ((numberp gnus-newsgroup-display)
5072             gnus-newsgroup-display)
5073            (t
5074             (condition-case ()
5075                 (cond
5076                  ((and (or (<= scored marked) (= scored number))
5077                        (numberp gnus-large-newsgroup)
5078                        (> number gnus-large-newsgroup))
5079                   (let* ((cursor-in-echo-area nil)
5080                          (initial (gnus-parameter-large-newsgroup-initial
5081                                    gnus-newsgroup-name))
5082                          (input
5083                           (read-string
5084                            (format
5085                             "How many articles from %s (%s %d): "
5086                             (gnus-limit-string
5087                              (gnus-group-decoded-name gnus-newsgroup-name)
5088                              35)
5089                             (if initial "max" "default")
5090                             number)
5091                            (if initial
5092                                (cons (number-to-string initial)
5093                                      0)))))
5094                     (if (string-match "^[ \t]*$" input) number input)))
5095                  ((and (> scored marked) (< scored number)
5096                        (> (- scored number) 20))
5097                   (let ((input
5098                          (read-string
5099                           (format "%s %s (%d scored, %d total): "
5100                                   "How many articles from"
5101                                   (gnus-group-decoded-name group)
5102                                   scored number))))
5103                     (if (string-match "^[ \t]*$" input)
5104                         number input)))
5105                  (t number))
5106               (quit
5107                (message "Quit getting the articles to read")
5108                nil))))))
5109     (setq select (if (stringp select) (string-to-number select) select))
5110     (if (or (null select) (zerop select))
5111         select
5112       (if (and (not (zerop scored)) (<= (abs select) scored))
5113           (progn
5114             (setq articles (sort scored-list '<))
5115             (setq number (length articles)))
5116         (setq articles (copy-sequence articles)))
5117
5118       (when (< (abs select) number)
5119         (if (< select 0)
5120             ;; Select the N oldest articles.
5121             (setcdr (nthcdr (1- (abs select)) articles) nil)
5122           ;; Select the N most recent articles.
5123           (setq articles (nthcdr (- number select) articles))))
5124       (setq gnus-newsgroup-unselected
5125             (gnus-sorted-difference gnus-newsgroup-unreads articles))
5126       (when gnus-alter-articles-to-read-function
5127         (setq gnus-newsgroup-unreads
5128               (sort
5129                (funcall gnus-alter-articles-to-read-function
5130                         gnus-newsgroup-name gnus-newsgroup-unreads)
5131                '<)))
5132       articles)))
5133
5134 (defun gnus-killed-articles (killed articles)
5135   (let (out)
5136     (while articles
5137       (when (inline (gnus-member-of-range (car articles) killed))
5138         (push (car articles) out))
5139       (setq articles (cdr articles)))
5140     out))
5141
5142 (defun gnus-uncompress-marks (marks)
5143   "Uncompress the mark ranges in MARKS."
5144   (let ((uncompressed '(score bookmark))
5145         out)
5146     (while marks
5147       (if (memq (caar marks) uncompressed)
5148           (push (car marks) out)
5149         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
5150       (setq marks (cdr marks)))
5151     out))
5152
5153 (defun gnus-article-mark-to-type (mark)
5154   "Return the type of MARK."
5155   (or (cadr (assq mark gnus-article-special-mark-lists))
5156       'list))
5157
5158 (defun gnus-article-unpropagatable-p (mark)
5159   "Return whether MARK should be propagated to backend."
5160   (memq mark gnus-article-unpropagated-mark-lists))
5161
5162 (defun gnus-adjust-marked-articles (info)
5163   "Set all article lists and remove all marks that are no longer valid."
5164   (let* ((marked-lists (gnus-info-marks info))
5165          (active (gnus-active (gnus-info-group info)))
5166          (min (car active))
5167          (max (cdr active))
5168          (types gnus-article-mark-lists)
5169          marks var articles article mark mark-type)
5170
5171     (dolist (marks marked-lists)
5172       (setq mark (car marks)
5173             mark-type (gnus-article-mark-to-type mark)
5174             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
5175
5176       ;; We set the variable according to the type of the marks list,
5177       ;; and then adjust the marks to a subset of the active articles.
5178       (cond
5179        ;; Adjust "simple" lists.
5180        ((eq mark-type 'list)
5181         (set var (setq articles (gnus-uncompress-range (cdr marks))))
5182         (when (memq mark '(tick dormant expire reply save))
5183           (while articles
5184             (when (or (< (setq article (pop articles)) min) (> article max))
5185               (set var (delq article (symbol-value var)))))))
5186        ;; Adjust assocs.
5187        ((eq mark-type 'tuple)
5188         (set var (setq articles (cdr marks)))
5189         (when (not (listp (cdr (symbol-value var))))
5190           (set var (list (symbol-value var))))
5191         (when (not (listp (cdr articles)))
5192           (setq articles (list articles)))
5193         (while articles
5194           (when (or (not (consp (setq article (pop articles))))
5195                     (< (car article) min)
5196                     (> (car article) max))
5197             (set var (delq article (symbol-value var))))))
5198        ;; Adjust ranges (sloppily).
5199        ((eq mark-type 'range)
5200         (cond
5201          ((eq mark 'seen)
5202           ;; Fix the record for `seen' if it looks like (seen NUM1 . NUM2).
5203           ;; It should be (seen (NUM1 . NUM2)).
5204           (when (numberp (cddr marks))
5205             (setcdr marks (list (cdr marks))))
5206           (setq articles (cdr marks))
5207           (while (and articles
5208                       (or (and (consp (car articles))
5209                                (> min (cdar articles)))
5210                           (and (numberp (car articles))
5211                                (> min (car articles)))))
5212             (pop articles))
5213           (set var articles))))))))
5214
5215 (defun gnus-update-missing-marks (missing)
5216   "Go through the list of MISSING articles and remove them from the mark lists."
5217   (when missing
5218     (let (var m)
5219       ;; Go through all types.
5220       (dolist (elem gnus-article-mark-lists)
5221         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
5222           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
5223           (when (symbol-value var)
5224             ;; This list has articles.  So we delete all missing
5225             ;; articles from it.
5226             (setq m missing)
5227             (while m
5228               (set var (delq (pop m) (symbol-value var))))))))))
5229
5230 (defun gnus-update-marks ()
5231   "Enter the various lists of marked articles into the newsgroup info list."
5232   (let ((types gnus-article-mark-lists)
5233         (info (gnus-get-info gnus-newsgroup-name))
5234         type list newmarked symbol delta-marks)
5235     (when info
5236       ;; Add all marks lists to the list of marks lists.
5237       (while (setq type (pop types))
5238         (setq list (symbol-value
5239                     (setq symbol
5240                           (intern (format "gnus-newsgroup-%s" (car type))))))
5241
5242         (when list
5243           ;; Get rid of the entries of the articles that have the
5244           ;; default score.
5245           (when (and (eq (cdr type) 'score)
5246                      gnus-save-score
5247                      list)
5248             (let* ((arts list)
5249                    (prev (cons nil list))
5250                    (all prev))
5251               (while arts
5252                 (if (or (not (consp (car arts)))
5253                         (= (cdar arts) gnus-summary-default-score))
5254                     (setcdr prev (cdr arts))
5255                   (setq prev arts))
5256                 (setq arts (cdr arts)))
5257               (setq list (cdr all)))))
5258
5259         (when (eq (cdr type) 'seen)
5260           (setq list (gnus-range-add list gnus-newsgroup-unseen)))
5261
5262         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
5263           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
5264
5265         (when (and (gnus-check-backend-function
5266                     'request-set-mark gnus-newsgroup-name)
5267                    (not (gnus-article-unpropagatable-p (cdr type))))
5268           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
5269                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
5270                  (add (gnus-remove-from-range
5271                        (gnus-copy-sequence list) old)))
5272             (when add
5273               (push (list add 'add (list (cdr type))) delta-marks))
5274             (when del
5275               (push (list del 'del (list (cdr type))) delta-marks))))
5276
5277         (when list
5278           (push (cons (cdr type) list) newmarked)))
5279
5280       (when delta-marks
5281         (unless (gnus-check-group gnus-newsgroup-name)
5282           (error "Can't open server for %s" gnus-newsgroup-name))
5283         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
5284
5285       ;; Enter these new marks into the info of the group.
5286       (if (nthcdr 3 info)
5287           (setcar (nthcdr 3 info) newmarked)
5288         ;; Add the marks lists to the end of the info.
5289         (when newmarked
5290           (setcdr (nthcdr 2 info) (list newmarked))))
5291
5292       ;; Cut off the end of the info if there's nothing else there.
5293       (let ((i 5))
5294         (while (and (> i 2)
5295                     (not (nth i info)))
5296           (when (nthcdr (decf i) info)
5297             (setcdr (nthcdr i info) nil)))))))
5298
5299 (defun gnus-set-mode-line (where)
5300   "Set the mode line of the article or summary buffers.
5301 If WHERE is `summary', the summary mode line format will be used."
5302   ;; Is this mode line one we keep updated?
5303   (when (and (memq where gnus-updated-mode-lines)
5304              (symbol-value
5305               (intern (format "gnus-%s-mode-line-format-spec" where))))
5306     (let (mode-string)
5307       (save-excursion
5308         ;; We evaluate this in the summary buffer since these
5309         ;; variables are buffer-local to that buffer.
5310         (set-buffer gnus-summary-buffer)
5311        ;; We bind all these variables that are used in the `eval' form
5312         ;; below.
5313         (let* ((mformat (symbol-value
5314                          (intern
5315                           (format "gnus-%s-mode-line-format-spec" where))))
5316                (gnus-tmp-group-name (gnus-group-decoded-name
5317                                      gnus-newsgroup-name))
5318                (gnus-tmp-article-number (or gnus-current-article 0))
5319                (gnus-tmp-unread gnus-newsgroup-unreads)
5320                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
5321                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
5322                (gnus-tmp-unread-and-unselected
5323                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
5324                             (zerop gnus-tmp-unselected))
5325                        "")
5326                       ((zerop gnus-tmp-unselected)
5327                        (format "{%d more}" gnus-tmp-unread-and-unticked))
5328                       (t (format "{%d(+%d) more}"
5329                                  gnus-tmp-unread-and-unticked
5330                                  gnus-tmp-unselected))))
5331                (gnus-tmp-subject
5332                 (if (and gnus-current-headers
5333                          (vectorp gnus-current-headers))
5334                     (gnus-mode-string-quote
5335                      (mail-header-subject gnus-current-headers))
5336                   ""))
5337                bufname-length max-len
5338                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5339           (setq mode-string (eval mformat))
5340           (setq bufname-length (if (string-match "%b" mode-string)
5341                                    (- (length
5342                                        (buffer-name
5343                                         (if (eq where 'summary)
5344                                             nil
5345                                           (get-buffer gnus-article-buffer))))
5346                                       2)
5347                                  0))
5348           (setq max-len (max 4 (if gnus-mode-non-string-length
5349                                    (- (window-width)
5350                                       gnus-mode-non-string-length
5351                                       bufname-length)
5352                                  (length mode-string))))
5353           ;; We might have to chop a bit of the string off...
5354           (when (> (length mode-string) max-len)
5355             (setq mode-string
5356                   (concat (truncate-string-to-width mode-string (- max-len 3))
5357                           "...")))
5358           ;; Pad the mode string a bit.
5359           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5360       ;; Update the mode line.
5361       (setq mode-line-buffer-identification
5362             (gnus-mode-line-buffer-identification (list mode-string)))
5363       (set-buffer-modified-p t))))
5364
5365 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5366   "Go through the HEADERS list and add all Xrefs to a hash table.
5367 The resulting hash table is returned, or nil if no Xrefs were found."
5368   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5369          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5370          (xref-hashtb (gnus-make-hashtable))
5371          start group entry number xrefs header)
5372     (while headers
5373       (setq header (pop headers))
5374       (when (and (setq xrefs (mail-header-xref header))
5375                  (not (memq (setq number (mail-header-number header))
5376                             unreads)))
5377         (setq start 0)
5378         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5379           (setq start (match-end 0))
5380           (setq group (if prefix
5381                           (concat prefix (substring xrefs (match-beginning 1)
5382                                                     (match-end 1)))
5383                         (substring xrefs (match-beginning 1) (match-end 1))))
5384           (setq number
5385                 (string-to-int (substring xrefs (match-beginning 2)
5386                                           (match-end 2))))
5387           (if (setq entry (gnus-gethash group xref-hashtb))
5388               (setcdr entry (cons number (cdr entry)))
5389             (gnus-sethash group (cons number nil) xref-hashtb)))))
5390     (and start xref-hashtb)))
5391
5392 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5393   "Look through all the headers and mark the Xrefs as read."
5394   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5395         name entry info xref-hashtb idlist method nth4)
5396     (save-excursion
5397       (set-buffer gnus-group-buffer)
5398       (when (setq xref-hashtb
5399                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5400         (mapatoms
5401          (lambda (group)
5402            (unless (string= from-newsgroup (setq name (symbol-name group)))
5403              (setq idlist (symbol-value group))
5404              ;; Dead groups are not updated.
5405              (and (prog1
5406                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5407                             info (nth 2 entry))
5408                     (when (stringp (setq nth4 (gnus-info-method info)))
5409                       (setq nth4 (gnus-server-to-method nth4))))
5410                   ;; Only do the xrefs if the group has the same
5411                   ;; select method as the group we have just read.
5412                   (or (gnus-methods-equal-p
5413                        nth4 (gnus-find-method-for-group from-newsgroup))
5414                       virtual
5415                       (equal nth4 (setq method (gnus-find-method-for-group
5416                                                 from-newsgroup)))
5417                       (and (equal (car nth4) (car method))
5418                            (equal (nth 1 nth4) (nth 1 method))))
5419                   gnus-use-cross-reference
5420                   (or (not (eq gnus-use-cross-reference t))
5421                       virtual
5422                       ;; Only do cross-references on subscribed
5423                       ;; groups, if that is what is wanted.
5424                       (<= (gnus-info-level info) gnus-level-subscribed))
5425                   (gnus-group-make-articles-read name idlist))))
5426          xref-hashtb)))))
5427
5428 (defun gnus-compute-read-articles (group articles)
5429   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5430          (info (nth 2 entry))
5431          (active (gnus-active group))
5432          ninfo)
5433     (when entry
5434       ;; First peel off all invalid article numbers.
5435       (when active
5436         (let ((ids articles)
5437               id first)
5438           (while (setq id (pop ids))
5439             (when (and first (> id (cdr active)))
5440               ;; We'll end up in this situation in one particular
5441               ;; obscure situation.  If you re-scan a group and get
5442               ;; a new article that is cross-posted to a different
5443               ;; group that has not been re-scanned, you might get
5444               ;; crossposted article that has a higher number than
5445               ;; Gnus believes possible.  So we re-activate this
5446               ;; group as well.  This might mean doing the
5447               ;; crossposting thingy will *increase* the number
5448               ;; of articles in some groups.  Tsk, tsk.
5449               (setq active (or (gnus-activate-group group) active)))
5450             (when (or (> id (cdr active))
5451                       (< id (car active)))
5452               (setq articles (delq id articles))))))
5453       ;; If the read list is nil, we init it.
5454       (if (and active
5455                (null (gnus-info-read info))
5456                (> (car active) 1))
5457           (setq ninfo (cons 1 (1- (car active))))
5458         (setq ninfo (gnus-info-read info)))
5459       ;; Then we add the read articles to the range.
5460       (gnus-add-to-range
5461        ninfo (setq articles (sort articles '<))))))
5462
5463 (defun gnus-group-make-articles-read (group articles)
5464   "Update the info of GROUP to say that ARTICLES are read."
5465   (let* ((num 0)
5466          (entry (gnus-gethash group gnus-newsrc-hashtb))
5467          (info (nth 2 entry))
5468          (active (gnus-active group))
5469          range)
5470     (when entry
5471       (setq range (gnus-compute-read-articles group articles))
5472       (save-excursion
5473         (set-buffer gnus-group-buffer)
5474         (gnus-undo-register
5475           `(progn
5476              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5477              (gnus-info-set-read ',info ',(gnus-info-read info))
5478              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5479              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5480              (gnus-group-update-group ,group t))))
5481       ;; Add the read articles to the range.
5482       (gnus-info-set-read info range)
5483       (gnus-request-set-mark group (list (list range 'add '(read))))
5484       ;; Then we have to re-compute how many unread
5485       ;; articles there are in this group.
5486       (when active
5487         (cond
5488          ((not range)
5489           (setq num (- (1+ (cdr active)) (car active))))
5490          ((not (listp (cdr range)))
5491           (setq num (- (cdr active) (- (1+ (cdr range))
5492                                        (car range)))))
5493          (t
5494           (while range
5495             (if (numberp (car range))
5496                 (setq num (1+ num))
5497               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5498             (setq range (cdr range)))
5499           (setq num (- (cdr active) num))))
5500         ;; Update the number of unread articles.
5501         (setcar entry num)
5502         ;; Update the group buffer.
5503         (gnus-group-update-group group t)))))
5504
5505 (defvar gnus-newsgroup-none-id 0)
5506
5507 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5508   (let ((cur nntp-server-buffer)
5509         (dependencies
5510          (or dependencies
5511              (save-excursion (set-buffer gnus-summary-buffer)
5512                              gnus-newsgroup-dependencies)))
5513         headers id end ref
5514         (mail-parse-charset gnus-newsgroup-charset)
5515         (mail-parse-ignored-charsets
5516          (save-excursion (condition-case nil
5517                              (set-buffer gnus-summary-buffer)
5518                            (error))
5519                          gnus-newsgroup-ignored-charsets)))
5520     (save-excursion
5521       (set-buffer nntp-server-buffer)
5522       ;; Translate all TAB characters into SPACE characters.
5523       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5524       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5525       (gnus-run-hooks 'gnus-parse-headers-hook)
5526       (let ((case-fold-search t)
5527             in-reply-to header p lines chars)
5528         (goto-char (point-min))
5529         ;; Search to the beginning of the next header.  Error messages
5530         ;; do not begin with 2 or 3.
5531         (while (re-search-forward "^[23][0-9]+ " nil t)
5532           (setq id nil
5533                 ref nil)
5534           ;; This implementation of this function, with nine
5535           ;; search-forwards instead of the one re-search-forward and
5536           ;; a case (which basically was the old function) is actually
5537           ;; about twice as fast, even though it looks messier.  You
5538           ;; can't have everything, I guess.  Speed and elegance
5539           ;; doesn't always go hand in hand.
5540           (setq
5541            header
5542            (vector
5543             ;; Number.
5544             (prog1
5545                 (read cur)
5546               (end-of-line)
5547               (setq p (point))
5548               (narrow-to-region (point)
5549                                 (or (and (search-forward "\n.\n" nil t)
5550                                          (- (point) 2))
5551                                     (point))))
5552             ;; Subject.
5553             (progn
5554               (goto-char p)
5555               (if (search-forward "\nsubject:" nil t)
5556                   (funcall gnus-decode-encoded-word-function
5557                            (nnheader-header-value))
5558                 "(none)"))
5559             ;; From.
5560             (progn
5561               (goto-char p)
5562               (if (search-forward "\nfrom:" nil t)
5563                   (funcall gnus-decode-encoded-word-function
5564                            (nnheader-header-value))
5565                 "(nobody)"))
5566             ;; Date.
5567             (progn
5568               (goto-char p)
5569               (if (search-forward "\ndate:" nil t)
5570                   (nnheader-header-value) ""))
5571             ;; Message-ID.
5572             (progn
5573               (goto-char p)
5574               (setq id (if (re-search-forward
5575                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5576                            ;; We do it this way to make sure the Message-ID
5577                            ;; is (somewhat) syntactically valid.
5578                            (buffer-substring (match-beginning 1)
5579                                              (match-end 1))
5580                          ;; If there was no message-id, we just fake one
5581                          ;; to make subsequent routines simpler.
5582                          (nnheader-generate-fake-message-id))))
5583             ;; References.
5584             (progn
5585               (goto-char p)
5586               (if (search-forward "\nreferences:" nil t)
5587                   (progn
5588                     (setq end (point))
5589                     (prog1
5590                         (nnheader-header-value)
5591                       (setq ref
5592                             (buffer-substring
5593                              (progn
5594                                (end-of-line)
5595                                (search-backward ">" end t)
5596                                (1+ (point)))
5597                              (progn
5598                                (search-backward "<" end t)
5599                                (point))))))
5600                 ;; Get the references from the in-reply-to header if there
5601                 ;; were no references and the in-reply-to header looks
5602                 ;; promising.
5603                 (if (and (search-forward "\nin-reply-to:" nil t)
5604                          (setq in-reply-to (nnheader-header-value))
5605                          (string-match "<[^>]+>" in-reply-to))
5606                     (let (ref2)
5607                       (setq ref (substring in-reply-to (match-beginning 0)
5608                                            (match-end 0)))
5609                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5610                         (setq ref2 (substring in-reply-to (match-beginning 0)
5611                                               (match-end 0)))
5612                         (when (> (length ref2) (length ref))
5613                           (setq ref ref2)))
5614                       ref)
5615                   (setq ref nil))))
5616             ;; Chars.
5617             (progn
5618               (goto-char p)
5619               (if (search-forward "\nchars: " nil t)
5620                   (if (numberp (setq chars (ignore-errors (read cur))))
5621                       chars -1)
5622                 -1))
5623             ;; Lines.
5624             (progn
5625               (goto-char p)
5626               (if (search-forward "\nlines: " nil t)
5627                   (if (numberp (setq lines (ignore-errors (read cur))))
5628                       lines -1)
5629                 -1))
5630             ;; Xref.
5631             (progn
5632               (goto-char p)
5633               (and (search-forward "\nxref:" nil t)
5634                    (nnheader-header-value)))
5635             ;; Extra.
5636             (when gnus-extra-headers
5637               (let ((extra gnus-extra-headers)
5638                     out)
5639                 (while extra
5640                   (goto-char p)
5641                   (when (search-forward
5642                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5643                     (push (cons (car extra) (nnheader-header-value))
5644                           out))
5645                   (pop extra))
5646                 out))))
5647           (when (equal id ref)
5648             (setq ref nil))
5649
5650           (when gnus-alter-header-function
5651             (funcall gnus-alter-header-function header)
5652             (setq id (mail-header-id header)
5653                   ref (gnus-parent-id (mail-header-references header))))
5654
5655           (when (setq header
5656                       (gnus-dependencies-add-header
5657                        header dependencies force-new))
5658             (push header headers))
5659           (goto-char (point-max))
5660           (widen))
5661         (nreverse headers)))))
5662
5663 ;; Goes through the xover lines and returns a list of vectors
5664 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5665                                                   force-new dependencies
5666                                                   group also-fetch-heads)
5667   "Parse the news overview data in the server buffer.
5668 Return a list of headers that match SEQUENCE (see
5669 `nntp-retrieve-headers')."
5670   ;; Get the Xref when the users reads the articles since most/some
5671   ;; NNTP servers do not include Xrefs when using XOVER.
5672   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5673   (let ((mail-parse-charset gnus-newsgroup-charset)
5674         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5675         (cur nntp-server-buffer)
5676         (dependencies (or dependencies gnus-newsgroup-dependencies))
5677         (allp (cond
5678                ((eq gnus-read-all-available-headers t)
5679                 t)
5680                ((stringp gnus-read-all-available-headers)
5681                 (string-match gnus-read-all-available-headers group))
5682                (t
5683                 nil)))
5684         number headers header)
5685     (save-excursion
5686       (set-buffer nntp-server-buffer)
5687       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5688       ;; Allow the user to mangle the headers before parsing them.
5689       (gnus-run-hooks 'gnus-parse-headers-hook)
5690       (goto-char (point-min))
5691       (gnus-parse-without-error
5692         (while (and (or sequence allp)
5693                     (not (eobp)))
5694           (setq number (read cur))
5695           (when (not allp)
5696             (while (and sequence
5697                         (< (car sequence) number))
5698               (setq sequence (cdr sequence))))
5699           (when (and (or allp
5700                          (and sequence
5701                               (eq number (car sequence))))
5702                      (progn
5703                        (setq sequence (cdr sequence))
5704                        (setq header (inline
5705                                       (gnus-nov-parse-line
5706                                        number dependencies force-new)))))
5707             (push header headers))
5708           (forward-line 1)))
5709       ;; A common bug in inn is that if you have posted an article and
5710       ;; then retrieves the active file, it will answer correctly --
5711       ;; the new article is included.  However, a NOV entry for the
5712       ;; article may not have been generated yet, so this may fail.
5713       ;; We work around this problem by retrieving the last few
5714       ;; headers using HEAD.
5715       (if (or (not also-fetch-heads)
5716               (not sequence))
5717           ;; We (probably) got all the headers.
5718           (nreverse headers)
5719         (let ((gnus-nov-is-evil t))
5720           (nconc
5721            (nreverse headers)
5722            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5723              (gnus-get-newsgroup-headers))))))))
5724
5725 (defun gnus-article-get-xrefs ()
5726   "Fill in the Xref value in `gnus-current-headers', if necessary.
5727 This is meant to be called in `gnus-article-internal-prepare-hook'."
5728   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5729                                  gnus-current-headers)))
5730     (or (not gnus-use-cross-reference)
5731         (not headers)
5732         (and (mail-header-xref headers)
5733              (not (string= (mail-header-xref headers) "")))
5734         (let ((case-fold-search t)
5735               xref)
5736           (save-restriction
5737             (nnheader-narrow-to-headers)
5738             (goto-char (point-min))
5739             (when (or (and (not (eobp))
5740                            (eq (downcase (char-after)) ?x)
5741                            (looking-at "Xref:"))
5742                       (search-forward "\nXref:" nil t))
5743               (goto-char (1+ (match-end 0)))
5744               (setq xref (buffer-substring (point)
5745                                            (progn (end-of-line) (point))))
5746               (mail-header-set-xref headers xref)))))))
5747
5748 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5749   "Find article ID and insert the summary line for that article.
5750 OLD-HEADER can either be a header or a line number to insert
5751 the subject line on."
5752   (let* ((line (and (numberp old-header) old-header))
5753          (old-header (and (vectorp old-header) old-header))
5754          (header (cond ((and old-header use-old-header)
5755                         old-header)
5756                        ((and (numberp id)
5757                              (gnus-number-to-header id))
5758                         (gnus-number-to-header id))
5759                        (t
5760                         (gnus-read-header id))))
5761          (number (and (numberp id) id))
5762          d)
5763     (when header
5764       ;; Rebuild the thread that this article is part of and go to the
5765       ;; article we have fetched.
5766       (when (and (not gnus-show-threads)
5767                  old-header)
5768         (when (and number
5769                    (setq d (gnus-data-find (mail-header-number old-header))))
5770           (goto-char (gnus-data-pos d))
5771           (gnus-data-remove
5772            number
5773            (- (gnus-point-at-bol)
5774               (prog1
5775                   (1+ (gnus-point-at-eol))
5776                 (gnus-delete-line))))))
5777       (when old-header
5778         (mail-header-set-number header (mail-header-number old-header)))
5779       (setq gnus-newsgroup-sparse
5780             (delq (setq number (mail-header-number header))
5781                   gnus-newsgroup-sparse))
5782       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5783       (push number gnus-newsgroup-limit)
5784       (gnus-rebuild-thread (mail-header-id header) line)
5785       (gnus-summary-goto-subject number nil t))
5786     (when (and (numberp number)
5787                (> number 0))
5788       ;; We have to update the boundaries even if we can't fetch the
5789       ;; article if ID is a number -- so that the next `P' or `N'
5790       ;; command will fetch the previous (or next) article even
5791       ;; if the one we tried to fetch this time has been canceled.
5792       (when (> number gnus-newsgroup-end)
5793         (setq gnus-newsgroup-end number))
5794       (when (< number gnus-newsgroup-begin)
5795         (setq gnus-newsgroup-begin number))
5796       (setq gnus-newsgroup-unselected
5797             (delq number gnus-newsgroup-unselected)))
5798     ;; Report back a success?
5799     (and header (mail-header-number header))))
5800
5801 ;;; Process/prefix in the summary buffer
5802
5803 (defun gnus-summary-work-articles (n)
5804   "Return a list of articles to be worked upon.
5805 The prefix argument, the list of process marked articles, and the
5806 current article will be taken into consideration."
5807   (save-excursion
5808     (set-buffer gnus-summary-buffer)
5809     (cond
5810      (n
5811       ;; A numerical prefix has been given.
5812       (setq n (prefix-numeric-value n))
5813       (let ((backward (< n 0))
5814             (n (abs (prefix-numeric-value n)))
5815             articles article)
5816         (save-excursion
5817           (while
5818               (and (> n 0)
5819                    (push (setq article (gnus-summary-article-number))
5820                          articles)
5821                    (if backward
5822                        (gnus-summary-find-prev nil article)
5823                      (gnus-summary-find-next nil article)))
5824             (decf n)))
5825         (nreverse articles)))
5826      ((and (gnus-region-active-p) (mark))
5827       (message "region active")
5828       ;; Work on the region between point and mark.
5829       (let ((max (max (point) (mark)))
5830             articles article)
5831         (save-excursion
5832           (goto-char (min (min (point) (mark))))
5833           (while
5834               (and
5835                (push (setq article (gnus-summary-article-number)) articles)
5836                (gnus-summary-find-next nil article)
5837                (< (point) max)))
5838           (nreverse articles))))
5839      (gnus-newsgroup-processable
5840       ;; There are process-marked articles present.
5841       ;; Save current state.
5842       (gnus-summary-save-process-mark)
5843       ;; Return the list.
5844       (reverse gnus-newsgroup-processable))
5845      (t
5846       ;; Just return the current article.
5847       (list (gnus-summary-article-number))))))
5848
5849 (defmacro gnus-summary-iterate (arg &rest forms)
5850   "Iterate over the process/prefixed articles and do FORMS.
5851 ARG is the interactive prefix given to the command.  FORMS will be
5852 executed with point over the summary line of the articles."
5853   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5854     `(let ((,articles (gnus-summary-work-articles ,arg)))
5855        (while ,articles
5856          (gnus-summary-goto-subject (car ,articles))
5857          ,@forms
5858          (pop ,articles)))))
5859
5860 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5861 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5862
5863 (defun gnus-summary-save-process-mark ()
5864   "Push the current set of process marked articles on the stack."
5865   (interactive)
5866   (push (copy-sequence gnus-newsgroup-processable)
5867         gnus-newsgroup-process-stack))
5868
5869 (defun gnus-summary-kill-process-mark ()
5870   "Push the current set of process marked articles on the stack and unmark."
5871   (interactive)
5872   (gnus-summary-save-process-mark)
5873   (gnus-summary-unmark-all-processable))
5874
5875 (defun gnus-summary-yank-process-mark ()
5876   "Pop the last process mark state off the stack and restore it."
5877   (interactive)
5878   (unless gnus-newsgroup-process-stack
5879     (error "Empty mark stack"))
5880   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5881
5882 (defun gnus-summary-process-mark-set (set)
5883   "Make SET into the current process marked articles."
5884   (gnus-summary-unmark-all-processable)
5885   (while set
5886     (gnus-summary-set-process-mark (pop set))))
5887
5888 ;;; Searching and stuff
5889
5890 (defun gnus-summary-search-group (&optional backward use-level)
5891   "Search for next unread newsgroup.
5892 If optional argument BACKWARD is non-nil, search backward instead."
5893   (save-excursion
5894     (set-buffer gnus-group-buffer)
5895     (when (gnus-group-search-forward
5896            backward nil (if use-level (gnus-group-group-level) nil))
5897       (gnus-group-group-name))))
5898
5899 (defun gnus-summary-best-group (&optional exclude-group)
5900   "Find the name of the best unread group.
5901 If EXCLUDE-GROUP, do not go to this group."
5902   (save-excursion
5903     (set-buffer gnus-group-buffer)
5904     (save-excursion
5905       (gnus-group-best-unread-group exclude-group))))
5906
5907 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5908   (if backward (gnus-summary-find-prev)
5909     (let* ((dummy (gnus-summary-article-intangible-p))
5910            (article (or article (gnus-summary-article-number)))
5911            (arts (gnus-data-find-list article))
5912            result)
5913       (when (and (not dummy)
5914                  (or (not gnus-summary-check-current)
5915                      (not unread)
5916                      (not (gnus-data-unread-p (car arts)))))
5917         (setq arts (cdr arts)))
5918       (when (setq result
5919                   (if unread
5920                       (progn
5921                         (while arts
5922                           (when (or (and undownloaded
5923                                          (eq gnus-undownloaded-mark
5924                                              (gnus-data-mark (car arts))))
5925                                     (gnus-data-unread-p (car arts)))
5926                             (setq result (car arts)
5927                                   arts nil))
5928                           (setq arts (cdr arts)))
5929                         result)
5930                     (car arts)))
5931         (goto-char (gnus-data-pos result))
5932         (gnus-data-number result)))))
5933
5934 (defun gnus-summary-find-prev (&optional unread article)
5935   (let* ((eobp (eobp))
5936          (article (or article (gnus-summary-article-number)))
5937          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5938          result)
5939     (when (and (not eobp)
5940                (or (not gnus-summary-check-current)
5941                    (not unread)
5942                    (not (gnus-data-unread-p (car arts)))))
5943       (setq arts (cdr arts)))
5944     (when (setq result
5945                 (if unread
5946                     (progn
5947                       (while arts
5948                         (when (gnus-data-unread-p (car arts))
5949                           (setq result (car arts)
5950                                 arts nil))
5951                         (setq arts (cdr arts)))
5952                       result)
5953                   (car arts)))
5954       (goto-char (gnus-data-pos result))
5955       (gnus-data-number result))))
5956
5957 (defun gnus-summary-find-subject (subject &optional unread backward article)
5958   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5959          (article (or article (gnus-summary-article-number)))
5960          (articles (gnus-data-list backward))
5961          (arts (gnus-data-find-list article articles))
5962          result)
5963     (when (or (not gnus-summary-check-current)
5964               (not unread)
5965               (not (gnus-data-unread-p (car arts))))
5966       (setq arts (cdr arts)))
5967     (while arts
5968       (and (or (not unread)
5969                (gnus-data-unread-p (car arts)))
5970            (vectorp (gnus-data-header (car arts)))
5971            (gnus-subject-equal
5972             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5973            (setq result (car arts)
5974                  arts nil))
5975       (setq arts (cdr arts)))
5976     (and result
5977          (goto-char (gnus-data-pos result))
5978          (gnus-data-number result))))
5979
5980 (defun gnus-summary-search-forward (&optional unread subject backward)
5981   "Search forward for an article.
5982 If UNREAD, look for unread articles.  If SUBJECT, look for
5983 articles with that subject.  If BACKWARD, search backward instead."
5984   (cond (subject (gnus-summary-find-subject subject unread backward))
5985         (backward (gnus-summary-find-prev unread))
5986         (t (gnus-summary-find-next unread))))
5987
5988 (defun gnus-recenter (&optional n)
5989   "Center point in window and redisplay frame.
5990 Also do horizontal recentering."
5991   (interactive "P")
5992   (when (and gnus-auto-center-summary
5993              (not (eq gnus-auto-center-summary 'vertical)))
5994     (gnus-horizontal-recenter))
5995   (recenter n))
5996
5997 (defun gnus-summary-recenter ()
5998   "Center point in the summary window.
5999 If `gnus-auto-center-summary' is nil, or the article buffer isn't
6000 displayed, no centering will be performed."
6001   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
6002 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
6003   (interactive)
6004   (let* ((top (cond ((< (window-height) 4) 0)
6005                     ((< (window-height) 7) 1)
6006                     (t (if (numberp gnus-auto-center-summary)
6007                            gnus-auto-center-summary
6008                          2))))
6009          (height (1- (window-height)))
6010          (bottom (save-excursion (goto-char (point-max))
6011                                  (forward-line (- height))
6012                                  (point)))
6013          (window (get-buffer-window (current-buffer))))
6014     ;; The user has to want it.
6015     (when gnus-auto-center-summary
6016       (when (get-buffer-window gnus-article-buffer)
6017         ;; Only do recentering when the article buffer is displayed,
6018       ;; Set the window start to either `bottom', which is the biggest
6019         ;; possible valid number, or the second line from the top,
6020         ;; whichever is the least.
6021         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
6022           (if (> bottom top-pos)
6023               ;; Keep the second line from the top visible
6024               (set-window-start window top-pos t)
6025             ;; Try to keep the bottom line visible; if it's partially
6026             ;; obscured, either scroll one more line to make it fully
6027             ;; visible, or revert to using TOP-POS.
6028             (save-excursion
6029               (goto-char (point-max))
6030               (forward-line -1)
6031               (let ((last-line-start (point)))
6032                 (goto-char bottom)
6033                 (set-window-start window (point) t)
6034                 (when (not (pos-visible-in-window-p last-line-start window))
6035                   (forward-line 1)
6036                   (set-window-start window (min (point) top-pos) t)))))))
6037       ;; Do horizontal recentering while we're at it.
6038       (when (and (get-buffer-window (current-buffer) t)
6039                  (not (eq gnus-auto-center-summary 'vertical)))
6040         (let ((selected (selected-window)))
6041           (select-window (get-buffer-window (current-buffer) t))
6042           (gnus-summary-position-point)
6043           (gnus-horizontal-recenter)
6044           (select-window selected))))))
6045
6046 (defun gnus-summary-jump-to-group (newsgroup)
6047   "Move point to NEWSGROUP in group mode buffer."
6048   ;; Keep update point of group mode buffer if visible.
6049   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
6050       (save-window-excursion
6051         ;; Take care of tree window mode.
6052         (when (get-buffer-window gnus-group-buffer)
6053           (pop-to-buffer gnus-group-buffer))
6054         (gnus-group-jump-to-group newsgroup))
6055     (save-excursion
6056       ;; Take care of tree window mode.
6057       (if (get-buffer-window gnus-group-buffer)
6058           (pop-to-buffer gnus-group-buffer)
6059         (set-buffer gnus-group-buffer))
6060       (gnus-group-jump-to-group newsgroup))))
6061
6062 ;; This function returns a list of article numbers based on the
6063 ;; difference between the ranges of read articles in this group and
6064 ;; the range of active articles.
6065 (defun gnus-list-of-unread-articles (group)
6066   (let* ((read (gnus-info-read (gnus-get-info group)))
6067          (active (or (gnus-active group) (gnus-activate-group group)))
6068          (last (cdr active))
6069          first nlast unread)
6070     ;; If none are read, then all are unread.
6071     (if (not read)
6072         (setq first (car active))
6073       ;; If the range of read articles is a single range, then the
6074       ;; first unread article is the article after the last read
6075       ;; article.  Sounds logical, doesn't it?
6076       (if (and (not (listp (cdr read)))
6077                (or (< (car read) (car active))
6078                    (progn (setq read (list read))
6079                           nil)))
6080           (setq first (max (car active) (1+ (cdr read))))
6081         ;; `read' is a list of ranges.
6082         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
6083                                   (caar read)))
6084                   1)
6085           (setq first (car active)))
6086         (while read
6087           (when first
6088             (while (< first nlast)
6089               (push first unread)
6090               (setq first (1+ first))))
6091           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
6092           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
6093           (setq read (cdr read)))))
6094     ;; And add the last unread articles.
6095     (while (<= first last)
6096       (push first unread)
6097       (setq first (1+ first)))
6098     ;; Return the list of unread articles.
6099     (delq 0 (nreverse unread))))
6100
6101 (defun gnus-list-of-read-articles (group)
6102   "Return a list of unread, unticked and non-dormant articles."
6103   (let* ((info (gnus-get-info group))
6104          (marked (gnus-info-marks info))
6105          (active (gnus-active group)))
6106     (and info active
6107          (gnus-list-range-difference
6108           (gnus-list-range-difference
6109            (gnus-sorted-complement
6110             (gnus-uncompress-range active)
6111             (gnus-list-of-unread-articles group))
6112            (cdr (assq 'dormant marked)))
6113           (cdr (assq 'tick marked))))))
6114
6115 ;; Various summary commands
6116
6117 (defun gnus-summary-select-article-buffer ()
6118   "Reconfigure windows to show article buffer."
6119   (interactive)
6120   (if (not (gnus-buffer-live-p gnus-article-buffer))
6121       (error "There is no article buffer for this summary buffer")
6122     (gnus-configure-windows 'article)
6123     (select-window (get-buffer-window gnus-article-buffer))))
6124
6125 (defun gnus-summary-universal-argument (arg)
6126   "Perform any operation on all articles that are process/prefixed."
6127   (interactive "P")
6128   (let ((articles (gnus-summary-work-articles arg))
6129         func article)
6130     (if (eq
6131          (setq
6132           func
6133           (key-binding
6134            (read-key-sequence
6135             (substitute-command-keys
6136              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
6137          'undefined)
6138         (gnus-error 1 "Undefined key")
6139       (save-excursion
6140         (while articles
6141           (gnus-summary-goto-subject (setq article (pop articles)))
6142           (let (gnus-newsgroup-processable)
6143             (command-execute func))
6144           (gnus-summary-remove-process-mark article)))))
6145   (gnus-summary-position-point))
6146
6147 (defun gnus-summary-toggle-truncation (&optional arg)
6148   "Toggle truncation of summary lines.
6149 With arg, turn line truncation on iff arg is positive."
6150   (interactive "P")
6151   (setq truncate-lines
6152         (if (null arg) (not truncate-lines)
6153           (> (prefix-numeric-value arg) 0)))
6154   (redraw-display))
6155
6156 (defun gnus-summary-reselect-current-group (&optional all rescan)
6157   "Exit and then reselect the current newsgroup.
6158 The prefix argument ALL means to select all articles."
6159   (interactive "P")
6160   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
6161     (error "Ephemeral groups can't be reselected"))
6162   (let ((current-subject (gnus-summary-article-number))
6163         (group gnus-newsgroup-name))
6164     (setq gnus-newsgroup-begin nil)
6165     (gnus-summary-exit)
6166     ;; We have to adjust the point of group mode buffer because
6167     ;; point was moved to the next unread newsgroup by exiting.
6168     (gnus-summary-jump-to-group group)
6169     (when rescan
6170       (save-excursion
6171         (gnus-group-get-new-news-this-group 1)))
6172     (gnus-group-read-group all t)
6173     (gnus-summary-goto-subject current-subject nil t)))
6174
6175 (defun gnus-summary-rescan-group (&optional all)
6176   "Exit the newsgroup, ask for new articles, and select the newsgroup."
6177   (interactive "P")
6178   (gnus-summary-reselect-current-group all t))
6179
6180 (defun gnus-summary-update-info (&optional non-destructive)
6181   (save-excursion
6182     (let ((group gnus-newsgroup-name))
6183       (when group
6184         (when gnus-newsgroup-kill-headers
6185           (setq gnus-newsgroup-killed
6186                 (gnus-compress-sequence
6187                  (gnus-sorted-union
6188                   (gnus-list-range-intersection
6189                    gnus-newsgroup-unselected gnus-newsgroup-killed)
6190                   gnus-newsgroup-unreads)
6191                  t)))
6192         (unless (listp (cdr gnus-newsgroup-killed))
6193           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
6194         (let ((headers gnus-newsgroup-headers))
6195           ;; Set the new ranges of read articles.
6196           (save-excursion
6197             (set-buffer gnus-group-buffer)
6198             (gnus-undo-force-boundary))
6199           (gnus-update-read-articles
6200            group (gnus-sorted-union
6201                   gnus-newsgroup-unreads gnus-newsgroup-unselected))
6202           ;; Set the current article marks.
6203           (let ((gnus-newsgroup-scored
6204                  (if (and (not gnus-save-score)
6205                           (not non-destructive))
6206                      nil
6207                    gnus-newsgroup-scored)))
6208             (save-excursion
6209               (gnus-update-marks)))
6210           ;; Do the cross-ref thing.
6211           (when gnus-use-cross-reference
6212             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
6213           ;; Do not switch windows but change the buffer to work.
6214           (set-buffer gnus-group-buffer)
6215           (unless (gnus-ephemeral-group-p group)
6216             (gnus-group-update-group group)))))))
6217
6218 (defun gnus-summary-save-newsrc (&optional force)
6219   "Save the current number of read/marked articles in the dribble buffer.
6220 The dribble buffer will then be saved.
6221 If FORCE (the prefix), also save the .newsrc file(s)."
6222   (interactive "P")
6223   (gnus-summary-update-info t)
6224   (if force
6225       (gnus-save-newsrc-file)
6226     (gnus-dribble-save)))
6227
6228 (defun gnus-summary-exit (&optional temporary)
6229   "Exit reading current newsgroup, and then return to group selection mode.
6230 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
6231   (interactive)
6232   (gnus-set-global-variables)
6233   (when (gnus-buffer-live-p gnus-article-buffer)
6234     (save-excursion
6235       (set-buffer gnus-article-buffer)
6236       (mm-destroy-parts gnus-article-mime-handles)
6237       ;; Set it to nil for safety reason.
6238       (setq gnus-article-mime-handle-alist nil)
6239       (setq gnus-article-mime-handles nil)))
6240   (gnus-kill-save-kill-buffer)
6241   (gnus-async-halt-prefetch)
6242   (let* ((group gnus-newsgroup-name)
6243          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
6244          (mode major-mode)
6245          (group-point nil)
6246          (buf (current-buffer)))
6247     (unless quit-config
6248       ;; Do adaptive scoring, and possibly save score files.
6249       (when gnus-newsgroup-adaptive
6250         (gnus-score-adaptive))
6251       (when gnus-use-scoring
6252         (gnus-score-save)))
6253     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
6254     ;; If we have several article buffers, we kill them at exit.
6255     (unless gnus-single-article-buffer
6256       (gnus-kill-buffer gnus-original-article-buffer)
6257       (setq gnus-article-current nil))
6258     (when gnus-use-cache
6259       (gnus-cache-possibly-remove-articles)
6260       (gnus-cache-save-buffers))
6261     (gnus-async-prefetch-remove-group group)
6262     (when gnus-suppress-duplicates
6263       (gnus-dup-enter-articles))
6264     (when gnus-use-trees
6265       (gnus-tree-close group))
6266     (when gnus-use-cache
6267       (gnus-cache-write-active))
6268     ;; Remove entries for this group.
6269     (nnmail-purge-split-history (gnus-group-real-name group))
6270     ;; Make all changes in this group permanent.
6271     (unless quit-config
6272       (gnus-run-hooks 'gnus-exit-group-hook)
6273       (gnus-summary-update-info))
6274     (gnus-close-group group)
6275     ;; Make sure where we were, and go to next newsgroup.
6276     (set-buffer gnus-group-buffer)
6277     (unless quit-config
6278       (gnus-group-jump-to-group group))
6279     (gnus-run-hooks 'gnus-summary-exit-hook)
6280     (unless (or quit-config
6281                 ;; If this group has disappeared from the summary
6282                 ;; buffer, don't skip forwards.
6283                 (not (string= group (gnus-group-group-name))))
6284       (gnus-group-next-unread-group 1))
6285     (setq group-point (point))
6286     (if temporary
6287         nil                             ;Nothing to do.
6288       ;; If we have several article buffers, we kill them at exit.
6289       (unless gnus-single-article-buffer
6290         (gnus-kill-buffer gnus-article-buffer)
6291         (gnus-kill-buffer gnus-original-article-buffer)
6292         (setq gnus-article-current nil))
6293       (set-buffer buf)
6294       (if (not gnus-kill-summary-on-exit)
6295           (progn
6296             (gnus-deaden-summary)
6297             (setq mode nil))
6298        ;; We set all buffer-local variables to nil.  It is unclear why
6299         ;; this is needed, but if we don't, buffer-local variables are
6300         ;; not garbage-collected, it seems.  This would the lead to en
6301         ;; ever-growing Emacs.
6302         (gnus-summary-clear-local-variables)
6303         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6304           (gnus-summary-clear-local-variables))
6305         (when (get-buffer gnus-article-buffer)
6306           (bury-buffer gnus-article-buffer))
6307         ;; We clear the global counterparts of the buffer-local
6308         ;; variables as well, just to be on the safe side.
6309         (set-buffer gnus-group-buffer)
6310         (gnus-summary-clear-local-variables)
6311         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6312           (gnus-summary-clear-local-variables)))
6313       (setq gnus-current-select-method gnus-select-method)
6314       (pop-to-buffer gnus-group-buffer)
6315       (if (not quit-config)
6316           (progn
6317             (goto-char group-point)
6318             (gnus-configure-windows 'group 'force))
6319         (gnus-handle-ephemeral-exit quit-config))
6320       ;; Return to group mode buffer.
6321       (when (eq mode 'gnus-summary-mode)
6322         (gnus-kill-buffer buf))
6323       ;; Clear the current group name.
6324       (unless quit-config
6325         (setq gnus-newsgroup-name nil)))))
6326
6327 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6328 (defun gnus-summary-exit-no-update (&optional no-questions)
6329   "Quit reading current newsgroup without updating read article info."
6330   (interactive)
6331   (let* ((group gnus-newsgroup-name)
6332          (quit-config (gnus-group-quit-config group)))
6333     (when (or no-questions
6334               gnus-expert-user
6335               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6336       (gnus-async-halt-prefetch)
6337       (mapcar 'funcall
6338               (delq 'gnus-summary-expire-articles
6339                     (copy-sequence gnus-summary-prepare-exit-hook)))
6340       (when (gnus-buffer-live-p gnus-article-buffer)
6341         (save-excursion
6342           (set-buffer gnus-article-buffer)
6343           (mm-destroy-parts gnus-article-mime-handles)
6344           ;; Set it to nil for safety reason.
6345           (setq gnus-article-mime-handle-alist nil)
6346           (setq gnus-article-mime-handles nil)))
6347       ;; If we have several article buffers, we kill them at exit.
6348       (unless gnus-single-article-buffer
6349         (gnus-kill-buffer gnus-article-buffer)
6350         (gnus-kill-buffer gnus-original-article-buffer)
6351         (setq gnus-article-current nil))
6352       (if (not gnus-kill-summary-on-exit)
6353           (gnus-deaden-summary)
6354         (gnus-close-group group)
6355         (gnus-summary-clear-local-variables)
6356         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6357           (gnus-summary-clear-local-variables))
6358         (set-buffer gnus-group-buffer)
6359         (gnus-summary-clear-local-variables)
6360         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6361           (gnus-summary-clear-local-variables))
6362         (when (get-buffer gnus-summary-buffer)
6363           (kill-buffer gnus-summary-buffer)))
6364       (unless gnus-single-article-buffer
6365         (setq gnus-article-current nil))
6366       (when gnus-use-trees
6367         (gnus-tree-close group))
6368       (gnus-async-prefetch-remove-group group)
6369       (when (get-buffer gnus-article-buffer)
6370         (bury-buffer gnus-article-buffer))
6371       ;; Return to the group buffer.
6372       (gnus-configure-windows 'group 'force)
6373       ;; Clear the current group name.
6374       (setq gnus-newsgroup-name nil)
6375       (when (equal (gnus-group-group-name) group)
6376         (gnus-group-next-unread-group 1))
6377       (when quit-config
6378         (gnus-handle-ephemeral-exit quit-config)))))
6379
6380 (defun gnus-handle-ephemeral-exit (quit-config)
6381   "Handle movement when leaving an ephemeral group.
6382 The state which existed when entering the ephemeral is reset."
6383   (if (not (buffer-name (car quit-config)))
6384       (gnus-configure-windows 'group 'force)
6385     (set-buffer (car quit-config))
6386     (cond ((eq major-mode 'gnus-summary-mode)
6387            (gnus-set-global-variables))
6388           ((eq major-mode 'gnus-article-mode)
6389            (save-excursion
6390              ;; The `gnus-summary-buffer' variable may point
6391              ;; to the old summary buffer when using a single
6392              ;; article buffer.
6393              (unless (gnus-buffer-live-p gnus-summary-buffer)
6394                (set-buffer gnus-group-buffer))
6395              (set-buffer gnus-summary-buffer)
6396              (gnus-set-global-variables))))
6397     (if (or (eq (cdr quit-config) 'article)
6398             (eq (cdr quit-config) 'pick))
6399         (progn
6400           ;; The current article may be from the ephemeral group
6401           ;; thus it is best that we reload this article
6402           (gnus-summary-show-article)
6403           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6404               (gnus-configure-windows 'pick 'force)
6405             (gnus-configure-windows (cdr quit-config) 'force)))
6406       (gnus-configure-windows (cdr quit-config) 'force))
6407     (when (eq major-mode 'gnus-summary-mode)
6408       (gnus-summary-next-subject 1 nil t)
6409       (gnus-summary-recenter)
6410       (gnus-summary-position-point))))
6411
6412 ;;; Dead summaries.
6413
6414 (defvar gnus-dead-summary-mode-map nil)
6415
6416 (unless gnus-dead-summary-mode-map
6417   (setq gnus-dead-summary-mode-map (make-keymap))
6418   (suppress-keymap gnus-dead-summary-mode-map)
6419   (substitute-key-definition
6420    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6421   (dolist (key '("\C-d" "\r" "\177" [delete]))
6422     (define-key gnus-dead-summary-mode-map
6423       key 'gnus-summary-wake-up-the-dead))
6424   (dolist (key '("q" "Q"))
6425     (define-key gnus-dead-summary-mode-map key 'bury-buffer)))
6426
6427 (defvar gnus-dead-summary-mode nil
6428   "Minor mode for Gnus summary buffers.")
6429
6430 (defun gnus-dead-summary-mode (&optional arg)
6431   "Minor mode for Gnus summary buffers."
6432   (interactive "P")
6433   (when (eq major-mode 'gnus-summary-mode)
6434     (make-local-variable 'gnus-dead-summary-mode)
6435     (setq gnus-dead-summary-mode
6436           (if (null arg) (not gnus-dead-summary-mode)
6437             (> (prefix-numeric-value arg) 0)))
6438     (when gnus-dead-summary-mode
6439       (gnus-add-minor-mode
6440        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6441
6442 (defun gnus-deaden-summary ()
6443   "Make the current summary buffer into a dead summary buffer."
6444   ;; Kill any previous dead summary buffer.
6445   (when (and gnus-dead-summary
6446              (buffer-name gnus-dead-summary))
6447     (save-excursion
6448       (set-buffer gnus-dead-summary)
6449       (when gnus-dead-summary-mode
6450         (kill-buffer (current-buffer)))))
6451   ;; Make this the current dead summary.
6452   (setq gnus-dead-summary (current-buffer))
6453   (gnus-dead-summary-mode 1)
6454   (let ((name (buffer-name)))
6455     (when (string-match "Summary" name)
6456       (rename-buffer
6457        (concat (substring name 0 (match-beginning 0)) "Dead "
6458                (substring name (match-beginning 0)))
6459        t)
6460       (bury-buffer))))
6461
6462 (defun gnus-kill-or-deaden-summary (buffer)
6463   "Kill or deaden the summary BUFFER."
6464   (save-excursion
6465     (when (and (buffer-name buffer)
6466                (not gnus-single-article-buffer))
6467       (save-excursion
6468         (set-buffer buffer)
6469         (gnus-kill-buffer gnus-article-buffer)
6470         (gnus-kill-buffer gnus-original-article-buffer)))
6471     (cond
6472      ;; Kill the buffer.
6473      (gnus-kill-summary-on-exit
6474       (when (and gnus-use-trees
6475                  (gnus-buffer-exists-p buffer))
6476         (save-excursion
6477           (set-buffer buffer)
6478           (gnus-tree-close gnus-newsgroup-name)))
6479       (gnus-kill-buffer buffer))
6480      ;; Deaden the buffer.
6481      ((gnus-buffer-exists-p buffer)
6482       (save-excursion
6483         (set-buffer buffer)
6484         (gnus-deaden-summary))))))
6485
6486 (defun gnus-summary-wake-up-the-dead (&rest args)
6487   "Wake up the dead summary buffer."
6488   (interactive)
6489   (gnus-dead-summary-mode -1)
6490   (let ((name (buffer-name)))
6491     (when (string-match "Dead " name)
6492       (rename-buffer
6493        (concat (substring name 0 (match-beginning 0))
6494                (substring name (match-end 0)))
6495        t)))
6496   (gnus-message 3 "This dead summary is now alive again"))
6497
6498 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6499 (defun gnus-summary-fetch-faq (&optional faq-dir)
6500   "Fetch the FAQ for the current group.
6501 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6502 in."
6503   (interactive
6504    (list
6505     (when current-prefix-arg
6506       (completing-read
6507        "Faq dir: " (and (listp gnus-group-faq-directory)
6508                         (mapcar (lambda (file) (list file))
6509                                 gnus-group-faq-directory))))))
6510   (let (gnus-faq-buffer)
6511     (when (setq gnus-faq-buffer
6512                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6513       (gnus-configure-windows 'summary-faq))))
6514
6515 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6516 (defun gnus-summary-describe-group (&optional force)
6517   "Describe the current newsgroup."
6518   (interactive "P")
6519   (gnus-group-describe-group force gnus-newsgroup-name))
6520
6521 (defun gnus-summary-describe-briefly ()
6522   "Describe summary mode commands briefly."
6523   (interactive)
6524   (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")))
6525
6526 ;; Walking around group mode buffer from summary mode.
6527
6528 (defun gnus-summary-next-group (&optional no-article target-group backward)
6529   "Exit current newsgroup and then select next unread newsgroup.
6530 If prefix argument NO-ARTICLE is non-nil, no article is selected
6531 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6532 previous group instead."
6533   (interactive "P")
6534   ;; Stop pre-fetching.
6535   (gnus-async-halt-prefetch)
6536   (let ((current-group gnus-newsgroup-name)
6537         (current-buffer (current-buffer))
6538         entered)
6539     (while (not entered)
6540       ;; Then we find what group we are supposed to enter.
6541       (set-buffer gnus-group-buffer)
6542       (gnus-group-jump-to-group current-group)
6543       (setq target-group
6544             (or target-group
6545                 (if (eq gnus-keep-same-level 'best)
6546                     (gnus-summary-best-group gnus-newsgroup-name)
6547                   (gnus-summary-search-group backward gnus-keep-same-level))))
6548       (if (not target-group)
6549           ;; There are no further groups, so we return to the group
6550           ;; buffer.
6551           (progn
6552             (gnus-message 5 "Returning to the group buffer")
6553             (setq entered t)
6554             (when (gnus-buffer-live-p current-buffer)
6555               (set-buffer current-buffer)
6556               (gnus-summary-exit))
6557             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6558         ;; We try to enter the target group.
6559         (gnus-group-jump-to-group target-group)
6560         (let ((unreads (gnus-group-group-unread)))
6561           (if (and (or (eq t unreads)
6562                        (and unreads (not (zerop unreads))))
6563                    (progn
6564                      ;; Now we semi-exit this group to update Xrefs
6565                      ;; and all variables.  We can't do a real exit,
6566                      ;; because the window conf must remain the same
6567                      ;; in case the user is prompted for info, and we
6568                      ;; don't want the window conf to change before
6569                      ;; that...
6570                      (when (gnus-buffer-live-p current-buffer)
6571                        (set-buffer current-buffer)
6572                        (gnus-summary-exit t))
6573                      (gnus-summary-read-group
6574                       target-group nil no-article
6575                       (and (buffer-name current-buffer) current-buffer)
6576                       nil backward)))
6577               (setq entered t)
6578             (setq current-group target-group
6579                   target-group nil)))))))
6580
6581 (defun gnus-summary-prev-group (&optional no-article)
6582   "Exit current newsgroup and then select previous unread newsgroup.
6583 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6584   (interactive "P")
6585   (gnus-summary-next-group no-article nil t))
6586
6587 ;; Walking around summary lines.
6588
6589 (defun gnus-summary-first-subject (&optional unread undownloaded unseen)
6590   "Go to the first unread subject.
6591 If UNREAD is non-nil, go to the first unread article.
6592 Returns the article selected or nil if there are no unread articles."
6593   (interactive "P")
6594   (prog1
6595       (cond
6596        ;; Empty summary.
6597        ((null gnus-newsgroup-data)
6598         (gnus-message 3 "No articles in the group")
6599         nil)
6600        ;; Pick the first article.
6601        ((not unread)
6602         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6603         (gnus-data-number (car gnus-newsgroup-data)))
6604        ;; No unread articles.
6605        ((null gnus-newsgroup-unreads)
6606         (gnus-message 3 "No more unread articles")
6607         nil)
6608        ;; Find the first unread article.
6609        (t
6610         (let ((data gnus-newsgroup-data))
6611           (while (and data
6612                       (and (not (and undownloaded
6613                                      (eq gnus-undownloaded-mark
6614                                          (gnus-data-mark (car data)))))
6615                            (if unseen
6616                                (or (not (memq
6617                                          (gnus-data-number (car data))
6618                                          gnus-newsgroup-unseen))
6619                                    (not (gnus-data-unread-p (car data))))
6620                              (not (gnus-data-unread-p (car data))))))
6621             (setq data (cdr data)))
6622           (when data
6623             (goto-char (gnus-data-pos (car data)))
6624             (gnus-data-number (car data))))))
6625     (gnus-summary-position-point)))
6626
6627 (defun gnus-summary-next-subject (n &optional unread dont-display)
6628   "Go to next N'th summary line.
6629 If N is negative, go to the previous N'th subject line.
6630 If UNREAD is non-nil, only unread articles are selected.
6631 The difference between N and the actual number of steps taken is
6632 returned."
6633   (interactive "p")
6634   (let ((backward (< n 0))
6635         (n (abs n)))
6636     (while (and (> n 0)
6637                 (if backward
6638                     (gnus-summary-find-prev unread)
6639                   (gnus-summary-find-next unread)))
6640       (unless (zerop (setq n (1- n)))
6641         (gnus-summary-show-thread)))
6642     (when (/= 0 n)
6643       (gnus-message 7 "No more%s articles"
6644                     (if unread " unread" "")))
6645     (unless dont-display
6646       (gnus-summary-recenter)
6647       (gnus-summary-position-point))
6648     n))
6649
6650 (defun gnus-summary-next-unread-subject (n)
6651   "Go to next N'th unread summary line."
6652   (interactive "p")
6653   (gnus-summary-next-subject n t))
6654
6655 (defun gnus-summary-prev-subject (n &optional unread)
6656   "Go to previous N'th summary line.
6657 If optional argument UNREAD is non-nil, only unread article is selected."
6658   (interactive "p")
6659   (gnus-summary-next-subject (- n) unread))
6660
6661 (defun gnus-summary-prev-unread-subject (n)
6662   "Go to previous N'th unread summary line."
6663   (interactive "p")
6664   (gnus-summary-next-subject (- n) t))
6665
6666 (defun gnus-summary-goto-subject (article &optional force silent)
6667   "Go the subject line of ARTICLE.
6668 If FORCE, also allow jumping to articles not currently shown."
6669   (interactive "nArticle number: ")
6670   (unless (numberp article)
6671     (error "Article %s is not a number" article))
6672   (let ((b (point))
6673         (data (gnus-data-find article)))
6674     ;; We read in the article if we have to.
6675     (and (not data)
6676          force
6677          (gnus-summary-insert-subject
6678           article
6679           (if (or (numberp force) (vectorp force)) force)
6680           t)
6681          (setq data (gnus-data-find article)))
6682     (goto-char b)
6683     (if (not data)
6684         (progn
6685           (unless silent
6686             (gnus-message 3 "Can't find article %d" article))
6687           nil)
6688       (let ((pt (gnus-data-pos data)))
6689         (goto-char pt)
6690         (gnus-summary-set-article-display-arrow pt))
6691       (gnus-summary-position-point)
6692       article)))
6693
6694 ;; Walking around summary lines with displaying articles.
6695
6696 (defun gnus-summary-expand-window (&optional arg)
6697   "Make the summary buffer take up the entire Emacs frame.
6698 Given a prefix, will force an `article' buffer configuration."
6699   (interactive "P")
6700   (if arg
6701       (gnus-configure-windows 'article 'force)
6702     (gnus-configure-windows 'summary 'force)))
6703
6704 (defun gnus-summary-display-article (article &optional all-header)
6705   "Display ARTICLE in article buffer."
6706   (when (gnus-buffer-live-p gnus-article-buffer)
6707     (with-current-buffer gnus-article-buffer
6708       (mm-enable-multibyte)))
6709   (gnus-set-global-variables)
6710   (when (gnus-buffer-live-p gnus-article-buffer)
6711     (with-current-buffer gnus-article-buffer
6712       (setq gnus-article-charset gnus-newsgroup-charset)
6713       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6714       (mm-enable-multibyte)))
6715   (if (null article)
6716       nil
6717     (prog1
6718         (if gnus-summary-display-article-function
6719             (funcall gnus-summary-display-article-function article all-header)
6720           (gnus-article-prepare article all-header))
6721       (gnus-run-hooks 'gnus-select-article-hook)
6722       (when (and gnus-current-article
6723                  (not (zerop gnus-current-article)))
6724         (gnus-summary-goto-subject gnus-current-article))
6725       (gnus-summary-recenter)
6726       (when (and gnus-use-trees gnus-show-threads)
6727         (gnus-possibly-generate-tree article)
6728         (gnus-highlight-selected-tree article))
6729       ;; Successfully display article.
6730       (gnus-article-set-window-start
6731        (cdr (assq article gnus-newsgroup-bookmarks))))))
6732
6733 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6734   "Select the current article.
6735 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6736 non-nil, the article will be re-fetched even if it already present in
6737 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6738 be displayed."
6739   ;; Make sure we are in the summary buffer to work around bbdb bug.
6740   (unless (eq major-mode 'gnus-summary-mode)
6741     (set-buffer gnus-summary-buffer))
6742   (let ((article (or article (gnus-summary-article-number)))
6743         (all-headers (not (not all-headers))) ;Must be t or nil.
6744         gnus-summary-display-article-function)
6745     (and (not pseudo)
6746          (gnus-summary-article-pseudo-p article)
6747          (error "This is a pseudo-article"))
6748     (save-excursion
6749       (set-buffer gnus-summary-buffer)
6750       (if (or (and gnus-single-article-buffer
6751                    (or (null gnus-current-article)
6752                        (null gnus-article-current)
6753                        (null (get-buffer gnus-article-buffer))
6754                        (not (eq article (cdr gnus-article-current)))
6755                        (not (equal (car gnus-article-current)
6756                                    gnus-newsgroup-name))))
6757               (and (not gnus-single-article-buffer)
6758                    (or (null gnus-current-article)
6759                        (not (eq gnus-current-article article))))
6760               force)
6761           ;; The requested article is different from the current article.
6762           (progn
6763             (gnus-summary-display-article article all-headers)
6764             (when (gnus-buffer-live-p gnus-article-buffer)
6765               (with-current-buffer gnus-article-buffer
6766                 (if (not gnus-article-decoded-p) ;; a local variable
6767                     (mm-disable-multibyte))))
6768             (gnus-article-set-window-start
6769              (cdr (assq article gnus-newsgroup-bookmarks)))
6770             article)
6771         'old))))
6772
6773 (defun gnus-summary-force-verify-and-decrypt ()
6774   (interactive)
6775   (let ((mm-verify-option 'known)
6776         (mm-decrypt-option 'known)
6777         (gnus-buttonized-mime-types (append (list "multipart/signed"
6778                                                   "multipart/encrypted")
6779                                             gnus-buttonized-mime-types)))
6780     (gnus-summary-select-article nil 'force)))
6781
6782 (defun gnus-summary-set-current-mark (&optional current-mark)
6783   "Obsolete function."
6784   nil)
6785
6786 (defun gnus-summary-next-article (&optional unread subject backward push)
6787   "Select the next article.
6788 If UNREAD, only unread articles are selected.
6789 If SUBJECT, only articles with SUBJECT are selected.
6790 If BACKWARD, the previous article is selected instead of the next."
6791   (interactive "P")
6792   (cond
6793    ;; Is there such an article?
6794    ((and (gnus-summary-search-forward unread subject backward)
6795          (or (gnus-summary-display-article (gnus-summary-article-number))
6796              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6797     (gnus-summary-position-point))
6798    ;; If not, we try the first unread, if that is wanted.
6799    ((and subject
6800          gnus-auto-select-same
6801          (gnus-summary-first-unread-article))
6802     (gnus-summary-position-point)
6803     (gnus-message 6 "Wrapped"))
6804    ;; Try to get next/previous article not displayed in this group.
6805    ((and gnus-auto-extend-newsgroup
6806          (not unread) (not subject))
6807     (gnus-summary-goto-article
6808      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6809      nil (count-lines (point-min) (point))))
6810    ;; Go to next/previous group.
6811    (t
6812     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6813       (gnus-summary-jump-to-group gnus-newsgroup-name))
6814     (let ((cmd last-command-char)
6815           (point
6816            (save-excursion
6817              (set-buffer gnus-group-buffer)
6818              (point)))
6819           (group
6820            (if (eq gnus-keep-same-level 'best)
6821                (gnus-summary-best-group gnus-newsgroup-name)
6822              (gnus-summary-search-group backward gnus-keep-same-level))))
6823       ;; For some reason, the group window gets selected.  We change
6824       ;; it back.
6825       (select-window (get-buffer-window (current-buffer)))
6826       ;; Select next unread newsgroup automagically.
6827       (cond
6828        ((or (not gnus-auto-select-next)
6829             (not cmd))
6830         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6831        ((or (eq gnus-auto-select-next 'quietly)
6832             (and (eq gnus-auto-select-next 'slightly-quietly)
6833                  push)
6834             (and (eq gnus-auto-select-next 'almost-quietly)
6835                  (gnus-summary-last-article-p)))
6836         ;; Select quietly.
6837         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6838             (gnus-summary-exit)
6839           (gnus-message 7 "No more%s articles (%s)..."
6840                         (if unread " unread" "")
6841                         (if group (concat "selecting " group)
6842                           "exiting"))
6843           (gnus-summary-next-group nil group backward)))
6844        (t
6845         (when (gnus-key-press-event-p last-input-event)
6846           (gnus-summary-walk-group-buffer
6847            gnus-newsgroup-name cmd unread backward point))))))))
6848
6849 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6850   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6851                       (?\C-p (gnus-group-prev-unread-group 1))))
6852         (cursor-in-echo-area t)
6853         keve key group ended)
6854     (save-excursion
6855       (set-buffer gnus-group-buffer)
6856       (goto-char start)
6857       (setq group
6858             (if (eq gnus-keep-same-level 'best)
6859                 (gnus-summary-best-group gnus-newsgroup-name)
6860               (gnus-summary-search-group backward gnus-keep-same-level))))
6861     (while (not ended)
6862       (gnus-message
6863        5 "No more%s articles%s" (if unread " unread" "")
6864        (if (and group
6865                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6866            (format " (Type %s for %s [%s])"
6867                    (single-key-description cmd) group
6868                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6869          (format " (Type %s to exit %s)"
6870                  (single-key-description cmd)
6871                  gnus-newsgroup-name)))
6872       ;; Confirm auto selection.
6873       (setq key (car (setq keve (gnus-read-event-char))))
6874       (setq ended t)
6875       (cond
6876        ((assq key keystrokes)
6877         (let ((obuf (current-buffer)))
6878           (switch-to-buffer gnus-group-buffer)
6879           (when group
6880             (gnus-group-jump-to-group group))
6881           (eval (cadr (assq key keystrokes)))
6882           (setq group (gnus-group-group-name))
6883           (switch-to-buffer obuf))
6884         (setq ended nil))
6885        ((equal key cmd)
6886         (if (or (not group)
6887                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6888             (gnus-summary-exit)
6889           (gnus-summary-next-group nil group backward)))
6890        (t
6891         (push (cdr keve) unread-command-events))))))
6892
6893 (defun gnus-summary-next-unread-article ()
6894   "Select unread article after current one."
6895   (interactive)
6896   (gnus-summary-next-article
6897    (or (not (eq gnus-summary-goto-unread 'never))
6898        (gnus-summary-last-article-p (gnus-summary-article-number)))
6899    (and gnus-auto-select-same
6900         (gnus-summary-article-subject))))
6901
6902 (defun gnus-summary-prev-article (&optional unread subject)
6903   "Select the article after the current one.
6904 If UNREAD is non-nil, only unread articles are selected."
6905   (interactive "P")
6906   (gnus-summary-next-article unread subject t))
6907
6908 (defun gnus-summary-prev-unread-article ()
6909   "Select unread article before current one."
6910   (interactive)
6911   (gnus-summary-prev-article
6912    (or (not (eq gnus-summary-goto-unread 'never))
6913        (gnus-summary-first-article-p (gnus-summary-article-number)))
6914    (and gnus-auto-select-same
6915         (gnus-summary-article-subject))))
6916
6917 (defun gnus-summary-next-page (&optional lines circular)
6918   "Show next page of the selected article.
6919 If at the end of the current article, select the next article.
6920 LINES says how many lines should be scrolled up.
6921
6922 If CIRCULAR is non-nil, go to the start of the article instead of
6923 selecting the next article when reaching the end of the current
6924 article."
6925   (interactive "P")
6926   (setq gnus-summary-buffer (current-buffer))
6927   (gnus-set-global-variables)
6928   (let ((article (gnus-summary-article-number))
6929         (article-window (get-buffer-window gnus-article-buffer t))
6930         endp)
6931     ;; If the buffer is empty, we have no article.
6932     (unless article
6933       (error "No article to select"))
6934     (gnus-configure-windows 'article)
6935     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6936         (if (and (eq gnus-summary-goto-unread 'never)
6937                  (not (gnus-summary-last-article-p article)))
6938             (gnus-summary-next-article)
6939           (gnus-summary-next-unread-article))
6940       (if (or (null gnus-current-article)
6941               (null gnus-article-current)
6942               (/= article (cdr gnus-article-current))
6943               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6944           ;; Selected subject is different from current article's.
6945           (gnus-summary-display-article article)
6946         (when article-window
6947           (gnus-eval-in-buffer-window gnus-article-buffer
6948             (setq endp (gnus-article-next-page lines)))
6949           (when endp
6950             (cond (circular
6951                    (gnus-summary-beginning-of-article))
6952                   (lines
6953                    (gnus-message 3 "End of message"))
6954                   ((null lines)
6955                    (if (and (eq gnus-summary-goto-unread 'never)
6956                             (not (gnus-summary-last-article-p article)))
6957                        (gnus-summary-next-article)
6958                      (gnus-summary-next-unread-article))))))))
6959     (gnus-summary-recenter)
6960     (gnus-summary-position-point)))
6961
6962 (defun gnus-summary-prev-page (&optional lines move)
6963   "Show previous page of selected article.
6964 Argument LINES specifies lines to be scrolled down.
6965 If MOVE, move to the previous unread article if point is at
6966 the beginning of the buffer."
6967   (interactive "P")
6968   (let ((article (gnus-summary-article-number))
6969         (article-window (get-buffer-window gnus-article-buffer t))
6970         endp)
6971     (gnus-configure-windows 'article)
6972     (if (or (null gnus-current-article)
6973             (null gnus-article-current)
6974             (/= article (cdr gnus-article-current))
6975             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6976         ;; Selected subject is different from current article's.
6977         (gnus-summary-display-article article)
6978       (gnus-summary-recenter)
6979       (when article-window
6980         (gnus-eval-in-buffer-window gnus-article-buffer
6981           (setq endp (gnus-article-prev-page lines)))
6982         (when (and move endp)
6983           (cond (lines
6984                  (gnus-message 3 "Beginning of message"))
6985                 ((null lines)
6986                  (if (and (eq gnus-summary-goto-unread 'never)
6987                           (not (gnus-summary-first-article-p article)))
6988                      (gnus-summary-prev-article)
6989                    (gnus-summary-prev-unread-article))))))))
6990   (gnus-summary-position-point))
6991
6992 (defun gnus-summary-prev-page-or-article (&optional lines)
6993   "Show previous page of selected article.
6994 Argument LINES specifies lines to be scrolled down.
6995 If at the beginning of the article, go to the next article."
6996   (interactive "P")
6997   (gnus-summary-prev-page lines t))
6998
6999 (defun gnus-summary-scroll-up (lines)
7000   "Scroll up (or down) one line current article.
7001 Argument LINES specifies lines to be scrolled up (or down if negative)."
7002   (interactive "p")
7003   (gnus-configure-windows 'article)
7004   (gnus-summary-show-thread)
7005   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
7006     (gnus-eval-in-buffer-window gnus-article-buffer
7007       (cond ((> lines 0)
7008              (when (gnus-article-next-page lines)
7009                (gnus-message 3 "End of message")))
7010             ((< lines 0)
7011              (gnus-article-prev-page (- lines))))))
7012   (gnus-summary-recenter)
7013   (gnus-summary-position-point))
7014
7015 (defun gnus-summary-scroll-down (lines)
7016   "Scroll down (or up) one line current article.
7017 Argument LINES specifies lines to be scrolled down (or up if negative)."
7018   (interactive "p")
7019   (gnus-summary-scroll-up (- lines)))
7020
7021 (defun gnus-summary-next-same-subject ()
7022   "Select next article which has the same subject as current one."
7023   (interactive)
7024   (gnus-summary-next-article nil (gnus-summary-article-subject)))
7025
7026 (defun gnus-summary-prev-same-subject ()
7027   "Select previous article which has the same subject as current one."
7028   (interactive)
7029   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
7030
7031 (defun gnus-summary-next-unread-same-subject ()
7032   "Select next unread article which has the same subject as current one."
7033   (interactive)
7034   (gnus-summary-next-article t (gnus-summary-article-subject)))
7035
7036 (defun gnus-summary-prev-unread-same-subject ()
7037   "Select previous unread article which has the same subject as current one."
7038   (interactive)
7039   (gnus-summary-prev-article t (gnus-summary-article-subject)))
7040
7041 (defun gnus-summary-first-unread-article ()
7042   "Select the first unread article.
7043 Return nil if there are no unread articles."
7044   (interactive)
7045   (prog1
7046       (when (gnus-summary-first-subject t)
7047         (gnus-summary-show-thread)
7048         (gnus-summary-first-subject t)
7049         (gnus-summary-display-article (gnus-summary-article-number)))
7050     (gnus-summary-position-point)))
7051
7052 (defun gnus-summary-first-unread-subject ()
7053   "Place the point on the subject line of the first unread article.
7054 Return nil if there are no unread articles."
7055   (interactive)
7056   (prog1
7057       (when (gnus-summary-first-subject t)
7058         (gnus-summary-show-thread)
7059         (gnus-summary-first-subject t))
7060     (gnus-summary-position-point)))
7061
7062 (defun gnus-summary-first-unseen-subject ()
7063   "Place the point on the subject line of the first unseen article.
7064 Return nil if there are no unseen articles."
7065   (interactive)
7066   (prog1
7067       (when (gnus-summary-first-subject t t t)
7068         (gnus-summary-show-thread)
7069         (gnus-summary-first-subject t t t))
7070     (gnus-summary-position-point)))
7071
7072 (defun gnus-summary-first-unseen-or-unread-subject ()
7073   "Place the point on the subject line of the first unseen article.
7074 Return nil if there are no unseen articles."
7075   (interactive)
7076   (prog1
7077       (unless (when (gnus-summary-first-subject t t t)
7078                 (gnus-summary-show-thread)
7079                 (gnus-summary-first-subject t t t))
7080         (when (gnus-summary-first-subject t)
7081           (gnus-summary-show-thread)
7082           (gnus-summary-first-subject t)))
7083     (gnus-summary-position-point)))
7084
7085 (defun gnus-summary-first-article ()
7086   "Select the first article.
7087 Return nil if there are no articles."
7088   (interactive)
7089   (prog1
7090       (when (gnus-summary-first-subject)
7091         (gnus-summary-show-thread)
7092         (gnus-summary-first-subject)
7093         (gnus-summary-display-article (gnus-summary-article-number)))
7094     (gnus-summary-position-point)))
7095
7096 (defun gnus-summary-best-unread-article (&optional arg)
7097   "Select the unread article with the highest score.
7098 If given a prefix argument, select the next unread article that has a
7099 score higher than the default score."
7100   (interactive "P")
7101   (let ((article (if arg
7102                      (gnus-summary-better-unread-subject)
7103                    (gnus-summary-best-unread-subject))))
7104     (if article
7105         (gnus-summary-goto-article article)
7106       (error "No unread articles"))))
7107
7108 (defun gnus-summary-best-unread-subject ()
7109   "Select the unread subject with the highest score."
7110   (interactive)
7111   (let ((best -1000000)
7112         (data gnus-newsgroup-data)
7113         article score)
7114     (while data
7115       (and (gnus-data-unread-p (car data))
7116            (> (setq score
7117                     (gnus-summary-article-score (gnus-data-number (car data))))
7118               best)
7119            (setq best score
7120                  article (gnus-data-number (car data))))
7121       (setq data (cdr data)))
7122     (when article
7123       (gnus-summary-goto-subject article))
7124     (gnus-summary-position-point)
7125     article))
7126
7127 (defun gnus-summary-better-unread-subject ()
7128   "Select the first unread subject that has a score over the default score."
7129   (interactive)
7130   (let ((data gnus-newsgroup-data)
7131         article score)
7132     (while (and (setq article (gnus-data-number (car data)))
7133                 (or (gnus-data-read-p (car data))
7134                     (not (> (gnus-summary-article-score article)
7135                             gnus-summary-default-score))))
7136       (setq data (cdr data)))
7137     (when article
7138       (gnus-summary-goto-subject article))
7139     (gnus-summary-position-point)
7140     article))
7141
7142 (defun gnus-summary-last-subject ()
7143   "Go to the last displayed subject line in the group."
7144   (let ((article (gnus-data-number (car (gnus-data-list t)))))
7145     (when article
7146       (gnus-summary-goto-subject article))))
7147
7148 (defun gnus-summary-goto-article (article &optional all-headers force)
7149   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
7150 If ALL-HEADERS is non-nil, no header lines are hidden.
7151 If FORCE, go to the article even if it isn't displayed.  If FORCE
7152 is a number, it is the line the article is to be displayed on."
7153   (interactive
7154    (list
7155     (completing-read
7156      "Article number or Message-ID: "
7157      (mapcar (lambda (number) (list (int-to-string number)))
7158              gnus-newsgroup-limit))
7159     current-prefix-arg
7160     t))
7161   (prog1
7162       (if (and (stringp article)
7163                (string-match "@" article))
7164           (gnus-summary-refer-article article)
7165         (when (stringp article)
7166           (setq article (string-to-number article)))
7167         (if (gnus-summary-goto-subject article force)
7168             (gnus-summary-display-article article all-headers)
7169           (gnus-message 4 "Couldn't go to article %s" article) nil))
7170     (gnus-summary-position-point)))
7171
7172 (defun gnus-summary-goto-last-article ()
7173   "Go to the previously read article."
7174   (interactive)
7175   (prog1
7176       (when gnus-last-article
7177         (gnus-summary-goto-article gnus-last-article nil t))
7178     (gnus-summary-position-point)))
7179
7180 (defun gnus-summary-pop-article (number)
7181   "Pop one article off the history and go to the previous.
7182 NUMBER articles will be popped off."
7183   (interactive "p")
7184   (let (to)
7185     (setq gnus-newsgroup-history
7186           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
7187     (if to
7188         (gnus-summary-goto-article (car to) nil t)
7189       (error "Article history empty")))
7190   (gnus-summary-position-point))
7191
7192 ;; Summary commands and functions for limiting the summary buffer.
7193
7194 (defun gnus-summary-limit-to-articles (n)
7195   "Limit the summary buffer to the next N articles.
7196 If not given a prefix, use the process marked articles instead."
7197   (interactive "P")
7198   (prog1
7199       (let ((articles (gnus-summary-work-articles n)))
7200         (setq gnus-newsgroup-processable nil)
7201         (gnus-summary-limit articles))
7202     (gnus-summary-position-point)))
7203
7204 (defun gnus-summary-pop-limit (&optional total)
7205   "Restore the previous limit.
7206 If given a prefix, remove all limits."
7207   (interactive "P")
7208   (when total
7209     (setq gnus-newsgroup-limits
7210           (list (mapcar (lambda (h) (mail-header-number h))
7211                         gnus-newsgroup-headers))))
7212   (unless gnus-newsgroup-limits
7213     (error "No limit to pop"))
7214   (prog1
7215       (gnus-summary-limit nil 'pop)
7216     (gnus-summary-position-point)))
7217
7218 (defun gnus-summary-limit-to-subject (subject &optional header not-matching)
7219   "Limit the summary buffer to articles that have subjects that match a regexp.
7220 If NOT-MATCHING, excluding articles that have subjects that match a regexp."
7221   (interactive
7222    (list (read-string (if current-prefix-arg
7223                           "Exclude subject (regexp): "
7224                         "Limit to subject (regexp): "))
7225          nil current-prefix-arg))
7226   (unless header
7227     (setq header "subject"))
7228   (when (not (equal "" subject))
7229     (prog1
7230         (let ((articles (gnus-summary-find-matching
7231                          (or header "subject") subject 'all nil nil
7232                          not-matching)))
7233           (unless articles
7234             (error "Found no matches for \"%s\"" subject))
7235           (gnus-summary-limit articles))
7236       (gnus-summary-position-point))))
7237
7238 (defun gnus-summary-limit-to-author (from &optional not-matching)
7239   "Limit the summary buffer to articles that have authors that match a regexp.
7240 If NOT-MATCHING, excluding articles that have authors that match a regexp."
7241   (interactive
7242    (list (read-string (if current-prefix-arg
7243                           "Exclude author (regexp): "
7244                         "Limit to author (regexp): "))
7245          current-prefix-arg))
7246   (gnus-summary-limit-to-subject from "from" not-matching))
7247
7248 (defun gnus-summary-limit-to-age (age &optional younger-p)
7249   "Limit the summary buffer to articles that are older than (or equal) AGE days.
7250 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
7251 articles that are younger than AGE days."
7252   (interactive
7253    (let ((younger current-prefix-arg)
7254          (days-got nil)
7255          days)
7256      (while (not days-got)
7257        (setq days (if younger
7258                       (read-string "Limit to articles within (in days): ")
7259                     (read-string "Limit to articles older than (in days): ")))
7260        (when (> (length days) 0)
7261          (setq days (read days)))
7262        (if (numberp days)
7263            (progn
7264              (setq days-got t)
7265              (if (< days 0)
7266                  (progn
7267                    (setq younger (not younger))
7268                    (setq days (* days -1)))))
7269          (message "Please enter a number.")
7270          (sleep-for 1)))
7271      (list days younger)))
7272   (prog1
7273       (let ((data gnus-newsgroup-data)
7274             (cutoff (days-to-time age))
7275             articles d date is-younger)
7276         (while (setq d (pop data))
7277           (when (and (vectorp (gnus-data-header d))
7278                      (setq date (mail-header-date (gnus-data-header d))))
7279             (setq is-younger (time-less-p
7280                               (time-since (condition-case ()
7281                                               (date-to-time date)
7282                                             (error '(0 0))))
7283                               cutoff))
7284             (when (if younger-p
7285                       is-younger
7286                     (not is-younger))
7287               (push (gnus-data-number d) articles))))
7288         (gnus-summary-limit (nreverse articles)))
7289     (gnus-summary-position-point)))
7290
7291 (defun gnus-summary-limit-to-extra (header regexp &optional not-matching)
7292   "Limit the summary buffer to articles that match an 'extra' header."
7293   (interactive
7294    (let ((header
7295           (intern
7296            (gnus-completing-read-with-default
7297             (symbol-name (car gnus-extra-headers))
7298             (if current-prefix-arg
7299                 "Exclude extra header:"
7300               "Limit extra header:")
7301             (mapcar (lambda (x)
7302                       (cons (symbol-name x) x))
7303                     gnus-extra-headers)
7304             nil
7305             t))))
7306      (list header
7307            (read-string (format "%s header %s (regexp): "
7308                                 (if current-prefix-arg "Exclude" "Limit to")
7309                                 header))
7310            current-prefix-arg)))
7311   (when (not (equal "" regexp))
7312     (prog1
7313         (let ((articles (gnus-summary-find-matching
7314                          (cons 'extra header) regexp 'all nil nil
7315                          not-matching)))
7316           (unless articles
7317             (error "Found no matches for \"%s\"" regexp))
7318           (gnus-summary-limit articles))
7319       (gnus-summary-position-point))))
7320
7321 (defun gnus-summary-limit-to-display-predicate ()
7322   "Limit the summary buffer to the predicated in the `display' group parameter."
7323   (interactive)
7324   (unless gnus-newsgroup-display
7325     (error "There is no `display' group parameter"))
7326   (let (articles)
7327     (dolist (number gnus-newsgroup-articles)
7328       (when (funcall gnus-newsgroup-display)
7329         (push number articles)))
7330     (gnus-summary-limit articles))
7331   (gnus-summary-position-point))
7332
7333 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7334 (make-obsolete
7335  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
7336
7337 (defun gnus-summary-limit-to-unread (&optional all)
7338   "Limit the summary buffer to articles that are not marked as read.
7339 If ALL is non-nil, limit strictly to unread articles."
7340   (interactive "P")
7341   (if all
7342       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
7343     (gnus-summary-limit-to-marks
7344      ;; Concat all the marks that say that an article is read and have
7345      ;; those removed.
7346      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
7347            gnus-killed-mark gnus-spam-mark gnus-kill-file-mark
7348            gnus-low-score-mark gnus-expirable-mark
7349            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
7350            gnus-duplicate-mark gnus-souped-mark)
7351      'reverse)))
7352
7353 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
7354 (make-obsolete 'gnus-summary-delete-marked-with
7355                'gnus-summary-limit-exclude-marks)
7356
7357 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
7358   "Exclude articles that are marked with MARKS (e.g. \"DK\").
7359 If REVERSE, limit the summary buffer to articles that are marked
7360 with MARKS.  MARKS can either be a string of marks or a list of marks.
7361 Returns how many articles were removed."
7362   (interactive "sMarks: ")
7363   (gnus-summary-limit-to-marks marks t))
7364
7365 (defun gnus-summary-limit-to-marks (marks &optional reverse)
7366   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
7367 If REVERSE (the prefix), limit the summary buffer to articles that are
7368 not marked with MARKS.  MARKS can either be a string of marks or a
7369 list of marks.
7370 Returns how many articles were removed."
7371   (interactive "sMarks: \nP")
7372   (prog1
7373       (let ((data gnus-newsgroup-data)
7374             (marks (if (listp marks) marks
7375                      (append marks nil))) ; Transform to list.
7376             articles)
7377         (while data
7378           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
7379                   (memq (gnus-data-mark (car data)) marks))
7380             (push (gnus-data-number (car data)) articles))
7381           (setq data (cdr data)))
7382         (gnus-summary-limit articles))
7383     (gnus-summary-position-point)))
7384
7385 (defun gnus-summary-limit-to-score (score)
7386   "Limit to articles with score at or above SCORE."
7387   (interactive "NLimit to articles with score of at least: ")
7388   (let ((data gnus-newsgroup-data)
7389         articles)
7390     (while data
7391       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
7392                 score)
7393         (push (gnus-data-number (car data)) articles))
7394       (setq data (cdr data)))
7395     (prog1
7396         (gnus-summary-limit articles)
7397       (gnus-summary-position-point))))
7398
7399 (defun gnus-summary-limit-to-unseen ()
7400   "Limit to unseen articles."
7401   (interactive)
7402   (prog1
7403       (gnus-summary-limit gnus-newsgroup-unseen)
7404     (gnus-summary-position-point)))
7405
7406 (defun gnus-summary-limit-include-thread (id)
7407   "Display all the hidden articles that is in the thread with ID in it.
7408 When called interactively, ID is the Message-ID of the current
7409 article."
7410   (interactive (list (mail-header-id (gnus-summary-article-header))))
7411   (let ((articles (gnus-articles-in-thread
7412                    (gnus-id-to-thread (gnus-root-id id)))))
7413     (prog1
7414         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7415       (gnus-summary-limit-include-matching-articles
7416        "subject"
7417        (regexp-quote (gnus-simplify-subject-re
7418                       (mail-header-subject (gnus-id-to-header id)))))
7419       (gnus-summary-position-point))))
7420
7421 (defun gnus-summary-limit-include-matching-articles (header regexp)
7422   "Display all the hidden articles that have HEADERs that match REGEXP."
7423   (interactive (list (read-string "Match on header: ")
7424                      (read-string "Regexp: ")))
7425   (let ((articles (gnus-find-matching-articles header regexp)))
7426     (prog1
7427         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7428       (gnus-summary-position-point))))
7429
7430 (defun gnus-summary-limit-include-dormant ()
7431   "Display all the hidden articles that are marked as dormant.
7432 Note that this command only works on a subset of the articles currently
7433 fetched for this group."
7434   (interactive)
7435   (unless gnus-newsgroup-dormant
7436     (error "There are no dormant articles in this group"))
7437   (prog1
7438       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7439     (gnus-summary-position-point)))
7440
7441 (defun gnus-summary-limit-exclude-dormant ()
7442   "Hide all dormant articles."
7443   (interactive)
7444   (prog1
7445       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7446     (gnus-summary-position-point)))
7447
7448 (defun gnus-summary-limit-exclude-childless-dormant ()
7449   "Hide all dormant articles that have no children."
7450   (interactive)
7451   (let ((data (gnus-data-list t))
7452         articles d children)
7453     ;; Find all articles that are either not dormant or have
7454     ;; children.
7455     (while (setq d (pop data))
7456       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7457                 (and (setq children
7458                            (gnus-article-children (gnus-data-number d)))
7459                      (let (found)
7460                        (while children
7461                          (when (memq (car children) articles)
7462                            (setq children nil
7463                                  found t))
7464                          (pop children))
7465                        found)))
7466         (push (gnus-data-number d) articles)))
7467     ;; Do the limiting.
7468     (prog1
7469         (gnus-summary-limit articles)
7470       (gnus-summary-position-point))))
7471
7472 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7473   "Mark all unread excluded articles as read.
7474 If ALL, mark even excluded ticked and dormants as read."
7475   (interactive "P")
7476   (setq gnus-newsgroup-limit (sort gnus-newsgroup-limit '<))
7477   (let ((articles (gnus-sorted-ndifference
7478                    (sort
7479                     (mapcar (lambda (h) (mail-header-number h))
7480                             gnus-newsgroup-headers)
7481                     '<)
7482                    gnus-newsgroup-limit))
7483         article)
7484     (setq gnus-newsgroup-unreads
7485           (gnus-sorted-intersection gnus-newsgroup-unreads
7486                                     gnus-newsgroup-limit))
7487     (if all
7488         (setq gnus-newsgroup-dormant nil
7489               gnus-newsgroup-marked nil
7490               gnus-newsgroup-reads
7491               (nconc
7492                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7493                gnus-newsgroup-reads))
7494       (while (setq article (pop articles))
7495         (unless (or (memq article gnus-newsgroup-dormant)
7496                     (memq article gnus-newsgroup-marked))
7497           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7498
7499 (defun gnus-summary-limit (articles &optional pop)
7500   (if pop
7501       ;; We pop the previous limit off the stack and use that.
7502       (setq articles (car gnus-newsgroup-limits)
7503             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7504     ;; We use the new limit, so we push the old limit on the stack.
7505     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7506   ;; Set the limit.
7507   (setq gnus-newsgroup-limit articles)
7508   (let ((total (length gnus-newsgroup-data))
7509         (data (gnus-data-find-list (gnus-summary-article-number)))
7510         (gnus-summary-mark-below nil)   ; Inhibit this.
7511         found)
7512     ;; This will do all the work of generating the new summary buffer
7513     ;; according to the new limit.
7514     (gnus-summary-prepare)
7515     ;; Hide any threads, possibly.
7516     (gnus-summary-maybe-hide-threads)
7517     ;; Try to return to the article you were at, or one in the
7518     ;; neighborhood.
7519     (when data
7520       ;; We try to find some article after the current one.
7521       (while data
7522         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7523           (setq data nil
7524                 found t))
7525         (setq data (cdr data))))
7526     (unless found
7527       ;; If there is no data, that means that we were after the last
7528       ;; article.  The same goes when we can't find any articles
7529       ;; after the current one.
7530       (goto-char (point-max))
7531       (gnus-summary-find-prev))
7532     (gnus-set-mode-line 'summary)
7533     ;; We return how many articles were removed from the summary
7534     ;; buffer as a result of the new limit.
7535     (- total (length gnus-newsgroup-data))))
7536
7537 (defsubst gnus-invisible-cut-children (threads)
7538   (let ((num 0))
7539     (while threads
7540       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7541         (incf num))
7542       (pop threads))
7543     (< num 2)))
7544
7545 (defsubst gnus-cut-thread (thread)
7546   "Go forwards in the thread until we find an article that we want to display."
7547   (when (or (eq gnus-fetch-old-headers 'some)
7548             (eq gnus-fetch-old-headers 'invisible)
7549             (numberp gnus-fetch-old-headers)
7550             (eq gnus-build-sparse-threads 'some)
7551             (eq gnus-build-sparse-threads 'more))
7552     ;; Deal with old-fetched headers and sparse threads.
7553     (while (and
7554             thread
7555             (or
7556              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7557              (gnus-summary-article-ancient-p
7558               (mail-header-number (car thread))))
7559             (if (or (<= (length (cdr thread)) 1)
7560                     (eq gnus-fetch-old-headers 'invisible))
7561                 (setq gnus-newsgroup-limit
7562                       (delq (mail-header-number (car thread))
7563                             gnus-newsgroup-limit)
7564                       thread (cadr thread))
7565               (when (gnus-invisible-cut-children (cdr thread))
7566                 (let ((th (cdr thread)))
7567                   (while th
7568                     (if (memq (mail-header-number (caar th))
7569                               gnus-newsgroup-limit)
7570                         (setq thread (car th)
7571                               th nil)
7572                       (setq th (cdr th))))))))))
7573   thread)
7574
7575 (defun gnus-cut-threads (threads)
7576   "Cut off all uninteresting articles from the beginning of threads."
7577   (when (or (eq gnus-fetch-old-headers 'some)
7578             (eq gnus-fetch-old-headers 'invisible)
7579             (numberp gnus-fetch-old-headers)
7580             (eq gnus-build-sparse-threads 'some)
7581             (eq gnus-build-sparse-threads 'more))
7582     (let ((th threads))
7583       (while th
7584         (setcar th (gnus-cut-thread (car th)))
7585         (setq th (cdr th)))))
7586   ;; Remove nixed out threads.
7587   (delq nil threads))
7588
7589 (defun gnus-summary-initial-limit (&optional show-if-empty)
7590   "Figure out what the initial limit is supposed to be on group entry.
7591 This entails weeding out unwanted dormants, low-scored articles,
7592 fetch-old-headers verbiage, and so on."
7593   ;; Most groups have nothing to remove.
7594   (if (or gnus-inhibit-limiting
7595           (and (null gnus-newsgroup-dormant)
7596                (eq gnus-newsgroup-display 'gnus-not-ignore)
7597                (not (eq gnus-fetch-old-headers 'some))
7598                (not (numberp gnus-fetch-old-headers))
7599                (not (eq gnus-fetch-old-headers 'invisible))
7600                (null gnus-summary-expunge-below)
7601                (not (eq gnus-build-sparse-threads 'some))
7602                (not (eq gnus-build-sparse-threads 'more))
7603                (null gnus-thread-expunge-below)
7604                (not gnus-use-nocem)))
7605       ()                                ; Do nothing.
7606     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7607     (setq gnus-newsgroup-limit nil)
7608     (mapatoms
7609      (lambda (node)
7610        (unless (car (symbol-value node))
7611          ;; These threads have no parents -- they are roots.
7612          (let ((nodes (cdr (symbol-value node)))
7613                thread)
7614            (while nodes
7615              (if (and gnus-thread-expunge-below
7616                       (< (gnus-thread-total-score (car nodes))
7617                          gnus-thread-expunge-below))
7618                  (gnus-expunge-thread (pop nodes))
7619                (setq thread (pop nodes))
7620                (gnus-summary-limit-children thread))))))
7621      gnus-newsgroup-dependencies)
7622     ;; If this limitation resulted in an empty group, we might
7623     ;; pop the previous limit and use it instead.
7624     (when (and (not gnus-newsgroup-limit)
7625                show-if-empty)
7626       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7627     gnus-newsgroup-limit))
7628
7629 (defun gnus-summary-limit-children (thread)
7630   "Return 1 if this subthread is visible and 0 if it is not."
7631   ;; First we get the number of visible children to this thread.  This
7632   ;; is done by recursing down the thread using this function, so this
7633   ;; will really go down to a leaf article first, before slowly
7634   ;; working its way up towards the root.
7635   (when thread
7636     (let ((children
7637            (if (cdr thread)
7638                (apply '+ (mapcar 'gnus-summary-limit-children
7639                                  (cdr thread)))
7640              0))
7641           (number (mail-header-number (car thread)))
7642           score)
7643       (if (and
7644            (not (memq number gnus-newsgroup-marked))
7645            (or
7646             ;; If this article is dormant and has absolutely no visible
7647             ;; children, then this article isn't visible.
7648             (and (memq number gnus-newsgroup-dormant)
7649                  (zerop children))
7650             ;; If this is "fetch-old-headered" and there is no
7651             ;; visible children, then we don't want this article.
7652             (and (or (eq gnus-fetch-old-headers 'some)
7653                      (numberp gnus-fetch-old-headers))
7654                  (gnus-summary-article-ancient-p number)
7655                  (zerop children))
7656             ;; If this is "fetch-old-headered" and `invisible', then
7657             ;; we don't want this article.
7658             (and (eq gnus-fetch-old-headers 'invisible)
7659                  (gnus-summary-article-ancient-p number))
7660             ;; If this is a sparsely inserted article with no children,
7661             ;; we don't want it.
7662             (and (eq gnus-build-sparse-threads 'some)
7663                  (gnus-summary-article-sparse-p number)
7664                  (zerop children))
7665             ;; If we use expunging, and this article is really
7666             ;; low-scored, then we don't want this article.
7667             (when (and gnus-summary-expunge-below
7668                        (< (setq score
7669                                 (or (cdr (assq number gnus-newsgroup-scored))
7670                                     gnus-summary-default-score))
7671                           gnus-summary-expunge-below))
7672               ;; We increase the expunge-tally here, but that has
7673               ;; nothing to do with the limits, really.
7674               (incf gnus-newsgroup-expunged-tally)
7675               ;; We also mark as read here, if that's wanted.
7676               (when (and gnus-summary-mark-below
7677                          (< score gnus-summary-mark-below))
7678                 (setq gnus-newsgroup-unreads
7679                       (delq number gnus-newsgroup-unreads))
7680                 (if gnus-newsgroup-auto-expire
7681                     (push number gnus-newsgroup-expirable)
7682                   (push (cons number gnus-low-score-mark)
7683                         gnus-newsgroup-reads)))
7684               t)
7685             ;; Do the `display' group parameter.
7686             (and gnus-newsgroup-display
7687                  (not (funcall gnus-newsgroup-display)))
7688             ;; Check NoCeM things.
7689             (if (and gnus-use-nocem
7690                      (gnus-nocem-unwanted-article-p
7691                       (mail-header-id (car thread))))
7692                 (progn
7693                   (setq gnus-newsgroup-unreads
7694                         (delq number gnus-newsgroup-unreads))
7695                   t))))
7696           ;; Nope, invisible article.
7697           0
7698         ;; Ok, this article is to be visible, so we add it to the limit
7699         ;; and return 1.
7700         (push number gnus-newsgroup-limit)
7701         1))))
7702
7703 (defun gnus-expunge-thread (thread)
7704   "Mark all articles in THREAD as read."
7705   (let* ((number (mail-header-number (car thread))))
7706     (incf gnus-newsgroup-expunged-tally)
7707     ;; We also mark as read here, if that's wanted.
7708     (setq gnus-newsgroup-unreads
7709           (delq number gnus-newsgroup-unreads))
7710     (if gnus-newsgroup-auto-expire
7711         (push number gnus-newsgroup-expirable)
7712       (push (cons number gnus-low-score-mark)
7713             gnus-newsgroup-reads)))
7714   ;; Go recursively through all subthreads.
7715   (mapcar 'gnus-expunge-thread (cdr thread)))
7716
7717 ;; Summary article oriented commands
7718
7719 (defun gnus-summary-refer-parent-article (n)
7720   "Refer parent article N times.
7721 If N is negative, go to ancestor -N instead.
7722 The difference between N and the number of articles fetched is returned."
7723   (interactive "p")
7724   (let ((skip 1)
7725         error header ref)
7726     (when (not (natnump n))
7727       (setq skip (abs n)
7728             n 1))
7729     (while (and (> n 0)
7730                 (not error))
7731       (setq header (gnus-summary-article-header))
7732       (if (and (eq (mail-header-number header)
7733                    (cdr gnus-article-current))
7734                (equal gnus-newsgroup-name
7735                       (car gnus-article-current)))
7736           ;; If we try to find the parent of the currently
7737           ;; displayed article, then we take a look at the actual
7738           ;; References header, since this is slightly more
7739           ;; reliable than the References field we got from the
7740           ;; server.
7741           (save-excursion
7742             (set-buffer gnus-original-article-buffer)
7743             (nnheader-narrow-to-headers)
7744             (unless (setq ref (message-fetch-field "references"))
7745               (setq ref (message-fetch-field "in-reply-to")))
7746             (widen))
7747         (setq ref
7748               ;; It's not the current article, so we take a bet on
7749               ;; the value we got from the server.
7750               (mail-header-references header)))
7751       (if (and ref
7752                (not (equal ref "")))
7753           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7754             (gnus-message 1 "Couldn't find parent"))
7755         (gnus-message 1 "No references in article %d"
7756                       (gnus-summary-article-number))
7757         (setq error t))
7758       (decf n))
7759     (gnus-summary-position-point)
7760     n))
7761
7762 (defun gnus-summary-refer-references ()
7763   "Fetch all articles mentioned in the References header.
7764 Return the number of articles fetched."
7765   (interactive)
7766   (let ((ref (mail-header-references (gnus-summary-article-header)))
7767         (current (gnus-summary-article-number))
7768         (n 0))
7769     (if (or (not ref)
7770             (equal ref ""))
7771         (error "No References in the current article")
7772       ;; For each Message-ID in the References header...
7773       (while (string-match "<[^>]*>" ref)
7774         (incf n)
7775         ;; ... fetch that article.
7776         (gnus-summary-refer-article
7777          (prog1 (match-string 0 ref)
7778            (setq ref (substring ref (match-end 0))))))
7779       (gnus-summary-goto-subject current)
7780       (gnus-summary-position-point)
7781       n)))
7782
7783 (defun gnus-summary-refer-thread (&optional limit)
7784   "Fetch all articles in the current thread.
7785 If LIMIT (the numerical prefix), fetch that many old headers instead
7786 of what's specified by the `gnus-refer-thread-limit' variable."
7787   (interactive "P")
7788   (let ((id (mail-header-id (gnus-summary-article-header)))
7789         (limit (if limit (prefix-numeric-value limit)
7790                  gnus-refer-thread-limit)))
7791     ;; We want to fetch LIMIT *old* headers, but we also have to
7792     ;; re-fetch all the headers in the current buffer, because many of
7793     ;; them may be undisplayed.  So we adjust LIMIT.
7794     (when (numberp limit)
7795       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7796     (unless (eq gnus-fetch-old-headers 'invisible)
7797       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7798       ;; Retrieve the headers and read them in.
7799       (if (eq (gnus-retrieve-headers
7800                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7801               'nov)
7802           (gnus-build-all-threads)
7803         (error "Can't fetch thread from backends that don't support NOV"))
7804       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7805     (gnus-summary-limit-include-thread id)))
7806
7807 (defun gnus-summary-refer-article (message-id)
7808   "Fetch an article specified by MESSAGE-ID."
7809   (interactive "sMessage-ID: ")
7810   (when (and (stringp message-id)
7811              (not (zerop (length message-id))))
7812     ;; Construct the correct Message-ID if necessary.
7813     ;; Suggested by tale@pawl.rpi.edu.
7814     (unless (string-match "^<" message-id)
7815       (setq message-id (concat "<" message-id)))
7816     (unless (string-match ">$" message-id)
7817       (setq message-id (concat message-id ">")))
7818     (let* ((header (gnus-id-to-header message-id))
7819            (sparse (and header
7820                         (gnus-summary-article-sparse-p
7821                          (mail-header-number header))
7822                         (memq (mail-header-number header)
7823                               gnus-newsgroup-limit)))
7824            number)
7825       (cond
7826        ;; If the article is present in the buffer we just go to it.
7827        ((and header
7828              (or (not (gnus-summary-article-sparse-p
7829                        (mail-header-number header)))
7830                  sparse))
7831         (prog1
7832             (gnus-summary-goto-article
7833              (mail-header-number header) nil t)
7834           (when sparse
7835             (gnus-summary-update-article (mail-header-number header)))))
7836        (t
7837         ;; We fetch the article.
7838         (catch 'found
7839           (dolist (gnus-override-method (gnus-refer-article-methods))
7840             (gnus-check-server gnus-override-method)
7841             ;; Fetch the header, and display the article.
7842             (when (setq number (gnus-summary-insert-subject message-id))
7843               (gnus-summary-select-article nil nil nil number)
7844               (throw 'found t)))
7845           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7846
7847 (defun gnus-refer-article-methods ()
7848   "Return a list of referrable methods."
7849   (cond
7850    ;; No method, so we default to current and native.
7851    ((null gnus-refer-article-method)
7852     (list gnus-current-select-method gnus-select-method))
7853    ;; Current.
7854    ((eq 'current gnus-refer-article-method)
7855     (list gnus-current-select-method))
7856    ;; List of select methods.
7857    ((not (and (symbolp (car gnus-refer-article-method))
7858               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7859     (let (out)
7860       (dolist (method gnus-refer-article-method)
7861         (push (if (eq 'current method)
7862                   gnus-current-select-method
7863                 method)
7864               out))
7865       (nreverse out)))
7866    ;; One single select method.
7867    (t
7868     (list gnus-refer-article-method))))
7869
7870 (defun gnus-summary-edit-parameters ()
7871   "Edit the group parameters of the current group."
7872   (interactive)
7873   (gnus-group-edit-group gnus-newsgroup-name 'params))
7874
7875 (defun gnus-summary-customize-parameters ()
7876   "Customize the group parameters of the current group."
7877   (interactive)
7878   (gnus-group-customize gnus-newsgroup-name))
7879
7880 (defun gnus-summary-enter-digest-group (&optional force)
7881   "Enter an nndoc group based on the current article.
7882 If FORCE, force a digest interpretation.  If not, try
7883 to guess what the document format is."
7884   (interactive "P")
7885   (let ((conf gnus-current-window-configuration))
7886     (save-excursion
7887       (gnus-summary-select-article))
7888     (setq gnus-current-window-configuration conf)
7889     (let* ((name (format "%s-%d"
7890                          (gnus-group-prefixed-name
7891                           gnus-newsgroup-name (list 'nndoc ""))
7892                          (save-excursion
7893                            (set-buffer gnus-summary-buffer)
7894                            gnus-current-article)))
7895            (ogroup gnus-newsgroup-name)
7896            (params (append (gnus-info-params (gnus-get-info ogroup))
7897                            (list (cons 'to-group ogroup))
7898                            (list (cons 'save-article-group ogroup))))
7899            (case-fold-search t)
7900            (buf (current-buffer))
7901            dig to-address)
7902       (save-excursion
7903         (set-buffer gnus-original-article-buffer)
7904         ;; Have the digest group inherit the main mail address of
7905         ;; the parent article.
7906         (when (setq to-address (or (gnus-fetch-field "reply-to")
7907                                    (gnus-fetch-field "from")))
7908           (setq params (append
7909                         (list (cons 'to-address
7910                                     (funcall gnus-decode-encoded-word-function
7911                                              to-address))))))
7912         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7913         (insert-buffer-substring gnus-original-article-buffer)
7914         ;; Remove lines that may lead nndoc to misinterpret the
7915         ;; document type.
7916         (narrow-to-region
7917          (goto-char (point-min))
7918          (or (search-forward "\n\n" nil t) (point)))
7919         (goto-char (point-min))
7920         (delete-matching-lines "^Path:\\|^From ")
7921         (widen))
7922       (unwind-protect
7923           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7924                     (gnus-newsgroup-ephemeral-ignored-charsets
7925                      gnus-newsgroup-ignored-charsets))
7926                 (gnus-group-read-ephemeral-group
7927                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7928                               (nndoc-article-type
7929                                ,(if force 'mbox 'guess)))
7930                  t nil nil nil
7931                  `((adapt-file . ,(gnus-score-file-name gnus-newsgroup-name
7932                                                         "ADAPT")))))
7933               ;; Make all postings to this group go to the parent group.
7934               (nconc (gnus-info-params (gnus-get-info name))
7935                      params)
7936             ;; Couldn't select this doc group.
7937             (switch-to-buffer buf)
7938             (gnus-set-global-variables)
7939             (gnus-configure-windows 'summary)
7940             (gnus-message 3 "Article couldn't be entered?"))
7941         (kill-buffer dig)))))
7942
7943 (defun gnus-summary-read-document (n)
7944   "Open a new group based on the current article(s).
7945 This will allow you to read digests and other similar
7946 documents as newsgroups.
7947 Obeys the standard process/prefix convention."
7948   (interactive "P")
7949   (let* ((articles (gnus-summary-work-articles n))
7950          (ogroup gnus-newsgroup-name)
7951          (params (append (gnus-info-params (gnus-get-info ogroup))
7952                          (list (cons 'to-group ogroup))))
7953          article group egroup groups vgroup)
7954     (while (setq article (pop articles))
7955       (setq group (format "%s-%d" gnus-newsgroup-name article))
7956       (gnus-summary-remove-process-mark article)
7957       (when (gnus-summary-display-article article)
7958         (save-excursion
7959           (with-temp-buffer
7960             (insert-buffer-substring gnus-original-article-buffer)
7961             ;; Remove some headers that may lead nndoc to make
7962             ;; the wrong guess.
7963             (message-narrow-to-head)
7964             (goto-char (point-min))
7965             (delete-matching-lines "^\\(Path\\):\\|^From ")
7966             (widen)
7967             (if (setq egroup
7968                       (gnus-group-read-ephemeral-group
7969                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7970                                      (nndoc-article-type guess))
7971                        t nil t))
7972                 (progn
7973             ;; Make all postings to this group go to the parent group.
7974                   (nconc (gnus-info-params (gnus-get-info egroup))
7975                          params)
7976                   (push egroup groups))
7977               ;; Couldn't select this doc group.
7978               (gnus-error 3 "Article couldn't be entered"))))))
7979     ;; Now we have selected all the documents.
7980     (cond
7981      ((not groups)
7982       (error "None of the articles could be interpreted as documents"))
7983      ((gnus-group-read-ephemeral-group
7984        (setq vgroup (format
7985                      "nnvirtual:%s-%s" gnus-newsgroup-name
7986                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7987        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7988        t
7989        (cons (current-buffer) 'summary)))
7990      (t
7991       (error "Couldn't select virtual nndoc group")))))
7992
7993 (defun gnus-summary-isearch-article (&optional regexp-p)
7994   "Do incremental search forward on the current article.
7995 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7996   (interactive "P")
7997   (gnus-summary-select-article)
7998   (gnus-configure-windows 'article)
7999   (gnus-eval-in-buffer-window gnus-article-buffer
8000     (save-restriction
8001       (widen)
8002       (isearch-forward regexp-p))))
8003
8004 (defun gnus-summary-search-article-forward (regexp &optional backward)
8005   "Search for an article containing REGEXP forward.
8006 If BACKWARD, search backward instead."
8007   (interactive
8008    (list (read-string
8009           (format "Search article %s (regexp%s): "
8010                   (if current-prefix-arg "backward" "forward")
8011                   (if gnus-last-search-regexp
8012                       (concat ", default " gnus-last-search-regexp)
8013                     "")))
8014          current-prefix-arg))
8015   (if (string-equal regexp "")
8016       (setq regexp (or gnus-last-search-regexp ""))
8017     (setq gnus-last-search-regexp regexp)
8018     (setq gnus-article-before-search gnus-current-article))
8019   ;; Intentionally set gnus-last-article.
8020   (setq gnus-last-article gnus-article-before-search)
8021   (let ((gnus-last-article gnus-last-article))
8022     (if (gnus-summary-search-article regexp backward)
8023         (gnus-summary-show-thread)
8024       (error "Search failed: \"%s\"" regexp))))
8025
8026 (defun gnus-summary-search-article-backward (regexp)
8027   "Search for an article containing REGEXP backward."
8028   (interactive
8029    (list (read-string
8030           (format "Search article backward (regexp%s): "
8031                   (if gnus-last-search-regexp
8032                       (concat ", default " gnus-last-search-regexp)
8033                     "")))))
8034   (gnus-summary-search-article-forward regexp 'backward))
8035
8036 (defun gnus-summary-search-article (regexp &optional backward)
8037   "Search for an article containing REGEXP.
8038 Optional argument BACKWARD means do search for backward.
8039 `gnus-select-article-hook' is not called during the search."
8040   ;; We have to require this here to make sure that the following
8041   ;; dynamic binding isn't shadowed by autoloading.
8042   (require 'gnus-async)
8043   (require 'gnus-art)
8044   (let ((gnus-select-article-hook nil)  ;Disable hook.
8045         (gnus-article-prepare-hook nil)
8046         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
8047         (gnus-use-article-prefetch nil)
8048         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
8049         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
8050         (sum (current-buffer))
8051         (gnus-display-mime-function nil)
8052         (found nil)
8053         point)
8054     (gnus-save-hidden-threads
8055       (gnus-summary-select-article)
8056       (set-buffer gnus-article-buffer)
8057       (goto-char (window-point (get-buffer-window (current-buffer))))
8058       (when backward
8059         (forward-line -1))
8060       (while (not found)
8061         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
8062         (if (if backward
8063                 (re-search-backward regexp nil t)
8064               (re-search-forward regexp nil t))
8065             ;; We found the regexp.
8066             (progn
8067               (setq found 'found)
8068               (beginning-of-line)
8069               (set-window-start
8070                (get-buffer-window (current-buffer))
8071                (point))
8072               (forward-line 1)
8073               (set-window-point
8074                (get-buffer-window (current-buffer))
8075                (point))
8076               (set-buffer sum)
8077               (setq point (point)))
8078           ;; We didn't find it, so we go to the next article.
8079           (set-buffer sum)
8080           (setq found 'not)
8081           (while (eq found 'not)
8082             (if (not (if backward (gnus-summary-find-prev)
8083                        (gnus-summary-find-next)))
8084                 ;; No more articles.
8085                 (setq found t)
8086               ;; Select the next article and adjust point.
8087               (unless (gnus-summary-article-sparse-p
8088                        (gnus-summary-article-number))
8089                 (setq found nil)
8090                 (gnus-summary-select-article)
8091                 (set-buffer gnus-article-buffer)
8092                 (widen)
8093                 (goto-char (if backward (point-max) (point-min))))))))
8094       (gnus-message 7 ""))
8095     ;; Return whether we found the regexp.
8096     (when (eq found 'found)
8097       (goto-char point)
8098       (gnus-summary-show-thread)
8099       (gnus-summary-goto-subject gnus-current-article)
8100       (gnus-summary-position-point)
8101       t)))
8102
8103 (defun gnus-find-matching-articles (header regexp)
8104   "Return a list of all articles that match REGEXP on HEADER.
8105 This search includes all articles in the current group that Gnus has
8106 fetched headers for, whether they are displayed or not."
8107   (let ((articles nil)
8108         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
8109         (case-fold-search t))
8110     (dolist (header gnus-newsgroup-headers)
8111       (when (string-match regexp (funcall func header))
8112         (push (mail-header-number header) articles)))
8113     (nreverse articles)))
8114
8115 (defun gnus-summary-find-matching (header regexp &optional backward unread
8116                                           not-case-fold not-matching)
8117   "Return a list of all articles that match REGEXP on HEADER.
8118 The search stars on the current article and goes forwards unless
8119 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
8120 If UNREAD is non-nil, only unread articles will
8121 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
8122 in the comparisons. If NOT-MATCHING, return a list of all articles that
8123 not match REGEXP on HEADER."
8124   (let ((case-fold-search (not not-case-fold))
8125         articles d func)
8126     (if (consp header)
8127         (if (eq (car header) 'extra)
8128             (setq func
8129                   `(lambda (h)
8130                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
8131                          "")))
8132           (error "%s is an invalid header" header))
8133       (unless (fboundp (intern (concat "mail-header-" header)))
8134         (error "%s is not a valid header" header))
8135       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
8136     (dolist (d (if (eq backward 'all)
8137                    gnus-newsgroup-data
8138                  (gnus-data-find-list
8139                   (gnus-summary-article-number)
8140                   (gnus-data-list backward))))
8141       (when (and (or (not unread)       ; We want all articles...
8142                      (gnus-data-unread-p d)) ; Or just unreads.
8143                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
8144                  (if not-matching
8145                      (not (string-match
8146                            regexp
8147                            (funcall func (gnus-data-header d))))
8148                    (string-match regexp
8149                                  (funcall func (gnus-data-header d)))))
8150         (push (gnus-data-number d) articles))) ; Success!
8151     (nreverse articles)))
8152
8153 (defun gnus-summary-execute-command (header regexp command &optional backward)
8154   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
8155 If HEADER is an empty string (or nil), the match is done on the entire
8156 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
8157   (interactive
8158    (list (let ((completion-ignore-case t))
8159            (completing-read
8160             "Header name: "
8161             (mapcar (lambda (header) (list (format "%s" header)))
8162                     (append
8163                      '("Number" "Subject" "From" "Lines" "Date"
8164                        "Message-ID" "Xref" "References" "Body")
8165                      gnus-extra-headers))
8166             nil 'require-match))
8167          (read-string "Regexp: ")
8168          (read-key-sequence "Command: ")
8169          current-prefix-arg))
8170   (when (equal header "Body")
8171     (setq header ""))
8172   ;; Hidden thread subtrees must be searched as well.
8173   (gnus-summary-show-all-threads)
8174   ;; We don't want to change current point nor window configuration.
8175   (save-excursion
8176     (save-window-excursion
8177       (let (gnus-visual
8178             gnus-treat-strip-trailing-blank-lines
8179             gnus-treat-strip-leading-blank-lines
8180             gnus-treat-strip-multiple-blank-lines
8181             gnus-treat-hide-boring-headers
8182             gnus-treat-fold-newsgroups
8183             gnus-article-prepare-hook)
8184         (gnus-message 6 "Executing %s..." (key-description command))
8185         ;; We'd like to execute COMMAND interactively so as to give arguments.
8186         (gnus-execute header regexp
8187                       `(call-interactively ',(key-binding command))
8188                       backward)
8189         (gnus-message 6 "Executing %s...done" (key-description command))))))
8190
8191 (defun gnus-summary-beginning-of-article ()
8192   "Scroll the article back to the beginning."
8193   (interactive)
8194   (gnus-summary-select-article)
8195   (gnus-configure-windows 'article)
8196   (gnus-eval-in-buffer-window gnus-article-buffer
8197     (widen)
8198     (goto-char (point-min))
8199     (when gnus-page-broken
8200       (gnus-narrow-to-page))))
8201
8202 (defun gnus-summary-end-of-article ()
8203   "Scroll to the end of the article."
8204   (interactive)
8205   (gnus-summary-select-article)
8206   (gnus-configure-windows 'article)
8207   (gnus-eval-in-buffer-window gnus-article-buffer
8208     (widen)
8209     (goto-char (point-max))
8210     (recenter -3)
8211     (when gnus-page-broken
8212       (gnus-narrow-to-page))))
8213
8214 (defun gnus-summary-print-truncate-and-quote (string &optional len)
8215   "Truncate to LEN and quote all \"(\"'s in STRING."
8216   (gnus-replace-in-string (if (and len (> (length string) len))
8217                               (substring string 0 len)
8218                             string)
8219                           "[()]" "\\\\\\&"))
8220
8221 (defun gnus-summary-print-article (&optional filename n)
8222   "Generate and print a PostScript image of the N next (mail) articles.
8223
8224 If N is negative, print the N previous articles.  If N is nil and articles
8225 have been marked with the process mark, print these instead.
8226
8227 If the optional first argument FILENAME is nil, send the image to the
8228 printer.  If FILENAME is a string, save the PostScript image in a file with
8229 that name.  If FILENAME is a number, prompt the user for the name of the file
8230 to save in."
8231   (interactive (list (ps-print-preprint current-prefix-arg)))
8232   (dolist (article (gnus-summary-work-articles n))
8233     (gnus-summary-select-article nil nil 'pseudo article)
8234     (gnus-eval-in-buffer-window gnus-article-buffer
8235       (gnus-print-buffer))
8236     (gnus-summary-remove-process-mark article))
8237   (ps-despool filename))
8238
8239 (defun gnus-print-buffer ()
8240   (let ((buffer (generate-new-buffer " *print*")))
8241     (unwind-protect
8242         (progn
8243           (copy-to-buffer buffer (point-min) (point-max))
8244           (set-buffer buffer)
8245           (gnus-article-delete-invisible-text)
8246           (gnus-remove-text-with-property 'gnus-decoration)
8247           (when (gnus-visual-p 'article-highlight 'highlight)
8248             ;; Copy-to-buffer doesn't copy overlay.  So redo
8249             ;; highlight.
8250             (let ((gnus-article-buffer buffer))
8251               (gnus-article-highlight-citation t)
8252               (gnus-article-highlight-signature)))
8253           (let ((ps-left-header
8254                  (list
8255                   (concat "("
8256                           (gnus-summary-print-truncate-and-quote
8257                            (mail-header-subject gnus-current-headers)
8258                            66) ")")
8259                   (concat "("
8260                           (gnus-summary-print-truncate-and-quote
8261                            (mail-header-from gnus-current-headers)
8262                            45) ")")))
8263                 (ps-right-header
8264                  (list
8265                   "/pagenumberstring load"
8266                   (concat "("
8267                           (mail-header-date gnus-current-headers) ")"))))
8268             (gnus-run-hooks 'gnus-ps-print-hook)
8269             (save-excursion
8270               (if window-system
8271                   (ps-spool-buffer-with-faces)
8272                 (ps-spool-buffer)))))
8273       (kill-buffer buffer))))
8274
8275 (defun gnus-summary-show-article (&optional arg)
8276   "Force redisplaying of the current article.
8277 If ARG (the prefix) is a number, show the article with the charset
8278 defined in `gnus-summary-show-article-charset-alist', or the charset
8279 input.
8280 If ARG (the prefix) is non-nil and not a number, show the raw article
8281 without any article massaging functions being run.  Normally, the key strokes
8282 are `C-u g'."
8283   (interactive "P")
8284   (cond
8285    ((numberp arg)
8286     (gnus-summary-show-article t)
8287     (let ((gnus-newsgroup-charset
8288            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
8289                (mm-read-coding-system
8290                 "View as charset: " ;; actually it is coding system.
8291                 (save-excursion
8292                   (set-buffer gnus-article-buffer)
8293                   (mm-detect-coding-region (point) (point-max))))))
8294           (gnus-newsgroup-ignored-charsets 'gnus-all))
8295       (gnus-summary-select-article nil 'force)
8296       (let ((deps gnus-newsgroup-dependencies)
8297             head header lines)
8298         (save-excursion
8299           (set-buffer gnus-original-article-buffer)
8300           (save-restriction
8301             (message-narrow-to-head)
8302             (setq head (buffer-string))
8303             (goto-char (point-min))
8304             (unless (re-search-forward "^lines:[ \t]\\([0-9]+\\)" nil t)
8305               (goto-char (point-max))
8306               (widen)
8307               (setq lines (1- (count-lines (point) (point-max))))))
8308           (with-temp-buffer
8309             (insert (format "211 %d Article retrieved.\n"
8310                             (cdr gnus-article-current)))
8311             (insert head)
8312             (if lines (insert (format "Lines: %d\n" lines)))
8313             (insert ".\n")
8314             (let ((nntp-server-buffer (current-buffer)))
8315               (setq header (car (gnus-get-newsgroup-headers deps t))))))
8316         (gnus-data-set-header
8317          (gnus-data-find (cdr gnus-article-current))
8318          header)
8319         (gnus-summary-update-article-line
8320          (cdr gnus-article-current) header)
8321         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8322           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
8323    ((not arg)
8324     ;; Select the article the normal way.
8325     (gnus-summary-select-article nil 'force))
8326    (t
8327     ;; We have to require this here to make sure that the following
8328     ;; dynamic binding isn't shadowed by autoloading.
8329     (require 'gnus-async)
8330     (require 'gnus-art)
8331     ;; Bind the article treatment functions to nil.
8332     (let ((gnus-have-all-headers t)
8333           gnus-article-prepare-hook
8334           gnus-article-decode-hook
8335           gnus-display-mime-function
8336           gnus-break-pages)
8337       ;; Destroy any MIME parts.
8338       (when (gnus-buffer-live-p gnus-article-buffer)
8339         (save-excursion
8340           (set-buffer gnus-article-buffer)
8341           (mm-destroy-parts gnus-article-mime-handles)
8342           ;; Set it to nil for safety reason.
8343           (setq gnus-article-mime-handle-alist nil)
8344           (setq gnus-article-mime-handles nil)))
8345       (gnus-summary-select-article nil 'force))))
8346   (gnus-summary-goto-subject gnus-current-article)
8347   (gnus-summary-position-point))
8348
8349 (defun gnus-summary-show-raw-article ()
8350   "Show the raw article without any article massaging functions being run."
8351   (interactive)
8352   (gnus-summary-show-article t))
8353
8354 (defun gnus-summary-verbose-headers (&optional arg)
8355   "Toggle permanent full header display.
8356 If ARG is a positive number, turn header display on.
8357 If ARG is a negative number, turn header display off."
8358   (interactive "P")
8359   (setq gnus-show-all-headers
8360         (cond ((or (not (numberp arg))
8361                    (zerop arg))
8362                (not gnus-show-all-headers))
8363               ((natnump arg)
8364                t)))
8365   (gnus-summary-show-article))
8366
8367 (defun gnus-summary-toggle-header (&optional arg)
8368   "Show the headers if they are hidden, or hide them if they are shown.
8369 If ARG is a positive number, show the entire header.
8370 If ARG is a negative number, hide the unwanted header lines."
8371   (interactive "P")
8372   (let ((window (and (gnus-buffer-live-p gnus-article-buffer)
8373                      (get-buffer-window gnus-article-buffer t))))
8374     (with-current-buffer gnus-article-buffer
8375       (widen)
8376       (article-narrow-to-head)
8377       (let* ((buffer-read-only nil)
8378              (inhibit-point-motion-hooks t)
8379              (hidden (if (numberp arg)
8380                          (>= arg 0)
8381                        (gnus-article-hidden-text-p 'headers)))
8382              s e)
8383         (delete-region (point-min) (point-max))
8384         (with-current-buffer gnus-original-article-buffer
8385           (goto-char (setq s (point-min)))
8386           (setq e (if (search-forward "\n\n" nil t)
8387                       (1- (point))
8388                     (point-max))))
8389         (insert-buffer-substring gnus-original-article-buffer s e)
8390         (article-decode-encoded-words)
8391         (if hidden
8392             (let ((gnus-treat-hide-headers nil)
8393                   (gnus-treat-hide-boring-headers nil))
8394               (gnus-delete-wash-type 'headers)
8395               (gnus-treat-article 'head))
8396           (gnus-treat-article 'head))
8397         (widen)
8398         (if window
8399             (set-window-start window (goto-char (point-min))))
8400         (setq gnus-page-broken
8401               (when gnus-break-pages
8402                 (gnus-narrow-to-page)
8403                 t))
8404         (gnus-set-mode-line 'article)))))
8405
8406 (defun gnus-summary-show-all-headers ()
8407   "Make all header lines visible."
8408   (interactive)
8409   (gnus-summary-toggle-header 1))
8410
8411 (defun gnus-summary-caesar-message (&optional arg)
8412   "Caesar rotate the current article by 13.
8413 The numerical prefix specifies how many places to rotate each letter
8414 forward."
8415   (interactive "P")
8416   (gnus-summary-select-article)
8417   (let ((mail-header-separator ""))
8418     (gnus-eval-in-buffer-window gnus-article-buffer
8419       (save-restriction
8420         (widen)
8421         (let ((start (window-start))
8422               buffer-read-only)
8423           (message-caesar-buffer-body arg)
8424           (set-window-start (get-buffer-window (current-buffer)) start))))))
8425
8426 (autoload 'unmorse-region "morse"
8427   "Convert morse coded text in region to ordinary ASCII text."
8428   t)
8429
8430 (defun gnus-summary-morse-message (&optional arg)
8431   "Morse decode the current article."
8432   (interactive "P")
8433   (gnus-summary-select-article)
8434   (let ((mail-header-separator ""))
8435     (gnus-eval-in-buffer-window gnus-article-buffer
8436       (save-excursion
8437         (save-restriction
8438           (widen)
8439           (let ((pos (window-start))
8440                 buffer-read-only)
8441             (goto-char (point-min))
8442             (when (message-goto-body)
8443               (gnus-narrow-to-body))
8444             (goto-char (point-min))
8445             (while (re-search-forward "·" (point-max) t)
8446               (replace-match "."))
8447             (unmorse-region (point-min) (point-max))
8448             (widen)
8449             (set-window-start (get-buffer-window (current-buffer)) pos)))))))
8450
8451 (defun gnus-summary-stop-page-breaking ()
8452   "Stop page breaking in the current article."
8453   (interactive)
8454   (gnus-summary-select-article)
8455   (gnus-eval-in-buffer-window gnus-article-buffer
8456     (widen)
8457     (when (gnus-visual-p 'page-marker)
8458       (let ((buffer-read-only nil))
8459         (gnus-remove-text-with-property 'gnus-prev)
8460         (gnus-remove-text-with-property 'gnus-next))
8461       (setq gnus-page-broken nil))))
8462
8463 (defun gnus-summary-move-article (&optional n to-newsgroup
8464                                             select-method action)
8465   "Move the current article to a different newsgroup.
8466 If N is a positive number, move the N next articles.
8467 If N is a negative number, move the N previous articles.
8468 If N is nil and any articles have been marked with the process mark,
8469 move those articles instead.
8470 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8471 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8472 re-spool using this method.
8473
8474 When called interactively with TO-NEWSGROUP being nil, the value of
8475 the variable `gnus-move-split-methods' is used for finding a default
8476 for the target newsgroup.
8477
8478 For this function to work, both the current newsgroup and the
8479 newsgroup that you want to move to have to support the `request-move'
8480 and `request-accept' functions.
8481
8482 ACTION can be either `move' (the default), `crosspost' or `copy'."
8483   (interactive "P")
8484   (unless action
8485     (setq action 'move))
8486   ;; Check whether the source group supports the required functions.
8487   (cond ((and (eq action 'move)
8488               (not (gnus-check-backend-function
8489                     'request-move-article gnus-newsgroup-name)))
8490          (error "The current group does not support article moving"))
8491         ((and (eq action 'crosspost)
8492               (not (gnus-check-backend-function
8493                     'request-replace-article gnus-newsgroup-name)))
8494          (error "The current group does not support article editing")))
8495   (let ((articles (gnus-summary-work-articles n))
8496         (prefix (if (gnus-check-backend-function
8497                      'request-move-article gnus-newsgroup-name)
8498                     (gnus-group-real-prefix gnus-newsgroup-name)
8499                   ""))
8500         (names '((move "Move" "Moving")
8501                  (copy "Copy" "Copying")
8502                  (crosspost "Crosspost" "Crossposting")))
8503         (copy-buf (save-excursion
8504                     (nnheader-set-temp-buffer " *copy article*")))
8505         art-group to-method new-xref article to-groups)
8506     (unless (assq action names)
8507       (error "Unknown action %s" action))
8508     ;; Read the newsgroup name.
8509     (when (and (not to-newsgroup)
8510                (not select-method))
8511       (if (and gnus-move-split-methods
8512                (not
8513                 (and (memq gnus-current-article articles)
8514                      (gnus-buffer-live-p gnus-original-article-buffer))))
8515           ;; When `gnus-move-split-methods' is non-nil, we have to
8516           ;; select an article to give `gnus-read-move-group-name' an
8517           ;; opportunity to suggest an appropriate default.  However,
8518           ;; we needn't render or mark the article.
8519           (let ((gnus-display-mime-function nil)
8520                 (gnus-article-prepare-hook nil)
8521                 (gnus-mark-article-hook nil))
8522             (gnus-summary-select-article nil nil nil (car articles))))
8523       (setq to-newsgroup
8524             (gnus-read-move-group-name
8525              (cadr (assq action names))
8526              (symbol-value (intern (format "gnus-current-%s-group" action)))
8527              articles prefix))
8528       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8529     (setq to-method (or select-method
8530                         (gnus-server-to-method
8531                          (gnus-group-method to-newsgroup))))
8532     ;; Check the method we are to move this article to...
8533     (unless (gnus-check-backend-function
8534              'request-accept-article (car to-method))
8535       (error "%s does not support article copying" (car to-method)))
8536     (unless (gnus-check-server to-method)
8537       (error "Can't open server %s" (car to-method)))
8538     (gnus-message 6 "%s to %s: %s..."
8539                   (caddr (assq action names))
8540                   (or (car select-method) to-newsgroup) articles)
8541     (while articles
8542       (setq article (pop articles))
8543       (setq
8544        art-group
8545        (cond
8546         ;; Move the article.
8547         ((eq action 'move)
8548          ;; Remove this article from future suppression.
8549          (gnus-dup-unsuppress-article article)
8550          (gnus-request-move-article
8551           article                       ; Article to move
8552           gnus-newsgroup-name           ; From newsgroup
8553           (nth 1 (gnus-find-method-for-group
8554                   gnus-newsgroup-name)) ; Server
8555           (list 'gnus-request-accept-article
8556                 to-newsgroup (list 'quote select-method)
8557                 (not articles) t)       ; Accept form
8558           (not articles)))              ; Only save nov last time
8559         ;; Copy the article.
8560         ((eq action 'copy)
8561          (save-excursion
8562            (set-buffer copy-buf)
8563            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8564              (gnus-request-accept-article
8565               to-newsgroup select-method (not articles) t))))
8566         ;; Crosspost the article.
8567         ((eq action 'crosspost)
8568          (let ((xref (message-tokenize-header
8569                       (mail-header-xref (gnus-summary-article-header article))
8570                       " ")))
8571            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8572                                   ":" (number-to-string article)))
8573            (unless xref
8574              (setq xref (list (system-name))))
8575            (setq new-xref
8576                  (concat
8577                   (mapconcat 'identity
8578                              (delete "Xref:" (delete new-xref xref))
8579                              " ")
8580                   " " new-xref))
8581            (save-excursion
8582              (set-buffer copy-buf)
8583              ;; First put the article in the destination group.
8584              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8585              (when (consp (setq art-group
8586                                 (gnus-request-accept-article
8587                                  to-newsgroup select-method (not articles))))
8588                (setq new-xref (concat new-xref " " (car art-group)
8589                                       ":"
8590                                       (number-to-string (cdr art-group))))
8591                ;; Now we have the new Xrefs header, so we insert
8592                ;; it and replace the new article.
8593                (nnheader-replace-header "Xref" new-xref)
8594                (gnus-request-replace-article
8595                 (cdr art-group) to-newsgroup (current-buffer))
8596                art-group))))))
8597       (cond
8598        ((not art-group)
8599         (gnus-message 1 "Couldn't %s article %s: %s"
8600                       (cadr (assq action names)) article
8601                       (nnheader-get-report (car to-method))))
8602        ((eq art-group 'junk)
8603         (when (eq action 'move)
8604           (gnus-summary-mark-article article gnus-canceled-mark)
8605           (gnus-message 4 "Deleted article %s" article)))
8606        (t
8607         (let* ((pto-group (gnus-group-prefixed-name
8608                            (car art-group) to-method))
8609                (entry
8610                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8611                (info (nth 2 entry))
8612                (to-group (gnus-info-group info))
8613                to-marks)
8614           ;; Update the group that has been moved to.
8615           (when (and info
8616                      (memq action '(move copy)))
8617             (unless (member to-group to-groups)
8618               (push to-group to-groups))
8619
8620             (unless (memq article gnus-newsgroup-unreads)
8621               (push 'read to-marks)
8622               (gnus-info-set-read
8623                info (gnus-add-to-range (gnus-info-read info)
8624                                        (list (cdr art-group)))))
8625
8626             ;; See whether the article is to be put in the cache.
8627             (let ((marks gnus-article-mark-lists)
8628                   (to-article (cdr art-group)))
8629
8630               ;; Enter the article into the cache in the new group,
8631               ;; if that is required.
8632               (when gnus-use-cache
8633                 (gnus-cache-possibly-enter-article
8634                  to-group to-article
8635                  (memq article gnus-newsgroup-marked)
8636                  (memq article gnus-newsgroup-dormant)
8637                  (memq article gnus-newsgroup-unreads)))
8638
8639               (when gnus-preserve-marks
8640                 ;; Copy any marks over to the new group.
8641                 (when (and (equal to-group gnus-newsgroup-name)
8642                            (not (memq article gnus-newsgroup-unreads)))
8643                   ;; Mark this article as read in this group.
8644                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8645                   (setcdr (gnus-active to-group) to-article)
8646                   (setcdr gnus-newsgroup-active to-article))
8647
8648                 (while marks
8649                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8650                     (when (memq article (symbol-value
8651                                          (intern (format "gnus-newsgroup-%s"
8652                                                          (caar marks)))))
8653                       (push (cdar marks) to-marks)
8654                       ;; If the other group is the same as this group,
8655                       ;; then we have to add the mark to the list.
8656                       (when (equal to-group gnus-newsgroup-name)
8657                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8658                              (cons to-article
8659                                    (symbol-value
8660                                     (intern (format "gnus-newsgroup-%s"
8661                                                     (caar marks)))))))
8662                       ;; Copy the marks to other group.
8663                       (gnus-add-marked-articles
8664                        to-group (cdar marks) (list to-article) info)))
8665                   (setq marks (cdr marks)))
8666
8667                 (gnus-request-set-mark to-group (list (list (list to-article)
8668                                                             'add
8669                                                             to-marks))))
8670
8671               (gnus-dribble-enter
8672                (concat "(gnus-group-set-info '"
8673                        (gnus-prin1-to-string (gnus-get-info to-group))
8674                        ")"))))
8675
8676           ;; Update the Xref header in this article to point to
8677           ;; the new crossposted article we have just created.
8678           (when (eq action 'crosspost)
8679             (save-excursion
8680               (set-buffer copy-buf)
8681               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8682               (nnheader-replace-header "Xref" new-xref)
8683               (gnus-request-replace-article
8684                article gnus-newsgroup-name (current-buffer)))))
8685
8686         ;;;!!!Why is this necessary?
8687         (set-buffer gnus-summary-buffer)
8688
8689         (gnus-summary-goto-subject article)
8690         (when (eq action 'move)
8691           (gnus-summary-mark-article article gnus-canceled-mark))))
8692       (gnus-summary-remove-process-mark article))
8693     ;; Re-activate all groups that have been moved to.
8694     (save-excursion
8695       (set-buffer gnus-group-buffer)
8696       (let ((gnus-group-marked to-groups))
8697         (gnus-group-get-new-news-this-group nil t)))
8698
8699     (gnus-kill-buffer copy-buf)
8700     (gnus-summary-position-point)
8701     (gnus-set-mode-line 'summary)))
8702
8703 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8704   "Move the current article to a different newsgroup.
8705 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8706 When called interactively, if TO-NEWSGROUP is nil, use the value of
8707 the variable `gnus-move-split-methods' for finding a default target
8708 newsgroup.
8709 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8710 re-spool using this method."
8711   (interactive "P")
8712   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8713
8714 (defun gnus-summary-crosspost-article (&optional n)
8715   "Crosspost the current article to some other group."
8716   (interactive "P")
8717   (gnus-summary-move-article n nil nil 'crosspost))
8718
8719 (defcustom gnus-summary-respool-default-method nil
8720   "Default method type for respooling an article.
8721 If nil, use to the current newsgroup method."
8722   :type 'symbol
8723   :group 'gnus-summary-mail)
8724
8725 (defun gnus-summary-respool-article (&optional n method)
8726   "Respool the current article.
8727 The article will be squeezed through the mail spooling process again,
8728 which means that it will be put in some mail newsgroup or other
8729 depending on `nnmail-split-methods'.
8730 If N is a positive number, respool the N next articles.
8731 If N is a negative number, respool the N previous articles.
8732 If N is nil and any articles have been marked with the process mark,
8733 respool those articles instead.
8734
8735 Respooling can be done both from mail groups and \"real\" newsgroups.
8736 In the former case, the articles in question will be moved from the
8737 current group into whatever groups they are destined to.  In the
8738 latter case, they will be copied into the relevant groups."
8739   (interactive
8740    (list current-prefix-arg
8741          (let* ((methods (gnus-methods-using 'respool))
8742                 (methname
8743                  (symbol-name (or gnus-summary-respool-default-method
8744                                   (car (gnus-find-method-for-group
8745                                         gnus-newsgroup-name)))))
8746                 (method
8747                  (gnus-completing-read-with-default
8748                   methname "What backend do you want to use when respooling?"
8749                   methods nil t nil 'gnus-mail-method-history))
8750                 ms)
8751            (cond
8752             ((zerop (length (setq ms (gnus-servers-using-backend
8753                                       (intern method)))))
8754              (list (intern method) ""))
8755             ((= 1 (length ms))
8756              (car ms))
8757             (t
8758              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8759                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8760                            ms-alist))))))))
8761   (unless method
8762     (error "No method given for respooling"))
8763   (if (assoc (symbol-name
8764               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8765              (gnus-methods-using 'respool))
8766       (gnus-summary-move-article n nil method)
8767     (gnus-summary-copy-article n nil method)))
8768
8769 (defun gnus-summary-import-article (file &optional edit)
8770   "Import an arbitrary file into a mail newsgroup."
8771   (interactive "fImport file: \nP")
8772   (let ((group gnus-newsgroup-name)
8773         (now (current-time))
8774         atts lines group-art)
8775     (unless (gnus-check-backend-function 'request-accept-article group)
8776       (error "%s does not support article importing" group))
8777     (or (file-readable-p file)
8778         (not (file-regular-p file))
8779         (error "Can't read %s" file))
8780     (save-excursion
8781       (set-buffer (gnus-get-buffer-create " *import file*"))
8782       (erase-buffer)
8783       (nnheader-insert-file-contents file)
8784       (goto-char (point-min))
8785       (if (nnheader-article-p)
8786           (save-restriction
8787             (goto-char (point-min))
8788             (search-forward "\n\n" nil t)
8789             (narrow-to-region (point-min) (1- (point)))
8790             (goto-char (point-min))
8791             (unless (re-search-forward "^date:" nil t)
8792               (goto-char (point-max))
8793               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8794        ;; This doesn't look like an article, so we fudge some headers.
8795         (setq atts (file-attributes file)
8796               lines (count-lines (point-min) (point-max)))
8797         (insert "From: " (read-string "From: ") "\n"
8798                 "Subject: " (read-string "Subject: ") "\n"
8799                 "Date: " (message-make-date (nth 5 atts)) "\n"
8800                 "Message-ID: " (message-make-message-id) "\n"
8801                 "Lines: " (int-to-string lines) "\n"
8802                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8803       (setq group-art (gnus-request-accept-article group nil t))
8804       (kill-buffer (current-buffer)))
8805     (setq gnus-newsgroup-active (gnus-activate-group group))
8806     (forward-line 1)
8807     (gnus-summary-goto-article (cdr group-art) nil t)
8808     (when edit
8809       (gnus-summary-edit-article))))
8810
8811 (defun gnus-summary-create-article ()
8812   "Create an article in a mail newsgroup."
8813   (interactive)
8814   (let ((group gnus-newsgroup-name)
8815         (now (current-time))
8816         group-art)
8817     (unless (gnus-check-backend-function 'request-accept-article group)
8818       (error "%s does not support article importing" group))
8819     (save-excursion
8820       (set-buffer (gnus-get-buffer-create " *import file*"))
8821       (erase-buffer)
8822       (goto-char (point-min))
8823       ;; This doesn't look like an article, so we fudge some headers.
8824       (insert "From: " (read-string "From: ") "\n"
8825               "Subject: " (read-string "Subject: ") "\n"
8826               "Date: " (message-make-date now) "\n"
8827               "Message-ID: " (message-make-message-id) "\n")
8828       (setq group-art (gnus-request-accept-article group nil t))
8829       (kill-buffer (current-buffer)))
8830     (setq gnus-newsgroup-active (gnus-activate-group group))
8831     (forward-line 1)
8832     (gnus-summary-goto-article (cdr group-art) nil t)
8833     (gnus-summary-edit-article)))
8834
8835 (defun gnus-summary-article-posted-p ()
8836   "Say whether the current (mail) article is available from news as well.
8837 This will be the case if the article has both been mailed and posted."
8838   (interactive)
8839   (let ((id (mail-header-references (gnus-summary-article-header)))
8840         (gnus-override-method (car (gnus-refer-article-methods))))
8841     (if (gnus-request-head id "")
8842         (gnus-message 2 "The current message was found on %s"
8843                       gnus-override-method)
8844       (gnus-message 2 "The current message couldn't be found on %s"
8845                     gnus-override-method)
8846       nil)))
8847
8848 (defun gnus-summary-expire-articles (&optional now)
8849   "Expire all articles that are marked as expirable in the current group."
8850   (interactive)
8851   (when (gnus-check-backend-function
8852          'request-expire-articles gnus-newsgroup-name)
8853     ;; This backend supports expiry.
8854     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8855            (expirable (if total
8856                           (progn
8857                             ;; We need to update the info for
8858                             ;; this group for `gnus-list-of-read-articles'
8859                             ;; to give us the right answer.
8860                             (gnus-run-hooks 'gnus-exit-group-hook)
8861                             (gnus-summary-update-info)
8862                             (gnus-list-of-read-articles gnus-newsgroup-name))
8863                         (setq gnus-newsgroup-expirable
8864                               (sort gnus-newsgroup-expirable '<))))
8865            (expiry-wait (if now 'immediate
8866                           (gnus-group-find-parameter
8867                            gnus-newsgroup-name 'expiry-wait)))
8868            (nnmail-expiry-target
8869             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8870                 nnmail-expiry-target))
8871            es)
8872       (when expirable
8873         ;; There are expirable articles in this group, so we run them
8874         ;; through the expiry process.
8875         (gnus-message 6 "Expiring articles...")
8876         (unless (gnus-check-group gnus-newsgroup-name)
8877           (error "Can't open server for %s" gnus-newsgroup-name))
8878         ;; The list of articles that weren't expired is returned.
8879         (save-excursion
8880           (if expiry-wait
8881               (let ((nnmail-expiry-wait-function nil)
8882                     (nnmail-expiry-wait expiry-wait))
8883                 (setq es (gnus-request-expire-articles
8884                           expirable gnus-newsgroup-name)))
8885             (setq es (gnus-request-expire-articles
8886                       expirable gnus-newsgroup-name)))
8887           (unless total
8888             (setq gnus-newsgroup-expirable es))
8889           ;; We go through the old list of expirable, and mark all
8890           ;; really expired articles as nonexistent.
8891           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8892             (let ((gnus-use-cache nil))
8893               (dolist (article expirable)
8894                 (when (and (not (memq article es))
8895                            (gnus-data-find article))
8896                   (gnus-summary-mark-article article gnus-canceled-mark))))))
8897         (gnus-message 6 "Expiring articles...done")))))
8898
8899 (defun gnus-summary-expire-articles-now ()
8900   "Expunge all expirable articles in the current group.
8901 This means that *all* articles that are marked as expirable will be
8902 deleted forever, right now."
8903   (interactive)
8904   (or gnus-expert-user
8905       (gnus-yes-or-no-p
8906        "Are you really, really, really sure you want to delete all these messages? ")
8907       (error "Phew!"))
8908   (gnus-summary-expire-articles t))
8909
8910 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8911 (defun gnus-summary-delete-article (&optional n)
8912   "Delete the N next (mail) articles.
8913 This command actually deletes articles.  This is not a marking
8914 command.  The article will disappear forever from your life, never to
8915 return.
8916 If N is negative, delete backwards.
8917 If N is nil and articles have been marked with the process mark,
8918 delete these instead."
8919   (interactive "P")
8920   (unless (gnus-check-backend-function 'request-expire-articles
8921                                        gnus-newsgroup-name)
8922     (error "The current newsgroup does not support article deletion"))
8923   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8924     (error "Couldn't open server"))
8925   ;; Compute the list of articles to delete.
8926   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8927         (nnmail-expiry-target 'delete)
8928         not-deleted)
8929     (if (and gnus-novice-user
8930              (not (gnus-yes-or-no-p
8931                    (format "Do you really want to delete %s forever? "
8932                            (if (> (length articles) 1)
8933                                (format "these %s articles" (length articles))
8934                              "this article")))))
8935         ()
8936       ;; Delete the articles.
8937       (setq not-deleted (gnus-request-expire-articles
8938                          articles gnus-newsgroup-name 'force))
8939       (while articles
8940         (gnus-summary-remove-process-mark (car articles))
8941         ;; The backend might not have been able to delete the article
8942         ;; after all.
8943         (unless (memq (car articles) not-deleted)
8944           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8945         (setq articles (cdr articles)))
8946       (when not-deleted
8947         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8948     (gnus-summary-position-point)
8949     (gnus-set-mode-line 'summary)
8950     not-deleted))
8951
8952 (defun gnus-summary-edit-article (&optional arg)
8953   "Edit the current article.
8954 This will have permanent effect only in mail groups.
8955 If ARG is nil, edit the decoded articles.
8956 If ARG is 1, edit the raw articles.
8957 If ARG is 2, edit the raw articles even in read-only groups.
8958 If ARG is 3, edit the articles with the current handles.
8959 Otherwise, allow editing of articles even in read-only
8960 groups."
8961   (interactive "P")
8962   (let (force raw current-handles)
8963     (cond
8964      ((null arg))
8965      ((eq arg 1)
8966       (setq raw t))
8967      ((eq arg 2)
8968       (setq raw t
8969             force t))
8970      ((eq arg 3)
8971       (setq current-handles
8972             (and (gnus-buffer-live-p gnus-article-buffer)
8973                  (with-current-buffer gnus-article-buffer
8974                    (prog1
8975                        gnus-article-mime-handles
8976                      (setq gnus-article-mime-handles nil))))))
8977      (t
8978       (setq force t)))
8979     (when (and raw (not force)
8980                (member gnus-newsgroup-name '("nndraft:delayed"
8981                                              "nndraft:drafts"
8982                                              "nndraft:queue")))
8983       (error "Can't edit the raw article in group %s"
8984              gnus-newsgroup-name))
8985     (save-excursion
8986       (set-buffer gnus-summary-buffer)
8987       (let ((mail-parse-charset gnus-newsgroup-charset)
8988             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8989         (gnus-set-global-variables)
8990         (when (and (not force)
8991                    (gnus-group-read-only-p))
8992           (error "The current newsgroup does not support article editing"))
8993         (gnus-summary-show-article t)
8994         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8995           (with-current-buffer gnus-article-buffer
8996             (mm-enable-multibyte)))
8997         (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
8998             (setq raw t))
8999         (gnus-article-edit-article
9000          (if raw 'ignore
9001            `(lambda ()
9002               (let ((mbl mml-buffer-list))
9003                 (setq mml-buffer-list nil)
9004                 (mime-to-mml ,'current-handles)
9005                 (let ((mbl1 mml-buffer-list))
9006                   (setq mml-buffer-list mbl)
9007                   (set (make-local-variable 'mml-buffer-list) mbl1))
9008                 (make-local-hook 'kill-buffer-hook)
9009                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
9010          `(lambda (no-highlight)
9011             (let ((mail-parse-charset ',gnus-newsgroup-charset)
9012                   (message-options message-options)
9013                   (message-options-set-recipient)
9014                   (mail-parse-ignored-charsets
9015                    ',gnus-newsgroup-ignored-charsets))
9016               ,(if (not raw) '(progn
9017                                 (mml-to-mime)
9018                                 (mml-destroy-buffers)
9019                                 (remove-hook 'kill-buffer-hook
9020                                              'mml-destroy-buffers t)
9021                                 (kill-local-variable 'mml-buffer-list)))
9022               (gnus-summary-edit-article-done
9023                ,(or (mail-header-references gnus-current-headers) "")
9024                ,(gnus-group-read-only-p)
9025                ,gnus-summary-buffer no-highlight))))))))
9026
9027 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
9028
9029 (defun gnus-summary-edit-article-done (&optional references read-only buffer
9030                                                  no-highlight)
9031   "Make edits to the current article permanent."
9032   (interactive)
9033   (save-excursion
9034    ;; The buffer restriction contains the entire article if it exists.
9035     (when (article-goto-body)
9036       (let ((lines (count-lines (point) (point-max)))
9037             (length (- (point-max) (point)))
9038             (case-fold-search t)
9039             (body (copy-marker (point))))
9040         (goto-char (point-min))
9041         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
9042           (delete-region (match-beginning 1) (match-end 1))
9043           (insert (number-to-string length)))
9044         (goto-char (point-min))
9045         (when (re-search-forward
9046                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
9047           (delete-region (match-beginning 1) (match-end 1))
9048           (insert (number-to-string length)))
9049         (goto-char (point-min))
9050         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
9051           (delete-region (match-beginning 1) (match-end 1))
9052           (insert (number-to-string lines))))))
9053   ;; Replace the article.
9054   (let ((buf (current-buffer)))
9055     (with-temp-buffer
9056       (insert-buffer-substring buf)
9057
9058       (if (and (not read-only)
9059                (not (gnus-request-replace-article
9060                      (cdr gnus-article-current) (car gnus-article-current)
9061                      (current-buffer) t)))
9062           (error "Couldn't replace article")
9063         ;; Update the summary buffer.
9064         (if (and references
9065                  (equal (message-tokenize-header references " ")
9066                         (message-tokenize-header
9067                          (or (message-fetch-field "references") "") " ")))
9068             ;; We only have to update this line.
9069             (save-excursion
9070               (save-restriction
9071                 (message-narrow-to-head)
9072                 (let ((head (buffer-string))
9073                       header)
9074                   (with-temp-buffer
9075                     (insert (format "211 %d Article retrieved.\n"
9076                                     (cdr gnus-article-current)))
9077                     (insert head)
9078                     (insert ".\n")
9079                     (let ((nntp-server-buffer (current-buffer)))
9080                       (setq header (car (gnus-get-newsgroup-headers
9081                                          nil t))))
9082                     (save-excursion
9083                       (set-buffer gnus-summary-buffer)
9084                       (gnus-data-set-header
9085                        (gnus-data-find (cdr gnus-article-current))
9086                        header)
9087                       (gnus-summary-update-article-line
9088                        (cdr gnus-article-current) header)
9089                       (if (gnus-summary-goto-subject
9090                            (cdr gnus-article-current) nil t)
9091                           (gnus-summary-update-secondary-mark
9092                            (cdr gnus-article-current))))))))
9093           ;; Update threads.
9094           (set-buffer (or buffer gnus-summary-buffer))
9095           (gnus-summary-update-article (cdr gnus-article-current))
9096           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9097               (gnus-summary-update-secondary-mark
9098                (cdr gnus-article-current))))
9099         ;; Prettify the article buffer again.
9100         (unless no-highlight
9101           (save-excursion
9102             (set-buffer gnus-article-buffer)
9103             ;;;!!! Fix this -- article should be rehighlighted.
9104             ;;;(gnus-run-hooks 'gnus-article-display-hook)
9105             (set-buffer gnus-original-article-buffer)
9106             (gnus-request-article
9107              (cdr gnus-article-current)
9108              (car gnus-article-current) (current-buffer))))
9109         ;; Prettify the summary buffer line.
9110         (when (gnus-visual-p 'summary-highlight 'highlight)
9111           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
9112
9113 (defun gnus-summary-edit-wash (key)
9114   "Perform editing command KEY in the article buffer."
9115   (interactive
9116    (list
9117     (progn
9118       (message "%s" (concat (this-command-keys) "- "))
9119       (read-char))))
9120   (message "")
9121   (gnus-summary-edit-article)
9122   (execute-kbd-macro (concat (this-command-keys) key))
9123   (gnus-article-edit-done))
9124
9125 ;;; Respooling
9126
9127 (defun gnus-summary-respool-query (&optional silent trace)
9128   "Query where the respool algorithm would put this article."
9129   (interactive)
9130   (let (gnus-mark-article-hook)
9131     (gnus-summary-select-article)
9132     (save-excursion
9133       (set-buffer gnus-original-article-buffer)
9134       (save-restriction
9135         (message-narrow-to-head)
9136         (let ((groups (nnmail-article-group 'identity trace)))
9137           (unless silent
9138             (if groups
9139                 (message "This message would go to %s"
9140                          (mapconcat 'car groups ", "))
9141               (message "This message would go to no groups"))
9142             groups))))))
9143
9144 (defun gnus-summary-respool-trace ()
9145   "Trace where the respool algorithm would put this article.
9146 Display a buffer showing all fancy splitting patterns which matched."
9147   (interactive)
9148   (gnus-summary-respool-query nil t))
9149
9150 ;; Summary marking commands.
9151
9152 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
9153   "Mark articles which has the same subject as read, and then select the next.
9154 If UNMARK is positive, remove any kind of mark.
9155 If UNMARK is negative, tick articles."
9156   (interactive "P")
9157   (when unmark
9158     (setq unmark (prefix-numeric-value unmark)))
9159   (let ((count
9160          (gnus-summary-mark-same-subject
9161           (gnus-summary-article-subject) unmark)))
9162     ;; Select next unread article.  If auto-select-same mode, should
9163     ;; select the first unread article.
9164     (gnus-summary-next-article t (and gnus-auto-select-same
9165                                       (gnus-summary-article-subject)))
9166     (gnus-message 7 "%d article%s marked as %s"
9167                   count (if (= count 1) " is" "s are")
9168                   (if unmark "unread" "read"))))
9169
9170 (defun gnus-summary-kill-same-subject (&optional unmark)
9171   "Mark articles which has the same subject as read.
9172 If UNMARK is positive, remove any kind of mark.
9173 If UNMARK is negative, tick articles."
9174   (interactive "P")
9175   (when unmark
9176     (setq unmark (prefix-numeric-value unmark)))
9177   (let ((count
9178          (gnus-summary-mark-same-subject
9179           (gnus-summary-article-subject) unmark)))
9180     ;; If marked as read, go to next unread subject.
9181     (when (null unmark)
9182       ;; Go to next unread subject.
9183       (gnus-summary-next-subject 1 t))
9184     (gnus-message 7 "%d articles are marked as %s"
9185                   count (if unmark "unread" "read"))))
9186
9187 (defun gnus-summary-mark-same-subject (subject &optional unmark)
9188   "Mark articles with same SUBJECT as read, and return marked number.
9189 If optional argument UNMARK is positive, remove any kinds of marks.
9190 If optional argument UNMARK is negative, mark articles as unread instead."
9191   (let ((count 1))
9192     (save-excursion
9193       (cond
9194        ((null unmark)                   ; Mark as read.
9195         (while (and
9196                 (progn
9197                   (gnus-summary-mark-article-as-read gnus-killed-mark)
9198                   (gnus-summary-show-thread) t)
9199                 (gnus-summary-find-subject subject))
9200           (setq count (1+ count))))
9201        ((> unmark 0)                    ; Tick.
9202         (while (and
9203                 (progn
9204                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
9205                   (gnus-summary-show-thread) t)
9206                 (gnus-summary-find-subject subject))
9207           (setq count (1+ count))))
9208        (t                               ; Mark as unread.
9209         (while (and
9210                 (progn
9211                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
9212                   (gnus-summary-show-thread) t)
9213                 (gnus-summary-find-subject subject))
9214           (setq count (1+ count)))))
9215       (gnus-set-mode-line 'summary)
9216       ;; Return the number of marked articles.
9217       count)))
9218
9219 (defun gnus-summary-mark-as-processable (n &optional unmark)
9220   "Set the process mark on the next N articles.
9221 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
9222 the process mark instead.  The difference between N and the actual
9223 number of articles marked is returned."
9224   (interactive "P")
9225   (if (and (null n) (gnus-region-active-p))
9226       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
9227     (setq n (prefix-numeric-value n))
9228     (let ((backward (< n 0))
9229           (n (abs n)))
9230       (while (and
9231               (> n 0)
9232               (if unmark
9233                   (gnus-summary-remove-process-mark
9234                    (gnus-summary-article-number))
9235                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
9236               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
9237         (setq n (1- n)))
9238       (when (/= 0 n)
9239         (gnus-message 7 "No more articles"))
9240       (gnus-summary-recenter)
9241       (gnus-summary-position-point)
9242       n)))
9243
9244 (defun gnus-summary-unmark-as-processable (n)
9245   "Remove the process mark from the next N articles.
9246 If N is negative, unmark backward instead.  The difference between N and
9247 the actual number of articles unmarked is returned."
9248   (interactive "P")
9249   (gnus-summary-mark-as-processable n t))
9250
9251 (defun gnus-summary-unmark-all-processable ()
9252   "Remove the process mark from all articles."
9253   (interactive)
9254   (save-excursion
9255     (while gnus-newsgroup-processable
9256       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
9257   (gnus-summary-position-point))
9258
9259 (defun gnus-summary-add-mark (article type)
9260   "Mark ARTICLE with a mark of TYPE."
9261   (let ((vtype (car (assq type gnus-article-mark-lists)))
9262         var)
9263     (if (not vtype)
9264         (error "No such mark type: %s" type)
9265       (setq var (intern (format "gnus-newsgroup-%s" type)))
9266       (set var (cons article (symbol-value var)))
9267       (if (memq type '(processable cached replied forwarded recent saved))
9268           (gnus-summary-update-secondary-mark article)
9269         ;;; !!! This is bogus.  We should find out what primary
9270         ;;; !!! mark we want to set.
9271         (gnus-summary-update-mark gnus-del-mark 'unread)))))
9272
9273 (defun gnus-summary-mark-as-expirable (n)
9274   "Mark N articles forward as expirable.
9275 If N is negative, mark backward instead.  The difference between N and
9276 the actual number of articles marked is returned."
9277   (interactive "p")
9278   (gnus-summary-mark-forward n gnus-expirable-mark))
9279
9280 (defun gnus-summary-mark-as-spam (n)
9281   "Mark N articles forward as spam.
9282 If N is negative, mark backward instead.  The difference between N and
9283 the actual number of articles marked is returned."
9284   (interactive "p")
9285   (gnus-summary-mark-forward n gnus-spam-mark))
9286
9287 (defun gnus-summary-mark-article-as-replied (article)
9288   "Mark ARTICLE as replied to and update the summary line.
9289 ARTICLE can also be a list of articles."
9290   (interactive (list (gnus-summary-article-number)))
9291   (let ((articles (if (listp article) article (list article))))
9292     (dolist (article articles)
9293       (push article gnus-newsgroup-replied)
9294       (let ((buffer-read-only nil))
9295         (when (gnus-summary-goto-subject article nil t)
9296           (gnus-summary-update-secondary-mark article))))))
9297
9298 (defun gnus-summary-mark-article-as-forwarded (article)
9299   "Mark ARTICLE as forwarded and update the summary line.
9300 ARTICLE can also be a list of articles."
9301   (let ((articles (if (listp article) article (list article))))
9302     (dolist (article articles)
9303       (push article gnus-newsgroup-forwarded)
9304       (let ((buffer-read-only nil))
9305         (when (gnus-summary-goto-subject article nil t)
9306           (gnus-summary-update-secondary-mark article))))))
9307
9308 (defun gnus-summary-set-bookmark (article)
9309   "Set a bookmark in current article."
9310   (interactive (list (gnus-summary-article-number)))
9311   (when (or (not (get-buffer gnus-article-buffer))
9312             (not gnus-current-article)
9313             (not gnus-article-current)
9314             (not (equal gnus-newsgroup-name (car gnus-article-current))))
9315     (error "No current article selected"))
9316   ;; Remove old bookmark, if one exists.
9317   (let ((old (assq article gnus-newsgroup-bookmarks)))
9318     (when old
9319       (setq gnus-newsgroup-bookmarks
9320             (delq old gnus-newsgroup-bookmarks))))
9321   ;; Set the new bookmark, which is on the form
9322   ;; (article-number . line-number-in-body).
9323   (push
9324    (cons article
9325          (save-excursion
9326            (set-buffer gnus-article-buffer)
9327            (count-lines
9328             (min (point)
9329                  (save-excursion
9330                    (goto-char (point-min))
9331                    (search-forward "\n\n" nil t)
9332                    (point)))
9333             (point))))
9334    gnus-newsgroup-bookmarks)
9335   (gnus-message 6 "A bookmark has been added to the current article."))
9336
9337 (defun gnus-summary-remove-bookmark (article)
9338   "Remove the bookmark from the current article."
9339   (interactive (list (gnus-summary-article-number)))
9340   ;; Remove old bookmark, if one exists.
9341   (let ((old (assq article gnus-newsgroup-bookmarks)))
9342     (if old
9343         (progn
9344           (setq gnus-newsgroup-bookmarks
9345                 (delq old gnus-newsgroup-bookmarks))
9346           (gnus-message 6 "Removed bookmark."))
9347       (gnus-message 6 "No bookmark in current article."))))
9348
9349 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9350 (defun gnus-summary-mark-as-dormant (n)
9351   "Mark N articles forward as dormant.
9352 If N is negative, mark backward instead.  The difference between N and
9353 the actual number of articles marked is returned."
9354   (interactive "p")
9355   (gnus-summary-mark-forward n gnus-dormant-mark))
9356
9357 (defun gnus-summary-set-process-mark (article)
9358   "Set the process mark on ARTICLE and update the summary line."
9359   (setq gnus-newsgroup-processable
9360         (cons article
9361               (delq article gnus-newsgroup-processable)))
9362   (when (gnus-summary-goto-subject article)
9363     (gnus-summary-show-thread)
9364     (gnus-summary-goto-subject article)
9365     (gnus-summary-update-secondary-mark article)))
9366
9367 (defun gnus-summary-remove-process-mark (article)
9368   "Remove the process mark from ARTICLE and update the summary line."
9369   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
9370   (when (gnus-summary-goto-subject article)
9371     (gnus-summary-show-thread)
9372     (gnus-summary-goto-subject article)
9373     (gnus-summary-update-secondary-mark article)))
9374
9375 (defun gnus-summary-set-saved-mark (article)
9376   "Set the process mark on ARTICLE and update the summary line."
9377   (push article gnus-newsgroup-saved)
9378   (when (gnus-summary-goto-subject article)
9379     (gnus-summary-update-secondary-mark article)))
9380
9381 (defun gnus-summary-mark-forward (n &optional mark no-expire)
9382   "Mark N articles as read forwards.
9383 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
9384 The difference between N and the actual number of articles marked is
9385 returned.
9386 Iff NO-EXPIRE, auto-expiry will be inhibited."
9387   (interactive "p")
9388   (gnus-summary-show-thread)
9389   (let ((backward (< n 0))
9390         (gnus-summary-goto-unread
9391          (and gnus-summary-goto-unread
9392               (not (eq gnus-summary-goto-unread 'never))
9393               (not (memq mark (list gnus-unread-mark
9394                                     gnus-ticked-mark gnus-dormant-mark)))))
9395         (n (abs n))
9396         (mark (or mark gnus-del-mark)))
9397     (while (and (> n 0)
9398                 (gnus-summary-mark-article nil mark no-expire)
9399                 (zerop (gnus-summary-next-subject
9400                         (if backward -1 1)
9401                         (and gnus-summary-goto-unread
9402                              (not (eq gnus-summary-goto-unread 'never)))
9403                         t)))
9404       (setq n (1- n)))
9405     (when (/= 0 n)
9406       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
9407     (gnus-summary-recenter)
9408     (gnus-summary-position-point)
9409     (gnus-set-mode-line 'summary)
9410     n))
9411
9412 (defun gnus-summary-mark-article-as-read (mark)
9413   "Mark the current article quickly as read with MARK."
9414   (let ((article (gnus-summary-article-number)))
9415     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9416     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9417     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9418     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9419     (push (cons article mark) gnus-newsgroup-reads)
9420     ;; Possibly remove from cache, if that is used.
9421     (when gnus-use-cache
9422       (gnus-cache-enter-remove-article article))
9423     ;; Allow the backend to change the mark.
9424     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9425     ;; Check for auto-expiry.
9426     (when (and gnus-newsgroup-auto-expire
9427                (memq mark gnus-auto-expirable-marks))
9428       (setq mark gnus-expirable-mark)
9429       ;; Let the backend know about the mark change.
9430       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9431       (push article gnus-newsgroup-expirable))
9432     ;; Set the mark in the buffer.
9433     (gnus-summary-update-mark mark 'unread)
9434     t))
9435
9436 (defun gnus-summary-mark-article-as-unread (mark)
9437   "Mark the current article quickly as unread with MARK."
9438   (let* ((article (gnus-summary-article-number))
9439          (old-mark (gnus-summary-article-mark article)))
9440     ;; Allow the backend to change the mark.
9441     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9442     (if (eq mark old-mark)
9443         t
9444       (if (<= article 0)
9445           (progn
9446             (gnus-error 1 "Can't mark negative article numbers")
9447             nil)
9448         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9449         (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9450         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9451         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
9452         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
9453         (cond ((= mark gnus-ticked-mark)
9454                (setq gnus-newsgroup-marked
9455                      (gnus-add-to-sorted-list gnus-newsgroup-marked
9456                                               article)))
9457               ((= mark gnus-spam-mark)
9458                (setq gnus-newsgroup-spam-marked
9459                      (gnus-add-to-sorted-list gnus-newsgroup-spam-marked
9460                                               article)))
9461               ((= mark gnus-dormant-mark)
9462                (setq gnus-newsgroup-dormant
9463                      (gnus-add-to-sorted-list gnus-newsgroup-dormant
9464                                               article)))
9465               (t
9466                (setq gnus-newsgroup-unreads
9467                      (gnus-add-to-sorted-list gnus-newsgroup-unreads
9468                                               article))))
9469         (gnus-pull article gnus-newsgroup-reads)
9470
9471         ;; See whether the article is to be put in the cache.
9472         (and gnus-use-cache
9473              (vectorp (gnus-summary-article-header article))
9474              (save-excursion
9475                (gnus-cache-possibly-enter-article
9476                 gnus-newsgroup-name article
9477                 (= mark gnus-ticked-mark)
9478                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9479
9480         ;; Fix the mark.
9481         (gnus-summary-update-mark mark 'unread)
9482         t))))
9483
9484 (defun gnus-summary-mark-article (&optional article mark no-expire)
9485   "Mark ARTICLE with MARK.  MARK can be any character.
9486 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
9487 `??' (dormant) and `?E' (expirable).
9488 If MARK is nil, then the default character `?r' is used.
9489 If ARTICLE is nil, then the article on the current line will be
9490 marked.
9491 Iff NO-EXPIRE, auto-expiry will be inhibited."
9492   ;; The mark might be a string.
9493   (when (stringp mark)
9494     (setq mark (aref mark 0)))
9495   ;; If no mark is given, then we check auto-expiring.
9496   (when (null mark)
9497     (setq mark gnus-del-mark))
9498   (when (and (not no-expire)
9499              gnus-newsgroup-auto-expire
9500              (memq mark gnus-auto-expirable-marks))
9501     (setq mark gnus-expirable-mark))
9502   (let ((article (or article (gnus-summary-article-number)))
9503         (old-mark (gnus-summary-article-mark article)))
9504     ;; Allow the backend to change the mark.
9505     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
9506     (if (eq mark old-mark)
9507         t
9508       (unless article
9509         (error "No article on current line"))
9510       (if (not (if (or (= mark gnus-unread-mark)
9511                        (= mark gnus-ticked-mark)
9512                        (= mark gnus-spam-mark)
9513                        (= mark gnus-dormant-mark))
9514                    (gnus-mark-article-as-unread article mark)
9515                  (gnus-mark-article-as-read article mark)))
9516           t
9517         ;; See whether the article is to be put in the cache.
9518         (and gnus-use-cache
9519              (not (= mark gnus-canceled-mark))
9520              (vectorp (gnus-summary-article-header article))
9521              (save-excursion
9522                (gnus-cache-possibly-enter-article
9523                 gnus-newsgroup-name article
9524                 (= mark gnus-ticked-mark)
9525                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9526
9527         (when (gnus-summary-goto-subject article nil t)
9528           (let ((buffer-read-only nil))
9529             (gnus-summary-show-thread)
9530             ;; Fix the mark.
9531             (gnus-summary-update-mark mark 'unread)
9532             t))))))
9533
9534 (defun gnus-summary-update-secondary-mark (article)
9535   "Update the secondary (read, process, cache) mark."
9536   (gnus-summary-update-mark
9537    (cond ((memq article gnus-newsgroup-processable)
9538           gnus-process-mark)
9539          ((memq article gnus-newsgroup-cached)
9540           gnus-cached-mark)
9541          ((memq article gnus-newsgroup-replied)
9542           gnus-replied-mark)
9543          ((memq article gnus-newsgroup-forwarded)
9544           gnus-forwarded-mark)
9545          ((memq article gnus-newsgroup-saved)
9546           gnus-saved-mark)
9547          ((memq article gnus-newsgroup-recent)
9548           gnus-recent-mark)
9549          ((memq article gnus-newsgroup-unseen)
9550           gnus-unseen-mark)
9551          (t gnus-no-mark))
9552    'replied)
9553   (when (gnus-visual-p 'summary-highlight 'highlight)
9554     (gnus-run-hooks 'gnus-summary-update-hook))
9555   t)
9556
9557 (defun gnus-summary-update-mark (mark type)
9558   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9559         (buffer-read-only nil))
9560     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9561     (when forward
9562       (when (looking-at "\r")
9563         (incf forward))
9564       (when (<= (+ forward (point)) (point-max))
9565         ;; Go to the right position on the line.
9566         (goto-char (+ forward (point)))
9567         ;; Replace the old mark with the new mark.
9568         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9569         ;; Optionally update the marks by some user rule.
9570         (when (eq type 'unread)
9571           (gnus-data-set-mark
9572            (gnus-data-find (gnus-summary-article-number)) mark)
9573           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9574
9575 (defun gnus-mark-article-as-read (article &optional mark)
9576   "Enter ARTICLE in the pertinent lists and remove it from others."
9577   ;; Make the article expirable.
9578   (let ((mark (or mark gnus-del-mark)))
9579     (setq gnus-newsgroup-expirable
9580           (if (= mark gnus-expirable-mark)
9581               (gnus-add-to-sorted-list gnus-newsgroup-expirable article)
9582             (delq article gnus-newsgroup-expirable)))
9583     ;; Remove from unread and marked lists.
9584     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9585     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9586     (setq gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked))
9587     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9588     (push (cons article mark) gnus-newsgroup-reads)
9589     ;; Possibly remove from cache, if that is used.
9590     (when gnus-use-cache
9591       (gnus-cache-enter-remove-article article))
9592     t))
9593
9594 (defun gnus-mark-article-as-unread (article &optional mark)
9595   "Enter ARTICLE in the pertinent lists and remove it from others."
9596   (let ((mark (or mark gnus-ticked-mark)))
9597     (if (<= article 0)
9598         (progn
9599           (gnus-error 1 "Can't mark negative article numbers")
9600           nil)
9601       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9602             gnus-newsgroup-spam-marked (delq article gnus-newsgroup-spam-marked)
9603             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9604             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9605             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9606
9607       ;; Unsuppress duplicates?
9608       (when gnus-suppress-duplicates
9609         (gnus-dup-unsuppress-article article))
9610
9611       (cond ((= mark gnus-ticked-mark)
9612              (setq gnus-newsgroup-marked
9613                    (gnus-add-to-sorted-list gnus-newsgroup-marked article)))
9614             ((= mark gnus-spam-mark)
9615              (setq gnus-newsgroup-spam-marked
9616                    (gnus-add-to-sorted-list gnus-newsgroup-spam-marked article)))
9617             ((= mark gnus-dormant-mark)
9618              (setq gnus-newsgroup-dormant
9619                    (gnus-add-to-sorted-list gnus-newsgroup-dormant article)))
9620             (t
9621              (setq gnus-newsgroup-unreads
9622                    (gnus-add-to-sorted-list gnus-newsgroup-unreads article))))
9623       (gnus-pull article gnus-newsgroup-reads)
9624       t)))
9625
9626 (defalias 'gnus-summary-mark-as-unread-forward
9627   'gnus-summary-tick-article-forward)
9628 (make-obsolete 'gnus-summary-mark-as-unread-forward
9629                'gnus-summary-tick-article-forward)
9630 (defun gnus-summary-tick-article-forward (n)
9631   "Tick N articles forwards.
9632 If N is negative, tick backwards instead.
9633 The difference between N and the number of articles ticked is returned."
9634   (interactive "p")
9635   (gnus-summary-mark-forward n gnus-ticked-mark))
9636
9637 (defalias 'gnus-summary-mark-as-unread-backward
9638   'gnus-summary-tick-article-backward)
9639 (make-obsolete 'gnus-summary-mark-as-unread-backward
9640                'gnus-summary-tick-article-backward)
9641 (defun gnus-summary-tick-article-backward (n)
9642   "Tick N articles backwards.
9643 The difference between N and the number of articles ticked is returned."
9644   (interactive "p")
9645   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9646
9647 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9648 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9649 (defun gnus-summary-tick-article (&optional article clear-mark)
9650   "Mark current article as unread.
9651 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9652 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9653   (interactive)
9654   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9655                                        gnus-ticked-mark)))
9656
9657 (defun gnus-summary-mark-as-read-forward (n)
9658   "Mark N articles as read forwards.
9659 If N is negative, mark backwards instead.
9660 The difference between N and the actual number of articles marked is
9661 returned."
9662   (interactive "p")
9663   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9664
9665 (defun gnus-summary-mark-as-read-backward (n)
9666   "Mark the N articles as read backwards.
9667 The difference between N and the actual number of articles marked is
9668 returned."
9669   (interactive "p")
9670   (gnus-summary-mark-forward
9671    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9672
9673 (defun gnus-summary-mark-as-read (&optional article mark)
9674   "Mark current article as read.
9675 ARTICLE specifies the article to be marked as read.
9676 MARK specifies a string to be inserted at the beginning of the line."
9677   (gnus-summary-mark-article article mark))
9678
9679 (defun gnus-summary-clear-mark-forward (n)
9680   "Clear marks from N articles forward.
9681 If N is negative, clear backward instead.
9682 The difference between N and the number of marks cleared is returned."
9683   (interactive "p")
9684   (gnus-summary-mark-forward n gnus-unread-mark))
9685
9686 (defun gnus-summary-clear-mark-backward (n)
9687   "Clear marks from N articles backward.
9688 The difference between N and the number of marks cleared is returned."
9689   (interactive "p")
9690   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9691
9692 (defun gnus-summary-mark-unread-as-read ()
9693   "Intended to be used by `gnus-summary-mark-article-hook'."
9694   (when (memq gnus-current-article gnus-newsgroup-unreads)
9695     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9696
9697 (defun gnus-summary-mark-read-and-unread-as-read ()
9698   "Intended to be used by `gnus-summary-mark-article-hook'."
9699   (let ((mark (gnus-summary-article-mark)))
9700     (when (or (gnus-unread-mark-p mark)
9701               (gnus-read-mark-p mark))
9702       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9703
9704 (defun gnus-summary-mark-unread-as-ticked ()
9705   "Intended to be used by `gnus-summary-mark-article-hook'."
9706   (when (memq gnus-current-article gnus-newsgroup-unreads)
9707     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9708
9709 (defun gnus-summary-mark-region-as-read (point mark all)
9710   "Mark all unread articles between point and mark as read.
9711 If given a prefix, mark all articles between point and mark as read,
9712 even ticked and dormant ones."
9713   (interactive "r\nP")
9714   (save-excursion
9715     (let (article)
9716       (goto-char point)
9717       (beginning-of-line)
9718       (while (and
9719               (< (point) mark)
9720               (progn
9721                 (when (or all
9722                           (memq (setq article (gnus-summary-article-number))
9723                                 gnus-newsgroup-unreads))
9724                   (gnus-summary-mark-article article gnus-del-mark))
9725                 t)
9726               (gnus-summary-find-next))))))
9727
9728 (defun gnus-summary-mark-below (score mark)
9729   "Mark articles with score less than SCORE with MARK."
9730   (interactive "P\ncMark: ")
9731   (setq score (if score
9732                   (prefix-numeric-value score)
9733                 (or gnus-summary-default-score 0)))
9734   (save-excursion
9735     (set-buffer gnus-summary-buffer)
9736     (goto-char (point-min))
9737     (while
9738         (progn
9739           (and (< (gnus-summary-article-score) score)
9740                (gnus-summary-mark-article nil mark))
9741           (gnus-summary-find-next)))))
9742
9743 (defun gnus-summary-kill-below (&optional score)
9744   "Mark articles with score below SCORE as read."
9745   (interactive "P")
9746   (gnus-summary-mark-below score gnus-killed-mark))
9747
9748 (defun gnus-summary-clear-above (&optional score)
9749   "Clear all marks from articles with score above SCORE."
9750   (interactive "P")
9751   (gnus-summary-mark-above score gnus-unread-mark))
9752
9753 (defun gnus-summary-tick-above (&optional score)
9754   "Tick all articles with score above SCORE."
9755   (interactive "P")
9756   (gnus-summary-mark-above score gnus-ticked-mark))
9757
9758 (defun gnus-summary-mark-above (score mark)
9759   "Mark articles with score over SCORE with MARK."
9760   (interactive "P\ncMark: ")
9761   (setq score (if score
9762                   (prefix-numeric-value score)
9763                 (or gnus-summary-default-score 0)))
9764   (save-excursion
9765     (set-buffer gnus-summary-buffer)
9766     (goto-char (point-min))
9767     (while (and (progn
9768                   (when (> (gnus-summary-article-score) score)
9769                     (gnus-summary-mark-article nil mark))
9770                   t)
9771                 (gnus-summary-find-next)))))
9772
9773 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9774 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9775 (defun gnus-summary-limit-include-expunged (&optional no-error)
9776   "Display all the hidden articles that were expunged for low scores."
9777   (interactive)
9778   (let ((buffer-read-only nil))
9779     (let ((scored gnus-newsgroup-scored)
9780           headers h)
9781       (while scored
9782         (unless (gnus-summary-article-header (caar scored))
9783           (and (setq h (gnus-number-to-header (caar scored)))
9784                (< (cdar scored) gnus-summary-expunge-below)
9785                (push h headers)))
9786         (setq scored (cdr scored)))
9787       (if (not headers)
9788           (when (not no-error)
9789             (error "No expunged articles hidden"))
9790         (goto-char (point-min))
9791         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9792         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9793         (mapcar (lambda (x) (push (mail-header-number x)
9794                                   gnus-newsgroup-limit))
9795                 headers)
9796         (gnus-summary-prepare-unthreaded (nreverse headers))
9797         (goto-char (point-min))
9798         (gnus-summary-position-point)
9799         t))))
9800
9801 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9802   "Mark all unread articles in this newsgroup as read.
9803 If prefix argument ALL is non-nil, ticked and dormant articles will
9804 also be marked as read.
9805 If QUIETLY is non-nil, no questions will be asked.
9806 If TO-HERE is non-nil, it should be a point in the buffer.  All
9807 articles before (after, if REVERSE is set) this point will be marked as read.
9808 Note that this function will only catch up the unread article
9809 in the current summary buffer limitation.
9810 The number of articles marked as read is returned."
9811   (interactive "P")
9812   (prog1
9813       (save-excursion
9814         (when (or quietly
9815                   (not gnus-interactive-catchup) ;Without confirmation?
9816                   gnus-expert-user
9817                   (gnus-y-or-n-p
9818                    (if all
9819                        "Mark absolutely all articles as read? "
9820                      "Mark all unread articles as read? ")))
9821           (if (and not-mark
9822                    (not gnus-newsgroup-adaptive)
9823                    (not gnus-newsgroup-auto-expire)
9824                    (not gnus-suppress-duplicates)
9825                    (or (not gnus-use-cache)
9826                        (eq gnus-use-cache 'passive)))
9827               (progn
9828                 (when all
9829                   (setq gnus-newsgroup-marked nil
9830                         gnus-newsgroup-spam-marked nil
9831                         gnus-newsgroup-dormant nil))
9832                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9833             ;; We actually mark all articles as canceled, which we
9834             ;; have to do when using auto-expiry or adaptive scoring.
9835             (gnus-summary-show-all-threads)
9836             (if (and to-here reverse)
9837                 (progn
9838                   (goto-char to-here)
9839                   (while (and
9840                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9841                           (gnus-summary-find-next (not all) nil nil t))))
9842               (when (gnus-summary-first-subject (not all) t)
9843                 (while (and
9844                         (if to-here (< (point) to-here) t)
9845                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9846                         (gnus-summary-find-next (not all) nil nil t)))))
9847             (gnus-set-mode-line 'summary))
9848           t))
9849     (gnus-summary-position-point)))
9850
9851 (defun gnus-summary-catchup-to-here (&optional all)
9852   "Mark all unticked articles before the current one as read.
9853 If ALL is non-nil, also mark ticked and dormant articles as read."
9854   (interactive "P")
9855   (save-excursion
9856     (gnus-save-hidden-threads
9857       (let ((beg (point)))
9858         ;; We check that there are unread articles.
9859         (when (or all (gnus-summary-find-prev))
9860           (gnus-summary-catchup all t beg)))))
9861   (gnus-summary-position-point))
9862
9863 (defun gnus-summary-catchup-from-here (&optional all)
9864   "Mark all unticked articles after the current one as read.
9865 If ALL is non-nil, also mark ticked and dormant articles as read."
9866   (interactive "P")
9867   (save-excursion
9868     (gnus-save-hidden-threads
9869       (let ((beg (point)))
9870         ;; We check that there are unread articles.
9871         (when (or all (gnus-summary-find-next))
9872           (gnus-summary-catchup all t beg nil t)))))
9873
9874   (gnus-summary-position-point))
9875 (defun gnus-summary-catchup-all (&optional quietly)
9876   "Mark all articles in this newsgroup as read."
9877   (interactive "P")
9878   (gnus-summary-catchup t quietly))
9879
9880 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9881   "Mark all unread articles in this group as read, then exit.
9882 If prefix argument ALL is non-nil, all articles are marked as read.
9883 If QUIETLY is non-nil, no questions will be asked."
9884   (interactive "P")
9885   (when (gnus-summary-catchup all quietly nil 'fast)
9886     ;; Select next newsgroup or exit.
9887     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9888              (eq gnus-auto-select-next 'quietly))
9889         (gnus-summary-next-group nil)
9890       (gnus-summary-exit))))
9891
9892 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9893   "Mark all articles in this newsgroup as read, and then exit."
9894   (interactive "P")
9895   (gnus-summary-catchup-and-exit t quietly))
9896
9897 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9898   "Mark all articles in this group as read and select the next group.
9899 If given a prefix, mark all articles, unread as well as ticked, as
9900 read."
9901   (interactive "P")
9902   (save-excursion
9903     (gnus-summary-catchup all))
9904   (gnus-summary-next-group))
9905
9906 ;;;
9907 ;;; with article
9908 ;;;
9909
9910 (defmacro gnus-with-article (article &rest forms)
9911   "Select ARTICLE and perform FORMS in the original article buffer.
9912 Then replace the article with the result."
9913   `(progn
9914      ;; We don't want the article to be marked as read.
9915      (let (gnus-mark-article-hook)
9916        (gnus-summary-select-article t t nil ,article))
9917      (set-buffer gnus-original-article-buffer)
9918      ,@forms
9919      (if (not (gnus-check-backend-function
9920                'request-replace-article (car gnus-article-current)))
9921          (gnus-message 5 "Read-only group; not replacing")
9922        (unless (gnus-request-replace-article
9923                 ,article (car gnus-article-current)
9924                 (current-buffer) t)
9925          (error "Couldn't replace article")))
9926      ;; The cache and backlog have to be flushed somewhat.
9927      (when gnus-keep-backlog
9928        (gnus-backlog-remove-article
9929         (car gnus-article-current) (cdr gnus-article-current)))
9930      (when gnus-use-cache
9931        (gnus-cache-update-article
9932         (car gnus-article-current) (cdr gnus-article-current)))))
9933
9934 (put 'gnus-with-article 'lisp-indent-function 1)
9935 (put 'gnus-with-article 'edebug-form-spec '(form body))
9936
9937 ;; Thread-based commands.
9938
9939 (defun gnus-summary-articles-in-thread (&optional article)
9940   "Return a list of all articles in the current thread.
9941 If ARTICLE is non-nil, return all articles in the thread that starts
9942 with that article."
9943   (let* ((article (or article (gnus-summary-article-number)))
9944          (data (gnus-data-find-list article))
9945          (top-level (gnus-data-level (car data)))
9946          (top-subject
9947           (cond ((null gnus-thread-operation-ignore-subject)
9948                  (gnus-simplify-subject-re
9949                   (mail-header-subject (gnus-data-header (car data)))))
9950                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9951                  (gnus-simplify-subject-fuzzy
9952                   (mail-header-subject (gnus-data-header (car data)))))
9953                 (t nil)))
9954          (end-point (save-excursion
9955                       (if (gnus-summary-go-to-next-thread)
9956                           (point) (point-max))))
9957          articles)
9958     (while (and data
9959                 (< (gnus-data-pos (car data)) end-point))
9960       (when (or (not top-subject)
9961                 (string= top-subject
9962                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9963                              (gnus-simplify-subject-fuzzy
9964                               (mail-header-subject
9965                                (gnus-data-header (car data))))
9966                            (gnus-simplify-subject-re
9967                             (mail-header-subject
9968                              (gnus-data-header (car data)))))))
9969         (push (gnus-data-number (car data)) articles))
9970       (unless (and (setq data (cdr data))
9971                    (> (gnus-data-level (car data)) top-level))
9972         (setq data nil)))
9973     ;; Return the list of articles.
9974     (nreverse articles)))
9975
9976 (defun gnus-summary-rethread-current ()
9977   "Rethread the thread the current article is part of."
9978   (interactive)
9979   (let* ((gnus-show-threads t)
9980          (article (gnus-summary-article-number))
9981          (id (mail-header-id (gnus-summary-article-header)))
9982          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9983     (unless id
9984       (error "No article on the current line"))
9985     (gnus-rebuild-thread id)
9986     (gnus-summary-goto-subject article)))
9987
9988 (defun gnus-summary-reparent-thread ()
9989   "Make the current article child of the marked (or previous) article.
9990
9991 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9992 is non-nil or the Subject: of both articles are the same."
9993   (interactive)
9994   (unless (not (gnus-group-read-only-p))
9995     (error "The current newsgroup does not support article editing"))
9996   (unless (<= (length gnus-newsgroup-processable) 1)
9997     (error "No more than one article may be marked"))
9998   (save-window-excursion
9999     (let ((gnus-article-buffer " *reparent*")
10000           (current-article (gnus-summary-article-number))
10001           ;; First grab the marked article, otherwise one line up.
10002           (parent-article (if (not (null gnus-newsgroup-processable))
10003                               (car gnus-newsgroup-processable)
10004                             (save-excursion
10005                               (if (eq (forward-line -1) 0)
10006                                   (gnus-summary-article-number)
10007                                 (error "Beginning of summary buffer"))))))
10008       (unless (not (eq current-article parent-article))
10009         (error "An article may not be self-referential"))
10010       (let ((message-id (mail-header-id
10011                          (gnus-summary-article-header parent-article))))
10012         (unless (and message-id (not (equal message-id "")))
10013           (error "No message-id in desired parent"))
10014         (gnus-with-article current-article
10015           (save-restriction
10016             (goto-char (point-min))
10017             (message-narrow-to-head)
10018             (if (re-search-forward "^References: " nil t)
10019                 (progn
10020                   (re-search-forward "^[^ \t]" nil t)
10021                   (forward-line -1)
10022                   (end-of-line)
10023                   (insert " " message-id))
10024               (insert "References: " message-id "\n"))))
10025         (set-buffer gnus-summary-buffer)
10026         (gnus-summary-unmark-all-processable)
10027         (gnus-summary-update-article current-article)
10028         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
10029             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
10030         (gnus-summary-rethread-current)
10031         (gnus-message 3 "Article %d is now the child of article %d"
10032                       current-article parent-article)))))
10033
10034 (defun gnus-summary-toggle-threads (&optional arg)
10035   "Toggle showing conversation threads.
10036 If ARG is positive number, turn showing conversation threads on."
10037   (interactive "P")
10038   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
10039     (setq gnus-show-threads
10040           (if (null arg) (not gnus-show-threads)
10041             (> (prefix-numeric-value arg) 0)))
10042     (gnus-summary-prepare)
10043     (gnus-summary-goto-subject current)
10044     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
10045     (gnus-summary-position-point)))
10046
10047 (defun gnus-summary-show-all-threads ()
10048   "Show all threads."
10049   (interactive)
10050   (save-excursion
10051     (let ((buffer-read-only nil))
10052       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
10053   (gnus-summary-position-point))
10054
10055 (defun gnus-summary-show-thread ()
10056   "Show thread subtrees.
10057 Returns nil if no thread was there to be shown."
10058   (interactive)
10059   (let ((buffer-read-only nil)
10060         (orig (point))
10061         ;; first goto end then to beg, to have point at beg after let
10062         (end (progn (end-of-line) (point)))
10063         (beg (progn (beginning-of-line) (point))))
10064     (prog1
10065         ;; Any hidden lines here?
10066         (search-forward "\r" end t)
10067       (subst-char-in-region beg end ?\^M ?\n t)
10068       (goto-char orig)
10069       (gnus-summary-position-point))))
10070
10071 (defun gnus-summary-maybe-hide-threads ()
10072   "If requested, hide the threads that should be hidden."
10073   (when (and gnus-show-threads
10074              gnus-thread-hide-subtree)
10075     (gnus-summary-hide-all-threads
10076      (if (or (consp gnus-thread-hide-subtree)
10077              (gnus-functionp gnus-thread-hide-subtree))
10078          (gnus-make-predicate gnus-thread-hide-subtree)
10079        nil))))
10080
10081 ;;; Hiding predicates.
10082
10083 (defun gnus-article-unread-p (header)
10084   (memq (mail-header-number header) gnus-newsgroup-unreads))
10085
10086 (defun gnus-article-unseen-p (header)
10087   (memq (mail-header-number header) gnus-newsgroup-unseen))
10088
10089 (defun gnus-map-articles (predicate articles)
10090   "Map PREDICATE over ARTICLES and return non-nil if any predicate is non-nil."
10091   (apply 'gnus-or (mapcar predicate
10092                           (mapcar 'gnus-summary-article-header articles))))
10093
10094 (defun gnus-summary-hide-all-threads (&optional predicate)
10095   "Hide all thread subtrees.
10096 If PREDICATE is supplied, threads that satisfy this predicate
10097 will not be hidden."
10098   (interactive)
10099   (save-excursion
10100     (goto-char (point-min))
10101     (let ((end nil))
10102       (while (not end)
10103         (when (or (not predicate)
10104                   (gnus-map-articles
10105                    predicate (gnus-summary-article-children)))
10106             (gnus-summary-hide-thread))
10107         (setq end (not (zerop (gnus-summary-next-thread 1 t)))))))
10108   (gnus-summary-position-point))
10109
10110 (defun gnus-summary-hide-thread ()
10111   "Hide thread subtrees.
10112 If PREDICATE is supplied, threads that satisfy this predicate
10113 will not be hidden.
10114 Returns nil if no threads were there to be hidden."
10115   (interactive)
10116   (let ((buffer-read-only nil)
10117         (start (point))
10118         (article (gnus-summary-article-number)))
10119     (goto-char start)
10120     ;; Go forward until either the buffer ends or the subthread
10121     ;; ends.
10122     (when (and (not (eobp))
10123                (or (zerop (gnus-summary-next-thread 1 t))
10124                    (goto-char (point-max))))
10125       (prog1
10126           (if (and (> (point) start)
10127                    (search-backward "\n" start t))
10128               (progn
10129                 (subst-char-in-region start (point) ?\n ?\^M)
10130                 (gnus-summary-goto-subject article))
10131             (goto-char start)
10132             nil)))))
10133
10134 (defun gnus-summary-go-to-next-thread (&optional previous)
10135   "Go to the same level (or less) next thread.
10136 If PREVIOUS is non-nil, go to previous thread instead.
10137 Return the article number moved to, or nil if moving was impossible."
10138   (let ((level (gnus-summary-thread-level))
10139         (way (if previous -1 1))
10140         (beg (point)))
10141     (forward-line way)
10142     (while (and (not (eobp))
10143                 (< level (gnus-summary-thread-level)))
10144       (forward-line way))
10145     (if (eobp)
10146         (progn
10147           (goto-char beg)
10148           nil)
10149       (setq beg (point))
10150       (prog1
10151           (gnus-summary-article-number)
10152         (goto-char beg)))))
10153
10154 (defun gnus-summary-next-thread (n &optional silent)
10155   "Go to the same level next N'th thread.
10156 If N is negative, search backward instead.
10157 Returns the difference between N and the number of skips actually
10158 done.
10159
10160 If SILENT, don't output messages."
10161   (interactive "p")
10162   (let ((backward (< n 0))
10163         (n (abs n)))
10164     (while (and (> n 0)
10165                 (gnus-summary-go-to-next-thread backward))
10166       (decf n))
10167     (unless silent
10168       (gnus-summary-position-point))
10169     (when (and (not silent) (/= 0 n))
10170       (gnus-message 7 "No more threads"))
10171     n))
10172
10173 (defun gnus-summary-prev-thread (n)
10174   "Go to the same level previous N'th thread.
10175 Returns the difference between N and the number of skips actually
10176 done."
10177   (interactive "p")
10178   (gnus-summary-next-thread (- n)))
10179
10180 (defun gnus-summary-go-down-thread ()
10181   "Go down one level in the current thread."
10182   (let ((children (gnus-summary-article-children)))
10183     (when children
10184       (gnus-summary-goto-subject (car children)))))
10185
10186 (defun gnus-summary-go-up-thread ()
10187   "Go up one level in the current thread."
10188   (let ((parent (gnus-summary-article-parent)))
10189     (when parent
10190       (gnus-summary-goto-subject parent))))
10191
10192 (defun gnus-summary-down-thread (n)
10193   "Go down thread N steps.
10194 If N is negative, go up instead.
10195 Returns the difference between N and how many steps down that were
10196 taken."
10197   (interactive "p")
10198   (let ((up (< n 0))
10199         (n (abs n)))
10200     (while (and (> n 0)
10201                 (if up (gnus-summary-go-up-thread)
10202                   (gnus-summary-go-down-thread)))
10203       (setq n (1- n)))
10204     (gnus-summary-position-point)
10205     (when (/= 0 n)
10206       (gnus-message 7 "Can't go further"))
10207     n))
10208
10209 (defun gnus-summary-up-thread (n)
10210   "Go up thread N steps.
10211 If N is negative, go down instead.
10212 Returns the difference between N and how many steps down that were
10213 taken."
10214   (interactive "p")
10215   (gnus-summary-down-thread (- n)))
10216
10217 (defun gnus-summary-top-thread ()
10218   "Go to the top of the thread."
10219   (interactive)
10220   (while (gnus-summary-go-up-thread))
10221   (gnus-summary-article-number))
10222
10223 (defun gnus-summary-kill-thread (&optional unmark)
10224   "Mark articles under current thread as read.
10225 If the prefix argument is positive, remove any kinds of marks.
10226 If the prefix argument is negative, tick articles instead."
10227   (interactive "P")
10228   (when unmark
10229     (setq unmark (prefix-numeric-value unmark)))
10230   (let ((articles (gnus-summary-articles-in-thread)))
10231     (save-excursion
10232       ;; Expand the thread.
10233       (gnus-summary-show-thread)
10234       ;; Mark all the articles.
10235       (while articles
10236         (gnus-summary-goto-subject (car articles))
10237         (cond ((null unmark)
10238                (gnus-summary-mark-article-as-read gnus-killed-mark))
10239               ((> unmark 0)
10240                (gnus-summary-mark-article-as-unread gnus-unread-mark))
10241               (t
10242                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
10243         (setq articles (cdr articles))))
10244     ;; Hide killed subtrees.
10245     (and (null unmark)
10246          gnus-thread-hide-killed
10247          (gnus-summary-hide-thread))
10248     ;; If marked as read, go to next unread subject.
10249     (when (null unmark)
10250       ;; Go to next unread subject.
10251       (gnus-summary-next-subject 1 t)))
10252   (gnus-set-mode-line 'summary))
10253
10254 ;; Summary sorting commands
10255
10256 (defun gnus-summary-sort-by-number (&optional reverse)
10257   "Sort the summary buffer by article number.
10258 Argument REVERSE means reverse order."
10259   (interactive "P")
10260   (gnus-summary-sort 'number reverse))
10261
10262 (defun gnus-summary-sort-by-random (&optional reverse)
10263   "Randomize the order in the summary buffer.
10264 Argument REVERSE means to randomize in reverse order."
10265   (interactive "P")
10266   (gnus-summary-sort 'random reverse))
10267
10268 (defun gnus-summary-sort-by-author (&optional reverse)
10269   "Sort the summary buffer by author name alphabetically.
10270 If `case-fold-search' is non-nil, case of letters is ignored.
10271 Argument REVERSE means reverse order."
10272   (interactive "P")
10273   (gnus-summary-sort 'author reverse))
10274
10275 (defun gnus-summary-sort-by-subject (&optional reverse)
10276   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
10277 If `case-fold-search' is non-nil, case of letters is ignored.
10278 Argument REVERSE means reverse order."
10279   (interactive "P")
10280   (gnus-summary-sort 'subject reverse))
10281
10282 (defun gnus-summary-sort-by-date (&optional reverse)
10283   "Sort the summary buffer by date.
10284 Argument REVERSE means reverse order."
10285   (interactive "P")
10286   (gnus-summary-sort 'date reverse))
10287
10288 (defun gnus-summary-sort-by-score (&optional reverse)
10289   "Sort the summary buffer by score.
10290 Argument REVERSE means reverse order."
10291   (interactive "P")
10292   (gnus-summary-sort 'score reverse))
10293
10294 (defun gnus-summary-sort-by-lines (&optional reverse)
10295   "Sort the summary buffer by the number of lines.
10296 Argument REVERSE means reverse order."
10297   (interactive "P")
10298   (gnus-summary-sort 'lines reverse))
10299
10300 (defun gnus-summary-sort-by-chars (&optional reverse)
10301   "Sort the summary buffer by article length.
10302 Argument REVERSE means reverse order."
10303   (interactive "P")
10304   (gnus-summary-sort 'chars reverse))
10305
10306 (defun gnus-summary-sort-by-original (&optional reverse)
10307   "Sort the summary buffer using the default sorting method.
10308 Argument REVERSE means reverse order."
10309   (interactive "P")
10310   (let* ((buffer-read-only)
10311          (gnus-summary-prepare-hook nil))
10312     ;; We do the sorting by regenerating the threads.
10313     (gnus-summary-prepare)
10314     ;; Hide subthreads if needed.
10315     (gnus-summary-maybe-hide-threads)))
10316
10317 (defun gnus-summary-sort (predicate reverse)
10318   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
10319   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
10320          (article (intern (format "gnus-article-sort-by-%s" predicate)))
10321          (gnus-thread-sort-functions
10322           (if (not reverse)
10323               thread
10324             `(lambda (t1 t2)
10325                (,thread t2 t1))))
10326          (gnus-sort-gathered-threads-function
10327           gnus-thread-sort-functions)
10328          (gnus-article-sort-functions
10329           (if (not reverse)
10330               article
10331             `(lambda (t1 t2)
10332                (,article t2 t1))))
10333          (buffer-read-only)
10334          (gnus-summary-prepare-hook nil))
10335     ;; We do the sorting by regenerating the threads.
10336     (gnus-summary-prepare)
10337     ;; Hide subthreads if needed.
10338     (gnus-summary-maybe-hide-threads)))
10339
10340 ;; Summary saving commands.
10341
10342 (defun gnus-summary-save-article (&optional n not-saved)
10343   "Save the current article using the default saver function.
10344 If N is a positive number, save the N next articles.
10345 If N is a negative number, save the N previous articles.
10346 If N is nil and any articles have been marked with the process mark,
10347 save those articles instead.
10348 The variable `gnus-default-article-saver' specifies the saver function."
10349   (interactive "P")
10350   (let* ((articles (gnus-summary-work-articles n))
10351          (save-buffer (save-excursion
10352                         (nnheader-set-temp-buffer " *Gnus Save*")))
10353          (num (length articles))
10354          header file)
10355     (dolist (article articles)
10356       (setq header (gnus-summary-article-header article))
10357       (if (not (vectorp header))
10358           ;; This is a pseudo-article.
10359           (if (assq 'name header)
10360               (gnus-copy-file (cdr (assq 'name header)))
10361             (gnus-message 1 "Article %d is unsaveable" article))
10362         ;; This is a real article.
10363         (save-window-excursion
10364           (let ((gnus-display-mime-function nil)
10365                 (gnus-article-prepare-hook nil))
10366             (gnus-summary-select-article t nil nil article)))
10367         (save-excursion
10368           (set-buffer save-buffer)
10369           (erase-buffer)
10370           (insert-buffer-substring gnus-original-article-buffer))
10371         (setq file (gnus-article-save save-buffer file num))
10372         (gnus-summary-remove-process-mark article)
10373         (unless not-saved
10374           (gnus-summary-set-saved-mark article))))
10375     (gnus-kill-buffer save-buffer)
10376     (gnus-summary-position-point)
10377     (gnus-set-mode-line 'summary)
10378     n))
10379
10380 (defun gnus-summary-pipe-output (&optional arg)
10381   "Pipe the current article to a subprocess.
10382 If N is a positive number, pipe the N next articles.
10383 If N is a negative number, pipe the N previous articles.
10384 If N is nil and any articles have been marked with the process mark,
10385 pipe those articles instead."
10386   (interactive "P")
10387   (require 'gnus-art)
10388   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
10389     (gnus-summary-save-article arg t))
10390   (let ((buffer (get-buffer "*Shell Command Output*")))
10391     (if (and buffer
10392              (with-current-buffer buffer (> (point-max) (point-min))))
10393         (gnus-configure-windows 'pipe))))
10394
10395 (defun gnus-summary-save-article-mail (&optional arg)
10396   "Append the current article to an mail file.
10397 If N is a positive number, save the N next articles.
10398 If N is a negative number, save the N previous articles.
10399 If N is nil and any articles have been marked with the process mark,
10400 save those articles instead."
10401   (interactive "P")
10402   (require 'gnus-art)
10403   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
10404     (gnus-summary-save-article arg)))
10405
10406 (defun gnus-summary-save-article-rmail (&optional arg)
10407   "Append the current article to an rmail file.
10408 If N is a positive number, save the N next articles.
10409 If N is a negative number, save the N previous articles.
10410 If N is nil and any articles have been marked with the process mark,
10411 save those articles instead."
10412   (interactive "P")
10413   (require 'gnus-art)
10414   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
10415     (gnus-summary-save-article arg)))
10416
10417 (defun gnus-summary-save-article-file (&optional arg)
10418   "Append the current article to a file.
10419 If N is a positive number, save the N next articles.
10420 If N is a negative number, save the N previous articles.
10421 If N is nil and any articles have been marked with the process mark,
10422 save those articles instead."
10423   (interactive "P")
10424   (require 'gnus-art)
10425   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
10426     (gnus-summary-save-article arg)))
10427
10428 (defun gnus-summary-write-article-file (&optional arg)
10429   "Write the current article to a file, deleting the previous file.
10430 If N is a positive number, save the N next articles.
10431 If N is a negative number, save the N previous articles.
10432 If N is nil and any articles have been marked with the process mark,
10433 save those articles instead."
10434   (interactive "P")
10435   (require 'gnus-art)
10436   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
10437     (gnus-summary-save-article arg)))
10438
10439 (defun gnus-summary-save-article-body-file (&optional arg)
10440   "Append the current article body to a file.
10441 If N is a positive number, save the N next articles.
10442 If N is a negative number, save the N previous articles.
10443 If N is nil and any articles have been marked with the process mark,
10444 save those articles instead."
10445   (interactive "P")
10446   (require 'gnus-art)
10447   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
10448     (gnus-summary-save-article arg)))
10449
10450 (defun gnus-summary-muttprint (&optional arg)
10451   "Print the current article using Muttprint.
10452 If N is a positive number, save the N next articles.
10453 If N is a negative number, save the N previous articles.
10454 If N is nil and any articles have been marked with the process mark,
10455 save those articles instead."
10456   (interactive "P")
10457   (require 'gnus-art)
10458   (let ((gnus-default-article-saver 'gnus-summary-pipe-to-muttprint))
10459     (gnus-summary-save-article arg t)))
10460
10461 (defun gnus-summary-pipe-message (program)
10462   "Pipe the current article through PROGRAM."
10463   (interactive "sProgram: ")
10464   (gnus-summary-select-article)
10465   (let ((mail-header-separator ""))
10466     (gnus-eval-in-buffer-window gnus-article-buffer
10467       (save-restriction
10468         (widen)
10469         (let ((start (window-start))
10470               buffer-read-only)
10471           (message-pipe-buffer-body program)
10472           (set-window-start (get-buffer-window (current-buffer)) start))))))
10473
10474 (defun gnus-get-split-value (methods)
10475   "Return a value based on the split METHODS."
10476   (let (split-name method result match)
10477     (when methods
10478       (save-excursion
10479         (set-buffer gnus-original-article-buffer)
10480         (save-restriction
10481           (nnheader-narrow-to-headers)
10482           (while (and methods (not split-name))
10483             (goto-char (point-min))
10484             (setq method (pop methods))
10485             (setq match (car method))
10486             (when (cond
10487                    ((stringp match)
10488                     ;; Regular expression.
10489                     (ignore-errors
10490                       (re-search-forward match nil t)))
10491                    ((gnus-functionp match)
10492                     ;; Function.
10493                     (save-restriction
10494                       (widen)
10495                       (setq result (funcall match gnus-newsgroup-name))))
10496                    ((consp match)
10497                     ;; Form.
10498                     (save-restriction
10499                       (widen)
10500                       (setq result (eval match)))))
10501               (setq split-name (cdr method))
10502               (cond ((stringp result)
10503                      (push (expand-file-name
10504                             result gnus-article-save-directory)
10505                            split-name))
10506                     ((consp result)
10507                      (setq split-name (append result split-name)))))))))
10508     (nreverse split-name)))
10509
10510 (defun gnus-valid-move-group-p (group)
10511   (and (boundp group)
10512        (symbol-name group)
10513        (symbol-value group)
10514        (gnus-get-function (gnus-find-method-for-group
10515                            (symbol-name group)) 'request-accept-article t)))
10516
10517 (defun gnus-read-move-group-name (prompt default articles prefix)
10518   "Read a group name."
10519   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
10520          (minibuffer-confirm-incomplete nil) ; XEmacs
10521          (prom
10522           (format "%s %s to:"
10523                   prompt
10524                   (if (> (length articles) 1)
10525                       (format "these %d articles" (length articles))
10526                     "this article")))
10527          (to-newsgroup
10528           (cond
10529            ((null split-name)
10530             (gnus-completing-read-with-default
10531              default prom
10532              gnus-active-hashtb
10533              'gnus-valid-move-group-p
10534              nil prefix
10535              'gnus-group-history))
10536            ((= 1 (length split-name))
10537             (gnus-completing-read-with-default
10538              (car split-name) prom
10539              gnus-active-hashtb
10540              'gnus-valid-move-group-p
10541              nil nil
10542              'gnus-group-history))
10543            (t
10544             (gnus-completing-read-with-default
10545              nil prom
10546              (mapcar (lambda (el) (list el))
10547                      (nreverse split-name))
10548              nil nil nil
10549              'gnus-group-history))))
10550          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
10551     (when to-newsgroup
10552       (if (or (string= to-newsgroup "")
10553               (string= to-newsgroup prefix))
10554           (setq to-newsgroup default))
10555       (unless to-newsgroup
10556         (error "No group name entered"))
10557       (or (gnus-active to-newsgroup)
10558           (gnus-activate-group to-newsgroup nil nil to-method)
10559           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
10560                                      to-newsgroup))
10561               (or (and (gnus-request-create-group to-newsgroup to-method)
10562                        (gnus-activate-group
10563                         to-newsgroup nil nil to-method)
10564                        (gnus-subscribe-group to-newsgroup))
10565                   (error "Couldn't create group %s" to-newsgroup)))
10566           (error "No such group: %s" to-newsgroup)))
10567     to-newsgroup))
10568
10569 (defun gnus-summary-save-parts (type dir n &optional reverse)
10570   "Save parts matching TYPE to DIR.
10571 If REVERSE, save parts that do not match TYPE."
10572   (interactive
10573    (list (read-string "Save parts of type: "
10574                       (or (car gnus-summary-save-parts-type-history)
10575                           gnus-summary-save-parts-default-mime)
10576                       'gnus-summary-save-parts-type-history)
10577          (setq gnus-summary-save-parts-last-directory
10578                (read-file-name "Save to directory: "
10579                                gnus-summary-save-parts-last-directory
10580                                nil t))
10581          current-prefix-arg))
10582   (gnus-summary-iterate n
10583     (let ((gnus-display-mime-function nil)
10584           (gnus-inhibit-treatment t))
10585       (gnus-summary-select-article))
10586     (save-excursion
10587       (set-buffer gnus-article-buffer)
10588       (let ((handles (or gnus-article-mime-handles
10589                          (mm-dissect-buffer nil gnus-article-loose-mime)
10590                          (mm-uu-dissect))))
10591         (when handles
10592           (gnus-summary-save-parts-1 type dir handles reverse)
10593           (unless gnus-article-mime-handles ;; Don't destroy this case.
10594             (mm-destroy-parts handles)))))))
10595
10596 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10597   (if (stringp (car handle))
10598       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10599               (cdr handle))
10600     (when (if reverse
10601               (not (string-match type (mm-handle-media-type handle)))
10602             (string-match type (mm-handle-media-type handle)))
10603       (let ((file (expand-file-name
10604                    (file-name-nondirectory
10605                     (or
10606                      (mail-content-type-get
10607                       (mm-handle-disposition handle) 'filename)
10608                      (concat gnus-newsgroup-name
10609                              "." (number-to-string
10610                                   (cdr gnus-article-current)))))
10611                    dir)))
10612         (unless (file-exists-p file)
10613           (mm-save-part-to-file handle file))))))
10614
10615 ;; Summary extract commands
10616
10617 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10618   (let ((buffer-read-only nil)
10619         (article (gnus-summary-article-number))
10620         after-article b e)
10621     (unless (gnus-summary-goto-subject article)
10622       (error "No such article: %d" article))
10623     (gnus-summary-position-point)
10624     ;; If all commands are to be bunched up on one line, we collect
10625     ;; them here.
10626     (unless gnus-view-pseudos-separately
10627       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10628             files action)
10629         (while ps
10630           (setq action (cdr (assq 'action (car ps))))
10631           (setq files (list (cdr (assq 'name (car ps)))))
10632           (while (and ps (cdr ps)
10633                       (string= (or action "1")
10634                                (or (cdr (assq 'action (cadr ps))) "2")))
10635             (push (cdr (assq 'name (cadr ps))) files)
10636             (setcdr ps (cddr ps)))
10637           (when files
10638             (when (not (string-match "%s" action))
10639               (push " " files))
10640             (push " " files)
10641             (when (assq 'execute (car ps))
10642               (setcdr (assq 'execute (car ps))
10643                       (funcall (if (string-match "%s" action)
10644                                    'format 'concat)
10645                                action
10646                                (mapconcat
10647                                 (lambda (f)
10648                                   (if (equal f " ")
10649                                       f
10650                                     (mm-quote-arg f)))
10651                                 files " ")))))
10652           (setq ps (cdr ps)))))
10653     (if (and gnus-view-pseudos (not not-view))
10654         (while pslist
10655           (when (assq 'execute (car pslist))
10656             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10657                                   (eq gnus-view-pseudos 'not-confirm)))
10658           (setq pslist (cdr pslist)))
10659       (save-excursion
10660         (while pslist
10661           (setq after-article (or (cdr (assq 'article (car pslist)))
10662                                   (gnus-summary-article-number)))
10663           (gnus-summary-goto-subject after-article)
10664           (forward-line 1)
10665           (setq b (point))
10666           (insert "    " (file-name-nondirectory
10667                           (cdr (assq 'name (car pslist))))
10668                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10669           (setq e (point))
10670           (forward-line -1)             ; back to `b'
10671           (gnus-add-text-properties
10672            b (1- e) (list 'gnus-number gnus-reffed-article-number
10673                           gnus-mouse-face-prop gnus-mouse-face))
10674           (gnus-data-enter
10675            after-article gnus-reffed-article-number
10676            gnus-unread-mark b (car pslist) 0 (- e b))
10677           (setq gnus-newsgroup-unreads
10678                 (gnus-add-to-sorted-list gnus-newsgroup-unreads
10679                                          gnus-reffed-article-number))
10680           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10681           (setq pslist (cdr pslist)))))))
10682
10683 (defun gnus-pseudos< (p1 p2)
10684   (let ((c1 (cdr (assq 'action p1)))
10685         (c2 (cdr (assq 'action p2))))
10686     (and c1 c2 (string< c1 c2))))
10687
10688 (defun gnus-request-pseudo-article (props)
10689   (cond ((assq 'execute props)
10690          (gnus-execute-command (cdr (assq 'execute props)))))
10691   (let ((gnus-current-article (gnus-summary-article-number)))
10692     (gnus-run-hooks 'gnus-mark-article-hook)))
10693
10694 (defun gnus-execute-command (command &optional automatic)
10695   (save-excursion
10696     (gnus-article-setup-buffer)
10697     (set-buffer gnus-article-buffer)
10698     (setq buffer-read-only nil)
10699     (let ((command (if automatic command
10700                      (read-string "Command: " (cons command 0)))))
10701       (erase-buffer)
10702       (insert "$ " command "\n\n")
10703       (if gnus-view-pseudo-asynchronously
10704           (start-process "gnus-execute" (current-buffer) shell-file-name
10705                          shell-command-switch command)
10706         (call-process shell-file-name nil t nil
10707                       shell-command-switch command)))))
10708
10709 ;; Summary kill commands.
10710
10711 (defun gnus-summary-edit-global-kill (article)
10712   "Edit the \"global\" kill file."
10713   (interactive (list (gnus-summary-article-number)))
10714   (gnus-group-edit-global-kill article))
10715
10716 (defun gnus-summary-edit-local-kill ()
10717   "Edit a local kill file applied to the current newsgroup."
10718   (interactive)
10719   (setq gnus-current-headers (gnus-summary-article-header))
10720   (gnus-group-edit-local-kill
10721    (gnus-summary-article-number) gnus-newsgroup-name))
10722
10723 ;;; Header reading.
10724
10725 (defun gnus-read-header (id &optional header)
10726   "Read the headers of article ID and enter them into the Gnus system."
10727   (let ((group gnus-newsgroup-name)
10728         (gnus-override-method
10729          (or
10730           gnus-override-method
10731           (and (gnus-news-group-p gnus-newsgroup-name)
10732                (car (gnus-refer-article-methods)))))
10733         where)
10734     ;; First we check to see whether the header in question is already
10735     ;; fetched.
10736     (if (stringp id)
10737         ;; This is a Message-ID.
10738         (setq header (or header (gnus-id-to-header id)))
10739       ;; This is an article number.
10740       (setq header (or header (gnus-summary-article-header id))))
10741     (if (and header
10742              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10743         ;; We have found the header.
10744         header
10745       ;; If this is a sparse article, we have to nix out its
10746       ;; previous entry in the thread hashtb.
10747       (when (and header
10748                  (gnus-summary-article-sparse-p (mail-header-number header)))
10749         (let* ((parent (gnus-parent-id (mail-header-references header)))
10750                (thread (and parent (gnus-id-to-thread parent))))
10751           (when thread
10752             (delq (assq header thread) thread))))
10753       ;; We have to really fetch the header to this article.
10754       (save-excursion
10755         (set-buffer nntp-server-buffer)
10756         (when (setq where (gnus-request-head id group))
10757           (nnheader-fold-continuation-lines)
10758           (goto-char (point-max))
10759           (insert ".\n")
10760           (goto-char (point-min))
10761           (insert "211 ")
10762           (princ (cond
10763                   ((numberp id) id)
10764                   ((cdr where) (cdr where))
10765                   (header (mail-header-number header))
10766                   (t gnus-reffed-article-number))
10767                  (current-buffer))
10768           (insert " Article retrieved.\n"))
10769         (if (or (not where)
10770                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10771             ()                          ; Malformed head.
10772           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10773             (when (and (stringp id)
10774                        (not (string= (gnus-group-real-name group)
10775                                      (car where))))
10776               ;; If we fetched by Message-ID and the article came
10777               ;; from a different group, we fudge some bogus article
10778               ;; numbers for this article.
10779               (mail-header-set-number header gnus-reffed-article-number))
10780             (save-excursion
10781               (set-buffer gnus-summary-buffer)
10782               (decf gnus-reffed-article-number)
10783               (gnus-remove-header (mail-header-number header))
10784               (push header gnus-newsgroup-headers)
10785               (setq gnus-current-headers header)
10786               (push (mail-header-number header) gnus-newsgroup-limit)))
10787           header)))))
10788
10789 (defun gnus-remove-header (number)
10790   "Remove header NUMBER from `gnus-newsgroup-headers'."
10791   (if (and gnus-newsgroup-headers
10792            (= number (mail-header-number (car gnus-newsgroup-headers))))
10793       (pop gnus-newsgroup-headers)
10794     (let ((headers gnus-newsgroup-headers))
10795       (while (and (cdr headers)
10796                   (not (= number (mail-header-number (cadr headers)))))
10797         (pop headers))
10798       (when (cdr headers)
10799         (setcdr headers (cddr headers))))))
10800
10801 ;;;
10802 ;;; summary highlights
10803 ;;;
10804
10805 (defun gnus-highlight-selected-summary ()
10806   "Highlight selected article in summary buffer."
10807   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10808   (when gnus-summary-selected-face
10809     (save-excursion
10810       (let* ((beg (progn (beginning-of-line) (point)))
10811              (end (progn (end-of-line) (point)))
10812              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10813              (from (if (get-text-property beg gnus-mouse-face-prop)
10814                        beg
10815                      (or (next-single-property-change
10816                           beg gnus-mouse-face-prop nil end)
10817                          beg)))
10818              (to
10819               (if (= from end)
10820                   (- from 2)
10821                 (or (next-single-property-change
10822                      from gnus-mouse-face-prop nil end)
10823                     end))))
10824         ;; If no mouse-face prop on line we will have to = from = end,
10825         ;; so we highlight the entire line instead.
10826         (when (= (+ to 2) from)
10827           (setq from beg)
10828           (setq to end))
10829         (if gnus-newsgroup-selected-overlay
10830             ;; Move old overlay.
10831             (gnus-move-overlay
10832              gnus-newsgroup-selected-overlay from to (current-buffer))
10833           ;; Create new overlay.
10834           (gnus-overlay-put
10835            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10836            'face gnus-summary-selected-face))))))
10837
10838 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10839 (defun gnus-summary-highlight-line ()
10840   "Highlight current line according to `gnus-summary-highlight'."
10841   (let* ((list gnus-summary-highlight)
10842          (beg (gnus-point-at-bol))
10843          (article (gnus-summary-article-number))
10844          (score (or (cdr (assq (or article gnus-current-article)
10845                                gnus-newsgroup-scored))
10846                     gnus-summary-default-score 0))
10847          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10848          (inhibit-read-only t)
10849          (default gnus-summary-default-score)
10850          (default-high gnus-summary-default-high-score)
10851          (default-low gnus-summary-default-low-score))
10852     ;; Eval the cars of the lists until we find a match.
10853     (while (and list
10854                 (not (eval (caar list))))
10855       (setq list (cdr list)))
10856     (let ((face (cdar list)))
10857       (unless (eq face (get-text-property beg 'face))
10858         (gnus-put-text-property-excluding-characters-with-faces
10859          beg (gnus-point-at-eol) 'face
10860          (setq face (if (boundp face) (symbol-value face) face)))
10861         (when gnus-summary-highlight-line-function
10862           (funcall gnus-summary-highlight-line-function article face))))))
10863
10864 (defun gnus-update-read-articles (group unread &optional compute)
10865   "Update the list of read articles in GROUP.
10866 UNREAD is a sorted list."
10867   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10868          (entry (gnus-gethash group gnus-newsrc-hashtb))
10869          (info (nth 2 entry))
10870          (prev 1)
10871          read)
10872     (if (or (not info) (not active))
10873         ;; There is no info on this group if it was, in fact,
10874         ;; killed.  Gnus stores no information on killed groups, so
10875         ;; there's nothing to be done.
10876         ;; One could store the information somewhere temporarily,
10877         ;; perhaps...  Hmmm...
10878         ()
10879       ;; Remove any negative articles numbers.
10880       (while (and unread (< (car unread) 0))
10881         (setq unread (cdr unread)))
10882       ;; Remove any expired article numbers
10883       (while (and unread (< (car unread) (car active)))
10884         (setq unread (cdr unread)))
10885       ;; Compute the ranges of read articles by looking at the list of
10886       ;; unread articles.
10887       (while unread
10888         (when (/= (car unread) prev)
10889           (push (if (= prev (1- (car unread))) prev
10890                   (cons prev (1- (car unread))))
10891                 read))
10892         (setq prev (1+ (car unread)))
10893         (setq unread (cdr unread)))
10894       (when (<= prev (cdr active))
10895         (push (cons prev (cdr active)) read))
10896       (setq read (if (> (length read) 1) (nreverse read) read))
10897       (if compute
10898           read
10899         (save-excursion
10900           (let (setmarkundo)
10901             ;; Propagate the read marks to the backend.
10902             (when (gnus-check-backend-function 'request-set-mark group)
10903               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10904                     (add (gnus-remove-from-range read (gnus-info-read info))))
10905                 (when (or add del)
10906                   (unless (gnus-check-group group)
10907                     (error "Can't open server for %s" group))
10908                   (gnus-request-set-mark
10909                    group (delq nil (list (if add (list add 'add '(read)))
10910                                          (if del (list del 'del '(read))))))
10911                   (setq setmarkundo
10912                         `(gnus-request-set-mark
10913                           ,group
10914                           ',(delq nil (list
10915                                        (if del (list del 'add '(read)))
10916                                        (if add (list add 'del '(read))))))))))
10917             (set-buffer gnus-group-buffer)
10918             (gnus-undo-register
10919               `(progn
10920                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10921                  (gnus-info-set-read ',info ',(gnus-info-read info))
10922                  (gnus-get-unread-articles-in-group ',info
10923                                                     (gnus-active ,group))
10924                  (gnus-group-update-group ,group t)
10925                  ,setmarkundo))))
10926         ;; Enter this list into the group info.
10927         (gnus-info-set-read info read)
10928         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10929         (gnus-get-unread-articles-in-group info (gnus-active group))
10930         t))))
10931
10932 (defun gnus-offer-save-summaries ()
10933   "Offer to save all active summary buffers."
10934   (let (buffers)
10935     ;; Go through all buffers and find all summaries.
10936     (dolist (buffer (buffer-list))
10937       (when (and (setq buffer (buffer-name buffer))
10938                  (string-match "Summary" buffer)
10939                  (save-excursion
10940                    (set-buffer buffer)
10941                    ;; We check that this is, indeed, a summary buffer.
10942                    (and (eq major-mode 'gnus-summary-mode)
10943                         ;; Also make sure this isn't bogus.
10944                         gnus-newsgroup-prepared
10945                         ;; Also make sure that this isn't a
10946                         ;; dead summary buffer.
10947                         (not gnus-dead-summary-mode))))
10948         (push buffer buffers)))
10949     ;; Go through all these summary buffers and offer to save them.
10950     (when buffers
10951       (save-excursion
10952         (map-y-or-n-p
10953          "Update summary buffer %s? "
10954          (lambda (buf)
10955            (switch-to-buffer buf)
10956            (gnus-summary-exit))
10957          buffers)))))
10958
10959 (defun gnus-summary-setup-default-charset ()
10960   "Setup newsgroup default charset."
10961   (if (member gnus-newsgroup-name '("nndraft:delayed" "nndraft:drafts"))
10962       (setq gnus-newsgroup-charset nil)
10963     (let* ((ignored-charsets
10964             (or gnus-newsgroup-ephemeral-ignored-charsets
10965                 (append
10966                  (and gnus-newsgroup-name
10967                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10968                  gnus-newsgroup-ignored-charsets))))
10969       (setq gnus-newsgroup-charset
10970             (or gnus-newsgroup-ephemeral-charset
10971                 (and gnus-newsgroup-name
10972                      (gnus-parameter-charset gnus-newsgroup-name))
10973                 gnus-default-charset))
10974       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10975            ignored-charsets))))
10976
10977 ;;;
10978 ;;; Mime Commands
10979 ;;;
10980
10981 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10982   "Display the current article buffer fully MIME-buttonized.
10983 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10984 treated as multipart/mixed."
10985   (interactive "P")
10986   (require 'gnus-art)
10987   (let ((gnus-unbuttonized-mime-types nil)
10988         (gnus-mime-display-multipart-as-mixed show-all-parts))
10989     (gnus-summary-show-article)))
10990
10991 (defun gnus-summary-repair-multipart (article)
10992   "Add a Content-Type header to a multipart article without one."
10993   (interactive (list (gnus-summary-article-number)))
10994   (gnus-with-article article
10995     (message-narrow-to-head)
10996     (message-remove-header "Mime-Version")
10997     (goto-char (point-max))
10998     (insert "Mime-Version: 1.0\n")
10999     (widen)
11000     (when (search-forward "\n--" nil t)
11001       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
11002         (message-narrow-to-head)
11003         (message-remove-header "Content-Type")
11004         (goto-char (point-max))
11005         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
11006                         separator))
11007         (widen))))
11008   (let (gnus-mark-article-hook)
11009     (gnus-summary-select-article t t nil article)))
11010
11011 (defun gnus-summary-toggle-display-buttonized ()
11012   "Toggle the buttonizing of the article buffer."
11013   (interactive)
11014   (require 'gnus-art)
11015   (if (setq gnus-inhibit-mime-unbuttonizing
11016             (not gnus-inhibit-mime-unbuttonizing))
11017       (let ((gnus-unbuttonized-mime-types nil))
11018         (gnus-summary-show-article))
11019     (gnus-summary-show-article)))
11020
11021 ;;;
11022 ;;; Generic summary marking commands
11023 ;;;
11024
11025 (defvar gnus-summary-marking-alist
11026   '((read gnus-del-mark "d")
11027     (unread gnus-unread-mark "u")
11028     (ticked gnus-ticked-mark "!")
11029     (dormant gnus-dormant-mark "?")
11030     (expirable gnus-expirable-mark "e"))
11031   "An alist of names/marks/keystrokes.")
11032
11033 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
11034 (defvar gnus-summary-mark-map)
11035
11036 (defun gnus-summary-make-all-marking-commands ()
11037   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
11038   (dolist (elem gnus-summary-marking-alist)
11039     (apply 'gnus-summary-make-marking-command elem)))
11040
11041 (defun gnus-summary-make-marking-command (name mark keystroke)
11042   (let ((map (make-sparse-keymap)))
11043     (define-key gnus-summary-generic-mark-map keystroke map)
11044     (dolist (lway `((next "next" next nil "n")
11045                     (next-unread "next unread" next t "N")
11046                     (prev "previous" prev nil "p")
11047                     (prev-unread "previous unread" prev t "P")
11048                     (nomove "" nil nil ,keystroke)))
11049       (let ((func (gnus-summary-make-marking-command-1
11050                    mark (car lway) lway name)))
11051         (setq func (eval func))
11052         (define-key map (nth 4 lway) func)))))
11053
11054 (defun gnus-summary-make-marking-command-1 (mark way lway name)
11055   `(defun ,(intern
11056             (format "gnus-summary-put-mark-as-%s%s"
11057                     name (if (eq way 'nomove)
11058                              ""
11059                            (concat "-" (symbol-name way)))))
11060      (n)
11061      ,(format
11062        "Mark the current article as %s%s.
11063 If N, the prefix, then repeat N times.
11064 If N is negative, move in reverse order.
11065 The difference between N and the actual number of articles marked is
11066 returned."
11067        name (cadr lway))
11068      (interactive "p")
11069      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
11070
11071 (defun gnus-summary-generic-mark (n mark move unread)
11072   "Mark N articles with MARK."
11073   (unless (eq major-mode 'gnus-summary-mode)
11074     (error "This command can only be used in the summary buffer"))
11075   (gnus-summary-show-thread)
11076   (let ((nummove
11077          (cond
11078           ((eq move 'next) 1)
11079           ((eq move 'prev) -1)
11080           (t 0))))
11081     (if (zerop nummove)
11082         (setq n 1)
11083       (when (< n 0)
11084         (setq n (abs n)
11085               nummove (* -1 nummove))))
11086     (while (and (> n 0)
11087                 (gnus-summary-mark-article nil mark)
11088                 (zerop (gnus-summary-next-subject nummove unread t)))
11089       (setq n (1- n)))
11090     (when (/= 0 n)
11091       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11092     (gnus-summary-recenter)
11093     (gnus-summary-position-point)
11094     (gnus-set-mode-line 'summary)
11095     n))
11096
11097 (defun gnus-summary-insert-articles (articles)
11098   (when (setq articles
11099               (gnus-sorted-difference articles
11100                                       (mapcar (lambda (h)
11101                                                 (mail-header-number h))
11102                                               gnus-newsgroup-headers)))
11103     (setq gnus-newsgroup-headers
11104           (merge 'list
11105                  gnus-newsgroup-headers
11106                  (gnus-fetch-headers articles)
11107                  'gnus-article-sort-by-number))
11108     ;; Suppress duplicates?
11109     (when gnus-suppress-duplicates
11110       (gnus-dup-suppress-articles))
11111
11112     ;; We might want to build some more threads first.
11113     (when (and gnus-fetch-old-headers
11114                (eq gnus-headers-retrieved-by 'nov))
11115       (if (eq gnus-fetch-old-headers 'invisible)
11116           (gnus-build-all-threads)
11117         (gnus-build-old-threads)))
11118     ;; Let the Gnus agent mark articles as read.
11119     (when gnus-agent
11120       (gnus-agent-get-undownloaded-list))
11121     ;; Remove list identifiers from subject
11122     (when gnus-list-identifiers
11123       (gnus-summary-remove-list-identifiers))
11124     ;; First and last article in this newsgroup.
11125     (when gnus-newsgroup-headers
11126       (setq gnus-newsgroup-begin
11127             (mail-header-number (car gnus-newsgroup-headers))
11128             gnus-newsgroup-end
11129             (mail-header-number
11130              (gnus-last-element gnus-newsgroup-headers))))
11131     (when gnus-use-scoring
11132       (gnus-possibly-score-headers))))
11133
11134 (defun gnus-summary-insert-old-articles (&optional all)
11135   "Insert all old articles in this group.
11136 If ALL is non-nil, already read articles become readable.
11137 If ALL is a number, fetch this number of articles."
11138   (interactive "P")
11139   (prog1
11140       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11141             older len)
11142         (setq older
11143               (gnus-sorted-difference
11144                (gnus-uncompress-range (list gnus-newsgroup-active))
11145                old))
11146         (setq len (length older))
11147         (cond
11148          ((null older) nil)
11149          ((numberp all)
11150           (if (< all len)
11151               (setq older (last older all))))
11152          (all nil)
11153          (t
11154           (if (and (numberp gnus-large-newsgroup)
11155                    (> len gnus-large-newsgroup))
11156               (let* ((cursor-in-echo-area nil)
11157                      (initial (gnus-parameter-large-newsgroup-initial
11158                                gnus-newsgroup-name))
11159                      (input
11160                       (read-string
11161                        (format
11162                         "How many articles from %s (%s %d): "
11163                         (gnus-limit-string
11164                          (gnus-group-decoded-name gnus-newsgroup-name) 35)
11165                         (if initial "max" "default")
11166                         len)
11167                        (if initial
11168                            (cons (number-to-string initial)
11169                                  0)))))
11170                 (unless (string-match "^[ \t]*$" input)
11171                   (setq all (string-to-number input))
11172                   (if (< all len)
11173                       (setq older (last older all))))))))
11174         (if (not older)
11175             (message "No old news.")
11176           (gnus-summary-insert-articles older)
11177           (gnus-summary-limit (gnus-sorted-nunion old older))))
11178     (gnus-summary-position-point)))
11179
11180 (defun gnus-summary-insert-new-articles ()
11181   "Insert all new articles in this group."
11182   (interactive)
11183   (prog1
11184       (let ((old (sort (mapcar 'car gnus-newsgroup-data) '<))
11185             (old-active gnus-newsgroup-active)
11186             (nnmail-fetched-sources (list t))
11187             i new)
11188         (setq gnus-newsgroup-active
11189               (gnus-activate-group gnus-newsgroup-name 'scan))
11190         (setq i (cdr gnus-newsgroup-active))
11191         (while (> i (cdr old-active))
11192           (push i new)
11193           (decf i))
11194         (if (not new)
11195             (message "No gnus is bad news.")
11196           (gnus-summary-insert-articles new)
11197           (setq gnus-newsgroup-unreads
11198                 (gnus-sorted-nunion gnus-newsgroup-unreads new))
11199           (gnus-summary-limit (gnus-sorted-nunion old new))))
11200     (gnus-summary-position-point)))
11201
11202 (gnus-summary-make-all-marking-commands)
11203
11204 (gnus-ems-redefine)
11205
11206 (provide 'gnus-sum)
11207
11208 (run-hooks 'gnus-sum-load-hook)
11209
11210 ;;; gnus-sum.el ends here