4ab0498e0f488fe122d4319928059ad937ca2941
[gnus] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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 ;; Recursive :-(.
40 ;; (require 'gnus-art)
41 (require 'nnoo)
42 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
43 (autoload 'gnus-cache-write-active "gnus-cache")
44 (autoload 'gnus-mailing-list-insinuate "gnus-ml" nil t)
45 (autoload 'turn-on-gnus-mailing-list-mode "gnus-ml" nil t)
46 (autoload 'mm-uu-dissect "mm-uu")
47
48 (defcustom gnus-kill-summary-on-exit t
49   "*If non-nil, kill the summary buffer when you exit from it.
50 If nil, the summary will become a \"*Dead Summary*\" buffer, and
51 it will be killed sometime later."
52   :group 'gnus-summary-exit
53   :type 'boolean)
54
55 (defcustom gnus-fetch-old-headers nil
56   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
57 If an unread article in the group refers to an older, already read (or
58 just marked as read) article, the old article will not normally be
59 displayed in the Summary buffer.  If this variable is non-nil, Gnus
60 will attempt to grab the headers to the old articles, and thereby
61 build complete threads.  If it has the value `some', only enough
62 headers to connect otherwise loose threads will be displayed.  This
63 variable can also be a number.  In that case, no more than that number
64 of old headers will be fetched.  If it has the value `invisible', all
65 old headers will be fetched, but none will be displayed.
66
67 The server has to support NOV for any of this to work."
68   :group 'gnus-thread
69   :type '(choice (const :tag "off" nil)
70                  (const some)
71                  number
72                  (sexp :menu-tag "other" t)))
73
74 (defcustom gnus-refer-thread-limit 200
75   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
76 If t, fetch all the available old headers."
77   :group 'gnus-thread
78   :type '(choice number
79                  (sexp :menu-tag "other" t)))
80
81 (defcustom gnus-summary-make-false-root 'adopt
82   "*nil means that Gnus won't gather loose threads.
83 If the root of a thread has expired or been read in a previous
84 session, the information necessary to build a complete thread has been
85 lost.  Instead of having many small sub-threads from this original thread
86 scattered all over the summary buffer, Gnus can gather them.
87
88 If non-nil, Gnus will try to gather all loose sub-threads from an
89 original thread into one large thread.
90
91 If this variable is non-nil, it should be one of `none', `adopt',
92 `dummy' or `empty'.
93
94 If this variable is `none', Gnus will not make a false root, but just
95 present the sub-threads after another.
96 If this variable is `dummy', Gnus will create a dummy root that will
97 have all the sub-threads as children.
98 If this variable is `adopt', Gnus will make one of the \"children\"
99 the parent and mark all the step-children as such.
100 If this variable is `empty', the \"children\" are printed with empty
101 subject fields.  (Or rather, they will be printed with a string
102 given by the `gnus-summary-same-subject' variable.)"
103   :group 'gnus-thread
104   :type '(choice (const :tag "off" nil)
105                  (const none)
106                  (const dummy)
107                  (const adopt)
108                  (const empty)))
109
110 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
111   "*A regexp to match subjects to be excluded from loose thread gathering.
112 As loose thread gathering is done on subjects only, that means that
113 there can be many false gatherings performed.  By rooting out certain
114 common subjects, gathering might become saner."
115   :group 'gnus-thread
116   :type 'regexp)
117
118 (defcustom gnus-summary-gather-subject-limit nil
119   "*Maximum length of subject comparisons when gathering loose threads.
120 Use nil to compare full subjects.  Setting this variable to a low
121 number will help gather threads that have been corrupted by
122 newsreaders chopping off subject lines, but it might also mean that
123 unrelated articles that have subject that happen to begin with the
124 same few characters will be incorrectly gathered.
125
126 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
127 comparing subjects."
128   :group 'gnus-thread
129   :type '(choice (const :tag "off" nil)
130                  (const fuzzy)
131                  (sexp :menu-tag "on" t)))
132
133 (defcustom gnus-simplify-subject-functions nil
134   "List of functions taking a string argument that simplify subjects.
135 The functions are applied recursively.
136
137 Useful functions to put in this list include: `gnus-simplify-subject-re',
138 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
139   :group 'gnus-thread
140   :type '(repeat function))
141
142 (defcustom gnus-simplify-ignored-prefixes nil
143   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
144   :group 'gnus-thread
145   :type '(choice (const :tag "off" nil)
146                  regexp))
147
148 (defcustom gnus-build-sparse-threads nil
149   "*If non-nil, fill in the gaps in threads.
150 If `some', only fill in the gaps that are needed to tie loose threads
151 together.  If `more', fill in all leaf nodes that Gnus can find.  If
152 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
153   :group 'gnus-thread
154   :type '(choice (const :tag "off" nil)
155                  (const some)
156                  (const more)
157                  (sexp :menu-tag "all" t)))
158
159 (defcustom gnus-summary-thread-gathering-function
160   'gnus-gather-threads-by-subject
161   "*Function used for gathering loose threads.
162 There are two pre-defined functions: `gnus-gather-threads-by-subject',
163 which only takes Subjects into consideration; and
164 `gnus-gather-threads-by-references', which compared the References
165 headers of the articles to find matches."
166   :group 'gnus-thread
167   :type '(radio (function-item gnus-gather-threads-by-subject)
168                 (function-item gnus-gather-threads-by-references)
169                 (function :tag "other")))
170
171 (defcustom gnus-summary-same-subject ""
172   "*String indicating that the current article has the same subject as the previous.
173 This variable will only be used if the value of
174 `gnus-summary-make-false-root' is `empty'."
175   :group 'gnus-summary-format
176   :type 'string)
177
178 (defcustom gnus-summary-goto-unread t
179   "*If t, many commands will go to the next unread article.
180 This applies to marking commands as well as other commands that
181 \"naturally\" select the next article, like, for instance, `SPC' at
182 the end of an article.
183
184 If nil, the marking commands do NOT go to the next unread article
185 (they go to the next article instead).  If `never', commands that
186 usually go to the next unread article, will go to the next article,
187 whether it is read or not."
188   :group 'gnus-summary-marks
189   :link '(custom-manual "(gnus)Setting Marks")
190   :type '(choice (const :tag "off" nil)
191                  (const never)
192                  (sexp :menu-tag "on" t)))
193
194 (defcustom gnus-summary-default-score 0
195   "*Default article score level.
196 All scores generated by the score files will be added to this score.
197 If this variable is nil, scoring will be disabled."
198   :group 'gnus-score-default
199   :type '(choice (const :tag "disable")
200                  integer))
201
202 (defcustom gnus-summary-default-high-score 0
203   "*Default threshold for a high scored article.
204 An article will be highlighted as high scored if its score is greater
205 than this score."
206   :group 'gnus-score-default
207   :type 'integer)
208
209 (defcustom gnus-summary-default-low-score 0
210   "*Default threshold for a low scored article.
211 An article will be highlighted as low scored if its score is smaller
212 than this score."
213   :group 'gnus-score-default
214   :type 'integer)
215
216 (defcustom gnus-summary-zcore-fuzz 0
217   "*Fuzziness factor for the zcore in the summary buffer.
218 Articles with scores closer than this to `gnus-summary-default-score'
219 will not be marked."
220   :group 'gnus-summary-format
221   :type 'integer)
222
223 (defcustom gnus-simplify-subject-fuzzy-regexp nil
224   "*Strings to be removed when doing fuzzy matches.
225 This can either be a regular expression or list of regular expressions
226 that will be removed from subject strings if fuzzy subject
227 simplification is selected."
228   :group 'gnus-thread
229   :type '(repeat regexp))
230
231 (defcustom gnus-show-threads t
232   "*If non-nil, display threads in summary mode."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-subtree nil
237   "*If non-nil, hide all threads initially.
238 If threads are hidden, you have to run the command
239 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
240 to expose hidden threads."
241   :group 'gnus-thread
242   :type 'boolean)
243
244 (defcustom gnus-thread-hide-killed t
245   "*If non-nil, hide killed threads automatically."
246   :group 'gnus-thread
247   :type 'boolean)
248
249 (defcustom gnus-thread-ignore-subject t
250   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
251 If nil, articles that have different subjects from their parents will
252 start separate threads."
253   :group 'gnus-thread
254   :type 'boolean)
255
256 (defcustom gnus-thread-operation-ignore-subject t
257   "*If non-nil, subjects will be ignored when doing thread commands.
258 This affects commands like `gnus-summary-kill-thread' and
259 `gnus-summary-lower-thread'.
260
261 If this variable is nil, articles in the same thread with different
262 subjects will not be included in the operation in question.  If this
263 variable is `fuzzy', only articles that have subjects that are fuzzily
264 equal will be included."
265   :group 'gnus-thread
266   :type '(choice (const :tag "off" nil)
267                  (const fuzzy)
268                  (sexp :tag "on" t)))
269
270 (defcustom gnus-thread-indent-level 4
271   "*Number that says how much each sub-thread should be indented."
272   :group 'gnus-thread
273   :type 'integer)
274
275 (defcustom gnus-auto-extend-newsgroup t
276   "*If non-nil, extend newsgroup forward and backward when requested."
277   :group 'gnus-summary-choose
278   :type 'boolean)
279
280 (defcustom gnus-auto-select-first t
281   "*If nil, don't select the first unread article when entering a group.
282 If this variable is `best', select the highest-scored unread article
283 in the group.  If t, select the first unread article.
284
285 This variable can also be a function to place point on a likely
286 subject line.  Useful values include `gnus-summary-first-unread-subject',
287 `gnus-summary-first-unread-article' and
288 `gnus-summary-best-unread-article'.
289
290 If you want to prevent automatic selection of the first unread article
291 in some newsgroups, set the variable to nil in
292 `gnus-select-group-hook'."
293   :group 'gnus-group-select
294   :type '(choice (const :tag "none" nil)
295                  (const best)
296                  (sexp :menu-tag "first" t)
297                  (function-item gnus-summary-first-unread-subject)
298                  (function-item gnus-summary-first-unread-article)
299                  (function-item gnus-summary-best-unread-article)))
300
301 (defcustom gnus-auto-select-next t
302   "*If non-nil, offer to go to the next group from the end of the previous.
303 If the value is t and the next newsgroup is empty, Gnus will exit
304 summary mode and go back to group mode.  If the value is neither nil
305 nor t, Gnus will select the following unread newsgroup.  In
306 particular, if the value is the symbol `quietly', the next unread
307 newsgroup will be selected without any confirmation, and if it is
308 `almost-quietly', the next group will be selected without any
309 confirmation if you are located on the last article in the group.
310 Finally, if this variable is `slightly-quietly', the `Z n' command
311 will go to the next group without confirmation."
312   :group 'gnus-summary-maneuvering
313   :type '(choice (const :tag "off" nil)
314                  (const quietly)
315                  (const almost-quietly)
316                  (const slightly-quietly)
317                  (sexp :menu-tag "on" t)))
318
319 (defcustom gnus-auto-select-same nil
320   "*If non-nil, select the next article with the same subject.
321 If there are no more articles with the same subject, go to
322 the first unread article."
323   :group 'gnus-summary-maneuvering
324   :type 'boolean)
325
326 (defcustom gnus-summary-check-current nil
327   "*If non-nil, consider the current article when moving.
328 The \"unread\" movement commands will stay on the same line if the
329 current article is unread."
330   :group 'gnus-summary-maneuvering
331   :type 'boolean)
332
333 (defcustom gnus-auto-center-summary t
334   "*If non-nil, always center the current summary buffer.
335 In particular, if `vertical' do only vertical recentering.  If non-nil
336 and non-`vertical', do both horizontal and vertical recentering."
337   :group 'gnus-summary-maneuvering
338   :type '(choice (const :tag "none" nil)
339                  (const vertical)
340                  (integer :tag "height")
341                  (sexp :menu-tag "both" t)))
342
343 (defcustom gnus-show-all-headers nil
344   "*If non-nil, don't hide any headers."
345   :group 'gnus-article-hiding
346   :group 'gnus-article-headers
347   :type 'boolean)
348
349 (defcustom gnus-summary-ignore-duplicates nil
350   "*If non-nil, ignore articles with identical Message-ID headers."
351   :group 'gnus-summary
352   :type 'boolean)
353
354 (defcustom gnus-single-article-buffer t
355   "*If non-nil, display all articles in the same buffer.
356 If nil, each group will get its own article buffer."
357   :group 'gnus-article-various
358   :type 'boolean)
359
360 (defcustom gnus-break-pages t
361   "*If non-nil, do page breaking on articles.
362 The page delimiter is specified by the `gnus-page-delimiter'
363 variable."
364   :group 'gnus-article-various
365   :type 'boolean)
366
367 (defcustom gnus-move-split-methods nil
368   "*Variable used to suggest where articles are to be moved to.
369 It uses the same syntax as the `gnus-split-methods' variable.
370 However, whereas `gnus-split-methods' specifies file names as targets,
371 this variable specifies group names."
372   :group 'gnus-summary-mail
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-unread-mark ?           ;Whitespace
378   "*Mark used for unread articles."
379   :group 'gnus-summary-marks
380   :type 'character)
381
382 (defcustom gnus-ticked-mark ?!
383   "*Mark used for ticked articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-dormant-mark ??
388   "*Mark used for dormant articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-del-mark ?r
393   "*Mark used for del'd articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-read-mark ?R
398   "*Mark used for read articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-expirable-mark ?E
403   "*Mark used for expirable articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-killed-mark ?K
408   "*Mark used for killed articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-souped-mark ?F
413   "*Mark used for souped articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-kill-file-mark ?X
418   "*Mark used for articles killed by kill files."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-low-score-mark ?Y
423   "*Mark used for articles with a low score."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-catchup-mark ?C
428   "*Mark used for articles that are caught up."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-replied-mark ?A
433   "*Mark used for articles that have been replied to."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-forwarded-mark ?F
438   "*Mark used for articles that have been forwarded."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-recent-mark ?N
443   "*Mark used for articles that are recent."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-cached-mark ?*
448   "*Mark used for articles that are in the cache."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-saved-mark ?S
453   "*Mark used for articles that have been saved."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-unseen-mark ?.
458   "*Mark used for articles that haven't been seen."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-no-mark ?               ;Whitespace
463   "*Mark used for articles that have no other secondary mark."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-ancient-mark ?O
468   "*Mark used for ancient articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-sparse-mark ?Q
473   "*Mark used for sparsely reffed articles."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-canceled-mark ?G
478   "*Mark used for canceled articles."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-duplicate-mark ?M
483   "*Mark used for duplicate articles."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-undownloaded-mark ?@
488   "*Mark used for articles that weren't downloaded."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-downloadable-mark ?%
493   "*Mark used for articles that are to be downloaded."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-unsendable-mark ?=
498   "*Mark used for articles that won't be sent."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-score-over-mark ?+
503   "*Score mark used for articles with high scores."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-score-below-mark ?-
508   "*Score mark used for articles with low scores."
509   :group 'gnus-summary-marks
510   :type 'character)
511
512 (defcustom gnus-empty-thread-mark ?     ;Whitespace
513   "*There is no thread under the article."
514   :group 'gnus-summary-marks
515   :type 'character)
516
517 (defcustom gnus-not-empty-thread-mark ?=
518   "*There is a thread under the article."
519   :group 'gnus-summary-marks
520   :type 'character)
521
522 (defcustom gnus-view-pseudo-asynchronously nil
523   "*If non-nil, Gnus will view pseudo-articles asynchronously."
524   :group 'gnus-extract-view
525   :type 'boolean)
526
527 (defcustom gnus-auto-expirable-marks
528   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
529         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
530         gnus-souped-mark gnus-duplicate-mark)
531   "*The list of marks converted into expiration if a group is auto-expirable."
532   :version "21.1"
533   :group 'gnus-summary
534   :type '(repeat character))
535
536 (defcustom gnus-inhibit-user-auto-expire t
537   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
538   :version "21.1"
539   :group 'gnus-summary
540   :type 'boolean)
541
542 (defcustom gnus-view-pseudos nil
543   "*If `automatic', pseudo-articles will be viewed automatically.
544 If `not-confirm', pseudos will be viewed automatically, and the user
545 will not be asked to confirm the command."
546   :group 'gnus-extract-view
547   :type '(choice (const :tag "off" nil)
548                  (const automatic)
549                  (const not-confirm)))
550
551 (defcustom gnus-view-pseudos-separately t
552   "*If non-nil, one pseudo-article will be created for each file to be viewed.
553 If nil, all files that use the same viewing command will be given as a
554 list of parameters to that command."
555   :group 'gnus-extract-view
556   :type 'boolean)
557
558 (defcustom gnus-insert-pseudo-articles t
559   "*If non-nil, insert pseudo-articles when decoding articles."
560   :group 'gnus-extract-view
561   :type 'boolean)
562
563 (defcustom gnus-summary-dummy-line-format
564   "  %(:                          :%) %S\n"
565   "*The format specification for the dummy roots in the summary buffer.
566 It works along the same lines as a normal formatting string,
567 with some simple extensions.
568
569 %S  The subject"
570   :group 'gnus-threading
571   :type 'string)
572
573 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
574   "*The format specification for the summary mode line.
575 It works along the same lines as a normal formatting string,
576 with some simple extensions:
577
578 %G  Group name
579 %p  Unprefixed group name
580 %A  Current article number
581 %z  Current article score
582 %V  Gnus version
583 %U  Number of unread articles in the group
584 %e  Number of unselected articles in the group
585 %Z  A string with unread/unselected article counts
586 %g  Shortish group name
587 %S  Subject of the current article
588 %u  User-defined spec
589 %s  Current score file name
590 %d  Number of dormant articles
591 %r  Number of articles that have been marked as read in this session
592 %E  Number of articles expunged by the score files"
593   :group 'gnus-summary-format
594   :type 'string)
595
596 (defcustom gnus-list-identifiers nil
597   "Regexp that matches list identifiers to be removed from subject.
598 This can also be a list of regexps."
599   :version "21.1"
600   :group 'gnus-summary-format
601   :group 'gnus-article-hiding
602   :type '(choice (const :tag "none" nil)
603                  (regexp :value ".*")
604                  (repeat :value (".*") regexp)))
605
606 (defcustom gnus-summary-mark-below 0
607   "*Mark all articles with a score below this variable as read.
608 This variable is local to each summary buffer and usually set by the
609 score file."
610   :group 'gnus-score-default
611   :type 'integer)
612
613 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
614   "*List of functions used for sorting articles in the summary buffer.
615
616 Each function takes two articles and returns non-nil if the first
617 article should be sorted before the other.  If you use more than one
618 function, the primary sort function should be the last.  You should
619 probably always include `gnus-article-sort-by-number' in the list of
620 sorting functions -- preferably first.  Also note that sorting by date
621 is often much slower than sorting by number, and the sorting order is
622 very similar.  (Sorting by date means sorting by the time the message
623 was sent, sorting by number means sorting by arrival time.)
624
625 Ready-made functions include `gnus-article-sort-by-number',
626 `gnus-article-sort-by-author', `gnus-article-sort-by-subject',
627 `gnus-article-sort-by-date' and `gnus-article-sort-by-score'.
628
629 When threading is turned on, the variable `gnus-thread-sort-functions'
630 controls how articles are sorted."
631   :group 'gnus-summary-sort
632   :type '(repeat (choice (function-item gnus-article-sort-by-number)
633                          (function-item gnus-article-sort-by-author)
634                          (function-item gnus-article-sort-by-subject)
635                          (function-item gnus-article-sort-by-date)
636                          (function-item gnus-article-sort-by-score)
637                          (function :tag "other"))))
638
639 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
640   "*List of functions used for sorting threads in the summary buffer.
641 By default, threads are sorted by article number.
642
643 Each function takes two threads and returns non-nil if the first
644 thread should be sorted before the other.  If you use more than one
645 function, the primary sort function should be the last.  You should
646 probably always include `gnus-thread-sort-by-number' in the list of
647 sorting functions -- preferably first.  Also note that sorting by date
648 is often much slower than sorting by number, and the sorting order is
649 very similar.  (Sorting by date means sorting by the time the message
650 was sent, sorting by number means sorting by arrival time.)
651
652 Ready-made functions include `gnus-thread-sort-by-number',
653 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
654 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
655 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').
656
657 When threading is turned off, the variable
658 `gnus-article-sort-functions' controls how articles are sorted."
659   :group 'gnus-summary-sort
660   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
661                          (function-item gnus-thread-sort-by-author)
662                          (function-item gnus-thread-sort-by-subject)
663                          (function-item gnus-thread-sort-by-date)
664                          (function-item gnus-thread-sort-by-score)
665                          (function-item gnus-thread-sort-by-total-score)
666                          (function :tag "other"))))
667
668 (defcustom gnus-thread-score-function '+
669   "*Function used for calculating the total score of a thread.
670
671 The function is called with the scores of the article and each
672 subthread and should then return the score of the thread.
673
674 Some functions you can use are `+', `max', or `min'."
675   :group 'gnus-summary-sort
676   :type 'function)
677
678 (defcustom gnus-summary-expunge-below nil
679   "All articles that have a score less than this variable will be expunged.
680 This variable is local to the summary buffers."
681   :group 'gnus-score-default
682   :type '(choice (const :tag "off" nil)
683                  integer))
684
685 (defcustom gnus-thread-expunge-below nil
686   "All threads that have a total score less than this variable will be expunged.
687 See `gnus-thread-score-function' for en explanation of what a
688 \"thread score\" is.
689
690 This variable is local to the summary buffers."
691   :group 'gnus-threading
692   :group 'gnus-score-default
693   :type '(choice (const :tag "off" nil)
694                  integer))
695
696 (defcustom gnus-summary-mode-hook nil
697   "*A hook for Gnus summary mode.
698 This hook is run before any variables are set in the summary buffer."
699   :options '(turn-on-gnus-mailing-list-mode gnus-pick-mode)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook nil
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-display-arrow
811   (and (fboundp 'display-graphic-p)
812        (display-graphic-p))
813   "*If non-nil, display an arrow highlighting the current article."
814   :version "21.1"
815   :group 'gnus-summary
816   :type 'boolean)
817
818 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
819   "Face used for highlighting the current article in the summary buffer."
820   :group 'gnus-summary-visual
821   :type 'face)
822
823 (defcustom gnus-summary-highlight
824   '(((= mark gnus-canceled-mark)
825      . gnus-summary-cancelled-face)
826     ((and (> score default-high)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-high-ticked-face)
830     ((and (< score default-low)
831           (or (= mark gnus-dormant-mark)
832               (= mark gnus-ticked-mark)))
833      . gnus-summary-low-ticked-face)
834     ((or (= mark gnus-dormant-mark)
835          (= mark gnus-ticked-mark))
836      . gnus-summary-normal-ticked-face)
837     ((and (> score default-high) (= mark gnus-ancient-mark))
838      . gnus-summary-high-ancient-face)
839     ((and (< score default-low) (= mark gnus-ancient-mark))
840      . gnus-summary-low-ancient-face)
841     ((= mark gnus-ancient-mark)
842      . gnus-summary-normal-ancient-face)
843     ((and (> score default-high) (= mark gnus-unread-mark))
844      . gnus-summary-high-unread-face)
845     ((and (< score default-low) (= mark gnus-unread-mark))
846      . gnus-summary-low-unread-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
850                                                   gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
853                                                  gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default-high)
861      . gnus-summary-high-read-face)
862     ((< score default-low)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:        The article's score
876 default:      The default article score.
877 default-high: The default score for high scored articles.
878 default-low:  The default score for low scored articles.
879 below:        The score below which articles are automatically marked as read.
880 mark:         The articles mark."
881   :group 'gnus-summary-visual
882   :type '(repeat (cons (sexp :tag "Form" nil)
883                        face)))
884
885 (defcustom gnus-alter-header-function nil
886   "Function called to allow alteration of article header structures.
887 The function is called with one parameter, the article header vector,
888 which it may alter in any way.")
889
890 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
891   "Variable that says which function should be used to decode a string with encoded words.")
892
893 (defcustom gnus-extra-headers nil
894   "*Extra headers to parse."
895   :version "21.1"
896   :group 'gnus-summary
897   :type '(repeat symbol))
898
899 (defcustom gnus-ignored-from-addresses
900   (and user-mail-address (regexp-quote user-mail-address))
901   "*Regexp of From headers that may be suppressed in favor of To headers."
902   :version "21.1"
903   :group 'gnus-summary
904   :type 'regexp)
905
906 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
907   "List of charsets that should be ignored.
908 When these charsets are used in the \"charset\" parameter, the
909 default charset will be used instead."
910   :version "21.1"
911   :type '(repeat symbol)
912   :group 'gnus-charset)
913
914 (gnus-define-group-parameter
915  ignored-charsets
916  :type list
917  :function-document
918  "Return the ignored charsets of GROUP."
919  :variable gnus-group-ignored-charsets-alist
920  :variable-default
921  '(("alt\\.chinese\\.text" iso-8859-1))
922  :variable-document
923  "Alist of regexps (to match group names) and charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926  :variable-group gnus-charset
927  :variable-type '(repeat (cons (regexp :tag "Group")
928                                (repeat symbol)))
929  :parameter-type '(choice :tag "Ignored charsets"
930                           :value nil
931                           (repeat (symbol)))
932  :parameter-document       "\
933 List of charsets that should be ignored.
934
935 When these charsets are used in the \"charset\" parameter, the
936 default charset will be used instead.")
937
938 (defcustom gnus-group-highlight-words-alist nil
939   "Alist of group regexps and highlight regexps.
940 This variable uses the same syntax as `gnus-emphasis-alist'."
941   :version "21.1"
942   :type '(repeat (cons (regexp :tag "Group")
943                        (repeat (list (regexp :tag "Highlight regexp")
944                                      (number :tag "Group for entire word" 0)
945                                      (number :tag "Group for displayed part" 0)
946                                      (symbol :tag "Face"
947                                              gnus-emphasis-highlight-words)))))
948   :group 'gnus-summary-visual)
949
950 (defcustom gnus-summary-show-article-charset-alist
951   nil
952   "Alist of number and charset.
953 The article will be shown with the charset corresponding to the
954 numbered argument.
955 For example: ((1 . cn-gb-2312) (2 . big5))."
956   :version "21.1"
957   :type '(repeat (cons (number :tag "Argument" 1)
958                        (symbol :tag "Charset")))
959   :group 'gnus-charset)
960
961 (defcustom gnus-preserve-marks t
962   "Whether marks are preserved when moving, copying and respooling messages."
963   :version "21.1"
964   :type 'boolean
965   :group 'gnus-summary-marks)
966
967 (defcustom gnus-alter-articles-to-read-function nil
968   "Function to be called to alter the list of articles to be selected."
969   :type '(choice (const nil) function)
970   :group 'gnus-summary)
971
972 (defcustom gnus-orphan-score nil
973   "*All orphans get this score added.  Set in the score file."
974   :group 'gnus-score-default
975   :type '(choice (const nil)
976                  integer))
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 (defcustom gnus-summary-save-parts-default-mime "image/.*"
987   "*A regexp to match MIME parts when saving multiple parts of a message
988 with gnus-summary-save-parts (X m). This regexp will be used by default
989 when prompting the user for which type of files to save."
990   :group 'gnus-summary
991   :type 'regexp)
992
993 (defcustom gnus-read-all-available-headers nil
994   "Whether Gnus should parse all headers made available to it.
995 This is mostly relevant for slow backends where the user may
996 wish to widen the summary buffer to include all headers
997 that were fetched.  Say, for nnultimate groups."
998   :group 'gnus-summary
999   :type '(choice boolean regexp))
1000
1001 ;;; Internal variables
1002
1003 (defvar gnus-summary-display-cache nil)
1004 (defvar gnus-article-mime-handles nil)
1005 (defvar gnus-article-decoded-p nil)
1006 (defvar gnus-article-charset nil)
1007 (defvar gnus-article-ignored-charsets nil)
1008 (defvar gnus-scores-exclude-files nil)
1009 (defvar gnus-page-broken nil)
1010 (defvar gnus-inhibit-mime-unbuttonizing nil)
1011
1012 (defvar gnus-original-article nil)
1013 (defvar gnus-article-internal-prepare-hook nil)
1014 (defvar gnus-newsgroup-process-stack nil)
1015
1016 (defvar gnus-thread-indent-array nil)
1017 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1018 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1019   "Function called to sort the articles within a thread after it has been gathered together.")
1020
1021 (defvar gnus-summary-save-parts-type-history nil)
1022 (defvar gnus-summary-save-parts-last-directory nil)
1023
1024 (defvar gnus-summary-save-parts-type-history nil)
1025 (defvar gnus-summary-save-parts-last-directory nil)
1026
1027 ;; Avoid highlighting in kill files.
1028 (defvar gnus-summary-inhibit-highlight nil)
1029 (defvar gnus-newsgroup-selected-overlay nil)
1030 (defvar gnus-inhibit-limiting nil)
1031 (defvar gnus-newsgroup-adaptive-score-file nil)
1032 (defvar gnus-current-score-file nil)
1033 (defvar gnus-current-move-group nil)
1034 (defvar gnus-current-copy-group nil)
1035 (defvar gnus-current-crosspost-group nil)
1036 (defvar gnus-newsgroup-display nil)
1037
1038 (defvar gnus-newsgroup-dependencies nil)
1039 (defvar gnus-newsgroup-adaptive nil)
1040 (defvar gnus-summary-display-article-function nil)
1041 (defvar gnus-summary-highlight-line-function nil
1042   "Function called after highlighting a summary line.")
1043
1044 (defvar gnus-summary-line-format-alist
1045   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1046     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1047     (?s gnus-tmp-subject-or-nil ?s)
1048     (?n gnus-tmp-name ?s)
1049     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1050         ?s)
1051     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1052             gnus-tmp-from) ?s)
1053     (?F gnus-tmp-from ?s)
1054     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1055     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1056     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1057     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1058     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1059     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1060     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1061     (?L gnus-tmp-lines ?s)
1062     (?I gnus-tmp-indentation ?s)
1063     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1064     (?R gnus-tmp-replied ?c)
1065     (?\[ gnus-tmp-opening-bracket ?c)
1066     (?\] gnus-tmp-closing-bracket ?c)
1067     (?\> (make-string gnus-tmp-level ? ) ?s)
1068     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1069     (?i gnus-tmp-score ?d)
1070     (?z gnus-tmp-score-char ?c)
1071     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1072     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1073     (?U gnus-tmp-unread ?c)
1074     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1075     (?t (gnus-summary-number-of-articles-in-thread
1076          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1077         ?d)
1078     (?e (gnus-summary-number-of-articles-in-thread
1079          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1080         ?c)
1081     (?u gnus-tmp-user-defined ?s)
1082     (?P (gnus-pick-line-number) ?d)
1083     (?B gnus-tmp-thread-tree-header-string ?s))
1084   "An alist of format specifications that can appear in summary lines.
1085 These are paired with what variables they correspond with, along with
1086 the type of the variable (string, integer, character, etc).")
1087
1088 (defvar gnus-summary-dummy-line-format-alist
1089   `((?S gnus-tmp-subject ?s)
1090     (?N gnus-tmp-number ?d)
1091     (?u gnus-tmp-user-defined ?s)))
1092
1093 (defvar gnus-summary-mode-line-format-alist
1094   `((?G gnus-tmp-group-name ?s)
1095     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1096     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1097     (?A gnus-tmp-article-number ?d)
1098     (?Z gnus-tmp-unread-and-unselected ?s)
1099     (?V gnus-version ?s)
1100     (?U gnus-tmp-unread-and-unticked ?d)
1101     (?S gnus-tmp-subject ?s)
1102     (?e gnus-tmp-unselected ?d)
1103     (?u gnus-tmp-user-defined ?s)
1104     (?d (length gnus-newsgroup-dormant) ?d)
1105     (?t (length gnus-newsgroup-marked) ?d)
1106     (?r (length gnus-newsgroup-reads) ?d)
1107     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1108     (?E gnus-newsgroup-expunged-tally ?d)
1109     (?s (gnus-current-score-file-nondirectory) ?s)))
1110
1111 (defvar gnus-last-search-regexp nil
1112   "Default regexp for article search command.")
1113
1114 (defvar gnus-last-shell-command nil
1115   "Default shell command on article.")
1116
1117 (defvar gnus-newsgroup-begin nil)
1118 (defvar gnus-newsgroup-end nil)
1119 (defvar gnus-newsgroup-last-rmail nil)
1120 (defvar gnus-newsgroup-last-mail nil)
1121 (defvar gnus-newsgroup-last-folder nil)
1122 (defvar gnus-newsgroup-last-file nil)
1123 (defvar gnus-newsgroup-auto-expire nil)
1124 (defvar gnus-newsgroup-active nil)
1125
1126 (defvar gnus-newsgroup-data nil)
1127 (defvar gnus-newsgroup-data-reverse nil)
1128 (defvar gnus-newsgroup-limit nil)
1129 (defvar gnus-newsgroup-limits nil)
1130
1131 (defvar gnus-newsgroup-unreads nil
1132   "List of unread articles in the current newsgroup.")
1133
1134 (defvar gnus-newsgroup-unselected nil
1135   "List of unselected unread articles in the current newsgroup.")
1136
1137 (defvar gnus-newsgroup-reads nil
1138   "Alist of read articles and article marks in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-expunged-tally nil)
1141
1142 (defvar gnus-newsgroup-marked nil
1143   "List of ticked articles in the current newsgroup (a subset of unread art).")
1144
1145 (defvar gnus-newsgroup-killed nil
1146   "List of ranges of articles that have been through the scoring process.")
1147
1148 (defvar gnus-newsgroup-cached nil
1149   "List of articles that come from the article cache.")
1150
1151 (defvar gnus-newsgroup-saved nil
1152   "List of articles that have been saved.")
1153
1154 (defvar gnus-newsgroup-kill-headers nil)
1155
1156 (defvar gnus-newsgroup-replied nil
1157   "List of articles that have been replied to in the current newsgroup.")
1158
1159 (defvar gnus-newsgroup-forwarded nil
1160   "List of articles that have been forwarded in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-recent nil
1163   "List of articles that have are recent in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-expirable nil
1166   "List of articles in the current newsgroup that can be expired.")
1167
1168 (defvar gnus-newsgroup-processable nil
1169   "List of articles in the current newsgroup that can be processed.")
1170
1171 (defvar gnus-newsgroup-downloadable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-undownloaded nil
1175   "List of articles in the current newsgroup that haven't been downloaded..")
1176
1177 (defvar gnus-newsgroup-unsendable nil
1178   "List of articles in the current newsgroup that won't be sent.")
1179
1180 (defvar gnus-newsgroup-bookmarks nil
1181   "List of articles in the current newsgroup that have bookmarks.")
1182
1183 (defvar gnus-newsgroup-dormant nil
1184   "List of dormant articles in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-unseen nil
1187   "List of unseen articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-seen nil
1190   "Range of seen articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-articles nil
1193   "List of articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-scored nil
1196   "List of scored articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-headers nil
1199   "List of article headers in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-threads nil)
1202
1203 (defvar gnus-newsgroup-prepared nil
1204   "Whether the current group has been prepared properly.")
1205
1206 (defvar gnus-newsgroup-ancient nil
1207   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-sparse nil)
1210
1211 (defvar gnus-current-article nil)
1212 (defvar gnus-article-current nil)
1213 (defvar gnus-current-headers nil)
1214 (defvar gnus-have-all-headers nil)
1215 (defvar gnus-last-article nil)
1216 (defvar gnus-newsgroup-history nil)
1217 (defvar gnus-newsgroup-charset nil)
1218 (defvar gnus-newsgroup-ephemeral-charset nil)
1219 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1220
1221 (defvar gnus-article-before-search nil)
1222
1223 (defconst gnus-summary-local-variables
1224   '(gnus-newsgroup-name
1225     gnus-newsgroup-begin gnus-newsgroup-end
1226     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1227     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1228     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1229     gnus-newsgroup-unselected gnus-newsgroup-marked
1230     gnus-newsgroup-reads gnus-newsgroup-saved
1231     gnus-newsgroup-replied gnus-newsgroup-forwarded
1232     gnus-newsgroup-recent
1233     gnus-newsgroup-expirable
1234     gnus-newsgroup-processable gnus-newsgroup-killed
1235     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1236     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1237     gnus-newsgroup-seen gnus-newsgroup-articles
1238     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1239     gnus-newsgroup-headers gnus-newsgroup-threads
1240     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1241     gnus-current-article gnus-current-headers gnus-have-all-headers
1242     gnus-last-article gnus-article-internal-prepare-hook
1243     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1244     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1245     gnus-thread-expunge-below
1246     gnus-score-alist gnus-current-score-file
1247     (gnus-summary-expunge-below . global)
1248     (gnus-summary-mark-below . global)
1249     (gnus-orphan-score . global)
1250     gnus-newsgroup-active gnus-scores-exclude-files
1251     gnus-newsgroup-history gnus-newsgroup-ancient
1252     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1253     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1254     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1255     (gnus-newsgroup-expunged-tally . 0)
1256     gnus-cache-removable-articles gnus-newsgroup-cached
1257     gnus-newsgroup-data gnus-newsgroup-data-reverse
1258     gnus-newsgroup-limit gnus-newsgroup-limits
1259     gnus-newsgroup-charset gnus-newsgroup-display)
1260   "Variables that are buffer-local to the summary buffers.")
1261
1262 (defvar gnus-newsgroup-variables nil
1263   "Variables that have separate values in the newsgroups.")
1264
1265 ;; Byte-compiler warning.
1266 (eval-when-compile (defvar gnus-article-mode-map))
1267
1268 ;; MIME stuff.
1269
1270 (defvar gnus-decode-encoded-word-methods
1271   '(mail-decode-encoded-word-string)
1272   "List of methods used to decode encoded words.
1273
1274 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1275 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1276 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1277 whose names match REGEXP.
1278
1279 For example:
1280 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1281  mail-decode-encoded-word-string
1282  (\"chinese\" . rfc1843-decode-string))")
1283
1284 (defvar gnus-decode-encoded-word-methods-cache nil)
1285
1286 (defun gnus-multi-decode-encoded-word-string (string)
1287   "Apply the functions from `gnus-encoded-word-methods' that match."
1288   (unless (and gnus-decode-encoded-word-methods-cache
1289                (eq gnus-newsgroup-name
1290                    (car gnus-decode-encoded-word-methods-cache)))
1291     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1292     (mapcar (lambda (x)
1293               (if (symbolp x)
1294                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1295                 (if (and gnus-newsgroup-name
1296                          (string-match (car x) gnus-newsgroup-name))
1297                     (nconc gnus-decode-encoded-word-methods-cache
1298                            (list (cdr x))))))
1299             gnus-decode-encoded-word-methods))
1300   (let ((xlist gnus-decode-encoded-word-methods-cache))
1301     (pop xlist)
1302     (while xlist
1303       (setq string (funcall (pop xlist) string))))
1304   string)
1305
1306 ;; Subject simplification.
1307
1308 (defun gnus-simplify-whitespace (str)
1309   "Remove excessive whitespace from STR."
1310   (let ((mystr str))
1311     ;; Multiple spaces.
1312     (while (string-match "[ \t][ \t]+" mystr)
1313       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1314                           " "
1315                           (substring mystr (match-end 0)))))
1316     ;; Leading spaces.
1317     (when (string-match "^[ \t]+" mystr)
1318       (setq mystr (substring mystr (match-end 0))))
1319     ;; Trailing spaces.
1320     (when (string-match "[ \t]+$" mystr)
1321       (setq mystr (substring mystr 0 (match-beginning 0))))
1322     mystr))
1323
1324 (defsubst gnus-simplify-subject-re (subject)
1325   "Remove \"Re:\" from subject lines."
1326   (if (string-match message-subject-re-regexp subject)
1327       (substring subject (match-end 0))
1328     subject))
1329
1330 (defun gnus-simplify-subject (subject &optional re-only)
1331   "Remove `Re:' and words in parentheses.
1332 If RE-ONLY is non-nil, strip leading `Re:'s only."
1333   (let ((case-fold-search t))           ;Ignore case.
1334     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1335     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1336       (setq subject (substring subject (match-end 0))))
1337     ;; Remove uninteresting prefixes.
1338     (when (and (not re-only)
1339                gnus-simplify-ignored-prefixes
1340                (string-match gnus-simplify-ignored-prefixes subject))
1341       (setq subject (substring subject (match-end 0))))
1342     ;; Remove words in parentheses from end.
1343     (unless re-only
1344       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1345         (setq subject (substring subject 0 (match-beginning 0)))))
1346     ;; Return subject string.
1347     subject))
1348
1349 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1350 ;; all whitespace.
1351 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1352   (goto-char (point-min))
1353   (while (re-search-forward regexp nil t)
1354     (replace-match (or newtext ""))))
1355
1356 (defun gnus-simplify-buffer-fuzzy ()
1357   "Simplify string in the buffer fuzzily.
1358 The string in the accessible portion of the current buffer is simplified.
1359 It is assumed to be a single-line subject.
1360 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1361 matter is removed.  Additional things can be deleted by setting
1362 `gnus-simplify-subject-fuzzy-regexp'."
1363   (let ((case-fold-search t)
1364         (modified-tick))
1365     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1366
1367     (while (not (eq modified-tick (buffer-modified-tick)))
1368       (setq modified-tick (buffer-modified-tick))
1369       (cond
1370        ((listp gnus-simplify-subject-fuzzy-regexp)
1371         (mapcar 'gnus-simplify-buffer-fuzzy-step
1372                 gnus-simplify-subject-fuzzy-regexp))
1373        (gnus-simplify-subject-fuzzy-regexp
1374         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1375       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1376       (gnus-simplify-buffer-fuzzy-step
1377        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1378       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1379
1380     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1381     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1382     (gnus-simplify-buffer-fuzzy-step " $")
1383     (gnus-simplify-buffer-fuzzy-step "^ +")))
1384
1385 (defun gnus-simplify-subject-fuzzy (subject)
1386   "Simplify a subject string fuzzily.
1387 See `gnus-simplify-buffer-fuzzy' for details."
1388   (save-excursion
1389     (gnus-set-work-buffer)
1390     (let ((case-fold-search t))
1391       ;; Remove uninteresting prefixes.
1392       (when (and gnus-simplify-ignored-prefixes
1393                  (string-match gnus-simplify-ignored-prefixes subject))
1394         (setq subject (substring subject (match-end 0))))
1395       (insert subject)
1396       (inline (gnus-simplify-buffer-fuzzy))
1397       (buffer-string))))
1398
1399 (defsubst gnus-simplify-subject-fully (subject)
1400   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1401   (cond
1402    (gnus-simplify-subject-functions
1403     (gnus-map-function gnus-simplify-subject-functions subject))
1404    ((null gnus-summary-gather-subject-limit)
1405     (gnus-simplify-subject-re subject))
1406    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1407     (gnus-simplify-subject-fuzzy subject))
1408    ((numberp gnus-summary-gather-subject-limit)
1409     (gnus-limit-string (gnus-simplify-subject-re subject)
1410                        gnus-summary-gather-subject-limit))
1411    (t
1412     subject)))
1413
1414 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1415   "Check whether two subjects are equal.
1416 If optional argument simple-first is t, first argument is already
1417 simplified."
1418   (cond
1419    ((null simple-first)
1420     (equal (gnus-simplify-subject-fully s1)
1421            (gnus-simplify-subject-fully s2)))
1422    (t
1423     (equal s1
1424            (gnus-simplify-subject-fully s2)))))
1425
1426 (defun gnus-summary-bubble-group ()
1427   "Increase the score of the current group.
1428 This is a handy function to add to `gnus-summary-exit-hook' to
1429 increase the score of each group you read."
1430   (gnus-group-add-score gnus-newsgroup-name))
1431
1432 \f
1433 ;;;
1434 ;;; Gnus summary mode
1435 ;;;
1436
1437 (put 'gnus-summary-mode 'mode-class 'special)
1438
1439 (defvar gnus-article-commands-menu)
1440
1441 (when t
1442   ;; Non-orthogonal keys
1443
1444   (gnus-define-keys gnus-summary-mode-map
1445     " " gnus-summary-next-page
1446     "\177" gnus-summary-prev-page
1447     [delete] gnus-summary-prev-page
1448     [backspace] gnus-summary-prev-page
1449     "\r" gnus-summary-scroll-up
1450     "\M-\r" gnus-summary-scroll-down
1451     "n" gnus-summary-next-unread-article
1452     "p" gnus-summary-prev-unread-article
1453     "N" gnus-summary-next-article
1454     "P" gnus-summary-prev-article
1455     "\M-\C-n" gnus-summary-next-same-subject
1456     "\M-\C-p" gnus-summary-prev-same-subject
1457     "\M-n" gnus-summary-next-unread-subject
1458     "\M-p" gnus-summary-prev-unread-subject
1459     "." gnus-summary-first-unread-article
1460     "," gnus-summary-best-unread-article
1461     "\M-s" gnus-summary-search-article-forward
1462     "\M-r" gnus-summary-search-article-backward
1463     "<" gnus-summary-beginning-of-article
1464     ">" gnus-summary-end-of-article
1465     "j" gnus-summary-goto-article
1466     "^" gnus-summary-refer-parent-article
1467     "\M-^" gnus-summary-refer-article
1468     "u" gnus-summary-tick-article-forward
1469     "!" gnus-summary-tick-article-forward
1470     "U" gnus-summary-tick-article-backward
1471     "d" gnus-summary-mark-as-read-forward
1472     "D" gnus-summary-mark-as-read-backward
1473     "E" gnus-summary-mark-as-expirable
1474     "\M-u" gnus-summary-clear-mark-forward
1475     "\M-U" gnus-summary-clear-mark-backward
1476     "k" gnus-summary-kill-same-subject-and-select
1477     "\C-k" gnus-summary-kill-same-subject
1478     "\M-\C-k" gnus-summary-kill-thread
1479     "\M-\C-l" gnus-summary-lower-thread
1480     "e" gnus-summary-edit-article
1481     "#" gnus-summary-mark-as-processable
1482     "\M-#" gnus-summary-unmark-as-processable
1483     "\M-\C-t" gnus-summary-toggle-threads
1484     "\M-\C-s" gnus-summary-show-thread
1485     "\M-\C-h" gnus-summary-hide-thread
1486     "\M-\C-f" gnus-summary-next-thread
1487     "\M-\C-b" gnus-summary-prev-thread
1488     [(meta down)] gnus-summary-next-thread
1489     [(meta up)] gnus-summary-prev-thread
1490     "\M-\C-u" gnus-summary-up-thread
1491     "\M-\C-d" gnus-summary-down-thread
1492     "&" gnus-summary-execute-command
1493     "c" gnus-summary-catchup-and-exit
1494     "\C-w" gnus-summary-mark-region-as-read
1495     "\C-t" gnus-summary-toggle-truncation
1496     "?" gnus-summary-mark-as-dormant
1497     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1498     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1499     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1500     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1501     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1502     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1503     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1504     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1505     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1506     "=" gnus-summary-expand-window
1507     "\C-x\C-s" gnus-summary-reselect-current-group
1508     "\M-g" gnus-summary-rescan-group
1509     "w" gnus-summary-stop-page-breaking
1510     "\C-c\C-r" gnus-summary-caesar-message
1511     "f" gnus-summary-followup
1512     "F" gnus-summary-followup-with-original
1513     "C" gnus-summary-cancel-article
1514     "r" gnus-summary-reply
1515     "R" gnus-summary-reply-with-original
1516     "\C-c\C-f" gnus-summary-mail-forward
1517     "o" gnus-summary-save-article
1518     "\C-o" gnus-summary-save-article-mail
1519     "|" gnus-summary-pipe-output
1520     "\M-k" gnus-summary-edit-local-kill
1521     "\M-K" gnus-summary-edit-global-kill
1522     ;; "V" gnus-version
1523     "\C-c\C-d" gnus-summary-describe-group
1524     "q" gnus-summary-exit
1525     "Q" gnus-summary-exit-no-update
1526     "\C-c\C-i" gnus-info-find-node
1527     gnus-mouse-2 gnus-mouse-pick-article
1528     "m" gnus-summary-mail-other-window
1529     "a" gnus-summary-post-news
1530     "x" gnus-summary-limit-to-unread
1531     "s" gnus-summary-isearch-article
1532     "t" gnus-summary-toggle-header
1533     "g" gnus-summary-show-article
1534     "l" gnus-summary-goto-last-article
1535     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1536     "\C-d" gnus-summary-enter-digest-group
1537     "\M-\C-d" gnus-summary-read-document
1538     "\M-\C-e" gnus-summary-edit-parameters
1539     "\M-\C-a" gnus-summary-customize-parameters
1540     "\C-c\C-b" gnus-bug
1541     "*" gnus-cache-enter-article
1542     "\M-*" gnus-cache-remove-article
1543     "\M-&" gnus-summary-universal-argument
1544     "\C-l" gnus-recenter
1545     "I" gnus-summary-increase-score
1546     "L" gnus-summary-lower-score
1547     "\M-i" gnus-symbolic-argument
1548     "h" gnus-summary-select-article-buffer
1549
1550     "b" gnus-article-view-part
1551     "\M-t" gnus-summary-toggle-display-buttonized
1552
1553     "V" gnus-summary-score-map
1554     "X" gnus-uu-extract-map
1555     "S" gnus-summary-send-map)
1556
1557   ;; Sort of orthogonal keymap
1558   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1559     "t" gnus-summary-tick-article-forward
1560     "!" gnus-summary-tick-article-forward
1561     "d" gnus-summary-mark-as-read-forward
1562     "r" gnus-summary-mark-as-read-forward
1563     "c" gnus-summary-clear-mark-forward
1564     " " gnus-summary-clear-mark-forward
1565     "e" gnus-summary-mark-as-expirable
1566     "x" gnus-summary-mark-as-expirable
1567     "?" gnus-summary-mark-as-dormant
1568     "b" gnus-summary-set-bookmark
1569     "B" gnus-summary-remove-bookmark
1570     "#" gnus-summary-mark-as-processable
1571     "\M-#" gnus-summary-unmark-as-processable
1572     "S" gnus-summary-limit-include-expunged
1573     "C" gnus-summary-catchup
1574     "H" gnus-summary-catchup-to-here
1575     "h" gnus-summary-catchup-from-here
1576     "\C-c" gnus-summary-catchup-all
1577     "k" gnus-summary-kill-same-subject-and-select
1578     "K" gnus-summary-kill-same-subject
1579     "P" gnus-uu-mark-map)
1580
1581   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1582     "c" gnus-summary-clear-above
1583     "u" gnus-summary-tick-above
1584     "m" gnus-summary-mark-above
1585     "k" gnus-summary-kill-below)
1586
1587   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1588     "/" gnus-summary-limit-to-subject
1589     "n" gnus-summary-limit-to-articles
1590     "w" gnus-summary-pop-limit
1591     "s" gnus-summary-limit-to-subject
1592     "a" gnus-summary-limit-to-author
1593     "u" gnus-summary-limit-to-unread
1594     "m" gnus-summary-limit-to-marks
1595     "M" gnus-summary-limit-exclude-marks
1596     "v" gnus-summary-limit-to-score
1597     "*" gnus-summary-limit-include-cached
1598     "D" gnus-summary-limit-include-dormant
1599     "T" gnus-summary-limit-include-thread
1600     "d" gnus-summary-limit-exclude-dormant
1601     "t" gnus-summary-limit-to-age
1602     "x" gnus-summary-limit-to-extra
1603     "p" gnus-summary-limit-to-display-predicate
1604     "E" gnus-summary-limit-include-expunged
1605     "c" gnus-summary-limit-exclude-childless-dormant
1606     "C" gnus-summary-limit-mark-excluded-as-read
1607     "o" gnus-summary-insert-old-articles
1608     "N" gnus-summary-insert-new-articles)
1609
1610   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1611     "n" gnus-summary-next-unread-article
1612     "p" gnus-summary-prev-unread-article
1613     "N" gnus-summary-next-article
1614     "P" gnus-summary-prev-article
1615     "\C-n" gnus-summary-next-same-subject
1616     "\C-p" gnus-summary-prev-same-subject
1617     "\M-n" gnus-summary-next-unread-subject
1618     "\M-p" gnus-summary-prev-unread-subject
1619     "f" gnus-summary-first-unread-article
1620     "b" gnus-summary-best-unread-article
1621     "j" gnus-summary-goto-article
1622     "g" gnus-summary-goto-subject
1623     "l" gnus-summary-goto-last-article
1624     "o" gnus-summary-pop-article)
1625
1626   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1627     "k" gnus-summary-kill-thread
1628     "l" gnus-summary-lower-thread
1629     "i" gnus-summary-raise-thread
1630     "T" gnus-summary-toggle-threads
1631     "t" gnus-summary-rethread-current
1632     "^" gnus-summary-reparent-thread
1633     "s" gnus-summary-show-thread
1634     "S" gnus-summary-show-all-threads
1635     "h" gnus-summary-hide-thread
1636     "H" gnus-summary-hide-all-threads
1637     "n" gnus-summary-next-thread
1638     "p" gnus-summary-prev-thread
1639     "u" gnus-summary-up-thread
1640     "o" gnus-summary-top-thread
1641     "d" gnus-summary-down-thread
1642     "#" gnus-uu-mark-thread
1643     "\M-#" gnus-uu-unmark-thread)
1644
1645   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1646     "g" gnus-summary-prepare
1647     "c" gnus-summary-insert-cached-articles)
1648
1649   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1650     "c" gnus-summary-catchup-and-exit
1651     "C" gnus-summary-catchup-all-and-exit
1652     "E" gnus-summary-exit-no-update
1653     "Q" gnus-summary-exit
1654     "Z" gnus-summary-exit
1655     "n" gnus-summary-catchup-and-goto-next-group
1656     "R" gnus-summary-reselect-current-group
1657     "G" gnus-summary-rescan-group
1658     "N" gnus-summary-next-group
1659     "s" gnus-summary-save-newsrc
1660     "P" gnus-summary-prev-group)
1661
1662   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1663     " " gnus-summary-next-page
1664     "n" gnus-summary-next-page
1665     "\177" gnus-summary-prev-page
1666     [delete] gnus-summary-prev-page
1667     "p" gnus-summary-prev-page
1668     "\r" gnus-summary-scroll-up
1669     "\M-\r" gnus-summary-scroll-down
1670     "<" gnus-summary-beginning-of-article
1671     ">" gnus-summary-end-of-article
1672     "b" gnus-summary-beginning-of-article
1673     "e" gnus-summary-end-of-article
1674     "^" gnus-summary-refer-parent-article
1675     "r" gnus-summary-refer-parent-article
1676     "D" gnus-summary-enter-digest-group
1677     "R" gnus-summary-refer-references
1678     "T" gnus-summary-refer-thread
1679     "g" gnus-summary-show-article
1680     "s" gnus-summary-isearch-article
1681     "P" gnus-summary-print-article
1682     "M" gnus-mailing-list-insinuate
1683     "t" gnus-article-babel)
1684
1685   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1686     "b" gnus-article-add-buttons
1687     "B" gnus-article-add-buttons-to-head
1688     "o" gnus-article-treat-overstrike
1689     "e" gnus-article-emphasize
1690     "w" gnus-article-fill-cited-article
1691     "Q" gnus-article-fill-long-lines
1692     "C" gnus-article-capitalize-sentences
1693     "c" gnus-article-remove-cr
1694     "q" gnus-article-de-quoted-unreadable
1695     "6" gnus-article-de-base64-unreadable
1696     "Z" gnus-article-decode-HZ
1697     "h" gnus-article-wash-html
1698     "s" gnus-summary-force-verify-and-decrypt
1699     "f" gnus-article-display-x-face
1700     "l" gnus-summary-stop-page-breaking
1701     "r" gnus-summary-caesar-message
1702     "t" gnus-summary-toggle-header
1703     "g" gnus-summary-toggle-smiley
1704     "v" gnus-summary-verbose-headers
1705     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1706     "p" gnus-article-verify-x-pgp-sig
1707     "d" gnus-article-treat-dumbquotes)
1708
1709   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1710     "a" gnus-article-hide
1711     "h" gnus-article-hide-headers
1712     "b" gnus-article-hide-boring-headers
1713     "s" gnus-article-hide-signature
1714     "c" gnus-article-hide-citation
1715     "C" gnus-article-hide-citation-in-followups
1716     "l" gnus-article-hide-list-identifiers
1717     "p" gnus-article-hide-pgp
1718     "B" gnus-article-strip-banner
1719     "P" gnus-article-hide-pem
1720     "\C-c" gnus-article-hide-citation-maybe)
1721
1722   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1723     "a" gnus-article-highlight
1724     "h" gnus-article-highlight-headers
1725     "c" gnus-article-highlight-citation
1726     "s" gnus-article-highlight-signature)
1727
1728   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1729     "w" gnus-article-decode-mime-words
1730     "c" gnus-article-decode-charset
1731     "v" gnus-mime-view-all-parts
1732     "b" gnus-article-view-part)
1733
1734   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1735     "z" gnus-article-date-ut
1736     "u" gnus-article-date-ut
1737     "l" gnus-article-date-local
1738     "p" gnus-article-date-english
1739     "e" gnus-article-date-lapsed
1740     "o" gnus-article-date-original
1741     "i" gnus-article-date-iso8601
1742     "s" gnus-article-date-user)
1743
1744   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1745     "t" gnus-article-remove-trailing-blank-lines
1746     "l" gnus-article-strip-leading-blank-lines
1747     "m" gnus-article-strip-multiple-blank-lines
1748     "a" gnus-article-strip-blank-lines
1749     "A" gnus-article-strip-all-blank-lines
1750     "s" gnus-article-strip-leading-space
1751     "e" gnus-article-strip-trailing-space
1752     "w" gnus-article-remove-leading-whitespace)
1753
1754   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1755     "v" gnus-version
1756     "f" gnus-summary-fetch-faq
1757     "d" gnus-summary-describe-group
1758     "h" gnus-summary-describe-briefly
1759     "i" gnus-info-find-node)
1760
1761   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1762     "e" gnus-summary-expire-articles
1763     "\M-\C-e" gnus-summary-expire-articles-now
1764     "\177" gnus-summary-delete-article
1765     [delete] gnus-summary-delete-article
1766     [backspace] gnus-summary-delete-article
1767     "m" gnus-summary-move-article
1768     "r" gnus-summary-respool-article
1769     "w" gnus-summary-edit-article
1770     "c" gnus-summary-copy-article
1771     "B" gnus-summary-crosspost-article
1772     "q" gnus-summary-respool-query
1773     "t" gnus-summary-respool-trace
1774     "i" gnus-summary-import-article
1775     "I" gnus-summary-create-article
1776     "p" gnus-summary-article-posted-p)
1777
1778   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1779     "o" gnus-summary-save-article
1780     "m" gnus-summary-save-article-mail
1781     "F" gnus-summary-write-article-file
1782     "r" gnus-summary-save-article-rmail
1783     "f" gnus-summary-save-article-file
1784     "b" gnus-summary-save-article-body-file
1785     "h" gnus-summary-save-article-folder
1786     "v" gnus-summary-save-article-vm
1787     "p" gnus-summary-pipe-output
1788     "s" gnus-soup-add-article)
1789
1790   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1791     "b" gnus-summary-display-buttonized
1792     "m" gnus-summary-repair-multipart
1793     "v" gnus-article-view-part
1794     "o" gnus-article-save-part
1795     "c" gnus-article-copy-part
1796     "C" gnus-article-view-part-as-charset
1797     "e" gnus-article-externalize-part
1798     "E" gnus-article-encrypt-body
1799     "i" gnus-article-inline-part
1800     "|" gnus-article-pipe-part))
1801
1802 (defvar gnus-article-post-menu nil)
1803
1804 (defun gnus-summary-make-menu-bar ()
1805   (gnus-turn-off-edit-menu 'summary)
1806
1807   (unless (boundp 'gnus-summary-misc-menu)
1808
1809     (easy-menu-define
1810       gnus-summary-kill-menu gnus-summary-mode-map ""
1811       (cons
1812        "Score"
1813        (nconc
1814         (list
1815          ["Customize" gnus-score-customize t])
1816         (gnus-make-score-map 'increase)
1817         (gnus-make-score-map 'lower)
1818         '(("Mark"
1819            ["Kill below" gnus-summary-kill-below t]
1820            ["Mark above" gnus-summary-mark-above t]
1821            ["Tick above" gnus-summary-tick-above t]
1822            ["Clear above" gnus-summary-clear-above t])
1823           ["Current score" gnus-summary-current-score t]
1824           ["Set score" gnus-summary-set-score t]
1825           ["Switch current score file..." gnus-score-change-score-file t]
1826           ["Set mark below..." gnus-score-set-mark-below t]
1827           ["Set expunge below..." gnus-score-set-expunge-below t]
1828           ["Edit current score file" gnus-score-edit-current-scores t]
1829           ["Edit score file" gnus-score-edit-file t]
1830           ["Trace score" gnus-score-find-trace t]
1831           ["Find words" gnus-score-find-favourite-words t]
1832           ["Rescore buffer" gnus-summary-rescore t]
1833           ["Increase score..." gnus-summary-increase-score t]
1834           ["Lower score..." gnus-summary-lower-score t]))))
1835
1836 ;; Define both the Article menu in the summary buffer and the equivalent
1837     ;; Commands menu in the article buffer here for consistency.
1838     (let ((innards
1839            `(("Hide"
1840               ["All" gnus-article-hide t]
1841               ["Headers" gnus-article-hide-headers t]
1842               ["Signature" gnus-article-hide-signature t]
1843               ["Citation" gnus-article-hide-citation t]
1844               ["List identifiers" gnus-article-hide-list-identifiers t]
1845               ["PGP" gnus-article-hide-pgp t]
1846               ["Banner" gnus-article-strip-banner t]
1847               ["Boring headers" gnus-article-hide-boring-headers t])
1848              ("Highlight"
1849               ["All" gnus-article-highlight t]
1850               ["Headers" gnus-article-highlight-headers t]
1851               ["Signature" gnus-article-highlight-signature t]
1852               ["Citation" gnus-article-highlight-citation t])
1853              ("MIME"
1854               ["Words" gnus-article-decode-mime-words t]
1855               ["Charset" gnus-article-decode-charset t]
1856               ["QP" gnus-article-de-quoted-unreadable t]
1857               ["Base64" gnus-article-de-base64-unreadable t]
1858               ["View all" gnus-mime-view-all-parts t]
1859               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1860               ["Encrypt body" gnus-article-encrypt-body t])
1861              ("Date"
1862               ["Local" gnus-article-date-local t]
1863               ["ISO8601" gnus-article-date-iso8601 t]
1864               ["UT" gnus-article-date-ut t]
1865               ["Original" gnus-article-date-original t]
1866               ["Lapsed" gnus-article-date-lapsed t]
1867               ["User-defined" gnus-article-date-user t])
1868              ("Washing"
1869               ("Remove Blanks"
1870                ["Leading" gnus-article-strip-leading-blank-lines t]
1871                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1872                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1873                ["All of the above" gnus-article-strip-blank-lines t]
1874                ["All" gnus-article-strip-all-blank-lines t]
1875                ["Leading space" gnus-article-strip-leading-space t]
1876                ["Trailing space" gnus-article-strip-trailing-space t]
1877                ["Leading space in headers"
1878                 gnus-article-remove-leading-whitespace t])
1879               ["Overstrike" gnus-article-treat-overstrike t]
1880               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1881               ["Emphasis" gnus-article-emphasize t]
1882               ["Word wrap" gnus-article-fill-cited-article t]
1883               ["Fill long lines" gnus-article-fill-long-lines t]
1884               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1885               ["CR" gnus-article-remove-cr t]
1886               ["Show X-Face" gnus-article-display-x-face t]
1887               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1888               ["Base64" gnus-article-de-base64-unreadable t]
1889               ["Rot 13" gnus-summary-caesar-message
1890                ,@(if (featurep 'xemacs) '(t)
1891                    '(:help "\"Caesar rotate\" article by 13"))]
1892               ["Unix pipe" gnus-summary-pipe-message t]
1893               ["Add buttons" gnus-article-add-buttons t]
1894               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1895               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1896               ["Verbose header" gnus-summary-verbose-headers t]
1897               ["Toggle header" gnus-summary-toggle-header t]
1898               ["Toggle smiley" gnus-summary-toggle-smiley t]
1899               ["Html" gnus-article-wash-html t]
1900               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1901               ["HZ" gnus-article-decode-HZ t])
1902              ("Output"
1903               ["Save in default format" gnus-summary-save-article
1904                ,@(if (featurep 'xemacs) '(t)
1905                    '(:help "Save article using default method"))]
1906               ["Save in file" gnus-summary-save-article-file
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Save article in file"))]
1909               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1910               ["Save in MH folder" gnus-summary-save-article-folder t]
1911               ["Save in VM folder" gnus-summary-save-article-vm t]
1912               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1913               ["Save body in file" gnus-summary-save-article-body-file t]
1914               ["Pipe through a filter" gnus-summary-pipe-output t]
1915               ["Add to SOUP packet" gnus-soup-add-article t]
1916               ["Print" gnus-summary-print-article t])
1917              ("Backend"
1918               ["Respool article..." gnus-summary-respool-article t]
1919               ["Move article..." gnus-summary-move-article
1920                (gnus-check-backend-function
1921                 'request-move-article gnus-newsgroup-name)]
1922               ["Copy article..." gnus-summary-copy-article t]
1923               ["Crosspost article..." gnus-summary-crosspost-article
1924                (gnus-check-backend-function
1925                 'request-replace-article gnus-newsgroup-name)]
1926               ["Import file..." gnus-summary-import-article t]
1927               ["Create article..." gnus-summary-create-article t]
1928               ["Check if posted" gnus-summary-article-posted-p t]
1929               ["Edit article" gnus-summary-edit-article
1930                (not (gnus-group-read-only-p))]
1931               ["Delete article" gnus-summary-delete-article
1932                (gnus-check-backend-function
1933                 'request-expire-articles gnus-newsgroup-name)]
1934               ["Query respool" gnus-summary-respool-query t]
1935               ["Trace respool" gnus-summary-respool-trace t]
1936               ["Delete expirable articles" gnus-summary-expire-articles-now
1937                (gnus-check-backend-function
1938                 'request-expire-articles gnus-newsgroup-name)])
1939              ("Extract"
1940               ["Uudecode" gnus-uu-decode-uu
1941                ,@(if (featurep 'xemacs) '(t)
1942                    '(:help "Decode uuencoded article(s)"))]
1943               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1944               ["Unshar" gnus-uu-decode-unshar t]
1945               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1946               ["Save" gnus-uu-decode-save t]
1947               ["Binhex" gnus-uu-decode-binhex t]
1948               ["Postscript" gnus-uu-decode-postscript t])
1949              ("Cache"
1950               ["Enter article" gnus-cache-enter-article t]
1951               ["Remove article" gnus-cache-remove-article t])
1952              ["Translate" gnus-article-babel t]
1953              ["Select article buffer" gnus-summary-select-article-buffer t]
1954              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1955              ["Isearch article..." gnus-summary-isearch-article t]
1956              ["Beginning of the article" gnus-summary-beginning-of-article t]
1957              ["End of the article" gnus-summary-end-of-article t]
1958              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1959              ["Fetch referenced articles" gnus-summary-refer-references t]
1960              ["Fetch current thread" gnus-summary-refer-thread t]
1961              ["Fetch article with id..." gnus-summary-refer-article t]
1962              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1963              ["Redisplay" gnus-summary-show-article t]
1964              ["Raw article" gnus-summary-show-raw-article t])))
1965       (easy-menu-define
1966         gnus-summary-article-menu gnus-summary-mode-map ""
1967         (cons "Article" innards))
1968
1969       (if (not (keymapp gnus-summary-article-menu))
1970           (easy-menu-define
1971             gnus-article-commands-menu gnus-article-mode-map ""
1972             (cons "Commands" innards))
1973         ;; in Emacs, don't share menu.
1974         (setq gnus-article-commands-menu
1975               (copy-keymap gnus-summary-article-menu))
1976         (define-key gnus-article-mode-map [menu-bar commands]
1977           (cons "Commands" gnus-article-commands-menu))))
1978
1979     (easy-menu-define
1980       gnus-summary-thread-menu gnus-summary-mode-map ""
1981       '("Threads"
1982         ["Toggle threading" gnus-summary-toggle-threads t]
1983         ["Hide threads" gnus-summary-hide-all-threads t]
1984         ["Show threads" gnus-summary-show-all-threads t]
1985         ["Hide thread" gnus-summary-hide-thread t]
1986         ["Show thread" gnus-summary-show-thread t]
1987         ["Go to next thread" gnus-summary-next-thread t]
1988         ["Go to previous thread" gnus-summary-prev-thread t]
1989         ["Go down thread" gnus-summary-down-thread t]
1990         ["Go up thread" gnus-summary-up-thread t]
1991         ["Top of thread" gnus-summary-top-thread t]
1992         ["Mark thread as read" gnus-summary-kill-thread t]
1993         ["Lower thread score" gnus-summary-lower-thread t]
1994         ["Raise thread score" gnus-summary-raise-thread t]
1995         ["Rethread current" gnus-summary-rethread-current t]))
1996
1997     (easy-menu-define
1998       gnus-summary-post-menu gnus-summary-mode-map ""
1999       `("Post"
2000         ["Post an article" gnus-summary-post-news
2001          ,@(if (featurep 'xemacs) '(t)
2002              '(:help "Post an article"))]
2003         ["Followup" gnus-summary-followup
2004          ,@(if (featurep 'xemacs) '(t)
2005              '(:help "Post followup to this article"))]
2006         ["Followup and yank" gnus-summary-followup-with-original
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Post followup to this article, quoting its contents"))]
2009         ["Supersede article" gnus-summary-supersede-article t]
2010         ["Cancel article" gnus-summary-cancel-article
2011          ,@(if (featurep 'xemacs) '(t)
2012              '(:help "Cancel an article you posted"))]
2013         ["Reply" gnus-summary-reply t]
2014         ["Reply and yank" gnus-summary-reply-with-original t]
2015         ["Wide reply" gnus-summary-wide-reply t]
2016         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2017          ,@(if (featurep 'xemacs) '(t)
2018              '(:help "Mail a reply, quoting this article"))]
2019         ["Mail forward" gnus-summary-mail-forward t]
2020         ["Post forward" gnus-summary-post-forward t]
2021         ["Digest and mail" gnus-uu-digest-mail-forward t]
2022         ["Digest and post" gnus-uu-digest-post-forward t]
2023         ["Resend message" gnus-summary-resend-message t]
2024         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2025         ["Send a mail" gnus-summary-mail-other-window t]
2026         ["Uuencode and post" gnus-uu-post-news
2027          ,@(if (featurep 'xemacs) '(t)
2028              '(:help "Post a uuencoded article"))]
2029         ["Followup via news" gnus-summary-followup-to-mail t]
2030         ["Followup via news and yank"
2031          gnus-summary-followup-to-mail-with-original t]
2032         ;;("Draft"
2033         ;;["Send" gnus-summary-send-draft t]
2034         ;;["Send bounced" gnus-resend-bounced-mail t])
2035         ))
2036
2037     (cond
2038      ((not (keymapp gnus-summary-post-menu))
2039       (setq gnus-article-post-menu gnus-summary-post-menu))
2040      ((not gnus-article-post-menu)
2041       ;; Don't share post menu.
2042       (setq gnus-article-post-menu
2043             (copy-keymap gnus-summary-post-menu))))
2044     (define-key gnus-article-mode-map [menu-bar post]
2045       (cons "Post" gnus-article-post-menu))
2046
2047     (easy-menu-define
2048       gnus-summary-misc-menu gnus-summary-mode-map ""
2049       `("Gnus"
2050         ("Mark Read"
2051          ["Mark as read" gnus-summary-mark-as-read-forward t]
2052          ["Mark same subject and select"
2053           gnus-summary-kill-same-subject-and-select t]
2054          ["Mark same subject" gnus-summary-kill-same-subject t]
2055          ["Catchup" gnus-summary-catchup
2056           ,@(if (featurep 'xemacs) '(t)
2057               '(:help "Mark unread articles in this group as read"))]
2058          ["Catchup all" gnus-summary-catchup-all t]
2059          ["Catchup to here" gnus-summary-catchup-to-here t]
2060          ["Catchup from here" gnus-summary-catchup-from-here t]
2061          ["Catchup region" gnus-summary-mark-region-as-read t]
2062          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2063         ("Mark Various"
2064          ["Tick" gnus-summary-tick-article-forward t]
2065          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2066          ["Remove marks" gnus-summary-clear-mark-forward t]
2067          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2068          ["Set bookmark" gnus-summary-set-bookmark t]
2069          ["Remove bookmark" gnus-summary-remove-bookmark t])
2070         ("Mark Limit"
2071          ["Marks..." gnus-summary-limit-to-marks t]
2072          ["Subject..." gnus-summary-limit-to-subject t]
2073          ["Author..." gnus-summary-limit-to-author t]
2074          ["Age..." gnus-summary-limit-to-age t]
2075          ["Extra..." gnus-summary-limit-to-extra t]
2076          ["Score" gnus-summary-limit-to-score t]
2077          ["Score" gnus-summary-limit-to-display-predicate t]
2078          ["Unread" gnus-summary-limit-to-unread t]
2079          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2080          ["Articles" gnus-summary-limit-to-articles t]
2081          ["Pop limit" gnus-summary-pop-limit t]
2082          ["Show dormant" gnus-summary-limit-include-dormant t]
2083          ["Hide childless dormant"
2084           gnus-summary-limit-exclude-childless-dormant t]
2085          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2086          ["Hide marked" gnus-summary-limit-exclude-marks t]
2087          ["Show expunged" gnus-summary-limit-include-expunged t])
2088         ("Process Mark"
2089          ["Set mark" gnus-summary-mark-as-processable t]
2090          ["Remove mark" gnus-summary-unmark-as-processable t]
2091          ["Remove all marks" gnus-summary-unmark-all-processable t]
2092          ["Mark above" gnus-uu-mark-over t]
2093          ["Mark series" gnus-uu-mark-series t]
2094          ["Mark region" gnus-uu-mark-region t]
2095          ["Unmark region" gnus-uu-unmark-region t]
2096          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2097          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2098          ["Mark all" gnus-uu-mark-all t]
2099          ["Mark buffer" gnus-uu-mark-buffer t]
2100          ["Mark sparse" gnus-uu-mark-sparse t]
2101          ["Mark thread" gnus-uu-mark-thread t]
2102          ["Unmark thread" gnus-uu-unmark-thread t]
2103          ("Process Mark Sets"
2104           ["Kill" gnus-summary-kill-process-mark t]
2105           ["Yank" gnus-summary-yank-process-mark
2106            gnus-newsgroup-process-stack]
2107           ["Save" gnus-summary-save-process-mark t]))
2108         ("Scroll article"
2109          ["Page forward" gnus-summary-next-page
2110           ,@(if (featurep 'xemacs) '(t)
2111               '(:help "Show next page of article"))]
2112          ["Page backward" gnus-summary-prev-page
2113           ,@(if (featurep 'xemacs) '(t)
2114               '(:help "Show previous page of article"))]
2115          ["Line forward" gnus-summary-scroll-up t])
2116         ("Move"
2117          ["Next unread article" gnus-summary-next-unread-article t]
2118          ["Previous unread article" gnus-summary-prev-unread-article t]
2119          ["Next article" gnus-summary-next-article t]
2120          ["Previous article" gnus-summary-prev-article t]
2121          ["Next unread subject" gnus-summary-next-unread-subject t]
2122          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2123          ["Next article same subject" gnus-summary-next-same-subject t]
2124          ["Previous article same subject" gnus-summary-prev-same-subject t]
2125          ["First unread article" gnus-summary-first-unread-article t]
2126          ["Best unread article" gnus-summary-best-unread-article t]
2127          ["Go to subject number..." gnus-summary-goto-subject t]
2128          ["Go to article number..." gnus-summary-goto-article t]
2129          ["Go to the last article" gnus-summary-goto-last-article t]
2130          ["Pop article off history" gnus-summary-pop-article t])
2131         ("Sort"
2132          ["Sort by number" gnus-summary-sort-by-number t]
2133          ["Sort by author" gnus-summary-sort-by-author t]
2134          ["Sort by subject" gnus-summary-sort-by-subject t]
2135          ["Sort by date" gnus-summary-sort-by-date t]
2136          ["Sort by score" gnus-summary-sort-by-score t]
2137          ["Sort by lines" gnus-summary-sort-by-lines t]
2138          ["Sort by characters" gnus-summary-sort-by-chars t]
2139          ["Original sort" gnus-summary-sort-by-original t])
2140         ("Help"
2141          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2142          ["Describe group" gnus-summary-describe-group t]
2143          ["Read manual" gnus-info-find-node t])
2144         ("Modes"
2145          ["Pick and read" gnus-pick-mode t]
2146          ["Binary" gnus-binary-mode t])
2147         ("Regeneration"
2148          ["Regenerate" gnus-summary-prepare t]
2149          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2150          ["Toggle threading" gnus-summary-toggle-threads t])
2151         ["See old articles" gnus-summary-insert-old-articles t]
2152         ["See new articles" gnus-summary-insert-new-articles t]
2153         ["Filter articles..." gnus-summary-execute-command t]
2154         ["Run command on subjects..." gnus-summary-universal-argument t]
2155         ["Search articles forward..." gnus-summary-search-article-forward t]
2156         ["Search articles backward..." gnus-summary-search-article-backward t]
2157         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2158         ["Expand window" gnus-summary-expand-window t]
2159         ["Expire expirable articles" gnus-summary-expire-articles
2160          (gnus-check-backend-function
2161           'request-expire-articles gnus-newsgroup-name)]
2162         ["Edit local kill file" gnus-summary-edit-local-kill t]
2163         ["Edit main kill file" gnus-summary-edit-global-kill t]
2164         ["Edit group parameters" gnus-summary-edit-parameters t]
2165         ["Customize group parameters" gnus-summary-customize-parameters t]
2166         ["Send a bug report" gnus-bug t]
2167         ("Exit"
2168          ["Catchup and exit" gnus-summary-catchup-and-exit
2169           ,@(if (featurep 'xemacs) '(t)
2170               '(:help "Mark unread articles in this group as read, then exit"))]
2171          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2172          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2173          ["Exit group" gnus-summary-exit
2174           ,@(if (featurep 'xemacs) '(t)
2175               '(:help "Exit current group, return to group selection mode"))]
2176          ["Exit group without updating" gnus-summary-exit-no-update t]
2177          ["Exit and goto next group" gnus-summary-next-group t]
2178          ["Exit and goto prev group" gnus-summary-prev-group t]
2179          ["Reselect group" gnus-summary-reselect-current-group t]
2180          ["Rescan group" gnus-summary-rescan-group t]
2181          ["Update dribble" gnus-summary-save-newsrc t])))
2182
2183     (gnus-run-hooks 'gnus-summary-menu-hook)))
2184
2185 (defvar gnus-summary-tool-bar-map nil)
2186
2187 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2188 (defun gnus-summary-make-tool-bar ()
2189   (if (and (fboundp 'tool-bar-add-item-from-menu)
2190            (default-value 'tool-bar-mode)
2191            (not gnus-summary-tool-bar-map))
2192       (setq gnus-summary-tool-bar-map
2193             (let ((tool-bar-map (make-sparse-keymap))
2194                   (load-path (mm-image-load-path)))
2195               (tool-bar-add-item-from-menu
2196                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2197               (tool-bar-add-item-from-menu
2198                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2199               (tool-bar-add-item-from-menu
2200                'gnus-summary-post-news "post" gnus-summary-mode-map)
2201               (tool-bar-add-item-from-menu
2202                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2203               (tool-bar-add-item-from-menu
2204                'gnus-summary-followup "followup" gnus-summary-mode-map)
2205               (tool-bar-add-item-from-menu
2206                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2207               (tool-bar-add-item-from-menu
2208                'gnus-summary-reply "reply" gnus-summary-mode-map)
2209               (tool-bar-add-item-from-menu
2210                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2211               (tool-bar-add-item-from-menu
2212                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2213               (tool-bar-add-item-from-menu
2214                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2215               (tool-bar-add-item-from-menu
2216                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2217               (tool-bar-add-item-from-menu
2218                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2219               (tool-bar-add-item-from-menu
2220                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2221               (tool-bar-add-item-from-menu
2222                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2223               (tool-bar-add-item-from-menu
2224                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2225               tool-bar-map)))
2226   (if gnus-summary-tool-bar-map
2227       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2228
2229 (defun gnus-score-set-default (var value)
2230   "A version of set that updates the GNU Emacs menu-bar."
2231   (set var value)
2232   ;; It is the message that forces the active status to be updated.
2233   (message ""))
2234
2235 (defun gnus-make-score-map (type)
2236   "Make a summary score map of type TYPE."
2237   (if t
2238       nil
2239     (let ((headers '(("author" "from" string)
2240                      ("subject" "subject" string)
2241                      ("article body" "body" string)
2242                      ("article head" "head" string)
2243                      ("xref" "xref" string)
2244                      ("extra header" "extra" string)
2245                      ("lines" "lines" number)
2246                      ("followups to author" "followup" string)))
2247           (types '((number ("less than" <)
2248                            ("greater than" >)
2249                            ("equal" =))
2250                    (string ("substring" s)
2251                            ("exact string" e)
2252                            ("fuzzy string" f)
2253                            ("regexp" r))))
2254           (perms '(("temporary" (current-time-string))
2255                    ("permanent" nil)
2256                    ("immediate" now)))
2257           header)
2258       (list
2259        (apply
2260         'nconc
2261         (list
2262          (if (eq type 'lower)
2263              "Lower score"
2264            "Increase score"))
2265         (let (outh)
2266           (while headers
2267             (setq header (car headers))
2268             (setq outh
2269                   (cons
2270                    (apply
2271                     'nconc
2272                     (list (car header))
2273                     (let ((ts (cdr (assoc (nth 2 header) types)))
2274                           outt)
2275                       (while ts
2276                         (setq outt
2277                               (cons
2278                                (apply
2279                                 'nconc
2280                                 (list (caar ts))
2281                                 (let ((ps perms)
2282                                       outp)
2283                                   (while ps
2284                                     (setq outp
2285                                           (cons
2286                                            (vector
2287                                             (caar ps)
2288                                             (list
2289                                              'gnus-summary-score-entry
2290                                              (nth 1 header)
2291                                              (if (or (string= (nth 1 header)
2292                                                               "head")
2293                                                      (string= (nth 1 header)
2294                                                               "body"))
2295                                                  ""
2296                                                (list 'gnus-summary-header
2297                                                      (nth 1 header)))
2298                                              (list 'quote (nth 1 (car ts)))
2299                                              (list 'gnus-score-delta-default
2300                                                    nil)
2301                                              (nth 1 (car ps))
2302                                              t)
2303                                             t)
2304                                            outp))
2305                                     (setq ps (cdr ps)))
2306                                   (list (nreverse outp))))
2307                                outt))
2308                         (setq ts (cdr ts)))
2309                       (list (nreverse outt))))
2310                    outh))
2311             (setq headers (cdr headers)))
2312           (list (nreverse outh))))))))
2313
2314 \f
2315
2316 (defun gnus-summary-mode (&optional group)
2317   "Major mode for reading articles.
2318
2319 All normal editing commands are switched off.
2320 \\<gnus-summary-mode-map>
2321 Each line in this buffer represents one article.  To read an
2322 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2323 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2324 respectively.
2325
2326 You can also post articles and send mail from this buffer.  To
2327 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2328 of an article, type `\\[gnus-summary-reply]'.
2329
2330 There are approx. one gazillion commands you can execute in this
2331 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2332
2333 The following commands are available:
2334
2335 \\{gnus-summary-mode-map}"
2336   (interactive)
2337   (kill-all-local-variables)
2338   (when (gnus-visual-p 'summary-menu 'menu)
2339     (gnus-summary-make-menu-bar)
2340     (gnus-summary-make-tool-bar))
2341   (gnus-summary-make-local-variables)
2342   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2343     (gnus-summary-make-local-variables))
2344   (gnus-make-thread-indent-array)
2345   (gnus-simplify-mode-line)
2346   (setq major-mode 'gnus-summary-mode)
2347   (setq mode-name "Summary")
2348   (make-local-variable 'minor-mode-alist)
2349   (use-local-map gnus-summary-mode-map)
2350   (buffer-disable-undo)
2351   (setq buffer-read-only t)             ;Disable modification
2352   (setq truncate-lines t)
2353   (setq selective-display t)
2354   (setq selective-display-ellipses t)   ;Display `...'
2355   (gnus-summary-set-display-table)
2356   (gnus-set-default-directory)
2357   (setq gnus-newsgroup-name group)
2358   (make-local-variable 'gnus-summary-line-format)
2359   (make-local-variable 'gnus-summary-line-format-spec)
2360   (make-local-variable 'gnus-summary-dummy-line-format)
2361   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2362   (make-local-variable 'gnus-summary-mark-positions)
2363   (make-local-hook 'pre-command-hook)
2364   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2365   (gnus-run-hooks 'gnus-summary-mode-hook)
2366   (turn-on-gnus-mailing-list-mode)
2367   (mm-enable-multibyte-mule4)
2368   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2369   (gnus-update-summary-mark-positions))
2370
2371 (defun gnus-summary-make-local-variables ()
2372   "Make all the local summary buffer variables."
2373   (let (global)
2374     (dolist (local gnus-summary-local-variables)
2375       (if (consp local)
2376           (progn
2377             (if (eq (cdr local) 'global)
2378                 ;; Copy the global value of the variable.
2379                 (setq global (symbol-value (car local)))
2380               ;; Use the value from the list.
2381               (setq global (eval (cdr local))))
2382             (set (make-local-variable (car local)) global))
2383         ;; Simple nil-valued local variable.
2384         (set (make-local-variable local) nil)))))
2385
2386 (defun gnus-summary-clear-local-variables ()
2387   (let ((locals gnus-summary-local-variables))
2388     (while locals
2389       (if (consp (car locals))
2390           (and (vectorp (caar locals))
2391                (set (caar locals) nil))
2392         (and (vectorp (car locals))
2393              (set (car locals) nil)))
2394       (setq locals (cdr locals)))))
2395
2396 ;; Summary data functions.
2397
2398 (defmacro gnus-data-number (data)
2399   `(car ,data))
2400
2401 (defmacro gnus-data-set-number (data number)
2402   `(setcar ,data ,number))
2403
2404 (defmacro gnus-data-mark (data)
2405   `(nth 1 ,data))
2406
2407 (defmacro gnus-data-set-mark (data mark)
2408   `(setcar (nthcdr 1 ,data) ,mark))
2409
2410 (defmacro gnus-data-pos (data)
2411   `(nth 2 ,data))
2412
2413 (defmacro gnus-data-set-pos (data pos)
2414   `(setcar (nthcdr 2 ,data) ,pos))
2415
2416 (defmacro gnus-data-header (data)
2417   `(nth 3 ,data))
2418
2419 (defmacro gnus-data-set-header (data header)
2420   `(setf (nth 3 ,data) ,header))
2421
2422 (defmacro gnus-data-level (data)
2423   `(nth 4 ,data))
2424
2425 (defmacro gnus-data-unread-p (data)
2426   `(= (nth 1 ,data) gnus-unread-mark))
2427
2428 (defmacro gnus-data-read-p (data)
2429   `(/= (nth 1 ,data) gnus-unread-mark))
2430
2431 (defmacro gnus-data-pseudo-p (data)
2432   `(consp (nth 3 ,data)))
2433
2434 (defmacro gnus-data-find (number)
2435   `(assq ,number gnus-newsgroup-data))
2436
2437 (defmacro gnus-data-find-list (number &optional data)
2438   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2439      (memq (assq ,number bdata)
2440            bdata)))
2441
2442 (defmacro gnus-data-make (number mark pos header level)
2443   `(list ,number ,mark ,pos ,header ,level))
2444
2445 (defun gnus-data-enter (after-article number mark pos header level offset)
2446   (let ((data (gnus-data-find-list after-article)))
2447     (unless data
2448       (error "No such article: %d" after-article))
2449     (setcdr data (cons (gnus-data-make number mark pos header level)
2450                        (cdr data)))
2451     (setq gnus-newsgroup-data-reverse nil)
2452     (gnus-data-update-list (cddr data) offset)))
2453
2454 (defun gnus-data-enter-list (after-article list &optional offset)
2455   (when list
2456     (let ((data (and after-article (gnus-data-find-list after-article)))
2457           (ilist list))
2458       (if (not (or data
2459                    after-article))
2460           (let ((odata gnus-newsgroup-data))
2461             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2462             (when offset
2463               (gnus-data-update-list odata offset)))
2464       ;; Find the last element in the list to be spliced into the main
2465         ;; list.
2466         (while (cdr list)
2467           (setq list (cdr list)))
2468         (if (not data)
2469             (progn
2470               (setcdr list gnus-newsgroup-data)
2471               (setq gnus-newsgroup-data ilist)
2472               (when offset
2473                 (gnus-data-update-list (cdr list) offset)))
2474           (setcdr list (cdr data))
2475           (setcdr data ilist)
2476           (when offset
2477             (gnus-data-update-list (cdr list) offset))))
2478       (setq gnus-newsgroup-data-reverse nil))))
2479
2480 (defun gnus-data-remove (article &optional offset)
2481   (let ((data gnus-newsgroup-data))
2482     (if (= (gnus-data-number (car data)) article)
2483         (progn
2484           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2485                 gnus-newsgroup-data-reverse nil)
2486           (when offset
2487             (gnus-data-update-list gnus-newsgroup-data offset)))
2488       (while (cdr data)
2489         (when (= (gnus-data-number (cadr data)) article)
2490           (setcdr data (cddr data))
2491           (when offset
2492             (gnus-data-update-list (cdr data) offset))
2493           (setq data nil
2494                 gnus-newsgroup-data-reverse nil))
2495         (setq data (cdr data))))))
2496
2497 (defmacro gnus-data-list (backward)
2498   `(if ,backward
2499        (or gnus-newsgroup-data-reverse
2500            (setq gnus-newsgroup-data-reverse
2501                  (reverse gnus-newsgroup-data)))
2502      gnus-newsgroup-data))
2503
2504 (defun gnus-data-update-list (data offset)
2505   "Add OFFSET to the POS of all data entries in DATA."
2506   (setq gnus-newsgroup-data-reverse nil)
2507   (while data
2508     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2509     (setq data (cdr data))))
2510
2511 (defun gnus-summary-article-pseudo-p (article)
2512   "Say whether this article is a pseudo article or not."
2513   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2514
2515 (defmacro gnus-summary-article-sparse-p (article)
2516   "Say whether this article is a sparse article or not."
2517   `(memq ,article gnus-newsgroup-sparse))
2518
2519 (defmacro gnus-summary-article-ancient-p (article)
2520   "Say whether this article is a sparse article or not."
2521   `(memq ,article gnus-newsgroup-ancient))
2522
2523 (defun gnus-article-parent-p (number)
2524   "Say whether this article is a parent or not."
2525   (let ((data (gnus-data-find-list number)))
2526     (and (cdr data)              ; There has to be an article after...
2527          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2528             (gnus-data-level (nth 1 data))))))
2529
2530 (defun gnus-article-children (number)
2531   "Return a list of all children to NUMBER."
2532   (let* ((data (gnus-data-find-list number))
2533          (level (gnus-data-level (car data)))
2534          children)
2535     (setq data (cdr data))
2536     (while (and data
2537                 (= (gnus-data-level (car data)) (1+ level)))
2538       (push (gnus-data-number (car data)) children)
2539       (setq data (cdr data)))
2540     children))
2541
2542 (defmacro gnus-summary-skip-intangible ()
2543   "If the current article is intangible, then jump to a different article."
2544   '(let ((to (get-text-property (point) 'gnus-intangible)))
2545      (and to (gnus-summary-goto-subject to))))
2546
2547 (defmacro gnus-summary-article-intangible-p ()
2548   "Say whether this article is intangible or not."
2549   '(get-text-property (point) 'gnus-intangible))
2550
2551 (defun gnus-article-read-p (article)
2552   "Say whether ARTICLE is read or not."
2553   (not (or (memq article gnus-newsgroup-marked)
2554            (memq article gnus-newsgroup-unreads)
2555            (memq article gnus-newsgroup-unselected)
2556            (memq article gnus-newsgroup-dormant))))
2557
2558 ;; Some summary mode macros.
2559
2560 (defmacro gnus-summary-article-number ()
2561   "The article number of the article on the current line.
2562 If there isn's an article number here, then we return the current
2563 article number."
2564   '(progn
2565      (gnus-summary-skip-intangible)
2566      (or (get-text-property (point) 'gnus-number)
2567          (gnus-summary-last-subject))))
2568
2569 (defmacro gnus-summary-article-header (&optional number)
2570   "Return the header of article NUMBER."
2571   `(gnus-data-header (gnus-data-find
2572                       ,(or number '(gnus-summary-article-number)))))
2573
2574 (defmacro gnus-summary-thread-level (&optional number)
2575   "Return the level of thread that starts with article NUMBER."
2576   `(if (and (eq gnus-summary-make-false-root 'dummy)
2577             (get-text-property (point) 'gnus-intangible))
2578        0
2579      (gnus-data-level (gnus-data-find
2580                        ,(or number '(gnus-summary-article-number))))))
2581
2582 (defmacro gnus-summary-article-mark (&optional number)
2583   "Return the mark of article NUMBER."
2584   `(gnus-data-mark (gnus-data-find
2585                     ,(or number '(gnus-summary-article-number)))))
2586
2587 (defmacro gnus-summary-article-pos (&optional number)
2588   "Return the position of the line of article NUMBER."
2589   `(gnus-data-pos (gnus-data-find
2590                    ,(or number '(gnus-summary-article-number)))))
2591
2592 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2593 (defmacro gnus-summary-article-subject (&optional number)
2594   "Return current subject string or nil if nothing."
2595   `(let ((headers
2596           ,(if number
2597                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2598              '(gnus-data-header (assq (gnus-summary-article-number)
2599                                       gnus-newsgroup-data)))))
2600      (and headers
2601           (vectorp headers)
2602           (mail-header-subject headers))))
2603
2604 (defmacro gnus-summary-article-score (&optional number)
2605   "Return current article score."
2606   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2607                   gnus-newsgroup-scored))
2608        gnus-summary-default-score 0))
2609
2610 (defun gnus-summary-article-children (&optional number)
2611   "Return a list of article numbers that are children of article NUMBER."
2612   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2613          (level (gnus-data-level (car data)))
2614          l children)
2615     (while (and (setq data (cdr data))
2616                 (> (setq l (gnus-data-level (car data))) level))
2617       (and (= (1+ level) l)
2618            (push (gnus-data-number (car data))
2619                  children)))
2620     (nreverse children)))
2621
2622 (defun gnus-summary-article-parent (&optional number)
2623   "Return the article number of the parent of article NUMBER."
2624   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2625                                     (gnus-data-list t)))
2626          (level (gnus-data-level (car data))))
2627     (if (zerop level)
2628         ()                              ; This is a root.
2629       ;; We search until we find an article with a level less than
2630       ;; this one.  That function has to be the parent.
2631       (while (and (setq data (cdr data))
2632                   (not (< (gnus-data-level (car data)) level))))
2633       (and data (gnus-data-number (car data))))))
2634
2635 (defun gnus-unread-mark-p (mark)
2636   "Say whether MARK is the unread mark."
2637   (= mark gnus-unread-mark))
2638
2639 (defun gnus-read-mark-p (mark)
2640   "Say whether MARK is one of the marks that mark as read.
2641 This is all marks except unread, ticked, dormant, and expirable."
2642   (not (or (= mark gnus-unread-mark)
2643            (= mark gnus-ticked-mark)
2644            (= mark gnus-dormant-mark)
2645            (= mark gnus-expirable-mark))))
2646
2647 (defmacro gnus-article-mark (number)
2648   "Return the MARK of article NUMBER.
2649 This macro should only be used when computing the mark the \"first\"
2650 time; i.e., when generating the summary lines.  After that,
2651 `gnus-summary-article-mark' should be used to examine the
2652 marks of articles."
2653   `(cond
2654     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2655     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2656     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2657     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2658     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2659     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2660     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2661     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2662            gnus-ancient-mark))))
2663
2664 ;; Saving hidden threads.
2665
2666 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2667 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2668
2669 (defmacro gnus-save-hidden-threads (&rest forms)
2670   "Save hidden threads, eval FORMS, and restore the hidden threads."
2671   (let ((config (make-symbol "config")))
2672     `(let ((,config (gnus-hidden-threads-configuration)))
2673        (unwind-protect
2674            (save-excursion
2675              ,@forms)
2676          (gnus-restore-hidden-threads-configuration ,config)))))
2677
2678 (defun gnus-data-compute-positions ()
2679   "Compute the positions of all articles."
2680   (setq gnus-newsgroup-data-reverse nil)
2681   (let ((data gnus-newsgroup-data))
2682     (save-excursion
2683       (gnus-save-hidden-threads
2684         (gnus-summary-show-all-threads)
2685         (goto-char (point-min))
2686         (while data
2687           (while (get-text-property (point) 'gnus-intangible)
2688             (forward-line 1))
2689           (gnus-data-set-pos (car data) (+ (point) 3))
2690           (setq data (cdr data))
2691           (forward-line 1))))))
2692
2693 (defun gnus-hidden-threads-configuration ()
2694   "Return the current hidden threads configuration."
2695   (save-excursion
2696     (let (config)
2697       (goto-char (point-min))
2698       (while (search-forward "\r" nil t)
2699         (push (1- (point)) config))
2700       config)))
2701
2702 (defun gnus-restore-hidden-threads-configuration (config)
2703   "Restore hidden threads configuration from CONFIG."
2704   (save-excursion
2705     (let (point buffer-read-only)
2706       (while (setq point (pop config))
2707         (when (and (< point (point-max))
2708                    (goto-char point)
2709                    (eq (char-after) ?\n))
2710           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2711
2712 ;; Various summary mode internalish functions.
2713
2714 (defun gnus-mouse-pick-article (e)
2715   (interactive "e")
2716   (mouse-set-point e)
2717   (gnus-summary-next-page nil t))
2718
2719 (defun gnus-summary-set-display-table ()
2720   "Change the display table.
2721 Odd characters have a tendency to mess
2722 up nicely formatted displays - we make all possible glyphs
2723 display only a single character."
2724
2725   ;; We start from the standard display table, if any.
2726   (let ((table (or (copy-sequence standard-display-table)
2727                    (make-display-table)))
2728         (i 32))
2729     ;; Nix out all the control chars...
2730     (while (>= (setq i (1- i)) 0)
2731       (aset table i [??]))
2732    ;; ... but not newline and cr, of course.  (cr is necessary for the
2733     ;; selective display).
2734     (aset table ?\n nil)
2735     (aset table ?\r nil)
2736     ;; We keep TAB as well.
2737     (aset table ?\t nil)
2738     ;; We nix out any glyphs over 126 that are not set already.
2739     (let ((i 256))
2740       (while (>= (setq i (1- i)) 127)
2741         ;; Only modify if the entry is nil.
2742         (unless (aref table i)
2743           (aset table i [??]))))
2744     (setq buffer-display-table table)))
2745
2746 (defun gnus-summary-set-article-display-arrow (pos)
2747   "Update the overlay arrow to point to line at position POS."
2748   (when (and gnus-summary-display-arrow
2749              (boundp 'overlay-arrow-position)
2750              (boundp 'overlay-arrow-string))
2751     (save-excursion
2752       (goto-char pos)
2753       (beginning-of-line)
2754       (unless overlay-arrow-position
2755         (setq overlay-arrow-position (make-marker)))
2756       (setq overlay-arrow-string "=>"
2757             overlay-arrow-position (set-marker overlay-arrow-position
2758                                                (point)
2759                                                (current-buffer))))))
2760
2761 (defun gnus-summary-buffer-name (group)
2762   "Return the summary buffer name of GROUP."
2763   (concat "*Summary " group "*"))
2764
2765 (defun gnus-summary-setup-buffer (group)
2766   "Initialize summary buffer."
2767   (let ((buffer (gnus-summary-buffer-name group)))
2768     (if (get-buffer buffer)
2769         (progn
2770           (set-buffer buffer)
2771           (setq gnus-summary-buffer (current-buffer))
2772           (not gnus-newsgroup-prepared))
2773       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2774       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2775       (gnus-summary-mode group)
2776       (when gnus-carpal
2777         (gnus-carpal-setup-buffer 'summary))
2778       (unless gnus-single-article-buffer
2779         (make-local-variable 'gnus-article-buffer)
2780         (make-local-variable 'gnus-article-current)
2781         (make-local-variable 'gnus-original-article-buffer))
2782       (setq gnus-newsgroup-name group)
2783       ;; Set any local variables in the group parameters.
2784       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2785       t)))
2786
2787 (defun gnus-set-global-variables ()
2788   "Set the global equivalents of the buffer-local variables.
2789 They are set to the latest values they had.  These reflect the summary
2790 buffer that was in action when the last article was fetched."
2791   (when (eq major-mode 'gnus-summary-mode)
2792     (setq gnus-summary-buffer (current-buffer))
2793     (let ((name gnus-newsgroup-name)
2794           (marked gnus-newsgroup-marked)
2795           (unread gnus-newsgroup-unreads)
2796           (headers gnus-current-headers)
2797           (data gnus-newsgroup-data)
2798           (summary gnus-summary-buffer)
2799           (article-buffer gnus-article-buffer)
2800           (original gnus-original-article-buffer)
2801           (gac gnus-article-current)
2802           (reffed gnus-reffed-article-number)
2803           (score-file gnus-current-score-file)
2804           (default-charset gnus-newsgroup-charset)
2805           vlist)
2806       (let ((locals gnus-newsgroup-variables))
2807         (while locals
2808           (if (consp (car locals))
2809               (push (eval (caar locals)) vlist)
2810             (push (eval (car locals)) vlist))
2811           (setq locals (cdr locals)))
2812         (setq vlist (nreverse vlist)))
2813       (save-excursion
2814         (set-buffer gnus-group-buffer)
2815         (setq gnus-newsgroup-name name
2816               gnus-newsgroup-marked marked
2817               gnus-newsgroup-unreads unread
2818               gnus-current-headers headers
2819               gnus-newsgroup-data data
2820               gnus-article-current gac
2821               gnus-summary-buffer summary
2822               gnus-article-buffer article-buffer
2823               gnus-original-article-buffer original
2824               gnus-reffed-article-number reffed
2825               gnus-current-score-file score-file
2826               gnus-newsgroup-charset default-charset)
2827         (let ((locals gnus-newsgroup-variables))
2828           (while locals
2829             (if (consp (car locals))
2830                 (set (caar locals) (pop vlist))
2831               (set (car locals) (pop vlist)))
2832             (setq locals (cdr locals))))
2833         ;; The article buffer also has local variables.
2834         (when (gnus-buffer-live-p gnus-article-buffer)
2835           (set-buffer gnus-article-buffer)
2836           (setq gnus-summary-buffer summary))))))
2837
2838 (defun gnus-summary-article-unread-p (article)
2839   "Say whether ARTICLE is unread or not."
2840   (memq article gnus-newsgroup-unreads))
2841
2842 (defun gnus-summary-first-article-p (&optional article)
2843   "Return whether ARTICLE is the first article in the buffer."
2844   (if (not (setq article (or article (gnus-summary-article-number))))
2845       nil
2846     (eq article (caar gnus-newsgroup-data))))
2847
2848 (defun gnus-summary-last-article-p (&optional article)
2849   "Return whether ARTICLE is the last article in the buffer."
2850   (if (not (setq article (or article (gnus-summary-article-number))))
2851       ;; All non-existent numbers are the last article.  :-)
2852       t
2853     (not (cdr (gnus-data-find-list article)))))
2854
2855 (defun gnus-make-thread-indent-array ()
2856   (let ((n 200))
2857     (unless (and gnus-thread-indent-array
2858                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2859       (setq gnus-thread-indent-array (make-vector 201 "")
2860             gnus-thread-indent-array-level gnus-thread-indent-level)
2861       (while (>= n 0)
2862         (aset gnus-thread-indent-array n
2863               (make-string (* n gnus-thread-indent-level) ? ))
2864         (setq n (1- n))))))
2865
2866 (defun gnus-update-summary-mark-positions ()
2867   "Compute where the summary marks are to go."
2868   (save-excursion
2869     (when (gnus-buffer-exists-p gnus-summary-buffer)
2870       (set-buffer gnus-summary-buffer))
2871     (let ((gnus-replied-mark 129)
2872           (gnus-score-below-mark 130)
2873           (gnus-score-over-mark 130)
2874           (gnus-download-mark 131)
2875           (spec gnus-summary-line-format-spec)
2876           gnus-visual pos)
2877       (save-excursion
2878         (gnus-set-work-buffer)
2879         (let ((gnus-summary-line-format-spec spec)
2880               (gnus-newsgroup-downloadable '((0 . t))))
2881           (gnus-summary-insert-line
2882            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2883            0 nil 128 t nil "" nil 1)
2884           (goto-char (point-min))
2885           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2886                                              (- (point) 2)))))
2887           (goto-char (point-min))
2888           (push (cons 'replied (and (search-forward "\201" nil t)
2889                                     (- (point) 2)))
2890                 pos)
2891           (goto-char (point-min))
2892           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2893                 pos)
2894           (goto-char (point-min))
2895           (push (cons 'download
2896                       (and (search-forward "\203" nil t) (- (point) 2)))
2897                 pos)))
2898       (setq gnus-summary-mark-positions pos))))
2899
2900 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2901   "Insert a dummy root in the summary buffer."
2902   (beginning-of-line)
2903   (gnus-add-text-properties
2904    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2905    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2906
2907 (defun gnus-summary-from-or-to-or-newsgroups (header)
2908   (let ((to (cdr (assq 'To (mail-header-extra header))))
2909         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2910         (mail-parse-charset gnus-newsgroup-charset)
2911         (mail-parse-ignored-charsets
2912          (save-excursion (set-buffer gnus-summary-buffer)
2913                          gnus-newsgroup-ignored-charsets)))
2914     (cond
2915      ((and to
2916            gnus-ignored-from-addresses
2917            (string-match gnus-ignored-from-addresses
2918                          (mail-header-from header)))
2919       (concat "-> "
2920               (or (car (funcall gnus-extract-address-components
2921                                 (funcall
2922                                  gnus-decode-encoded-word-function to)))
2923                   (funcall gnus-decode-encoded-word-function to))))
2924      ((and newsgroups
2925            gnus-ignored-from-addresses
2926            (string-match gnus-ignored-from-addresses
2927                          (mail-header-from header)))
2928       (concat "=> " newsgroups))
2929      (t
2930       (or (car (funcall gnus-extract-address-components
2931                         (mail-header-from header)))
2932           (mail-header-from header))))))
2933
2934 (defun gnus-summary-insert-line (gnus-tmp-header
2935                                  gnus-tmp-level gnus-tmp-current
2936                                  gnus-tmp-unread gnus-tmp-replied
2937                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2938                                  &optional gnus-tmp-dummy gnus-tmp-score
2939                                  gnus-tmp-process)
2940   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2941          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2942          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2943          (gnus-tmp-score-char
2944           (if (or (null gnus-summary-default-score)
2945                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2946                       gnus-summary-zcore-fuzz))
2947               ?                         ;Whitespace
2948             (if (< gnus-tmp-score gnus-summary-default-score)
2949                 gnus-score-below-mark gnus-score-over-mark)))
2950          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2951          (gnus-tmp-replied
2952           (cond (gnus-tmp-process gnus-process-mark)
2953                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2954                  gnus-cached-mark)
2955                 (gnus-tmp-replied gnus-replied-mark)
2956                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2957                  gnus-forwarded-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2959                  gnus-saved-mark)
2960                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2961                  gnus-recent-mark)
2962                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2963                  gnus-unseen-mark)
2964                 (t gnus-no-mark)))
2965          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2966          (gnus-tmp-name
2967           (cond
2968            ((string-match "<[^>]+> *$" gnus-tmp-from)
2969             (let ((beg (match-beginning 0)))
2970               (or (and (string-match "^\".+\"" gnus-tmp-from)
2971                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2972                   (substring gnus-tmp-from 0 beg))))
2973            ((string-match "(.+)" gnus-tmp-from)
2974             (substring gnus-tmp-from
2975                        (1+ (match-beginning 0)) (1- (match-end 0))))
2976            (t gnus-tmp-from)))
2977          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2978          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2979          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2980          (buffer-read-only nil))
2981     (when (string= gnus-tmp-name "")
2982       (setq gnus-tmp-name gnus-tmp-from))
2983     (unless (numberp gnus-tmp-lines)
2984       (setq gnus-tmp-lines -1))
2985     (if (= gnus-tmp-lines -1)
2986         (setq gnus-tmp-lines "?")
2987       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2988     (gnus-put-text-property
2989      (point)
2990      (progn (eval gnus-summary-line-format-spec) (point))
2991      'gnus-number gnus-tmp-number)
2992     (when (gnus-visual-p 'summary-highlight 'highlight)
2993       (forward-line -1)
2994       (gnus-run-hooks 'gnus-summary-update-hook)
2995       (forward-line 1))))
2996
2997 (defun gnus-summary-update-line (&optional dont-update)
2998   "Update summary line after change."
2999   (when (and gnus-summary-default-score
3000              (not gnus-summary-inhibit-highlight))
3001     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3002            (article (gnus-summary-article-number))
3003            (score (gnus-summary-article-score article)))
3004       (unless dont-update
3005         (if (and gnus-summary-mark-below
3006                  (< (gnus-summary-article-score)
3007                     gnus-summary-mark-below))
3008             ;; This article has a low score, so we mark it as read.
3009             (when (memq article gnus-newsgroup-unreads)
3010               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3011           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3012             ;; This article was previously marked as read on account
3013             ;; of a low score, but now it has risen, so we mark it as
3014             ;; unread.
3015             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3016         (gnus-summary-update-mark
3017          (if (or (null gnus-summary-default-score)
3018                  (<= (abs (- score gnus-summary-default-score))
3019                      gnus-summary-zcore-fuzz))
3020              ?                          ;Whitespace
3021            (if (< score gnus-summary-default-score)
3022                gnus-score-below-mark gnus-score-over-mark))
3023          'score))
3024       ;; Do visual highlighting.
3025       (when (gnus-visual-p 'summary-highlight 'highlight)
3026         (gnus-run-hooks 'gnus-summary-update-hook)))))
3027
3028 (defvar gnus-tmp-new-adopts nil)
3029
3030 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3031   "Return the number of articles in THREAD.
3032 This may be 0 in some cases -- if none of the articles in
3033 the thread are to be displayed."
3034   (let* ((number
3035          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3036           (cond
3037            ((not (listp thread))
3038             1)
3039            ((and (consp thread) (cdr thread))
3040             (apply
3041              '+ 1 (mapcar
3042                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3043            ((null thread)
3044             1)
3045            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3046             1)
3047            (t 0))))
3048     (when (and level (zerop level) gnus-tmp-new-adopts)
3049       (incf number
3050             (apply '+ (mapcar
3051                        'gnus-summary-number-of-articles-in-thread
3052                        gnus-tmp-new-adopts))))
3053     (if char
3054         (if (> number 1) gnus-not-empty-thread-mark
3055           gnus-empty-thread-mark)
3056       number)))
3057
3058 (defun gnus-summary-set-local-parameters (group)
3059   "Go through the local params of GROUP and set all variable specs in that list."
3060   (let ((params (gnus-group-find-parameter group))
3061         (vars '(quit-config))           ; Ignore quit-config.
3062         elem)
3063     (while params
3064       (setq elem (car params)
3065             params (cdr params))
3066       (and (consp elem)                 ; Has to be a cons.
3067            (consp (cdr elem))           ; The cdr has to be a list.
3068            (symbolp (car elem))         ; Has to be a symbol in there.
3069            (not (memq (car elem) vars))
3070            (ignore-errors               ; So we set it.
3071              (push (car elem) vars)
3072              (make-local-variable (car elem))
3073              (set (car elem) (eval (nth 1 elem))))))))
3074
3075 (defun gnus-summary-read-group (group &optional show-all no-article
3076                                       kill-buffer no-display backward
3077                                       select-articles)
3078   "Start reading news in newsgroup GROUP.
3079 If SHOW-ALL is non-nil, already read articles are also listed.
3080 If NO-ARTICLE is non-nil, no article is selected initially.
3081 If NO-DISPLAY, don't generate a summary buffer."
3082   (let (result)
3083     (while (and group
3084                 (null (setq result
3085                             (let ((gnus-auto-select-next nil))
3086                               (or (gnus-summary-read-group-1
3087                                    group show-all no-article
3088                                    kill-buffer no-display
3089                                    select-articles)
3090                                   (setq show-all nil
3091                                         select-articles nil)))))
3092                 (eq gnus-auto-select-next 'quietly))
3093       (set-buffer gnus-group-buffer)
3094       ;; The entry function called above goes to the next
3095       ;; group automatically, so we go two groups back
3096       ;; if we are searching for the previous group.
3097       (when backward
3098         (gnus-group-prev-unread-group 2))
3099       (if (not (equal group (gnus-group-group-name)))
3100           (setq group (gnus-group-group-name))
3101         (setq group nil)))
3102     result))
3103
3104 (defun gnus-summary-read-group-1 (group show-all no-article
3105                                         kill-buffer no-display
3106                                         &optional select-articles)
3107   ;; Killed foreign groups can't be entered.
3108   ;;  (when (and (not (gnus-group-native-p group))
3109   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3110   ;;    (error "Dead non-native groups can't be entered"))
3111   (gnus-message 5 "Retrieving newsgroup: %s..."
3112                 (gnus-group-decoded-name group))
3113   (let* ((new-group (gnus-summary-setup-buffer group))
3114          (quit-config (gnus-group-quit-config group))
3115          (did-select (and new-group (gnus-select-newsgroup
3116                                      group show-all select-articles))))
3117     (cond
3118      ;; This summary buffer exists already, so we just select it.
3119      ((not new-group)
3120       (gnus-set-global-variables)
3121       (when kill-buffer
3122         (gnus-kill-or-deaden-summary kill-buffer))
3123       (gnus-configure-windows 'summary 'force)
3124       (gnus-set-mode-line 'summary)
3125       (gnus-summary-position-point)
3126       (message "")
3127       t)
3128      ;; We couldn't select this group.
3129      ((null did-select)
3130       (when (and (eq major-mode 'gnus-summary-mode)
3131                  (not (equal (current-buffer) kill-buffer)))
3132         (kill-buffer (current-buffer))
3133         (if (not quit-config)
3134             (progn
3135               ;; Update the info -- marks might need to be removed,
3136               ;; for instance.
3137               (gnus-summary-update-info)
3138               (set-buffer gnus-group-buffer)
3139               (gnus-group-jump-to-group group)
3140               (gnus-group-next-unread-group 1))
3141           (gnus-handle-ephemeral-exit quit-config)))
3142       (let ((grpinfo (gnus-get-info group)))
3143         (if (null (gnus-info-read grpinfo))
3144             (gnus-message 3 "Group %s contains no messages"
3145                           (gnus-group-decoded-name group))
3146           (gnus-message 3 "Can't select group")))
3147       nil)
3148      ;; The user did a `C-g' while prompting for number of articles,
3149      ;; so we exit this group.
3150      ((eq did-select 'quit)
3151       (and (eq major-mode 'gnus-summary-mode)
3152            (not (equal (current-buffer) kill-buffer))
3153            (kill-buffer (current-buffer)))
3154       (when kill-buffer
3155         (gnus-kill-or-deaden-summary kill-buffer))
3156       (if (not quit-config)
3157           (progn
3158             (set-buffer gnus-group-buffer)
3159             (gnus-group-jump-to-group group)
3160             (gnus-group-next-unread-group 1)
3161             (gnus-configure-windows 'group 'force))
3162         (gnus-handle-ephemeral-exit quit-config))
3163       ;; Finally signal the quit.
3164       (signal 'quit nil))
3165      ;; The group was successfully selected.
3166      (t
3167       (gnus-set-global-variables)
3168       ;; Save the active value in effect when the group was entered.
3169       (setq gnus-newsgroup-active
3170             (gnus-copy-sequence
3171              (gnus-active gnus-newsgroup-name)))
3172       ;; You can change the summary buffer in some way with this hook.
3173       (gnus-run-hooks 'gnus-select-group-hook)
3174       (gnus-update-format-specifications
3175        nil 'summary 'summary-mode 'summary-dummy)
3176       (gnus-update-summary-mark-positions)
3177       ;; Do score processing.
3178       (when gnus-use-scoring
3179         (gnus-possibly-score-headers))
3180       ;; Check whether to fill in the gaps in the threads.
3181       (when gnus-build-sparse-threads
3182         (gnus-build-sparse-threads))
3183       ;; Find the initial limit.
3184       (if gnus-show-threads
3185           (if show-all
3186               (let ((gnus-newsgroup-dormant nil))
3187                 (gnus-summary-initial-limit show-all))
3188             (gnus-summary-initial-limit show-all))
3189         ;; When untreaded, all articles are always shown.
3190         (setq gnus-newsgroup-limit
3191               (mapcar
3192                (lambda (header) (mail-header-number header))
3193                gnus-newsgroup-headers)))
3194       ;; Generate the summary buffer.
3195       (unless no-display
3196         (gnus-summary-prepare))
3197       (when gnus-use-trees
3198         (gnus-tree-open group)
3199         (setq gnus-summary-highlight-line-function
3200               'gnus-tree-highlight-article))
3201       ;; If the summary buffer is empty, but there are some low-scored
3202       ;; articles or some excluded dormants, we include these in the
3203       ;; buffer.
3204       (when (and (zerop (buffer-size))
3205                  (not no-display))
3206         (cond (gnus-newsgroup-dormant
3207                (gnus-summary-limit-include-dormant))
3208               ((and gnus-newsgroup-scored show-all)
3209                (gnus-summary-limit-include-expunged t))))
3210       ;; Function `gnus-apply-kill-file' must be called in this hook.
3211       (gnus-run-hooks 'gnus-apply-kill-hook)
3212       (if (and (zerop (buffer-size))
3213                (not no-display))
3214           (progn
3215             ;; This newsgroup is empty.
3216             (gnus-summary-catchup-and-exit nil t)
3217             (gnus-message 6 "No unread news")
3218             (when kill-buffer
3219               (gnus-kill-or-deaden-summary kill-buffer))
3220             ;; Return nil from this function.
3221             nil)
3222         ;; Hide conversation thread subtrees.  We cannot do this in
3223         ;; gnus-summary-prepare-hook since kill processing may not
3224         ;; work with hidden articles.
3225         (and gnus-show-threads
3226              gnus-thread-hide-subtree
3227              (gnus-summary-hide-all-threads))
3228         (when kill-buffer
3229           (gnus-kill-or-deaden-summary kill-buffer))
3230         ;; Show first unread article if requested.
3231         (if (and (not no-article)
3232                  (not no-display)
3233                  gnus-newsgroup-unreads
3234                  gnus-auto-select-first)
3235             (progn
3236               (gnus-configure-windows 'summary)
3237               (cond
3238                ((eq gnus-auto-select-first 'best)
3239                 (gnus-summary-best-unread-article))
3240                ((eq gnus-auto-select-first t)
3241                 (gnus-summary-first-unread-article))
3242                ((gnus-functionp gnus-auto-select-first)
3243                 (funcall gnus-auto-select-first))))
3244           ;; Don't select any articles, just move point to the first
3245           ;; article in the group.
3246           (goto-char (point-min))
3247           (gnus-summary-position-point)
3248           (gnus-configure-windows 'summary 'force)
3249           (gnus-set-mode-line 'summary))
3250         (when (get-buffer-window gnus-group-buffer t)
3251           ;; Gotta use windows, because recenter does weird stuff if
3252           ;; the current buffer ain't the displayed window.
3253           (let ((owin (selected-window)))
3254             (select-window (get-buffer-window gnus-group-buffer t))
3255             (when (gnus-group-goto-group group)
3256               (recenter))
3257             (select-window owin)))
3258         ;; Mark this buffer as "prepared".
3259         (setq gnus-newsgroup-prepared t)
3260         (gnus-run-hooks 'gnus-summary-prepared-hook)
3261         t)))))
3262
3263 (defun gnus-summary-prepare ()
3264   "Generate the summary buffer."
3265   (interactive)
3266   (let ((buffer-read-only nil))
3267     (erase-buffer)
3268     (setq gnus-newsgroup-data nil
3269           gnus-newsgroup-data-reverse nil)
3270     (gnus-run-hooks 'gnus-summary-generate-hook)
3271     ;; Generate the buffer, either with threads or without.
3272     (when gnus-newsgroup-headers
3273       (gnus-summary-prepare-threads
3274        (if gnus-show-threads
3275            (gnus-sort-gathered-threads
3276             (funcall gnus-summary-thread-gathering-function
3277                      (gnus-sort-threads
3278                       (gnus-cut-threads (gnus-make-threads)))))
3279          ;; Unthreaded display.
3280          (gnus-sort-articles gnus-newsgroup-headers))))
3281     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3282     ;; Call hooks for modifying summary buffer.
3283     (goto-char (point-min))
3284     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3285
3286 (defsubst gnus-general-simplify-subject (subject)
3287   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3288   (setq subject
3289         (cond
3290          ;; Truncate the subject.
3291          (gnus-simplify-subject-functions
3292           (gnus-map-function gnus-simplify-subject-functions subject))
3293          ((numberp gnus-summary-gather-subject-limit)
3294           (setq subject (gnus-simplify-subject-re subject))
3295           (if (> (length subject) gnus-summary-gather-subject-limit)
3296               (substring subject 0 gnus-summary-gather-subject-limit)
3297             subject))
3298          ;; Fuzzily simplify it.
3299          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3300           (gnus-simplify-subject-fuzzy subject))
3301          ;; Just remove the leading "Re:".
3302          (t
3303           (gnus-simplify-subject-re subject))))
3304
3305   (if (and gnus-summary-gather-exclude-subject
3306            (string-match gnus-summary-gather-exclude-subject subject))
3307       nil                         ; This article shouldn't be gathered
3308     subject))
3309
3310 (defun gnus-summary-simplify-subject-query ()
3311   "Query where the respool algorithm would put this article."
3312   (interactive)
3313   (gnus-summary-select-article)
3314   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3315
3316 (defun gnus-gather-threads-by-subject (threads)
3317   "Gather threads by looking at Subject headers."
3318   (if (not gnus-summary-make-false-root)
3319       threads
3320     (let ((hashtb (gnus-make-hashtable 1024))
3321           (prev threads)
3322           (result threads)
3323           subject hthread whole-subject)
3324       (while threads
3325         (setq subject (gnus-general-simplify-subject
3326                        (setq whole-subject (mail-header-subject
3327                                             (caar threads)))))
3328         (when subject
3329           (if (setq hthread (gnus-gethash subject hashtb))
3330               (progn
3331                 ;; We enter a dummy root into the thread, if we
3332                 ;; haven't done that already.
3333                 (unless (stringp (caar hthread))
3334                   (setcar hthread (list whole-subject (car hthread))))
3335                 ;; We add this new gathered thread to this gathered
3336                 ;; thread.
3337                 (setcdr (car hthread)
3338                         (nconc (cdar hthread) (list (car threads))))
3339                 ;; Remove it from the list of threads.
3340                 (setcdr prev (cdr threads))
3341                 (setq threads prev))
3342             ;; Enter this thread into the hash table.
3343             (gnus-sethash subject threads hashtb)))
3344         (setq prev threads)
3345         (setq threads (cdr threads)))
3346       result)))
3347
3348 (defun gnus-gather-threads-by-references (threads)
3349   "Gather threads by looking at References headers."
3350   (let ((idhashtb (gnus-make-hashtable 1024))
3351         (thhashtb (gnus-make-hashtable 1024))
3352         (prev threads)
3353         (result threads)
3354         ids references id gthread gid entered ref)
3355     (while threads
3356       (when (setq references (mail-header-references (caar threads)))
3357         (setq id (mail-header-id (caar threads))
3358               ids (gnus-split-references references)
3359               entered nil)
3360         (while (setq ref (pop ids))
3361           (setq ids (delete ref ids))
3362           (if (not (setq gid (gnus-gethash ref idhashtb)))
3363               (progn
3364                 (gnus-sethash ref id idhashtb)
3365                 (gnus-sethash id threads thhashtb))
3366             (setq gthread (gnus-gethash gid thhashtb))
3367             (unless entered
3368               ;; We enter a dummy root into the thread, if we
3369               ;; haven't done that already.
3370               (unless (stringp (caar gthread))
3371                 (setcar gthread (list (mail-header-subject (caar gthread))
3372                                       (car gthread))))
3373               ;; We add this new gathered thread to this gathered
3374               ;; thread.
3375               (setcdr (car gthread)
3376                       (nconc (cdar gthread) (list (car threads)))))
3377             ;; Add it into the thread hash table.
3378             (gnus-sethash id gthread thhashtb)
3379             (setq entered t)
3380             ;; Remove it from the list of threads.
3381             (setcdr prev (cdr threads))
3382             (setq threads prev))))
3383       (setq prev threads)
3384       (setq threads (cdr threads)))
3385     result))
3386
3387 (defun gnus-sort-gathered-threads (threads)
3388   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3389   (let ((result threads))
3390     (while threads
3391       (when (stringp (caar threads))
3392         (setcdr (car threads)
3393                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3394       (setq threads (cdr threads)))
3395     result))
3396
3397 (defun gnus-thread-loop-p (root thread)
3398   "Say whether ROOT is in THREAD."
3399   (let ((stack (list thread))
3400         (infloop 0)
3401         th)
3402     (while (setq thread (pop stack))
3403       (setq th (cdr thread))
3404       (while (and th
3405                   (not (eq (caar th) root)))
3406         (pop th))
3407       (if th
3408           ;; We have found a loop.
3409           (let (ref-dep)
3410             (setcdr thread (delq (car th) (cdr thread)))
3411             (if (boundp (setq ref-dep (intern "none"
3412                                               gnus-newsgroup-dependencies)))
3413                 (setcdr (symbol-value ref-dep)
3414                         (nconc (cdr (symbol-value ref-dep))
3415                                (list (car th))))
3416               (set ref-dep (list nil (car th))))
3417             (setq infloop 1
3418                   stack nil))
3419         ;; Push all the subthreads onto the stack.
3420         (push (cdr thread) stack)))
3421     infloop))
3422
3423 (defun gnus-make-threads ()
3424   "Go through the dependency hashtb and find the roots.  Return all threads."
3425   (let (threads)
3426     (while (catch 'infloop
3427              (mapatoms
3428               (lambda (refs)
3429                 ;; Deal with self-referencing References loops.
3430                 (when (and (car (symbol-value refs))
3431                            (not (zerop
3432                                  (apply
3433                                   '+
3434                                   (mapcar
3435                                    (lambda (thread)
3436                                      (gnus-thread-loop-p
3437                                       (car (symbol-value refs)) thread))
3438                                    (cdr (symbol-value refs)))))))
3439                   (setq threads nil)
3440                   (throw 'infloop t))
3441                 (unless (car (symbol-value refs))
3442              ;; These threads do not refer back to any other articles,
3443                   ;; so they're roots.
3444                   (setq threads (append (cdr (symbol-value refs)) threads))))
3445               gnus-newsgroup-dependencies)))
3446     threads))
3447
3448 ;; Build the thread tree.
3449 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3450   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3451
3452 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3453 if it was already present.
3454
3455 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3456 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3457 Message-IDs will be renamed be renamed to a unique Message-ID before
3458 being entered.
3459
3460 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3461   (let* ((id (mail-header-id header))
3462          (id-dep (and id (intern id dependencies)))
3463          ref ref-dep ref-header)
3464     ;; Enter this `header' in the `dependencies' table.
3465     (cond
3466      ((not id-dep)
3467       (setq header nil))
3468      ;; The first two cases do the normal part: enter a new `header'
3469      ;; in the `dependencies' table.
3470      ((not (boundp id-dep))
3471       (set id-dep (list header)))
3472      ((null (car (symbol-value id-dep)))
3473       (setcar (symbol-value id-dep) header))
3474
3475      ;; From here the `header' was already present in the
3476      ;; `dependencies' table.
3477      (force-new
3478       ;; Overrides an existing entry;
3479       ;; just set the header part of the entry.
3480       (setcar (symbol-value id-dep) header))
3481
3482      ;; Renames the existing `header' to a unique Message-ID.
3483      ((not gnus-summary-ignore-duplicates)
3484       ;; An article with this Message-ID has already been seen.
3485       ;; We rename the Message-ID.
3486       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3487            (list header))
3488       (mail-header-set-id header id))
3489
3490      ;; The last case ignores an existing entry, except it adds any
3491      ;; additional Xrefs (in case the two articles came from different
3492      ;; servers.
3493      ;; Also sets `header' to `nil' meaning that the `dependencies'
3494      ;; table was *not* modified.
3495      (t
3496       (mail-header-set-xref
3497        (car (symbol-value id-dep))
3498        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3499                    "")
3500                (or (mail-header-xref header) "")))
3501       (setq header nil)))
3502
3503     (when header
3504       ;; First check if that we are not creating a References loop.
3505       (setq ref (gnus-parent-id (mail-header-references header)))
3506       (while (and ref
3507                   (setq ref-dep (intern-soft ref dependencies))
3508                   (boundp ref-dep)
3509                   (setq ref-header (car (symbol-value ref-dep))))
3510         (if (string= id ref)
3511             ;; Yuk!  This is a reference loop.  Make the article be a
3512             ;; root article.
3513             (progn
3514               (mail-header-set-references (car (symbol-value id-dep)) "none")
3515               (setq ref nil))
3516           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3517       (setq ref (gnus-parent-id (mail-header-references header)))
3518       (setq ref-dep (intern (or ref "none") dependencies))
3519       (if (boundp ref-dep)
3520           (setcdr (symbol-value ref-dep)
3521                   (nconc (cdr (symbol-value ref-dep))
3522                          (list (symbol-value id-dep))))
3523         (set ref-dep (list nil (symbol-value id-dep)))))
3524     header))
3525
3526 (defun gnus-build-sparse-threads ()
3527   (let ((headers gnus-newsgroup-headers)
3528         (mail-parse-charset gnus-newsgroup-charset)
3529         (gnus-summary-ignore-duplicates t)
3530         header references generation relations
3531         subject child end new-child date)
3532     ;; First we create an alist of generations/relations, where
3533     ;; generations is how much we trust the relation, and the relation
3534     ;; is parent/child.
3535     (gnus-message 7 "Making sparse threads...")
3536     (save-excursion
3537       (nnheader-set-temp-buffer " *gnus sparse threads*")
3538       (while (setq header (pop headers))
3539         (when (and (setq references (mail-header-references header))
3540                    (not (string= references "")))
3541           (insert references)
3542           (setq child (mail-header-id header)
3543                 subject (mail-header-subject header)
3544                 date (mail-header-date header)
3545                 generation 0)
3546           (while (search-backward ">" nil t)
3547             (setq end (1+ (point)))
3548             (when (search-backward "<" nil t)
3549               (setq new-child (buffer-substring (point) end))
3550               (push (list (incf generation)
3551                           child (setq child new-child)
3552                           subject date)
3553                     relations)))
3554           (when child
3555             (push (list (1+ generation) child nil subject) relations))
3556           (erase-buffer)))
3557       (kill-buffer (current-buffer)))
3558     ;; Sort over trustworthiness.
3559     (mapcar
3560      (lambda (relation)
3561        (when (gnus-dependencies-add-header
3562               (make-full-mail-header
3563                gnus-reffed-article-number
3564                (nth 3 relation) "" (or (nth 4 relation) "")
3565                (nth 1 relation)
3566                (or (nth 2 relation) "") 0 0 "")
3567               gnus-newsgroup-dependencies nil)
3568          (push gnus-reffed-article-number gnus-newsgroup-limit)
3569          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3570          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3571                gnus-newsgroup-reads)
3572          (decf gnus-reffed-article-number)))
3573      (sort relations 'car-less-than-car))
3574     (gnus-message 7 "Making sparse threads...done")))
3575
3576 (defun gnus-build-old-threads ()
3577   ;; Look at all the articles that refer back to old articles, and
3578   ;; fetch the headers for the articles that aren't there.  This will
3579   ;; build complete threads - if the roots haven't been expired by the
3580   ;; server, that is.
3581   (let ((mail-parse-charset gnus-newsgroup-charset)
3582         id heads)
3583     (mapatoms
3584      (lambda (refs)
3585        (when (not (car (symbol-value refs)))
3586          (setq heads (cdr (symbol-value refs)))
3587          (while heads
3588            (if (memq (mail-header-number (caar heads))
3589                      gnus-newsgroup-dormant)
3590                (setq heads (cdr heads))
3591              (setq id (symbol-name refs))
3592              (while (and (setq id (gnus-build-get-header id))
3593                          (not (car (gnus-id-to-thread id)))))
3594              (setq heads nil)))))
3595      gnus-newsgroup-dependencies)))
3596
3597 ;; This function has to be called with point after the article number
3598 ;; on the beginning of the line.
3599 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3600   (let ((eol (gnus-point-at-eol))
3601         (buffer (current-buffer))
3602         header)
3603
3604     ;; overview: [num subject from date id refs chars lines misc]
3605     (unwind-protect
3606         (progn
3607           (narrow-to-region (point) eol)
3608           (unless (eobp)
3609             (forward-char))
3610
3611           (setq header
3612                 (make-full-mail-header
3613                  number                 ; number
3614                  (funcall gnus-decode-encoded-word-function
3615                           (nnheader-nov-field)) ; subject
3616                  (funcall gnus-decode-encoded-word-function
3617                           (nnheader-nov-field)) ; from
3618                  (nnheader-nov-field)   ; date
3619                  (nnheader-nov-read-message-id) ; id
3620                  (nnheader-nov-field)   ; refs
3621                  (nnheader-nov-read-integer) ; chars
3622                  (nnheader-nov-read-integer) ; lines
3623                  (unless (eobp)
3624                    (if (looking-at "Xref: ")
3625                        (goto-char (match-end 0)))
3626                    (nnheader-nov-field)) ; Xref
3627                  (nnheader-nov-parse-extra)))) ; extra
3628
3629       (widen))
3630
3631     (when gnus-alter-header-function
3632       (funcall gnus-alter-header-function header))
3633     (gnus-dependencies-add-header header dependencies force-new)))
3634
3635 (defun gnus-build-get-header (id)
3636   "Look through the buffer of NOV lines and find the header to ID.
3637 Enter this line into the dependencies hash table, and return
3638 the id of the parent article (if any)."
3639   (let ((deps gnus-newsgroup-dependencies)
3640         found header)
3641     (prog1
3642         (save-excursion
3643           (set-buffer nntp-server-buffer)
3644           (let ((case-fold-search nil))
3645             (goto-char (point-min))
3646             (while (and (not found)
3647                         (search-forward id nil t))
3648               (beginning-of-line)
3649               (setq found (looking-at
3650                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3651                                    (regexp-quote id))))
3652               (or found (beginning-of-line 2)))
3653             (when found
3654               (beginning-of-line)
3655               (and
3656                (setq header (gnus-nov-parse-line
3657                              (read (current-buffer)) deps))
3658                (gnus-parent-id (mail-header-references header))))))
3659       (when header
3660         (let ((number (mail-header-number header)))
3661           (push number gnus-newsgroup-limit)
3662           (push header gnus-newsgroup-headers)
3663           (if (memq number gnus-newsgroup-unselected)
3664               (progn
3665                 (push number gnus-newsgroup-unreads)
3666                 (setq gnus-newsgroup-unselected
3667                       (delq number gnus-newsgroup-unselected)))
3668             (push number gnus-newsgroup-ancient)))))))
3669
3670 (defun gnus-build-all-threads ()
3671   "Read all the headers."
3672   (let ((gnus-summary-ignore-duplicates t)
3673         (mail-parse-charset gnus-newsgroup-charset)
3674         (dependencies gnus-newsgroup-dependencies)
3675         header article)
3676     (save-excursion
3677       (set-buffer nntp-server-buffer)
3678       (let ((case-fold-search nil))
3679         (goto-char (point-min))
3680         (while (not (eobp))
3681           (ignore-errors
3682             (setq article (read (current-buffer))
3683                   header (gnus-nov-parse-line article dependencies)))
3684           (when header
3685             (save-excursion
3686               (set-buffer gnus-summary-buffer)
3687               (push header gnus-newsgroup-headers)
3688               (if (memq (setq article (mail-header-number header))
3689                         gnus-newsgroup-unselected)
3690                   (progn
3691                     (push article gnus-newsgroup-unreads)
3692                     (setq gnus-newsgroup-unselected
3693                           (delq article gnus-newsgroup-unselected)))
3694                 (push article gnus-newsgroup-ancient)))
3695             (forward-line 1)))))))
3696
3697 (defun gnus-summary-update-article-line (article header)
3698   "Update the line for ARTICLE using HEADERS."
3699   (let* ((id (mail-header-id header))
3700          (thread (gnus-id-to-thread id)))
3701     (unless thread
3702       (error "Article in no thread"))
3703     ;; Update the thread.
3704     (setcar thread header)
3705     (gnus-summary-goto-subject article)
3706     (let* ((datal (gnus-data-find-list article))
3707            (data (car datal))
3708            (length (when (cdr datal)
3709                      (- (gnus-data-pos data)
3710                         (gnus-data-pos (cadr datal)))))
3711            (buffer-read-only nil)
3712            (level (gnus-summary-thread-level)))
3713       (gnus-delete-line)
3714       (gnus-summary-insert-line
3715        header level nil (gnus-article-mark article)
3716        (memq article gnus-newsgroup-replied)
3717        (memq article gnus-newsgroup-expirable)
3718        ;; Only insert the Subject string when it's different
3719        ;; from the previous Subject string.
3720        (if (and
3721             gnus-show-threads
3722             (gnus-subject-equal
3723              (condition-case ()
3724                  (mail-header-subject
3725                   (gnus-data-header
3726                    (cadr
3727                     (gnus-data-find-list
3728                      article
3729                      (gnus-data-list t)))))
3730                ;; Error on the side of excessive subjects.
3731                (error ""))
3732              (mail-header-subject header)))
3733            ""
3734          (mail-header-subject header))
3735        nil (cdr (assq article gnus-newsgroup-scored))
3736        (memq article gnus-newsgroup-processable))
3737       (when length
3738         (gnus-data-update-list
3739          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3740
3741 (defun gnus-summary-update-article (article &optional iheader)
3742   "Update ARTICLE in the summary buffer."
3743   (set-buffer gnus-summary-buffer)
3744   (let* ((header (gnus-summary-article-header article))
3745          (id (mail-header-id header))
3746          (data (gnus-data-find article))
3747          (thread (gnus-id-to-thread id))
3748          (references (mail-header-references header))
3749          (parent
3750           (gnus-id-to-thread
3751            (or (gnus-parent-id
3752                 (when (and references
3753                            (not (equal "" references)))
3754                   references))
3755                "none")))
3756          (buffer-read-only nil)
3757          (old (car thread)))
3758     (when thread
3759       (unless iheader
3760         (setcar thread nil)
3761         (when parent
3762           (delq thread parent)))
3763       (if (gnus-summary-insert-subject id header)
3764        ;; Set the (possibly) new article number in the data structure.
3765           (gnus-data-set-number data (gnus-id-to-article id))
3766         (setcar thread old)
3767         nil))))
3768
3769 (defun gnus-rebuild-thread (id &optional line)
3770   "Rebuild the thread containing ID.
3771 If LINE, insert the rebuilt thread starting on line LINE."
3772   (let ((buffer-read-only nil)
3773         old-pos current thread data)
3774     (if (not gnus-show-threads)
3775         (setq thread (list (car (gnus-id-to-thread id))))
3776       ;; Get the thread this article is part of.
3777       (setq thread (gnus-remove-thread id)))
3778     (setq old-pos (gnus-point-at-bol))
3779     (setq current (save-excursion
3780                     (and (re-search-backward "[\r\n]" nil t)
3781                          (gnus-summary-article-number))))
3782     ;; If this is a gathered thread, we have to go some re-gathering.
3783     (when (stringp (car thread))
3784       (let ((subject (car thread))
3785             roots thr)
3786         (setq thread (cdr thread))
3787         (while thread
3788           (unless (memq (setq thr (gnus-id-to-thread
3789                                    (gnus-root-id
3790                                     (mail-header-id (caar thread)))))
3791                         roots)
3792             (push thr roots))
3793           (setq thread (cdr thread)))
3794         ;; We now have all (unique) roots.
3795         (if (= (length roots) 1)
3796             ;; All the loose roots are now one solid root.
3797             (setq thread (car roots))
3798           (setq thread (cons subject (gnus-sort-threads roots))))))
3799     (let (threads)
3800       ;; We then insert this thread into the summary buffer.
3801       (when line
3802         (goto-char (point-min))
3803         (forward-line (1- line)))
3804       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3805         (if gnus-show-threads
3806             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3807           (gnus-summary-prepare-unthreaded thread))
3808         (setq data (nreverse gnus-newsgroup-data))
3809         (setq threads gnus-newsgroup-threads))
3810       ;; We splice the new data into the data structure.
3811       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3812       ;;!!! then we want to insert at the beginning of the buffer.
3813       ;;!!! That happens to be true with Gnus now, but that may
3814       ;;!!! change in the future.  Perhaps.
3815       (gnus-data-enter-list
3816        (if line nil current) data (- (point) old-pos))
3817       (setq gnus-newsgroup-threads
3818             (nconc threads gnus-newsgroup-threads))
3819       (gnus-data-compute-positions))))
3820
3821 (defun gnus-number-to-header (number)
3822   "Return the header for article NUMBER."
3823   (let ((headers gnus-newsgroup-headers))
3824     (while (and headers
3825                 (not (= number (mail-header-number (car headers)))))
3826       (pop headers))
3827     (when headers
3828       (car headers))))
3829
3830 (defun gnus-parent-headers (in-headers &optional generation)
3831   "Return the headers of the GENERATIONeth parent of HEADERS."
3832   (unless generation
3833     (setq generation 1))
3834   (let ((parent t)
3835         (headers in-headers)
3836         references)
3837     (while (and parent
3838                 (not (zerop generation))
3839                 (setq references (mail-header-references headers)))
3840       (setq headers (if (and references
3841                              (setq parent (gnus-parent-id references)))
3842                         (car (gnus-id-to-thread parent))
3843                       nil))
3844       (decf generation))
3845     (and (not (eq headers in-headers))
3846          headers)))
3847
3848 (defun gnus-id-to-thread (id)
3849   "Return the (sub-)thread where ID appears."
3850   (gnus-gethash id gnus-newsgroup-dependencies))
3851
3852 (defun gnus-id-to-article (id)
3853   "Return the article number of ID."
3854   (let ((thread (gnus-id-to-thread id)))
3855     (when (and thread
3856                (car thread))
3857       (mail-header-number (car thread)))))
3858
3859 (defun gnus-id-to-header (id)
3860   "Return the article headers of ID."
3861   (car (gnus-id-to-thread id)))
3862
3863 (defun gnus-article-displayed-root-p (article)
3864   "Say whether ARTICLE is a root(ish) article."
3865   (let ((level (gnus-summary-thread-level article))
3866         (refs (mail-header-references  (gnus-summary-article-header article)))
3867         particle)
3868     (cond
3869      ((null level) nil)
3870      ((zerop level) t)
3871      ((null refs) t)
3872      ((null (gnus-parent-id refs)) t)
3873      ((and (= 1 level)
3874            (null (setq particle (gnus-id-to-article
3875                                  (gnus-parent-id refs))))
3876            (null (gnus-summary-thread-level particle)))))))
3877
3878 (defun gnus-root-id (id)
3879   "Return the id of the root of the thread where ID appears."
3880   (let (last-id prev)
3881     (while (and id (setq prev (car (gnus-id-to-thread id))))
3882       (setq last-id id
3883             id (gnus-parent-id (mail-header-references prev))))
3884     last-id))
3885
3886 (defun gnus-articles-in-thread (thread)
3887   "Return the list of articles in THREAD."
3888   (cons (mail-header-number (car thread))
3889         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3890
3891 (defun gnus-remove-thread (id &optional dont-remove)
3892   "Remove the thread that has ID in it."
3893   (let (headers thread last-id)
3894     ;; First go up in this thread until we find the root.
3895     (setq last-id (gnus-root-id id)
3896           headers (message-flatten-list (gnus-id-to-thread last-id)))
3897     ;; We have now found the real root of this thread.  It might have
3898     ;; been gathered into some loose thread, so we have to search
3899     ;; through the threads to find the thread we wanted.
3900     (let ((threads gnus-newsgroup-threads)
3901           sub)
3902       (while threads
3903         (setq sub (car threads))
3904         (if (stringp (car sub))
3905             ;; This is a gathered thread, so we look at the roots
3906             ;; below it to find whether this article is in this
3907             ;; gathered root.
3908             (progn
3909               (setq sub (cdr sub))
3910               (while sub
3911                 (when (member (caar sub) headers)
3912                   (setq thread (car threads)
3913                         threads nil
3914                         sub nil))
3915                 (setq sub (cdr sub))))
3916           ;; It's an ordinary thread, so we check it.
3917           (when (eq (car sub) (car headers))
3918             (setq thread sub
3919                   threads nil)))
3920         (setq threads (cdr threads)))
3921       ;; If this article is in no thread, then it's a root.
3922       (if thread
3923           (unless dont-remove
3924             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3925         (setq thread (gnus-id-to-thread last-id)))
3926       (when thread
3927         (prog1
3928             thread                      ; We return this thread.
3929           (unless dont-remove
3930             (if (stringp (car thread))
3931                 (progn
3932                   ;; If we use dummy roots, then we have to remove the
3933                   ;; dummy root as well.
3934                   (when (eq gnus-summary-make-false-root 'dummy)
3935                     ;; We go to the dummy root by going to
3936                     ;; the first sub-"thread", and then one line up.
3937                     (gnus-summary-goto-article
3938                      (mail-header-number (caadr thread)))
3939                     (forward-line -1)
3940                     (gnus-delete-line)
3941                     (gnus-data-compute-positions))
3942                   (setq thread (cdr thread))
3943                   (while thread
3944                     (gnus-remove-thread-1 (car thread))
3945                     (setq thread (cdr thread))))
3946               (gnus-remove-thread-1 thread))))))))
3947
3948 (defun gnus-remove-thread-1 (thread)
3949   "Remove the thread THREAD recursively."
3950   (let ((number (mail-header-number (pop thread)))
3951         d)
3952     (setq thread (reverse thread))
3953     (while thread
3954       (gnus-remove-thread-1 (pop thread)))
3955     (when (setq d (gnus-data-find number))
3956       (goto-char (gnus-data-pos d))
3957       (gnus-summary-show-thread)
3958       (gnus-data-remove
3959        number
3960        (- (gnus-point-at-bol)
3961           (prog1
3962               (1+ (gnus-point-at-eol))
3963             (gnus-delete-line)))))))
3964
3965 (defun gnus-sort-threads-1 (threads func)
3966   (sort (mapcar (lambda (thread)
3967                   (cons (car thread)
3968                         (and (cdr thread)
3969                              (gnus-sort-threads-1 (cdr thread) func))))
3970                 threads) func))
3971
3972 (defun gnus-sort-threads (threads)
3973   "Sort THREADS."
3974   (if (not gnus-thread-sort-functions)
3975       threads
3976     (gnus-message 8 "Sorting threads...")
3977     (prog1
3978         (gnus-sort-threads-1
3979          threads
3980          (gnus-make-sort-function gnus-thread-sort-functions))
3981       (gnus-message 8 "Sorting threads...done"))))
3982
3983 (defun gnus-sort-articles (articles)
3984   "Sort ARTICLES."
3985   (when gnus-article-sort-functions
3986     (gnus-message 7 "Sorting articles...")
3987     (prog1
3988         (setq gnus-newsgroup-headers
3989               (sort articles (gnus-make-sort-function
3990                               gnus-article-sort-functions)))
3991       (gnus-message 7 "Sorting articles...done"))))
3992
3993 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3994 (defmacro gnus-thread-header (thread)
3995   "Return header of first article in THREAD.
3996 Note that THREAD must never, ever be anything else than a variable -
3997 using some other form will lead to serious barfage."
3998   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3999   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4000   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4001         (vector thread) 2))
4002
4003 (defsubst gnus-article-sort-by-number (h1 h2)
4004   "Sort articles by article number."
4005   (< (mail-header-number h1)
4006      (mail-header-number h2)))
4007
4008 (defun gnus-thread-sort-by-number (h1 h2)
4009   "Sort threads by root article number."
4010   (gnus-article-sort-by-number
4011    (gnus-thread-header h1) (gnus-thread-header h2)))
4012
4013 (defsubst gnus-article-sort-by-lines (h1 h2)
4014   "Sort articles by article Lines header."
4015   (< (mail-header-lines h1)
4016      (mail-header-lines h2)))
4017
4018 (defun gnus-thread-sort-by-lines (h1 h2)
4019   "Sort threads by root article Lines header."
4020   (gnus-article-sort-by-lines
4021    (gnus-thread-header h1) (gnus-thread-header h2)))
4022
4023 (defsubst gnus-article-sort-by-chars (h1 h2)
4024   "Sort articles by octet length."
4025   (< (mail-header-chars h1)
4026      (mail-header-chars h2)))
4027
4028 (defun gnus-thread-sort-by-chars (h1 h2)
4029   "Sort threads by root article octet length."
4030   (gnus-article-sort-by-chars
4031    (gnus-thread-header h1) (gnus-thread-header h2)))
4032
4033 (defsubst gnus-article-sort-by-author (h1 h2)
4034   "Sort articles by root author."
4035   (string-lessp
4036    (let ((extract (funcall
4037                    gnus-extract-address-components
4038                    (mail-header-from h1))))
4039      (or (car extract) (cadr extract) ""))
4040    (let ((extract (funcall
4041                    gnus-extract-address-components
4042                    (mail-header-from h2))))
4043      (or (car extract) (cadr extract) ""))))
4044
4045 (defun gnus-thread-sort-by-author (h1 h2)
4046   "Sort threads by root author."
4047   (gnus-article-sort-by-author
4048    (gnus-thread-header h1)  (gnus-thread-header h2)))
4049
4050 (defsubst gnus-article-sort-by-subject (h1 h2)
4051   "Sort articles by root subject."
4052   (string-lessp
4053    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4054    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4055
4056 (defun gnus-thread-sort-by-subject (h1 h2)
4057   "Sort threads by root subject."
4058   (gnus-article-sort-by-subject
4059    (gnus-thread-header h1) (gnus-thread-header h2)))
4060
4061 (defsubst gnus-article-sort-by-date (h1 h2)
4062   "Sort articles by root article date."
4063   (time-less-p
4064    (gnus-date-get-time (mail-header-date h1))
4065    (gnus-date-get-time (mail-header-date h2))))
4066
4067 (defun gnus-thread-sort-by-date (h1 h2)
4068   "Sort threads by root article date."
4069   (gnus-article-sort-by-date
4070    (gnus-thread-header h1) (gnus-thread-header h2)))
4071
4072 (defsubst gnus-article-sort-by-score (h1 h2)
4073   "Sort articles by root article score.
4074 Unscored articles will be counted as having a score of zero."
4075   (> (or (cdr (assq (mail-header-number h1)
4076                     gnus-newsgroup-scored))
4077          gnus-summary-default-score 0)
4078      (or (cdr (assq (mail-header-number h2)
4079                     gnus-newsgroup-scored))
4080          gnus-summary-default-score 0)))
4081
4082 (defun gnus-thread-sort-by-score (h1 h2)
4083   "Sort threads by root article score."
4084   (gnus-article-sort-by-score
4085    (gnus-thread-header h1) (gnus-thread-header h2)))
4086
4087 (defun gnus-thread-sort-by-total-score (h1 h2)
4088   "Sort threads by the sum of all scores in the thread.
4089 Unscored articles will be counted as having a score of zero."
4090   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4091
4092 (defun gnus-thread-total-score (thread)
4093   ;; This function find the total score of THREAD.
4094   (cond ((null thread)
4095          0)
4096         ((consp thread)
4097          (if (stringp (car thread))
4098              (apply gnus-thread-score-function 0
4099                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4100            (gnus-thread-total-score-1 thread)))
4101         (t
4102          (gnus-thread-total-score-1 (list thread)))))
4103
4104 (defun gnus-thread-total-score-1 (root)
4105   ;; This function find the total score of the thread below ROOT.
4106   (setq root (car root))
4107   (apply gnus-thread-score-function
4108          (or (append
4109               (mapcar 'gnus-thread-total-score
4110                       (cdr (gnus-id-to-thread (mail-header-id root))))
4111               (when (> (mail-header-number root) 0)
4112                 (list (or (cdr (assq (mail-header-number root)
4113                                      gnus-newsgroup-scored))
4114                           gnus-summary-default-score 0))))
4115              (list gnus-summary-default-score)
4116              '(0))))
4117
4118 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4119 (defvar gnus-tmp-prev-subject nil)
4120 (defvar gnus-tmp-false-parent nil)
4121 (defvar gnus-tmp-root-expunged nil)
4122 (defvar gnus-tmp-dummy-line nil)
4123
4124 (eval-when-compile (defvar gnus-tmp-header))
4125 (defun gnus-extra-header (type &optional header)
4126   "Return the extra header of TYPE."
4127   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4128       ""))
4129
4130 (defvar gnus-tmp-thread-tree-header-string "")
4131
4132 (defvar gnus-sum-thread-tree-root "> "
4133   "With %B spec, used for the root of a thread.
4134 If nil, use subject instead.")
4135 (defvar gnus-sum-thread-tree-single-indent ""
4136   "With %B spec, used for a thread with just one message.
4137 If nil, use subject instead.")
4138 (defvar gnus-sum-thread-tree-vertical "| "
4139   "With %B spec, used for drawing a vertical line.")
4140 (defvar gnus-sum-thread-tree-indent "  "
4141   "With %B spec, used for indenting.")
4142 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4143   "With %B spec, used for a leaf with brothers.")
4144 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4145   "With %B spec, used for a leaf without brothers.")
4146
4147 (defun gnus-summary-prepare-threads (threads)
4148   "Prepare summary buffer from THREADS and indentation LEVEL.
4149 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4150 or a straight list of headers."
4151   (gnus-message 7 "Generating summary...")
4152
4153   (setq gnus-newsgroup-threads threads)
4154   (beginning-of-line)
4155
4156   (let ((gnus-tmp-level 0)
4157         (default-score (or gnus-summary-default-score 0))
4158         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4159         thread number subject stack state gnus-tmp-gathered beg-match
4160         new-roots gnus-tmp-new-adopts thread-end
4161         gnus-tmp-header gnus-tmp-unread
4162         gnus-tmp-replied gnus-tmp-subject-or-nil
4163         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4164         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4165         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4166         tree-stack)
4167
4168     (setq gnus-tmp-prev-subject nil)
4169
4170     (if (vectorp (car threads))
4171         ;; If this is a straight (sic) list of headers, then a
4172         ;; threaded summary display isn't required, so we just create
4173         ;; an unthreaded one.
4174         (gnus-summary-prepare-unthreaded threads)
4175
4176       ;; Do the threaded display.
4177
4178       (while (or threads stack gnus-tmp-new-adopts new-roots)
4179
4180         (if (and (= gnus-tmp-level 0)
4181                  (or (not stack)
4182                      (= (caar stack) 0))
4183                  (not gnus-tmp-false-parent)
4184                  (or gnus-tmp-new-adopts new-roots))
4185             (if gnus-tmp-new-adopts
4186                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4187                       thread (list (car gnus-tmp-new-adopts))
4188                       gnus-tmp-header (caar thread)
4189                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4190               (when new-roots
4191                 (setq thread (list (car new-roots))
4192                       gnus-tmp-header (caar thread)
4193                       new-roots (cdr new-roots))))
4194
4195           (if threads
4196               ;; If there are some threads, we do them before the
4197               ;; threads on the stack.
4198               (setq thread threads
4199                     gnus-tmp-header (caar thread))
4200             ;; There were no current threads, so we pop something off
4201             ;; the stack.
4202             (setq state (car stack)
4203                   gnus-tmp-level (car state)
4204                   tree-stack (cadr state)
4205                   thread (caddr state)
4206                   stack (cdr stack)
4207                   gnus-tmp-header (caar thread))))
4208
4209         (setq gnus-tmp-false-parent nil)
4210         (setq gnus-tmp-root-expunged nil)
4211         (setq thread-end nil)
4212
4213         (if (stringp gnus-tmp-header)
4214             ;; The header is a dummy root.
4215             (cond
4216              ((eq gnus-summary-make-false-root 'adopt)
4217               ;; We let the first article adopt the rest.
4218               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4219                                                (cddar thread)))
4220               (setq gnus-tmp-gathered
4221                     (nconc (mapcar
4222                             (lambda (h) (mail-header-number (car h)))
4223                             (cddar thread))
4224                            gnus-tmp-gathered))
4225               (setq thread (cons (list (caar thread)
4226                                        (cadar thread))
4227                                  (cdr thread)))
4228               (setq gnus-tmp-level -1
4229                     gnus-tmp-false-parent t))
4230              ((eq gnus-summary-make-false-root 'empty)
4231               ;; We print adopted articles with empty subject fields.
4232               (setq gnus-tmp-gathered
4233                     (nconc (mapcar
4234                             (lambda (h) (mail-header-number (car h)))
4235                             (cddar thread))
4236                            gnus-tmp-gathered))
4237               (setq gnus-tmp-level -1))
4238              ((eq gnus-summary-make-false-root 'dummy)
4239               ;; We remember that we probably want to output a dummy
4240               ;; root.
4241               (setq gnus-tmp-dummy-line gnus-tmp-header)
4242               (setq gnus-tmp-prev-subject gnus-tmp-header))
4243              (t
4244               ;; We do not make a root for the gathered
4245               ;; sub-threads at all.
4246               (setq gnus-tmp-level -1)))
4247
4248           (setq number (mail-header-number gnus-tmp-header)
4249                 subject (mail-header-subject gnus-tmp-header))
4250
4251           (cond
4252            ;; If the thread has changed subject, we might want to make
4253            ;; this subthread into a root.
4254            ((and (null gnus-thread-ignore-subject)
4255                  (not (zerop gnus-tmp-level))
4256                  gnus-tmp-prev-subject
4257                  (not (inline
4258                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4259             (setq new-roots (nconc new-roots (list (car thread)))
4260                   thread-end t
4261                   gnus-tmp-header nil))
4262            ;; If the article lies outside the current limit,
4263            ;; then we do not display it.
4264            ((not (memq number gnus-newsgroup-limit))
4265             (setq gnus-tmp-gathered
4266                   (nconc (mapcar
4267                           (lambda (h) (mail-header-number (car h)))
4268                           (cdar thread))
4269                          gnus-tmp-gathered))
4270             (setq gnus-tmp-new-adopts (if (cdar thread)
4271                                           (append gnus-tmp-new-adopts
4272                                                   (cdar thread))
4273                                         gnus-tmp-new-adopts)
4274                   thread-end t
4275                   gnus-tmp-header nil)
4276             (when (zerop gnus-tmp-level)
4277               (setq gnus-tmp-root-expunged t)))
4278            ;; Perhaps this article is to be marked as read?
4279            ((and gnus-summary-mark-below
4280                  (< (or (cdr (assq number gnus-newsgroup-scored))
4281                         default-score)
4282                     gnus-summary-mark-below)
4283                  ;; Don't touch sparse articles.
4284                  (not (gnus-summary-article-sparse-p number))
4285                  (not (gnus-summary-article-ancient-p number)))
4286             (setq gnus-newsgroup-unreads
4287                   (delq number gnus-newsgroup-unreads))
4288             (if gnus-newsgroup-auto-expire
4289                 (push number gnus-newsgroup-expirable)
4290               (push (cons number gnus-low-score-mark)
4291                     gnus-newsgroup-reads))))
4292
4293           (when gnus-tmp-header
4294             ;; We may have an old dummy line to output before this
4295             ;; article.
4296             (when (and gnus-tmp-dummy-line
4297                        (gnus-subject-equal
4298                         gnus-tmp-dummy-line
4299                         (mail-header-subject gnus-tmp-header)))
4300               (gnus-summary-insert-dummy-line
4301                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4302               (setq gnus-tmp-dummy-line nil))
4303
4304             ;; Compute the mark.
4305             (setq gnus-tmp-unread (gnus-article-mark number))
4306
4307             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4308                                   gnus-tmp-header gnus-tmp-level)
4309                   gnus-newsgroup-data)
4310
4311             ;; Actually insert the line.
4312             (setq
4313              gnus-tmp-subject-or-nil
4314              (cond
4315               ((and gnus-thread-ignore-subject
4316                     gnus-tmp-prev-subject
4317                     (not (inline (gnus-subject-equal
4318                                   gnus-tmp-prev-subject subject))))
4319                subject)
4320               ((zerop gnus-tmp-level)
4321                (if (and (eq gnus-summary-make-false-root 'empty)
4322                         (memq number gnus-tmp-gathered)
4323                         gnus-tmp-prev-subject
4324                         (inline (gnus-subject-equal
4325                                  gnus-tmp-prev-subject subject)))
4326                    gnus-summary-same-subject
4327                  subject))
4328               (t gnus-summary-same-subject)))
4329             (if (and (eq gnus-summary-make-false-root 'adopt)
4330                      (= gnus-tmp-level 1)
4331                      (memq number gnus-tmp-gathered))
4332                 (setq gnus-tmp-opening-bracket ?\<
4333                       gnus-tmp-closing-bracket ?\>)
4334               (setq gnus-tmp-opening-bracket ?\[
4335                     gnus-tmp-closing-bracket ?\]))
4336             (setq
4337              gnus-tmp-indentation
4338              (aref gnus-thread-indent-array gnus-tmp-level)
4339              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4340              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4341                                 gnus-summary-default-score 0)
4342              gnus-tmp-score-char
4343              (if (or (null gnus-summary-default-score)
4344                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4345                          gnus-summary-zcore-fuzz))
4346                  ?                      ;Whitespace
4347                (if (< gnus-tmp-score gnus-summary-default-score)
4348                    gnus-score-below-mark gnus-score-over-mark))
4349              gnus-tmp-replied
4350              (cond ((memq number gnus-newsgroup-processable)
4351                     gnus-process-mark)
4352                    ((memq number gnus-newsgroup-cached)
4353                     gnus-cached-mark)
4354                    ((memq number gnus-newsgroup-replied)
4355                     gnus-replied-mark)
4356                    ((memq number gnus-newsgroup-forwarded)
4357                     gnus-forwarded-mark)
4358                    ((memq number gnus-newsgroup-saved)
4359                     gnus-saved-mark)
4360                    ((memq number gnus-newsgroup-recent)
4361                     gnus-recent-mark)
4362                    ((memq number gnus-newsgroup-unseen)
4363                     gnus-unseen-mark)
4364                    (t gnus-no-mark))
4365              gnus-tmp-from (mail-header-from gnus-tmp-header)
4366              gnus-tmp-name
4367              (cond
4368               ((string-match "<[^>]+> *$" gnus-tmp-from)
4369                (setq beg-match (match-beginning 0))
4370                (or (and (string-match "^\".+\"" gnus-tmp-from)
4371                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4372                    (substring gnus-tmp-from 0 beg-match)))
4373               ((string-match "(.+)" gnus-tmp-from)
4374                (substring gnus-tmp-from
4375                           (1+ (match-beginning 0)) (1- (match-end 0))))
4376               (t gnus-tmp-from))
4377              gnus-tmp-thread-tree-header-string
4378              (cond 
4379               ((not gnus-show-threads) "")
4380               ((zerop gnus-tmp-level)
4381                (if (cdar thread) 
4382                    (or gnus-sum-thread-tree-root subject)
4383                  (or gnus-sum-thread-tree-single-indent subject)))
4384               (t
4385                (concat (apply 'concat
4386                               (mapcar (lambda (item) 
4387                                         (if (= item 1) 
4388                                             gnus-sum-thread-tree-vertical
4389                                           gnus-sum-thread-tree-indent))
4390                                       (cdr (reverse tree-stack))))
4391                        (if (nth 1 thread) 
4392                            gnus-sum-thread-tree-leaf-with-other
4393                          gnus-sum-thread-tree-single-leaf)))))
4394             (when (string= gnus-tmp-name "")
4395               (setq gnus-tmp-name gnus-tmp-from))
4396             (unless (numberp gnus-tmp-lines)
4397               (setq gnus-tmp-lines -1))
4398             (if (= gnus-tmp-lines -1)
4399                 (setq gnus-tmp-lines "?")
4400               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4401             (gnus-put-text-property
4402              (point)
4403              (progn (eval gnus-summary-line-format-spec) (point))
4404              'gnus-number number)
4405             (when gnus-visual-p
4406               (forward-line -1)
4407               (gnus-run-hooks 'gnus-summary-update-hook)
4408               (forward-line 1))
4409
4410             (setq gnus-tmp-prev-subject subject)))
4411
4412         (when (nth 1 thread)
4413           (push (list (max 0 gnus-tmp-level)
4414                       (copy-list tree-stack)
4415                       (nthcdr 1 thread))
4416                 stack))
4417         (push (if (nth 1 thread) 1 0) tree-stack)
4418         (incf gnus-tmp-level)
4419         (setq threads (if thread-end nil (cdar thread)))
4420         (unless threads
4421           (setq gnus-tmp-level 0)))))
4422   (gnus-message 7 "Generating summary...done"))
4423
4424 (defun gnus-summary-prepare-unthreaded (headers)
4425   "Generate an unthreaded summary buffer based on HEADERS."
4426   (let (header number mark)
4427
4428     (beginning-of-line)
4429
4430     (while headers
4431       ;; We may have to root out some bad articles...
4432       (when (memq (setq number (mail-header-number
4433                                 (setq header (pop headers))))
4434                   gnus-newsgroup-limit)
4435         ;; Mark article as read when it has a low score.
4436         (when (and gnus-summary-mark-below
4437                    (< (or (cdr (assq number gnus-newsgroup-scored))
4438                           gnus-summary-default-score 0)
4439                       gnus-summary-mark-below)
4440                    (not (gnus-summary-article-ancient-p number)))
4441           (setq gnus-newsgroup-unreads
4442                 (delq number gnus-newsgroup-unreads))
4443           (if gnus-newsgroup-auto-expire
4444               (push number gnus-newsgroup-expirable)
4445             (push (cons number gnus-low-score-mark)
4446                   gnus-newsgroup-reads)))
4447
4448         (setq mark (gnus-article-mark number))
4449         (push (gnus-data-make number mark (1+ (point)) header 0)
4450               gnus-newsgroup-data)
4451         (gnus-summary-insert-line
4452          header 0 number
4453          mark (memq number gnus-newsgroup-replied)
4454          (memq number gnus-newsgroup-expirable)
4455          (mail-header-subject header) nil
4456          (cdr (assq number gnus-newsgroup-scored))
4457          (memq number gnus-newsgroup-processable))))))
4458
4459 (defun gnus-summary-remove-list-identifiers ()
4460   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4461   (let ((regexp (if (consp gnus-list-identifiers)
4462                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4463                   gnus-list-identifiers))
4464         changed subject)
4465     (when regexp
4466       (dolist (header gnus-newsgroup-headers)
4467         (setq subject (mail-header-subject header)
4468               changed nil)
4469         (while (string-match
4470                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4471                 subject)
4472           (setq subject
4473                 (concat (substring subject 0 (match-beginning 2))
4474                         (substring subject (match-end 0)))
4475                 changed t))
4476         (when (and changed
4477                    (string-match
4478                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4479           (setq subject
4480                 (concat (substring subject 0 (match-beginning 1))
4481                         (substring subject (match-end 1)))))
4482         (when changed
4483           (mail-header-set-subject header subject))))))
4484
4485 (defun gnus-fetch-headers (articles)
4486   "Fetch headers of ARTICLES."
4487   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4488     (gnus-message 5 "Fetching headers for %s..." name)
4489     (prog1
4490         (if (eq 'nov
4491                 (setq gnus-headers-retrieved-by
4492                       (gnus-retrieve-headers
4493                        articles gnus-newsgroup-name
4494                        ;; We might want to fetch old headers, but
4495                        ;; not if there is only 1 article.
4496                        (and (or (and
4497                                  (not (eq gnus-fetch-old-headers 'some))
4498                                  (not (numberp gnus-fetch-old-headers)))
4499                                 (> (length articles) 1))
4500                             gnus-fetch-old-headers))))
4501             (gnus-get-newsgroup-headers-xover
4502              articles nil nil gnus-newsgroup-name t)
4503           (gnus-get-newsgroup-headers))
4504       (gnus-message 5 "Fetching headers for %s...done" name))))
4505
4506 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4507   "Select newsgroup GROUP.
4508 If READ-ALL is non-nil, all articles in the group are selected.
4509 If SELECT-ARTICLES, only select those articles from GROUP."
4510   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4511          ;;!!! Dirty hack; should be removed.
4512          (gnus-summary-ignore-duplicates
4513           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4514               t
4515             gnus-summary-ignore-duplicates))
4516          (info (nth 2 entry))
4517          articles fetched-articles cached)
4518
4519     (unless (gnus-check-server
4520              (set (make-local-variable 'gnus-current-select-method)
4521                   (gnus-find-method-for-group group)))
4522       (error "Couldn't open server"))
4523
4524     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4525         (gnus-activate-group group)     ; Or we can activate it...
4526         (progn                          ; Or we bug out.
4527           (when (equal major-mode 'gnus-summary-mode)
4528             (kill-buffer (current-buffer)))
4529           (error "Couldn't activate group %s: %s"
4530                  group (gnus-status-message group))))
4531
4532     (unless (gnus-request-group group t)
4533       (when (equal major-mode 'gnus-summary-mode)
4534         (kill-buffer (current-buffer)))
4535       (error "Couldn't request group %s: %s"
4536              group (gnus-status-message group)))
4537
4538     (setq gnus-newsgroup-name group
4539           gnus-newsgroup-unselected nil
4540           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4541     
4542     (let ((display (gnus-group-find-parameter group 'display)))
4543       (setq gnus-newsgroup-display
4544             (cond
4545              ((eq display 'all)
4546               'gnus-not-ignore)
4547              ((arrayp display)
4548               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4549              (t
4550               nil))))
4551       
4552     (gnus-summary-setup-default-charset)
4553
4554     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4555     (when (gnus-virtual-group-p group)
4556       (setq cached gnus-newsgroup-cached))
4557
4558     (setq gnus-newsgroup-unreads
4559           (gnus-set-difference
4560            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4561            gnus-newsgroup-dormant))
4562
4563     (setq gnus-newsgroup-processable nil)
4564
4565     (gnus-update-read-articles group gnus-newsgroup-unreads)
4566
4567     ;; Adjust and set lists of article marks.
4568     (when info
4569       (gnus-adjust-marked-articles info))
4570     
4571     (if (setq articles select-articles)
4572         (setq gnus-newsgroup-unselected
4573               (gnus-sorted-intersection
4574                gnus-newsgroup-unreads
4575                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4576       (setq articles (gnus-articles-to-read group read-all)))
4577
4578     (cond
4579      ((null articles)
4580       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4581       'quit)
4582      ((eq articles 0) nil)
4583      (t
4584       ;; Init the dependencies hash table.
4585       (setq gnus-newsgroup-dependencies
4586             (gnus-make-hashtable (length articles)))
4587       (gnus-set-global-variables)
4588       ;; Retrieve the headers and read them in.
4589       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4590
4591       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4592       (when cached
4593         (setq gnus-newsgroup-cached cached))
4594
4595       ;; Suppress duplicates?
4596       (when gnus-suppress-duplicates
4597         (gnus-dup-suppress-articles))
4598
4599       ;; Set the initial limit.
4600       (setq gnus-newsgroup-limit (copy-sequence articles))
4601       ;; Remove canceled articles from the list of unread articles.
4602       (setq fetched-articles
4603             (mapcar (lambda (headers) (mail-header-number headers))
4604                     gnus-newsgroup-headers))
4605       (setq gnus-newsgroup-articles fetched-articles)
4606       (setq gnus-newsgroup-unreads
4607             (gnus-set-sorted-intersection
4608              gnus-newsgroup-unreads fetched-articles))
4609
4610       (let ((marks (assq 'seen (gnus-info-marks info))))
4611         ;; The `seen' marks are treated specially.
4612         (when (setq gnus-newsgroup-seen (cdr marks))
4613           (dolist (article gnus-newsgroup-articles)
4614             (unless (gnus-member-of-range
4615                      article gnus-newsgroup-seen)
4616               (push article gnus-newsgroup-unseen)))))
4617
4618       ;; Removed marked articles that do not exist.
4619       (gnus-update-missing-marks
4620        (gnus-sorted-complement fetched-articles articles))
4621       ;; We might want to build some more threads first.
4622       (when (and gnus-fetch-old-headers
4623                  (eq gnus-headers-retrieved-by 'nov))
4624         (if (eq gnus-fetch-old-headers 'invisible)
4625             (gnus-build-all-threads)
4626           (gnus-build-old-threads)))
4627       ;; Let the Gnus agent mark articles as read.
4628       (when gnus-agent
4629         (gnus-agent-get-undownloaded-list))
4630       ;; Remove list identifiers from subject
4631       (when gnus-list-identifiers
4632         (gnus-summary-remove-list-identifiers))
4633       ;; Check whether auto-expire is to be done in this group.
4634       (setq gnus-newsgroup-auto-expire
4635             (gnus-group-auto-expirable-p group))
4636       ;; Set up the article buffer now, if necessary.
4637       (unless gnus-single-article-buffer
4638         (gnus-article-setup-buffer))
4639       ;; First and last article in this newsgroup.
4640       (when gnus-newsgroup-headers
4641         (setq gnus-newsgroup-begin
4642               (mail-header-number (car gnus-newsgroup-headers))
4643               gnus-newsgroup-end
4644               (mail-header-number
4645                (gnus-last-element gnus-newsgroup-headers))))
4646       ;; GROUP is successfully selected.
4647       (or gnus-newsgroup-headers t)))))
4648
4649 (defun gnus-summary-display-make-predicate (display)
4650   (require 'gnus-agent)
4651   (when (= (length display) 1)
4652     (setq display (car display)))
4653   (unless gnus-summary-display-cache
4654     (dolist (elem (append (list (cons 'read 'read)
4655                                 (cons 'unseen 'unseen))
4656                           gnus-article-mark-lists))
4657       (push (cons (cdr elem)
4658                   (gnus-byte-compile
4659                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4660             gnus-summary-display-cache)))
4661   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4662     (gnus-get-predicate display)))
4663
4664 ;; Uses the dynamically bound `number' variable.
4665 (defvar number)
4666 (defun gnus-article-marked-p (type &optional article)
4667   (let ((article (or article number)))
4668     (cond
4669      ((eq type 'tick)
4670       (memq article gnus-newsgroup-marked))
4671      ((eq type 'unsend)
4672       (memq article gnus-newsgroup-unsendable))
4673      ((eq type 'undownload)
4674       (memq article gnus-newsgroup-undownloaded))
4675      ((eq type 'download)
4676       (memq article gnus-newsgroup-downloadable))
4677      ((eq type 'unread)
4678       (memq article gnus-newsgroup-unreads))
4679      ((eq type 'read)
4680       (memq article gnus-newsgroup-reads))
4681      ((eq type 'dormant)
4682       (memq article gnus-newsgroup-dormant) )
4683      ((eq type 'expire)
4684       (memq article gnus-newsgroup-expirable))
4685      ((eq type 'reply)
4686       (memq article gnus-newsgroup-replied))
4687      ((eq type 'killed)
4688       (memq article gnus-newsgroup-killed))
4689      ((eq type 'bookmark)
4690       (assq article gnus-newsgroup-bookmarks))
4691      ((eq type 'score)
4692       (assq article gnus-newsgroup-scored))
4693      ((eq type 'save)
4694       (memq article gnus-newsgroup-saved))
4695      ((eq type 'cache)
4696       (memq article gnus-newsgroup-cached))
4697      ((eq type 'forward)
4698       (memq article gnus-newsgroup-forwarded))
4699      ((eq type 'seen)
4700       (not (memq article gnus-newsgroup-unseen)))
4701      ((eq type 'recent)
4702       (memq article gnus-newsgroup-recent))
4703      (t t))))
4704
4705 (defun gnus-articles-to-read (group &optional read-all)
4706   "Find out what articles the user wants to read."
4707   (let* ((articles
4708           ;; Select all articles if `read-all' is non-nil, or if there
4709           ;; are no unread articles.
4710           (if (or read-all
4711                   (and (zerop (length gnus-newsgroup-marked))
4712                        (zerop (length gnus-newsgroup-unreads)))
4713                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4714               ;; We want to select the headers for all the articles in
4715               ;; the group, so we select either all the active
4716               ;; articles in the group, or (if that's nil), the
4717               ;; articles in the cache.
4718               (or
4719                (gnus-uncompress-range (gnus-active group))
4720                (gnus-cache-articles-in-group group))
4721             ;; Select only the "normal" subset of articles.
4722             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4723                           (copy-sequence gnus-newsgroup-unreads))
4724                   '<)))
4725          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4726          (scored (length scored-list))
4727          (number (length articles))
4728          (marked (+ (length gnus-newsgroup-marked)
4729                     (length gnus-newsgroup-dormant)))
4730          (select
4731           (cond
4732            ((numberp read-all)
4733             read-all)
4734            (t
4735             (condition-case ()
4736                 (cond
4737                  ((and (or (<= scored marked) (= scored number))
4738                        (numberp gnus-large-newsgroup)
4739                        (> number gnus-large-newsgroup))
4740                   (let ((input
4741                          (read-string
4742                           (format
4743                            "How many articles from %s (default %d): "
4744                            (gnus-limit-string
4745                             (gnus-group-decoded-name gnus-newsgroup-name)
4746                             35)
4747                            number))))
4748                     (if (string-match "^[ \t]*$" input) number input)))
4749                  ((and (> scored marked) (< scored number)
4750                        (> (- scored number) 20))
4751                   (let ((input
4752                          (read-string
4753                           (format "%s %s (%d scored, %d total): "
4754                                   "How many articles from"
4755                                   (gnus-group-decoded-name group)
4756                                   scored number))))
4757                     (if (string-match "^[ \t]*$" input)
4758                         number input)))
4759                  (t number))
4760               (quit
4761                (message "Quit getting the articles to read")
4762                nil))))))
4763     (setq select (if (stringp select) (string-to-number select) select))
4764     (if (or (null select) (zerop select))
4765         select
4766       (if (and (not (zerop scored)) (<= (abs select) scored))
4767           (progn
4768             (setq articles (sort scored-list '<))
4769             (setq number (length articles)))
4770         (setq articles (copy-sequence articles)))
4771
4772       (when (< (abs select) number)
4773         (if (< select 0)
4774             ;; Select the N oldest articles.
4775             (setcdr (nthcdr (1- (abs select)) articles) nil)
4776           ;; Select the N most recent articles.
4777           (setq articles (nthcdr (- number select) articles))))
4778       (setq gnus-newsgroup-unselected
4779             (gnus-sorted-intersection
4780              gnus-newsgroup-unreads
4781              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4782       (when gnus-alter-articles-to-read-function
4783         (setq gnus-newsgroup-unreads
4784               (sort
4785                (funcall gnus-alter-articles-to-read-function
4786                         gnus-newsgroup-name gnus-newsgroup-unreads)
4787                '<)))
4788       articles)))
4789
4790 (defun gnus-killed-articles (killed articles)
4791   (let (out)
4792     (while articles
4793       (when (inline (gnus-member-of-range (car articles) killed))
4794         (push (car articles) out))
4795       (setq articles (cdr articles)))
4796     out))
4797
4798 (defun gnus-uncompress-marks (marks)
4799   "Uncompress the mark ranges in MARKS."
4800   (let ((uncompressed '(score bookmark))
4801         out)
4802     (while marks
4803       (if (memq (caar marks) uncompressed)
4804           (push (car marks) out)
4805         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4806       (setq marks (cdr marks)))
4807     out))
4808
4809 (defun gnus-article-mark-to-type (mark)
4810   "Return the type of MARK."
4811   (or (cadr (assq mark gnus-article-special-mark-lists))
4812       'list))
4813
4814 (defun gnus-article-unpropagatable-p (mark)
4815   "Return whether MARK should be propagated to backend."
4816   (memq mark gnus-article-unpropagated-mark-lists))
4817
4818 (defun gnus-adjust-marked-articles (info)
4819   "Set all article lists and remove all marks that are no longer valid."
4820   (let* ((marked-lists (gnus-info-marks info))
4821          (active (gnus-active (gnus-info-group info)))
4822          (min (car active))
4823          (max (cdr active))
4824          (types gnus-article-mark-lists)
4825          marks var articles article mark mark-type)
4826
4827     (dolist (marks marked-lists)
4828       (setq mark (car marks)
4829             mark-type (gnus-article-mark-to-type mark)
4830             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4831
4832       ;; We set the variable according to the type of the marks list,
4833       ;; and then adjust the marks to a subset of the active articles.
4834       (cond
4835        ;; Adjust "simple" lists.
4836        ((eq mark-type 'list)
4837         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4838         (when (memq mark '(tick dormant expire reply save))
4839           (while articles
4840             (when (or (< (setq article (pop articles)) min) (> article max))
4841               (set var (delq article (symbol-value var)))))))
4842        ;; Adjust assocs.
4843        ((eq mark-type 'tuple)
4844         (set var (setq articles (cdr marks)))
4845         (when (not (listp (cdr (symbol-value var))))
4846           (set var (list (symbol-value var))))
4847         (when (not (listp (cdr articles)))
4848           (setq articles (list articles)))
4849         (while articles
4850           (when (or (not (consp (setq article (pop articles))))
4851                     (< (car article) min)
4852                     (> (car article) max))
4853             (set var (delq article (symbol-value var))))))
4854        ((eq mark-type 'range)
4855         (cond
4856          ((eq mark 'seen))))))))
4857
4858 (defun gnus-update-missing-marks (missing)
4859   "Go through the list of MISSING articles and remove them from the mark lists."
4860   (when missing
4861     (let (var m)
4862       ;; Go through all types.
4863       (dolist (elem gnus-article-mark-lists)
4864         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4865           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4866           (when (symbol-value var)
4867             ;; This list has articles.  So we delete all missing
4868             ;; articles from it.
4869             (setq m missing)
4870             (while m
4871               (set var (delq (pop m) (symbol-value var))))))))))
4872
4873 (defun gnus-update-marks ()
4874   "Enter the various lists of marked articles into the newsgroup info list."
4875   (let ((types gnus-article-mark-lists)
4876         (info (gnus-get-info gnus-newsgroup-name))
4877         type list newmarked symbol delta-marks)
4878     (when info
4879       ;; Add all marks lists to the list of marks lists.
4880       (while (setq type (pop types))
4881         (setq list (symbol-value
4882                     (setq symbol
4883                           (intern (format "gnus-newsgroup-%s" (car type))))))
4884
4885         (when list
4886           ;; Get rid of the entries of the articles that have the
4887           ;; default score.
4888           (when (and (eq (cdr type) 'score)
4889                      gnus-save-score
4890                      list)
4891             (let* ((arts list)
4892                    (prev (cons nil list))
4893                    (all prev))
4894               (while arts
4895                 (if (or (not (consp (car arts)))
4896                         (= (cdar arts) gnus-summary-default-score))
4897                     (setcdr prev (cdr arts))
4898                   (setq prev arts))
4899                 (setq arts (cdr arts)))
4900               (setq list (cdr all)))))
4901
4902         (when (eq (cdr type) 'seen)
4903           (setq list 
4904                 (if list
4905                     (gnus-add-to-range list gnus-newsgroup-unseen)
4906                   (gnus-compress-sequence gnus-newsgroup-articles))))
4907
4908         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4909           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4910
4911         (when (and (gnus-check-backend-function
4912                     'request-set-mark gnus-newsgroup-name)
4913                    (not (gnus-article-unpropagatable-p (cdr type))))
4914           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4915                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4916                  (add (gnus-remove-from-range
4917                        (gnus-copy-sequence list) old)))
4918             (when add
4919               (push (list add 'add (list (cdr type))) delta-marks))
4920             (when del
4921               (push (list del 'del (list (cdr type))) delta-marks))))
4922
4923         (when list
4924           (push (cons (cdr type) list) newmarked)))
4925
4926       (when delta-marks
4927         (unless (gnus-check-group gnus-newsgroup-name)
4928           (error "Can't open server for %s" gnus-newsgroup-name))
4929         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4930
4931       ;; Enter these new marks into the info of the group.
4932       (if (nthcdr 3 info)
4933           (setcar (nthcdr 3 info) newmarked)
4934         ;; Add the marks lists to the end of the info.
4935         (when newmarked
4936           (setcdr (nthcdr 2 info) (list newmarked))))
4937
4938       ;; Cut off the end of the info if there's nothing else there.
4939       (let ((i 5))
4940         (while (and (> i 2)
4941                     (not (nth i info)))
4942           (when (nthcdr (decf i) info)
4943             (setcdr (nthcdr i info) nil)))))))
4944
4945 (defun gnus-set-mode-line (where)
4946   "Set the mode line of the article or summary buffers.
4947 If WHERE is `summary', the summary mode line format will be used."
4948   ;; Is this mode line one we keep updated?
4949   (when (and (memq where gnus-updated-mode-lines)
4950              (symbol-value
4951               (intern (format "gnus-%s-mode-line-format-spec" where))))
4952     (let (mode-string)
4953       (save-excursion
4954         ;; We evaluate this in the summary buffer since these
4955         ;; variables are buffer-local to that buffer.
4956         (set-buffer gnus-summary-buffer)
4957        ;; We bind all these variables that are used in the `eval' form
4958         ;; below.
4959         (let* ((mformat (symbol-value
4960                          (intern
4961                           (format "gnus-%s-mode-line-format-spec" where))))
4962                (gnus-tmp-group-name (gnus-group-decoded-name
4963                                      gnus-newsgroup-name))
4964                (gnus-tmp-article-number (or gnus-current-article 0))
4965                (gnus-tmp-unread gnus-newsgroup-unreads)
4966                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4967                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4968                (gnus-tmp-unread-and-unselected
4969                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4970                             (zerop gnus-tmp-unselected))
4971                        "")
4972                       ((zerop gnus-tmp-unselected)
4973                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4974                       (t (format "{%d(+%d) more}"
4975                                  gnus-tmp-unread-and-unticked
4976                                  gnus-tmp-unselected))))
4977                (gnus-tmp-subject
4978                 (if (and gnus-current-headers
4979                          (vectorp gnus-current-headers))
4980                     (gnus-mode-string-quote
4981                      (mail-header-subject gnus-current-headers))
4982                   ""))
4983                bufname-length max-len
4984                gnus-tmp-header) ;; passed as argument to any user-format-funcs
4985           (setq mode-string (eval mformat))
4986           (setq bufname-length (if (string-match "%b" mode-string)
4987                                    (- (length
4988                                        (buffer-name
4989                                         (if (eq where 'summary)
4990                                             nil
4991                                           (get-buffer gnus-article-buffer))))
4992                                       2)
4993                                  0))
4994           (setq max-len (max 4 (if gnus-mode-non-string-length
4995                                    (- (window-width)
4996                                       gnus-mode-non-string-length
4997                                       bufname-length)
4998                                  (length mode-string))))
4999           ;; We might have to chop a bit of the string off...
5000           (when (> (length mode-string) max-len)
5001             (setq mode-string
5002                   (concat (truncate-string-to-width mode-string (- max-len 3))
5003                           "...")))
5004           ;; Pad the mode string a bit.
5005           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5006       ;; Update the mode line.
5007       (setq mode-line-buffer-identification
5008             (gnus-mode-line-buffer-identification (list mode-string)))
5009       (set-buffer-modified-p t))))
5010
5011 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5012   "Go through the HEADERS list and add all Xrefs to a hash table.
5013 The resulting hash table is returned, or nil if no Xrefs were found."
5014   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5015          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5016          (xref-hashtb (gnus-make-hashtable))
5017          start group entry number xrefs header)
5018     (while headers
5019       (setq header (pop headers))
5020       (when (and (setq xrefs (mail-header-xref header))
5021                  (not (memq (setq number (mail-header-number header))
5022                             unreads)))
5023         (setq start 0)
5024         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5025           (setq start (match-end 0))
5026           (setq group (if prefix
5027                           (concat prefix (substring xrefs (match-beginning 1)
5028                                                     (match-end 1)))
5029                         (substring xrefs (match-beginning 1) (match-end 1))))
5030           (setq number
5031                 (string-to-int (substring xrefs (match-beginning 2)
5032                                           (match-end 2))))
5033           (if (setq entry (gnus-gethash group xref-hashtb))
5034               (setcdr entry (cons number (cdr entry)))
5035             (gnus-sethash group (cons number nil) xref-hashtb)))))
5036     (and start xref-hashtb)))
5037
5038 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5039   "Look through all the headers and mark the Xrefs as read."
5040   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5041         name entry info xref-hashtb idlist method nth4)
5042     (save-excursion
5043       (set-buffer gnus-group-buffer)
5044       (when (setq xref-hashtb
5045                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5046         (mapatoms
5047          (lambda (group)
5048            (unless (string= from-newsgroup (setq name (symbol-name group)))
5049              (setq idlist (symbol-value group))
5050              ;; Dead groups are not updated.
5051              (and (prog1
5052                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5053                             info (nth 2 entry))
5054                     (when (stringp (setq nth4 (gnus-info-method info)))
5055                       (setq nth4 (gnus-server-to-method nth4))))
5056                   ;; Only do the xrefs if the group has the same
5057                   ;; select method as the group we have just read.
5058                   (or (gnus-methods-equal-p
5059                        nth4 (gnus-find-method-for-group from-newsgroup))
5060                       virtual
5061                       (equal nth4 (setq method (gnus-find-method-for-group
5062                                                 from-newsgroup)))
5063                       (and (equal (car nth4) (car method))
5064                            (equal (nth 1 nth4) (nth 1 method))))
5065                   gnus-use-cross-reference
5066                   (or (not (eq gnus-use-cross-reference t))
5067                       virtual
5068                       ;; Only do cross-references on subscribed
5069                       ;; groups, if that is what is wanted.
5070                       (<= (gnus-info-level info) gnus-level-subscribed))
5071                   (gnus-group-make-articles-read name idlist))))
5072          xref-hashtb)))))
5073
5074 (defun gnus-compute-read-articles (group articles)
5075   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5076          (info (nth 2 entry))
5077          (active (gnus-active group))
5078          ninfo)
5079     (when entry
5080       ;; First peel off all invalid article numbers.
5081       (when active
5082         (let ((ids articles)
5083               id first)
5084           (while (setq id (pop ids))
5085             (when (and first (> id (cdr active)))
5086               ;; We'll end up in this situation in one particular
5087               ;; obscure situation.  If you re-scan a group and get
5088               ;; a new article that is cross-posted to a different
5089               ;; group that has not been re-scanned, you might get
5090               ;; crossposted article that has a higher number than
5091               ;; Gnus believes possible.  So we re-activate this
5092               ;; group as well.  This might mean doing the
5093               ;; crossposting thingy will *increase* the number
5094               ;; of articles in some groups.  Tsk, tsk.
5095               (setq active (or (gnus-activate-group group) active)))
5096             (when (or (> id (cdr active))
5097                       (< id (car active)))
5098               (setq articles (delq id articles))))))
5099       ;; If the read list is nil, we init it.
5100       (if (and active
5101                (null (gnus-info-read info))
5102                (> (car active) 1))
5103           (setq ninfo (cons 1 (1- (car active))))
5104         (setq ninfo (gnus-info-read info)))
5105       ;; Then we add the read articles to the range.
5106       (gnus-add-to-range
5107        ninfo (setq articles (sort articles '<))))))
5108
5109 (defun gnus-group-make-articles-read (group articles)
5110   "Update the info of GROUP to say that ARTICLES are read."
5111   (let* ((num 0)
5112          (entry (gnus-gethash group gnus-newsrc-hashtb))
5113          (info (nth 2 entry))
5114          (active (gnus-active group))
5115          range)
5116     (when entry
5117       (setq range (gnus-compute-read-articles group articles))
5118       (save-excursion
5119         (set-buffer gnus-group-buffer)
5120         (gnus-undo-register
5121           `(progn
5122              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5123              (gnus-info-set-read ',info ',(gnus-info-read info))
5124              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5125              (gnus-group-update-group ,group t))))
5126       ;; Add the read articles to the range.
5127       (gnus-info-set-read info range)
5128       (gnus-request-set-mark group (list (list range 'add '(read))))
5129       ;; Then we have to re-compute how many unread
5130       ;; articles there are in this group.
5131       (when active
5132         (cond
5133          ((not range)
5134           (setq num (- (1+ (cdr active)) (car active))))
5135          ((not (listp (cdr range)))
5136           (setq num (- (cdr active) (- (1+ (cdr range))
5137                                        (car range)))))
5138          (t
5139           (while range
5140             (if (numberp (car range))
5141                 (setq num (1+ num))
5142               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5143             (setq range (cdr range)))
5144           (setq num (- (cdr active) num))))
5145         ;; Update the number of unread articles.
5146         (setcar entry num)
5147         ;; Update the group buffer.
5148         (gnus-group-update-group group t)))))
5149
5150 (defvar gnus-newsgroup-none-id 0)
5151
5152 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5153   (let ((cur nntp-server-buffer)
5154         (dependencies
5155          (or dependencies
5156              (save-excursion (set-buffer gnus-summary-buffer)
5157                              gnus-newsgroup-dependencies)))
5158         headers id end ref
5159         (mail-parse-charset gnus-newsgroup-charset)
5160         (mail-parse-ignored-charsets
5161          (save-excursion (condition-case nil
5162                              (set-buffer gnus-summary-buffer)
5163                            (error))
5164                          gnus-newsgroup-ignored-charsets)))
5165     (save-excursion
5166       (set-buffer nntp-server-buffer)
5167       ;; Translate all TAB characters into SPACE characters.
5168       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5169       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5170       (gnus-run-hooks 'gnus-parse-headers-hook)
5171       (let ((case-fold-search t)
5172             in-reply-to header p lines chars)
5173         (goto-char (point-min))
5174         ;; Search to the beginning of the next header.  Error messages
5175         ;; do not begin with 2 or 3.
5176         (while (re-search-forward "^[23][0-9]+ " nil t)
5177           (setq id nil
5178                 ref nil)
5179           ;; This implementation of this function, with nine
5180           ;; search-forwards instead of the one re-search-forward and
5181           ;; a case (which basically was the old function) is actually
5182           ;; about twice as fast, even though it looks messier.  You
5183           ;; can't have everything, I guess.  Speed and elegance
5184           ;; doesn't always go hand in hand.
5185           (setq
5186            header
5187            (vector
5188             ;; Number.
5189             (prog1
5190                 (read cur)
5191               (end-of-line)
5192               (setq p (point))
5193               (narrow-to-region (point)
5194                                 (or (and (search-forward "\n.\n" nil t)
5195                                          (- (point) 2))
5196                                     (point))))
5197             ;; Subject.
5198             (progn
5199               (goto-char p)
5200               (if (search-forward "\nsubject:" nil t)
5201                   (funcall gnus-decode-encoded-word-function
5202                            (nnheader-header-value))
5203                 "(none)"))
5204             ;; From.
5205             (progn
5206               (goto-char p)
5207               (if (search-forward "\nfrom:" nil t)
5208                   (funcall gnus-decode-encoded-word-function
5209                            (nnheader-header-value))
5210                 "(nobody)"))
5211             ;; Date.
5212             (progn
5213               (goto-char p)
5214               (if (search-forward "\ndate:" nil t)
5215                   (nnheader-header-value) ""))
5216             ;; Message-ID.
5217             (progn
5218               (goto-char p)
5219               (setq id (if (re-search-forward
5220                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5221                       ;; We do it this way to make sure the Message-ID
5222                            ;; is (somewhat) syntactically valid.
5223                            (buffer-substring (match-beginning 1)
5224                                              (match-end 1))
5225                        ;; If there was no message-id, we just fake one
5226                          ;; to make subsequent routines simpler.
5227                          (nnheader-generate-fake-message-id))))
5228             ;; References.
5229             (progn
5230               (goto-char p)
5231               (if (search-forward "\nreferences:" nil t)
5232                   (progn
5233                     (setq end (point))
5234                     (prog1
5235                         (nnheader-header-value)
5236                       (setq ref
5237                             (buffer-substring
5238                              (progn
5239                                (end-of-line)
5240                                (search-backward ">" end t)
5241                                (1+ (point)))
5242                              (progn
5243                                (search-backward "<" end t)
5244                                (point))))))
5245             ;; Get the references from the in-reply-to header if there
5246                 ;; were no references and the in-reply-to header looks
5247                 ;; promising.
5248                 (if (and (search-forward "\nin-reply-to:" nil t)
5249                          (setq in-reply-to (nnheader-header-value))
5250                          (string-match "<[^>]+>" in-reply-to))
5251                     (let (ref2)
5252                       (setq ref (substring in-reply-to (match-beginning 0)
5253                                            (match-end 0)))
5254                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5255                         (setq ref2 (substring in-reply-to (match-beginning 0)
5256                                               (match-end 0)))
5257                         (when (> (length ref2) (length ref))
5258                           (setq ref ref2)))
5259                       ref)
5260                   (setq ref nil))))
5261             ;; Chars.
5262             (progn
5263               (goto-char p)
5264               (if (search-forward "\nchars: " nil t)
5265                   (if (numberp (setq chars (ignore-errors (read cur))))
5266                       chars -1)
5267                 -1))
5268             ;; Lines.
5269             (progn
5270               (goto-char p)
5271               (if (search-forward "\nlines: " nil t)
5272                   (if (numberp (setq lines (ignore-errors (read cur))))
5273                       lines -1)
5274                 -1))
5275             ;; Xref.
5276             (progn
5277               (goto-char p)
5278               (and (search-forward "\nxref:" nil t)
5279                    (nnheader-header-value)))
5280             ;; Extra.
5281             (when gnus-extra-headers
5282               (let ((extra gnus-extra-headers)
5283                     out)
5284                 (while extra
5285                   (goto-char p)
5286                   (when (search-forward
5287                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5288                     (push (cons (car extra) (nnheader-header-value))
5289                           out))
5290                   (pop extra))
5291                 out))))
5292           (when (equal id ref)
5293             (setq ref nil))
5294
5295           (when gnus-alter-header-function
5296             (funcall gnus-alter-header-function header)
5297             (setq id (mail-header-id header)
5298                   ref (gnus-parent-id (mail-header-references header))))
5299
5300           (when (setq header
5301                       (gnus-dependencies-add-header
5302                        header dependencies force-new))
5303             (push header headers))
5304           (goto-char (point-max))
5305           (widen))
5306         (nreverse headers)))))
5307
5308 ;; Goes through the xover lines and returns a list of vectors
5309 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5310                                                   force-new dependencies
5311                                                   group also-fetch-heads)
5312   "Parse the news overview data in the server buffer.
5313 Return a list of headers that match SEQUENCE (see
5314 `nntp-retrieve-headers')."
5315   ;; Get the Xref when the users reads the articles since most/some
5316   ;; NNTP servers do not include Xrefs when using XOVER.
5317   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5318   (let ((mail-parse-charset gnus-newsgroup-charset)
5319         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5320         (cur nntp-server-buffer)
5321         (dependencies (or dependencies gnus-newsgroup-dependencies))
5322         (allp (cond
5323                ((eq gnus-read-all-available-headers t)
5324                 t)
5325                ((stringp gnus-read-all-available-headers)
5326                 (string-match gnus-read-all-available-headers group))
5327                (t
5328                 nil)))
5329         number headers header)
5330     (save-excursion
5331       (set-buffer nntp-server-buffer)
5332       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5333       ;; Allow the user to mangle the headers before parsing them.
5334       (gnus-run-hooks 'gnus-parse-headers-hook)
5335       (goto-char (point-min))
5336       (while (not (eobp))
5337         (condition-case ()
5338             (while (and (or sequence allp)
5339                         (not (eobp)))
5340               (setq number (read cur))
5341               (when (not allp)
5342                 (while (and sequence
5343                             (< (car sequence) number))
5344                   (setq sequence (cdr sequence))))
5345               (when (and (or allp
5346                              (and sequence
5347                                   (eq number (car sequence))))
5348                          (progn
5349                            (setq sequence (cdr sequence))
5350                            (setq header (inline
5351                                           (gnus-nov-parse-line
5352                                            number dependencies force-new)))))
5353                 (push header headers))
5354               (forward-line 1))
5355           (error
5356            (gnus-error 4 "Strange nov line (%d)"
5357                        (count-lines (point-min) (point)))))
5358         (forward-line 1))
5359       ;; A common bug in inn is that if you have posted an article and
5360       ;; then retrieves the active file, it will answer correctly --
5361       ;; the new article is included.  However, a NOV entry for the
5362       ;; article may not have been generated yet, so this may fail.
5363       ;; We work around this problem by retrieving the last few
5364       ;; headers using HEAD.
5365       (if (or (not also-fetch-heads)
5366               (not sequence))
5367           ;; We (probably) got all the headers.
5368           (nreverse headers)
5369         (let ((gnus-nov-is-evil t))
5370           (nconc
5371            (nreverse headers)
5372            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5373              (gnus-get-newsgroup-headers))))))))
5374
5375 (defun gnus-article-get-xrefs ()
5376   "Fill in the Xref value in `gnus-current-headers', if necessary.
5377 This is meant to be called in `gnus-article-internal-prepare-hook'."
5378   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5379                                  gnus-current-headers)))
5380     (or (not gnus-use-cross-reference)
5381         (not headers)
5382         (and (mail-header-xref headers)
5383              (not (string= (mail-header-xref headers) "")))
5384         (let ((case-fold-search t)
5385               xref)
5386           (save-restriction
5387             (nnheader-narrow-to-headers)
5388             (goto-char (point-min))
5389             (when (or (and (not (eobp))
5390                            (eq (downcase (char-after)) ?x)
5391                            (looking-at "Xref:"))
5392                       (search-forward "\nXref:" nil t))
5393               (goto-char (1+ (match-end 0)))
5394               (setq xref (buffer-substring (point)
5395                                            (progn (end-of-line) (point))))
5396               (mail-header-set-xref headers xref)))))))
5397
5398 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5399   "Find article ID and insert the summary line for that article.
5400 OLD-HEADER can either be a header or a line number to insert
5401 the subject line on."
5402   (let* ((line (and (numberp old-header) old-header))
5403          (old-header (and (vectorp old-header) old-header))
5404          (header (cond ((and old-header use-old-header)
5405                         old-header)
5406                        ((and (numberp id)
5407                              (gnus-number-to-header id))
5408                         (gnus-number-to-header id))
5409                        (t
5410                         (gnus-read-header id))))
5411          (number (and (numberp id) id))
5412          d)
5413     (when header
5414       ;; Rebuild the thread that this article is part of and go to the
5415       ;; article we have fetched.
5416       (when (and (not gnus-show-threads)
5417                  old-header)
5418         (when (and number
5419                    (setq d (gnus-data-find (mail-header-number old-header))))
5420           (goto-char (gnus-data-pos d))
5421           (gnus-data-remove
5422            number
5423            (- (gnus-point-at-bol)
5424               (prog1
5425                   (1+ (gnus-point-at-eol))
5426                 (gnus-delete-line))))))
5427       (when old-header
5428         (mail-header-set-number header (mail-header-number old-header)))
5429       (setq gnus-newsgroup-sparse
5430             (delq (setq number (mail-header-number header))
5431                   gnus-newsgroup-sparse))
5432       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5433       (push number gnus-newsgroup-limit)
5434       (gnus-rebuild-thread (mail-header-id header) line)
5435       (gnus-summary-goto-subject number nil t))
5436     (when (and (numberp number)
5437                (> number 0))
5438       ;; We have to update the boundaries even if we can't fetch the
5439       ;; article if ID is a number -- so that the next `P' or `N'
5440       ;; command will fetch the previous (or next) article even
5441       ;; if the one we tried to fetch this time has been canceled.
5442       (when (> number gnus-newsgroup-end)
5443         (setq gnus-newsgroup-end number))
5444       (when (< number gnus-newsgroup-begin)
5445         (setq gnus-newsgroup-begin number))
5446       (setq gnus-newsgroup-unselected
5447             (delq number gnus-newsgroup-unselected)))
5448     ;; Report back a success?
5449     (and header (mail-header-number header))))
5450
5451 ;;; Process/prefix in the summary buffer
5452
5453 (defun gnus-summary-work-articles (n)
5454   "Return a list of articles to be worked upon.
5455 The prefix argument, the list of process marked articles, and the
5456 current article will be taken into consideration."
5457   (save-excursion
5458     (set-buffer gnus-summary-buffer)
5459     (cond
5460      (n
5461       ;; A numerical prefix has been given.
5462       (setq n (prefix-numeric-value n))
5463       (let ((backward (< n 0))
5464             (n (abs (prefix-numeric-value n)))
5465             articles article)
5466         (save-excursion
5467           (while
5468               (and (> n 0)
5469                    (push (setq article (gnus-summary-article-number))
5470                          articles)
5471                    (if backward
5472                        (gnus-summary-find-prev nil article)
5473                      (gnus-summary-find-next nil article)))
5474             (decf n)))
5475         (nreverse articles)))
5476      ((and (gnus-region-active-p) (mark))
5477       (message "region active")
5478       ;; Work on the region between point and mark.
5479       (let ((max (max (point) (mark)))
5480             articles article)
5481         (save-excursion
5482           (goto-char (min (min (point) (mark))))
5483           (while
5484               (and
5485                (push (setq article (gnus-summary-article-number)) articles)
5486                (gnus-summary-find-next nil article)
5487                (< (point) max)))
5488           (nreverse articles))))
5489      (gnus-newsgroup-processable
5490       ;; There are process-marked articles present.
5491       ;; Save current state.
5492       (gnus-summary-save-process-mark)
5493       ;; Return the list.
5494       (reverse gnus-newsgroup-processable))
5495      (t
5496       ;; Just return the current article.
5497       (list (gnus-summary-article-number))))))
5498
5499 (defmacro gnus-summary-iterate (arg &rest forms)
5500   "Iterate over the process/prefixed articles and do FORMS.
5501 ARG is the interactive prefix given to the command.  FORMS will be
5502 executed with point over the summary line of the articles."
5503   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5504     `(let ((,articles (gnus-summary-work-articles ,arg)))
5505        (while ,articles
5506          (gnus-summary-goto-subject (car ,articles))
5507          ,@forms
5508          (pop ,articles)))))
5509
5510 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5511 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5512
5513 (defun gnus-summary-save-process-mark ()
5514   "Push the current set of process marked articles on the stack."
5515   (interactive)
5516   (push (copy-sequence gnus-newsgroup-processable)
5517         gnus-newsgroup-process-stack))
5518
5519 (defun gnus-summary-kill-process-mark ()
5520   "Push the current set of process marked articles on the stack and unmark."
5521   (interactive)
5522   (gnus-summary-save-process-mark)
5523   (gnus-summary-unmark-all-processable))
5524
5525 (defun gnus-summary-yank-process-mark ()
5526   "Pop the last process mark state off the stack and restore it."
5527   (interactive)
5528   (unless gnus-newsgroup-process-stack
5529     (error "Empty mark stack"))
5530   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5531
5532 (defun gnus-summary-process-mark-set (set)
5533   "Make SET into the current process marked articles."
5534   (gnus-summary-unmark-all-processable)
5535   (while set
5536     (gnus-summary-set-process-mark (pop set))))
5537
5538 ;;; Searching and stuff
5539
5540 (defun gnus-summary-search-group (&optional backward use-level)
5541   "Search for next unread newsgroup.
5542 If optional argument BACKWARD is non-nil, search backward instead."
5543   (save-excursion
5544     (set-buffer gnus-group-buffer)
5545     (when (gnus-group-search-forward
5546            backward nil (if use-level (gnus-group-group-level) nil))
5547       (gnus-group-group-name))))
5548
5549 (defun gnus-summary-best-group (&optional exclude-group)
5550   "Find the name of the best unread group.
5551 If EXCLUDE-GROUP, do not go to this group."
5552   (save-excursion
5553     (set-buffer gnus-group-buffer)
5554     (save-excursion
5555       (gnus-group-best-unread-group exclude-group))))
5556
5557 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5558   (if backward (gnus-summary-find-prev)
5559     (let* ((dummy (gnus-summary-article-intangible-p))
5560            (article (or article (gnus-summary-article-number)))
5561            (arts (gnus-data-find-list article))
5562            result)
5563       (when (and (not dummy)
5564                  (or (not gnus-summary-check-current)
5565                      (not unread)
5566                      (not (gnus-data-unread-p (car arts)))))
5567         (setq arts (cdr arts)))
5568       (when (setq result
5569                   (if unread
5570                       (progn
5571                         (while arts
5572                           (when (or (and undownloaded
5573                                          (eq gnus-undownloaded-mark
5574                                              (gnus-data-mark (car arts))))
5575                                     (gnus-data-unread-p (car arts)))
5576                             (setq result (car arts)
5577                                   arts nil))
5578                           (setq arts (cdr arts)))
5579                         result)
5580                     (car arts)))
5581         (goto-char (gnus-data-pos result))
5582         (gnus-data-number result)))))
5583
5584 (defun gnus-summary-find-prev (&optional unread article)
5585   (let* ((eobp (eobp))
5586          (article (or article (gnus-summary-article-number)))
5587          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5588          result)
5589     (when (and (not eobp)
5590                (or (not gnus-summary-check-current)
5591                    (not unread)
5592                    (not (gnus-data-unread-p (car arts)))))
5593       (setq arts (cdr arts)))
5594     (when (setq result
5595                 (if unread
5596                     (progn
5597                       (while arts
5598                         (when (gnus-data-unread-p (car arts))
5599                           (setq result (car arts)
5600                                 arts nil))
5601                         (setq arts (cdr arts)))
5602                       result)
5603                   (car arts)))
5604       (goto-char (gnus-data-pos result))
5605       (gnus-data-number result))))
5606
5607 (defun gnus-summary-find-subject (subject &optional unread backward article)
5608   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5609          (article (or article (gnus-summary-article-number)))
5610          (articles (gnus-data-list backward))
5611          (arts (gnus-data-find-list article articles))
5612          result)
5613     (when (or (not gnus-summary-check-current)
5614               (not unread)
5615               (not (gnus-data-unread-p (car arts))))
5616       (setq arts (cdr arts)))
5617     (while arts
5618       (and (or (not unread)
5619                (gnus-data-unread-p (car arts)))
5620            (vectorp (gnus-data-header (car arts)))
5621            (gnus-subject-equal
5622             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5623            (setq result (car arts)
5624                  arts nil))
5625       (setq arts (cdr arts)))
5626     (and result
5627          (goto-char (gnus-data-pos result))
5628          (gnus-data-number result))))
5629
5630 (defun gnus-summary-search-forward (&optional unread subject backward)
5631   "Search forward for an article.
5632 If UNREAD, look for unread articles.  If SUBJECT, look for
5633 articles with that subject.  If BACKWARD, search backward instead."
5634   (cond (subject (gnus-summary-find-subject subject unread backward))
5635         (backward (gnus-summary-find-prev unread))
5636         (t (gnus-summary-find-next unread))))
5637
5638 (defun gnus-recenter (&optional n)
5639   "Center point in window and redisplay frame.
5640 Also do horizontal recentering."
5641   (interactive "P")
5642   (when (and gnus-auto-center-summary
5643              (not (eq gnus-auto-center-summary 'vertical)))
5644     (gnus-horizontal-recenter))
5645   (recenter n))
5646
5647 (defun gnus-summary-recenter ()
5648   "Center point in the summary window.
5649 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5650 displayed, no centering will be performed."
5651   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5652 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5653   (interactive)
5654   (let* ((top (cond ((< (window-height) 4) 0)
5655                     ((< (window-height) 7) 1)
5656                     (t (if (numberp gnus-auto-center-summary)
5657                            gnus-auto-center-summary
5658                          2))))
5659          (height (1- (window-height)))
5660          (bottom (save-excursion (goto-char (point-max))
5661                                  (forward-line (- height))
5662                                  (point)))
5663          (window (get-buffer-window (current-buffer))))
5664     ;; The user has to want it.
5665     (when gnus-auto-center-summary
5666       (when (get-buffer-window gnus-article-buffer)
5667         ;; Only do recentering when the article buffer is displayed,
5668       ;; Set the window start to either `bottom', which is the biggest
5669         ;; possible valid number, or the second line from the top,
5670         ;; whichever is the least.
5671         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5672           (if (> bottom top-pos)
5673               ;; Keep the second line from the top visible
5674               (set-window-start window top-pos t)
5675             ;; Try to keep the bottom line visible; if it's partially
5676             ;; obscured, either scroll one more line to make it fully
5677             ;; visible, or revert to using TOP-POS.
5678             (save-excursion
5679               (goto-char (point-max))
5680               (forward-line -1)
5681               (let ((last-line-start (point)))
5682                 (goto-char bottom)
5683                 (set-window-start window (point) t)
5684                 (when (not (pos-visible-in-window-p last-line-start window))
5685                   (forward-line 1)
5686                   (set-window-start window (min (point) top-pos) t)))))))
5687       ;; Do horizontal recentering while we're at it.
5688       (when (and (get-buffer-window (current-buffer) t)
5689                  (not (eq gnus-auto-center-summary 'vertical)))
5690         (let ((selected (selected-window)))
5691           (select-window (get-buffer-window (current-buffer) t))
5692           (gnus-summary-position-point)
5693           (gnus-horizontal-recenter)
5694           (select-window selected))))))
5695
5696 (defun gnus-summary-jump-to-group (newsgroup)
5697   "Move point to NEWSGROUP in group mode buffer."
5698   ;; Keep update point of group mode buffer if visible.
5699   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5700       (save-window-excursion
5701         ;; Take care of tree window mode.
5702         (when (get-buffer-window gnus-group-buffer)
5703           (pop-to-buffer gnus-group-buffer))
5704         (gnus-group-jump-to-group newsgroup))
5705     (save-excursion
5706       ;; Take care of tree window mode.
5707       (if (get-buffer-window gnus-group-buffer)
5708           (pop-to-buffer gnus-group-buffer)
5709         (set-buffer gnus-group-buffer))
5710       (gnus-group-jump-to-group newsgroup))))
5711
5712 ;; This function returns a list of article numbers based on the
5713 ;; difference between the ranges of read articles in this group and
5714 ;; the range of active articles.
5715 (defun gnus-list-of-unread-articles (group)
5716   (let* ((read (gnus-info-read (gnus-get-info group)))
5717          (active (or (gnus-active group) (gnus-activate-group group)))
5718          (last (cdr active))
5719          first nlast unread)
5720     ;; If none are read, then all are unread.
5721     (if (not read)
5722         (setq first (car active))
5723       ;; If the range of read articles is a single range, then the
5724       ;; first unread article is the article after the last read
5725       ;; article.  Sounds logical, doesn't it?
5726       (if (and (not (listp (cdr read)))
5727                (or (< (car read) (car active))
5728                    (progn (setq read (list read))
5729                           nil)))
5730           (setq first (max (car active) (1+ (cdr read))))
5731         ;; `read' is a list of ranges.
5732         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5733                                   (caar read)))
5734                   1)
5735           (setq first (car active)))
5736         (while read
5737           (when first
5738             (while (< first nlast)
5739               (push first unread)
5740               (setq first (1+ first))))
5741           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5742           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5743           (setq read (cdr read)))))
5744     ;; And add the last unread articles.
5745     (while (<= first last)
5746       (push first unread)
5747       (setq first (1+ first)))
5748     ;; Return the list of unread articles.
5749     (delq 0 (nreverse unread))))
5750
5751 (defun gnus-list-of-read-articles (group)
5752   "Return a list of unread, unticked and non-dormant articles."
5753   (let* ((info (gnus-get-info group))
5754          (marked (gnus-info-marks info))
5755          (active (gnus-active group)))
5756     (and info active
5757          (gnus-set-difference
5758           (gnus-sorted-complement
5759            (gnus-uncompress-range active)
5760            (gnus-list-of-unread-articles group))
5761           (append
5762            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5763            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5764
5765 ;; Various summary commands
5766
5767 (defun gnus-summary-select-article-buffer ()
5768   "Reconfigure windows to show article buffer."
5769   (interactive)
5770   (if (not (gnus-buffer-live-p gnus-article-buffer))
5771       (error "There is no article buffer for this summary buffer")
5772     (gnus-configure-windows 'article)
5773     (select-window (get-buffer-window gnus-article-buffer))))
5774
5775 (defun gnus-summary-universal-argument (arg)
5776   "Perform any operation on all articles that are process/prefixed."
5777   (interactive "P")
5778   (let ((articles (gnus-summary-work-articles arg))
5779         func article)
5780     (if (eq
5781          (setq
5782           func
5783           (key-binding
5784            (read-key-sequence
5785             (substitute-command-keys
5786              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5787          'undefined)
5788         (gnus-error 1 "Undefined key")
5789       (save-excursion
5790         (while articles
5791           (gnus-summary-goto-subject (setq article (pop articles)))
5792           (let (gnus-newsgroup-processable)
5793             (command-execute func))
5794           (gnus-summary-remove-process-mark article)))))
5795   (gnus-summary-position-point))
5796
5797 (defun gnus-summary-toggle-truncation (&optional arg)
5798   "Toggle truncation of summary lines.
5799 With arg, turn line truncation on iff arg is positive."
5800   (interactive "P")
5801   (setq truncate-lines
5802         (if (null arg) (not truncate-lines)
5803           (> (prefix-numeric-value arg) 0)))
5804   (redraw-display))
5805
5806 (defun gnus-summary-reselect-current-group (&optional all rescan)
5807   "Exit and then reselect the current newsgroup.
5808 The prefix argument ALL means to select all articles."
5809   (interactive "P")
5810   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5811     (error "Ephemeral groups can't be reselected"))
5812   (let ((current-subject (gnus-summary-article-number))
5813         (group gnus-newsgroup-name))
5814     (setq gnus-newsgroup-begin nil)
5815     (gnus-summary-exit)
5816     ;; We have to adjust the point of group mode buffer because
5817     ;; point was moved to the next unread newsgroup by exiting.
5818     (gnus-summary-jump-to-group group)
5819     (when rescan
5820       (save-excursion
5821         (gnus-group-get-new-news-this-group 1)))
5822     (gnus-group-read-group all t)
5823     (gnus-summary-goto-subject current-subject nil t)))
5824
5825 (defun gnus-summary-rescan-group (&optional all)
5826   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5827   (interactive "P")
5828   (gnus-summary-reselect-current-group all t))
5829
5830 (defun gnus-summary-update-info (&optional non-destructive)
5831   (save-excursion
5832     (let ((group gnus-newsgroup-name))
5833       (when group
5834         (when gnus-newsgroup-kill-headers
5835           (setq gnus-newsgroup-killed
5836                 (gnus-compress-sequence
5837                  (nconc
5838                   (gnus-set-sorted-intersection
5839                    (gnus-uncompress-range gnus-newsgroup-killed)
5840                    (setq gnus-newsgroup-unselected
5841                          (sort gnus-newsgroup-unselected '<)))
5842                   (setq gnus-newsgroup-unreads
5843                         (sort gnus-newsgroup-unreads '<)))
5844                  t)))
5845         (unless (listp (cdr gnus-newsgroup-killed))
5846           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5847         (let ((headers gnus-newsgroup-headers))
5848           ;; Set the new ranges of read articles.
5849           (save-excursion
5850             (set-buffer gnus-group-buffer)
5851             (gnus-undo-force-boundary))
5852           (gnus-update-read-articles
5853            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5854           ;; Set the current article marks.
5855           (let ((gnus-newsgroup-scored
5856                  (if (and (not gnus-save-score)
5857                           (not non-destructive))
5858                      nil
5859                    gnus-newsgroup-scored)))
5860             (save-excursion
5861               (gnus-update-marks)))
5862           ;; Do the cross-ref thing.
5863           (when gnus-use-cross-reference
5864             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5865           ;; Do not switch windows but change the buffer to work.
5866           (set-buffer gnus-group-buffer)
5867           (unless (gnus-ephemeral-group-p group)
5868             (gnus-group-update-group group)))))))
5869
5870 (defun gnus-summary-save-newsrc (&optional force)
5871   "Save the current number of read/marked articles in the dribble buffer.
5872 The dribble buffer will then be saved.
5873 If FORCE (the prefix), also save the .newsrc file(s)."
5874   (interactive "P")
5875   (gnus-summary-update-info t)
5876   (if force
5877       (gnus-save-newsrc-file)
5878     (gnus-dribble-save)))
5879
5880 (defun gnus-summary-exit (&optional temporary)
5881   "Exit reading current newsgroup, and then return to group selection mode.
5882 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5883   (interactive)
5884   (gnus-set-global-variables)
5885   (when (gnus-buffer-live-p gnus-article-buffer)
5886     (save-excursion
5887       (set-buffer gnus-article-buffer)
5888       (mm-destroy-parts gnus-article-mime-handles)
5889       ;; Set it to nil for safety reason.
5890       (setq gnus-article-mime-handle-alist nil)
5891       (setq gnus-article-mime-handles nil)))
5892   (gnus-kill-save-kill-buffer)
5893   (gnus-async-halt-prefetch)
5894   (let* ((group gnus-newsgroup-name)
5895          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5896          (mode major-mode)
5897          (group-point nil)
5898          (buf (current-buffer)))
5899     (unless quit-config
5900       ;; Do adaptive scoring, and possibly save score files.
5901       (when gnus-newsgroup-adaptive
5902         (gnus-score-adaptive))
5903       (when gnus-use-scoring
5904         (gnus-score-save)))
5905     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5906     ;; If we have several article buffers, we kill them at exit.
5907     (unless gnus-single-article-buffer
5908       (gnus-kill-buffer gnus-original-article-buffer)
5909       (setq gnus-article-current nil))
5910     (when gnus-use-cache
5911       (gnus-cache-possibly-remove-articles)
5912       (gnus-cache-save-buffers))
5913     (gnus-async-prefetch-remove-group group)
5914     (when gnus-suppress-duplicates
5915       (gnus-dup-enter-articles))
5916     (when gnus-use-trees
5917       (gnus-tree-close group))
5918     (when gnus-use-cache
5919       (gnus-cache-write-active))
5920     ;; Remove entries for this group.
5921     (nnmail-purge-split-history (gnus-group-real-name group))
5922     ;; Make all changes in this group permanent.
5923     (unless quit-config
5924       (gnus-run-hooks 'gnus-exit-group-hook)
5925       (gnus-summary-update-info))
5926     (gnus-close-group group)
5927     ;; Make sure where we were, and go to next newsgroup.
5928     (set-buffer gnus-group-buffer)
5929     (unless quit-config
5930       (gnus-group-jump-to-group group))
5931     (gnus-run-hooks 'gnus-summary-exit-hook)
5932     (unless (or quit-config
5933                 ;; If this group has disappeared from the summary
5934                 ;; buffer, don't skip forwards.
5935                 (not (string= group (gnus-group-group-name))))
5936       (gnus-group-next-unread-group 1))
5937     (setq group-point (point))
5938     (if temporary
5939         nil                             ;Nothing to do.
5940       ;; If we have several article buffers, we kill them at exit.
5941       (unless gnus-single-article-buffer
5942         (gnus-kill-buffer gnus-article-buffer)
5943         (gnus-kill-buffer gnus-original-article-buffer)
5944         (setq gnus-article-current nil))
5945       (set-buffer buf)
5946       (if (not gnus-kill-summary-on-exit)
5947           (progn
5948             (gnus-deaden-summary)
5949             (setq mode nil))
5950        ;; We set all buffer-local variables to nil.  It is unclear why
5951         ;; this is needed, but if we don't, buffer-local variables are
5952         ;; not garbage-collected, it seems.  This would the lead to en
5953         ;; ever-growing Emacs.
5954         (gnus-summary-clear-local-variables)
5955         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5956           (gnus-summary-clear-local-variables))
5957         (when (get-buffer gnus-article-buffer)
5958           (bury-buffer gnus-article-buffer))
5959         ;; We clear the global counterparts of the buffer-local
5960         ;; variables as well, just to be on the safe side.
5961         (set-buffer gnus-group-buffer)
5962         (gnus-summary-clear-local-variables)
5963         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5964           (gnus-summary-clear-local-variables)))
5965       (setq gnus-current-select-method gnus-select-method)
5966       (pop-to-buffer gnus-group-buffer)
5967       (if (not quit-config)
5968           (progn
5969             (goto-char group-point)
5970             (gnus-configure-windows 'group 'force))
5971         (gnus-handle-ephemeral-exit quit-config))
5972       ;; Return to group mode buffer.
5973       (when (eq mode 'gnus-summary-mode)
5974         (gnus-kill-buffer buf))
5975       ;; Clear the current group name.
5976       (unless quit-config
5977         (setq gnus-newsgroup-name nil)))))
5978
5979 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5980 (defun gnus-summary-exit-no-update (&optional no-questions)
5981   "Quit reading current newsgroup without updating read article info."
5982   (interactive)
5983   (let* ((group gnus-newsgroup-name)
5984          (quit-config (gnus-group-quit-config group)))
5985     (when (or no-questions
5986               gnus-expert-user
5987               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5988       (gnus-async-halt-prefetch)
5989       (mapcar 'funcall
5990               (delq 'gnus-summary-expire-articles
5991                     (copy-sequence gnus-summary-prepare-exit-hook)))
5992       (when (gnus-buffer-live-p gnus-article-buffer)
5993         (save-excursion
5994           (set-buffer gnus-article-buffer)
5995           (mm-destroy-parts gnus-article-mime-handles)
5996           ;; Set it to nil for safety reason.
5997           (setq gnus-article-mime-handle-alist nil)
5998           (setq gnus-article-mime-handles nil)))
5999       ;; If we have several article buffers, we kill them at exit.
6000       (unless gnus-single-article-buffer
6001         (gnus-kill-buffer gnus-article-buffer)
6002         (gnus-kill-buffer gnus-original-article-buffer)
6003         (setq gnus-article-current nil))
6004       (if (not gnus-kill-summary-on-exit)
6005           (gnus-deaden-summary)
6006         (gnus-close-group group)
6007         (gnus-summary-clear-local-variables)
6008         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6009           (gnus-summary-clear-local-variables))
6010         (set-buffer gnus-group-buffer)
6011         (gnus-summary-clear-local-variables)
6012         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6013           (gnus-summary-clear-local-variables))
6014         (when (get-buffer gnus-summary-buffer)
6015           (kill-buffer gnus-summary-buffer)))
6016       (unless gnus-single-article-buffer
6017         (setq gnus-article-current nil))
6018       (when gnus-use-trees
6019         (gnus-tree-close group))
6020       (gnus-async-prefetch-remove-group group)
6021       (when (get-buffer gnus-article-buffer)
6022         (bury-buffer gnus-article-buffer))
6023       ;; Return to the group buffer.
6024       (gnus-configure-windows 'group 'force)
6025       ;; Clear the current group name.
6026       (setq gnus-newsgroup-name nil)
6027       (when (equal (gnus-group-group-name) group)
6028         (gnus-group-next-unread-group 1))
6029       (when quit-config
6030         (gnus-handle-ephemeral-exit quit-config)))))
6031
6032 (defun gnus-handle-ephemeral-exit (quit-config)
6033   "Handle movement when leaving an ephemeral group.
6034 The state which existed when entering the ephemeral is reset."
6035   (if (not (buffer-name (car quit-config)))
6036       (gnus-configure-windows 'group 'force)
6037     (set-buffer (car quit-config))
6038     (cond ((eq major-mode 'gnus-summary-mode)
6039            (gnus-set-global-variables))
6040           ((eq major-mode 'gnus-article-mode)
6041            (save-excursion
6042              ;; The `gnus-summary-buffer' variable may point
6043              ;; to the old summary buffer when using a single
6044              ;; article buffer.
6045              (unless (gnus-buffer-live-p gnus-summary-buffer)
6046                (set-buffer gnus-group-buffer))
6047              (set-buffer gnus-summary-buffer)
6048              (gnus-set-global-variables))))
6049     (if (or (eq (cdr quit-config) 'article)
6050             (eq (cdr quit-config) 'pick))
6051         (progn
6052           ;; The current article may be from the ephemeral group
6053           ;; thus it is best that we reload this article
6054           (gnus-summary-show-article)
6055           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6056               (gnus-configure-windows 'pick 'force)
6057             (gnus-configure-windows (cdr quit-config) 'force)))
6058       (gnus-configure-windows (cdr quit-config) 'force))
6059     (when (eq major-mode 'gnus-summary-mode)
6060       (gnus-summary-next-subject 1 nil t)
6061       (gnus-summary-recenter)
6062       (gnus-summary-position-point))))
6063
6064 ;;; Dead summaries.
6065
6066 (defvar gnus-dead-summary-mode-map nil)
6067
6068 (unless gnus-dead-summary-mode-map
6069   (setq gnus-dead-summary-mode-map (make-keymap))
6070   (suppress-keymap gnus-dead-summary-mode-map)
6071   (substitute-key-definition
6072    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6073   (let ((keys '("\C-d" "\r" "\177" [delete])))
6074     (while keys
6075       (define-key gnus-dead-summary-mode-map
6076         (pop keys) 'gnus-summary-wake-up-the-dead))))
6077
6078 (defvar gnus-dead-summary-mode nil
6079   "Minor mode for Gnus summary buffers.")
6080
6081 (defun gnus-dead-summary-mode (&optional arg)
6082   "Minor mode for Gnus summary buffers."
6083   (interactive "P")
6084   (when (eq major-mode 'gnus-summary-mode)
6085     (make-local-variable 'gnus-dead-summary-mode)
6086     (setq gnus-dead-summary-mode
6087           (if (null arg) (not gnus-dead-summary-mode)
6088             (> (prefix-numeric-value arg) 0)))
6089     (when gnus-dead-summary-mode
6090       (gnus-add-minor-mode
6091        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6092
6093 (defun gnus-deaden-summary ()
6094   "Make the current summary buffer into a dead summary buffer."
6095   ;; Kill any previous dead summary buffer.
6096   (when (and gnus-dead-summary
6097              (buffer-name gnus-dead-summary))
6098     (save-excursion
6099       (set-buffer gnus-dead-summary)
6100       (when gnus-dead-summary-mode
6101         (kill-buffer (current-buffer)))))
6102   ;; Make this the current dead summary.
6103   (setq gnus-dead-summary (current-buffer))
6104   (gnus-dead-summary-mode 1)
6105   (let ((name (buffer-name)))
6106     (when (string-match "Summary" name)
6107       (rename-buffer
6108        (concat (substring name 0 (match-beginning 0)) "Dead "
6109                (substring name (match-beginning 0)))
6110        t)
6111       (bury-buffer))))
6112
6113 (defun gnus-kill-or-deaden-summary (buffer)
6114   "Kill or deaden the summary BUFFER."
6115   (save-excursion
6116     (when (and (buffer-name buffer)
6117                (not gnus-single-article-buffer))
6118       (save-excursion
6119         (set-buffer buffer)
6120         (gnus-kill-buffer gnus-article-buffer)
6121         (gnus-kill-buffer gnus-original-article-buffer)))
6122     (cond (gnus-kill-summary-on-exit
6123            (when (and gnus-use-trees
6124                       (gnus-buffer-exists-p buffer))
6125              (save-excursion
6126                (set-buffer buffer)
6127                (gnus-tree-close gnus-newsgroup-name)))
6128            (gnus-kill-buffer buffer))
6129           ((gnus-buffer-exists-p buffer)
6130            (save-excursion
6131              (set-buffer buffer)
6132              (gnus-deaden-summary))))))
6133
6134 (defun gnus-summary-wake-up-the-dead (&rest args)
6135   "Wake up the dead summary buffer."
6136   (interactive)
6137   (gnus-dead-summary-mode -1)
6138   (let ((name (buffer-name)))
6139     (when (string-match "Dead " name)
6140       (rename-buffer
6141        (concat (substring name 0 (match-beginning 0))
6142                (substring name (match-end 0)))
6143        t)))
6144   (gnus-message 3 "This dead summary is now alive again"))
6145
6146 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6147 (defun gnus-summary-fetch-faq (&optional faq-dir)
6148   "Fetch the FAQ for the current group.
6149 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6150 in."
6151   (interactive
6152    (list
6153     (when current-prefix-arg
6154       (completing-read
6155        "Faq dir: " (and (listp gnus-group-faq-directory)
6156                         (mapcar (lambda (file) (list file))
6157                                 gnus-group-faq-directory))))))
6158   (let (gnus-faq-buffer)
6159     (when (setq gnus-faq-buffer
6160                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6161       (gnus-configure-windows 'summary-faq))))
6162
6163 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6164 (defun gnus-summary-describe-group (&optional force)
6165   "Describe the current newsgroup."
6166   (interactive "P")
6167   (gnus-group-describe-group force gnus-newsgroup-name))
6168
6169 (defun gnus-summary-describe-briefly ()
6170   "Describe summary mode commands briefly."
6171   (interactive)
6172   (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")))
6173
6174 ;; Walking around group mode buffer from summary mode.
6175
6176 (defun gnus-summary-next-group (&optional no-article target-group backward)
6177   "Exit current newsgroup and then select next unread newsgroup.
6178 If prefix argument NO-ARTICLE is non-nil, no article is selected
6179 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6180 previous group instead."
6181   (interactive "P")
6182   ;; Stop pre-fetching.
6183   (gnus-async-halt-prefetch)
6184   (let ((current-group gnus-newsgroup-name)
6185         (current-buffer (current-buffer))
6186         entered)
6187    ;; First we semi-exit this group to update Xrefs and all variables.
6188     ;; We can't do a real exit, because the window conf must remain
6189     ;; the same in case the user is prompted for info, and we don't
6190     ;; want the window conf to change before that...
6191     (gnus-summary-exit t)
6192     (while (not entered)
6193       ;; Then we find what group we are supposed to enter.
6194       (set-buffer gnus-group-buffer)
6195       (gnus-group-jump-to-group current-group)
6196       (setq target-group
6197             (or target-group
6198                 (if (eq gnus-keep-same-level 'best)
6199                     (gnus-summary-best-group gnus-newsgroup-name)
6200                   (gnus-summary-search-group backward gnus-keep-same-level))))
6201       (if (not target-group)
6202           ;; There are no further groups, so we return to the group
6203           ;; buffer.
6204           (progn
6205             (gnus-message 5 "Returning to the group buffer")
6206             (setq entered t)
6207             (when (gnus-buffer-live-p current-buffer)
6208               (set-buffer current-buffer)
6209               (gnus-summary-exit))
6210             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6211         ;; We try to enter the target group.
6212         (gnus-group-jump-to-group target-group)
6213         (let ((unreads (gnus-group-group-unread)))
6214           (if (and (or (eq t unreads)
6215                        (and unreads (not (zerop unreads))))
6216                    (gnus-summary-read-group
6217                     target-group nil no-article
6218                     (and (buffer-name current-buffer) current-buffer)
6219                     nil backward))
6220               (setq entered t)
6221             (setq current-group target-group
6222                   target-group nil)))))))
6223
6224 (defun gnus-summary-prev-group (&optional no-article)
6225   "Exit current newsgroup and then select previous unread newsgroup.
6226 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6227   (interactive "P")
6228   (gnus-summary-next-group no-article nil t))
6229
6230 ;; Walking around summary lines.
6231
6232 (defun gnus-summary-first-subject (&optional unread undownloaded)
6233   "Go to the first unread subject.
6234 If UNREAD is non-nil, go to the first unread article.
6235 Returns the article selected or nil if there are no unread articles."
6236   (interactive "P")
6237   (prog1
6238       (cond
6239        ;; Empty summary.
6240        ((null gnus-newsgroup-data)
6241         (gnus-message 3 "No articles in the group")
6242         nil)
6243        ;; Pick the first article.
6244        ((not unread)
6245         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6246         (gnus-data-number (car gnus-newsgroup-data)))
6247        ;; No unread articles.
6248        ((null gnus-newsgroup-unreads)
6249         (gnus-message 3 "No more unread articles")
6250         nil)
6251        ;; Find the first unread article.
6252        (t
6253         (let ((data gnus-newsgroup-data))
6254           (while (and data
6255                       (and (not (and undownloaded
6256                                      (eq gnus-undownloaded-mark
6257                                          (gnus-data-mark (car data)))))
6258                            (not (gnus-data-unread-p (car data)))))
6259             (setq data (cdr data)))
6260           (when data
6261             (goto-char (gnus-data-pos (car data)))
6262             (gnus-data-number (car data))))))
6263     (gnus-summary-position-point)))
6264
6265 (defun gnus-summary-next-subject (n &optional unread dont-display)
6266   "Go to next N'th summary line.
6267 If N is negative, go to the previous N'th subject line.
6268 If UNREAD is non-nil, only unread articles are selected.
6269 The difference between N and the actual number of steps taken is
6270 returned."
6271   (interactive "p")
6272   (let ((backward (< n 0))
6273         (n (abs n)))
6274     (while (and (> n 0)
6275                 (if backward
6276                     (gnus-summary-find-prev unread)
6277                   (gnus-summary-find-next unread)))
6278       (unless (zerop (setq n (1- n)))
6279         (gnus-summary-show-thread)))
6280     (when (/= 0 n)
6281       (gnus-message 7 "No more%s articles"
6282                     (if unread " unread" "")))
6283     (unless dont-display
6284       (gnus-summary-recenter)
6285       (gnus-summary-position-point))
6286     n))
6287
6288 (defun gnus-summary-next-unread-subject (n)
6289   "Go to next N'th unread summary line."
6290   (interactive "p")
6291   (gnus-summary-next-subject n t))
6292
6293 (defun gnus-summary-prev-subject (n &optional unread)
6294   "Go to previous N'th summary line.
6295 If optional argument UNREAD is non-nil, only unread article is selected."
6296   (interactive "p")
6297   (gnus-summary-next-subject (- n) unread))
6298
6299 (defun gnus-summary-prev-unread-subject (n)
6300   "Go to previous N'th unread summary line."
6301   (interactive "p")
6302   (gnus-summary-next-subject (- n) t))
6303
6304 (defun gnus-summary-goto-subject (article &optional force silent)
6305   "Go the subject line of ARTICLE.
6306 If FORCE, also allow jumping to articles not currently shown."
6307   (interactive "nArticle number: ")
6308   (let ((b (point))
6309         (data (gnus-data-find article)))
6310     ;; We read in the article if we have to.
6311     (and (not data)
6312          force
6313          (gnus-summary-insert-subject
6314           article
6315           (if (or (numberp force) (vectorp force)) force)
6316           t)
6317          (setq data (gnus-data-find article)))
6318     (goto-char b)
6319     (if (not data)
6320         (progn
6321           (unless silent
6322             (gnus-message 3 "Can't find article %d" article))
6323           nil)
6324       (let ((pt (gnus-data-pos data)))
6325         (goto-char pt)
6326         (gnus-summary-set-article-display-arrow pt))
6327       (gnus-summary-position-point)
6328       article)))
6329
6330 ;; Walking around summary lines with displaying articles.
6331
6332 (defun gnus-summary-expand-window (&optional arg)
6333   "Make the summary buffer take up the entire Emacs frame.
6334 Given a prefix, will force an `article' buffer configuration."
6335   (interactive "P")
6336   (if arg
6337       (gnus-configure-windows 'article 'force)
6338     (gnus-configure-windows 'summary 'force)))
6339
6340 (defun gnus-summary-display-article (article &optional all-header)
6341   "Display ARTICLE in article buffer."
6342   (when (gnus-buffer-live-p gnus-article-buffer)
6343     (with-current-buffer gnus-article-buffer
6344       (mm-enable-multibyte-mule4)))
6345   (gnus-set-global-variables)
6346   (when (gnus-buffer-live-p gnus-article-buffer)
6347     (with-current-buffer gnus-article-buffer
6348       (setq gnus-article-charset gnus-newsgroup-charset)
6349       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6350       (mm-enable-multibyte-mule4)))
6351   (if (null article)
6352       nil
6353     (prog1
6354         (if gnus-summary-display-article-function
6355             (funcall gnus-summary-display-article-function article all-header)
6356           (gnus-article-prepare article all-header))
6357       (gnus-run-hooks 'gnus-select-article-hook)
6358       (when (and gnus-current-article
6359                  (not (zerop gnus-current-article)))
6360         (gnus-summary-goto-subject gnus-current-article))
6361       (gnus-summary-recenter)
6362       (when (and gnus-use-trees gnus-show-threads)
6363         (gnus-possibly-generate-tree article)
6364         (gnus-highlight-selected-tree article))
6365       ;; Successfully display article.
6366       (gnus-article-set-window-start
6367        (cdr (assq article gnus-newsgroup-bookmarks))))))
6368
6369 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6370   "Select the current article.
6371 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6372 non-nil, the article will be re-fetched even if it already present in
6373 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6374 be displayed."
6375   ;; Make sure we are in the summary buffer to work around bbdb bug.
6376   (unless (eq major-mode 'gnus-summary-mode)
6377     (set-buffer gnus-summary-buffer))
6378   (let ((article (or article (gnus-summary-article-number)))
6379         (all-headers (not (not all-headers))) ;Must be T or NIL.
6380         gnus-summary-display-article-function)
6381     (and (not pseudo)
6382          (gnus-summary-article-pseudo-p article)
6383          (error "This is a pseudo-article"))
6384     (save-excursion
6385       (set-buffer gnus-summary-buffer)
6386       (if (or (and gnus-single-article-buffer
6387                    (or (null gnus-current-article)
6388                        (null gnus-article-current)
6389                        (null (get-buffer gnus-article-buffer))
6390                        (not (eq article (cdr gnus-article-current)))
6391                        (not (equal (car gnus-article-current)
6392                                    gnus-newsgroup-name))))
6393               (and (not gnus-single-article-buffer)
6394                    (or (null gnus-current-article)
6395                        (not (eq gnus-current-article article))))
6396               force)
6397        ;; The requested article is different from the current article.
6398           (progn
6399             (gnus-summary-display-article article all-headers)
6400             (when (gnus-buffer-live-p gnus-article-buffer)
6401               (with-current-buffer gnus-article-buffer
6402                 (if (not gnus-article-decoded-p) ;; a local variable
6403                     (mm-disable-multibyte-mule4))))
6404             (when (or all-headers gnus-show-all-headers)
6405               (gnus-article-show-all-headers))
6406             (gnus-article-set-window-start
6407              (cdr (assq article gnus-newsgroup-bookmarks)))
6408             article)
6409         (when (or all-headers gnus-show-all-headers)
6410           (gnus-article-show-all-headers))
6411         'old))))
6412
6413 (defun gnus-summary-force-verify-and-decrypt ()
6414   (interactive)
6415   (let ((mm-verify-option 'known)
6416         (mm-decrypt-option 'known))
6417     (gnus-summary-select-article nil 'force)))
6418
6419 (defun gnus-summary-set-current-mark (&optional current-mark)
6420   "Obsolete function."
6421   nil)
6422
6423 (defun gnus-summary-next-article (&optional unread subject backward push)
6424   "Select the next article.
6425 If UNREAD, only unread articles are selected.
6426 If SUBJECT, only articles with SUBJECT are selected.
6427 If BACKWARD, the previous article is selected instead of the next."
6428   (interactive "P")
6429   (cond
6430    ;; Is there such an article?
6431    ((and (gnus-summary-search-forward unread subject backward)
6432          (or (gnus-summary-display-article (gnus-summary-article-number))
6433              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6434     (gnus-summary-position-point))
6435    ;; If not, we try the first unread, if that is wanted.
6436    ((and subject
6437          gnus-auto-select-same
6438          (gnus-summary-first-unread-article))
6439     (gnus-summary-position-point)
6440     (gnus-message 6 "Wrapped"))
6441    ;; Try to get next/previous article not displayed in this group.
6442    ((and gnus-auto-extend-newsgroup
6443          (not unread) (not subject))
6444     (gnus-summary-goto-article
6445      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6446      nil (count-lines (point-min) (point))))
6447    ;; Go to next/previous group.
6448    (t
6449     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6450       (gnus-summary-jump-to-group gnus-newsgroup-name))
6451     (let ((cmd last-command-char)
6452           (point
6453            (save-excursion
6454              (set-buffer gnus-group-buffer)
6455              (point)))
6456           (group
6457            (if (eq gnus-keep-same-level 'best)
6458                (gnus-summary-best-group gnus-newsgroup-name)
6459              (gnus-summary-search-group backward gnus-keep-same-level))))
6460       ;; For some reason, the group window gets selected.  We change
6461       ;; it back.
6462       (select-window (get-buffer-window (current-buffer)))
6463       ;; Select next unread newsgroup automagically.
6464       (cond
6465        ((or (not gnus-auto-select-next)
6466             (not cmd))
6467         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6468        ((or (eq gnus-auto-select-next 'quietly)
6469             (and (eq gnus-auto-select-next 'slightly-quietly)
6470                  push)
6471             (and (eq gnus-auto-select-next 'almost-quietly)
6472                  (gnus-summary-last-article-p)))
6473         ;; Select quietly.
6474         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6475             (gnus-summary-exit)
6476           (gnus-message 7 "No more%s articles (%s)..."
6477                         (if unread " unread" "")
6478                         (if group (concat "selecting " group)
6479                           "exiting"))
6480           (gnus-summary-next-group nil group backward)))
6481        (t
6482         (when (gnus-key-press-event-p last-input-event)
6483           (gnus-summary-walk-group-buffer
6484            gnus-newsgroup-name cmd unread backward point))))))))
6485
6486 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6487   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6488                       (?\C-p (gnus-group-prev-unread-group 1))))
6489         (cursor-in-echo-area t)
6490         keve key group ended)
6491     (save-excursion
6492       (set-buffer gnus-group-buffer)
6493       (goto-char start)
6494       (setq group
6495             (if (eq gnus-keep-same-level 'best)
6496                 (gnus-summary-best-group gnus-newsgroup-name)
6497               (gnus-summary-search-group backward gnus-keep-same-level))))
6498     (while (not ended)
6499       (gnus-message
6500        5 "No more%s articles%s" (if unread " unread" "")
6501        (if (and group
6502                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6503            (format " (Type %s for %s [%s])"
6504                    (single-key-description cmd) group
6505                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6506          (format " (Type %s to exit %s)"
6507                  (single-key-description cmd)
6508                  gnus-newsgroup-name)))
6509       ;; Confirm auto selection.
6510       (setq key (car (setq keve (gnus-read-event-char))))
6511       (setq ended t)
6512       (cond
6513        ((assq key keystrokes)
6514         (let ((obuf (current-buffer)))
6515           (switch-to-buffer gnus-group-buffer)
6516           (when group
6517             (gnus-group-jump-to-group group))
6518           (eval (cadr (assq key keystrokes)))
6519           (setq group (gnus-group-group-name))
6520           (switch-to-buffer obuf))
6521         (setq ended nil))
6522        ((equal key cmd)
6523         (if (or (not group)
6524                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6525             (gnus-summary-exit)
6526           (gnus-summary-next-group nil group backward)))
6527        (t
6528         (push (cdr keve) unread-command-events))))))
6529
6530 (defun gnus-summary-next-unread-article ()
6531   "Select unread article after current one."
6532   (interactive)
6533   (gnus-summary-next-article
6534    (or (not (eq gnus-summary-goto-unread 'never))
6535        (gnus-summary-last-article-p (gnus-summary-article-number)))
6536    (and gnus-auto-select-same
6537         (gnus-summary-article-subject))))
6538
6539 (defun gnus-summary-prev-article (&optional unread subject)
6540   "Select the article after the current one.
6541 If UNREAD is non-nil, only unread articles are selected."
6542   (interactive "P")
6543   (gnus-summary-next-article unread subject t))
6544
6545 (defun gnus-summary-prev-unread-article ()
6546   "Select unread article before current one."
6547   (interactive)
6548   (gnus-summary-prev-article
6549    (or (not (eq gnus-summary-goto-unread 'never))
6550        (gnus-summary-first-article-p (gnus-summary-article-number)))
6551    (and gnus-auto-select-same
6552         (gnus-summary-article-subject))))
6553
6554 (defun gnus-summary-next-page (&optional lines circular)
6555   "Show next page of the selected article.
6556 If at the end of the current article, select the next article.
6557 LINES says how many lines should be scrolled up.
6558
6559 If CIRCULAR is non-nil, go to the start of the article instead of
6560 selecting the next article when reaching the end of the current
6561 article."
6562   (interactive "P")
6563   (setq gnus-summary-buffer (current-buffer))
6564   (gnus-set-global-variables)
6565   (let ((article (gnus-summary-article-number))
6566         (article-window (get-buffer-window gnus-article-buffer t))
6567         endp)
6568     ;; If the buffer is empty, we have no article.
6569     (unless article
6570       (error "No article to select"))
6571     (gnus-configure-windows 'article)
6572     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6573         (if (and (eq gnus-summary-goto-unread 'never)
6574                  (not (gnus-summary-last-article-p article)))
6575             (gnus-summary-next-article)
6576           (gnus-summary-next-unread-article))
6577       (if (or (null gnus-current-article)
6578               (null gnus-article-current)
6579               (/= article (cdr gnus-article-current))
6580               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6581           ;; Selected subject is different from current article's.
6582           (gnus-summary-display-article article)
6583         (when article-window
6584           (gnus-eval-in-buffer-window gnus-article-buffer
6585             (setq endp (gnus-article-next-page lines)))
6586           (when endp
6587             (cond (circular
6588                    (gnus-summary-beginning-of-article))
6589                   (lines
6590                    (gnus-message 3 "End of message"))
6591                   ((null lines)
6592                    (if (and (eq gnus-summary-goto-unread 'never)
6593                             (not (gnus-summary-last-article-p article)))
6594                        (gnus-summary-next-article)
6595                      (gnus-summary-next-unread-article))))))))
6596     (gnus-summary-recenter)
6597     (gnus-summary-position-point)))
6598
6599 (defun gnus-summary-prev-page (&optional lines move)
6600   "Show previous page of selected article.
6601 Argument LINES specifies lines to be scrolled down.
6602 If MOVE, move to the previous unread article if point is at
6603 the beginning of the buffer."
6604   (interactive "P")
6605   (let ((article (gnus-summary-article-number))
6606         (article-window (get-buffer-window gnus-article-buffer t))
6607         endp)
6608     (gnus-configure-windows 'article)
6609     (if (or (null gnus-current-article)
6610             (null gnus-article-current)
6611             (/= article (cdr gnus-article-current))
6612             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6613         ;; Selected subject is different from current article's.
6614         (gnus-summary-display-article article)
6615       (gnus-summary-recenter)
6616       (when article-window
6617         (gnus-eval-in-buffer-window gnus-article-buffer
6618           (setq endp (gnus-article-prev-page lines)))
6619         (when (and move endp)
6620           (cond (lines
6621                  (gnus-message 3 "Beginning of message"))
6622                 ((null lines)
6623                  (if (and (eq gnus-summary-goto-unread 'never)
6624                           (not (gnus-summary-first-article-p article)))
6625                      (gnus-summary-prev-article)
6626                    (gnus-summary-prev-unread-article))))))))
6627   (gnus-summary-position-point))
6628
6629 (defun gnus-summary-prev-page-or-article (&optional lines)
6630   "Show previous page of selected article.
6631 Argument LINES specifies lines to be scrolled down.
6632 If at the beginning of the article, go to the next article."
6633   (interactive "P")
6634   (gnus-summary-prev-page lines t))
6635
6636 (defun gnus-summary-scroll-up (lines)
6637   "Scroll up (or down) one line current article.
6638 Argument LINES specifies lines to be scrolled up (or down if negative)."
6639   (interactive "p")
6640   (gnus-configure-windows 'article)
6641   (gnus-summary-show-thread)
6642   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6643     (gnus-eval-in-buffer-window gnus-article-buffer
6644       (cond ((> lines 0)
6645              (when (gnus-article-next-page lines)
6646                (gnus-message 3 "End of message")))
6647             ((< lines 0)
6648              (gnus-article-prev-page (- lines))))))
6649   (gnus-summary-recenter)
6650   (gnus-summary-position-point))
6651
6652 (defun gnus-summary-scroll-down (lines)
6653   "Scroll down (or up) one line current article.
6654 Argument LINES specifies lines to be scrolled down (or up if negative)."
6655   (interactive "p")
6656   (gnus-summary-scroll-up (- lines)))
6657
6658 (defun gnus-summary-next-same-subject ()
6659   "Select next article which has the same subject as current one."
6660   (interactive)
6661   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6662
6663 (defun gnus-summary-prev-same-subject ()
6664   "Select previous article which has the same subject as current one."
6665   (interactive)
6666   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6667
6668 (defun gnus-summary-next-unread-same-subject ()
6669   "Select next unread article which has the same subject as current one."
6670   (interactive)
6671   (gnus-summary-next-article t (gnus-summary-article-subject)))
6672
6673 (defun gnus-summary-prev-unread-same-subject ()
6674   "Select previous unread article which has the same subject as current one."
6675   (interactive)
6676   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6677
6678 (defun gnus-summary-first-unread-article ()
6679   "Select the first unread article.
6680 Return nil if there are no unread articles."
6681   (interactive)
6682   (prog1
6683       (when (gnus-summary-first-subject t)
6684         (gnus-summary-show-thread)
6685         (gnus-summary-first-subject t)
6686         (gnus-summary-display-article (gnus-summary-article-number)))
6687     (gnus-summary-position-point)))
6688
6689 (defun gnus-summary-first-unread-subject ()
6690   "Place the point on the subject line of the first unread article.
6691 Return nil if there are no unread articles."
6692   (interactive)
6693   (prog1
6694       (when (gnus-summary-first-subject t)
6695         (gnus-summary-show-thread)
6696         (gnus-summary-first-subject t))
6697     (gnus-summary-position-point)))
6698
6699 (defun gnus-summary-first-article ()
6700   "Select the first article.
6701 Return nil if there are no articles."
6702   (interactive)
6703   (prog1
6704       (when (gnus-summary-first-subject)
6705         (gnus-summary-show-thread)
6706         (gnus-summary-first-subject)
6707         (gnus-summary-display-article (gnus-summary-article-number)))
6708     (gnus-summary-position-point)))
6709
6710 (defun gnus-summary-best-unread-article ()
6711   "Select the unread article with the highest score."
6712   (interactive)
6713   (let ((best -1000000)
6714         (data gnus-newsgroup-data)
6715         article score)
6716     (while data
6717       (and (gnus-data-unread-p (car data))
6718            (> (setq score
6719                     (gnus-summary-article-score (gnus-data-number (car data))))
6720               best)
6721            (setq best score
6722                  article (gnus-data-number (car data))))
6723       (setq data (cdr data)))
6724     (prog1
6725         (if article
6726             (gnus-summary-goto-article article)
6727           (error "No unread articles"))
6728       (gnus-summary-position-point))))
6729
6730 (defun gnus-summary-last-subject ()
6731   "Go to the last displayed subject line in the group."
6732   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6733     (when article
6734       (gnus-summary-goto-subject article))))
6735
6736 (defun gnus-summary-goto-article (article &optional all-headers force)
6737   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6738 If ALL-HEADERS is non-nil, no header lines are hidden.
6739 If FORCE, go to the article even if it isn't displayed.  If FORCE
6740 is a number, it is the line the article is to be displayed on."
6741   (interactive
6742    (list
6743     (completing-read
6744      "Article number or Message-ID: "
6745      (mapcar (lambda (number) (list (int-to-string number)))
6746              gnus-newsgroup-limit))
6747     current-prefix-arg
6748     t))
6749   (prog1
6750       (if (and (stringp article)
6751                (string-match "@" article))
6752           (gnus-summary-refer-article article)
6753         (when (stringp article)
6754           (setq article (string-to-number article)))
6755         (if (gnus-summary-goto-subject article force)
6756             (gnus-summary-display-article article all-headers)
6757           (gnus-message 4 "Couldn't go to article %s" article) nil))
6758     (gnus-summary-position-point)))
6759
6760 (defun gnus-summary-goto-last-article ()
6761   "Go to the previously read article."
6762   (interactive)
6763   (prog1
6764       (when gnus-last-article
6765         (gnus-summary-goto-article gnus-last-article nil t))
6766     (gnus-summary-position-point)))
6767
6768 (defun gnus-summary-pop-article (number)
6769   "Pop one article off the history and go to the previous.
6770 NUMBER articles will be popped off."
6771   (interactive "p")
6772   (let (to)
6773     (setq gnus-newsgroup-history
6774           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6775     (if to
6776         (gnus-summary-goto-article (car to) nil t)
6777       (error "Article history empty")))
6778   (gnus-summary-position-point))
6779
6780 ;; Summary commands and functions for limiting the summary buffer.
6781
6782 (defun gnus-summary-limit-to-articles (n)
6783   "Limit the summary buffer to the next N articles.
6784 If not given a prefix, use the process marked articles instead."
6785   (interactive "P")
6786   (prog1
6787       (let ((articles (gnus-summary-work-articles n)))
6788         (setq gnus-newsgroup-processable nil)
6789         (gnus-summary-limit articles))
6790     (gnus-summary-position-point)))
6791
6792 (defun gnus-summary-pop-limit (&optional total)
6793   "Restore the previous limit.
6794 If given a prefix, remove all limits."
6795   (interactive "P")
6796   (when total
6797     (setq gnus-newsgroup-limits
6798           (list (mapcar (lambda (h) (mail-header-number h))
6799                         gnus-newsgroup-headers))))
6800   (unless gnus-newsgroup-limits
6801     (error "No limit to pop"))
6802   (prog1
6803       (gnus-summary-limit nil 'pop)
6804     (gnus-summary-position-point)))
6805
6806 (defun gnus-summary-limit-to-subject (subject &optional header)
6807   "Limit the summary buffer to articles that have subjects that match a regexp."
6808   (interactive "sLimit to subject (regexp): ")
6809   (unless header
6810     (setq header "subject"))
6811   (when (not (equal "" subject))
6812     (prog1
6813         (let ((articles (gnus-summary-find-matching
6814                          (or header "subject") subject 'all)))
6815           (unless articles
6816             (error "Found no matches for \"%s\"" subject))
6817           (gnus-summary-limit articles))
6818       (gnus-summary-position-point))))
6819
6820 (defun gnus-summary-limit-to-author (from)
6821   "Limit the summary buffer to articles that have authors that match a regexp."
6822   (interactive "sLimit to author (regexp): ")
6823   (gnus-summary-limit-to-subject from "from"))
6824
6825 (defun gnus-summary-limit-to-age (age &optional younger-p)
6826   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6827 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6828 articles that are younger than AGE days."
6829   (interactive
6830    (let ((younger current-prefix-arg)
6831          (days-got nil)
6832          days)
6833      (while (not days-got)
6834        (setq days (if younger
6835                       (read-string "Limit to articles within (in days): ")
6836                     (read-string "Limit to articles older than (in days): ")))
6837        (when (> (length days) 0)
6838          (setq days (read days)))
6839        (if (numberp days)
6840            (setq days-got t)
6841          (message "Please enter a number.")
6842          (sleep-for 1)))
6843      (list days younger)))
6844   (prog1
6845       (let ((data gnus-newsgroup-data)
6846             (cutoff (days-to-time age))
6847             articles d date is-younger)
6848         (while (setq d (pop data))
6849           (when (and (vectorp (gnus-data-header d))
6850                      (setq date (mail-header-date (gnus-data-header d))))
6851             (setq is-younger (time-less-p
6852                               (time-since (condition-case ()
6853                                               (date-to-time date)
6854                                             (error '(0 0))))
6855                               cutoff))
6856             (when (if younger-p
6857                       is-younger
6858                     (not is-younger))
6859               (push (gnus-data-number d) articles))))
6860         (gnus-summary-limit (nreverse articles)))
6861     (gnus-summary-position-point)))
6862
6863 (defun gnus-summary-limit-to-extra (header regexp)
6864   "Limit the summary buffer to articles that match an 'extra' header."
6865   (interactive
6866    (let ((header
6867           (intern
6868            (gnus-completing-read
6869             (symbol-name (car gnus-extra-headers))
6870             "Limit extra header:"
6871             (mapcar (lambda (x)
6872                       (cons (symbol-name x) x))
6873                     gnus-extra-headers)
6874             nil
6875             t))))
6876      (list header
6877            (read-string (format "Limit to header %s (regexp): " header)))))
6878   (when (not (equal "" regexp))
6879     (prog1
6880         (let ((articles (gnus-summary-find-matching
6881                          (cons 'extra header) regexp 'all)))
6882           (unless articles
6883             (error "Found no matches for \"%s\"" regexp))
6884           (gnus-summary-limit articles))
6885       (gnus-summary-position-point))))
6886
6887 (defun gnus-summary-limit-to-display-predicate ()
6888   "Limit the summary buffer to the predicated in the `display' group parameter."
6889   (interactive)
6890   (unless gnus-newsgroup-display
6891     (error "There is no `diplay' group parameter"))
6892   (let (articles)
6893     (dolist (number gnus-newsgroup-articles)
6894       (when (funcall gnus-newsgroup-display)
6895         (push number articles)))
6896     (gnus-summary-limit articles))
6897   (gnus-summary-position-point))
6898
6899 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6900 (make-obsolete
6901  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6902
6903 (defun gnus-summary-limit-to-unread (&optional all)
6904   "Limit the summary buffer to articles that are not marked as read.
6905 If ALL is non-nil, limit strictly to unread articles."
6906   (interactive "P")
6907   (if all
6908       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6909     (gnus-summary-limit-to-marks
6910      ;; Concat all the marks that say that an article is read and have
6911      ;; those removed.
6912      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6913            gnus-killed-mark gnus-kill-file-mark
6914            gnus-low-score-mark gnus-expirable-mark
6915            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6916            gnus-duplicate-mark gnus-souped-mark)
6917      'reverse)))
6918
6919 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6920 (make-obsolete 'gnus-summary-delete-marked-with
6921                'gnus-summary-limit-exlude-marks)
6922
6923 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6924   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6925 If REVERSE, limit the summary buffer to articles that are marked
6926 with MARKS.  MARKS can either be a string of marks or a list of marks.
6927 Returns how many articles were removed."
6928   (interactive "sMarks: ")
6929   (gnus-summary-limit-to-marks marks t))
6930
6931 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6932   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6933 If REVERSE (the prefix), limit the summary buffer to articles that are
6934 not marked with MARKS.  MARKS can either be a string of marks or a
6935 list of marks.
6936 Returns how many articles were removed."
6937   (interactive "sMarks: \nP")
6938   (prog1
6939       (let ((data gnus-newsgroup-data)
6940             (marks (if (listp marks) marks
6941                      (append marks nil))) ; Transform to list.
6942             articles)
6943         (while data
6944           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6945                   (memq (gnus-data-mark (car data)) marks))
6946             (push (gnus-data-number (car data)) articles))
6947           (setq data (cdr data)))
6948         (gnus-summary-limit articles))
6949     (gnus-summary-position-point)))
6950
6951 (defun gnus-summary-limit-to-score (&optional score)
6952   "Limit to articles with score at or above SCORE."
6953   (interactive "P")
6954   (setq score (if score
6955                   (prefix-numeric-value score)
6956                 (or gnus-summary-default-score 0)))
6957   (let ((data gnus-newsgroup-data)
6958         articles)
6959     (while data
6960       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6961                 score)
6962         (push (gnus-data-number (car data)) articles))
6963       (setq data (cdr data)))
6964     (prog1
6965         (gnus-summary-limit articles)
6966       (gnus-summary-position-point))))
6967
6968 (defun gnus-summary-limit-include-thread (id)
6969   "Display all the hidden articles that is in the thread with ID in it.
6970 When called interactively, ID is the Message-ID of the current
6971 article."
6972   (interactive (list (mail-header-id (gnus-summary-article-header))))
6973   (let ((articles (gnus-articles-in-thread
6974                    (gnus-id-to-thread (gnus-root-id id)))))
6975     (prog1
6976         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6977       (gnus-summary-limit-include-matching-articles
6978        "subject"
6979        (regexp-quote (gnus-simplify-subject-re
6980                       (mail-header-subject (gnus-id-to-header id)))))
6981       (gnus-summary-position-point))))
6982
6983 (defun gnus-summary-limit-include-matching-articles (header regexp)
6984   "Display all the hidden articles that have HEADERs that match REGEXP."
6985   (interactive (list (read-string "Match on header: ")
6986                      (read-string "Regexp: ")))
6987   (let ((articles (gnus-find-matching-articles header regexp)))
6988     (prog1
6989         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6990       (gnus-summary-position-point))))
6991
6992 (defun gnus-summary-limit-include-dormant ()
6993   "Display all the hidden articles that are marked as dormant.
6994 Note that this command only works on a subset of the articles currently
6995 fetched for this group."
6996   (interactive)
6997   (unless gnus-newsgroup-dormant
6998     (error "There are no dormant articles in this group"))
6999   (prog1
7000       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7001     (gnus-summary-position-point)))
7002
7003 (defun gnus-summary-limit-exclude-dormant ()
7004   "Hide all dormant articles."
7005   (interactive)
7006   (prog1
7007       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7008     (gnus-summary-position-point)))
7009
7010 (defun gnus-summary-limit-exclude-childless-dormant ()
7011   "Hide all dormant articles that have no children."
7012   (interactive)
7013   (let ((data (gnus-data-list t))
7014         articles d children)
7015     ;; Find all articles that are either not dormant or have
7016     ;; children.
7017     (while (setq d (pop data))
7018       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7019                 (and (setq children
7020                            (gnus-article-children (gnus-data-number d)))
7021                      (let (found)
7022                        (while children
7023                          (when (memq (car children) articles)
7024                            (setq children nil
7025                                  found t))
7026                          (pop children))
7027                        found)))
7028         (push (gnus-data-number d) articles)))
7029     ;; Do the limiting.
7030     (prog1
7031         (gnus-summary-limit articles)
7032       (gnus-summary-position-point))))
7033
7034 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7035   "Mark all unread excluded articles as read.
7036 If ALL, mark even excluded ticked and dormants as read."
7037   (interactive "P")
7038   (let ((articles (gnus-sorted-complement
7039                    (sort
7040                     (mapcar (lambda (h) (mail-header-number h))
7041                             gnus-newsgroup-headers)
7042                     '<)
7043                    (sort gnus-newsgroup-limit '<)))
7044         article)
7045     (setq gnus-newsgroup-unreads
7046           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7047     (if all
7048         (setq gnus-newsgroup-dormant nil
7049               gnus-newsgroup-marked nil
7050               gnus-newsgroup-reads
7051               (nconc
7052                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7053                gnus-newsgroup-reads))
7054       (while (setq article (pop articles))
7055         (unless (or (memq article gnus-newsgroup-dormant)
7056                     (memq article gnus-newsgroup-marked))
7057           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7058
7059 (defun gnus-summary-limit (articles &optional pop)
7060   (if pop
7061       ;; We pop the previous limit off the stack and use that.
7062       (setq articles (car gnus-newsgroup-limits)
7063             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7064     ;; We use the new limit, so we push the old limit on the stack.
7065     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7066   ;; Set the limit.
7067   (setq gnus-newsgroup-limit articles)
7068   (let ((total (length gnus-newsgroup-data))
7069         (data (gnus-data-find-list (gnus-summary-article-number)))
7070         (gnus-summary-mark-below nil)   ; Inhibit this.
7071         found)
7072     ;; This will do all the work of generating the new summary buffer
7073     ;; according to the new limit.
7074     (gnus-summary-prepare)
7075     ;; Hide any threads, possibly.
7076     (and gnus-show-threads
7077          gnus-thread-hide-subtree
7078          (gnus-summary-hide-all-threads))
7079     ;; Try to return to the article you were at, or one in the
7080     ;; neighborhood.
7081     (when data
7082       ;; We try to find some article after the current one.
7083       (while data
7084         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7085           (setq data nil
7086                 found t))
7087         (setq data (cdr data))))
7088     (unless found
7089       ;; If there is no data, that means that we were after the last
7090       ;; article.  The same goes when we can't find any articles
7091       ;; after the current one.
7092       (goto-char (point-max))
7093       (gnus-summary-find-prev))
7094     (gnus-set-mode-line 'summary)
7095     ;; We return how many articles were removed from the summary
7096     ;; buffer as a result of the new limit.
7097     (- total (length gnus-newsgroup-data))))
7098
7099 (defsubst gnus-invisible-cut-children (threads)
7100   (let ((num 0))
7101     (while threads
7102       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7103         (incf num))
7104       (pop threads))
7105     (< num 2)))
7106
7107 (defsubst gnus-cut-thread (thread)
7108   "Go forwards in the thread until we find an article that we want to display."
7109   (when (or (eq gnus-fetch-old-headers 'some)
7110             (eq gnus-fetch-old-headers 'invisible)
7111             (numberp gnus-fetch-old-headers)
7112             (eq gnus-build-sparse-threads 'some)
7113             (eq gnus-build-sparse-threads 'more))
7114     ;; Deal with old-fetched headers and sparse threads.
7115     (while (and
7116             thread
7117             (or
7118              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7119              (gnus-summary-article-ancient-p
7120               (mail-header-number (car thread))))
7121             (if (or (<= (length (cdr thread)) 1)
7122                     (eq gnus-fetch-old-headers 'invisible))
7123                 (setq gnus-newsgroup-limit
7124                       (delq (mail-header-number (car thread))
7125                             gnus-newsgroup-limit)
7126                       thread (cadr thread))
7127               (when (gnus-invisible-cut-children (cdr thread))
7128                 (let ((th (cdr thread)))
7129                   (while th
7130                     (if (memq (mail-header-number (caar th))
7131                               gnus-newsgroup-limit)
7132                         (setq thread (car th)
7133                               th nil)
7134                       (setq th (cdr th))))))))))
7135   thread)
7136
7137 (defun gnus-cut-threads (threads)
7138   "Cut off all uninteresting articles from the beginning of threads."
7139   (when (or (eq gnus-fetch-old-headers 'some)
7140             (eq gnus-fetch-old-headers 'invisible)
7141             (numberp gnus-fetch-old-headers)
7142             (eq gnus-build-sparse-threads 'some)
7143             (eq gnus-build-sparse-threads 'more))
7144     (let ((th threads))
7145       (while th
7146         (setcar th (gnus-cut-thread (car th)))
7147         (setq th (cdr th)))))
7148   ;; Remove nixed out threads.
7149   (delq nil threads))
7150
7151 (defun gnus-summary-initial-limit (&optional show-if-empty)
7152   "Figure out what the initial limit is supposed to be on group entry.
7153 This entails weeding out unwanted dormants, low-scored articles,
7154 fetch-old-headers verbiage, and so on."
7155   ;; Most groups have nothing to remove.
7156   (if (or gnus-inhibit-limiting
7157           (and (null gnus-newsgroup-dormant)
7158                (eq gnus-newsgroup-display 'gnus-not-ignore)
7159                (not (eq gnus-fetch-old-headers 'some))
7160                (not (numberp gnus-fetch-old-headers))
7161                (not (eq gnus-fetch-old-headers 'invisible))
7162                (null gnus-summary-expunge-below)
7163                (not (eq gnus-build-sparse-threads 'some))
7164                (not (eq gnus-build-sparse-threads 'more))
7165                (null gnus-thread-expunge-below)
7166                (not gnus-use-nocem)))
7167       ()                                ; Do nothing.
7168     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7169     (setq gnus-newsgroup-limit nil)
7170     (mapatoms
7171      (lambda (node)
7172        (unless (car (symbol-value node))
7173          ;; These threads have no parents -- they are roots.
7174          (let ((nodes (cdr (symbol-value node)))
7175                thread)
7176            (while nodes
7177              (if (and gnus-thread-expunge-below
7178                       (< (gnus-thread-total-score (car nodes))
7179                          gnus-thread-expunge-below))
7180                  (gnus-expunge-thread (pop nodes))
7181                (setq thread (pop nodes))
7182                (gnus-summary-limit-children thread))))))
7183      gnus-newsgroup-dependencies)
7184     ;; If this limitation resulted in an empty group, we might
7185     ;; pop the previous limit and use it instead.
7186     (when (and (not gnus-newsgroup-limit)
7187                show-if-empty)
7188       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7189     gnus-newsgroup-limit))
7190
7191 (defun gnus-summary-limit-children (thread)
7192   "Return 1 if this subthread is visible and 0 if it is not."
7193   ;; First we get the number of visible children to this thread.  This
7194   ;; is done by recursing down the thread using this function, so this
7195   ;; will really go down to a leaf article first, before slowly
7196   ;; working its way up towards the root.
7197   (when thread
7198     (let ((children
7199            (if (cdr thread)
7200                (apply '+ (mapcar 'gnus-summary-limit-children
7201                                  (cdr thread)))
7202              0))
7203           (number (mail-header-number (car thread)))
7204           score)
7205       (if (and
7206            (not (memq number gnus-newsgroup-marked))
7207            (or
7208             ;; If this article is dormant and has absolutely no visible
7209             ;; children, then this article isn't visible.
7210             (and (memq number gnus-newsgroup-dormant)
7211                  (zerop children))
7212             ;; If this is "fetch-old-headered" and there is no
7213             ;; visible children, then we don't want this article.
7214             (and (or (eq gnus-fetch-old-headers 'some)
7215                      (numberp gnus-fetch-old-headers))
7216                  (gnus-summary-article-ancient-p number)
7217                  (zerop children))
7218             ;; If this is "fetch-old-headered" and `invisible', then
7219             ;; we don't want this article.
7220             (and (eq gnus-fetch-old-headers 'invisible)
7221                  (gnus-summary-article-ancient-p number))
7222             ;; If this is a sparsely inserted article with no children,
7223             ;; we don't want it.
7224             (and (eq gnus-build-sparse-threads 'some)
7225                  (gnus-summary-article-sparse-p number)
7226                  (zerop children))
7227             ;; If we use expunging, and this article is really
7228             ;; low-scored, then we don't want this article.
7229             (when (and gnus-summary-expunge-below
7230                        (< (setq score
7231                                 (or (cdr (assq number gnus-newsgroup-scored))
7232                                     gnus-summary-default-score))
7233                           gnus-summary-expunge-below))
7234               ;; We increase the expunge-tally here, but that has
7235               ;; nothing to do with the limits, really.
7236               (incf gnus-newsgroup-expunged-tally)
7237               ;; We also mark as read here, if that's wanted.
7238               (when (and gnus-summary-mark-below
7239                          (< score gnus-summary-mark-below))
7240                 (setq gnus-newsgroup-unreads
7241                       (delq number gnus-newsgroup-unreads))
7242                 (if gnus-newsgroup-auto-expire
7243                     (push number gnus-newsgroup-expirable)
7244                   (push (cons number gnus-low-score-mark)
7245                         gnus-newsgroup-reads)))
7246               t)
7247             ;; Do the `display' group parameter.
7248             (and gnus-newsgroup-display
7249                  (not (funcall gnus-newsgroup-display)))
7250             ;; Check NoCeM things.
7251             (if (and gnus-use-nocem
7252                      (gnus-nocem-unwanted-article-p
7253                       (mail-header-id (car thread))))
7254                 (progn
7255                   (setq gnus-newsgroup-unreads
7256                         (delq number gnus-newsgroup-unreads))
7257                   t))))
7258           ;; Nope, invisible article.
7259           0
7260         ;; Ok, this article is to be visible, so we add it to the limit
7261         ;; and return 1.
7262         (push number gnus-newsgroup-limit)
7263         1))))
7264
7265 (defun gnus-expunge-thread (thread)
7266   "Mark all articles in THREAD as read."
7267   (let* ((number (mail-header-number (car thread))))
7268     (incf gnus-newsgroup-expunged-tally)
7269     ;; We also mark as read here, if that's wanted.
7270     (setq gnus-newsgroup-unreads
7271           (delq number gnus-newsgroup-unreads))
7272     (if gnus-newsgroup-auto-expire
7273         (push number gnus-newsgroup-expirable)
7274       (push (cons number gnus-low-score-mark)
7275             gnus-newsgroup-reads)))
7276   ;; Go recursively through all subthreads.
7277   (mapcar 'gnus-expunge-thread (cdr thread)))
7278
7279 ;; Summary article oriented commands
7280
7281 (defun gnus-summary-refer-parent-article (n)
7282   "Refer parent article N times.
7283 If N is negative, go to ancestor -N instead.
7284 The difference between N and the number of articles fetched is returned."
7285   (interactive "p")
7286   (let ((skip 1)
7287         error header ref)
7288     (when (not (natnump n))
7289       (setq skip (abs n)
7290             n 1))
7291     (while (and (> n 0)
7292                 (not error))
7293       (setq header (gnus-summary-article-header))
7294       (if (and (eq (mail-header-number header)
7295                    (cdr gnus-article-current))
7296                (equal gnus-newsgroup-name
7297                       (car gnus-article-current)))
7298           ;; If we try to find the parent of the currently
7299           ;; displayed article, then we take a look at the actual
7300           ;; References header, since this is slightly more
7301           ;; reliable than the References field we got from the
7302           ;; server.
7303           (save-excursion
7304             (set-buffer gnus-original-article-buffer)
7305             (nnheader-narrow-to-headers)
7306             (unless (setq ref (message-fetch-field "references"))
7307               (setq ref (message-fetch-field "in-reply-to")))
7308             (widen))
7309         (setq ref
7310               ;; It's not the current article, so we take a bet on
7311               ;; the value we got from the server.
7312               (mail-header-references header)))
7313       (if (and ref
7314                (not (equal ref "")))
7315           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7316             (gnus-message 1 "Couldn't find parent"))
7317         (gnus-message 1 "No references in article %d"
7318                       (gnus-summary-article-number))
7319         (setq error t))
7320       (decf n))
7321     (gnus-summary-position-point)
7322     n))
7323
7324 (defun gnus-summary-refer-references ()
7325   "Fetch all articles mentioned in the References header.
7326 Return the number of articles fetched."
7327   (interactive)
7328   (let ((ref (mail-header-references (gnus-summary-article-header)))
7329         (current (gnus-summary-article-number))
7330         (n 0))
7331     (if (or (not ref)
7332             (equal ref ""))
7333         (error "No References in the current article")
7334       ;; For each Message-ID in the References header...
7335       (while (string-match "<[^>]*>" ref)
7336         (incf n)
7337         ;; ... fetch that article.
7338         (gnus-summary-refer-article
7339          (prog1 (match-string 0 ref)
7340            (setq ref (substring ref (match-end 0))))))
7341       (gnus-summary-goto-subject current)
7342       (gnus-summary-position-point)
7343       n)))
7344
7345 (defun gnus-summary-refer-thread (&optional limit)
7346   "Fetch all articles in the current thread.
7347 If LIMIT (the numerical prefix), fetch that many old headers instead
7348 of what's specified by the `gnus-refer-thread-limit' variable."
7349   (interactive "P")
7350   (let ((id (mail-header-id (gnus-summary-article-header)))
7351         (limit (if limit (prefix-numeric-value limit)
7352                  gnus-refer-thread-limit)))
7353     ;; We want to fetch LIMIT *old* headers, but we also have to
7354     ;; re-fetch all the headers in the current buffer, because many of
7355     ;; them may be undisplayed.  So we adjust LIMIT.
7356     (when (numberp limit)
7357       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7358     (unless (eq gnus-fetch-old-headers 'invisible)
7359       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7360       ;; Retrieve the headers and read them in.
7361       (if (eq (gnus-retrieve-headers
7362                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7363               'nov)
7364           (gnus-build-all-threads)
7365         (error "Can't fetch thread from backends that don't support NOV"))
7366       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7367     (gnus-summary-limit-include-thread id)))
7368
7369 (defun gnus-summary-refer-article (message-id)
7370   "Fetch an article specified by MESSAGE-ID."
7371   (interactive "sMessage-ID: ")
7372   (when (and (stringp message-id)
7373              (not (zerop (length message-id))))
7374     ;; Construct the correct Message-ID if necessary.
7375     ;; Suggested by tale@pawl.rpi.edu.
7376     (unless (string-match "^<" message-id)
7377       (setq message-id (concat "<" message-id)))
7378     (unless (string-match ">$" message-id)
7379       (setq message-id (concat message-id ">")))
7380     (let* ((header (gnus-id-to-header message-id))
7381            (sparse (and header
7382                         (gnus-summary-article-sparse-p
7383                          (mail-header-number header))
7384                         (memq (mail-header-number header)
7385                               gnus-newsgroup-limit)))
7386            number)
7387       (cond
7388        ;; If the article is present in the buffer we just go to it.
7389        ((and header
7390              (or (not (gnus-summary-article-sparse-p
7391                        (mail-header-number header)))
7392                  sparse))
7393         (prog1
7394             (gnus-summary-goto-article
7395              (mail-header-number header) nil t)
7396           (when sparse
7397             (gnus-summary-update-article (mail-header-number header)))))
7398        (t
7399         ;; We fetch the article.
7400         (catch 'found
7401           (dolist (gnus-override-method (gnus-refer-article-methods))
7402             (gnus-check-server gnus-override-method)
7403             ;; Fetch the header, and display the article.
7404             (when (setq number (gnus-summary-insert-subject message-id))
7405               (gnus-summary-select-article nil nil nil number)
7406               (throw 'found t)))
7407           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7408
7409 (defun gnus-refer-article-methods ()
7410   "Return a list of referrable methods."
7411   (cond
7412    ;; No method, so we default to current and native.
7413    ((null gnus-refer-article-method)
7414     (list gnus-current-select-method gnus-select-method))
7415    ;; Current.
7416    ((eq 'current gnus-refer-article-method)
7417     (list gnus-current-select-method))
7418    ;; List of select methods.
7419    ((not (and (symbolp (car gnus-refer-article-method))
7420               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7421     (let (out)
7422       (dolist (method gnus-refer-article-method)
7423         (push (if (eq 'current method)
7424                   gnus-current-select-method
7425                 method)
7426               out))
7427       (nreverse out)))
7428    ;; One single select method.
7429    (t
7430     (list gnus-refer-article-method))))
7431
7432 (defun gnus-summary-edit-parameters ()
7433   "Edit the group parameters of the current group."
7434   (interactive)
7435   (gnus-group-edit-group gnus-newsgroup-name 'params))
7436
7437 (defun gnus-summary-customize-parameters ()
7438   "Customize the group parameters of the current group."
7439   (interactive)
7440   (gnus-group-customize gnus-newsgroup-name))
7441
7442 (defun gnus-summary-enter-digest-group (&optional force)
7443   "Enter an nndoc group based on the current article.
7444 If FORCE, force a digest interpretation.  If not, try
7445 to guess what the document format is."
7446   (interactive "P")
7447   (let ((conf gnus-current-window-configuration))
7448     (save-excursion
7449       (gnus-summary-select-article))
7450     (setq gnus-current-window-configuration conf)
7451     (let* ((name (format "%s-%d"
7452                          (gnus-group-prefixed-name
7453                           gnus-newsgroup-name (list 'nndoc ""))
7454                          (save-excursion
7455                            (set-buffer gnus-summary-buffer)
7456                            gnus-current-article)))
7457            (ogroup gnus-newsgroup-name)
7458            (params (append (gnus-info-params (gnus-get-info ogroup))
7459                            (list (cons 'to-group ogroup))
7460                            (list (cons 'save-article-group ogroup))))
7461            (case-fold-search t)
7462            (buf (current-buffer))
7463            dig to-address)
7464       (save-excursion
7465         (set-buffer gnus-original-article-buffer)
7466         ;; Have the digest group inherit the main mail address of
7467         ;; the parent article.
7468         (when (setq to-address (or (message-fetch-field "reply-to")
7469                                    (message-fetch-field "from")))
7470           (setq params (append
7471                         (list (cons 'to-address
7472                                     (funcall gnus-decode-encoded-word-function
7473                                              to-address))))))
7474         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7475         (insert-buffer-substring gnus-original-article-buffer)
7476         ;; Remove lines that may lead nndoc to misinterpret the
7477         ;; document type.
7478         (narrow-to-region
7479          (goto-char (point-min))
7480          (or (search-forward "\n\n" nil t) (point)))
7481         (goto-char (point-min))
7482         (delete-matching-lines "^Path:\\|^From ")
7483         (widen))
7484       (unwind-protect
7485           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7486                     (gnus-newsgroup-ephemeral-ignored-charsets
7487                      gnus-newsgroup-ignored-charsets))
7488                 (gnus-group-read-ephemeral-group
7489                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7490                               (nndoc-article-type
7491                                ,(if force 'mbox 'guess))) t))
7492             ;; Make all postings to this group go to the parent group.
7493               (nconc (gnus-info-params (gnus-get-info name))
7494                      params)
7495             ;; Couldn't select this doc group.
7496             (switch-to-buffer buf)
7497             (gnus-set-global-variables)
7498             (gnus-configure-windows 'summary)
7499             (gnus-message 3 "Article couldn't be entered?"))
7500         (kill-buffer dig)))))
7501
7502 (defun gnus-summary-read-document (n)
7503   "Open a new group based on the current article(s).
7504 This will allow you to read digests and other similar
7505 documents as newsgroups.
7506 Obeys the standard process/prefix convention."
7507   (interactive "P")
7508   (let* ((articles (gnus-summary-work-articles n))
7509          (ogroup gnus-newsgroup-name)
7510          (params (append (gnus-info-params (gnus-get-info ogroup))
7511                          (list (cons 'to-group ogroup))))
7512          article group egroup groups vgroup)
7513     (while (setq article (pop articles))
7514       (setq group (format "%s-%d" gnus-newsgroup-name article))
7515       (gnus-summary-remove-process-mark article)
7516       (when (gnus-summary-display-article article)
7517         (save-excursion
7518           (with-temp-buffer
7519             (insert-buffer-substring gnus-original-article-buffer)
7520             ;; Remove some headers that may lead nndoc to make
7521             ;; the wrong guess.
7522             (message-narrow-to-head)
7523             (goto-char (point-min))
7524             (delete-matching-lines "^\\(Path\\):\\|^From ")
7525             (widen)
7526             (if (setq egroup
7527                       (gnus-group-read-ephemeral-group
7528                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7529                                      (nndoc-article-type guess))
7530                        t nil t))
7531                 (progn
7532             ;; Make all postings to this group go to the parent group.
7533                   (nconc (gnus-info-params (gnus-get-info egroup))
7534                          params)
7535                   (push egroup groups))
7536               ;; Couldn't select this doc group.
7537               (gnus-error 3 "Article couldn't be entered"))))))
7538     ;; Now we have selected all the documents.
7539     (cond
7540      ((not groups)
7541       (error "None of the articles could be interpreted as documents"))
7542      ((gnus-group-read-ephemeral-group
7543        (setq vgroup (format
7544                      "nnvirtual:%s-%s" gnus-newsgroup-name
7545                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7546        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7547        t
7548        (cons (current-buffer) 'summary)))
7549      (t
7550       (error "Couldn't select virtual nndoc group")))))
7551
7552 (defun gnus-summary-isearch-article (&optional regexp-p)
7553   "Do incremental search forward on the current article.
7554 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7555   (interactive "P")
7556   (gnus-summary-select-article)
7557   (gnus-configure-windows 'article)
7558   (gnus-eval-in-buffer-window gnus-article-buffer
7559     (save-restriction
7560       (widen)
7561       (isearch-forward regexp-p))))
7562
7563 (defun gnus-summary-search-article-forward (regexp &optional backward)
7564   "Search for an article containing REGEXP forward.
7565 If BACKWARD, search backward instead."
7566   (interactive
7567    (list (read-string
7568           (format "Search article %s (regexp%s): "
7569                   (if current-prefix-arg "backward" "forward")
7570                   (if gnus-last-search-regexp
7571                       (concat ", default " gnus-last-search-regexp)
7572                     "")))
7573          current-prefix-arg))
7574   (if (string-equal regexp "")
7575       (setq regexp (or gnus-last-search-regexp ""))
7576     (setq gnus-last-search-regexp regexp)
7577     (setq gnus-article-before-search gnus-current-article))
7578   ;; Intentionally set gnus-last-article.
7579   (setq gnus-last-article gnus-article-before-search)
7580   (let ((gnus-last-article gnus-last-article))
7581     (if (gnus-summary-search-article regexp backward)
7582         (gnus-summary-show-thread)
7583       (error "Search failed: \"%s\"" regexp))))
7584
7585 (defun gnus-summary-search-article-backward (regexp)
7586   "Search for an article containing REGEXP backward."
7587   (interactive
7588    (list (read-string
7589           (format "Search article backward (regexp%s): "
7590                   (if gnus-last-search-regexp
7591                       (concat ", default " gnus-last-search-regexp)
7592                     "")))))
7593   (gnus-summary-search-article-forward regexp 'backward))
7594
7595 (defun gnus-summary-search-article (regexp &optional backward)
7596   "Search for an article containing REGEXP.
7597 Optional argument BACKWARD means do search for backward.
7598 `gnus-select-article-hook' is not called during the search."
7599   ;; We have to require this here to make sure that the following
7600   ;; dynamic binding isn't shadowed by autoloading.
7601   (require 'gnus-async)
7602   (require 'gnus-art)
7603   (let ((gnus-select-article-hook nil)  ;Disable hook.
7604         (gnus-article-prepare-hook nil)
7605         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7606         (gnus-use-article-prefetch nil)
7607         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7608         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7609         (sum (current-buffer))
7610         (gnus-display-mime-function nil)
7611         (found nil)
7612         point)
7613     (gnus-save-hidden-threads
7614       (gnus-summary-select-article)
7615       (set-buffer gnus-article-buffer)
7616       (goto-char (window-point (get-buffer-window (current-buffer))))
7617       (when backward
7618         (forward-line -1))
7619       (while (not found)
7620         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7621         (if (if backward
7622                 (re-search-backward regexp nil t)
7623               (re-search-forward regexp nil t))
7624             ;; We found the regexp.
7625             (progn
7626               (setq found 'found)
7627               (beginning-of-line)
7628               (set-window-start
7629                (get-buffer-window (current-buffer))
7630                (point))
7631               (forward-line 1)
7632               (set-window-point
7633                (get-buffer-window (current-buffer))
7634                (point))
7635               (set-buffer sum)
7636               (setq point (point)))
7637           ;; We didn't find it, so we go to the next article.
7638           (set-buffer sum)
7639           (setq found 'not)
7640           (while (eq found 'not)
7641             (if (not (if backward (gnus-summary-find-prev)
7642                        (gnus-summary-find-next)))
7643                 ;; No more articles.
7644                 (setq found t)
7645               ;; Select the next article and adjust point.
7646               (unless (gnus-summary-article-sparse-p
7647                        (gnus-summary-article-number))
7648                 (setq found nil)
7649                 (gnus-summary-select-article)
7650                 (set-buffer gnus-article-buffer)
7651                 (widen)
7652                 (goto-char (if backward (point-max) (point-min))))))))
7653       (gnus-message 7 ""))
7654     ;; Return whether we found the regexp.
7655     (when (eq found 'found)
7656       (goto-char point)
7657       (gnus-summary-show-thread)
7658       (gnus-summary-goto-subject gnus-current-article)
7659       (gnus-summary-position-point)
7660       t)))
7661
7662 (defun gnus-find-matching-articles (header regexp)
7663   "Return a list of all articles that match REGEXP on HEADER.
7664 This search includes all articles in the current group that Gnus has
7665 fetched headers for, whether they are displayed or not."
7666   (let ((articles nil)
7667         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7668         (case-fold-search t))
7669     (dolist (header gnus-newsgroup-headers)
7670       (when (string-match regexp (funcall func header))
7671         (push (mail-header-number header) articles)))
7672     (nreverse articles)))
7673
7674 (defun gnus-summary-find-matching (header regexp &optional backward unread
7675                                           not-case-fold)
7676   "Return a list of all articles that match REGEXP on HEADER.
7677 The search stars on the current article and goes forwards unless
7678 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7679 If UNREAD is non-nil, only unread articles will
7680 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7681 in the comparisons."
7682   (let ((case-fold-search (not not-case-fold))
7683         articles d func)
7684     (if (consp header)
7685         (if (eq (car header) 'extra)
7686             (setq func
7687                   `(lambda (h)
7688                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7689                          "")))
7690           (error "%s is an invalid header" header))
7691       (unless (fboundp (intern (concat "mail-header-" header)))
7692         (error "%s is not a valid header" header))
7693       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7694     (dolist (d (if (eq backward 'all)
7695                    gnus-newsgroup-data
7696                  (gnus-data-find-list
7697                   (gnus-summary-article-number)
7698                   (gnus-data-list backward))))
7699       (when (and (or (not unread)       ; We want all articles...
7700                      (gnus-data-unread-p d)) ; Or just unreads.
7701                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7702                  (string-match regexp
7703                                (funcall func (gnus-data-header d)))) ; Match.
7704         (push (gnus-data-number d) articles))) ; Success!
7705     (nreverse articles)))
7706
7707 (defun gnus-summary-execute-command (header regexp command &optional backward)
7708   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7709 If HEADER is an empty string (or nil), the match is done on the entire
7710 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7711   (interactive
7712    (list (let ((completion-ignore-case t))
7713            (completing-read
7714             "Header name: "
7715             (mapcar (lambda (header) (list (format "%s" header)))
7716                     (append
7717                      '("Number" "Subject" "From" "Lines" "Date"
7718                        "Message-ID" "Xref" "References" "Body")
7719                      gnus-extra-headers))
7720             nil 'require-match))
7721          (read-string "Regexp: ")
7722          (read-key-sequence "Command: ")
7723          current-prefix-arg))
7724   (when (equal header "Body")
7725     (setq header ""))
7726   ;; Hidden thread subtrees must be searched as well.
7727   (gnus-summary-show-all-threads)
7728   ;; We don't want to change current point nor window configuration.
7729   (save-excursion
7730     (save-window-excursion
7731       (gnus-message 6 "Executing %s..." (key-description command))
7732 ;; We'd like to execute COMMAND interactively so as to give arguments.
7733       (gnus-execute header regexp
7734                     `(call-interactively ',(key-binding command))
7735                     backward)
7736       (gnus-message 6 "Executing %s...done" (key-description command)))))
7737
7738 (defun gnus-summary-beginning-of-article ()
7739   "Scroll the article back to the beginning."
7740   (interactive)
7741   (gnus-summary-select-article)
7742   (gnus-configure-windows 'article)
7743   (gnus-eval-in-buffer-window gnus-article-buffer
7744     (widen)
7745     (goto-char (point-min))
7746     (when gnus-page-broken
7747       (gnus-narrow-to-page))))
7748
7749 (defun gnus-summary-end-of-article ()
7750   "Scroll to the end of the article."
7751   (interactive)
7752   (gnus-summary-select-article)
7753   (gnus-configure-windows 'article)
7754   (gnus-eval-in-buffer-window gnus-article-buffer
7755     (widen)
7756     (goto-char (point-max))
7757     (recenter -3)
7758     (when gnus-page-broken
7759       (gnus-narrow-to-page))))
7760
7761 (defun gnus-summary-print-article (&optional filename n)
7762   "Generate and print a PostScript image of the N next (mail) articles.
7763
7764 If N is negative, print the N previous articles.  If N is nil and articles
7765 have been marked with the process mark, print these instead.
7766
7767 If the optional first argument FILENAME is nil, send the image to the
7768 printer.  If FILENAME is a string, save the PostScript image in a file with
7769 that name.  If FILENAME is a number, prompt the user for the name of the file
7770 to save in."
7771   (interactive (list (ps-print-preprint current-prefix-arg)))
7772   (dolist (article (gnus-summary-work-articles n))
7773     (gnus-summary-select-article nil nil 'pseudo article)
7774     (gnus-eval-in-buffer-window gnus-article-buffer
7775       (let ((buffer (generate-new-buffer " *print*")))
7776         (unwind-protect
7777             (progn
7778               (copy-to-buffer buffer (point-min) (point-max))
7779               (set-buffer buffer)
7780               (gnus-article-delete-invisible-text)
7781               (when (gnus-visual-p 'article-highlight 'highlight)
7782                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7783                 ;; highlight.
7784                 (let ((gnus-article-buffer buffer))
7785                   (gnus-article-highlight-citation t)
7786                   (gnus-article-highlight-signature)))
7787               (let ((ps-left-header
7788                      (list
7789                       (concat "("
7790                               (mail-header-subject gnus-current-headers) ")")
7791                       (concat "("
7792                               (mail-header-from gnus-current-headers) ")")))
7793                     (ps-right-header
7794                      (list
7795                       "/pagenumberstring load"
7796                       (concat "("
7797                               (mail-header-date gnus-current-headers) ")"))))
7798                 (gnus-run-hooks 'gnus-ps-print-hook)
7799                 (save-excursion
7800                   (if window-system
7801                       (ps-spool-buffer-with-faces)
7802                     (ps-spool-buffer)))))
7803           (kill-buffer buffer))))
7804     (gnus-summary-remove-process-mark article))
7805   (ps-despool filename))
7806
7807 (defun gnus-summary-show-article (&optional arg)
7808   "Force re-fetching of the current article.
7809 If ARG (the prefix) is a number, show the article with the charset
7810 defined in `gnus-summary-show-article-charset-alist', or the charset
7811 input.
7812 If ARG (the prefix) is non-nil and not a number, show the raw article
7813 without any article massaging functions being run."
7814   (interactive "P")
7815   (cond
7816    ((numberp arg)
7817     (gnus-summary-show-article t)
7818     (let ((gnus-newsgroup-charset
7819            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7820                (mm-read-coding-system
7821                 "View as charset: "
7822                 (save-excursion
7823                   (set-buffer gnus-article-buffer)
7824                   (detect-coding-region (point) (point-max) t)))))
7825           (gnus-newsgroup-ignored-charsets 'gnus-all))
7826       (gnus-summary-select-article nil 'force)
7827       (let ((deps gnus-newsgroup-dependencies)
7828             head header)
7829         (save-excursion
7830           (set-buffer gnus-original-article-buffer)
7831           (save-restriction
7832             (message-narrow-to-head)
7833             (setq head (buffer-string)))
7834           (with-temp-buffer
7835             (insert (format "211 %d Article retrieved.\n"
7836                             (cdr gnus-article-current)))
7837             (insert head)
7838             (insert ".\n")
7839             (let ((nntp-server-buffer (current-buffer)))
7840               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7841         (gnus-data-set-header
7842          (gnus-data-find (cdr gnus-article-current))
7843          header)
7844         (gnus-summary-update-article-line
7845          (cdr gnus-article-current) header)
7846         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7847           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7848    ((not arg)
7849     ;; Select the article the normal way.
7850     (gnus-summary-select-article nil 'force))
7851    (t
7852     ;; We have to require this here to make sure that the following
7853     ;; dynamic binding isn't shadowed by autoloading.
7854     (require 'gnus-async)
7855     (require 'gnus-art)
7856     ;; Bind the article treatment functions to nil.
7857     (let ((gnus-have-all-headers t)
7858           gnus-article-prepare-hook
7859           gnus-article-decode-hook
7860           gnus-display-mime-function
7861           gnus-break-pages)
7862       ;; Destroy any MIME parts.
7863       (when (gnus-buffer-live-p gnus-article-buffer)
7864         (save-excursion
7865           (set-buffer gnus-article-buffer)
7866           (mm-destroy-parts gnus-article-mime-handles)
7867           ;; Set it to nil for safety reason.
7868           (setq gnus-article-mime-handle-alist nil)
7869           (setq gnus-article-mime-handles nil)))
7870       (gnus-summary-select-article nil 'force))))
7871   (gnus-summary-goto-subject gnus-current-article)
7872   (gnus-summary-position-point))
7873
7874 (defun gnus-summary-show-raw-article ()
7875   "Show the raw article without any article massaging functions being run."
7876   (interactive)
7877   (gnus-summary-show-article t))
7878
7879 (defun gnus-summary-verbose-headers (&optional arg)
7880   "Toggle permanent full header display.
7881 If ARG is a positive number, turn header display on.
7882 If ARG is a negative number, turn header display off."
7883   (interactive "P")
7884   (setq gnus-show-all-headers
7885         (cond ((or (not (numberp arg))
7886                    (zerop arg))
7887                (not gnus-show-all-headers))
7888               ((natnump arg)
7889                t)))
7890   (gnus-summary-show-article))
7891
7892 (defun gnus-summary-toggle-header (&optional arg)
7893   "Show the headers if they are hidden, or hide them if they are shown.
7894 If ARG is a positive number, show the entire header.
7895 If ARG is a negative number, hide the unwanted header lines."
7896   (interactive "P")
7897   (save-excursion
7898     (set-buffer gnus-article-buffer)
7899     (save-restriction
7900       (let* ((buffer-read-only nil)
7901              (inhibit-point-motion-hooks t)
7902              hidden e)
7903         (setq hidden
7904               (if (numberp arg)
7905                   (>= arg 0)
7906                 (save-restriction
7907                   (article-narrow-to-head)
7908                   (gnus-article-hidden-text-p 'headers))))
7909         (goto-char (point-min))
7910         (when (search-forward "\n\n" nil t)
7911           (delete-region (point-min) (1- (point))))
7912         (goto-char (point-min))
7913         (save-excursion
7914           (set-buffer gnus-original-article-buffer)
7915           (goto-char (point-min))
7916           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7917         (insert-buffer-substring gnus-original-article-buffer 1 e)
7918         (save-restriction
7919           (narrow-to-region (point-min) (point))
7920           (article-decode-encoded-words)
7921           (if  hidden
7922               (let ((gnus-treat-hide-headers nil)
7923                     (gnus-treat-hide-boring-headers nil))
7924                 (setq gnus-article-wash-types
7925                       (delq 'headers gnus-article-wash-types))
7926                 (gnus-treat-article 'head))
7927             (gnus-treat-article 'head)))
7928         (gnus-set-mode-line 'article)))))
7929
7930 (defun gnus-summary-show-all-headers ()
7931   "Make all header lines visible."
7932   (interactive)
7933   (gnus-summary-toggle-header 1))
7934
7935 (defun gnus-summary-caesar-message (&optional arg)
7936   "Caesar rotate the current article by 13.
7937 The numerical prefix specifies how many places to rotate each letter
7938 forward."
7939   (interactive "P")
7940   (gnus-summary-select-article)
7941   (let ((mail-header-separator ""))
7942     (gnus-eval-in-buffer-window gnus-article-buffer
7943       (save-restriction
7944         (widen)
7945         (let ((start (window-start))
7946               buffer-read-only)
7947           (message-caesar-buffer-body arg)
7948           (set-window-start (get-buffer-window (current-buffer)) start))))))
7949
7950 (defun gnus-summary-stop-page-breaking ()
7951   "Stop page breaking in the current article."
7952   (interactive)
7953   (gnus-summary-select-article)
7954   (gnus-eval-in-buffer-window gnus-article-buffer
7955     (widen)
7956     (when (gnus-visual-p 'page-marker)
7957       (let ((buffer-read-only nil))
7958         (gnus-remove-text-with-property 'gnus-prev)
7959         (gnus-remove-text-with-property 'gnus-next))
7960       (setq gnus-page-broken nil))))
7961
7962 (defun gnus-summary-move-article (&optional n to-newsgroup
7963                                             select-method action)
7964   "Move the current article to a different newsgroup.
7965 If N is a positive number, move the N next articles.
7966 If N is a negative number, move the N previous articles.
7967 If N is nil and any articles have been marked with the process mark,
7968 move those articles instead.
7969 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7970 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7971 re-spool using this method.
7972
7973 For this function to work, both the current newsgroup and the
7974 newsgroup that you want to move to have to support the `request-move'
7975 and `request-accept' functions.
7976
7977 ACTION can be either `move' (the default), `crosspost' or `copy'."
7978   (interactive "P")
7979   (unless action
7980     (setq action 'move))
7981   ;; Check whether the source group supports the required functions.
7982   (cond ((and (eq action 'move)
7983               (not (gnus-check-backend-function
7984                     'request-move-article gnus-newsgroup-name)))
7985          (error "The current group does not support article moving"))
7986         ((and (eq action 'crosspost)
7987               (not (gnus-check-backend-function
7988                     'request-replace-article gnus-newsgroup-name)))
7989          (error "The current group does not support article editing")))
7990   (let ((articles (gnus-summary-work-articles n))
7991         (prefix (if (gnus-check-backend-function
7992                      'request-move-article gnus-newsgroup-name)
7993                     (gnus-group-real-prefix gnus-newsgroup-name)
7994                   ""))
7995         (names '((move "Move" "Moving")
7996                  (copy "Copy" "Copying")
7997                  (crosspost "Crosspost" "Crossposting")))
7998         (copy-buf (save-excursion
7999                     (nnheader-set-temp-buffer " *copy article*")))
8000         art-group to-method new-xref article to-groups)
8001     (unless (assq action names)
8002       (error "Unknown action %s" action))
8003     ;; Read the newsgroup name.
8004     (when (and (not to-newsgroup)
8005                (not select-method))
8006       (setq to-newsgroup
8007             (gnus-read-move-group-name
8008              (cadr (assq action names))
8009              (symbol-value (intern (format "gnus-current-%s-group" action)))
8010              articles prefix))
8011       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8012     (setq to-method (or select-method
8013                         (gnus-server-to-method
8014                          (gnus-group-method to-newsgroup))))
8015     ;; Check the method we are to move this article to...
8016     (unless (gnus-check-backend-function
8017              'request-accept-article (car to-method))
8018       (error "%s does not support article copying" (car to-method)))
8019     (unless (gnus-check-server to-method)
8020       (error "Can't open server %s" (car to-method)))
8021     (gnus-message 6 "%s to %s: %s..."
8022                   (caddr (assq action names))
8023                   (or (car select-method) to-newsgroup) articles)
8024     (while articles
8025       (setq article (pop articles))
8026       (setq
8027        art-group
8028        (cond
8029         ;; Move the article.
8030         ((eq action 'move)
8031          ;; Remove this article from future suppression.
8032          (gnus-dup-unsuppress-article article)
8033          (gnus-request-move-article
8034           article                       ; Article to move
8035           gnus-newsgroup-name           ; From newsgroup
8036           (nth 1 (gnus-find-method-for-group
8037                   gnus-newsgroup-name)) ; Server
8038           (list 'gnus-request-accept-article
8039                 to-newsgroup (list 'quote select-method)
8040                 (not articles) t)       ; Accept form
8041           (not articles)))              ; Only save nov last time
8042         ;; Copy the article.
8043         ((eq action 'copy)
8044          (save-excursion
8045            (set-buffer copy-buf)
8046            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8047              (gnus-request-accept-article
8048               to-newsgroup select-method (not articles) t))))
8049         ;; Crosspost the article.
8050         ((eq action 'crosspost)
8051          (let ((xref (message-tokenize-header
8052                       (mail-header-xref (gnus-summary-article-header article))
8053                       " ")))
8054            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8055                                   ":" article))
8056            (unless xref
8057              (setq xref (list (system-name))))
8058            (setq new-xref
8059                  (concat
8060                   (mapconcat 'identity
8061                              (delete "Xref:" (delete new-xref xref))
8062                              " ")
8063                   " " new-xref))
8064            (save-excursion
8065              (set-buffer copy-buf)
8066              ;; First put the article in the destination group.
8067              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8068              (when (consp (setq art-group
8069                                 (gnus-request-accept-article
8070                                  to-newsgroup select-method (not articles))))
8071                (setq new-xref (concat new-xref " " (car art-group)
8072                                       ":" (cdr art-group)))
8073                ;; Now we have the new Xrefs header, so we insert
8074                ;; it and replace the new article.
8075                (nnheader-replace-header "Xref" new-xref)
8076                (gnus-request-replace-article
8077                 (cdr art-group) to-newsgroup (current-buffer))
8078                art-group))))))
8079       (cond
8080        ((not art-group)
8081         (gnus-message 1 "Couldn't %s article %s: %s"
8082                       (cadr (assq action names)) article
8083                       (nnheader-get-report (car to-method))))
8084        ((eq art-group 'junk)
8085         (when (eq action 'move)
8086           (gnus-summary-mark-article article gnus-canceled-mark)
8087           (gnus-message 4 "Deleted article %s" article)))
8088        (t
8089         (let* ((pto-group (gnus-group-prefixed-name
8090                            (car art-group) to-method))
8091                (entry
8092                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8093                (info (nth 2 entry))
8094                (to-group (gnus-info-group info))
8095                to-marks)
8096           ;; Update the group that has been moved to.
8097           (when (and info
8098                      (memq action '(move copy)))
8099             (unless (member to-group to-groups)
8100               (push to-group to-groups))
8101
8102             (unless (memq article gnus-newsgroup-unreads)
8103               (push 'read to-marks)
8104               (gnus-info-set-read
8105                info (gnus-add-to-range (gnus-info-read info)
8106                                        (list (cdr art-group)))))
8107
8108             ;; See whether the article is to be put in the cache.
8109             (let ((marks gnus-article-mark-lists)
8110                   (to-article (cdr art-group)))
8111
8112               ;; Enter the article into the cache in the new group,
8113               ;; if that is required.
8114               (when gnus-use-cache
8115                 (gnus-cache-possibly-enter-article
8116                  to-group to-article
8117                  (memq article gnus-newsgroup-marked)
8118                  (memq article gnus-newsgroup-dormant)
8119                  (memq article gnus-newsgroup-unreads)))
8120
8121               (when gnus-preserve-marks
8122                 ;; Copy any marks over to the new group.
8123                 (when (and (equal to-group gnus-newsgroup-name)
8124                            (not (memq article gnus-newsgroup-unreads)))
8125                   ;; Mark this article as read in this group.
8126                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8127                   (setcdr (gnus-active to-group) to-article)
8128                   (setcdr gnus-newsgroup-active to-article))
8129
8130                 (while marks
8131                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8132                     (when (memq article (symbol-value
8133                                          (intern (format "gnus-newsgroup-%s"
8134                                                          (caar marks)))))
8135                       (push (cdar marks) to-marks)
8136                       ;; If the other group is the same as this group,
8137                       ;; then we have to add the mark to the list.
8138                       (when (equal to-group gnus-newsgroup-name)
8139                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8140                              (cons to-article
8141                                    (symbol-value
8142                                     (intern (format "gnus-newsgroup-%s"
8143                                                     (caar marks)))))))
8144                       ;; Copy the marks to other group.
8145                       (gnus-add-marked-articles
8146                        to-group (cdar marks) (list to-article) info)))
8147                   (setq marks (cdr marks)))
8148
8149                 (gnus-request-set-mark to-group (list (list (list to-article)
8150                                                             'add
8151                                                             to-marks))))
8152
8153               (gnus-dribble-enter
8154                (concat "(gnus-group-set-info '"
8155                        (gnus-prin1-to-string (gnus-get-info to-group))
8156                        ")"))))
8157
8158           ;; Update the Xref header in this article to point to
8159           ;; the new crossposted article we have just created.
8160           (when (eq action 'crosspost)
8161             (save-excursion
8162               (set-buffer copy-buf)
8163               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8164               (nnheader-replace-header "Xref" new-xref)
8165               (gnus-request-replace-article
8166                article gnus-newsgroup-name (current-buffer)))))
8167
8168         ;;;!!!Why is this necessary?
8169         (set-buffer gnus-summary-buffer)
8170
8171         (gnus-summary-goto-subject article)
8172         (when (eq action 'move)
8173           (gnus-summary-mark-article article gnus-canceled-mark))))
8174       (gnus-summary-remove-process-mark article))
8175     ;; Re-activate all groups that have been moved to.
8176     (while to-groups
8177       (save-excursion
8178         (set-buffer gnus-group-buffer)
8179         (when (gnus-group-goto-group (car to-groups) t)
8180           (gnus-group-get-new-news-this-group 1 t))
8181         (pop to-groups)))
8182
8183     (gnus-kill-buffer copy-buf)
8184     (gnus-summary-position-point)
8185     (gnus-set-mode-line 'summary)))
8186
8187 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8188   "Move the current article to a different newsgroup.
8189 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8190 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8191 re-spool using this method."
8192   (interactive "P")
8193   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8194
8195 (defun gnus-summary-crosspost-article (&optional n)
8196   "Crosspost the current article to some other group."
8197   (interactive "P")
8198   (gnus-summary-move-article n nil nil 'crosspost))
8199
8200 (defcustom gnus-summary-respool-default-method nil
8201   "Default method for respooling an article.
8202 If nil, use to the current newsgroup method."
8203   :type '(choice (gnus-select-method :value (nnml ""))
8204                  (const nil))
8205   :group 'gnus-summary-mail)
8206
8207 (defun gnus-summary-respool-article (&optional n method)
8208   "Respool the current article.
8209 The article will be squeezed through the mail spooling process again,
8210 which means that it will be put in some mail newsgroup or other
8211 depending on `nnmail-split-methods'.
8212 If N is a positive number, respool the N next articles.
8213 If N is a negative number, respool the N previous articles.
8214 If N is nil and any articles have been marked with the process mark,
8215 respool those articles instead.
8216
8217 Respooling can be done both from mail groups and \"real\" newsgroups.
8218 In the former case, the articles in question will be moved from the
8219 current group into whatever groups they are destined to.  In the
8220 latter case, they will be copied into the relevant groups."
8221   (interactive
8222    (list current-prefix-arg
8223          (let* ((methods (gnus-methods-using 'respool))
8224                 (methname
8225                  (symbol-name (or gnus-summary-respool-default-method
8226                                   (car (gnus-find-method-for-group
8227                                         gnus-newsgroup-name)))))
8228                 (method
8229                  (gnus-completing-read
8230                   methname "What backend do you want to use when respooling?"
8231                   methods nil t nil 'gnus-mail-method-history))
8232                 ms)
8233            (cond
8234             ((zerop (length (setq ms (gnus-servers-using-backend
8235                                       (intern method)))))
8236              (list (intern method) ""))
8237             ((= 1 (length ms))
8238              (car ms))
8239             (t
8240              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8241                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8242                            ms-alist))))))))
8243   (unless method
8244     (error "No method given for respooling"))
8245   (if (assoc (symbol-name
8246               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8247              (gnus-methods-using 'respool))
8248       (gnus-summary-move-article n nil method)
8249     (gnus-summary-copy-article n nil method)))
8250
8251 (defun gnus-summary-import-article (file &optional edit)
8252   "Import an arbitrary file into a mail newsgroup."
8253   (interactive "fImport file: \nP")
8254   (let ((group gnus-newsgroup-name)
8255         (now (current-time))
8256         atts lines group-art)
8257     (unless (gnus-check-backend-function 'request-accept-article group)
8258       (error "%s does not support article importing" group))
8259     (or (file-readable-p file)
8260         (not (file-regular-p file))
8261         (error "Can't read %s" file))
8262     (save-excursion
8263       (set-buffer (gnus-get-buffer-create " *import file*"))
8264       (erase-buffer)
8265       (nnheader-insert-file-contents file)
8266       (goto-char (point-min))
8267       (if (nnheader-article-p)
8268           (save-restriction
8269             (goto-char (point-min))
8270             (search-forward "\n\n" nil t)
8271             (narrow-to-region (point-min) (1- (point)))
8272             (goto-char (point-min))
8273             (unless (re-search-forward "^date:" nil t)
8274               (goto-char (point-max))
8275               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8276        ;; This doesn't look like an article, so we fudge some headers.
8277         (setq atts (file-attributes file)
8278               lines (count-lines (point-min) (point-max)))
8279         (insert "From: " (read-string "From: ") "\n"
8280                 "Subject: " (read-string "Subject: ") "\n"
8281                 "Date: " (message-make-date (nth 5 atts)) "\n"
8282                 "Message-ID: " (message-make-message-id) "\n"
8283                 "Lines: " (int-to-string lines) "\n"
8284                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8285       (setq group-art (gnus-request-accept-article group nil t))
8286       (kill-buffer (current-buffer)))
8287     (setq gnus-newsgroup-active (gnus-activate-group group))
8288     (forward-line 1)
8289     (gnus-summary-goto-article (cdr group-art) nil t)
8290     (when edit
8291       (gnus-summary-edit-article))))
8292
8293 (defun gnus-summary-create-article ()
8294   "Create an article in a mail newsgroup."
8295   (interactive)
8296   (let ((group gnus-newsgroup-name)
8297         (now (current-time))
8298         group-art)
8299     (unless (gnus-check-backend-function 'request-accept-article group)
8300       (error "%s does not support article importing" group))
8301     (save-excursion
8302       (set-buffer (gnus-get-buffer-create " *import file*"))
8303       (erase-buffer)
8304       (goto-char (point-min))
8305       ;; This doesn't look like an article, so we fudge some headers.
8306       (insert "From: " (read-string "From: ") "\n"
8307               "Subject: " (read-string "Subject: ") "\n"
8308               "Date: " (message-make-date now) "\n"
8309               "Message-ID: " (message-make-message-id) "\n")
8310       (setq group-art (gnus-request-accept-article group nil t))
8311       (kill-buffer (current-buffer)))
8312     (setq gnus-newsgroup-active (gnus-activate-group group))
8313     (forward-line 1)
8314     (gnus-summary-goto-article (cdr group-art) nil t)
8315     (gnus-summary-edit-article)))
8316
8317 (defun gnus-summary-article-posted-p ()
8318   "Say whether the current (mail) article is available from news as well.
8319 This will be the case if the article has both been mailed and posted."
8320   (interactive)
8321   (let ((id (mail-header-references (gnus-summary-article-header)))
8322         (gnus-override-method (car (gnus-refer-article-methods))))
8323     (if (gnus-request-head id "")
8324         (gnus-message 2 "The current message was found on %s"
8325                       gnus-override-method)
8326       (gnus-message 2 "The current message couldn't be found on %s"
8327                     gnus-override-method)
8328       nil)))
8329
8330 (defun gnus-summary-expire-articles (&optional now)
8331   "Expire all articles that are marked as expirable in the current group."
8332   (interactive)
8333   (when (gnus-check-backend-function
8334          'request-expire-articles gnus-newsgroup-name)
8335     ;; This backend supports expiry.
8336     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8337            (expirable (if total
8338                           (progn
8339                             ;; We need to update the info for
8340                             ;; this group for `gnus-list-of-read-articles'
8341                             ;; to give us the right answer.
8342                             (gnus-run-hooks 'gnus-exit-group-hook)
8343                             (gnus-summary-update-info)
8344                             (gnus-list-of-read-articles gnus-newsgroup-name))
8345                         (setq gnus-newsgroup-expirable
8346                               (sort gnus-newsgroup-expirable '<))))
8347            (expiry-wait (if now 'immediate
8348                           (gnus-group-find-parameter
8349                            gnus-newsgroup-name 'expiry-wait)))
8350            (nnmail-expiry-target
8351             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8352                 nnmail-expiry-target))
8353            es)
8354       (when expirable
8355         ;; There are expirable articles in this group, so we run them
8356         ;; through the expiry process.
8357         (gnus-message 6 "Expiring articles...")
8358         (unless (gnus-check-group gnus-newsgroup-name)
8359           (error "Can't open server for %s" gnus-newsgroup-name))
8360         ;; The list of articles that weren't expired is returned.
8361         (save-excursion
8362           (if expiry-wait
8363               (let ((nnmail-expiry-wait-function nil)
8364                     (nnmail-expiry-wait expiry-wait))
8365                 (setq es (gnus-request-expire-articles
8366                           expirable gnus-newsgroup-name)))
8367             (setq es (gnus-request-expire-articles
8368                       expirable gnus-newsgroup-name)))
8369           (unless total
8370             (setq gnus-newsgroup-expirable es))
8371           ;; We go through the old list of expirable, and mark all
8372           ;; really expired articles as nonexistent.
8373           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8374             (let ((gnus-use-cache nil))
8375               (while expirable
8376                 (unless (memq (car expirable) es)
8377                   (when (gnus-data-find (car expirable))
8378                     (gnus-summary-mark-article
8379                      (car expirable) gnus-canceled-mark)))
8380                 (setq expirable (cdr expirable))))))
8381         (gnus-message 6 "Expiring articles...done")))))
8382
8383 (defun gnus-summary-expire-articles-now ()
8384   "Expunge all expirable articles in the current group.
8385 This means that *all* articles that are marked as expirable will be
8386 deleted forever, right now."
8387   (interactive)
8388   (or gnus-expert-user
8389       (gnus-yes-or-no-p
8390        "Are you really, really, really sure you want to delete all these messages? ")
8391       (error "Phew!"))
8392   (gnus-summary-expire-articles t))
8393
8394 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8395 (defun gnus-summary-delete-article (&optional n)
8396   "Delete the N next (mail) articles.
8397 This command actually deletes articles.  This is not a marking
8398 command.  The article will disappear forever from your life, never to
8399 return.
8400 If N is negative, delete backwards.
8401 If N is nil and articles have been marked with the process mark,
8402 delete these instead."
8403   (interactive "P")
8404   (unless (gnus-check-backend-function 'request-expire-articles
8405                                        gnus-newsgroup-name)
8406     (error "The current newsgroup does not support article deletion"))
8407   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8408     (error "Couldn't open server"))
8409   ;; Compute the list of articles to delete.
8410   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8411         not-deleted)
8412     (if (and gnus-novice-user
8413              (not (gnus-yes-or-no-p
8414                    (format "Do you really want to delete %s forever? "
8415                            (if (> (length articles) 1)
8416                                (format "these %s articles" (length articles))
8417                              "this article")))))
8418         ()
8419       ;; Delete the articles.
8420       (setq not-deleted (gnus-request-expire-articles
8421                          articles gnus-newsgroup-name 'force))
8422       (while articles
8423         (gnus-summary-remove-process-mark (car articles))
8424         ;; The backend might not have been able to delete the article
8425         ;; after all.
8426         (unless (memq (car articles) not-deleted)
8427           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8428         (setq articles (cdr articles)))
8429       (when not-deleted
8430         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8431     (gnus-summary-position-point)
8432     (gnus-set-mode-line 'summary)
8433     not-deleted))
8434
8435 (defun gnus-summary-edit-article (&optional arg)
8436   "Edit the current article.
8437 This will have permanent effect only in mail groups.
8438 If ARG is nil, edit the decoded articles.
8439 If ARG is 1, edit the raw articles.
8440 If ARG is 2, edit the raw articles even in read-only groups.
8441 If ARG is 3, edit the articles with the current handles.
8442 Otherwise, allow editing of articles even in read-only
8443 groups."
8444   (interactive "P")
8445   (let (force raw current-handles)
8446     (cond
8447      ((null arg))
8448      ((eq arg 1) (setq raw t))
8449      ((eq arg 2) (setq raw t
8450                        force t))
8451      ((eq arg 3) (setq current-handles
8452                        (and (gnus-buffer-live-p gnus-article-buffer)
8453                             (with-current-buffer gnus-article-buffer
8454                               (prog1
8455                                   gnus-article-mime-handles
8456                                 (setq gnus-article-mime-handles nil))))))
8457      (t (setq force t)))
8458     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8459         (error "Can't edit the raw article in group nndraft:drafts"))
8460     (save-excursion
8461       (set-buffer gnus-summary-buffer)
8462       (let ((mail-parse-charset gnus-newsgroup-charset)
8463             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8464         (gnus-set-global-variables)
8465         (when (and (not force)
8466                    (gnus-group-read-only-p))
8467           (error "The current newsgroup does not support article editing"))
8468         (gnus-summary-show-article t)
8469         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8470           (with-current-buffer gnus-article-buffer
8471             (mm-enable-multibyte-mule4)))
8472         (if (equal gnus-newsgroup-name "nndraft:drafts")
8473             (setq raw t))
8474         (gnus-article-edit-article
8475          (if raw 'ignore
8476            `(lambda ()
8477               (let ((mbl mml-buffer-list))
8478                 (setq mml-buffer-list nil)
8479                 (mime-to-mml ,'current-handles)
8480                 (let ((mbl1 mml-buffer-list))
8481                   (setq mml-buffer-list mbl)
8482                   (set (make-local-variable 'mml-buffer-list) mbl1))
8483                 (make-local-hook 'kill-buffer-hook)
8484                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8485          `(lambda (no-highlight)
8486             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8487                   (message-options message-options)
8488                   (message-options-set-recipient)
8489                   (mail-parse-ignored-charsets
8490                    ',gnus-newsgroup-ignored-charsets))
8491               ,(if (not raw) '(progn
8492                                 (mml-to-mime)
8493                                 (mml-destroy-buffers)
8494                                 (remove-hook 'kill-buffer-hook
8495                                              'mml-destroy-buffers t)
8496                                 (kill-local-variable 'mml-buffer-list)))
8497               (gnus-summary-edit-article-done
8498                ,(or (mail-header-references gnus-current-headers) "")
8499                ,(gnus-group-read-only-p)
8500                ,gnus-summary-buffer no-highlight))))))))
8501
8502 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8503
8504 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8505                                                  no-highlight)
8506   "Make edits to the current article permanent."
8507   (interactive)
8508   (save-excursion
8509    ;; The buffer restriction contains the entire article if it exists.
8510     (when (article-goto-body)
8511       (let ((lines (count-lines (point) (point-max)))
8512             (length (- (point-max) (point)))
8513             (case-fold-search t)
8514             (body (copy-marker (point))))
8515         (goto-char (point-min))
8516         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8517           (delete-region (match-beginning 1) (match-end 1))
8518           (insert (number-to-string length)))
8519         (goto-char (point-min))
8520         (when (re-search-forward
8521                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8522           (delete-region (match-beginning 1) (match-end 1))
8523           (insert (number-to-string length)))
8524         (goto-char (point-min))
8525         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8526           (delete-region (match-beginning 1) (match-end 1))
8527           (insert (number-to-string lines))))))
8528   ;; Replace the article.
8529   (let ((buf (current-buffer)))
8530     (with-temp-buffer
8531       (insert-buffer-substring buf)
8532
8533       (if (and (not read-only)
8534                (not (gnus-request-replace-article
8535                      (cdr gnus-article-current) (car gnus-article-current)
8536                      (current-buffer) t)))
8537           (error "Couldn't replace article")
8538         ;; Update the summary buffer.
8539         (if (and references
8540                  (equal (message-tokenize-header references " ")
8541                         (message-tokenize-header
8542                          (or (message-fetch-field "references") "") " ")))
8543             ;; We only have to update this line.
8544             (save-excursion
8545               (save-restriction
8546                 (message-narrow-to-head)
8547                 (let ((head (buffer-string))
8548                       header)
8549                   (with-temp-buffer
8550                     (insert (format "211 %d Article retrieved.\n"
8551                                     (cdr gnus-article-current)))
8552                     (insert head)
8553                     (insert ".\n")
8554                     (let ((nntp-server-buffer (current-buffer)))
8555                       (setq header (car (gnus-get-newsgroup-headers
8556                                          (save-excursion
8557                                            (set-buffer gnus-summary-buffer)
8558                                            gnus-newsgroup-dependencies)
8559                                          t))))
8560                     (save-excursion
8561                       (set-buffer gnus-summary-buffer)
8562                       (gnus-data-set-header
8563                        (gnus-data-find (cdr gnus-article-current))
8564                        header)
8565                       (gnus-summary-update-article-line
8566                        (cdr gnus-article-current) header)
8567                       (if (gnus-summary-goto-subject
8568                            (cdr gnus-article-current) nil t)
8569                           (gnus-summary-update-secondary-mark
8570                            (cdr gnus-article-current))))))))
8571           ;; Update threads.
8572           (set-buffer (or buffer gnus-summary-buffer))
8573           (gnus-summary-update-article (cdr gnus-article-current))
8574           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8575               (gnus-summary-update-secondary-mark
8576                (cdr gnus-article-current))))
8577         ;; Prettify the article buffer again.
8578         (unless no-highlight
8579           (save-excursion
8580             (set-buffer gnus-article-buffer)
8581             ;;;!!! Fix this -- article should be rehighlighted.
8582             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8583             (set-buffer gnus-original-article-buffer)
8584             (gnus-request-article
8585              (cdr gnus-article-current)
8586              (car gnus-article-current) (current-buffer))))
8587         ;; Prettify the summary buffer line.
8588         (when (gnus-visual-p 'summary-highlight 'highlight)
8589           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8590
8591 (defun gnus-summary-edit-wash (key)
8592   "Perform editing command KEY in the article buffer."
8593   (interactive
8594    (list
8595     (progn
8596       (message "%s" (concat (this-command-keys) "- "))
8597       (read-char))))
8598   (message "")
8599   (gnus-summary-edit-article)
8600   (execute-kbd-macro (concat (this-command-keys) key))
8601   (gnus-article-edit-done))
8602
8603
8604 (defun gnus-summary-toggle-smiley (&optional arg)
8605   "Toggle the display of smilies as small graphical icons."
8606   (interactive "P")
8607   (save-excursion
8608     (set-buffer gnus-article-buffer)
8609     (gnus-smiley-display arg)
8610     ))
8611
8612 ;;; Respooling
8613
8614 (defun gnus-summary-respool-query (&optional silent trace)
8615   "Query where the respool algorithm would put this article."
8616   (interactive)
8617   (let (gnus-mark-article-hook)
8618     (gnus-summary-select-article)
8619     (save-excursion
8620       (set-buffer gnus-original-article-buffer)
8621       (save-restriction
8622         (message-narrow-to-head)
8623         (let ((groups (nnmail-article-group 'identity trace)))
8624           (unless silent
8625             (if groups
8626                 (message "This message would go to %s"
8627                          (mapconcat 'car groups ", "))
8628               (message "This message would go to no groups"))
8629             groups))))))
8630
8631 (defun gnus-summary-respool-trace ()
8632   "Trace where the respool algorithm would put this article.
8633 Display a buffer showing all fancy splitting patterns which matched."
8634   (interactive)
8635   (gnus-summary-respool-query nil t))
8636
8637 ;; Summary marking commands.
8638
8639 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8640   "Mark articles which has the same subject as read, and then select the next.
8641 If UNMARK is positive, remove any kind of mark.
8642 If UNMARK is negative, tick articles."
8643   (interactive "P")
8644   (when unmark
8645     (setq unmark (prefix-numeric-value unmark)))
8646   (let ((count
8647          (gnus-summary-mark-same-subject
8648           (gnus-summary-article-subject) unmark)))
8649     ;; Select next unread article.  If auto-select-same mode, should
8650     ;; select the first unread article.
8651     (gnus-summary-next-article t (and gnus-auto-select-same
8652                                       (gnus-summary-article-subject)))
8653     (gnus-message 7 "%d article%s marked as %s"
8654                   count (if (= count 1) " is" "s are")
8655                   (if unmark "unread" "read"))))
8656
8657 (defun gnus-summary-kill-same-subject (&optional unmark)
8658   "Mark articles which has the same subject as read.
8659 If UNMARK is positive, remove any kind of mark.
8660 If UNMARK is negative, tick articles."
8661   (interactive "P")
8662   (when unmark
8663     (setq unmark (prefix-numeric-value unmark)))
8664   (let ((count
8665          (gnus-summary-mark-same-subject
8666           (gnus-summary-article-subject) unmark)))
8667     ;; If marked as read, go to next unread subject.
8668     (when (null unmark)
8669       ;; Go to next unread subject.
8670       (gnus-summary-next-subject 1 t))
8671     (gnus-message 7 "%d articles are marked as %s"
8672                   count (if unmark "unread" "read"))))
8673
8674 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8675   "Mark articles with same SUBJECT as read, and return marked number.
8676 If optional argument UNMARK is positive, remove any kinds of marks.
8677 If optional argument UNMARK is negative, mark articles as unread instead."
8678   (let ((count 1))
8679     (save-excursion
8680       (cond
8681        ((null unmark)                   ; Mark as read.
8682         (while (and
8683                 (progn
8684                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8685                   (gnus-summary-show-thread) t)
8686                 (gnus-summary-find-subject subject))
8687           (setq count (1+ count))))
8688        ((> unmark 0)                    ; Tick.
8689         (while (and
8690                 (progn
8691                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8692                   (gnus-summary-show-thread) t)
8693                 (gnus-summary-find-subject subject))
8694           (setq count (1+ count))))
8695        (t                               ; Mark as unread.
8696         (while (and
8697                 (progn
8698                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8699                   (gnus-summary-show-thread) t)
8700                 (gnus-summary-find-subject subject))
8701           (setq count (1+ count)))))
8702       (gnus-set-mode-line 'summary)
8703       ;; Return the number of marked articles.
8704       count)))
8705
8706 (defun gnus-summary-mark-as-processable (n &optional unmark)
8707   "Set the process mark on the next N articles.
8708 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8709 the process mark instead.  The difference between N and the actual
8710 number of articles marked is returned."
8711   (interactive "P")
8712   (if (and (null n) (gnus-region-active-p))
8713       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8714     (setq n (prefix-numeric-value n))
8715     (let ((backward (< n 0))
8716           (n (abs n)))
8717       (while (and
8718               (> n 0)
8719               (if unmark
8720                   (gnus-summary-remove-process-mark
8721                    (gnus-summary-article-number))
8722                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8723               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8724         (setq n (1- n)))
8725       (when (/= 0 n)
8726         (gnus-message 7 "No more articles"))
8727       (gnus-summary-recenter)
8728       (gnus-summary-position-point)
8729       n)))
8730
8731 (defun gnus-summary-unmark-as-processable (n)
8732   "Remove the process mark from the next N articles.
8733 If N is negative, unmark backward instead.  The difference between N and
8734 the actual number of articles unmarked is returned."
8735   (interactive "P")
8736   (gnus-summary-mark-as-processable n t))
8737
8738 (defun gnus-summary-unmark-all-processable ()
8739   "Remove the process mark from all articles."
8740   (interactive)
8741   (save-excursion
8742     (while gnus-newsgroup-processable
8743       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8744   (gnus-summary-position-point))
8745
8746 (defun gnus-summary-add-mark (article type)
8747   "Mark ARTICLE with a mark of TYPE."
8748   (let ((vtype (car (assq type gnus-article-mark-lists)))
8749         var)
8750     (if (not vtype)
8751         (error "No such mark type: %s" type)
8752       (setq var (intern (format "gnus-newsgroup-%s" type)))
8753       (set var (cons article (symbol-value var)))
8754       (if (memq type '(processable cached replied forwarded recent saved))
8755           (gnus-summary-update-secondary-mark article)
8756         ;;; !!! This is bogus.  We should find out what primary
8757         ;;; !!! mark we want to set.
8758         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8759
8760 (defun gnus-summary-mark-as-expirable (n)
8761   "Mark N articles forward as expirable.
8762 If N is negative, mark backward instead.  The difference between N and
8763 the actual number of articles marked is returned."
8764   (interactive "p")
8765   (gnus-summary-mark-forward n gnus-expirable-mark))
8766
8767 (defun gnus-summary-mark-article-as-replied (article)
8768   "Mark ARTICLE as replied to and update the summary line.
8769 ARTICLE can also be a list of articles."
8770   (interactive (list (gnus-summary-article-number)))
8771   (let ((articles (if (listp article) article (list article))))
8772     (dolist (article articles)
8773       (push article gnus-newsgroup-replied)
8774       (let ((buffer-read-only nil))
8775         (when (gnus-summary-goto-subject article nil t)
8776           (gnus-summary-update-secondary-mark article))))))
8777
8778 (defun gnus-summary-mark-article-as-forwarded (article)
8779   "Mark ARTICLE as forwarded and update the summary line.
8780 ARTICLE can also be a list of articles."
8781   (let ((articles (if (listp article) article (list article))))
8782     (dolist (article articles)
8783       (push article gnus-newsgroup-forwarded)
8784       (let ((buffer-read-only nil))
8785         (when (gnus-summary-goto-subject article nil t)
8786           (gnus-summary-update-secondary-mark article))))))
8787
8788 (defun gnus-summary-set-bookmark (article)
8789   "Set a bookmark in current article."
8790   (interactive (list (gnus-summary-article-number)))
8791   (when (or (not (get-buffer gnus-article-buffer))
8792             (not gnus-current-article)
8793             (not gnus-article-current)
8794             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8795     (error "No current article selected"))
8796   ;; Remove old bookmark, if one exists.
8797   (let ((old (assq article gnus-newsgroup-bookmarks)))
8798     (when old
8799       (setq gnus-newsgroup-bookmarks
8800             (delq old gnus-newsgroup-bookmarks))))
8801   ;; Set the new bookmark, which is on the form
8802   ;; (article-number . line-number-in-body).
8803   (push
8804    (cons article
8805          (save-excursion
8806            (set-buffer gnus-article-buffer)
8807            (count-lines
8808             (min (point)
8809                  (save-excursion
8810                    (goto-char (point-min))
8811                    (search-forward "\n\n" nil t)
8812                    (point)))
8813             (point))))
8814    gnus-newsgroup-bookmarks)
8815   (gnus-message 6 "A bookmark has been added to the current article."))
8816
8817 (defun gnus-summary-remove-bookmark (article)
8818   "Remove the bookmark from the current article."
8819   (interactive (list (gnus-summary-article-number)))
8820   ;; Remove old bookmark, if one exists.
8821   (let ((old (assq article gnus-newsgroup-bookmarks)))
8822     (if old
8823         (progn
8824           (setq gnus-newsgroup-bookmarks
8825                 (delq old gnus-newsgroup-bookmarks))
8826           (gnus-message 6 "Removed bookmark."))
8827       (gnus-message 6 "No bookmark in current article."))))
8828
8829 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8830 (defun gnus-summary-mark-as-dormant (n)
8831   "Mark N articles forward as dormant.
8832 If N is negative, mark backward instead.  The difference between N and
8833 the actual number of articles marked is returned."
8834   (interactive "p")
8835   (gnus-summary-mark-forward n gnus-dormant-mark))
8836
8837 (defun gnus-summary-set-process-mark (article)
8838   "Set the process mark on ARTICLE and update the summary line."
8839   (setq gnus-newsgroup-processable
8840         (cons article
8841               (delq article gnus-newsgroup-processable)))
8842   (when (gnus-summary-goto-subject article)
8843     (gnus-summary-show-thread)
8844     (gnus-summary-goto-subject article)
8845     (gnus-summary-update-secondary-mark article)))
8846
8847 (defun gnus-summary-remove-process-mark (article)
8848   "Remove the process mark from ARTICLE and update the summary line."
8849   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8850   (when (gnus-summary-goto-subject article)
8851     (gnus-summary-show-thread)
8852     (gnus-summary-goto-subject article)
8853     (gnus-summary-update-secondary-mark article)))
8854
8855 (defun gnus-summary-set-saved-mark (article)
8856   "Set the process mark on ARTICLE and update the summary line."
8857   (push article gnus-newsgroup-saved)
8858   (when (gnus-summary-goto-subject article)
8859     (gnus-summary-update-secondary-mark article)))
8860
8861 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8862   "Mark N articles as read forwards.
8863 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8864 The difference between N and the actual number of articles marked is
8865 returned.
8866 Iff NO-EXPIRE, auto-expiry will be inhibited."
8867   (interactive "p")
8868   (gnus-summary-show-thread)
8869   (let ((backward (< n 0))
8870         (gnus-summary-goto-unread
8871          (and gnus-summary-goto-unread
8872               (not (eq gnus-summary-goto-unread 'never))
8873               (not (memq mark (list gnus-unread-mark
8874                                     gnus-ticked-mark gnus-dormant-mark)))))
8875         (n (abs n))
8876         (mark (or mark gnus-del-mark)))
8877     (while (and (> n 0)
8878                 (gnus-summary-mark-article nil mark no-expire)
8879                 (zerop (gnus-summary-next-subject
8880                         (if backward -1 1)
8881                         (and gnus-summary-goto-unread
8882                              (not (eq gnus-summary-goto-unread 'never)))
8883                         t)))
8884       (setq n (1- n)))
8885     (when (/= 0 n)
8886       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8887     (gnus-summary-recenter)
8888     (gnus-summary-position-point)
8889     (gnus-set-mode-line 'summary)
8890     n))
8891
8892 (defun gnus-summary-mark-article-as-read (mark)
8893   "Mark the current article quickly as read with MARK."
8894   (let ((article (gnus-summary-article-number)))
8895     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8896     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8897     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8898     (push (cons article mark) gnus-newsgroup-reads)
8899     ;; Possibly remove from cache, if that is used.
8900     (when gnus-use-cache
8901       (gnus-cache-enter-remove-article article))
8902     ;; Allow the backend to change the mark.
8903     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8904     ;; Check for auto-expiry.
8905     (when (and gnus-newsgroup-auto-expire
8906                (memq mark gnus-auto-expirable-marks))
8907       (setq mark gnus-expirable-mark)
8908       ;; Let the backend know about the mark change.
8909       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8910       (push article gnus-newsgroup-expirable))
8911     ;; Set the mark in the buffer.
8912     (gnus-summary-update-mark mark 'unread)
8913     t))
8914
8915 (defun gnus-summary-mark-article-as-unread (mark)
8916   "Mark the current article quickly as unread with MARK."
8917   (let* ((article (gnus-summary-article-number))
8918          (old-mark (gnus-summary-article-mark article)))
8919     ;; Allow the backend to change the mark.
8920     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8921     (if (eq mark old-mark)
8922         t
8923       (if (<= article 0)
8924           (progn
8925             (gnus-error 1 "Can't mark negative article numbers")
8926             nil)
8927         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8928         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8929         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8930         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8931         (cond ((= mark gnus-ticked-mark)
8932                (push article gnus-newsgroup-marked))
8933               ((= mark gnus-dormant-mark)
8934                (push article gnus-newsgroup-dormant))
8935               (t
8936                (push article gnus-newsgroup-unreads)))
8937         (gnus-pull article gnus-newsgroup-reads)
8938
8939         ;; See whether the article is to be put in the cache.
8940         (and gnus-use-cache
8941              (vectorp (gnus-summary-article-header article))
8942              (save-excursion
8943                (gnus-cache-possibly-enter-article
8944                 gnus-newsgroup-name article
8945                 (= mark gnus-ticked-mark)
8946                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8947
8948         ;; Fix the mark.
8949         (gnus-summary-update-mark mark 'unread)
8950         t))))
8951
8952 (defun gnus-summary-mark-article (&optional article mark no-expire)
8953   "Mark ARTICLE with MARK.  MARK can be any character.
8954 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8955 `??' (dormant) and `?E' (expirable).
8956 If MARK is nil, then the default character `?r' is used.
8957 If ARTICLE is nil, then the article on the current line will be
8958 marked.
8959 Iff NO-EXPIRE, auto-expiry will be inhibited."
8960   ;; The mark might be a string.
8961   (when (stringp mark)
8962     (setq mark (aref mark 0)))
8963   ;; If no mark is given, then we check auto-expiring.
8964   (when (null mark)
8965     (setq mark gnus-del-mark))
8966   (when (and (not no-expire)
8967              gnus-newsgroup-auto-expire
8968              (memq mark gnus-auto-expirable-marks))
8969     (setq mark gnus-expirable-mark))
8970   (let ((article (or article (gnus-summary-article-number)))
8971         (old-mark (gnus-summary-article-mark article)))
8972     ;; Allow the backend to change the mark.
8973     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8974     (if (eq mark old-mark)
8975         t
8976       (unless article
8977         (error "No article on current line"))
8978       (if (not (if (or (= mark gnus-unread-mark)
8979                        (= mark gnus-ticked-mark)
8980                        (= mark gnus-dormant-mark))
8981                    (gnus-mark-article-as-unread article mark)
8982                  (gnus-mark-article-as-read article mark)))
8983           t
8984         ;; See whether the article is to be put in the cache.
8985         (and gnus-use-cache
8986              (not (= mark gnus-canceled-mark))
8987              (vectorp (gnus-summary-article-header article))
8988              (save-excursion
8989                (gnus-cache-possibly-enter-article
8990                 gnus-newsgroup-name article
8991                 (= mark gnus-ticked-mark)
8992                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8993
8994         (when (gnus-summary-goto-subject article nil t)
8995           (let ((buffer-read-only nil))
8996             (gnus-summary-show-thread)
8997             ;; Fix the mark.
8998             (gnus-summary-update-mark mark 'unread)
8999             t))))))
9000
9001 (defun gnus-summary-update-secondary-mark (article)
9002   "Update the secondary (read, process, cache) mark."
9003   (gnus-summary-update-mark
9004    (cond ((memq article gnus-newsgroup-processable)
9005           gnus-process-mark)
9006          ((memq article gnus-newsgroup-cached)
9007           gnus-cached-mark)
9008          ((memq article gnus-newsgroup-replied)
9009           gnus-replied-mark)
9010          ((memq article gnus-newsgroup-forwarded)
9011           gnus-forwarded-mark)
9012          ((memq article gnus-newsgroup-saved)
9013           gnus-saved-mark)
9014          ((memq article gnus-newsgroup-recent)
9015           gnus-recent-mark)
9016          ((memq article gnus-newsgroup-unseen)
9017           gnus-unseen-mark)
9018          (t gnus-no-mark))
9019    'replied)
9020   (when (gnus-visual-p 'summary-highlight 'highlight)
9021     (gnus-run-hooks 'gnus-summary-update-hook))
9022   t)
9023
9024 (defun gnus-summary-update-mark (mark type)
9025   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9026         (buffer-read-only nil))
9027     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9028     (when forward
9029       (when (looking-at "\r")
9030         (incf forward))
9031       (when (<= (+ forward (point)) (point-max))
9032         ;; Go to the right position on the line.
9033         (goto-char (+ forward (point)))
9034         ;; Replace the old mark with the new mark.
9035         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9036         ;; Optionally update the marks by some user rule.
9037         (when (eq type 'unread)
9038           (gnus-data-set-mark
9039            (gnus-data-find (gnus-summary-article-number)) mark)
9040           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9041
9042 (defun gnus-mark-article-as-read (article &optional mark)
9043   "Enter ARTICLE in the pertinent lists and remove it from others."
9044   ;; Make the article expirable.
9045   (let ((mark (or mark gnus-del-mark)))
9046     (if (= mark gnus-expirable-mark)
9047         (push article gnus-newsgroup-expirable)
9048       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9049     ;; Remove from unread and marked lists.
9050     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9051     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9052     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9053     (push (cons article mark) gnus-newsgroup-reads)
9054     ;; Possibly remove from cache, if that is used.
9055     (when gnus-use-cache
9056       (gnus-cache-enter-remove-article article))
9057     t))
9058
9059 (defun gnus-mark-article-as-unread (article &optional mark)
9060   "Enter ARTICLE in the pertinent lists and remove it from others."
9061   (let ((mark (or mark gnus-ticked-mark)))
9062     (if (<= article 0)
9063         (progn
9064           (gnus-error 1 "Can't mark negative article numbers")
9065           nil)
9066       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9067             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9068             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9069             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9070
9071       ;; Unsuppress duplicates?
9072       (when gnus-suppress-duplicates
9073         (gnus-dup-unsuppress-article article))
9074
9075       (cond ((= mark gnus-ticked-mark)
9076              (push article gnus-newsgroup-marked))
9077             ((= mark gnus-dormant-mark)
9078              (push article gnus-newsgroup-dormant))
9079             (t
9080              (push article gnus-newsgroup-unreads)))
9081       (gnus-pull article gnus-newsgroup-reads)
9082       t)))
9083
9084 (defalias 'gnus-summary-mark-as-unread-forward
9085   'gnus-summary-tick-article-forward)
9086 (make-obsolete 'gnus-summary-mark-as-unread-forward
9087                'gnus-summary-tick-article-forward)
9088 (defun gnus-summary-tick-article-forward (n)
9089   "Tick N articles forwards.
9090 If N is negative, tick backwards instead.
9091 The difference between N and the number of articles ticked is returned."
9092   (interactive "p")
9093   (gnus-summary-mark-forward n gnus-ticked-mark))
9094
9095 (defalias 'gnus-summary-mark-as-unread-backward
9096   'gnus-summary-tick-article-backward)
9097 (make-obsolete 'gnus-summary-mark-as-unread-backward
9098                'gnus-summary-tick-article-backward)
9099 (defun gnus-summary-tick-article-backward (n)
9100   "Tick N articles backwards.
9101 The difference between N and the number of articles ticked is returned."
9102   (interactive "p")
9103   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9104
9105 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9106 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9107 (defun gnus-summary-tick-article (&optional article clear-mark)
9108   "Mark current article as unread.
9109 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9110 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9111   (interactive)
9112   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9113                                        gnus-ticked-mark)))
9114
9115 (defun gnus-summary-mark-as-read-forward (n)
9116   "Mark N articles as read forwards.
9117 If N is negative, mark backwards instead.
9118 The difference between N and the actual number of articles marked is
9119 returned."
9120   (interactive "p")
9121   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9122
9123 (defun gnus-summary-mark-as-read-backward (n)
9124   "Mark the N articles as read backwards.
9125 The difference between N and the actual number of articles marked is
9126 returned."
9127   (interactive "p")
9128   (gnus-summary-mark-forward
9129    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9130
9131 (defun gnus-summary-mark-as-read (&optional article mark)
9132   "Mark current article as read.
9133 ARTICLE specifies the article to be marked as read.
9134 MARK specifies a string to be inserted at the beginning of the line."
9135   (gnus-summary-mark-article article mark))
9136
9137 (defun gnus-summary-clear-mark-forward (n)
9138   "Clear marks from N articles forward.
9139 If N is negative, clear backward instead.
9140 The difference between N and the number of marks cleared is returned."
9141   (interactive "p")
9142   (gnus-summary-mark-forward n gnus-unread-mark))
9143
9144 (defun gnus-summary-clear-mark-backward (n)
9145   "Clear marks from N articles backward.
9146 The difference between N and the number of marks cleared is returned."
9147   (interactive "p")
9148   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9149
9150 (defun gnus-summary-mark-unread-as-read ()
9151   "Intended to be used by `gnus-summary-mark-article-hook'."
9152   (when (memq gnus-current-article gnus-newsgroup-unreads)
9153     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9154
9155 (defun gnus-summary-mark-read-and-unread-as-read ()
9156   "Intended to be used by `gnus-summary-mark-article-hook'."
9157   (let ((mark (gnus-summary-article-mark)))
9158     (when (or (gnus-unread-mark-p mark)
9159               (gnus-read-mark-p mark))
9160       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9161
9162 (defun gnus-summary-mark-unread-as-ticked ()
9163   "Intended to be used by `gnus-summary-mark-article-hook'."
9164   (when (memq gnus-current-article gnus-newsgroup-unreads)
9165     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9166
9167 (defun gnus-summary-mark-region-as-read (point mark all)
9168   "Mark all unread articles between point and mark as read.
9169 If given a prefix, mark all articles between point and mark as read,
9170 even ticked and dormant ones."
9171   (interactive "r\nP")
9172   (save-excursion
9173     (let (article)
9174       (goto-char point)
9175       (beginning-of-line)
9176       (while (and
9177               (< (point) mark)
9178               (progn
9179                 (when (or all
9180                           (memq (setq article (gnus-summary-article-number))
9181                                 gnus-newsgroup-unreads))
9182                   (gnus-summary-mark-article article gnus-del-mark))
9183                 t)
9184               (gnus-summary-find-next))))))
9185
9186 (defun gnus-summary-mark-below (score mark)
9187   "Mark articles with score less than SCORE with MARK."
9188   (interactive "P\ncMark: ")
9189   (setq score (if score
9190                   (prefix-numeric-value score)
9191                 (or gnus-summary-default-score 0)))
9192   (save-excursion
9193     (set-buffer gnus-summary-buffer)
9194     (goto-char (point-min))
9195     (while
9196         (progn
9197           (and (< (gnus-summary-article-score) score)
9198                (gnus-summary-mark-article nil mark))
9199           (gnus-summary-find-next)))))
9200
9201 (defun gnus-summary-kill-below (&optional score)
9202   "Mark articles with score below SCORE as read."
9203   (interactive "P")
9204   (gnus-summary-mark-below score gnus-killed-mark))
9205
9206 (defun gnus-summary-clear-above (&optional score)
9207   "Clear all marks from articles with score above SCORE."
9208   (interactive "P")
9209   (gnus-summary-mark-above score gnus-unread-mark))
9210
9211 (defun gnus-summary-tick-above (&optional score)
9212   "Tick all articles with score above SCORE."
9213   (interactive "P")
9214   (gnus-summary-mark-above score gnus-ticked-mark))
9215
9216 (defun gnus-summary-mark-above (score mark)
9217   "Mark articles with score over SCORE with MARK."
9218   (interactive "P\ncMark: ")
9219   (setq score (if score
9220                   (prefix-numeric-value score)
9221                 (or gnus-summary-default-score 0)))
9222   (save-excursion
9223     (set-buffer gnus-summary-buffer)
9224     (goto-char (point-min))
9225     (while (and (progn
9226                   (when (> (gnus-summary-article-score) score)
9227                     (gnus-summary-mark-article nil mark))
9228                   t)
9229                 (gnus-summary-find-next)))))
9230
9231 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9232 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9233 (defun gnus-summary-limit-include-expunged (&optional no-error)
9234   "Display all the hidden articles that were expunged for low scores."
9235   (interactive)
9236   (let ((buffer-read-only nil))
9237     (let ((scored gnus-newsgroup-scored)
9238           headers h)
9239       (while scored
9240         (unless (gnus-summary-article-header (caar scored))
9241           (and (setq h (gnus-number-to-header (caar scored)))
9242                (< (cdar scored) gnus-summary-expunge-below)
9243                (push h headers)))
9244         (setq scored (cdr scored)))
9245       (if (not headers)
9246           (when (not no-error)
9247             (error "No expunged articles hidden"))
9248         (goto-char (point-min))
9249         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9250         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9251         (mapcar (lambda (x) (push (mail-header-number x)
9252                                   gnus-newsgroup-limit))
9253                 headers)
9254         (gnus-summary-prepare-unthreaded (nreverse headers))
9255         (goto-char (point-min))
9256         (gnus-summary-position-point)
9257         t))))
9258
9259 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9260   "Mark all unread articles in this newsgroup as read.
9261 If prefix argument ALL is non-nil, ticked and dormant articles will
9262 also be marked as read.
9263 If QUIETLY is non-nil, no questions will be asked.
9264 If TO-HERE is non-nil, it should be a point in the buffer.  All
9265 articles before (after, if REVERSE is set) this point will be marked as read.
9266 Note that this function will only catch up the unread article
9267 in the current summary buffer limitation.
9268 The number of articles marked as read is returned."
9269   (interactive "P")
9270   (prog1
9271       (save-excursion
9272         (when (or quietly
9273                   (not gnus-interactive-catchup) ;Without confirmation?
9274                   gnus-expert-user
9275                   (gnus-y-or-n-p
9276                    (if all
9277                        "Mark absolutely all articles as read? "
9278                      "Mark all unread articles as read? ")))
9279           (if (and not-mark
9280                    (not gnus-newsgroup-adaptive)
9281                    (not gnus-newsgroup-auto-expire)
9282                    (not gnus-suppress-duplicates)
9283                    (or (not gnus-use-cache)
9284                        (eq gnus-use-cache 'passive)))
9285               (progn
9286                 (when all
9287                   (setq gnus-newsgroup-marked nil
9288                         gnus-newsgroup-dormant nil))
9289                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9290             ;; We actually mark all articles as canceled, which we
9291             ;; have to do when using auto-expiry or adaptive scoring.
9292             (gnus-summary-show-all-threads)
9293             (if (and to-here reverse)
9294                 (progn
9295                   (goto-char to-here)
9296                   (while (and
9297                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9298                           (gnus-summary-find-next (not all) nil nil t))))
9299               (when (gnus-summary-first-subject (not all) t)
9300                 (while (and
9301                         (if to-here (< (point) to-here) t)
9302                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9303                         (gnus-summary-find-next (not all) nil nil t)))))
9304             (gnus-set-mode-line 'summary))
9305           t))
9306     (gnus-summary-position-point)))
9307
9308 (defun gnus-summary-catchup-to-here (&optional all)
9309   "Mark all unticked articles before the current one as read.
9310 If ALL is non-nil, also mark ticked and dormant articles as read."
9311   (interactive "P")
9312   (save-excursion
9313     (gnus-save-hidden-threads
9314       (let ((beg (point)))
9315         ;; We check that there are unread articles.
9316         (when (or all (gnus-summary-find-prev))
9317           (gnus-summary-catchup all t beg)))))
9318   (gnus-summary-position-point))
9319
9320 (defun gnus-summary-catchup-from-here (&optional all)
9321   "Mark all unticked articles after the current one as read.
9322 If ALL is non-nil, also mark ticked and dormant articles as read."
9323   (interactive "P")
9324   (save-excursion
9325     (gnus-save-hidden-threads
9326       (let ((beg (point)))
9327         ;; We check that there are unread articles.
9328         (when (or all (gnus-summary-find-next))
9329           (gnus-summary-catchup all t beg nil t)))))
9330
9331   (gnus-summary-position-point))
9332 (defun gnus-summary-catchup-all (&optional quietly)
9333   "Mark all articles in this newsgroup as read."
9334   (interactive "P")
9335   (gnus-summary-catchup t quietly))
9336
9337 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9338   "Mark all unread articles in this group as read, then exit.
9339 If prefix argument ALL is non-nil, all articles are marked as read.
9340 If QUIETLY is non-nil, no questions will be asked."
9341   (interactive "P")
9342   (when (gnus-summary-catchup all quietly nil 'fast)
9343     ;; Select next newsgroup or exit.
9344     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9345              (eq gnus-auto-select-next 'quietly))
9346         (gnus-summary-next-group nil)
9347       (gnus-summary-exit))))
9348
9349 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9350   "Mark all articles in this newsgroup as read, and then exit."
9351   (interactive "P")
9352   (gnus-summary-catchup-and-exit t quietly))
9353
9354 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9355   "Mark all articles in this group as read and select the next group.
9356 If given a prefix, mark all articles, unread as well as ticked, as
9357 read."
9358   (interactive "P")
9359   (save-excursion
9360     (gnus-summary-catchup all))
9361   (gnus-summary-next-group))
9362
9363 ;;;
9364 ;;; with article
9365 ;;;
9366
9367 (defmacro gnus-with-article (article &rest forms)
9368   "Select ARTICLE and perform FORMS in the original article buffer.
9369 Then replace the article with the result."
9370   `(progn
9371      ;; We don't want the article to be marked as read.
9372      (let (gnus-mark-article-hook)
9373        (gnus-summary-select-article t t nil ,article))
9374      (set-buffer gnus-original-article-buffer)
9375      ,@forms
9376      (if (not (gnus-check-backend-function
9377                'request-replace-article (car gnus-article-current)))
9378          (gnus-message 5 "Read-only group; not replacing")
9379        (unless (gnus-request-replace-article
9380                 ,article (car gnus-article-current)
9381                 (current-buffer) t)
9382          (error "Couldn't replace article")))
9383      ;; The cache and backlog have to be flushed somewhat.
9384      (when gnus-keep-backlog
9385        (gnus-backlog-remove-article
9386         (car gnus-article-current) (cdr gnus-article-current)))
9387      (when gnus-use-cache
9388        (gnus-cache-update-article
9389         (car gnus-article-current) (cdr gnus-article-current)))))
9390
9391 (put 'gnus-with-article 'lisp-indent-function 1)
9392 (put 'gnus-with-article 'edebug-form-spec '(form body))
9393
9394 ;; Thread-based commands.
9395
9396 (defun gnus-summary-articles-in-thread (&optional article)
9397   "Return a list of all articles in the current thread.
9398 If ARTICLE is non-nil, return all articles in the thread that starts
9399 with that article."
9400   (let* ((article (or article (gnus-summary-article-number)))
9401          (data (gnus-data-find-list article))
9402          (top-level (gnus-data-level (car data)))
9403          (top-subject
9404           (cond ((null gnus-thread-operation-ignore-subject)
9405                  (gnus-simplify-subject-re
9406                   (mail-header-subject (gnus-data-header (car data)))))
9407                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9408                  (gnus-simplify-subject-fuzzy
9409                   (mail-header-subject (gnus-data-header (car data)))))
9410                 (t nil)))
9411          (end-point (save-excursion
9412                       (if (gnus-summary-go-to-next-thread)
9413                           (point) (point-max))))
9414          articles)
9415     (while (and data
9416                 (< (gnus-data-pos (car data)) end-point))
9417       (when (or (not top-subject)
9418                 (string= top-subject
9419                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9420                              (gnus-simplify-subject-fuzzy
9421                               (mail-header-subject
9422                                (gnus-data-header (car data))))
9423                            (gnus-simplify-subject-re
9424                             (mail-header-subject
9425                              (gnus-data-header (car data)))))))
9426         (push (gnus-data-number (car data)) articles))
9427       (unless (and (setq data (cdr data))
9428                    (> (gnus-data-level (car data)) top-level))
9429         (setq data nil)))
9430     ;; Return the list of articles.
9431     (nreverse articles)))
9432
9433 (defun gnus-summary-rethread-current ()
9434   "Rethread the thread the current article is part of."
9435   (interactive)
9436   (let* ((gnus-show-threads t)
9437          (article (gnus-summary-article-number))
9438          (id (mail-header-id (gnus-summary-article-header)))
9439          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9440     (unless id
9441       (error "No article on the current line"))
9442     (gnus-rebuild-thread id)
9443     (gnus-summary-goto-subject article)))
9444
9445 (defun gnus-summary-reparent-thread ()
9446   "Make the current article child of the marked (or previous) article.
9447
9448 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9449 is non-nil or the Subject: of both articles are the same."
9450   (interactive)
9451   (unless (not (gnus-group-read-only-p))
9452     (error "The current newsgroup does not support article editing"))
9453   (unless (<= (length gnus-newsgroup-processable) 1)
9454     (error "No more than one article may be marked"))
9455   (save-window-excursion
9456     (let ((gnus-article-buffer " *reparent*")
9457           (current-article (gnus-summary-article-number))
9458           ;; First grab the marked article, otherwise one line up.
9459           (parent-article (if (not (null gnus-newsgroup-processable))
9460                               (car gnus-newsgroup-processable)
9461                             (save-excursion
9462                               (if (eq (forward-line -1) 0)
9463                                   (gnus-summary-article-number)
9464                                 (error "Beginning of summary buffer"))))))
9465       (unless (not (eq current-article parent-article))
9466         (error "An article may not be self-referential"))
9467       (let ((message-id (mail-header-id
9468                          (gnus-summary-article-header parent-article))))
9469         (unless (and message-id (not (equal message-id "")))
9470           (error "No message-id in desired parent"))
9471         (gnus-with-article current-article
9472           (save-restriction
9473             (goto-char (point-min))
9474             (message-narrow-to-head)
9475             (if (re-search-forward "^References: " nil t)
9476                 (progn
9477                   (re-search-forward "^[^ \t]" nil t)
9478                   (forward-line -1)
9479                   (end-of-line)
9480                   (insert " " message-id))
9481               (insert "References: " message-id "\n"))))
9482         (set-buffer gnus-summary-buffer)
9483         (gnus-summary-unmark-all-processable)
9484         (gnus-summary-update-article current-article)
9485         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9486             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9487         (gnus-summary-rethread-current)
9488         (gnus-message 3 "Article %d is now the child of article %d"
9489                       current-article parent-article)))))
9490
9491 (defun gnus-summary-toggle-threads (&optional arg)
9492   "Toggle showing conversation threads.
9493 If ARG is positive number, turn showing conversation threads on."
9494   (interactive "P")
9495   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9496     (setq gnus-show-threads
9497           (if (null arg) (not gnus-show-threads)
9498             (> (prefix-numeric-value arg) 0)))
9499     (gnus-summary-prepare)
9500     (gnus-summary-goto-subject current)
9501     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9502     (gnus-summary-position-point)))
9503
9504 (defun gnus-summary-show-all-threads ()
9505   "Show all threads."
9506   (interactive)
9507   (save-excursion
9508     (let ((buffer-read-only nil))
9509       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9510   (gnus-summary-position-point))
9511
9512 (defun gnus-summary-show-thread ()
9513   "Show thread subtrees.
9514 Returns nil if no thread was there to be shown."
9515   (interactive)
9516   (let ((buffer-read-only nil)
9517         (orig (point))
9518         ;; first goto end then to beg, to have point at beg after let
9519         (end (progn (end-of-line) (point)))
9520         (beg (progn (beginning-of-line) (point))))
9521     (prog1
9522         ;; Any hidden lines here?
9523         (search-forward "\r" end t)
9524       (subst-char-in-region beg end ?\^M ?\n t)
9525       (goto-char orig)
9526       (gnus-summary-position-point))))
9527
9528 (defun gnus-summary-hide-all-threads ()
9529   "Hide all thread subtrees."
9530   (interactive)
9531   (save-excursion
9532     (goto-char (point-min))
9533     (gnus-summary-hide-thread)
9534     (while (zerop (gnus-summary-next-thread 1 t))
9535       (gnus-summary-hide-thread)))
9536   (gnus-summary-position-point))
9537
9538 (defun gnus-summary-hide-thread ()
9539   "Hide thread subtrees.
9540 Returns nil if no threads were there to be hidden."
9541   (interactive)
9542   (let ((buffer-read-only nil)
9543         (start (point))
9544         (article (gnus-summary-article-number)))
9545     (goto-char start)
9546     ;; Go forward until either the buffer ends or the subthread
9547     ;; ends.
9548     (when (and (not (eobp))
9549                (or (zerop (gnus-summary-next-thread 1 t))
9550                    (goto-char (point-max))))
9551       (prog1
9552           (if (and (> (point) start)
9553                    (search-backward "\n" start t))
9554               (progn
9555                 (subst-char-in-region start (point) ?\n ?\^M)
9556                 (gnus-summary-goto-subject article))
9557             (goto-char start)
9558             nil)))))
9559
9560 (defun gnus-summary-go-to-next-thread (&optional previous)
9561   "Go to the same level (or less) next thread.
9562 If PREVIOUS is non-nil, go to previous thread instead.
9563 Return the article number moved to, or nil if moving was impossible."
9564   (let ((level (gnus-summary-thread-level))
9565         (way (if previous -1 1))
9566         (beg (point)))
9567     (forward-line way)
9568     (while (and (not (eobp))
9569                 (< level (gnus-summary-thread-level)))
9570       (forward-line way))
9571     (if (eobp)
9572         (progn
9573           (goto-char beg)
9574           nil)
9575       (setq beg (point))
9576       (prog1
9577           (gnus-summary-article-number)
9578         (goto-char beg)))))
9579
9580 (defun gnus-summary-next-thread (n &optional silent)
9581   "Go to the same level next N'th thread.
9582 If N is negative, search backward instead.
9583 Returns the difference between N and the number of skips actually
9584 done.
9585
9586 If SILENT, don't output messages."
9587   (interactive "p")
9588   (let ((backward (< n 0))
9589         (n (abs n)))
9590     (while (and (> n 0)
9591                 (gnus-summary-go-to-next-thread backward))
9592       (decf n))
9593     (unless silent
9594       (gnus-summary-position-point))
9595     (when (and (not silent) (/= 0 n))
9596       (gnus-message 7 "No more threads"))
9597     n))
9598
9599 (defun gnus-summary-prev-thread (n)
9600   "Go to the same level previous N'th thread.
9601 Returns the difference between N and the number of skips actually
9602 done."
9603   (interactive "p")
9604   (gnus-summary-next-thread (- n)))
9605
9606 (defun gnus-summary-go-down-thread ()
9607   "Go down one level in the current thread."
9608   (let ((children (gnus-summary-article-children)))
9609     (when children
9610       (gnus-summary-goto-subject (car children)))))
9611
9612 (defun gnus-summary-go-up-thread ()
9613   "Go up one level in the current thread."
9614   (let ((parent (gnus-summary-article-parent)))
9615     (when parent
9616       (gnus-summary-goto-subject parent))))
9617
9618 (defun gnus-summary-down-thread (n)
9619   "Go down thread N steps.
9620 If N is negative, go up instead.
9621 Returns the difference between N and how many steps down that were
9622 taken."
9623   (interactive "p")
9624   (let ((up (< n 0))
9625         (n (abs n)))
9626     (while (and (> n 0)
9627                 (if up (gnus-summary-go-up-thread)
9628                   (gnus-summary-go-down-thread)))
9629       (setq n (1- n)))
9630     (gnus-summary-position-point)
9631     (when (/= 0 n)
9632       (gnus-message 7 "Can't go further"))
9633     n))
9634
9635 (defun gnus-summary-up-thread (n)
9636   "Go up thread N steps.
9637 If N is negative, go down instead.
9638 Returns the difference between N and how many steps down that were
9639 taken."
9640   (interactive "p")
9641   (gnus-summary-down-thread (- n)))
9642
9643 (defun gnus-summary-top-thread ()
9644   "Go to the top of the thread."
9645   (interactive)
9646   (while (gnus-summary-go-up-thread))
9647   (gnus-summary-article-number))
9648
9649 (defun gnus-summary-kill-thread (&optional unmark)
9650   "Mark articles under current thread as read.
9651 If the prefix argument is positive, remove any kinds of marks.
9652 If the prefix argument is negative, tick articles instead."
9653   (interactive "P")
9654   (when unmark
9655     (setq unmark (prefix-numeric-value unmark)))
9656   (let ((articles (gnus-summary-articles-in-thread)))
9657     (save-excursion
9658       ;; Expand the thread.
9659       (gnus-summary-show-thread)
9660       ;; Mark all the articles.
9661       (while articles
9662         (gnus-summary-goto-subject (car articles))
9663         (cond ((null unmark)
9664                (gnus-summary-mark-article-as-read gnus-killed-mark))
9665               ((> unmark 0)
9666                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9667               (t
9668                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9669         (setq articles (cdr articles))))
9670     ;; Hide killed subtrees.
9671     (and (null unmark)
9672          gnus-thread-hide-killed
9673          (gnus-summary-hide-thread))
9674     ;; If marked as read, go to next unread subject.
9675     (when (null unmark)
9676       ;; Go to next unread subject.
9677       (gnus-summary-next-subject 1 t)))
9678   (gnus-set-mode-line 'summary))
9679
9680 ;; Summary sorting commands
9681
9682 (defun gnus-summary-sort-by-number (&optional reverse)
9683   "Sort the summary buffer by article number.
9684 Argument REVERSE means reverse order."
9685   (interactive "P")
9686   (gnus-summary-sort 'number reverse))
9687
9688 (defun gnus-summary-sort-by-author (&optional reverse)
9689   "Sort the summary buffer by author name alphabetically.
9690 If `case-fold-search' is non-nil, case of letters is ignored.
9691 Argument REVERSE means reverse order."
9692   (interactive "P")
9693   (gnus-summary-sort 'author reverse))
9694
9695 (defun gnus-summary-sort-by-subject (&optional reverse)
9696   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9697 If `case-fold-search' is non-nil, case of letters is ignored.
9698 Argument REVERSE means reverse order."
9699   (interactive "P")
9700   (gnus-summary-sort 'subject reverse))
9701
9702 (defun gnus-summary-sort-by-date (&optional reverse)
9703   "Sort the summary buffer by date.
9704 Argument REVERSE means reverse order."
9705   (interactive "P")
9706   (gnus-summary-sort 'date reverse))
9707
9708 (defun gnus-summary-sort-by-score (&optional reverse)
9709   "Sort the summary buffer by score.
9710 Argument REVERSE means reverse order."
9711   (interactive "P")
9712   (gnus-summary-sort 'score reverse))
9713
9714 (defun gnus-summary-sort-by-lines (&optional reverse)
9715   "Sort the summary buffer by the number of lines.
9716 Argument REVERSE means reverse order."
9717   (interactive "P")
9718   (gnus-summary-sort 'lines reverse))
9719
9720 (defun gnus-summary-sort-by-chars (&optional reverse)
9721   "Sort the summary buffer by article length.
9722 Argument REVERSE means reverse order."
9723   (interactive "P")
9724   (gnus-summary-sort 'chars reverse))
9725
9726 (defun gnus-summary-sort-by-original (&optional reverse)
9727   "Sort the summary buffer using the default sorting method.
9728 Argument REVERSE means reverse order."
9729   (interactive "P")
9730   (let* ((buffer-read-only)
9731          (gnus-summary-prepare-hook nil))
9732     ;; We do the sorting by regenerating the threads.
9733     (gnus-summary-prepare)
9734     ;; Hide subthreads if needed.
9735     (when (and gnus-show-threads gnus-thread-hide-subtree)
9736       (gnus-summary-hide-all-threads))))
9737
9738 (defun gnus-summary-sort (predicate reverse)
9739   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9740   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9741          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9742          (gnus-thread-sort-functions
9743           (if (not reverse)
9744               thread
9745             `(lambda (t1 t2)
9746                (,thread t2 t1))))
9747          (gnus-sort-gathered-threads-function
9748           gnus-thread-sort-functions)
9749          (gnus-article-sort-functions
9750           (if (not reverse)
9751               article
9752             `(lambda (t1 t2)
9753                (,article t2 t1))))
9754          (buffer-read-only)
9755          (gnus-summary-prepare-hook nil))
9756     ;; We do the sorting by regenerating the threads.
9757     (gnus-summary-prepare)
9758     ;; Hide subthreads if needed.
9759     (when (and gnus-show-threads gnus-thread-hide-subtree)
9760       (gnus-summary-hide-all-threads))))
9761
9762 ;; Summary saving commands.
9763
9764 (defun gnus-summary-save-article (&optional n not-saved)
9765   "Save the current article using the default saver function.
9766 If N is a positive number, save the N next articles.
9767 If N is a negative number, save the N previous articles.
9768 If N is nil and any articles have been marked with the process mark,
9769 save those articles instead.
9770 The variable `gnus-default-article-saver' specifies the saver function."
9771   (interactive "P")
9772   (let* ((articles (gnus-summary-work-articles n))
9773          (save-buffer (save-excursion
9774                         (nnheader-set-temp-buffer " *Gnus Save*")))
9775          (num (length articles))
9776          header file)
9777     (dolist (article articles)
9778       (setq header (gnus-summary-article-header article))
9779       (if (not (vectorp header))
9780           ;; This is a pseudo-article.
9781           (if (assq 'name header)
9782               (gnus-copy-file (cdr (assq 'name header)))
9783             (gnus-message 1 "Article %d is unsaveable" article))
9784         ;; This is a real article.
9785         (save-window-excursion
9786           (gnus-summary-select-article t nil nil article))
9787         (save-excursion
9788           (set-buffer save-buffer)
9789           (erase-buffer)
9790           (insert-buffer-substring gnus-original-article-buffer))
9791         (setq file (gnus-article-save save-buffer file num))
9792         (gnus-summary-remove-process-mark article)
9793         (unless not-saved
9794           (gnus-summary-set-saved-mark article))))
9795     (gnus-kill-buffer save-buffer)
9796     (gnus-summary-position-point)
9797     (gnus-set-mode-line 'summary)
9798     n))
9799
9800 (defun gnus-summary-pipe-output (&optional arg)
9801   "Pipe the current article to a subprocess.
9802 If N is a positive number, pipe the N next articles.
9803 If N is a negative number, pipe the N previous articles.
9804 If N is nil and any articles have been marked with the process mark,
9805 pipe those articles instead."
9806   (interactive "P")
9807   (require 'gnus-art)
9808   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9809     (gnus-summary-save-article arg t))
9810   (let ((buffer (get-buffer "*Shell Command Output*")))
9811     (if (and buffer
9812              (with-current-buffer buffer (> (point-max) (point-min))))
9813         (gnus-configure-windows 'pipe))))
9814
9815 (defun gnus-summary-save-article-mail (&optional arg)
9816   "Append the current article to an mail file.
9817 If N is a positive number, save the N next articles.
9818 If N is a negative number, save the N previous articles.
9819 If N is nil and any articles have been marked with the process mark,
9820 save those articles instead."
9821   (interactive "P")
9822   (require 'gnus-art)
9823   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9824     (gnus-summary-save-article arg)))
9825
9826 (defun gnus-summary-save-article-rmail (&optional arg)
9827   "Append the current article to an rmail file.
9828 If N is a positive number, save the N next articles.
9829 If N is a negative number, save the N previous articles.
9830 If N is nil and any articles have been marked with the process mark,
9831 save those articles instead."
9832   (interactive "P")
9833   (require 'gnus-art)
9834   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9835     (gnus-summary-save-article arg)))
9836
9837 (defun gnus-summary-save-article-file (&optional arg)
9838   "Append the current article to a file.
9839 If N is a positive number, save the N next articles.
9840 If N is a negative number, save the N previous articles.
9841 If N is nil and any articles have been marked with the process mark,
9842 save those articles instead."
9843   (interactive "P")
9844   (require 'gnus-art)
9845   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9846     (gnus-summary-save-article arg)))
9847
9848 (defun gnus-summary-write-article-file (&optional arg)
9849   "Write the current article to a file, deleting the previous file.
9850 If N is a positive number, save the N next articles.
9851 If N is a negative number, save the N previous articles.
9852 If N is nil and any articles have been marked with the process mark,
9853 save those articles instead."
9854   (interactive "P")
9855   (require 'gnus-art)
9856   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9857     (gnus-summary-save-article arg)))
9858
9859 (defun gnus-summary-save-article-body-file (&optional arg)
9860   "Append the current article body to a file.
9861 If N is a positive number, save the N next articles.
9862 If N is a negative number, save the N previous articles.
9863 If N is nil and any articles have been marked with the process mark,
9864 save those articles instead."
9865   (interactive "P")
9866   (require 'gnus-art)
9867   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9868     (gnus-summary-save-article arg)))
9869
9870 (defun gnus-summary-pipe-message (program)
9871   "Pipe the current article through PROGRAM."
9872   (interactive "sProgram: ")
9873   (gnus-summary-select-article)
9874   (let ((mail-header-separator ""))
9875     (gnus-eval-in-buffer-window gnus-article-buffer
9876       (save-restriction
9877         (widen)
9878         (let ((start (window-start))
9879               buffer-read-only)
9880           (message-pipe-buffer-body program)
9881           (set-window-start (get-buffer-window (current-buffer)) start))))))
9882
9883 (defun gnus-get-split-value (methods)
9884   "Return a value based on the split METHODS."
9885   (let (split-name method result match)
9886     (when methods
9887       (save-excursion
9888         (set-buffer gnus-original-article-buffer)
9889         (save-restriction
9890           (nnheader-narrow-to-headers)
9891           (while (and methods (not split-name))
9892             (goto-char (point-min))
9893             (setq method (pop methods))
9894             (setq match (car method))
9895             (when (cond
9896                    ((stringp match)
9897                     ;; Regular expression.
9898                     (ignore-errors
9899                       (re-search-forward match nil t)))
9900                    ((gnus-functionp match)
9901                     ;; Function.
9902                     (save-restriction
9903                       (widen)
9904                       (setq result (funcall match gnus-newsgroup-name))))
9905                    ((consp match)
9906                     ;; Form.
9907                     (save-restriction
9908                       (widen)
9909                       (setq result (eval match)))))
9910               (setq split-name (cdr method))
9911               (cond ((stringp result)
9912                      (push (expand-file-name
9913                             result gnus-article-save-directory)
9914                            split-name))
9915                     ((consp result)
9916                      (setq split-name (append result split-name)))))))))
9917     (nreverse split-name)))
9918
9919 (defun gnus-valid-move-group-p (group)
9920   (and (boundp group)
9921        (symbol-name group)
9922        (symbol-value group)
9923        (gnus-get-function (gnus-find-method-for-group
9924                            (symbol-name group)) 'request-accept-article t)))
9925
9926 (defun gnus-read-move-group-name (prompt default articles prefix)
9927   "Read a group name."
9928   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9929          (minibuffer-confirm-incomplete nil) ; XEmacs
9930          (prom
9931           (format "%s %s to:"
9932                   prompt
9933                   (if (> (length articles) 1)
9934                       (format "these %d articles" (length articles))
9935                     "this article")))
9936          (to-newsgroup
9937           (cond
9938            ((null split-name)
9939             (gnus-completing-read default prom
9940                                   gnus-active-hashtb
9941                                   'gnus-valid-move-group-p
9942                                   nil prefix
9943                                   'gnus-group-history))
9944            ((= 1 (length split-name))
9945             (gnus-completing-read (car split-name) prom
9946                                   gnus-active-hashtb
9947                                   'gnus-valid-move-group-p
9948                                   nil nil
9949                                   'gnus-group-history))
9950            (t
9951             (gnus-completing-read nil prom
9952                                   (mapcar (lambda (el) (list el))
9953                                           (nreverse split-name))
9954                                   nil nil nil
9955                                   'gnus-group-history))))
9956          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9957     (when to-newsgroup
9958       (if (or (string= to-newsgroup "")
9959               (string= to-newsgroup prefix))
9960           (setq to-newsgroup default))
9961       (unless to-newsgroup
9962         (error "No group name entered"))
9963       (or (gnus-active to-newsgroup)
9964           (gnus-activate-group to-newsgroup nil nil to-method)
9965           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9966                                      to-newsgroup))
9967               (or (and (gnus-request-create-group to-newsgroup to-method)
9968                        (gnus-activate-group
9969                         to-newsgroup nil nil to-method)
9970                        (gnus-subscribe-group to-newsgroup))
9971                   (error "Couldn't create group %s" to-newsgroup)))
9972           (error "No such group: %s" to-newsgroup)))
9973     to-newsgroup))
9974
9975 (defun gnus-summary-save-parts (type dir n &optional reverse)
9976   "Save parts matching TYPE to DIR.
9977 If REVERSE, save parts that do not match TYPE."
9978   (interactive
9979    (list (read-string "Save parts of type: "
9980                       (or (car gnus-summary-save-parts-type-history)
9981                           gnus-summary-save-parts-default-mime)
9982                       'gnus-summary-save-parts-type-history)
9983          (setq gnus-summary-save-parts-last-directory
9984                (read-file-name "Save to directory: "
9985                                gnus-summary-save-parts-last-directory
9986                                nil t))
9987          current-prefix-arg))
9988   (gnus-summary-iterate n
9989     (let ((gnus-display-mime-function nil)
9990           (gnus-inhibit-treatment t))
9991       (gnus-summary-select-article))
9992     (save-excursion
9993       (set-buffer gnus-article-buffer)
9994       (let ((handles (or gnus-article-mime-handles
9995                          (mm-dissect-buffer) (mm-uu-dissect))))
9996         (when handles
9997           (gnus-summary-save-parts-1 type dir handles reverse)
9998           (unless gnus-article-mime-handles ;; Don't destroy this case.
9999             (mm-destroy-parts handles)))))))
10000
10001 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10002   (if (stringp (car handle))
10003       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10004               (cdr handle))
10005     (when (if reverse
10006               (not (string-match type (mm-handle-media-type handle)))
10007             (string-match type (mm-handle-media-type handle)))
10008       (let ((file (expand-file-name
10009                    (file-name-nondirectory
10010                     (or
10011                      (mail-content-type-get
10012                       (mm-handle-disposition handle) 'filename)
10013                      (concat gnus-newsgroup-name
10014                              "." (number-to-string
10015                                   (cdr gnus-article-current)))))
10016                    dir)))
10017         (unless (file-exists-p file)
10018           (mm-save-part-to-file handle file))))))
10019
10020 ;; Summary extract commands
10021
10022 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10023   (let ((buffer-read-only nil)
10024         (article (gnus-summary-article-number))
10025         after-article b e)
10026     (unless (gnus-summary-goto-subject article)
10027       (error "No such article: %d" article))
10028     (gnus-summary-position-point)
10029     ;; If all commands are to be bunched up on one line, we collect
10030     ;; them here.
10031     (unless gnus-view-pseudos-separately
10032       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10033             files action)
10034         (while ps
10035           (setq action (cdr (assq 'action (car ps))))
10036           (setq files (list (cdr (assq 'name (car ps)))))
10037           (while (and ps (cdr ps)
10038                       (string= (or action "1")
10039                                (or (cdr (assq 'action (cadr ps))) "2")))
10040             (push (cdr (assq 'name (cadr ps))) files)
10041             (setcdr ps (cddr ps)))
10042           (when files
10043             (when (not (string-match "%s" action))
10044               (push " " files))
10045             (push " " files)
10046             (when (assq 'execute (car ps))
10047               (setcdr (assq 'execute (car ps))
10048                       (funcall (if (string-match "%s" action)
10049                                    'format 'concat)
10050                                action
10051                                (mapconcat
10052                                 (lambda (f)
10053                                   (if (equal f " ")
10054                                       f
10055                                     (mm-quote-arg f)))
10056                                 files " ")))))
10057           (setq ps (cdr ps)))))
10058     (if (and gnus-view-pseudos (not not-view))
10059         (while pslist
10060           (when (assq 'execute (car pslist))
10061             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10062                                   (eq gnus-view-pseudos 'not-confirm)))
10063           (setq pslist (cdr pslist)))
10064       (save-excursion
10065         (while pslist
10066           (setq after-article (or (cdr (assq 'article (car pslist)))
10067                                   (gnus-summary-article-number)))
10068           (gnus-summary-goto-subject after-article)
10069           (forward-line 1)
10070           (setq b (point))
10071           (insert "    " (file-name-nondirectory
10072                           (cdr (assq 'name (car pslist))))
10073                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10074           (setq e (point))
10075           (forward-line -1)             ; back to `b'
10076           (gnus-add-text-properties
10077            b (1- e) (list 'gnus-number gnus-reffed-article-number
10078                           gnus-mouse-face-prop gnus-mouse-face))
10079           (gnus-data-enter
10080            after-article gnus-reffed-article-number
10081            gnus-unread-mark b (car pslist) 0 (- e b))
10082           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10083           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10084           (setq pslist (cdr pslist)))))))
10085
10086 (defun gnus-pseudos< (p1 p2)
10087   (let ((c1 (cdr (assq 'action p1)))
10088         (c2 (cdr (assq 'action p2))))
10089     (and c1 c2 (string< c1 c2))))
10090
10091 (defun gnus-request-pseudo-article (props)
10092   (cond ((assq 'execute props)
10093          (gnus-execute-command (cdr (assq 'execute props)))))
10094   (let ((gnus-current-article (gnus-summary-article-number)))
10095     (gnus-run-hooks 'gnus-mark-article-hook)))
10096
10097 (defun gnus-execute-command (command &optional automatic)
10098   (save-excursion
10099     (gnus-article-setup-buffer)
10100     (set-buffer gnus-article-buffer)
10101     (setq buffer-read-only nil)
10102     (let ((command (if automatic command
10103                      (read-string "Command: " (cons command 0)))))
10104       (erase-buffer)
10105       (insert "$ " command "\n\n")
10106       (if gnus-view-pseudo-asynchronously
10107           (start-process "gnus-execute" (current-buffer) shell-file-name
10108                          shell-command-switch command)
10109         (call-process shell-file-name nil t nil
10110                       shell-command-switch command)))))
10111
10112 ;; Summary kill commands.
10113
10114 (defun gnus-summary-edit-global-kill (article)
10115   "Edit the \"global\" kill file."
10116   (interactive (list (gnus-summary-article-number)))
10117   (gnus-group-edit-global-kill article))
10118
10119 (defun gnus-summary-edit-local-kill ()
10120   "Edit a local kill file applied to the current newsgroup."
10121   (interactive)
10122   (setq gnus-current-headers (gnus-summary-article-header))
10123   (gnus-group-edit-local-kill
10124    (gnus-summary-article-number) gnus-newsgroup-name))
10125
10126 ;;; Header reading.
10127
10128 (defun gnus-read-header (id &optional header)
10129   "Read the headers of article ID and enter them into the Gnus system."
10130   (let ((group gnus-newsgroup-name)
10131         (gnus-override-method
10132          (or
10133           gnus-override-method
10134           (and (gnus-news-group-p gnus-newsgroup-name)
10135                (car (gnus-refer-article-methods)))))
10136         where)
10137     ;; First we check to see whether the header in question is already
10138     ;; fetched.
10139     (if (stringp id)
10140         ;; This is a Message-ID.
10141         (setq header (or header (gnus-id-to-header id)))
10142       ;; This is an article number.
10143       (setq header (or header (gnus-summary-article-header id))))
10144     (if (and header
10145              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10146         ;; We have found the header.
10147         header
10148       ;; If this is a sparse article, we have to nix out its
10149       ;; previous entry in the thread hashtb.
10150       (when (and header
10151                  (gnus-summary-article-sparse-p (mail-header-number header)))
10152         (let* ((parent (gnus-parent-id (mail-header-references header)))
10153                (thread (and parent (gnus-id-to-thread parent))))
10154           (when thread
10155             (delq (assq header thread) thread))))
10156       ;; We have to really fetch the header to this article.
10157       (save-excursion
10158         (set-buffer nntp-server-buffer)
10159         (when (setq where (gnus-request-head id group))
10160           (nnheader-fold-continuation-lines)
10161           (goto-char (point-max))
10162           (insert ".\n")
10163           (goto-char (point-min))
10164           (insert "211 ")
10165           (princ (cond
10166                   ((numberp id) id)
10167                   ((cdr where) (cdr where))
10168                   (header (mail-header-number header))
10169                   (t gnus-reffed-article-number))
10170                  (current-buffer))
10171           (insert " Article retrieved.\n"))
10172         (if (or (not where)
10173                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10174             ()                          ; Malformed head.
10175           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10176             (when (and (stringp id)
10177                        (not (string= (gnus-group-real-name group)
10178                                      (car where))))
10179               ;; If we fetched by Message-ID and the article came
10180               ;; from a different group, we fudge some bogus article
10181               ;; numbers for this article.
10182               (mail-header-set-number header gnus-reffed-article-number))
10183             (save-excursion
10184               (set-buffer gnus-summary-buffer)
10185               (decf gnus-reffed-article-number)
10186               (gnus-remove-header (mail-header-number header))
10187               (push header gnus-newsgroup-headers)
10188               (setq gnus-current-headers header)
10189               (push (mail-header-number header) gnus-newsgroup-limit)))
10190           header)))))
10191
10192 (defun gnus-remove-header (number)
10193   "Remove header NUMBER from `gnus-newsgroup-headers'."
10194   (if (and gnus-newsgroup-headers
10195            (= number (mail-header-number (car gnus-newsgroup-headers))))
10196       (pop gnus-newsgroup-headers)
10197     (let ((headers gnus-newsgroup-headers))
10198       (while (and (cdr headers)
10199                   (not (= number (mail-header-number (cadr headers)))))
10200         (pop headers))
10201       (when (cdr headers)
10202         (setcdr headers (cddr headers))))))
10203
10204 ;;;
10205 ;;; summary highlights
10206 ;;;
10207
10208 (defun gnus-highlight-selected-summary ()
10209   "Highlight selected article in summary buffer."
10210   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10211   (when gnus-summary-selected-face
10212     (save-excursion
10213       (let* ((beg (progn (beginning-of-line) (point)))
10214              (end (progn (end-of-line) (point)))
10215              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10216              (from (if (get-text-property beg gnus-mouse-face-prop)
10217                        beg
10218                      (or (next-single-property-change
10219                           beg gnus-mouse-face-prop nil end)
10220                          beg)))
10221              (to
10222               (if (= from end)
10223                   (- from 2)
10224                 (or (next-single-property-change
10225                      from gnus-mouse-face-prop nil end)
10226                     end))))
10227         ;; If no mouse-face prop on line we will have to = from = end,
10228         ;; so we highlight the entire line instead.
10229         (when (= (+ to 2) from)
10230           (setq from beg)
10231           (setq to end))
10232         (if gnus-newsgroup-selected-overlay
10233             ;; Move old overlay.
10234             (gnus-move-overlay
10235              gnus-newsgroup-selected-overlay from to (current-buffer))
10236           ;; Create new overlay.
10237           (gnus-overlay-put
10238            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10239            'face gnus-summary-selected-face))))))
10240
10241 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10242 (defun gnus-summary-highlight-line ()
10243   "Highlight current line according to `gnus-summary-highlight'."
10244   (let* ((list gnus-summary-highlight)
10245          (p (point))
10246          (end (progn (end-of-line) (point)))
10247          ;; now find out where the line starts and leave point there.
10248          (beg (progn (beginning-of-line) (point)))
10249          (article (gnus-summary-article-number))
10250          (score (or (cdr (assq (or article gnus-current-article)
10251                                gnus-newsgroup-scored))
10252                     gnus-summary-default-score 0))
10253          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10254          (inhibit-read-only t))
10255     ;; Eval the cars of the lists until we find a match.
10256     (let ((default gnus-summary-default-score)
10257           (default-high gnus-summary-default-high-score)
10258           (default-low gnus-summary-default-low-score))
10259       (while (and list
10260                   (not (eval (caar list))))
10261         (setq list (cdr list))))
10262     (let ((face (cdar list)))
10263       (unless (eq face (get-text-property beg 'face))
10264         (gnus-put-text-property-excluding-characters-with-faces
10265          beg end 'face
10266          (setq face (if (boundp face) (symbol-value face) face)))
10267         (when gnus-summary-highlight-line-function
10268           (funcall gnus-summary-highlight-line-function article face))))
10269     (goto-char p)))
10270
10271 (defun gnus-update-read-articles (group unread &optional compute)
10272   "Update the list of read articles in GROUP."
10273   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10274          (entry (gnus-gethash group gnus-newsrc-hashtb))
10275          (info (nth 2 entry))
10276          (prev 1)
10277          (unread (sort (copy-sequence unread) '<))
10278          read)
10279     (if (or (not info) (not active))
10280         ;; There is no info on this group if it was, in fact,
10281         ;; killed.  Gnus stores no information on killed groups, so
10282         ;; there's nothing to be done.
10283         ;; One could store the information somewhere temporarily,
10284         ;; perhaps...  Hmmm...
10285         ()
10286       ;; Remove any negative articles numbers.
10287       (while (and unread (< (car unread) 0))
10288         (setq unread (cdr unread)))
10289       ;; Remove any expired article numbers
10290       (while (and unread (< (car unread) (car active)))
10291         (setq unread (cdr unread)))
10292       ;; Compute the ranges of read articles by looking at the list of
10293       ;; unread articles.
10294       (while unread
10295         (when (/= (car unread) prev)
10296           (push (if (= prev (1- (car unread))) prev
10297                   (cons prev (1- (car unread))))
10298                 read))
10299         (setq prev (1+ (car unread)))
10300         (setq unread (cdr unread)))
10301       (when (<= prev (cdr active))
10302         (push (cons prev (cdr active)) read))
10303       (setq read (if (> (length read) 1) (nreverse read) read))
10304       (if compute
10305           read
10306         (save-excursion
10307           (let (setmarkundo)
10308             ;; Propagate the read marks to the backend.
10309             (when (gnus-check-backend-function 'request-set-mark group)
10310               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10311                     (add (gnus-remove-from-range read (gnus-info-read info))))
10312                 (when (or add del)
10313                   (unless (gnus-check-group group)
10314                     (error "Can't open server for %s" group))
10315                   (gnus-request-set-mark
10316                    group (delq nil (list (if add (list add 'add '(read)))
10317                                          (if del (list del 'del '(read))))))
10318                   (setq setmarkundo
10319                         `(gnus-request-set-mark
10320                           ,group
10321                           ',(delq nil (list
10322                                        (if del (list del 'add '(read)))
10323                                        (if add (list add 'del '(read))))))))))
10324             (set-buffer gnus-group-buffer)
10325             (gnus-undo-register
10326               `(progn
10327                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10328                  (gnus-info-set-read ',info ',(gnus-info-read info))
10329                  (gnus-get-unread-articles-in-group ',info
10330                                                     (gnus-active ,group))
10331                  (gnus-group-update-group ,group t)
10332                  ,setmarkundo))))
10333         ;; Enter this list into the group info.
10334         (gnus-info-set-read info read)
10335         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10336         (gnus-get-unread-articles-in-group info (gnus-active group))
10337         t))))
10338
10339 (defun gnus-offer-save-summaries ()
10340   "Offer to save all active summary buffers."
10341   (let (buffers)
10342     ;; Go through all buffers and find all summaries.
10343     (dolist (buffer (buffer-list))
10344       (when (and (setq buffer (buffer-name buffer))
10345                  (string-match "Summary" buffer)
10346                  (save-excursion
10347                    (set-buffer buffer)
10348                    ;; We check that this is, indeed, a summary buffer.
10349                    (and (eq major-mode 'gnus-summary-mode)
10350                         ;; Also make sure this isn't bogus.
10351                         gnus-newsgroup-prepared
10352                         ;; Also make sure that this isn't a
10353                         ;; dead summary buffer.
10354                         (not gnus-dead-summary-mode))))
10355         (push buffer buffers)))
10356     ;; Go through all these summary buffers and offer to save them.
10357     (when buffers
10358       (save-excursion
10359         (map-y-or-n-p
10360          "Update summary buffer %s? "
10361          (lambda (buf)
10362            (switch-to-buffer buf)
10363            (gnus-summary-exit))
10364          buffers)))))
10365
10366 (defun gnus-summary-setup-default-charset ()
10367   "Setup newsgroup default charset."
10368   (if (equal gnus-newsgroup-name "nndraft:drafts")
10369       (setq gnus-newsgroup-charset nil)
10370     (let* ((ignored-charsets
10371             (or gnus-newsgroup-ephemeral-ignored-charsets
10372                 (append
10373                  (and gnus-newsgroup-name
10374                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10375                  gnus-newsgroup-ignored-charsets))))
10376       (setq gnus-newsgroup-charset
10377             (or gnus-newsgroup-ephemeral-charset
10378                 (and gnus-newsgroup-name
10379                      (gnus-parameter-charset gnus-newsgroup-name))
10380                 gnus-default-charset))
10381       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10382            ignored-charsets))))
10383
10384 ;;;
10385 ;;; Mime Commands
10386 ;;;
10387
10388 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10389   "Display the current article buffer fully MIME-buttonized.
10390 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10391 treated as multipart/mixed."
10392   (interactive "P")
10393   (require 'gnus-art)
10394   (let ((gnus-unbuttonized-mime-types nil)
10395         (gnus-mime-display-multipart-as-mixed show-all-parts))
10396     (gnus-summary-show-article)))
10397
10398 (defun gnus-summary-repair-multipart (article)
10399   "Add a Content-Type header to a multipart article without one."
10400   (interactive (list (gnus-summary-article-number)))
10401   (gnus-with-article article
10402     (message-narrow-to-head)
10403     (message-remove-header "Mime-Version")
10404     (goto-char (point-max))
10405     (insert "Mime-Version: 1.0\n")
10406     (widen)
10407     (when (search-forward "\n--" nil t)
10408       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10409         (message-narrow-to-head)
10410         (message-remove-header "Content-Type")
10411         (goto-char (point-max))
10412         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10413                         separator))
10414         (widen))))
10415   (let (gnus-mark-article-hook)
10416     (gnus-summary-select-article t t nil article)))
10417
10418 (defun gnus-summary-toggle-display-buttonized ()
10419   "Toggle the buttonizing of the article buffer."
10420   (interactive)
10421   (require 'gnus-art)
10422   (if (setq gnus-inhibit-mime-unbuttonizing
10423             (not gnus-inhibit-mime-unbuttonizing))
10424       (let ((gnus-unbuttonized-mime-types nil))
10425         (gnus-summary-show-article))
10426     (gnus-summary-show-article)))
10427
10428 ;;;
10429 ;;; Generic summary marking commands
10430 ;;;
10431
10432 (defvar gnus-summary-marking-alist
10433   '((read gnus-del-mark "d")
10434     (unread gnus-unread-mark "u")
10435     (ticked gnus-ticked-mark "!")
10436     (dormant gnus-dormant-mark "?")
10437     (expirable gnus-expirable-mark "e"))
10438   "An alist of names/marks/keystrokes.")
10439
10440 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10441 (defvar gnus-summary-mark-map)
10442
10443 (defun gnus-summary-make-all-marking-commands ()
10444   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10445   (dolist (elem gnus-summary-marking-alist)
10446     (apply 'gnus-summary-make-marking-command elem)))
10447
10448 (defun gnus-summary-make-marking-command (name mark keystroke)
10449   (let ((map (make-sparse-keymap)))
10450     (define-key gnus-summary-generic-mark-map keystroke map)
10451     (dolist (lway `((next "next" next nil "n")
10452                     (next-unread "next unread" next t "N")
10453                     (prev "previous" prev nil "p")
10454                     (prev-unread "previous unread" prev t "P")
10455                     (nomove "" nil nil ,keystroke)))
10456       (let ((func (gnus-summary-make-marking-command-1
10457                    mark (car lway) lway name)))
10458         (setq func (eval func))
10459         (define-key map (nth 4 lway) func)))))
10460
10461 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10462   `(defun ,(intern
10463             (format "gnus-summary-put-mark-as-%s%s"
10464                     name (if (eq way 'nomove)
10465                              ""
10466                            (concat "-" (symbol-name way)))))
10467      (n)
10468      ,(format
10469        "Mark the current article as %s%s.
10470 If N, the prefix, then repeat N times.
10471 If N is negative, move in reverse order.
10472 The difference between N and the actual number of articles marked is
10473 returned."
10474        name (cadr lway))
10475      (interactive "p")
10476      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10477
10478 (defun gnus-summary-generic-mark (n mark move unread)
10479   "Mark N articles with MARK."
10480   (unless (eq major-mode 'gnus-summary-mode)
10481     (error "This command can only be used in the summary buffer"))
10482   (gnus-summary-show-thread)
10483   (let ((nummove
10484          (cond
10485           ((eq move 'next) 1)
10486           ((eq move 'prev) -1)
10487           (t 0))))
10488     (if (zerop nummove)
10489         (setq n 1)
10490       (when (< n 0)
10491         (setq n (abs n)
10492               nummove (* -1 nummove))))
10493     (while (and (> n 0)
10494                 (gnus-summary-mark-article nil mark)
10495                 (zerop (gnus-summary-next-subject nummove unread t)))
10496       (setq n (1- n)))
10497     (when (/= 0 n)
10498       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10499     (gnus-summary-recenter)
10500     (gnus-summary-position-point)
10501     (gnus-set-mode-line 'summary)
10502     n))
10503
10504 (defun gnus-summary-insert-articles (articles)
10505   (when (setq articles
10506               (gnus-set-difference articles
10507                                    (mapcar (lambda (h) (mail-header-number h))
10508                                            gnus-newsgroup-headers)))
10509     (setq gnus-newsgroup-headers
10510           (merge 'list
10511                  gnus-newsgroup-headers
10512                  (gnus-fetch-headers articles)
10513                  'gnus-article-sort-by-number))
10514     ;; Suppress duplicates?
10515     (when gnus-suppress-duplicates
10516       (gnus-dup-suppress-articles))
10517
10518     ;; We might want to build some more threads first.
10519     (when (and gnus-fetch-old-headers
10520                (eq gnus-headers-retrieved-by 'nov))
10521       (if (eq gnus-fetch-old-headers 'invisible)
10522           (gnus-build-all-threads)
10523         (gnus-build-old-threads)))
10524     ;; Let the Gnus agent mark articles as read.
10525     (when gnus-agent
10526       (gnus-agent-get-undownloaded-list))
10527     ;; Remove list identifiers from subject
10528     (when gnus-list-identifiers
10529       (gnus-summary-remove-list-identifiers))
10530     ;; First and last article in this newsgroup.
10531     (when gnus-newsgroup-headers
10532       (setq gnus-newsgroup-begin
10533             (mail-header-number (car gnus-newsgroup-headers))
10534             gnus-newsgroup-end
10535             (mail-header-number
10536              (gnus-last-element gnus-newsgroup-headers))))
10537     (when gnus-use-scoring
10538       (gnus-possibly-score-headers))))
10539
10540 (defun gnus-summary-insert-old-articles (&optional all)
10541   "Insert all old articles in this group.
10542 If ALL is non-nil, already read articles become readable.
10543 If ALL is a number, fetch this number of articles."
10544   (interactive "P")
10545   (prog1
10546       (let ((old (mapcar 'car gnus-newsgroup-data))
10547             (i (car gnus-newsgroup-active))
10548             older len)
10549         (while (<= i (cdr gnus-newsgroup-active))
10550           (or (memq i old) (push i older))
10551           (incf i))
10552         (setq len (length older))
10553         (cond
10554          ((null older) nil)
10555          ((numberp all)
10556           (if (< all len)
10557               (setq older (subseq older 0 all))))
10558          (all nil)
10559          (t
10560           (if (and (numberp gnus-large-newsgroup)
10561                    (> len gnus-large-newsgroup))
10562               (let ((input
10563                      (read-string
10564                       (format
10565                        "How many articles from %s (default %d): "
10566                        (gnus-limit-string
10567                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10568                        len))))
10569                 (unless (string-match "^[ \t]*$" input)
10570                   (setq all (string-to-number input))
10571                   (if (< all len)
10572                       (setq older (subseq older 0 all))))))))
10573         (if (not older)
10574             (message "No old news.")
10575           (gnus-summary-insert-articles older)
10576           (gnus-summary-limit (gnus-union older old))))
10577     (gnus-summary-position-point)))
10578
10579 (defun gnus-summary-insert-new-articles ()
10580   "Insert all new articles in this group."
10581   (interactive)
10582   (prog1
10583       (let ((old (mapcar 'car gnus-newsgroup-data))
10584             (old-active gnus-newsgroup-active)
10585             (nnmail-fetched-sources (list t))
10586             i new)
10587         (setq gnus-newsgroup-active
10588               (gnus-activate-group gnus-newsgroup-name 'scan))
10589         (setq i (1+ (cdr old-active)))
10590         (while (<= i (cdr gnus-newsgroup-active))
10591           (push i new)
10592           (incf i))
10593         (if (not new)
10594             (message "No gnus is bad news.")
10595           (setq new (nreverse new))
10596           (gnus-summary-insert-articles new)
10597           (setq gnus-newsgroup-unreads
10598                 (append gnus-newsgroup-unreads new))
10599           (gnus-summary-limit (gnus-union old new))))
10600     (gnus-summary-position-point)))
10601
10602 (gnus-summary-make-all-marking-commands)
10603
10604 (gnus-ems-redefine)
10605
10606 (provide 'gnus-sum)
10607
10608 (run-hooks 'gnus-sum-load-hook)
10609
10610 ;;; gnus-sum.el ends here