e6d6fe05fe2ee0b518a20a4b2fd62f5d1c7fdeea
[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 gnus-tmp-from)
1075         ?s)
1076     (?t (gnus-summary-number-of-articles-in-thread
1077          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1078         ?d)
1079     (?e (gnus-summary-number-of-articles-in-thread
1080          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1081         ?c)
1082     (?u gnus-tmp-user-defined ?s)
1083     (?P (gnus-pick-line-number) ?d)
1084     (?B gnus-tmp-thread-tree-header-string ?s)
1085     (user-date (gnus-user-date 
1086                 ,(macroexpand '(mail-header-date gnus-tmp-header))) ?s))
1087   "An alist of format specifications that can appear in summary lines.
1088 These are paired with what variables they correspond with, along with
1089 the type of the variable (string, integer, character, etc).")
1090
1091 (defvar gnus-summary-dummy-line-format-alist
1092   `((?S gnus-tmp-subject ?s)
1093     (?N gnus-tmp-number ?d)
1094     (?u gnus-tmp-user-defined ?s)))
1095
1096 (defvar gnus-summary-mode-line-format-alist
1097   `((?G gnus-tmp-group-name ?s)
1098     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1099     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1100     (?A gnus-tmp-article-number ?d)
1101     (?Z gnus-tmp-unread-and-unselected ?s)
1102     (?V gnus-version ?s)
1103     (?U gnus-tmp-unread-and-unticked ?d)
1104     (?S gnus-tmp-subject ?s)
1105     (?e gnus-tmp-unselected ?d)
1106     (?u gnus-tmp-user-defined ?s)
1107     (?d (length gnus-newsgroup-dormant) ?d)
1108     (?t (length gnus-newsgroup-marked) ?d)
1109     (?r (length gnus-newsgroup-reads) ?d)
1110     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1111     (?E gnus-newsgroup-expunged-tally ?d)
1112     (?s (gnus-current-score-file-nondirectory) ?s)))
1113
1114 (defvar gnus-last-search-regexp nil
1115   "Default regexp for article search command.")
1116
1117 (defvar gnus-last-shell-command nil
1118   "Default shell command on article.")
1119
1120 (defvar gnus-newsgroup-begin nil)
1121 (defvar gnus-newsgroup-end nil)
1122 (defvar gnus-newsgroup-last-rmail nil)
1123 (defvar gnus-newsgroup-last-mail nil)
1124 (defvar gnus-newsgroup-last-folder nil)
1125 (defvar gnus-newsgroup-last-file nil)
1126 (defvar gnus-newsgroup-auto-expire nil)
1127 (defvar gnus-newsgroup-active nil)
1128
1129 (defvar gnus-newsgroup-data nil)
1130 (defvar gnus-newsgroup-data-reverse nil)
1131 (defvar gnus-newsgroup-limit nil)
1132 (defvar gnus-newsgroup-limits nil)
1133
1134 (defvar gnus-newsgroup-unreads nil
1135   "List of unread articles in the current newsgroup.")
1136
1137 (defvar gnus-newsgroup-unselected nil
1138   "List of unselected unread articles in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-reads nil
1141   "Alist of read articles and article marks in the current newsgroup.")
1142
1143 (defvar gnus-newsgroup-expunged-tally nil)
1144
1145 (defvar gnus-newsgroup-marked nil
1146   "List of ticked articles in the current newsgroup (a subset of unread art).")
1147
1148 (defvar gnus-newsgroup-killed nil
1149   "List of ranges of articles that have been through the scoring process.")
1150
1151 (defvar gnus-newsgroup-cached nil
1152   "List of articles that come from the article cache.")
1153
1154 (defvar gnus-newsgroup-saved nil
1155   "List of articles that have been saved.")
1156
1157 (defvar gnus-newsgroup-kill-headers nil)
1158
1159 (defvar gnus-newsgroup-replied nil
1160   "List of articles that have been replied to in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-forwarded nil
1163   "List of articles that have been forwarded in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-recent nil
1166   "List of articles that have are recent in the current newsgroup.")
1167
1168 (defvar gnus-newsgroup-expirable nil
1169   "List of articles in the current newsgroup that can be expired.")
1170
1171 (defvar gnus-newsgroup-processable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-downloadable nil
1175   "List of articles in the current newsgroup that can be processed.")
1176
1177 (defvar gnus-newsgroup-undownloaded nil
1178   "List of articles in the current newsgroup that haven't been downloaded..")
1179
1180 (defvar gnus-newsgroup-unsendable nil
1181   "List of articles in the current newsgroup that won't be sent.")
1182
1183 (defvar gnus-newsgroup-bookmarks nil
1184   "List of articles in the current newsgroup that have bookmarks.")
1185
1186 (defvar gnus-newsgroup-dormant nil
1187   "List of dormant articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-unseen nil
1190   "List of unseen articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-seen nil
1193   "Range of seen articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-articles nil
1196   "List of articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-scored nil
1199   "List of scored articles in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-headers nil
1202   "List of article headers in the current newsgroup.")
1203
1204 (defvar gnus-newsgroup-threads nil)
1205
1206 (defvar gnus-newsgroup-prepared nil
1207   "Whether the current group has been prepared properly.")
1208
1209 (defvar gnus-newsgroup-ancient nil
1210   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1211
1212 (defvar gnus-newsgroup-sparse nil)
1213
1214 (defvar gnus-current-article nil)
1215 (defvar gnus-article-current nil)
1216 (defvar gnus-current-headers nil)
1217 (defvar gnus-have-all-headers nil)
1218 (defvar gnus-last-article nil)
1219 (defvar gnus-newsgroup-history nil)
1220 (defvar gnus-newsgroup-charset nil)
1221 (defvar gnus-newsgroup-ephemeral-charset nil)
1222 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1223
1224 (defvar gnus-article-before-search nil)
1225
1226 (defconst gnus-summary-local-variables
1227   '(gnus-newsgroup-name
1228     gnus-newsgroup-begin gnus-newsgroup-end
1229     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1230     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1231     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1232     gnus-newsgroup-unselected gnus-newsgroup-marked
1233     gnus-newsgroup-reads gnus-newsgroup-saved
1234     gnus-newsgroup-replied gnus-newsgroup-forwarded
1235     gnus-newsgroup-recent
1236     gnus-newsgroup-expirable
1237     gnus-newsgroup-processable gnus-newsgroup-killed
1238     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1239     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1240     gnus-newsgroup-seen gnus-newsgroup-articles
1241     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1242     gnus-newsgroup-headers gnus-newsgroup-threads
1243     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1244     gnus-current-article gnus-current-headers gnus-have-all-headers
1245     gnus-last-article gnus-article-internal-prepare-hook
1246     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1247     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1248     gnus-thread-expunge-below
1249     gnus-score-alist gnus-current-score-file
1250     (gnus-summary-expunge-below . global)
1251     (gnus-summary-mark-below . global)
1252     (gnus-orphan-score . global)
1253     gnus-newsgroup-active gnus-scores-exclude-files
1254     gnus-newsgroup-history gnus-newsgroup-ancient
1255     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1256     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1257     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1258     (gnus-newsgroup-expunged-tally . 0)
1259     gnus-cache-removable-articles gnus-newsgroup-cached
1260     gnus-newsgroup-data gnus-newsgroup-data-reverse
1261     gnus-newsgroup-limit gnus-newsgroup-limits
1262     gnus-newsgroup-charset gnus-newsgroup-display)
1263   "Variables that are buffer-local to the summary buffers.")
1264
1265 (defvar gnus-newsgroup-variables nil
1266   "Variables that have separate values in the newsgroups.")
1267
1268 ;; Byte-compiler warning.
1269 (eval-when-compile (defvar gnus-article-mode-map))
1270
1271 ;; MIME stuff.
1272
1273 (defvar gnus-decode-encoded-word-methods
1274   '(mail-decode-encoded-word-string)
1275   "List of methods used to decode encoded words.
1276
1277 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1278 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1279 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1280 whose names match REGEXP.
1281
1282 For example:
1283 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1284  mail-decode-encoded-word-string
1285  (\"chinese\" . rfc1843-decode-string))")
1286
1287 (defvar gnus-decode-encoded-word-methods-cache nil)
1288
1289 (defun gnus-multi-decode-encoded-word-string (string)
1290   "Apply the functions from `gnus-encoded-word-methods' that match."
1291   (unless (and gnus-decode-encoded-word-methods-cache
1292                (eq gnus-newsgroup-name
1293                    (car gnus-decode-encoded-word-methods-cache)))
1294     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1295     (mapcar (lambda (x)
1296               (if (symbolp x)
1297                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1298                 (if (and gnus-newsgroup-name
1299                          (string-match (car x) gnus-newsgroup-name))
1300                     (nconc gnus-decode-encoded-word-methods-cache
1301                            (list (cdr x))))))
1302             gnus-decode-encoded-word-methods))
1303   (let ((xlist gnus-decode-encoded-word-methods-cache))
1304     (pop xlist)
1305     (while xlist
1306       (setq string (funcall (pop xlist) string))))
1307   string)
1308
1309 ;; Subject simplification.
1310
1311 (defun gnus-simplify-whitespace (str)
1312   "Remove excessive whitespace from STR."
1313   (let ((mystr str))
1314     ;; Multiple spaces.
1315     (while (string-match "[ \t][ \t]+" mystr)
1316       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1317                           " "
1318                           (substring mystr (match-end 0)))))
1319     ;; Leading spaces.
1320     (when (string-match "^[ \t]+" mystr)
1321       (setq mystr (substring mystr (match-end 0))))
1322     ;; Trailing spaces.
1323     (when (string-match "[ \t]+$" mystr)
1324       (setq mystr (substring mystr 0 (match-beginning 0))))
1325     mystr))
1326
1327 (defsubst gnus-simplify-subject-re (subject)
1328   "Remove \"Re:\" from subject lines."
1329   (if (string-match message-subject-re-regexp subject)
1330       (substring subject (match-end 0))
1331     subject))
1332
1333 (defun gnus-simplify-subject (subject &optional re-only)
1334   "Remove `Re:' and words in parentheses.
1335 If RE-ONLY is non-nil, strip leading `Re:'s only."
1336   (let ((case-fold-search t))           ;Ignore case.
1337     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1338     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1339       (setq subject (substring subject (match-end 0))))
1340     ;; Remove uninteresting prefixes.
1341     (when (and (not re-only)
1342                gnus-simplify-ignored-prefixes
1343                (string-match gnus-simplify-ignored-prefixes subject))
1344       (setq subject (substring subject (match-end 0))))
1345     ;; Remove words in parentheses from end.
1346     (unless re-only
1347       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1348         (setq subject (substring subject 0 (match-beginning 0)))))
1349     ;; Return subject string.
1350     subject))
1351
1352 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1353 ;; all whitespace.
1354 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1355   (goto-char (point-min))
1356   (while (re-search-forward regexp nil t)
1357     (replace-match (or newtext ""))))
1358
1359 (defun gnus-simplify-buffer-fuzzy ()
1360   "Simplify string in the buffer fuzzily.
1361 The string in the accessible portion of the current buffer is simplified.
1362 It is assumed to be a single-line subject.
1363 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1364 matter is removed.  Additional things can be deleted by setting
1365 `gnus-simplify-subject-fuzzy-regexp'."
1366   (let ((case-fold-search t)
1367         (modified-tick))
1368     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1369
1370     (while (not (eq modified-tick (buffer-modified-tick)))
1371       (setq modified-tick (buffer-modified-tick))
1372       (cond
1373        ((listp gnus-simplify-subject-fuzzy-regexp)
1374         (mapcar 'gnus-simplify-buffer-fuzzy-step
1375                 gnus-simplify-subject-fuzzy-regexp))
1376        (gnus-simplify-subject-fuzzy-regexp
1377         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1378       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1379       (gnus-simplify-buffer-fuzzy-step
1380        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1381       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1382
1383     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1384     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1385     (gnus-simplify-buffer-fuzzy-step " $")
1386     (gnus-simplify-buffer-fuzzy-step "^ +")))
1387
1388 (defun gnus-simplify-subject-fuzzy (subject)
1389   "Simplify a subject string fuzzily.
1390 See `gnus-simplify-buffer-fuzzy' for details."
1391   (save-excursion
1392     (gnus-set-work-buffer)
1393     (let ((case-fold-search t))
1394       ;; Remove uninteresting prefixes.
1395       (when (and gnus-simplify-ignored-prefixes
1396                  (string-match gnus-simplify-ignored-prefixes subject))
1397         (setq subject (substring subject (match-end 0))))
1398       (insert subject)
1399       (inline (gnus-simplify-buffer-fuzzy))
1400       (buffer-string))))
1401
1402 (defsubst gnus-simplify-subject-fully (subject)
1403   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1404   (cond
1405    (gnus-simplify-subject-functions
1406     (gnus-map-function gnus-simplify-subject-functions subject))
1407    ((null gnus-summary-gather-subject-limit)
1408     (gnus-simplify-subject-re subject))
1409    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1410     (gnus-simplify-subject-fuzzy subject))
1411    ((numberp gnus-summary-gather-subject-limit)
1412     (gnus-limit-string (gnus-simplify-subject-re subject)
1413                        gnus-summary-gather-subject-limit))
1414    (t
1415     subject)))
1416
1417 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1418   "Check whether two subjects are equal.
1419 If optional argument simple-first is t, first argument is already
1420 simplified."
1421   (cond
1422    ((null simple-first)
1423     (equal (gnus-simplify-subject-fully s1)
1424            (gnus-simplify-subject-fully s2)))
1425    (t
1426     (equal s1
1427            (gnus-simplify-subject-fully s2)))))
1428
1429 (defun gnus-summary-bubble-group ()
1430   "Increase the score of the current group.
1431 This is a handy function to add to `gnus-summary-exit-hook' to
1432 increase the score of each group you read."
1433   (gnus-group-add-score gnus-newsgroup-name))
1434
1435 \f
1436 ;;;
1437 ;;; Gnus summary mode
1438 ;;;
1439
1440 (put 'gnus-summary-mode 'mode-class 'special)
1441
1442 (defvar gnus-article-commands-menu)
1443
1444 (when t
1445   ;; Non-orthogonal keys
1446
1447   (gnus-define-keys gnus-summary-mode-map
1448     " " gnus-summary-next-page
1449     "\177" gnus-summary-prev-page
1450     [delete] gnus-summary-prev-page
1451     [backspace] gnus-summary-prev-page
1452     "\r" gnus-summary-scroll-up
1453     "\M-\r" gnus-summary-scroll-down
1454     "n" gnus-summary-next-unread-article
1455     "p" gnus-summary-prev-unread-article
1456     "N" gnus-summary-next-article
1457     "P" gnus-summary-prev-article
1458     "\M-\C-n" gnus-summary-next-same-subject
1459     "\M-\C-p" gnus-summary-prev-same-subject
1460     "\M-n" gnus-summary-next-unread-subject
1461     "\M-p" gnus-summary-prev-unread-subject
1462     "." gnus-summary-first-unread-article
1463     "," gnus-summary-best-unread-article
1464     "\M-s" gnus-summary-search-article-forward
1465     "\M-r" gnus-summary-search-article-backward
1466     "<" gnus-summary-beginning-of-article
1467     ">" gnus-summary-end-of-article
1468     "j" gnus-summary-goto-article
1469     "^" gnus-summary-refer-parent-article
1470     "\M-^" gnus-summary-refer-article
1471     "u" gnus-summary-tick-article-forward
1472     "!" gnus-summary-tick-article-forward
1473     "U" gnus-summary-tick-article-backward
1474     "d" gnus-summary-mark-as-read-forward
1475     "D" gnus-summary-mark-as-read-backward
1476     "E" gnus-summary-mark-as-expirable
1477     "\M-u" gnus-summary-clear-mark-forward
1478     "\M-U" gnus-summary-clear-mark-backward
1479     "k" gnus-summary-kill-same-subject-and-select
1480     "\C-k" gnus-summary-kill-same-subject
1481     "\M-\C-k" gnus-summary-kill-thread
1482     "\M-\C-l" gnus-summary-lower-thread
1483     "e" gnus-summary-edit-article
1484     "#" gnus-summary-mark-as-processable
1485     "\M-#" gnus-summary-unmark-as-processable
1486     "\M-\C-t" gnus-summary-toggle-threads
1487     "\M-\C-s" gnus-summary-show-thread
1488     "\M-\C-h" gnus-summary-hide-thread
1489     "\M-\C-f" gnus-summary-next-thread
1490     "\M-\C-b" gnus-summary-prev-thread
1491     [(meta down)] gnus-summary-next-thread
1492     [(meta up)] gnus-summary-prev-thread
1493     "\M-\C-u" gnus-summary-up-thread
1494     "\M-\C-d" gnus-summary-down-thread
1495     "&" gnus-summary-execute-command
1496     "c" gnus-summary-catchup-and-exit
1497     "\C-w" gnus-summary-mark-region-as-read
1498     "\C-t" gnus-summary-toggle-truncation
1499     "?" gnus-summary-mark-as-dormant
1500     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1501     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1502     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1503     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1504     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1505     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1506     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1507     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1508     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1509     "=" gnus-summary-expand-window
1510     "\C-x\C-s" gnus-summary-reselect-current-group
1511     "\M-g" gnus-summary-rescan-group
1512     "w" gnus-summary-stop-page-breaking
1513     "\C-c\C-r" gnus-summary-caesar-message
1514     "f" gnus-summary-followup
1515     "F" gnus-summary-followup-with-original
1516     "C" gnus-summary-cancel-article
1517     "r" gnus-summary-reply
1518     "R" gnus-summary-reply-with-original
1519     "\C-c\C-f" gnus-summary-mail-forward
1520     "o" gnus-summary-save-article
1521     "\C-o" gnus-summary-save-article-mail
1522     "|" gnus-summary-pipe-output
1523     "\M-k" gnus-summary-edit-local-kill
1524     "\M-K" gnus-summary-edit-global-kill
1525     ;; "V" gnus-version
1526     "\C-c\C-d" gnus-summary-describe-group
1527     "q" gnus-summary-exit
1528     "Q" gnus-summary-exit-no-update
1529     "\C-c\C-i" gnus-info-find-node
1530     gnus-mouse-2 gnus-mouse-pick-article
1531     "m" gnus-summary-mail-other-window
1532     "a" gnus-summary-post-news
1533     "x" gnus-summary-limit-to-unread
1534     "s" gnus-summary-isearch-article
1535     "t" gnus-summary-toggle-header
1536     "g" gnus-summary-show-article
1537     "l" gnus-summary-goto-last-article
1538     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1539     "\C-d" gnus-summary-enter-digest-group
1540     "\M-\C-d" gnus-summary-read-document
1541     "\M-\C-e" gnus-summary-edit-parameters
1542     "\M-\C-a" gnus-summary-customize-parameters
1543     "\C-c\C-b" gnus-bug
1544     "*" gnus-cache-enter-article
1545     "\M-*" gnus-cache-remove-article
1546     "\M-&" gnus-summary-universal-argument
1547     "\C-l" gnus-recenter
1548     "I" gnus-summary-increase-score
1549     "L" gnus-summary-lower-score
1550     "\M-i" gnus-symbolic-argument
1551     "h" gnus-summary-select-article-buffer
1552
1553     "b" gnus-article-view-part
1554     "\M-t" gnus-summary-toggle-display-buttonized
1555
1556     "V" gnus-summary-score-map
1557     "X" gnus-uu-extract-map
1558     "S" gnus-summary-send-map)
1559
1560   ;; Sort of orthogonal keymap
1561   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1562     "t" gnus-summary-tick-article-forward
1563     "!" gnus-summary-tick-article-forward
1564     "d" gnus-summary-mark-as-read-forward
1565     "r" gnus-summary-mark-as-read-forward
1566     "c" gnus-summary-clear-mark-forward
1567     " " gnus-summary-clear-mark-forward
1568     "e" gnus-summary-mark-as-expirable
1569     "x" gnus-summary-mark-as-expirable
1570     "?" gnus-summary-mark-as-dormant
1571     "b" gnus-summary-set-bookmark
1572     "B" gnus-summary-remove-bookmark
1573     "#" gnus-summary-mark-as-processable
1574     "\M-#" gnus-summary-unmark-as-processable
1575     "S" gnus-summary-limit-include-expunged
1576     "C" gnus-summary-catchup
1577     "H" gnus-summary-catchup-to-here
1578     "h" gnus-summary-catchup-from-here
1579     "\C-c" gnus-summary-catchup-all
1580     "k" gnus-summary-kill-same-subject-and-select
1581     "K" gnus-summary-kill-same-subject
1582     "P" gnus-uu-mark-map)
1583
1584   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1585     "c" gnus-summary-clear-above
1586     "u" gnus-summary-tick-above
1587     "m" gnus-summary-mark-above
1588     "k" gnus-summary-kill-below)
1589
1590   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1591     "/" gnus-summary-limit-to-subject
1592     "n" gnus-summary-limit-to-articles
1593     "w" gnus-summary-pop-limit
1594     "s" gnus-summary-limit-to-subject
1595     "a" gnus-summary-limit-to-author
1596     "u" gnus-summary-limit-to-unread
1597     "m" gnus-summary-limit-to-marks
1598     "M" gnus-summary-limit-exclude-marks
1599     "v" gnus-summary-limit-to-score
1600     "*" gnus-summary-limit-include-cached
1601     "D" gnus-summary-limit-include-dormant
1602     "T" gnus-summary-limit-include-thread
1603     "d" gnus-summary-limit-exclude-dormant
1604     "t" gnus-summary-limit-to-age
1605     "x" gnus-summary-limit-to-extra
1606     "p" gnus-summary-limit-to-display-predicate
1607     "E" gnus-summary-limit-include-expunged
1608     "c" gnus-summary-limit-exclude-childless-dormant
1609     "C" gnus-summary-limit-mark-excluded-as-read
1610     "o" gnus-summary-insert-old-articles
1611     "N" gnus-summary-insert-new-articles)
1612
1613   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1614     "n" gnus-summary-next-unread-article
1615     "p" gnus-summary-prev-unread-article
1616     "N" gnus-summary-next-article
1617     "P" gnus-summary-prev-article
1618     "\C-n" gnus-summary-next-same-subject
1619     "\C-p" gnus-summary-prev-same-subject
1620     "\M-n" gnus-summary-next-unread-subject
1621     "\M-p" gnus-summary-prev-unread-subject
1622     "f" gnus-summary-first-unread-article
1623     "b" gnus-summary-best-unread-article
1624     "j" gnus-summary-goto-article
1625     "g" gnus-summary-goto-subject
1626     "l" gnus-summary-goto-last-article
1627     "o" gnus-summary-pop-article)
1628
1629   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1630     "k" gnus-summary-kill-thread
1631     "l" gnus-summary-lower-thread
1632     "i" gnus-summary-raise-thread
1633     "T" gnus-summary-toggle-threads
1634     "t" gnus-summary-rethread-current
1635     "^" gnus-summary-reparent-thread
1636     "s" gnus-summary-show-thread
1637     "S" gnus-summary-show-all-threads
1638     "h" gnus-summary-hide-thread
1639     "H" gnus-summary-hide-all-threads
1640     "n" gnus-summary-next-thread
1641     "p" gnus-summary-prev-thread
1642     "u" gnus-summary-up-thread
1643     "o" gnus-summary-top-thread
1644     "d" gnus-summary-down-thread
1645     "#" gnus-uu-mark-thread
1646     "\M-#" gnus-uu-unmark-thread)
1647
1648   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1649     "g" gnus-summary-prepare
1650     "c" gnus-summary-insert-cached-articles)
1651
1652   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1653     "c" gnus-summary-catchup-and-exit
1654     "C" gnus-summary-catchup-all-and-exit
1655     "E" gnus-summary-exit-no-update
1656     "Q" gnus-summary-exit
1657     "Z" gnus-summary-exit
1658     "n" gnus-summary-catchup-and-goto-next-group
1659     "R" gnus-summary-reselect-current-group
1660     "G" gnus-summary-rescan-group
1661     "N" gnus-summary-next-group
1662     "s" gnus-summary-save-newsrc
1663     "P" gnus-summary-prev-group)
1664
1665   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1666     " " gnus-summary-next-page
1667     "n" gnus-summary-next-page
1668     "\177" gnus-summary-prev-page
1669     [delete] gnus-summary-prev-page
1670     "p" gnus-summary-prev-page
1671     "\r" gnus-summary-scroll-up
1672     "\M-\r" gnus-summary-scroll-down
1673     "<" gnus-summary-beginning-of-article
1674     ">" gnus-summary-end-of-article
1675     "b" gnus-summary-beginning-of-article
1676     "e" gnus-summary-end-of-article
1677     "^" gnus-summary-refer-parent-article
1678     "r" gnus-summary-refer-parent-article
1679     "D" gnus-summary-enter-digest-group
1680     "R" gnus-summary-refer-references
1681     "T" gnus-summary-refer-thread
1682     "g" gnus-summary-show-article
1683     "s" gnus-summary-isearch-article
1684     "P" gnus-summary-print-article
1685     "M" gnus-mailing-list-insinuate
1686     "t" gnus-article-babel)
1687
1688   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1689     "b" gnus-article-add-buttons
1690     "B" gnus-article-add-buttons-to-head
1691     "o" gnus-article-treat-overstrike
1692     "e" gnus-article-emphasize
1693     "w" gnus-article-fill-cited-article
1694     "Q" gnus-article-fill-long-lines
1695     "C" gnus-article-capitalize-sentences
1696     "c" gnus-article-remove-cr
1697     "q" gnus-article-de-quoted-unreadable
1698     "6" gnus-article-de-base64-unreadable
1699     "Z" gnus-article-decode-HZ
1700     "h" gnus-article-wash-html
1701     "s" gnus-summary-force-verify-and-decrypt
1702     "f" gnus-article-display-x-face
1703     "l" gnus-summary-stop-page-breaking
1704     "r" gnus-summary-caesar-message
1705     "t" gnus-summary-toggle-header
1706     "g" gnus-summary-toggle-smiley
1707     "v" gnus-summary-verbose-headers
1708     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1709     "p" gnus-article-verify-x-pgp-sig
1710     "d" gnus-article-treat-dumbquotes)
1711
1712   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1713     "a" gnus-article-hide
1714     "h" gnus-article-hide-headers
1715     "b" gnus-article-hide-boring-headers
1716     "s" gnus-article-hide-signature
1717     "c" gnus-article-hide-citation
1718     "C" gnus-article-hide-citation-in-followups
1719     "l" gnus-article-hide-list-identifiers
1720     "p" gnus-article-hide-pgp
1721     "B" gnus-article-strip-banner
1722     "P" gnus-article-hide-pem
1723     "\C-c" gnus-article-hide-citation-maybe)
1724
1725   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1726     "a" gnus-article-highlight
1727     "h" gnus-article-highlight-headers
1728     "c" gnus-article-highlight-citation
1729     "s" gnus-article-highlight-signature)
1730
1731   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1732     "w" gnus-article-decode-mime-words
1733     "c" gnus-article-decode-charset
1734     "v" gnus-mime-view-all-parts
1735     "b" gnus-article-view-part)
1736
1737   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1738     "z" gnus-article-date-ut
1739     "u" gnus-article-date-ut
1740     "l" gnus-article-date-local
1741     "p" gnus-article-date-english
1742     "e" gnus-article-date-lapsed
1743     "o" gnus-article-date-original
1744     "i" gnus-article-date-iso8601
1745     "s" gnus-article-date-user)
1746
1747   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1748     "t" gnus-article-remove-trailing-blank-lines
1749     "l" gnus-article-strip-leading-blank-lines
1750     "m" gnus-article-strip-multiple-blank-lines
1751     "a" gnus-article-strip-blank-lines
1752     "A" gnus-article-strip-all-blank-lines
1753     "s" gnus-article-strip-leading-space
1754     "e" gnus-article-strip-trailing-space
1755     "w" gnus-article-remove-leading-whitespace)
1756
1757   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1758     "v" gnus-version
1759     "f" gnus-summary-fetch-faq
1760     "d" gnus-summary-describe-group
1761     "h" gnus-summary-describe-briefly
1762     "i" gnus-info-find-node)
1763
1764   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1765     "e" gnus-summary-expire-articles
1766     "\M-\C-e" gnus-summary-expire-articles-now
1767     "\177" gnus-summary-delete-article
1768     [delete] gnus-summary-delete-article
1769     [backspace] gnus-summary-delete-article
1770     "m" gnus-summary-move-article
1771     "r" gnus-summary-respool-article
1772     "w" gnus-summary-edit-article
1773     "c" gnus-summary-copy-article
1774     "B" gnus-summary-crosspost-article
1775     "q" gnus-summary-respool-query
1776     "t" gnus-summary-respool-trace
1777     "i" gnus-summary-import-article
1778     "I" gnus-summary-create-article
1779     "p" gnus-summary-article-posted-p)
1780
1781   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1782     "o" gnus-summary-save-article
1783     "m" gnus-summary-save-article-mail
1784     "F" gnus-summary-write-article-file
1785     "r" gnus-summary-save-article-rmail
1786     "f" gnus-summary-save-article-file
1787     "b" gnus-summary-save-article-body-file
1788     "h" gnus-summary-save-article-folder
1789     "v" gnus-summary-save-article-vm
1790     "p" gnus-summary-pipe-output
1791     "s" gnus-soup-add-article)
1792
1793   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1794     "b" gnus-summary-display-buttonized
1795     "m" gnus-summary-repair-multipart
1796     "v" gnus-article-view-part
1797     "o" gnus-article-save-part
1798     "c" gnus-article-copy-part
1799     "C" gnus-article-view-part-as-charset
1800     "e" gnus-article-externalize-part
1801     "E" gnus-article-encrypt-body
1802     "i" gnus-article-inline-part
1803     "|" gnus-article-pipe-part))
1804
1805 (defvar gnus-article-post-menu nil)
1806
1807 (defun gnus-summary-make-menu-bar ()
1808   (gnus-turn-off-edit-menu 'summary)
1809
1810   (unless (boundp 'gnus-summary-misc-menu)
1811
1812     (easy-menu-define
1813       gnus-summary-kill-menu gnus-summary-mode-map ""
1814       (cons
1815        "Score"
1816        (nconc
1817         (list
1818          ["Customize" gnus-score-customize t])
1819         (gnus-make-score-map 'increase)
1820         (gnus-make-score-map 'lower)
1821         '(("Mark"
1822            ["Kill below" gnus-summary-kill-below t]
1823            ["Mark above" gnus-summary-mark-above t]
1824            ["Tick above" gnus-summary-tick-above t]
1825            ["Clear above" gnus-summary-clear-above t])
1826           ["Current score" gnus-summary-current-score t]
1827           ["Set score" gnus-summary-set-score t]
1828           ["Switch current score file..." gnus-score-change-score-file t]
1829           ["Set mark below..." gnus-score-set-mark-below t]
1830           ["Set expunge below..." gnus-score-set-expunge-below t]
1831           ["Edit current score file" gnus-score-edit-current-scores t]
1832           ["Edit score file" gnus-score-edit-file t]
1833           ["Trace score" gnus-score-find-trace t]
1834           ["Find words" gnus-score-find-favourite-words t]
1835           ["Rescore buffer" gnus-summary-rescore t]
1836           ["Increase score..." gnus-summary-increase-score t]
1837           ["Lower score..." gnus-summary-lower-score t]))))
1838
1839 ;; Define both the Article menu in the summary buffer and the equivalent
1840     ;; Commands menu in the article buffer here for consistency.
1841     (let ((innards
1842            `(("Hide"
1843               ["All" gnus-article-hide t]
1844               ["Headers" gnus-article-hide-headers t]
1845               ["Signature" gnus-article-hide-signature t]
1846               ["Citation" gnus-article-hide-citation t]
1847               ["List identifiers" gnus-article-hide-list-identifiers t]
1848               ["PGP" gnus-article-hide-pgp t]
1849               ["Banner" gnus-article-strip-banner t]
1850               ["Boring headers" gnus-article-hide-boring-headers t])
1851              ("Highlight"
1852               ["All" gnus-article-highlight t]
1853               ["Headers" gnus-article-highlight-headers t]
1854               ["Signature" gnus-article-highlight-signature t]
1855               ["Citation" gnus-article-highlight-citation t])
1856              ("MIME"
1857               ["Words" gnus-article-decode-mime-words t]
1858               ["Charset" gnus-article-decode-charset t]
1859               ["QP" gnus-article-de-quoted-unreadable t]
1860               ["Base64" gnus-article-de-base64-unreadable t]
1861               ["View all" gnus-mime-view-all-parts t]
1862               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1863               ["Encrypt body" gnus-article-encrypt-body t])
1864              ("Date"
1865               ["Local" gnus-article-date-local t]
1866               ["ISO8601" gnus-article-date-iso8601 t]
1867               ["UT" gnus-article-date-ut t]
1868               ["Original" gnus-article-date-original t]
1869               ["Lapsed" gnus-article-date-lapsed t]
1870               ["User-defined" gnus-article-date-user t])
1871              ("Washing"
1872               ("Remove Blanks"
1873                ["Leading" gnus-article-strip-leading-blank-lines t]
1874                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1875                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1876                ["All of the above" gnus-article-strip-blank-lines t]
1877                ["All" gnus-article-strip-all-blank-lines t]
1878                ["Leading space" gnus-article-strip-leading-space t]
1879                ["Trailing space" gnus-article-strip-trailing-space t]
1880                ["Leading space in headers"
1881                 gnus-article-remove-leading-whitespace t])
1882               ["Overstrike" gnus-article-treat-overstrike t]
1883               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1884               ["Emphasis" gnus-article-emphasize t]
1885               ["Word wrap" gnus-article-fill-cited-article t]
1886               ["Fill long lines" gnus-article-fill-long-lines t]
1887               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1888               ["CR" gnus-article-remove-cr t]
1889               ["Show X-Face" gnus-article-display-x-face t]
1890               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1891               ["Base64" gnus-article-de-base64-unreadable t]
1892               ["Rot 13" gnus-summary-caesar-message
1893                ,@(if (featurep 'xemacs) '(t)
1894                    '(:help "\"Caesar rotate\" article by 13"))]
1895               ["Unix pipe" gnus-summary-pipe-message t]
1896               ["Add buttons" gnus-article-add-buttons t]
1897               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1898               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1899               ["Verbose header" gnus-summary-verbose-headers t]
1900               ["Toggle header" gnus-summary-toggle-header t]
1901               ["Toggle smiley" gnus-summary-toggle-smiley t]
1902               ["Html" gnus-article-wash-html t]
1903               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1904               ["HZ" gnus-article-decode-HZ t])
1905              ("Output"
1906               ["Save in default format" gnus-summary-save-article
1907                ,@(if (featurep 'xemacs) '(t)
1908                    '(:help "Save article using default method"))]
1909               ["Save in file" gnus-summary-save-article-file
1910                ,@(if (featurep 'xemacs) '(t)
1911                    '(:help "Save article in file"))]
1912               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1913               ["Save in MH folder" gnus-summary-save-article-folder t]
1914               ["Save in VM folder" gnus-summary-save-article-vm t]
1915               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1916               ["Save body in file" gnus-summary-save-article-body-file t]
1917               ["Pipe through a filter" gnus-summary-pipe-output t]
1918               ["Add to SOUP packet" gnus-soup-add-article t]
1919               ["Print" gnus-summary-print-article t])
1920              ("Backend"
1921               ["Respool article..." gnus-summary-respool-article t]
1922               ["Move article..." gnus-summary-move-article
1923                (gnus-check-backend-function
1924                 'request-move-article gnus-newsgroup-name)]
1925               ["Copy article..." gnus-summary-copy-article t]
1926               ["Crosspost article..." gnus-summary-crosspost-article
1927                (gnus-check-backend-function
1928                 'request-replace-article gnus-newsgroup-name)]
1929               ["Import file..." gnus-summary-import-article t]
1930               ["Create article..." gnus-summary-create-article t]
1931               ["Check if posted" gnus-summary-article-posted-p t]
1932               ["Edit article" gnus-summary-edit-article
1933                (not (gnus-group-read-only-p))]
1934               ["Delete article" gnus-summary-delete-article
1935                (gnus-check-backend-function
1936                 'request-expire-articles gnus-newsgroup-name)]
1937               ["Query respool" gnus-summary-respool-query t]
1938               ["Trace respool" gnus-summary-respool-trace t]
1939               ["Delete expirable articles" gnus-summary-expire-articles-now
1940                (gnus-check-backend-function
1941                 'request-expire-articles gnus-newsgroup-name)])
1942              ("Extract"
1943               ["Uudecode" gnus-uu-decode-uu
1944                ,@(if (featurep 'xemacs) '(t)
1945                    '(:help "Decode uuencoded article(s)"))]
1946               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1947               ["Unshar" gnus-uu-decode-unshar t]
1948               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1949               ["Save" gnus-uu-decode-save t]
1950               ["Binhex" gnus-uu-decode-binhex t]
1951               ["Postscript" gnus-uu-decode-postscript t])
1952              ("Cache"
1953               ["Enter article" gnus-cache-enter-article t]
1954               ["Remove article" gnus-cache-remove-article t])
1955              ["Translate" gnus-article-babel t]
1956              ["Select article buffer" gnus-summary-select-article-buffer t]
1957              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1958              ["Isearch article..." gnus-summary-isearch-article t]
1959              ["Beginning of the article" gnus-summary-beginning-of-article t]
1960              ["End of the article" gnus-summary-end-of-article t]
1961              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1962              ["Fetch referenced articles" gnus-summary-refer-references t]
1963              ["Fetch current thread" gnus-summary-refer-thread t]
1964              ["Fetch article with id..." gnus-summary-refer-article t]
1965              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1966              ["Redisplay" gnus-summary-show-article t]
1967              ["Raw article" gnus-summary-show-raw-article t])))
1968       (easy-menu-define
1969         gnus-summary-article-menu gnus-summary-mode-map ""
1970         (cons "Article" innards))
1971
1972       (if (not (keymapp gnus-summary-article-menu))
1973           (easy-menu-define
1974             gnus-article-commands-menu gnus-article-mode-map ""
1975             (cons "Commands" innards))
1976         ;; in Emacs, don't share menu.
1977         (setq gnus-article-commands-menu
1978               (copy-keymap gnus-summary-article-menu))
1979         (define-key gnus-article-mode-map [menu-bar commands]
1980           (cons "Commands" gnus-article-commands-menu))))
1981
1982     (easy-menu-define
1983       gnus-summary-thread-menu gnus-summary-mode-map ""
1984       '("Threads"
1985         ["Toggle threading" gnus-summary-toggle-threads t]
1986         ["Hide threads" gnus-summary-hide-all-threads t]
1987         ["Show threads" gnus-summary-show-all-threads t]
1988         ["Hide thread" gnus-summary-hide-thread t]
1989         ["Show thread" gnus-summary-show-thread t]
1990         ["Go to next thread" gnus-summary-next-thread t]
1991         ["Go to previous thread" gnus-summary-prev-thread t]
1992         ["Go down thread" gnus-summary-down-thread t]
1993         ["Go up thread" gnus-summary-up-thread t]
1994         ["Top of thread" gnus-summary-top-thread t]
1995         ["Mark thread as read" gnus-summary-kill-thread t]
1996         ["Lower thread score" gnus-summary-lower-thread t]
1997         ["Raise thread score" gnus-summary-raise-thread t]
1998         ["Rethread current" gnus-summary-rethread-current t]))
1999
2000     (easy-menu-define
2001       gnus-summary-post-menu gnus-summary-mode-map ""
2002       `("Post"
2003         ["Post an article" gnus-summary-post-news
2004          ,@(if (featurep 'xemacs) '(t)
2005              '(:help "Post an article"))]
2006         ["Followup" gnus-summary-followup
2007          ,@(if (featurep 'xemacs) '(t)
2008              '(:help "Post followup to this article"))]
2009         ["Followup and yank" gnus-summary-followup-with-original
2010          ,@(if (featurep 'xemacs) '(t)
2011              '(:help "Post followup to this article, quoting its contents"))]
2012         ["Supersede article" gnus-summary-supersede-article t]
2013         ["Cancel article" gnus-summary-cancel-article
2014          ,@(if (featurep 'xemacs) '(t)
2015              '(:help "Cancel an article you posted"))]
2016         ["Reply" gnus-summary-reply t]
2017         ["Reply and yank" gnus-summary-reply-with-original t]
2018         ["Wide reply" gnus-summary-wide-reply t]
2019         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2020          ,@(if (featurep 'xemacs) '(t)
2021              '(:help "Mail a reply, quoting this article"))]
2022         ["Mail forward" gnus-summary-mail-forward t]
2023         ["Post forward" gnus-summary-post-forward t]
2024         ["Digest and mail" gnus-uu-digest-mail-forward t]
2025         ["Digest and post" gnus-uu-digest-post-forward t]
2026         ["Resend message" gnus-summary-resend-message t]
2027         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2028         ["Send a mail" gnus-summary-mail-other-window t]
2029         ["Uuencode and post" gnus-uu-post-news
2030          ,@(if (featurep 'xemacs) '(t)
2031              '(:help "Post a uuencoded article"))]
2032         ["Followup via news" gnus-summary-followup-to-mail t]
2033         ["Followup via news and yank"
2034          gnus-summary-followup-to-mail-with-original t]
2035         ;;("Draft"
2036         ;;["Send" gnus-summary-send-draft t]
2037         ;;["Send bounced" gnus-resend-bounced-mail t])
2038         ))
2039
2040     (cond
2041      ((not (keymapp gnus-summary-post-menu))
2042       (setq gnus-article-post-menu gnus-summary-post-menu))
2043      ((not gnus-article-post-menu)
2044       ;; Don't share post menu.
2045       (setq gnus-article-post-menu
2046             (copy-keymap gnus-summary-post-menu))))
2047     (define-key gnus-article-mode-map [menu-bar post]
2048       (cons "Post" gnus-article-post-menu))
2049
2050     (easy-menu-define
2051       gnus-summary-misc-menu gnus-summary-mode-map ""
2052       `("Gnus"
2053         ("Mark Read"
2054          ["Mark as read" gnus-summary-mark-as-read-forward t]
2055          ["Mark same subject and select"
2056           gnus-summary-kill-same-subject-and-select t]
2057          ["Mark same subject" gnus-summary-kill-same-subject t]
2058          ["Catchup" gnus-summary-catchup
2059           ,@(if (featurep 'xemacs) '(t)
2060               '(:help "Mark unread articles in this group as read"))]
2061          ["Catchup all" gnus-summary-catchup-all t]
2062          ["Catchup to here" gnus-summary-catchup-to-here t]
2063          ["Catchup from here" gnus-summary-catchup-from-here t]
2064          ["Catchup region" gnus-summary-mark-region-as-read t]
2065          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2066         ("Mark Various"
2067          ["Tick" gnus-summary-tick-article-forward t]
2068          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2069          ["Remove marks" gnus-summary-clear-mark-forward t]
2070          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2071          ["Set bookmark" gnus-summary-set-bookmark t]
2072          ["Remove bookmark" gnus-summary-remove-bookmark t])
2073         ("Mark Limit"
2074          ["Marks..." gnus-summary-limit-to-marks t]
2075          ["Subject..." gnus-summary-limit-to-subject t]
2076          ["Author..." gnus-summary-limit-to-author t]
2077          ["Age..." gnus-summary-limit-to-age t]
2078          ["Extra..." gnus-summary-limit-to-extra t]
2079          ["Score" gnus-summary-limit-to-score t]
2080          ["Display Predicate" gnus-summary-limit-to-display-predicate t]
2081          ["Unread" gnus-summary-limit-to-unread t]
2082          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2083          ["Articles" gnus-summary-limit-to-articles t]
2084          ["Pop limit" gnus-summary-pop-limit t]
2085          ["Show dormant" gnus-summary-limit-include-dormant t]
2086          ["Hide childless dormant"
2087           gnus-summary-limit-exclude-childless-dormant t]
2088          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2089          ["Hide marked" gnus-summary-limit-exclude-marks t]
2090          ["Show expunged" gnus-summary-limit-include-expunged t])
2091         ("Process Mark"
2092          ["Set mark" gnus-summary-mark-as-processable t]
2093          ["Remove mark" gnus-summary-unmark-as-processable t]
2094          ["Remove all marks" gnus-summary-unmark-all-processable t]
2095          ["Mark above" gnus-uu-mark-over t]
2096          ["Mark series" gnus-uu-mark-series t]
2097          ["Mark region" gnus-uu-mark-region t]
2098          ["Unmark region" gnus-uu-unmark-region t]
2099          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2100          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2101          ["Mark all" gnus-uu-mark-all t]
2102          ["Mark buffer" gnus-uu-mark-buffer t]
2103          ["Mark sparse" gnus-uu-mark-sparse t]
2104          ["Mark thread" gnus-uu-mark-thread t]
2105          ["Unmark thread" gnus-uu-unmark-thread t]
2106          ("Process Mark Sets"
2107           ["Kill" gnus-summary-kill-process-mark t]
2108           ["Yank" gnus-summary-yank-process-mark
2109            gnus-newsgroup-process-stack]
2110           ["Save" gnus-summary-save-process-mark t]))
2111         ("Scroll article"
2112          ["Page forward" gnus-summary-next-page
2113           ,@(if (featurep 'xemacs) '(t)
2114               '(:help "Show next page of article"))]
2115          ["Page backward" gnus-summary-prev-page
2116           ,@(if (featurep 'xemacs) '(t)
2117               '(:help "Show previous page of article"))]
2118          ["Line forward" gnus-summary-scroll-up t])
2119         ("Move"
2120          ["Next unread article" gnus-summary-next-unread-article t]
2121          ["Previous unread article" gnus-summary-prev-unread-article t]
2122          ["Next article" gnus-summary-next-article t]
2123          ["Previous article" gnus-summary-prev-article t]
2124          ["Next unread subject" gnus-summary-next-unread-subject t]
2125          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2126          ["Next article same subject" gnus-summary-next-same-subject t]
2127          ["Previous article same subject" gnus-summary-prev-same-subject t]
2128          ["First unread article" gnus-summary-first-unread-article t]
2129          ["Best unread article" gnus-summary-best-unread-article t]
2130          ["Go to subject number..." gnus-summary-goto-subject t]
2131          ["Go to article number..." gnus-summary-goto-article t]
2132          ["Go to the last article" gnus-summary-goto-last-article t]
2133          ["Pop article off history" gnus-summary-pop-article t])
2134         ("Sort"
2135          ["Sort by number" gnus-summary-sort-by-number t]
2136          ["Sort by author" gnus-summary-sort-by-author t]
2137          ["Sort by subject" gnus-summary-sort-by-subject t]
2138          ["Sort by date" gnus-summary-sort-by-date t]
2139          ["Sort by score" gnus-summary-sort-by-score t]
2140          ["Sort by lines" gnus-summary-sort-by-lines t]
2141          ["Sort by characters" gnus-summary-sort-by-chars t]
2142          ["Original sort" gnus-summary-sort-by-original t])
2143         ("Help"
2144          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2145          ["Describe group" gnus-summary-describe-group t]
2146          ["Read manual" gnus-info-find-node t])
2147         ("Modes"
2148          ["Pick and read" gnus-pick-mode t]
2149          ["Binary" gnus-binary-mode t])
2150         ("Regeneration"
2151          ["Regenerate" gnus-summary-prepare t]
2152          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2153          ["Toggle threading" gnus-summary-toggle-threads t])
2154         ["See old articles" gnus-summary-insert-old-articles t]
2155         ["See new articles" gnus-summary-insert-new-articles t]
2156         ["Filter articles..." gnus-summary-execute-command t]
2157         ["Run command on subjects..." gnus-summary-universal-argument t]
2158         ["Search articles forward..." gnus-summary-search-article-forward t]
2159         ["Search articles backward..." gnus-summary-search-article-backward t]
2160         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2161         ["Expand window" gnus-summary-expand-window t]
2162         ["Expire expirable articles" gnus-summary-expire-articles
2163          (gnus-check-backend-function
2164           'request-expire-articles gnus-newsgroup-name)]
2165         ["Edit local kill file" gnus-summary-edit-local-kill t]
2166         ["Edit main kill file" gnus-summary-edit-global-kill t]
2167         ["Edit group parameters" gnus-summary-edit-parameters t]
2168         ["Customize group parameters" gnus-summary-customize-parameters t]
2169         ["Send a bug report" gnus-bug t]
2170         ("Exit"
2171          ["Catchup and exit" gnus-summary-catchup-and-exit
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Mark unread articles in this group as read, then exit"))]
2174          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2175          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2176          ["Exit group" gnus-summary-exit
2177           ,@(if (featurep 'xemacs) '(t)
2178               '(:help "Exit current group, return to group selection mode"))]
2179          ["Exit group without updating" gnus-summary-exit-no-update t]
2180          ["Exit and goto next group" gnus-summary-next-group t]
2181          ["Exit and goto prev group" gnus-summary-prev-group t]
2182          ["Reselect group" gnus-summary-reselect-current-group t]
2183          ["Rescan group" gnus-summary-rescan-group t]
2184          ["Update dribble" gnus-summary-save-newsrc t])))
2185
2186     (gnus-run-hooks 'gnus-summary-menu-hook)))
2187
2188 (defvar gnus-summary-tool-bar-map nil)
2189
2190 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2191 (defun gnus-summary-make-tool-bar ()
2192   (if (and (fboundp 'tool-bar-add-item-from-menu)
2193            (default-value 'tool-bar-mode)
2194            (not gnus-summary-tool-bar-map))
2195       (setq gnus-summary-tool-bar-map
2196             (let ((tool-bar-map (make-sparse-keymap))
2197                   (load-path (mm-image-load-path)))
2198               (tool-bar-add-item-from-menu
2199                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2200               (tool-bar-add-item-from-menu
2201                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2202               (tool-bar-add-item-from-menu
2203                'gnus-summary-post-news "post" gnus-summary-mode-map)
2204               (tool-bar-add-item-from-menu
2205                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2206               (tool-bar-add-item-from-menu
2207                'gnus-summary-followup "followup" gnus-summary-mode-map)
2208               (tool-bar-add-item-from-menu
2209                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2210               (tool-bar-add-item-from-menu
2211                'gnus-summary-reply "reply" gnus-summary-mode-map)
2212               (tool-bar-add-item-from-menu
2213                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2214               (tool-bar-add-item-from-menu
2215                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2216               (tool-bar-add-item-from-menu
2217                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2218               (tool-bar-add-item-from-menu
2219                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2220               (tool-bar-add-item-from-menu
2221                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2222               (tool-bar-add-item-from-menu
2223                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2224               (tool-bar-add-item-from-menu
2225                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2226               (tool-bar-add-item-from-menu
2227                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2228               tool-bar-map)))
2229   (if gnus-summary-tool-bar-map
2230       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2231
2232 (defun gnus-score-set-default (var value)
2233   "A version of set that updates the GNU Emacs menu-bar."
2234   (set var value)
2235   ;; It is the message that forces the active status to be updated.
2236   (message ""))
2237
2238 (defun gnus-make-score-map (type)
2239   "Make a summary score map of type TYPE."
2240   (if t
2241       nil
2242     (let ((headers '(("author" "from" string)
2243                      ("subject" "subject" string)
2244                      ("article body" "body" string)
2245                      ("article head" "head" string)
2246                      ("xref" "xref" string)
2247                      ("extra header" "extra" string)
2248                      ("lines" "lines" number)
2249                      ("followups to author" "followup" string)))
2250           (types '((number ("less than" <)
2251                            ("greater than" >)
2252                            ("equal" =))
2253                    (string ("substring" s)
2254                            ("exact string" e)
2255                            ("fuzzy string" f)
2256                            ("regexp" r))))
2257           (perms '(("temporary" (current-time-string))
2258                    ("permanent" nil)
2259                    ("immediate" now)))
2260           header)
2261       (list
2262        (apply
2263         'nconc
2264         (list
2265          (if (eq type 'lower)
2266              "Lower score"
2267            "Increase score"))
2268         (let (outh)
2269           (while headers
2270             (setq header (car headers))
2271             (setq outh
2272                   (cons
2273                    (apply
2274                     'nconc
2275                     (list (car header))
2276                     (let ((ts (cdr (assoc (nth 2 header) types)))
2277                           outt)
2278                       (while ts
2279                         (setq outt
2280                               (cons
2281                                (apply
2282                                 'nconc
2283                                 (list (caar ts))
2284                                 (let ((ps perms)
2285                                       outp)
2286                                   (while ps
2287                                     (setq outp
2288                                           (cons
2289                                            (vector
2290                                             (caar ps)
2291                                             (list
2292                                              'gnus-summary-score-entry
2293                                              (nth 1 header)
2294                                              (if (or (string= (nth 1 header)
2295                                                               "head")
2296                                                      (string= (nth 1 header)
2297                                                               "body"))
2298                                                  ""
2299                                                (list 'gnus-summary-header
2300                                                      (nth 1 header)))
2301                                              (list 'quote (nth 1 (car ts)))
2302                                              (list 'gnus-score-delta-default
2303                                                    nil)
2304                                              (nth 1 (car ps))
2305                                              t)
2306                                             t)
2307                                            outp))
2308                                     (setq ps (cdr ps)))
2309                                   (list (nreverse outp))))
2310                                outt))
2311                         (setq ts (cdr ts)))
2312                       (list (nreverse outt))))
2313                    outh))
2314             (setq headers (cdr headers)))
2315           (list (nreverse outh))))))))
2316
2317 \f
2318
2319 (defun gnus-summary-mode (&optional group)
2320   "Major mode for reading articles.
2321
2322 All normal editing commands are switched off.
2323 \\<gnus-summary-mode-map>
2324 Each line in this buffer represents one article.  To read an
2325 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2326 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2327 respectively.
2328
2329 You can also post articles and send mail from this buffer.  To
2330 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2331 of an article, type `\\[gnus-summary-reply]'.
2332
2333 There are approx. one gazillion commands you can execute in this
2334 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2335
2336 The following commands are available:
2337
2338 \\{gnus-summary-mode-map}"
2339   (interactive)
2340   (kill-all-local-variables)
2341   (when (gnus-visual-p 'summary-menu 'menu)
2342     (gnus-summary-make-menu-bar)
2343     (gnus-summary-make-tool-bar))
2344   (gnus-summary-make-local-variables)
2345   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2346     (gnus-summary-make-local-variables))
2347   (gnus-make-thread-indent-array)
2348   (gnus-simplify-mode-line)
2349   (setq major-mode 'gnus-summary-mode)
2350   (setq mode-name "Summary")
2351   (make-local-variable 'minor-mode-alist)
2352   (use-local-map gnus-summary-mode-map)
2353   (buffer-disable-undo)
2354   (setq buffer-read-only t)             ;Disable modification
2355   (setq truncate-lines t)
2356   (setq selective-display t)
2357   (setq selective-display-ellipses t)   ;Display `...'
2358   (gnus-summary-set-display-table)
2359   (gnus-set-default-directory)
2360   (setq gnus-newsgroup-name group)
2361   (make-local-variable 'gnus-summary-line-format)
2362   (make-local-variable 'gnus-summary-line-format-spec)
2363   (make-local-variable 'gnus-summary-dummy-line-format)
2364   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2365   (make-local-variable 'gnus-summary-mark-positions)
2366   (make-local-hook 'pre-command-hook)
2367   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2368   (gnus-run-hooks 'gnus-summary-mode-hook)
2369   (turn-on-gnus-mailing-list-mode)
2370   (mm-enable-multibyte-mule4)
2371   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2372   (gnus-update-summary-mark-positions))
2373
2374 (defun gnus-summary-make-local-variables ()
2375   "Make all the local summary buffer variables."
2376   (let (global)
2377     (dolist (local gnus-summary-local-variables)
2378       (if (consp local)
2379           (progn
2380             (if (eq (cdr local) 'global)
2381                 ;; Copy the global value of the variable.
2382                 (setq global (symbol-value (car local)))
2383               ;; Use the value from the list.
2384               (setq global (eval (cdr local))))
2385             (set (make-local-variable (car local)) global))
2386         ;; Simple nil-valued local variable.
2387         (set (make-local-variable local) nil)))))
2388
2389 (defun gnus-summary-clear-local-variables ()
2390   (let ((locals gnus-summary-local-variables))
2391     (while locals
2392       (if (consp (car locals))
2393           (and (vectorp (caar locals))
2394                (set (caar locals) nil))
2395         (and (vectorp (car locals))
2396              (set (car locals) nil)))
2397       (setq locals (cdr locals)))))
2398
2399 ;; Summary data functions.
2400
2401 (defmacro gnus-data-number (data)
2402   `(car ,data))
2403
2404 (defmacro gnus-data-set-number (data number)
2405   `(setcar ,data ,number))
2406
2407 (defmacro gnus-data-mark (data)
2408   `(nth 1 ,data))
2409
2410 (defmacro gnus-data-set-mark (data mark)
2411   `(setcar (nthcdr 1 ,data) ,mark))
2412
2413 (defmacro gnus-data-pos (data)
2414   `(nth 2 ,data))
2415
2416 (defmacro gnus-data-set-pos (data pos)
2417   `(setcar (nthcdr 2 ,data) ,pos))
2418
2419 (defmacro gnus-data-header (data)
2420   `(nth 3 ,data))
2421
2422 (defmacro gnus-data-set-header (data header)
2423   `(setf (nth 3 ,data) ,header))
2424
2425 (defmacro gnus-data-level (data)
2426   `(nth 4 ,data))
2427
2428 (defmacro gnus-data-unread-p (data)
2429   `(= (nth 1 ,data) gnus-unread-mark))
2430
2431 (defmacro gnus-data-read-p (data)
2432   `(/= (nth 1 ,data) gnus-unread-mark))
2433
2434 (defmacro gnus-data-pseudo-p (data)
2435   `(consp (nth 3 ,data)))
2436
2437 (defmacro gnus-data-find (number)
2438   `(assq ,number gnus-newsgroup-data))
2439
2440 (defmacro gnus-data-find-list (number &optional data)
2441   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2442      (memq (assq ,number bdata)
2443            bdata)))
2444
2445 (defmacro gnus-data-make (number mark pos header level)
2446   `(list ,number ,mark ,pos ,header ,level))
2447
2448 (defun gnus-data-enter (after-article number mark pos header level offset)
2449   (let ((data (gnus-data-find-list after-article)))
2450     (unless data
2451       (error "No such article: %d" after-article))
2452     (setcdr data (cons (gnus-data-make number mark pos header level)
2453                        (cdr data)))
2454     (setq gnus-newsgroup-data-reverse nil)
2455     (gnus-data-update-list (cddr data) offset)))
2456
2457 (defun gnus-data-enter-list (after-article list &optional offset)
2458   (when list
2459     (let ((data (and after-article (gnus-data-find-list after-article)))
2460           (ilist list))
2461       (if (not (or data
2462                    after-article))
2463           (let ((odata gnus-newsgroup-data))
2464             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2465             (when offset
2466               (gnus-data-update-list odata offset)))
2467       ;; Find the last element in the list to be spliced into the main
2468         ;; list.
2469         (while (cdr list)
2470           (setq list (cdr list)))
2471         (if (not data)
2472             (progn
2473               (setcdr list gnus-newsgroup-data)
2474               (setq gnus-newsgroup-data ilist)
2475               (when offset
2476                 (gnus-data-update-list (cdr list) offset)))
2477           (setcdr list (cdr data))
2478           (setcdr data ilist)
2479           (when offset
2480             (gnus-data-update-list (cdr list) offset))))
2481       (setq gnus-newsgroup-data-reverse nil))))
2482
2483 (defun gnus-data-remove (article &optional offset)
2484   (let ((data gnus-newsgroup-data))
2485     (if (= (gnus-data-number (car data)) article)
2486         (progn
2487           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2488                 gnus-newsgroup-data-reverse nil)
2489           (when offset
2490             (gnus-data-update-list gnus-newsgroup-data offset)))
2491       (while (cdr data)
2492         (when (= (gnus-data-number (cadr data)) article)
2493           (setcdr data (cddr data))
2494           (when offset
2495             (gnus-data-update-list (cdr data) offset))
2496           (setq data nil
2497                 gnus-newsgroup-data-reverse nil))
2498         (setq data (cdr data))))))
2499
2500 (defmacro gnus-data-list (backward)
2501   `(if ,backward
2502        (or gnus-newsgroup-data-reverse
2503            (setq gnus-newsgroup-data-reverse
2504                  (reverse gnus-newsgroup-data)))
2505      gnus-newsgroup-data))
2506
2507 (defun gnus-data-update-list (data offset)
2508   "Add OFFSET to the POS of all data entries in DATA."
2509   (setq gnus-newsgroup-data-reverse nil)
2510   (while data
2511     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2512     (setq data (cdr data))))
2513
2514 (defun gnus-summary-article-pseudo-p (article)
2515   "Say whether this article is a pseudo article or not."
2516   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2517
2518 (defmacro gnus-summary-article-sparse-p (article)
2519   "Say whether this article is a sparse article or not."
2520   `(memq ,article gnus-newsgroup-sparse))
2521
2522 (defmacro gnus-summary-article-ancient-p (article)
2523   "Say whether this article is a sparse article or not."
2524   `(memq ,article gnus-newsgroup-ancient))
2525
2526 (defun gnus-article-parent-p (number)
2527   "Say whether this article is a parent or not."
2528   (let ((data (gnus-data-find-list number)))
2529     (and (cdr data)              ; There has to be an article after...
2530          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2531             (gnus-data-level (nth 1 data))))))
2532
2533 (defun gnus-article-children (number)
2534   "Return a list of all children to NUMBER."
2535   (let* ((data (gnus-data-find-list number))
2536          (level (gnus-data-level (car data)))
2537          children)
2538     (setq data (cdr data))
2539     (while (and data
2540                 (= (gnus-data-level (car data)) (1+ level)))
2541       (push (gnus-data-number (car data)) children)
2542       (setq data (cdr data)))
2543     children))
2544
2545 (defmacro gnus-summary-skip-intangible ()
2546   "If the current article is intangible, then jump to a different article."
2547   '(let ((to (get-text-property (point) 'gnus-intangible)))
2548      (and to (gnus-summary-goto-subject to))))
2549
2550 (defmacro gnus-summary-article-intangible-p ()
2551   "Say whether this article is intangible or not."
2552   '(get-text-property (point) 'gnus-intangible))
2553
2554 (defun gnus-article-read-p (article)
2555   "Say whether ARTICLE is read or not."
2556   (not (or (memq article gnus-newsgroup-marked)
2557            (memq article gnus-newsgroup-unreads)
2558            (memq article gnus-newsgroup-unselected)
2559            (memq article gnus-newsgroup-dormant))))
2560
2561 ;; Some summary mode macros.
2562
2563 (defmacro gnus-summary-article-number ()
2564   "The article number of the article on the current line.
2565 If there isn's an article number here, then we return the current
2566 article number."
2567   '(progn
2568      (gnus-summary-skip-intangible)
2569      (or (get-text-property (point) 'gnus-number)
2570          (gnus-summary-last-subject))))
2571
2572 (defmacro gnus-summary-article-header (&optional number)
2573   "Return the header of article NUMBER."
2574   `(gnus-data-header (gnus-data-find
2575                       ,(or number '(gnus-summary-article-number)))))
2576
2577 (defmacro gnus-summary-thread-level (&optional number)
2578   "Return the level of thread that starts with article NUMBER."
2579   `(if (and (eq gnus-summary-make-false-root 'dummy)
2580             (get-text-property (point) 'gnus-intangible))
2581        0
2582      (gnus-data-level (gnus-data-find
2583                        ,(or number '(gnus-summary-article-number))))))
2584
2585 (defmacro gnus-summary-article-mark (&optional number)
2586   "Return the mark of article NUMBER."
2587   `(gnus-data-mark (gnus-data-find
2588                     ,(or number '(gnus-summary-article-number)))))
2589
2590 (defmacro gnus-summary-article-pos (&optional number)
2591   "Return the position of the line of article NUMBER."
2592   `(gnus-data-pos (gnus-data-find
2593                    ,(or number '(gnus-summary-article-number)))))
2594
2595 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2596 (defmacro gnus-summary-article-subject (&optional number)
2597   "Return current subject string or nil if nothing."
2598   `(let ((headers
2599           ,(if number
2600                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2601              '(gnus-data-header (assq (gnus-summary-article-number)
2602                                       gnus-newsgroup-data)))))
2603      (and headers
2604           (vectorp headers)
2605           (mail-header-subject headers))))
2606
2607 (defmacro gnus-summary-article-score (&optional number)
2608   "Return current article score."
2609   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2610                   gnus-newsgroup-scored))
2611        gnus-summary-default-score 0))
2612
2613 (defun gnus-summary-article-children (&optional number)
2614   "Return a list of article numbers that are children of article NUMBER."
2615   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2616          (level (gnus-data-level (car data)))
2617          l children)
2618     (while (and (setq data (cdr data))
2619                 (> (setq l (gnus-data-level (car data))) level))
2620       (and (= (1+ level) l)
2621            (push (gnus-data-number (car data))
2622                  children)))
2623     (nreverse children)))
2624
2625 (defun gnus-summary-article-parent (&optional number)
2626   "Return the article number of the parent of article NUMBER."
2627   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2628                                     (gnus-data-list t)))
2629          (level (gnus-data-level (car data))))
2630     (if (zerop level)
2631         ()                              ; This is a root.
2632       ;; We search until we find an article with a level less than
2633       ;; this one.  That function has to be the parent.
2634       (while (and (setq data (cdr data))
2635                   (not (< (gnus-data-level (car data)) level))))
2636       (and data (gnus-data-number (car data))))))
2637
2638 (defun gnus-unread-mark-p (mark)
2639   "Say whether MARK is the unread mark."
2640   (= mark gnus-unread-mark))
2641
2642 (defun gnus-read-mark-p (mark)
2643   "Say whether MARK is one of the marks that mark as read.
2644 This is all marks except unread, ticked, dormant, and expirable."
2645   (not (or (= mark gnus-unread-mark)
2646            (= mark gnus-ticked-mark)
2647            (= mark gnus-dormant-mark)
2648            (= mark gnus-expirable-mark))))
2649
2650 (defmacro gnus-article-mark (number)
2651   "Return the MARK of article NUMBER.
2652 This macro should only be used when computing the mark the \"first\"
2653 time; i.e., when generating the summary lines.  After that,
2654 `gnus-summary-article-mark' should be used to examine the
2655 marks of articles."
2656   `(cond
2657     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2658     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2659     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2660     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2661     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2662     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2663     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2664     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2665            gnus-ancient-mark))))
2666
2667 ;; Saving hidden threads.
2668
2669 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2670 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2671
2672 (defmacro gnus-save-hidden-threads (&rest forms)
2673   "Save hidden threads, eval FORMS, and restore the hidden threads."
2674   (let ((config (make-symbol "config")))
2675     `(let ((,config (gnus-hidden-threads-configuration)))
2676        (unwind-protect
2677            (save-excursion
2678              ,@forms)
2679          (gnus-restore-hidden-threads-configuration ,config)))))
2680
2681 (defun gnus-data-compute-positions ()
2682   "Compute the positions of all articles."
2683   (setq gnus-newsgroup-data-reverse nil)
2684   (let ((data gnus-newsgroup-data))
2685     (save-excursion
2686       (gnus-save-hidden-threads
2687         (gnus-summary-show-all-threads)
2688         (goto-char (point-min))
2689         (while data
2690           (while (get-text-property (point) 'gnus-intangible)
2691             (forward-line 1))
2692           (gnus-data-set-pos (car data) (+ (point) 3))
2693           (setq data (cdr data))
2694           (forward-line 1))))))
2695
2696 (defun gnus-hidden-threads-configuration ()
2697   "Return the current hidden threads configuration."
2698   (save-excursion
2699     (let (config)
2700       (goto-char (point-min))
2701       (while (search-forward "\r" nil t)
2702         (push (1- (point)) config))
2703       config)))
2704
2705 (defun gnus-restore-hidden-threads-configuration (config)
2706   "Restore hidden threads configuration from CONFIG."
2707   (save-excursion
2708     (let (point buffer-read-only)
2709       (while (setq point (pop config))
2710         (when (and (< point (point-max))
2711                    (goto-char point)
2712                    (eq (char-after) ?\n))
2713           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2714
2715 ;; Various summary mode internalish functions.
2716
2717 (defun gnus-mouse-pick-article (e)
2718   (interactive "e")
2719   (mouse-set-point e)
2720   (gnus-summary-next-page nil t))
2721
2722 (defun gnus-summary-set-display-table ()
2723   "Change the display table.
2724 Odd characters have a tendency to mess
2725 up nicely formatted displays - we make all possible glyphs
2726 display only a single character."
2727
2728   ;; We start from the standard display table, if any.
2729   (let ((table (or (copy-sequence standard-display-table)
2730                    (make-display-table)))
2731         (i 32))
2732     ;; Nix out all the control chars...
2733     (while (>= (setq i (1- i)) 0)
2734       (aset table i [??]))
2735    ;; ... but not newline and cr, of course.  (cr is necessary for the
2736     ;; selective display).
2737     (aset table ?\n nil)
2738     (aset table ?\r nil)
2739     ;; We keep TAB as well.
2740     (aset table ?\t nil)
2741     ;; We nix out any glyphs over 126 that are not set already.
2742     (let ((i 256))
2743       (while (>= (setq i (1- i)) 127)
2744         ;; Only modify if the entry is nil.
2745         (unless (aref table i)
2746           (aset table i [??]))))
2747     (setq buffer-display-table table)))
2748
2749 (defun gnus-summary-set-article-display-arrow (pos)
2750   "Update the overlay arrow to point to line at position POS."
2751   (when (and gnus-summary-display-arrow
2752              (boundp 'overlay-arrow-position)
2753              (boundp 'overlay-arrow-string))
2754     (save-excursion
2755       (goto-char pos)
2756       (beginning-of-line)
2757       (unless overlay-arrow-position
2758         (setq overlay-arrow-position (make-marker)))
2759       (setq overlay-arrow-string "=>"
2760             overlay-arrow-position (set-marker overlay-arrow-position
2761                                                (point)
2762                                                (current-buffer))))))
2763
2764 (defun gnus-summary-buffer-name (group)
2765   "Return the summary buffer name of GROUP."
2766   (concat "*Summary " group "*"))
2767
2768 (defun gnus-summary-setup-buffer (group)
2769   "Initialize summary buffer."
2770   (let ((buffer (gnus-summary-buffer-name group)))
2771     (if (get-buffer buffer)
2772         (progn
2773           (set-buffer buffer)
2774           (setq gnus-summary-buffer (current-buffer))
2775           (not gnus-newsgroup-prepared))
2776       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2777       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2778       (gnus-summary-mode group)
2779       (when gnus-carpal
2780         (gnus-carpal-setup-buffer 'summary))
2781       (unless gnus-single-article-buffer
2782         (make-local-variable 'gnus-article-buffer)
2783         (make-local-variable 'gnus-article-current)
2784         (make-local-variable 'gnus-original-article-buffer))
2785       (setq gnus-newsgroup-name group)
2786       ;; Set any local variables in the group parameters.
2787       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2788       t)))
2789
2790 (defun gnus-set-global-variables ()
2791   "Set the global equivalents of the buffer-local variables.
2792 They are set to the latest values they had.  These reflect the summary
2793 buffer that was in action when the last article was fetched."
2794   (when (eq major-mode 'gnus-summary-mode)
2795     (setq gnus-summary-buffer (current-buffer))
2796     (let ((name gnus-newsgroup-name)
2797           (marked gnus-newsgroup-marked)
2798           (unread gnus-newsgroup-unreads)
2799           (headers gnus-current-headers)
2800           (data gnus-newsgroup-data)
2801           (summary gnus-summary-buffer)
2802           (article-buffer gnus-article-buffer)
2803           (original gnus-original-article-buffer)
2804           (gac gnus-article-current)
2805           (reffed gnus-reffed-article-number)
2806           (score-file gnus-current-score-file)
2807           (default-charset gnus-newsgroup-charset)
2808           vlist)
2809       (let ((locals gnus-newsgroup-variables))
2810         (while locals
2811           (if (consp (car locals))
2812               (push (eval (caar locals)) vlist)
2813             (push (eval (car locals)) vlist))
2814           (setq locals (cdr locals)))
2815         (setq vlist (nreverse vlist)))
2816       (save-excursion
2817         (set-buffer gnus-group-buffer)
2818         (setq gnus-newsgroup-name name
2819               gnus-newsgroup-marked marked
2820               gnus-newsgroup-unreads unread
2821               gnus-current-headers headers
2822               gnus-newsgroup-data data
2823               gnus-article-current gac
2824               gnus-summary-buffer summary
2825               gnus-article-buffer article-buffer
2826               gnus-original-article-buffer original
2827               gnus-reffed-article-number reffed
2828               gnus-current-score-file score-file
2829               gnus-newsgroup-charset default-charset)
2830         (let ((locals gnus-newsgroup-variables))
2831           (while locals
2832             (if (consp (car locals))
2833                 (set (caar locals) (pop vlist))
2834               (set (car locals) (pop vlist)))
2835             (setq locals (cdr locals))))
2836         ;; The article buffer also has local variables.
2837         (when (gnus-buffer-live-p gnus-article-buffer)
2838           (set-buffer gnus-article-buffer)
2839           (setq gnus-summary-buffer summary))))))
2840
2841 (defun gnus-summary-article-unread-p (article)
2842   "Say whether ARTICLE is unread or not."
2843   (memq article gnus-newsgroup-unreads))
2844
2845 (defun gnus-summary-first-article-p (&optional article)
2846   "Return whether ARTICLE is the first article in the buffer."
2847   (if (not (setq article (or article (gnus-summary-article-number))))
2848       nil
2849     (eq article (caar gnus-newsgroup-data))))
2850
2851 (defun gnus-summary-last-article-p (&optional article)
2852   "Return whether ARTICLE is the last article in the buffer."
2853   (if (not (setq article (or article (gnus-summary-article-number))))
2854       ;; All non-existent numbers are the last article.  :-)
2855       t
2856     (not (cdr (gnus-data-find-list article)))))
2857
2858 (defun gnus-make-thread-indent-array ()
2859   (let ((n 200))
2860     (unless (and gnus-thread-indent-array
2861                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2862       (setq gnus-thread-indent-array (make-vector 201 "")
2863             gnus-thread-indent-array-level gnus-thread-indent-level)
2864       (while (>= n 0)
2865         (aset gnus-thread-indent-array n
2866               (make-string (* n gnus-thread-indent-level) ? ))
2867         (setq n (1- n))))))
2868
2869 (defun gnus-update-summary-mark-positions ()
2870   "Compute where the summary marks are to go."
2871   (save-excursion
2872     (when (gnus-buffer-exists-p gnus-summary-buffer)
2873       (set-buffer gnus-summary-buffer))
2874     (let ((gnus-replied-mark 129)
2875           (gnus-score-below-mark 130)
2876           (gnus-score-over-mark 130)
2877           (gnus-download-mark 131)
2878           (spec gnus-summary-line-format-spec)
2879           gnus-visual pos)
2880       (save-excursion
2881         (gnus-set-work-buffer)
2882         (let ((gnus-summary-line-format-spec spec)
2883               (gnus-newsgroup-downloadable '((0 . t))))
2884           (gnus-summary-insert-line
2885            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2886            0 nil 128 t nil "" nil 1)
2887           (goto-char (point-min))
2888           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2889                                              (- (point) 2)))))
2890           (goto-char (point-min))
2891           (push (cons 'replied (and (search-forward "\201" nil t)
2892                                     (- (point) 2)))
2893                 pos)
2894           (goto-char (point-min))
2895           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2896                 pos)
2897           (goto-char (point-min))
2898           (push (cons 'download
2899                       (and (search-forward "\203" nil t) (- (point) 2)))
2900                 pos)))
2901       (setq gnus-summary-mark-positions pos))))
2902
2903 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2904   "Insert a dummy root in the summary buffer."
2905   (beginning-of-line)
2906   (gnus-add-text-properties
2907    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2908    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2909
2910 (defun gnus-summary-extract-address-component (from)
2911   (or (car (funcall gnus-extract-address-components from))
2912       from))
2913
2914 (defun gnus-summary-from-or-to-or-newsgroups (header gnus-tmp-from)
2915   (let ((mail-parse-charset gnus-newsgroup-charset)
2916         ; Is it really necessary to do this next part for each summary line?
2917         ; Luckily, doesn't seem to slow things down much.
2918         (mail-parse-ignored-charsets
2919          (save-excursion (set-buffer gnus-summary-buffer)
2920                          gnus-newsgroup-ignored-charsets)))
2921     (or
2922      (and gnus-ignored-from-addresses
2923           (string-match gnus-ignored-from-addresses gnus-tmp-from)
2924           (let ((extra-headers (mail-header-extra header))
2925                 to
2926                 newsgroups)
2927             (cond
2928              ((setq to (cdr (assq 'To extra-headers)))
2929               (concat "-> "
2930                       (gnus-summary-extract-address-component
2931                        (funcall gnus-decode-encoded-word-function to))))
2932              ((setq newsgroups (cdr (assq 'Newsgroups extra-headers)))
2933               (concat "=> " newsgroups)))))
2934      (gnus-summary-extract-address-component gnus-tmp-from))))
2935
2936 (defun gnus-summary-insert-line (gnus-tmp-header
2937                                  gnus-tmp-level gnus-tmp-current
2938                                  gnus-tmp-unread gnus-tmp-replied
2939                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2940                                  &optional gnus-tmp-dummy gnus-tmp-score
2941                                  gnus-tmp-process)
2942   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2943          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2944          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2945          (gnus-tmp-score-char
2946           (if (or (null gnus-summary-default-score)
2947                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2948                       gnus-summary-zcore-fuzz))
2949               ?                         ;Whitespace
2950             (if (< gnus-tmp-score gnus-summary-default-score)
2951                 gnus-score-below-mark gnus-score-over-mark)))
2952          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2953          (gnus-tmp-replied
2954           (cond (gnus-tmp-process gnus-process-mark)
2955                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2956                  gnus-cached-mark)
2957                 (gnus-tmp-replied gnus-replied-mark)
2958                 ((memq gnus-tmp-current gnus-newsgroup-forwarded)
2959                  gnus-forwarded-mark)
2960                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2961                  gnus-saved-mark)
2962                 ((memq gnus-tmp-number gnus-newsgroup-recent)
2963                  gnus-recent-mark)
2964                 ((memq gnus-tmp-number gnus-newsgroup-unseen)
2965                  gnus-unseen-mark)
2966                 (t gnus-no-mark)))
2967          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2968          (gnus-tmp-name
2969           (cond
2970            ((string-match "<[^>]+> *$" gnus-tmp-from)
2971             (let ((beg (match-beginning 0)))
2972               (or (and (string-match "^\".+\"" gnus-tmp-from)
2973                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2974                   (substring gnus-tmp-from 0 beg))))
2975            ((string-match "(.+)" gnus-tmp-from)
2976             (substring gnus-tmp-from
2977                        (1+ (match-beginning 0)) (1- (match-end 0))))
2978            (t gnus-tmp-from)))
2979          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2980          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2981          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2982          (buffer-read-only nil))
2983     (when (string= gnus-tmp-name "")
2984       (setq gnus-tmp-name gnus-tmp-from))
2985     (unless (numberp gnus-tmp-lines)
2986       (setq gnus-tmp-lines -1))
2987     (if (= gnus-tmp-lines -1)
2988         (setq gnus-tmp-lines "?")
2989       (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
2990     (gnus-put-text-property
2991      (point)
2992      (progn (eval gnus-summary-line-format-spec) (point))
2993      'gnus-number gnus-tmp-number)
2994     (when (gnus-visual-p 'summary-highlight 'highlight)
2995       (forward-line -1)
2996       (gnus-run-hooks 'gnus-summary-update-hook)
2997       (forward-line 1))))
2998
2999 (defun gnus-summary-update-line (&optional dont-update)
3000   "Update summary line after change."
3001   (when (and gnus-summary-default-score
3002              (not gnus-summary-inhibit-highlight))
3003     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
3004            (article (gnus-summary-article-number))
3005            (score (gnus-summary-article-score article)))
3006       (unless dont-update
3007         (if (and gnus-summary-mark-below
3008                  (< (gnus-summary-article-score)
3009                     gnus-summary-mark-below))
3010             ;; This article has a low score, so we mark it as read.
3011             (when (memq article gnus-newsgroup-unreads)
3012               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3013           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3014             ;; This article was previously marked as read on account
3015             ;; of a low score, but now it has risen, so we mark it as
3016             ;; unread.
3017             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3018         (gnus-summary-update-mark
3019          (if (or (null gnus-summary-default-score)
3020                  (<= (abs (- score gnus-summary-default-score))
3021                      gnus-summary-zcore-fuzz))
3022              ?                          ;Whitespace
3023            (if (< score gnus-summary-default-score)
3024                gnus-score-below-mark gnus-score-over-mark))
3025          'score))
3026       ;; Do visual highlighting.
3027       (when (gnus-visual-p 'summary-highlight 'highlight)
3028         (gnus-run-hooks 'gnus-summary-update-hook)))))
3029
3030 (defvar gnus-tmp-new-adopts nil)
3031
3032 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3033   "Return the number of articles in THREAD.
3034 This may be 0 in some cases -- if none of the articles in
3035 the thread are to be displayed."
3036   (let* ((number
3037          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3038           (cond
3039            ((not (listp thread))
3040             1)
3041            ((and (consp thread) (cdr thread))
3042             (apply
3043              '+ 1 (mapcar
3044                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3045            ((null thread)
3046             1)
3047            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3048             1)
3049            (t 0))))
3050     (when (and level (zerop level) gnus-tmp-new-adopts)
3051       (incf number
3052             (apply '+ (mapcar
3053                        'gnus-summary-number-of-articles-in-thread
3054                        gnus-tmp-new-adopts))))
3055     (if char
3056         (if (> number 1) gnus-not-empty-thread-mark
3057           gnus-empty-thread-mark)
3058       number)))
3059
3060 (defun gnus-summary-set-local-parameters (group)
3061   "Go through the local params of GROUP and set all variable specs in that list."
3062   (let ((params (gnus-group-find-parameter group))
3063         (vars '(quit-config))           ; Ignore quit-config.
3064         elem)
3065     (while params
3066       (setq elem (car params)
3067             params (cdr params))
3068       (and (consp elem)                 ; Has to be a cons.
3069            (consp (cdr elem))           ; The cdr has to be a list.
3070            (symbolp (car elem))         ; Has to be a symbol in there.
3071            (not (memq (car elem) vars))
3072            (ignore-errors               ; So we set it.
3073              (push (car elem) vars)
3074              (make-local-variable (car elem))
3075              (set (car elem) (eval (nth 1 elem))))))))
3076
3077 (defun gnus-summary-read-group (group &optional show-all no-article
3078                                       kill-buffer no-display backward
3079                                       select-articles)
3080   "Start reading news in newsgroup GROUP.
3081 If SHOW-ALL is non-nil, already read articles are also listed.
3082 If NO-ARTICLE is non-nil, no article is selected initially.
3083 If NO-DISPLAY, don't generate a summary buffer."
3084   (let (result)
3085     (while (and group
3086                 (null (setq result
3087                             (let ((gnus-auto-select-next nil))
3088                               (or (gnus-summary-read-group-1
3089                                    group show-all no-article
3090                                    kill-buffer no-display
3091                                    select-articles)
3092                                   (setq show-all nil
3093                                         select-articles nil)))))
3094                 (eq gnus-auto-select-next 'quietly))
3095       (set-buffer gnus-group-buffer)
3096       ;; The entry function called above goes to the next
3097       ;; group automatically, so we go two groups back
3098       ;; if we are searching for the previous group.
3099       (when backward
3100         (gnus-group-prev-unread-group 2))
3101       (if (not (equal group (gnus-group-group-name)))
3102           (setq group (gnus-group-group-name))
3103         (setq group nil)))
3104     result))
3105
3106 (defun gnus-summary-read-group-1 (group show-all no-article
3107                                         kill-buffer no-display
3108                                         &optional select-articles)
3109   ;; Killed foreign groups can't be entered.
3110   ;;  (when (and (not (gnus-group-native-p group))
3111   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3112   ;;    (error "Dead non-native groups can't be entered"))
3113   (gnus-message 5 "Retrieving newsgroup: %s..."
3114                 (gnus-group-decoded-name group))
3115   (let* ((new-group (gnus-summary-setup-buffer group))
3116          (quit-config (gnus-group-quit-config group))
3117          (did-select (and new-group (gnus-select-newsgroup
3118                                      group show-all select-articles))))
3119     (cond
3120      ;; This summary buffer exists already, so we just select it.
3121      ((not new-group)
3122       (gnus-set-global-variables)
3123       (when kill-buffer
3124         (gnus-kill-or-deaden-summary kill-buffer))
3125       (gnus-configure-windows 'summary 'force)
3126       (gnus-set-mode-line 'summary)
3127       (gnus-summary-position-point)
3128       (message "")
3129       t)
3130      ;; We couldn't select this group.
3131      ((null did-select)
3132       (when (and (eq major-mode 'gnus-summary-mode)
3133                  (not (equal (current-buffer) kill-buffer)))
3134         (kill-buffer (current-buffer))
3135         (if (not quit-config)
3136             (progn
3137               ;; Update the info -- marks might need to be removed,
3138               ;; for instance.
3139               (gnus-summary-update-info)
3140               (set-buffer gnus-group-buffer)
3141               (gnus-group-jump-to-group group)
3142               (gnus-group-next-unread-group 1))
3143           (gnus-handle-ephemeral-exit quit-config)))
3144       (let ((grpinfo (gnus-get-info group)))
3145         (if (null (gnus-info-read grpinfo))
3146             (gnus-message 3 "Group %s contains no messages"
3147                           (gnus-group-decoded-name group))
3148           (gnus-message 3 "Can't select group")))
3149       nil)
3150      ;; The user did a `C-g' while prompting for number of articles,
3151      ;; so we exit this group.
3152      ((eq did-select 'quit)
3153       (and (eq major-mode 'gnus-summary-mode)
3154            (not (equal (current-buffer) kill-buffer))
3155            (kill-buffer (current-buffer)))
3156       (when kill-buffer
3157         (gnus-kill-or-deaden-summary kill-buffer))
3158       (if (not quit-config)
3159           (progn
3160             (set-buffer gnus-group-buffer)
3161             (gnus-group-jump-to-group group)
3162             (gnus-group-next-unread-group 1)
3163             (gnus-configure-windows 'group 'force))
3164         (gnus-handle-ephemeral-exit quit-config))
3165       ;; Finally signal the quit.
3166       (signal 'quit nil))
3167      ;; The group was successfully selected.
3168      (t
3169       (gnus-set-global-variables)
3170       ;; Save the active value in effect when the group was entered.
3171       (setq gnus-newsgroup-active
3172             (gnus-copy-sequence
3173              (gnus-active gnus-newsgroup-name)))
3174       ;; You can change the summary buffer in some way with this hook.
3175       (gnus-run-hooks 'gnus-select-group-hook)
3176       (gnus-update-format-specifications
3177        nil 'summary 'summary-mode 'summary-dummy)
3178       (gnus-update-summary-mark-positions)
3179       ;; Do score processing.
3180       (when gnus-use-scoring
3181         (gnus-possibly-score-headers))
3182       ;; Check whether to fill in the gaps in the threads.
3183       (when gnus-build-sparse-threads
3184         (gnus-build-sparse-threads))
3185       ;; Find the initial limit.
3186       (if gnus-show-threads
3187           (if show-all
3188               (let ((gnus-newsgroup-dormant nil))
3189                 (gnus-summary-initial-limit show-all))
3190             (gnus-summary-initial-limit show-all))
3191         ;; When untreaded, all articles are always shown.
3192         (setq gnus-newsgroup-limit
3193               (mapcar
3194                (lambda (header) (mail-header-number header))
3195                gnus-newsgroup-headers)))
3196       ;; Generate the summary buffer.
3197       (unless no-display
3198         (gnus-summary-prepare))
3199       (when gnus-use-trees
3200         (gnus-tree-open group)
3201         (setq gnus-summary-highlight-line-function
3202               'gnus-tree-highlight-article))
3203       ;; If the summary buffer is empty, but there are some low-scored
3204       ;; articles or some excluded dormants, we include these in the
3205       ;; buffer.
3206       (when (and (zerop (buffer-size))
3207                  (not no-display))
3208         (cond (gnus-newsgroup-dormant
3209                (gnus-summary-limit-include-dormant))
3210               ((and gnus-newsgroup-scored show-all)
3211                (gnus-summary-limit-include-expunged t))))
3212       ;; Function `gnus-apply-kill-file' must be called in this hook.
3213       (gnus-run-hooks 'gnus-apply-kill-hook)
3214       (if (and (zerop (buffer-size))
3215                (not no-display))
3216           (progn
3217             ;; This newsgroup is empty.
3218             (gnus-summary-catchup-and-exit nil t)
3219             (gnus-message 6 "No unread news")
3220             (when kill-buffer
3221               (gnus-kill-or-deaden-summary kill-buffer))
3222             ;; Return nil from this function.
3223             nil)
3224         ;; Hide conversation thread subtrees.  We cannot do this in
3225         ;; gnus-summary-prepare-hook since kill processing may not
3226         ;; work with hidden articles.
3227         (and gnus-show-threads
3228              gnus-thread-hide-subtree
3229              (gnus-summary-hide-all-threads))
3230         (when kill-buffer
3231           (gnus-kill-or-deaden-summary kill-buffer))
3232         ;; Show first unread article if requested.
3233         (if (and (not no-article)
3234                  (not no-display)
3235                  gnus-newsgroup-unreads
3236                  gnus-auto-select-first)
3237             (progn
3238               (gnus-configure-windows 'summary)
3239               (cond
3240                ((eq gnus-auto-select-first 'best)
3241                 (gnus-summary-best-unread-article))
3242                ((eq gnus-auto-select-first t)
3243                 (gnus-summary-first-unread-article))
3244                ((gnus-functionp gnus-auto-select-first)
3245                 (funcall gnus-auto-select-first))))
3246           ;; Don't select any articles, just move point to the first
3247           ;; article in the group.
3248           (goto-char (point-min))
3249           (gnus-summary-position-point)
3250           (gnus-configure-windows 'summary 'force)
3251           (gnus-set-mode-line 'summary))
3252         (when (get-buffer-window gnus-group-buffer t)
3253           ;; Gotta use windows, because recenter does weird stuff if
3254           ;; the current buffer ain't the displayed window.
3255           (let ((owin (selected-window)))
3256             (select-window (get-buffer-window gnus-group-buffer t))
3257             (when (gnus-group-goto-group group)
3258               (recenter))
3259             (select-window owin)))
3260         ;; Mark this buffer as "prepared".
3261         (setq gnus-newsgroup-prepared t)
3262         (gnus-run-hooks 'gnus-summary-prepared-hook)
3263         t)))))
3264
3265 (defun gnus-summary-prepare ()
3266   "Generate the summary buffer."
3267   (interactive)
3268   (let ((buffer-read-only nil))
3269     (erase-buffer)
3270     (setq gnus-newsgroup-data nil
3271           gnus-newsgroup-data-reverse nil)
3272     (gnus-run-hooks 'gnus-summary-generate-hook)
3273     ;; Generate the buffer, either with threads or without.
3274     (when gnus-newsgroup-headers
3275       (gnus-summary-prepare-threads
3276        (if gnus-show-threads
3277            (gnus-sort-gathered-threads
3278             (funcall gnus-summary-thread-gathering-function
3279                      (gnus-sort-threads
3280                       (gnus-cut-threads (gnus-make-threads)))))
3281          ;; Unthreaded display.
3282          (gnus-sort-articles gnus-newsgroup-headers))))
3283     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3284     ;; Call hooks for modifying summary buffer.
3285     (goto-char (point-min))
3286     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3287
3288 (defsubst gnus-general-simplify-subject (subject)
3289   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3290   (setq subject
3291         (cond
3292          ;; Truncate the subject.
3293          (gnus-simplify-subject-functions
3294           (gnus-map-function gnus-simplify-subject-functions subject))
3295          ((numberp gnus-summary-gather-subject-limit)
3296           (setq subject (gnus-simplify-subject-re subject))
3297           (if (> (length subject) gnus-summary-gather-subject-limit)
3298               (substring subject 0 gnus-summary-gather-subject-limit)
3299             subject))
3300          ;; Fuzzily simplify it.
3301          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3302           (gnus-simplify-subject-fuzzy subject))
3303          ;; Just remove the leading "Re:".
3304          (t
3305           (gnus-simplify-subject-re subject))))
3306
3307   (if (and gnus-summary-gather-exclude-subject
3308            (string-match gnus-summary-gather-exclude-subject subject))
3309       nil                         ; This article shouldn't be gathered
3310     subject))
3311
3312 (defun gnus-summary-simplify-subject-query ()
3313   "Query where the respool algorithm would put this article."
3314   (interactive)
3315   (gnus-summary-select-article)
3316   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3317
3318 (defun gnus-gather-threads-by-subject (threads)
3319   "Gather threads by looking at Subject headers."
3320   (if (not gnus-summary-make-false-root)
3321       threads
3322     (let ((hashtb (gnus-make-hashtable 1024))
3323           (prev threads)
3324           (result threads)
3325           subject hthread whole-subject)
3326       (while threads
3327         (setq subject (gnus-general-simplify-subject
3328                        (setq whole-subject (mail-header-subject
3329                                             (caar threads)))))
3330         (when subject
3331           (if (setq hthread (gnus-gethash subject hashtb))
3332               (progn
3333                 ;; We enter a dummy root into the thread, if we
3334                 ;; haven't done that already.
3335                 (unless (stringp (caar hthread))
3336                   (setcar hthread (list whole-subject (car hthread))))
3337                 ;; We add this new gathered thread to this gathered
3338                 ;; thread.
3339                 (setcdr (car hthread)
3340                         (nconc (cdar hthread) (list (car threads))))
3341                 ;; Remove it from the list of threads.
3342                 (setcdr prev (cdr threads))
3343                 (setq threads prev))
3344             ;; Enter this thread into the hash table.
3345             (gnus-sethash subject threads hashtb)))
3346         (setq prev threads)
3347         (setq threads (cdr threads)))
3348       result)))
3349
3350 (defun gnus-gather-threads-by-references (threads)
3351   "Gather threads by looking at References headers."
3352   (let ((idhashtb (gnus-make-hashtable 1024))
3353         (thhashtb (gnus-make-hashtable 1024))
3354         (prev threads)
3355         (result threads)
3356         ids references id gthread gid entered ref)
3357     (while threads
3358       (when (setq references (mail-header-references (caar threads)))
3359         (setq id (mail-header-id (caar threads))
3360               ids (gnus-split-references references)
3361               entered nil)
3362         (while (setq ref (pop ids))
3363           (setq ids (delete ref ids))
3364           (if (not (setq gid (gnus-gethash ref idhashtb)))
3365               (progn
3366                 (gnus-sethash ref id idhashtb)
3367                 (gnus-sethash id threads thhashtb))
3368             (setq gthread (gnus-gethash gid thhashtb))
3369             (unless entered
3370               ;; We enter a dummy root into the thread, if we
3371               ;; haven't done that already.
3372               (unless (stringp (caar gthread))
3373                 (setcar gthread (list (mail-header-subject (caar gthread))
3374                                       (car gthread))))
3375               ;; We add this new gathered thread to this gathered
3376               ;; thread.
3377               (setcdr (car gthread)
3378                       (nconc (cdar gthread) (list (car threads)))))
3379             ;; Add it into the thread hash table.
3380             (gnus-sethash id gthread thhashtb)
3381             (setq entered t)
3382             ;; Remove it from the list of threads.
3383             (setcdr prev (cdr threads))
3384             (setq threads prev))))
3385       (setq prev threads)
3386       (setq threads (cdr threads)))
3387     result))
3388
3389 (defun gnus-sort-gathered-threads (threads)
3390   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3391   (let ((result threads))
3392     (while threads
3393       (when (stringp (caar threads))
3394         (setcdr (car threads)
3395                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3396       (setq threads (cdr threads)))
3397     result))
3398
3399 (defun gnus-thread-loop-p (root thread)
3400   "Say whether ROOT is in THREAD."
3401   (let ((stack (list thread))
3402         (infloop 0)
3403         th)
3404     (while (setq thread (pop stack))
3405       (setq th (cdr thread))
3406       (while (and th
3407                   (not (eq (caar th) root)))
3408         (pop th))
3409       (if th
3410           ;; We have found a loop.
3411           (let (ref-dep)
3412             (setcdr thread (delq (car th) (cdr thread)))
3413             (if (boundp (setq ref-dep (intern "none"
3414                                               gnus-newsgroup-dependencies)))
3415                 (setcdr (symbol-value ref-dep)
3416                         (nconc (cdr (symbol-value ref-dep))
3417                                (list (car th))))
3418               (set ref-dep (list nil (car th))))
3419             (setq infloop 1
3420                   stack nil))
3421         ;; Push all the subthreads onto the stack.
3422         (push (cdr thread) stack)))
3423     infloop))
3424
3425 (defun gnus-make-threads ()
3426   "Go through the dependency hashtb and find the roots.  Return all threads."
3427   (let (threads)
3428     (while (catch 'infloop
3429              (mapatoms
3430               (lambda (refs)
3431                 ;; Deal with self-referencing References loops.
3432                 (when (and (car (symbol-value refs))
3433                            (not (zerop
3434                                  (apply
3435                                   '+
3436                                   (mapcar
3437                                    (lambda (thread)
3438                                      (gnus-thread-loop-p
3439                                       (car (symbol-value refs)) thread))
3440                                    (cdr (symbol-value refs)))))))
3441                   (setq threads nil)
3442                   (throw 'infloop t))
3443                 (unless (car (symbol-value refs))
3444              ;; These threads do not refer back to any other articles,
3445                   ;; so they're roots.
3446                   (setq threads (append (cdr (symbol-value refs)) threads))))
3447               gnus-newsgroup-dependencies)))
3448     threads))
3449
3450 ;; Build the thread tree.
3451 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3452   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3453
3454 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3455 if it was already present.
3456
3457 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3458 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3459 Message-IDs will be renamed be renamed to a unique Message-ID before
3460 being entered.
3461
3462 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3463   (let* ((id (mail-header-id header))
3464          (id-dep (and id (intern id dependencies)))
3465          ref ref-dep ref-header)
3466     ;; Enter this `header' in the `dependencies' table.
3467     (cond
3468      ((not id-dep)
3469       (setq header nil))
3470      ;; The first two cases do the normal part: enter a new `header'
3471      ;; in the `dependencies' table.
3472      ((not (boundp id-dep))
3473       (set id-dep (list header)))
3474      ((null (car (symbol-value id-dep)))
3475       (setcar (symbol-value id-dep) header))
3476
3477      ;; From here the `header' was already present in the
3478      ;; `dependencies' table.
3479      (force-new
3480       ;; Overrides an existing entry;
3481       ;; just set the header part of the entry.
3482       (setcar (symbol-value id-dep) header))
3483
3484      ;; Renames the existing `header' to a unique Message-ID.
3485      ((not gnus-summary-ignore-duplicates)
3486       ;; An article with this Message-ID has already been seen.
3487       ;; We rename the Message-ID.
3488       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3489            (list header))
3490       (mail-header-set-id header id))
3491
3492      ;; The last case ignores an existing entry, except it adds any
3493      ;; additional Xrefs (in case the two articles came from different
3494      ;; servers.
3495      ;; Also sets `header' to `nil' meaning that the `dependencies'
3496      ;; table was *not* modified.
3497      (t
3498       (mail-header-set-xref
3499        (car (symbol-value id-dep))
3500        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3501                    "")
3502                (or (mail-header-xref header) "")))
3503       (setq header nil)))
3504
3505     (when header
3506       ;; First check if that we are not creating a References loop.
3507       (setq ref (gnus-parent-id (mail-header-references header)))
3508       (while (and ref
3509                   (setq ref-dep (intern-soft ref dependencies))
3510                   (boundp ref-dep)
3511                   (setq ref-header (car (symbol-value ref-dep))))
3512         (if (string= id ref)
3513             ;; Yuk!  This is a reference loop.  Make the article be a
3514             ;; root article.
3515             (progn
3516               (mail-header-set-references (car (symbol-value id-dep)) "none")
3517               (setq ref nil))
3518           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3519       (setq ref (gnus-parent-id (mail-header-references header)))
3520       (setq ref-dep (intern (or ref "none") dependencies))
3521       (if (boundp ref-dep)
3522           (setcdr (symbol-value ref-dep)
3523                   (nconc (cdr (symbol-value ref-dep))
3524                          (list (symbol-value id-dep))))
3525         (set ref-dep (list nil (symbol-value id-dep)))))
3526     header))
3527
3528 (defun gnus-build-sparse-threads ()
3529   (let ((headers gnus-newsgroup-headers)
3530         (mail-parse-charset gnus-newsgroup-charset)
3531         (gnus-summary-ignore-duplicates t)
3532         header references generation relations
3533         subject child end new-child date)
3534     ;; First we create an alist of generations/relations, where
3535     ;; generations is how much we trust the relation, and the relation
3536     ;; is parent/child.
3537     (gnus-message 7 "Making sparse threads...")
3538     (save-excursion
3539       (nnheader-set-temp-buffer " *gnus sparse threads*")
3540       (while (setq header (pop headers))
3541         (when (and (setq references (mail-header-references header))
3542                    (not (string= references "")))
3543           (insert references)
3544           (setq child (mail-header-id header)
3545                 subject (mail-header-subject header)
3546                 date (mail-header-date header)
3547                 generation 0)
3548           (while (search-backward ">" nil t)
3549             (setq end (1+ (point)))
3550             (when (search-backward "<" nil t)
3551               (setq new-child (buffer-substring (point) end))
3552               (push (list (incf generation)
3553                           child (setq child new-child)
3554                           subject date)
3555                     relations)))
3556           (when child
3557             (push (list (1+ generation) child nil subject) relations))
3558           (erase-buffer)))
3559       (kill-buffer (current-buffer)))
3560     ;; Sort over trustworthiness.
3561     (mapcar
3562      (lambda (relation)
3563        (when (gnus-dependencies-add-header
3564               (make-full-mail-header
3565                gnus-reffed-article-number
3566                (nth 3 relation) "" (or (nth 4 relation) "")
3567                (nth 1 relation)
3568                (or (nth 2 relation) "") 0 0 "")
3569               gnus-newsgroup-dependencies nil)
3570          (push gnus-reffed-article-number gnus-newsgroup-limit)
3571          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3572          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3573                gnus-newsgroup-reads)
3574          (decf gnus-reffed-article-number)))
3575      (sort relations 'car-less-than-car))
3576     (gnus-message 7 "Making sparse threads...done")))
3577
3578 (defun gnus-build-old-threads ()
3579   ;; Look at all the articles that refer back to old articles, and
3580   ;; fetch the headers for the articles that aren't there.  This will
3581   ;; build complete threads - if the roots haven't been expired by the
3582   ;; server, that is.
3583   (let ((mail-parse-charset gnus-newsgroup-charset)
3584         id heads)
3585     (mapatoms
3586      (lambda (refs)
3587        (when (not (car (symbol-value refs)))
3588          (setq heads (cdr (symbol-value refs)))
3589          (while heads
3590            (if (memq (mail-header-number (caar heads))
3591                      gnus-newsgroup-dormant)
3592                (setq heads (cdr heads))
3593              (setq id (symbol-name refs))
3594              (while (and (setq id (gnus-build-get-header id))
3595                          (not (car (gnus-id-to-thread id)))))
3596              (setq heads nil)))))
3597      gnus-newsgroup-dependencies)))
3598
3599 ;; This function has to be called with point after the article number
3600 ;; on the beginning of the line.
3601 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3602   (let ((eol (gnus-point-at-eol))
3603         (buffer (current-buffer))
3604         header)
3605
3606     ;; overview: [num subject from date id refs chars lines misc]
3607     (unwind-protect
3608         (progn
3609           (narrow-to-region (point) eol)
3610           (unless (eobp)
3611             (forward-char))
3612
3613           (setq header
3614                 (make-full-mail-header
3615                  number                 ; number
3616                  (funcall gnus-decode-encoded-word-function
3617                           (nnheader-nov-field)) ; subject
3618                  (funcall gnus-decode-encoded-word-function
3619                           (nnheader-nov-field)) ; from
3620                  (nnheader-nov-field)   ; date
3621                  (nnheader-nov-read-message-id) ; id
3622                  (nnheader-nov-field)   ; refs
3623                  (nnheader-nov-read-integer) ; chars
3624                  (nnheader-nov-read-integer) ; lines
3625                  (unless (eobp)
3626                    (if (looking-at "Xref: ")
3627                        (goto-char (match-end 0)))
3628                    (nnheader-nov-field)) ; Xref
3629                  (nnheader-nov-parse-extra)))) ; extra
3630
3631       (widen))
3632
3633     (when gnus-alter-header-function
3634       (funcall gnus-alter-header-function header))
3635     (gnus-dependencies-add-header header dependencies force-new)))
3636
3637 (defun gnus-build-get-header (id)
3638   "Look through the buffer of NOV lines and find the header to ID.
3639 Enter this line into the dependencies hash table, and return
3640 the id of the parent article (if any)."
3641   (let ((deps gnus-newsgroup-dependencies)
3642         found header)
3643     (prog1
3644         (save-excursion
3645           (set-buffer nntp-server-buffer)
3646           (let ((case-fold-search nil))
3647             (goto-char (point-min))
3648             (while (and (not found)
3649                         (search-forward id nil t))
3650               (beginning-of-line)
3651               (setq found (looking-at
3652                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3653                                    (regexp-quote id))))
3654               (or found (beginning-of-line 2)))
3655             (when found
3656               (beginning-of-line)
3657               (and
3658                (setq header (gnus-nov-parse-line
3659                              (read (current-buffer)) deps))
3660                (gnus-parent-id (mail-header-references header))))))
3661       (when header
3662         (let ((number (mail-header-number header)))
3663           (push number gnus-newsgroup-limit)
3664           (push header gnus-newsgroup-headers)
3665           (if (memq number gnus-newsgroup-unselected)
3666               (progn
3667                 (push number gnus-newsgroup-unreads)
3668                 (setq gnus-newsgroup-unselected
3669                       (delq number gnus-newsgroup-unselected)))
3670             (push number gnus-newsgroup-ancient)))))))
3671
3672 (defun gnus-build-all-threads ()
3673   "Read all the headers."
3674   (let ((gnus-summary-ignore-duplicates t)
3675         (mail-parse-charset gnus-newsgroup-charset)
3676         (dependencies gnus-newsgroup-dependencies)
3677         header article)
3678     (save-excursion
3679       (set-buffer nntp-server-buffer)
3680       (let ((case-fold-search nil))
3681         (goto-char (point-min))
3682         (while (not (eobp))
3683           (ignore-errors
3684             (setq article (read (current-buffer))
3685                   header (gnus-nov-parse-line article dependencies)))
3686           (when header
3687             (save-excursion
3688               (set-buffer gnus-summary-buffer)
3689               (push header gnus-newsgroup-headers)
3690               (if (memq (setq article (mail-header-number header))
3691                         gnus-newsgroup-unselected)
3692                   (progn
3693                     (push article gnus-newsgroup-unreads)
3694                     (setq gnus-newsgroup-unselected
3695                           (delq article gnus-newsgroup-unselected)))
3696                 (push article gnus-newsgroup-ancient)))
3697             (forward-line 1)))))))
3698
3699 (defun gnus-summary-update-article-line (article header)
3700   "Update the line for ARTICLE using HEADERS."
3701   (let* ((id (mail-header-id header))
3702          (thread (gnus-id-to-thread id)))
3703     (unless thread
3704       (error "Article in no thread"))
3705     ;; Update the thread.
3706     (setcar thread header)
3707     (gnus-summary-goto-subject article)
3708     (let* ((datal (gnus-data-find-list article))
3709            (data (car datal))
3710            (length (when (cdr datal)
3711                      (- (gnus-data-pos data)
3712                         (gnus-data-pos (cadr datal)))))
3713            (buffer-read-only nil)
3714            (level (gnus-summary-thread-level)))
3715       (gnus-delete-line)
3716       (gnus-summary-insert-line
3717        header level nil (gnus-article-mark article)
3718        (memq article gnus-newsgroup-replied)
3719        (memq article gnus-newsgroup-expirable)
3720        ;; Only insert the Subject string when it's different
3721        ;; from the previous Subject string.
3722        (if (and
3723             gnus-show-threads
3724             (gnus-subject-equal
3725              (condition-case ()
3726                  (mail-header-subject
3727                   (gnus-data-header
3728                    (cadr
3729                     (gnus-data-find-list
3730                      article
3731                      (gnus-data-list t)))))
3732                ;; Error on the side of excessive subjects.
3733                (error ""))
3734              (mail-header-subject header)))
3735            ""
3736          (mail-header-subject header))
3737        nil (cdr (assq article gnus-newsgroup-scored))
3738        (memq article gnus-newsgroup-processable))
3739       (when length
3740         (gnus-data-update-list
3741          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3742
3743 (defun gnus-summary-update-article (article &optional iheader)
3744   "Update ARTICLE in the summary buffer."
3745   (set-buffer gnus-summary-buffer)
3746   (let* ((header (gnus-summary-article-header article))
3747          (id (mail-header-id header))
3748          (data (gnus-data-find article))
3749          (thread (gnus-id-to-thread id))
3750          (references (mail-header-references header))
3751          (parent
3752           (gnus-id-to-thread
3753            (or (gnus-parent-id
3754                 (when (and references
3755                            (not (equal "" references)))
3756                   references))
3757                "none")))
3758          (buffer-read-only nil)
3759          (old (car thread)))
3760     (when thread
3761       (unless iheader
3762         (setcar thread nil)
3763         (when parent
3764           (delq thread parent)))
3765       (if (gnus-summary-insert-subject id header)
3766        ;; Set the (possibly) new article number in the data structure.
3767           (gnus-data-set-number data (gnus-id-to-article id))
3768         (setcar thread old)
3769         nil))))
3770
3771 (defun gnus-rebuild-thread (id &optional line)
3772   "Rebuild the thread containing ID.
3773 If LINE, insert the rebuilt thread starting on line LINE."
3774   (let ((buffer-read-only nil)
3775         old-pos current thread data)
3776     (if (not gnus-show-threads)
3777         (setq thread (list (car (gnus-id-to-thread id))))
3778       ;; Get the thread this article is part of.
3779       (setq thread (gnus-remove-thread id)))
3780     (setq old-pos (gnus-point-at-bol))
3781     (setq current (save-excursion
3782                     (and (re-search-backward "[\r\n]" nil t)
3783                          (gnus-summary-article-number))))
3784     ;; If this is a gathered thread, we have to go some re-gathering.
3785     (when (stringp (car thread))
3786       (let ((subject (car thread))
3787             roots thr)
3788         (setq thread (cdr thread))
3789         (while thread
3790           (unless (memq (setq thr (gnus-id-to-thread
3791                                    (gnus-root-id
3792                                     (mail-header-id (caar thread)))))
3793                         roots)
3794             (push thr roots))
3795           (setq thread (cdr thread)))
3796         ;; We now have all (unique) roots.
3797         (if (= (length roots) 1)
3798             ;; All the loose roots are now one solid root.
3799             (setq thread (car roots))
3800           (setq thread (cons subject (gnus-sort-threads roots))))))
3801     (let (threads)
3802       ;; We then insert this thread into the summary buffer.
3803       (when line
3804         (goto-char (point-min))
3805         (forward-line (1- line)))
3806       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3807         (if gnus-show-threads
3808             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3809           (gnus-summary-prepare-unthreaded thread))
3810         (setq data (nreverse gnus-newsgroup-data))
3811         (setq threads gnus-newsgroup-threads))
3812       ;; We splice the new data into the data structure.
3813       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3814       ;;!!! then we want to insert at the beginning of the buffer.
3815       ;;!!! That happens to be true with Gnus now, but that may
3816       ;;!!! change in the future.  Perhaps.
3817       (gnus-data-enter-list
3818        (if line nil current) data (- (point) old-pos))
3819       (setq gnus-newsgroup-threads
3820             (nconc threads gnus-newsgroup-threads))
3821       (gnus-data-compute-positions))))
3822
3823 (defun gnus-number-to-header (number)
3824   "Return the header for article NUMBER."
3825   (let ((headers gnus-newsgroup-headers))
3826     (while (and headers
3827                 (not (= number (mail-header-number (car headers)))))
3828       (pop headers))
3829     (when headers
3830       (car headers))))
3831
3832 (defun gnus-parent-headers (in-headers &optional generation)
3833   "Return the headers of the GENERATIONeth parent of HEADERS."
3834   (unless generation
3835     (setq generation 1))
3836   (let ((parent t)
3837         (headers in-headers)
3838         references)
3839     (while (and parent
3840                 (not (zerop generation))
3841                 (setq references (mail-header-references headers)))
3842       (setq headers (if (and references
3843                              (setq parent (gnus-parent-id references)))
3844                         (car (gnus-id-to-thread parent))
3845                       nil))
3846       (decf generation))
3847     (and (not (eq headers in-headers))
3848          headers)))
3849
3850 (defun gnus-id-to-thread (id)
3851   "Return the (sub-)thread where ID appears."
3852   (gnus-gethash id gnus-newsgroup-dependencies))
3853
3854 (defun gnus-id-to-article (id)
3855   "Return the article number of ID."
3856   (let ((thread (gnus-id-to-thread id)))
3857     (when (and thread
3858                (car thread))
3859       (mail-header-number (car thread)))))
3860
3861 (defun gnus-id-to-header (id)
3862   "Return the article headers of ID."
3863   (car (gnus-id-to-thread id)))
3864
3865 (defun gnus-article-displayed-root-p (article)
3866   "Say whether ARTICLE is a root(ish) article."
3867   (let ((level (gnus-summary-thread-level article))
3868         (refs (mail-header-references  (gnus-summary-article-header article)))
3869         particle)
3870     (cond
3871      ((null level) nil)
3872      ((zerop level) t)
3873      ((null refs) t)
3874      ((null (gnus-parent-id refs)) t)
3875      ((and (= 1 level)
3876            (null (setq particle (gnus-id-to-article
3877                                  (gnus-parent-id refs))))
3878            (null (gnus-summary-thread-level particle)))))))
3879
3880 (defun gnus-root-id (id)
3881   "Return the id of the root of the thread where ID appears."
3882   (let (last-id prev)
3883     (while (and id (setq prev (car (gnus-id-to-thread id))))
3884       (setq last-id id
3885             id (gnus-parent-id (mail-header-references prev))))
3886     last-id))
3887
3888 (defun gnus-articles-in-thread (thread)
3889   "Return the list of articles in THREAD."
3890   (cons (mail-header-number (car thread))
3891         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3892
3893 (defun gnus-remove-thread (id &optional dont-remove)
3894   "Remove the thread that has ID in it."
3895   (let (headers thread last-id)
3896     ;; First go up in this thread until we find the root.
3897     (setq last-id (gnus-root-id id)
3898           headers (message-flatten-list (gnus-id-to-thread last-id)))
3899     ;; We have now found the real root of this thread.  It might have
3900     ;; been gathered into some loose thread, so we have to search
3901     ;; through the threads to find the thread we wanted.
3902     (let ((threads gnus-newsgroup-threads)
3903           sub)
3904       (while threads
3905         (setq sub (car threads))
3906         (if (stringp (car sub))
3907             ;; This is a gathered thread, so we look at the roots
3908             ;; below it to find whether this article is in this
3909             ;; gathered root.
3910             (progn
3911               (setq sub (cdr sub))
3912               (while sub
3913                 (when (member (caar sub) headers)
3914                   (setq thread (car threads)
3915                         threads nil
3916                         sub nil))
3917                 (setq sub (cdr sub))))
3918           ;; It's an ordinary thread, so we check it.
3919           (when (eq (car sub) (car headers))
3920             (setq thread sub
3921                   threads nil)))
3922         (setq threads (cdr threads)))
3923       ;; If this article is in no thread, then it's a root.
3924       (if thread
3925           (unless dont-remove
3926             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3927         (setq thread (gnus-id-to-thread last-id)))
3928       (when thread
3929         (prog1
3930             thread                      ; We return this thread.
3931           (unless dont-remove
3932             (if (stringp (car thread))
3933                 (progn
3934                   ;; If we use dummy roots, then we have to remove the
3935                   ;; dummy root as well.
3936                   (when (eq gnus-summary-make-false-root 'dummy)
3937                     ;; We go to the dummy root by going to
3938                     ;; the first sub-"thread", and then one line up.
3939                     (gnus-summary-goto-article
3940                      (mail-header-number (caadr thread)))
3941                     (forward-line -1)
3942                     (gnus-delete-line)
3943                     (gnus-data-compute-positions))
3944                   (setq thread (cdr thread))
3945                   (while thread
3946                     (gnus-remove-thread-1 (car thread))
3947                     (setq thread (cdr thread))))
3948               (gnus-remove-thread-1 thread))))))))
3949
3950 (defun gnus-remove-thread-1 (thread)
3951   "Remove the thread THREAD recursively."
3952   (let ((number (mail-header-number (pop thread)))
3953         d)
3954     (setq thread (reverse thread))
3955     (while thread
3956       (gnus-remove-thread-1 (pop thread)))
3957     (when (setq d (gnus-data-find number))
3958       (goto-char (gnus-data-pos d))
3959       (gnus-summary-show-thread)
3960       (gnus-data-remove
3961        number
3962        (- (gnus-point-at-bol)
3963           (prog1
3964               (1+ (gnus-point-at-eol))
3965             (gnus-delete-line)))))))
3966
3967 (defun gnus-sort-threads-1 (threads func)
3968   (sort (mapcar (lambda (thread)
3969                   (cons (car thread)
3970                         (and (cdr thread)
3971                              (gnus-sort-threads-1 (cdr thread) func))))
3972                 threads) func))
3973
3974 (defun gnus-sort-threads (threads)
3975   "Sort THREADS."
3976   (if (not gnus-thread-sort-functions)
3977       threads
3978     (gnus-message 8 "Sorting threads...")
3979     (prog1
3980         (gnus-sort-threads-1
3981          threads
3982          (gnus-make-sort-function gnus-thread-sort-functions))
3983       (gnus-message 8 "Sorting threads...done"))))
3984
3985 (defun gnus-sort-articles (articles)
3986   "Sort ARTICLES."
3987   (when gnus-article-sort-functions
3988     (gnus-message 7 "Sorting articles...")
3989     (prog1
3990         (setq gnus-newsgroup-headers
3991               (sort articles (gnus-make-sort-function
3992                               gnus-article-sort-functions)))
3993       (gnus-message 7 "Sorting articles...done"))))
3994
3995 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3996 (defmacro gnus-thread-header (thread)
3997   "Return header of first article in THREAD.
3998 Note that THREAD must never, ever be anything else than a variable -
3999 using some other form will lead to serious barfage."
4000   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
4001   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
4002   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
4003         (vector thread) 2))
4004
4005 (defsubst gnus-article-sort-by-number (h1 h2)
4006   "Sort articles by article number."
4007   (< (mail-header-number h1)
4008      (mail-header-number h2)))
4009
4010 (defun gnus-thread-sort-by-number (h1 h2)
4011   "Sort threads by root article number."
4012   (gnus-article-sort-by-number
4013    (gnus-thread-header h1) (gnus-thread-header h2)))
4014
4015 (defsubst gnus-article-sort-by-lines (h1 h2)
4016   "Sort articles by article Lines header."
4017   (< (mail-header-lines h1)
4018      (mail-header-lines h2)))
4019
4020 (defun gnus-thread-sort-by-lines (h1 h2)
4021   "Sort threads by root article Lines header."
4022   (gnus-article-sort-by-lines
4023    (gnus-thread-header h1) (gnus-thread-header h2)))
4024
4025 (defsubst gnus-article-sort-by-chars (h1 h2)
4026   "Sort articles by octet length."
4027   (< (mail-header-chars h1)
4028      (mail-header-chars h2)))
4029
4030 (defun gnus-thread-sort-by-chars (h1 h2)
4031   "Sort threads by root article octet length."
4032   (gnus-article-sort-by-chars
4033    (gnus-thread-header h1) (gnus-thread-header h2)))
4034
4035 (defsubst gnus-article-sort-by-author (h1 h2)
4036   "Sort articles by root author."
4037   (string-lessp
4038    (let ((extract (funcall
4039                    gnus-extract-address-components
4040                    (mail-header-from h1))))
4041      (or (car extract) (cadr extract) ""))
4042    (let ((extract (funcall
4043                    gnus-extract-address-components
4044                    (mail-header-from h2))))
4045      (or (car extract) (cadr extract) ""))))
4046
4047 (defun gnus-thread-sort-by-author (h1 h2)
4048   "Sort threads by root author."
4049   (gnus-article-sort-by-author
4050    (gnus-thread-header h1)  (gnus-thread-header h2)))
4051
4052 (defsubst gnus-article-sort-by-subject (h1 h2)
4053   "Sort articles by root subject."
4054   (string-lessp
4055    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4056    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4057
4058 (defun gnus-thread-sort-by-subject (h1 h2)
4059   "Sort threads by root subject."
4060   (gnus-article-sort-by-subject
4061    (gnus-thread-header h1) (gnus-thread-header h2)))
4062
4063 (defsubst gnus-article-sort-by-date (h1 h2)
4064   "Sort articles by root article date."
4065   (time-less-p
4066    (gnus-date-get-time (mail-header-date h1))
4067    (gnus-date-get-time (mail-header-date h2))))
4068
4069 (defun gnus-thread-sort-by-date (h1 h2)
4070   "Sort threads by root article date."
4071   (gnus-article-sort-by-date
4072    (gnus-thread-header h1) (gnus-thread-header h2)))
4073
4074 (defsubst gnus-article-sort-by-score (h1 h2)
4075   "Sort articles by root article score.
4076 Unscored articles will be counted as having a score of zero."
4077   (> (or (cdr (assq (mail-header-number h1)
4078                     gnus-newsgroup-scored))
4079          gnus-summary-default-score 0)
4080      (or (cdr (assq (mail-header-number h2)
4081                     gnus-newsgroup-scored))
4082          gnus-summary-default-score 0)))
4083
4084 (defun gnus-thread-sort-by-score (h1 h2)
4085   "Sort threads by root article score."
4086   (gnus-article-sort-by-score
4087    (gnus-thread-header h1) (gnus-thread-header h2)))
4088
4089 (defun gnus-thread-sort-by-total-score (h1 h2)
4090   "Sort threads by the sum of all scores in the thread.
4091 Unscored articles will be counted as having a score of zero."
4092   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4093
4094 (defun gnus-thread-total-score (thread)
4095   ;; This function find the total score of THREAD.
4096   (cond ((null thread)
4097          0)
4098         ((consp thread)
4099          (if (stringp (car thread))
4100              (apply gnus-thread-score-function 0
4101                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4102            (gnus-thread-total-score-1 thread)))
4103         (t
4104          (gnus-thread-total-score-1 (list thread)))))
4105
4106 (defun gnus-thread-total-score-1 (root)
4107   ;; This function find the total score of the thread below ROOT.
4108   (setq root (car root))
4109   (apply gnus-thread-score-function
4110          (or (append
4111               (mapcar 'gnus-thread-total-score
4112                       (cdr (gnus-id-to-thread (mail-header-id root))))
4113               (when (> (mail-header-number root) 0)
4114                 (list (or (cdr (assq (mail-header-number root)
4115                                      gnus-newsgroup-scored))
4116                           gnus-summary-default-score 0))))
4117              (list gnus-summary-default-score)
4118              '(0))))
4119
4120 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4121 (defvar gnus-tmp-prev-subject nil)
4122 (defvar gnus-tmp-false-parent nil)
4123 (defvar gnus-tmp-root-expunged nil)
4124 (defvar gnus-tmp-dummy-line nil)
4125
4126 (eval-when-compile (defvar gnus-tmp-header))
4127 (defun gnus-extra-header (type &optional header)
4128   "Return the extra header of TYPE."
4129   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4130       ""))
4131
4132 (defvar gnus-tmp-thread-tree-header-string "")
4133
4134 (defvar gnus-sum-thread-tree-root "> "
4135   "With %B spec, used for the root of a thread.
4136 If nil, use subject instead.")
4137 (defvar gnus-sum-thread-tree-single-indent ""
4138   "With %B spec, used for a thread with just one message.
4139 If nil, use subject instead.")
4140 (defvar gnus-sum-thread-tree-vertical "| "
4141   "With %B spec, used for drawing a vertical line.")
4142 (defvar gnus-sum-thread-tree-indent "  "
4143   "With %B spec, used for indenting.")
4144 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4145   "With %B spec, used for a leaf with brothers.")
4146 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4147   "With %B spec, used for a leaf without brothers.")
4148
4149 (defun gnus-summary-prepare-threads (threads)
4150   "Prepare summary buffer from THREADS and indentation LEVEL.
4151 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4152 or a straight list of headers."
4153   (gnus-message 7 "Generating summary...")
4154
4155   (setq gnus-newsgroup-threads threads)
4156   (beginning-of-line)
4157
4158   (let ((gnus-tmp-level 0)
4159         (default-score (or gnus-summary-default-score 0))
4160         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4161         thread number subject stack state gnus-tmp-gathered beg-match
4162         new-roots gnus-tmp-new-adopts thread-end
4163         gnus-tmp-header gnus-tmp-unread
4164         gnus-tmp-replied gnus-tmp-subject-or-nil
4165         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4166         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4167         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4168         tree-stack)
4169
4170     (setq gnus-tmp-prev-subject nil)
4171
4172     (if (vectorp (car threads))
4173         ;; If this is a straight (sic) list of headers, then a
4174         ;; threaded summary display isn't required, so we just create
4175         ;; an unthreaded one.
4176         (gnus-summary-prepare-unthreaded threads)
4177
4178       ;; Do the threaded display.
4179
4180       (while (or threads stack gnus-tmp-new-adopts new-roots)
4181
4182         (if (and (= gnus-tmp-level 0)
4183                  (or (not stack)
4184                      (= (caar stack) 0))
4185                  (not gnus-tmp-false-parent)
4186                  (or gnus-tmp-new-adopts new-roots))
4187             (if gnus-tmp-new-adopts
4188                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4189                       thread (list (car gnus-tmp-new-adopts))
4190                       gnus-tmp-header (caar thread)
4191                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4192               (when new-roots
4193                 (setq thread (list (car new-roots))
4194                       gnus-tmp-header (caar thread)
4195                       new-roots (cdr new-roots))))
4196
4197           (if threads
4198               ;; If there are some threads, we do them before the
4199               ;; threads on the stack.
4200               (setq thread threads
4201                     gnus-tmp-header (caar thread))
4202             ;; There were no current threads, so we pop something off
4203             ;; the stack.
4204             (setq state (car stack)
4205                   gnus-tmp-level (car state)
4206                   tree-stack (cadr state)
4207                   thread (caddr state)
4208                   stack (cdr stack)
4209                   gnus-tmp-header (caar thread))))
4210
4211         (setq gnus-tmp-false-parent nil)
4212         (setq gnus-tmp-root-expunged nil)
4213         (setq thread-end nil)
4214
4215         (if (stringp gnus-tmp-header)
4216             ;; The header is a dummy root.
4217             (cond
4218              ((eq gnus-summary-make-false-root 'adopt)
4219               ;; We let the first article adopt the rest.
4220               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4221                                                (cddar thread)))
4222               (setq gnus-tmp-gathered
4223                     (nconc (mapcar
4224                             (lambda (h) (mail-header-number (car h)))
4225                             (cddar thread))
4226                            gnus-tmp-gathered))
4227               (setq thread (cons (list (caar thread)
4228                                        (cadar thread))
4229                                  (cdr thread)))
4230               (setq gnus-tmp-level -1
4231                     gnus-tmp-false-parent t))
4232              ((eq gnus-summary-make-false-root 'empty)
4233               ;; We print adopted articles with empty subject fields.
4234               (setq gnus-tmp-gathered
4235                     (nconc (mapcar
4236                             (lambda (h) (mail-header-number (car h)))
4237                             (cddar thread))
4238                            gnus-tmp-gathered))
4239               (setq gnus-tmp-level -1))
4240              ((eq gnus-summary-make-false-root 'dummy)
4241               ;; We remember that we probably want to output a dummy
4242               ;; root.
4243               (setq gnus-tmp-dummy-line gnus-tmp-header)
4244               (setq gnus-tmp-prev-subject gnus-tmp-header))
4245              (t
4246               ;; We do not make a root for the gathered
4247               ;; sub-threads at all.
4248               (setq gnus-tmp-level -1)))
4249
4250           (setq number (mail-header-number gnus-tmp-header)
4251                 subject (mail-header-subject gnus-tmp-header))
4252
4253           (cond
4254            ;; If the thread has changed subject, we might want to make
4255            ;; this subthread into a root.
4256            ((and (null gnus-thread-ignore-subject)
4257                  (not (zerop gnus-tmp-level))
4258                  gnus-tmp-prev-subject
4259                  (not (inline
4260                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4261             (setq new-roots (nconc new-roots (list (car thread)))
4262                   thread-end t
4263                   gnus-tmp-header nil))
4264            ;; If the article lies outside the current limit,
4265            ;; then we do not display it.
4266            ((not (memq number gnus-newsgroup-limit))
4267             (setq gnus-tmp-gathered
4268                   (nconc (mapcar
4269                           (lambda (h) (mail-header-number (car h)))
4270                           (cdar thread))
4271                          gnus-tmp-gathered))
4272             (setq gnus-tmp-new-adopts (if (cdar thread)
4273                                           (append gnus-tmp-new-adopts
4274                                                   (cdar thread))
4275                                         gnus-tmp-new-adopts)
4276                   thread-end t
4277                   gnus-tmp-header nil)
4278             (when (zerop gnus-tmp-level)
4279               (setq gnus-tmp-root-expunged t)))
4280            ;; Perhaps this article is to be marked as read?
4281            ((and gnus-summary-mark-below
4282                  (< (or (cdr (assq number gnus-newsgroup-scored))
4283                         default-score)
4284                     gnus-summary-mark-below)
4285                  ;; Don't touch sparse articles.
4286                  (not (gnus-summary-article-sparse-p number))
4287                  (not (gnus-summary-article-ancient-p number)))
4288             (setq gnus-newsgroup-unreads
4289                   (delq number gnus-newsgroup-unreads))
4290             (if gnus-newsgroup-auto-expire
4291                 (push number gnus-newsgroup-expirable)
4292               (push (cons number gnus-low-score-mark)
4293                     gnus-newsgroup-reads))))
4294
4295           (when gnus-tmp-header
4296             ;; We may have an old dummy line to output before this
4297             ;; article.
4298             (when (and gnus-tmp-dummy-line
4299                        (gnus-subject-equal
4300                         gnus-tmp-dummy-line
4301                         (mail-header-subject gnus-tmp-header)))
4302               (gnus-summary-insert-dummy-line
4303                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4304               (setq gnus-tmp-dummy-line nil))
4305
4306             ;; Compute the mark.
4307             (setq gnus-tmp-unread (gnus-article-mark number))
4308
4309             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4310                                   gnus-tmp-header gnus-tmp-level)
4311                   gnus-newsgroup-data)
4312
4313             ;; Actually insert the line.
4314             (setq
4315              gnus-tmp-subject-or-nil
4316              (cond
4317               ((and gnus-thread-ignore-subject
4318                     gnus-tmp-prev-subject
4319                     (not (inline (gnus-subject-equal
4320                                   gnus-tmp-prev-subject subject))))
4321                subject)
4322               ((zerop gnus-tmp-level)
4323                (if (and (eq gnus-summary-make-false-root 'empty)
4324                         (memq number gnus-tmp-gathered)
4325                         gnus-tmp-prev-subject
4326                         (inline (gnus-subject-equal
4327                                  gnus-tmp-prev-subject subject)))
4328                    gnus-summary-same-subject
4329                  subject))
4330               (t gnus-summary-same-subject)))
4331             (if (and (eq gnus-summary-make-false-root 'adopt)
4332                      (= gnus-tmp-level 1)
4333                      (memq number gnus-tmp-gathered))
4334                 (setq gnus-tmp-opening-bracket ?\<
4335                       gnus-tmp-closing-bracket ?\>)
4336               (setq gnus-tmp-opening-bracket ?\[
4337                     gnus-tmp-closing-bracket ?\]))
4338             (setq
4339              gnus-tmp-indentation
4340              (aref gnus-thread-indent-array gnus-tmp-level)
4341              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4342              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4343                                 gnus-summary-default-score 0)
4344              gnus-tmp-score-char
4345              (if (or (null gnus-summary-default-score)
4346                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4347                          gnus-summary-zcore-fuzz))
4348                  ?                      ;Whitespace
4349                (if (< gnus-tmp-score gnus-summary-default-score)
4350                    gnus-score-below-mark gnus-score-over-mark))
4351              gnus-tmp-replied
4352              (cond ((memq number gnus-newsgroup-processable)
4353                     gnus-process-mark)
4354                    ((memq number gnus-newsgroup-cached)
4355                     gnus-cached-mark)
4356                    ((memq number gnus-newsgroup-replied)
4357                     gnus-replied-mark)
4358                    ((memq number gnus-newsgroup-forwarded)
4359                     gnus-forwarded-mark)
4360                    ((memq number gnus-newsgroup-saved)
4361                     gnus-saved-mark)
4362                    ((memq number gnus-newsgroup-recent)
4363                     gnus-recent-mark)
4364                    ((memq number gnus-newsgroup-unseen)
4365                     gnus-unseen-mark)
4366                    (t gnus-no-mark))
4367              gnus-tmp-from (mail-header-from gnus-tmp-header)
4368              gnus-tmp-name
4369              (cond
4370               ((string-match "<[^>]+> *$" gnus-tmp-from)
4371                (setq beg-match (match-beginning 0))
4372                (or (and (string-match "^\".+\"" gnus-tmp-from)
4373                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4374                    (substring gnus-tmp-from 0 beg-match)))
4375               ((string-match "(.+)" gnus-tmp-from)
4376                (substring gnus-tmp-from
4377                           (1+ (match-beginning 0)) (1- (match-end 0))))
4378               (t gnus-tmp-from))
4379              gnus-tmp-thread-tree-header-string
4380              (cond 
4381               ((not gnus-show-threads) "")
4382               ((zerop gnus-tmp-level)
4383                (if (cdar thread) 
4384                    (or gnus-sum-thread-tree-root subject)
4385                  (or gnus-sum-thread-tree-single-indent subject)))
4386               (t
4387                (concat (apply 'concat
4388                               (mapcar (lambda (item) 
4389                                         (if (= item 1) 
4390                                             gnus-sum-thread-tree-vertical
4391                                           gnus-sum-thread-tree-indent))
4392                                       (cdr (reverse tree-stack))))
4393                        (if (nth 1 thread) 
4394                            gnus-sum-thread-tree-leaf-with-other
4395                          gnus-sum-thread-tree-single-leaf)))))
4396             (when (string= gnus-tmp-name "")
4397               (setq gnus-tmp-name gnus-tmp-from))
4398             (unless (numberp gnus-tmp-lines)
4399               (setq gnus-tmp-lines -1))
4400             (if (= gnus-tmp-lines -1)
4401                 (setq gnus-tmp-lines "?")
4402               (setq gnus-tmp-lines (number-to-string gnus-tmp-lines)))
4403             (gnus-put-text-property
4404              (point)
4405              (progn (eval gnus-summary-line-format-spec) (point))
4406              'gnus-number number)
4407             (when gnus-visual-p
4408               (forward-line -1)
4409               (gnus-run-hooks 'gnus-summary-update-hook)
4410               (forward-line 1))
4411
4412             (setq gnus-tmp-prev-subject subject)))
4413
4414         (when (nth 1 thread)
4415           (push (list (max 0 gnus-tmp-level)
4416                       (copy-list tree-stack)
4417                       (nthcdr 1 thread))
4418                 stack))
4419         (push (if (nth 1 thread) 1 0) tree-stack)
4420         (incf gnus-tmp-level)
4421         (setq threads (if thread-end nil (cdar thread)))
4422         (unless threads
4423           (setq gnus-tmp-level 0)))))
4424   (gnus-message 7 "Generating summary...done"))
4425
4426 (defun gnus-summary-prepare-unthreaded (headers)
4427   "Generate an unthreaded summary buffer based on HEADERS."
4428   (let (header number mark)
4429
4430     (beginning-of-line)
4431
4432     (while headers
4433       ;; We may have to root out some bad articles...
4434       (when (memq (setq number (mail-header-number
4435                                 (setq header (pop headers))))
4436                   gnus-newsgroup-limit)
4437         ;; Mark article as read when it has a low score.
4438         (when (and gnus-summary-mark-below
4439                    (< (or (cdr (assq number gnus-newsgroup-scored))
4440                           gnus-summary-default-score 0)
4441                       gnus-summary-mark-below)
4442                    (not (gnus-summary-article-ancient-p number)))
4443           (setq gnus-newsgroup-unreads
4444                 (delq number gnus-newsgroup-unreads))
4445           (if gnus-newsgroup-auto-expire
4446               (push number gnus-newsgroup-expirable)
4447             (push (cons number gnus-low-score-mark)
4448                   gnus-newsgroup-reads)))
4449
4450         (setq mark (gnus-article-mark number))
4451         (push (gnus-data-make number mark (1+ (point)) header 0)
4452               gnus-newsgroup-data)
4453         (gnus-summary-insert-line
4454          header 0 number
4455          mark (memq number gnus-newsgroup-replied)
4456          (memq number gnus-newsgroup-expirable)
4457          (mail-header-subject header) nil
4458          (cdr (assq number gnus-newsgroup-scored))
4459          (memq number gnus-newsgroup-processable))))))
4460
4461 (defun gnus-summary-remove-list-identifiers ()
4462   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4463   (let ((regexp (if (consp gnus-list-identifiers)
4464                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4465                   gnus-list-identifiers))
4466         changed subject)
4467     (when regexp
4468       (dolist (header gnus-newsgroup-headers)
4469         (setq subject (mail-header-subject header)
4470               changed nil)
4471         (while (string-match
4472                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4473                 subject)
4474           (setq subject
4475                 (concat (substring subject 0 (match-beginning 2))
4476                         (substring subject (match-end 0)))
4477                 changed t))
4478         (when (and changed
4479                    (string-match
4480                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4481           (setq subject
4482                 (concat (substring subject 0 (match-beginning 1))
4483                         (substring subject (match-end 1)))))
4484         (when changed
4485           (mail-header-set-subject header subject))))))
4486
4487 (defun gnus-fetch-headers (articles)
4488   "Fetch headers of ARTICLES."
4489   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4490     (gnus-message 5 "Fetching headers for %s..." name)
4491     (prog1
4492         (if (eq 'nov
4493                 (setq gnus-headers-retrieved-by
4494                       (gnus-retrieve-headers
4495                        articles gnus-newsgroup-name
4496                        ;; We might want to fetch old headers, but
4497                        ;; not if there is only 1 article.
4498                        (and (or (and
4499                                  (not (eq gnus-fetch-old-headers 'some))
4500                                  (not (numberp gnus-fetch-old-headers)))
4501                                 (> (length articles) 1))
4502                             gnus-fetch-old-headers))))
4503             (gnus-get-newsgroup-headers-xover
4504              articles nil nil gnus-newsgroup-name t)
4505           (gnus-get-newsgroup-headers))
4506       (gnus-message 5 "Fetching headers for %s...done" name))))
4507
4508 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4509   "Select newsgroup GROUP.
4510 If READ-ALL is non-nil, all articles in the group are selected.
4511 If SELECT-ARTICLES, only select those articles from GROUP."
4512   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4513          ;;!!! Dirty hack; should be removed.
4514          (gnus-summary-ignore-duplicates
4515           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4516               t
4517             gnus-summary-ignore-duplicates))
4518          (info (nth 2 entry))
4519          articles fetched-articles cached)
4520
4521     (unless (gnus-check-server
4522              (set (make-local-variable 'gnus-current-select-method)
4523                   (gnus-find-method-for-group group)))
4524       (error "Couldn't open server"))
4525
4526     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4527         (gnus-activate-group group)     ; Or we can activate it...
4528         (progn                          ; Or we bug out.
4529           (when (equal major-mode 'gnus-summary-mode)
4530             (kill-buffer (current-buffer)))
4531           (error "Couldn't activate group %s: %s"
4532                  group (gnus-status-message group))))
4533
4534     (unless (gnus-request-group group t)
4535       (when (equal major-mode 'gnus-summary-mode)
4536         (kill-buffer (current-buffer)))
4537       (error "Couldn't request group %s: %s"
4538              group (gnus-status-message group)))
4539
4540     (setq gnus-newsgroup-name group
4541           gnus-newsgroup-unselected nil
4542           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4543     
4544     (let ((display (gnus-group-find-parameter group 'display)))
4545       (setq gnus-newsgroup-display
4546             (cond
4547              ((not (zerop (or (car-safe read-all) 0)))
4548               ;; The user entered the group with C-u SPC/RET, let's show
4549               ;; all articles.
4550               'gnus-not-ignore)
4551              ((eq display 'all)
4552               'gnus-not-ignore)
4553              ((arrayp display)
4554               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4555              ((numberp display)
4556               ;; The following is probably the "correct" solution, but
4557               ;; it makes Gnus fetch all headers and then limit the
4558               ;; articles (which is slow), so instead we hack the
4559               ;; select-articles parameter instead. -- Simon Josefsson
4560               ;; <jas@kth.se>
4561               ;;
4562               ;; (gnus-byte-compile
4563               ;;  `(lambda () (> number ,(- (cdr (gnus-active group))
4564               ;;                         display)))))
4565               (setq select-articles
4566                     (gnus-uncompress-range
4567                      (cons (let ((tmp (- (cdr (gnus-active group)) display)))
4568                              (if (> tmp 0)
4569                                  tmp
4570                                1))
4571                            (cdr (gnus-active group)))))
4572               nil)
4573              (t
4574               nil))))
4575       
4576     (gnus-summary-setup-default-charset)
4577
4578     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4579     (when (gnus-virtual-group-p group)
4580       (setq cached gnus-newsgroup-cached))
4581
4582     (setq gnus-newsgroup-unreads
4583           (gnus-set-difference
4584            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4585            gnus-newsgroup-dormant))
4586
4587     (setq gnus-newsgroup-processable nil)
4588
4589     (gnus-update-read-articles group gnus-newsgroup-unreads)
4590
4591     ;; Adjust and set lists of article marks.
4592     (when info
4593       (gnus-adjust-marked-articles info))
4594     
4595     (if (setq articles select-articles)
4596         (setq gnus-newsgroup-unselected
4597               (gnus-sorted-intersection
4598                gnus-newsgroup-unreads
4599                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4600       (setq articles (gnus-articles-to-read group read-all)))
4601
4602     (cond
4603      ((null articles)
4604       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4605       'quit)
4606      ((eq articles 0) nil)
4607      (t
4608       ;; Init the dependencies hash table.
4609       (setq gnus-newsgroup-dependencies
4610             (gnus-make-hashtable (length articles)))
4611       (gnus-set-global-variables)
4612       ;; Retrieve the headers and read them in.
4613       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4614
4615       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4616       (when cached
4617         (setq gnus-newsgroup-cached cached))
4618
4619       ;; Suppress duplicates?
4620       (when gnus-suppress-duplicates
4621         (gnus-dup-suppress-articles))
4622
4623       ;; Set the initial limit.
4624       (setq gnus-newsgroup-limit (copy-sequence articles))
4625       ;; Remove canceled articles from the list of unread articles.
4626       (setq fetched-articles
4627             (mapcar (lambda (headers) (mail-header-number headers))
4628                     gnus-newsgroup-headers))
4629       (setq gnus-newsgroup-articles fetched-articles)
4630       (setq gnus-newsgroup-unreads
4631             (gnus-set-sorted-intersection
4632              gnus-newsgroup-unreads fetched-articles))
4633
4634       (let ((marks (assq 'seen (gnus-info-marks info))))
4635         ;; The `seen' marks are treated specially.
4636         (when (setq gnus-newsgroup-seen (cdr marks))
4637           (dolist (article gnus-newsgroup-articles)
4638             (unless (gnus-member-of-range
4639                      article gnus-newsgroup-seen)
4640               (push article gnus-newsgroup-unseen)))))
4641
4642       ;; Removed marked articles that do not exist.
4643       (gnus-update-missing-marks
4644        (gnus-sorted-complement fetched-articles articles))
4645       ;; We might want to build some more threads first.
4646       (when (and gnus-fetch-old-headers
4647                  (eq gnus-headers-retrieved-by 'nov))
4648         (if (eq gnus-fetch-old-headers 'invisible)
4649             (gnus-build-all-threads)
4650           (gnus-build-old-threads)))
4651       ;; Let the Gnus agent mark articles as read.
4652       (when gnus-agent
4653         (gnus-agent-get-undownloaded-list))
4654       ;; Remove list identifiers from subject
4655       (when gnus-list-identifiers
4656         (gnus-summary-remove-list-identifiers))
4657       ;; Check whether auto-expire is to be done in this group.
4658       (setq gnus-newsgroup-auto-expire
4659             (gnus-group-auto-expirable-p group))
4660       ;; Set up the article buffer now, if necessary.
4661       (unless gnus-single-article-buffer
4662         (gnus-article-setup-buffer))
4663       ;; First and last article in this newsgroup.
4664       (when gnus-newsgroup-headers
4665         (setq gnus-newsgroup-begin
4666               (mail-header-number (car gnus-newsgroup-headers))
4667               gnus-newsgroup-end
4668               (mail-header-number
4669                (gnus-last-element gnus-newsgroup-headers))))
4670       ;; GROUP is successfully selected.
4671       (or gnus-newsgroup-headers t)))))
4672
4673 (defun gnus-summary-display-make-predicate (display)
4674   (require 'gnus-agent)
4675   (when (= (length display) 1)
4676     (setq display (car display)))
4677   (unless gnus-summary-display-cache
4678     (dolist (elem (append (list (cons 'read 'read)
4679                                 (cons 'unseen 'unseen))
4680                           gnus-article-mark-lists))
4681       (push (cons (cdr elem)
4682                   (gnus-byte-compile
4683                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4684             gnus-summary-display-cache)))
4685   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4686     (gnus-get-predicate display)))
4687
4688 ;; Uses the dynamically bound `number' variable.
4689 (defvar number)
4690 (defun gnus-article-marked-p (type &optional article)
4691   (let ((article (or article number)))
4692     (cond
4693      ((eq type 'tick)
4694       (memq article gnus-newsgroup-marked))
4695      ((eq type 'unsend)
4696       (memq article gnus-newsgroup-unsendable))
4697      ((eq type 'undownload)
4698       (memq article gnus-newsgroup-undownloaded))
4699      ((eq type 'download)
4700       (memq article gnus-newsgroup-downloadable))
4701      ((eq type 'unread)
4702       (memq article gnus-newsgroup-unreads))
4703      ((eq type 'read)
4704       (memq article gnus-newsgroup-reads))
4705      ((eq type 'dormant)
4706       (memq article gnus-newsgroup-dormant) )
4707      ((eq type 'expire)
4708       (memq article gnus-newsgroup-expirable))
4709      ((eq type 'reply)
4710       (memq article gnus-newsgroup-replied))
4711      ((eq type 'killed)
4712       (memq article gnus-newsgroup-killed))
4713      ((eq type 'bookmark)
4714       (assq article gnus-newsgroup-bookmarks))
4715      ((eq type 'score)
4716       (assq article gnus-newsgroup-scored))
4717      ((eq type 'save)
4718       (memq article gnus-newsgroup-saved))
4719      ((eq type 'cache)
4720       (memq article gnus-newsgroup-cached))
4721      ((eq type 'forward)
4722       (memq article gnus-newsgroup-forwarded))
4723      ((eq type 'seen)
4724       (not (memq article gnus-newsgroup-unseen)))
4725      ((eq type 'recent)
4726       (memq article gnus-newsgroup-recent))
4727      (t t))))
4728
4729 (defun gnus-articles-to-read (group &optional read-all)
4730   "Find out what articles the user wants to read."
4731   (let* ((articles
4732           ;; Select all articles if `read-all' is non-nil, or if there
4733           ;; are no unread articles.
4734           (if (or read-all
4735                   (and (zerop (length gnus-newsgroup-marked))
4736                        (zerop (length gnus-newsgroup-unreads)))
4737                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4738               ;; We want to select the headers for all the articles in
4739               ;; the group, so we select either all the active
4740               ;; articles in the group, or (if that's nil), the
4741               ;; articles in the cache.
4742               (or
4743                (gnus-uncompress-range (gnus-active group))
4744                (gnus-cache-articles-in-group group))
4745             ;; Select only the "normal" subset of articles.
4746             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4747                           (copy-sequence gnus-newsgroup-unreads))
4748                   '<)))
4749          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4750          (scored (length scored-list))
4751          (number (length articles))
4752          (marked (+ (length gnus-newsgroup-marked)
4753                     (length gnus-newsgroup-dormant)))
4754          (select
4755           (cond
4756            ((numberp read-all)
4757             read-all)
4758            (t
4759             (condition-case ()
4760                 (cond
4761                  ((and (or (<= scored marked) (= scored number))
4762                        (numberp gnus-large-newsgroup)
4763                        (> number gnus-large-newsgroup))
4764                   (let ((input
4765                          (read-string
4766                           (format
4767                            "How many articles from %s (default %d): "
4768                            (gnus-limit-string
4769                             (gnus-group-decoded-name gnus-newsgroup-name)
4770                             35)
4771                            number))))
4772                     (if (string-match "^[ \t]*$" input) number input)))
4773                  ((and (> scored marked) (< scored number)
4774                        (> (- scored number) 20))
4775                   (let ((input
4776                          (read-string
4777                           (format "%s %s (%d scored, %d total): "
4778                                   "How many articles from"
4779                                   (gnus-group-decoded-name group)
4780                                   scored number))))
4781                     (if (string-match "^[ \t]*$" input)
4782                         number input)))
4783                  (t number))
4784               (quit
4785                (message "Quit getting the articles to read")
4786                nil))))))
4787     (setq select (if (stringp select) (string-to-number select) select))
4788     (if (or (null select) (zerop select))
4789         select
4790       (if (and (not (zerop scored)) (<= (abs select) scored))
4791           (progn
4792             (setq articles (sort scored-list '<))
4793             (setq number (length articles)))
4794         (setq articles (copy-sequence articles)))
4795
4796       (when (< (abs select) number)
4797         (if (< select 0)
4798             ;; Select the N oldest articles.
4799             (setcdr (nthcdr (1- (abs select)) articles) nil)
4800           ;; Select the N most recent articles.
4801           (setq articles (nthcdr (- number select) articles))))
4802       (setq gnus-newsgroup-unselected
4803             (gnus-sorted-intersection
4804              gnus-newsgroup-unreads
4805              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4806       (when gnus-alter-articles-to-read-function
4807         (setq gnus-newsgroup-unreads
4808               (sort
4809                (funcall gnus-alter-articles-to-read-function
4810                         gnus-newsgroup-name gnus-newsgroup-unreads)
4811                '<)))
4812       articles)))
4813
4814 (defun gnus-killed-articles (killed articles)
4815   (let (out)
4816     (while articles
4817       (when (inline (gnus-member-of-range (car articles) killed))
4818         (push (car articles) out))
4819       (setq articles (cdr articles)))
4820     out))
4821
4822 (defun gnus-uncompress-marks (marks)
4823   "Uncompress the mark ranges in MARKS."
4824   (let ((uncompressed '(score bookmark))
4825         out)
4826     (while marks
4827       (if (memq (caar marks) uncompressed)
4828           (push (car marks) out)
4829         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4830       (setq marks (cdr marks)))
4831     out))
4832
4833 (defun gnus-article-mark-to-type (mark)
4834   "Return the type of MARK."
4835   (or (cadr (assq mark gnus-article-special-mark-lists))
4836       'list))
4837
4838 (defun gnus-article-unpropagatable-p (mark)
4839   "Return whether MARK should be propagated to backend."
4840   (memq mark gnus-article-unpropagated-mark-lists))
4841
4842 (defun gnus-adjust-marked-articles (info)
4843   "Set all article lists and remove all marks that are no longer valid."
4844   (let* ((marked-lists (gnus-info-marks info))
4845          (active (gnus-active (gnus-info-group info)))
4846          (min (car active))
4847          (max (cdr active))
4848          (types gnus-article-mark-lists)
4849          marks var articles article mark mark-type)
4850
4851     (dolist (marks marked-lists)
4852       (setq mark (car marks)
4853             mark-type (gnus-article-mark-to-type mark)
4854             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4855
4856       ;; We set the variable according to the type of the marks list,
4857       ;; and then adjust the marks to a subset of the active articles.
4858       (cond
4859        ;; Adjust "simple" lists.
4860        ((eq mark-type 'list)
4861         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4862         (when (memq mark '(tick dormant expire reply save))
4863           (while articles
4864             (when (or (< (setq article (pop articles)) min) (> article max))
4865               (set var (delq article (symbol-value var)))))))
4866        ;; Adjust assocs.
4867        ((eq mark-type 'tuple)
4868         (set var (setq articles (cdr marks)))
4869         (when (not (listp (cdr (symbol-value var))))
4870           (set var (list (symbol-value var))))
4871         (when (not (listp (cdr articles)))
4872           (setq articles (list articles)))
4873         (while articles
4874           (when (or (not (consp (setq article (pop articles))))
4875                     (< (car article) min)
4876                     (> (car article) max))
4877             (set var (delq article (symbol-value var))))))
4878        ((eq mark-type 'range)
4879         (cond
4880          ((eq mark 'seen))))))))
4881
4882 (defun gnus-update-missing-marks (missing)
4883   "Go through the list of MISSING articles and remove them from the mark lists."
4884   (when missing
4885     (let (var m)
4886       ;; Go through all types.
4887       (dolist (elem gnus-article-mark-lists)
4888         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4889           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4890           (when (symbol-value var)
4891             ;; This list has articles.  So we delete all missing
4892             ;; articles from it.
4893             (setq m missing)
4894             (while m
4895               (set var (delq (pop m) (symbol-value var))))))))))
4896
4897 (defun gnus-update-marks ()
4898   "Enter the various lists of marked articles into the newsgroup info list."
4899   (let ((types gnus-article-mark-lists)
4900         (info (gnus-get-info gnus-newsgroup-name))
4901         type list newmarked symbol delta-marks)
4902     (when info
4903       ;; Add all marks lists to the list of marks lists.
4904       (while (setq type (pop types))
4905         (setq list (symbol-value
4906                     (setq symbol
4907                           (intern (format "gnus-newsgroup-%s" (car type))))))
4908
4909         (when list
4910           ;; Get rid of the entries of the articles that have the
4911           ;; default score.
4912           (when (and (eq (cdr type) 'score)
4913                      gnus-save-score
4914                      list)
4915             (let* ((arts list)
4916                    (prev (cons nil list))
4917                    (all prev))
4918               (while arts
4919                 (if (or (not (consp (car arts)))
4920                         (= (cdar arts) gnus-summary-default-score))
4921                     (setcdr prev (cdr arts))
4922                   (setq prev arts))
4923                 (setq arts (cdr arts)))
4924               (setq list (cdr all)))))
4925
4926         (when (eq (cdr type) 'seen)
4927           (setq list 
4928                 (if list
4929                     (gnus-add-to-range list gnus-newsgroup-unseen)
4930                   (gnus-compress-sequence gnus-newsgroup-articles))))
4931
4932         (when (eq (gnus-article-mark-to-type (cdr type)) 'list)
4933           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4934
4935         (when (and (gnus-check-backend-function
4936                     'request-set-mark gnus-newsgroup-name)
4937                    (not (gnus-article-unpropagatable-p (cdr type))))
4938           (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4939                  (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4940                  (add (gnus-remove-from-range
4941                        (gnus-copy-sequence list) old)))
4942             (when add
4943               (push (list add 'add (list (cdr type))) delta-marks))
4944             (when del
4945               (push (list del 'del (list (cdr type))) delta-marks))))
4946
4947         (when list
4948           (push (cons (cdr type) list) newmarked)))
4949
4950       (when delta-marks
4951         (unless (gnus-check-group gnus-newsgroup-name)
4952           (error "Can't open server for %s" gnus-newsgroup-name))
4953         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4954
4955       ;; Enter these new marks into the info of the group.
4956       (if (nthcdr 3 info)
4957           (setcar (nthcdr 3 info) newmarked)
4958         ;; Add the marks lists to the end of the info.
4959         (when newmarked
4960           (setcdr (nthcdr 2 info) (list newmarked))))
4961
4962       ;; Cut off the end of the info if there's nothing else there.
4963       (let ((i 5))
4964         (while (and (> i 2)
4965                     (not (nth i info)))
4966           (when (nthcdr (decf i) info)
4967             (setcdr (nthcdr i info) nil)))))))
4968
4969 (defun gnus-set-mode-line (where)
4970   "Set the mode line of the article or summary buffers.
4971 If WHERE is `summary', the summary mode line format will be used."
4972   ;; Is this mode line one we keep updated?
4973   (when (and (memq where gnus-updated-mode-lines)
4974              (symbol-value
4975               (intern (format "gnus-%s-mode-line-format-spec" where))))
4976     (let (mode-string)
4977       (save-excursion
4978         ;; We evaluate this in the summary buffer since these
4979         ;; variables are buffer-local to that buffer.
4980         (set-buffer gnus-summary-buffer)
4981        ;; We bind all these variables that are used in the `eval' form
4982         ;; below.
4983         (let* ((mformat (symbol-value
4984                          (intern
4985                           (format "gnus-%s-mode-line-format-spec" where))))
4986                (gnus-tmp-group-name (gnus-group-decoded-name
4987                                      gnus-newsgroup-name))
4988                (gnus-tmp-article-number (or gnus-current-article 0))
4989                (gnus-tmp-unread gnus-newsgroup-unreads)
4990                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4991                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4992                (gnus-tmp-unread-and-unselected
4993                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4994                             (zerop gnus-tmp-unselected))
4995                        "")
4996                       ((zerop gnus-tmp-unselected)
4997                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4998                       (t (format "{%d(+%d) more}"
4999                                  gnus-tmp-unread-and-unticked
5000                                  gnus-tmp-unselected))))
5001                (gnus-tmp-subject
5002                 (if (and gnus-current-headers
5003                          (vectorp gnus-current-headers))
5004                     (gnus-mode-string-quote
5005                      (mail-header-subject gnus-current-headers))
5006                   ""))
5007                bufname-length max-len
5008                gnus-tmp-header) ;; passed as argument to any user-format-funcs
5009           (setq mode-string (eval mformat))
5010           (setq bufname-length (if (string-match "%b" mode-string)
5011                                    (- (length
5012                                        (buffer-name
5013                                         (if (eq where 'summary)
5014                                             nil
5015                                           (get-buffer gnus-article-buffer))))
5016                                       2)
5017                                  0))
5018           (setq max-len (max 4 (if gnus-mode-non-string-length
5019                                    (- (window-width)
5020                                       gnus-mode-non-string-length
5021                                       bufname-length)
5022                                  (length mode-string))))
5023           ;; We might have to chop a bit of the string off...
5024           (when (> (length mode-string) max-len)
5025             (setq mode-string
5026                   (concat (truncate-string-to-width mode-string (- max-len 3))
5027                           "...")))
5028           ;; Pad the mode string a bit.
5029           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5030       ;; Update the mode line.
5031       (setq mode-line-buffer-identification
5032             (gnus-mode-line-buffer-identification (list mode-string)))
5033       (set-buffer-modified-p t))))
5034
5035 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5036   "Go through the HEADERS list and add all Xrefs to a hash table.
5037 The resulting hash table is returned, or nil if no Xrefs were found."
5038   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5039          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5040          (xref-hashtb (gnus-make-hashtable))
5041          start group entry number xrefs header)
5042     (while headers
5043       (setq header (pop headers))
5044       (when (and (setq xrefs (mail-header-xref header))
5045                  (not (memq (setq number (mail-header-number header))
5046                             unreads)))
5047         (setq start 0)
5048         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5049           (setq start (match-end 0))
5050           (setq group (if prefix
5051                           (concat prefix (substring xrefs (match-beginning 1)
5052                                                     (match-end 1)))
5053                         (substring xrefs (match-beginning 1) (match-end 1))))
5054           (setq number
5055                 (string-to-int (substring xrefs (match-beginning 2)
5056                                           (match-end 2))))
5057           (if (setq entry (gnus-gethash group xref-hashtb))
5058               (setcdr entry (cons number (cdr entry)))
5059             (gnus-sethash group (cons number nil) xref-hashtb)))))
5060     (and start xref-hashtb)))
5061
5062 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5063   "Look through all the headers and mark the Xrefs as read."
5064   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5065         name entry info xref-hashtb idlist method nth4)
5066     (save-excursion
5067       (set-buffer gnus-group-buffer)
5068       (when (setq xref-hashtb
5069                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5070         (mapatoms
5071          (lambda (group)
5072            (unless (string= from-newsgroup (setq name (symbol-name group)))
5073              (setq idlist (symbol-value group))
5074              ;; Dead groups are not updated.
5075              (and (prog1
5076                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5077                             info (nth 2 entry))
5078                     (when (stringp (setq nth4 (gnus-info-method info)))
5079                       (setq nth4 (gnus-server-to-method nth4))))
5080                   ;; Only do the xrefs if the group has the same
5081                   ;; select method as the group we have just read.
5082                   (or (gnus-methods-equal-p
5083                        nth4 (gnus-find-method-for-group from-newsgroup))
5084                       virtual
5085                       (equal nth4 (setq method (gnus-find-method-for-group
5086                                                 from-newsgroup)))
5087                       (and (equal (car nth4) (car method))
5088                            (equal (nth 1 nth4) (nth 1 method))))
5089                   gnus-use-cross-reference
5090                   (or (not (eq gnus-use-cross-reference t))
5091                       virtual
5092                       ;; Only do cross-references on subscribed
5093                       ;; groups, if that is what is wanted.
5094                       (<= (gnus-info-level info) gnus-level-subscribed))
5095                   (gnus-group-make-articles-read name idlist))))
5096          xref-hashtb)))))
5097
5098 (defun gnus-compute-read-articles (group articles)
5099   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5100          (info (nth 2 entry))
5101          (active (gnus-active group))
5102          ninfo)
5103     (when entry
5104       ;; First peel off all invalid article numbers.
5105       (when active
5106         (let ((ids articles)
5107               id first)
5108           (while (setq id (pop ids))
5109             (when (and first (> id (cdr active)))
5110               ;; We'll end up in this situation in one particular
5111               ;; obscure situation.  If you re-scan a group and get
5112               ;; a new article that is cross-posted to a different
5113               ;; group that has not been re-scanned, you might get
5114               ;; crossposted article that has a higher number than
5115               ;; Gnus believes possible.  So we re-activate this
5116               ;; group as well.  This might mean doing the
5117               ;; crossposting thingy will *increase* the number
5118               ;; of articles in some groups.  Tsk, tsk.
5119               (setq active (or (gnus-activate-group group) active)))
5120             (when (or (> id (cdr active))
5121                       (< id (car active)))
5122               (setq articles (delq id articles))))))
5123       ;; If the read list is nil, we init it.
5124       (if (and active
5125                (null (gnus-info-read info))
5126                (> (car active) 1))
5127           (setq ninfo (cons 1 (1- (car active))))
5128         (setq ninfo (gnus-info-read info)))
5129       ;; Then we add the read articles to the range.
5130       (gnus-add-to-range
5131        ninfo (setq articles (sort articles '<))))))
5132
5133 (defun gnus-group-make-articles-read (group articles)
5134   "Update the info of GROUP to say that ARTICLES are read."
5135   (let* ((num 0)
5136          (entry (gnus-gethash group gnus-newsrc-hashtb))
5137          (info (nth 2 entry))
5138          (active (gnus-active group))
5139          range)
5140     (when entry
5141       (setq range (gnus-compute-read-articles group articles))
5142       (save-excursion
5143         (set-buffer gnus-group-buffer)
5144         (gnus-undo-register
5145           `(progn
5146              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5147              (gnus-info-set-read ',info ',(gnus-info-read info))
5148              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5149              (gnus-request-set-mark ,group (list (list ',range 'del '(read))))
5150              (gnus-group-update-group ,group t))))
5151       ;; Add the read articles to the range.
5152       (gnus-info-set-read info range)
5153       (gnus-request-set-mark group (list (list range 'add '(read))))
5154       ;; Then we have to re-compute how many unread
5155       ;; articles there are in this group.
5156       (when active
5157         (cond
5158          ((not range)
5159           (setq num (- (1+ (cdr active)) (car active))))
5160          ((not (listp (cdr range)))
5161           (setq num (- (cdr active) (- (1+ (cdr range))
5162                                        (car range)))))
5163          (t
5164           (while range
5165             (if (numberp (car range))
5166                 (setq num (1+ num))
5167               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5168             (setq range (cdr range)))
5169           (setq num (- (cdr active) num))))
5170         ;; Update the number of unread articles.
5171         (setcar entry num)
5172         ;; Update the group buffer.
5173         (gnus-group-update-group group t)))))
5174
5175 (defvar gnus-newsgroup-none-id 0)
5176
5177 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5178   (let ((cur nntp-server-buffer)
5179         (dependencies
5180          (or dependencies
5181              (save-excursion (set-buffer gnus-summary-buffer)
5182                              gnus-newsgroup-dependencies)))
5183         headers id end ref
5184         (mail-parse-charset gnus-newsgroup-charset)
5185         (mail-parse-ignored-charsets
5186          (save-excursion (condition-case nil
5187                              (set-buffer gnus-summary-buffer)
5188                            (error))
5189                          gnus-newsgroup-ignored-charsets)))
5190     (save-excursion
5191       (set-buffer nntp-server-buffer)
5192       ;; Translate all TAB characters into SPACE characters.
5193       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5194       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5195       (gnus-run-hooks 'gnus-parse-headers-hook)
5196       (let ((case-fold-search t)
5197             in-reply-to header p lines chars)
5198         (goto-char (point-min))
5199         ;; Search to the beginning of the next header.  Error messages
5200         ;; do not begin with 2 or 3.
5201         (while (re-search-forward "^[23][0-9]+ " nil t)
5202           (setq id nil
5203                 ref nil)
5204           ;; This implementation of this function, with nine
5205           ;; search-forwards instead of the one re-search-forward and
5206           ;; a case (which basically was the old function) is actually
5207           ;; about twice as fast, even though it looks messier.  You
5208           ;; can't have everything, I guess.  Speed and elegance
5209           ;; doesn't always go hand in hand.
5210           (setq
5211            header
5212            (vector
5213             ;; Number.
5214             (prog1
5215                 (read cur)
5216               (end-of-line)
5217               (setq p (point))
5218               (narrow-to-region (point)
5219                                 (or (and (search-forward "\n.\n" nil t)
5220                                          (- (point) 2))
5221                                     (point))))
5222             ;; Subject.
5223             (progn
5224               (goto-char p)
5225               (if (search-forward "\nsubject:" nil t)
5226                   (funcall gnus-decode-encoded-word-function
5227                            (nnheader-header-value))
5228                 "(none)"))
5229             ;; From.
5230             (progn
5231               (goto-char p)
5232               (if (search-forward "\nfrom:" nil t)
5233                   (funcall gnus-decode-encoded-word-function
5234                            (nnheader-header-value))
5235                 "(nobody)"))
5236             ;; Date.
5237             (progn
5238               (goto-char p)
5239               (if (search-forward "\ndate:" nil t)
5240                   (nnheader-header-value) ""))
5241             ;; Message-ID.
5242             (progn
5243               (goto-char p)
5244               (setq id (if (re-search-forward
5245                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5246                       ;; We do it this way to make sure the Message-ID
5247                            ;; is (somewhat) syntactically valid.
5248                            (buffer-substring (match-beginning 1)
5249                                              (match-end 1))
5250                        ;; If there was no message-id, we just fake one
5251                          ;; to make subsequent routines simpler.
5252                          (nnheader-generate-fake-message-id))))
5253             ;; References.
5254             (progn
5255               (goto-char p)
5256               (if (search-forward "\nreferences:" nil t)
5257                   (progn
5258                     (setq end (point))
5259                     (prog1
5260                         (nnheader-header-value)
5261                       (setq ref
5262                             (buffer-substring
5263                              (progn
5264                                (end-of-line)
5265                                (search-backward ">" end t)
5266                                (1+ (point)))
5267                              (progn
5268                                (search-backward "<" end t)
5269                                (point))))))
5270             ;; Get the references from the in-reply-to header if there
5271                 ;; were no references and the in-reply-to header looks
5272                 ;; promising.
5273                 (if (and (search-forward "\nin-reply-to:" nil t)
5274                          (setq in-reply-to (nnheader-header-value))
5275                          (string-match "<[^>]+>" in-reply-to))
5276                     (let (ref2)
5277                       (setq ref (substring in-reply-to (match-beginning 0)
5278                                            (match-end 0)))
5279                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5280                         (setq ref2 (substring in-reply-to (match-beginning 0)
5281                                               (match-end 0)))
5282                         (when (> (length ref2) (length ref))
5283                           (setq ref ref2)))
5284                       ref)
5285                   (setq ref nil))))
5286             ;; Chars.
5287             (progn
5288               (goto-char p)
5289               (if (search-forward "\nchars: " nil t)
5290                   (if (numberp (setq chars (ignore-errors (read cur))))
5291                       chars -1)
5292                 -1))
5293             ;; Lines.
5294             (progn
5295               (goto-char p)
5296               (if (search-forward "\nlines: " nil t)
5297                   (if (numberp (setq lines (ignore-errors (read cur))))
5298                       lines -1)
5299                 -1))
5300             ;; Xref.
5301             (progn
5302               (goto-char p)
5303               (and (search-forward "\nxref:" nil t)
5304                    (nnheader-header-value)))
5305             ;; Extra.
5306             (when gnus-extra-headers
5307               (let ((extra gnus-extra-headers)
5308                     out)
5309                 (while extra
5310                   (goto-char p)
5311                   (when (search-forward
5312                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5313                     (push (cons (car extra) (nnheader-header-value))
5314                           out))
5315                   (pop extra))
5316                 out))))
5317           (when (equal id ref)
5318             (setq ref nil))
5319
5320           (when gnus-alter-header-function
5321             (funcall gnus-alter-header-function header)
5322             (setq id (mail-header-id header)
5323                   ref (gnus-parent-id (mail-header-references header))))
5324
5325           (when (setq header
5326                       (gnus-dependencies-add-header
5327                        header dependencies force-new))
5328             (push header headers))
5329           (goto-char (point-max))
5330           (widen))
5331         (nreverse headers)))))
5332
5333 ;; Goes through the xover lines and returns a list of vectors
5334 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5335                                                   force-new dependencies
5336                                                   group also-fetch-heads)
5337   "Parse the news overview data in the server buffer.
5338 Return a list of headers that match SEQUENCE (see
5339 `nntp-retrieve-headers')."
5340   ;; Get the Xref when the users reads the articles since most/some
5341   ;; NNTP servers do not include Xrefs when using XOVER.
5342   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5343   (let ((mail-parse-charset gnus-newsgroup-charset)
5344         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5345         (cur nntp-server-buffer)
5346         (dependencies (or dependencies gnus-newsgroup-dependencies))
5347         (allp (cond
5348                ((eq gnus-read-all-available-headers t)
5349                 t)
5350                ((stringp gnus-read-all-available-headers)
5351                 (string-match gnus-read-all-available-headers group))
5352                (t
5353                 nil)))
5354         number headers header)
5355     (save-excursion
5356       (set-buffer nntp-server-buffer)
5357       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5358       ;; Allow the user to mangle the headers before parsing them.
5359       (gnus-run-hooks 'gnus-parse-headers-hook)
5360       (goto-char (point-min))
5361       (while (not (eobp))
5362         (condition-case ()
5363             (while (and (or sequence allp)
5364                         (not (eobp)))
5365               (setq number (read cur))
5366               (when (not allp)
5367                 (while (and sequence
5368                             (< (car sequence) number))
5369                   (setq sequence (cdr sequence))))
5370               (when (and (or allp
5371                              (and sequence
5372                                   (eq number (car sequence))))
5373                          (progn
5374                            (setq sequence (cdr sequence))
5375                            (setq header (inline
5376                                           (gnus-nov-parse-line
5377                                            number dependencies force-new)))))
5378                 (push header headers))
5379               (forward-line 1))
5380           (error
5381            (gnus-error 4 "Strange nov line (%d)"
5382                        (count-lines (point-min) (point)))))
5383         (forward-line 1))
5384       ;; A common bug in inn is that if you have posted an article and
5385       ;; then retrieves the active file, it will answer correctly --
5386       ;; the new article is included.  However, a NOV entry for the
5387       ;; article may not have been generated yet, so this may fail.
5388       ;; We work around this problem by retrieving the last few
5389       ;; headers using HEAD.
5390       (if (or (not also-fetch-heads)
5391               (not sequence))
5392           ;; We (probably) got all the headers.
5393           (nreverse headers)
5394         (let ((gnus-nov-is-evil t))
5395           (nconc
5396            (nreverse headers)
5397            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5398              (gnus-get-newsgroup-headers))))))))
5399
5400 (defun gnus-article-get-xrefs ()
5401   "Fill in the Xref value in `gnus-current-headers', if necessary.
5402 This is meant to be called in `gnus-article-internal-prepare-hook'."
5403   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5404                                  gnus-current-headers)))
5405     (or (not gnus-use-cross-reference)
5406         (not headers)
5407         (and (mail-header-xref headers)
5408              (not (string= (mail-header-xref headers) "")))
5409         (let ((case-fold-search t)
5410               xref)
5411           (save-restriction
5412             (nnheader-narrow-to-headers)
5413             (goto-char (point-min))
5414             (when (or (and (not (eobp))
5415                            (eq (downcase (char-after)) ?x)
5416                            (looking-at "Xref:"))
5417                       (search-forward "\nXref:" nil t))
5418               (goto-char (1+ (match-end 0)))
5419               (setq xref (buffer-substring (point)
5420                                            (progn (end-of-line) (point))))
5421               (mail-header-set-xref headers xref)))))))
5422
5423 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5424   "Find article ID and insert the summary line for that article.
5425 OLD-HEADER can either be a header or a line number to insert
5426 the subject line on."
5427   (let* ((line (and (numberp old-header) old-header))
5428          (old-header (and (vectorp old-header) old-header))
5429          (header (cond ((and old-header use-old-header)
5430                         old-header)
5431                        ((and (numberp id)
5432                              (gnus-number-to-header id))
5433                         (gnus-number-to-header id))
5434                        (t
5435                         (gnus-read-header id))))
5436          (number (and (numberp id) id))
5437          d)
5438     (when header
5439       ;; Rebuild the thread that this article is part of and go to the
5440       ;; article we have fetched.
5441       (when (and (not gnus-show-threads)
5442                  old-header)
5443         (when (and number
5444                    (setq d (gnus-data-find (mail-header-number old-header))))
5445           (goto-char (gnus-data-pos d))
5446           (gnus-data-remove
5447            number
5448            (- (gnus-point-at-bol)
5449               (prog1
5450                   (1+ (gnus-point-at-eol))
5451                 (gnus-delete-line))))))
5452       (when old-header
5453         (mail-header-set-number header (mail-header-number old-header)))
5454       (setq gnus-newsgroup-sparse
5455             (delq (setq number (mail-header-number header))
5456                   gnus-newsgroup-sparse))
5457       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5458       (push number gnus-newsgroup-limit)
5459       (gnus-rebuild-thread (mail-header-id header) line)
5460       (gnus-summary-goto-subject number nil t))
5461     (when (and (numberp number)
5462                (> number 0))
5463       ;; We have to update the boundaries even if we can't fetch the
5464       ;; article if ID is a number -- so that the next `P' or `N'
5465       ;; command will fetch the previous (or next) article even
5466       ;; if the one we tried to fetch this time has been canceled.
5467       (when (> number gnus-newsgroup-end)
5468         (setq gnus-newsgroup-end number))
5469       (when (< number gnus-newsgroup-begin)
5470         (setq gnus-newsgroup-begin number))
5471       (setq gnus-newsgroup-unselected
5472             (delq number gnus-newsgroup-unselected)))
5473     ;; Report back a success?
5474     (and header (mail-header-number header))))
5475
5476 ;;; Process/prefix in the summary buffer
5477
5478 (defun gnus-summary-work-articles (n)
5479   "Return a list of articles to be worked upon.
5480 The prefix argument, the list of process marked articles, and the
5481 current article will be taken into consideration."
5482   (save-excursion
5483     (set-buffer gnus-summary-buffer)
5484     (cond
5485      (n
5486       ;; A numerical prefix has been given.
5487       (setq n (prefix-numeric-value n))
5488       (let ((backward (< n 0))
5489             (n (abs (prefix-numeric-value n)))
5490             articles article)
5491         (save-excursion
5492           (while
5493               (and (> n 0)
5494                    (push (setq article (gnus-summary-article-number))
5495                          articles)
5496                    (if backward
5497                        (gnus-summary-find-prev nil article)
5498                      (gnus-summary-find-next nil article)))
5499             (decf n)))
5500         (nreverse articles)))
5501      ((and (gnus-region-active-p) (mark))
5502       (message "region active")
5503       ;; Work on the region between point and mark.
5504       (let ((max (max (point) (mark)))
5505             articles article)
5506         (save-excursion
5507           (goto-char (min (min (point) (mark))))
5508           (while
5509               (and
5510                (push (setq article (gnus-summary-article-number)) articles)
5511                (gnus-summary-find-next nil article)
5512                (< (point) max)))
5513           (nreverse articles))))
5514      (gnus-newsgroup-processable
5515       ;; There are process-marked articles present.
5516       ;; Save current state.
5517       (gnus-summary-save-process-mark)
5518       ;; Return the list.
5519       (reverse gnus-newsgroup-processable))
5520      (t
5521       ;; Just return the current article.
5522       (list (gnus-summary-article-number))))))
5523
5524 (defmacro gnus-summary-iterate (arg &rest forms)
5525   "Iterate over the process/prefixed articles and do FORMS.
5526 ARG is the interactive prefix given to the command.  FORMS will be
5527 executed with point over the summary line of the articles."
5528   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5529     `(let ((,articles (gnus-summary-work-articles ,arg)))
5530        (while ,articles
5531          (gnus-summary-goto-subject (car ,articles))
5532          ,@forms
5533          (pop ,articles)))))
5534
5535 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5536 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5537
5538 (defun gnus-summary-save-process-mark ()
5539   "Push the current set of process marked articles on the stack."
5540   (interactive)
5541   (push (copy-sequence gnus-newsgroup-processable)
5542         gnus-newsgroup-process-stack))
5543
5544 (defun gnus-summary-kill-process-mark ()
5545   "Push the current set of process marked articles on the stack and unmark."
5546   (interactive)
5547   (gnus-summary-save-process-mark)
5548   (gnus-summary-unmark-all-processable))
5549
5550 (defun gnus-summary-yank-process-mark ()
5551   "Pop the last process mark state off the stack and restore it."
5552   (interactive)
5553   (unless gnus-newsgroup-process-stack
5554     (error "Empty mark stack"))
5555   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5556
5557 (defun gnus-summary-process-mark-set (set)
5558   "Make SET into the current process marked articles."
5559   (gnus-summary-unmark-all-processable)
5560   (while set
5561     (gnus-summary-set-process-mark (pop set))))
5562
5563 ;;; Searching and stuff
5564
5565 (defun gnus-summary-search-group (&optional backward use-level)
5566   "Search for next unread newsgroup.
5567 If optional argument BACKWARD is non-nil, search backward instead."
5568   (save-excursion
5569     (set-buffer gnus-group-buffer)
5570     (when (gnus-group-search-forward
5571            backward nil (if use-level (gnus-group-group-level) nil))
5572       (gnus-group-group-name))))
5573
5574 (defun gnus-summary-best-group (&optional exclude-group)
5575   "Find the name of the best unread group.
5576 If EXCLUDE-GROUP, do not go to this group."
5577   (save-excursion
5578     (set-buffer gnus-group-buffer)
5579     (save-excursion
5580       (gnus-group-best-unread-group exclude-group))))
5581
5582 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5583   (if backward (gnus-summary-find-prev)
5584     (let* ((dummy (gnus-summary-article-intangible-p))
5585            (article (or article (gnus-summary-article-number)))
5586            (arts (gnus-data-find-list article))
5587            result)
5588       (when (and (not dummy)
5589                  (or (not gnus-summary-check-current)
5590                      (not unread)
5591                      (not (gnus-data-unread-p (car arts)))))
5592         (setq arts (cdr arts)))
5593       (when (setq result
5594                   (if unread
5595                       (progn
5596                         (while arts
5597                           (when (or (and undownloaded
5598                                          (eq gnus-undownloaded-mark
5599                                              (gnus-data-mark (car arts))))
5600                                     (gnus-data-unread-p (car arts)))
5601                             (setq result (car arts)
5602                                   arts nil))
5603                           (setq arts (cdr arts)))
5604                         result)
5605                     (car arts)))
5606         (goto-char (gnus-data-pos result))
5607         (gnus-data-number result)))))
5608
5609 (defun gnus-summary-find-prev (&optional unread article)
5610   (let* ((eobp (eobp))
5611          (article (or article (gnus-summary-article-number)))
5612          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5613          result)
5614     (when (and (not eobp)
5615                (or (not gnus-summary-check-current)
5616                    (not unread)
5617                    (not (gnus-data-unread-p (car arts)))))
5618       (setq arts (cdr arts)))
5619     (when (setq result
5620                 (if unread
5621                     (progn
5622                       (while arts
5623                         (when (gnus-data-unread-p (car arts))
5624                           (setq result (car arts)
5625                                 arts nil))
5626                         (setq arts (cdr arts)))
5627                       result)
5628                   (car arts)))
5629       (goto-char (gnus-data-pos result))
5630       (gnus-data-number result))))
5631
5632 (defun gnus-summary-find-subject (subject &optional unread backward article)
5633   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5634          (article (or article (gnus-summary-article-number)))
5635          (articles (gnus-data-list backward))
5636          (arts (gnus-data-find-list article articles))
5637          result)
5638     (when (or (not gnus-summary-check-current)
5639               (not unread)
5640               (not (gnus-data-unread-p (car arts))))
5641       (setq arts (cdr arts)))
5642     (while arts
5643       (and (or (not unread)
5644                (gnus-data-unread-p (car arts)))
5645            (vectorp (gnus-data-header (car arts)))
5646            (gnus-subject-equal
5647             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5648            (setq result (car arts)
5649                  arts nil))
5650       (setq arts (cdr arts)))
5651     (and result
5652          (goto-char (gnus-data-pos result))
5653          (gnus-data-number result))))
5654
5655 (defun gnus-summary-search-forward (&optional unread subject backward)
5656   "Search forward for an article.
5657 If UNREAD, look for unread articles.  If SUBJECT, look for
5658 articles with that subject.  If BACKWARD, search backward instead."
5659   (cond (subject (gnus-summary-find-subject subject unread backward))
5660         (backward (gnus-summary-find-prev unread))
5661         (t (gnus-summary-find-next unread))))
5662
5663 (defun gnus-recenter (&optional n)
5664   "Center point in window and redisplay frame.
5665 Also do horizontal recentering."
5666   (interactive "P")
5667   (when (and gnus-auto-center-summary
5668              (not (eq gnus-auto-center-summary 'vertical)))
5669     (gnus-horizontal-recenter))
5670   (recenter n))
5671
5672 (defun gnus-summary-recenter ()
5673   "Center point in the summary window.
5674 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5675 displayed, no centering will be performed."
5676   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5677 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5678   (interactive)
5679   (let* ((top (cond ((< (window-height) 4) 0)
5680                     ((< (window-height) 7) 1)
5681                     (t (if (numberp gnus-auto-center-summary)
5682                            gnus-auto-center-summary
5683                          2))))
5684          (height (1- (window-height)))
5685          (bottom (save-excursion (goto-char (point-max))
5686                                  (forward-line (- height))
5687                                  (point)))
5688          (window (get-buffer-window (current-buffer))))
5689     ;; The user has to want it.
5690     (when gnus-auto-center-summary
5691       (when (get-buffer-window gnus-article-buffer)
5692         ;; Only do recentering when the article buffer is displayed,
5693       ;; Set the window start to either `bottom', which is the biggest
5694         ;; possible valid number, or the second line from the top,
5695         ;; whichever is the least.
5696         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5697           (if (> bottom top-pos)
5698               ;; Keep the second line from the top visible
5699               (set-window-start window top-pos t)
5700             ;; Try to keep the bottom line visible; if it's partially
5701             ;; obscured, either scroll one more line to make it fully
5702             ;; visible, or revert to using TOP-POS.
5703             (save-excursion
5704               (goto-char (point-max))
5705               (forward-line -1)
5706               (let ((last-line-start (point)))
5707                 (goto-char bottom)
5708                 (set-window-start window (point) t)
5709                 (when (not (pos-visible-in-window-p last-line-start window))
5710                   (forward-line 1)
5711                   (set-window-start window (min (point) top-pos) t)))))))
5712       ;; Do horizontal recentering while we're at it.
5713       (when (and (get-buffer-window (current-buffer) t)
5714                  (not (eq gnus-auto-center-summary 'vertical)))
5715         (let ((selected (selected-window)))
5716           (select-window (get-buffer-window (current-buffer) t))
5717           (gnus-summary-position-point)
5718           (gnus-horizontal-recenter)
5719           (select-window selected))))))
5720
5721 (defun gnus-summary-jump-to-group (newsgroup)
5722   "Move point to NEWSGROUP in group mode buffer."
5723   ;; Keep update point of group mode buffer if visible.
5724   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5725       (save-window-excursion
5726         ;; Take care of tree window mode.
5727         (when (get-buffer-window gnus-group-buffer)
5728           (pop-to-buffer gnus-group-buffer))
5729         (gnus-group-jump-to-group newsgroup))
5730     (save-excursion
5731       ;; Take care of tree window mode.
5732       (if (get-buffer-window gnus-group-buffer)
5733           (pop-to-buffer gnus-group-buffer)
5734         (set-buffer gnus-group-buffer))
5735       (gnus-group-jump-to-group newsgroup))))
5736
5737 ;; This function returns a list of article numbers based on the
5738 ;; difference between the ranges of read articles in this group and
5739 ;; the range of active articles.
5740 (defun gnus-list-of-unread-articles (group)
5741   (let* ((read (gnus-info-read (gnus-get-info group)))
5742          (active (or (gnus-active group) (gnus-activate-group group)))
5743          (last (cdr active))
5744          first nlast unread)
5745     ;; If none are read, then all are unread.
5746     (if (not read)
5747         (setq first (car active))
5748       ;; If the range of read articles is a single range, then the
5749       ;; first unread article is the article after the last read
5750       ;; article.  Sounds logical, doesn't it?
5751       (if (and (not (listp (cdr read)))
5752                (or (< (car read) (car active))
5753                    (progn (setq read (list read))
5754                           nil)))
5755           (setq first (max (car active) (1+ (cdr read))))
5756         ;; `read' is a list of ranges.
5757         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5758                                   (caar read)))
5759                   1)
5760           (setq first (car active)))
5761         (while read
5762           (when first
5763             (while (< first nlast)
5764               (push first unread)
5765               (setq first (1+ first))))
5766           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5767           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5768           (setq read (cdr read)))))
5769     ;; And add the last unread articles.
5770     (while (<= first last)
5771       (push first unread)
5772       (setq first (1+ first)))
5773     ;; Return the list of unread articles.
5774     (delq 0 (nreverse unread))))
5775
5776 (defun gnus-list-of-read-articles (group)
5777   "Return a list of unread, unticked and non-dormant articles."
5778   (let* ((info (gnus-get-info group))
5779          (marked (gnus-info-marks info))
5780          (active (gnus-active group)))
5781     (and info active
5782          (gnus-set-difference
5783           (gnus-sorted-complement
5784            (gnus-uncompress-range active)
5785            (gnus-list-of-unread-articles group))
5786           (append
5787            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5788            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5789
5790 ;; Various summary commands
5791
5792 (defun gnus-summary-select-article-buffer ()
5793   "Reconfigure windows to show article buffer."
5794   (interactive)
5795   (if (not (gnus-buffer-live-p gnus-article-buffer))
5796       (error "There is no article buffer for this summary buffer")
5797     (gnus-configure-windows 'article)
5798     (select-window (get-buffer-window gnus-article-buffer))))
5799
5800 (defun gnus-summary-universal-argument (arg)
5801   "Perform any operation on all articles that are process/prefixed."
5802   (interactive "P")
5803   (let ((articles (gnus-summary-work-articles arg))
5804         func article)
5805     (if (eq
5806          (setq
5807           func
5808           (key-binding
5809            (read-key-sequence
5810             (substitute-command-keys
5811              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5812          'undefined)
5813         (gnus-error 1 "Undefined key")
5814       (save-excursion
5815         (while articles
5816           (gnus-summary-goto-subject (setq article (pop articles)))
5817           (let (gnus-newsgroup-processable)
5818             (command-execute func))
5819           (gnus-summary-remove-process-mark article)))))
5820   (gnus-summary-position-point))
5821
5822 (defun gnus-summary-toggle-truncation (&optional arg)
5823   "Toggle truncation of summary lines.
5824 With arg, turn line truncation on iff arg is positive."
5825   (interactive "P")
5826   (setq truncate-lines
5827         (if (null arg) (not truncate-lines)
5828           (> (prefix-numeric-value arg) 0)))
5829   (redraw-display))
5830
5831 (defun gnus-summary-reselect-current-group (&optional all rescan)
5832   "Exit and then reselect the current newsgroup.
5833 The prefix argument ALL means to select all articles."
5834   (interactive "P")
5835   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5836     (error "Ephemeral groups can't be reselected"))
5837   (let ((current-subject (gnus-summary-article-number))
5838         (group gnus-newsgroup-name))
5839     (setq gnus-newsgroup-begin nil)
5840     (gnus-summary-exit)
5841     ;; We have to adjust the point of group mode buffer because
5842     ;; point was moved to the next unread newsgroup by exiting.
5843     (gnus-summary-jump-to-group group)
5844     (when rescan
5845       (save-excursion
5846         (gnus-group-get-new-news-this-group 1)))
5847     (gnus-group-read-group all t)
5848     (gnus-summary-goto-subject current-subject nil t)))
5849
5850 (defun gnus-summary-rescan-group (&optional all)
5851   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5852   (interactive "P")
5853   (gnus-summary-reselect-current-group all t))
5854
5855 (defun gnus-summary-update-info (&optional non-destructive)
5856   (save-excursion
5857     (let ((group gnus-newsgroup-name))
5858       (when group
5859         (when gnus-newsgroup-kill-headers
5860           (setq gnus-newsgroup-killed
5861                 (gnus-compress-sequence
5862                  (nconc
5863                   (gnus-set-sorted-intersection
5864                    (gnus-uncompress-range gnus-newsgroup-killed)
5865                    (setq gnus-newsgroup-unselected
5866                          (sort gnus-newsgroup-unselected '<)))
5867                   (setq gnus-newsgroup-unreads
5868                         (sort gnus-newsgroup-unreads '<)))
5869                  t)))
5870         (unless (listp (cdr gnus-newsgroup-killed))
5871           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5872         (let ((headers gnus-newsgroup-headers))
5873           ;; Set the new ranges of read articles.
5874           (save-excursion
5875             (set-buffer gnus-group-buffer)
5876             (gnus-undo-force-boundary))
5877           (gnus-update-read-articles
5878            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5879           ;; Set the current article marks.
5880           (let ((gnus-newsgroup-scored
5881                  (if (and (not gnus-save-score)
5882                           (not non-destructive))
5883                      nil
5884                    gnus-newsgroup-scored)))
5885             (save-excursion
5886               (gnus-update-marks)))
5887           ;; Do the cross-ref thing.
5888           (when gnus-use-cross-reference
5889             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5890           ;; Do not switch windows but change the buffer to work.
5891           (set-buffer gnus-group-buffer)
5892           (unless (gnus-ephemeral-group-p group)
5893             (gnus-group-update-group group)))))))
5894
5895 (defun gnus-summary-save-newsrc (&optional force)
5896   "Save the current number of read/marked articles in the dribble buffer.
5897 The dribble buffer will then be saved.
5898 If FORCE (the prefix), also save the .newsrc file(s)."
5899   (interactive "P")
5900   (gnus-summary-update-info t)
5901   (if force
5902       (gnus-save-newsrc-file)
5903     (gnus-dribble-save)))
5904
5905 (defun gnus-summary-exit (&optional temporary)
5906   "Exit reading current newsgroup, and then return to group selection mode.
5907 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5908   (interactive)
5909   (gnus-set-global-variables)
5910   (when (gnus-buffer-live-p gnus-article-buffer)
5911     (save-excursion
5912       (set-buffer gnus-article-buffer)
5913       (mm-destroy-parts gnus-article-mime-handles)
5914       ;; Set it to nil for safety reason.
5915       (setq gnus-article-mime-handle-alist nil)
5916       (setq gnus-article-mime-handles nil)))
5917   (gnus-kill-save-kill-buffer)
5918   (gnus-async-halt-prefetch)
5919   (let* ((group gnus-newsgroup-name)
5920          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5921          (mode major-mode)
5922          (group-point nil)
5923          (buf (current-buffer)))
5924     (unless quit-config
5925       ;; Do adaptive scoring, and possibly save score files.
5926       (when gnus-newsgroup-adaptive
5927         (gnus-score-adaptive))
5928       (when gnus-use-scoring
5929         (gnus-score-save)))
5930     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5931     ;; If we have several article buffers, we kill them at exit.
5932     (unless gnus-single-article-buffer
5933       (gnus-kill-buffer gnus-original-article-buffer)
5934       (setq gnus-article-current nil))
5935     (when gnus-use-cache
5936       (gnus-cache-possibly-remove-articles)
5937       (gnus-cache-save-buffers))
5938     (gnus-async-prefetch-remove-group group)
5939     (when gnus-suppress-duplicates
5940       (gnus-dup-enter-articles))
5941     (when gnus-use-trees
5942       (gnus-tree-close group))
5943     (when gnus-use-cache
5944       (gnus-cache-write-active))
5945     ;; Remove entries for this group.
5946     (nnmail-purge-split-history (gnus-group-real-name group))
5947     ;; Make all changes in this group permanent.
5948     (unless quit-config
5949       (gnus-run-hooks 'gnus-exit-group-hook)
5950       (gnus-summary-update-info))
5951     (gnus-close-group group)
5952     ;; Make sure where we were, and go to next newsgroup.
5953     (set-buffer gnus-group-buffer)
5954     (unless quit-config
5955       (gnus-group-jump-to-group group))
5956     (gnus-run-hooks 'gnus-summary-exit-hook)
5957     (unless (or quit-config
5958                 ;; If this group has disappeared from the summary
5959                 ;; buffer, don't skip forwards.
5960                 (not (string= group (gnus-group-group-name))))
5961       (gnus-group-next-unread-group 1))
5962     (setq group-point (point))
5963     (if temporary
5964         nil                             ;Nothing to do.
5965       ;; If we have several article buffers, we kill them at exit.
5966       (unless gnus-single-article-buffer
5967         (gnus-kill-buffer gnus-article-buffer)
5968         (gnus-kill-buffer gnus-original-article-buffer)
5969         (setq gnus-article-current nil))
5970       (set-buffer buf)
5971       (if (not gnus-kill-summary-on-exit)
5972           (progn
5973             (gnus-deaden-summary)
5974             (setq mode nil))
5975        ;; We set all buffer-local variables to nil.  It is unclear why
5976         ;; this is needed, but if we don't, buffer-local variables are
5977         ;; not garbage-collected, it seems.  This would the lead to en
5978         ;; ever-growing Emacs.
5979         (gnus-summary-clear-local-variables)
5980         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5981           (gnus-summary-clear-local-variables))
5982         (when (get-buffer gnus-article-buffer)
5983           (bury-buffer gnus-article-buffer))
5984         ;; We clear the global counterparts of the buffer-local
5985         ;; variables as well, just to be on the safe side.
5986         (set-buffer gnus-group-buffer)
5987         (gnus-summary-clear-local-variables)
5988         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5989           (gnus-summary-clear-local-variables)))
5990       (setq gnus-current-select-method gnus-select-method)
5991       (pop-to-buffer gnus-group-buffer)
5992       (if (not quit-config)
5993           (progn
5994             (goto-char group-point)
5995             (gnus-configure-windows 'group 'force))
5996         (gnus-handle-ephemeral-exit quit-config))
5997       ;; Return to group mode buffer.
5998       (when (eq mode 'gnus-summary-mode)
5999         (gnus-kill-buffer buf))
6000       ;; Clear the current group name.
6001       (unless quit-config
6002         (setq gnus-newsgroup-name nil)))))
6003
6004 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
6005 (defun gnus-summary-exit-no-update (&optional no-questions)
6006   "Quit reading current newsgroup without updating read article info."
6007   (interactive)
6008   (let* ((group gnus-newsgroup-name)
6009          (quit-config (gnus-group-quit-config group)))
6010     (when (or no-questions
6011               gnus-expert-user
6012               (gnus-y-or-n-p "Discard changes to this group and exit? "))
6013       (gnus-async-halt-prefetch)
6014       (mapcar 'funcall
6015               (delq 'gnus-summary-expire-articles
6016                     (copy-sequence gnus-summary-prepare-exit-hook)))
6017       (when (gnus-buffer-live-p gnus-article-buffer)
6018         (save-excursion
6019           (set-buffer gnus-article-buffer)
6020           (mm-destroy-parts gnus-article-mime-handles)
6021           ;; Set it to nil for safety reason.
6022           (setq gnus-article-mime-handle-alist nil)
6023           (setq gnus-article-mime-handles nil)))
6024       ;; If we have several article buffers, we kill them at exit.
6025       (unless gnus-single-article-buffer
6026         (gnus-kill-buffer gnus-article-buffer)
6027         (gnus-kill-buffer gnus-original-article-buffer)
6028         (setq gnus-article-current nil))
6029       (if (not gnus-kill-summary-on-exit)
6030           (gnus-deaden-summary)
6031         (gnus-close-group group)
6032         (gnus-summary-clear-local-variables)
6033         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6034           (gnus-summary-clear-local-variables))
6035         (set-buffer gnus-group-buffer)
6036         (gnus-summary-clear-local-variables)
6037         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6038           (gnus-summary-clear-local-variables))
6039         (when (get-buffer gnus-summary-buffer)
6040           (kill-buffer gnus-summary-buffer)))
6041       (unless gnus-single-article-buffer
6042         (setq gnus-article-current nil))
6043       (when gnus-use-trees
6044         (gnus-tree-close group))
6045       (gnus-async-prefetch-remove-group group)
6046       (when (get-buffer gnus-article-buffer)
6047         (bury-buffer gnus-article-buffer))
6048       ;; Return to the group buffer.
6049       (gnus-configure-windows 'group 'force)
6050       ;; Clear the current group name.
6051       (setq gnus-newsgroup-name nil)
6052       (when (equal (gnus-group-group-name) group)
6053         (gnus-group-next-unread-group 1))
6054       (when quit-config
6055         (gnus-handle-ephemeral-exit quit-config)))))
6056
6057 (defun gnus-handle-ephemeral-exit (quit-config)
6058   "Handle movement when leaving an ephemeral group.
6059 The state which existed when entering the ephemeral is reset."
6060   (if (not (buffer-name (car quit-config)))
6061       (gnus-configure-windows 'group 'force)
6062     (set-buffer (car quit-config))
6063     (cond ((eq major-mode 'gnus-summary-mode)
6064            (gnus-set-global-variables))
6065           ((eq major-mode 'gnus-article-mode)
6066            (save-excursion
6067              ;; The `gnus-summary-buffer' variable may point
6068              ;; to the old summary buffer when using a single
6069              ;; article buffer.
6070              (unless (gnus-buffer-live-p gnus-summary-buffer)
6071                (set-buffer gnus-group-buffer))
6072              (set-buffer gnus-summary-buffer)
6073              (gnus-set-global-variables))))
6074     (if (or (eq (cdr quit-config) 'article)
6075             (eq (cdr quit-config) 'pick))
6076         (progn
6077           ;; The current article may be from the ephemeral group
6078           ;; thus it is best that we reload this article
6079           (gnus-summary-show-article)
6080           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6081               (gnus-configure-windows 'pick 'force)
6082             (gnus-configure-windows (cdr quit-config) 'force)))
6083       (gnus-configure-windows (cdr quit-config) 'force))
6084     (when (eq major-mode 'gnus-summary-mode)
6085       (gnus-summary-next-subject 1 nil t)
6086       (gnus-summary-recenter)
6087       (gnus-summary-position-point))))
6088
6089 ;;; Dead summaries.
6090
6091 (defvar gnus-dead-summary-mode-map nil)
6092
6093 (unless gnus-dead-summary-mode-map
6094   (setq gnus-dead-summary-mode-map (make-keymap))
6095   (suppress-keymap gnus-dead-summary-mode-map)
6096   (substitute-key-definition
6097    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6098   (let ((keys '("\C-d" "\r" "\177" [delete])))
6099     (while keys
6100       (define-key gnus-dead-summary-mode-map
6101         (pop keys) 'gnus-summary-wake-up-the-dead))))
6102
6103 (defvar gnus-dead-summary-mode nil
6104   "Minor mode for Gnus summary buffers.")
6105
6106 (defun gnus-dead-summary-mode (&optional arg)
6107   "Minor mode for Gnus summary buffers."
6108   (interactive "P")
6109   (when (eq major-mode 'gnus-summary-mode)
6110     (make-local-variable 'gnus-dead-summary-mode)
6111     (setq gnus-dead-summary-mode
6112           (if (null arg) (not gnus-dead-summary-mode)
6113             (> (prefix-numeric-value arg) 0)))
6114     (when gnus-dead-summary-mode
6115       (gnus-add-minor-mode
6116        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6117
6118 (defun gnus-deaden-summary ()
6119   "Make the current summary buffer into a dead summary buffer."
6120   ;; Kill any previous dead summary buffer.
6121   (when (and gnus-dead-summary
6122              (buffer-name gnus-dead-summary))
6123     (save-excursion
6124       (set-buffer gnus-dead-summary)
6125       (when gnus-dead-summary-mode
6126         (kill-buffer (current-buffer)))))
6127   ;; Make this the current dead summary.
6128   (setq gnus-dead-summary (current-buffer))
6129   (gnus-dead-summary-mode 1)
6130   (let ((name (buffer-name)))
6131     (when (string-match "Summary" name)
6132       (rename-buffer
6133        (concat (substring name 0 (match-beginning 0)) "Dead "
6134                (substring name (match-beginning 0)))
6135        t)
6136       (bury-buffer))))
6137
6138 (defun gnus-kill-or-deaden-summary (buffer)
6139   "Kill or deaden the summary BUFFER."
6140   (save-excursion
6141     (when (and (buffer-name buffer)
6142                (not gnus-single-article-buffer))
6143       (save-excursion
6144         (set-buffer buffer)
6145         (gnus-kill-buffer gnus-article-buffer)
6146         (gnus-kill-buffer gnus-original-article-buffer)))
6147     (cond (gnus-kill-summary-on-exit
6148            (when (and gnus-use-trees
6149                       (gnus-buffer-exists-p buffer))
6150              (save-excursion
6151                (set-buffer buffer)
6152                (gnus-tree-close gnus-newsgroup-name)))
6153            (gnus-kill-buffer buffer))
6154           ((gnus-buffer-exists-p buffer)
6155            (save-excursion
6156              (set-buffer buffer)
6157              (gnus-deaden-summary))))))
6158
6159 (defun gnus-summary-wake-up-the-dead (&rest args)
6160   "Wake up the dead summary buffer."
6161   (interactive)
6162   (gnus-dead-summary-mode -1)
6163   (let ((name (buffer-name)))
6164     (when (string-match "Dead " name)
6165       (rename-buffer
6166        (concat (substring name 0 (match-beginning 0))
6167                (substring name (match-end 0)))
6168        t)))
6169   (gnus-message 3 "This dead summary is now alive again"))
6170
6171 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6172 (defun gnus-summary-fetch-faq (&optional faq-dir)
6173   "Fetch the FAQ for the current group.
6174 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6175 in."
6176   (interactive
6177    (list
6178     (when current-prefix-arg
6179       (completing-read
6180        "Faq dir: " (and (listp gnus-group-faq-directory)
6181                         (mapcar (lambda (file) (list file))
6182                                 gnus-group-faq-directory))))))
6183   (let (gnus-faq-buffer)
6184     (when (setq gnus-faq-buffer
6185                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6186       (gnus-configure-windows 'summary-faq))))
6187
6188 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6189 (defun gnus-summary-describe-group (&optional force)
6190   "Describe the current newsgroup."
6191   (interactive "P")
6192   (gnus-group-describe-group force gnus-newsgroup-name))
6193
6194 (defun gnus-summary-describe-briefly ()
6195   "Describe summary mode commands briefly."
6196   (interactive)
6197   (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")))
6198
6199 ;; Walking around group mode buffer from summary mode.
6200
6201 (defun gnus-summary-next-group (&optional no-article target-group backward)
6202   "Exit current newsgroup and then select next unread newsgroup.
6203 If prefix argument NO-ARTICLE is non-nil, no article is selected
6204 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6205 previous group instead."
6206   (interactive "P")
6207   ;; Stop pre-fetching.
6208   (gnus-async-halt-prefetch)
6209   (let ((current-group gnus-newsgroup-name)
6210         (current-buffer (current-buffer))
6211         entered)
6212    ;; First we semi-exit this group to update Xrefs and all variables.
6213     ;; We can't do a real exit, because the window conf must remain
6214     ;; the same in case the user is prompted for info, and we don't
6215     ;; want the window conf to change before that...
6216     (gnus-summary-exit t)
6217     (while (not entered)
6218       ;; Then we find what group we are supposed to enter.
6219       (set-buffer gnus-group-buffer)
6220       (gnus-group-jump-to-group current-group)
6221       (setq target-group
6222             (or target-group
6223                 (if (eq gnus-keep-same-level 'best)
6224                     (gnus-summary-best-group gnus-newsgroup-name)
6225                   (gnus-summary-search-group backward gnus-keep-same-level))))
6226       (if (not target-group)
6227           ;; There are no further groups, so we return to the group
6228           ;; buffer.
6229           (progn
6230             (gnus-message 5 "Returning to the group buffer")
6231             (setq entered t)
6232             (when (gnus-buffer-live-p current-buffer)
6233               (set-buffer current-buffer)
6234               (gnus-summary-exit))
6235             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6236         ;; We try to enter the target group.
6237         (gnus-group-jump-to-group target-group)
6238         (let ((unreads (gnus-group-group-unread)))
6239           (if (and (or (eq t unreads)
6240                        (and unreads (not (zerop unreads))))
6241                    (gnus-summary-read-group
6242                     target-group nil no-article
6243                     (and (buffer-name current-buffer) current-buffer)
6244                     nil backward))
6245               (setq entered t)
6246             (setq current-group target-group
6247                   target-group nil)))))))
6248
6249 (defun gnus-summary-prev-group (&optional no-article)
6250   "Exit current newsgroup and then select previous unread newsgroup.
6251 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6252   (interactive "P")
6253   (gnus-summary-next-group no-article nil t))
6254
6255 ;; Walking around summary lines.
6256
6257 (defun gnus-summary-first-subject (&optional unread undownloaded)
6258   "Go to the first unread subject.
6259 If UNREAD is non-nil, go to the first unread article.
6260 Returns the article selected or nil if there are no unread articles."
6261   (interactive "P")
6262   (prog1
6263       (cond
6264        ;; Empty summary.
6265        ((null gnus-newsgroup-data)
6266         (gnus-message 3 "No articles in the group")
6267         nil)
6268        ;; Pick the first article.
6269        ((not unread)
6270         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6271         (gnus-data-number (car gnus-newsgroup-data)))
6272        ;; No unread articles.
6273        ((null gnus-newsgroup-unreads)
6274         (gnus-message 3 "No more unread articles")
6275         nil)
6276        ;; Find the first unread article.
6277        (t
6278         (let ((data gnus-newsgroup-data))
6279           (while (and data
6280                       (and (not (and undownloaded
6281                                      (eq gnus-undownloaded-mark
6282                                          (gnus-data-mark (car data)))))
6283                            (not (gnus-data-unread-p (car data)))))
6284             (setq data (cdr data)))
6285           (when data
6286             (goto-char (gnus-data-pos (car data)))
6287             (gnus-data-number (car data))))))
6288     (gnus-summary-position-point)))
6289
6290 (defun gnus-summary-next-subject (n &optional unread dont-display)
6291   "Go to next N'th summary line.
6292 If N is negative, go to the previous N'th subject line.
6293 If UNREAD is non-nil, only unread articles are selected.
6294 The difference between N and the actual number of steps taken is
6295 returned."
6296   (interactive "p")
6297   (let ((backward (< n 0))
6298         (n (abs n)))
6299     (while (and (> n 0)
6300                 (if backward
6301                     (gnus-summary-find-prev unread)
6302                   (gnus-summary-find-next unread)))
6303       (unless (zerop (setq n (1- n)))
6304         (gnus-summary-show-thread)))
6305     (when (/= 0 n)
6306       (gnus-message 7 "No more%s articles"
6307                     (if unread " unread" "")))
6308     (unless dont-display
6309       (gnus-summary-recenter)
6310       (gnus-summary-position-point))
6311     n))
6312
6313 (defun gnus-summary-next-unread-subject (n)
6314   "Go to next N'th unread summary line."
6315   (interactive "p")
6316   (gnus-summary-next-subject n t))
6317
6318 (defun gnus-summary-prev-subject (n &optional unread)
6319   "Go to previous N'th summary line.
6320 If optional argument UNREAD is non-nil, only unread article is selected."
6321   (interactive "p")
6322   (gnus-summary-next-subject (- n) unread))
6323
6324 (defun gnus-summary-prev-unread-subject (n)
6325   "Go to previous N'th unread summary line."
6326   (interactive "p")
6327   (gnus-summary-next-subject (- n) t))
6328
6329 (defun gnus-summary-goto-subject (article &optional force silent)
6330   "Go the subject line of ARTICLE.
6331 If FORCE, also allow jumping to articles not currently shown."
6332   (interactive "nArticle number: ")
6333   (let ((b (point))
6334         (data (gnus-data-find article)))
6335     ;; We read in the article if we have to.
6336     (and (not data)
6337          force
6338          (gnus-summary-insert-subject
6339           article
6340           (if (or (numberp force) (vectorp force)) force)
6341           t)
6342          (setq data (gnus-data-find article)))
6343     (goto-char b)
6344     (if (not data)
6345         (progn
6346           (unless silent
6347             (gnus-message 3 "Can't find article %d" article))
6348           nil)
6349       (let ((pt (gnus-data-pos data)))
6350         (goto-char pt)
6351         (gnus-summary-set-article-display-arrow pt))
6352       (gnus-summary-position-point)
6353       article)))
6354
6355 ;; Walking around summary lines with displaying articles.
6356
6357 (defun gnus-summary-expand-window (&optional arg)
6358   "Make the summary buffer take up the entire Emacs frame.
6359 Given a prefix, will force an `article' buffer configuration."
6360   (interactive "P")
6361   (if arg
6362       (gnus-configure-windows 'article 'force)
6363     (gnus-configure-windows 'summary 'force)))
6364
6365 (defun gnus-summary-display-article (article &optional all-header)
6366   "Display ARTICLE in article buffer."
6367   (when (gnus-buffer-live-p gnus-article-buffer)
6368     (with-current-buffer gnus-article-buffer
6369       (mm-enable-multibyte-mule4)))
6370   (gnus-set-global-variables)
6371   (when (gnus-buffer-live-p gnus-article-buffer)
6372     (with-current-buffer gnus-article-buffer
6373       (setq gnus-article-charset gnus-newsgroup-charset)
6374       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6375       (mm-enable-multibyte-mule4)))
6376   (if (null article)
6377       nil
6378     (prog1
6379         (if gnus-summary-display-article-function
6380             (funcall gnus-summary-display-article-function article all-header)
6381           (gnus-article-prepare article all-header))
6382       (gnus-run-hooks 'gnus-select-article-hook)
6383       (when (and gnus-current-article
6384                  (not (zerop gnus-current-article)))
6385         (gnus-summary-goto-subject gnus-current-article))
6386       (gnus-summary-recenter)
6387       (when (and gnus-use-trees gnus-show-threads)
6388         (gnus-possibly-generate-tree article)
6389         (gnus-highlight-selected-tree article))
6390       ;; Successfully display article.
6391       (gnus-article-set-window-start
6392        (cdr (assq article gnus-newsgroup-bookmarks))))))
6393
6394 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6395   "Select the current article.
6396 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6397 non-nil, the article will be re-fetched even if it already present in
6398 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6399 be displayed."
6400   ;; Make sure we are in the summary buffer to work around bbdb bug.
6401   (unless (eq major-mode 'gnus-summary-mode)
6402     (set-buffer gnus-summary-buffer))
6403   (let ((article (or article (gnus-summary-article-number)))
6404         (all-headers (not (not all-headers))) ;Must be T or NIL.
6405         gnus-summary-display-article-function)
6406     (and (not pseudo)
6407          (gnus-summary-article-pseudo-p article)
6408          (error "This is a pseudo-article"))
6409     (save-excursion
6410       (set-buffer gnus-summary-buffer)
6411       (if (or (and gnus-single-article-buffer
6412                    (or (null gnus-current-article)
6413                        (null gnus-article-current)
6414                        (null (get-buffer gnus-article-buffer))
6415                        (not (eq article (cdr gnus-article-current)))
6416                        (not (equal (car gnus-article-current)
6417                                    gnus-newsgroup-name))))
6418               (and (not gnus-single-article-buffer)
6419                    (or (null gnus-current-article)
6420                        (not (eq gnus-current-article article))))
6421               force)
6422        ;; The requested article is different from the current article.
6423           (progn
6424             (gnus-summary-display-article article all-headers)
6425             (when (gnus-buffer-live-p gnus-article-buffer)
6426               (with-current-buffer gnus-article-buffer
6427                 (if (not gnus-article-decoded-p) ;; a local variable
6428                     (mm-disable-multibyte-mule4))))
6429             (when (or all-headers gnus-show-all-headers)
6430               (gnus-article-show-all-headers))
6431             (gnus-article-set-window-start
6432              (cdr (assq article gnus-newsgroup-bookmarks)))
6433             article)
6434         (when (or all-headers gnus-show-all-headers)
6435           (gnus-article-show-all-headers))
6436         'old))))
6437
6438 (defun gnus-summary-force-verify-and-decrypt ()
6439   (interactive)
6440   (let ((mm-verify-option 'known)
6441         (mm-decrypt-option 'known))
6442     (gnus-summary-select-article nil 'force)))
6443
6444 (defun gnus-summary-set-current-mark (&optional current-mark)
6445   "Obsolete function."
6446   nil)
6447
6448 (defun gnus-summary-next-article (&optional unread subject backward push)
6449   "Select the next article.
6450 If UNREAD, only unread articles are selected.
6451 If SUBJECT, only articles with SUBJECT are selected.
6452 If BACKWARD, the previous article is selected instead of the next."
6453   (interactive "P")
6454   (cond
6455    ;; Is there such an article?
6456    ((and (gnus-summary-search-forward unread subject backward)
6457          (or (gnus-summary-display-article (gnus-summary-article-number))
6458              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6459     (gnus-summary-position-point))
6460    ;; If not, we try the first unread, if that is wanted.
6461    ((and subject
6462          gnus-auto-select-same
6463          (gnus-summary-first-unread-article))
6464     (gnus-summary-position-point)
6465     (gnus-message 6 "Wrapped"))
6466    ;; Try to get next/previous article not displayed in this group.
6467    ((and gnus-auto-extend-newsgroup
6468          (not unread) (not subject))
6469     (gnus-summary-goto-article
6470      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6471      nil (count-lines (point-min) (point))))
6472    ;; Go to next/previous group.
6473    (t
6474     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6475       (gnus-summary-jump-to-group gnus-newsgroup-name))
6476     (let ((cmd last-command-char)
6477           (point
6478            (save-excursion
6479              (set-buffer gnus-group-buffer)
6480              (point)))
6481           (group
6482            (if (eq gnus-keep-same-level 'best)
6483                (gnus-summary-best-group gnus-newsgroup-name)
6484              (gnus-summary-search-group backward gnus-keep-same-level))))
6485       ;; For some reason, the group window gets selected.  We change
6486       ;; it back.
6487       (select-window (get-buffer-window (current-buffer)))
6488       ;; Select next unread newsgroup automagically.
6489       (cond
6490        ((or (not gnus-auto-select-next)
6491             (not cmd))
6492         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6493        ((or (eq gnus-auto-select-next 'quietly)
6494             (and (eq gnus-auto-select-next 'slightly-quietly)
6495                  push)
6496             (and (eq gnus-auto-select-next 'almost-quietly)
6497                  (gnus-summary-last-article-p)))
6498         ;; Select quietly.
6499         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6500             (gnus-summary-exit)
6501           (gnus-message 7 "No more%s articles (%s)..."
6502                         (if unread " unread" "")
6503                         (if group (concat "selecting " group)
6504                           "exiting"))
6505           (gnus-summary-next-group nil group backward)))
6506        (t
6507         (when (gnus-key-press-event-p last-input-event)
6508           (gnus-summary-walk-group-buffer
6509            gnus-newsgroup-name cmd unread backward point))))))))
6510
6511 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6512   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6513                       (?\C-p (gnus-group-prev-unread-group 1))))
6514         (cursor-in-echo-area t)
6515         keve key group ended)
6516     (save-excursion
6517       (set-buffer gnus-group-buffer)
6518       (goto-char start)
6519       (setq group
6520             (if (eq gnus-keep-same-level 'best)
6521                 (gnus-summary-best-group gnus-newsgroup-name)
6522               (gnus-summary-search-group backward gnus-keep-same-level))))
6523     (while (not ended)
6524       (gnus-message
6525        5 "No more%s articles%s" (if unread " unread" "")
6526        (if (and group
6527                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6528            (format " (Type %s for %s [%s])"
6529                    (single-key-description cmd) group
6530                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6531          (format " (Type %s to exit %s)"
6532                  (single-key-description cmd)
6533                  gnus-newsgroup-name)))
6534       ;; Confirm auto selection.
6535       (setq key (car (setq keve (gnus-read-event-char))))
6536       (setq ended t)
6537       (cond
6538        ((assq key keystrokes)
6539         (let ((obuf (current-buffer)))
6540           (switch-to-buffer gnus-group-buffer)
6541           (when group
6542             (gnus-group-jump-to-group group))
6543           (eval (cadr (assq key keystrokes)))
6544           (setq group (gnus-group-group-name))
6545           (switch-to-buffer obuf))
6546         (setq ended nil))
6547        ((equal key cmd)
6548         (if (or (not group)
6549                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6550             (gnus-summary-exit)
6551           (gnus-summary-next-group nil group backward)))
6552        (t
6553         (push (cdr keve) unread-command-events))))))
6554
6555 (defun gnus-summary-next-unread-article ()
6556   "Select unread article after current one."
6557   (interactive)
6558   (gnus-summary-next-article
6559    (or (not (eq gnus-summary-goto-unread 'never))
6560        (gnus-summary-last-article-p (gnus-summary-article-number)))
6561    (and gnus-auto-select-same
6562         (gnus-summary-article-subject))))
6563
6564 (defun gnus-summary-prev-article (&optional unread subject)
6565   "Select the article after the current one.
6566 If UNREAD is non-nil, only unread articles are selected."
6567   (interactive "P")
6568   (gnus-summary-next-article unread subject t))
6569
6570 (defun gnus-summary-prev-unread-article ()
6571   "Select unread article before current one."
6572   (interactive)
6573   (gnus-summary-prev-article
6574    (or (not (eq gnus-summary-goto-unread 'never))
6575        (gnus-summary-first-article-p (gnus-summary-article-number)))
6576    (and gnus-auto-select-same
6577         (gnus-summary-article-subject))))
6578
6579 (defun gnus-summary-next-page (&optional lines circular)
6580   "Show next page of the selected article.
6581 If at the end of the current article, select the next article.
6582 LINES says how many lines should be scrolled up.
6583
6584 If CIRCULAR is non-nil, go to the start of the article instead of
6585 selecting the next article when reaching the end of the current
6586 article."
6587   (interactive "P")
6588   (setq gnus-summary-buffer (current-buffer))
6589   (gnus-set-global-variables)
6590   (let ((article (gnus-summary-article-number))
6591         (article-window (get-buffer-window gnus-article-buffer t))
6592         endp)
6593     ;; If the buffer is empty, we have no article.
6594     (unless article
6595       (error "No article to select"))
6596     (gnus-configure-windows 'article)
6597     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6598         (if (and (eq gnus-summary-goto-unread 'never)
6599                  (not (gnus-summary-last-article-p article)))
6600             (gnus-summary-next-article)
6601           (gnus-summary-next-unread-article))
6602       (if (or (null gnus-current-article)
6603               (null gnus-article-current)
6604               (/= article (cdr gnus-article-current))
6605               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6606           ;; Selected subject is different from current article's.
6607           (gnus-summary-display-article article)
6608         (when article-window
6609           (gnus-eval-in-buffer-window gnus-article-buffer
6610             (setq endp (gnus-article-next-page lines)))
6611           (when endp
6612             (cond (circular
6613                    (gnus-summary-beginning-of-article))
6614                   (lines
6615                    (gnus-message 3 "End of message"))
6616                   ((null lines)
6617                    (if (and (eq gnus-summary-goto-unread 'never)
6618                             (not (gnus-summary-last-article-p article)))
6619                        (gnus-summary-next-article)
6620                      (gnus-summary-next-unread-article))))))))
6621     (gnus-summary-recenter)
6622     (gnus-summary-position-point)))
6623
6624 (defun gnus-summary-prev-page (&optional lines move)
6625   "Show previous page of selected article.
6626 Argument LINES specifies lines to be scrolled down.
6627 If MOVE, move to the previous unread article if point is at
6628 the beginning of the buffer."
6629   (interactive "P")
6630   (let ((article (gnus-summary-article-number))
6631         (article-window (get-buffer-window gnus-article-buffer t))
6632         endp)
6633     (gnus-configure-windows 'article)
6634     (if (or (null gnus-current-article)
6635             (null gnus-article-current)
6636             (/= article (cdr gnus-article-current))
6637             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6638         ;; Selected subject is different from current article's.
6639         (gnus-summary-display-article article)
6640       (gnus-summary-recenter)
6641       (when article-window
6642         (gnus-eval-in-buffer-window gnus-article-buffer
6643           (setq endp (gnus-article-prev-page lines)))
6644         (when (and move endp)
6645           (cond (lines
6646                  (gnus-message 3 "Beginning of message"))
6647                 ((null lines)
6648                  (if (and (eq gnus-summary-goto-unread 'never)
6649                           (not (gnus-summary-first-article-p article)))
6650                      (gnus-summary-prev-article)
6651                    (gnus-summary-prev-unread-article))))))))
6652   (gnus-summary-position-point))
6653
6654 (defun gnus-summary-prev-page-or-article (&optional lines)
6655   "Show previous page of selected article.
6656 Argument LINES specifies lines to be scrolled down.
6657 If at the beginning of the article, go to the next article."
6658   (interactive "P")
6659   (gnus-summary-prev-page lines t))
6660
6661 (defun gnus-summary-scroll-up (lines)
6662   "Scroll up (or down) one line current article.
6663 Argument LINES specifies lines to be scrolled up (or down if negative)."
6664   (interactive "p")
6665   (gnus-configure-windows 'article)
6666   (gnus-summary-show-thread)
6667   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6668     (gnus-eval-in-buffer-window gnus-article-buffer
6669       (cond ((> lines 0)
6670              (when (gnus-article-next-page lines)
6671                (gnus-message 3 "End of message")))
6672             ((< lines 0)
6673              (gnus-article-prev-page (- lines))))))
6674   (gnus-summary-recenter)
6675   (gnus-summary-position-point))
6676
6677 (defun gnus-summary-scroll-down (lines)
6678   "Scroll down (or up) one line current article.
6679 Argument LINES specifies lines to be scrolled down (or up if negative)."
6680   (interactive "p")
6681   (gnus-summary-scroll-up (- lines)))
6682
6683 (defun gnus-summary-next-same-subject ()
6684   "Select next article which has the same subject as current one."
6685   (interactive)
6686   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6687
6688 (defun gnus-summary-prev-same-subject ()
6689   "Select previous article which has the same subject as current one."
6690   (interactive)
6691   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6692
6693 (defun gnus-summary-next-unread-same-subject ()
6694   "Select next unread article which has the same subject as current one."
6695   (interactive)
6696   (gnus-summary-next-article t (gnus-summary-article-subject)))
6697
6698 (defun gnus-summary-prev-unread-same-subject ()
6699   "Select previous unread article which has the same subject as current one."
6700   (interactive)
6701   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6702
6703 (defun gnus-summary-first-unread-article ()
6704   "Select the first unread article.
6705 Return nil if there are no unread articles."
6706   (interactive)
6707   (prog1
6708       (when (gnus-summary-first-subject t)
6709         (gnus-summary-show-thread)
6710         (gnus-summary-first-subject t)
6711         (gnus-summary-display-article (gnus-summary-article-number)))
6712     (gnus-summary-position-point)))
6713
6714 (defun gnus-summary-first-unread-subject ()
6715   "Place the point on the subject line of the first unread article.
6716 Return nil if there are no unread articles."
6717   (interactive)
6718   (prog1
6719       (when (gnus-summary-first-subject t)
6720         (gnus-summary-show-thread)
6721         (gnus-summary-first-subject t))
6722     (gnus-summary-position-point)))
6723
6724 (defun gnus-summary-first-article ()
6725   "Select the first article.
6726 Return nil if there are no articles."
6727   (interactive)
6728   (prog1
6729       (when (gnus-summary-first-subject)
6730         (gnus-summary-show-thread)
6731         (gnus-summary-first-subject)
6732         (gnus-summary-display-article (gnus-summary-article-number)))
6733     (gnus-summary-position-point)))
6734
6735 (defun gnus-summary-best-unread-article ()
6736   "Select the unread article with the highest score."
6737   (interactive)
6738   (let ((best -1000000)
6739         (data gnus-newsgroup-data)
6740         article score)
6741     (while data
6742       (and (gnus-data-unread-p (car data))
6743            (> (setq score
6744                     (gnus-summary-article-score (gnus-data-number (car data))))
6745               best)
6746            (setq best score
6747                  article (gnus-data-number (car data))))
6748       (setq data (cdr data)))
6749     (prog1
6750         (if article
6751             (gnus-summary-goto-article article)
6752           (error "No unread articles"))
6753       (gnus-summary-position-point))))
6754
6755 (defun gnus-summary-last-subject ()
6756   "Go to the last displayed subject line in the group."
6757   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6758     (when article
6759       (gnus-summary-goto-subject article))))
6760
6761 (defun gnus-summary-goto-article (article &optional all-headers force)
6762   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6763 If ALL-HEADERS is non-nil, no header lines are hidden.
6764 If FORCE, go to the article even if it isn't displayed.  If FORCE
6765 is a number, it is the line the article is to be displayed on."
6766   (interactive
6767    (list
6768     (completing-read
6769      "Article number or Message-ID: "
6770      (mapcar (lambda (number) (list (int-to-string number)))
6771              gnus-newsgroup-limit))
6772     current-prefix-arg
6773     t))
6774   (prog1
6775       (if (and (stringp article)
6776                (string-match "@" article))
6777           (gnus-summary-refer-article article)
6778         (when (stringp article)
6779           (setq article (string-to-number article)))
6780         (if (gnus-summary-goto-subject article force)
6781             (gnus-summary-display-article article all-headers)
6782           (gnus-message 4 "Couldn't go to article %s" article) nil))
6783     (gnus-summary-position-point)))
6784
6785 (defun gnus-summary-goto-last-article ()
6786   "Go to the previously read article."
6787   (interactive)
6788   (prog1
6789       (when gnus-last-article
6790         (gnus-summary-goto-article gnus-last-article nil t))
6791     (gnus-summary-position-point)))
6792
6793 (defun gnus-summary-pop-article (number)
6794   "Pop one article off the history and go to the previous.
6795 NUMBER articles will be popped off."
6796   (interactive "p")
6797   (let (to)
6798     (setq gnus-newsgroup-history
6799           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6800     (if to
6801         (gnus-summary-goto-article (car to) nil t)
6802       (error "Article history empty")))
6803   (gnus-summary-position-point))
6804
6805 ;; Summary commands and functions for limiting the summary buffer.
6806
6807 (defun gnus-summary-limit-to-articles (n)
6808   "Limit the summary buffer to the next N articles.
6809 If not given a prefix, use the process marked articles instead."
6810   (interactive "P")
6811   (prog1
6812       (let ((articles (gnus-summary-work-articles n)))
6813         (setq gnus-newsgroup-processable nil)
6814         (gnus-summary-limit articles))
6815     (gnus-summary-position-point)))
6816
6817 (defun gnus-summary-pop-limit (&optional total)
6818   "Restore the previous limit.
6819 If given a prefix, remove all limits."
6820   (interactive "P")
6821   (when total
6822     (setq gnus-newsgroup-limits
6823           (list (mapcar (lambda (h) (mail-header-number h))
6824                         gnus-newsgroup-headers))))
6825   (unless gnus-newsgroup-limits
6826     (error "No limit to pop"))
6827   (prog1
6828       (gnus-summary-limit nil 'pop)
6829     (gnus-summary-position-point)))
6830
6831 (defun gnus-summary-limit-to-subject (subject &optional header)
6832   "Limit the summary buffer to articles that have subjects that match a regexp."
6833   (interactive "sLimit to subject (regexp): ")
6834   (unless header
6835     (setq header "subject"))
6836   (when (not (equal "" subject))
6837     (prog1
6838         (let ((articles (gnus-summary-find-matching
6839                          (or header "subject") subject 'all)))
6840           (unless articles
6841             (error "Found no matches for \"%s\"" subject))
6842           (gnus-summary-limit articles))
6843       (gnus-summary-position-point))))
6844
6845 (defun gnus-summary-limit-to-author (from)
6846   "Limit the summary buffer to articles that have authors that match a regexp."
6847   (interactive "sLimit to author (regexp): ")
6848   (gnus-summary-limit-to-subject from "from"))
6849
6850 (defun gnus-summary-limit-to-age (age &optional younger-p)
6851   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6852 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6853 articles that are younger than AGE days."
6854   (interactive
6855    (let ((younger current-prefix-arg)
6856          (days-got nil)
6857          days)
6858      (while (not days-got)
6859        (setq days (if younger
6860                       (read-string "Limit to articles within (in days): ")
6861                     (read-string "Limit to articles older than (in days): ")))
6862        (when (> (length days) 0)
6863          (setq days (read days)))
6864        (if (numberp days)
6865            (setq days-got t)
6866          (message "Please enter a number.")
6867          (sleep-for 1)))
6868      (list days younger)))
6869   (prog1
6870       (let ((data gnus-newsgroup-data)
6871             (cutoff (days-to-time age))
6872             articles d date is-younger)
6873         (while (setq d (pop data))
6874           (when (and (vectorp (gnus-data-header d))
6875                      (setq date (mail-header-date (gnus-data-header d))))
6876             (setq is-younger (time-less-p
6877                               (time-since (condition-case ()
6878                                               (date-to-time date)
6879                                             (error '(0 0))))
6880                               cutoff))
6881             (when (if younger-p
6882                       is-younger
6883                     (not is-younger))
6884               (push (gnus-data-number d) articles))))
6885         (gnus-summary-limit (nreverse articles)))
6886     (gnus-summary-position-point)))
6887
6888 (defun gnus-summary-limit-to-extra (header regexp)
6889   "Limit the summary buffer to articles that match an 'extra' header."
6890   (interactive
6891    (let ((header
6892           (intern
6893            (gnus-completing-read
6894             (symbol-name (car gnus-extra-headers))
6895             "Limit extra header:"
6896             (mapcar (lambda (x)
6897                       (cons (symbol-name x) x))
6898                     gnus-extra-headers)
6899             nil
6900             t))))
6901      (list header
6902            (read-string (format "Limit to header %s (regexp): " header)))))
6903   (when (not (equal "" regexp))
6904     (prog1
6905         (let ((articles (gnus-summary-find-matching
6906                          (cons 'extra header) regexp 'all)))
6907           (unless articles
6908             (error "Found no matches for \"%s\"" regexp))
6909           (gnus-summary-limit articles))
6910       (gnus-summary-position-point))))
6911
6912 (defun gnus-summary-limit-to-display-predicate ()
6913   "Limit the summary buffer to the predicated in the `display' group parameter."
6914   (interactive)
6915   (unless gnus-newsgroup-display
6916     (error "There is no `display' group parameter"))
6917   (let (articles)
6918     (dolist (number gnus-newsgroup-articles)
6919       (when (funcall gnus-newsgroup-display)
6920         (push number articles)))
6921     (gnus-summary-limit articles))
6922   (gnus-summary-position-point))
6923
6924 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6925 (make-obsolete
6926  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6927
6928 (defun gnus-summary-limit-to-unread (&optional all)
6929   "Limit the summary buffer to articles that are not marked as read.
6930 If ALL is non-nil, limit strictly to unread articles."
6931   (interactive "P")
6932   (if all
6933       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6934     (gnus-summary-limit-to-marks
6935      ;; Concat all the marks that say that an article is read and have
6936      ;; those removed.
6937      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6938            gnus-killed-mark gnus-kill-file-mark
6939            gnus-low-score-mark gnus-expirable-mark
6940            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6941            gnus-duplicate-mark gnus-souped-mark)
6942      'reverse)))
6943
6944 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6945 (make-obsolete 'gnus-summary-delete-marked-with
6946                'gnus-summary-limit-exlude-marks)
6947
6948 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6949   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6950 If REVERSE, limit the summary buffer to articles that are marked
6951 with MARKS.  MARKS can either be a string of marks or a list of marks.
6952 Returns how many articles were removed."
6953   (interactive "sMarks: ")
6954   (gnus-summary-limit-to-marks marks t))
6955
6956 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6957   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6958 If REVERSE (the prefix), limit the summary buffer to articles that are
6959 not marked with MARKS.  MARKS can either be a string of marks or a
6960 list of marks.
6961 Returns how many articles were removed."
6962   (interactive "sMarks: \nP")
6963   (prog1
6964       (let ((data gnus-newsgroup-data)
6965             (marks (if (listp marks) marks
6966                      (append marks nil))) ; Transform to list.
6967             articles)
6968         (while data
6969           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6970                   (memq (gnus-data-mark (car data)) marks))
6971             (push (gnus-data-number (car data)) articles))
6972           (setq data (cdr data)))
6973         (gnus-summary-limit articles))
6974     (gnus-summary-position-point)))
6975
6976 (defun gnus-summary-limit-to-score (score)
6977   "Limit to articles with score at or above SCORE."
6978   (interactive "NLimit to articles with score of at least: ")
6979   (let ((data gnus-newsgroup-data)
6980         articles)
6981     (while data
6982       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6983                 score)
6984         (push (gnus-data-number (car data)) articles))
6985       (setq data (cdr data)))
6986     (prog1
6987         (gnus-summary-limit articles)
6988       (gnus-summary-position-point))))
6989
6990 (defun gnus-summary-limit-include-thread (id)
6991   "Display all the hidden articles that is in the thread with ID in it.
6992 When called interactively, ID is the Message-ID of the current
6993 article."
6994   (interactive (list (mail-header-id (gnus-summary-article-header))))
6995   (let ((articles (gnus-articles-in-thread
6996                    (gnus-id-to-thread (gnus-root-id id)))))
6997     (prog1
6998         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6999       (gnus-summary-limit-include-matching-articles
7000        "subject"
7001        (regexp-quote (gnus-simplify-subject-re
7002                       (mail-header-subject (gnus-id-to-header id)))))
7003       (gnus-summary-position-point))))
7004
7005 (defun gnus-summary-limit-include-matching-articles (header regexp)
7006   "Display all the hidden articles that have HEADERs that match REGEXP."
7007   (interactive (list (read-string "Match on header: ")
7008                      (read-string "Regexp: ")))
7009   (let ((articles (gnus-find-matching-articles header regexp)))
7010     (prog1
7011         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
7012       (gnus-summary-position-point))))
7013
7014 (defun gnus-summary-limit-include-dormant ()
7015   "Display all the hidden articles that are marked as dormant.
7016 Note that this command only works on a subset of the articles currently
7017 fetched for this group."
7018   (interactive)
7019   (unless gnus-newsgroup-dormant
7020     (error "There are no dormant articles in this group"))
7021   (prog1
7022       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
7023     (gnus-summary-position-point)))
7024
7025 (defun gnus-summary-limit-exclude-dormant ()
7026   "Hide all dormant articles."
7027   (interactive)
7028   (prog1
7029       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7030     (gnus-summary-position-point)))
7031
7032 (defun gnus-summary-limit-exclude-childless-dormant ()
7033   "Hide all dormant articles that have no children."
7034   (interactive)
7035   (let ((data (gnus-data-list t))
7036         articles d children)
7037     ;; Find all articles that are either not dormant or have
7038     ;; children.
7039     (while (setq d (pop data))
7040       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7041                 (and (setq children
7042                            (gnus-article-children (gnus-data-number d)))
7043                      (let (found)
7044                        (while children
7045                          (when (memq (car children) articles)
7046                            (setq children nil
7047                                  found t))
7048                          (pop children))
7049                        found)))
7050         (push (gnus-data-number d) articles)))
7051     ;; Do the limiting.
7052     (prog1
7053         (gnus-summary-limit articles)
7054       (gnus-summary-position-point))))
7055
7056 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7057   "Mark all unread excluded articles as read.
7058 If ALL, mark even excluded ticked and dormants as read."
7059   (interactive "P")
7060   (let ((articles (gnus-sorted-complement
7061                    (sort
7062                     (mapcar (lambda (h) (mail-header-number h))
7063                             gnus-newsgroup-headers)
7064                     '<)
7065                    (sort gnus-newsgroup-limit '<)))
7066         article)
7067     (setq gnus-newsgroup-unreads
7068           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7069     (if all
7070         (setq gnus-newsgroup-dormant nil
7071               gnus-newsgroup-marked nil
7072               gnus-newsgroup-reads
7073               (nconc
7074                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7075                gnus-newsgroup-reads))
7076       (while (setq article (pop articles))
7077         (unless (or (memq article gnus-newsgroup-dormant)
7078                     (memq article gnus-newsgroup-marked))
7079           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7080
7081 (defun gnus-summary-limit (articles &optional pop)
7082   (if pop
7083       ;; We pop the previous limit off the stack and use that.
7084       (setq articles (car gnus-newsgroup-limits)
7085             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7086     ;; We use the new limit, so we push the old limit on the stack.
7087     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7088   ;; Set the limit.
7089   (setq gnus-newsgroup-limit articles)
7090   (let ((total (length gnus-newsgroup-data))
7091         (data (gnus-data-find-list (gnus-summary-article-number)))
7092         (gnus-summary-mark-below nil)   ; Inhibit this.
7093         found)
7094     ;; This will do all the work of generating the new summary buffer
7095     ;; according to the new limit.
7096     (gnus-summary-prepare)
7097     ;; Hide any threads, possibly.
7098     (and gnus-show-threads
7099          gnus-thread-hide-subtree
7100          (gnus-summary-hide-all-threads))
7101     ;; Try to return to the article you were at, or one in the
7102     ;; neighborhood.
7103     (when data
7104       ;; We try to find some article after the current one.
7105       (while data
7106         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7107           (setq data nil
7108                 found t))
7109         (setq data (cdr data))))
7110     (unless found
7111       ;; If there is no data, that means that we were after the last
7112       ;; article.  The same goes when we can't find any articles
7113       ;; after the current one.
7114       (goto-char (point-max))
7115       (gnus-summary-find-prev))
7116     (gnus-set-mode-line 'summary)
7117     ;; We return how many articles were removed from the summary
7118     ;; buffer as a result of the new limit.
7119     (- total (length gnus-newsgroup-data))))
7120
7121 (defsubst gnus-invisible-cut-children (threads)
7122   (let ((num 0))
7123     (while threads
7124       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7125         (incf num))
7126       (pop threads))
7127     (< num 2)))
7128
7129 (defsubst gnus-cut-thread (thread)
7130   "Go forwards in the thread until we find an article that we want to display."
7131   (when (or (eq gnus-fetch-old-headers 'some)
7132             (eq gnus-fetch-old-headers 'invisible)
7133             (numberp gnus-fetch-old-headers)
7134             (eq gnus-build-sparse-threads 'some)
7135             (eq gnus-build-sparse-threads 'more))
7136     ;; Deal with old-fetched headers and sparse threads.
7137     (while (and
7138             thread
7139             (or
7140              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7141              (gnus-summary-article-ancient-p
7142               (mail-header-number (car thread))))
7143             (if (or (<= (length (cdr thread)) 1)
7144                     (eq gnus-fetch-old-headers 'invisible))
7145                 (setq gnus-newsgroup-limit
7146                       (delq (mail-header-number (car thread))
7147                             gnus-newsgroup-limit)
7148                       thread (cadr thread))
7149               (when (gnus-invisible-cut-children (cdr thread))
7150                 (let ((th (cdr thread)))
7151                   (while th
7152                     (if (memq (mail-header-number (caar th))
7153                               gnus-newsgroup-limit)
7154                         (setq thread (car th)
7155                               th nil)
7156                       (setq th (cdr th))))))))))
7157   thread)
7158
7159 (defun gnus-cut-threads (threads)
7160   "Cut off all uninteresting articles from the beginning of threads."
7161   (when (or (eq gnus-fetch-old-headers 'some)
7162             (eq gnus-fetch-old-headers 'invisible)
7163             (numberp gnus-fetch-old-headers)
7164             (eq gnus-build-sparse-threads 'some)
7165             (eq gnus-build-sparse-threads 'more))
7166     (let ((th threads))
7167       (while th
7168         (setcar th (gnus-cut-thread (car th)))
7169         (setq th (cdr th)))))
7170   ;; Remove nixed out threads.
7171   (delq nil threads))
7172
7173 (defun gnus-summary-initial-limit (&optional show-if-empty)
7174   "Figure out what the initial limit is supposed to be on group entry.
7175 This entails weeding out unwanted dormants, low-scored articles,
7176 fetch-old-headers verbiage, and so on."
7177   ;; Most groups have nothing to remove.
7178   (if (or gnus-inhibit-limiting
7179           (and (null gnus-newsgroup-dormant)
7180                (eq gnus-newsgroup-display 'gnus-not-ignore)
7181                (not (eq gnus-fetch-old-headers 'some))
7182                (not (numberp gnus-fetch-old-headers))
7183                (not (eq gnus-fetch-old-headers 'invisible))
7184                (null gnus-summary-expunge-below)
7185                (not (eq gnus-build-sparse-threads 'some))
7186                (not (eq gnus-build-sparse-threads 'more))
7187                (null gnus-thread-expunge-below)
7188                (not gnus-use-nocem)))
7189       ()                                ; Do nothing.
7190     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7191     (setq gnus-newsgroup-limit nil)
7192     (mapatoms
7193      (lambda (node)
7194        (unless (car (symbol-value node))
7195          ;; These threads have no parents -- they are roots.
7196          (let ((nodes (cdr (symbol-value node)))
7197                thread)
7198            (while nodes
7199              (if (and gnus-thread-expunge-below
7200                       (< (gnus-thread-total-score (car nodes))
7201                          gnus-thread-expunge-below))
7202                  (gnus-expunge-thread (pop nodes))
7203                (setq thread (pop nodes))
7204                (gnus-summary-limit-children thread))))))
7205      gnus-newsgroup-dependencies)
7206     ;; If this limitation resulted in an empty group, we might
7207     ;; pop the previous limit and use it instead.
7208     (when (and (not gnus-newsgroup-limit)
7209                show-if-empty)
7210       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7211     gnus-newsgroup-limit))
7212
7213 (defun gnus-summary-limit-children (thread)
7214   "Return 1 if this subthread is visible and 0 if it is not."
7215   ;; First we get the number of visible children to this thread.  This
7216   ;; is done by recursing down the thread using this function, so this
7217   ;; will really go down to a leaf article first, before slowly
7218   ;; working its way up towards the root.
7219   (when thread
7220     (let ((children
7221            (if (cdr thread)
7222                (apply '+ (mapcar 'gnus-summary-limit-children
7223                                  (cdr thread)))
7224              0))
7225           (number (mail-header-number (car thread)))
7226           score)
7227       (if (and
7228            (not (memq number gnus-newsgroup-marked))
7229            (or
7230             ;; If this article is dormant and has absolutely no visible
7231             ;; children, then this article isn't visible.
7232             (and (memq number gnus-newsgroup-dormant)
7233                  (zerop children))
7234             ;; If this is "fetch-old-headered" and there is no
7235             ;; visible children, then we don't want this article.
7236             (and (or (eq gnus-fetch-old-headers 'some)
7237                      (numberp gnus-fetch-old-headers))
7238                  (gnus-summary-article-ancient-p number)
7239                  (zerop children))
7240             ;; If this is "fetch-old-headered" and `invisible', then
7241             ;; we don't want this article.
7242             (and (eq gnus-fetch-old-headers 'invisible)
7243                  (gnus-summary-article-ancient-p number))
7244             ;; If this is a sparsely inserted article with no children,
7245             ;; we don't want it.
7246             (and (eq gnus-build-sparse-threads 'some)
7247                  (gnus-summary-article-sparse-p number)
7248                  (zerop children))
7249             ;; If we use expunging, and this article is really
7250             ;; low-scored, then we don't want this article.
7251             (when (and gnus-summary-expunge-below
7252                        (< (setq score
7253                                 (or (cdr (assq number gnus-newsgroup-scored))
7254                                     gnus-summary-default-score))
7255                           gnus-summary-expunge-below))
7256               ;; We increase the expunge-tally here, but that has
7257               ;; nothing to do with the limits, really.
7258               (incf gnus-newsgroup-expunged-tally)
7259               ;; We also mark as read here, if that's wanted.
7260               (when (and gnus-summary-mark-below
7261                          (< score gnus-summary-mark-below))
7262                 (setq gnus-newsgroup-unreads
7263                       (delq number gnus-newsgroup-unreads))
7264                 (if gnus-newsgroup-auto-expire
7265                     (push number gnus-newsgroup-expirable)
7266                   (push (cons number gnus-low-score-mark)
7267                         gnus-newsgroup-reads)))
7268               t)
7269             ;; Do the `display' group parameter.
7270             (and gnus-newsgroup-display
7271                  (not (funcall gnus-newsgroup-display)))
7272             ;; Check NoCeM things.
7273             (if (and gnus-use-nocem
7274                      (gnus-nocem-unwanted-article-p
7275                       (mail-header-id (car thread))))
7276                 (progn
7277                   (setq gnus-newsgroup-unreads
7278                         (delq number gnus-newsgroup-unreads))
7279                   t))))
7280           ;; Nope, invisible article.
7281           0
7282         ;; Ok, this article is to be visible, so we add it to the limit
7283         ;; and return 1.
7284         (push number gnus-newsgroup-limit)
7285         1))))
7286
7287 (defun gnus-expunge-thread (thread)
7288   "Mark all articles in THREAD as read."
7289   (let* ((number (mail-header-number (car thread))))
7290     (incf gnus-newsgroup-expunged-tally)
7291     ;; We also mark as read here, if that's wanted.
7292     (setq gnus-newsgroup-unreads
7293           (delq number gnus-newsgroup-unreads))
7294     (if gnus-newsgroup-auto-expire
7295         (push number gnus-newsgroup-expirable)
7296       (push (cons number gnus-low-score-mark)
7297             gnus-newsgroup-reads)))
7298   ;; Go recursively through all subthreads.
7299   (mapcar 'gnus-expunge-thread (cdr thread)))
7300
7301 ;; Summary article oriented commands
7302
7303 (defun gnus-summary-refer-parent-article (n)
7304   "Refer parent article N times.
7305 If N is negative, go to ancestor -N instead.
7306 The difference between N and the number of articles fetched is returned."
7307   (interactive "p")
7308   (let ((skip 1)
7309         error header ref)
7310     (when (not (natnump n))
7311       (setq skip (abs n)
7312             n 1))
7313     (while (and (> n 0)
7314                 (not error))
7315       (setq header (gnus-summary-article-header))
7316       (if (and (eq (mail-header-number header)
7317                    (cdr gnus-article-current))
7318                (equal gnus-newsgroup-name
7319                       (car gnus-article-current)))
7320           ;; If we try to find the parent of the currently
7321           ;; displayed article, then we take a look at the actual
7322           ;; References header, since this is slightly more
7323           ;; reliable than the References field we got from the
7324           ;; server.
7325           (save-excursion
7326             (set-buffer gnus-original-article-buffer)
7327             (nnheader-narrow-to-headers)
7328             (unless (setq ref (message-fetch-field "references"))
7329               (setq ref (message-fetch-field "in-reply-to")))
7330             (widen))
7331         (setq ref
7332               ;; It's not the current article, so we take a bet on
7333               ;; the value we got from the server.
7334               (mail-header-references header)))
7335       (if (and ref
7336                (not (equal ref "")))
7337           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7338             (gnus-message 1 "Couldn't find parent"))
7339         (gnus-message 1 "No references in article %d"
7340                       (gnus-summary-article-number))
7341         (setq error t))
7342       (decf n))
7343     (gnus-summary-position-point)
7344     n))
7345
7346 (defun gnus-summary-refer-references ()
7347   "Fetch all articles mentioned in the References header.
7348 Return the number of articles fetched."
7349   (interactive)
7350   (let ((ref (mail-header-references (gnus-summary-article-header)))
7351         (current (gnus-summary-article-number))
7352         (n 0))
7353     (if (or (not ref)
7354             (equal ref ""))
7355         (error "No References in the current article")
7356       ;; For each Message-ID in the References header...
7357       (while (string-match "<[^>]*>" ref)
7358         (incf n)
7359         ;; ... fetch that article.
7360         (gnus-summary-refer-article
7361          (prog1 (match-string 0 ref)
7362            (setq ref (substring ref (match-end 0))))))
7363       (gnus-summary-goto-subject current)
7364       (gnus-summary-position-point)
7365       n)))
7366
7367 (defun gnus-summary-refer-thread (&optional limit)
7368   "Fetch all articles in the current thread.
7369 If LIMIT (the numerical prefix), fetch that many old headers instead
7370 of what's specified by the `gnus-refer-thread-limit' variable."
7371   (interactive "P")
7372   (let ((id (mail-header-id (gnus-summary-article-header)))
7373         (limit (if limit (prefix-numeric-value limit)
7374                  gnus-refer-thread-limit)))
7375     ;; We want to fetch LIMIT *old* headers, but we also have to
7376     ;; re-fetch all the headers in the current buffer, because many of
7377     ;; them may be undisplayed.  So we adjust LIMIT.
7378     (when (numberp limit)
7379       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7380     (unless (eq gnus-fetch-old-headers 'invisible)
7381       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7382       ;; Retrieve the headers and read them in.
7383       (if (eq (gnus-retrieve-headers
7384                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7385               'nov)
7386           (gnus-build-all-threads)
7387         (error "Can't fetch thread from backends that don't support NOV"))
7388       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7389     (gnus-summary-limit-include-thread id)))
7390
7391 (defun gnus-summary-refer-article (message-id)
7392   "Fetch an article specified by MESSAGE-ID."
7393   (interactive "sMessage-ID: ")
7394   (when (and (stringp message-id)
7395              (not (zerop (length message-id))))
7396     ;; Construct the correct Message-ID if necessary.
7397     ;; Suggested by tale@pawl.rpi.edu.
7398     (unless (string-match "^<" message-id)
7399       (setq message-id (concat "<" message-id)))
7400     (unless (string-match ">$" message-id)
7401       (setq message-id (concat message-id ">")))
7402     (let* ((header (gnus-id-to-header message-id))
7403            (sparse (and header
7404                         (gnus-summary-article-sparse-p
7405                          (mail-header-number header))
7406                         (memq (mail-header-number header)
7407                               gnus-newsgroup-limit)))
7408            number)
7409       (cond
7410        ;; If the article is present in the buffer we just go to it.
7411        ((and header
7412              (or (not (gnus-summary-article-sparse-p
7413                        (mail-header-number header)))
7414                  sparse))
7415         (prog1
7416             (gnus-summary-goto-article
7417              (mail-header-number header) nil t)
7418           (when sparse
7419             (gnus-summary-update-article (mail-header-number header)))))
7420        (t
7421         ;; We fetch the article.
7422         (catch 'found
7423           (dolist (gnus-override-method (gnus-refer-article-methods))
7424             (gnus-check-server gnus-override-method)
7425             ;; Fetch the header, and display the article.
7426             (when (setq number (gnus-summary-insert-subject message-id))
7427               (gnus-summary-select-article nil nil nil number)
7428               (throw 'found t)))
7429           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7430
7431 (defun gnus-refer-article-methods ()
7432   "Return a list of referrable methods."
7433   (cond
7434    ;; No method, so we default to current and native.
7435    ((null gnus-refer-article-method)
7436     (list gnus-current-select-method gnus-select-method))
7437    ;; Current.
7438    ((eq 'current gnus-refer-article-method)
7439     (list gnus-current-select-method))
7440    ;; List of select methods.
7441    ((not (and (symbolp (car gnus-refer-article-method))
7442               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7443     (let (out)
7444       (dolist (method gnus-refer-article-method)
7445         (push (if (eq 'current method)
7446                   gnus-current-select-method
7447                 method)
7448               out))
7449       (nreverse out)))
7450    ;; One single select method.
7451    (t
7452     (list gnus-refer-article-method))))
7453
7454 (defun gnus-summary-edit-parameters ()
7455   "Edit the group parameters of the current group."
7456   (interactive)
7457   (gnus-group-edit-group gnus-newsgroup-name 'params))
7458
7459 (defun gnus-summary-customize-parameters ()
7460   "Customize the group parameters of the current group."
7461   (interactive)
7462   (gnus-group-customize gnus-newsgroup-name))
7463
7464 (defun gnus-summary-enter-digest-group (&optional force)
7465   "Enter an nndoc group based on the current article.
7466 If FORCE, force a digest interpretation.  If not, try
7467 to guess what the document format is."
7468   (interactive "P")
7469   (let ((conf gnus-current-window-configuration))
7470     (save-excursion
7471       (gnus-summary-select-article))
7472     (setq gnus-current-window-configuration conf)
7473     (let* ((name (format "%s-%d"
7474                          (gnus-group-prefixed-name
7475                           gnus-newsgroup-name (list 'nndoc ""))
7476                          (save-excursion
7477                            (set-buffer gnus-summary-buffer)
7478                            gnus-current-article)))
7479            (ogroup gnus-newsgroup-name)
7480            (params (append (gnus-info-params (gnus-get-info ogroup))
7481                            (list (cons 'to-group ogroup))
7482                            (list (cons 'save-article-group ogroup))))
7483            (case-fold-search t)
7484            (buf (current-buffer))
7485            dig to-address)
7486       (save-excursion
7487         (set-buffer gnus-original-article-buffer)
7488         ;; Have the digest group inherit the main mail address of
7489         ;; the parent article.
7490         (when (setq to-address (or (message-fetch-field "reply-to")
7491                                    (message-fetch-field "from")))
7492           (setq params (append
7493                         (list (cons 'to-address
7494                                     (funcall gnus-decode-encoded-word-function
7495                                              to-address))))))
7496         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7497         (insert-buffer-substring gnus-original-article-buffer)
7498         ;; Remove lines that may lead nndoc to misinterpret the
7499         ;; document type.
7500         (narrow-to-region
7501          (goto-char (point-min))
7502          (or (search-forward "\n\n" nil t) (point)))
7503         (goto-char (point-min))
7504         (delete-matching-lines "^Path:\\|^From ")
7505         (widen))
7506       (unwind-protect
7507           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7508                     (gnus-newsgroup-ephemeral-ignored-charsets
7509                      gnus-newsgroup-ignored-charsets))
7510                 (gnus-group-read-ephemeral-group
7511                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7512                               (nndoc-article-type
7513                                ,(if force 'mbox 'guess))) t))
7514             ;; Make all postings to this group go to the parent group.
7515               (nconc (gnus-info-params (gnus-get-info name))
7516                      params)
7517             ;; Couldn't select this doc group.
7518             (switch-to-buffer buf)
7519             (gnus-set-global-variables)
7520             (gnus-configure-windows 'summary)
7521             (gnus-message 3 "Article couldn't be entered?"))
7522         (kill-buffer dig)))))
7523
7524 (defun gnus-summary-read-document (n)
7525   "Open a new group based on the current article(s).
7526 This will allow you to read digests and other similar
7527 documents as newsgroups.
7528 Obeys the standard process/prefix convention."
7529   (interactive "P")
7530   (let* ((articles (gnus-summary-work-articles n))
7531          (ogroup gnus-newsgroup-name)
7532          (params (append (gnus-info-params (gnus-get-info ogroup))
7533                          (list (cons 'to-group ogroup))))
7534          article group egroup groups vgroup)
7535     (while (setq article (pop articles))
7536       (setq group (format "%s-%d" gnus-newsgroup-name article))
7537       (gnus-summary-remove-process-mark article)
7538       (when (gnus-summary-display-article article)
7539         (save-excursion
7540           (with-temp-buffer
7541             (insert-buffer-substring gnus-original-article-buffer)
7542             ;; Remove some headers that may lead nndoc to make
7543             ;; the wrong guess.
7544             (message-narrow-to-head)
7545             (goto-char (point-min))
7546             (delete-matching-lines "^\\(Path\\):\\|^From ")
7547             (widen)
7548             (if (setq egroup
7549                       (gnus-group-read-ephemeral-group
7550                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7551                                      (nndoc-article-type guess))
7552                        t nil t))
7553                 (progn
7554             ;; Make all postings to this group go to the parent group.
7555                   (nconc (gnus-info-params (gnus-get-info egroup))
7556                          params)
7557                   (push egroup groups))
7558               ;; Couldn't select this doc group.
7559               (gnus-error 3 "Article couldn't be entered"))))))
7560     ;; Now we have selected all the documents.
7561     (cond
7562      ((not groups)
7563       (error "None of the articles could be interpreted as documents"))
7564      ((gnus-group-read-ephemeral-group
7565        (setq vgroup (format
7566                      "nnvirtual:%s-%s" gnus-newsgroup-name
7567                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7568        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7569        t
7570        (cons (current-buffer) 'summary)))
7571      (t
7572       (error "Couldn't select virtual nndoc group")))))
7573
7574 (defun gnus-summary-isearch-article (&optional regexp-p)
7575   "Do incremental search forward on the current article.
7576 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7577   (interactive "P")
7578   (gnus-summary-select-article)
7579   (gnus-configure-windows 'article)
7580   (gnus-eval-in-buffer-window gnus-article-buffer
7581     (save-restriction
7582       (widen)
7583       (isearch-forward regexp-p))))
7584
7585 (defun gnus-summary-search-article-forward (regexp &optional backward)
7586   "Search for an article containing REGEXP forward.
7587 If BACKWARD, search backward instead."
7588   (interactive
7589    (list (read-string
7590           (format "Search article %s (regexp%s): "
7591                   (if current-prefix-arg "backward" "forward")
7592                   (if gnus-last-search-regexp
7593                       (concat ", default " gnus-last-search-regexp)
7594                     "")))
7595          current-prefix-arg))
7596   (if (string-equal regexp "")
7597       (setq regexp (or gnus-last-search-regexp ""))
7598     (setq gnus-last-search-regexp regexp)
7599     (setq gnus-article-before-search gnus-current-article))
7600   ;; Intentionally set gnus-last-article.
7601   (setq gnus-last-article gnus-article-before-search)
7602   (let ((gnus-last-article gnus-last-article))
7603     (if (gnus-summary-search-article regexp backward)
7604         (gnus-summary-show-thread)
7605       (error "Search failed: \"%s\"" regexp))))
7606
7607 (defun gnus-summary-search-article-backward (regexp)
7608   "Search for an article containing REGEXP backward."
7609   (interactive
7610    (list (read-string
7611           (format "Search article backward (regexp%s): "
7612                   (if gnus-last-search-regexp
7613                       (concat ", default " gnus-last-search-regexp)
7614                     "")))))
7615   (gnus-summary-search-article-forward regexp 'backward))
7616
7617 (defun gnus-summary-search-article (regexp &optional backward)
7618   "Search for an article containing REGEXP.
7619 Optional argument BACKWARD means do search for backward.
7620 `gnus-select-article-hook' is not called during the search."
7621   ;; We have to require this here to make sure that the following
7622   ;; dynamic binding isn't shadowed by autoloading.
7623   (require 'gnus-async)
7624   (require 'gnus-art)
7625   (let ((gnus-select-article-hook nil)  ;Disable hook.
7626         (gnus-article-prepare-hook nil)
7627         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7628         (gnus-use-article-prefetch nil)
7629         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7630         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7631         (sum (current-buffer))
7632         (gnus-display-mime-function nil)
7633         (found nil)
7634         point)
7635     (gnus-save-hidden-threads
7636       (gnus-summary-select-article)
7637       (set-buffer gnus-article-buffer)
7638       (goto-char (window-point (get-buffer-window (current-buffer))))
7639       (when backward
7640         (forward-line -1))
7641       (while (not found)
7642         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7643         (if (if backward
7644                 (re-search-backward regexp nil t)
7645               (re-search-forward regexp nil t))
7646             ;; We found the regexp.
7647             (progn
7648               (setq found 'found)
7649               (beginning-of-line)
7650               (set-window-start
7651                (get-buffer-window (current-buffer))
7652                (point))
7653               (forward-line 1)
7654               (set-window-point
7655                (get-buffer-window (current-buffer))
7656                (point))
7657               (set-buffer sum)
7658               (setq point (point)))
7659           ;; We didn't find it, so we go to the next article.
7660           (set-buffer sum)
7661           (setq found 'not)
7662           (while (eq found 'not)
7663             (if (not (if backward (gnus-summary-find-prev)
7664                        (gnus-summary-find-next)))
7665                 ;; No more articles.
7666                 (setq found t)
7667               ;; Select the next article and adjust point.
7668               (unless (gnus-summary-article-sparse-p
7669                        (gnus-summary-article-number))
7670                 (setq found nil)
7671                 (gnus-summary-select-article)
7672                 (set-buffer gnus-article-buffer)
7673                 (widen)
7674                 (goto-char (if backward (point-max) (point-min))))))))
7675       (gnus-message 7 ""))
7676     ;; Return whether we found the regexp.
7677     (when (eq found 'found)
7678       (goto-char point)
7679       (gnus-summary-show-thread)
7680       (gnus-summary-goto-subject gnus-current-article)
7681       (gnus-summary-position-point)
7682       t)))
7683
7684 (defun gnus-find-matching-articles (header regexp)
7685   "Return a list of all articles that match REGEXP on HEADER.
7686 This search includes all articles in the current group that Gnus has
7687 fetched headers for, whether they are displayed or not."
7688   (let ((articles nil)
7689         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7690         (case-fold-search t))
7691     (dolist (header gnus-newsgroup-headers)
7692       (when (string-match regexp (funcall func header))
7693         (push (mail-header-number header) articles)))
7694     (nreverse articles)))
7695
7696 (defun gnus-summary-find-matching (header regexp &optional backward unread
7697                                           not-case-fold)
7698   "Return a list of all articles that match REGEXP on HEADER.
7699 The search stars on the current article and goes forwards unless
7700 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7701 If UNREAD is non-nil, only unread articles will
7702 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7703 in the comparisons."
7704   (let ((case-fold-search (not not-case-fold))
7705         articles d func)
7706     (if (consp header)
7707         (if (eq (car header) 'extra)
7708             (setq func
7709                   `(lambda (h)
7710                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7711                          "")))
7712           (error "%s is an invalid header" header))
7713       (unless (fboundp (intern (concat "mail-header-" header)))
7714         (error "%s is not a valid header" header))
7715       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7716     (dolist (d (if (eq backward 'all)
7717                    gnus-newsgroup-data
7718                  (gnus-data-find-list
7719                   (gnus-summary-article-number)
7720                   (gnus-data-list backward))))
7721       (when (and (or (not unread)       ; We want all articles...
7722                      (gnus-data-unread-p d)) ; Or just unreads.
7723                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7724                  (string-match regexp
7725                                (funcall func (gnus-data-header d)))) ; Match.
7726         (push (gnus-data-number d) articles))) ; Success!
7727     (nreverse articles)))
7728
7729 (defun gnus-summary-execute-command (header regexp command &optional backward)
7730   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7731 If HEADER is an empty string (or nil), the match is done on the entire
7732 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7733   (interactive
7734    (list (let ((completion-ignore-case t))
7735            (completing-read
7736             "Header name: "
7737             (mapcar (lambda (header) (list (format "%s" header)))
7738                     (append
7739                      '("Number" "Subject" "From" "Lines" "Date"
7740                        "Message-ID" "Xref" "References" "Body")
7741                      gnus-extra-headers))
7742             nil 'require-match))
7743          (read-string "Regexp: ")
7744          (read-key-sequence "Command: ")
7745          current-prefix-arg))
7746   (when (equal header "Body")
7747     (setq header ""))
7748   ;; Hidden thread subtrees must be searched as well.
7749   (gnus-summary-show-all-threads)
7750   ;; We don't want to change current point nor window configuration.
7751   (save-excursion
7752     (save-window-excursion
7753       (gnus-message 6 "Executing %s..." (key-description command))
7754 ;; We'd like to execute COMMAND interactively so as to give arguments.
7755       (gnus-execute header regexp
7756                     `(call-interactively ',(key-binding command))
7757                     backward)
7758       (gnus-message 6 "Executing %s...done" (key-description command)))))
7759
7760 (defun gnus-summary-beginning-of-article ()
7761   "Scroll the article back to the beginning."
7762   (interactive)
7763   (gnus-summary-select-article)
7764   (gnus-configure-windows 'article)
7765   (gnus-eval-in-buffer-window gnus-article-buffer
7766     (widen)
7767     (goto-char (point-min))
7768     (when gnus-page-broken
7769       (gnus-narrow-to-page))))
7770
7771 (defun gnus-summary-end-of-article ()
7772   "Scroll to the end of the article."
7773   (interactive)
7774   (gnus-summary-select-article)
7775   (gnus-configure-windows 'article)
7776   (gnus-eval-in-buffer-window gnus-article-buffer
7777     (widen)
7778     (goto-char (point-max))
7779     (recenter -3)
7780     (when gnus-page-broken
7781       (gnus-narrow-to-page))))
7782
7783 (defun gnus-summary-print-article (&optional filename n)
7784   "Generate and print a PostScript image of the N next (mail) articles.
7785
7786 If N is negative, print the N previous articles.  If N is nil and articles
7787 have been marked with the process mark, print these instead.
7788
7789 If the optional first argument FILENAME is nil, send the image to the
7790 printer.  If FILENAME is a string, save the PostScript image in a file with
7791 that name.  If FILENAME is a number, prompt the user for the name of the file
7792 to save in."
7793   (interactive (list (ps-print-preprint current-prefix-arg)))
7794   (dolist (article (gnus-summary-work-articles n))
7795     (gnus-summary-select-article nil nil 'pseudo article)
7796     (gnus-eval-in-buffer-window gnus-article-buffer
7797       (let ((buffer (generate-new-buffer " *print*")))
7798         (unwind-protect
7799             (progn
7800               (copy-to-buffer buffer (point-min) (point-max))
7801               (set-buffer buffer)
7802               (gnus-article-delete-invisible-text)
7803               (when (gnus-visual-p 'article-highlight 'highlight)
7804                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7805                 ;; highlight.
7806                 (let ((gnus-article-buffer buffer))
7807                   (gnus-article-highlight-citation t)
7808                   (gnus-article-highlight-signature)))
7809               (let ((ps-left-header
7810                      (list
7811                       (concat "("
7812                               (mail-header-subject gnus-current-headers) ")")
7813                       (concat "("
7814                               (mail-header-from gnus-current-headers) ")")))
7815                     (ps-right-header
7816                      (list
7817                       "/pagenumberstring load"
7818                       (concat "("
7819                               (mail-header-date gnus-current-headers) ")"))))
7820                 (gnus-run-hooks 'gnus-ps-print-hook)
7821                 (save-excursion
7822                   (if window-system
7823                       (ps-spool-buffer-with-faces)
7824                     (ps-spool-buffer)))))
7825           (kill-buffer buffer))))
7826     (gnus-summary-remove-process-mark article))
7827   (ps-despool filename))
7828
7829 (defun gnus-summary-show-article (&optional arg)
7830   "Force re-fetching of the current article.
7831 If ARG (the prefix) is a number, show the article with the charset
7832 defined in `gnus-summary-show-article-charset-alist', or the charset
7833 input.
7834 If ARG (the prefix) is non-nil and not a number, show the raw article
7835 without any article massaging functions being run."
7836   (interactive "P")
7837   (cond
7838    ((numberp arg)
7839     (gnus-summary-show-article t)
7840     (let ((gnus-newsgroup-charset
7841            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7842                (mm-read-coding-system
7843                 "View as charset: "
7844                 (save-excursion
7845                   (set-buffer gnus-article-buffer)
7846                   (let ((coding-systems
7847                          (detect-coding-region (point) (point-max))))
7848                     (or (car-safe coding-systems)
7849                         coding-systems))))))
7850           (gnus-newsgroup-ignored-charsets 'gnus-all))
7851       (gnus-summary-select-article nil 'force)
7852       (let ((deps gnus-newsgroup-dependencies)
7853             head header)
7854         (save-excursion
7855           (set-buffer gnus-original-article-buffer)
7856           (save-restriction
7857             (message-narrow-to-head)
7858             (setq head (buffer-string)))
7859           (with-temp-buffer
7860             (insert (format "211 %d Article retrieved.\n"
7861                             (cdr gnus-article-current)))
7862             (insert head)
7863             (insert ".\n")
7864             (let ((nntp-server-buffer (current-buffer)))
7865               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7866         (gnus-data-set-header
7867          (gnus-data-find (cdr gnus-article-current))
7868          header)
7869         (gnus-summary-update-article-line
7870          (cdr gnus-article-current) header)
7871         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7872           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7873    ((not arg)
7874     ;; Select the article the normal way.
7875     (gnus-summary-select-article nil 'force))
7876    (t
7877     ;; We have to require this here to make sure that the following
7878     ;; dynamic binding isn't shadowed by autoloading.
7879     (require 'gnus-async)
7880     (require 'gnus-art)
7881     ;; Bind the article treatment functions to nil.
7882     (let ((gnus-have-all-headers t)
7883           gnus-article-prepare-hook
7884           gnus-article-decode-hook
7885           gnus-display-mime-function
7886           gnus-break-pages)
7887       ;; Destroy any MIME parts.
7888       (when (gnus-buffer-live-p gnus-article-buffer)
7889         (save-excursion
7890           (set-buffer gnus-article-buffer)
7891           (mm-destroy-parts gnus-article-mime-handles)
7892           ;; Set it to nil for safety reason.
7893           (setq gnus-article-mime-handle-alist nil)
7894           (setq gnus-article-mime-handles nil)))
7895       (gnus-summary-select-article nil 'force))))
7896   (gnus-summary-goto-subject gnus-current-article)
7897   (gnus-summary-position-point))
7898
7899 (defun gnus-summary-show-raw-article ()
7900   "Show the raw article without any article massaging functions being run."
7901   (interactive)
7902   (gnus-summary-show-article t))
7903
7904 (defun gnus-summary-verbose-headers (&optional arg)
7905   "Toggle permanent full header display.
7906 If ARG is a positive number, turn header display on.
7907 If ARG is a negative number, turn header display off."
7908   (interactive "P")
7909   (setq gnus-show-all-headers
7910         (cond ((or (not (numberp arg))
7911                    (zerop arg))
7912                (not gnus-show-all-headers))
7913               ((natnump arg)
7914                t)))
7915   (gnus-summary-show-article))
7916
7917 (defun gnus-summary-toggle-header (&optional arg)
7918   "Show the headers if they are hidden, or hide them if they are shown.
7919 If ARG is a positive number, show the entire header.
7920 If ARG is a negative number, hide the unwanted header lines."
7921   (interactive "P")
7922   (save-excursion
7923     (set-buffer gnus-article-buffer)
7924     (save-restriction
7925       (let* ((buffer-read-only nil)
7926              (inhibit-point-motion-hooks t)
7927              hidden e)
7928         (setq hidden
7929               (if (numberp arg)
7930                   (>= arg 0)
7931                 (save-restriction
7932                   (article-narrow-to-head)
7933                   (gnus-article-hidden-text-p 'headers))))
7934         (goto-char (point-min))
7935         (when (search-forward "\n\n" nil t)
7936           (delete-region (point-min) (1- (point))))
7937         (goto-char (point-min))
7938         (save-excursion
7939           (set-buffer gnus-original-article-buffer)
7940           (goto-char (point-min))
7941           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7942         (insert-buffer-substring gnus-original-article-buffer 1 e)
7943         (save-restriction
7944           (narrow-to-region (point-min) (point))
7945           (article-decode-encoded-words)
7946           (if  hidden
7947               (let ((gnus-treat-hide-headers nil)
7948                     (gnus-treat-hide-boring-headers nil))
7949                 (setq gnus-article-wash-types
7950                       (delq 'headers gnus-article-wash-types))
7951                 (gnus-treat-article 'head))
7952             (gnus-treat-article 'head)))
7953         (gnus-set-mode-line 'article)))))
7954
7955 (defun gnus-summary-show-all-headers ()
7956   "Make all header lines visible."
7957   (interactive)
7958   (gnus-summary-toggle-header 1))
7959
7960 (defun gnus-summary-caesar-message (&optional arg)
7961   "Caesar rotate the current article by 13.
7962 The numerical prefix specifies how many places to rotate each letter
7963 forward."
7964   (interactive "P")
7965   (gnus-summary-select-article)
7966   (let ((mail-header-separator ""))
7967     (gnus-eval-in-buffer-window gnus-article-buffer
7968       (save-restriction
7969         (widen)
7970         (let ((start (window-start))
7971               buffer-read-only)
7972           (message-caesar-buffer-body arg)
7973           (set-window-start (get-buffer-window (current-buffer)) start))))))
7974
7975 (defun gnus-summary-stop-page-breaking ()
7976   "Stop page breaking in the current article."
7977   (interactive)
7978   (gnus-summary-select-article)
7979   (gnus-eval-in-buffer-window gnus-article-buffer
7980     (widen)
7981     (when (gnus-visual-p 'page-marker)
7982       (let ((buffer-read-only nil))
7983         (gnus-remove-text-with-property 'gnus-prev)
7984         (gnus-remove-text-with-property 'gnus-next))
7985       (setq gnus-page-broken nil))))
7986
7987 (defun gnus-summary-move-article (&optional n to-newsgroup
7988                                             select-method action)
7989   "Move the current article to a different newsgroup.
7990 If N is a positive number, move the N next articles.
7991 If N is a negative number, move the N previous articles.
7992 If N is nil and any articles have been marked with the process mark,
7993 move those articles instead.
7994 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7995 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7996 re-spool using this method.
7997
7998 For this function to work, both the current newsgroup and the
7999 newsgroup that you want to move to have to support the `request-move'
8000 and `request-accept' functions.
8001
8002 ACTION can be either `move' (the default), `crosspost' or `copy'."
8003   (interactive "P")
8004   (unless action
8005     (setq action 'move))
8006   ;; Check whether the source group supports the required functions.
8007   (cond ((and (eq action 'move)
8008               (not (gnus-check-backend-function
8009                     'request-move-article gnus-newsgroup-name)))
8010          (error "The current group does not support article moving"))
8011         ((and (eq action 'crosspost)
8012               (not (gnus-check-backend-function
8013                     'request-replace-article gnus-newsgroup-name)))
8014          (error "The current group does not support article editing")))
8015   (let ((articles (gnus-summary-work-articles n))
8016         (prefix (if (gnus-check-backend-function
8017                      'request-move-article gnus-newsgroup-name)
8018                     (gnus-group-real-prefix gnus-newsgroup-name)
8019                   ""))
8020         (names '((move "Move" "Moving")
8021                  (copy "Copy" "Copying")
8022                  (crosspost "Crosspost" "Crossposting")))
8023         (copy-buf (save-excursion
8024                     (nnheader-set-temp-buffer " *copy article*")))
8025         art-group to-method new-xref article to-groups)
8026     (unless (assq action names)
8027       (error "Unknown action %s" action))
8028     ;; Read the newsgroup name.
8029     (when (and (not to-newsgroup)
8030                (not select-method))
8031       (setq to-newsgroup
8032             (gnus-read-move-group-name
8033              (cadr (assq action names))
8034              (symbol-value (intern (format "gnus-current-%s-group" action)))
8035              articles prefix))
8036       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8037     (setq to-method (or select-method
8038                         (gnus-server-to-method
8039                          (gnus-group-method to-newsgroup))))
8040     ;; Check the method we are to move this article to...
8041     (unless (gnus-check-backend-function
8042              'request-accept-article (car to-method))
8043       (error "%s does not support article copying" (car to-method)))
8044     (unless (gnus-check-server to-method)
8045       (error "Can't open server %s" (car to-method)))
8046     (gnus-message 6 "%s to %s: %s..."
8047                   (caddr (assq action names))
8048                   (or (car select-method) to-newsgroup) articles)
8049     (while articles
8050       (setq article (pop articles))
8051       (setq
8052        art-group
8053        (cond
8054         ;; Move the article.
8055         ((eq action 'move)
8056          ;; Remove this article from future suppression.
8057          (gnus-dup-unsuppress-article article)
8058          (gnus-request-move-article
8059           article                       ; Article to move
8060           gnus-newsgroup-name           ; From newsgroup
8061           (nth 1 (gnus-find-method-for-group
8062                   gnus-newsgroup-name)) ; Server
8063           (list 'gnus-request-accept-article
8064                 to-newsgroup (list 'quote select-method)
8065                 (not articles) t)       ; Accept form
8066           (not articles)))              ; Only save nov last time
8067         ;; Copy the article.
8068         ((eq action 'copy)
8069          (save-excursion
8070            (set-buffer copy-buf)
8071            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8072              (gnus-request-accept-article
8073               to-newsgroup select-method (not articles) t))))
8074         ;; Crosspost the article.
8075         ((eq action 'crosspost)
8076          (let ((xref (message-tokenize-header
8077                       (mail-header-xref (gnus-summary-article-header article))
8078                       " ")))
8079            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8080                                   ":" (number-to-string article)))
8081            (unless xref
8082              (setq xref (list (system-name))))
8083            (setq new-xref
8084                  (concat
8085                   (mapconcat 'identity
8086                              (delete "Xref:" (delete new-xref xref))
8087                              " ")
8088                   " " new-xref))
8089            (save-excursion
8090              (set-buffer copy-buf)
8091              ;; First put the article in the destination group.
8092              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8093              (when (consp (setq art-group
8094                                 (gnus-request-accept-article
8095                                  to-newsgroup select-method (not articles))))
8096                (setq new-xref (concat new-xref " " (car art-group)
8097                                       ":"
8098                                       (number-to-string (cdr art-group))))
8099                ;; Now we have the new Xrefs header, so we insert
8100                ;; it and replace the new article.
8101                (nnheader-replace-header "Xref" new-xref)
8102                (gnus-request-replace-article
8103                 (cdr art-group) to-newsgroup (current-buffer))
8104                art-group))))))
8105       (cond
8106        ((not art-group)
8107         (gnus-message 1 "Couldn't %s article %s: %s"
8108                       (cadr (assq action names)) article
8109                       (nnheader-get-report (car to-method))))
8110        ((eq art-group 'junk)
8111         (when (eq action 'move)
8112           (gnus-summary-mark-article article gnus-canceled-mark)
8113           (gnus-message 4 "Deleted article %s" article)))
8114        (t
8115         (let* ((pto-group (gnus-group-prefixed-name
8116                            (car art-group) to-method))
8117                (entry
8118                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8119                (info (nth 2 entry))
8120                (to-group (gnus-info-group info))
8121                to-marks)
8122           ;; Update the group that has been moved to.
8123           (when (and info
8124                      (memq action '(move copy)))
8125             (unless (member to-group to-groups)
8126               (push to-group to-groups))
8127
8128             (unless (memq article gnus-newsgroup-unreads)
8129               (push 'read to-marks)
8130               (gnus-info-set-read
8131                info (gnus-add-to-range (gnus-info-read info)
8132                                        (list (cdr art-group)))))
8133
8134             ;; See whether the article is to be put in the cache.
8135             (let ((marks gnus-article-mark-lists)
8136                   (to-article (cdr art-group)))
8137
8138               ;; Enter the article into the cache in the new group,
8139               ;; if that is required.
8140               (when gnus-use-cache
8141                 (gnus-cache-possibly-enter-article
8142                  to-group to-article
8143                  (memq article gnus-newsgroup-marked)
8144                  (memq article gnus-newsgroup-dormant)
8145                  (memq article gnus-newsgroup-unreads)))
8146
8147               (when gnus-preserve-marks
8148                 ;; Copy any marks over to the new group.
8149                 (when (and (equal to-group gnus-newsgroup-name)
8150                            (not (memq article gnus-newsgroup-unreads)))
8151                   ;; Mark this article as read in this group.
8152                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8153                   (setcdr (gnus-active to-group) to-article)
8154                   (setcdr gnus-newsgroup-active to-article))
8155
8156                 (while marks
8157                   (when (eq (gnus-article-mark-to-type (cdar marks)) 'list)
8158                     (when (memq article (symbol-value
8159                                          (intern (format "gnus-newsgroup-%s"
8160                                                          (caar marks)))))
8161                       (push (cdar marks) to-marks)
8162                       ;; If the other group is the same as this group,
8163                       ;; then we have to add the mark to the list.
8164                       (when (equal to-group gnus-newsgroup-name)
8165                         (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8166                              (cons to-article
8167                                    (symbol-value
8168                                     (intern (format "gnus-newsgroup-%s"
8169                                                     (caar marks)))))))
8170                       ;; Copy the marks to other group.
8171                       (gnus-add-marked-articles
8172                        to-group (cdar marks) (list to-article) info)))
8173                   (setq marks (cdr marks)))
8174
8175                 (gnus-request-set-mark to-group (list (list (list to-article)
8176                                                             'add
8177                                                             to-marks))))
8178
8179               (gnus-dribble-enter
8180                (concat "(gnus-group-set-info '"
8181                        (gnus-prin1-to-string (gnus-get-info to-group))
8182                        ")"))))
8183
8184           ;; Update the Xref header in this article to point to
8185           ;; the new crossposted article we have just created.
8186           (when (eq action 'crosspost)
8187             (save-excursion
8188               (set-buffer copy-buf)
8189               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8190               (nnheader-replace-header "Xref" new-xref)
8191               (gnus-request-replace-article
8192                article gnus-newsgroup-name (current-buffer)))))
8193
8194         ;;;!!!Why is this necessary?
8195         (set-buffer gnus-summary-buffer)
8196
8197         (gnus-summary-goto-subject article)
8198         (when (eq action 'move)
8199           (gnus-summary-mark-article article gnus-canceled-mark))))
8200       (gnus-summary-remove-process-mark article))
8201     ;; Re-activate all groups that have been moved to.
8202     (save-excursion
8203       (set-buffer gnus-group-buffer)
8204       (let ((gnus-group-marked to-groups))
8205         (gnus-group-get-new-news-this-group nil t)))
8206
8207     (gnus-kill-buffer copy-buf)
8208     (gnus-summary-position-point)
8209     (gnus-set-mode-line 'summary)))
8210
8211 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8212   "Move the current article to a different newsgroup.
8213 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8214 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8215 re-spool using this method."
8216   (interactive "P")
8217   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8218
8219 (defun gnus-summary-crosspost-article (&optional n)
8220   "Crosspost the current article to some other group."
8221   (interactive "P")
8222   (gnus-summary-move-article n nil nil 'crosspost))
8223
8224 (defcustom gnus-summary-respool-default-method nil
8225   "Default method type for respooling an article.
8226 If nil, use to the current newsgroup method."
8227   :type 'symbol
8228   :group 'gnus-summary-mail)
8229
8230 (defun gnus-summary-respool-article (&optional n method)
8231   "Respool the current article.
8232 The article will be squeezed through the mail spooling process again,
8233 which means that it will be put in some mail newsgroup or other
8234 depending on `nnmail-split-methods'.
8235 If N is a positive number, respool the N next articles.
8236 If N is a negative number, respool the N previous articles.
8237 If N is nil and any articles have been marked with the process mark,
8238 respool those articles instead.
8239
8240 Respooling can be done both from mail groups and \"real\" newsgroups.
8241 In the former case, the articles in question will be moved from the
8242 current group into whatever groups they are destined to.  In the
8243 latter case, they will be copied into the relevant groups."
8244   (interactive
8245    (list current-prefix-arg
8246          (let* ((methods (gnus-methods-using 'respool))
8247                 (methname
8248                  (symbol-name (or gnus-summary-respool-default-method
8249                                   (car (gnus-find-method-for-group
8250                                         gnus-newsgroup-name)))))
8251                 (method
8252                  (gnus-completing-read
8253                   methname "What backend do you want to use when respooling?"
8254                   methods nil t nil 'gnus-mail-method-history))
8255                 ms)
8256            (cond
8257             ((zerop (length (setq ms (gnus-servers-using-backend
8258                                       (intern method)))))
8259              (list (intern method) ""))
8260             ((= 1 (length ms))
8261              (car ms))
8262             (t
8263              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8264                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8265                            ms-alist))))))))
8266   (unless method
8267     (error "No method given for respooling"))
8268   (if (assoc (symbol-name
8269               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8270              (gnus-methods-using 'respool))
8271       (gnus-summary-move-article n nil method)
8272     (gnus-summary-copy-article n nil method)))
8273
8274 (defun gnus-summary-import-article (file &optional edit)
8275   "Import an arbitrary file into a mail newsgroup."
8276   (interactive "fImport file: \nP")
8277   (let ((group gnus-newsgroup-name)
8278         (now (current-time))
8279         atts lines group-art)
8280     (unless (gnus-check-backend-function 'request-accept-article group)
8281       (error "%s does not support article importing" group))
8282     (or (file-readable-p file)
8283         (not (file-regular-p file))
8284         (error "Can't read %s" file))
8285     (save-excursion
8286       (set-buffer (gnus-get-buffer-create " *import file*"))
8287       (erase-buffer)
8288       (nnheader-insert-file-contents file)
8289       (goto-char (point-min))
8290       (if (nnheader-article-p)
8291           (save-restriction
8292             (goto-char (point-min))
8293             (search-forward "\n\n" nil t)
8294             (narrow-to-region (point-min) (1- (point)))
8295             (goto-char (point-min))
8296             (unless (re-search-forward "^date:" nil t)
8297               (goto-char (point-max))
8298               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8299        ;; This doesn't look like an article, so we fudge some headers.
8300         (setq atts (file-attributes file)
8301               lines (count-lines (point-min) (point-max)))
8302         (insert "From: " (read-string "From: ") "\n"
8303                 "Subject: " (read-string "Subject: ") "\n"
8304                 "Date: " (message-make-date (nth 5 atts)) "\n"
8305                 "Message-ID: " (message-make-message-id) "\n"
8306                 "Lines: " (int-to-string lines) "\n"
8307                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8308       (setq group-art (gnus-request-accept-article group nil t))
8309       (kill-buffer (current-buffer)))
8310     (setq gnus-newsgroup-active (gnus-activate-group group))
8311     (forward-line 1)
8312     (gnus-summary-goto-article (cdr group-art) nil t)
8313     (when edit
8314       (gnus-summary-edit-article))))
8315
8316 (defun gnus-summary-create-article ()
8317   "Create an article in a mail newsgroup."
8318   (interactive)
8319   (let ((group gnus-newsgroup-name)
8320         (now (current-time))
8321         group-art)
8322     (unless (gnus-check-backend-function 'request-accept-article group)
8323       (error "%s does not support article importing" group))
8324     (save-excursion
8325       (set-buffer (gnus-get-buffer-create " *import file*"))
8326       (erase-buffer)
8327       (goto-char (point-min))
8328       ;; This doesn't look like an article, so we fudge some headers.
8329       (insert "From: " (read-string "From: ") "\n"
8330               "Subject: " (read-string "Subject: ") "\n"
8331               "Date: " (message-make-date now) "\n"
8332               "Message-ID: " (message-make-message-id) "\n")
8333       (setq group-art (gnus-request-accept-article group nil t))
8334       (kill-buffer (current-buffer)))
8335     (setq gnus-newsgroup-active (gnus-activate-group group))
8336     (forward-line 1)
8337     (gnus-summary-goto-article (cdr group-art) nil t)
8338     (gnus-summary-edit-article)))
8339
8340 (defun gnus-summary-article-posted-p ()
8341   "Say whether the current (mail) article is available from news as well.
8342 This will be the case if the article has both been mailed and posted."
8343   (interactive)
8344   (let ((id (mail-header-references (gnus-summary-article-header)))
8345         (gnus-override-method (car (gnus-refer-article-methods))))
8346     (if (gnus-request-head id "")
8347         (gnus-message 2 "The current message was found on %s"
8348                       gnus-override-method)
8349       (gnus-message 2 "The current message couldn't be found on %s"
8350                     gnus-override-method)
8351       nil)))
8352
8353 (defun gnus-summary-expire-articles (&optional now)
8354   "Expire all articles that are marked as expirable in the current group."
8355   (interactive)
8356   (when (gnus-check-backend-function
8357          'request-expire-articles gnus-newsgroup-name)
8358     ;; This backend supports expiry.
8359     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8360            (expirable (if total
8361                           (progn
8362                             ;; We need to update the info for
8363                             ;; this group for `gnus-list-of-read-articles'
8364                             ;; to give us the right answer.
8365                             (gnus-run-hooks 'gnus-exit-group-hook)
8366                             (gnus-summary-update-info)
8367                             (gnus-list-of-read-articles gnus-newsgroup-name))
8368                         (setq gnus-newsgroup-expirable
8369                               (sort gnus-newsgroup-expirable '<))))
8370            (expiry-wait (if now 'immediate
8371                           (gnus-group-find-parameter
8372                            gnus-newsgroup-name 'expiry-wait)))
8373            (nnmail-expiry-target
8374             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8375                 nnmail-expiry-target))
8376            es)
8377       (when expirable
8378         ;; There are expirable articles in this group, so we run them
8379         ;; through the expiry process.
8380         (gnus-message 6 "Expiring articles...")
8381         (unless (gnus-check-group gnus-newsgroup-name)
8382           (error "Can't open server for %s" gnus-newsgroup-name))
8383         ;; The list of articles that weren't expired is returned.
8384         (save-excursion
8385           (if expiry-wait
8386               (let ((nnmail-expiry-wait-function nil)
8387                     (nnmail-expiry-wait expiry-wait))
8388                 (setq es (gnus-request-expire-articles
8389                           expirable gnus-newsgroup-name)))
8390             (setq es (gnus-request-expire-articles
8391                       expirable gnus-newsgroup-name)))
8392           (unless total
8393             (setq gnus-newsgroup-expirable es))
8394           ;; We go through the old list of expirable, and mark all
8395           ;; really expired articles as nonexistent.
8396           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8397             (let ((gnus-use-cache nil))
8398               (while expirable
8399                 (unless (memq (car expirable) es)
8400                   (when (gnus-data-find (car expirable))
8401                     (gnus-summary-mark-article
8402                      (car expirable) gnus-canceled-mark)))
8403                 (setq expirable (cdr expirable))))))
8404         (gnus-message 6 "Expiring articles...done")))))
8405
8406 (defun gnus-summary-expire-articles-now ()
8407   "Expunge all expirable articles in the current group.
8408 This means that *all* articles that are marked as expirable will be
8409 deleted forever, right now."
8410   (interactive)
8411   (or gnus-expert-user
8412       (gnus-yes-or-no-p
8413        "Are you really, really, really sure you want to delete all these messages? ")
8414       (error "Phew!"))
8415   (gnus-summary-expire-articles t))
8416
8417 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8418 (defun gnus-summary-delete-article (&optional n)
8419   "Delete the N next (mail) articles.
8420 This command actually deletes articles.  This is not a marking
8421 command.  The article will disappear forever from your life, never to
8422 return.
8423 If N is negative, delete backwards.
8424 If N is nil and articles have been marked with the process mark,
8425 delete these instead."
8426   (interactive "P")
8427   (unless (gnus-check-backend-function 'request-expire-articles
8428                                        gnus-newsgroup-name)
8429     (error "The current newsgroup does not support article deletion"))
8430   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8431     (error "Couldn't open server"))
8432   ;; Compute the list of articles to delete.
8433   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8434         not-deleted)
8435     (if (and gnus-novice-user
8436              (not (gnus-yes-or-no-p
8437                    (format "Do you really want to delete %s forever? "
8438                            (if (> (length articles) 1)
8439                                (format "these %s articles" (length articles))
8440                              "this article")))))
8441         ()
8442       ;; Delete the articles.
8443       (setq not-deleted (gnus-request-expire-articles
8444                          articles gnus-newsgroup-name 'force))
8445       (while articles
8446         (gnus-summary-remove-process-mark (car articles))
8447         ;; The backend might not have been able to delete the article
8448         ;; after all.
8449         (unless (memq (car articles) not-deleted)
8450           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8451         (setq articles (cdr articles)))
8452       (when not-deleted
8453         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8454     (gnus-summary-position-point)
8455     (gnus-set-mode-line 'summary)
8456     not-deleted))
8457
8458 (defun gnus-summary-edit-article (&optional arg)
8459   "Edit the current article.
8460 This will have permanent effect only in mail groups.
8461 If ARG is nil, edit the decoded articles.
8462 If ARG is 1, edit the raw articles.
8463 If ARG is 2, edit the raw articles even in read-only groups.
8464 If ARG is 3, edit the articles with the current handles.
8465 Otherwise, allow editing of articles even in read-only
8466 groups."
8467   (interactive "P")
8468   (let (force raw current-handles)
8469     (cond
8470      ((null arg))
8471      ((eq arg 1) (setq raw t))
8472      ((eq arg 2) (setq raw t
8473                        force t))
8474      ((eq arg 3) (setq current-handles
8475                        (and (gnus-buffer-live-p gnus-article-buffer)
8476                             (with-current-buffer gnus-article-buffer
8477                               (prog1
8478                                   gnus-article-mime-handles
8479                                 (setq gnus-article-mime-handles nil))))))
8480      (t (setq force t)))
8481     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8482         (error "Can't edit the raw article in group nndraft:drafts"))
8483     (save-excursion
8484       (set-buffer gnus-summary-buffer)
8485       (let ((mail-parse-charset gnus-newsgroup-charset)
8486             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8487         (gnus-set-global-variables)
8488         (when (and (not force)
8489                    (gnus-group-read-only-p))
8490           (error "The current newsgroup does not support article editing"))
8491         (gnus-summary-show-article t)
8492         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8493           (with-current-buffer gnus-article-buffer
8494             (mm-enable-multibyte-mule4)))
8495         (if (equal gnus-newsgroup-name "nndraft:drafts")
8496             (setq raw t))
8497         (gnus-article-edit-article
8498          (if raw 'ignore
8499            `(lambda ()
8500               (let ((mbl mml-buffer-list))
8501                 (setq mml-buffer-list nil)
8502                 (mime-to-mml ,'current-handles)
8503                 (let ((mbl1 mml-buffer-list))
8504                   (setq mml-buffer-list mbl)
8505                   (set (make-local-variable 'mml-buffer-list) mbl1))
8506                 (make-local-hook 'kill-buffer-hook)
8507                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8508          `(lambda (no-highlight)
8509             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8510                   (message-options message-options)
8511                   (message-options-set-recipient)
8512                   (mail-parse-ignored-charsets
8513                    ',gnus-newsgroup-ignored-charsets))
8514               ,(if (not raw) '(progn
8515                                 (mml-to-mime)
8516                                 (mml-destroy-buffers)
8517                                 (remove-hook 'kill-buffer-hook
8518                                              'mml-destroy-buffers t)
8519                                 (kill-local-variable 'mml-buffer-list)))
8520               (gnus-summary-edit-article-done
8521                ,(or (mail-header-references gnus-current-headers) "")
8522                ,(gnus-group-read-only-p)
8523                ,gnus-summary-buffer no-highlight))))))))
8524
8525 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8526
8527 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8528                                                  no-highlight)
8529   "Make edits to the current article permanent."
8530   (interactive)
8531   (save-excursion
8532    ;; The buffer restriction contains the entire article if it exists.
8533     (when (article-goto-body)
8534       (let ((lines (count-lines (point) (point-max)))
8535             (length (- (point-max) (point)))
8536             (case-fold-search t)
8537             (body (copy-marker (point))))
8538         (goto-char (point-min))
8539         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8540           (delete-region (match-beginning 1) (match-end 1))
8541           (insert (number-to-string length)))
8542         (goto-char (point-min))
8543         (when (re-search-forward
8544                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8545           (delete-region (match-beginning 1) (match-end 1))
8546           (insert (number-to-string length)))
8547         (goto-char (point-min))
8548         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8549           (delete-region (match-beginning 1) (match-end 1))
8550           (insert (number-to-string lines))))))
8551   ;; Replace the article.
8552   (let ((buf (current-buffer)))
8553     (with-temp-buffer
8554       (insert-buffer-substring buf)
8555
8556       (if (and (not read-only)
8557                (not (gnus-request-replace-article
8558                      (cdr gnus-article-current) (car gnus-article-current)
8559                      (current-buffer) t)))
8560           (error "Couldn't replace article")
8561         ;; Update the summary buffer.
8562         (if (and references
8563                  (equal (message-tokenize-header references " ")
8564                         (message-tokenize-header
8565                          (or (message-fetch-field "references") "") " ")))
8566             ;; We only have to update this line.
8567             (save-excursion
8568               (save-restriction
8569                 (message-narrow-to-head)
8570                 (let ((head (buffer-string))
8571                       header)
8572                   (with-temp-buffer
8573                     (insert (format "211 %d Article retrieved.\n"
8574                                     (cdr gnus-article-current)))
8575                     (insert head)
8576                     (insert ".\n")
8577                     (let ((nntp-server-buffer (current-buffer)))
8578                       (setq header (car (gnus-get-newsgroup-headers
8579                                          (save-excursion
8580                                            (set-buffer gnus-summary-buffer)
8581                                            gnus-newsgroup-dependencies)
8582                                          t))))
8583                     (save-excursion
8584                       (set-buffer gnus-summary-buffer)
8585                       (gnus-data-set-header
8586                        (gnus-data-find (cdr gnus-article-current))
8587                        header)
8588                       (gnus-summary-update-article-line
8589                        (cdr gnus-article-current) header)
8590                       (if (gnus-summary-goto-subject
8591                            (cdr gnus-article-current) nil t)
8592                           (gnus-summary-update-secondary-mark
8593                            (cdr gnus-article-current))))))))
8594           ;; Update threads.
8595           (set-buffer (or buffer gnus-summary-buffer))
8596           (gnus-summary-update-article (cdr gnus-article-current))
8597           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8598               (gnus-summary-update-secondary-mark
8599                (cdr gnus-article-current))))
8600         ;; Prettify the article buffer again.
8601         (unless no-highlight
8602           (save-excursion
8603             (set-buffer gnus-article-buffer)
8604             ;;;!!! Fix this -- article should be rehighlighted.
8605             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8606             (set-buffer gnus-original-article-buffer)
8607             (gnus-request-article
8608              (cdr gnus-article-current)
8609              (car gnus-article-current) (current-buffer))))
8610         ;; Prettify the summary buffer line.
8611         (when (gnus-visual-p 'summary-highlight 'highlight)
8612           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8613
8614 (defun gnus-summary-edit-wash (key)
8615   "Perform editing command KEY in the article buffer."
8616   (interactive
8617    (list
8618     (progn
8619       (message "%s" (concat (this-command-keys) "- "))
8620       (read-char))))
8621   (message "")
8622   (gnus-summary-edit-article)
8623   (execute-kbd-macro (concat (this-command-keys) key))
8624   (gnus-article-edit-done))
8625
8626
8627 (defun gnus-summary-toggle-smiley (&optional arg)
8628   "Toggle the display of smilies as small graphical icons."
8629   (interactive "P")
8630   (save-excursion
8631     (set-buffer gnus-article-buffer)
8632     (gnus-smiley-display arg)
8633     ))
8634
8635 ;;; Respooling
8636
8637 (defun gnus-summary-respool-query (&optional silent trace)
8638   "Query where the respool algorithm would put this article."
8639   (interactive)
8640   (let (gnus-mark-article-hook)
8641     (gnus-summary-select-article)
8642     (save-excursion
8643       (set-buffer gnus-original-article-buffer)
8644       (save-restriction
8645         (message-narrow-to-head)
8646         (let ((groups (nnmail-article-group 'identity trace)))
8647           (unless silent
8648             (if groups
8649                 (message "This message would go to %s"
8650                          (mapconcat 'car groups ", "))
8651               (message "This message would go to no groups"))
8652             groups))))))
8653
8654 (defun gnus-summary-respool-trace ()
8655   "Trace where the respool algorithm would put this article.
8656 Display a buffer showing all fancy splitting patterns which matched."
8657   (interactive)
8658   (gnus-summary-respool-query nil t))
8659
8660 ;; Summary marking commands.
8661
8662 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8663   "Mark articles which has the same subject as read, and then select the next.
8664 If UNMARK is positive, remove any kind of mark.
8665 If UNMARK is negative, tick articles."
8666   (interactive "P")
8667   (when unmark
8668     (setq unmark (prefix-numeric-value unmark)))
8669   (let ((count
8670          (gnus-summary-mark-same-subject
8671           (gnus-summary-article-subject) unmark)))
8672     ;; Select next unread article.  If auto-select-same mode, should
8673     ;; select the first unread article.
8674     (gnus-summary-next-article t (and gnus-auto-select-same
8675                                       (gnus-summary-article-subject)))
8676     (gnus-message 7 "%d article%s marked as %s"
8677                   count (if (= count 1) " is" "s are")
8678                   (if unmark "unread" "read"))))
8679
8680 (defun gnus-summary-kill-same-subject (&optional unmark)
8681   "Mark articles which has the same subject as read.
8682 If UNMARK is positive, remove any kind of mark.
8683 If UNMARK is negative, tick articles."
8684   (interactive "P")
8685   (when unmark
8686     (setq unmark (prefix-numeric-value unmark)))
8687   (let ((count
8688          (gnus-summary-mark-same-subject
8689           (gnus-summary-article-subject) unmark)))
8690     ;; If marked as read, go to next unread subject.
8691     (when (null unmark)
8692       ;; Go to next unread subject.
8693       (gnus-summary-next-subject 1 t))
8694     (gnus-message 7 "%d articles are marked as %s"
8695                   count (if unmark "unread" "read"))))
8696
8697 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8698   "Mark articles with same SUBJECT as read, and return marked number.
8699 If optional argument UNMARK is positive, remove any kinds of marks.
8700 If optional argument UNMARK is negative, mark articles as unread instead."
8701   (let ((count 1))
8702     (save-excursion
8703       (cond
8704        ((null unmark)                   ; Mark as read.
8705         (while (and
8706                 (progn
8707                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8708                   (gnus-summary-show-thread) t)
8709                 (gnus-summary-find-subject subject))
8710           (setq count (1+ count))))
8711        ((> unmark 0)                    ; Tick.
8712         (while (and
8713                 (progn
8714                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8715                   (gnus-summary-show-thread) t)
8716                 (gnus-summary-find-subject subject))
8717           (setq count (1+ count))))
8718        (t                               ; Mark as unread.
8719         (while (and
8720                 (progn
8721                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8722                   (gnus-summary-show-thread) t)
8723                 (gnus-summary-find-subject subject))
8724           (setq count (1+ count)))))
8725       (gnus-set-mode-line 'summary)
8726       ;; Return the number of marked articles.
8727       count)))
8728
8729 (defun gnus-summary-mark-as-processable (n &optional unmark)
8730   "Set the process mark on the next N articles.
8731 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8732 the process mark instead.  The difference between N and the actual
8733 number of articles marked is returned."
8734   (interactive "P")
8735   (if (and (null n) (gnus-region-active-p))
8736       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8737     (setq n (prefix-numeric-value n))
8738     (let ((backward (< n 0))
8739           (n (abs n)))
8740       (while (and
8741               (> n 0)
8742               (if unmark
8743                   (gnus-summary-remove-process-mark
8744                    (gnus-summary-article-number))
8745                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8746               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8747         (setq n (1- n)))
8748       (when (/= 0 n)
8749         (gnus-message 7 "No more articles"))
8750       (gnus-summary-recenter)
8751       (gnus-summary-position-point)
8752       n)))
8753
8754 (defun gnus-summary-unmark-as-processable (n)
8755   "Remove the process mark from the next N articles.
8756 If N is negative, unmark backward instead.  The difference between N and
8757 the actual number of articles unmarked is returned."
8758   (interactive "P")
8759   (gnus-summary-mark-as-processable n t))
8760
8761 (defun gnus-summary-unmark-all-processable ()
8762   "Remove the process mark from all articles."
8763   (interactive)
8764   (save-excursion
8765     (while gnus-newsgroup-processable
8766       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8767   (gnus-summary-position-point))
8768
8769 (defun gnus-summary-add-mark (article type)
8770   "Mark ARTICLE with a mark of TYPE."
8771   (let ((vtype (car (assq type gnus-article-mark-lists)))
8772         var)
8773     (if (not vtype)
8774         (error "No such mark type: %s" type)
8775       (setq var (intern (format "gnus-newsgroup-%s" type)))
8776       (set var (cons article (symbol-value var)))
8777       (if (memq type '(processable cached replied forwarded recent saved))
8778           (gnus-summary-update-secondary-mark article)
8779         ;;; !!! This is bogus.  We should find out what primary
8780         ;;; !!! mark we want to set.
8781         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8782
8783 (defun gnus-summary-mark-as-expirable (n)
8784   "Mark N articles forward as expirable.
8785 If N is negative, mark backward instead.  The difference between N and
8786 the actual number of articles marked is returned."
8787   (interactive "p")
8788   (gnus-summary-mark-forward n gnus-expirable-mark))
8789
8790 (defun gnus-summary-mark-article-as-replied (article)
8791   "Mark ARTICLE as replied to and update the summary line.
8792 ARTICLE can also be a list of articles."
8793   (interactive (list (gnus-summary-article-number)))
8794   (let ((articles (if (listp article) article (list article))))
8795     (dolist (article articles)
8796       (push article gnus-newsgroup-replied)
8797       (let ((buffer-read-only nil))
8798         (when (gnus-summary-goto-subject article nil t)
8799           (gnus-summary-update-secondary-mark article))))))
8800
8801 (defun gnus-summary-mark-article-as-forwarded (article)
8802   "Mark ARTICLE as forwarded and update the summary line.
8803 ARTICLE can also be a list of articles."
8804   (let ((articles (if (listp article) article (list article))))
8805     (dolist (article articles)
8806       (push article gnus-newsgroup-forwarded)
8807       (let ((buffer-read-only nil))
8808         (when (gnus-summary-goto-subject article nil t)
8809           (gnus-summary-update-secondary-mark article))))))
8810
8811 (defun gnus-summary-set-bookmark (article)
8812   "Set a bookmark in current article."
8813   (interactive (list (gnus-summary-article-number)))
8814   (when (or (not (get-buffer gnus-article-buffer))
8815             (not gnus-current-article)
8816             (not gnus-article-current)
8817             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8818     (error "No current article selected"))
8819   ;; Remove old bookmark, if one exists.
8820   (let ((old (assq article gnus-newsgroup-bookmarks)))
8821     (when old
8822       (setq gnus-newsgroup-bookmarks
8823             (delq old gnus-newsgroup-bookmarks))))
8824   ;; Set the new bookmark, which is on the form
8825   ;; (article-number . line-number-in-body).
8826   (push
8827    (cons article
8828          (save-excursion
8829            (set-buffer gnus-article-buffer)
8830            (count-lines
8831             (min (point)
8832                  (save-excursion
8833                    (goto-char (point-min))
8834                    (search-forward "\n\n" nil t)
8835                    (point)))
8836             (point))))
8837    gnus-newsgroup-bookmarks)
8838   (gnus-message 6 "A bookmark has been added to the current article."))
8839
8840 (defun gnus-summary-remove-bookmark (article)
8841   "Remove the bookmark from the current article."
8842   (interactive (list (gnus-summary-article-number)))
8843   ;; Remove old bookmark, if one exists.
8844   (let ((old (assq article gnus-newsgroup-bookmarks)))
8845     (if old
8846         (progn
8847           (setq gnus-newsgroup-bookmarks
8848                 (delq old gnus-newsgroup-bookmarks))
8849           (gnus-message 6 "Removed bookmark."))
8850       (gnus-message 6 "No bookmark in current article."))))
8851
8852 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8853 (defun gnus-summary-mark-as-dormant (n)
8854   "Mark N articles forward as dormant.
8855 If N is negative, mark backward instead.  The difference between N and
8856 the actual number of articles marked is returned."
8857   (interactive "p")
8858   (gnus-summary-mark-forward n gnus-dormant-mark))
8859
8860 (defun gnus-summary-set-process-mark (article)
8861   "Set the process mark on ARTICLE and update the summary line."
8862   (setq gnus-newsgroup-processable
8863         (cons article
8864               (delq article gnus-newsgroup-processable)))
8865   (when (gnus-summary-goto-subject article)
8866     (gnus-summary-show-thread)
8867     (gnus-summary-goto-subject article)
8868     (gnus-summary-update-secondary-mark article)))
8869
8870 (defun gnus-summary-remove-process-mark (article)
8871   "Remove the process mark from ARTICLE and update the summary line."
8872   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8873   (when (gnus-summary-goto-subject article)
8874     (gnus-summary-show-thread)
8875     (gnus-summary-goto-subject article)
8876     (gnus-summary-update-secondary-mark article)))
8877
8878 (defun gnus-summary-set-saved-mark (article)
8879   "Set the process mark on ARTICLE and update the summary line."
8880   (push article gnus-newsgroup-saved)
8881   (when (gnus-summary-goto-subject article)
8882     (gnus-summary-update-secondary-mark article)))
8883
8884 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8885   "Mark N articles as read forwards.
8886 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8887 The difference between N and the actual number of articles marked is
8888 returned.
8889 Iff NO-EXPIRE, auto-expiry will be inhibited."
8890   (interactive "p")
8891   (gnus-summary-show-thread)
8892   (let ((backward (< n 0))
8893         (gnus-summary-goto-unread
8894          (and gnus-summary-goto-unread
8895               (not (eq gnus-summary-goto-unread 'never))
8896               (not (memq mark (list gnus-unread-mark
8897                                     gnus-ticked-mark gnus-dormant-mark)))))
8898         (n (abs n))
8899         (mark (or mark gnus-del-mark)))
8900     (while (and (> n 0)
8901                 (gnus-summary-mark-article nil mark no-expire)
8902                 (zerop (gnus-summary-next-subject
8903                         (if backward -1 1)
8904                         (and gnus-summary-goto-unread
8905                              (not (eq gnus-summary-goto-unread 'never)))
8906                         t)))
8907       (setq n (1- n)))
8908     (when (/= 0 n)
8909       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8910     (gnus-summary-recenter)
8911     (gnus-summary-position-point)
8912     (gnus-set-mode-line 'summary)
8913     n))
8914
8915 (defun gnus-summary-mark-article-as-read (mark)
8916   "Mark the current article quickly as read with MARK."
8917   (let ((article (gnus-summary-article-number)))
8918     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8919     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8920     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8921     (push (cons article mark) gnus-newsgroup-reads)
8922     ;; Possibly remove from cache, if that is used.
8923     (when gnus-use-cache
8924       (gnus-cache-enter-remove-article article))
8925     ;; Allow the backend to change the mark.
8926     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8927     ;; Check for auto-expiry.
8928     (when (and gnus-newsgroup-auto-expire
8929                (memq mark gnus-auto-expirable-marks))
8930       (setq mark gnus-expirable-mark)
8931       ;; Let the backend know about the mark change.
8932       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8933       (push article gnus-newsgroup-expirable))
8934     ;; Set the mark in the buffer.
8935     (gnus-summary-update-mark mark 'unread)
8936     t))
8937
8938 (defun gnus-summary-mark-article-as-unread (mark)
8939   "Mark the current article quickly as unread with MARK."
8940   (let* ((article (gnus-summary-article-number))
8941          (old-mark (gnus-summary-article-mark article)))
8942     ;; Allow the backend to change the mark.
8943     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8944     (if (eq mark old-mark)
8945         t
8946       (if (<= article 0)
8947           (progn
8948             (gnus-error 1 "Can't mark negative article numbers")
8949             nil)
8950         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8951         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8952         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8953         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8954         (cond ((= mark gnus-ticked-mark)
8955                (push article gnus-newsgroup-marked))
8956               ((= mark gnus-dormant-mark)
8957                (push article gnus-newsgroup-dormant))
8958               (t
8959                (push article gnus-newsgroup-unreads)))
8960         (gnus-pull article gnus-newsgroup-reads)
8961
8962         ;; See whether the article is to be put in the cache.
8963         (and gnus-use-cache
8964              (vectorp (gnus-summary-article-header article))
8965              (save-excursion
8966                (gnus-cache-possibly-enter-article
8967                 gnus-newsgroup-name article
8968                 (= mark gnus-ticked-mark)
8969                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8970
8971         ;; Fix the mark.
8972         (gnus-summary-update-mark mark 'unread)
8973         t))))
8974
8975 (defun gnus-summary-mark-article (&optional article mark no-expire)
8976   "Mark ARTICLE with MARK.  MARK can be any character.
8977 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8978 `??' (dormant) and `?E' (expirable).
8979 If MARK is nil, then the default character `?r' is used.
8980 If ARTICLE is nil, then the article on the current line will be
8981 marked.
8982 Iff NO-EXPIRE, auto-expiry will be inhibited."
8983   ;; The mark might be a string.
8984   (when (stringp mark)
8985     (setq mark (aref mark 0)))
8986   ;; If no mark is given, then we check auto-expiring.
8987   (when (null mark)
8988     (setq mark gnus-del-mark))
8989   (when (and (not no-expire)
8990              gnus-newsgroup-auto-expire
8991              (memq mark gnus-auto-expirable-marks))
8992     (setq mark gnus-expirable-mark))
8993   (let ((article (or article (gnus-summary-article-number)))
8994         (old-mark (gnus-summary-article-mark article)))
8995     ;; Allow the backend to change the mark.
8996     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8997     (if (eq mark old-mark)
8998         t
8999       (unless article
9000         (error "No article on current line"))
9001       (if (not (if (or (= mark gnus-unread-mark)
9002                        (= mark gnus-ticked-mark)
9003                        (= mark gnus-dormant-mark))
9004                    (gnus-mark-article-as-unread article mark)
9005                  (gnus-mark-article-as-read article mark)))
9006           t
9007         ;; See whether the article is to be put in the cache.
9008         (and gnus-use-cache
9009              (not (= mark gnus-canceled-mark))
9010              (vectorp (gnus-summary-article-header article))
9011              (save-excursion
9012                (gnus-cache-possibly-enter-article
9013                 gnus-newsgroup-name article
9014                 (= mark gnus-ticked-mark)
9015                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
9016
9017         (when (gnus-summary-goto-subject article nil t)
9018           (let ((buffer-read-only nil))
9019             (gnus-summary-show-thread)
9020             ;; Fix the mark.
9021             (gnus-summary-update-mark mark 'unread)
9022             t))))))
9023
9024 (defun gnus-summary-update-secondary-mark (article)
9025   "Update the secondary (read, process, cache) mark."
9026   (gnus-summary-update-mark
9027    (cond ((memq article gnus-newsgroup-processable)
9028           gnus-process-mark)
9029          ((memq article gnus-newsgroup-cached)
9030           gnus-cached-mark)
9031          ((memq article gnus-newsgroup-replied)
9032           gnus-replied-mark)
9033          ((memq article gnus-newsgroup-forwarded)
9034           gnus-forwarded-mark)
9035          ((memq article gnus-newsgroup-saved)
9036           gnus-saved-mark)
9037          ((memq article gnus-newsgroup-recent)
9038           gnus-recent-mark)
9039          ((memq article gnus-newsgroup-unseen)
9040           gnus-unseen-mark)
9041          (t gnus-no-mark))
9042    'replied)
9043   (when (gnus-visual-p 'summary-highlight 'highlight)
9044     (gnus-run-hooks 'gnus-summary-update-hook))
9045   t)
9046
9047 (defun gnus-summary-update-mark (mark type)
9048   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9049         (buffer-read-only nil))
9050     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9051     (when forward
9052       (when (looking-at "\r")
9053         (incf forward))
9054       (when (<= (+ forward (point)) (point-max))
9055         ;; Go to the right position on the line.
9056         (goto-char (+ forward (point)))
9057         ;; Replace the old mark with the new mark.
9058         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9059         ;; Optionally update the marks by some user rule.
9060         (when (eq type 'unread)
9061           (gnus-data-set-mark
9062            (gnus-data-find (gnus-summary-article-number)) mark)
9063           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9064
9065 (defun gnus-mark-article-as-read (article &optional mark)
9066   "Enter ARTICLE in the pertinent lists and remove it from others."
9067   ;; Make the article expirable.
9068   (let ((mark (or mark gnus-del-mark)))
9069     (if (= mark gnus-expirable-mark)
9070         (push article gnus-newsgroup-expirable)
9071       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9072     ;; Remove from unread and marked lists.
9073     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9074     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9075     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9076     (push (cons article mark) gnus-newsgroup-reads)
9077     ;; Possibly remove from cache, if that is used.
9078     (when gnus-use-cache
9079       (gnus-cache-enter-remove-article article))
9080     t))
9081
9082 (defun gnus-mark-article-as-unread (article &optional mark)
9083   "Enter ARTICLE in the pertinent lists and remove it from others."
9084   (let ((mark (or mark gnus-ticked-mark)))
9085     (if (<= article 0)
9086         (progn
9087           (gnus-error 1 "Can't mark negative article numbers")
9088           nil)
9089       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9090             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9091             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9092             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9093
9094       ;; Unsuppress duplicates?
9095       (when gnus-suppress-duplicates
9096         (gnus-dup-unsuppress-article article))
9097
9098       (cond ((= mark gnus-ticked-mark)
9099              (push article gnus-newsgroup-marked))
9100             ((= mark gnus-dormant-mark)
9101              (push article gnus-newsgroup-dormant))
9102             (t
9103              (push article gnus-newsgroup-unreads)))
9104       (gnus-pull article gnus-newsgroup-reads)
9105       t)))
9106
9107 (defalias 'gnus-summary-mark-as-unread-forward
9108   'gnus-summary-tick-article-forward)
9109 (make-obsolete 'gnus-summary-mark-as-unread-forward
9110                'gnus-summary-tick-article-forward)
9111 (defun gnus-summary-tick-article-forward (n)
9112   "Tick N articles forwards.
9113 If N is negative, tick backwards instead.
9114 The difference between N and the number of articles ticked is returned."
9115   (interactive "p")
9116   (gnus-summary-mark-forward n gnus-ticked-mark))
9117
9118 (defalias 'gnus-summary-mark-as-unread-backward
9119   'gnus-summary-tick-article-backward)
9120 (make-obsolete 'gnus-summary-mark-as-unread-backward
9121                'gnus-summary-tick-article-backward)
9122 (defun gnus-summary-tick-article-backward (n)
9123   "Tick N articles backwards.
9124 The difference between N and the number of articles ticked is returned."
9125   (interactive "p")
9126   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9127
9128 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9129 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9130 (defun gnus-summary-tick-article (&optional article clear-mark)
9131   "Mark current article as unread.
9132 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9133 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9134   (interactive)
9135   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9136                                        gnus-ticked-mark)))
9137
9138 (defun gnus-summary-mark-as-read-forward (n)
9139   "Mark N articles as read forwards.
9140 If N is negative, mark backwards instead.
9141 The difference between N and the actual number of articles marked is
9142 returned."
9143   (interactive "p")
9144   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9145
9146 (defun gnus-summary-mark-as-read-backward (n)
9147   "Mark the N articles as read backwards.
9148 The difference between N and the actual number of articles marked is
9149 returned."
9150   (interactive "p")
9151   (gnus-summary-mark-forward
9152    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9153
9154 (defun gnus-summary-mark-as-read (&optional article mark)
9155   "Mark current article as read.
9156 ARTICLE specifies the article to be marked as read.
9157 MARK specifies a string to be inserted at the beginning of the line."
9158   (gnus-summary-mark-article article mark))
9159
9160 (defun gnus-summary-clear-mark-forward (n)
9161   "Clear marks from N articles forward.
9162 If N is negative, clear backward instead.
9163 The difference between N and the number of marks cleared is returned."
9164   (interactive "p")
9165   (gnus-summary-mark-forward n gnus-unread-mark))
9166
9167 (defun gnus-summary-clear-mark-backward (n)
9168   "Clear marks from N articles backward.
9169 The difference between N and the number of marks cleared is returned."
9170   (interactive "p")
9171   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9172
9173 (defun gnus-summary-mark-unread-as-read ()
9174   "Intended to be used by `gnus-summary-mark-article-hook'."
9175   (when (memq gnus-current-article gnus-newsgroup-unreads)
9176     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9177
9178 (defun gnus-summary-mark-read-and-unread-as-read ()
9179   "Intended to be used by `gnus-summary-mark-article-hook'."
9180   (let ((mark (gnus-summary-article-mark)))
9181     (when (or (gnus-unread-mark-p mark)
9182               (gnus-read-mark-p mark))
9183       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9184
9185 (defun gnus-summary-mark-unread-as-ticked ()
9186   "Intended to be used by `gnus-summary-mark-article-hook'."
9187   (when (memq gnus-current-article gnus-newsgroup-unreads)
9188     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9189
9190 (defun gnus-summary-mark-region-as-read (point mark all)
9191   "Mark all unread articles between point and mark as read.
9192 If given a prefix, mark all articles between point and mark as read,
9193 even ticked and dormant ones."
9194   (interactive "r\nP")
9195   (save-excursion
9196     (let (article)
9197       (goto-char point)
9198       (beginning-of-line)
9199       (while (and
9200               (< (point) mark)
9201               (progn
9202                 (when (or all
9203                           (memq (setq article (gnus-summary-article-number))
9204                                 gnus-newsgroup-unreads))
9205                   (gnus-summary-mark-article article gnus-del-mark))
9206                 t)
9207               (gnus-summary-find-next))))))
9208
9209 (defun gnus-summary-mark-below (score mark)
9210   "Mark articles with score less than SCORE with MARK."
9211   (interactive "P\ncMark: ")
9212   (setq score (if score
9213                   (prefix-numeric-value score)
9214                 (or gnus-summary-default-score 0)))
9215   (save-excursion
9216     (set-buffer gnus-summary-buffer)
9217     (goto-char (point-min))
9218     (while
9219         (progn
9220           (and (< (gnus-summary-article-score) score)
9221                (gnus-summary-mark-article nil mark))
9222           (gnus-summary-find-next)))))
9223
9224 (defun gnus-summary-kill-below (&optional score)
9225   "Mark articles with score below SCORE as read."
9226   (interactive "P")
9227   (gnus-summary-mark-below score gnus-killed-mark))
9228
9229 (defun gnus-summary-clear-above (&optional score)
9230   "Clear all marks from articles with score above SCORE."
9231   (interactive "P")
9232   (gnus-summary-mark-above score gnus-unread-mark))
9233
9234 (defun gnus-summary-tick-above (&optional score)
9235   "Tick all articles with score above SCORE."
9236   (interactive "P")
9237   (gnus-summary-mark-above score gnus-ticked-mark))
9238
9239 (defun gnus-summary-mark-above (score mark)
9240   "Mark articles with score over SCORE with MARK."
9241   (interactive "P\ncMark: ")
9242   (setq score (if score
9243                   (prefix-numeric-value score)
9244                 (or gnus-summary-default-score 0)))
9245   (save-excursion
9246     (set-buffer gnus-summary-buffer)
9247     (goto-char (point-min))
9248     (while (and (progn
9249                   (when (> (gnus-summary-article-score) score)
9250                     (gnus-summary-mark-article nil mark))
9251                   t)
9252                 (gnus-summary-find-next)))))
9253
9254 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9255 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9256 (defun gnus-summary-limit-include-expunged (&optional no-error)
9257   "Display all the hidden articles that were expunged for low scores."
9258   (interactive)
9259   (let ((buffer-read-only nil))
9260     (let ((scored gnus-newsgroup-scored)
9261           headers h)
9262       (while scored
9263         (unless (gnus-summary-article-header (caar scored))
9264           (and (setq h (gnus-number-to-header (caar scored)))
9265                (< (cdar scored) gnus-summary-expunge-below)
9266                (push h headers)))
9267         (setq scored (cdr scored)))
9268       (if (not headers)
9269           (when (not no-error)
9270             (error "No expunged articles hidden"))
9271         (goto-char (point-min))
9272         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9273         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9274         (mapcar (lambda (x) (push (mail-header-number x)
9275                                   gnus-newsgroup-limit))
9276                 headers)
9277         (gnus-summary-prepare-unthreaded (nreverse headers))
9278         (goto-char (point-min))
9279         (gnus-summary-position-point)
9280         t))))
9281
9282 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9283   "Mark all unread articles in this newsgroup as read.
9284 If prefix argument ALL is non-nil, ticked and dormant articles will
9285 also be marked as read.
9286 If QUIETLY is non-nil, no questions will be asked.
9287 If TO-HERE is non-nil, it should be a point in the buffer.  All
9288 articles before (after, if REVERSE is set) this point will be marked as read.
9289 Note that this function will only catch up the unread article
9290 in the current summary buffer limitation.
9291 The number of articles marked as read is returned."
9292   (interactive "P")
9293   (prog1
9294       (save-excursion
9295         (when (or quietly
9296                   (not gnus-interactive-catchup) ;Without confirmation?
9297                   gnus-expert-user
9298                   (gnus-y-or-n-p
9299                    (if all
9300                        "Mark absolutely all articles as read? "
9301                      "Mark all unread articles as read? ")))
9302           (if (and not-mark
9303                    (not gnus-newsgroup-adaptive)
9304                    (not gnus-newsgroup-auto-expire)
9305                    (not gnus-suppress-duplicates)
9306                    (or (not gnus-use-cache)
9307                        (eq gnus-use-cache 'passive)))
9308               (progn
9309                 (when all
9310                   (setq gnus-newsgroup-marked nil
9311                         gnus-newsgroup-dormant nil))
9312                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9313             ;; We actually mark all articles as canceled, which we
9314             ;; have to do when using auto-expiry or adaptive scoring.
9315             (gnus-summary-show-all-threads)
9316             (if (and to-here reverse)
9317                 (progn
9318                   (goto-char to-here)
9319                   (while (and
9320                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9321                           (gnus-summary-find-next (not all) nil nil t))))
9322               (when (gnus-summary-first-subject (not all) t)
9323                 (while (and
9324                         (if to-here (< (point) to-here) t)
9325                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9326                         (gnus-summary-find-next (not all) nil nil t)))))
9327             (gnus-set-mode-line 'summary))
9328           t))
9329     (gnus-summary-position-point)))
9330
9331 (defun gnus-summary-catchup-to-here (&optional all)
9332   "Mark all unticked articles before the current one as read.
9333 If ALL is non-nil, also mark ticked and dormant articles as read."
9334   (interactive "P")
9335   (save-excursion
9336     (gnus-save-hidden-threads
9337       (let ((beg (point)))
9338         ;; We check that there are unread articles.
9339         (when (or all (gnus-summary-find-prev))
9340           (gnus-summary-catchup all t beg)))))
9341   (gnus-summary-position-point))
9342
9343 (defun gnus-summary-catchup-from-here (&optional all)
9344   "Mark all unticked articles after the current one as read.
9345 If ALL is non-nil, also mark ticked and dormant articles as read."
9346   (interactive "P")
9347   (save-excursion
9348     (gnus-save-hidden-threads
9349       (let ((beg (point)))
9350         ;; We check that there are unread articles.
9351         (when (or all (gnus-summary-find-next))
9352           (gnus-summary-catchup all t beg nil t)))))
9353
9354   (gnus-summary-position-point))
9355 (defun gnus-summary-catchup-all (&optional quietly)
9356   "Mark all articles in this newsgroup as read."
9357   (interactive "P")
9358   (gnus-summary-catchup t quietly))
9359
9360 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9361   "Mark all unread articles in this group as read, then exit.
9362 If prefix argument ALL is non-nil, all articles are marked as read.
9363 If QUIETLY is non-nil, no questions will be asked."
9364   (interactive "P")
9365   (when (gnus-summary-catchup all quietly nil 'fast)
9366     ;; Select next newsgroup or exit.
9367     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9368              (eq gnus-auto-select-next 'quietly))
9369         (gnus-summary-next-group nil)
9370       (gnus-summary-exit))))
9371
9372 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9373   "Mark all articles in this newsgroup as read, and then exit."
9374   (interactive "P")
9375   (gnus-summary-catchup-and-exit t quietly))
9376
9377 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9378   "Mark all articles in this group as read and select the next group.
9379 If given a prefix, mark all articles, unread as well as ticked, as
9380 read."
9381   (interactive "P")
9382   (save-excursion
9383     (gnus-summary-catchup all))
9384   (gnus-summary-next-group))
9385
9386 ;;;
9387 ;;; with article
9388 ;;;
9389
9390 (defmacro gnus-with-article (article &rest forms)
9391   "Select ARTICLE and perform FORMS in the original article buffer.
9392 Then replace the article with the result."
9393   `(progn
9394      ;; We don't want the article to be marked as read.
9395      (let (gnus-mark-article-hook)
9396        (gnus-summary-select-article t t nil ,article))
9397      (set-buffer gnus-original-article-buffer)
9398      ,@forms
9399      (if (not (gnus-check-backend-function
9400                'request-replace-article (car gnus-article-current)))
9401          (gnus-message 5 "Read-only group; not replacing")
9402        (unless (gnus-request-replace-article
9403                 ,article (car gnus-article-current)
9404                 (current-buffer) t)
9405          (error "Couldn't replace article")))
9406      ;; The cache and backlog have to be flushed somewhat.
9407      (when gnus-keep-backlog
9408        (gnus-backlog-remove-article
9409         (car gnus-article-current) (cdr gnus-article-current)))
9410      (when gnus-use-cache
9411        (gnus-cache-update-article
9412         (car gnus-article-current) (cdr gnus-article-current)))))
9413
9414 (put 'gnus-with-article 'lisp-indent-function 1)
9415 (put 'gnus-with-article 'edebug-form-spec '(form body))
9416
9417 ;; Thread-based commands.
9418
9419 (defun gnus-summary-articles-in-thread (&optional article)
9420   "Return a list of all articles in the current thread.
9421 If ARTICLE is non-nil, return all articles in the thread that starts
9422 with that article."
9423   (let* ((article (or article (gnus-summary-article-number)))
9424          (data (gnus-data-find-list article))
9425          (top-level (gnus-data-level (car data)))
9426          (top-subject
9427           (cond ((null gnus-thread-operation-ignore-subject)
9428                  (gnus-simplify-subject-re
9429                   (mail-header-subject (gnus-data-header (car data)))))
9430                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9431                  (gnus-simplify-subject-fuzzy
9432                   (mail-header-subject (gnus-data-header (car data)))))
9433                 (t nil)))
9434          (end-point (save-excursion
9435                       (if (gnus-summary-go-to-next-thread)
9436                           (point) (point-max))))
9437          articles)
9438     (while (and data
9439                 (< (gnus-data-pos (car data)) end-point))
9440       (when (or (not top-subject)
9441                 (string= top-subject
9442                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9443                              (gnus-simplify-subject-fuzzy
9444                               (mail-header-subject
9445                                (gnus-data-header (car data))))
9446                            (gnus-simplify-subject-re
9447                             (mail-header-subject
9448                              (gnus-data-header (car data)))))))
9449         (push (gnus-data-number (car data)) articles))
9450       (unless (and (setq data (cdr data))
9451                    (> (gnus-data-level (car data)) top-level))
9452         (setq data nil)))
9453     ;; Return the list of articles.
9454     (nreverse articles)))
9455
9456 (defun gnus-summary-rethread-current ()
9457   "Rethread the thread the current article is part of."
9458   (interactive)
9459   (let* ((gnus-show-threads t)
9460          (article (gnus-summary-article-number))
9461          (id (mail-header-id (gnus-summary-article-header)))
9462          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9463     (unless id
9464       (error "No article on the current line"))
9465     (gnus-rebuild-thread id)
9466     (gnus-summary-goto-subject article)))
9467
9468 (defun gnus-summary-reparent-thread ()
9469   "Make the current article child of the marked (or previous) article.
9470
9471 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9472 is non-nil or the Subject: of both articles are the same."
9473   (interactive)
9474   (unless (not (gnus-group-read-only-p))
9475     (error "The current newsgroup does not support article editing"))
9476   (unless (<= (length gnus-newsgroup-processable) 1)
9477     (error "No more than one article may be marked"))
9478   (save-window-excursion
9479     (let ((gnus-article-buffer " *reparent*")
9480           (current-article (gnus-summary-article-number))
9481           ;; First grab the marked article, otherwise one line up.
9482           (parent-article (if (not (null gnus-newsgroup-processable))
9483                               (car gnus-newsgroup-processable)
9484                             (save-excursion
9485                               (if (eq (forward-line -1) 0)
9486                                   (gnus-summary-article-number)
9487                                 (error "Beginning of summary buffer"))))))
9488       (unless (not (eq current-article parent-article))
9489         (error "An article may not be self-referential"))
9490       (let ((message-id (mail-header-id
9491                          (gnus-summary-article-header parent-article))))
9492         (unless (and message-id (not (equal message-id "")))
9493           (error "No message-id in desired parent"))
9494         (gnus-with-article current-article
9495           (save-restriction
9496             (goto-char (point-min))
9497             (message-narrow-to-head)
9498             (if (re-search-forward "^References: " nil t)
9499                 (progn
9500                   (re-search-forward "^[^ \t]" nil t)
9501                   (forward-line -1)
9502                   (end-of-line)
9503                   (insert " " message-id))
9504               (insert "References: " message-id "\n"))))
9505         (set-buffer gnus-summary-buffer)
9506         (gnus-summary-unmark-all-processable)
9507         (gnus-summary-update-article current-article)
9508         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9509             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9510         (gnus-summary-rethread-current)
9511         (gnus-message 3 "Article %d is now the child of article %d"
9512                       current-article parent-article)))))
9513
9514 (defun gnus-summary-toggle-threads (&optional arg)
9515   "Toggle showing conversation threads.
9516 If ARG is positive number, turn showing conversation threads on."
9517   (interactive "P")
9518   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9519     (setq gnus-show-threads
9520           (if (null arg) (not gnus-show-threads)
9521             (> (prefix-numeric-value arg) 0)))
9522     (gnus-summary-prepare)
9523     (gnus-summary-goto-subject current)
9524     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9525     (gnus-summary-position-point)))
9526
9527 (defun gnus-summary-show-all-threads ()
9528   "Show all threads."
9529   (interactive)
9530   (save-excursion
9531     (let ((buffer-read-only nil))
9532       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9533   (gnus-summary-position-point))
9534
9535 (defun gnus-summary-show-thread ()
9536   "Show thread subtrees.
9537 Returns nil if no thread was there to be shown."
9538   (interactive)
9539   (let ((buffer-read-only nil)
9540         (orig (point))
9541         ;; first goto end then to beg, to have point at beg after let
9542         (end (progn (end-of-line) (point)))
9543         (beg (progn (beginning-of-line) (point))))
9544     (prog1
9545         ;; Any hidden lines here?
9546         (search-forward "\r" end t)
9547       (subst-char-in-region beg end ?\^M ?\n t)
9548       (goto-char orig)
9549       (gnus-summary-position-point))))
9550
9551 (defun gnus-summary-hide-all-threads ()
9552   "Hide all thread subtrees."
9553   (interactive)
9554   (save-excursion
9555     (goto-char (point-min))
9556     (gnus-summary-hide-thread)
9557     (while (zerop (gnus-summary-next-thread 1 t))
9558       (gnus-summary-hide-thread)))
9559   (gnus-summary-position-point))
9560
9561 (defun gnus-summary-hide-thread ()
9562   "Hide thread subtrees.
9563 Returns nil if no threads were there to be hidden."
9564   (interactive)
9565   (let ((buffer-read-only nil)
9566         (start (point))
9567         (article (gnus-summary-article-number)))
9568     (goto-char start)
9569     ;; Go forward until either the buffer ends or the subthread
9570     ;; ends.
9571     (when (and (not (eobp))
9572                (or (zerop (gnus-summary-next-thread 1 t))
9573                    (goto-char (point-max))))
9574       (prog1
9575           (if (and (> (point) start)
9576                    (search-backward "\n" start t))
9577               (progn
9578                 (subst-char-in-region start (point) ?\n ?\^M)
9579                 (gnus-summary-goto-subject article))
9580             (goto-char start)
9581             nil)))))
9582
9583 (defun gnus-summary-go-to-next-thread (&optional previous)
9584   "Go to the same level (or less) next thread.
9585 If PREVIOUS is non-nil, go to previous thread instead.
9586 Return the article number moved to, or nil if moving was impossible."
9587   (let ((level (gnus-summary-thread-level))
9588         (way (if previous -1 1))
9589         (beg (point)))
9590     (forward-line way)
9591     (while (and (not (eobp))
9592                 (< level (gnus-summary-thread-level)))
9593       (forward-line way))
9594     (if (eobp)
9595         (progn
9596           (goto-char beg)
9597           nil)
9598       (setq beg (point))
9599       (prog1
9600           (gnus-summary-article-number)
9601         (goto-char beg)))))
9602
9603 (defun gnus-summary-next-thread (n &optional silent)
9604   "Go to the same level next N'th thread.
9605 If N is negative, search backward instead.
9606 Returns the difference between N and the number of skips actually
9607 done.
9608
9609 If SILENT, don't output messages."
9610   (interactive "p")
9611   (let ((backward (< n 0))
9612         (n (abs n)))
9613     (while (and (> n 0)
9614                 (gnus-summary-go-to-next-thread backward))
9615       (decf n))
9616     (unless silent
9617       (gnus-summary-position-point))
9618     (when (and (not silent) (/= 0 n))
9619       (gnus-message 7 "No more threads"))
9620     n))
9621
9622 (defun gnus-summary-prev-thread (n)
9623   "Go to the same level previous N'th thread.
9624 Returns the difference between N and the number of skips actually
9625 done."
9626   (interactive "p")
9627   (gnus-summary-next-thread (- n)))
9628
9629 (defun gnus-summary-go-down-thread ()
9630   "Go down one level in the current thread."
9631   (let ((children (gnus-summary-article-children)))
9632     (when children
9633       (gnus-summary-goto-subject (car children)))))
9634
9635 (defun gnus-summary-go-up-thread ()
9636   "Go up one level in the current thread."
9637   (let ((parent (gnus-summary-article-parent)))
9638     (when parent
9639       (gnus-summary-goto-subject parent))))
9640
9641 (defun gnus-summary-down-thread (n)
9642   "Go down thread N steps.
9643 If N is negative, go up instead.
9644 Returns the difference between N and how many steps down that were
9645 taken."
9646   (interactive "p")
9647   (let ((up (< n 0))
9648         (n (abs n)))
9649     (while (and (> n 0)
9650                 (if up (gnus-summary-go-up-thread)
9651                   (gnus-summary-go-down-thread)))
9652       (setq n (1- n)))
9653     (gnus-summary-position-point)
9654     (when (/= 0 n)
9655       (gnus-message 7 "Can't go further"))
9656     n))
9657
9658 (defun gnus-summary-up-thread (n)
9659   "Go up thread N steps.
9660 If N is negative, go down instead.
9661 Returns the difference between N and how many steps down that were
9662 taken."
9663   (interactive "p")
9664   (gnus-summary-down-thread (- n)))
9665
9666 (defun gnus-summary-top-thread ()
9667   "Go to the top of the thread."
9668   (interactive)
9669   (while (gnus-summary-go-up-thread))
9670   (gnus-summary-article-number))
9671
9672 (defun gnus-summary-kill-thread (&optional unmark)
9673   "Mark articles under current thread as read.
9674 If the prefix argument is positive, remove any kinds of marks.
9675 If the prefix argument is negative, tick articles instead."
9676   (interactive "P")
9677   (when unmark
9678     (setq unmark (prefix-numeric-value unmark)))
9679   (let ((articles (gnus-summary-articles-in-thread)))
9680     (save-excursion
9681       ;; Expand the thread.
9682       (gnus-summary-show-thread)
9683       ;; Mark all the articles.
9684       (while articles
9685         (gnus-summary-goto-subject (car articles))
9686         (cond ((null unmark)
9687                (gnus-summary-mark-article-as-read gnus-killed-mark))
9688               ((> unmark 0)
9689                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9690               (t
9691                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9692         (setq articles (cdr articles))))
9693     ;; Hide killed subtrees.
9694     (and (null unmark)
9695          gnus-thread-hide-killed
9696          (gnus-summary-hide-thread))
9697     ;; If marked as read, go to next unread subject.
9698     (when (null unmark)
9699       ;; Go to next unread subject.
9700       (gnus-summary-next-subject 1 t)))
9701   (gnus-set-mode-line 'summary))
9702
9703 ;; Summary sorting commands
9704
9705 (defun gnus-summary-sort-by-number (&optional reverse)
9706   "Sort the summary buffer by article number.
9707 Argument REVERSE means reverse order."
9708   (interactive "P")
9709   (gnus-summary-sort 'number reverse))
9710
9711 (defun gnus-summary-sort-by-author (&optional reverse)
9712   "Sort the summary buffer by author name alphabetically.
9713 If `case-fold-search' is non-nil, case of letters is ignored.
9714 Argument REVERSE means reverse order."
9715   (interactive "P")
9716   (gnus-summary-sort 'author reverse))
9717
9718 (defun gnus-summary-sort-by-subject (&optional reverse)
9719   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9720 If `case-fold-search' is non-nil, case of letters is ignored.
9721 Argument REVERSE means reverse order."
9722   (interactive "P")
9723   (gnus-summary-sort 'subject reverse))
9724
9725 (defun gnus-summary-sort-by-date (&optional reverse)
9726   "Sort the summary buffer by date.
9727 Argument REVERSE means reverse order."
9728   (interactive "P")
9729   (gnus-summary-sort 'date reverse))
9730
9731 (defun gnus-summary-sort-by-score (&optional reverse)
9732   "Sort the summary buffer by score.
9733 Argument REVERSE means reverse order."
9734   (interactive "P")
9735   (gnus-summary-sort 'score reverse))
9736
9737 (defun gnus-summary-sort-by-lines (&optional reverse)
9738   "Sort the summary buffer by the number of lines.
9739 Argument REVERSE means reverse order."
9740   (interactive "P")
9741   (gnus-summary-sort 'lines reverse))
9742
9743 (defun gnus-summary-sort-by-chars (&optional reverse)
9744   "Sort the summary buffer by article length.
9745 Argument REVERSE means reverse order."
9746   (interactive "P")
9747   (gnus-summary-sort 'chars reverse))
9748
9749 (defun gnus-summary-sort-by-original (&optional reverse)
9750   "Sort the summary buffer using the default sorting method.
9751 Argument REVERSE means reverse order."
9752   (interactive "P")
9753   (let* ((buffer-read-only)
9754          (gnus-summary-prepare-hook nil))
9755     ;; We do the sorting by regenerating the threads.
9756     (gnus-summary-prepare)
9757     ;; Hide subthreads if needed.
9758     (when (and gnus-show-threads gnus-thread-hide-subtree)
9759       (gnus-summary-hide-all-threads))))
9760
9761 (defun gnus-summary-sort (predicate reverse)
9762   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9763   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9764          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9765          (gnus-thread-sort-functions
9766           (if (not reverse)
9767               thread
9768             `(lambda (t1 t2)
9769                (,thread t2 t1))))
9770          (gnus-sort-gathered-threads-function
9771           gnus-thread-sort-functions)
9772          (gnus-article-sort-functions
9773           (if (not reverse)
9774               article
9775             `(lambda (t1 t2)
9776                (,article t2 t1))))
9777          (buffer-read-only)
9778          (gnus-summary-prepare-hook nil))
9779     ;; We do the sorting by regenerating the threads.
9780     (gnus-summary-prepare)
9781     ;; Hide subthreads if needed.
9782     (when (and gnus-show-threads gnus-thread-hide-subtree)
9783       (gnus-summary-hide-all-threads))))
9784
9785 ;; Summary saving commands.
9786
9787 (defun gnus-summary-save-article (&optional n not-saved)
9788   "Save the current article using the default saver function.
9789 If N is a positive number, save the N next articles.
9790 If N is a negative number, save the N previous articles.
9791 If N is nil and any articles have been marked with the process mark,
9792 save those articles instead.
9793 The variable `gnus-default-article-saver' specifies the saver function."
9794   (interactive "P")
9795   (let* ((articles (gnus-summary-work-articles n))
9796          (save-buffer (save-excursion
9797                         (nnheader-set-temp-buffer " *Gnus Save*")))
9798          (num (length articles))
9799          header file)
9800     (dolist (article articles)
9801       (setq header (gnus-summary-article-header article))
9802       (if (not (vectorp header))
9803           ;; This is a pseudo-article.
9804           (if (assq 'name header)
9805               (gnus-copy-file (cdr (assq 'name header)))
9806             (gnus-message 1 "Article %d is unsaveable" article))
9807         ;; This is a real article.
9808         (save-window-excursion
9809           (gnus-summary-select-article t nil nil article))
9810         (save-excursion
9811           (set-buffer save-buffer)
9812           (erase-buffer)
9813           (insert-buffer-substring gnus-original-article-buffer))
9814         (setq file (gnus-article-save save-buffer file num))
9815         (gnus-summary-remove-process-mark article)
9816         (unless not-saved
9817           (gnus-summary-set-saved-mark article))))
9818     (gnus-kill-buffer save-buffer)
9819     (gnus-summary-position-point)
9820     (gnus-set-mode-line 'summary)
9821     n))
9822
9823 (defun gnus-summary-pipe-output (&optional arg)
9824   "Pipe the current article to a subprocess.
9825 If N is a positive number, pipe the N next articles.
9826 If N is a negative number, pipe the N previous articles.
9827 If N is nil and any articles have been marked with the process mark,
9828 pipe those articles instead."
9829   (interactive "P")
9830   (require 'gnus-art)
9831   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9832     (gnus-summary-save-article arg t))
9833   (let ((buffer (get-buffer "*Shell Command Output*")))
9834     (if (and buffer
9835              (with-current-buffer buffer (> (point-max) (point-min))))
9836         (gnus-configure-windows 'pipe))))
9837
9838 (defun gnus-summary-save-article-mail (&optional arg)
9839   "Append the current article to an mail file.
9840 If N is a positive number, save the N next articles.
9841 If N is a negative number, save the N previous articles.
9842 If N is nil and any articles have been marked with the process mark,
9843 save those articles instead."
9844   (interactive "P")
9845   (require 'gnus-art)
9846   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9847     (gnus-summary-save-article arg)))
9848
9849 (defun gnus-summary-save-article-rmail (&optional arg)
9850   "Append the current article to an rmail file.
9851 If N is a positive number, save the N next articles.
9852 If N is a negative number, save the N previous articles.
9853 If N is nil and any articles have been marked with the process mark,
9854 save those articles instead."
9855   (interactive "P")
9856   (require 'gnus-art)
9857   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9858     (gnus-summary-save-article arg)))
9859
9860 (defun gnus-summary-save-article-file (&optional arg)
9861   "Append the current article to a file.
9862 If N is a positive number, save the N next articles.
9863 If N is a negative number, save the N previous articles.
9864 If N is nil and any articles have been marked with the process mark,
9865 save those articles instead."
9866   (interactive "P")
9867   (require 'gnus-art)
9868   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9869     (gnus-summary-save-article arg)))
9870
9871 (defun gnus-summary-write-article-file (&optional arg)
9872   "Write the current article to a file, deleting the previous file.
9873 If N is a positive number, save the N next articles.
9874 If N is a negative number, save the N previous articles.
9875 If N is nil and any articles have been marked with the process mark,
9876 save those articles instead."
9877   (interactive "P")
9878   (require 'gnus-art)
9879   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9880     (gnus-summary-save-article arg)))
9881
9882 (defun gnus-summary-save-article-body-file (&optional arg)
9883   "Append the current article body to a file.
9884 If N is a positive number, save the N next articles.
9885 If N is a negative number, save the N previous articles.
9886 If N is nil and any articles have been marked with the process mark,
9887 save those articles instead."
9888   (interactive "P")
9889   (require 'gnus-art)
9890   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9891     (gnus-summary-save-article arg)))
9892
9893 (defun gnus-summary-pipe-message (program)
9894   "Pipe the current article through PROGRAM."
9895   (interactive "sProgram: ")
9896   (gnus-summary-select-article)
9897   (let ((mail-header-separator ""))
9898     (gnus-eval-in-buffer-window gnus-article-buffer
9899       (save-restriction
9900         (widen)
9901         (let ((start (window-start))
9902               buffer-read-only)
9903           (message-pipe-buffer-body program)
9904           (set-window-start (get-buffer-window (current-buffer)) start))))))
9905
9906 (defun gnus-get-split-value (methods)
9907   "Return a value based on the split METHODS."
9908   (let (split-name method result match)
9909     (when methods
9910       (save-excursion
9911         (set-buffer gnus-original-article-buffer)
9912         (save-restriction
9913           (nnheader-narrow-to-headers)
9914           (while (and methods (not split-name))
9915             (goto-char (point-min))
9916             (setq method (pop methods))
9917             (setq match (car method))
9918             (when (cond
9919                    ((stringp match)
9920                     ;; Regular expression.
9921                     (ignore-errors
9922                       (re-search-forward match nil t)))
9923                    ((gnus-functionp match)
9924                     ;; Function.
9925                     (save-restriction
9926                       (widen)
9927                       (setq result (funcall match gnus-newsgroup-name))))
9928                    ((consp match)
9929                     ;; Form.
9930                     (save-restriction
9931                       (widen)
9932                       (setq result (eval match)))))
9933               (setq split-name (cdr method))
9934               (cond ((stringp result)
9935                      (push (expand-file-name
9936                             result gnus-article-save-directory)
9937                            split-name))
9938                     ((consp result)
9939                      (setq split-name (append result split-name)))))))))
9940     (nreverse split-name)))
9941
9942 (defun gnus-valid-move-group-p (group)
9943   (and (boundp group)
9944        (symbol-name group)
9945        (symbol-value group)
9946        (gnus-get-function (gnus-find-method-for-group
9947                            (symbol-name group)) 'request-accept-article t)))
9948
9949 (defun gnus-read-move-group-name (prompt default articles prefix)
9950   "Read a group name."
9951   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9952          (minibuffer-confirm-incomplete nil) ; XEmacs
9953          (prom
9954           (format "%s %s to:"
9955                   prompt
9956                   (if (> (length articles) 1)
9957                       (format "these %d articles" (length articles))
9958                     "this article")))
9959          (to-newsgroup
9960           (cond
9961            ((null split-name)
9962             (gnus-completing-read default prom
9963                                   gnus-active-hashtb
9964                                   'gnus-valid-move-group-p
9965                                   nil prefix
9966                                   'gnus-group-history))
9967            ((= 1 (length split-name))
9968             (gnus-completing-read (car split-name) prom
9969                                   gnus-active-hashtb
9970                                   'gnus-valid-move-group-p
9971                                   nil nil
9972                                   'gnus-group-history))
9973            (t
9974             (gnus-completing-read nil prom
9975                                   (mapcar (lambda (el) (list el))
9976                                           (nreverse split-name))
9977                                   nil nil nil
9978                                   'gnus-group-history))))
9979          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9980     (when to-newsgroup
9981       (if (or (string= to-newsgroup "")
9982               (string= to-newsgroup prefix))
9983           (setq to-newsgroup default))
9984       (unless to-newsgroup
9985         (error "No group name entered"))
9986       (or (gnus-active to-newsgroup)
9987           (gnus-activate-group to-newsgroup nil nil to-method)
9988           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9989                                      to-newsgroup))
9990               (or (and (gnus-request-create-group to-newsgroup to-method)
9991                        (gnus-activate-group
9992                         to-newsgroup nil nil to-method)
9993                        (gnus-subscribe-group to-newsgroup))
9994                   (error "Couldn't create group %s" to-newsgroup)))
9995           (error "No such group: %s" to-newsgroup)))
9996     to-newsgroup))
9997
9998 (defun gnus-summary-save-parts (type dir n &optional reverse)
9999   "Save parts matching TYPE to DIR.
10000 If REVERSE, save parts that do not match TYPE."
10001   (interactive
10002    (list (read-string "Save parts of type: "
10003                       (or (car gnus-summary-save-parts-type-history)
10004                           gnus-summary-save-parts-default-mime)
10005                       'gnus-summary-save-parts-type-history)
10006          (setq gnus-summary-save-parts-last-directory
10007                (read-file-name "Save to directory: "
10008                                gnus-summary-save-parts-last-directory
10009                                nil t))
10010          current-prefix-arg))
10011   (gnus-summary-iterate n
10012     (let ((gnus-display-mime-function nil)
10013           (gnus-inhibit-treatment t))
10014       (gnus-summary-select-article))
10015     (save-excursion
10016       (set-buffer gnus-article-buffer)
10017       (let ((handles (or gnus-article-mime-handles
10018                          (mm-dissect-buffer) (mm-uu-dissect))))
10019         (when handles
10020           (gnus-summary-save-parts-1 type dir handles reverse)
10021           (unless gnus-article-mime-handles ;; Don't destroy this case.
10022             (mm-destroy-parts handles)))))))
10023
10024 (defun gnus-summary-save-parts-1 (type dir handle reverse)
10025   (if (stringp (car handle))
10026       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
10027               (cdr handle))
10028     (when (if reverse
10029               (not (string-match type (mm-handle-media-type handle)))
10030             (string-match type (mm-handle-media-type handle)))
10031       (let ((file (expand-file-name
10032                    (file-name-nondirectory
10033                     (or
10034                      (mail-content-type-get
10035                       (mm-handle-disposition handle) 'filename)
10036                      (concat gnus-newsgroup-name
10037                              "." (number-to-string
10038                                   (cdr gnus-article-current)))))
10039                    dir)))
10040         (unless (file-exists-p file)
10041           (mm-save-part-to-file handle file))))))
10042
10043 ;; Summary extract commands
10044
10045 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10046   (let ((buffer-read-only nil)
10047         (article (gnus-summary-article-number))
10048         after-article b e)
10049     (unless (gnus-summary-goto-subject article)
10050       (error "No such article: %d" article))
10051     (gnus-summary-position-point)
10052     ;; If all commands are to be bunched up on one line, we collect
10053     ;; them here.
10054     (unless gnus-view-pseudos-separately
10055       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10056             files action)
10057         (while ps
10058           (setq action (cdr (assq 'action (car ps))))
10059           (setq files (list (cdr (assq 'name (car ps)))))
10060           (while (and ps (cdr ps)
10061                       (string= (or action "1")
10062                                (or (cdr (assq 'action (cadr ps))) "2")))
10063             (push (cdr (assq 'name (cadr ps))) files)
10064             (setcdr ps (cddr ps)))
10065           (when files
10066             (when (not (string-match "%s" action))
10067               (push " " files))
10068             (push " " files)
10069             (when (assq 'execute (car ps))
10070               (setcdr (assq 'execute (car ps))
10071                       (funcall (if (string-match "%s" action)
10072                                    'format 'concat)
10073                                action
10074                                (mapconcat
10075                                 (lambda (f)
10076                                   (if (equal f " ")
10077                                       f
10078                                     (mm-quote-arg f)))
10079                                 files " ")))))
10080           (setq ps (cdr ps)))))
10081     (if (and gnus-view-pseudos (not not-view))
10082         (while pslist
10083           (when (assq 'execute (car pslist))
10084             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10085                                   (eq gnus-view-pseudos 'not-confirm)))
10086           (setq pslist (cdr pslist)))
10087       (save-excursion
10088         (while pslist
10089           (setq after-article (or (cdr (assq 'article (car pslist)))
10090                                   (gnus-summary-article-number)))
10091           (gnus-summary-goto-subject after-article)
10092           (forward-line 1)
10093           (setq b (point))
10094           (insert "    " (file-name-nondirectory
10095                           (cdr (assq 'name (car pslist))))
10096                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10097           (setq e (point))
10098           (forward-line -1)             ; back to `b'
10099           (gnus-add-text-properties
10100            b (1- e) (list 'gnus-number gnus-reffed-article-number
10101                           gnus-mouse-face-prop gnus-mouse-face))
10102           (gnus-data-enter
10103            after-article gnus-reffed-article-number
10104            gnus-unread-mark b (car pslist) 0 (- e b))
10105           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10106           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10107           (setq pslist (cdr pslist)))))))
10108
10109 (defun gnus-pseudos< (p1 p2)
10110   (let ((c1 (cdr (assq 'action p1)))
10111         (c2 (cdr (assq 'action p2))))
10112     (and c1 c2 (string< c1 c2))))
10113
10114 (defun gnus-request-pseudo-article (props)
10115   (cond ((assq 'execute props)
10116          (gnus-execute-command (cdr (assq 'execute props)))))
10117   (let ((gnus-current-article (gnus-summary-article-number)))
10118     (gnus-run-hooks 'gnus-mark-article-hook)))
10119
10120 (defun gnus-execute-command (command &optional automatic)
10121   (save-excursion
10122     (gnus-article-setup-buffer)
10123     (set-buffer gnus-article-buffer)
10124     (setq buffer-read-only nil)
10125     (let ((command (if automatic command
10126                      (read-string "Command: " (cons command 0)))))
10127       (erase-buffer)
10128       (insert "$ " command "\n\n")
10129       (if gnus-view-pseudo-asynchronously
10130           (start-process "gnus-execute" (current-buffer) shell-file-name
10131                          shell-command-switch command)
10132         (call-process shell-file-name nil t nil
10133                       shell-command-switch command)))))
10134
10135 ;; Summary kill commands.
10136
10137 (defun gnus-summary-edit-global-kill (article)
10138   "Edit the \"global\" kill file."
10139   (interactive (list (gnus-summary-article-number)))
10140   (gnus-group-edit-global-kill article))
10141
10142 (defun gnus-summary-edit-local-kill ()
10143   "Edit a local kill file applied to the current newsgroup."
10144   (interactive)
10145   (setq gnus-current-headers (gnus-summary-article-header))
10146   (gnus-group-edit-local-kill
10147    (gnus-summary-article-number) gnus-newsgroup-name))
10148
10149 ;;; Header reading.
10150
10151 (defun gnus-read-header (id &optional header)
10152   "Read the headers of article ID and enter them into the Gnus system."
10153   (let ((group gnus-newsgroup-name)
10154         (gnus-override-method
10155          (or
10156           gnus-override-method
10157           (and (gnus-news-group-p gnus-newsgroup-name)
10158                (car (gnus-refer-article-methods)))))
10159         where)
10160     ;; First we check to see whether the header in question is already
10161     ;; fetched.
10162     (if (stringp id)
10163         ;; This is a Message-ID.
10164         (setq header (or header (gnus-id-to-header id)))
10165       ;; This is an article number.
10166       (setq header (or header (gnus-summary-article-header id))))
10167     (if (and header
10168              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10169         ;; We have found the header.
10170         header
10171       ;; If this is a sparse article, we have to nix out its
10172       ;; previous entry in the thread hashtb.
10173       (when (and header
10174                  (gnus-summary-article-sparse-p (mail-header-number header)))
10175         (let* ((parent (gnus-parent-id (mail-header-references header)))
10176                (thread (and parent (gnus-id-to-thread parent))))
10177           (when thread
10178             (delq (assq header thread) thread))))
10179       ;; We have to really fetch the header to this article.
10180       (save-excursion
10181         (set-buffer nntp-server-buffer)
10182         (when (setq where (gnus-request-head id group))
10183           (nnheader-fold-continuation-lines)
10184           (goto-char (point-max))
10185           (insert ".\n")
10186           (goto-char (point-min))
10187           (insert "211 ")
10188           (princ (cond
10189                   ((numberp id) id)
10190                   ((cdr where) (cdr where))
10191                   (header (mail-header-number header))
10192                   (t gnus-reffed-article-number))
10193                  (current-buffer))
10194           (insert " Article retrieved.\n"))
10195         (if (or (not where)
10196                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10197             ()                          ; Malformed head.
10198           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10199             (when (and (stringp id)
10200                        (not (string= (gnus-group-real-name group)
10201                                      (car where))))
10202               ;; If we fetched by Message-ID and the article came
10203               ;; from a different group, we fudge some bogus article
10204               ;; numbers for this article.
10205               (mail-header-set-number header gnus-reffed-article-number))
10206             (save-excursion
10207               (set-buffer gnus-summary-buffer)
10208               (decf gnus-reffed-article-number)
10209               (gnus-remove-header (mail-header-number header))
10210               (push header gnus-newsgroup-headers)
10211               (setq gnus-current-headers header)
10212               (push (mail-header-number header) gnus-newsgroup-limit)))
10213           header)))))
10214
10215 (defun gnus-remove-header (number)
10216   "Remove header NUMBER from `gnus-newsgroup-headers'."
10217   (if (and gnus-newsgroup-headers
10218            (= number (mail-header-number (car gnus-newsgroup-headers))))
10219       (pop gnus-newsgroup-headers)
10220     (let ((headers gnus-newsgroup-headers))
10221       (while (and (cdr headers)
10222                   (not (= number (mail-header-number (cadr headers)))))
10223         (pop headers))
10224       (when (cdr headers)
10225         (setcdr headers (cddr headers))))))
10226
10227 ;;;
10228 ;;; summary highlights
10229 ;;;
10230
10231 (defun gnus-highlight-selected-summary ()
10232   "Highlight selected article in summary buffer."
10233   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10234   (when gnus-summary-selected-face
10235     (save-excursion
10236       (let* ((beg (progn (beginning-of-line) (point)))
10237              (end (progn (end-of-line) (point)))
10238              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10239              (from (if (get-text-property beg gnus-mouse-face-prop)
10240                        beg
10241                      (or (next-single-property-change
10242                           beg gnus-mouse-face-prop nil end)
10243                          beg)))
10244              (to
10245               (if (= from end)
10246                   (- from 2)
10247                 (or (next-single-property-change
10248                      from gnus-mouse-face-prop nil end)
10249                     end))))
10250         ;; If no mouse-face prop on line we will have to = from = end,
10251         ;; so we highlight the entire line instead.
10252         (when (= (+ to 2) from)
10253           (setq from beg)
10254           (setq to end))
10255         (if gnus-newsgroup-selected-overlay
10256             ;; Move old overlay.
10257             (gnus-move-overlay
10258              gnus-newsgroup-selected-overlay from to (current-buffer))
10259           ;; Create new overlay.
10260           (gnus-overlay-put
10261            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10262            'face gnus-summary-selected-face))))))
10263
10264 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10265 (defun gnus-summary-highlight-line ()
10266   "Highlight current line according to `gnus-summary-highlight'."
10267   (let* ((list gnus-summary-highlight)
10268          (p (point))
10269          (end (progn (end-of-line) (point)))
10270          ;; now find out where the line starts and leave point there.
10271          (beg (progn (beginning-of-line) (point)))
10272          (article (gnus-summary-article-number))
10273          (score (or (cdr (assq (or article gnus-current-article)
10274                                gnus-newsgroup-scored))
10275                     gnus-summary-default-score 0))
10276          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10277          (inhibit-read-only t))
10278     ;; Eval the cars of the lists until we find a match.
10279     (let ((default gnus-summary-default-score)
10280           (default-high gnus-summary-default-high-score)
10281           (default-low gnus-summary-default-low-score))
10282       (while (and list
10283                   (not (eval (caar list))))
10284         (setq list (cdr list))))
10285     (let ((face (cdar list)))
10286       (unless (eq face (get-text-property beg 'face))
10287         (gnus-put-text-property-excluding-characters-with-faces
10288          beg end 'face
10289          (setq face (if (boundp face) (symbol-value face) face)))
10290         (when gnus-summary-highlight-line-function
10291           (funcall gnus-summary-highlight-line-function article face))))
10292     (goto-char p)))
10293
10294 (defun gnus-update-read-articles (group unread &optional compute)
10295   "Update the list of read articles in GROUP."
10296   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10297          (entry (gnus-gethash group gnus-newsrc-hashtb))
10298          (info (nth 2 entry))
10299          (prev 1)
10300          (unread (sort (copy-sequence unread) '<))
10301          read)
10302     (if (or (not info) (not active))
10303         ;; There is no info on this group if it was, in fact,
10304         ;; killed.  Gnus stores no information on killed groups, so
10305         ;; there's nothing to be done.
10306         ;; One could store the information somewhere temporarily,
10307         ;; perhaps...  Hmmm...
10308         ()
10309       ;; Remove any negative articles numbers.
10310       (while (and unread (< (car unread) 0))
10311         (setq unread (cdr unread)))
10312       ;; Remove any expired article numbers
10313       (while (and unread (< (car unread) (car active)))
10314         (setq unread (cdr unread)))
10315       ;; Compute the ranges of read articles by looking at the list of
10316       ;; unread articles.
10317       (while unread
10318         (when (/= (car unread) prev)
10319           (push (if (= prev (1- (car unread))) prev
10320                   (cons prev (1- (car unread))))
10321                 read))
10322         (setq prev (1+ (car unread)))
10323         (setq unread (cdr unread)))
10324       (when (<= prev (cdr active))
10325         (push (cons prev (cdr active)) read))
10326       (setq read (if (> (length read) 1) (nreverse read) read))
10327       (if compute
10328           read
10329         (save-excursion
10330           (let (setmarkundo)
10331             ;; Propagate the read marks to the backend.
10332             (when (gnus-check-backend-function 'request-set-mark group)
10333               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10334                     (add (gnus-remove-from-range read (gnus-info-read info))))
10335                 (when (or add del)
10336                   (unless (gnus-check-group group)
10337                     (error "Can't open server for %s" group))
10338                   (gnus-request-set-mark
10339                    group (delq nil (list (if add (list add 'add '(read)))
10340                                          (if del (list del 'del '(read))))))
10341                   (setq setmarkundo
10342                         `(gnus-request-set-mark
10343                           ,group
10344                           ',(delq nil (list
10345                                        (if del (list del 'add '(read)))
10346                                        (if add (list add 'del '(read))))))))))
10347             (set-buffer gnus-group-buffer)
10348             (gnus-undo-register
10349               `(progn
10350                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10351                  (gnus-info-set-read ',info ',(gnus-info-read info))
10352                  (gnus-get-unread-articles-in-group ',info
10353                                                     (gnus-active ,group))
10354                  (gnus-group-update-group ,group t)
10355                  ,setmarkundo))))
10356         ;; Enter this list into the group info.
10357         (gnus-info-set-read info read)
10358         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10359         (gnus-get-unread-articles-in-group info (gnus-active group))
10360         t))))
10361
10362 (defun gnus-offer-save-summaries ()
10363   "Offer to save all active summary buffers."
10364   (let (buffers)
10365     ;; Go through all buffers and find all summaries.
10366     (dolist (buffer (buffer-list))
10367       (when (and (setq buffer (buffer-name buffer))
10368                  (string-match "Summary" buffer)
10369                  (save-excursion
10370                    (set-buffer buffer)
10371                    ;; We check that this is, indeed, a summary buffer.
10372                    (and (eq major-mode 'gnus-summary-mode)
10373                         ;; Also make sure this isn't bogus.
10374                         gnus-newsgroup-prepared
10375                         ;; Also make sure that this isn't a
10376                         ;; dead summary buffer.
10377                         (not gnus-dead-summary-mode))))
10378         (push buffer buffers)))
10379     ;; Go through all these summary buffers and offer to save them.
10380     (when buffers
10381       (save-excursion
10382         (map-y-or-n-p
10383          "Update summary buffer %s? "
10384          (lambda (buf)
10385            (switch-to-buffer buf)
10386            (gnus-summary-exit))
10387          buffers)))))
10388
10389 (defun gnus-summary-setup-default-charset ()
10390   "Setup newsgroup default charset."
10391   (if (equal gnus-newsgroup-name "nndraft:drafts")
10392       (setq gnus-newsgroup-charset nil)
10393     (let* ((ignored-charsets
10394             (or gnus-newsgroup-ephemeral-ignored-charsets
10395                 (append
10396                  (and gnus-newsgroup-name
10397                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10398                  gnus-newsgroup-ignored-charsets))))
10399       (setq gnus-newsgroup-charset
10400             (or gnus-newsgroup-ephemeral-charset
10401                 (and gnus-newsgroup-name
10402                      (gnus-parameter-charset gnus-newsgroup-name))
10403                 gnus-default-charset))
10404       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10405            ignored-charsets))))
10406
10407 ;;;
10408 ;;; Mime Commands
10409 ;;;
10410
10411 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10412   "Display the current article buffer fully MIME-buttonized.
10413 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10414 treated as multipart/mixed."
10415   (interactive "P")
10416   (require 'gnus-art)
10417   (let ((gnus-unbuttonized-mime-types nil)
10418         (gnus-mime-display-multipart-as-mixed show-all-parts))
10419     (gnus-summary-show-article)))
10420
10421 (defun gnus-summary-repair-multipart (article)
10422   "Add a Content-Type header to a multipart article without one."
10423   (interactive (list (gnus-summary-article-number)))
10424   (gnus-with-article article
10425     (message-narrow-to-head)
10426     (message-remove-header "Mime-Version")
10427     (goto-char (point-max))
10428     (insert "Mime-Version: 1.0\n")
10429     (widen)
10430     (when (search-forward "\n--" nil t)
10431       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10432         (message-narrow-to-head)
10433         (message-remove-header "Content-Type")
10434         (goto-char (point-max))
10435         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10436                         separator))
10437         (widen))))
10438   (let (gnus-mark-article-hook)
10439     (gnus-summary-select-article t t nil article)))
10440
10441 (defun gnus-summary-toggle-display-buttonized ()
10442   "Toggle the buttonizing of the article buffer."
10443   (interactive)
10444   (require 'gnus-art)
10445   (if (setq gnus-inhibit-mime-unbuttonizing
10446             (not gnus-inhibit-mime-unbuttonizing))
10447       (let ((gnus-unbuttonized-mime-types nil))
10448         (gnus-summary-show-article))
10449     (gnus-summary-show-article)))
10450
10451 ;;;
10452 ;;; Generic summary marking commands
10453 ;;;
10454
10455 (defvar gnus-summary-marking-alist
10456   '((read gnus-del-mark "d")
10457     (unread gnus-unread-mark "u")
10458     (ticked gnus-ticked-mark "!")
10459     (dormant gnus-dormant-mark "?")
10460     (expirable gnus-expirable-mark "e"))
10461   "An alist of names/marks/keystrokes.")
10462
10463 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10464 (defvar gnus-summary-mark-map)
10465
10466 (defun gnus-summary-make-all-marking-commands ()
10467   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10468   (dolist (elem gnus-summary-marking-alist)
10469     (apply 'gnus-summary-make-marking-command elem)))
10470
10471 (defun gnus-summary-make-marking-command (name mark keystroke)
10472   (let ((map (make-sparse-keymap)))
10473     (define-key gnus-summary-generic-mark-map keystroke map)
10474     (dolist (lway `((next "next" next nil "n")
10475                     (next-unread "next unread" next t "N")
10476                     (prev "previous" prev nil "p")
10477                     (prev-unread "previous unread" prev t "P")
10478                     (nomove "" nil nil ,keystroke)))
10479       (let ((func (gnus-summary-make-marking-command-1
10480                    mark (car lway) lway name)))
10481         (setq func (eval func))
10482         (define-key map (nth 4 lway) func)))))
10483
10484 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10485   `(defun ,(intern
10486             (format "gnus-summary-put-mark-as-%s%s"
10487                     name (if (eq way 'nomove)
10488                              ""
10489                            (concat "-" (symbol-name way)))))
10490      (n)
10491      ,(format
10492        "Mark the current article as %s%s.
10493 If N, the prefix, then repeat N times.
10494 If N is negative, move in reverse order.
10495 The difference between N and the actual number of articles marked is
10496 returned."
10497        name (cadr lway))
10498      (interactive "p")
10499      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10500
10501 (defun gnus-summary-generic-mark (n mark move unread)
10502   "Mark N articles with MARK."
10503   (unless (eq major-mode 'gnus-summary-mode)
10504     (error "This command can only be used in the summary buffer"))
10505   (gnus-summary-show-thread)
10506   (let ((nummove
10507          (cond
10508           ((eq move 'next) 1)
10509           ((eq move 'prev) -1)
10510           (t 0))))
10511     (if (zerop nummove)
10512         (setq n 1)
10513       (when (< n 0)
10514         (setq n (abs n)
10515               nummove (* -1 nummove))))
10516     (while (and (> n 0)
10517                 (gnus-summary-mark-article nil mark)
10518                 (zerop (gnus-summary-next-subject nummove unread t)))
10519       (setq n (1- n)))
10520     (when (/= 0 n)
10521       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10522     (gnus-summary-recenter)
10523     (gnus-summary-position-point)
10524     (gnus-set-mode-line 'summary)
10525     n))
10526
10527 (defun gnus-summary-insert-articles (articles)
10528   (when (setq articles
10529               (gnus-set-difference articles
10530                                    (mapcar (lambda (h) (mail-header-number h))
10531                                            gnus-newsgroup-headers)))
10532     (setq gnus-newsgroup-headers
10533           (merge 'list
10534                  gnus-newsgroup-headers
10535                  (gnus-fetch-headers articles)
10536                  'gnus-article-sort-by-number))
10537     ;; Suppress duplicates?
10538     (when gnus-suppress-duplicates
10539       (gnus-dup-suppress-articles))
10540
10541     ;; We might want to build some more threads first.
10542     (when (and gnus-fetch-old-headers
10543                (eq gnus-headers-retrieved-by 'nov))
10544       (if (eq gnus-fetch-old-headers 'invisible)
10545           (gnus-build-all-threads)
10546         (gnus-build-old-threads)))
10547     ;; Let the Gnus agent mark articles as read.
10548     (when gnus-agent
10549       (gnus-agent-get-undownloaded-list))
10550     ;; Remove list identifiers from subject
10551     (when gnus-list-identifiers
10552       (gnus-summary-remove-list-identifiers))
10553     ;; First and last article in this newsgroup.
10554     (when gnus-newsgroup-headers
10555       (setq gnus-newsgroup-begin
10556             (mail-header-number (car gnus-newsgroup-headers))
10557             gnus-newsgroup-end
10558             (mail-header-number
10559              (gnus-last-element gnus-newsgroup-headers))))
10560     (when gnus-use-scoring
10561       (gnus-possibly-score-headers))))
10562
10563 (defun gnus-summary-insert-old-articles (&optional all)
10564   "Insert all old articles in this group.
10565 If ALL is non-nil, already read articles become readable.
10566 If ALL is a number, fetch this number of articles."
10567   (interactive "P")
10568   (prog1
10569       (let ((old (mapcar 'car gnus-newsgroup-data))
10570             (i (car gnus-newsgroup-active))
10571             older len)
10572         (while (<= i (cdr gnus-newsgroup-active))
10573           (or (memq i old) (push i older))
10574           (incf i))
10575         (setq len (length older))
10576         (cond
10577          ((null older) nil)
10578          ((numberp all)
10579           (if (< all len)
10580               (setq older (subseq older 0 all))))
10581          (all nil)
10582          (t
10583           (if (and (numberp gnus-large-newsgroup)
10584                    (> len gnus-large-newsgroup))
10585               (let ((input
10586                      (read-string
10587                       (format
10588                        "How many articles from %s (default %d): "
10589                        (gnus-limit-string
10590                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10591                        len))))
10592                 (unless (string-match "^[ \t]*$" input)
10593                   (setq all (string-to-number input))
10594                   (if (< all len)
10595                       (setq older (subseq older 0 all))))))))
10596         (if (not older)
10597             (message "No old news.")
10598           (gnus-summary-insert-articles older)
10599           (gnus-summary-limit (gnus-union older old))))
10600     (gnus-summary-position-point)))
10601
10602 (defun gnus-summary-insert-new-articles ()
10603   "Insert all new articles in this group."
10604   (interactive)
10605   (prog1
10606       (let ((old (mapcar 'car gnus-newsgroup-data))
10607             (old-active gnus-newsgroup-active)
10608             (nnmail-fetched-sources (list t))
10609             i new)
10610         (setq gnus-newsgroup-active
10611               (gnus-activate-group gnus-newsgroup-name 'scan))
10612         (setq i (1+ (cdr old-active)))
10613         (while (<= i (cdr gnus-newsgroup-active))
10614           (push i new)
10615           (incf i))
10616         (if (not new)
10617             (message "No gnus is bad news.")
10618           (setq new (nreverse new))
10619           (gnus-summary-insert-articles new)
10620           (setq gnus-newsgroup-unreads
10621                 (append gnus-newsgroup-unreads new))
10622           (gnus-summary-limit (gnus-union old new))))
10623     (gnus-summary-position-point)))
10624
10625 (gnus-summary-make-all-marking-commands)
10626
10627 (gnus-ems-redefine)
10628
10629 (provide 'gnus-sum)
10630
10631 (run-hooks 'gnus-sum-load-hook)
10632
10633 ;;; gnus-sum.el ends here