* gnus-spec.el (gnus-correct-substring): Take optional END.
[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)
700   :group 'gnus-summary-various
701   :type 'hook)
702
703 ;; Extracted from gnus-xmas-redefine in order to preserve user settings
704 (when (featurep 'xemacs)
705   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
706   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar)
707   (add-hook 'gnus-summary-mode-hook
708             'gnus-xmas-switch-horizontal-scrollbar-off))
709
710 (defcustom gnus-summary-menu-hook nil
711   "*Hook run after the creation of the summary mode menu."
712   :group 'gnus-summary-visual
713   :type 'hook)
714
715 (defcustom gnus-summary-exit-hook nil
716   "*A hook called on exit from the summary buffer.
717 It will be called with point in the group buffer."
718   :group 'gnus-summary-exit
719   :type 'hook)
720
721 (defcustom gnus-summary-prepare-hook nil
722   "*A hook called after the summary buffer has been generated.
723 If you want to modify the summary buffer, you can use this hook."
724   :group 'gnus-summary-various
725   :type 'hook)
726
727 (defcustom gnus-summary-prepared-hook nil
728   "*A hook called as the last thing after the summary buffer has been generated."
729   :group 'gnus-summary-various
730   :type 'hook)
731
732 (defcustom gnus-summary-generate-hook nil
733   "*A hook run just before generating the summary buffer.
734 This hook is commonly used to customize threading variables and the
735 like."
736   :group 'gnus-summary-various
737   :type 'hook)
738
739 (defcustom gnus-select-group-hook nil
740   "*A hook called when a newsgroup is selected.
741
742 If you'd like to simplify subjects like the
743 `gnus-summary-next-same-subject' command does, you can use the
744 following hook:
745
746  (setq gnus-select-group-hook
747       (list
748         (lambda ()
749           (mapcar (lambda (header)
750                      (mail-header-set-subject
751                       header
752                       (gnus-simplify-subject
753                        (mail-header-subject header) 're-only)))
754                   gnus-newsgroup-headers))))"
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-select-article-hook nil
759   "*A hook called when an article is selected."
760   :group 'gnus-summary-choose
761   :type 'hook)
762
763 (defcustom gnus-visual-mark-article-hook
764   (list 'gnus-highlight-selected-summary)
765   "*Hook run after selecting an article in the summary buffer.
766 It is meant to be used for highlighting the article in some way.  It
767 is not run if `gnus-visual' is nil."
768   :group 'gnus-summary-visual
769   :type 'hook)
770
771 (defcustom gnus-parse-headers-hook nil
772   "*A hook called before parsing the headers."
773   :group 'gnus-various
774   :type 'hook)
775
776 (defcustom gnus-exit-group-hook nil
777   "*A hook called when exiting summary mode.
778 This hook is not called from the non-updating exit commands like `Q'."
779   :group 'gnus-various
780   :type 'hook)
781
782 (defcustom gnus-summary-update-hook
783   (list 'gnus-summary-highlight-line)
784   "*A hook called when a summary line is changed.
785 The hook will not be called if `gnus-visual' is nil.
786
787 The default function `gnus-summary-highlight-line' will
788 highlight the line according to the `gnus-summary-highlight'
789 variable."
790   :group 'gnus-summary-visual
791   :type 'hook)
792
793 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
794   "*A hook called when an article is selected for the first time.
795 The hook is intended to mark an article as read (or unread)
796 automatically when it is selected."
797   :group 'gnus-summary-choose
798   :type 'hook)
799
800 (defcustom gnus-group-no-more-groups-hook nil
801   "*A hook run when returning to group mode having no more (unread) groups."
802   :group 'gnus-group-select
803   :type 'hook)
804
805 (defcustom gnus-ps-print-hook nil
806   "*A hook run before ps-printing something from Gnus."
807   :group 'gnus-summary
808   :type 'hook)
809
810 (defcustom gnus-summary-display-arrow
811   (and (fboundp 'display-graphic-p)
812        (display-graphic-p))
813   "*If non-nil, display an arrow highlighting the current article."
814   :version "21.1"
815   :group 'gnus-summary
816   :type 'boolean)
817
818 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
819   "Face used for highlighting the current article in the summary buffer."
820   :group 'gnus-summary-visual
821   :type 'face)
822
823 (defcustom gnus-summary-highlight
824   '(((= mark gnus-canceled-mark)
825      . gnus-summary-cancelled-face)
826     ((and (> score default-high)
827           (or (= mark gnus-dormant-mark)
828               (= mark gnus-ticked-mark)))
829      . gnus-summary-high-ticked-face)
830     ((and (< score default-low)
831           (or (= mark gnus-dormant-mark)
832               (= mark gnus-ticked-mark)))
833      . gnus-summary-low-ticked-face)
834     ((or (= mark gnus-dormant-mark)
835          (= mark gnus-ticked-mark))
836      . gnus-summary-normal-ticked-face)
837     ((and (> score default-high) (= mark gnus-ancient-mark))
838      . gnus-summary-high-ancient-face)
839     ((and (< score default-low) (= mark gnus-ancient-mark))
840      . gnus-summary-low-ancient-face)
841     ((= mark gnus-ancient-mark)
842      . gnus-summary-normal-ancient-face)
843     ((and (> score default-high) (= mark gnus-unread-mark))
844      . gnus-summary-high-unread-face)
845     ((and (< score default-low) (= mark gnus-unread-mark))
846      . gnus-summary-low-unread-face)
847     ((= mark gnus-unread-mark)
848      . gnus-summary-normal-unread-face)
849     ((and (> score default-high) (memq mark (list gnus-downloadable-mark
850                                                   gnus-undownloaded-mark)))
851      . gnus-summary-high-unread-face)
852     ((and (< score default-low) (memq mark (list gnus-downloadable-mark
853                                                  gnus-undownloaded-mark)))
854      . gnus-summary-low-unread-face)
855     ((and (memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
856           (memq article gnus-newsgroup-unreads))
857      . gnus-summary-normal-unread-face)
858     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
859      . gnus-summary-normal-read-face)
860     ((> score default-high)
861      . gnus-summary-high-read-face)
862     ((< score default-low)
863      . gnus-summary-low-read-face)
864     (t
865      . gnus-summary-normal-read-face))
866   "*Controls the highlighting of summary buffer lines.
867
868 A list of (FORM . FACE) pairs.  When deciding how a a particular
869 summary line should be displayed, each form is evaluated.  The content
870 of the face field after the first true form is used.  You can change
871 how those summary lines are displayed, by editing the face field.
872
873 You can use the following variables in the FORM field.
874
875 score:        The article's score
876 default:      The default article score.
877 default-high: The default score for high scored articles.
878 default-low:  The default score for low scored articles.
879 below:        The score below which articles are automatically marked as read.
880 mark:         The articles mark."
881   :group 'gnus-summary-visual
882   :type '(repeat (cons (sexp :tag "Form" nil)
883                        face)))
884
885 (defcustom gnus-alter-header-function nil
886   "Function called to allow alteration of article header structures.
887 The function is called with one parameter, the article header vector,
888 which it may alter in any way.")
889
890 (defvar gnus-decode-encoded-word-function 'mail-decode-encoded-word-string
891   "Variable that says which function should be used to decode a string with encoded words.")
892
893 (defcustom gnus-extra-headers nil
894   "*Extra headers to parse."
895   :version "21.1"
896   :group 'gnus-summary
897   :type '(repeat symbol))
898
899 (defcustom gnus-ignored-from-addresses
900   (and user-mail-address (regexp-quote user-mail-address))
901   "*Regexp of From headers that may be suppressed in favor of To headers."
902   :version "21.1"
903   :group 'gnus-summary
904   :type 'regexp)
905
906 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
907   "List of charsets that should be ignored.
908 When these charsets are used in the \"charset\" parameter, the
909 default charset will be used instead."
910   :version "21.1"
911   :type '(repeat symbol)
912   :group 'gnus-charset)
913
914 (gnus-define-group-parameter
915  ignored-charsets
916  :type list
917  :function-document
918  "Return the ignored charsets of GROUP."
919  :variable gnus-group-ignored-charsets-alist
920  :variable-default
921  '(("alt\\.chinese\\.text" iso-8859-1))
922  :variable-document
923  "Alist of regexps (to match group names) and charsets that should be ignored.
924 When these charsets are used in the \"charset\" parameter, the
925 default charset will be used instead."
926  :variable-group gnus-charset
927  :variable-type '(repeat (cons (regexp :tag "Group")
928                                (repeat symbol)))
929  :parameter-type '(choice :tag "Ignored charsets"
930                           :value nil
931                           (repeat (symbol)))
932  :parameter-document       "\
933 List of charsets that should be ignored.
934
935 When these charsets are used in the \"charset\" parameter, the
936 default charset will be used instead.")
937
938 (defcustom gnus-group-highlight-words-alist nil
939   "Alist of group regexps and highlight regexps.
940 This variable uses the same syntax as `gnus-emphasis-alist'."
941   :version "21.1"
942   :type '(repeat (cons (regexp :tag "Group")
943                        (repeat (list (regexp :tag "Highlight regexp")
944                                      (number :tag "Group for entire word" 0)
945                                      (number :tag "Group for displayed part" 0)
946                                      (symbol :tag "Face"
947                                              gnus-emphasis-highlight-words)))))
948   :group 'gnus-summary-visual)
949
950 (defcustom gnus-summary-show-article-charset-alist
951   nil
952   "Alist of number and charset.
953 The article will be shown with the charset corresponding to the
954 numbered argument.
955 For example: ((1 . cn-gb-2312) (2 . big5))."
956   :version "21.1"
957   :type '(repeat (cons (number :tag "Argument" 1)
958                        (symbol :tag "Charset")))
959   :group 'gnus-charset)
960
961 (defcustom gnus-preserve-marks t
962   "Whether marks are preserved when moving, copying and respooling messages."
963   :version "21.1"
964   :type 'boolean
965   :group 'gnus-summary-marks)
966
967 (defcustom gnus-alter-articles-to-read-function nil
968   "Function to be called to alter the list of articles to be selected."
969   :type '(choice (const nil) function)
970   :group 'gnus-summary)
971
972 (defcustom gnus-orphan-score nil
973   "*All orphans get this score added.  Set in the score file."
974   :group 'gnus-score-default
975   :type '(choice (const nil)
976                  integer))
977
978 (defcustom gnus-summary-save-parts-default-mime "image/.*"
979   "*A regexp to match MIME parts when saving multiple parts of a message
980 with gnus-summary-save-parts (X m). This regexp will be used by default
981 when prompting the user for which type of files to save."
982   :group 'gnus-summary
983   :type 'regexp)
984
985
986 (defcustom gnus-summary-save-parts-default-mime "image/.*"
987   "*A regexp to match MIME parts when saving multiple parts of a message
988 with gnus-summary-save-parts (X m). This regexp will be used by default
989 when prompting the user for which type of files to save."
990   :group 'gnus-summary
991   :type 'regexp)
992
993 (defcustom gnus-read-all-available-headers nil
994   "Whether Gnus should parse all headers made available to it.
995 This is mostly relevant for slow backends where the user may
996 wish to widen the summary buffer to include all headers
997 that were fetched.  Say, for nnultimate groups."
998   :group 'gnus-summary
999   :type '(choice boolean regexp))
1000
1001 ;;; Internal variables
1002
1003 (defvar gnus-summary-display-cache nil)
1004 (defvar gnus-article-mime-handles nil)
1005 (defvar gnus-article-decoded-p nil)
1006 (defvar gnus-article-charset nil)
1007 (defvar gnus-article-ignored-charsets nil)
1008 (defvar gnus-scores-exclude-files nil)
1009 (defvar gnus-page-broken nil)
1010 (defvar gnus-inhibit-mime-unbuttonizing nil)
1011
1012 (defvar gnus-original-article nil)
1013 (defvar gnus-article-internal-prepare-hook nil)
1014 (defvar gnus-newsgroup-process-stack nil)
1015
1016 (defvar gnus-thread-indent-array nil)
1017 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
1018 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
1019   "Function called to sort the articles within a thread after it has been gathered together.")
1020
1021 (defvar gnus-summary-save-parts-type-history nil)
1022 (defvar gnus-summary-save-parts-last-directory nil)
1023
1024 (defvar gnus-summary-save-parts-type-history nil)
1025 (defvar gnus-summary-save-parts-last-directory nil)
1026
1027 ;; Avoid highlighting in kill files.
1028 (defvar gnus-summary-inhibit-highlight nil)
1029 (defvar gnus-newsgroup-selected-overlay nil)
1030 (defvar gnus-inhibit-limiting nil)
1031 (defvar gnus-newsgroup-adaptive-score-file nil)
1032 (defvar gnus-current-score-file nil)
1033 (defvar gnus-current-move-group nil)
1034 (defvar gnus-current-copy-group nil)
1035 (defvar gnus-current-crosspost-group nil)
1036 (defvar gnus-newsgroup-display nil)
1037
1038 (defvar gnus-newsgroup-dependencies nil)
1039 (defvar gnus-newsgroup-adaptive nil)
1040 (defvar gnus-summary-display-article-function nil)
1041 (defvar gnus-summary-highlight-line-function nil
1042   "Function called after highlighting a summary line.")
1043
1044 (defvar gnus-summary-line-format-alist
1045   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1046     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1047     (?s gnus-tmp-subject-or-nil ?s)
1048     (?n gnus-tmp-name ?s)
1049     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1050         ?s)
1051     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1052             gnus-tmp-from) ?s)
1053     (?F gnus-tmp-from ?s)
1054     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1055     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1056     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1057     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
1058     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1059     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1060     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1061     (?L gnus-tmp-lines ?s)
1062     (?I gnus-tmp-indentation ?s)
1063     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1064     (?R gnus-tmp-replied ?c)
1065     (?\[ gnus-tmp-opening-bracket ?c)
1066     (?\] gnus-tmp-closing-bracket ?c)
1067     (?\> (make-string gnus-tmp-level ? ) ?s)
1068     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1069     (?i gnus-tmp-score ?d)
1070     (?z gnus-tmp-score-char ?c)
1071     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1072     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1073     (?U gnus-tmp-unread ?c)
1074     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1075     (?t (gnus-summary-number-of-articles-in-thread
1076          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1077         ?d)
1078     (?e (gnus-summary-number-of-articles-in-thread
1079          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1080         ?c)
1081     (?u gnus-tmp-user-defined ?s)
1082     (?P (gnus-pick-line-number) ?d)
1083     (?B gnus-tmp-thread-tree-header-string ?s))
1084   "An alist of format specifications that can appear in summary lines.
1085 These are paired with what variables they correspond with, along with
1086 the type of the variable (string, integer, character, etc).")
1087
1088 (defvar gnus-summary-dummy-line-format-alist
1089   `((?S gnus-tmp-subject ?s)
1090     (?N gnus-tmp-number ?d)
1091     (?u gnus-tmp-user-defined ?s)))
1092
1093 (defvar gnus-summary-mode-line-format-alist
1094   `((?G gnus-tmp-group-name ?s)
1095     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1096     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1097     (?A gnus-tmp-article-number ?d)
1098     (?Z gnus-tmp-unread-and-unselected ?s)
1099     (?V gnus-version ?s)
1100     (?U gnus-tmp-unread-and-unticked ?d)
1101     (?S gnus-tmp-subject ?s)
1102     (?e gnus-tmp-unselected ?d)
1103     (?u gnus-tmp-user-defined ?s)
1104     (?d (length gnus-newsgroup-dormant) ?d)
1105     (?t (length gnus-newsgroup-marked) ?d)
1106     (?r (length gnus-newsgroup-reads) ?d)
1107     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1108     (?E gnus-newsgroup-expunged-tally ?d)
1109     (?s (gnus-current-score-file-nondirectory) ?s)))
1110
1111 (defvar gnus-last-search-regexp nil
1112   "Default regexp for article search command.")
1113
1114 (defvar gnus-last-shell-command nil
1115   "Default shell command on article.")
1116
1117 (defvar gnus-newsgroup-begin nil)
1118 (defvar gnus-newsgroup-end nil)
1119 (defvar gnus-newsgroup-last-rmail nil)
1120 (defvar gnus-newsgroup-last-mail nil)
1121 (defvar gnus-newsgroup-last-folder nil)
1122 (defvar gnus-newsgroup-last-file nil)
1123 (defvar gnus-newsgroup-auto-expire nil)
1124 (defvar gnus-newsgroup-active nil)
1125
1126 (defvar gnus-newsgroup-data nil)
1127 (defvar gnus-newsgroup-data-reverse nil)
1128 (defvar gnus-newsgroup-limit nil)
1129 (defvar gnus-newsgroup-limits nil)
1130
1131 (defvar gnus-newsgroup-unreads nil
1132   "List of unread articles in the current newsgroup.")
1133
1134 (defvar gnus-newsgroup-unselected nil
1135   "List of unselected unread articles in the current newsgroup.")
1136
1137 (defvar gnus-newsgroup-reads nil
1138   "Alist of read articles and article marks in the current newsgroup.")
1139
1140 (defvar gnus-newsgroup-expunged-tally nil)
1141
1142 (defvar gnus-newsgroup-marked nil
1143   "List of ticked articles in the current newsgroup (a subset of unread art).")
1144
1145 (defvar gnus-newsgroup-killed nil
1146   "List of ranges of articles that have been through the scoring process.")
1147
1148 (defvar gnus-newsgroup-cached nil
1149   "List of articles that come from the article cache.")
1150
1151 (defvar gnus-newsgroup-saved nil
1152   "List of articles that have been saved.")
1153
1154 (defvar gnus-newsgroup-kill-headers nil)
1155
1156 (defvar gnus-newsgroup-replied nil
1157   "List of articles that have been replied to in the current newsgroup.")
1158
1159 (defvar gnus-newsgroup-forwarded nil
1160   "List of articles that have been forwarded in the current newsgroup.")
1161
1162 (defvar gnus-newsgroup-recent nil
1163   "List of articles that have are recent in the current newsgroup.")
1164
1165 (defvar gnus-newsgroup-expirable nil
1166   "List of articles in the current newsgroup that can be expired.")
1167
1168 (defvar gnus-newsgroup-processable nil
1169   "List of articles in the current newsgroup that can be processed.")
1170
1171 (defvar gnus-newsgroup-downloadable nil
1172   "List of articles in the current newsgroup that can be processed.")
1173
1174 (defvar gnus-newsgroup-undownloaded nil
1175   "List of articles in the current newsgroup that haven't been downloaded..")
1176
1177 (defvar gnus-newsgroup-unsendable nil
1178   "List of articles in the current newsgroup that won't be sent.")
1179
1180 (defvar gnus-newsgroup-bookmarks nil
1181   "List of articles in the current newsgroup that have bookmarks.")
1182
1183 (defvar gnus-newsgroup-dormant nil
1184   "List of dormant articles in the current newsgroup.")
1185
1186 (defvar gnus-newsgroup-unseen nil
1187   "List of unseen articles in the current newsgroup.")
1188
1189 (defvar gnus-newsgroup-seen nil
1190   "Range of seen articles in the current newsgroup.")
1191
1192 (defvar gnus-newsgroup-articles nil
1193   "List of articles in the current newsgroup.")
1194
1195 (defvar gnus-newsgroup-scored nil
1196   "List of scored articles in the current newsgroup.")
1197
1198 (defvar gnus-newsgroup-headers nil
1199   "List of article headers in the current newsgroup.")
1200
1201 (defvar gnus-newsgroup-threads nil)
1202
1203 (defvar gnus-newsgroup-prepared nil
1204   "Whether the current group has been prepared properly.")
1205
1206 (defvar gnus-newsgroup-ancient nil
1207   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1208
1209 (defvar gnus-newsgroup-sparse nil)
1210
1211 (defvar gnus-current-article nil)
1212 (defvar gnus-article-current nil)
1213 (defvar gnus-current-headers nil)
1214 (defvar gnus-have-all-headers nil)
1215 (defvar gnus-last-article nil)
1216 (defvar gnus-newsgroup-history nil)
1217 (defvar gnus-newsgroup-charset nil)
1218 (defvar gnus-newsgroup-ephemeral-charset nil)
1219 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1220
1221 (defvar gnus-article-before-search nil)
1222
1223 (defconst gnus-summary-local-variables
1224   '(gnus-newsgroup-name
1225     gnus-newsgroup-begin gnus-newsgroup-end
1226     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1227     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1228     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1229     gnus-newsgroup-unselected gnus-newsgroup-marked
1230     gnus-newsgroup-reads gnus-newsgroup-saved
1231     gnus-newsgroup-replied gnus-newsgroup-forwarded
1232     gnus-newsgroup-recent
1233     gnus-newsgroup-expirable
1234     gnus-newsgroup-processable gnus-newsgroup-killed
1235     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1236     gnus-newsgroup-unsendable gnus-newsgroup-unseen
1237     gnus-newsgroup-seen gnus-newsgroup-articles
1238     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1239     gnus-newsgroup-headers gnus-newsgroup-threads
1240     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1241     gnus-current-article gnus-current-headers gnus-have-all-headers
1242     gnus-last-article gnus-article-internal-prepare-hook
1243     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1244     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1245     gnus-thread-expunge-below
1246     gnus-score-alist gnus-current-score-file
1247     (gnus-summary-expunge-below . global)
1248     (gnus-summary-mark-below . global)
1249     (gnus-orphan-score . global)
1250     gnus-newsgroup-active gnus-scores-exclude-files
1251     gnus-newsgroup-history gnus-newsgroup-ancient
1252     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1253     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1254     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1255     (gnus-newsgroup-expunged-tally . 0)
1256     gnus-cache-removable-articles gnus-newsgroup-cached
1257     gnus-newsgroup-data gnus-newsgroup-data-reverse
1258     gnus-newsgroup-limit gnus-newsgroup-limits
1259     gnus-newsgroup-charset gnus-newsgroup-display)
1260   "Variables that are buffer-local to the summary buffers.")
1261
1262 (defvar gnus-newsgroup-variables nil
1263   "Variables that have separate values in the newsgroups.")
1264
1265 ;; Byte-compiler warning.
1266 (eval-when-compile (defvar gnus-article-mode-map))
1267
1268 ;; MIME stuff.
1269
1270 (defvar gnus-decode-encoded-word-methods
1271   '(mail-decode-encoded-word-string)
1272   "List of methods used to decode encoded words.
1273
1274 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item is
1275 FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
1276 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
1277 whose names match REGEXP.
1278
1279 For example:
1280 ((\"chinese\" . gnus-decode-encoded-word-string-by-guess)
1281  mail-decode-encoded-word-string
1282  (\"chinese\" . rfc1843-decode-string))")
1283
1284 (defvar gnus-decode-encoded-word-methods-cache nil)
1285
1286 (defun gnus-multi-decode-encoded-word-string (string)
1287   "Apply the functions from `gnus-encoded-word-methods' that match."
1288   (unless (and gnus-decode-encoded-word-methods-cache
1289                (eq gnus-newsgroup-name
1290                    (car gnus-decode-encoded-word-methods-cache)))
1291     (setq gnus-decode-encoded-word-methods-cache (list gnus-newsgroup-name))
1292     (mapcar (lambda (x)
1293               (if (symbolp x)
1294                   (nconc gnus-decode-encoded-word-methods-cache (list x))
1295                 (if (and gnus-newsgroup-name
1296                          (string-match (car x) gnus-newsgroup-name))
1297                     (nconc gnus-decode-encoded-word-methods-cache
1298                            (list (cdr x))))))
1299             gnus-decode-encoded-word-methods))
1300   (let ((xlist gnus-decode-encoded-word-methods-cache))
1301     (pop xlist)
1302     (while xlist
1303       (setq string (funcall (pop xlist) string))))
1304   string)
1305
1306 ;; Subject simplification.
1307
1308 (defun gnus-simplify-whitespace (str)
1309   "Remove excessive whitespace from STR."
1310   (let ((mystr str))
1311     ;; Multiple spaces.
1312     (while (string-match "[ \t][ \t]+" mystr)
1313       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1314                           " "
1315                           (substring mystr (match-end 0)))))
1316     ;; Leading spaces.
1317     (when (string-match "^[ \t]+" mystr)
1318       (setq mystr (substring mystr (match-end 0))))
1319     ;; Trailing spaces.
1320     (when (string-match "[ \t]+$" mystr)
1321       (setq mystr (substring mystr 0 (match-beginning 0))))
1322     mystr))
1323
1324 (defsubst gnus-simplify-subject-re (subject)
1325   "Remove \"Re:\" from subject lines."
1326   (if (string-match message-subject-re-regexp subject)
1327       (substring subject (match-end 0))
1328     subject))
1329
1330 (defun gnus-simplify-subject (subject &optional re-only)
1331   "Remove `Re:' and words in parentheses.
1332 If RE-ONLY is non-nil, strip leading `Re:'s only."
1333   (let ((case-fold-search t))           ;Ignore case.
1334     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1335     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1336       (setq subject (substring subject (match-end 0))))
1337     ;; Remove uninteresting prefixes.
1338     (when (and (not re-only)
1339                gnus-simplify-ignored-prefixes
1340                (string-match gnus-simplify-ignored-prefixes subject))
1341       (setq subject (substring subject (match-end 0))))
1342     ;; Remove words in parentheses from end.
1343     (unless re-only
1344       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1345         (setq subject (substring subject 0 (match-beginning 0)))))
1346     ;; Return subject string.
1347     subject))
1348
1349 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1350 ;; all whitespace.
1351 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1352   (goto-char (point-min))
1353   (while (re-search-forward regexp nil t)
1354     (replace-match (or newtext ""))))
1355
1356 (defun gnus-simplify-buffer-fuzzy ()
1357   "Simplify string in the buffer fuzzily.
1358 The string in the accessible portion of the current buffer is simplified.
1359 It is assumed to be a single-line subject.
1360 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1361 matter is removed.  Additional things can be deleted by setting
1362 `gnus-simplify-subject-fuzzy-regexp'."
1363   (let ((case-fold-search t)
1364         (modified-tick))
1365     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1366
1367     (while (not (eq modified-tick (buffer-modified-tick)))
1368       (setq modified-tick (buffer-modified-tick))
1369       (cond
1370        ((listp gnus-simplify-subject-fuzzy-regexp)
1371         (mapcar 'gnus-simplify-buffer-fuzzy-step
1372                 gnus-simplify-subject-fuzzy-regexp))
1373        (gnus-simplify-subject-fuzzy-regexp
1374         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1375       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1376       (gnus-simplify-buffer-fuzzy-step
1377        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1378       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1379
1380     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1381     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1382     (gnus-simplify-buffer-fuzzy-step " $")
1383     (gnus-simplify-buffer-fuzzy-step "^ +")))
1384
1385 (defun gnus-simplify-subject-fuzzy (subject)
1386   "Simplify a subject string fuzzily.
1387 See `gnus-simplify-buffer-fuzzy' for details."
1388   (save-excursion
1389     (gnus-set-work-buffer)
1390     (let ((case-fold-search t))
1391       ;; Remove uninteresting prefixes.
1392       (when (and gnus-simplify-ignored-prefixes
1393                  (string-match gnus-simplify-ignored-prefixes subject))
1394         (setq subject (substring subject (match-end 0))))
1395       (insert subject)
1396       (inline (gnus-simplify-buffer-fuzzy))
1397       (buffer-string))))
1398
1399 (defsubst gnus-simplify-subject-fully (subject)
1400   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1401   (cond
1402    (gnus-simplify-subject-functions
1403     (gnus-map-function gnus-simplify-subject-functions subject))
1404    ((null gnus-summary-gather-subject-limit)
1405     (gnus-simplify-subject-re subject))
1406    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1407     (gnus-simplify-subject-fuzzy subject))
1408    ((numberp gnus-summary-gather-subject-limit)
1409     (gnus-limit-string (gnus-simplify-subject-re subject)
1410                        gnus-summary-gather-subject-limit))
1411    (t
1412     subject)))
1413
1414 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1415   "Check whether two subjects are equal.
1416 If optional argument simple-first is t, first argument is already
1417 simplified."
1418   (cond
1419    ((null simple-first)
1420     (equal (gnus-simplify-subject-fully s1)
1421            (gnus-simplify-subject-fully s2)))
1422    (t
1423     (equal s1
1424            (gnus-simplify-subject-fully s2)))))
1425
1426 (defun gnus-summary-bubble-group ()
1427   "Increase the score of the current group.
1428 This is a handy function to add to `gnus-summary-exit-hook' to
1429 increase the score of each group you read."
1430   (gnus-group-add-score gnus-newsgroup-name))
1431
1432 \f
1433 ;;;
1434 ;;; Gnus summary mode
1435 ;;;
1436
1437 (put 'gnus-summary-mode 'mode-class 'special)
1438
1439 (defvar gnus-article-commands-menu)
1440
1441 (when t
1442   ;; Non-orthogonal keys
1443
1444   (gnus-define-keys gnus-summary-mode-map
1445     " " gnus-summary-next-page
1446     "\177" gnus-summary-prev-page
1447     [delete] gnus-summary-prev-page
1448     [backspace] gnus-summary-prev-page
1449     "\r" gnus-summary-scroll-up
1450     "\M-\r" gnus-summary-scroll-down
1451     "n" gnus-summary-next-unread-article
1452     "p" gnus-summary-prev-unread-article
1453     "N" gnus-summary-next-article
1454     "P" gnus-summary-prev-article
1455     "\M-\C-n" gnus-summary-next-same-subject
1456     "\M-\C-p" gnus-summary-prev-same-subject
1457     "\M-n" gnus-summary-next-unread-subject
1458     "\M-p" gnus-summary-prev-unread-subject
1459     "." gnus-summary-first-unread-article
1460     "," gnus-summary-best-unread-article
1461     "\M-s" gnus-summary-search-article-forward
1462     "\M-r" gnus-summary-search-article-backward
1463     "<" gnus-summary-beginning-of-article
1464     ">" gnus-summary-end-of-article
1465     "j" gnus-summary-goto-article
1466     "^" gnus-summary-refer-parent-article
1467     "\M-^" gnus-summary-refer-article
1468     "u" gnus-summary-tick-article-forward
1469     "!" gnus-summary-tick-article-forward
1470     "U" gnus-summary-tick-article-backward
1471     "d" gnus-summary-mark-as-read-forward
1472     "D" gnus-summary-mark-as-read-backward
1473     "E" gnus-summary-mark-as-expirable
1474     "\M-u" gnus-summary-clear-mark-forward
1475     "\M-U" gnus-summary-clear-mark-backward
1476     "k" gnus-summary-kill-same-subject-and-select
1477     "\C-k" gnus-summary-kill-same-subject
1478     "\M-\C-k" gnus-summary-kill-thread
1479     "\M-\C-l" gnus-summary-lower-thread
1480     "e" gnus-summary-edit-article
1481     "#" gnus-summary-mark-as-processable
1482     "\M-#" gnus-summary-unmark-as-processable
1483     "\M-\C-t" gnus-summary-toggle-threads
1484     "\M-\C-s" gnus-summary-show-thread
1485     "\M-\C-h" gnus-summary-hide-thread
1486     "\M-\C-f" gnus-summary-next-thread
1487     "\M-\C-b" gnus-summary-prev-thread
1488     [(meta down)] gnus-summary-next-thread
1489     [(meta up)] gnus-summary-prev-thread
1490     "\M-\C-u" gnus-summary-up-thread
1491     "\M-\C-d" gnus-summary-down-thread
1492     "&" gnus-summary-execute-command
1493     "c" gnus-summary-catchup-and-exit
1494     "\C-w" gnus-summary-mark-region-as-read
1495     "\C-t" gnus-summary-toggle-truncation
1496     "?" gnus-summary-mark-as-dormant
1497     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1498     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1499     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1500     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1501     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1502     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1503     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1504     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1505     "\C-c\C-s\C-o" gnus-summary-sort-by-original
1506     "=" gnus-summary-expand-window
1507     "\C-x\C-s" gnus-summary-reselect-current-group
1508     "\M-g" gnus-summary-rescan-group
1509     "w" gnus-summary-stop-page-breaking
1510     "\C-c\C-r" gnus-summary-caesar-message
1511     "f" gnus-summary-followup
1512     "F" gnus-summary-followup-with-original
1513     "C" gnus-summary-cancel-article
1514     "r" gnus-summary-reply
1515     "R" gnus-summary-reply-with-original
1516     "\C-c\C-f" gnus-summary-mail-forward
1517     "o" gnus-summary-save-article
1518     "\C-o" gnus-summary-save-article-mail
1519     "|" gnus-summary-pipe-output
1520     "\M-k" gnus-summary-edit-local-kill
1521     "\M-K" gnus-summary-edit-global-kill
1522     ;; "V" gnus-version
1523     "\C-c\C-d" gnus-summary-describe-group
1524     "q" gnus-summary-exit
1525     "Q" gnus-summary-exit-no-update
1526     "\C-c\C-i" gnus-info-find-node
1527     gnus-mouse-2 gnus-mouse-pick-article
1528     "m" gnus-summary-mail-other-window
1529     "a" gnus-summary-post-news
1530     "x" gnus-summary-limit-to-unread
1531     "s" gnus-summary-isearch-article
1532     "t" gnus-summary-toggle-header
1533     "g" gnus-summary-show-article
1534     "l" gnus-summary-goto-last-article
1535     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1536     "\C-d" gnus-summary-enter-digest-group
1537     "\M-\C-d" gnus-summary-read-document
1538     "\M-\C-e" gnus-summary-edit-parameters
1539     "\M-\C-a" gnus-summary-customize-parameters
1540     "\C-c\C-b" gnus-bug
1541     "*" gnus-cache-enter-article
1542     "\M-*" gnus-cache-remove-article
1543     "\M-&" gnus-summary-universal-argument
1544     "\C-l" gnus-recenter
1545     "I" gnus-summary-increase-score
1546     "L" gnus-summary-lower-score
1547     "\M-i" gnus-symbolic-argument
1548     "h" gnus-summary-select-article-buffer
1549
1550     "b" gnus-article-view-part
1551     "\M-t" gnus-summary-toggle-display-buttonized
1552
1553     "V" gnus-summary-score-map
1554     "X" gnus-uu-extract-map
1555     "S" gnus-summary-send-map)
1556
1557   ;; Sort of orthogonal keymap
1558   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1559     "t" gnus-summary-tick-article-forward
1560     "!" gnus-summary-tick-article-forward
1561     "d" gnus-summary-mark-as-read-forward
1562     "r" gnus-summary-mark-as-read-forward
1563     "c" gnus-summary-clear-mark-forward
1564     " " gnus-summary-clear-mark-forward
1565     "e" gnus-summary-mark-as-expirable
1566     "x" gnus-summary-mark-as-expirable
1567     "?" gnus-summary-mark-as-dormant
1568     "b" gnus-summary-set-bookmark
1569     "B" gnus-summary-remove-bookmark
1570     "#" gnus-summary-mark-as-processable
1571     "\M-#" gnus-summary-unmark-as-processable
1572     "S" gnus-summary-limit-include-expunged
1573     "C" gnus-summary-catchup
1574     "H" gnus-summary-catchup-to-here
1575     "h" gnus-summary-catchup-from-here
1576     "\C-c" gnus-summary-catchup-all
1577     "k" gnus-summary-kill-same-subject-and-select
1578     "K" gnus-summary-kill-same-subject
1579     "P" gnus-uu-mark-map)
1580
1581   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1582     "c" gnus-summary-clear-above
1583     "u" gnus-summary-tick-above
1584     "m" gnus-summary-mark-above
1585     "k" gnus-summary-kill-below)
1586
1587   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1588     "/" gnus-summary-limit-to-subject
1589     "n" gnus-summary-limit-to-articles
1590     "w" gnus-summary-pop-limit
1591     "s" gnus-summary-limit-to-subject
1592     "a" gnus-summary-limit-to-author
1593     "u" gnus-summary-limit-to-unread
1594     "m" gnus-summary-limit-to-marks
1595     "M" gnus-summary-limit-exclude-marks
1596     "v" gnus-summary-limit-to-score
1597     "*" gnus-summary-limit-include-cached
1598     "D" gnus-summary-limit-include-dormant
1599     "T" gnus-summary-limit-include-thread
1600     "d" gnus-summary-limit-exclude-dormant
1601     "t" gnus-summary-limit-to-age
1602     "x" gnus-summary-limit-to-extra
1603     "p" gnus-summary-limit-to-display-predicate
1604     "E" gnus-summary-limit-include-expunged
1605     "c" gnus-summary-limit-exclude-childless-dormant
1606     "C" gnus-summary-limit-mark-excluded-as-read
1607     "o" gnus-summary-insert-old-articles
1608     "N" gnus-summary-insert-new-articles)
1609
1610   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1611     "n" gnus-summary-next-unread-article
1612     "p" gnus-summary-prev-unread-article
1613     "N" gnus-summary-next-article
1614     "P" gnus-summary-prev-article
1615     "\C-n" gnus-summary-next-same-subject
1616     "\C-p" gnus-summary-prev-same-subject
1617     "\M-n" gnus-summary-next-unread-subject
1618     "\M-p" gnus-summary-prev-unread-subject
1619     "f" gnus-summary-first-unread-article
1620     "b" gnus-summary-best-unread-article
1621     "j" gnus-summary-goto-article
1622     "g" gnus-summary-goto-subject
1623     "l" gnus-summary-goto-last-article
1624     "o" gnus-summary-pop-article)
1625
1626   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1627     "k" gnus-summary-kill-thread
1628     "l" gnus-summary-lower-thread
1629     "i" gnus-summary-raise-thread
1630     "T" gnus-summary-toggle-threads
1631     "t" gnus-summary-rethread-current
1632     "^" gnus-summary-reparent-thread
1633     "s" gnus-summary-show-thread
1634     "S" gnus-summary-show-all-threads
1635     "h" gnus-summary-hide-thread
1636     "H" gnus-summary-hide-all-threads
1637     "n" gnus-summary-next-thread
1638     "p" gnus-summary-prev-thread
1639     "u" gnus-summary-up-thread
1640     "o" gnus-summary-top-thread
1641     "d" gnus-summary-down-thread
1642     "#" gnus-uu-mark-thread
1643     "\M-#" gnus-uu-unmark-thread)
1644
1645   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1646     "g" gnus-summary-prepare
1647     "c" gnus-summary-insert-cached-articles)
1648
1649   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1650     "c" gnus-summary-catchup-and-exit
1651     "C" gnus-summary-catchup-all-and-exit
1652     "E" gnus-summary-exit-no-update
1653     "Q" gnus-summary-exit
1654     "Z" gnus-summary-exit
1655     "n" gnus-summary-catchup-and-goto-next-group
1656     "R" gnus-summary-reselect-current-group
1657     "G" gnus-summary-rescan-group
1658     "N" gnus-summary-next-group
1659     "s" gnus-summary-save-newsrc
1660     "P" gnus-summary-prev-group)
1661
1662   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1663     " " gnus-summary-next-page
1664     "n" gnus-summary-next-page
1665     "\177" gnus-summary-prev-page
1666     [delete] gnus-summary-prev-page
1667     "p" gnus-summary-prev-page
1668     "\r" gnus-summary-scroll-up
1669     "\M-\r" gnus-summary-scroll-down
1670     "<" gnus-summary-beginning-of-article
1671     ">" gnus-summary-end-of-article
1672     "b" gnus-summary-beginning-of-article
1673     "e" gnus-summary-end-of-article
1674     "^" gnus-summary-refer-parent-article
1675     "r" gnus-summary-refer-parent-article
1676     "D" gnus-summary-enter-digest-group
1677     "R" gnus-summary-refer-references
1678     "T" gnus-summary-refer-thread
1679     "g" gnus-summary-show-article
1680     "s" gnus-summary-isearch-article
1681     "P" gnus-summary-print-article
1682     "M" gnus-mailing-list-insinuate
1683     "t" gnus-article-babel)
1684
1685   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1686     "b" gnus-article-add-buttons
1687     "B" gnus-article-add-buttons-to-head
1688     "o" gnus-article-treat-overstrike
1689     "e" gnus-article-emphasize
1690     "w" gnus-article-fill-cited-article
1691     "Q" gnus-article-fill-long-lines
1692     "C" gnus-article-capitalize-sentences
1693     "c" gnus-article-remove-cr
1694     "q" gnus-article-de-quoted-unreadable
1695     "6" gnus-article-de-base64-unreadable
1696     "Z" gnus-article-decode-HZ
1697     "h" gnus-article-wash-html
1698     "s" gnus-summary-force-verify-and-decrypt
1699     "f" gnus-article-display-x-face
1700     "l" gnus-summary-stop-page-breaking
1701     "r" gnus-summary-caesar-message
1702     "t" gnus-summary-toggle-header
1703     "v" gnus-summary-verbose-headers
1704     "a" gnus-article-strip-headers-in-body ;; mnemonic: wash archive
1705     "p" gnus-article-verify-x-pgp-sig
1706     "d" gnus-article-treat-dumbquotes)
1707
1708   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1709     "a" gnus-article-hide
1710     "h" gnus-article-hide-headers
1711     "b" gnus-article-hide-boring-headers
1712     "s" gnus-article-hide-signature
1713     "c" gnus-article-hide-citation
1714     "C" gnus-article-hide-citation-in-followups
1715     "l" gnus-article-hide-list-identifiers
1716     "p" gnus-article-hide-pgp
1717     "B" gnus-article-strip-banner
1718     "P" gnus-article-hide-pem
1719     "\C-c" gnus-article-hide-citation-maybe)
1720
1721   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1722     "a" gnus-article-highlight
1723     "h" gnus-article-highlight-headers
1724     "c" gnus-article-highlight-citation
1725     "s" gnus-article-highlight-signature)
1726
1727   (gnus-define-keys (gnus-summary-wash-mime-map "M" gnus-summary-wash-map)
1728     "w" gnus-article-decode-mime-words
1729     "c" gnus-article-decode-charset
1730     "v" gnus-mime-view-all-parts
1731     "b" gnus-article-view-part)
1732
1733   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1734     "z" gnus-article-date-ut
1735     "u" gnus-article-date-ut
1736     "l" gnus-article-date-local
1737     "p" gnus-article-date-english
1738     "e" gnus-article-date-lapsed
1739     "o" gnus-article-date-original
1740     "i" gnus-article-date-iso8601
1741     "s" gnus-article-date-user)
1742
1743   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1744     "t" gnus-article-remove-trailing-blank-lines
1745     "l" gnus-article-strip-leading-blank-lines
1746     "m" gnus-article-strip-multiple-blank-lines
1747     "a" gnus-article-strip-blank-lines
1748     "A" gnus-article-strip-all-blank-lines
1749     "s" gnus-article-strip-leading-space
1750     "e" gnus-article-strip-trailing-space
1751     "w" gnus-article-remove-leading-whitespace)
1752
1753   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1754     "v" gnus-version
1755     "f" gnus-summary-fetch-faq
1756     "d" gnus-summary-describe-group
1757     "h" gnus-summary-describe-briefly
1758     "i" gnus-info-find-node)
1759
1760   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1761     "e" gnus-summary-expire-articles
1762     "\M-\C-e" gnus-summary-expire-articles-now
1763     "\177" gnus-summary-delete-article
1764     [delete] gnus-summary-delete-article
1765     [backspace] gnus-summary-delete-article
1766     "m" gnus-summary-move-article
1767     "r" gnus-summary-respool-article
1768     "w" gnus-summary-edit-article
1769     "c" gnus-summary-copy-article
1770     "B" gnus-summary-crosspost-article
1771     "q" gnus-summary-respool-query
1772     "t" gnus-summary-respool-trace
1773     "i" gnus-summary-import-article
1774     "I" gnus-summary-create-article
1775     "p" gnus-summary-article-posted-p)
1776
1777   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1778     "o" gnus-summary-save-article
1779     "m" gnus-summary-save-article-mail
1780     "F" gnus-summary-write-article-file
1781     "r" gnus-summary-save-article-rmail
1782     "f" gnus-summary-save-article-file
1783     "b" gnus-summary-save-article-body-file
1784     "h" gnus-summary-save-article-folder
1785     "v" gnus-summary-save-article-vm
1786     "p" gnus-summary-pipe-output
1787     "s" gnus-soup-add-article)
1788
1789   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1790     "b" gnus-summary-display-buttonized
1791     "m" gnus-summary-repair-multipart
1792     "v" gnus-article-view-part
1793     "o" gnus-article-save-part
1794     "c" gnus-article-copy-part
1795     "C" gnus-article-view-part-as-charset
1796     "e" gnus-article-externalize-part
1797     "E" gnus-article-encrypt-body
1798     "i" gnus-article-inline-part
1799     "|" gnus-article-pipe-part))
1800
1801 (defvar gnus-article-post-menu nil)
1802
1803 (defun gnus-summary-make-menu-bar ()
1804   (gnus-turn-off-edit-menu 'summary)
1805
1806   (unless (boundp 'gnus-summary-misc-menu)
1807
1808     (easy-menu-define
1809       gnus-summary-kill-menu gnus-summary-mode-map ""
1810       (cons
1811        "Score"
1812        (nconc
1813         (list
1814          ["Customize" gnus-score-customize t])
1815         (gnus-make-score-map 'increase)
1816         (gnus-make-score-map 'lower)
1817         '(("Mark"
1818            ["Kill below" gnus-summary-kill-below t]
1819            ["Mark above" gnus-summary-mark-above t]
1820            ["Tick above" gnus-summary-tick-above t]
1821            ["Clear above" gnus-summary-clear-above t])
1822           ["Current score" gnus-summary-current-score t]
1823           ["Set score" gnus-summary-set-score t]
1824           ["Switch current score file..." gnus-score-change-score-file t]
1825           ["Set mark below..." gnus-score-set-mark-below t]
1826           ["Set expunge below..." gnus-score-set-expunge-below t]
1827           ["Edit current score file" gnus-score-edit-current-scores t]
1828           ["Edit score file" gnus-score-edit-file t]
1829           ["Trace score" gnus-score-find-trace t]
1830           ["Find words" gnus-score-find-favourite-words t]
1831           ["Rescore buffer" gnus-summary-rescore t]
1832           ["Increase score..." gnus-summary-increase-score t]
1833           ["Lower score..." gnus-summary-lower-score t]))))
1834
1835 ;; Define both the Article menu in the summary buffer and the equivalent
1836     ;; Commands menu in the article buffer here for consistency.
1837     (let ((innards
1838            `(("Hide"
1839               ["All" gnus-article-hide t]
1840               ["Headers" gnus-article-hide-headers t]
1841               ["Signature" gnus-article-hide-signature t]
1842               ["Citation" gnus-article-hide-citation t]
1843               ["List identifiers" gnus-article-hide-list-identifiers t]
1844               ["PGP" gnus-article-hide-pgp t]
1845               ["Banner" gnus-article-strip-banner t]
1846               ["Boring headers" gnus-article-hide-boring-headers t])
1847              ("Highlight"
1848               ["All" gnus-article-highlight t]
1849               ["Headers" gnus-article-highlight-headers t]
1850               ["Signature" gnus-article-highlight-signature t]
1851               ["Citation" gnus-article-highlight-citation t])
1852              ("MIME"
1853               ["Words" gnus-article-decode-mime-words t]
1854               ["Charset" gnus-article-decode-charset t]
1855               ["QP" gnus-article-de-quoted-unreadable t]
1856               ["Base64" gnus-article-de-base64-unreadable t]
1857               ["View all" gnus-mime-view-all-parts t]
1858               ["Verify and Decrypt" gnus-summary-force-verify-and-decrypt t]
1859               ["Encrypt body" gnus-article-encrypt-body t])
1860              ("Date"
1861               ["Local" gnus-article-date-local t]
1862               ["ISO8601" gnus-article-date-iso8601 t]
1863               ["UT" gnus-article-date-ut t]
1864               ["Original" gnus-article-date-original t]
1865               ["Lapsed" gnus-article-date-lapsed t]
1866               ["User-defined" gnus-article-date-user t])
1867              ("Washing"
1868               ("Remove Blanks"
1869                ["Leading" gnus-article-strip-leading-blank-lines t]
1870                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1871                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1872                ["All of the above" gnus-article-strip-blank-lines t]
1873                ["All" gnus-article-strip-all-blank-lines t]
1874                ["Leading space" gnus-article-strip-leading-space t]
1875                ["Trailing space" gnus-article-strip-trailing-space t]
1876                ["Leading space in headers"
1877                 gnus-article-remove-leading-whitespace t])
1878               ["Overstrike" gnus-article-treat-overstrike t]
1879               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1880               ["Emphasis" gnus-article-emphasize t]
1881               ["Word wrap" gnus-article-fill-cited-article t]
1882               ["Fill long lines" gnus-article-fill-long-lines t]
1883               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1884               ["CR" gnus-article-remove-cr t]
1885               ["Show X-Face" gnus-article-display-x-face t]
1886               ["Quoted-Printable" gnus-article-de-quoted-unreadable t]
1887               ["Base64" gnus-article-de-base64-unreadable t]
1888               ["Rot 13" gnus-summary-caesar-message
1889                ,@(if (featurep 'xemacs) '(t)
1890                    '(:help "\"Caesar rotate\" article by 13"))]
1891               ["Unix pipe" gnus-summary-pipe-message t]
1892               ["Add buttons" gnus-article-add-buttons t]
1893               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1894               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1895               ["Verbose header" gnus-summary-verbose-headers t]
1896               ["Toggle header" gnus-summary-toggle-header t]
1897               ["Html" gnus-article-wash-html t]
1898               ["Verify X-PGP-Sig" gnus-article-verify-x-pgp-sig t]
1899               ["HZ" gnus-article-decode-HZ t])
1900              ("Output"
1901               ["Save in default format" gnus-summary-save-article
1902                ,@(if (featurep 'xemacs) '(t)
1903                    '(:help "Save article using default method"))]
1904               ["Save in file" gnus-summary-save-article-file
1905                ,@(if (featurep 'xemacs) '(t)
1906                    '(:help "Save article in file"))]
1907               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1908               ["Save in MH folder" gnus-summary-save-article-folder t]
1909               ["Save in VM folder" gnus-summary-save-article-vm t]
1910               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1911               ["Save body in file" gnus-summary-save-article-body-file t]
1912               ["Pipe through a filter" gnus-summary-pipe-output t]
1913               ["Add to SOUP packet" gnus-soup-add-article t]
1914               ["Print" gnus-summary-print-article t])
1915              ("Backend"
1916               ["Respool article..." gnus-summary-respool-article t]
1917               ["Move article..." gnus-summary-move-article
1918                (gnus-check-backend-function
1919                 'request-move-article gnus-newsgroup-name)]
1920               ["Copy article..." gnus-summary-copy-article t]
1921               ["Crosspost article..." gnus-summary-crosspost-article
1922                (gnus-check-backend-function
1923                 'request-replace-article gnus-newsgroup-name)]
1924               ["Import file..." gnus-summary-import-article t]
1925               ["Create article..." gnus-summary-create-article t]
1926               ["Check if posted" gnus-summary-article-posted-p t]
1927               ["Edit article" gnus-summary-edit-article
1928                (not (gnus-group-read-only-p))]
1929               ["Delete article" gnus-summary-delete-article
1930                (gnus-check-backend-function
1931                 'request-expire-articles gnus-newsgroup-name)]
1932               ["Query respool" gnus-summary-respool-query t]
1933               ["Trace respool" gnus-summary-respool-trace t]
1934               ["Delete expirable articles" gnus-summary-expire-articles-now
1935                (gnus-check-backend-function
1936                 'request-expire-articles gnus-newsgroup-name)])
1937              ("Extract"
1938               ["Uudecode" gnus-uu-decode-uu
1939                ,@(if (featurep 'xemacs) '(t)
1940                    '(:help "Decode uuencoded article(s)"))]
1941               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1942               ["Unshar" gnus-uu-decode-unshar t]
1943               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1944               ["Save" gnus-uu-decode-save t]
1945               ["Binhex" gnus-uu-decode-binhex t]
1946               ["Postscript" gnus-uu-decode-postscript t])
1947              ("Cache"
1948               ["Enter article" gnus-cache-enter-article t]
1949               ["Remove article" gnus-cache-remove-article t])
1950              ["Translate" gnus-article-babel t]
1951              ["Select article buffer" gnus-summary-select-article-buffer t]
1952              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1953              ["Isearch article..." gnus-summary-isearch-article t]
1954              ["Beginning of the article" gnus-summary-beginning-of-article t]
1955              ["End of the article" gnus-summary-end-of-article t]
1956              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1957              ["Fetch referenced articles" gnus-summary-refer-references t]
1958              ["Fetch current thread" gnus-summary-refer-thread t]
1959              ["Fetch article with id..." gnus-summary-refer-article t]
1960              ["Setup Mailing List Params" gnus-mailing-list-insinuate t]
1961              ["Redisplay" gnus-summary-show-article t]
1962              ["Raw article" gnus-summary-show-raw-article t])))
1963       (easy-menu-define
1964         gnus-summary-article-menu gnus-summary-mode-map ""
1965         (cons "Article" innards))
1966
1967       (if (not (keymapp gnus-summary-article-menu))
1968           (easy-menu-define
1969             gnus-article-commands-menu gnus-article-mode-map ""
1970             (cons "Commands" innards))
1971         ;; in Emacs, don't share menu.
1972         (setq gnus-article-commands-menu
1973               (copy-keymap gnus-summary-article-menu))
1974         (define-key gnus-article-mode-map [menu-bar commands]
1975           (cons "Commands" gnus-article-commands-menu))))
1976
1977     (easy-menu-define
1978       gnus-summary-thread-menu gnus-summary-mode-map ""
1979       '("Threads"
1980         ["Toggle threading" gnus-summary-toggle-threads t]
1981         ["Hide threads" gnus-summary-hide-all-threads t]
1982         ["Show threads" gnus-summary-show-all-threads t]
1983         ["Hide thread" gnus-summary-hide-thread t]
1984         ["Show thread" gnus-summary-show-thread t]
1985         ["Go to next thread" gnus-summary-next-thread t]
1986         ["Go to previous thread" gnus-summary-prev-thread t]
1987         ["Go down thread" gnus-summary-down-thread t]
1988         ["Go up thread" gnus-summary-up-thread t]
1989         ["Top of thread" gnus-summary-top-thread t]
1990         ["Mark thread as read" gnus-summary-kill-thread t]
1991         ["Lower thread score" gnus-summary-lower-thread t]
1992         ["Raise thread score" gnus-summary-raise-thread t]
1993         ["Rethread current" gnus-summary-rethread-current t]))
1994
1995     (easy-menu-define
1996       gnus-summary-post-menu gnus-summary-mode-map ""
1997       `("Post"
1998         ["Post an article" gnus-summary-post-news
1999          ,@(if (featurep 'xemacs) '(t)
2000              '(:help "Post an article"))]
2001         ["Followup" gnus-summary-followup
2002          ,@(if (featurep 'xemacs) '(t)
2003              '(:help "Post followup to this article"))]
2004         ["Followup and yank" gnus-summary-followup-with-original
2005          ,@(if (featurep 'xemacs) '(t)
2006              '(:help "Post followup to this article, quoting its contents"))]
2007         ["Supersede article" gnus-summary-supersede-article t]
2008         ["Cancel article" gnus-summary-cancel-article
2009          ,@(if (featurep 'xemacs) '(t)
2010              '(:help "Cancel an article you posted"))]
2011         ["Reply" gnus-summary-reply t]
2012         ["Reply and yank" gnus-summary-reply-with-original t]
2013         ["Wide reply" gnus-summary-wide-reply t]
2014         ["Wide reply and yank" gnus-summary-wide-reply-with-original
2015          ,@(if (featurep 'xemacs) '(t)
2016              '(:help "Mail a reply, quoting this article"))]
2017         ["Mail forward" gnus-summary-mail-forward t]
2018         ["Post forward" gnus-summary-post-forward t]
2019         ["Digest and mail" gnus-uu-digest-mail-forward t]
2020         ["Digest and post" gnus-uu-digest-post-forward t]
2021         ["Resend message" gnus-summary-resend-message t]
2022         ["Send bounced mail" gnus-summary-resend-bounced-mail t]
2023         ["Send a mail" gnus-summary-mail-other-window t]
2024         ["Uuencode and post" gnus-uu-post-news
2025          ,@(if (featurep 'xemacs) '(t)
2026              '(:help "Post a uuencoded article"))]
2027         ["Followup via news" gnus-summary-followup-to-mail t]
2028         ["Followup via news and yank"
2029          gnus-summary-followup-to-mail-with-original t]
2030         ;;("Draft"
2031         ;;["Send" gnus-summary-send-draft t]
2032         ;;["Send bounced" gnus-resend-bounced-mail t])
2033         ))
2034
2035     (cond
2036      ((not (keymapp gnus-summary-post-menu))
2037       (setq gnus-article-post-menu gnus-summary-post-menu))
2038      ((not gnus-article-post-menu)
2039       ;; Don't share post menu.
2040       (setq gnus-article-post-menu
2041             (copy-keymap gnus-summary-post-menu))))
2042     (define-key gnus-article-mode-map [menu-bar post]
2043       (cons "Post" gnus-article-post-menu))
2044
2045     (easy-menu-define
2046       gnus-summary-misc-menu gnus-summary-mode-map ""
2047       `("Gnus"
2048         ("Mark Read"
2049          ["Mark as read" gnus-summary-mark-as-read-forward t]
2050          ["Mark same subject and select"
2051           gnus-summary-kill-same-subject-and-select t]
2052          ["Mark same subject" gnus-summary-kill-same-subject t]
2053          ["Catchup" gnus-summary-catchup
2054           ,@(if (featurep 'xemacs) '(t)
2055               '(:help "Mark unread articles in this group as read"))]
2056          ["Catchup all" gnus-summary-catchup-all t]
2057          ["Catchup to here" gnus-summary-catchup-to-here t]
2058          ["Catchup from here" gnus-summary-catchup-from-here t]
2059          ["Catchup region" gnus-summary-mark-region-as-read t]
2060          ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
2061         ("Mark Various"
2062          ["Tick" gnus-summary-tick-article-forward t]
2063          ["Mark as dormant" gnus-summary-mark-as-dormant t]
2064          ["Remove marks" gnus-summary-clear-mark-forward t]
2065          ["Set expirable mark" gnus-summary-mark-as-expirable t]
2066          ["Set bookmark" gnus-summary-set-bookmark t]
2067          ["Remove bookmark" gnus-summary-remove-bookmark t])
2068         ("Mark Limit"
2069          ["Marks..." gnus-summary-limit-to-marks t]
2070          ["Subject..." gnus-summary-limit-to-subject t]
2071          ["Author..." gnus-summary-limit-to-author t]
2072          ["Age..." gnus-summary-limit-to-age t]
2073          ["Extra..." gnus-summary-limit-to-extra t]
2074          ["Score" gnus-summary-limit-to-score t]
2075          ["Score" gnus-summary-limit-to-display-predicate t]
2076          ["Unread" gnus-summary-limit-to-unread t]
2077          ["Non-dormant" gnus-summary-limit-exclude-dormant t]
2078          ["Articles" gnus-summary-limit-to-articles t]
2079          ["Pop limit" gnus-summary-pop-limit t]
2080          ["Show dormant" gnus-summary-limit-include-dormant t]
2081          ["Hide childless dormant"
2082           gnus-summary-limit-exclude-childless-dormant t]
2083          ;;["Hide thread" gnus-summary-limit-exclude-thread t]
2084          ["Hide marked" gnus-summary-limit-exclude-marks t]
2085          ["Show expunged" gnus-summary-limit-include-expunged t])
2086         ("Process Mark"
2087          ["Set mark" gnus-summary-mark-as-processable t]
2088          ["Remove mark" gnus-summary-unmark-as-processable t]
2089          ["Remove all marks" gnus-summary-unmark-all-processable t]
2090          ["Mark above" gnus-uu-mark-over t]
2091          ["Mark series" gnus-uu-mark-series t]
2092          ["Mark region" gnus-uu-mark-region t]
2093          ["Unmark region" gnus-uu-unmark-region t]
2094          ["Mark by regexp..." gnus-uu-mark-by-regexp t]
2095          ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
2096          ["Mark all" gnus-uu-mark-all t]
2097          ["Mark buffer" gnus-uu-mark-buffer t]
2098          ["Mark sparse" gnus-uu-mark-sparse t]
2099          ["Mark thread" gnus-uu-mark-thread t]
2100          ["Unmark thread" gnus-uu-unmark-thread t]
2101          ("Process Mark Sets"
2102           ["Kill" gnus-summary-kill-process-mark t]
2103           ["Yank" gnus-summary-yank-process-mark
2104            gnus-newsgroup-process-stack]
2105           ["Save" gnus-summary-save-process-mark t]))
2106         ("Scroll article"
2107          ["Page forward" gnus-summary-next-page
2108           ,@(if (featurep 'xemacs) '(t)
2109               '(:help "Show next page of article"))]
2110          ["Page backward" gnus-summary-prev-page
2111           ,@(if (featurep 'xemacs) '(t)
2112               '(:help "Show previous page of article"))]
2113          ["Line forward" gnus-summary-scroll-up t])
2114         ("Move"
2115          ["Next unread article" gnus-summary-next-unread-article t]
2116          ["Previous unread article" gnus-summary-prev-unread-article t]
2117          ["Next article" gnus-summary-next-article t]
2118          ["Previous article" gnus-summary-prev-article t]
2119          ["Next unread subject" gnus-summary-next-unread-subject t]
2120          ["Previous unread subject" gnus-summary-prev-unread-subject t]
2121          ["Next article same subject" gnus-summary-next-same-subject t]
2122          ["Previous article same subject" gnus-summary-prev-same-subject t]
2123          ["First unread article" gnus-summary-first-unread-article t]
2124          ["Best unread article" gnus-summary-best-unread-article t]
2125          ["Go to subject number..." gnus-summary-goto-subject t]
2126          ["Go to article number..." gnus-summary-goto-article t]
2127          ["Go to the last article" gnus-summary-goto-last-article t]
2128          ["Pop article off history" gnus-summary-pop-article t])
2129         ("Sort"
2130          ["Sort by number" gnus-summary-sort-by-number t]
2131          ["Sort by author" gnus-summary-sort-by-author t]
2132          ["Sort by subject" gnus-summary-sort-by-subject t]
2133          ["Sort by date" gnus-summary-sort-by-date t]
2134          ["Sort by score" gnus-summary-sort-by-score t]
2135          ["Sort by lines" gnus-summary-sort-by-lines t]
2136          ["Sort by characters" gnus-summary-sort-by-chars t]
2137          ["Original sort" gnus-summary-sort-by-original t])
2138         ("Help"
2139          ["Fetch group FAQ" gnus-summary-fetch-faq t]
2140          ["Describe group" gnus-summary-describe-group t]
2141          ["Read manual" gnus-info-find-node t])
2142         ("Modes"
2143          ["Pick and read" gnus-pick-mode t]
2144          ["Binary" gnus-binary-mode t])
2145         ("Regeneration"
2146          ["Regenerate" gnus-summary-prepare t]
2147          ["Insert cached articles" gnus-summary-insert-cached-articles t]
2148          ["Toggle threading" gnus-summary-toggle-threads t])
2149         ["See old articles" gnus-summary-insert-old-articles t]
2150         ["See new articles" gnus-summary-insert-new-articles t]
2151         ["Filter articles..." gnus-summary-execute-command t]
2152         ["Run command on subjects..." gnus-summary-universal-argument t]
2153         ["Search articles forward..." gnus-summary-search-article-forward t]
2154         ["Search articles backward..." gnus-summary-search-article-backward t]
2155         ["Toggle line truncation" gnus-summary-toggle-truncation t]
2156         ["Expand window" gnus-summary-expand-window t]
2157         ["Expire expirable articles" gnus-summary-expire-articles
2158          (gnus-check-backend-function
2159           'request-expire-articles gnus-newsgroup-name)]
2160         ["Edit local kill file" gnus-summary-edit-local-kill t]
2161         ["Edit main kill file" gnus-summary-edit-global-kill t]
2162         ["Edit group parameters" gnus-summary-edit-parameters t]
2163         ["Customize group parameters" gnus-summary-customize-parameters t]
2164         ["Send a bug report" gnus-bug t]
2165         ("Exit"
2166          ["Catchup and exit" gnus-summary-catchup-and-exit
2167           ,@(if (featurep 'xemacs) '(t)
2168               '(:help "Mark unread articles in this group as read, then exit"))]
2169          ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
2170          ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
2171          ["Exit group" gnus-summary-exit
2172           ,@(if (featurep 'xemacs) '(t)
2173               '(:help "Exit current group, return to group selection mode"))]
2174          ["Exit group without updating" gnus-summary-exit-no-update t]
2175          ["Exit and goto next group" gnus-summary-next-group t]
2176          ["Exit and goto prev group" gnus-summary-prev-group t]
2177          ["Reselect group" gnus-summary-reselect-current-group t]
2178          ["Rescan group" gnus-summary-rescan-group t]
2179          ["Update dribble" gnus-summary-save-newsrc t])))
2180
2181     (gnus-run-hooks 'gnus-summary-menu-hook)))
2182
2183 (defvar gnus-summary-tool-bar-map nil)
2184
2185 ;; Emacs 21 tool bar.  Should be no-op otherwise.
2186 (defun gnus-summary-make-tool-bar ()
2187   (if (and (fboundp 'tool-bar-add-item-from-menu)
2188            (default-value 'tool-bar-mode)
2189            (not gnus-summary-tool-bar-map))
2190       (setq gnus-summary-tool-bar-map
2191             (let ((tool-bar-map (make-sparse-keymap))
2192                   (load-path (mm-image-load-path)))
2193               (tool-bar-add-item-from-menu
2194                'gnus-summary-prev-unread "prev-ur" gnus-summary-mode-map)
2195               (tool-bar-add-item-from-menu
2196                'gnus-summary-next-unread "next-ur" gnus-summary-mode-map)
2197               (tool-bar-add-item-from-menu
2198                'gnus-summary-post-news "post" gnus-summary-mode-map)
2199               (tool-bar-add-item-from-menu
2200                'gnus-summary-followup-with-original "fuwo" gnus-summary-mode-map)
2201               (tool-bar-add-item-from-menu
2202                'gnus-summary-followup "followup" gnus-summary-mode-map)
2203               (tool-bar-add-item-from-menu
2204                'gnus-summary-reply-with-original "reply-wo" gnus-summary-mode-map)
2205               (tool-bar-add-item-from-menu
2206                'gnus-summary-reply "reply" gnus-summary-mode-map)
2207               (tool-bar-add-item-from-menu
2208                'gnus-summary-caesar-message "rot13" gnus-summary-mode-map)
2209               (tool-bar-add-item-from-menu
2210                'gnus-uu-decode-uu "uu-decode" gnus-summary-mode-map)
2211               (tool-bar-add-item-from-menu
2212                'gnus-summary-save-article-file "save-aif" gnus-summary-mode-map)
2213               (tool-bar-add-item-from-menu
2214                'gnus-summary-save-article "save-art" gnus-summary-mode-map)
2215               (tool-bar-add-item-from-menu
2216                'gnus-uu-post-news "uu-post" gnus-summary-mode-map)
2217               (tool-bar-add-item-from-menu
2218                'gnus-summary-catchup "catchup" gnus-summary-mode-map)
2219               (tool-bar-add-item-from-menu
2220                'gnus-summary-catchup-and-exit "cu-exit" gnus-summary-mode-map)
2221               (tool-bar-add-item-from-menu
2222                'gnus-summary-exit "exit-summ" gnus-summary-mode-map)
2223               tool-bar-map)))
2224   (if gnus-summary-tool-bar-map
2225       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
2226
2227 (defun gnus-score-set-default (var value)
2228   "A version of set that updates the GNU Emacs menu-bar."
2229   (set var value)
2230   ;; It is the message that forces the active status to be updated.
2231   (message ""))
2232
2233 (defun gnus-make-score-map (type)
2234   "Make a summary score map of type TYPE."
2235   (if t
2236       nil
2237     (let ((headers '(("author" "from" string)
2238                      ("subject" "subject" string)
2239                      ("article body" "body" string)
2240                      ("article head" "head" string)
2241                      ("xref" "xref" string)
2242                      ("extra header" "extra" string)
2243                      ("lines" "lines" number)
2244                      ("followups to author" "followup" string)))
2245           (types '((number ("less than" <)
2246                            ("greater than" >)
2247                            ("equal" =))
2248                    (string ("substring" s)
2249                            ("exact string" e)
2250                            ("fuzzy string" f)
2251                            ("regexp" r))))
2252           (perms '(("temporary" (current-time-string))
2253                    ("permanent" nil)
2254                    ("immediate" now)))
2255           header)
2256       (list
2257        (apply
2258         'nconc
2259         (list
2260          (if (eq type 'lower)
2261              "Lower score"
2262            "Increase score"))
2263         (let (outh)
2264           (while headers
2265             (setq header (car headers))
2266             (setq outh
2267                   (cons
2268                    (apply
2269                     'nconc
2270                     (list (car header))
2271                     (let ((ts (cdr (assoc (nth 2 header) types)))
2272                           outt)
2273                       (while ts
2274                         (setq outt
2275                               (cons
2276                                (apply
2277                                 'nconc
2278                                 (list (caar ts))
2279                                 (let ((ps perms)
2280                                       outp)
2281                                   (while ps
2282                                     (setq outp
2283                                           (cons
2284                                            (vector
2285                                             (caar ps)
2286                                             (list
2287                                              'gnus-summary-score-entry
2288                                              (nth 1 header)
2289                                              (if (or (string= (nth 1 header)
2290                                                               "head")
2291                                                      (string= (nth 1 header)
2292                                                               "body"))
2293                                                  ""
2294                                                (list 'gnus-summary-header
2295                                                      (nth 1 header)))
2296                                              (list 'quote (nth 1 (car ts)))
2297                                              (list 'gnus-score-delta-default
2298                                                    nil)
2299                                              (nth 1 (car ps))
2300                                              t)
2301                                             t)
2302                                            outp))
2303                                     (setq ps (cdr ps)))
2304                                   (list (nreverse outp))))
2305                                outt))
2306                         (setq ts (cdr ts)))
2307                       (list (nreverse outt))))
2308                    outh))
2309             (setq headers (cdr headers)))
2310           (list (nreverse outh))))))))
2311
2312 \f
2313
2314 (defun gnus-summary-mode (&optional group)
2315   "Major mode for reading articles.
2316
2317 All normal editing commands are switched off.
2318 \\<gnus-summary-mode-map>
2319 Each line in this buffer represents one article.  To read an
2320 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2321 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2322 respectively.
2323
2324 You can also post articles and send mail from this buffer.  To
2325 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2326 of an article, type `\\[gnus-summary-reply]'.
2327
2328 There are approx. one gazillion commands you can execute in this
2329 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2330
2331 The following commands are available:
2332
2333 \\{gnus-summary-mode-map}"
2334   (interactive)
2335   (kill-all-local-variables)
2336   (when (gnus-visual-p 'summary-menu 'menu)
2337     (gnus-summary-make-menu-bar)
2338     (gnus-summary-make-tool-bar))
2339   (gnus-summary-make-local-variables)
2340   (let ((gnus-summary-local-variables gnus-newsgroup-variables))
2341     (gnus-summary-make-local-variables))
2342   (gnus-make-thread-indent-array)
2343   (gnus-simplify-mode-line)
2344   (setq major-mode 'gnus-summary-mode)
2345   (setq mode-name "Summary")
2346   (make-local-variable 'minor-mode-alist)
2347   (use-local-map gnus-summary-mode-map)
2348   (buffer-disable-undo)
2349   (setq buffer-read-only t)             ;Disable modification
2350   (setq truncate-lines t)
2351   (setq selective-display t)
2352   (setq selective-display-ellipses t)   ;Display `...'
2353   (gnus-summary-set-display-table)
2354   (gnus-set-default-directory)
2355   (setq gnus-newsgroup-name group)
2356   (make-local-variable 'gnus-summary-line-format)
2357   (make-local-variable 'gnus-summary-line-format-spec)
2358   (make-local-variable 'gnus-summary-dummy-line-format)
2359   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2360   (make-local-variable 'gnus-summary-mark-positions)
2361   (make-local-hook 'pre-command-hook)
2362   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2363   (gnus-run-hooks 'gnus-summary-mode-hook)
2364   (turn-on-gnus-mailing-list-mode)
2365   (mm-enable-multibyte-mule4)
2366   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2367   (gnus-update-summary-mark-positions))
2368
2369 (defun gnus-summary-make-local-variables ()
2370   "Make all the local summary buffer variables."
2371   (let (global)
2372     (dolist (local gnus-summary-local-variables)
2373       (if (consp local)
2374           (progn
2375             (if (eq (cdr local) 'global)
2376                 ;; Copy the global value of the variable.
2377                 (setq global (symbol-value (car local)))
2378               ;; Use the value from the list.
2379               (setq global (eval (cdr local))))
2380             (set (make-local-variable (car local)) global))
2381         ;; Simple nil-valued local variable.
2382         (set (make-local-variable local) nil)))))
2383
2384 (defun gnus-summary-clear-local-variables ()
2385   (let ((locals gnus-summary-local-variables))
2386     (while locals
2387       (if (consp (car locals))
2388           (and (vectorp (caar locals))
2389                (set (caar locals) nil))
2390         (and (vectorp (car locals))
2391              (set (car locals) nil)))
2392       (setq locals (cdr locals)))))
2393
2394 ;; Summary data functions.
2395
2396 (defmacro gnus-data-number (data)
2397   `(car ,data))
2398
2399 (defmacro gnus-data-set-number (data number)
2400   `(setcar ,data ,number))
2401
2402 (defmacro gnus-data-mark (data)
2403   `(nth 1 ,data))
2404
2405 (defmacro gnus-data-set-mark (data mark)
2406   `(setcar (nthcdr 1 ,data) ,mark))
2407
2408 (defmacro gnus-data-pos (data)
2409   `(nth 2 ,data))
2410
2411 (defmacro gnus-data-set-pos (data pos)
2412   `(setcar (nthcdr 2 ,data) ,pos))
2413
2414 (defmacro gnus-data-header (data)
2415   `(nth 3 ,data))
2416
2417 (defmacro gnus-data-set-header (data header)
2418   `(setf (nth 3 ,data) ,header))
2419
2420 (defmacro gnus-data-level (data)
2421   `(nth 4 ,data))
2422
2423 (defmacro gnus-data-unread-p (data)
2424   `(= (nth 1 ,data) gnus-unread-mark))
2425
2426 (defmacro gnus-data-read-p (data)
2427   `(/= (nth 1 ,data) gnus-unread-mark))
2428
2429 (defmacro gnus-data-pseudo-p (data)
2430   `(consp (nth 3 ,data)))
2431
2432 (defmacro gnus-data-find (number)
2433   `(assq ,number gnus-newsgroup-data))
2434
2435 (defmacro gnus-data-find-list (number &optional data)
2436   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2437      (memq (assq ,number bdata)
2438            bdata)))
2439
2440 (defmacro gnus-data-make (number mark pos header level)
2441   `(list ,number ,mark ,pos ,header ,level))
2442
2443 (defun gnus-data-enter (after-article number mark pos header level offset)
2444   (let ((data (gnus-data-find-list after-article)))
2445     (unless data
2446       (error "No such article: %d" after-article))
2447     (setcdr data (cons (gnus-data-make number mark pos header level)
2448                        (cdr data)))
2449     (setq gnus-newsgroup-data-reverse nil)
2450     (gnus-data-update-list (cddr data) offset)))
2451
2452 (defun gnus-data-enter-list (after-article list &optional offset)
2453   (when list
2454     (let ((data (and after-article (gnus-data-find-list after-article)))
2455           (ilist list))
2456       (if (not (or data
2457                    after-article))
2458           (let ((odata gnus-newsgroup-data))
2459             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2460             (when offset
2461               (gnus-data-update-list odata offset)))
2462       ;; Find the last element in the list to be spliced into the main
2463         ;; list.
2464         (while (cdr list)
2465           (setq list (cdr list)))
2466         (if (not data)
2467             (progn
2468               (setcdr list gnus-newsgroup-data)
2469               (setq gnus-newsgroup-data ilist)
2470               (when offset
2471                 (gnus-data-update-list (cdr list) offset)))
2472           (setcdr list (cdr data))
2473           (setcdr data ilist)
2474           (when offset
2475             (gnus-data-update-list (cdr list) offset))))
2476       (setq gnus-newsgroup-data-reverse nil))))
2477
2478 (defun gnus-data-remove (article &optional offset)
2479   (let ((data gnus-newsgroup-data))
2480     (if (= (gnus-data-number (car data)) article)
2481         (progn
2482           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2483                 gnus-newsgroup-data-reverse nil)
2484           (when offset
2485             (gnus-data-update-list gnus-newsgroup-data offset)))
2486       (while (cdr data)
2487         (when (= (gnus-data-number (cadr data)) article)
2488           (setcdr data (cddr data))
2489           (when offset
2490             (gnus-data-update-list (cdr data) offset))
2491           (setq data nil
2492                 gnus-newsgroup-data-reverse nil))
2493         (setq data (cdr data))))))
2494
2495 (defmacro gnus-data-list (backward)
2496   `(if ,backward
2497        (or gnus-newsgroup-data-reverse
2498            (setq gnus-newsgroup-data-reverse
2499                  (reverse gnus-newsgroup-data)))
2500      gnus-newsgroup-data))
2501
2502 (defun gnus-data-update-list (data offset)
2503   "Add OFFSET to the POS of all data entries in DATA."
2504   (setq gnus-newsgroup-data-reverse nil)
2505   (while data
2506     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2507     (setq data (cdr data))))
2508
2509 (defun gnus-summary-article-pseudo-p (article)
2510   "Say whether this article is a pseudo article or not."
2511   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2512
2513 (defmacro gnus-summary-article-sparse-p (article)
2514   "Say whether this article is a sparse article or not."
2515   `(memq ,article gnus-newsgroup-sparse))
2516
2517 (defmacro gnus-summary-article-ancient-p (article)
2518   "Say whether this article is a sparse article or not."
2519   `(memq ,article gnus-newsgroup-ancient))
2520
2521 (defun gnus-article-parent-p (number)
2522   "Say whether this article is a parent or not."
2523   (let ((data (gnus-data-find-list number)))
2524     (and (cdr data)              ; There has to be an article after...
2525          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2526             (gnus-data-level (nth 1 data))))))
2527
2528 (defun gnus-article-children (number)
2529   "Return a list of all children to NUMBER."
2530   (let* ((data (gnus-data-find-list number))
2531          (level (gnus-data-level (car data)))
2532          children)
2533     (setq data (cdr data))
2534     (while (and data
2535                 (= (gnus-data-level (car data)) (1+ level)))
2536       (push (gnus-data-number (car data)) children)
2537       (setq data (cdr data)))
2538     children))
2539
2540 (defmacro gnus-summary-skip-intangible ()
2541   "If the current article is intangible, then jump to a different article."
2542   '(let ((to (get-text-property (point) 'gnus-intangible)))
2543      (and to (gnus-summary-goto-subject to))))
2544
2545 (defmacro gnus-summary-article-intangible-p ()
2546   "Say whether this article is intangible or not."
2547   '(get-text-property (point) 'gnus-intangible))
2548
2549 (defun gnus-article-read-p (article)
2550   "Say whether ARTICLE is read or not."
2551   (not (or (memq article gnus-newsgroup-marked)
2552            (memq article gnus-newsgroup-unreads)
2553            (memq article gnus-newsgroup-unselected)
2554            (memq article gnus-newsgroup-dormant))))
2555
2556 ;; Some summary mode macros.
2557
2558 (defmacro gnus-summary-article-number ()
2559   "The article number of the article on the current line.
2560 If there isn's an article number here, then we return the current
2561 article number."
2562   '(progn
2563      (gnus-summary-skip-intangible)
2564      (or (get-text-property (point) 'gnus-number)
2565          (gnus-summary-last-subject))))
2566
2567 (defmacro gnus-summary-article-header (&optional number)
2568   "Return the header of article NUMBER."
2569   `(gnus-data-header (gnus-data-find
2570                       ,(or number '(gnus-summary-article-number)))))
2571
2572 (defmacro gnus-summary-thread-level (&optional number)
2573   "Return the level of thread that starts with article NUMBER."
2574   `(if (and (eq gnus-summary-make-false-root 'dummy)
2575             (get-text-property (point) 'gnus-intangible))
2576        0
2577      (gnus-data-level (gnus-data-find
2578                        ,(or number '(gnus-summary-article-number))))))
2579
2580 (defmacro gnus-summary-article-mark (&optional number)
2581   "Return the mark of article NUMBER."
2582   `(gnus-data-mark (gnus-data-find
2583                     ,(or number '(gnus-summary-article-number)))))
2584
2585 (defmacro gnus-summary-article-pos (&optional number)
2586   "Return the position of the line of article NUMBER."
2587   `(gnus-data-pos (gnus-data-find
2588                    ,(or number '(gnus-summary-article-number)))))
2589
2590 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2591 (defmacro gnus-summary-article-subject (&optional number)
2592   "Return current subject string or nil if nothing."
2593   `(let ((headers
2594           ,(if number
2595                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2596              '(gnus-data-header (assq (gnus-summary-article-number)
2597                                       gnus-newsgroup-data)))))
2598      (and headers
2599           (vectorp headers)
2600           (mail-header-subject headers))))
2601
2602 (defmacro gnus-summary-article-score (&optional number)
2603   "Return current article score."
2604   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2605                   gnus-newsgroup-scored))
2606        gnus-summary-default-score 0))
2607
2608 (defun gnus-summary-article-children (&optional number)
2609   "Return a list of article numbers that are children of article NUMBER."
2610   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2611          (level (gnus-data-level (car data)))
2612          l children)
2613     (while (and (setq data (cdr data))
2614                 (> (setq l (gnus-data-level (car data))) level))
2615       (and (= (1+ level) l)
2616            (push (gnus-data-number (car data))
2617                  children)))
2618     (nreverse children)))
2619
2620 (defun gnus-summary-article-parent (&optional number)
2621   "Return the article number of the parent of article NUMBER."
2622   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2623                                     (gnus-data-list t)))
2624          (level (gnus-data-level (car data))))
2625     (if (zerop level)
2626         ()                              ; This is a root.
2627       ;; We search until we find an article with a level less than
2628       ;; this one.  That function has to be the parent.
2629       (while (and (setq data (cdr data))
2630                   (not (< (gnus-data-level (car data)) level))))
2631       (and data (gnus-data-number (car data))))))
2632
2633 (defun gnus-unread-mark-p (mark)
2634   "Say whether MARK is the unread mark."
2635   (= mark gnus-unread-mark))
2636
2637 (defun gnus-read-mark-p (mark)
2638   "Say whether MARK is one of the marks that mark as read.
2639 This is all marks except unread, ticked, dormant, and expirable."
2640   (not (or (= mark gnus-unread-mark)
2641            (= mark gnus-ticked-mark)
2642            (= mark gnus-dormant-mark)
2643            (= mark gnus-expirable-mark))))
2644
2645 (defmacro gnus-article-mark (number)
2646   "Return the MARK of article NUMBER.
2647 This macro should only be used when computing the mark the \"first\"
2648 time; i.e., when generating the summary lines.  After that,
2649 `gnus-summary-article-mark' should be used to examine the
2650 marks of articles."
2651   `(cond
2652     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2653     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2654     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2655     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2656     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2657     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2658     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2659     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2660            gnus-ancient-mark))))
2661
2662 ;; Saving hidden threads.
2663
2664 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2665 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2666
2667 (defmacro gnus-save-hidden-threads (&rest forms)
2668   "Save hidden threads, eval FORMS, and restore the hidden threads."
2669   (let ((config (make-symbol "config")))
2670     `(let ((,config (gnus-hidden-threads-configuration)))
2671        (unwind-protect
2672            (save-excursion
2673              ,@forms)
2674          (gnus-restore-hidden-threads-configuration ,config)))))
2675
2676 (defun gnus-data-compute-positions ()
2677   "Compute the positions of all articles."
2678   (setq gnus-newsgroup-data-reverse nil)
2679   (let ((data gnus-newsgroup-data))
2680     (save-excursion
2681       (gnus-save-hidden-threads
2682         (gnus-summary-show-all-threads)
2683         (goto-char (point-min))
2684         (while data
2685           (while (get-text-property (point) 'gnus-intangible)
2686             (forward-line 1))
2687           (gnus-data-set-pos (car data) (+ (point) 3))
2688           (setq data (cdr data))
2689           (forward-line 1))))))
2690
2691 (defun gnus-hidden-threads-configuration ()
2692   "Return the current hidden threads configuration."
2693   (save-excursion
2694     (let (config)
2695       (goto-char (point-min))
2696       (while (search-forward "\r" nil t)
2697         (push (1- (point)) config))
2698       config)))
2699
2700 (defun gnus-restore-hidden-threads-configuration (config)
2701   "Restore hidden threads configuration from CONFIG."
2702   (save-excursion
2703     (let (point buffer-read-only)
2704       (while (setq point (pop config))
2705         (when (and (< point (point-max))
2706                    (goto-char point)
2707                    (eq (char-after) ?\n))
2708           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2709
2710 ;; Various summary mode internalish functions.
2711
2712 (defun gnus-mouse-pick-article (e)
2713   (interactive "e")
2714   (mouse-set-point e)
2715   (gnus-summary-next-page nil t))
2716
2717 (defun gnus-summary-set-display-table ()
2718   "Change the display table.
2719 Odd characters have a tendency to mess
2720 up nicely formatted displays - we make all possible glyphs
2721 display only a single character."
2722
2723   ;; We start from the standard display table, if any.
2724   (let ((table (or (copy-sequence standard-display-table)
2725                    (make-display-table)))
2726         (i 32))
2727     ;; Nix out all the control chars...
2728     (while (>= (setq i (1- i)) 0)
2729       (aset table i [??]))
2730    ;; ... but not newline and cr, of course.  (cr is necessary for the
2731     ;; selective display).
2732     (aset table ?\n nil)
2733     (aset table ?\r nil)
2734     ;; We keep TAB as well.
2735     (aset table ?\t nil)
2736     ;; We nix out any glyphs over 126 that are not set already.
2737     (let ((i 256))
2738       (while (>= (setq i (1- i)) 127)
2739         ;; Only modify if the entry is nil.
2740         (unless (aref table i)
2741           (aset table i [??]))))
2742     (setq buffer-display-table table)))
2743
2744 (defun gnus-summary-set-article-display-arrow (pos)
2745   "Update the overlay arrow to point to line at position POS."
2746   (when (and gnus-summary-display-arrow
2747              (boundp 'overlay-arrow-position)
2748              (boundp 'overlay-arrow-string))
2749     (save-excursion
2750       (goto-char pos)
2751       (beginning-of-line)
2752       (unless overlay-arrow-position
2753         (setq overlay-arrow-position (make-marker)))
2754       (setq overlay-arrow-string "=>"
2755             overlay-arrow-position (set-marker overlay-arrow-position
2756                                                (point)
2757                                                (current-buffer))))))
2758
2759 (defun gnus-summary-buffer-name (group)
2760   "Return the summary buffer name of GROUP."
2761   (concat "*Summary " group "*"))
2762
2763 (defun gnus-summary-setup-buffer (group)
2764   "Initialize summary buffer."
2765   (let ((buffer (gnus-summary-buffer-name group)))
2766     (if (get-buffer buffer)
2767         (progn
2768           (set-buffer buffer)
2769           (setq gnus-summary-buffer (current-buffer))
2770           (not gnus-newsgroup-prepared))
2771       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2772       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2773       (gnus-summary-mode group)
2774       (when gnus-carpal
2775         (gnus-carpal-setup-buffer 'summary))
2776       (unless gnus-single-article-buffer
2777         (make-local-variable 'gnus-article-buffer)
2778         (make-local-variable 'gnus-article-current)
2779         (make-local-variable 'gnus-original-article-buffer))
2780       (setq gnus-newsgroup-name group)
2781       ;; Set any local variables in the group parameters.
2782       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2783       t)))
2784
2785 (defun gnus-set-global-variables ()
2786   "Set the global equivalents of the buffer-local variables.
2787 They are set to the latest values they had.  These reflect the summary
2788 buffer that was in action when the last article was fetched."
2789   (when (eq major-mode 'gnus-summary-mode)
2790     (setq gnus-summary-buffer (current-buffer))
2791     (let ((name gnus-newsgroup-name)
2792           (marked gnus-newsgroup-marked)
2793           (unread gnus-newsgroup-unreads)
2794           (headers gnus-current-headers)
2795           (data gnus-newsgroup-data)
2796           (summary gnus-summary-buffer)
2797           (article-buffer gnus-article-buffer)
2798           (original gnus-original-article-buffer)
2799           (gac gnus-article-current)
2800           (reffed gnus-reffed-article-number)
2801           (score-file gnus-current-score-file)
2802           (default-charset gnus-newsgroup-charset)
2803           vlist)
2804       (let ((locals gnus-newsgroup-variables))
2805         (while locals
2806           (if (consp (car locals))
2807               (push (eval (caar locals)) vlist)
2808             (push (eval (car locals)) vlist))
2809           (setq locals (cdr locals)))
2810         (setq vlist (nreverse vlist)))
2811       (save-excursion
2812         (set-buffer gnus-group-buffer)
2813         (setq gnus-newsgroup-name name
2814               gnus-newsgroup-marked marked
2815               gnus-newsgroup-unreads unread
2816               gnus-current-headers headers
2817               gnus-newsgroup-data data
2818               gnus-article-current gac
2819               gnus-summary-buffer summary
2820               gnus-article-buffer article-buffer
2821               gnus-original-article-buffer original
2822               gnus-reffed-article-number reffed
2823               gnus-current-score-file score-file
2824               gnus-newsgroup-charset default-charset)
2825         (let ((locals gnus-newsgroup-variables))
2826           (while locals
2827             (if (consp (car locals))
2828                 (set (caar locals) (pop vlist))
2829               (set (car locals) (pop vlist)))
2830             (setq locals (cdr locals))))
2831         ;; The article buffer also has local variables.
2832         (when (gnus-buffer-live-p gnus-article-buffer)
2833           (set-buffer gnus-article-buffer)
2834           (setq gnus-summary-buffer summary))))))
2835
2836 (defun gnus-summary-article-unread-p (article)
2837   "Say whether ARTICLE is unread or not."
2838   (memq article gnus-newsgroup-unreads))
2839
2840 (defun gnus-summary-first-article-p (&optional article)
2841   "Return whether ARTICLE is the first article in the buffer."
2842   (if (not (setq article (or article (gnus-summary-article-number))))
2843       nil
2844     (eq article (caar gnus-newsgroup-data))))
2845
2846 (defun gnus-summary-last-article-p (&optional article)
2847   "Return whether ARTICLE is the last article in the buffer."
2848   (if (not (setq article (or article (gnus-summary-article-number))))
2849       ;; All non-existent numbers are the last article.  :-)
2850       t
2851     (not (cdr (gnus-data-find-list article)))))
2852
2853 (defun gnus-make-thread-indent-array ()
2854   (let ((n 200))
2855     (unless (and gnus-thread-indent-array
2856                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2857       (setq gnus-thread-indent-array (make-vector 201 "")
2858             gnus-thread-indent-array-level gnus-thread-indent-level)
2859       (while (>= n 0)
2860         (aset gnus-thread-indent-array n
2861               (make-string (* n gnus-thread-indent-level) ? ))
2862         (setq n (1- n))))))
2863
2864 (defun gnus-update-summary-mark-positions ()
2865   "Compute where the summary marks are to go."
2866   (save-excursion
2867     (when (gnus-buffer-exists-p gnus-summary-buffer)
2868       (set-buffer gnus-summary-buffer))
2869     (let ((gnus-replied-mark 129)
2870           (gnus-score-below-mark 130)
2871           (gnus-score-over-mark 130)
2872           (gnus-download-mark 131)
2873           (spec gnus-summary-line-format-spec)
2874           gnus-visual pos)
2875       (save-excursion
2876         (gnus-set-work-buffer)
2877         (let ((gnus-summary-line-format-spec spec)
2878               (gnus-newsgroup-downloadable '((0 . t))))
2879           (gnus-summary-insert-line
2880            [0 "" "" "05 Apr 2001 23:33:09 +0400" "" "" 0 0 "" nil]
2881            0 nil 128 t nil "" nil 1)
2882           (goto-char (point-min))
2883           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2884                                              (- (point) 2)))))
2885           (goto-char (point-min))
2886           (push (cons 'replied (and (search-forward "\201" nil t)
2887                                     (- (point) 2)))
2888                 pos)
2889           (goto-char (point-min))
2890           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2891                 pos)
2892           (goto-char (point-min))
2893           (push (cons 'download
2894                       (and (search-forward "\203" nil t) (- (point) 2)))
2895                 pos)))
2896       (setq gnus-summary-mark-positions pos))))
2897
2898 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2899   "Insert a dummy root in the summary buffer."
2900   (beginning-of-line)
2901   (gnus-add-text-properties
2902    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2903    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2904
2905 (defun gnus-summary-from-or-to-or-newsgroups (header)
2906   (let ((to (cdr (assq 'To (mail-header-extra header))))
2907         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2908         (mail-parse-charset gnus-newsgroup-charset)
2909         (mail-parse-ignored-charsets
2910          (save-excursion (set-buffer gnus-summary-buffer)
2911                          gnus-newsgroup-ignored-charsets)))
2912     (cond
2913      ((and to
2914            gnus-ignored-from-addresses
2915            (string-match gnus-ignored-from-addresses
2916                          (mail-header-from header)))
2917       (concat "-> "
2918               (or (car (funcall gnus-extract-address-components
2919                                 (funcall
2920                                  gnus-decode-encoded-word-function to)))
2921                   (funcall gnus-decode-encoded-word-function to))))
2922      ((and newsgroups
2923            gnus-ignored-from-addresses
2924            (string-match gnus-ignored-from-addresses
2925                          (mail-header-from header)))
2926       (concat "=> " newsgroups))
2927      (t
2928       (or (car (funcall gnus-extract-address-components
2929                         (mail-header-from header)))
2930           (mail-header-from header))))))
2931
2932 (defun gnus-summary-insert-line (gnus-tmp-header
2933                                  gnus-tmp-level gnus-tmp-current
2934                                  gnus-tmp-unread gnus-tmp-replied
2935                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2936                                  &optional gnus-tmp-dummy gnus-tmp-score
2937                                  gnus-tmp-process)
2938   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2939          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2940          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2941          (gnus-tmp-score-char
2942           (if (or (null gnus-summary-default-score)
2943                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2944                       gnus-summary-zcore-fuzz))
2945               ?                         ;Whitespace
2946             (if (< gnus-tmp-score gnus-summary-default-score)
2947                 gnus-score-below-mark gnus-score-over-mark)))
2948          (gnus-tmp-replied
2949           (cond (gnus-tmp-process gnus-process-mark)
2950                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2951                  gnus-cached-mark)
2952                 (gnus-tmp-replied gnus-replied-mark)
2953                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2954                  gnus-saved-mark)
2955                 (t gnus-no-mark)))
2956          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2957          (gnus-tmp-name
2958           (cond
2959            ((string-match "<[^>]+> *$" gnus-tmp-from)
2960             (let ((beg (match-beginning 0)))
2961               (or (and (string-match "^\".+\"" gnus-tmp-from)
2962                        (substring gnus-tmp-from 1 (1- (match-end 0))))
2963                   (substring gnus-tmp-from 0 beg))))
2964            ((string-match "(.+)" gnus-tmp-from)
2965             (substring gnus-tmp-from
2966                        (1+ (match-beginning 0)) (1- (match-end 0))))
2967            (t gnus-tmp-from)))
2968          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2969          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2970          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2971          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2972          (buffer-read-only nil))
2973     (when (string= gnus-tmp-name "")
2974       (setq gnus-tmp-name gnus-tmp-from))
2975     (unless (numberp gnus-tmp-lines)
2976       (setq gnus-tmp-lines -1))
2977     (when (= gnus-tmp-lines -1)
2978       (setq gnus-tmp-lines "?"))
2979     (gnus-put-text-property
2980      (point)
2981      (progn (eval gnus-summary-line-format-spec) (point))
2982      'gnus-number gnus-tmp-number)
2983     (when (gnus-visual-p 'summary-highlight 'highlight)
2984       (forward-line -1)
2985       (gnus-run-hooks 'gnus-summary-update-hook)
2986       (forward-line 1))))
2987
2988 (defun gnus-summary-update-line (&optional dont-update)
2989   "Update summary line after change."
2990   (when (and gnus-summary-default-score
2991              (not gnus-summary-inhibit-highlight))
2992     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2993            (article (gnus-summary-article-number))
2994            (score (gnus-summary-article-score article)))
2995       (unless dont-update
2996         (if (and gnus-summary-mark-below
2997                  (< (gnus-summary-article-score)
2998                     gnus-summary-mark-below))
2999             ;; This article has a low score, so we mark it as read.
3000             (when (memq article gnus-newsgroup-unreads)
3001               (gnus-summary-mark-article-as-read gnus-low-score-mark))
3002           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
3003             ;; This article was previously marked as read on account
3004             ;; of a low score, but now it has risen, so we mark it as
3005             ;; unread.
3006             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
3007         (gnus-summary-update-mark
3008          (if (or (null gnus-summary-default-score)
3009                  (<= (abs (- score gnus-summary-default-score))
3010                      gnus-summary-zcore-fuzz))
3011              ?                          ;Whitespace
3012            (if (< score gnus-summary-default-score)
3013                gnus-score-below-mark gnus-score-over-mark))
3014          'score))
3015       ;; Do visual highlighting.
3016       (when (gnus-visual-p 'summary-highlight 'highlight)
3017         (gnus-run-hooks 'gnus-summary-update-hook)))))
3018
3019 (defvar gnus-tmp-new-adopts nil)
3020
3021 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
3022   "Return the number of articles in THREAD.
3023 This may be 0 in some cases -- if none of the articles in
3024 the thread are to be displayed."
3025   (let* ((number
3026          ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
3027           (cond
3028            ((not (listp thread))
3029             1)
3030            ((and (consp thread) (cdr thread))
3031             (apply
3032              '+ 1 (mapcar
3033                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
3034            ((null thread)
3035             1)
3036            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
3037             1)
3038            (t 0))))
3039     (when (and level (zerop level) gnus-tmp-new-adopts)
3040       (incf number
3041             (apply '+ (mapcar
3042                        'gnus-summary-number-of-articles-in-thread
3043                        gnus-tmp-new-adopts))))
3044     (if char
3045         (if (> number 1) gnus-not-empty-thread-mark
3046           gnus-empty-thread-mark)
3047       number)))
3048
3049 (defun gnus-summary-set-local-parameters (group)
3050   "Go through the local params of GROUP and set all variable specs in that list."
3051   (let ((params (gnus-group-find-parameter group))
3052         (vars '(quit-config))           ; Ignore quit-config.
3053         elem)
3054     (while params
3055       (setq elem (car params)
3056             params (cdr params))
3057       (and (consp elem)                 ; Has to be a cons.
3058            (consp (cdr elem))           ; The cdr has to be a list.
3059            (symbolp (car elem))         ; Has to be a symbol in there.
3060            (not (memq (car elem) vars))
3061            (ignore-errors               ; So we set it.
3062              (push (car elem) vars)
3063              (make-local-variable (car elem))
3064              (set (car elem) (eval (nth 1 elem))))))))
3065
3066 (defun gnus-summary-read-group (group &optional show-all no-article
3067                                       kill-buffer no-display backward
3068                                       select-articles)
3069   "Start reading news in newsgroup GROUP.
3070 If SHOW-ALL is non-nil, already read articles are also listed.
3071 If NO-ARTICLE is non-nil, no article is selected initially.
3072 If NO-DISPLAY, don't generate a summary buffer."
3073   (let (result)
3074     (while (and group
3075                 (null (setq result
3076                             (let ((gnus-auto-select-next nil))
3077                               (or (gnus-summary-read-group-1
3078                                    group show-all no-article
3079                                    kill-buffer no-display
3080                                    select-articles)
3081                                   (setq show-all nil
3082                                         select-articles nil)))))
3083                 (eq gnus-auto-select-next 'quietly))
3084       (set-buffer gnus-group-buffer)
3085       ;; The entry function called above goes to the next
3086       ;; group automatically, so we go two groups back
3087       ;; if we are searching for the previous group.
3088       (when backward
3089         (gnus-group-prev-unread-group 2))
3090       (if (not (equal group (gnus-group-group-name)))
3091           (setq group (gnus-group-group-name))
3092         (setq group nil)))
3093     result))
3094
3095 (defun gnus-summary-read-group-1 (group show-all no-article
3096                                         kill-buffer no-display
3097                                         &optional select-articles)
3098   ;; Killed foreign groups can't be entered.
3099   ;;  (when (and (not (gnus-group-native-p group))
3100   ;;         (not (gnus-gethash group gnus-newsrc-hashtb)))
3101   ;;    (error "Dead non-native groups can't be entered"))
3102   (gnus-message 5 "Retrieving newsgroup: %s..."
3103                 (gnus-group-decoded-name group))
3104   (let* ((new-group (gnus-summary-setup-buffer group))
3105          (quit-config (gnus-group-quit-config group))
3106          (did-select (and new-group (gnus-select-newsgroup
3107                                      group show-all select-articles))))
3108     (cond
3109      ;; This summary buffer exists already, so we just select it.
3110      ((not new-group)
3111       (gnus-set-global-variables)
3112       (when kill-buffer
3113         (gnus-kill-or-deaden-summary kill-buffer))
3114       (gnus-configure-windows 'summary 'force)
3115       (gnus-set-mode-line 'summary)
3116       (gnus-summary-position-point)
3117       (message "")
3118       t)
3119      ;; We couldn't select this group.
3120      ((null did-select)
3121       (when (and (eq major-mode 'gnus-summary-mode)
3122                  (not (equal (current-buffer) kill-buffer)))
3123         (kill-buffer (current-buffer))
3124         (if (not quit-config)
3125             (progn
3126               ;; Update the info -- marks might need to be removed,
3127               ;; for instance.
3128               (gnus-summary-update-info)
3129               (set-buffer gnus-group-buffer)
3130               (gnus-group-jump-to-group group)
3131               (gnus-group-next-unread-group 1))
3132           (gnus-handle-ephemeral-exit quit-config)))
3133       (let ((grpinfo (gnus-get-info group)))
3134         (if (null (gnus-info-read grpinfo))
3135             (gnus-message 3 "Group %s contains no messages"
3136                           (gnus-group-decoded-name group))
3137           (gnus-message 3 "Can't select group")))
3138       nil)
3139      ;; The user did a `C-g' while prompting for number of articles,
3140      ;; so we exit this group.
3141      ((eq did-select 'quit)
3142       (and (eq major-mode 'gnus-summary-mode)
3143            (not (equal (current-buffer) kill-buffer))
3144            (kill-buffer (current-buffer)))
3145       (when kill-buffer
3146         (gnus-kill-or-deaden-summary kill-buffer))
3147       (if (not quit-config)
3148           (progn
3149             (set-buffer gnus-group-buffer)
3150             (gnus-group-jump-to-group group)
3151             (gnus-group-next-unread-group 1)
3152             (gnus-configure-windows 'group 'force))
3153         (gnus-handle-ephemeral-exit quit-config))
3154       ;; Finally signal the quit.
3155       (signal 'quit nil))
3156      ;; The group was successfully selected.
3157      (t
3158       (gnus-set-global-variables)
3159       ;; Save the active value in effect when the group was entered.
3160       (setq gnus-newsgroup-active
3161             (gnus-copy-sequence
3162              (gnus-active gnus-newsgroup-name)))
3163       ;; You can change the summary buffer in some way with this hook.
3164       (gnus-run-hooks 'gnus-select-group-hook)
3165       (gnus-update-format-specifications
3166        nil 'summary 'summary-mode 'summary-dummy)
3167       (gnus-update-summary-mark-positions)
3168       ;; Do score processing.
3169       (when gnus-use-scoring
3170         (gnus-possibly-score-headers))
3171       ;; Check whether to fill in the gaps in the threads.
3172       (when gnus-build-sparse-threads
3173         (gnus-build-sparse-threads))
3174       ;; Find the initial limit.
3175       (if gnus-show-threads
3176           (if show-all
3177               (let ((gnus-newsgroup-dormant nil))
3178                 (gnus-summary-initial-limit show-all))
3179             (gnus-summary-initial-limit show-all))
3180         ;; When untreaded, all articles are always shown.
3181         (setq gnus-newsgroup-limit
3182               (mapcar
3183                (lambda (header) (mail-header-number header))
3184                gnus-newsgroup-headers)))
3185       ;; Generate the summary buffer.
3186       (unless no-display
3187         (gnus-summary-prepare))
3188       (when gnus-use-trees
3189         (gnus-tree-open group)
3190         (setq gnus-summary-highlight-line-function
3191               'gnus-tree-highlight-article))
3192       ;; If the summary buffer is empty, but there are some low-scored
3193       ;; articles or some excluded dormants, we include these in the
3194       ;; buffer.
3195       (when (and (zerop (buffer-size))
3196                  (not no-display))
3197         (cond (gnus-newsgroup-dormant
3198                (gnus-summary-limit-include-dormant))
3199               ((and gnus-newsgroup-scored show-all)
3200                (gnus-summary-limit-include-expunged t))))
3201       ;; Function `gnus-apply-kill-file' must be called in this hook.
3202       (gnus-run-hooks 'gnus-apply-kill-hook)
3203       (if (and (zerop (buffer-size))
3204                (not no-display))
3205           (progn
3206             ;; This newsgroup is empty.
3207             (gnus-summary-catchup-and-exit nil t)
3208             (gnus-message 6 "No unread news")
3209             (when kill-buffer
3210               (gnus-kill-or-deaden-summary kill-buffer))
3211             ;; Return nil from this function.
3212             nil)
3213         ;; Hide conversation thread subtrees.  We cannot do this in
3214         ;; gnus-summary-prepare-hook since kill processing may not
3215         ;; work with hidden articles.
3216         (and gnus-show-threads
3217              gnus-thread-hide-subtree
3218              (gnus-summary-hide-all-threads))
3219         (when kill-buffer
3220           (gnus-kill-or-deaden-summary kill-buffer))
3221         ;; Show first unread article if requested.
3222         (if (and (not no-article)
3223                  (not no-display)
3224                  gnus-newsgroup-unreads
3225                  gnus-auto-select-first)
3226             (progn
3227               (gnus-configure-windows 'summary)
3228               (cond
3229                ((eq gnus-auto-select-first 'best)
3230                 (gnus-summary-best-unread-article))
3231                ((eq gnus-auto-select-first t)
3232                 (gnus-summary-first-unread-article))
3233                ((gnus-functionp gnus-auto-select-first)
3234                 (funcall gnus-auto-select-first))))
3235           ;; Don't select any articles, just move point to the first
3236           ;; article in the group.
3237           (goto-char (point-min))
3238           (gnus-summary-position-point)
3239           (gnus-configure-windows 'summary 'force)
3240           (gnus-set-mode-line 'summary))
3241         (when (get-buffer-window gnus-group-buffer t)
3242           ;; Gotta use windows, because recenter does weird stuff if
3243           ;; the current buffer ain't the displayed window.
3244           (let ((owin (selected-window)))
3245             (select-window (get-buffer-window gnus-group-buffer t))
3246             (when (gnus-group-goto-group group)
3247               (recenter))
3248             (select-window owin)))
3249         ;; Mark this buffer as "prepared".
3250         (setq gnus-newsgroup-prepared t)
3251         (gnus-run-hooks 'gnus-summary-prepared-hook)
3252         t)))))
3253
3254 (defun gnus-summary-prepare ()
3255   "Generate the summary buffer."
3256   (interactive)
3257   (let ((buffer-read-only nil))
3258     (erase-buffer)
3259     (setq gnus-newsgroup-data nil
3260           gnus-newsgroup-data-reverse nil)
3261     (gnus-run-hooks 'gnus-summary-generate-hook)
3262     ;; Generate the buffer, either with threads or without.
3263     (when gnus-newsgroup-headers
3264       (gnus-summary-prepare-threads
3265        (if gnus-show-threads
3266            (gnus-sort-gathered-threads
3267             (funcall gnus-summary-thread-gathering-function
3268                      (gnus-sort-threads
3269                       (gnus-cut-threads (gnus-make-threads)))))
3270          ;; Unthreaded display.
3271          (gnus-sort-articles gnus-newsgroup-headers))))
3272     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
3273     ;; Call hooks for modifying summary buffer.
3274     (goto-char (point-min))
3275     (gnus-run-hooks 'gnus-summary-prepare-hook)))
3276
3277 (defsubst gnus-general-simplify-subject (subject)
3278   "Simply subject by the same rules as gnus-gather-threads-by-subject."
3279   (setq subject
3280         (cond
3281          ;; Truncate the subject.
3282          (gnus-simplify-subject-functions
3283           (gnus-map-function gnus-simplify-subject-functions subject))
3284          ((numberp gnus-summary-gather-subject-limit)
3285           (setq subject (gnus-simplify-subject-re subject))
3286           (if (> (length subject) gnus-summary-gather-subject-limit)
3287               (substring subject 0 gnus-summary-gather-subject-limit)
3288             subject))
3289          ;; Fuzzily simplify it.
3290          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3291           (gnus-simplify-subject-fuzzy subject))
3292          ;; Just remove the leading "Re:".
3293          (t
3294           (gnus-simplify-subject-re subject))))
3295
3296   (if (and gnus-summary-gather-exclude-subject
3297            (string-match gnus-summary-gather-exclude-subject subject))
3298       nil                         ; This article shouldn't be gathered
3299     subject))
3300
3301 (defun gnus-summary-simplify-subject-query ()
3302   "Query where the respool algorithm would put this article."
3303   (interactive)
3304   (gnus-summary-select-article)
3305   (message (gnus-general-simplify-subject (gnus-summary-article-subject))))
3306
3307 (defun gnus-gather-threads-by-subject (threads)
3308   "Gather threads by looking at Subject headers."
3309   (if (not gnus-summary-make-false-root)
3310       threads
3311     (let ((hashtb (gnus-make-hashtable 1024))
3312           (prev threads)
3313           (result threads)
3314           subject hthread whole-subject)
3315       (while threads
3316         (setq subject (gnus-general-simplify-subject
3317                        (setq whole-subject (mail-header-subject
3318                                             (caar threads)))))
3319         (when subject
3320           (if (setq hthread (gnus-gethash subject hashtb))
3321               (progn
3322                 ;; We enter a dummy root into the thread, if we
3323                 ;; haven't done that already.
3324                 (unless (stringp (caar hthread))
3325                   (setcar hthread (list whole-subject (car hthread))))
3326                 ;; We add this new gathered thread to this gathered
3327                 ;; thread.
3328                 (setcdr (car hthread)
3329                         (nconc (cdar hthread) (list (car threads))))
3330                 ;; Remove it from the list of threads.
3331                 (setcdr prev (cdr threads))
3332                 (setq threads prev))
3333             ;; Enter this thread into the hash table.
3334             (gnus-sethash subject threads hashtb)))
3335         (setq prev threads)
3336         (setq threads (cdr threads)))
3337       result)))
3338
3339 (defun gnus-gather-threads-by-references (threads)
3340   "Gather threads by looking at References headers."
3341   (let ((idhashtb (gnus-make-hashtable 1024))
3342         (thhashtb (gnus-make-hashtable 1024))
3343         (prev threads)
3344         (result threads)
3345         ids references id gthread gid entered ref)
3346     (while threads
3347       (when (setq references (mail-header-references (caar threads)))
3348         (setq id (mail-header-id (caar threads))
3349               ids (gnus-split-references references)
3350               entered nil)
3351         (while (setq ref (pop ids))
3352           (setq ids (delete ref ids))
3353           (if (not (setq gid (gnus-gethash ref idhashtb)))
3354               (progn
3355                 (gnus-sethash ref id idhashtb)
3356                 (gnus-sethash id threads thhashtb))
3357             (setq gthread (gnus-gethash gid thhashtb))
3358             (unless entered
3359               ;; We enter a dummy root into the thread, if we
3360               ;; haven't done that already.
3361               (unless (stringp (caar gthread))
3362                 (setcar gthread (list (mail-header-subject (caar gthread))
3363                                       (car gthread))))
3364               ;; We add this new gathered thread to this gathered
3365               ;; thread.
3366               (setcdr (car gthread)
3367                       (nconc (cdar gthread) (list (car threads)))))
3368             ;; Add it into the thread hash table.
3369             (gnus-sethash id gthread thhashtb)
3370             (setq entered t)
3371             ;; Remove it from the list of threads.
3372             (setcdr prev (cdr threads))
3373             (setq threads prev))))
3374       (setq prev threads)
3375       (setq threads (cdr threads)))
3376     result))
3377
3378 (defun gnus-sort-gathered-threads (threads)
3379   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3380   (let ((result threads))
3381     (while threads
3382       (when (stringp (caar threads))
3383         (setcdr (car threads)
3384                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3385       (setq threads (cdr threads)))
3386     result))
3387
3388 (defun gnus-thread-loop-p (root thread)
3389   "Say whether ROOT is in THREAD."
3390   (let ((stack (list thread))
3391         (infloop 0)
3392         th)
3393     (while (setq thread (pop stack))
3394       (setq th (cdr thread))
3395       (while (and th
3396                   (not (eq (caar th) root)))
3397         (pop th))
3398       (if th
3399           ;; We have found a loop.
3400           (let (ref-dep)
3401             (setcdr thread (delq (car th) (cdr thread)))
3402             (if (boundp (setq ref-dep (intern "none"
3403                                               gnus-newsgroup-dependencies)))
3404                 (setcdr (symbol-value ref-dep)
3405                         (nconc (cdr (symbol-value ref-dep))
3406                                (list (car th))))
3407               (set ref-dep (list nil (car th))))
3408             (setq infloop 1
3409                   stack nil))
3410         ;; Push all the subthreads onto the stack.
3411         (push (cdr thread) stack)))
3412     infloop))
3413
3414 (defun gnus-make-threads ()
3415   "Go through the dependency hashtb and find the roots.  Return all threads."
3416   (let (threads)
3417     (while (catch 'infloop
3418              (mapatoms
3419               (lambda (refs)
3420                 ;; Deal with self-referencing References loops.
3421                 (when (and (car (symbol-value refs))
3422                            (not (zerop
3423                                  (apply
3424                                   '+
3425                                   (mapcar
3426                                    (lambda (thread)
3427                                      (gnus-thread-loop-p
3428                                       (car (symbol-value refs)) thread))
3429                                    (cdr (symbol-value refs)))))))
3430                   (setq threads nil)
3431                   (throw 'infloop t))
3432                 (unless (car (symbol-value refs))
3433              ;; These threads do not refer back to any other articles,
3434                   ;; so they're roots.
3435                   (setq threads (append (cdr (symbol-value refs)) threads))))
3436               gnus-newsgroup-dependencies)))
3437     threads))
3438
3439 ;; Build the thread tree.
3440 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3441   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3442
3443 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3444 if it was already present.
3445
3446 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3447 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3448 Message-IDs will be renamed be renamed to a unique Message-ID before
3449 being entered.
3450
3451 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3452   (let* ((id (mail-header-id header))
3453          (id-dep (and id (intern id dependencies)))
3454          ref ref-dep ref-header)
3455     ;; Enter this `header' in the `dependencies' table.
3456     (cond
3457      ((not id-dep)
3458       (setq header nil))
3459      ;; The first two cases do the normal part: enter a new `header'
3460      ;; in the `dependencies' table.
3461      ((not (boundp id-dep))
3462       (set id-dep (list header)))
3463      ((null (car (symbol-value id-dep)))
3464       (setcar (symbol-value id-dep) header))
3465
3466      ;; From here the `header' was already present in the
3467      ;; `dependencies' table.
3468      (force-new
3469       ;; Overrides an existing entry;
3470       ;; just set the header part of the entry.
3471       (setcar (symbol-value id-dep) header))
3472
3473      ;; Renames the existing `header' to a unique Message-ID.
3474      ((not gnus-summary-ignore-duplicates)
3475       ;; An article with this Message-ID has already been seen.
3476       ;; We rename the Message-ID.
3477       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3478            (list header))
3479       (mail-header-set-id header id))
3480
3481      ;; The last case ignores an existing entry, except it adds any
3482      ;; additional Xrefs (in case the two articles came from different
3483      ;; servers.
3484      ;; Also sets `header' to `nil' meaning that the `dependencies'
3485      ;; table was *not* modified.
3486      (t
3487       (mail-header-set-xref
3488        (car (symbol-value id-dep))
3489        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3490                    "")
3491                (or (mail-header-xref header) "")))
3492       (setq header nil)))
3493
3494     (when header
3495       ;; First check if that we are not creating a References loop.
3496       (setq ref (gnus-parent-id (mail-header-references header)))
3497       (while (and ref
3498                   (setq ref-dep (intern-soft ref dependencies))
3499                   (boundp ref-dep)
3500                   (setq ref-header (car (symbol-value ref-dep))))
3501         (if (string= id ref)
3502             ;; Yuk!  This is a reference loop.  Make the article be a
3503             ;; root article.
3504             (progn
3505               (mail-header-set-references (car (symbol-value id-dep)) "none")
3506               (setq ref nil))
3507           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3508       (setq ref (gnus-parent-id (mail-header-references header)))
3509       (setq ref-dep (intern (or ref "none") dependencies))
3510       (if (boundp ref-dep)
3511           (setcdr (symbol-value ref-dep)
3512                   (nconc (cdr (symbol-value ref-dep))
3513                          (list (symbol-value id-dep))))
3514         (set ref-dep (list nil (symbol-value id-dep)))))
3515     header))
3516
3517 (defun gnus-build-sparse-threads ()
3518   (let ((headers gnus-newsgroup-headers)
3519         (mail-parse-charset gnus-newsgroup-charset)
3520         (gnus-summary-ignore-duplicates t)
3521         header references generation relations
3522         subject child end new-child date)
3523     ;; First we create an alist of generations/relations, where
3524     ;; generations is how much we trust the relation, and the relation
3525     ;; is parent/child.
3526     (gnus-message 7 "Making sparse threads...")
3527     (save-excursion
3528       (nnheader-set-temp-buffer " *gnus sparse threads*")
3529       (while (setq header (pop headers))
3530         (when (and (setq references (mail-header-references header))
3531                    (not (string= references "")))
3532           (insert references)
3533           (setq child (mail-header-id header)
3534                 subject (mail-header-subject header)
3535                 date (mail-header-date header)
3536                 generation 0)
3537           (while (search-backward ">" nil t)
3538             (setq end (1+ (point)))
3539             (when (search-backward "<" nil t)
3540               (setq new-child (buffer-substring (point) end))
3541               (push (list (incf generation)
3542                           child (setq child new-child)
3543                           subject date)
3544                     relations)))
3545           (when child
3546             (push (list (1+ generation) child nil subject) relations))
3547           (erase-buffer)))
3548       (kill-buffer (current-buffer)))
3549     ;; Sort over trustworthiness.
3550     (mapcar
3551      (lambda (relation)
3552        (when (gnus-dependencies-add-header
3553               (make-full-mail-header
3554                gnus-reffed-article-number
3555                (nth 3 relation) "" (or (nth 4 relation) "")
3556                (nth 1 relation)
3557                (or (nth 2 relation) "") 0 0 "")
3558               gnus-newsgroup-dependencies nil)
3559          (push gnus-reffed-article-number gnus-newsgroup-limit)
3560          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3561          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3562                gnus-newsgroup-reads)
3563          (decf gnus-reffed-article-number)))
3564      (sort relations 'car-less-than-car))
3565     (gnus-message 7 "Making sparse threads...done")))
3566
3567 (defun gnus-build-old-threads ()
3568   ;; Look at all the articles that refer back to old articles, and
3569   ;; fetch the headers for the articles that aren't there.  This will
3570   ;; build complete threads - if the roots haven't been expired by the
3571   ;; server, that is.
3572   (let ((mail-parse-charset gnus-newsgroup-charset)
3573         id heads)
3574     (mapatoms
3575      (lambda (refs)
3576        (when (not (car (symbol-value refs)))
3577          (setq heads (cdr (symbol-value refs)))
3578          (while heads
3579            (if (memq (mail-header-number (caar heads))
3580                      gnus-newsgroup-dormant)
3581                (setq heads (cdr heads))
3582              (setq id (symbol-name refs))
3583              (while (and (setq id (gnus-build-get-header id))
3584                          (not (car (gnus-id-to-thread id)))))
3585              (setq heads nil)))))
3586      gnus-newsgroup-dependencies)))
3587
3588 ;; This function has to be called with point after the article number
3589 ;; on the beginning of the line.
3590 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3591   (let ((eol (gnus-point-at-eol))
3592         (buffer (current-buffer))
3593         header)
3594
3595     ;; overview: [num subject from date id refs chars lines misc]
3596     (unwind-protect
3597         (progn
3598           (narrow-to-region (point) eol)
3599           (unless (eobp)
3600             (forward-char))
3601
3602           (setq header
3603                 (make-full-mail-header
3604                  number                 ; number
3605                  (funcall gnus-decode-encoded-word-function
3606                           (nnheader-nov-field)) ; subject
3607                  (funcall gnus-decode-encoded-word-function
3608                           (nnheader-nov-field)) ; from
3609                  (nnheader-nov-field)   ; date
3610                  (nnheader-nov-read-message-id) ; id
3611                  (nnheader-nov-field)   ; refs
3612                  (nnheader-nov-read-integer) ; chars
3613                  (nnheader-nov-read-integer) ; lines
3614                  (unless (eobp)
3615                    (if (looking-at "Xref: ")
3616                        (goto-char (match-end 0)))
3617                    (nnheader-nov-field)) ; Xref
3618                  (nnheader-nov-parse-extra)))) ; extra
3619
3620       (widen))
3621
3622     (when gnus-alter-header-function
3623       (funcall gnus-alter-header-function header))
3624     (gnus-dependencies-add-header header dependencies force-new)))
3625
3626 (defun gnus-build-get-header (id)
3627   "Look through the buffer of NOV lines and find the header to ID.
3628 Enter this line into the dependencies hash table, and return
3629 the id of the parent article (if any)."
3630   (let ((deps gnus-newsgroup-dependencies)
3631         found header)
3632     (prog1
3633         (save-excursion
3634           (set-buffer nntp-server-buffer)
3635           (let ((case-fold-search nil))
3636             (goto-char (point-min))
3637             (while (and (not found)
3638                         (search-forward id nil t))
3639               (beginning-of-line)
3640               (setq found (looking-at
3641                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3642                                    (regexp-quote id))))
3643               (or found (beginning-of-line 2)))
3644             (when found
3645               (beginning-of-line)
3646               (and
3647                (setq header (gnus-nov-parse-line
3648                              (read (current-buffer)) deps))
3649                (gnus-parent-id (mail-header-references header))))))
3650       (when header
3651         (let ((number (mail-header-number header)))
3652           (push number gnus-newsgroup-limit)
3653           (push header gnus-newsgroup-headers)
3654           (if (memq number gnus-newsgroup-unselected)
3655               (progn
3656                 (push number gnus-newsgroup-unreads)
3657                 (setq gnus-newsgroup-unselected
3658                       (delq number gnus-newsgroup-unselected)))
3659             (push number gnus-newsgroup-ancient)))))))
3660
3661 (defun gnus-build-all-threads ()
3662   "Read all the headers."
3663   (let ((gnus-summary-ignore-duplicates t)
3664         (mail-parse-charset gnus-newsgroup-charset)
3665         (dependencies gnus-newsgroup-dependencies)
3666         header article)
3667     (save-excursion
3668       (set-buffer nntp-server-buffer)
3669       (let ((case-fold-search nil))
3670         (goto-char (point-min))
3671         (while (not (eobp))
3672           (ignore-errors
3673             (setq article (read (current-buffer))
3674                   header (gnus-nov-parse-line article dependencies)))
3675           (when header
3676             (save-excursion
3677               (set-buffer gnus-summary-buffer)
3678               (push header gnus-newsgroup-headers)
3679               (if (memq (setq article (mail-header-number header))
3680                         gnus-newsgroup-unselected)
3681                   (progn
3682                     (push article gnus-newsgroup-unreads)
3683                     (setq gnus-newsgroup-unselected
3684                           (delq article gnus-newsgroup-unselected)))
3685                 (push article gnus-newsgroup-ancient)))
3686             (forward-line 1)))))))
3687
3688 (defun gnus-summary-update-article-line (article header)
3689   "Update the line for ARTICLE using HEADERS."
3690   (let* ((id (mail-header-id header))
3691          (thread (gnus-id-to-thread id)))
3692     (unless thread
3693       (error "Article in no thread"))
3694     ;; Update the thread.
3695     (setcar thread header)
3696     (gnus-summary-goto-subject article)
3697     (let* ((datal (gnus-data-find-list article))
3698            (data (car datal))
3699            (length (when (cdr datal)
3700                      (- (gnus-data-pos data)
3701                         (gnus-data-pos (cadr datal)))))
3702            (buffer-read-only nil)
3703            (level (gnus-summary-thread-level)))
3704       (gnus-delete-line)
3705       (gnus-summary-insert-line
3706        header level nil (gnus-article-mark article)
3707        (memq article gnus-newsgroup-replied)
3708        (memq article gnus-newsgroup-expirable)
3709        ;; Only insert the Subject string when it's different
3710        ;; from the previous Subject string.
3711        (if (and
3712             gnus-show-threads
3713             (gnus-subject-equal
3714              (condition-case ()
3715                  (mail-header-subject
3716                   (gnus-data-header
3717                    (cadr
3718                     (gnus-data-find-list
3719                      article
3720                      (gnus-data-list t)))))
3721                ;; Error on the side of excessive subjects.
3722                (error ""))
3723              (mail-header-subject header)))
3724            ""
3725          (mail-header-subject header))
3726        nil (cdr (assq article gnus-newsgroup-scored))
3727        (memq article gnus-newsgroup-processable))
3728       (when length
3729         (gnus-data-update-list
3730          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3731
3732 (defun gnus-summary-update-article (article &optional iheader)
3733   "Update ARTICLE in the summary buffer."
3734   (set-buffer gnus-summary-buffer)
3735   (let* ((header (gnus-summary-article-header article))
3736          (id (mail-header-id header))
3737          (data (gnus-data-find article))
3738          (thread (gnus-id-to-thread id))
3739          (references (mail-header-references header))
3740          (parent
3741           (gnus-id-to-thread
3742            (or (gnus-parent-id
3743                 (when (and references
3744                            (not (equal "" references)))
3745                   references))
3746                "none")))
3747          (buffer-read-only nil)
3748          (old (car thread)))
3749     (when thread
3750       (unless iheader
3751         (setcar thread nil)
3752         (when parent
3753           (delq thread parent)))
3754       (if (gnus-summary-insert-subject id header)
3755        ;; Set the (possibly) new article number in the data structure.
3756           (gnus-data-set-number data (gnus-id-to-article id))
3757         (setcar thread old)
3758         nil))))
3759
3760 (defun gnus-rebuild-thread (id &optional line)
3761   "Rebuild the thread containing ID.
3762 If LINE, insert the rebuilt thread starting on line LINE."
3763   (let ((buffer-read-only nil)
3764         old-pos current thread data)
3765     (if (not gnus-show-threads)
3766         (setq thread (list (car (gnus-id-to-thread id))))
3767       ;; Get the thread this article is part of.
3768       (setq thread (gnus-remove-thread id)))
3769     (setq old-pos (gnus-point-at-bol))
3770     (setq current (save-excursion
3771                     (and (re-search-backward "[\r\n]" nil t)
3772                          (gnus-summary-article-number))))
3773     ;; If this is a gathered thread, we have to go some re-gathering.
3774     (when (stringp (car thread))
3775       (let ((subject (car thread))
3776             roots thr)
3777         (setq thread (cdr thread))
3778         (while thread
3779           (unless (memq (setq thr (gnus-id-to-thread
3780                                    (gnus-root-id
3781                                     (mail-header-id (caar thread)))))
3782                         roots)
3783             (push thr roots))
3784           (setq thread (cdr thread)))
3785         ;; We now have all (unique) roots.
3786         (if (= (length roots) 1)
3787             ;; All the loose roots are now one solid root.
3788             (setq thread (car roots))
3789           (setq thread (cons subject (gnus-sort-threads roots))))))
3790     (let (threads)
3791       ;; We then insert this thread into the summary buffer.
3792       (when line
3793         (goto-char (point-min))
3794         (forward-line (1- line)))
3795       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3796         (if gnus-show-threads
3797             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3798           (gnus-summary-prepare-unthreaded thread))
3799         (setq data (nreverse gnus-newsgroup-data))
3800         (setq threads gnus-newsgroup-threads))
3801       ;; We splice the new data into the data structure.
3802       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3803       ;;!!! then we want to insert at the beginning of the buffer.
3804       ;;!!! That happens to be true with Gnus now, but that may
3805       ;;!!! change in the future.  Perhaps.
3806       (gnus-data-enter-list
3807        (if line nil current) data (- (point) old-pos))
3808       (setq gnus-newsgroup-threads
3809             (nconc threads gnus-newsgroup-threads))
3810       (gnus-data-compute-positions))))
3811
3812 (defun gnus-number-to-header (number)
3813   "Return the header for article NUMBER."
3814   (let ((headers gnus-newsgroup-headers))
3815     (while (and headers
3816                 (not (= number (mail-header-number (car headers)))))
3817       (pop headers))
3818     (when headers
3819       (car headers))))
3820
3821 (defun gnus-parent-headers (in-headers &optional generation)
3822   "Return the headers of the GENERATIONeth parent of HEADERS."
3823   (unless generation
3824     (setq generation 1))
3825   (let ((parent t)
3826         (headers in-headers)
3827         references)
3828     (while (and parent
3829                 (not (zerop generation))
3830                 (setq references (mail-header-references headers)))
3831       (setq headers (if (and references
3832                              (setq parent (gnus-parent-id references)))
3833                         (car (gnus-id-to-thread parent))
3834                       nil))
3835       (decf generation))
3836     (and (not (eq headers in-headers))
3837          headers)))
3838
3839 (defun gnus-id-to-thread (id)
3840   "Return the (sub-)thread where ID appears."
3841   (gnus-gethash id gnus-newsgroup-dependencies))
3842
3843 (defun gnus-id-to-article (id)
3844   "Return the article number of ID."
3845   (let ((thread (gnus-id-to-thread id)))
3846     (when (and thread
3847                (car thread))
3848       (mail-header-number (car thread)))))
3849
3850 (defun gnus-id-to-header (id)
3851   "Return the article headers of ID."
3852   (car (gnus-id-to-thread id)))
3853
3854 (defun gnus-article-displayed-root-p (article)
3855   "Say whether ARTICLE is a root(ish) article."
3856   (let ((level (gnus-summary-thread-level article))
3857         (refs (mail-header-references  (gnus-summary-article-header article)))
3858         particle)
3859     (cond
3860      ((null level) nil)
3861      ((zerop level) t)
3862      ((null refs) t)
3863      ((null (gnus-parent-id refs)) t)
3864      ((and (= 1 level)
3865            (null (setq particle (gnus-id-to-article
3866                                  (gnus-parent-id refs))))
3867            (null (gnus-summary-thread-level particle)))))))
3868
3869 (defun gnus-root-id (id)
3870   "Return the id of the root of the thread where ID appears."
3871   (let (last-id prev)
3872     (while (and id (setq prev (car (gnus-id-to-thread id))))
3873       (setq last-id id
3874             id (gnus-parent-id (mail-header-references prev))))
3875     last-id))
3876
3877 (defun gnus-articles-in-thread (thread)
3878   "Return the list of articles in THREAD."
3879   (cons (mail-header-number (car thread))
3880         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3881
3882 (defun gnus-remove-thread (id &optional dont-remove)
3883   "Remove the thread that has ID in it."
3884   (let (headers thread last-id)
3885     ;; First go up in this thread until we find the root.
3886     (setq last-id (gnus-root-id id)
3887           headers (message-flatten-list (gnus-id-to-thread last-id)))
3888     ;; We have now found the real root of this thread.  It might have
3889     ;; been gathered into some loose thread, so we have to search
3890     ;; through the threads to find the thread we wanted.
3891     (let ((threads gnus-newsgroup-threads)
3892           sub)
3893       (while threads
3894         (setq sub (car threads))
3895         (if (stringp (car sub))
3896             ;; This is a gathered thread, so we look at the roots
3897             ;; below it to find whether this article is in this
3898             ;; gathered root.
3899             (progn
3900               (setq sub (cdr sub))
3901               (while sub
3902                 (when (member (caar sub) headers)
3903                   (setq thread (car threads)
3904                         threads nil
3905                         sub nil))
3906                 (setq sub (cdr sub))))
3907           ;; It's an ordinary thread, so we check it.
3908           (when (eq (car sub) (car headers))
3909             (setq thread sub
3910                   threads nil)))
3911         (setq threads (cdr threads)))
3912       ;; If this article is in no thread, then it's a root.
3913       (if thread
3914           (unless dont-remove
3915             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3916         (setq thread (gnus-id-to-thread last-id)))
3917       (when thread
3918         (prog1
3919             thread                      ; We return this thread.
3920           (unless dont-remove
3921             (if (stringp (car thread))
3922                 (progn
3923                   ;; If we use dummy roots, then we have to remove the
3924                   ;; dummy root as well.
3925                   (when (eq gnus-summary-make-false-root 'dummy)
3926                     ;; We go to the dummy root by going to
3927                     ;; the first sub-"thread", and then one line up.
3928                     (gnus-summary-goto-article
3929                      (mail-header-number (caadr thread)))
3930                     (forward-line -1)
3931                     (gnus-delete-line)
3932                     (gnus-data-compute-positions))
3933                   (setq thread (cdr thread))
3934                   (while thread
3935                     (gnus-remove-thread-1 (car thread))
3936                     (setq thread (cdr thread))))
3937               (gnus-remove-thread-1 thread))))))))
3938
3939 (defun gnus-remove-thread-1 (thread)
3940   "Remove the thread THREAD recursively."
3941   (let ((number (mail-header-number (pop thread)))
3942         d)
3943     (setq thread (reverse thread))
3944     (while thread
3945       (gnus-remove-thread-1 (pop thread)))
3946     (when (setq d (gnus-data-find number))
3947       (goto-char (gnus-data-pos d))
3948       (gnus-summary-show-thread)
3949       (gnus-data-remove
3950        number
3951        (- (gnus-point-at-bol)
3952           (prog1
3953               (1+ (gnus-point-at-eol))
3954             (gnus-delete-line)))))))
3955
3956 (defun gnus-sort-threads-1 (threads func)
3957   (sort (mapcar (lambda (thread)
3958                   (cons (car thread)
3959                         (and (cdr thread)
3960                              (gnus-sort-threads-1 (cdr thread) func))))
3961                 threads) func))
3962
3963 (defun gnus-sort-threads (threads)
3964   "Sort THREADS."
3965   (if (not gnus-thread-sort-functions)
3966       threads
3967     (gnus-message 8 "Sorting threads...")
3968     (prog1
3969         (gnus-sort-threads-1
3970          threads
3971          (gnus-make-sort-function gnus-thread-sort-functions))
3972       (gnus-message 8 "Sorting threads...done"))))
3973
3974 (defun gnus-sort-articles (articles)
3975   "Sort ARTICLES."
3976   (when gnus-article-sort-functions
3977     (gnus-message 7 "Sorting articles...")
3978     (prog1
3979         (setq gnus-newsgroup-headers
3980               (sort articles (gnus-make-sort-function
3981                               gnus-article-sort-functions)))
3982       (gnus-message 7 "Sorting articles...done"))))
3983
3984 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3985 (defmacro gnus-thread-header (thread)
3986   "Return header of first article in THREAD.
3987 Note that THREAD must never, ever be anything else than a variable -
3988 using some other form will lead to serious barfage."
3989   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3990   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3991   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3992         (vector thread) 2))
3993
3994 (defsubst gnus-article-sort-by-number (h1 h2)
3995   "Sort articles by article number."
3996   (< (mail-header-number h1)
3997      (mail-header-number h2)))
3998
3999 (defun gnus-thread-sort-by-number (h1 h2)
4000   "Sort threads by root article number."
4001   (gnus-article-sort-by-number
4002    (gnus-thread-header h1) (gnus-thread-header h2)))
4003
4004 (defsubst gnus-article-sort-by-lines (h1 h2)
4005   "Sort articles by article Lines header."
4006   (< (mail-header-lines h1)
4007      (mail-header-lines h2)))
4008
4009 (defun gnus-thread-sort-by-lines (h1 h2)
4010   "Sort threads by root article Lines header."
4011   (gnus-article-sort-by-lines
4012    (gnus-thread-header h1) (gnus-thread-header h2)))
4013
4014 (defsubst gnus-article-sort-by-chars (h1 h2)
4015   "Sort articles by octet length."
4016   (< (mail-header-chars h1)
4017      (mail-header-chars h2)))
4018
4019 (defun gnus-thread-sort-by-chars (h1 h2)
4020   "Sort threads by root article octet length."
4021   (gnus-article-sort-by-chars
4022    (gnus-thread-header h1) (gnus-thread-header h2)))
4023
4024 (defsubst gnus-article-sort-by-author (h1 h2)
4025   "Sort articles by root author."
4026   (string-lessp
4027    (let ((extract (funcall
4028                    gnus-extract-address-components
4029                    (mail-header-from h1))))
4030      (or (car extract) (cadr extract) ""))
4031    (let ((extract (funcall
4032                    gnus-extract-address-components
4033                    (mail-header-from h2))))
4034      (or (car extract) (cadr extract) ""))))
4035
4036 (defun gnus-thread-sort-by-author (h1 h2)
4037   "Sort threads by root author."
4038   (gnus-article-sort-by-author
4039    (gnus-thread-header h1)  (gnus-thread-header h2)))
4040
4041 (defsubst gnus-article-sort-by-subject (h1 h2)
4042   "Sort articles by root subject."
4043   (string-lessp
4044    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
4045    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
4046
4047 (defun gnus-thread-sort-by-subject (h1 h2)
4048   "Sort threads by root subject."
4049   (gnus-article-sort-by-subject
4050    (gnus-thread-header h1) (gnus-thread-header h2)))
4051
4052 (defsubst gnus-article-sort-by-date (h1 h2)
4053   "Sort articles by root article date."
4054   (time-less-p
4055    (gnus-date-get-time (mail-header-date h1))
4056    (gnus-date-get-time (mail-header-date h2))))
4057
4058 (defun gnus-thread-sort-by-date (h1 h2)
4059   "Sort threads by root article date."
4060   (gnus-article-sort-by-date
4061    (gnus-thread-header h1) (gnus-thread-header h2)))
4062
4063 (defsubst gnus-article-sort-by-score (h1 h2)
4064   "Sort articles by root article score.
4065 Unscored articles will be counted as having a score of zero."
4066   (> (or (cdr (assq (mail-header-number h1)
4067                     gnus-newsgroup-scored))
4068          gnus-summary-default-score 0)
4069      (or (cdr (assq (mail-header-number h2)
4070                     gnus-newsgroup-scored))
4071          gnus-summary-default-score 0)))
4072
4073 (defun gnus-thread-sort-by-score (h1 h2)
4074   "Sort threads by root article score."
4075   (gnus-article-sort-by-score
4076    (gnus-thread-header h1) (gnus-thread-header h2)))
4077
4078 (defun gnus-thread-sort-by-total-score (h1 h2)
4079   "Sort threads by the sum of all scores in the thread.
4080 Unscored articles will be counted as having a score of zero."
4081   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
4082
4083 (defun gnus-thread-total-score (thread)
4084   ;; This function find the total score of THREAD.
4085   (cond ((null thread)
4086          0)
4087         ((consp thread)
4088          (if (stringp (car thread))
4089              (apply gnus-thread-score-function 0
4090                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
4091            (gnus-thread-total-score-1 thread)))
4092         (t
4093          (gnus-thread-total-score-1 (list thread)))))
4094
4095 (defun gnus-thread-total-score-1 (root)
4096   ;; This function find the total score of the thread below ROOT.
4097   (setq root (car root))
4098   (apply gnus-thread-score-function
4099          (or (append
4100               (mapcar 'gnus-thread-total-score
4101                       (cdr (gnus-id-to-thread (mail-header-id root))))
4102               (when (> (mail-header-number root) 0)
4103                 (list (or (cdr (assq (mail-header-number root)
4104                                      gnus-newsgroup-scored))
4105                           gnus-summary-default-score 0))))
4106              (list gnus-summary-default-score)
4107              '(0))))
4108
4109 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
4110 (defvar gnus-tmp-prev-subject nil)
4111 (defvar gnus-tmp-false-parent nil)
4112 (defvar gnus-tmp-root-expunged nil)
4113 (defvar gnus-tmp-dummy-line nil)
4114
4115 (eval-when-compile (defvar gnus-tmp-header))
4116 (defun gnus-extra-header (type &optional header)
4117   "Return the extra header of TYPE."
4118   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
4119       ""))
4120
4121 (defvar gnus-tmp-thread-tree-header-string "")
4122
4123 (defvar gnus-sum-thread-tree-root "> "
4124   "With %B spec, used for the root of a thread.
4125 If nil, use subject instead.")
4126 (defvar gnus-sum-thread-tree-single-indent ""
4127   "With %B spec, used for a thread with just one message.
4128 If nil, use subject instead.")
4129 (defvar gnus-sum-thread-tree-vertical "| "
4130   "With %B spec, used for drawing a vertical line.")
4131 (defvar gnus-sum-thread-tree-indent "  "
4132   "With %B spec, used for indenting.")
4133 (defvar gnus-sum-thread-tree-leaf-with-other "+-> "
4134   "With %B spec, used for a leaf with brothers.")
4135 (defvar gnus-sum-thread-tree-single-leaf "\\-> "
4136   "With %B spec, used for a leaf without brothers.")
4137
4138 (defun gnus-summary-prepare-threads (threads)
4139   "Prepare summary buffer from THREADS and indentation LEVEL.
4140 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
4141 or a straight list of headers."
4142   (gnus-message 7 "Generating summary...")
4143
4144   (setq gnus-newsgroup-threads threads)
4145   (beginning-of-line)
4146
4147   (let ((gnus-tmp-level 0)
4148         (default-score (or gnus-summary-default-score 0))
4149         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
4150         thread number subject stack state gnus-tmp-gathered beg-match
4151         new-roots gnus-tmp-new-adopts thread-end
4152         gnus-tmp-header gnus-tmp-unread
4153         gnus-tmp-replied gnus-tmp-subject-or-nil
4154         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
4155         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
4156         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket
4157         tree-stack)
4158
4159     (setq gnus-tmp-prev-subject nil)
4160
4161     (if (vectorp (car threads))
4162         ;; If this is a straight (sic) list of headers, then a
4163         ;; threaded summary display isn't required, so we just create
4164         ;; an unthreaded one.
4165         (gnus-summary-prepare-unthreaded threads)
4166
4167       ;; Do the threaded display.
4168
4169       (while (or threads stack gnus-tmp-new-adopts new-roots)
4170
4171         (if (and (= gnus-tmp-level 0)
4172                  (or (not stack)
4173                      (= (caar stack) 0))
4174                  (not gnus-tmp-false-parent)
4175                  (or gnus-tmp-new-adopts new-roots))
4176             (if gnus-tmp-new-adopts
4177                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
4178                       thread (list (car gnus-tmp-new-adopts))
4179                       gnus-tmp-header (caar thread)
4180                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
4181               (when new-roots
4182                 (setq thread (list (car new-roots))
4183                       gnus-tmp-header (caar thread)
4184                       new-roots (cdr new-roots))))
4185
4186           (if threads
4187               ;; If there are some threads, we do them before the
4188               ;; threads on the stack.
4189               (setq thread threads
4190                     gnus-tmp-header (caar thread))
4191             ;; There were no current threads, so we pop something off
4192             ;; the stack.
4193             (setq state (car stack)
4194                   gnus-tmp-level (car state)
4195                   tree-stack (cadr state)
4196                   thread (caddr state)
4197                   stack (cdr stack)
4198                   gnus-tmp-header (caar thread))))
4199
4200         (setq gnus-tmp-false-parent nil)
4201         (setq gnus-tmp-root-expunged nil)
4202         (setq thread-end nil)
4203
4204         (if (stringp gnus-tmp-header)
4205             ;; The header is a dummy root.
4206             (cond
4207              ((eq gnus-summary-make-false-root 'adopt)
4208               ;; We let the first article adopt the rest.
4209               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
4210                                                (cddar thread)))
4211               (setq gnus-tmp-gathered
4212                     (nconc (mapcar
4213                             (lambda (h) (mail-header-number (car h)))
4214                             (cddar thread))
4215                            gnus-tmp-gathered))
4216               (setq thread (cons (list (caar thread)
4217                                        (cadar thread))
4218                                  (cdr thread)))
4219               (setq gnus-tmp-level -1
4220                     gnus-tmp-false-parent t))
4221              ((eq gnus-summary-make-false-root 'empty)
4222               ;; We print adopted articles with empty subject fields.
4223               (setq gnus-tmp-gathered
4224                     (nconc (mapcar
4225                             (lambda (h) (mail-header-number (car h)))
4226                             (cddar thread))
4227                            gnus-tmp-gathered))
4228               (setq gnus-tmp-level -1))
4229              ((eq gnus-summary-make-false-root 'dummy)
4230               ;; We remember that we probably want to output a dummy
4231               ;; root.
4232               (setq gnus-tmp-dummy-line gnus-tmp-header)
4233               (setq gnus-tmp-prev-subject gnus-tmp-header))
4234              (t
4235               ;; We do not make a root for the gathered
4236               ;; sub-threads at all.
4237               (setq gnus-tmp-level -1)))
4238
4239           (setq number (mail-header-number gnus-tmp-header)
4240                 subject (mail-header-subject gnus-tmp-header))
4241
4242           (cond
4243            ;; If the thread has changed subject, we might want to make
4244            ;; this subthread into a root.
4245            ((and (null gnus-thread-ignore-subject)
4246                  (not (zerop gnus-tmp-level))
4247                  gnus-tmp-prev-subject
4248                  (not (inline
4249                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
4250             (setq new-roots (nconc new-roots (list (car thread)))
4251                   thread-end t
4252                   gnus-tmp-header nil))
4253            ;; If the article lies outside the current limit,
4254            ;; then we do not display it.
4255            ((not (memq number gnus-newsgroup-limit))
4256             (setq gnus-tmp-gathered
4257                   (nconc (mapcar
4258                           (lambda (h) (mail-header-number (car h)))
4259                           (cdar thread))
4260                          gnus-tmp-gathered))
4261             (setq gnus-tmp-new-adopts (if (cdar thread)
4262                                           (append gnus-tmp-new-adopts
4263                                                   (cdar thread))
4264                                         gnus-tmp-new-adopts)
4265                   thread-end t
4266                   gnus-tmp-header nil)
4267             (when (zerop gnus-tmp-level)
4268               (setq gnus-tmp-root-expunged t)))
4269            ;; Perhaps this article is to be marked as read?
4270            ((and gnus-summary-mark-below
4271                  (< (or (cdr (assq number gnus-newsgroup-scored))
4272                         default-score)
4273                     gnus-summary-mark-below)
4274                  ;; Don't touch sparse articles.
4275                  (not (gnus-summary-article-sparse-p number))
4276                  (not (gnus-summary-article-ancient-p number)))
4277             (setq gnus-newsgroup-unreads
4278                   (delq number gnus-newsgroup-unreads))
4279             (if gnus-newsgroup-auto-expire
4280                 (push number gnus-newsgroup-expirable)
4281               (push (cons number gnus-low-score-mark)
4282                     gnus-newsgroup-reads))))
4283
4284           (when gnus-tmp-header
4285             ;; We may have an old dummy line to output before this
4286             ;; article.
4287             (when (and gnus-tmp-dummy-line
4288                        (gnus-subject-equal
4289                         gnus-tmp-dummy-line
4290                         (mail-header-subject gnus-tmp-header)))
4291               (gnus-summary-insert-dummy-line
4292                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
4293               (setq gnus-tmp-dummy-line nil))
4294
4295             ;; Compute the mark.
4296             (setq gnus-tmp-unread (gnus-article-mark number))
4297
4298             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
4299                                   gnus-tmp-header gnus-tmp-level)
4300                   gnus-newsgroup-data)
4301
4302             ;; Actually insert the line.
4303             (setq
4304              gnus-tmp-subject-or-nil
4305              (cond
4306               ((and gnus-thread-ignore-subject
4307                     gnus-tmp-prev-subject
4308                     (not (inline (gnus-subject-equal
4309                                   gnus-tmp-prev-subject subject))))
4310                subject)
4311               ((zerop gnus-tmp-level)
4312                (if (and (eq gnus-summary-make-false-root 'empty)
4313                         (memq number gnus-tmp-gathered)
4314                         gnus-tmp-prev-subject
4315                         (inline (gnus-subject-equal
4316                                  gnus-tmp-prev-subject subject)))
4317                    gnus-summary-same-subject
4318                  subject))
4319               (t gnus-summary-same-subject)))
4320             (if (and (eq gnus-summary-make-false-root 'adopt)
4321                      (= gnus-tmp-level 1)
4322                      (memq number gnus-tmp-gathered))
4323                 (setq gnus-tmp-opening-bracket ?\<
4324                       gnus-tmp-closing-bracket ?\>)
4325               (setq gnus-tmp-opening-bracket ?\[
4326                     gnus-tmp-closing-bracket ?\]))
4327             (setq
4328              gnus-tmp-indentation
4329              (aref gnus-thread-indent-array gnus-tmp-level)
4330              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4331              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4332                                 gnus-summary-default-score 0)
4333              gnus-tmp-score-char
4334              (if (or (null gnus-summary-default-score)
4335                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4336                          gnus-summary-zcore-fuzz))
4337                  ?                      ;Whitespace
4338                (if (< gnus-tmp-score gnus-summary-default-score)
4339                    gnus-score-below-mark gnus-score-over-mark))
4340              gnus-tmp-replied
4341              (cond ((memq number gnus-newsgroup-processable)
4342                     gnus-process-mark)
4343                    ((memq number gnus-newsgroup-cached)
4344                     gnus-cached-mark)
4345                    ((memq number gnus-newsgroup-replied)
4346                     gnus-replied-mark)
4347                    ((memq number gnus-newsgroup-forwarded)
4348                     gnus-forwarded-mark)
4349                    ((memq number gnus-newsgroup-saved)
4350                     gnus-saved-mark)
4351                    ((memq number gnus-newsgroup-recent)
4352                     gnus-recent-mark)
4353                    ((memq number gnus-newsgroup-unseen)
4354                     gnus-unseen-mark)
4355                    (t gnus-no-mark))
4356              gnus-tmp-from (mail-header-from gnus-tmp-header)
4357              gnus-tmp-name
4358              (cond
4359               ((string-match "<[^>]+> *$" gnus-tmp-from)
4360                (setq beg-match (match-beginning 0))
4361                (or (and (string-match "^\".+\"" gnus-tmp-from)
4362                         (substring gnus-tmp-from 1 (1- (match-end 0))))
4363                    (substring gnus-tmp-from 0 beg-match)))
4364               ((string-match "(.+)" gnus-tmp-from)
4365                (substring gnus-tmp-from
4366                           (1+ (match-beginning 0)) (1- (match-end 0))))
4367               (t gnus-tmp-from))
4368              gnus-tmp-thread-tree-header-string
4369              (cond 
4370               ((not gnus-show-threads) "")
4371               ((zerop gnus-tmp-level)
4372                (if (cdar thread) 
4373                    (or gnus-sum-thread-tree-root subject)
4374                  (or gnus-sum-thread-tree-single-indent subject)))
4375               (t
4376                (concat (apply 'concat
4377                               (mapcar (lambda (item) 
4378                                         (if (= item 1) 
4379                                             gnus-sum-thread-tree-vertical
4380                                           gnus-sum-thread-tree-indent))
4381                                       (cdr (reverse tree-stack))))
4382                        (if (nth 1 thread) 
4383                            gnus-sum-thread-tree-leaf-with-other
4384                          gnus-sum-thread-tree-single-leaf)))))
4385             (when (string= gnus-tmp-name "")
4386               (setq gnus-tmp-name gnus-tmp-from))
4387             (unless (numberp gnus-tmp-lines)
4388               (setq gnus-tmp-lines -1))
4389             (when (= gnus-tmp-lines -1)
4390               (setq gnus-tmp-lines "?"))
4391             (gnus-put-text-property
4392              (point)
4393              (progn (eval gnus-summary-line-format-spec) (point))
4394              'gnus-number number)
4395             (when gnus-visual-p
4396               (forward-line -1)
4397               (gnus-run-hooks 'gnus-summary-update-hook)
4398               (forward-line 1))
4399
4400             (setq gnus-tmp-prev-subject subject)))
4401
4402         (when (nth 1 thread)
4403           (push (list (max 0 gnus-tmp-level)
4404                       (copy-list tree-stack)
4405                       (nthcdr 1 thread))
4406                 stack))
4407         (push (if (nth 1 thread) 1 0) tree-stack)
4408         (incf gnus-tmp-level)
4409         (setq threads (if thread-end nil (cdar thread)))
4410         (unless threads
4411           (setq gnus-tmp-level 0)))))
4412   (gnus-message 7 "Generating summary...done"))
4413
4414 (defun gnus-summary-prepare-unthreaded (headers)
4415   "Generate an unthreaded summary buffer based on HEADERS."
4416   (let (header number mark)
4417
4418     (beginning-of-line)
4419
4420     (while headers
4421       ;; We may have to root out some bad articles...
4422       (when (memq (setq number (mail-header-number
4423                                 (setq header (pop headers))))
4424                   gnus-newsgroup-limit)
4425         ;; Mark article as read when it has a low score.
4426         (when (and gnus-summary-mark-below
4427                    (< (or (cdr (assq number gnus-newsgroup-scored))
4428                           gnus-summary-default-score 0)
4429                       gnus-summary-mark-below)
4430                    (not (gnus-summary-article-ancient-p number)))
4431           (setq gnus-newsgroup-unreads
4432                 (delq number gnus-newsgroup-unreads))
4433           (if gnus-newsgroup-auto-expire
4434               (push number gnus-newsgroup-expirable)
4435             (push (cons number gnus-low-score-mark)
4436                   gnus-newsgroup-reads)))
4437
4438         (setq mark (gnus-article-mark number))
4439         (push (gnus-data-make number mark (1+ (point)) header 0)
4440               gnus-newsgroup-data)
4441         (gnus-summary-insert-line
4442          header 0 number
4443          mark (memq number gnus-newsgroup-replied)
4444          (memq number gnus-newsgroup-expirable)
4445          (mail-header-subject header) nil
4446          (cdr (assq number gnus-newsgroup-scored))
4447          (memq number gnus-newsgroup-processable))))))
4448
4449 (defun gnus-summary-remove-list-identifiers ()
4450   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4451   (let ((regexp (if (consp gnus-list-identifiers)
4452                     (mapconcat 'identity gnus-list-identifiers " *\\|")
4453                   gnus-list-identifiers))
4454         changed subject)
4455     (when regexp
4456       (dolist (header gnus-newsgroup-headers)
4457         (setq subject (mail-header-subject header)
4458               changed nil)
4459         (while (string-match
4460                 (concat "^\\(R[Ee]: +\\)*\\(" regexp " *\\)")
4461                 subject)
4462           (setq subject
4463                 (concat (substring subject 0 (match-beginning 2))
4464                         (substring subject (match-end 0)))
4465                 changed t))
4466         (when (and changed
4467                    (string-match
4468                     "^\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" subject))
4469           (setq subject
4470                 (concat (substring subject 0 (match-beginning 1))
4471                         (substring subject (match-end 1)))))
4472         (when changed
4473           (mail-header-set-subject header subject))))))
4474
4475 (defun gnus-fetch-headers (articles)
4476   "Fetch headers of ARTICLES."
4477   (let ((name (gnus-group-decoded-name gnus-newsgroup-name)))
4478     (gnus-message 5 "Fetching headers for %s..." name)
4479     (prog1
4480         (if (eq 'nov
4481                 (setq gnus-headers-retrieved-by
4482                       (gnus-retrieve-headers
4483                        articles gnus-newsgroup-name
4484                        ;; We might want to fetch old headers, but
4485                        ;; not if there is only 1 article.
4486                        (and (or (and
4487                                  (not (eq gnus-fetch-old-headers 'some))
4488                                  (not (numberp gnus-fetch-old-headers)))
4489                                 (> (length articles) 1))
4490                             gnus-fetch-old-headers))))
4491             (gnus-get-newsgroup-headers-xover
4492              articles nil nil gnus-newsgroup-name t)
4493           (gnus-get-newsgroup-headers))
4494       (gnus-message 5 "Fetching headers for %s...done" name))))
4495
4496 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4497   "Select newsgroup GROUP.
4498 If READ-ALL is non-nil, all articles in the group are selected.
4499 If SELECT-ARTICLES, only select those articles from GROUP."
4500   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4501          ;;!!! Dirty hack; should be removed.
4502          (gnus-summary-ignore-duplicates
4503           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4504               t
4505             gnus-summary-ignore-duplicates))
4506          (info (nth 2 entry))
4507          articles fetched-articles cached)
4508
4509     (unless (gnus-check-server
4510              (setq gnus-current-select-method
4511                    (gnus-find-method-for-group group)))
4512       (error "Couldn't open server"))
4513
4514     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4515         (gnus-activate-group group)     ; Or we can activate it...
4516         (progn                          ; Or we bug out.
4517           (when (equal major-mode 'gnus-summary-mode)
4518             (kill-buffer (current-buffer)))
4519           (error "Couldn't activate group %s: %s"
4520                  group (gnus-status-message group))))
4521
4522     (unless (gnus-request-group group t)
4523       (when (equal major-mode 'gnus-summary-mode)
4524         (kill-buffer (current-buffer)))
4525       (error "Couldn't request group %s: %s"
4526              group (gnus-status-message group)))
4527
4528     (setq gnus-newsgroup-name group
4529           gnus-newsgroup-unselected nil
4530           gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4531     
4532     (let ((display (gnus-group-find-parameter group 'display)))
4533       (setq gnus-newsgroup-display
4534             (cond
4535              ((eq display 'all)
4536               'gnus-not-ignore)
4537              ((arrayp display)
4538               (gnus-summary-display-make-predicate (mapcar 'identity display)))
4539              (t
4540               nil))))
4541       
4542     (gnus-summary-setup-default-charset)
4543
4544     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4545     (when (gnus-virtual-group-p group)
4546       (setq cached gnus-newsgroup-cached))
4547
4548     (setq gnus-newsgroup-unreads
4549           (gnus-set-difference
4550            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4551            gnus-newsgroup-dormant))
4552
4553     (setq gnus-newsgroup-processable nil)
4554
4555     (gnus-update-read-articles group gnus-newsgroup-unreads)
4556
4557     ;; Adjust and set lists of article marks.
4558     (when info
4559       (gnus-adjust-marked-articles info))
4560     
4561     (if (setq articles select-articles)
4562         (setq gnus-newsgroup-unselected
4563               (gnus-sorted-intersection
4564                gnus-newsgroup-unreads
4565                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4566       (setq articles (gnus-articles-to-read group read-all)))
4567
4568     (cond
4569      ((null articles)
4570       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4571       'quit)
4572      ((eq articles 0) nil)
4573      (t
4574       ;; Init the dependencies hash table.
4575       (setq gnus-newsgroup-dependencies
4576             (gnus-make-hashtable (length articles)))
4577       (gnus-set-global-variables)
4578       ;; Retrieve the headers and read them in.
4579       (setq gnus-newsgroup-headers (gnus-fetch-headers articles))
4580
4581       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4582       (when cached
4583         (setq gnus-newsgroup-cached cached))
4584
4585       ;; Suppress duplicates?
4586       (when gnus-suppress-duplicates
4587         (gnus-dup-suppress-articles))
4588
4589       ;; Set the initial limit.
4590       (setq gnus-newsgroup-limit (copy-sequence articles))
4591       ;; Remove canceled articles from the list of unread articles.
4592       (setq fetched-articles
4593             (mapcar (lambda (headers) (mail-header-number headers))
4594                     gnus-newsgroup-headers))
4595       (setq gnus-newsgroup-articles fetched-articles)
4596       (setq gnus-newsgroup-unreads
4597             (gnus-set-sorted-intersection
4598              gnus-newsgroup-unreads fetched-articles))
4599
4600       (let ((marks (assq 'seen (gnus-info-marks info))))
4601         ;; The `seen' marks are treated specially.
4602         (when (setq gnus-newsgroup-seen (cdr marks))
4603           (dolist (article gnus-newsgroup-articles)
4604             (unless (gnus-member-of-range
4605                      article gnus-newsgroup-seen)
4606               (push article gnus-newsgroup-unseen)))))
4607
4608       ;; Removed marked articles that do not exist.
4609       (gnus-update-missing-marks
4610        (gnus-sorted-complement fetched-articles articles))
4611       ;; We might want to build some more threads first.
4612       (when (and gnus-fetch-old-headers
4613                  (eq gnus-headers-retrieved-by 'nov))
4614         (if (eq gnus-fetch-old-headers 'invisible)
4615             (gnus-build-all-threads)
4616           (gnus-build-old-threads)))
4617       ;; Let the Gnus agent mark articles as read.
4618       (when gnus-agent
4619         (gnus-agent-get-undownloaded-list))
4620       ;; Remove list identifiers from subject
4621       (when gnus-list-identifiers
4622         (gnus-summary-remove-list-identifiers))
4623       ;; Check whether auto-expire is to be done in this group.
4624       (setq gnus-newsgroup-auto-expire
4625             (gnus-group-auto-expirable-p group))
4626       ;; Set up the article buffer now, if necessary.
4627       (unless gnus-single-article-buffer
4628         (gnus-article-setup-buffer))
4629       ;; First and last article in this newsgroup.
4630       (when gnus-newsgroup-headers
4631         (setq gnus-newsgroup-begin
4632               (mail-header-number (car gnus-newsgroup-headers))
4633               gnus-newsgroup-end
4634               (mail-header-number
4635                (gnus-last-element gnus-newsgroup-headers))))
4636       ;; GROUP is successfully selected.
4637       (or gnus-newsgroup-headers t)))))
4638
4639 (defun gnus-summary-display-make-predicate (display)
4640   (require 'gnus-agent)
4641   (when (= (length display) 1)
4642     (setq display (car display)))
4643   (unless gnus-summary-display-cache
4644     (dolist (elem (append (list (cons 'read 'read)
4645                                 (cons 'unseen 'unseen))
4646                           gnus-article-mark-lists))
4647       (push (cons (cdr elem)
4648                   (gnus-byte-compile
4649                    `(lambda () (gnus-article-marked-p ',(cdr elem)))))
4650             gnus-summary-display-cache)))
4651   (let ((gnus-category-predicate-alist gnus-summary-display-cache))
4652     (gnus-get-predicate display)))
4653
4654 ;; Uses the dynamically bound `number' variable.
4655 (defvar number)
4656 (defun gnus-article-marked-p (type &optional article)
4657   (let ((article (or article number)))
4658     (cond
4659      ((eq type 'tick)
4660       (memq article gnus-newsgroup-marked))
4661      ((eq type 'unsend)
4662       (memq article gnus-newsgroup-unsendable))
4663      ((eq type 'undownload)
4664       (memq article gnus-newsgroup-undownloaded))
4665      ((eq type 'download)
4666       (memq article gnus-newsgroup-downloadable))
4667      ((eq type 'unread)
4668       (memq article gnus-newsgroup-unreads))
4669      ((eq type 'read)
4670       (memq article gnus-newsgroup-reads))
4671      ((eq type 'dormant)
4672       (memq article gnus-newsgroup-dormant) )
4673      ((eq type 'expire)
4674       (memq article gnus-newsgroup-expirable))
4675      ((eq type 'reply)
4676       (memq article gnus-newsgroup-replied))
4677      ((eq type 'killed)
4678       (memq article gnus-newsgroup-killed))
4679      ((eq type 'bookmark)
4680       (assq article gnus-newsgroup-bookmarks))
4681      ((eq type 'score)
4682       (assq article gnus-newsgroup-scored))
4683      ((eq type 'save)
4684       (memq article gnus-newsgroup-saved))
4685      ((eq type 'cache)
4686       (memq article gnus-newsgroup-cached))
4687      ((eq type 'forward)
4688       (memq article gnus-newsgroup-forwarded))
4689      ((eq type 'seen)
4690       (not (memq article gnus-newsgroup-unseen)))
4691      ((eq type 'recent)
4692       (memq article gnus-newsgroup-recent))
4693      (t t))))
4694
4695 (defun gnus-articles-to-read (group &optional read-all)
4696   "Find out what articles the user wants to read."
4697   (let* ((articles
4698           ;; Select all articles if `read-all' is non-nil, or if there
4699           ;; are no unread articles.
4700           (if (or read-all
4701                   (and (zerop (length gnus-newsgroup-marked))
4702                        (zerop (length gnus-newsgroup-unreads)))
4703                   (eq gnus-newsgroup-display 'gnus-not-ignore))
4704               ;; We want to select the headers for all the articles in
4705               ;; the group, so we select either all the active
4706               ;; articles in the group, or (if that's nil), the
4707               ;; articles in the cache.
4708               (or
4709                (gnus-uncompress-range (gnus-active group))
4710                (gnus-cache-articles-in-group group))
4711             ;; Select only the "normal" subset of articles.
4712             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4713                           (copy-sequence gnus-newsgroup-unreads))
4714                   '<)))
4715          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4716          (scored (length scored-list))
4717          (number (length articles))
4718          (marked (+ (length gnus-newsgroup-marked)
4719                     (length gnus-newsgroup-dormant)))
4720          (select
4721           (cond
4722            ((numberp read-all)
4723             read-all)
4724            (t
4725             (condition-case ()
4726                 (cond
4727                  ((and (or (<= scored marked) (= scored number))
4728                        (numberp gnus-large-newsgroup)
4729                        (> number gnus-large-newsgroup))
4730                   (let ((input
4731                          (read-string
4732                           (format
4733                            "How many articles from %s (default %d): "
4734                            (gnus-limit-string
4735                             (gnus-group-decoded-name gnus-newsgroup-name)
4736                             35)
4737                            number))))
4738                     (if (string-match "^[ \t]*$" input) number input)))
4739                  ((and (> scored marked) (< scored number)
4740                        (> (- scored number) 20))
4741                   (let ((input
4742                          (read-string
4743                           (format "%s %s (%d scored, %d total): "
4744                                   "How many articles from"
4745                                   (gnus-group-decoded-name group)
4746                                   scored number))))
4747                     (if (string-match "^[ \t]*$" input)
4748                         number input)))
4749                  (t number))
4750               (quit
4751                (message "Quit getting the articles to read")
4752                nil))))))
4753     (setq select (if (stringp select) (string-to-number select) select))
4754     (if (or (null select) (zerop select))
4755         select
4756       (if (and (not (zerop scored)) (<= (abs select) scored))
4757           (progn
4758             (setq articles (sort scored-list '<))
4759             (setq number (length articles)))
4760         (setq articles (copy-sequence articles)))
4761
4762       (when (< (abs select) number)
4763         (if (< select 0)
4764             ;; Select the N oldest articles.
4765             (setcdr (nthcdr (1- (abs select)) articles) nil)
4766           ;; Select the N most recent articles.
4767           (setq articles (nthcdr (- number select) articles))))
4768       (setq gnus-newsgroup-unselected
4769             (gnus-sorted-intersection
4770              gnus-newsgroup-unreads
4771              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4772       (when gnus-alter-articles-to-read-function
4773         (setq gnus-newsgroup-unreads
4774               (sort
4775                (funcall gnus-alter-articles-to-read-function
4776                         gnus-newsgroup-name gnus-newsgroup-unreads)
4777                '<)))
4778       articles)))
4779
4780 (defun gnus-killed-articles (killed articles)
4781   (let (out)
4782     (while articles
4783       (when (inline (gnus-member-of-range (car articles) killed))
4784         (push (car articles) out))
4785       (setq articles (cdr articles)))
4786     out))
4787
4788 (defun gnus-uncompress-marks (marks)
4789   "Uncompress the mark ranges in MARKS."
4790   (let ((uncompressed '(score bookmark))
4791         out)
4792     (while marks
4793       (if (memq (caar marks) uncompressed)
4794           (push (car marks) out)
4795         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4796       (setq marks (cdr marks)))
4797     out))
4798
4799 (defun gnus-article-mark-to-type (mark)
4800   "Return the type of MARK."
4801   (or (cadr (assq mark gnus-article-special-mark-lists))
4802       'list))
4803
4804 (defun gnus-adjust-marked-articles (info)
4805   "Set all article lists and remove all marks that are no longer valid."
4806   (let* ((marked-lists (gnus-info-marks info))
4807          (active (gnus-active (gnus-info-group info)))
4808          (min (car active))
4809          (max (cdr active))
4810          (types gnus-article-mark-lists)
4811          marks var articles article mark mark-type)
4812
4813     (dolist (marks marked-lists)
4814       (setq mark (car marks)
4815             mark-type (gnus-article-mark-to-type mark)
4816             var (intern (format "gnus-newsgroup-%s" (car (rassq mark types)))))
4817
4818       ;; We set the variable according to the type of the marks list,
4819       ;; and then adjust the marks to a subset of the active articles.
4820       (cond
4821        ;; Adjust "simple" lists.
4822        ((eq mark-type 'list)
4823         (set var (setq articles (gnus-uncompress-range (cdr marks))))
4824         (when (memq mark '(tick dormant expire reply save))
4825           (while articles
4826             (when (or (< (setq article (pop articles)) min) (> article max))
4827               (set var (delq article (symbol-value var)))))))
4828        ;; Adjust assocs.
4829        ((eq mark-type 'tuple)
4830         (set var (setq articles (cdr marks)))
4831         (when (not (listp (cdr (symbol-value var))))
4832           (set var (list (symbol-value var))))
4833         (when (not (listp (cdr articles)))
4834           (setq articles (list articles)))
4835         (while articles
4836           (when (or (not (consp (setq article (pop articles))))
4837                     (< (car article) min)
4838                     (> (car article) max))
4839             (set var (delq article (symbol-value var))))))
4840        ((eq mark-type 'range)
4841         (cond
4842          ((eq mark 'seen))))))))
4843
4844 (defun gnus-update-missing-marks (missing)
4845   "Go through the list of MISSING articles and remove them from the mark lists."
4846   (when missing
4847     (let (var m)
4848       ;; Go through all types.
4849       (dolist (elem gnus-article-mark-lists)
4850         (when (eq (gnus-article-mark-to-type (cdr elem)) 'list)
4851           (setq var (intern (format "gnus-newsgroup-%s" (car elem))))
4852           (when (symbol-value var)
4853             ;; This list has articles.  So we delete all missing
4854             ;; articles from it.
4855             (setq m missing)
4856             (while m
4857               (set var (delq (pop m) (symbol-value var))))))))))
4858
4859 (defun gnus-update-marks ()
4860   "Enter the various lists of marked articles into the newsgroup info list."
4861   (let ((types gnus-article-mark-lists)
4862         (info (gnus-get-info gnus-newsgroup-name))
4863         (uncompressed '(score bookmark killed seen))
4864         type list newmarked symbol delta-marks)
4865     (when info
4866       ;; Add all marks lists to the list of marks lists.
4867       (while (setq type (pop types))
4868         (setq list (symbol-value
4869                     (setq symbol
4870                           (intern (format "gnus-newsgroup-%s" (car type))))))
4871
4872         (when list
4873           ;; Get rid of the entries of the articles that have the
4874           ;; default score.
4875           (when (and (eq (cdr type) 'score)
4876                      gnus-save-score
4877                      list)
4878             (let* ((arts list)
4879                    (prev (cons nil list))
4880                    (all prev))
4881               (while arts
4882                 (if (or (not (consp (car arts)))
4883                         (= (cdar arts) gnus-summary-default-score))
4884                     (setcdr prev (cdr arts))
4885                   (setq prev arts))
4886                 (setq arts (cdr arts)))
4887               (setq list (cdr all)))))
4888
4889         (when (eq (cdr type) 'seen)
4890           (setq list 
4891                 (if list
4892                     (gnus-add-to-range list gnus-newsgroup-unseen)
4893                   (gnus-compress-sequence gnus-newsgroup-articles))))
4894
4895         (unless (memq (cdr type) uncompressed)
4896           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4897
4898         (when (gnus-check-backend-function
4899                'request-set-mark gnus-newsgroup-name)
4900           ;; propagate flags to server, with the following exceptions:
4901           ;; uncompressed:s are not proper flags (they are cons cells)
4902           ;; cache is a internal gnus flag
4903           ;; download are local to one gnus installation (well)
4904           ;; unsend are for nndraft groups only
4905           ;; xxx: generality of this?  this suits nnimap anyway
4906           (unless (memq (cdr type) (append '(cache download unsend)
4907                                            uncompressed))
4908             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4909                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4910                    (add (gnus-remove-from-range
4911                          (gnus-copy-sequence list) old)))
4912               (when add
4913                 (push (list add 'add (list (cdr type))) delta-marks))
4914               (when del
4915                 (push (list del 'del (list (cdr type))) delta-marks)))))
4916
4917         (when list
4918           (push (cons (cdr type) list) newmarked)))
4919
4920       (when delta-marks
4921         (unless (gnus-check-group gnus-newsgroup-name)
4922           (error "Can't open server for %s" gnus-newsgroup-name))
4923         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4924
4925       ;; Enter these new marks into the info of the group.
4926       (if (nthcdr 3 info)
4927           (setcar (nthcdr 3 info) newmarked)
4928         ;; Add the marks lists to the end of the info.
4929         (when newmarked
4930           (setcdr (nthcdr 2 info) (list newmarked))))
4931
4932       ;; Cut off the end of the info if there's nothing else there.
4933       (let ((i 5))
4934         (while (and (> i 2)
4935                     (not (nth i info)))
4936           (when (nthcdr (decf i) info)
4937             (setcdr (nthcdr i info) nil)))))))
4938
4939 (defun gnus-set-mode-line (where)
4940   "Set the mode line of the article or summary buffers.
4941 If WHERE is `summary', the summary mode line format will be used."
4942   ;; Is this mode line one we keep updated?
4943   (when (and (memq where gnus-updated-mode-lines)
4944              (symbol-value
4945               (intern (format "gnus-%s-mode-line-format-spec" where))))
4946     (let (mode-string)
4947       (save-excursion
4948         ;; We evaluate this in the summary buffer since these
4949         ;; variables are buffer-local to that buffer.
4950         (set-buffer gnus-summary-buffer)
4951        ;; We bind all these variables that are used in the `eval' form
4952         ;; below.
4953         (let* ((mformat (symbol-value
4954                          (intern
4955                           (format "gnus-%s-mode-line-format-spec" where))))
4956                (gnus-tmp-group-name (gnus-group-decoded-name
4957                                      gnus-newsgroup-name))
4958                (gnus-tmp-article-number (or gnus-current-article 0))
4959                (gnus-tmp-unread gnus-newsgroup-unreads)
4960                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4961                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4962                (gnus-tmp-unread-and-unselected
4963                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4964                             (zerop gnus-tmp-unselected))
4965                        "")
4966                       ((zerop gnus-tmp-unselected)
4967                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4968                       (t (format "{%d(+%d) more}"
4969                                  gnus-tmp-unread-and-unticked
4970                                  gnus-tmp-unselected))))
4971                (gnus-tmp-subject
4972                 (if (and gnus-current-headers
4973                          (vectorp gnus-current-headers))
4974                     (gnus-mode-string-quote
4975                      (mail-header-subject gnus-current-headers))
4976                   ""))
4977                bufname-length max-len
4978                gnus-tmp-header) ;; passed as argument to any user-format-funcs
4979           (setq mode-string (eval mformat))
4980           (setq bufname-length (if (string-match "%b" mode-string)
4981                                    (- (length
4982                                        (buffer-name
4983                                         (if (eq where 'summary)
4984                                             nil
4985                                           (get-buffer gnus-article-buffer))))
4986                                       2)
4987                                  0))
4988           (setq max-len (max 4 (if gnus-mode-non-string-length
4989                                    (- (window-width)
4990                                       gnus-mode-non-string-length
4991                                       bufname-length)
4992                                  (length mode-string))))
4993           ;; We might have to chop a bit of the string off...
4994           (when (> (length mode-string) max-len)
4995             (setq mode-string
4996                   (concat (truncate-string-to-width mode-string (- max-len 3))
4997                           "...")))
4998           ;; Pad the mode string a bit.
4999           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
5000       ;; Update the mode line.
5001       (setq mode-line-buffer-identification
5002             (gnus-mode-line-buffer-identification (list mode-string)))
5003       (set-buffer-modified-p t))))
5004
5005 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
5006   "Go through the HEADERS list and add all Xrefs to a hash table.
5007 The resulting hash table is returned, or nil if no Xrefs were found."
5008   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
5009          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
5010          (xref-hashtb (gnus-make-hashtable))
5011          start group entry number xrefs header)
5012     (while headers
5013       (setq header (pop headers))
5014       (when (and (setq xrefs (mail-header-xref header))
5015                  (not (memq (setq number (mail-header-number header))
5016                             unreads)))
5017         (setq start 0)
5018         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
5019           (setq start (match-end 0))
5020           (setq group (if prefix
5021                           (concat prefix (substring xrefs (match-beginning 1)
5022                                                     (match-end 1)))
5023                         (substring xrefs (match-beginning 1) (match-end 1))))
5024           (setq number
5025                 (string-to-int (substring xrefs (match-beginning 2)
5026                                           (match-end 2))))
5027           (if (setq entry (gnus-gethash group xref-hashtb))
5028               (setcdr entry (cons number (cdr entry)))
5029             (gnus-sethash group (cons number nil) xref-hashtb)))))
5030     (and start xref-hashtb)))
5031
5032 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
5033   "Look through all the headers and mark the Xrefs as read."
5034   (let ((virtual (gnus-virtual-group-p from-newsgroup))
5035         name entry info xref-hashtb idlist method nth4)
5036     (save-excursion
5037       (set-buffer gnus-group-buffer)
5038       (when (setq xref-hashtb
5039                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
5040         (mapatoms
5041          (lambda (group)
5042            (unless (string= from-newsgroup (setq name (symbol-name group)))
5043              (setq idlist (symbol-value group))
5044              ;; Dead groups are not updated.
5045              (and (prog1
5046                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
5047                             info (nth 2 entry))
5048                     (when (stringp (setq nth4 (gnus-info-method info)))
5049                       (setq nth4 (gnus-server-to-method nth4))))
5050                   ;; Only do the xrefs if the group has the same
5051                   ;; select method as the group we have just read.
5052                   (or (gnus-methods-equal-p
5053                        nth4 (gnus-find-method-for-group from-newsgroup))
5054                       virtual
5055                       (equal nth4 (setq method (gnus-find-method-for-group
5056                                                 from-newsgroup)))
5057                       (and (equal (car nth4) (car method))
5058                            (equal (nth 1 nth4) (nth 1 method))))
5059                   gnus-use-cross-reference
5060                   (or (not (eq gnus-use-cross-reference t))
5061                       virtual
5062                       ;; Only do cross-references on subscribed
5063                       ;; groups, if that is what is wanted.
5064                       (<= (gnus-info-level info) gnus-level-subscribed))
5065                   (gnus-group-make-articles-read name idlist))))
5066          xref-hashtb)))))
5067
5068 (defun gnus-compute-read-articles (group articles)
5069   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5070          (info (nth 2 entry))
5071          (active (gnus-active group))
5072          ninfo)
5073     (when entry
5074       ;; First peel off all invalid article numbers.
5075       (when active
5076         (let ((ids articles)
5077               id first)
5078           (while (setq id (pop ids))
5079             (when (and first (> id (cdr active)))
5080               ;; We'll end up in this situation in one particular
5081               ;; obscure situation.  If you re-scan a group and get
5082               ;; a new article that is cross-posted to a different
5083               ;; group that has not been re-scanned, you might get
5084               ;; crossposted article that has a higher number than
5085               ;; Gnus believes possible.  So we re-activate this
5086               ;; group as well.  This might mean doing the
5087               ;; crossposting thingy will *increase* the number
5088               ;; of articles in some groups.  Tsk, tsk.
5089               (setq active (or (gnus-activate-group group) active)))
5090             (when (or (> id (cdr active))
5091                       (< id (car active)))
5092               (setq articles (delq id articles))))))
5093       ;; If the read list is nil, we init it.
5094       (if (and active
5095                (null (gnus-info-read info))
5096                (> (car active) 1))
5097           (setq ninfo (cons 1 (1- (car active))))
5098         (setq ninfo (gnus-info-read info)))
5099       ;; Then we add the read articles to the range.
5100       (gnus-add-to-range
5101        ninfo (setq articles (sort articles '<))))))
5102
5103 (defun gnus-group-make-articles-read (group articles)
5104   "Update the info of GROUP to say that ARTICLES are read."
5105   (let* ((num 0)
5106          (entry (gnus-gethash group gnus-newsrc-hashtb))
5107          (info (nth 2 entry))
5108          (active (gnus-active group))
5109          range)
5110     (when entry
5111       (setq range (gnus-compute-read-articles group articles))
5112       (save-excursion
5113         (set-buffer gnus-group-buffer)
5114         (gnus-undo-register
5115           `(progn
5116              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
5117              (gnus-info-set-read ',info ',(gnus-info-read info))
5118              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
5119              (gnus-group-update-group ,group t))))
5120       ;; Add the read articles to the range.
5121       (gnus-info-set-read info range)
5122       (gnus-request-set-mark group (list (list range 'add '(read))))
5123       ;; Then we have to re-compute how many unread
5124       ;; articles there are in this group.
5125       (when active
5126         (cond
5127          ((not range)
5128           (setq num (- (1+ (cdr active)) (car active))))
5129          ((not (listp (cdr range)))
5130           (setq num (- (cdr active) (- (1+ (cdr range))
5131                                        (car range)))))
5132          (t
5133           (while range
5134             (if (numberp (car range))
5135                 (setq num (1+ num))
5136               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
5137             (setq range (cdr range)))
5138           (setq num (- (cdr active) num))))
5139         ;; Update the number of unread articles.
5140         (setcar entry num)
5141         ;; Update the group buffer.
5142         (gnus-group-update-group group t)))))
5143
5144 (defvar gnus-newsgroup-none-id 0)
5145
5146 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
5147   (let ((cur nntp-server-buffer)
5148         (dependencies
5149          (or dependencies
5150              (save-excursion (set-buffer gnus-summary-buffer)
5151                              gnus-newsgroup-dependencies)))
5152         headers id end ref
5153         (mail-parse-charset gnus-newsgroup-charset)
5154         (mail-parse-ignored-charsets
5155          (save-excursion (condition-case nil
5156                              (set-buffer gnus-summary-buffer)
5157                            (error))
5158                          gnus-newsgroup-ignored-charsets)))
5159     (save-excursion
5160       (set-buffer nntp-server-buffer)
5161       ;; Translate all TAB characters into SPACE characters.
5162       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
5163       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5164       (gnus-run-hooks 'gnus-parse-headers-hook)
5165       (let ((case-fold-search t)
5166             in-reply-to header p lines chars)
5167         (goto-char (point-min))
5168         ;; Search to the beginning of the next header.  Error messages
5169         ;; do not begin with 2 or 3.
5170         (while (re-search-forward "^[23][0-9]+ " nil t)
5171           (setq id nil
5172                 ref nil)
5173           ;; This implementation of this function, with nine
5174           ;; search-forwards instead of the one re-search-forward and
5175           ;; a case (which basically was the old function) is actually
5176           ;; about twice as fast, even though it looks messier.  You
5177           ;; can't have everything, I guess.  Speed and elegance
5178           ;; doesn't always go hand in hand.
5179           (setq
5180            header
5181            (vector
5182             ;; Number.
5183             (prog1
5184                 (read cur)
5185               (end-of-line)
5186               (setq p (point))
5187               (narrow-to-region (point)
5188                                 (or (and (search-forward "\n.\n" nil t)
5189                                          (- (point) 2))
5190                                     (point))))
5191             ;; Subject.
5192             (progn
5193               (goto-char p)
5194               (if (search-forward "\nsubject:" nil t)
5195                   (funcall gnus-decode-encoded-word-function
5196                            (nnheader-header-value))
5197                 "(none)"))
5198             ;; From.
5199             (progn
5200               (goto-char p)
5201               (if (search-forward "\nfrom:" nil t)
5202                   (funcall gnus-decode-encoded-word-function
5203                            (nnheader-header-value))
5204                 "(nobody)"))
5205             ;; Date.
5206             (progn
5207               (goto-char p)
5208               (if (search-forward "\ndate:" nil t)
5209                   (nnheader-header-value) ""))
5210             ;; Message-ID.
5211             (progn
5212               (goto-char p)
5213               (setq id (if (re-search-forward
5214                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
5215                       ;; We do it this way to make sure the Message-ID
5216                            ;; is (somewhat) syntactically valid.
5217                            (buffer-substring (match-beginning 1)
5218                                              (match-end 1))
5219                        ;; If there was no message-id, we just fake one
5220                          ;; to make subsequent routines simpler.
5221                          (nnheader-generate-fake-message-id))))
5222             ;; References.
5223             (progn
5224               (goto-char p)
5225               (if (search-forward "\nreferences:" nil t)
5226                   (progn
5227                     (setq end (point))
5228                     (prog1
5229                         (nnheader-header-value)
5230                       (setq ref
5231                             (buffer-substring
5232                              (progn
5233                                (end-of-line)
5234                                (search-backward ">" end t)
5235                                (1+ (point)))
5236                              (progn
5237                                (search-backward "<" end t)
5238                                (point))))))
5239             ;; Get the references from the in-reply-to header if there
5240                 ;; were no references and the in-reply-to header looks
5241                 ;; promising.
5242                 (if (and (search-forward "\nin-reply-to:" nil t)
5243                          (setq in-reply-to (nnheader-header-value))
5244                          (string-match "<[^>]+>" in-reply-to))
5245                     (let (ref2)
5246                       (setq ref (substring in-reply-to (match-beginning 0)
5247                                            (match-end 0)))
5248                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
5249                         (setq ref2 (substring in-reply-to (match-beginning 0)
5250                                               (match-end 0)))
5251                         (when (> (length ref2) (length ref))
5252                           (setq ref ref2)))
5253                       ref)
5254                   (setq ref nil))))
5255             ;; Chars.
5256             (progn
5257               (goto-char p)
5258               (if (search-forward "\nchars: " nil t)
5259                   (if (numberp (setq chars (ignore-errors (read cur))))
5260                       chars -1)
5261                 -1))
5262             ;; Lines.
5263             (progn
5264               (goto-char p)
5265               (if (search-forward "\nlines: " nil t)
5266                   (if (numberp (setq lines (ignore-errors (read cur))))
5267                       lines -1)
5268                 -1))
5269             ;; Xref.
5270             (progn
5271               (goto-char p)
5272               (and (search-forward "\nxref:" nil t)
5273                    (nnheader-header-value)))
5274             ;; Extra.
5275             (when gnus-extra-headers
5276               (let ((extra gnus-extra-headers)
5277                     out)
5278                 (while extra
5279                   (goto-char p)
5280                   (when (search-forward
5281                          (concat "\n" (symbol-name (car extra)) ":") nil t)
5282                     (push (cons (car extra) (nnheader-header-value))
5283                           out))
5284                   (pop extra))
5285                 out))))
5286           (when (equal id ref)
5287             (setq ref nil))
5288
5289           (when gnus-alter-header-function
5290             (funcall gnus-alter-header-function header)
5291             (setq id (mail-header-id header)
5292                   ref (gnus-parent-id (mail-header-references header))))
5293
5294           (when (setq header
5295                       (gnus-dependencies-add-header
5296                        header dependencies force-new))
5297             (push header headers))
5298           (goto-char (point-max))
5299           (widen))
5300         (nreverse headers)))))
5301
5302 ;; Goes through the xover lines and returns a list of vectors
5303 (defun gnus-get-newsgroup-headers-xover (sequence &optional
5304                                                   force-new dependencies
5305                                                   group also-fetch-heads)
5306   "Parse the news overview data in the server buffer.
5307 Return a list of headers that match SEQUENCE (see
5308 `nntp-retrieve-headers')."
5309   ;; Get the Xref when the users reads the articles since most/some
5310   ;; NNTP servers do not include Xrefs when using XOVER.
5311   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
5312   (let ((mail-parse-charset gnus-newsgroup-charset)
5313         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5314         (cur nntp-server-buffer)
5315         (dependencies (or dependencies gnus-newsgroup-dependencies))
5316         (allp (cond
5317                ((eq gnus-read-all-available-headers t)
5318                 t)
5319                ((stringp gnus-read-all-available-headers)
5320                 (string-match gnus-read-all-available-headers group))
5321                (t
5322                 nil)))
5323         number headers header)
5324     (save-excursion
5325       (set-buffer nntp-server-buffer)
5326       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
5327       ;; Allow the user to mangle the headers before parsing them.
5328       (gnus-run-hooks 'gnus-parse-headers-hook)
5329       (goto-char (point-min))
5330       (while (not (eobp))
5331         (condition-case ()
5332             (while (and (or sequence allp)
5333                         (not (eobp)))
5334               (setq number (read cur))
5335               (when (not allp)
5336                 (while (and sequence
5337                             (< (car sequence) number))
5338                   (setq sequence (cdr sequence))))
5339               (when (and (or allp
5340                              (and sequence
5341                                   (eq number (car sequence))))
5342                          (progn
5343                            (setq sequence (cdr sequence))
5344                            (setq header (inline
5345                                           (gnus-nov-parse-line
5346                                            number dependencies force-new)))))
5347                 (push header headers))
5348               (forward-line 1))
5349           (error
5350            (gnus-error 4 "Strange nov line (%d)"
5351                        (count-lines (point-min) (point)))))
5352         (forward-line 1))
5353       ;; A common bug in inn is that if you have posted an article and
5354       ;; then retrieves the active file, it will answer correctly --
5355       ;; the new article is included.  However, a NOV entry for the
5356       ;; article may not have been generated yet, so this may fail.
5357       ;; We work around this problem by retrieving the last few
5358       ;; headers using HEAD.
5359       (if (or (not also-fetch-heads)
5360               (not sequence))
5361           ;; We (probably) got all the headers.
5362           (nreverse headers)
5363         (let ((gnus-nov-is-evil t))
5364           (nconc
5365            (nreverse headers)
5366            (when (eq (gnus-retrieve-headers sequence group) 'headers)
5367              (gnus-get-newsgroup-headers))))))))
5368
5369 (defun gnus-article-get-xrefs ()
5370   "Fill in the Xref value in `gnus-current-headers', if necessary.
5371 This is meant to be called in `gnus-article-internal-prepare-hook'."
5372   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
5373                                  gnus-current-headers)))
5374     (or (not gnus-use-cross-reference)
5375         (not headers)
5376         (and (mail-header-xref headers)
5377              (not (string= (mail-header-xref headers) "")))
5378         (let ((case-fold-search t)
5379               xref)
5380           (save-restriction
5381             (nnheader-narrow-to-headers)
5382             (goto-char (point-min))
5383             (when (or (and (not (eobp))
5384                            (eq (downcase (char-after)) ?x)
5385                            (looking-at "Xref:"))
5386                       (search-forward "\nXref:" nil t))
5387               (goto-char (1+ (match-end 0)))
5388               (setq xref (buffer-substring (point)
5389                                            (progn (end-of-line) (point))))
5390               (mail-header-set-xref headers xref)))))))
5391
5392 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
5393   "Find article ID and insert the summary line for that article.
5394 OLD-HEADER can either be a header or a line number to insert
5395 the subject line on."
5396   (let* ((line (and (numberp old-header) old-header))
5397          (old-header (and (vectorp old-header) old-header))
5398          (header (cond ((and old-header use-old-header)
5399                         old-header)
5400                        ((and (numberp id)
5401                              (gnus-number-to-header id))
5402                         (gnus-number-to-header id))
5403                        (t
5404                         (gnus-read-header id))))
5405          (number (and (numberp id) id))
5406          d)
5407     (when header
5408       ;; Rebuild the thread that this article is part of and go to the
5409       ;; article we have fetched.
5410       (when (and (not gnus-show-threads)
5411                  old-header)
5412         (when (and number
5413                    (setq d (gnus-data-find (mail-header-number old-header))))
5414           (goto-char (gnus-data-pos d))
5415           (gnus-data-remove
5416            number
5417            (- (gnus-point-at-bol)
5418               (prog1
5419                   (1+ (gnus-point-at-eol))
5420                 (gnus-delete-line))))))
5421       (when old-header
5422         (mail-header-set-number header (mail-header-number old-header)))
5423       (setq gnus-newsgroup-sparse
5424             (delq (setq number (mail-header-number header))
5425                   gnus-newsgroup-sparse))
5426       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
5427       (push number gnus-newsgroup-limit)
5428       (gnus-rebuild-thread (mail-header-id header) line)
5429       (gnus-summary-goto-subject number nil t))
5430     (when (and (numberp number)
5431                (> number 0))
5432       ;; We have to update the boundaries even if we can't fetch the
5433       ;; article if ID is a number -- so that the next `P' or `N'
5434       ;; command will fetch the previous (or next) article even
5435       ;; if the one we tried to fetch this time has been canceled.
5436       (when (> number gnus-newsgroup-end)
5437         (setq gnus-newsgroup-end number))
5438       (when (< number gnus-newsgroup-begin)
5439         (setq gnus-newsgroup-begin number))
5440       (setq gnus-newsgroup-unselected
5441             (delq number gnus-newsgroup-unselected)))
5442     ;; Report back a success?
5443     (and header (mail-header-number header))))
5444
5445 ;;; Process/prefix in the summary buffer
5446
5447 (defun gnus-summary-work-articles (n)
5448   "Return a list of articles to be worked upon.
5449 The prefix argument, the list of process marked articles, and the
5450 current article will be taken into consideration."
5451   (save-excursion
5452     (set-buffer gnus-summary-buffer)
5453     (cond
5454      (n
5455       ;; A numerical prefix has been given.
5456       (setq n (prefix-numeric-value n))
5457       (let ((backward (< n 0))
5458             (n (abs (prefix-numeric-value n)))
5459             articles article)
5460         (save-excursion
5461           (while
5462               (and (> n 0)
5463                    (push (setq article (gnus-summary-article-number))
5464                          articles)
5465                    (if backward
5466                        (gnus-summary-find-prev nil article)
5467                      (gnus-summary-find-next nil article)))
5468             (decf n)))
5469         (nreverse articles)))
5470      ((and (gnus-region-active-p) (mark))
5471       (message "region active")
5472       ;; Work on the region between point and mark.
5473       (let ((max (max (point) (mark)))
5474             articles article)
5475         (save-excursion
5476           (goto-char (min (min (point) (mark))))
5477           (while
5478               (and
5479                (push (setq article (gnus-summary-article-number)) articles)
5480                (gnus-summary-find-next nil article)
5481                (< (point) max)))
5482           (nreverse articles))))
5483      (gnus-newsgroup-processable
5484       ;; There are process-marked articles present.
5485       ;; Save current state.
5486       (gnus-summary-save-process-mark)
5487       ;; Return the list.
5488       (reverse gnus-newsgroup-processable))
5489      (t
5490       ;; Just return the current article.
5491       (list (gnus-summary-article-number))))))
5492
5493 (defmacro gnus-summary-iterate (arg &rest forms)
5494   "Iterate over the process/prefixed articles and do FORMS.
5495 ARG is the interactive prefix given to the command.  FORMS will be
5496 executed with point over the summary line of the articles."
5497   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5498     `(let ((,articles (gnus-summary-work-articles ,arg)))
5499        (while ,articles
5500          (gnus-summary-goto-subject (car ,articles))
5501          ,@forms
5502          (pop ,articles)))))
5503
5504 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5505 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5506
5507 (defun gnus-summary-save-process-mark ()
5508   "Push the current set of process marked articles on the stack."
5509   (interactive)
5510   (push (copy-sequence gnus-newsgroup-processable)
5511         gnus-newsgroup-process-stack))
5512
5513 (defun gnus-summary-kill-process-mark ()
5514   "Push the current set of process marked articles on the stack and unmark."
5515   (interactive)
5516   (gnus-summary-save-process-mark)
5517   (gnus-summary-unmark-all-processable))
5518
5519 (defun gnus-summary-yank-process-mark ()
5520   "Pop the last process mark state off the stack and restore it."
5521   (interactive)
5522   (unless gnus-newsgroup-process-stack
5523     (error "Empty mark stack"))
5524   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5525
5526 (defun gnus-summary-process-mark-set (set)
5527   "Make SET into the current process marked articles."
5528   (gnus-summary-unmark-all-processable)
5529   (while set
5530     (gnus-summary-set-process-mark (pop set))))
5531
5532 ;;; Searching and stuff
5533
5534 (defun gnus-summary-search-group (&optional backward use-level)
5535   "Search for next unread newsgroup.
5536 If optional argument BACKWARD is non-nil, search backward instead."
5537   (save-excursion
5538     (set-buffer gnus-group-buffer)
5539     (when (gnus-group-search-forward
5540            backward nil (if use-level (gnus-group-group-level) nil))
5541       (gnus-group-group-name))))
5542
5543 (defun gnus-summary-best-group (&optional exclude-group)
5544   "Find the name of the best unread group.
5545 If EXCLUDE-GROUP, do not go to this group."
5546   (save-excursion
5547     (set-buffer gnus-group-buffer)
5548     (save-excursion
5549       (gnus-group-best-unread-group exclude-group))))
5550
5551 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5552   (if backward (gnus-summary-find-prev)
5553     (let* ((dummy (gnus-summary-article-intangible-p))
5554            (article (or article (gnus-summary-article-number)))
5555            (arts (gnus-data-find-list article))
5556            result)
5557       (when (and (not dummy)
5558                  (or (not gnus-summary-check-current)
5559                      (not unread)
5560                      (not (gnus-data-unread-p (car arts)))))
5561         (setq arts (cdr arts)))
5562       (when (setq result
5563                   (if unread
5564                       (progn
5565                         (while arts
5566                           (when (or (and undownloaded
5567                                          (eq gnus-undownloaded-mark
5568                                              (gnus-data-mark (car arts))))
5569                                     (gnus-data-unread-p (car arts)))
5570                             (setq result (car arts)
5571                                   arts nil))
5572                           (setq arts (cdr arts)))
5573                         result)
5574                     (car arts)))
5575         (goto-char (gnus-data-pos result))
5576         (gnus-data-number result)))))
5577
5578 (defun gnus-summary-find-prev (&optional unread article)
5579   (let* ((eobp (eobp))
5580          (article (or article (gnus-summary-article-number)))
5581          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5582          result)
5583     (when (and (not eobp)
5584                (or (not gnus-summary-check-current)
5585                    (not unread)
5586                    (not (gnus-data-unread-p (car arts)))))
5587       (setq arts (cdr arts)))
5588     (when (setq result
5589                 (if unread
5590                     (progn
5591                       (while arts
5592                         (when (gnus-data-unread-p (car arts))
5593                           (setq result (car arts)
5594                                 arts nil))
5595                         (setq arts (cdr arts)))
5596                       result)
5597                   (car arts)))
5598       (goto-char (gnus-data-pos result))
5599       (gnus-data-number result))))
5600
5601 (defun gnus-summary-find-subject (subject &optional unread backward article)
5602   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5603          (article (or article (gnus-summary-article-number)))
5604          (articles (gnus-data-list backward))
5605          (arts (gnus-data-find-list article articles))
5606          result)
5607     (when (or (not gnus-summary-check-current)
5608               (not unread)
5609               (not (gnus-data-unread-p (car arts))))
5610       (setq arts (cdr arts)))
5611     (while arts
5612       (and (or (not unread)
5613                (gnus-data-unread-p (car arts)))
5614            (vectorp (gnus-data-header (car arts)))
5615            (gnus-subject-equal
5616             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5617            (setq result (car arts)
5618                  arts nil))
5619       (setq arts (cdr arts)))
5620     (and result
5621          (goto-char (gnus-data-pos result))
5622          (gnus-data-number result))))
5623
5624 (defun gnus-summary-search-forward (&optional unread subject backward)
5625   "Search forward for an article.
5626 If UNREAD, look for unread articles.  If SUBJECT, look for
5627 articles with that subject.  If BACKWARD, search backward instead."
5628   (cond (subject (gnus-summary-find-subject subject unread backward))
5629         (backward (gnus-summary-find-prev unread))
5630         (t (gnus-summary-find-next unread))))
5631
5632 (defun gnus-recenter (&optional n)
5633   "Center point in window and redisplay frame.
5634 Also do horizontal recentering."
5635   (interactive "P")
5636   (when (and gnus-auto-center-summary
5637              (not (eq gnus-auto-center-summary 'vertical)))
5638     (gnus-horizontal-recenter))
5639   (recenter n))
5640
5641 (defun gnus-summary-recenter ()
5642   "Center point in the summary window.
5643 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5644 displayed, no centering will be performed."
5645   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5646 ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5647   (interactive)
5648   (let* ((top (cond ((< (window-height) 4) 0)
5649                     ((< (window-height) 7) 1)
5650                     (t (if (numberp gnus-auto-center-summary)
5651                            gnus-auto-center-summary
5652                          2))))
5653          (height (1- (window-height)))
5654          (bottom (save-excursion (goto-char (point-max))
5655                                  (forward-line (- height))
5656                                  (point)))
5657          (window (get-buffer-window (current-buffer))))
5658     ;; The user has to want it.
5659     (when gnus-auto-center-summary
5660       (when (get-buffer-window gnus-article-buffer)
5661         ;; Only do recentering when the article buffer is displayed,
5662       ;; Set the window start to either `bottom', which is the biggest
5663         ;; possible valid number, or the second line from the top,
5664         ;; whichever is the least.
5665         (let ((top-pos (save-excursion (forward-line (- top)) (point))))
5666           (if (> bottom top-pos)
5667               ;; Keep the second line from the top visible
5668               (set-window-start window top-pos t)
5669             ;; Try to keep the bottom line visible; if it's partially
5670             ;; obscured, either scroll one more line to make it fully
5671             ;; visible, or revert to using TOP-POS.
5672             (save-excursion
5673               (goto-char (point-max))
5674               (forward-line -1)
5675               (let ((last-line-start (point)))
5676                 (goto-char bottom)
5677                 (set-window-start window (point) t)
5678                 (when (not (pos-visible-in-window-p last-line-start window))
5679                   (forward-line 1)
5680                   (set-window-start window (min (point) top-pos) t)))))))
5681       ;; Do horizontal recentering while we're at it.
5682       (when (and (get-buffer-window (current-buffer) t)
5683                  (not (eq gnus-auto-center-summary 'vertical)))
5684         (let ((selected (selected-window)))
5685           (select-window (get-buffer-window (current-buffer) t))
5686           (gnus-summary-position-point)
5687           (gnus-horizontal-recenter)
5688           (select-window selected))))))
5689
5690 (defun gnus-summary-jump-to-group (newsgroup)
5691   "Move point to NEWSGROUP in group mode buffer."
5692   ;; Keep update point of group mode buffer if visible.
5693   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5694       (save-window-excursion
5695         ;; Take care of tree window mode.
5696         (when (get-buffer-window gnus-group-buffer)
5697           (pop-to-buffer gnus-group-buffer))
5698         (gnus-group-jump-to-group newsgroup))
5699     (save-excursion
5700       ;; Take care of tree window mode.
5701       (if (get-buffer-window gnus-group-buffer)
5702           (pop-to-buffer gnus-group-buffer)
5703         (set-buffer gnus-group-buffer))
5704       (gnus-group-jump-to-group newsgroup))))
5705
5706 ;; This function returns a list of article numbers based on the
5707 ;; difference between the ranges of read articles in this group and
5708 ;; the range of active articles.
5709 (defun gnus-list-of-unread-articles (group)
5710   (let* ((read (gnus-info-read (gnus-get-info group)))
5711          (active (or (gnus-active group) (gnus-activate-group group)))
5712          (last (cdr active))
5713          first nlast unread)
5714     ;; If none are read, then all are unread.
5715     (if (not read)
5716         (setq first (car active))
5717       ;; If the range of read articles is a single range, then the
5718       ;; first unread article is the article after the last read
5719       ;; article.  Sounds logical, doesn't it?
5720       (if (and (not (listp (cdr read)))
5721                (or (< (car read) (car active))
5722                    (progn (setq read (list read))
5723                           nil)))
5724           (setq first (max (car active) (1+ (cdr read))))
5725         ;; `read' is a list of ranges.
5726         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5727                                   (caar read)))
5728                   1)
5729           (setq first (car active)))
5730         (while read
5731           (when first
5732             (while (< first nlast)
5733               (push first unread)
5734               (setq first (1+ first))))
5735           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5736           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5737           (setq read (cdr read)))))
5738     ;; And add the last unread articles.
5739     (while (<= first last)
5740       (push first unread)
5741       (setq first (1+ first)))
5742     ;; Return the list of unread articles.
5743     (delq 0 (nreverse unread))))
5744
5745 (defun gnus-list-of-read-articles (group)
5746   "Return a list of unread, unticked and non-dormant articles."
5747   (let* ((info (gnus-get-info group))
5748          (marked (gnus-info-marks info))
5749          (active (gnus-active group)))
5750     (and info active
5751          (gnus-set-difference
5752           (gnus-sorted-complement
5753            (gnus-uncompress-range active)
5754            (gnus-list-of-unread-articles group))
5755           (append
5756            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5757            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5758
5759 ;; Various summary commands
5760
5761 (defun gnus-summary-select-article-buffer ()
5762   "Reconfigure windows to show article buffer."
5763   (interactive)
5764   (if (not (gnus-buffer-live-p gnus-article-buffer))
5765       (error "There is no article buffer for this summary buffer")
5766     (gnus-configure-windows 'article)
5767     (select-window (get-buffer-window gnus-article-buffer))))
5768
5769 (defun gnus-summary-universal-argument (arg)
5770   "Perform any operation on all articles that are process/prefixed."
5771   (interactive "P")
5772   (let ((articles (gnus-summary-work-articles arg))
5773         func article)
5774     (if (eq
5775          (setq
5776           func
5777           (key-binding
5778            (read-key-sequence
5779             (substitute-command-keys
5780              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5781          'undefined)
5782         (gnus-error 1 "Undefined key")
5783       (save-excursion
5784         (while articles
5785           (gnus-summary-goto-subject (setq article (pop articles)))
5786           (let (gnus-newsgroup-processable)
5787             (command-execute func))
5788           (gnus-summary-remove-process-mark article)))))
5789   (gnus-summary-position-point))
5790
5791 (defun gnus-summary-toggle-truncation (&optional arg)
5792   "Toggle truncation of summary lines.
5793 With arg, turn line truncation on iff arg is positive."
5794   (interactive "P")
5795   (setq truncate-lines
5796         (if (null arg) (not truncate-lines)
5797           (> (prefix-numeric-value arg) 0)))
5798   (redraw-display))
5799
5800 (defun gnus-summary-reselect-current-group (&optional all rescan)
5801   "Exit and then reselect the current newsgroup.
5802 The prefix argument ALL means to select all articles."
5803   (interactive "P")
5804   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5805     (error "Ephemeral groups can't be reselected"))
5806   (let ((current-subject (gnus-summary-article-number))
5807         (group gnus-newsgroup-name))
5808     (setq gnus-newsgroup-begin nil)
5809     (gnus-summary-exit)
5810     ;; We have to adjust the point of group mode buffer because
5811     ;; point was moved to the next unread newsgroup by exiting.
5812     (gnus-summary-jump-to-group group)
5813     (when rescan
5814       (save-excursion
5815         (gnus-group-get-new-news-this-group 1)))
5816     (gnus-group-read-group all t)
5817     (gnus-summary-goto-subject current-subject nil t)))
5818
5819 (defun gnus-summary-rescan-group (&optional all)
5820   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5821   (interactive "P")
5822   (gnus-summary-reselect-current-group all t))
5823
5824 (defun gnus-summary-update-info (&optional non-destructive)
5825   (save-excursion
5826     (let ((group gnus-newsgroup-name))
5827       (when group
5828         (when gnus-newsgroup-kill-headers
5829           (setq gnus-newsgroup-killed
5830                 (gnus-compress-sequence
5831                  (nconc
5832                   (gnus-set-sorted-intersection
5833                    (gnus-uncompress-range gnus-newsgroup-killed)
5834                    (setq gnus-newsgroup-unselected
5835                          (sort gnus-newsgroup-unselected '<)))
5836                   (setq gnus-newsgroup-unreads
5837                         (sort gnus-newsgroup-unreads '<)))
5838                  t)))
5839         (unless (listp (cdr gnus-newsgroup-killed))
5840           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5841         (let ((headers gnus-newsgroup-headers))
5842           ;; Set the new ranges of read articles.
5843           (save-excursion
5844             (set-buffer gnus-group-buffer)
5845             (gnus-undo-force-boundary))
5846           (gnus-update-read-articles
5847            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5848           ;; Set the current article marks.
5849           (let ((gnus-newsgroup-scored
5850                  (if (and (not gnus-save-score)
5851                           (not non-destructive))
5852                      nil
5853                    gnus-newsgroup-scored)))
5854             (save-excursion
5855               (gnus-update-marks)))
5856           ;; Do the cross-ref thing.
5857           (when gnus-use-cross-reference
5858             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5859           ;; Do not switch windows but change the buffer to work.
5860           (set-buffer gnus-group-buffer)
5861           (unless (gnus-ephemeral-group-p group)
5862             (gnus-group-update-group group)))))))
5863
5864 (defun gnus-summary-save-newsrc (&optional force)
5865   "Save the current number of read/marked articles in the dribble buffer.
5866 The dribble buffer will then be saved.
5867 If FORCE (the prefix), also save the .newsrc file(s)."
5868   (interactive "P")
5869   (gnus-summary-update-info t)
5870   (if force
5871       (gnus-save-newsrc-file)
5872     (gnus-dribble-save)))
5873
5874 (defun gnus-summary-exit (&optional temporary)
5875   "Exit reading current newsgroup, and then return to group selection mode.
5876 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5877   (interactive)
5878   (gnus-set-global-variables)
5879   (when (gnus-buffer-live-p gnus-article-buffer)
5880     (save-excursion
5881       (set-buffer gnus-article-buffer)
5882       (mm-destroy-parts gnus-article-mime-handles)
5883       ;; Set it to nil for safety reason.
5884       (setq gnus-article-mime-handle-alist nil)
5885       (setq gnus-article-mime-handles nil)))
5886   (gnus-kill-save-kill-buffer)
5887   (gnus-async-halt-prefetch)
5888   (let* ((group gnus-newsgroup-name)
5889          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5890          (mode major-mode)
5891          (group-point nil)
5892          (buf (current-buffer)))
5893     (unless quit-config
5894       ;; Do adaptive scoring, and possibly save score files.
5895       (when gnus-newsgroup-adaptive
5896         (gnus-score-adaptive))
5897       (when gnus-use-scoring
5898         (gnus-score-save)))
5899     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5900     ;; If we have several article buffers, we kill them at exit.
5901     (unless gnus-single-article-buffer
5902       (gnus-kill-buffer gnus-original-article-buffer)
5903       (setq gnus-article-current nil))
5904     (when gnus-use-cache
5905       (gnus-cache-possibly-remove-articles)
5906       (gnus-cache-save-buffers))
5907     (gnus-async-prefetch-remove-group group)
5908     (when gnus-suppress-duplicates
5909       (gnus-dup-enter-articles))
5910     (when gnus-use-trees
5911       (gnus-tree-close group))
5912     (when gnus-use-cache
5913       (gnus-cache-write-active))
5914     ;; Remove entries for this group.
5915     (nnmail-purge-split-history (gnus-group-real-name group))
5916     ;; Make all changes in this group permanent.
5917     (unless quit-config
5918       (gnus-run-hooks 'gnus-exit-group-hook)
5919       (gnus-summary-update-info))
5920     (gnus-close-group group)
5921     ;; Make sure where we were, and go to next newsgroup.
5922     (set-buffer gnus-group-buffer)
5923     (unless quit-config
5924       (gnus-group-jump-to-group group))
5925     (gnus-run-hooks 'gnus-summary-exit-hook)
5926     (unless (or quit-config
5927                 ;; If this group has disappeared from the summary
5928                 ;; buffer, don't skip forwards.
5929                 (not (string= group (gnus-group-group-name))))
5930       (gnus-group-next-unread-group 1))
5931     (setq group-point (point))
5932     (if temporary
5933         nil                             ;Nothing to do.
5934       ;; If we have several article buffers, we kill them at exit.
5935       (unless gnus-single-article-buffer
5936         (gnus-kill-buffer gnus-article-buffer)
5937         (gnus-kill-buffer gnus-original-article-buffer)
5938         (setq gnus-article-current nil))
5939       (set-buffer buf)
5940       (if (not gnus-kill-summary-on-exit)
5941           (progn
5942             (gnus-deaden-summary)
5943             (setq mode nil))
5944        ;; We set all buffer-local variables to nil.  It is unclear why
5945         ;; this is needed, but if we don't, buffer-local variables are
5946         ;; not garbage-collected, it seems.  This would the lead to en
5947         ;; ever-growing Emacs.
5948         (gnus-summary-clear-local-variables)
5949         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5950           (gnus-summary-clear-local-variables))
5951         (when (get-buffer gnus-article-buffer)
5952           (bury-buffer gnus-article-buffer))
5953         ;; We clear the global counterparts of the buffer-local
5954         ;; variables as well, just to be on the safe side.
5955         (set-buffer gnus-group-buffer)
5956         (gnus-summary-clear-local-variables)
5957         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
5958           (gnus-summary-clear-local-variables)))
5959       (setq gnus-current-select-method gnus-select-method)
5960       (pop-to-buffer gnus-group-buffer)
5961       (if (not quit-config)
5962           (progn
5963             (goto-char group-point)
5964             (gnus-configure-windows 'group 'force))
5965         (gnus-handle-ephemeral-exit quit-config))
5966       ;; Return to group mode buffer.
5967       (when (eq mode 'gnus-summary-mode)
5968         (gnus-kill-buffer buf))
5969       ;; Clear the current group name.
5970       (unless quit-config
5971         (setq gnus-newsgroup-name nil)))))
5972
5973 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5974 (defun gnus-summary-exit-no-update (&optional no-questions)
5975   "Quit reading current newsgroup without updating read article info."
5976   (interactive)
5977   (let* ((group gnus-newsgroup-name)
5978          (quit-config (gnus-group-quit-config group)))
5979     (when (or no-questions
5980               gnus-expert-user
5981               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5982       (gnus-async-halt-prefetch)
5983       (mapcar 'funcall
5984               (delq 'gnus-summary-expire-articles
5985                     (copy-sequence gnus-summary-prepare-exit-hook)))
5986       (when (gnus-buffer-live-p gnus-article-buffer)
5987         (save-excursion
5988           (set-buffer gnus-article-buffer)
5989           (mm-destroy-parts gnus-article-mime-handles)
5990           ;; Set it to nil for safety reason.
5991           (setq gnus-article-mime-handle-alist nil)
5992           (setq gnus-article-mime-handles nil)))
5993       ;; If we have several article buffers, we kill them at exit.
5994       (unless gnus-single-article-buffer
5995         (gnus-kill-buffer gnus-article-buffer)
5996         (gnus-kill-buffer gnus-original-article-buffer)
5997         (setq gnus-article-current nil))
5998       (if (not gnus-kill-summary-on-exit)
5999           (gnus-deaden-summary)
6000         (gnus-close-group group)
6001         (gnus-summary-clear-local-variables)
6002         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6003           (gnus-summary-clear-local-variables))
6004         (set-buffer gnus-group-buffer)
6005         (gnus-summary-clear-local-variables)
6006         (let ((gnus-summary-local-variables gnus-newsgroup-variables))
6007           (gnus-summary-clear-local-variables))
6008         (when (get-buffer gnus-summary-buffer)
6009           (kill-buffer gnus-summary-buffer)))
6010       (unless gnus-single-article-buffer
6011         (setq gnus-article-current nil))
6012       (when gnus-use-trees
6013         (gnus-tree-close group))
6014       (gnus-async-prefetch-remove-group group)
6015       (when (get-buffer gnus-article-buffer)
6016         (bury-buffer gnus-article-buffer))
6017       ;; Return to the group buffer.
6018       (gnus-configure-windows 'group 'force)
6019       ;; Clear the current group name.
6020       (setq gnus-newsgroup-name nil)
6021       (when (equal (gnus-group-group-name) group)
6022         (gnus-group-next-unread-group 1))
6023       (when quit-config
6024         (gnus-handle-ephemeral-exit quit-config)))))
6025
6026 (defun gnus-handle-ephemeral-exit (quit-config)
6027   "Handle movement when leaving an ephemeral group.
6028 The state which existed when entering the ephemeral is reset."
6029   (if (not (buffer-name (car quit-config)))
6030       (gnus-configure-windows 'group 'force)
6031     (set-buffer (car quit-config))
6032     (cond ((eq major-mode 'gnus-summary-mode)
6033            (gnus-set-global-variables))
6034           ((eq major-mode 'gnus-article-mode)
6035            (save-excursion
6036              ;; The `gnus-summary-buffer' variable may point
6037              ;; to the old summary buffer when using a single
6038              ;; article buffer.
6039              (unless (gnus-buffer-live-p gnus-summary-buffer)
6040                (set-buffer gnus-group-buffer))
6041              (set-buffer gnus-summary-buffer)
6042              (gnus-set-global-variables))))
6043     (if (or (eq (cdr quit-config) 'article)
6044             (eq (cdr quit-config) 'pick))
6045         (progn
6046           ;; The current article may be from the ephemeral group
6047           ;; thus it is best that we reload this article
6048           (gnus-summary-show-article)
6049           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
6050               (gnus-configure-windows 'pick 'force)
6051             (gnus-configure-windows (cdr quit-config) 'force)))
6052       (gnus-configure-windows (cdr quit-config) 'force))
6053     (when (eq major-mode 'gnus-summary-mode)
6054       (gnus-summary-next-subject 1 nil t)
6055       (gnus-summary-recenter)
6056       (gnus-summary-position-point))))
6057
6058 ;;; Dead summaries.
6059
6060 (defvar gnus-dead-summary-mode-map nil)
6061
6062 (unless gnus-dead-summary-mode-map
6063   (setq gnus-dead-summary-mode-map (make-keymap))
6064   (suppress-keymap gnus-dead-summary-mode-map)
6065   (substitute-key-definition
6066    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
6067   (let ((keys '("\C-d" "\r" "\177" [delete])))
6068     (while keys
6069       (define-key gnus-dead-summary-mode-map
6070         (pop keys) 'gnus-summary-wake-up-the-dead))))
6071
6072 (defvar gnus-dead-summary-mode nil
6073   "Minor mode for Gnus summary buffers.")
6074
6075 (defun gnus-dead-summary-mode (&optional arg)
6076   "Minor mode for Gnus summary buffers."
6077   (interactive "P")
6078   (when (eq major-mode 'gnus-summary-mode)
6079     (make-local-variable 'gnus-dead-summary-mode)
6080     (setq gnus-dead-summary-mode
6081           (if (null arg) (not gnus-dead-summary-mode)
6082             (> (prefix-numeric-value arg) 0)))
6083     (when gnus-dead-summary-mode
6084       (gnus-add-minor-mode
6085        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
6086
6087 (defun gnus-deaden-summary ()
6088   "Make the current summary buffer into a dead summary buffer."
6089   ;; Kill any previous dead summary buffer.
6090   (when (and gnus-dead-summary
6091              (buffer-name gnus-dead-summary))
6092     (save-excursion
6093       (set-buffer gnus-dead-summary)
6094       (when gnus-dead-summary-mode
6095         (kill-buffer (current-buffer)))))
6096   ;; Make this the current dead summary.
6097   (setq gnus-dead-summary (current-buffer))
6098   (gnus-dead-summary-mode 1)
6099   (let ((name (buffer-name)))
6100     (when (string-match "Summary" name)
6101       (rename-buffer
6102        (concat (substring name 0 (match-beginning 0)) "Dead "
6103                (substring name (match-beginning 0)))
6104        t)
6105       (bury-buffer))))
6106
6107 (defun gnus-kill-or-deaden-summary (buffer)
6108   "Kill or deaden the summary BUFFER."
6109   (save-excursion
6110     (when (and (buffer-name buffer)
6111                (not gnus-single-article-buffer))
6112       (save-excursion
6113         (set-buffer buffer)
6114         (gnus-kill-buffer gnus-article-buffer)
6115         (gnus-kill-buffer gnus-original-article-buffer)))
6116     (cond (gnus-kill-summary-on-exit
6117            (when (and gnus-use-trees
6118                       (gnus-buffer-exists-p buffer))
6119              (save-excursion
6120                (set-buffer buffer)
6121                (gnus-tree-close gnus-newsgroup-name)))
6122            (gnus-kill-buffer buffer))
6123           ((gnus-buffer-exists-p buffer)
6124            (save-excursion
6125              (set-buffer buffer)
6126              (gnus-deaden-summary))))))
6127
6128 (defun gnus-summary-wake-up-the-dead (&rest args)
6129   "Wake up the dead summary buffer."
6130   (interactive)
6131   (gnus-dead-summary-mode -1)
6132   (let ((name (buffer-name)))
6133     (when (string-match "Dead " name)
6134       (rename-buffer
6135        (concat (substring name 0 (match-beginning 0))
6136                (substring name (match-end 0)))
6137        t)))
6138   (gnus-message 3 "This dead summary is now alive again"))
6139
6140 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
6141 (defun gnus-summary-fetch-faq (&optional faq-dir)
6142   "Fetch the FAQ for the current group.
6143 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
6144 in."
6145   (interactive
6146    (list
6147     (when current-prefix-arg
6148       (completing-read
6149        "Faq dir: " (and (listp gnus-group-faq-directory)
6150                         (mapcar (lambda (file) (list file))
6151                                 gnus-group-faq-directory))))))
6152   (let (gnus-faq-buffer)
6153     (when (setq gnus-faq-buffer
6154                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
6155       (gnus-configure-windows 'summary-faq))))
6156
6157 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6158 (defun gnus-summary-describe-group (&optional force)
6159   "Describe the current newsgroup."
6160   (interactive "P")
6161   (gnus-group-describe-group force gnus-newsgroup-name))
6162
6163 (defun gnus-summary-describe-briefly ()
6164   "Describe summary mode commands briefly."
6165   (interactive)
6166   (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")))
6167
6168 ;; Walking around group mode buffer from summary mode.
6169
6170 (defun gnus-summary-next-group (&optional no-article target-group backward)
6171   "Exit current newsgroup and then select next unread newsgroup.
6172 If prefix argument NO-ARTICLE is non-nil, no article is selected
6173 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
6174 previous group instead."
6175   (interactive "P")
6176   ;; Stop pre-fetching.
6177   (gnus-async-halt-prefetch)
6178   (let ((current-group gnus-newsgroup-name)
6179         (current-buffer (current-buffer))
6180         entered)
6181    ;; First we semi-exit this group to update Xrefs and all variables.
6182     ;; We can't do a real exit, because the window conf must remain
6183     ;; the same in case the user is prompted for info, and we don't
6184     ;; want the window conf to change before that...
6185     (gnus-summary-exit t)
6186     (while (not entered)
6187       ;; Then we find what group we are supposed to enter.
6188       (set-buffer gnus-group-buffer)
6189       (gnus-group-jump-to-group current-group)
6190       (setq target-group
6191             (or target-group
6192                 (if (eq gnus-keep-same-level 'best)
6193                     (gnus-summary-best-group gnus-newsgroup-name)
6194                   (gnus-summary-search-group backward gnus-keep-same-level))))
6195       (if (not target-group)
6196           ;; There are no further groups, so we return to the group
6197           ;; buffer.
6198           (progn
6199             (gnus-message 5 "Returning to the group buffer")
6200             (setq entered t)
6201             (when (gnus-buffer-live-p current-buffer)
6202               (set-buffer current-buffer)
6203               (gnus-summary-exit))
6204             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
6205         ;; We try to enter the target group.
6206         (gnus-group-jump-to-group target-group)
6207         (let ((unreads (gnus-group-group-unread)))
6208           (if (and (or (eq t unreads)
6209                        (and unreads (not (zerop unreads))))
6210                    (gnus-summary-read-group
6211                     target-group nil no-article
6212                     (and (buffer-name current-buffer) current-buffer)
6213                     nil backward))
6214               (setq entered t)
6215             (setq current-group target-group
6216                   target-group nil)))))))
6217
6218 (defun gnus-summary-prev-group (&optional no-article)
6219   "Exit current newsgroup and then select previous unread newsgroup.
6220 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
6221   (interactive "P")
6222   (gnus-summary-next-group no-article nil t))
6223
6224 ;; Walking around summary lines.
6225
6226 (defun gnus-summary-first-subject (&optional unread undownloaded)
6227   "Go to the first unread subject.
6228 If UNREAD is non-nil, go to the first unread article.
6229 Returns the article selected or nil if there are no unread articles."
6230   (interactive "P")
6231   (prog1
6232       (cond
6233        ;; Empty summary.
6234        ((null gnus-newsgroup-data)
6235         (gnus-message 3 "No articles in the group")
6236         nil)
6237        ;; Pick the first article.
6238        ((not unread)
6239         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
6240         (gnus-data-number (car gnus-newsgroup-data)))
6241        ;; No unread articles.
6242        ((null gnus-newsgroup-unreads)
6243         (gnus-message 3 "No more unread articles")
6244         nil)
6245        ;; Find the first unread article.
6246        (t
6247         (let ((data gnus-newsgroup-data))
6248           (while (and data
6249                       (and (not (and undownloaded
6250                                      (eq gnus-undownloaded-mark
6251                                          (gnus-data-mark (car data)))))
6252                            (not (gnus-data-unread-p (car data)))))
6253             (setq data (cdr data)))
6254           (when data
6255             (goto-char (gnus-data-pos (car data)))
6256             (gnus-data-number (car data))))))
6257     (gnus-summary-position-point)))
6258
6259 (defun gnus-summary-next-subject (n &optional unread dont-display)
6260   "Go to next N'th summary line.
6261 If N is negative, go to the previous N'th subject line.
6262 If UNREAD is non-nil, only unread articles are selected.
6263 The difference between N and the actual number of steps taken is
6264 returned."
6265   (interactive "p")
6266   (let ((backward (< n 0))
6267         (n (abs n)))
6268     (while (and (> n 0)
6269                 (if backward
6270                     (gnus-summary-find-prev unread)
6271                   (gnus-summary-find-next unread)))
6272       (unless (zerop (setq n (1- n)))
6273         (gnus-summary-show-thread)))
6274     (when (/= 0 n)
6275       (gnus-message 7 "No more%s articles"
6276                     (if unread " unread" "")))
6277     (unless dont-display
6278       (gnus-summary-recenter)
6279       (gnus-summary-position-point))
6280     n))
6281
6282 (defun gnus-summary-next-unread-subject (n)
6283   "Go to next N'th unread summary line."
6284   (interactive "p")
6285   (gnus-summary-next-subject n t))
6286
6287 (defun gnus-summary-prev-subject (n &optional unread)
6288   "Go to previous N'th summary line.
6289 If optional argument UNREAD is non-nil, only unread article is selected."
6290   (interactive "p")
6291   (gnus-summary-next-subject (- n) unread))
6292
6293 (defun gnus-summary-prev-unread-subject (n)
6294   "Go to previous N'th unread summary line."
6295   (interactive "p")
6296   (gnus-summary-next-subject (- n) t))
6297
6298 (defun gnus-summary-goto-subject (article &optional force silent)
6299   "Go the subject line of ARTICLE.
6300 If FORCE, also allow jumping to articles not currently shown."
6301   (interactive "nArticle number: ")
6302   (let ((b (point))
6303         (data (gnus-data-find article)))
6304     ;; We read in the article if we have to.
6305     (and (not data)
6306          force
6307          (gnus-summary-insert-subject
6308           article
6309           (if (or (numberp force) (vectorp force)) force)
6310           t)
6311          (setq data (gnus-data-find article)))
6312     (goto-char b)
6313     (if (not data)
6314         (progn
6315           (unless silent
6316             (gnus-message 3 "Can't find article %d" article))
6317           nil)
6318       (let ((pt (gnus-data-pos data)))
6319         (goto-char pt)
6320         (gnus-summary-set-article-display-arrow pt))
6321       (gnus-summary-position-point)
6322       article)))
6323
6324 ;; Walking around summary lines with displaying articles.
6325
6326 (defun gnus-summary-expand-window (&optional arg)
6327   "Make the summary buffer take up the entire Emacs frame.
6328 Given a prefix, will force an `article' buffer configuration."
6329   (interactive "P")
6330   (if arg
6331       (gnus-configure-windows 'article 'force)
6332     (gnus-configure-windows 'summary 'force)))
6333
6334 (defun gnus-summary-display-article (article &optional all-header)
6335   "Display ARTICLE in article buffer."
6336   (when (gnus-buffer-live-p gnus-article-buffer)
6337     (with-current-buffer gnus-article-buffer
6338       (mm-enable-multibyte-mule4)))
6339   (gnus-set-global-variables)
6340   (when (gnus-buffer-live-p gnus-article-buffer)
6341     (with-current-buffer gnus-article-buffer
6342       (setq gnus-article-charset gnus-newsgroup-charset)
6343       (setq gnus-article-ignored-charsets gnus-newsgroup-ignored-charsets)
6344       (mm-enable-multibyte-mule4)))
6345   (if (null article)
6346       nil
6347     (prog1
6348         (if gnus-summary-display-article-function
6349             (funcall gnus-summary-display-article-function article all-header)
6350           (gnus-article-prepare article all-header))
6351       (gnus-run-hooks 'gnus-select-article-hook)
6352       (when (and gnus-current-article
6353                  (not (zerop gnus-current-article)))
6354         (gnus-summary-goto-subject gnus-current-article))
6355       (gnus-summary-recenter)
6356       (when (and gnus-use-trees gnus-show-threads)
6357         (gnus-possibly-generate-tree article)
6358         (gnus-highlight-selected-tree article))
6359       ;; Successfully display article.
6360       (gnus-article-set-window-start
6361        (cdr (assq article gnus-newsgroup-bookmarks))))))
6362
6363 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
6364   "Select the current article.
6365 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
6366 non-nil, the article will be re-fetched even if it already present in
6367 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
6368 be displayed."
6369   ;; Make sure we are in the summary buffer to work around bbdb bug.
6370   (unless (eq major-mode 'gnus-summary-mode)
6371     (set-buffer gnus-summary-buffer))
6372   (let ((article (or article (gnus-summary-article-number)))
6373         (all-headers (not (not all-headers))) ;Must be T or NIL.
6374         gnus-summary-display-article-function)
6375     (and (not pseudo)
6376          (gnus-summary-article-pseudo-p article)
6377          (error "This is a pseudo-article"))
6378     (save-excursion
6379       (set-buffer gnus-summary-buffer)
6380       (if (or (and gnus-single-article-buffer
6381                    (or (null gnus-current-article)
6382                        (null gnus-article-current)
6383                        (null (get-buffer gnus-article-buffer))
6384                        (not (eq article (cdr gnus-article-current)))
6385                        (not (equal (car gnus-article-current)
6386                                    gnus-newsgroup-name))))
6387               (and (not gnus-single-article-buffer)
6388                    (or (null gnus-current-article)
6389                        (not (eq gnus-current-article article))))
6390               force)
6391        ;; The requested article is different from the current article.
6392           (progn
6393             (gnus-summary-display-article article all-headers)
6394             (when (gnus-buffer-live-p gnus-article-buffer)
6395               (with-current-buffer gnus-article-buffer
6396                 (if (not gnus-article-decoded-p) ;; a local variable
6397                     (mm-disable-multibyte-mule4))))
6398             (when (or all-headers gnus-show-all-headers)
6399               (gnus-article-show-all-headers))
6400             (gnus-article-set-window-start
6401              (cdr (assq article gnus-newsgroup-bookmarks)))
6402             article)
6403         (when (or all-headers gnus-show-all-headers)
6404           (gnus-article-show-all-headers))
6405         'old))))
6406
6407 (defun gnus-summary-force-verify-and-decrypt ()
6408   (interactive)
6409   (let ((mm-verify-option 'known)
6410         (mm-decrypt-option 'known))
6411     (gnus-summary-select-article nil 'force)))
6412
6413 (defun gnus-summary-set-current-mark (&optional current-mark)
6414   "Obsolete function."
6415   nil)
6416
6417 (defun gnus-summary-next-article (&optional unread subject backward push)
6418   "Select the next article.
6419 If UNREAD, only unread articles are selected.
6420 If SUBJECT, only articles with SUBJECT are selected.
6421 If BACKWARD, the previous article is selected instead of the next."
6422   (interactive "P")
6423   (cond
6424    ;; Is there such an article?
6425    ((and (gnus-summary-search-forward unread subject backward)
6426          (or (gnus-summary-display-article (gnus-summary-article-number))
6427              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
6428     (gnus-summary-position-point))
6429    ;; If not, we try the first unread, if that is wanted.
6430    ((and subject
6431          gnus-auto-select-same
6432          (gnus-summary-first-unread-article))
6433     (gnus-summary-position-point)
6434     (gnus-message 6 "Wrapped"))
6435    ;; Try to get next/previous article not displayed in this group.
6436    ((and gnus-auto-extend-newsgroup
6437          (not unread) (not subject))
6438     (gnus-summary-goto-article
6439      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
6440      nil (count-lines (point-min) (point))))
6441    ;; Go to next/previous group.
6442    (t
6443     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
6444       (gnus-summary-jump-to-group gnus-newsgroup-name))
6445     (let ((cmd last-command-char)
6446           (point
6447            (save-excursion
6448              (set-buffer gnus-group-buffer)
6449              (point)))
6450           (group
6451            (if (eq gnus-keep-same-level 'best)
6452                (gnus-summary-best-group gnus-newsgroup-name)
6453              (gnus-summary-search-group backward gnus-keep-same-level))))
6454       ;; For some reason, the group window gets selected.  We change
6455       ;; it back.
6456       (select-window (get-buffer-window (current-buffer)))
6457       ;; Select next unread newsgroup automagically.
6458       (cond
6459        ((or (not gnus-auto-select-next)
6460             (not cmd))
6461         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
6462        ((or (eq gnus-auto-select-next 'quietly)
6463             (and (eq gnus-auto-select-next 'slightly-quietly)
6464                  push)
6465             (and (eq gnus-auto-select-next 'almost-quietly)
6466                  (gnus-summary-last-article-p)))
6467         ;; Select quietly.
6468         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
6469             (gnus-summary-exit)
6470           (gnus-message 7 "No more%s articles (%s)..."
6471                         (if unread " unread" "")
6472                         (if group (concat "selecting " group)
6473                           "exiting"))
6474           (gnus-summary-next-group nil group backward)))
6475        (t
6476         (when (gnus-key-press-event-p last-input-event)
6477           (gnus-summary-walk-group-buffer
6478            gnus-newsgroup-name cmd unread backward point))))))))
6479
6480 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6481   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6482                       (?\C-p (gnus-group-prev-unread-group 1))))
6483         (cursor-in-echo-area t)
6484         keve key group ended)
6485     (save-excursion
6486       (set-buffer gnus-group-buffer)
6487       (goto-char start)
6488       (setq group
6489             (if (eq gnus-keep-same-level 'best)
6490                 (gnus-summary-best-group gnus-newsgroup-name)
6491               (gnus-summary-search-group backward gnus-keep-same-level))))
6492     (while (not ended)
6493       (gnus-message
6494        5 "No more%s articles%s" (if unread " unread" "")
6495        (if (and group
6496                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6497            (format " (Type %s for %s [%s])"
6498                    (single-key-description cmd) group
6499                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6500          (format " (Type %s to exit %s)"
6501                  (single-key-description cmd)
6502                  gnus-newsgroup-name)))
6503       ;; Confirm auto selection.
6504       (setq key (car (setq keve (gnus-read-event-char))))
6505       (setq ended t)
6506       (cond
6507        ((assq key keystrokes)
6508         (let ((obuf (current-buffer)))
6509           (switch-to-buffer gnus-group-buffer)
6510           (when group
6511             (gnus-group-jump-to-group group))
6512           (eval (cadr (assq key keystrokes)))
6513           (setq group (gnus-group-group-name))
6514           (switch-to-buffer obuf))
6515         (setq ended nil))
6516        ((equal key cmd)
6517         (if (or (not group)
6518                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6519             (gnus-summary-exit)
6520           (gnus-summary-next-group nil group backward)))
6521        (t
6522         (push (cdr keve) unread-command-events))))))
6523
6524 (defun gnus-summary-next-unread-article ()
6525   "Select unread article after current one."
6526   (interactive)
6527   (gnus-summary-next-article
6528    (or (not (eq gnus-summary-goto-unread 'never))
6529        (gnus-summary-last-article-p (gnus-summary-article-number)))
6530    (and gnus-auto-select-same
6531         (gnus-summary-article-subject))))
6532
6533 (defun gnus-summary-prev-article (&optional unread subject)
6534   "Select the article after the current one.
6535 If UNREAD is non-nil, only unread articles are selected."
6536   (interactive "P")
6537   (gnus-summary-next-article unread subject t))
6538
6539 (defun gnus-summary-prev-unread-article ()
6540   "Select unread article before current one."
6541   (interactive)
6542   (gnus-summary-prev-article
6543    (or (not (eq gnus-summary-goto-unread 'never))
6544        (gnus-summary-first-article-p (gnus-summary-article-number)))
6545    (and gnus-auto-select-same
6546         (gnus-summary-article-subject))))
6547
6548 (defun gnus-summary-next-page (&optional lines circular)
6549   "Show next page of the selected article.
6550 If at the end of the current article, select the next article.
6551 LINES says how many lines should be scrolled up.
6552
6553 If CIRCULAR is non-nil, go to the start of the article instead of
6554 selecting the next article when reaching the end of the current
6555 article."
6556   (interactive "P")
6557   (setq gnus-summary-buffer (current-buffer))
6558   (gnus-set-global-variables)
6559   (let ((article (gnus-summary-article-number))
6560         (article-window (get-buffer-window gnus-article-buffer t))
6561         endp)
6562     ;; If the buffer is empty, we have no article.
6563     (unless article
6564       (error "No article to select"))
6565     (gnus-configure-windows 'article)
6566     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6567         (if (and (eq gnus-summary-goto-unread 'never)
6568                  (not (gnus-summary-last-article-p article)))
6569             (gnus-summary-next-article)
6570           (gnus-summary-next-unread-article))
6571       (if (or (null gnus-current-article)
6572               (null gnus-article-current)
6573               (/= article (cdr gnus-article-current))
6574               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6575           ;; Selected subject is different from current article's.
6576           (gnus-summary-display-article article)
6577         (when article-window
6578           (gnus-eval-in-buffer-window gnus-article-buffer
6579             (setq endp (gnus-article-next-page lines)))
6580           (when endp
6581             (cond (circular
6582                    (gnus-summary-beginning-of-article))
6583                   (lines
6584                    (gnus-message 3 "End of message"))
6585                   ((null lines)
6586                    (if (and (eq gnus-summary-goto-unread 'never)
6587                             (not (gnus-summary-last-article-p article)))
6588                        (gnus-summary-next-article)
6589                      (gnus-summary-next-unread-article))))))))
6590     (gnus-summary-recenter)
6591     (gnus-summary-position-point)))
6592
6593 (defun gnus-summary-prev-page (&optional lines move)
6594   "Show previous page of selected article.
6595 Argument LINES specifies lines to be scrolled down.
6596 If MOVE, move to the previous unread article if point is at
6597 the beginning of the buffer."
6598   (interactive "P")
6599   (let ((article (gnus-summary-article-number))
6600         (article-window (get-buffer-window gnus-article-buffer t))
6601         endp)
6602     (gnus-configure-windows 'article)
6603     (if (or (null gnus-current-article)
6604             (null gnus-article-current)
6605             (/= article (cdr gnus-article-current))
6606             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6607         ;; Selected subject is different from current article's.
6608         (gnus-summary-display-article article)
6609       (gnus-summary-recenter)
6610       (when article-window
6611         (gnus-eval-in-buffer-window gnus-article-buffer
6612           (setq endp (gnus-article-prev-page lines)))
6613         (when (and move endp)
6614           (cond (lines
6615                  (gnus-message 3 "Beginning of message"))
6616                 ((null lines)
6617                  (if (and (eq gnus-summary-goto-unread 'never)
6618                           (not (gnus-summary-first-article-p article)))
6619                      (gnus-summary-prev-article)
6620                    (gnus-summary-prev-unread-article))))))))
6621   (gnus-summary-position-point))
6622
6623 (defun gnus-summary-prev-page-or-article (&optional lines)
6624   "Show previous page of selected article.
6625 Argument LINES specifies lines to be scrolled down.
6626 If at the beginning of the article, go to the next article."
6627   (interactive "P")
6628   (gnus-summary-prev-page lines t))
6629
6630 (defun gnus-summary-scroll-up (lines)
6631   "Scroll up (or down) one line current article.
6632 Argument LINES specifies lines to be scrolled up (or down if negative)."
6633   (interactive "p")
6634   (gnus-configure-windows 'article)
6635   (gnus-summary-show-thread)
6636   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6637     (gnus-eval-in-buffer-window gnus-article-buffer
6638       (cond ((> lines 0)
6639              (when (gnus-article-next-page lines)
6640                (gnus-message 3 "End of message")))
6641             ((< lines 0)
6642              (gnus-article-prev-page (- lines))))))
6643   (gnus-summary-recenter)
6644   (gnus-summary-position-point))
6645
6646 (defun gnus-summary-scroll-down (lines)
6647   "Scroll down (or up) one line current article.
6648 Argument LINES specifies lines to be scrolled down (or up if negative)."
6649   (interactive "p")
6650   (gnus-summary-scroll-up (- lines)))
6651
6652 (defun gnus-summary-next-same-subject ()
6653   "Select next article which has the same subject as current one."
6654   (interactive)
6655   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6656
6657 (defun gnus-summary-prev-same-subject ()
6658   "Select previous article which has the same subject as current one."
6659   (interactive)
6660   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6661
6662 (defun gnus-summary-next-unread-same-subject ()
6663   "Select next unread article which has the same subject as current one."
6664   (interactive)
6665   (gnus-summary-next-article t (gnus-summary-article-subject)))
6666
6667 (defun gnus-summary-prev-unread-same-subject ()
6668   "Select previous unread article which has the same subject as current one."
6669   (interactive)
6670   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6671
6672 (defun gnus-summary-first-unread-article ()
6673   "Select the first unread article.
6674 Return nil if there are no unread articles."
6675   (interactive)
6676   (prog1
6677       (when (gnus-summary-first-subject t)
6678         (gnus-summary-show-thread)
6679         (gnus-summary-first-subject t)
6680         (gnus-summary-display-article (gnus-summary-article-number)))
6681     (gnus-summary-position-point)))
6682
6683 (defun gnus-summary-first-unread-subject ()
6684   "Place the point on the subject line of the first unread article.
6685 Return nil if there are no unread articles."
6686   (interactive)
6687   (prog1
6688       (when (gnus-summary-first-subject t)
6689         (gnus-summary-show-thread)
6690         (gnus-summary-first-subject t))
6691     (gnus-summary-position-point)))
6692
6693 (defun gnus-summary-first-article ()
6694   "Select the first article.
6695 Return nil if there are no articles."
6696   (interactive)
6697   (prog1
6698       (when (gnus-summary-first-subject)
6699         (gnus-summary-show-thread)
6700         (gnus-summary-first-subject)
6701         (gnus-summary-display-article (gnus-summary-article-number)))
6702     (gnus-summary-position-point)))
6703
6704 (defun gnus-summary-best-unread-article ()
6705   "Select the unread article with the highest score."
6706   (interactive)
6707   (let ((best -1000000)
6708         (data gnus-newsgroup-data)
6709         article score)
6710     (while data
6711       (and (gnus-data-unread-p (car data))
6712            (> (setq score
6713                     (gnus-summary-article-score (gnus-data-number (car data))))
6714               best)
6715            (setq best score
6716                  article (gnus-data-number (car data))))
6717       (setq data (cdr data)))
6718     (prog1
6719         (if article
6720             (gnus-summary-goto-article article)
6721           (error "No unread articles"))
6722       (gnus-summary-position-point))))
6723
6724 (defun gnus-summary-last-subject ()
6725   "Go to the last displayed subject line in the group."
6726   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6727     (when article
6728       (gnus-summary-goto-subject article))))
6729
6730 (defun gnus-summary-goto-article (article &optional all-headers force)
6731   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6732 If ALL-HEADERS is non-nil, no header lines are hidden.
6733 If FORCE, go to the article even if it isn't displayed.  If FORCE
6734 is a number, it is the line the article is to be displayed on."
6735   (interactive
6736    (list
6737     (completing-read
6738      "Article number or Message-ID: "
6739      (mapcar (lambda (number) (list (int-to-string number)))
6740              gnus-newsgroup-limit))
6741     current-prefix-arg
6742     t))
6743   (prog1
6744       (if (and (stringp article)
6745                (string-match "@" article))
6746           (gnus-summary-refer-article article)
6747         (when (stringp article)
6748           (setq article (string-to-number article)))
6749         (if (gnus-summary-goto-subject article force)
6750             (gnus-summary-display-article article all-headers)
6751           (gnus-message 4 "Couldn't go to article %s" article) nil))
6752     (gnus-summary-position-point)))
6753
6754 (defun gnus-summary-goto-last-article ()
6755   "Go to the previously read article."
6756   (interactive)
6757   (prog1
6758       (when gnus-last-article
6759         (gnus-summary-goto-article gnus-last-article nil t))
6760     (gnus-summary-position-point)))
6761
6762 (defun gnus-summary-pop-article (number)
6763   "Pop one article off the history and go to the previous.
6764 NUMBER articles will be popped off."
6765   (interactive "p")
6766   (let (to)
6767     (setq gnus-newsgroup-history
6768           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6769     (if to
6770         (gnus-summary-goto-article (car to) nil t)
6771       (error "Article history empty")))
6772   (gnus-summary-position-point))
6773
6774 ;; Summary commands and functions for limiting the summary buffer.
6775
6776 (defun gnus-summary-limit-to-articles (n)
6777   "Limit the summary buffer to the next N articles.
6778 If not given a prefix, use the process marked articles instead."
6779   (interactive "P")
6780   (prog1
6781       (let ((articles (gnus-summary-work-articles n)))
6782         (setq gnus-newsgroup-processable nil)
6783         (gnus-summary-limit articles))
6784     (gnus-summary-position-point)))
6785
6786 (defun gnus-summary-pop-limit (&optional total)
6787   "Restore the previous limit.
6788 If given a prefix, remove all limits."
6789   (interactive "P")
6790   (when total
6791     (setq gnus-newsgroup-limits
6792           (list (mapcar (lambda (h) (mail-header-number h))
6793                         gnus-newsgroup-headers))))
6794   (unless gnus-newsgroup-limits
6795     (error "No limit to pop"))
6796   (prog1
6797       (gnus-summary-limit nil 'pop)
6798     (gnus-summary-position-point)))
6799
6800 (defun gnus-summary-limit-to-subject (subject &optional header)
6801   "Limit the summary buffer to articles that have subjects that match a regexp."
6802   (interactive "sLimit to subject (regexp): ")
6803   (unless header
6804     (setq header "subject"))
6805   (when (not (equal "" subject))
6806     (prog1
6807         (let ((articles (gnus-summary-find-matching
6808                          (or header "subject") subject 'all)))
6809           (unless articles
6810             (error "Found no matches for \"%s\"" subject))
6811           (gnus-summary-limit articles))
6812       (gnus-summary-position-point))))
6813
6814 (defun gnus-summary-limit-to-author (from)
6815   "Limit the summary buffer to articles that have authors that match a regexp."
6816   (interactive "sLimit to author (regexp): ")
6817   (gnus-summary-limit-to-subject from "from"))
6818
6819 (defun gnus-summary-limit-to-age (age &optional younger-p)
6820   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6821 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6822 articles that are younger than AGE days."
6823   (interactive
6824    (let ((younger current-prefix-arg)
6825          (days-got nil)
6826          days)
6827      (while (not days-got)
6828        (setq days (if younger
6829                       (read-string "Limit to articles within (in days): ")
6830                     (read-string "Limit to articles older than (in days): ")))
6831        (when (> (length days) 0)
6832          (setq days (read days)))
6833        (if (numberp days)
6834            (setq days-got t)
6835          (message "Please enter a number.")
6836          (sleep-for 1)))
6837      (list days younger)))
6838   (prog1
6839       (let ((data gnus-newsgroup-data)
6840             (cutoff (days-to-time age))
6841             articles d date is-younger)
6842         (while (setq d (pop data))
6843           (when (and (vectorp (gnus-data-header d))
6844                      (setq date (mail-header-date (gnus-data-header d))))
6845             (setq is-younger (time-less-p
6846                               (time-since (condition-case ()
6847                                               (date-to-time date)
6848                                             (error '(0 0))))
6849                               cutoff))
6850             (when (if younger-p
6851                       is-younger
6852                     (not is-younger))
6853               (push (gnus-data-number d) articles))))
6854         (gnus-summary-limit (nreverse articles)))
6855     (gnus-summary-position-point)))
6856
6857 (defun gnus-summary-limit-to-extra (header regexp)
6858   "Limit the summary buffer to articles that match an 'extra' header."
6859   (interactive
6860    (let ((header
6861           (intern
6862            (gnus-completing-read
6863             (symbol-name (car gnus-extra-headers))
6864             "Limit extra header:"
6865             (mapcar (lambda (x)
6866                       (cons (symbol-name x) x))
6867                     gnus-extra-headers)
6868             nil
6869             t))))
6870      (list header
6871            (read-string (format "Limit to header %s (regexp): " header)))))
6872   (when (not (equal "" regexp))
6873     (prog1
6874         (let ((articles (gnus-summary-find-matching
6875                          (cons 'extra header) regexp 'all)))
6876           (unless articles
6877             (error "Found no matches for \"%s\"" regexp))
6878           (gnus-summary-limit articles))
6879       (gnus-summary-position-point))))
6880
6881 (defun gnus-summary-limit-to-display-predicate ()
6882   "Limit the summary buffer to the predicated in the `display' group parameter."
6883   (interactive)
6884   (unless gnus-newsgroup-display
6885     (error "There is no `diplay' group parameter"))
6886   (let (articles)
6887     (dolist (number gnus-newsgroup-articles)
6888       (when (funcall gnus-newsgroup-display)
6889         (push number articles)))
6890     (gnus-summary-limit articles))
6891   (gnus-summary-position-point))
6892
6893 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6894 (make-obsolete
6895  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6896
6897 (defun gnus-summary-limit-to-unread (&optional all)
6898   "Limit the summary buffer to articles that are not marked as read.
6899 If ALL is non-nil, limit strictly to unread articles."
6900   (interactive "P")
6901   (if all
6902       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6903     (gnus-summary-limit-to-marks
6904      ;; Concat all the marks that say that an article is read and have
6905      ;; those removed.
6906      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6907            gnus-killed-mark gnus-kill-file-mark
6908            gnus-low-score-mark gnus-expirable-mark
6909            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6910            gnus-duplicate-mark gnus-souped-mark)
6911      'reverse)))
6912
6913 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6914 (make-obsolete 'gnus-summary-delete-marked-with
6915                'gnus-summary-limit-exlude-marks)
6916
6917 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6918   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6919 If REVERSE, limit the summary buffer to articles that are marked
6920 with MARKS.  MARKS can either be a string of marks or a list of marks.
6921 Returns how many articles were removed."
6922   (interactive "sMarks: ")
6923   (gnus-summary-limit-to-marks marks t))
6924
6925 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6926   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6927 If REVERSE (the prefix), limit the summary buffer to articles that are
6928 not marked with MARKS.  MARKS can either be a string of marks or a
6929 list of marks.
6930 Returns how many articles were removed."
6931   (interactive "sMarks: \nP")
6932   (prog1
6933       (let ((data gnus-newsgroup-data)
6934             (marks (if (listp marks) marks
6935                      (append marks nil))) ; Transform to list.
6936             articles)
6937         (while data
6938           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6939                   (memq (gnus-data-mark (car data)) marks))
6940             (push (gnus-data-number (car data)) articles))
6941           (setq data (cdr data)))
6942         (gnus-summary-limit articles))
6943     (gnus-summary-position-point)))
6944
6945 (defun gnus-summary-limit-to-score (&optional score)
6946   "Limit to articles with score at or above SCORE."
6947   (interactive "P")
6948   (setq score (if score
6949                   (prefix-numeric-value score)
6950                 (or gnus-summary-default-score 0)))
6951   (let ((data gnus-newsgroup-data)
6952         articles)
6953     (while data
6954       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6955                 score)
6956         (push (gnus-data-number (car data)) articles))
6957       (setq data (cdr data)))
6958     (prog1
6959         (gnus-summary-limit articles)
6960       (gnus-summary-position-point))))
6961
6962 (defun gnus-summary-limit-include-thread (id)
6963   "Display all the hidden articles that is in the thread with ID in it.
6964 When called interactively, ID is the Message-ID of the current
6965 article."
6966   (interactive (list (mail-header-id (gnus-summary-article-header))))
6967   (let ((articles (gnus-articles-in-thread
6968                    (gnus-id-to-thread (gnus-root-id id)))))
6969     (prog1
6970         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6971       (gnus-summary-limit-include-matching-articles
6972        "subject"
6973        (regexp-quote (gnus-simplify-subject-re
6974                       (mail-header-subject (gnus-id-to-header id)))))
6975       (gnus-summary-position-point))))
6976
6977 (defun gnus-summary-limit-include-matching-articles (header regexp)
6978   "Display all the hidden articles that have HEADERs that match REGEXP."
6979   (interactive (list (read-string "Match on header: ")
6980                      (read-string "Regexp: ")))
6981   (let ((articles (gnus-find-matching-articles header regexp)))
6982     (prog1
6983         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6984       (gnus-summary-position-point))))
6985
6986 (defun gnus-summary-limit-include-dormant ()
6987   "Display all the hidden articles that are marked as dormant.
6988 Note that this command only works on a subset of the articles currently
6989 fetched for this group."
6990   (interactive)
6991   (unless gnus-newsgroup-dormant
6992     (error "There are no dormant articles in this group"))
6993   (prog1
6994       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6995     (gnus-summary-position-point)))
6996
6997 (defun gnus-summary-limit-exclude-dormant ()
6998   "Hide all dormant articles."
6999   (interactive)
7000   (prog1
7001       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
7002     (gnus-summary-position-point)))
7003
7004 (defun gnus-summary-limit-exclude-childless-dormant ()
7005   "Hide all dormant articles that have no children."
7006   (interactive)
7007   (let ((data (gnus-data-list t))
7008         articles d children)
7009     ;; Find all articles that are either not dormant or have
7010     ;; children.
7011     (while (setq d (pop data))
7012       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
7013                 (and (setq children
7014                            (gnus-article-children (gnus-data-number d)))
7015                      (let (found)
7016                        (while children
7017                          (when (memq (car children) articles)
7018                            (setq children nil
7019                                  found t))
7020                          (pop children))
7021                        found)))
7022         (push (gnus-data-number d) articles)))
7023     ;; Do the limiting.
7024     (prog1
7025         (gnus-summary-limit articles)
7026       (gnus-summary-position-point))))
7027
7028 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
7029   "Mark all unread excluded articles as read.
7030 If ALL, mark even excluded ticked and dormants as read."
7031   (interactive "P")
7032   (let ((articles (gnus-sorted-complement
7033                    (sort
7034                     (mapcar (lambda (h) (mail-header-number h))
7035                             gnus-newsgroup-headers)
7036                     '<)
7037                    (sort gnus-newsgroup-limit '<)))
7038         article)
7039     (setq gnus-newsgroup-unreads
7040           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
7041     (if all
7042         (setq gnus-newsgroup-dormant nil
7043               gnus-newsgroup-marked nil
7044               gnus-newsgroup-reads
7045               (nconc
7046                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
7047                gnus-newsgroup-reads))
7048       (while (setq article (pop articles))
7049         (unless (or (memq article gnus-newsgroup-dormant)
7050                     (memq article gnus-newsgroup-marked))
7051           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
7052
7053 (defun gnus-summary-limit (articles &optional pop)
7054   (if pop
7055       ;; We pop the previous limit off the stack and use that.
7056       (setq articles (car gnus-newsgroup-limits)
7057             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
7058     ;; We use the new limit, so we push the old limit on the stack.
7059     (push gnus-newsgroup-limit gnus-newsgroup-limits))
7060   ;; Set the limit.
7061   (setq gnus-newsgroup-limit articles)
7062   (let ((total (length gnus-newsgroup-data))
7063         (data (gnus-data-find-list (gnus-summary-article-number)))
7064         (gnus-summary-mark-below nil)   ; Inhibit this.
7065         found)
7066     ;; This will do all the work of generating the new summary buffer
7067     ;; according to the new limit.
7068     (gnus-summary-prepare)
7069     ;; Hide any threads, possibly.
7070     (and gnus-show-threads
7071          gnus-thread-hide-subtree
7072          (gnus-summary-hide-all-threads))
7073     ;; Try to return to the article you were at, or one in the
7074     ;; neighborhood.
7075     (when data
7076       ;; We try to find some article after the current one.
7077       (while data
7078         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
7079           (setq data nil
7080                 found t))
7081         (setq data (cdr data))))
7082     (unless found
7083       ;; If there is no data, that means that we were after the last
7084       ;; article.  The same goes when we can't find any articles
7085       ;; after the current one.
7086       (goto-char (point-max))
7087       (gnus-summary-find-prev))
7088     (gnus-set-mode-line 'summary)
7089     ;; We return how many articles were removed from the summary
7090     ;; buffer as a result of the new limit.
7091     (- total (length gnus-newsgroup-data))))
7092
7093 (defsubst gnus-invisible-cut-children (threads)
7094   (let ((num 0))
7095     (while threads
7096       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
7097         (incf num))
7098       (pop threads))
7099     (< num 2)))
7100
7101 (defsubst gnus-cut-thread (thread)
7102   "Go forwards in the thread until we find an article that we want to display."
7103   (when (or (eq gnus-fetch-old-headers 'some)
7104             (eq gnus-fetch-old-headers 'invisible)
7105             (numberp gnus-fetch-old-headers)
7106             (eq gnus-build-sparse-threads 'some)
7107             (eq gnus-build-sparse-threads 'more))
7108     ;; Deal with old-fetched headers and sparse threads.
7109     (while (and
7110             thread
7111             (or
7112              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
7113              (gnus-summary-article-ancient-p
7114               (mail-header-number (car thread))))
7115             (if (or (<= (length (cdr thread)) 1)
7116                     (eq gnus-fetch-old-headers 'invisible))
7117                 (setq gnus-newsgroup-limit
7118                       (delq (mail-header-number (car thread))
7119                             gnus-newsgroup-limit)
7120                       thread (cadr thread))
7121               (when (gnus-invisible-cut-children (cdr thread))
7122                 (let ((th (cdr thread)))
7123                   (while th
7124                     (if (memq (mail-header-number (caar th))
7125                               gnus-newsgroup-limit)
7126                         (setq thread (car th)
7127                               th nil)
7128                       (setq th (cdr th))))))))))
7129   thread)
7130
7131 (defun gnus-cut-threads (threads)
7132   "Cut off all uninteresting articles from the beginning of threads."
7133   (when (or (eq gnus-fetch-old-headers 'some)
7134             (eq gnus-fetch-old-headers 'invisible)
7135             (numberp gnus-fetch-old-headers)
7136             (eq gnus-build-sparse-threads 'some)
7137             (eq gnus-build-sparse-threads 'more))
7138     (let ((th threads))
7139       (while th
7140         (setcar th (gnus-cut-thread (car th)))
7141         (setq th (cdr th)))))
7142   ;; Remove nixed out threads.
7143   (delq nil threads))
7144
7145 (defun gnus-summary-initial-limit (&optional show-if-empty)
7146   "Figure out what the initial limit is supposed to be on group entry.
7147 This entails weeding out unwanted dormants, low-scored articles,
7148 fetch-old-headers verbiage, and so on."
7149   ;; Most groups have nothing to remove.
7150   (if (or gnus-inhibit-limiting
7151           (and (null gnus-newsgroup-dormant)
7152                (eq gnus-newsgroup-display 'gnus-not-ignore)
7153                (not (eq gnus-fetch-old-headers 'some))
7154                (not (numberp gnus-fetch-old-headers))
7155                (not (eq gnus-fetch-old-headers 'invisible))
7156                (null gnus-summary-expunge-below)
7157                (not (eq gnus-build-sparse-threads 'some))
7158                (not (eq gnus-build-sparse-threads 'more))
7159                (null gnus-thread-expunge-below)
7160                (not gnus-use-nocem)))
7161       ()                                ; Do nothing.
7162     (push gnus-newsgroup-limit gnus-newsgroup-limits)
7163     (setq gnus-newsgroup-limit nil)
7164     (mapatoms
7165      (lambda (node)
7166        (unless (car (symbol-value node))
7167          ;; These threads have no parents -- they are roots.
7168          (let ((nodes (cdr (symbol-value node)))
7169                thread)
7170            (while nodes
7171              (if (and gnus-thread-expunge-below
7172                       (< (gnus-thread-total-score (car nodes))
7173                          gnus-thread-expunge-below))
7174                  (gnus-expunge-thread (pop nodes))
7175                (setq thread (pop nodes))
7176                (gnus-summary-limit-children thread))))))
7177      gnus-newsgroup-dependencies)
7178     ;; If this limitation resulted in an empty group, we might
7179     ;; pop the previous limit and use it instead.
7180     (when (and (not gnus-newsgroup-limit)
7181                show-if-empty)
7182       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
7183     gnus-newsgroup-limit))
7184
7185 (defun gnus-summary-limit-children (thread)
7186   "Return 1 if this subthread is visible and 0 if it is not."
7187   ;; First we get the number of visible children to this thread.  This
7188   ;; is done by recursing down the thread using this function, so this
7189   ;; will really go down to a leaf article first, before slowly
7190   ;; working its way up towards the root.
7191   (when thread
7192     (let ((children
7193            (if (cdr thread)
7194                (apply '+ (mapcar 'gnus-summary-limit-children
7195                                  (cdr thread)))
7196              0))
7197           (number (mail-header-number (car thread)))
7198           score)
7199       (if (and
7200            (not (memq number gnus-newsgroup-marked))
7201            (or
7202             ;; If this article is dormant and has absolutely no visible
7203             ;; children, then this article isn't visible.
7204             (and (memq number gnus-newsgroup-dormant)
7205                  (zerop children))
7206             ;; If this is "fetch-old-headered" and there is no
7207             ;; visible children, then we don't want this article.
7208             (and (or (eq gnus-fetch-old-headers 'some)
7209                      (numberp gnus-fetch-old-headers))
7210                  (gnus-summary-article-ancient-p number)
7211                  (zerop children))
7212             ;; If this is "fetch-old-headered" and `invisible', then
7213             ;; we don't want this article.
7214             (and (eq gnus-fetch-old-headers 'invisible)
7215                  (gnus-summary-article-ancient-p number))
7216             ;; If this is a sparsely inserted article with no children,
7217             ;; we don't want it.
7218             (and (eq gnus-build-sparse-threads 'some)
7219                  (gnus-summary-article-sparse-p number)
7220                  (zerop children))
7221             ;; If we use expunging, and this article is really
7222             ;; low-scored, then we don't want this article.
7223             (when (and gnus-summary-expunge-below
7224                        (< (setq score
7225                                 (or (cdr (assq number gnus-newsgroup-scored))
7226                                     gnus-summary-default-score))
7227                           gnus-summary-expunge-below))
7228               ;; We increase the expunge-tally here, but that has
7229               ;; nothing to do with the limits, really.
7230               (incf gnus-newsgroup-expunged-tally)
7231               ;; We also mark as read here, if that's wanted.
7232               (when (and gnus-summary-mark-below
7233                          (< score gnus-summary-mark-below))
7234                 (setq gnus-newsgroup-unreads
7235                       (delq number gnus-newsgroup-unreads))
7236                 (if gnus-newsgroup-auto-expire
7237                     (push number gnus-newsgroup-expirable)
7238                   (push (cons number gnus-low-score-mark)
7239                         gnus-newsgroup-reads)))
7240               t)
7241             ;; Do the `display' group parameter.
7242             (and gnus-newsgroup-display
7243                  (not (funcall gnus-newsgroup-display)))
7244             ;; Check NoCeM things.
7245             (if (and gnus-use-nocem
7246                      (gnus-nocem-unwanted-article-p
7247                       (mail-header-id (car thread))))
7248                 (progn
7249                   (setq gnus-newsgroup-unreads
7250                         (delq number gnus-newsgroup-unreads))
7251                   t))))
7252           ;; Nope, invisible article.
7253           0
7254         ;; Ok, this article is to be visible, so we add it to the limit
7255         ;; and return 1.
7256         (push number gnus-newsgroup-limit)
7257         1))))
7258
7259 (defun gnus-expunge-thread (thread)
7260   "Mark all articles in THREAD as read."
7261   (let* ((number (mail-header-number (car thread))))
7262     (incf gnus-newsgroup-expunged-tally)
7263     ;; We also mark as read here, if that's wanted.
7264     (setq gnus-newsgroup-unreads
7265           (delq number gnus-newsgroup-unreads))
7266     (if gnus-newsgroup-auto-expire
7267         (push number gnus-newsgroup-expirable)
7268       (push (cons number gnus-low-score-mark)
7269             gnus-newsgroup-reads)))
7270   ;; Go recursively through all subthreads.
7271   (mapcar 'gnus-expunge-thread (cdr thread)))
7272
7273 ;; Summary article oriented commands
7274
7275 (defun gnus-summary-refer-parent-article (n)
7276   "Refer parent article N times.
7277 If N is negative, go to ancestor -N instead.
7278 The difference between N and the number of articles fetched is returned."
7279   (interactive "p")
7280   (let ((skip 1)
7281         error header ref)
7282     (when (not (natnump n))
7283       (setq skip (abs n)
7284             n 1))
7285     (while (and (> n 0)
7286                 (not error))
7287       (setq header (gnus-summary-article-header))
7288       (if (and (eq (mail-header-number header)
7289                    (cdr gnus-article-current))
7290                (equal gnus-newsgroup-name
7291                       (car gnus-article-current)))
7292           ;; If we try to find the parent of the currently
7293           ;; displayed article, then we take a look at the actual
7294           ;; References header, since this is slightly more
7295           ;; reliable than the References field we got from the
7296           ;; server.
7297           (save-excursion
7298             (set-buffer gnus-original-article-buffer)
7299             (nnheader-narrow-to-headers)
7300             (unless (setq ref (message-fetch-field "references"))
7301               (setq ref (message-fetch-field "in-reply-to")))
7302             (widen))
7303         (setq ref
7304               ;; It's not the current article, so we take a bet on
7305               ;; the value we got from the server.
7306               (mail-header-references header)))
7307       (if (and ref
7308                (not (equal ref "")))
7309           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
7310             (gnus-message 1 "Couldn't find parent"))
7311         (gnus-message 1 "No references in article %d"
7312                       (gnus-summary-article-number))
7313         (setq error t))
7314       (decf n))
7315     (gnus-summary-position-point)
7316     n))
7317
7318 (defun gnus-summary-refer-references ()
7319   "Fetch all articles mentioned in the References header.
7320 Return the number of articles fetched."
7321   (interactive)
7322   (let ((ref (mail-header-references (gnus-summary-article-header)))
7323         (current (gnus-summary-article-number))
7324         (n 0))
7325     (if (or (not ref)
7326             (equal ref ""))
7327         (error "No References in the current article")
7328       ;; For each Message-ID in the References header...
7329       (while (string-match "<[^>]*>" ref)
7330         (incf n)
7331         ;; ... fetch that article.
7332         (gnus-summary-refer-article
7333          (prog1 (match-string 0 ref)
7334            (setq ref (substring ref (match-end 0))))))
7335       (gnus-summary-goto-subject current)
7336       (gnus-summary-position-point)
7337       n)))
7338
7339 (defun gnus-summary-refer-thread (&optional limit)
7340   "Fetch all articles in the current thread.
7341 If LIMIT (the numerical prefix), fetch that many old headers instead
7342 of what's specified by the `gnus-refer-thread-limit' variable."
7343   (interactive "P")
7344   (let ((id (mail-header-id (gnus-summary-article-header)))
7345         (limit (if limit (prefix-numeric-value limit)
7346                  gnus-refer-thread-limit)))
7347     ;; We want to fetch LIMIT *old* headers, but we also have to
7348     ;; re-fetch all the headers in the current buffer, because many of
7349     ;; them may be undisplayed.  So we adjust LIMIT.
7350     (when (numberp limit)
7351       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
7352     (unless (eq gnus-fetch-old-headers 'invisible)
7353       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
7354       ;; Retrieve the headers and read them in.
7355       (if (eq (gnus-retrieve-headers
7356                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
7357               'nov)
7358           (gnus-build-all-threads)
7359         (error "Can't fetch thread from backends that don't support NOV"))
7360       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
7361     (gnus-summary-limit-include-thread id)))
7362
7363 (defun gnus-summary-refer-article (message-id)
7364   "Fetch an article specified by MESSAGE-ID."
7365   (interactive "sMessage-ID: ")
7366   (when (and (stringp message-id)
7367              (not (zerop (length message-id))))
7368     ;; Construct the correct Message-ID if necessary.
7369     ;; Suggested by tale@pawl.rpi.edu.
7370     (unless (string-match "^<" message-id)
7371       (setq message-id (concat "<" message-id)))
7372     (unless (string-match ">$" message-id)
7373       (setq message-id (concat message-id ">")))
7374     (let* ((header (gnus-id-to-header message-id))
7375            (sparse (and header
7376                         (gnus-summary-article-sparse-p
7377                          (mail-header-number header))
7378                         (memq (mail-header-number header)
7379                               gnus-newsgroup-limit)))
7380            number)
7381       (cond
7382        ;; If the article is present in the buffer we just go to it.
7383        ((and header
7384              (or (not (gnus-summary-article-sparse-p
7385                        (mail-header-number header)))
7386                  sparse))
7387         (prog1
7388             (gnus-summary-goto-article
7389              (mail-header-number header) nil t)
7390           (when sparse
7391             (gnus-summary-update-article (mail-header-number header)))))
7392        (t
7393         ;; We fetch the article.
7394         (catch 'found
7395           (dolist (gnus-override-method (gnus-refer-article-methods))
7396             (gnus-check-server gnus-override-method)
7397             ;; Fetch the header, and display the article.
7398             (when (setq number (gnus-summary-insert-subject message-id))
7399               (gnus-summary-select-article nil nil nil number)
7400               (throw 'found t)))
7401           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
7402
7403 (defun gnus-refer-article-methods ()
7404   "Return a list of referrable methods."
7405   (cond
7406    ;; No method, so we default to current and native.
7407    ((null gnus-refer-article-method)
7408     (list gnus-current-select-method gnus-select-method))
7409    ;; Current.
7410    ((eq 'current gnus-refer-article-method)
7411     (list gnus-current-select-method))
7412    ;; List of select methods.
7413    ((not (and (symbolp (car gnus-refer-article-method))
7414               (assq (car gnus-refer-article-method) nnoo-definition-alist)))
7415     (let (out)
7416       (dolist (method gnus-refer-article-method)
7417         (push (if (eq 'current method)
7418                   gnus-current-select-method
7419                 method)
7420               out))
7421       (nreverse out)))
7422    ;; One single select method.
7423    (t
7424     (list gnus-refer-article-method))))
7425
7426 (defun gnus-summary-edit-parameters ()
7427   "Edit the group parameters of the current group."
7428   (interactive)
7429   (gnus-group-edit-group gnus-newsgroup-name 'params))
7430
7431 (defun gnus-summary-customize-parameters ()
7432   "Customize the group parameters of the current group."
7433   (interactive)
7434   (gnus-group-customize gnus-newsgroup-name))
7435
7436 (defun gnus-summary-enter-digest-group (&optional force)
7437   "Enter an nndoc group based on the current article.
7438 If FORCE, force a digest interpretation.  If not, try
7439 to guess what the document format is."
7440   (interactive "P")
7441   (let ((conf gnus-current-window-configuration))
7442     (save-excursion
7443       (gnus-summary-select-article))
7444     (setq gnus-current-window-configuration conf)
7445     (let* ((name (format "%s-%d"
7446                          (gnus-group-prefixed-name
7447                           gnus-newsgroup-name (list 'nndoc ""))
7448                          (save-excursion
7449                            (set-buffer gnus-summary-buffer)
7450                            gnus-current-article)))
7451            (ogroup gnus-newsgroup-name)
7452            (params (append (gnus-info-params (gnus-get-info ogroup))
7453                            (list (cons 'to-group ogroup))
7454                            (list (cons 'save-article-group ogroup))))
7455            (case-fold-search t)
7456            (buf (current-buffer))
7457            dig to-address)
7458       (save-excursion
7459         (set-buffer gnus-original-article-buffer)
7460         ;; Have the digest group inherit the main mail address of
7461         ;; the parent article.
7462         (when (setq to-address (or (message-fetch-field "reply-to")
7463                                    (message-fetch-field "from")))
7464           (setq params (append
7465                         (list (cons 'to-address
7466                                     (funcall gnus-decode-encoded-word-function
7467                                              to-address))))))
7468         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
7469         (insert-buffer-substring gnus-original-article-buffer)
7470         ;; Remove lines that may lead nndoc to misinterpret the
7471         ;; document type.
7472         (narrow-to-region
7473          (goto-char (point-min))
7474          (or (search-forward "\n\n" nil t) (point)))
7475         (goto-char (point-min))
7476         (delete-matching-lines "^Path:\\|^From ")
7477         (widen))
7478       (unwind-protect
7479           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
7480                     (gnus-newsgroup-ephemeral-ignored-charsets
7481                      gnus-newsgroup-ignored-charsets))
7482                 (gnus-group-read-ephemeral-group
7483                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
7484                               (nndoc-article-type
7485                                ,(if force 'mbox 'guess))) t))
7486             ;; Make all postings to this group go to the parent group.
7487               (nconc (gnus-info-params (gnus-get-info name))
7488                      params)
7489             ;; Couldn't select this doc group.
7490             (switch-to-buffer buf)
7491             (gnus-set-global-variables)
7492             (gnus-configure-windows 'summary)
7493             (gnus-message 3 "Article couldn't be entered?"))
7494         (kill-buffer dig)))))
7495
7496 (defun gnus-summary-read-document (n)
7497   "Open a new group based on the current article(s).
7498 This will allow you to read digests and other similar
7499 documents as newsgroups.
7500 Obeys the standard process/prefix convention."
7501   (interactive "P")
7502   (let* ((articles (gnus-summary-work-articles n))
7503          (ogroup gnus-newsgroup-name)
7504          (params (append (gnus-info-params (gnus-get-info ogroup))
7505                          (list (cons 'to-group ogroup))))
7506          article group egroup groups vgroup)
7507     (while (setq article (pop articles))
7508       (setq group (format "%s-%d" gnus-newsgroup-name article))
7509       (gnus-summary-remove-process-mark article)
7510       (when (gnus-summary-display-article article)
7511         (save-excursion
7512           (with-temp-buffer
7513             (insert-buffer-substring gnus-original-article-buffer)
7514             ;; Remove some headers that may lead nndoc to make
7515             ;; the wrong guess.
7516             (message-narrow-to-head)
7517             (goto-char (point-min))
7518             (delete-matching-lines "^\\(Path\\):\\|^From ")
7519             (widen)
7520             (if (setq egroup
7521                       (gnus-group-read-ephemeral-group
7522                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7523                                      (nndoc-article-type guess))
7524                        t nil t))
7525                 (progn
7526             ;; Make all postings to this group go to the parent group.
7527                   (nconc (gnus-info-params (gnus-get-info egroup))
7528                          params)
7529                   (push egroup groups))
7530               ;; Couldn't select this doc group.
7531               (gnus-error 3 "Article couldn't be entered"))))))
7532     ;; Now we have selected all the documents.
7533     (cond
7534      ((not groups)
7535       (error "None of the articles could be interpreted as documents"))
7536      ((gnus-group-read-ephemeral-group
7537        (setq vgroup (format
7538                      "nnvirtual:%s-%s" gnus-newsgroup-name
7539                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7540        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7541        t
7542        (cons (current-buffer) 'summary)))
7543      (t
7544       (error "Couldn't select virtual nndoc group")))))
7545
7546 (defun gnus-summary-isearch-article (&optional regexp-p)
7547   "Do incremental search forward on the current article.
7548 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7549   (interactive "P")
7550   (gnus-summary-select-article)
7551   (gnus-configure-windows 'article)
7552   (gnus-eval-in-buffer-window gnus-article-buffer
7553     (save-restriction
7554       (widen)
7555       (isearch-forward regexp-p))))
7556
7557 (defun gnus-summary-search-article-forward (regexp &optional backward)
7558   "Search for an article containing REGEXP forward.
7559 If BACKWARD, search backward instead."
7560   (interactive
7561    (list (read-string
7562           (format "Search article %s (regexp%s): "
7563                   (if current-prefix-arg "backward" "forward")
7564                   (if gnus-last-search-regexp
7565                       (concat ", default " gnus-last-search-regexp)
7566                     "")))
7567          current-prefix-arg))
7568   (if (string-equal regexp "")
7569       (setq regexp (or gnus-last-search-regexp ""))
7570     (setq gnus-last-search-regexp regexp)
7571     (setq gnus-article-before-search gnus-current-article))
7572   ;; Intentionally set gnus-last-article.
7573   (setq gnus-last-article gnus-article-before-search)
7574   (let ((gnus-last-article gnus-last-article))
7575     (if (gnus-summary-search-article regexp backward)
7576         (gnus-summary-show-thread)
7577       (error "Search failed: \"%s\"" regexp))))
7578
7579 (defun gnus-summary-search-article-backward (regexp)
7580   "Search for an article containing REGEXP backward."
7581   (interactive
7582    (list (read-string
7583           (format "Search article backward (regexp%s): "
7584                   (if gnus-last-search-regexp
7585                       (concat ", default " gnus-last-search-regexp)
7586                     "")))))
7587   (gnus-summary-search-article-forward regexp 'backward))
7588
7589 (defun gnus-summary-search-article (regexp &optional backward)
7590   "Search for an article containing REGEXP.
7591 Optional argument BACKWARD means do search for backward.
7592 `gnus-select-article-hook' is not called during the search."
7593   ;; We have to require this here to make sure that the following
7594   ;; dynamic binding isn't shadowed by autoloading.
7595   (require 'gnus-async)
7596   (require 'gnus-art)
7597   (let ((gnus-select-article-hook nil)  ;Disable hook.
7598         (gnus-article-prepare-hook nil)
7599         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7600         (gnus-use-article-prefetch nil)
7601         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7602         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7603         (sum (current-buffer))
7604         (gnus-display-mime-function nil)
7605         (found nil)
7606         point)
7607     (gnus-save-hidden-threads
7608       (gnus-summary-select-article)
7609       (set-buffer gnus-article-buffer)
7610       (goto-char (window-point (get-buffer-window (current-buffer))))
7611       (when backward
7612         (forward-line -1))
7613       (while (not found)
7614         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7615         (if (if backward
7616                 (re-search-backward regexp nil t)
7617               (re-search-forward regexp nil t))
7618             ;; We found the regexp.
7619             (progn
7620               (setq found 'found)
7621               (beginning-of-line)
7622               (set-window-start
7623                (get-buffer-window (current-buffer))
7624                (point))
7625               (forward-line 1)
7626               (set-window-point
7627                (get-buffer-window (current-buffer))
7628                (point))
7629               (set-buffer sum)
7630               (setq point (point)))
7631           ;; We didn't find it, so we go to the next article.
7632           (set-buffer sum)
7633           (setq found 'not)
7634           (while (eq found 'not)
7635             (if (not (if backward (gnus-summary-find-prev)
7636                        (gnus-summary-find-next)))
7637                 ;; No more articles.
7638                 (setq found t)
7639               ;; Select the next article and adjust point.
7640               (unless (gnus-summary-article-sparse-p
7641                        (gnus-summary-article-number))
7642                 (setq found nil)
7643                 (gnus-summary-select-article)
7644                 (set-buffer gnus-article-buffer)
7645                 (widen)
7646                 (goto-char (if backward (point-max) (point-min))))))))
7647       (gnus-message 7 ""))
7648     ;; Return whether we found the regexp.
7649     (when (eq found 'found)
7650       (goto-char point)
7651       (gnus-summary-show-thread)
7652       (gnus-summary-goto-subject gnus-current-article)
7653       (gnus-summary-position-point)
7654       t)))
7655
7656 (defun gnus-find-matching-articles (header regexp)
7657   "Return a list of all articles that match REGEXP on HEADER.
7658 This search includes all articles in the current group that Gnus has
7659 fetched headers for, whether they are displayed or not."
7660   (let ((articles nil)
7661         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
7662         (case-fold-search t))
7663     (dolist (header gnus-newsgroup-headers)
7664       (when (string-match regexp (funcall func header))
7665         (push (mail-header-number header) articles)))
7666     (nreverse articles)))
7667
7668 (defun gnus-summary-find-matching (header regexp &optional backward unread
7669                                           not-case-fold)
7670   "Return a list of all articles that match REGEXP on HEADER.
7671 The search stars on the current article and goes forwards unless
7672 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7673 If UNREAD is non-nil, only unread articles will
7674 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7675 in the comparisons."
7676   (let ((case-fold-search (not not-case-fold))
7677         articles d func)
7678     (if (consp header)
7679         (if (eq (car header) 'extra)
7680             (setq func
7681                   `(lambda (h)
7682                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7683                          "")))
7684           (error "%s is an invalid header" header))
7685       (unless (fboundp (intern (concat "mail-header-" header)))
7686         (error "%s is not a valid header" header))
7687       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7688     (dolist (d (if (eq backward 'all)
7689                    gnus-newsgroup-data
7690                  (gnus-data-find-list
7691                   (gnus-summary-article-number)
7692                   (gnus-data-list backward))))
7693       (when (and (or (not unread)       ; We want all articles...
7694                      (gnus-data-unread-p d)) ; Or just unreads.
7695                  (vectorp (gnus-data-header d)) ; It's not a pseudo.
7696                  (string-match regexp
7697                                (funcall func (gnus-data-header d)))) ; Match.
7698         (push (gnus-data-number d) articles))) ; Success!
7699     (nreverse articles)))
7700
7701 (defun gnus-summary-execute-command (header regexp command &optional backward)
7702   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7703 If HEADER is an empty string (or nil), the match is done on the entire
7704 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7705   (interactive
7706    (list (let ((completion-ignore-case t))
7707            (completing-read
7708             "Header name: "
7709             (mapcar (lambda (header) (list (format "%s" header)))
7710                     (append
7711                      '("Number" "Subject" "From" "Lines" "Date"
7712                        "Message-ID" "Xref" "References" "Body")
7713                      gnus-extra-headers))
7714             nil 'require-match))
7715          (read-string "Regexp: ")
7716          (read-key-sequence "Command: ")
7717          current-prefix-arg))
7718   (when (equal header "Body")
7719     (setq header ""))
7720   ;; Hidden thread subtrees must be searched as well.
7721   (gnus-summary-show-all-threads)
7722   ;; We don't want to change current point nor window configuration.
7723   (save-excursion
7724     (save-window-excursion
7725       (gnus-message 6 "Executing %s..." (key-description command))
7726 ;; We'd like to execute COMMAND interactively so as to give arguments.
7727       (gnus-execute header regexp
7728                     `(call-interactively ',(key-binding command))
7729                     backward)
7730       (gnus-message 6 "Executing %s...done" (key-description command)))))
7731
7732 (defun gnus-summary-beginning-of-article ()
7733   "Scroll the article back to the beginning."
7734   (interactive)
7735   (gnus-summary-select-article)
7736   (gnus-configure-windows 'article)
7737   (gnus-eval-in-buffer-window gnus-article-buffer
7738     (widen)
7739     (goto-char (point-min))
7740     (when gnus-page-broken
7741       (gnus-narrow-to-page))))
7742
7743 (defun gnus-summary-end-of-article ()
7744   "Scroll to the end of the article."
7745   (interactive)
7746   (gnus-summary-select-article)
7747   (gnus-configure-windows 'article)
7748   (gnus-eval-in-buffer-window gnus-article-buffer
7749     (widen)
7750     (goto-char (point-max))
7751     (recenter -3)
7752     (when gnus-page-broken
7753       (gnus-narrow-to-page))))
7754
7755 (defun gnus-summary-print-article (&optional filename n)
7756   "Generate and print a PostScript image of the N next (mail) articles.
7757
7758 If N is negative, print the N previous articles.  If N is nil and articles
7759 have been marked with the process mark, print these instead.
7760
7761 If the optional first argument FILENAME is nil, send the image to the
7762 printer.  If FILENAME is a string, save the PostScript image in a file with
7763 that name.  If FILENAME is a number, prompt the user for the name of the file
7764 to save in."
7765   (interactive (list (ps-print-preprint current-prefix-arg)))
7766   (dolist (article (gnus-summary-work-articles n))
7767     (gnus-summary-select-article nil nil 'pseudo article)
7768     (gnus-eval-in-buffer-window gnus-article-buffer
7769       (let ((buffer (generate-new-buffer " *print*")))
7770         (unwind-protect
7771             (progn
7772               (copy-to-buffer buffer (point-min) (point-max))
7773               (set-buffer buffer)
7774               (gnus-article-delete-invisible-text)
7775               (when (gnus-visual-p 'article-highlight 'highlight)
7776                 ;; Copy-to-buffer doesn't copy overlay.  So redo
7777                 ;; highlight.
7778                 (let ((gnus-article-buffer buffer))
7779                   (gnus-article-highlight-citation t)
7780                   (gnus-article-highlight-signature)))
7781               (let ((ps-left-header
7782                      (list
7783                       (concat "("
7784                               (mail-header-subject gnus-current-headers) ")")
7785                       (concat "("
7786                               (mail-header-from gnus-current-headers) ")")))
7787                     (ps-right-header
7788                      (list
7789                       "/pagenumberstring load"
7790                       (concat "("
7791                               (mail-header-date gnus-current-headers) ")"))))
7792                 (gnus-run-hooks 'gnus-ps-print-hook)
7793                 (save-excursion
7794                   (if window-system
7795                       (ps-spool-buffer-with-faces)
7796                     (ps-spool-buffer)))))
7797           (kill-buffer buffer))))
7798     (gnus-summary-remove-process-mark article))
7799   (ps-despool filename))
7800
7801 (defun gnus-summary-show-article (&optional arg)
7802   "Force re-fetching of the current article.
7803 If ARG (the prefix) is a number, show the article with the charset
7804 defined in `gnus-summary-show-article-charset-alist', or the charset
7805 input.
7806 If ARG (the prefix) is non-nil and not a number, show the raw article
7807 without any article massaging functions being run."
7808   (interactive "P")
7809   (cond
7810    ((numberp arg)
7811     (gnus-summary-show-article t)
7812     (let ((gnus-newsgroup-charset
7813            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7814                (mm-read-coding-system
7815                 "View as charset: "
7816                 (save-excursion
7817                   (set-buffer gnus-article-buffer)
7818                   (detect-coding-region (point) (point-max) t)))))
7819           (gnus-newsgroup-ignored-charsets 'gnus-all))
7820       (gnus-summary-select-article nil 'force)
7821       (let ((deps gnus-newsgroup-dependencies)
7822             head header)
7823         (save-excursion
7824           (set-buffer gnus-original-article-buffer)
7825           (save-restriction
7826             (message-narrow-to-head)
7827             (setq head (buffer-string)))
7828           (with-temp-buffer
7829             (insert (format "211 %d Article retrieved.\n"
7830                             (cdr gnus-article-current)))
7831             (insert head)
7832             (insert ".\n")
7833             (let ((nntp-server-buffer (current-buffer)))
7834               (setq header (car (gnus-get-newsgroup-headers deps t))))))
7835         (gnus-data-set-header
7836          (gnus-data-find (cdr gnus-article-current))
7837          header)
7838         (gnus-summary-update-article-line
7839          (cdr gnus-article-current) header)
7840         (when (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
7841           (gnus-summary-update-secondary-mark (cdr gnus-article-current))))))
7842    ((not arg)
7843     ;; Select the article the normal way.
7844     (gnus-summary-select-article nil 'force))
7845    (t
7846     ;; We have to require this here to make sure that the following
7847     ;; dynamic binding isn't shadowed by autoloading.
7848     (require 'gnus-async)
7849     (require 'gnus-art)
7850     ;; Bind the article treatment functions to nil.
7851     (let ((gnus-have-all-headers t)
7852           gnus-article-prepare-hook
7853           gnus-article-decode-hook
7854           gnus-display-mime-function
7855           gnus-break-pages)
7856       ;; Destroy any MIME parts.
7857       (when (gnus-buffer-live-p gnus-article-buffer)
7858         (save-excursion
7859           (set-buffer gnus-article-buffer)
7860           (mm-destroy-parts gnus-article-mime-handles)
7861           ;; Set it to nil for safety reason.
7862           (setq gnus-article-mime-handle-alist nil)
7863           (setq gnus-article-mime-handles nil)))
7864       (gnus-summary-select-article nil 'force))))
7865   (gnus-summary-goto-subject gnus-current-article)
7866   (gnus-summary-position-point))
7867
7868 (defun gnus-summary-show-raw-article ()
7869   "Show the raw article without any article massaging functions being run."
7870   (interactive)
7871   (gnus-summary-show-article t))
7872
7873 (defun gnus-summary-verbose-headers (&optional arg)
7874   "Toggle permanent full header display.
7875 If ARG is a positive number, turn header display on.
7876 If ARG is a negative number, turn header display off."
7877   (interactive "P")
7878   (setq gnus-show-all-headers
7879         (cond ((or (not (numberp arg))
7880                    (zerop arg))
7881                (not gnus-show-all-headers))
7882               ((natnump arg)
7883                t)))
7884   (gnus-summary-show-article))
7885
7886 (defun gnus-summary-toggle-header (&optional arg)
7887   "Show the headers if they are hidden, or hide them if they are shown.
7888 If ARG is a positive number, show the entire header.
7889 If ARG is a negative number, hide the unwanted header lines."
7890   (interactive "P")
7891   (save-excursion
7892     (set-buffer gnus-article-buffer)
7893     (save-restriction
7894       (let* ((buffer-read-only nil)
7895              (inhibit-point-motion-hooks t)
7896              hidden e)
7897         (setq hidden
7898               (if (numberp arg)
7899                   (>= arg 0)
7900                 (save-restriction
7901                   (article-narrow-to-head)
7902                   (gnus-article-hidden-text-p 'headers))))
7903         (goto-char (point-min))
7904         (when (search-forward "\n\n" nil t)
7905           (delete-region (point-min) (1- (point))))
7906         (goto-char (point-min))
7907         (save-excursion
7908           (set-buffer gnus-original-article-buffer)
7909           (goto-char (point-min))
7910           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7911         (insert-buffer-substring gnus-original-article-buffer 1 e)
7912         (save-restriction
7913           (narrow-to-region (point-min) (point))
7914           (article-decode-encoded-words)
7915           (if  hidden
7916               (let ((gnus-treat-hide-headers nil)
7917                     (gnus-treat-hide-boring-headers nil))
7918                 (setq gnus-article-wash-types
7919                       (delq 'headers gnus-article-wash-types))
7920                 (gnus-treat-article 'head))
7921             (gnus-treat-article 'head)))
7922         (gnus-set-mode-line 'article)))))
7923
7924 (defun gnus-summary-show-all-headers ()
7925   "Make all header lines visible."
7926   (interactive)
7927   (gnus-summary-toggle-header 1))
7928
7929 (defun gnus-summary-caesar-message (&optional arg)
7930   "Caesar rotate the current article by 13.
7931 The numerical prefix specifies how many places to rotate each letter
7932 forward."
7933   (interactive "P")
7934   (gnus-summary-select-article)
7935   (let ((mail-header-separator ""))
7936     (gnus-eval-in-buffer-window gnus-article-buffer
7937       (save-restriction
7938         (widen)
7939         (let ((start (window-start))
7940               buffer-read-only)
7941           (message-caesar-buffer-body arg)
7942           (set-window-start (get-buffer-window (current-buffer)) start))))))
7943
7944 (defun gnus-summary-stop-page-breaking ()
7945   "Stop page breaking in the current article."
7946   (interactive)
7947   (gnus-summary-select-article)
7948   (gnus-eval-in-buffer-window gnus-article-buffer
7949     (widen)
7950     (when (gnus-visual-p 'page-marker)
7951       (let ((buffer-read-only nil))
7952         (gnus-remove-text-with-property 'gnus-prev)
7953         (gnus-remove-text-with-property 'gnus-next))
7954       (setq gnus-page-broken nil))))
7955
7956 (defun gnus-summary-move-article (&optional n to-newsgroup
7957                                             select-method action)
7958   "Move the current article to a different newsgroup.
7959 If N is a positive number, move the N next articles.
7960 If N is a negative number, move the N previous articles.
7961 If N is nil and any articles have been marked with the process mark,
7962 move those articles instead.
7963 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7964 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7965 re-spool using this method.
7966
7967 For this function to work, both the current newsgroup and the
7968 newsgroup that you want to move to have to support the `request-move'
7969 and `request-accept' functions.
7970
7971 ACTION can be either `move' (the default), `crosspost' or `copy'."
7972   (interactive "P")
7973   (unless action
7974     (setq action 'move))
7975   ;; Check whether the source group supports the required functions.
7976   (cond ((and (eq action 'move)
7977               (not (gnus-check-backend-function
7978                     'request-move-article gnus-newsgroup-name)))
7979          (error "The current group does not support article moving"))
7980         ((and (eq action 'crosspost)
7981               (not (gnus-check-backend-function
7982                     'request-replace-article gnus-newsgroup-name)))
7983          (error "The current group does not support article editing")))
7984   (let ((articles (gnus-summary-work-articles n))
7985         (prefix (if (gnus-check-backend-function
7986                      'request-move-article gnus-newsgroup-name)
7987                     (gnus-group-real-prefix gnus-newsgroup-name)
7988                   ""))
7989         (names '((move "Move" "Moving")
7990                  (copy "Copy" "Copying")
7991                  (crosspost "Crosspost" "Crossposting")))
7992         (copy-buf (save-excursion
7993                     (nnheader-set-temp-buffer " *copy article*")))
7994         art-group to-method new-xref article to-groups)
7995     (unless (assq action names)
7996       (error "Unknown action %s" action))
7997     ;; Read the newsgroup name.
7998     (when (and (not to-newsgroup)
7999                (not select-method))
8000       (setq to-newsgroup
8001             (gnus-read-move-group-name
8002              (cadr (assq action names))
8003              (symbol-value (intern (format "gnus-current-%s-group" action)))
8004              articles prefix))
8005       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
8006     (setq to-method (or select-method
8007                         (gnus-server-to-method
8008                          (gnus-group-method to-newsgroup))))
8009     ;; Check the method we are to move this article to...
8010     (unless (gnus-check-backend-function
8011              'request-accept-article (car to-method))
8012       (error "%s does not support article copying" (car to-method)))
8013     (unless (gnus-check-server to-method)
8014       (error "Can't open server %s" (car to-method)))
8015     (gnus-message 6 "%s to %s: %s..."
8016                   (caddr (assq action names))
8017                   (or (car select-method) to-newsgroup) articles)
8018     (while articles
8019       (setq article (pop articles))
8020       (setq
8021        art-group
8022        (cond
8023         ;; Move the article.
8024         ((eq action 'move)
8025          ;; Remove this article from future suppression.
8026          (gnus-dup-unsuppress-article article)
8027          (gnus-request-move-article
8028           article                       ; Article to move
8029           gnus-newsgroup-name           ; From newsgroup
8030           (nth 1 (gnus-find-method-for-group
8031                   gnus-newsgroup-name)) ; Server
8032           (list 'gnus-request-accept-article
8033                 to-newsgroup (list 'quote select-method)
8034                 (not articles) t)       ; Accept form
8035           (not articles)))              ; Only save nov last time
8036         ;; Copy the article.
8037         ((eq action 'copy)
8038          (save-excursion
8039            (set-buffer copy-buf)
8040            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
8041              (gnus-request-accept-article
8042               to-newsgroup select-method (not articles) t))))
8043         ;; Crosspost the article.
8044         ((eq action 'crosspost)
8045          (let ((xref (message-tokenize-header
8046                       (mail-header-xref (gnus-summary-article-header article))
8047                       " ")))
8048            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
8049                                   ":" article))
8050            (unless xref
8051              (setq xref (list (system-name))))
8052            (setq new-xref
8053                  (concat
8054                   (mapconcat 'identity
8055                              (delete "Xref:" (delete new-xref xref))
8056                              " ")
8057                   " " new-xref))
8058            (save-excursion
8059              (set-buffer copy-buf)
8060              ;; First put the article in the destination group.
8061              (gnus-request-article-this-buffer article gnus-newsgroup-name)
8062              (when (consp (setq art-group
8063                                 (gnus-request-accept-article
8064                                  to-newsgroup select-method (not articles))))
8065                (setq new-xref (concat new-xref " " (car art-group)
8066                                       ":" (cdr art-group)))
8067                ;; Now we have the new Xrefs header, so we insert
8068                ;; it and replace the new article.
8069                (nnheader-replace-header "Xref" new-xref)
8070                (gnus-request-replace-article
8071                 (cdr art-group) to-newsgroup (current-buffer))
8072                art-group))))))
8073       (cond
8074        ((not art-group)
8075         (gnus-message 1 "Couldn't %s article %s: %s"
8076                       (cadr (assq action names)) article
8077                       (nnheader-get-report (car to-method))))
8078        ((eq art-group 'junk)
8079         (when (eq action 'move)
8080           (gnus-summary-mark-article article gnus-canceled-mark)
8081           (gnus-message 4 "Deleted article %s" article)))
8082        (t
8083         (let* ((pto-group (gnus-group-prefixed-name
8084                            (car art-group) to-method))
8085                (entry
8086                 (gnus-gethash pto-group gnus-newsrc-hashtb))
8087                (info (nth 2 entry))
8088                (to-group (gnus-info-group info))
8089                to-marks)
8090           ;; Update the group that has been moved to.
8091           (when (and info
8092                      (memq action '(move copy)))
8093             (unless (member to-group to-groups)
8094               (push to-group to-groups))
8095
8096             (unless (memq article gnus-newsgroup-unreads)
8097               (push 'read to-marks)
8098               (gnus-info-set-read
8099                info (gnus-add-to-range (gnus-info-read info)
8100                                        (list (cdr art-group)))))
8101
8102             ;; See whether the article is to be put in the cache.
8103             (let ((marks gnus-article-mark-lists)
8104                   (to-article (cdr art-group)))
8105
8106               ;; Enter the article into the cache in the new group,
8107               ;; if that is required.
8108               (when gnus-use-cache
8109                 (gnus-cache-possibly-enter-article
8110                  to-group to-article
8111                  (memq article gnus-newsgroup-marked)
8112                  (memq article gnus-newsgroup-dormant)
8113                  (memq article gnus-newsgroup-unreads)))
8114
8115               (when gnus-preserve-marks
8116                 ;; Copy any marks over to the new group.
8117                 (when (and (equal to-group gnus-newsgroup-name)
8118                            (not (memq article gnus-newsgroup-unreads)))
8119                   ;; Mark this article as read in this group.
8120                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
8121                   (setcdr (gnus-active to-group) to-article)
8122                   (setcdr gnus-newsgroup-active to-article))
8123
8124                 (while marks
8125                   (when (memq article (symbol-value
8126                                        (intern (format "gnus-newsgroup-%s"
8127                                                        (caar marks)))))
8128                     (push (cdar marks) to-marks)
8129                     ;; If the other group is the same as this group,
8130                     ;; then we have to add the mark to the list.
8131                     (when (equal to-group gnus-newsgroup-name)
8132                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
8133                            (cons to-article
8134                                  (symbol-value
8135                                   (intern (format "gnus-newsgroup-%s"
8136                                                   (caar marks)))))))
8137                     ;; Copy the marks to other group.
8138                     (gnus-add-marked-articles
8139                      to-group (cdar marks) (list to-article) info))
8140                   (setq marks (cdr marks)))
8141
8142                 (gnus-request-set-mark to-group (list (list (list to-article)
8143                                                             'add
8144                                                             to-marks))))
8145
8146               (gnus-dribble-enter
8147                (concat "(gnus-group-set-info '"
8148                        (gnus-prin1-to-string (gnus-get-info to-group))
8149                        ")"))))
8150
8151           ;; Update the Xref header in this article to point to
8152           ;; the new crossposted article we have just created.
8153           (when (eq action 'crosspost)
8154             (save-excursion
8155               (set-buffer copy-buf)
8156               (gnus-request-article-this-buffer article gnus-newsgroup-name)
8157               (nnheader-replace-header "Xref" new-xref)
8158               (gnus-request-replace-article
8159                article gnus-newsgroup-name (current-buffer)))))
8160
8161         ;;;!!!Why is this necessary?
8162         (set-buffer gnus-summary-buffer)
8163
8164         (gnus-summary-goto-subject article)
8165         (when (eq action 'move)
8166           (gnus-summary-mark-article article gnus-canceled-mark))))
8167       (gnus-summary-remove-process-mark article))
8168     ;; Re-activate all groups that have been moved to.
8169     (while to-groups
8170       (save-excursion
8171         (set-buffer gnus-group-buffer)
8172         (when (gnus-group-goto-group (car to-groups) t)
8173           (gnus-group-get-new-news-this-group 1 t))
8174         (pop to-groups)))
8175
8176     (gnus-kill-buffer copy-buf)
8177     (gnus-summary-position-point)
8178     (gnus-set-mode-line 'summary)))
8179
8180 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
8181   "Move the current article to a different newsgroup.
8182 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
8183 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
8184 re-spool using this method."
8185   (interactive "P")
8186   (gnus-summary-move-article n to-newsgroup select-method 'copy))
8187
8188 (defun gnus-summary-crosspost-article (&optional n)
8189   "Crosspost the current article to some other group."
8190   (interactive "P")
8191   (gnus-summary-move-article n nil nil 'crosspost))
8192
8193 (defcustom gnus-summary-respool-default-method nil
8194   "Default method for respooling an article.
8195 If nil, use to the current newsgroup method."
8196   :type '(choice (gnus-select-method :value (nnml ""))
8197                  (const nil))
8198   :group 'gnus-summary-mail)
8199
8200 (defun gnus-summary-respool-article (&optional n method)
8201   "Respool the current article.
8202 The article will be squeezed through the mail spooling process again,
8203 which means that it will be put in some mail newsgroup or other
8204 depending on `nnmail-split-methods'.
8205 If N is a positive number, respool the N next articles.
8206 If N is a negative number, respool the N previous articles.
8207 If N is nil and any articles have been marked with the process mark,
8208 respool those articles instead.
8209
8210 Respooling can be done both from mail groups and \"real\" newsgroups.
8211 In the former case, the articles in question will be moved from the
8212 current group into whatever groups they are destined to.  In the
8213 latter case, they will be copied into the relevant groups."
8214   (interactive
8215    (list current-prefix-arg
8216          (let* ((methods (gnus-methods-using 'respool))
8217                 (methname
8218                  (symbol-name (or gnus-summary-respool-default-method
8219                                   (car (gnus-find-method-for-group
8220                                         gnus-newsgroup-name)))))
8221                 (method
8222                  (gnus-completing-read
8223                   methname "What backend do you want to use when respooling?"
8224                   methods nil t nil 'gnus-mail-method-history))
8225                 ms)
8226            (cond
8227             ((zerop (length (setq ms (gnus-servers-using-backend
8228                                       (intern method)))))
8229              (list (intern method) ""))
8230             ((= 1 (length ms))
8231              (car ms))
8232             (t
8233              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
8234                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
8235                            ms-alist))))))))
8236   (unless method
8237     (error "No method given for respooling"))
8238   (if (assoc (symbol-name
8239               (car (gnus-find-method-for-group gnus-newsgroup-name)))
8240              (gnus-methods-using 'respool))
8241       (gnus-summary-move-article n nil method)
8242     (gnus-summary-copy-article n nil method)))
8243
8244 (defun gnus-summary-import-article (file &optional edit)
8245   "Import an arbitrary file into a mail newsgroup."
8246   (interactive "fImport file: \nP")
8247   (let ((group gnus-newsgroup-name)
8248         (now (current-time))
8249         atts lines group-art)
8250     (unless (gnus-check-backend-function 'request-accept-article group)
8251       (error "%s does not support article importing" group))
8252     (or (file-readable-p file)
8253         (not (file-regular-p file))
8254         (error "Can't read %s" file))
8255     (save-excursion
8256       (set-buffer (gnus-get-buffer-create " *import file*"))
8257       (erase-buffer)
8258       (nnheader-insert-file-contents file)
8259       (goto-char (point-min))
8260       (if (nnheader-article-p)
8261           (save-restriction
8262             (goto-char (point-min))
8263             (search-forward "\n\n" nil t)
8264             (narrow-to-region (point-min) (1- (point)))
8265             (goto-char (point-min))
8266             (unless (re-search-forward "^date:" nil t)
8267               (goto-char (point-max))
8268               (insert "Date: " (message-make-date (nth 5 atts)) "\n")))
8269        ;; This doesn't look like an article, so we fudge some headers.
8270         (setq atts (file-attributes file)
8271               lines (count-lines (point-min) (point-max)))
8272         (insert "From: " (read-string "From: ") "\n"
8273                 "Subject: " (read-string "Subject: ") "\n"
8274                 "Date: " (message-make-date (nth 5 atts)) "\n"
8275                 "Message-ID: " (message-make-message-id) "\n"
8276                 "Lines: " (int-to-string lines) "\n"
8277                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
8278       (setq group-art (gnus-request-accept-article group nil t))
8279       (kill-buffer (current-buffer)))
8280     (setq gnus-newsgroup-active (gnus-activate-group group))
8281     (forward-line 1)
8282     (gnus-summary-goto-article (cdr group-art) nil t)
8283     (when edit
8284       (gnus-summary-edit-article))))
8285
8286 (defun gnus-summary-create-article ()
8287   "Create an article in a mail newsgroup."
8288   (interactive)
8289   (let ((group gnus-newsgroup-name)
8290         (now (current-time))
8291         group-art)
8292     (unless (gnus-check-backend-function 'request-accept-article group)
8293       (error "%s does not support article importing" group))
8294     (save-excursion
8295       (set-buffer (gnus-get-buffer-create " *import file*"))
8296       (erase-buffer)
8297       (goto-char (point-min))
8298       ;; This doesn't look like an article, so we fudge some headers.
8299       (insert "From: " (read-string "From: ") "\n"
8300               "Subject: " (read-string "Subject: ") "\n"
8301               "Date: " (message-make-date now) "\n"
8302               "Message-ID: " (message-make-message-id) "\n")
8303       (setq group-art (gnus-request-accept-article group nil t))
8304       (kill-buffer (current-buffer)))
8305     (setq gnus-newsgroup-active (gnus-activate-group group))
8306     (forward-line 1)
8307     (gnus-summary-goto-article (cdr group-art) nil t)
8308     (gnus-summary-edit-article)))
8309
8310 (defun gnus-summary-article-posted-p ()
8311   "Say whether the current (mail) article is available from news as well.
8312 This will be the case if the article has both been mailed and posted."
8313   (interactive)
8314   (let ((id (mail-header-references (gnus-summary-article-header)))
8315         (gnus-override-method (car (gnus-refer-article-methods))))
8316     (if (gnus-request-head id "")
8317         (gnus-message 2 "The current message was found on %s"
8318                       gnus-override-method)
8319       (gnus-message 2 "The current message couldn't be found on %s"
8320                     gnus-override-method)
8321       nil)))
8322
8323 (defun gnus-summary-expire-articles (&optional now)
8324   "Expire all articles that are marked as expirable in the current group."
8325   (interactive)
8326   (when (gnus-check-backend-function
8327          'request-expire-articles gnus-newsgroup-name)
8328     ;; This backend supports expiry.
8329     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
8330            (expirable (if total
8331                           (progn
8332                             ;; We need to update the info for
8333                         ;; this group for `gnus-list-of-read-articles'
8334                             ;; to give us the right answer.
8335                             (gnus-run-hooks 'gnus-exit-group-hook)
8336                             (gnus-summary-update-info)
8337                             (gnus-list-of-read-articles gnus-newsgroup-name))
8338                         (setq gnus-newsgroup-expirable
8339                               (sort gnus-newsgroup-expirable '<))))
8340            (expiry-wait (if now 'immediate
8341                           (gnus-group-find-parameter
8342                            gnus-newsgroup-name 'expiry-wait)))
8343            (nnmail-expiry-target
8344             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
8345                 nnmail-expiry-target))
8346            es)
8347       (when expirable
8348         ;; There are expirable articles in this group, so we run them
8349         ;; through the expiry process.
8350         (gnus-message 6 "Expiring articles...")
8351         (unless (gnus-check-group gnus-newsgroup-name)
8352           (error "Can't open server for %s" gnus-newsgroup-name))
8353         ;; The list of articles that weren't expired is returned.
8354         (save-excursion
8355           (if expiry-wait
8356               (let ((nnmail-expiry-wait-function nil)
8357                     (nnmail-expiry-wait expiry-wait))
8358                 (setq es (gnus-request-expire-articles
8359                           expirable gnus-newsgroup-name)))
8360             (setq es (gnus-request-expire-articles
8361                       expirable gnus-newsgroup-name)))
8362           (unless total
8363             (setq gnus-newsgroup-expirable es))
8364           ;; We go through the old list of expirable, and mark all
8365           ;; really expired articles as nonexistent.
8366           (unless (eq es expirable) ;If nothing was expired, we don't mark.
8367             (let ((gnus-use-cache nil))
8368               (while expirable
8369                 (unless (memq (car expirable) es)
8370                   (when (gnus-data-find (car expirable))
8371                     (gnus-summary-mark-article
8372                      (car expirable) gnus-canceled-mark)))
8373                 (setq expirable (cdr expirable))))))
8374         (gnus-message 6 "Expiring articles...done")))))
8375
8376 (defun gnus-summary-expire-articles-now ()
8377   "Expunge all expirable articles in the current group.
8378 This means that *all* articles that are marked as expirable will be
8379 deleted forever, right now."
8380   (interactive)
8381   (or gnus-expert-user
8382       (gnus-yes-or-no-p
8383        "Are you really, really, really sure you want to delete all these messages? ")
8384       (error "Phew!"))
8385   (gnus-summary-expire-articles t))
8386
8387 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
8388 (defun gnus-summary-delete-article (&optional n)
8389   "Delete the N next (mail) articles.
8390 This command actually deletes articles.  This is not a marking
8391 command.  The article will disappear forever from your life, never to
8392 return.
8393 If N is negative, delete backwards.
8394 If N is nil and articles have been marked with the process mark,
8395 delete these instead."
8396   (interactive "P")
8397   (unless (gnus-check-backend-function 'request-expire-articles
8398                                        gnus-newsgroup-name)
8399     (error "The current newsgroup does not support article deletion"))
8400   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
8401     (error "Couldn't open server"))
8402   ;; Compute the list of articles to delete.
8403   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
8404         not-deleted)
8405     (if (and gnus-novice-user
8406              (not (gnus-yes-or-no-p
8407                    (format "Do you really want to delete %s forever? "
8408                            (if (> (length articles) 1)
8409                                (format "these %s articles" (length articles))
8410                              "this article")))))
8411         ()
8412       ;; Delete the articles.
8413       (setq not-deleted (gnus-request-expire-articles
8414                          articles gnus-newsgroup-name 'force))
8415       (while articles
8416         (gnus-summary-remove-process-mark (car articles))
8417         ;; The backend might not have been able to delete the article
8418         ;; after all.
8419         (unless (memq (car articles) not-deleted)
8420           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
8421         (setq articles (cdr articles)))
8422       (when not-deleted
8423         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
8424     (gnus-summary-position-point)
8425     (gnus-set-mode-line 'summary)
8426     not-deleted))
8427
8428 (defun gnus-summary-edit-article (&optional arg)
8429   "Edit the current article.
8430 This will have permanent effect only in mail groups.
8431 If ARG is nil, edit the decoded articles.
8432 If ARG is 1, edit the raw articles.
8433 If ARG is 2, edit the raw articles even in read-only groups.
8434 If ARG is 3, edit the articles with the current handles.
8435 Otherwise, allow editing of articles even in read-only
8436 groups."
8437   (interactive "P")
8438   (let (force raw current-handles)
8439     (cond
8440      ((null arg))
8441      ((eq arg 1) (setq raw t))
8442      ((eq arg 2) (setq raw t
8443                        force t))
8444      ((eq arg 3) (setq current-handles
8445                        (and (gnus-buffer-live-p gnus-article-buffer)
8446                             (with-current-buffer gnus-article-buffer
8447                               (prog1
8448                                   gnus-article-mime-handles
8449                                 (setq gnus-article-mime-handles nil))))))
8450      (t (setq force t)))
8451     (if (and raw (not force) (equal gnus-newsgroup-name "nndraft:drafts"))
8452         (error "Can't edit the raw article in group nndraft:drafts"))
8453     (save-excursion
8454       (set-buffer gnus-summary-buffer)
8455       (let ((mail-parse-charset gnus-newsgroup-charset)
8456             (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
8457         (gnus-set-global-variables)
8458         (when (and (not force)
8459                    (gnus-group-read-only-p))
8460           (error "The current newsgroup does not support article editing"))
8461         (gnus-summary-show-article t)
8462         (when (and (not raw) (gnus-buffer-live-p gnus-article-buffer))
8463           (with-current-buffer gnus-article-buffer
8464             (mm-enable-multibyte-mule4)))
8465         (if (equal gnus-newsgroup-name "nndraft:drafts")
8466             (setq raw t))
8467         (gnus-article-edit-article
8468          (if raw 'ignore
8469            `(lambda ()
8470               (let ((mbl mml-buffer-list))
8471                 (setq mml-buffer-list nil)
8472                 (mime-to-mml ,'current-handles)
8473                 (let ((mbl1 mml-buffer-list))
8474                   (setq mml-buffer-list mbl)
8475                   (set (make-local-variable 'mml-buffer-list) mbl1))
8476                 (make-local-hook 'kill-buffer-hook)
8477                 (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t))))
8478          `(lambda (no-highlight)
8479             (let ((mail-parse-charset ',gnus-newsgroup-charset)
8480                   (message-options message-options)
8481                   (message-options-set-recipient)
8482                   (mail-parse-ignored-charsets
8483                    ',gnus-newsgroup-ignored-charsets))
8484               ,(if (not raw) '(progn
8485                                 (mml-to-mime)
8486                                 (mml-destroy-buffers)
8487                                 (remove-hook 'kill-buffer-hook
8488                                              'mml-destroy-buffers t)
8489                                 (kill-local-variable 'mml-buffer-list)))
8490               (gnus-summary-edit-article-done
8491                ,(or (mail-header-references gnus-current-headers) "")
8492                ,(gnus-group-read-only-p)
8493                ,gnus-summary-buffer no-highlight))))))))
8494
8495 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
8496
8497 (defun gnus-summary-edit-article-done (&optional references read-only buffer
8498                                                  no-highlight)
8499   "Make edits to the current article permanent."
8500   (interactive)
8501   (save-excursion
8502    ;; The buffer restriction contains the entire article if it exists.
8503     (when (article-goto-body)
8504       (let ((lines (count-lines (point) (point-max)))
8505             (length (- (point-max) (point)))
8506             (case-fold-search t)
8507             (body (copy-marker (point))))
8508         (goto-char (point-min))
8509         (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
8510           (delete-region (match-beginning 1) (match-end 1))
8511           (insert (number-to-string length)))
8512         (goto-char (point-min))
8513         (when (re-search-forward
8514                "^x-content-length:[ \t]\\([0-9]+\\)" body t)
8515           (delete-region (match-beginning 1) (match-end 1))
8516           (insert (number-to-string length)))
8517         (goto-char (point-min))
8518         (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
8519           (delete-region (match-beginning 1) (match-end 1))
8520           (insert (number-to-string lines))))))
8521   ;; Replace the article.
8522   (let ((buf (current-buffer)))
8523     (with-temp-buffer
8524       (insert-buffer-substring buf)
8525
8526       (if (and (not read-only)
8527                (not (gnus-request-replace-article
8528                      (cdr gnus-article-current) (car gnus-article-current)
8529                      (current-buffer) t)))
8530           (error "Couldn't replace article")
8531         ;; Update the summary buffer.
8532         (if (and references
8533                  (equal (message-tokenize-header references " ")
8534                         (message-tokenize-header
8535                          (or (message-fetch-field "references") "") " ")))
8536             ;; We only have to update this line.
8537             (save-excursion
8538               (save-restriction
8539                 (message-narrow-to-head)
8540                 (let ((head (buffer-string))
8541                       header)
8542                   (with-temp-buffer
8543                     (insert (format "211 %d Article retrieved.\n"
8544                                     (cdr gnus-article-current)))
8545                     (insert head)
8546                     (insert ".\n")
8547                     (let ((nntp-server-buffer (current-buffer)))
8548                       (setq header (car (gnus-get-newsgroup-headers
8549                                          (save-excursion
8550                                            (set-buffer gnus-summary-buffer)
8551                                            gnus-newsgroup-dependencies)
8552                                          t))))
8553                     (save-excursion
8554                       (set-buffer gnus-summary-buffer)
8555                       (gnus-data-set-header
8556                        (gnus-data-find (cdr gnus-article-current))
8557                        header)
8558                       (gnus-summary-update-article-line
8559                        (cdr gnus-article-current) header)
8560                       (if (gnus-summary-goto-subject
8561                            (cdr gnus-article-current) nil t)
8562                           (gnus-summary-update-secondary-mark
8563                            (cdr gnus-article-current))))))))
8564           ;; Update threads.
8565           (set-buffer (or buffer gnus-summary-buffer))
8566           (gnus-summary-update-article (cdr gnus-article-current))
8567           (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
8568               (gnus-summary-update-secondary-mark
8569                (cdr gnus-article-current))))
8570         ;; Prettify the article buffer again.
8571         (unless no-highlight
8572           (save-excursion
8573             (set-buffer gnus-article-buffer)
8574             ;;;!!! Fix this -- article should be rehighlighted.
8575             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8576             (set-buffer gnus-original-article-buffer)
8577             (gnus-request-article
8578              (cdr gnus-article-current)
8579              (car gnus-article-current) (current-buffer))))
8580         ;; Prettify the summary buffer line.
8581         (when (gnus-visual-p 'summary-highlight 'highlight)
8582           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8583
8584 (defun gnus-summary-edit-wash (key)
8585   "Perform editing command KEY in the article buffer."
8586   (interactive
8587    (list
8588     (progn
8589       (message "%s" (concat (this-command-keys) "- "))
8590       (read-char))))
8591   (message "")
8592   (gnus-summary-edit-article)
8593   (execute-kbd-macro (concat (this-command-keys) key))
8594   (gnus-article-edit-done))
8595
8596 ;;; Respooling
8597
8598 (defun gnus-summary-respool-query (&optional silent trace)
8599   "Query where the respool algorithm would put this article."
8600   (interactive)
8601   (let (gnus-mark-article-hook)
8602     (gnus-summary-select-article)
8603     (save-excursion
8604       (set-buffer gnus-original-article-buffer)
8605       (save-restriction
8606         (message-narrow-to-head)
8607         (let ((groups (nnmail-article-group 'identity trace)))
8608           (unless silent
8609             (if groups
8610                 (message "This message would go to %s"
8611                          (mapconcat 'car groups ", "))
8612               (message "This message would go to no groups"))
8613             groups))))))
8614
8615 (defun gnus-summary-respool-trace ()
8616   "Trace where the respool algorithm would put this article.
8617 Display a buffer showing all fancy splitting patterns which matched."
8618   (interactive)
8619   (gnus-summary-respool-query nil t))
8620
8621 ;; Summary marking commands.
8622
8623 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8624   "Mark articles which has the same subject as read, and then select the next.
8625 If UNMARK is positive, remove any kind of mark.
8626 If UNMARK is negative, tick articles."
8627   (interactive "P")
8628   (when unmark
8629     (setq unmark (prefix-numeric-value unmark)))
8630   (let ((count
8631          (gnus-summary-mark-same-subject
8632           (gnus-summary-article-subject) unmark)))
8633     ;; Select next unread article.  If auto-select-same mode, should
8634     ;; select the first unread article.
8635     (gnus-summary-next-article t (and gnus-auto-select-same
8636                                       (gnus-summary-article-subject)))
8637     (gnus-message 7 "%d article%s marked as %s"
8638                   count (if (= count 1) " is" "s are")
8639                   (if unmark "unread" "read"))))
8640
8641 (defun gnus-summary-kill-same-subject (&optional unmark)
8642   "Mark articles which has the same subject as read.
8643 If UNMARK is positive, remove any kind of mark.
8644 If UNMARK is negative, tick articles."
8645   (interactive "P")
8646   (when unmark
8647     (setq unmark (prefix-numeric-value unmark)))
8648   (let ((count
8649          (gnus-summary-mark-same-subject
8650           (gnus-summary-article-subject) unmark)))
8651     ;; If marked as read, go to next unread subject.
8652     (when (null unmark)
8653       ;; Go to next unread subject.
8654       (gnus-summary-next-subject 1 t))
8655     (gnus-message 7 "%d articles are marked as %s"
8656                   count (if unmark "unread" "read"))))
8657
8658 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8659   "Mark articles with same SUBJECT as read, and return marked number.
8660 If optional argument UNMARK is positive, remove any kinds of marks.
8661 If optional argument UNMARK is negative, mark articles as unread instead."
8662   (let ((count 1))
8663     (save-excursion
8664       (cond
8665        ((null unmark)                   ; Mark as read.
8666         (while (and
8667                 (progn
8668                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8669                   (gnus-summary-show-thread) t)
8670                 (gnus-summary-find-subject subject))
8671           (setq count (1+ count))))
8672        ((> unmark 0)                    ; Tick.
8673         (while (and
8674                 (progn
8675                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8676                   (gnus-summary-show-thread) t)
8677                 (gnus-summary-find-subject subject))
8678           (setq count (1+ count))))
8679        (t                               ; Mark as unread.
8680         (while (and
8681                 (progn
8682                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8683                   (gnus-summary-show-thread) t)
8684                 (gnus-summary-find-subject subject))
8685           (setq count (1+ count)))))
8686       (gnus-set-mode-line 'summary)
8687       ;; Return the number of marked articles.
8688       count)))
8689
8690 (defun gnus-summary-mark-as-processable (n &optional unmark)
8691   "Set the process mark on the next N articles.
8692 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8693 the process mark instead.  The difference between N and the actual
8694 number of articles marked is returned."
8695   (interactive "P")
8696   (if (and (null n) (gnus-region-active-p))
8697       (gnus-uu-mark-region (region-beginning) (region-end) unmark)
8698     (setq n (prefix-numeric-value n))
8699     (let ((backward (< n 0))
8700           (n (abs n)))
8701       (while (and
8702               (> n 0)
8703               (if unmark
8704                   (gnus-summary-remove-process-mark
8705                    (gnus-summary-article-number))
8706                 (gnus-summary-set-process-mark (gnus-summary-article-number)))
8707               (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8708         (setq n (1- n)))
8709       (when (/= 0 n)
8710         (gnus-message 7 "No more articles"))
8711       (gnus-summary-recenter)
8712       (gnus-summary-position-point)
8713       n)))
8714
8715 (defun gnus-summary-unmark-as-processable (n)
8716   "Remove the process mark from the next N articles.
8717 If N is negative, unmark backward instead.  The difference between N and
8718 the actual number of articles unmarked is returned."
8719   (interactive "P")
8720   (gnus-summary-mark-as-processable n t))
8721
8722 (defun gnus-summary-unmark-all-processable ()
8723   "Remove the process mark from all articles."
8724   (interactive)
8725   (save-excursion
8726     (while gnus-newsgroup-processable
8727       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8728   (gnus-summary-position-point))
8729
8730 (defun gnus-summary-add-mark (article type)
8731   "Mark ARTICLE with a mark of TYPE."
8732   (let ((vtype (car (assq type gnus-article-mark-lists)))
8733         var)
8734     (if (not vtype)
8735         (error "No such mark type: %s" type)
8736       (setq var (intern (format "gnus-newsgroup-%s" type)))
8737       (set var (cons article (symbol-value var)))
8738       (if (memq type '(processable cached replied forwarded recent saved))
8739           (gnus-summary-update-secondary-mark article)
8740         ;;; !!! This is bogus.  We should find out what primary
8741         ;;; !!! mark we want to set.
8742         (gnus-summary-update-mark gnus-del-mark 'unread)))))
8743
8744 (defun gnus-summary-mark-as-expirable (n)
8745   "Mark N articles forward as expirable.
8746 If N is negative, mark backward instead.  The difference between N and
8747 the actual number of articles marked is returned."
8748   (interactive "p")
8749   (gnus-summary-mark-forward n gnus-expirable-mark))
8750
8751 (defun gnus-summary-mark-article-as-replied (article)
8752   "Mark ARTICLE as replied to and update the summary line.
8753 ARTICLE can also be a list of articles."
8754   (interactive (list (gnus-summary-article-number)))
8755   (let ((articles (if (listp article) article (list article))))
8756     (dolist (article articles)
8757       (push article gnus-newsgroup-replied)
8758       (let ((buffer-read-only nil))
8759         (when (gnus-summary-goto-subject article nil t)
8760           (gnus-summary-update-secondary-mark article))))))
8761
8762 (defun gnus-summary-mark-article-as-forwarded (article)
8763   "Mark ARTICLE as forwarded and update the summary line.
8764 ARTICLE can also be a list of articles."
8765   (let ((articles (if (listp article) article (list article))))
8766     (dolist (article articles)
8767       (push article gnus-newsgroup-forwarded)
8768       (let ((buffer-read-only nil))
8769         (when (gnus-summary-goto-subject article nil t)
8770           (gnus-summary-update-secondary-mark article))))))
8771
8772 (defun gnus-summary-set-bookmark (article)
8773   "Set a bookmark in current article."
8774   (interactive (list (gnus-summary-article-number)))
8775   (when (or (not (get-buffer gnus-article-buffer))
8776             (not gnus-current-article)
8777             (not gnus-article-current)
8778             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8779     (error "No current article selected"))
8780   ;; Remove old bookmark, if one exists.
8781   (let ((old (assq article gnus-newsgroup-bookmarks)))
8782     (when old
8783       (setq gnus-newsgroup-bookmarks
8784             (delq old gnus-newsgroup-bookmarks))))
8785   ;; Set the new bookmark, which is on the form
8786   ;; (article-number . line-number-in-body).
8787   (push
8788    (cons article
8789          (save-excursion
8790            (set-buffer gnus-article-buffer)
8791            (count-lines
8792             (min (point)
8793                  (save-excursion
8794                    (goto-char (point-min))
8795                    (search-forward "\n\n" nil t)
8796                    (point)))
8797             (point))))
8798    gnus-newsgroup-bookmarks)
8799   (gnus-message 6 "A bookmark has been added to the current article."))
8800
8801 (defun gnus-summary-remove-bookmark (article)
8802   "Remove the bookmark from the current article."
8803   (interactive (list (gnus-summary-article-number)))
8804   ;; Remove old bookmark, if one exists.
8805   (let ((old (assq article gnus-newsgroup-bookmarks)))
8806     (if old
8807         (progn
8808           (setq gnus-newsgroup-bookmarks
8809                 (delq old gnus-newsgroup-bookmarks))
8810           (gnus-message 6 "Removed bookmark."))
8811       (gnus-message 6 "No bookmark in current article."))))
8812
8813 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8814 (defun gnus-summary-mark-as-dormant (n)
8815   "Mark N articles forward as dormant.
8816 If N is negative, mark backward instead.  The difference between N and
8817 the actual number of articles marked is returned."
8818   (interactive "p")
8819   (gnus-summary-mark-forward n gnus-dormant-mark))
8820
8821 (defun gnus-summary-set-process-mark (article)
8822   "Set the process mark on ARTICLE and update the summary line."
8823   (setq gnus-newsgroup-processable
8824         (cons article
8825               (delq article gnus-newsgroup-processable)))
8826   (when (gnus-summary-goto-subject article)
8827     (gnus-summary-show-thread)
8828     (gnus-summary-goto-subject article)
8829     (gnus-summary-update-secondary-mark article)))
8830
8831 (defun gnus-summary-remove-process-mark (article)
8832   "Remove the process mark from ARTICLE and update the summary line."
8833   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8834   (when (gnus-summary-goto-subject article)
8835     (gnus-summary-show-thread)
8836     (gnus-summary-goto-subject article)
8837     (gnus-summary-update-secondary-mark article)))
8838
8839 (defun gnus-summary-set-saved-mark (article)
8840   "Set the process mark on ARTICLE and update the summary line."
8841   (push article gnus-newsgroup-saved)
8842   (when (gnus-summary-goto-subject article)
8843     (gnus-summary-update-secondary-mark article)))
8844
8845 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8846   "Mark N articles as read forwards.
8847 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8848 The difference between N and the actual number of articles marked is
8849 returned.
8850 Iff NO-EXPIRE, auto-expiry will be inhibited."
8851   (interactive "p")
8852   (gnus-summary-show-thread)
8853   (let ((backward (< n 0))
8854         (gnus-summary-goto-unread
8855          (and gnus-summary-goto-unread
8856               (not (eq gnus-summary-goto-unread 'never))
8857               (not (memq mark (list gnus-unread-mark
8858                                     gnus-ticked-mark gnus-dormant-mark)))))
8859         (n (abs n))
8860         (mark (or mark gnus-del-mark)))
8861     (while (and (> n 0)
8862                 (gnus-summary-mark-article nil mark no-expire)
8863                 (zerop (gnus-summary-next-subject
8864                         (if backward -1 1)
8865                         (and gnus-summary-goto-unread
8866                              (not (eq gnus-summary-goto-unread 'never)))
8867                         t)))
8868       (setq n (1- n)))
8869     (when (/= 0 n)
8870       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8871     (gnus-summary-recenter)
8872     (gnus-summary-position-point)
8873     (gnus-set-mode-line 'summary)
8874     n))
8875
8876 (defun gnus-summary-mark-article-as-read (mark)
8877   "Mark the current article quickly as read with MARK."
8878   (let ((article (gnus-summary-article-number)))
8879     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8880     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8881     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8882     (push (cons article mark) gnus-newsgroup-reads)
8883     ;; Possibly remove from cache, if that is used.
8884     (when gnus-use-cache
8885       (gnus-cache-enter-remove-article article))
8886     ;; Allow the backend to change the mark.
8887     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8888     ;; Check for auto-expiry.
8889     (when (and gnus-newsgroup-auto-expire
8890                (memq mark gnus-auto-expirable-marks))
8891       (setq mark gnus-expirable-mark)
8892       ;; Let the backend know about the mark change.
8893       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8894       (push article gnus-newsgroup-expirable))
8895     ;; Set the mark in the buffer.
8896     (gnus-summary-update-mark mark 'unread)
8897     t))
8898
8899 (defun gnus-summary-mark-article-as-unread (mark)
8900   "Mark the current article quickly as unread with MARK."
8901   (let* ((article (gnus-summary-article-number))
8902          (old-mark (gnus-summary-article-mark article)))
8903     ;; Allow the backend to change the mark.
8904     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8905     (if (eq mark old-mark)
8906         t
8907       (if (<= article 0)
8908           (progn
8909             (gnus-error 1 "Can't mark negative article numbers")
8910             nil)
8911         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8912         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8913         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8914         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8915         (cond ((= mark gnus-ticked-mark)
8916                (push article gnus-newsgroup-marked))
8917               ((= mark gnus-dormant-mark)
8918                (push article gnus-newsgroup-dormant))
8919               (t
8920                (push article gnus-newsgroup-unreads)))
8921         (gnus-pull article gnus-newsgroup-reads)
8922
8923         ;; See whether the article is to be put in the cache.
8924         (and gnus-use-cache
8925              (vectorp (gnus-summary-article-header article))
8926              (save-excursion
8927                (gnus-cache-possibly-enter-article
8928                 gnus-newsgroup-name article
8929                 (= mark gnus-ticked-mark)
8930                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8931
8932         ;; Fix the mark.
8933         (gnus-summary-update-mark mark 'unread)
8934         t))))
8935
8936 (defun gnus-summary-mark-article (&optional article mark no-expire)
8937   "Mark ARTICLE with MARK.  MARK can be any character.
8938 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8939 `??' (dormant) and `?E' (expirable).
8940 If MARK is nil, then the default character `?r' is used.
8941 If ARTICLE is nil, then the article on the current line will be
8942 marked.
8943 Iff NO-EXPIRE, auto-expiry will be inhibited."
8944   ;; The mark might be a string.
8945   (when (stringp mark)
8946     (setq mark (aref mark 0)))
8947   ;; If no mark is given, then we check auto-expiring.
8948   (when (null mark)
8949     (setq mark gnus-del-mark))
8950   (when (and (not no-expire)
8951              gnus-newsgroup-auto-expire
8952              (memq mark gnus-auto-expirable-marks))
8953     (setq mark gnus-expirable-mark))
8954   (let ((article (or article (gnus-summary-article-number)))
8955         (old-mark (gnus-summary-article-mark article)))
8956     ;; Allow the backend to change the mark.
8957     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8958     (if (eq mark old-mark)
8959         t
8960       (unless article
8961         (error "No article on current line"))
8962       (if (not (if (or (= mark gnus-unread-mark)
8963                        (= mark gnus-ticked-mark)
8964                        (= mark gnus-dormant-mark))
8965                    (gnus-mark-article-as-unread article mark)
8966                  (gnus-mark-article-as-read article mark)))
8967           t
8968         ;; See whether the article is to be put in the cache.
8969         (and gnus-use-cache
8970              (not (= mark gnus-canceled-mark))
8971              (vectorp (gnus-summary-article-header article))
8972              (save-excursion
8973                (gnus-cache-possibly-enter-article
8974                 gnus-newsgroup-name article
8975                 (= mark gnus-ticked-mark)
8976                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8977
8978         (when (gnus-summary-goto-subject article nil t)
8979           (let ((buffer-read-only nil))
8980             (gnus-summary-show-thread)
8981             ;; Fix the mark.
8982             (gnus-summary-update-mark mark 'unread)
8983             t))))))
8984
8985 (defun gnus-summary-update-secondary-mark (article)
8986   "Update the secondary (read, process, cache) mark."
8987   (gnus-summary-update-mark
8988    (cond ((memq article gnus-newsgroup-processable)
8989           gnus-process-mark)
8990          ((memq article gnus-newsgroup-cached)
8991           gnus-cached-mark)
8992          ((memq article gnus-newsgroup-replied)
8993           gnus-replied-mark)
8994          ((memq article gnus-newsgroup-forwarded)
8995           gnus-forwarded-mark)
8996          ((memq article gnus-newsgroup-saved)
8997           gnus-saved-mark)
8998          ((memq article gnus-newsgroup-recent)
8999           gnus-recent-mark)
9000          ((memq article gnus-newsgroup-unseen)
9001           gnus-unseen-mark)
9002          (t gnus-no-mark))
9003    'replied)
9004   (when (gnus-visual-p 'summary-highlight 'highlight)
9005     (gnus-run-hooks 'gnus-summary-update-hook))
9006   t)
9007
9008 (defun gnus-summary-update-mark (mark type)
9009   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
9010         (buffer-read-only nil))
9011     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
9012     (when forward
9013       (when (looking-at "\r")
9014         (incf forward))
9015       (when (<= (+ forward (point)) (point-max))
9016         ;; Go to the right position on the line.
9017         (goto-char (+ forward (point)))
9018         ;; Replace the old mark with the new mark.
9019         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
9020         ;; Optionally update the marks by some user rule.
9021         (when (eq type 'unread)
9022           (gnus-data-set-mark
9023            (gnus-data-find (gnus-summary-article-number)) mark)
9024           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
9025
9026 (defun gnus-mark-article-as-read (article &optional mark)
9027   "Enter ARTICLE in the pertinent lists and remove it from others."
9028   ;; Make the article expirable.
9029   (let ((mark (or mark gnus-del-mark)))
9030     (if (= mark gnus-expirable-mark)
9031         (push article gnus-newsgroup-expirable)
9032       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
9033     ;; Remove from unread and marked lists.
9034     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9035     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
9036     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
9037     (push (cons article mark) gnus-newsgroup-reads)
9038     ;; Possibly remove from cache, if that is used.
9039     (when gnus-use-cache
9040       (gnus-cache-enter-remove-article article))
9041     t))
9042
9043 (defun gnus-mark-article-as-unread (article &optional mark)
9044   "Enter ARTICLE in the pertinent lists and remove it from others."
9045   (let ((mark (or mark gnus-ticked-mark)))
9046     (if (<= article 0)
9047         (progn
9048           (gnus-error 1 "Can't mark negative article numbers")
9049           nil)
9050       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
9051             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
9052             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
9053             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
9054
9055       ;; Unsuppress duplicates?
9056       (when gnus-suppress-duplicates
9057         (gnus-dup-unsuppress-article article))
9058
9059       (cond ((= mark gnus-ticked-mark)
9060              (push article gnus-newsgroup-marked))
9061             ((= mark gnus-dormant-mark)
9062              (push article gnus-newsgroup-dormant))
9063             (t
9064              (push article gnus-newsgroup-unreads)))
9065       (gnus-pull article gnus-newsgroup-reads)
9066       t)))
9067
9068 (defalias 'gnus-summary-mark-as-unread-forward
9069   'gnus-summary-tick-article-forward)
9070 (make-obsolete 'gnus-summary-mark-as-unread-forward
9071                'gnus-summary-tick-article-forward)
9072 (defun gnus-summary-tick-article-forward (n)
9073   "Tick N articles forwards.
9074 If N is negative, tick backwards instead.
9075 The difference between N and the number of articles ticked is returned."
9076   (interactive "p")
9077   (gnus-summary-mark-forward n gnus-ticked-mark))
9078
9079 (defalias 'gnus-summary-mark-as-unread-backward
9080   'gnus-summary-tick-article-backward)
9081 (make-obsolete 'gnus-summary-mark-as-unread-backward
9082                'gnus-summary-tick-article-backward)
9083 (defun gnus-summary-tick-article-backward (n)
9084   "Tick N articles backwards.
9085 The difference between N and the number of articles ticked is returned."
9086   (interactive "p")
9087   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
9088
9089 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9090 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
9091 (defun gnus-summary-tick-article (&optional article clear-mark)
9092   "Mark current article as unread.
9093 Optional 1st argument ARTICLE specifies article number to be marked as unread.
9094 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
9095   (interactive)
9096   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
9097                                        gnus-ticked-mark)))
9098
9099 (defun gnus-summary-mark-as-read-forward (n)
9100   "Mark N articles as read forwards.
9101 If N is negative, mark backwards instead.
9102 The difference between N and the actual number of articles marked is
9103 returned."
9104   (interactive "p")
9105   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
9106
9107 (defun gnus-summary-mark-as-read-backward (n)
9108   "Mark the N articles as read backwards.
9109 The difference between N and the actual number of articles marked is
9110 returned."
9111   (interactive "p")
9112   (gnus-summary-mark-forward
9113    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
9114
9115 (defun gnus-summary-mark-as-read (&optional article mark)
9116   "Mark current article as read.
9117 ARTICLE specifies the article to be marked as read.
9118 MARK specifies a string to be inserted at the beginning of the line."
9119   (gnus-summary-mark-article article mark))
9120
9121 (defun gnus-summary-clear-mark-forward (n)
9122   "Clear marks from N articles forward.
9123 If N is negative, clear backward instead.
9124 The difference between N and the number of marks cleared is returned."
9125   (interactive "p")
9126   (gnus-summary-mark-forward n gnus-unread-mark))
9127
9128 (defun gnus-summary-clear-mark-backward (n)
9129   "Clear marks from N articles backward.
9130 The difference between N and the number of marks cleared is returned."
9131   (interactive "p")
9132   (gnus-summary-mark-forward (- n) gnus-unread-mark))
9133
9134 (defun gnus-summary-mark-unread-as-read ()
9135   "Intended to be used by `gnus-summary-mark-article-hook'."
9136   (when (memq gnus-current-article gnus-newsgroup-unreads)
9137     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
9138
9139 (defun gnus-summary-mark-read-and-unread-as-read ()
9140   "Intended to be used by `gnus-summary-mark-article-hook'."
9141   (let ((mark (gnus-summary-article-mark)))
9142     (when (or (gnus-unread-mark-p mark)
9143               (gnus-read-mark-p mark))
9144       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
9145
9146 (defun gnus-summary-mark-unread-as-ticked ()
9147   "Intended to be used by `gnus-summary-mark-article-hook'."
9148   (when (memq gnus-current-article gnus-newsgroup-unreads)
9149     (gnus-summary-mark-article gnus-current-article gnus-ticked-mark)))
9150
9151 (defun gnus-summary-mark-region-as-read (point mark all)
9152   "Mark all unread articles between point and mark as read.
9153 If given a prefix, mark all articles between point and mark as read,
9154 even ticked and dormant ones."
9155   (interactive "r\nP")
9156   (save-excursion
9157     (let (article)
9158       (goto-char point)
9159       (beginning-of-line)
9160       (while (and
9161               (< (point) mark)
9162               (progn
9163                 (when (or all
9164                           (memq (setq article (gnus-summary-article-number))
9165                                 gnus-newsgroup-unreads))
9166                   (gnus-summary-mark-article article gnus-del-mark))
9167                 t)
9168               (gnus-summary-find-next))))))
9169
9170 (defun gnus-summary-mark-below (score mark)
9171   "Mark articles with score less than SCORE with MARK."
9172   (interactive "P\ncMark: ")
9173   (setq score (if score
9174                   (prefix-numeric-value score)
9175                 (or gnus-summary-default-score 0)))
9176   (save-excursion
9177     (set-buffer gnus-summary-buffer)
9178     (goto-char (point-min))
9179     (while
9180         (progn
9181           (and (< (gnus-summary-article-score) score)
9182                (gnus-summary-mark-article nil mark))
9183           (gnus-summary-find-next)))))
9184
9185 (defun gnus-summary-kill-below (&optional score)
9186   "Mark articles with score below SCORE as read."
9187   (interactive "P")
9188   (gnus-summary-mark-below score gnus-killed-mark))
9189
9190 (defun gnus-summary-clear-above (&optional score)
9191   "Clear all marks from articles with score above SCORE."
9192   (interactive "P")
9193   (gnus-summary-mark-above score gnus-unread-mark))
9194
9195 (defun gnus-summary-tick-above (&optional score)
9196   "Tick all articles with score above SCORE."
9197   (interactive "P")
9198   (gnus-summary-mark-above score gnus-ticked-mark))
9199
9200 (defun gnus-summary-mark-above (score mark)
9201   "Mark articles with score over SCORE with MARK."
9202   (interactive "P\ncMark: ")
9203   (setq score (if score
9204                   (prefix-numeric-value score)
9205                 (or gnus-summary-default-score 0)))
9206   (save-excursion
9207     (set-buffer gnus-summary-buffer)
9208     (goto-char (point-min))
9209     (while (and (progn
9210                   (when (> (gnus-summary-article-score) score)
9211                     (gnus-summary-mark-article nil mark))
9212                   t)
9213                 (gnus-summary-find-next)))))
9214
9215 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
9216 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
9217 (defun gnus-summary-limit-include-expunged (&optional no-error)
9218   "Display all the hidden articles that were expunged for low scores."
9219   (interactive)
9220   (let ((buffer-read-only nil))
9221     (let ((scored gnus-newsgroup-scored)
9222           headers h)
9223       (while scored
9224         (unless (gnus-summary-article-header (caar scored))
9225           (and (setq h (gnus-number-to-header (caar scored)))
9226                (< (cdar scored) gnus-summary-expunge-below)
9227                (push h headers)))
9228         (setq scored (cdr scored)))
9229       (if (not headers)
9230           (when (not no-error)
9231             (error "No expunged articles hidden"))
9232         (goto-char (point-min))
9233         (push gnus-newsgroup-limit gnus-newsgroup-limits)
9234         (setq gnus-newsgroup-limit (copy-sequence gnus-newsgroup-limit))
9235         (mapcar (lambda (x) (push (mail-header-number x)
9236                                   gnus-newsgroup-limit))
9237                 headers)
9238         (gnus-summary-prepare-unthreaded (nreverse headers))
9239         (goto-char (point-min))
9240         (gnus-summary-position-point)
9241         t))))
9242
9243 (defun gnus-summary-catchup (&optional all quietly to-here not-mark reverse)
9244   "Mark all unread articles in this newsgroup as read.
9245 If prefix argument ALL is non-nil, ticked and dormant articles will
9246 also be marked as read.
9247 If QUIETLY is non-nil, no questions will be asked.
9248 If TO-HERE is non-nil, it should be a point in the buffer.  All
9249 articles before (after, if REVERSE is set) this point will be marked as read.
9250 Note that this function will only catch up the unread article
9251 in the current summary buffer limitation.
9252 The number of articles marked as read is returned."
9253   (interactive "P")
9254   (prog1
9255       (save-excursion
9256         (when (or quietly
9257                   (not gnus-interactive-catchup) ;Without confirmation?
9258                   gnus-expert-user
9259                   (gnus-y-or-n-p
9260                    (if all
9261                        "Mark absolutely all articles as read? "
9262                      "Mark all unread articles as read? ")))
9263           (if (and not-mark
9264                    (not gnus-newsgroup-adaptive)
9265                    (not gnus-newsgroup-auto-expire)
9266                    (not gnus-suppress-duplicates)
9267                    (or (not gnus-use-cache)
9268                        (eq gnus-use-cache 'passive)))
9269               (progn
9270                 (when all
9271                   (setq gnus-newsgroup-marked nil
9272                         gnus-newsgroup-dormant nil))
9273                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
9274             ;; We actually mark all articles as canceled, which we
9275             ;; have to do when using auto-expiry or adaptive scoring.
9276             (gnus-summary-show-all-threads)
9277             (if (and to-here reverse)
9278                 (progn
9279                   (goto-char to-here)
9280                   (while (and
9281                           (gnus-summary-mark-article-as-read gnus-catchup-mark)
9282                           (gnus-summary-find-next (not all) nil nil t))))
9283               (when (gnus-summary-first-subject (not all) t)
9284                 (while (and
9285                         (if to-here (< (point) to-here) t)
9286                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
9287                         (gnus-summary-find-next (not all) nil nil t)))))
9288             (gnus-set-mode-line 'summary))
9289           t))
9290     (gnus-summary-position-point)))
9291
9292 (defun gnus-summary-catchup-to-here (&optional all)
9293   "Mark all unticked articles before the current one as read.
9294 If ALL is non-nil, also mark ticked and dormant articles as read."
9295   (interactive "P")
9296   (save-excursion
9297     (gnus-save-hidden-threads
9298       (let ((beg (point)))
9299         ;; We check that there are unread articles.
9300         (when (or all (gnus-summary-find-prev))
9301           (gnus-summary-catchup all t beg)))))
9302   (gnus-summary-position-point))
9303
9304 (defun gnus-summary-catchup-from-here (&optional all)
9305   "Mark all unticked articles after the current one as read.
9306 If ALL is non-nil, also mark ticked and dormant articles as read."
9307   (interactive "P")
9308   (save-excursion
9309     (gnus-save-hidden-threads
9310       (let ((beg (point)))
9311         ;; We check that there are unread articles.
9312         (when (or all (gnus-summary-find-next))
9313           (gnus-summary-catchup all t beg nil t)))))
9314
9315   (gnus-summary-position-point))
9316 (defun gnus-summary-catchup-all (&optional quietly)
9317   "Mark all articles in this newsgroup as read."
9318   (interactive "P")
9319   (gnus-summary-catchup t quietly))
9320
9321 (defun gnus-summary-catchup-and-exit (&optional all quietly)
9322   "Mark all unread articles in this group as read, then exit.
9323 If prefix argument ALL is non-nil, all articles are marked as read.
9324 If QUIETLY is non-nil, no questions will be asked."
9325   (interactive "P")
9326   (when (gnus-summary-catchup all quietly nil 'fast)
9327     ;; Select next newsgroup or exit.
9328     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
9329              (eq gnus-auto-select-next 'quietly))
9330         (gnus-summary-next-group nil)
9331       (gnus-summary-exit))))
9332
9333 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
9334   "Mark all articles in this newsgroup as read, and then exit."
9335   (interactive "P")
9336   (gnus-summary-catchup-and-exit t quietly))
9337
9338 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
9339   "Mark all articles in this group as read and select the next group.
9340 If given a prefix, mark all articles, unread as well as ticked, as
9341 read."
9342   (interactive "P")
9343   (save-excursion
9344     (gnus-summary-catchup all))
9345   (gnus-summary-next-group))
9346
9347 ;;;
9348 ;;; with article
9349 ;;;
9350
9351 (defmacro gnus-with-article (article &rest forms)
9352   "Select ARTICLE and perform FORMS in the original article buffer.
9353 Then replace the article with the result."
9354   `(progn
9355      ;; We don't want the article to be marked as read.
9356      (let (gnus-mark-article-hook)
9357        (gnus-summary-select-article t t nil ,article))
9358      (set-buffer gnus-original-article-buffer)
9359      ,@forms
9360      (if (not (gnus-check-backend-function
9361                'request-replace-article (car gnus-article-current)))
9362          (gnus-message 5 "Read-only group; not replacing")
9363        (unless (gnus-request-replace-article
9364                 ,article (car gnus-article-current)
9365                 (current-buffer) t)
9366          (error "Couldn't replace article")))
9367      ;; The cache and backlog have to be flushed somewhat.
9368      (when gnus-keep-backlog
9369        (gnus-backlog-remove-article
9370         (car gnus-article-current) (cdr gnus-article-current)))
9371      (when gnus-use-cache
9372        (gnus-cache-update-article
9373         (car gnus-article-current) (cdr gnus-article-current)))))
9374
9375 (put 'gnus-with-article 'lisp-indent-function 1)
9376 (put 'gnus-with-article 'edebug-form-spec '(form body))
9377
9378 ;; Thread-based commands.
9379
9380 (defun gnus-summary-articles-in-thread (&optional article)
9381   "Return a list of all articles in the current thread.
9382 If ARTICLE is non-nil, return all articles in the thread that starts
9383 with that article."
9384   (let* ((article (or article (gnus-summary-article-number)))
9385          (data (gnus-data-find-list article))
9386          (top-level (gnus-data-level (car data)))
9387          (top-subject
9388           (cond ((null gnus-thread-operation-ignore-subject)
9389                  (gnus-simplify-subject-re
9390                   (mail-header-subject (gnus-data-header (car data)))))
9391                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
9392                  (gnus-simplify-subject-fuzzy
9393                   (mail-header-subject (gnus-data-header (car data)))))
9394                 (t nil)))
9395          (end-point (save-excursion
9396                       (if (gnus-summary-go-to-next-thread)
9397                           (point) (point-max))))
9398          articles)
9399     (while (and data
9400                 (< (gnus-data-pos (car data)) end-point))
9401       (when (or (not top-subject)
9402                 (string= top-subject
9403                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
9404                              (gnus-simplify-subject-fuzzy
9405                               (mail-header-subject
9406                                (gnus-data-header (car data))))
9407                            (gnus-simplify-subject-re
9408                             (mail-header-subject
9409                              (gnus-data-header (car data)))))))
9410         (push (gnus-data-number (car data)) articles))
9411       (unless (and (setq data (cdr data))
9412                    (> (gnus-data-level (car data)) top-level))
9413         (setq data nil)))
9414     ;; Return the list of articles.
9415     (nreverse articles)))
9416
9417 (defun gnus-summary-rethread-current ()
9418   "Rethread the thread the current article is part of."
9419   (interactive)
9420   (let* ((gnus-show-threads t)
9421          (article (gnus-summary-article-number))
9422          (id (mail-header-id (gnus-summary-article-header)))
9423          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
9424     (unless id
9425       (error "No article on the current line"))
9426     (gnus-rebuild-thread id)
9427     (gnus-summary-goto-subject article)))
9428
9429 (defun gnus-summary-reparent-thread ()
9430   "Make the current article child of the marked (or previous) article.
9431
9432 Note that the re-threading will only work if `gnus-thread-ignore-subject'
9433 is non-nil or the Subject: of both articles are the same."
9434   (interactive)
9435   (unless (not (gnus-group-read-only-p))
9436     (error "The current newsgroup does not support article editing"))
9437   (unless (<= (length gnus-newsgroup-processable) 1)
9438     (error "No more than one article may be marked"))
9439   (save-window-excursion
9440     (let ((gnus-article-buffer " *reparent*")
9441           (current-article (gnus-summary-article-number))
9442           ;; First grab the marked article, otherwise one line up.
9443           (parent-article (if (not (null gnus-newsgroup-processable))
9444                               (car gnus-newsgroup-processable)
9445                             (save-excursion
9446                               (if (eq (forward-line -1) 0)
9447                                   (gnus-summary-article-number)
9448                                 (error "Beginning of summary buffer"))))))
9449       (unless (not (eq current-article parent-article))
9450         (error "An article may not be self-referential"))
9451       (let ((message-id (mail-header-id
9452                          (gnus-summary-article-header parent-article))))
9453         (unless (and message-id (not (equal message-id "")))
9454           (error "No message-id in desired parent"))
9455         (gnus-with-article current-article
9456           (save-restriction
9457             (goto-char (point-min))
9458             (message-narrow-to-head)
9459             (if (re-search-forward "^References: " nil t)
9460                 (progn
9461                   (re-search-forward "^[^ \t]" nil t)
9462                   (forward-line -1)
9463                   (end-of-line)
9464                   (insert " " message-id))
9465               (insert "References: " message-id "\n"))))
9466         (set-buffer gnus-summary-buffer)
9467         (gnus-summary-unmark-all-processable)
9468         (gnus-summary-update-article current-article)
9469         (if (gnus-summary-goto-subject (cdr gnus-article-current) nil t)
9470             (gnus-summary-update-secondary-mark (cdr gnus-article-current)))
9471         (gnus-summary-rethread-current)
9472         (gnus-message 3 "Article %d is now the child of article %d"
9473                       current-article parent-article)))))
9474
9475 (defun gnus-summary-toggle-threads (&optional arg)
9476   "Toggle showing conversation threads.
9477 If ARG is positive number, turn showing conversation threads on."
9478   (interactive "P")
9479   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
9480     (setq gnus-show-threads
9481           (if (null arg) (not gnus-show-threads)
9482             (> (prefix-numeric-value arg) 0)))
9483     (gnus-summary-prepare)
9484     (gnus-summary-goto-subject current)
9485     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
9486     (gnus-summary-position-point)))
9487
9488 (defun gnus-summary-show-all-threads ()
9489   "Show all threads."
9490   (interactive)
9491   (save-excursion
9492     (let ((buffer-read-only nil))
9493       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
9494   (gnus-summary-position-point))
9495
9496 (defun gnus-summary-show-thread ()
9497   "Show thread subtrees.
9498 Returns nil if no thread was there to be shown."
9499   (interactive)
9500   (let ((buffer-read-only nil)
9501         (orig (point))
9502         ;; first goto end then to beg, to have point at beg after let
9503         (end (progn (end-of-line) (point)))
9504         (beg (progn (beginning-of-line) (point))))
9505     (prog1
9506         ;; Any hidden lines here?
9507         (search-forward "\r" end t)
9508       (subst-char-in-region beg end ?\^M ?\n t)
9509       (goto-char orig)
9510       (gnus-summary-position-point))))
9511
9512 (defun gnus-summary-hide-all-threads ()
9513   "Hide all thread subtrees."
9514   (interactive)
9515   (save-excursion
9516     (goto-char (point-min))
9517     (gnus-summary-hide-thread)
9518     (while (zerop (gnus-summary-next-thread 1 t))
9519       (gnus-summary-hide-thread)))
9520   (gnus-summary-position-point))
9521
9522 (defun gnus-summary-hide-thread ()
9523   "Hide thread subtrees.
9524 Returns nil if no threads were there to be hidden."
9525   (interactive)
9526   (let ((buffer-read-only nil)
9527         (start (point))
9528         (article (gnus-summary-article-number)))
9529     (goto-char start)
9530     ;; Go forward until either the buffer ends or the subthread
9531     ;; ends.
9532     (when (and (not (eobp))
9533                (or (zerop (gnus-summary-next-thread 1 t))
9534                    (goto-char (point-max))))
9535       (prog1
9536           (if (and (> (point) start)
9537                    (search-backward "\n" start t))
9538               (progn
9539                 (subst-char-in-region start (point) ?\n ?\^M)
9540                 (gnus-summary-goto-subject article))
9541             (goto-char start)
9542             nil)))))
9543
9544 (defun gnus-summary-go-to-next-thread (&optional previous)
9545   "Go to the same level (or less) next thread.
9546 If PREVIOUS is non-nil, go to previous thread instead.
9547 Return the article number moved to, or nil if moving was impossible."
9548   (let ((level (gnus-summary-thread-level))
9549         (way (if previous -1 1))
9550         (beg (point)))
9551     (forward-line way)
9552     (while (and (not (eobp))
9553                 (< level (gnus-summary-thread-level)))
9554       (forward-line way))
9555     (if (eobp)
9556         (progn
9557           (goto-char beg)
9558           nil)
9559       (setq beg (point))
9560       (prog1
9561           (gnus-summary-article-number)
9562         (goto-char beg)))))
9563
9564 (defun gnus-summary-next-thread (n &optional silent)
9565   "Go to the same level next N'th thread.
9566 If N is negative, search backward instead.
9567 Returns the difference between N and the number of skips actually
9568 done.
9569
9570 If SILENT, don't output messages."
9571   (interactive "p")
9572   (let ((backward (< n 0))
9573         (n (abs n)))
9574     (while (and (> n 0)
9575                 (gnus-summary-go-to-next-thread backward))
9576       (decf n))
9577     (unless silent
9578       (gnus-summary-position-point))
9579     (when (and (not silent) (/= 0 n))
9580       (gnus-message 7 "No more threads"))
9581     n))
9582
9583 (defun gnus-summary-prev-thread (n)
9584   "Go to the same level previous N'th thread.
9585 Returns the difference between N and the number of skips actually
9586 done."
9587   (interactive "p")
9588   (gnus-summary-next-thread (- n)))
9589
9590 (defun gnus-summary-go-down-thread ()
9591   "Go down one level in the current thread."
9592   (let ((children (gnus-summary-article-children)))
9593     (when children
9594       (gnus-summary-goto-subject (car children)))))
9595
9596 (defun gnus-summary-go-up-thread ()
9597   "Go up one level in the current thread."
9598   (let ((parent (gnus-summary-article-parent)))
9599     (when parent
9600       (gnus-summary-goto-subject parent))))
9601
9602 (defun gnus-summary-down-thread (n)
9603   "Go down thread N steps.
9604 If N is negative, go up instead.
9605 Returns the difference between N and how many steps down that were
9606 taken."
9607   (interactive "p")
9608   (let ((up (< n 0))
9609         (n (abs n)))
9610     (while (and (> n 0)
9611                 (if up (gnus-summary-go-up-thread)
9612                   (gnus-summary-go-down-thread)))
9613       (setq n (1- n)))
9614     (gnus-summary-position-point)
9615     (when (/= 0 n)
9616       (gnus-message 7 "Can't go further"))
9617     n))
9618
9619 (defun gnus-summary-up-thread (n)
9620   "Go up thread N steps.
9621 If N is negative, go down instead.
9622 Returns the difference between N and how many steps down that were
9623 taken."
9624   (interactive "p")
9625   (gnus-summary-down-thread (- n)))
9626
9627 (defun gnus-summary-top-thread ()
9628   "Go to the top of the thread."
9629   (interactive)
9630   (while (gnus-summary-go-up-thread))
9631   (gnus-summary-article-number))
9632
9633 (defun gnus-summary-kill-thread (&optional unmark)
9634   "Mark articles under current thread as read.
9635 If the prefix argument is positive, remove any kinds of marks.
9636 If the prefix argument is negative, tick articles instead."
9637   (interactive "P")
9638   (when unmark
9639     (setq unmark (prefix-numeric-value unmark)))
9640   (let ((articles (gnus-summary-articles-in-thread)))
9641     (save-excursion
9642       ;; Expand the thread.
9643       (gnus-summary-show-thread)
9644       ;; Mark all the articles.
9645       (while articles
9646         (gnus-summary-goto-subject (car articles))
9647         (cond ((null unmark)
9648                (gnus-summary-mark-article-as-read gnus-killed-mark))
9649               ((> unmark 0)
9650                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9651               (t
9652                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9653         (setq articles (cdr articles))))
9654     ;; Hide killed subtrees.
9655     (and (null unmark)
9656          gnus-thread-hide-killed
9657          (gnus-summary-hide-thread))
9658     ;; If marked as read, go to next unread subject.
9659     (when (null unmark)
9660       ;; Go to next unread subject.
9661       (gnus-summary-next-subject 1 t)))
9662   (gnus-set-mode-line 'summary))
9663
9664 ;; Summary sorting commands
9665
9666 (defun gnus-summary-sort-by-number (&optional reverse)
9667   "Sort the summary buffer by article number.
9668 Argument REVERSE means reverse order."
9669   (interactive "P")
9670   (gnus-summary-sort 'number reverse))
9671
9672 (defun gnus-summary-sort-by-author (&optional reverse)
9673   "Sort the summary buffer by author name alphabetically.
9674 If `case-fold-search' is non-nil, case of letters is ignored.
9675 Argument REVERSE means reverse order."
9676   (interactive "P")
9677   (gnus-summary-sort 'author reverse))
9678
9679 (defun gnus-summary-sort-by-subject (&optional reverse)
9680   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9681 If `case-fold-search' is non-nil, case of letters is ignored.
9682 Argument REVERSE means reverse order."
9683   (interactive "P")
9684   (gnus-summary-sort 'subject reverse))
9685
9686 (defun gnus-summary-sort-by-date (&optional reverse)
9687   "Sort the summary buffer by date.
9688 Argument REVERSE means reverse order."
9689   (interactive "P")
9690   (gnus-summary-sort 'date reverse))
9691
9692 (defun gnus-summary-sort-by-score (&optional reverse)
9693   "Sort the summary buffer by score.
9694 Argument REVERSE means reverse order."
9695   (interactive "P")
9696   (gnus-summary-sort 'score reverse))
9697
9698 (defun gnus-summary-sort-by-lines (&optional reverse)
9699   "Sort the summary buffer by the number of lines.
9700 Argument REVERSE means reverse order."
9701   (interactive "P")
9702   (gnus-summary-sort 'lines reverse))
9703
9704 (defun gnus-summary-sort-by-chars (&optional reverse)
9705   "Sort the summary buffer by article length.
9706 Argument REVERSE means reverse order."
9707   (interactive "P")
9708   (gnus-summary-sort 'chars reverse))
9709
9710 (defun gnus-summary-sort-by-original (&optional reverse)
9711   "Sort the summary buffer using the default sorting method.
9712 Argument REVERSE means reverse order."
9713   (interactive "P")
9714   (let* ((buffer-read-only)
9715          (gnus-summary-prepare-hook nil))
9716     ;; We do the sorting by regenerating the threads.
9717     (gnus-summary-prepare)
9718     ;; Hide subthreads if needed.
9719     (when (and gnus-show-threads gnus-thread-hide-subtree)
9720       (gnus-summary-hide-all-threads))))
9721
9722 (defun gnus-summary-sort (predicate reverse)
9723   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9724   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9725          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9726          (gnus-thread-sort-functions
9727           (if (not reverse)
9728               thread
9729             `(lambda (t1 t2)
9730                (,thread t2 t1))))
9731          (gnus-sort-gathered-threads-function
9732           gnus-thread-sort-functions)
9733          (gnus-article-sort-functions
9734           (if (not reverse)
9735               article
9736             `(lambda (t1 t2)
9737                (,article t2 t1))))
9738          (buffer-read-only)
9739          (gnus-summary-prepare-hook nil))
9740     ;; We do the sorting by regenerating the threads.
9741     (gnus-summary-prepare)
9742     ;; Hide subthreads if needed.
9743     (when (and gnus-show-threads gnus-thread-hide-subtree)
9744       (gnus-summary-hide-all-threads))))
9745
9746 ;; Summary saving commands.
9747
9748 (defun gnus-summary-save-article (&optional n not-saved)
9749   "Save the current article using the default saver function.
9750 If N is a positive number, save the N next articles.
9751 If N is a negative number, save the N previous articles.
9752 If N is nil and any articles have been marked with the process mark,
9753 save those articles instead.
9754 The variable `gnus-default-article-saver' specifies the saver function."
9755   (interactive "P")
9756   (let* ((articles (gnus-summary-work-articles n))
9757          (save-buffer (save-excursion
9758                         (nnheader-set-temp-buffer " *Gnus Save*")))
9759          (num (length articles))
9760          header file)
9761     (dolist (article articles)
9762       (setq header (gnus-summary-article-header article))
9763       (if (not (vectorp header))
9764           ;; This is a pseudo-article.
9765           (if (assq 'name header)
9766               (gnus-copy-file (cdr (assq 'name header)))
9767             (gnus-message 1 "Article %d is unsaveable" article))
9768         ;; This is a real article.
9769         (save-window-excursion
9770           (gnus-summary-select-article t nil nil article))
9771         (save-excursion
9772           (set-buffer save-buffer)
9773           (erase-buffer)
9774           (insert-buffer-substring gnus-original-article-buffer))
9775         (setq file (gnus-article-save save-buffer file num))
9776         (gnus-summary-remove-process-mark article)
9777         (unless not-saved
9778           (gnus-summary-set-saved-mark article))))
9779     (gnus-kill-buffer save-buffer)
9780     (gnus-summary-position-point)
9781     (gnus-set-mode-line 'summary)
9782     n))
9783
9784 (defun gnus-summary-pipe-output (&optional arg)
9785   "Pipe the current article to a subprocess.
9786 If N is a positive number, pipe the N next articles.
9787 If N is a negative number, pipe the N previous articles.
9788 If N is nil and any articles have been marked with the process mark,
9789 pipe those articles instead."
9790   (interactive "P")
9791   (require 'gnus-art)
9792   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9793     (gnus-summary-save-article arg t))
9794   (let ((buffer (get-buffer "*Shell Command Output*")))
9795     (if (and buffer
9796              (with-current-buffer buffer (> (point-max) (point-min))))
9797         (gnus-configure-windows 'pipe))))
9798
9799 (defun gnus-summary-save-article-mail (&optional arg)
9800   "Append the current article to an mail file.
9801 If N is a positive number, save the N next articles.
9802 If N is a negative number, save the N previous articles.
9803 If N is nil and any articles have been marked with the process mark,
9804 save those articles instead."
9805   (interactive "P")
9806   (require 'gnus-art)
9807   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9808     (gnus-summary-save-article arg)))
9809
9810 (defun gnus-summary-save-article-rmail (&optional arg)
9811   "Append the current article to an rmail file.
9812 If N is a positive number, save the N next articles.
9813 If N is a negative number, save the N previous articles.
9814 If N is nil and any articles have been marked with the process mark,
9815 save those articles instead."
9816   (interactive "P")
9817   (require 'gnus-art)
9818   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9819     (gnus-summary-save-article arg)))
9820
9821 (defun gnus-summary-save-article-file (&optional arg)
9822   "Append the current article to a file.
9823 If N is a positive number, save the N next articles.
9824 If N is a negative number, save the N previous articles.
9825 If N is nil and any articles have been marked with the process mark,
9826 save those articles instead."
9827   (interactive "P")
9828   (require 'gnus-art)
9829   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9830     (gnus-summary-save-article arg)))
9831
9832 (defun gnus-summary-write-article-file (&optional arg)
9833   "Write the current article to a file, deleting the previous file.
9834 If N is a positive number, save the N next articles.
9835 If N is a negative number, save the N previous articles.
9836 If N is nil and any articles have been marked with the process mark,
9837 save those articles instead."
9838   (interactive "P")
9839   (require 'gnus-art)
9840   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9841     (gnus-summary-save-article arg)))
9842
9843 (defun gnus-summary-save-article-body-file (&optional arg)
9844   "Append the current article body to a file.
9845 If N is a positive number, save the N next articles.
9846 If N is a negative number, save the N previous articles.
9847 If N is nil and any articles have been marked with the process mark,
9848 save those articles instead."
9849   (interactive "P")
9850   (require 'gnus-art)
9851   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9852     (gnus-summary-save-article arg)))
9853
9854 (defun gnus-summary-pipe-message (program)
9855   "Pipe the current article through PROGRAM."
9856   (interactive "sProgram: ")
9857   (gnus-summary-select-article)
9858   (let ((mail-header-separator ""))
9859     (gnus-eval-in-buffer-window gnus-article-buffer
9860       (save-restriction
9861         (widen)
9862         (let ((start (window-start))
9863               buffer-read-only)
9864           (message-pipe-buffer-body program)
9865           (set-window-start (get-buffer-window (current-buffer)) start))))))
9866
9867 (defun gnus-get-split-value (methods)
9868   "Return a value based on the split METHODS."
9869   (let (split-name method result match)
9870     (when methods
9871       (save-excursion
9872         (set-buffer gnus-original-article-buffer)
9873         (save-restriction
9874           (nnheader-narrow-to-headers)
9875           (while (and methods (not split-name))
9876             (goto-char (point-min))
9877             (setq method (pop methods))
9878             (setq match (car method))
9879             (when (cond
9880                    ((stringp match)
9881                     ;; Regular expression.
9882                     (ignore-errors
9883                       (re-search-forward match nil t)))
9884                    ((gnus-functionp match)
9885                     ;; Function.
9886                     (save-restriction
9887                       (widen)
9888                       (setq result (funcall match gnus-newsgroup-name))))
9889                    ((consp match)
9890                     ;; Form.
9891                     (save-restriction
9892                       (widen)
9893                       (setq result (eval match)))))
9894               (setq split-name (cdr method))
9895               (cond ((stringp result)
9896                      (push (expand-file-name
9897                             result gnus-article-save-directory)
9898                            split-name))
9899                     ((consp result)
9900                      (setq split-name (append result split-name)))))))))
9901     (nreverse split-name)))
9902
9903 (defun gnus-valid-move-group-p (group)
9904   (and (boundp group)
9905        (symbol-name group)
9906        (symbol-value group)
9907        (gnus-get-function (gnus-find-method-for-group
9908                            (symbol-name group)) 'request-accept-article t)))
9909
9910 (defun gnus-read-move-group-name (prompt default articles prefix)
9911   "Read a group name."
9912   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9913          (minibuffer-confirm-incomplete nil) ; XEmacs
9914          (prom
9915           (format "%s %s to:"
9916                   prompt
9917                   (if (> (length articles) 1)
9918                       (format "these %d articles" (length articles))
9919                     "this article")))
9920          (to-newsgroup
9921           (cond
9922            ((null split-name)
9923             (gnus-completing-read default prom
9924                                   gnus-active-hashtb
9925                                   'gnus-valid-move-group-p
9926                                   nil prefix
9927                                   'gnus-group-history))
9928            ((= 1 (length split-name))
9929             (gnus-completing-read (car split-name) prom
9930                                   gnus-active-hashtb
9931                                   'gnus-valid-move-group-p
9932                                   nil nil
9933                                   'gnus-group-history))
9934            (t
9935             (gnus-completing-read nil prom
9936                                   (mapcar (lambda (el) (list el))
9937                                           (nreverse split-name))
9938                                   nil nil nil
9939                                   'gnus-group-history))))
9940          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9941     (when to-newsgroup
9942       (if (or (string= to-newsgroup "")
9943               (string= to-newsgroup prefix))
9944           (setq to-newsgroup default))
9945       (unless to-newsgroup
9946         (error "No group name entered"))
9947       (or (gnus-active to-newsgroup)
9948           (gnus-activate-group to-newsgroup nil nil to-method)
9949           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9950                                      to-newsgroup))
9951               (or (and (gnus-request-create-group to-newsgroup to-method)
9952                        (gnus-activate-group
9953                         to-newsgroup nil nil to-method)
9954                        (gnus-subscribe-group to-newsgroup))
9955                   (error "Couldn't create group %s" to-newsgroup)))
9956           (error "No such group: %s" to-newsgroup)))
9957     to-newsgroup))
9958
9959 (defun gnus-summary-save-parts (type dir n &optional reverse)
9960   "Save parts matching TYPE to DIR.
9961 If REVERSE, save parts that do not match TYPE."
9962   (interactive
9963    (list (read-string "Save parts of type: "
9964                       (or (car gnus-summary-save-parts-type-history)
9965                           gnus-summary-save-parts-default-mime)
9966                       'gnus-summary-save-parts-type-history)
9967          (setq gnus-summary-save-parts-last-directory
9968                (read-file-name "Save to directory: "
9969                                gnus-summary-save-parts-last-directory
9970                                nil t))
9971          current-prefix-arg))
9972   (gnus-summary-iterate n
9973     (let ((gnus-display-mime-function nil)
9974           (gnus-inhibit-treatment t))
9975       (gnus-summary-select-article))
9976     (save-excursion
9977       (set-buffer gnus-article-buffer)
9978       (let ((handles (or gnus-article-mime-handles
9979                          (mm-dissect-buffer) (mm-uu-dissect))))
9980         (when handles
9981           (gnus-summary-save-parts-1 type dir handles reverse)
9982           (unless gnus-article-mime-handles ;; Don't destroy this case.
9983             (mm-destroy-parts handles)))))))
9984
9985 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9986   (if (stringp (car handle))
9987       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9988               (cdr handle))
9989     (when (if reverse
9990               (not (string-match type (mm-handle-media-type handle)))
9991             (string-match type (mm-handle-media-type handle)))
9992       (let ((file (expand-file-name
9993                    (file-name-nondirectory
9994                     (or
9995                      (mail-content-type-get
9996                       (mm-handle-disposition handle) 'filename)
9997                      (concat gnus-newsgroup-name
9998                              "." (number-to-string
9999                                   (cdr gnus-article-current)))))
10000                    dir)))
10001         (unless (file-exists-p file)
10002           (mm-save-part-to-file handle file))))))
10003
10004 ;; Summary extract commands
10005
10006 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
10007   (let ((buffer-read-only nil)
10008         (article (gnus-summary-article-number))
10009         after-article b e)
10010     (unless (gnus-summary-goto-subject article)
10011       (error "No such article: %d" article))
10012     (gnus-summary-position-point)
10013     ;; If all commands are to be bunched up on one line, we collect
10014     ;; them here.
10015     (unless gnus-view-pseudos-separately
10016       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
10017             files action)
10018         (while ps
10019           (setq action (cdr (assq 'action (car ps))))
10020           (setq files (list (cdr (assq 'name (car ps)))))
10021           (while (and ps (cdr ps)
10022                       (string= (or action "1")
10023                                (or (cdr (assq 'action (cadr ps))) "2")))
10024             (push (cdr (assq 'name (cadr ps))) files)
10025             (setcdr ps (cddr ps)))
10026           (when files
10027             (when (not (string-match "%s" action))
10028               (push " " files))
10029             (push " " files)
10030             (when (assq 'execute (car ps))
10031               (setcdr (assq 'execute (car ps))
10032                       (funcall (if (string-match "%s" action)
10033                                    'format 'concat)
10034                                action
10035                                (mapconcat
10036                                 (lambda (f)
10037                                   (if (equal f " ")
10038                                       f
10039                                     (mm-quote-arg f)))
10040                                 files " ")))))
10041           (setq ps (cdr ps)))))
10042     (if (and gnus-view-pseudos (not not-view))
10043         (while pslist
10044           (when (assq 'execute (car pslist))
10045             (gnus-execute-command (cdr (assq 'execute (car pslist)))
10046                                   (eq gnus-view-pseudos 'not-confirm)))
10047           (setq pslist (cdr pslist)))
10048       (save-excursion
10049         (while pslist
10050           (setq after-article (or (cdr (assq 'article (car pslist)))
10051                                   (gnus-summary-article-number)))
10052           (gnus-summary-goto-subject after-article)
10053           (forward-line 1)
10054           (setq b (point))
10055           (insert "    " (file-name-nondirectory
10056                           (cdr (assq 'name (car pslist))))
10057                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
10058           (setq e (point))
10059           (forward-line -1)             ; back to `b'
10060           (gnus-add-text-properties
10061            b (1- e) (list 'gnus-number gnus-reffed-article-number
10062                           gnus-mouse-face-prop gnus-mouse-face))
10063           (gnus-data-enter
10064            after-article gnus-reffed-article-number
10065            gnus-unread-mark b (car pslist) 0 (- e b))
10066           (push gnus-reffed-article-number gnus-newsgroup-unreads)
10067           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
10068           (setq pslist (cdr pslist)))))))
10069
10070 (defun gnus-pseudos< (p1 p2)
10071   (let ((c1 (cdr (assq 'action p1)))
10072         (c2 (cdr (assq 'action p2))))
10073     (and c1 c2 (string< c1 c2))))
10074
10075 (defun gnus-request-pseudo-article (props)
10076   (cond ((assq 'execute props)
10077          (gnus-execute-command (cdr (assq 'execute props)))))
10078   (let ((gnus-current-article (gnus-summary-article-number)))
10079     (gnus-run-hooks 'gnus-mark-article-hook)))
10080
10081 (defun gnus-execute-command (command &optional automatic)
10082   (save-excursion
10083     (gnus-article-setup-buffer)
10084     (set-buffer gnus-article-buffer)
10085     (setq buffer-read-only nil)
10086     (let ((command (if automatic command
10087                      (read-string "Command: " (cons command 0)))))
10088       (erase-buffer)
10089       (insert "$ " command "\n\n")
10090       (if gnus-view-pseudo-asynchronously
10091           (start-process "gnus-execute" (current-buffer) shell-file-name
10092                          shell-command-switch command)
10093         (call-process shell-file-name nil t nil
10094                       shell-command-switch command)))))
10095
10096 ;; Summary kill commands.
10097
10098 (defun gnus-summary-edit-global-kill (article)
10099   "Edit the \"global\" kill file."
10100   (interactive (list (gnus-summary-article-number)))
10101   (gnus-group-edit-global-kill article))
10102
10103 (defun gnus-summary-edit-local-kill ()
10104   "Edit a local kill file applied to the current newsgroup."
10105   (interactive)
10106   (setq gnus-current-headers (gnus-summary-article-header))
10107   (gnus-group-edit-local-kill
10108    (gnus-summary-article-number) gnus-newsgroup-name))
10109
10110 ;;; Header reading.
10111
10112 (defun gnus-read-header (id &optional header)
10113   "Read the headers of article ID and enter them into the Gnus system."
10114   (let ((group gnus-newsgroup-name)
10115         (gnus-override-method
10116          (or
10117           gnus-override-method
10118           (and (gnus-news-group-p gnus-newsgroup-name)
10119                (car (gnus-refer-article-methods)))))
10120         where)
10121     ;; First we check to see whether the header in question is already
10122     ;; fetched.
10123     (if (stringp id)
10124         ;; This is a Message-ID.
10125         (setq header (or header (gnus-id-to-header id)))
10126       ;; This is an article number.
10127       (setq header (or header (gnus-summary-article-header id))))
10128     (if (and header
10129              (not (gnus-summary-article-sparse-p (mail-header-number header))))
10130         ;; We have found the header.
10131         header
10132       ;; If this is a sparse article, we have to nix out its
10133       ;; previous entry in the thread hashtb.
10134       (when (and header
10135                  (gnus-summary-article-sparse-p (mail-header-number header)))
10136         (let* ((parent (gnus-parent-id (mail-header-references header)))
10137                (thread (and parent (gnus-id-to-thread parent))))
10138           (when thread
10139             (delq (assq header thread) thread))))
10140       ;; We have to really fetch the header to this article.
10141       (save-excursion
10142         (set-buffer nntp-server-buffer)
10143         (when (setq where (gnus-request-head id group))
10144           (nnheader-fold-continuation-lines)
10145           (goto-char (point-max))
10146           (insert ".\n")
10147           (goto-char (point-min))
10148           (insert "211 ")
10149           (princ (cond
10150                   ((numberp id) id)
10151                   ((cdr where) (cdr where))
10152                   (header (mail-header-number header))
10153                   (t gnus-reffed-article-number))
10154                  (current-buffer))
10155           (insert " Article retrieved.\n"))
10156         (if (or (not where)
10157                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
10158             ()                          ; Malformed head.
10159           (unless (gnus-summary-article-sparse-p (mail-header-number header))
10160             (when (and (stringp id)
10161                        (not (string= (gnus-group-real-name group)
10162                                      (car where))))
10163               ;; If we fetched by Message-ID and the article came
10164               ;; from a different group, we fudge some bogus article
10165               ;; numbers for this article.
10166               (mail-header-set-number header gnus-reffed-article-number))
10167             (save-excursion
10168               (set-buffer gnus-summary-buffer)
10169               (decf gnus-reffed-article-number)
10170               (gnus-remove-header (mail-header-number header))
10171               (push header gnus-newsgroup-headers)
10172               (setq gnus-current-headers header)
10173               (push (mail-header-number header) gnus-newsgroup-limit)))
10174           header)))))
10175
10176 (defun gnus-remove-header (number)
10177   "Remove header NUMBER from `gnus-newsgroup-headers'."
10178   (if (and gnus-newsgroup-headers
10179            (= number (mail-header-number (car gnus-newsgroup-headers))))
10180       (pop gnus-newsgroup-headers)
10181     (let ((headers gnus-newsgroup-headers))
10182       (while (and (cdr headers)
10183                   (not (= number (mail-header-number (cadr headers)))))
10184         (pop headers))
10185       (when (cdr headers)
10186         (setcdr headers (cddr headers))))))
10187
10188 ;;;
10189 ;;; summary highlights
10190 ;;;
10191
10192 (defun gnus-highlight-selected-summary ()
10193   "Highlight selected article in summary buffer."
10194   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
10195   (when gnus-summary-selected-face
10196     (save-excursion
10197       (let* ((beg (progn (beginning-of-line) (point)))
10198              (end (progn (end-of-line) (point)))
10199              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
10200              (from (if (get-text-property beg gnus-mouse-face-prop)
10201                        beg
10202                      (or (next-single-property-change
10203                           beg gnus-mouse-face-prop nil end)
10204                          beg)))
10205              (to
10206               (if (= from end)
10207                   (- from 2)
10208                 (or (next-single-property-change
10209                      from gnus-mouse-face-prop nil end)
10210                     end))))
10211         ;; If no mouse-face prop on line we will have to = from = end,
10212         ;; so we highlight the entire line instead.
10213         (when (= (+ to 2) from)
10214           (setq from beg)
10215           (setq to end))
10216         (if gnus-newsgroup-selected-overlay
10217             ;; Move old overlay.
10218             (gnus-move-overlay
10219              gnus-newsgroup-selected-overlay from to (current-buffer))
10220           ;; Create new overlay.
10221           (gnus-overlay-put
10222            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
10223            'face gnus-summary-selected-face))))))
10224
10225 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
10226 (defun gnus-summary-highlight-line ()
10227   "Highlight current line according to `gnus-summary-highlight'."
10228   (let* ((list gnus-summary-highlight)
10229          (p (point))
10230          (end (progn (end-of-line) (point)))
10231          ;; now find out where the line starts and leave point there.
10232          (beg (progn (beginning-of-line) (point)))
10233          (article (gnus-summary-article-number))
10234          (score (or (cdr (assq (or article gnus-current-article)
10235                                gnus-newsgroup-scored))
10236                     gnus-summary-default-score 0))
10237          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
10238          (inhibit-read-only t))
10239     ;; Eval the cars of the lists until we find a match.
10240     (let ((default gnus-summary-default-score)
10241           (default-high gnus-summary-default-high-score)
10242           (default-low gnus-summary-default-low-score))
10243       (while (and list
10244                   (not (eval (caar list))))
10245         (setq list (cdr list))))
10246     (let ((face (cdar list)))
10247       (unless (eq face (get-text-property beg 'face))
10248         (gnus-put-text-property-excluding-characters-with-faces
10249          beg end 'face
10250          (setq face (if (boundp face) (symbol-value face) face)))
10251         (when gnus-summary-highlight-line-function
10252           (funcall gnus-summary-highlight-line-function article face))))
10253     (goto-char p)))
10254
10255 (defun gnus-update-read-articles (group unread &optional compute)
10256   "Update the list of read articles in GROUP."
10257   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
10258          (entry (gnus-gethash group gnus-newsrc-hashtb))
10259          (info (nth 2 entry))
10260          (prev 1)
10261          (unread (sort (copy-sequence unread) '<))
10262          read)
10263     (if (or (not info) (not active))
10264         ;; There is no info on this group if it was, in fact,
10265         ;; killed.  Gnus stores no information on killed groups, so
10266         ;; there's nothing to be done.
10267         ;; One could store the information somewhere temporarily,
10268         ;; perhaps...  Hmmm...
10269         ()
10270       ;; Remove any negative articles numbers.
10271       (while (and unread (< (car unread) 0))
10272         (setq unread (cdr unread)))
10273       ;; Remove any expired article numbers
10274       (while (and unread (< (car unread) (car active)))
10275         (setq unread (cdr unread)))
10276       ;; Compute the ranges of read articles by looking at the list of
10277       ;; unread articles.
10278       (while unread
10279         (when (/= (car unread) prev)
10280           (push (if (= prev (1- (car unread))) prev
10281                   (cons prev (1- (car unread))))
10282                 read))
10283         (setq prev (1+ (car unread)))
10284         (setq unread (cdr unread)))
10285       (when (<= prev (cdr active))
10286         (push (cons prev (cdr active)) read))
10287       (setq read (if (> (length read) 1) (nreverse read) read))
10288       (if compute
10289           read
10290         (save-excursion
10291           (let (setmarkundo)
10292             ;; Propagate the read marks to the backend.
10293             (when (gnus-check-backend-function 'request-set-mark group)
10294               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
10295                     (add (gnus-remove-from-range read (gnus-info-read info))))
10296                 (when (or add del)
10297                   (unless (gnus-check-group group)
10298                     (error "Can't open server for %s" group))
10299                   (gnus-request-set-mark
10300                    group (delq nil (list (if add (list add 'add '(read)))
10301                                          (if del (list del 'del '(read))))))
10302                   (setq setmarkundo
10303                         `(gnus-request-set-mark
10304                           ,group
10305                           ',(delq nil (list
10306                                        (if del (list del 'add '(read)))
10307                                        (if add (list add 'del '(read))))))))))
10308             (set-buffer gnus-group-buffer)
10309             (gnus-undo-register
10310               `(progn
10311                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
10312                  (gnus-info-set-read ',info ',(gnus-info-read info))
10313                  (gnus-get-unread-articles-in-group ',info
10314                                                     (gnus-active ,group))
10315                  (gnus-group-update-group ,group t)
10316                  ,setmarkundo))))
10317         ;; Enter this list into the group info.
10318         (gnus-info-set-read info read)
10319         ;; Set the number of unread articles in gnus-newsrc-hashtb.
10320         (gnus-get-unread-articles-in-group info (gnus-active group))
10321         t))))
10322
10323 (defun gnus-offer-save-summaries ()
10324   "Offer to save all active summary buffers."
10325   (let (buffers)
10326     ;; Go through all buffers and find all summaries.
10327     (dolist (buffer (buffer-list))
10328       (when (and (setq buffer (buffer-name buffer))
10329                  (string-match "Summary" buffer)
10330                  (save-excursion
10331                    (set-buffer buffer)
10332                    ;; We check that this is, indeed, a summary buffer.
10333                    (and (eq major-mode 'gnus-summary-mode)
10334                         ;; Also make sure this isn't bogus.
10335                         gnus-newsgroup-prepared
10336                         ;; Also make sure that this isn't a
10337                         ;; dead summary buffer.
10338                         (not gnus-dead-summary-mode))))
10339         (push buffer buffers)))
10340     ;; Go through all these summary buffers and offer to save them.
10341     (when buffers
10342       (save-excursion
10343         (map-y-or-n-p
10344          "Update summary buffer %s? "
10345          (lambda (buf)
10346            (switch-to-buffer buf)
10347            (gnus-summary-exit))
10348          buffers)))))
10349
10350 (defun gnus-summary-setup-default-charset ()
10351   "Setup newsgroup default charset."
10352   (if (equal gnus-newsgroup-name "nndraft:drafts")
10353       (setq gnus-newsgroup-charset nil)
10354     (let* ((ignored-charsets
10355             (or gnus-newsgroup-ephemeral-ignored-charsets
10356                 (append
10357                  (and gnus-newsgroup-name
10358                       (gnus-parameter-ignored-charsets gnus-newsgroup-name))
10359                  gnus-newsgroup-ignored-charsets))))
10360       (setq gnus-newsgroup-charset
10361             (or gnus-newsgroup-ephemeral-charset
10362                 (and gnus-newsgroup-name
10363                      (gnus-parameter-charset gnus-newsgroup-name))
10364                 gnus-default-charset))
10365       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
10366            ignored-charsets))))
10367
10368 ;;;
10369 ;;; Mime Commands
10370 ;;;
10371
10372 (defun gnus-summary-display-buttonized (&optional show-all-parts)
10373   "Display the current article buffer fully MIME-buttonized.
10374 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
10375 treated as multipart/mixed."
10376   (interactive "P")
10377   (require 'gnus-art)
10378   (let ((gnus-unbuttonized-mime-types nil)
10379         (gnus-mime-display-multipart-as-mixed show-all-parts))
10380     (gnus-summary-show-article)))
10381
10382 (defun gnus-summary-repair-multipart (article)
10383   "Add a Content-Type header to a multipart article without one."
10384   (interactive (list (gnus-summary-article-number)))
10385   (gnus-with-article article
10386     (message-narrow-to-head)
10387     (message-remove-header "Mime-Version")
10388     (goto-char (point-max))
10389     (insert "Mime-Version: 1.0\n")
10390     (widen)
10391     (when (search-forward "\n--" nil t)
10392       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
10393         (message-narrow-to-head)
10394         (message-remove-header "Content-Type")
10395         (goto-char (point-max))
10396         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
10397                         separator))
10398         (widen))))
10399   (let (gnus-mark-article-hook)
10400     (gnus-summary-select-article t t nil article)))
10401
10402 (defun gnus-summary-toggle-display-buttonized ()
10403   "Toggle the buttonizing of the article buffer."
10404   (interactive)
10405   (require 'gnus-art)
10406   (if (setq gnus-inhibit-mime-unbuttonizing
10407             (not gnus-inhibit-mime-unbuttonizing))
10408       (let ((gnus-unbuttonized-mime-types nil))
10409         (gnus-summary-show-article))
10410     (gnus-summary-show-article)))
10411
10412 ;;;
10413 ;;; Generic summary marking commands
10414 ;;;
10415
10416 (defvar gnus-summary-marking-alist
10417   '((read gnus-del-mark "d")
10418     (unread gnus-unread-mark "u")
10419     (ticked gnus-ticked-mark "!")
10420     (dormant gnus-dormant-mark "?")
10421     (expirable gnus-expirable-mark "e"))
10422   "An alist of names/marks/keystrokes.")
10423
10424 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10425 (defvar gnus-summary-mark-map)
10426
10427 (defun gnus-summary-make-all-marking-commands ()
10428   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10429   (dolist (elem gnus-summary-marking-alist)
10430     (apply 'gnus-summary-make-marking-command elem)))
10431
10432 (defun gnus-summary-make-marking-command (name mark keystroke)
10433   (let ((map (make-sparse-keymap)))
10434     (define-key gnus-summary-generic-mark-map keystroke map)
10435     (dolist (lway `((next "next" next nil "n")
10436                     (next-unread "next unread" next t "N")
10437                     (prev "previous" prev nil "p")
10438                     (prev-unread "previous unread" prev t "P")
10439                     (nomove "" nil nil ,keystroke)))
10440       (let ((func (gnus-summary-make-marking-command-1
10441                    mark (car lway) lway name)))
10442         (setq func (eval func))
10443         (define-key map (nth 4 lway) func)))))
10444
10445 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10446   `(defun ,(intern
10447             (format "gnus-summary-put-mark-as-%s%s"
10448                     name (if (eq way 'nomove)
10449                              ""
10450                            (concat "-" (symbol-name way)))))
10451      (n)
10452      ,(format
10453        "Mark the current article as %s%s.
10454 If N, the prefix, then repeat N times.
10455 If N is negative, move in reverse order.
10456 The difference between N and the actual number of articles marked is
10457 returned."
10458        name (cadr lway))
10459      (interactive "p")
10460      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10461
10462 (defun gnus-summary-generic-mark (n mark move unread)
10463   "Mark N articles with MARK."
10464   (unless (eq major-mode 'gnus-summary-mode)
10465     (error "This command can only be used in the summary buffer"))
10466   (gnus-summary-show-thread)
10467   (let ((nummove
10468          (cond
10469           ((eq move 'next) 1)
10470           ((eq move 'prev) -1)
10471           (t 0))))
10472     (if (zerop nummove)
10473         (setq n 1)
10474       (when (< n 0)
10475         (setq n (abs n)
10476               nummove (* -1 nummove))))
10477     (while (and (> n 0)
10478                 (gnus-summary-mark-article nil mark)
10479                 (zerop (gnus-summary-next-subject nummove unread t)))
10480       (setq n (1- n)))
10481     (when (/= 0 n)
10482       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10483     (gnus-summary-recenter)
10484     (gnus-summary-position-point)
10485     (gnus-set-mode-line 'summary)
10486     n))
10487
10488 (defun gnus-summary-insert-articles (articles)
10489   (when (setq articles
10490               (gnus-set-difference articles
10491                                    (mapcar (lambda (h) (mail-header-number h))
10492                                            gnus-newsgroup-headers)))
10493     (setq gnus-newsgroup-headers
10494           (merge 'list
10495                  gnus-newsgroup-headers
10496                  (gnus-fetch-headers articles)
10497                  'gnus-article-sort-by-number))
10498     ;; Suppress duplicates?
10499     (when gnus-suppress-duplicates
10500       (gnus-dup-suppress-articles))
10501
10502     ;; We might want to build some more threads first.
10503     (when (and gnus-fetch-old-headers
10504                (eq gnus-headers-retrieved-by 'nov))
10505       (if (eq gnus-fetch-old-headers 'invisible)
10506           (gnus-build-all-threads)
10507         (gnus-build-old-threads)))
10508     ;; Let the Gnus agent mark articles as read.
10509     (when gnus-agent
10510       (gnus-agent-get-undownloaded-list))
10511     ;; Remove list identifiers from subject
10512     (when gnus-list-identifiers
10513       (gnus-summary-remove-list-identifiers))
10514     ;; First and last article in this newsgroup.
10515     (when gnus-newsgroup-headers
10516       (setq gnus-newsgroup-begin
10517             (mail-header-number (car gnus-newsgroup-headers))
10518             gnus-newsgroup-end
10519             (mail-header-number
10520              (gnus-last-element gnus-newsgroup-headers))))
10521     (when gnus-use-scoring
10522       (gnus-possibly-score-headers))))
10523
10524 (defun gnus-summary-insert-old-articles (&optional all)
10525   "Insert all old articles in this group.
10526 If ALL is non-nil, already read articles become readable.
10527 If ALL is a number, fetch this number of articles."
10528   (interactive "P")
10529   (prog1
10530       (let ((old (mapcar 'car gnus-newsgroup-data))
10531             (i (car gnus-newsgroup-active))
10532             older len)
10533         (while (<= i (cdr gnus-newsgroup-active))
10534           (or (memq i old) (push i older))
10535           (incf i))
10536         (setq len (length older))
10537         (cond
10538          ((null older) nil)
10539          ((numberp all)
10540           (if (< all len)
10541               (setq older (subseq older 0 all))))
10542          (all nil)
10543          (t
10544           (if (and (numberp gnus-large-newsgroup)
10545                    (> len gnus-large-newsgroup))
10546               (let ((input
10547                      (read-string
10548                       (format
10549                        "How many articles from %s (default %d): "
10550                        (gnus-limit-string
10551                         (gnus-group-decoded-name gnus-newsgroup-name) 35)
10552                        len))))
10553                 (unless (string-match "^[ \t]*$" input)
10554                   (setq all (string-to-number input))
10555                   (if (< all len)
10556                       (setq older (subseq older 0 all))))))))
10557         (if (not older)
10558             (message "No old news.")
10559           (gnus-summary-insert-articles older)
10560           (gnus-summary-limit (gnus-union older old))))
10561     (gnus-summary-position-point)))
10562
10563 (defun gnus-summary-insert-new-articles ()
10564   "Insert all new articles in this group."
10565   (interactive)
10566   (prog1
10567       (let ((old (mapcar 'car gnus-newsgroup-data))
10568             (old-active gnus-newsgroup-active)
10569             (nnmail-fetched-sources (list t))
10570             i new)
10571         (setq gnus-newsgroup-active
10572               (gnus-activate-group gnus-newsgroup-name 'scan))
10573         (setq i (1+ (cdr old-active)))
10574         (while (<= i (cdr gnus-newsgroup-active))
10575           (push i new)
10576           (incf i))
10577         (if (not new)
10578             (message "No gnus is bad news.")
10579           (setq new (nreverse new))
10580           (gnus-summary-insert-articles new)
10581           (setq gnus-newsgroup-unreads
10582                 (append gnus-newsgroup-unreads new))
10583           (gnus-summary-limit (gnus-union old new))))
10584     (gnus-summary-position-point)))
10585
10586 (gnus-summary-make-all-marking-commands)
10587
10588 (gnus-ems-redefine)
10589
10590 (provide 'gnus-sum)
10591
10592 (run-hooks 'gnus-sum-load-hook)
10593
10594 ;;; gnus-sum.el ends here