Synch with `t-gnus-6_14' and Gnus.
[elisp/gnus.git-] / lisp / gnus-sum.el
1 ;;; gnus-sum.el --- summary mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;         Katsumi Yamaoka  <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32
33 (require 'gnus)
34 (require 'gnus-group)
35 (require 'gnus-spec)
36 (require 'gnus-range)
37 (require 'gnus-int)
38 (require 'gnus-undo)
39 (require 'gnus-util)
40 (require 'mime-view)
41
42 (eval-when-compile
43   (require 'mime-play)
44   (require 'static))
45
46 (eval-and-compile
47   (autoload 'gnus-cache-articles-in-group "gnus-cache")
48   (autoload 'pgg-decrypt-region "pgg" nil t)
49   (autoload 'pgg-verify-region "pgg" nil t))
50
51 (autoload 'gnus-summary-limit-include-cached "gnus-cache" nil t)
52 (autoload 'gnus-set-summary-default-charset "gnus-i18n" nil t)
53
54 (defcustom gnus-kill-summary-on-exit t
55   "*If non-nil, kill the summary buffer when you exit from it.
56 If nil, the summary will become a \"*Dead Summary*\" buffer, and
57 it will be killed sometime later."
58   :group 'gnus-summary-exit
59   :type 'boolean)
60
61 (defcustom gnus-fetch-old-headers nil
62   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
63 If an unread article in the group refers to an older, already read (or
64 just marked as read) article, the old article will not normally be
65 displayed in the Summary buffer.  If this variable is non-nil, Gnus
66 will attempt to grab the headers to the old articles, and thereby
67 build complete threads.  If it has the value `some', only enough
68 headers to connect otherwise loose threads will be displayed.  This
69 variable can also be a number.  In that case, no more than that number
70 of old headers will be fetched.  If it has the value `invisible', all
71 old headers will be fetched, but none will be displayed.
72
73 The server has to support NOV for any of this to work."
74   :group 'gnus-thread
75   :type '(choice (const :tag "off" nil)
76                  (const some)
77                  number
78                  (sexp :menu-tag "other" t)))
79
80 (defcustom gnus-refer-thread-limit 200
81   "*The number of old headers to fetch when doing \\<gnus-summary-mode-map>\\[gnus-summary-refer-thread].
82 If t, fetch all the available old headers."
83   :group 'gnus-thread
84   :type '(choice number
85                  (sexp :menu-tag "other" t)))
86
87 (defcustom gnus-summary-make-false-root 'adopt
88   "*nil means that Gnus won't gather loose threads.
89 If the root of a thread has expired or been read in a previous
90 session, the information necessary to build a complete thread has been
91 lost.  Instead of having many small sub-threads from this original thread
92 scattered all over the summary buffer, Gnus can gather them.
93
94 If non-nil, Gnus will try to gather all loose sub-threads from an
95 original thread into one large thread.
96
97 If this variable is non-nil, it should be one of `none', `adopt',
98 `dummy' or `empty'.
99
100 If this variable is `none', Gnus will not make a false root, but just
101 present the sub-threads after another.
102 If this variable is `dummy', Gnus will create a dummy root that will
103 have all the sub-threads as children.
104 If this variable is `adopt', Gnus will make one of the \"children\"
105 the parent and mark all the step-children as such.
106 If this variable is `empty', the \"children\" are printed with empty
107 subject fields.  (Or rather, they will be printed with a string
108 given by the `gnus-summary-same-subject' variable.)"
109   :group 'gnus-thread
110   :type '(choice (const :tag "off" nil)
111                  (const none)
112                  (const dummy)
113                  (const adopt)
114                  (const empty)))
115
116 (defcustom gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
117   "*A regexp to match subjects to be excluded from loose thread gathering.
118 As loose thread gathering is done on subjects only, that means that
119 there can be many false gatherings performed.  By rooting out certain
120 common subjects, gathering might become saner."
121   :group 'gnus-thread
122   :type 'regexp)
123
124 (defcustom gnus-summary-gather-subject-limit nil
125   "*Maximum length of subject comparisons when gathering loose threads.
126 Use nil to compare full subjects.  Setting this variable to a low
127 number will help gather threads that have been corrupted by
128 newsreaders chopping off subject lines, but it might also mean that
129 unrelated articles that have subject that happen to begin with the
130 same few characters will be incorrectly gathered.
131
132 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
133 comparing subjects."
134   :group 'gnus-thread
135   :type '(choice (const :tag "off" nil)
136                  (const fuzzy)
137                  (sexp :menu-tag "on" t)))
138
139 (defcustom gnus-simplify-subject-functions nil
140   "List of functions taking a string argument that simplify subjects.
141 The functions are applied recursively.
142
143 Useful functions to put in this list include: `gnus-simplify-subject-re',
144 `gnus-simplify-subject-fuzzy' and `gnus-simplify-whitespace'."
145   :group 'gnus-thread
146   :type '(repeat function))
147
148 (defcustom gnus-simplify-ignored-prefixes nil
149   "*Regexp, matches for which are removed from subject lines when simplifying fuzzily."
150   :group 'gnus-thread
151   :type '(choice (const :tag "off" nil)
152                  regexp))
153
154 (defcustom gnus-build-sparse-threads nil
155   "*If non-nil, fill in the gaps in threads.
156 If `some', only fill in the gaps that are needed to tie loose threads
157 together.  If `more', fill in all leaf nodes that Gnus can find.  If
158 non-nil and non-`some', fill in all gaps that Gnus manages to guess."
159   :group 'gnus-thread
160   :type '(choice (const :tag "off" nil)
161                  (const some)
162                  (const more)
163                  (sexp :menu-tag "all" t)))
164
165 (defcustom gnus-summary-thread-gathering-function
166   'gnus-gather-threads-by-subject
167   "*Function used for gathering loose threads.
168 There are two pre-defined functions: `gnus-gather-threads-by-subject',
169 which only takes Subjects into consideration; and
170 `gnus-gather-threads-by-references', which compared the References
171 headers of the articles to find matches."
172   :group 'gnus-thread
173   :type '(radio (function-item gnus-gather-threads-by-subject)
174                 (function-item gnus-gather-threads-by-references)
175                 (function :tag "other")))
176
177 (defcustom gnus-summary-same-subject ""
178   "*String indicating that the current article has the same subject as the previous.
179 This variable will only be used if the value of
180 `gnus-summary-make-false-root' is `empty'."
181   :group 'gnus-summary-format
182   :type 'string)
183
184 (defcustom gnus-summary-goto-unread t
185   "*If t, many commands will go to the next unread article.
186 This applies to marking commands as well as other commands that
187 \"naturally\" select the next article, like, for instance, `SPC' at
188 the end of an article.
189
190 If nil, the marking commands do NOT go to the next unread article
191 (they go to the next article instead).  If `never', commands that
192 usually go to the next unread article, will go to the next article,
193 whether it is read or not."
194   :group 'gnus-summary-marks
195   :link '(custom-manual "(gnus)Setting Marks")
196   :type '(choice (const :tag "off" nil)
197                  (const never)
198                  (sexp :menu-tag "on" t)))
199
200 (defcustom gnus-summary-default-score 0
201   "*Default article score level.
202 All scores generated by the score files will be added to this score.
203 If this variable is nil, scoring will be disabled."
204   :group 'gnus-score-default
205   :type '(choice (const :tag "disable")
206                  integer))
207
208 (defcustom gnus-summary-zcore-fuzz 0
209   "*Fuzziness factor for the zcore in the summary buffer.
210 Articles with scores closer than this to `gnus-summary-default-score'
211 will not be marked."
212   :group 'gnus-summary-format
213   :type 'integer)
214
215 (defcustom gnus-simplify-subject-fuzzy-regexp nil
216   "*Strings to be removed when doing fuzzy matches.
217 This can either be a regular expression or list of regular expressions
218 that will be removed from subject strings if fuzzy subject
219 simplification is selected."
220   :group 'gnus-thread
221   :type '(repeat regexp))
222
223 (defcustom gnus-show-threads t
224   "*If non-nil, display threads in summary mode."
225   :group 'gnus-thread
226   :type 'boolean)
227
228 (defcustom gnus-thread-hide-subtree nil
229   "*If non-nil, hide all threads initially.
230 If threads are hidden, you have to run the command
231 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
232 to expose hidden threads."
233   :group 'gnus-thread
234   :type 'boolean)
235
236 (defcustom gnus-thread-hide-killed t
237   "*If non-nil, hide killed threads automatically."
238   :group 'gnus-thread
239   :type 'boolean)
240
241 (defcustom gnus-thread-ignore-subject t
242   "*If non-nil, which is the default, ignore subjects and do all threading based on the Reference header.
243 If nil, articles that have different subjects from their parents will
244 start separate threads."
245   :group 'gnus-thread
246   :type 'boolean)
247
248 (defcustom gnus-thread-operation-ignore-subject t
249   "*If non-nil, subjects will be ignored when doing thread commands.
250 This affects commands like `gnus-summary-kill-thread' and
251 `gnus-summary-lower-thread'.
252
253 If this variable is nil, articles in the same thread with different
254 subjects will not be included in the operation in question.  If this
255 variable is `fuzzy', only articles that have subjects that are fuzzily
256 equal will be included."
257   :group 'gnus-thread
258   :type '(choice (const :tag "off" nil)
259                  (const fuzzy)
260                  (sexp :tag "on" t)))
261
262 (defcustom gnus-thread-indent-level 4
263   "*Number that says how much each sub-thread should be indented."
264   :group 'gnus-thread
265   :type 'integer)
266
267 (defcustom gnus-auto-extend-newsgroup t
268   "*If non-nil, extend newsgroup forward and backward when requested."
269   :group 'gnus-summary-choose
270   :type 'boolean)
271
272 (defcustom gnus-auto-select-first t
273   "*If nil, don't select the first unread article when entering a group.
274 If this variable is `best', select the highest-scored unread article
275 in the group.  If t, select the first unread article.
276
277 This variable can also be a function to place point on a likely
278 subject line.  Useful values include `gnus-summary-first-unread-subject',
279 `gnus-summary-first-unread-article' and
280 `gnus-summary-best-unread-article'.
281
282 If you want to prevent automatic selection of the first unread article
283 in some newsgroups, set the variable to nil in
284 `gnus-select-group-hook'."
285   :group 'gnus-group-select
286   :type '(choice (const :tag "none" nil)
287                  (const best)
288                  (sexp :menu-tag "first" t)
289                  (function-item gnus-summary-first-unread-subject)
290                  (function-item gnus-summary-first-unread-article)
291                  (function-item gnus-summary-best-unread-article)))
292
293 (defcustom gnus-dont-select-after-jump-to-other-group nil
294   "If non-nil, don't select the first unread article after entering the
295 other group by the command `gnus-summary-jump-to-other-group'.  If nil,
296 it is depend on the value of `gnus-auto-select-first' whether to select
297 or not."
298   :group 'gnus-group-select
299   :type 'boolean)
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-show-mime t
368   "*If non-nil, do mime processing of articles.
369 The articles will simply be fed to the function given by
370 `gnus-article-display-method-for-mime'."
371   :group 'gnus-article-mime
372   :type 'boolean)
373
374 (defcustom gnus-move-split-methods nil
375   "*Variable used to suggest where articles are to be moved to.
376 It uses the same syntax as the `gnus-split-methods' variable."
377   :group 'gnus-summary-mail
378   :type '(repeat (choice (list :value (fun) function)
379                          (cons :value ("" "") regexp (repeat string))
380                          (sexp :value nil))))
381
382 (defcustom gnus-unread-mark ?  ;Whitespace
383   "*Mark used for unread articles."
384   :group 'gnus-summary-marks
385   :type 'character)
386
387 (defcustom gnus-ticked-mark ?!
388   "*Mark used for ticked articles."
389   :group 'gnus-summary-marks
390   :type 'character)
391
392 (defcustom gnus-dormant-mark ??
393   "*Mark used for dormant articles."
394   :group 'gnus-summary-marks
395   :type 'character)
396
397 (defcustom gnus-del-mark ?r
398   "*Mark used for del'd articles."
399   :group 'gnus-summary-marks
400   :type 'character)
401
402 (defcustom gnus-read-mark ?R
403   "*Mark used for read articles."
404   :group 'gnus-summary-marks
405   :type 'character)
406
407 (defcustom gnus-expirable-mark ?E
408   "*Mark used for expirable articles."
409   :group 'gnus-summary-marks
410   :type 'character)
411
412 (defcustom gnus-killed-mark ?K
413   "*Mark used for killed articles."
414   :group 'gnus-summary-marks
415   :type 'character)
416
417 (defcustom gnus-souped-mark ?F
418   "*Mark used for killed articles."
419   :group 'gnus-summary-marks
420   :type 'character)
421
422 (defcustom gnus-kill-file-mark ?X
423   "*Mark used for articles killed by kill files."
424   :group 'gnus-summary-marks
425   :type 'character)
426
427 (defcustom gnus-low-score-mark ?Y
428   "*Mark used for articles with a low score."
429   :group 'gnus-summary-marks
430   :type 'character)
431
432 (defcustom gnus-catchup-mark ?C
433   "*Mark used for articles that are caught up."
434   :group 'gnus-summary-marks
435   :type 'character)
436
437 (defcustom gnus-replied-mark ?A
438   "*Mark used for articles that have been replied to."
439   :group 'gnus-summary-marks
440   :type 'character)
441
442 (defcustom gnus-cached-mark ?*
443   "*Mark used for articles that are in the cache."
444   :group 'gnus-summary-marks
445   :type 'character)
446
447 (defcustom gnus-saved-mark ?S
448   "*Mark used for articles that have been saved to."
449   :group 'gnus-summary-marks
450   :type 'character)
451
452 (defcustom gnus-ancient-mark ?O
453   "*Mark used for ancient articles."
454   :group 'gnus-summary-marks
455   :type 'character)
456
457 (defcustom gnus-sparse-mark ?Q
458   "*Mark used for sparsely reffed articles."
459   :group 'gnus-summary-marks
460   :type 'character)
461
462 (defcustom gnus-canceled-mark ?G
463   "*Mark used for canceled articles."
464   :group 'gnus-summary-marks
465   :type 'character)
466
467 (defcustom gnus-duplicate-mark ?M
468   "*Mark used for duplicate articles."
469   :group 'gnus-summary-marks
470   :type 'character)
471
472 (defcustom gnus-undownloaded-mark ?@
473   "*Mark used for articles that weren't downloaded."
474   :group 'gnus-summary-marks
475   :type 'character)
476
477 (defcustom gnus-downloadable-mark ?%
478   "*Mark used for articles that are to be downloaded."
479   :group 'gnus-summary-marks
480   :type 'character)
481
482 (defcustom gnus-unsendable-mark ?=
483   "*Mark used for articles that won't be sent."
484   :group 'gnus-summary-marks
485   :type 'character)
486
487 (defcustom gnus-score-over-mark ?+
488   "*Score mark used for articles with high scores."
489   :group 'gnus-summary-marks
490   :type 'character)
491
492 (defcustom gnus-score-below-mark ?-
493   "*Score mark used for articles with low scores."
494   :group 'gnus-summary-marks
495   :type 'character)
496
497 (defcustom gnus-empty-thread-mark ?  ;Whitespace
498   "*There is no thread under the article."
499   :group 'gnus-summary-marks
500   :type 'character)
501
502 (defcustom gnus-not-empty-thread-mark ?=
503   "*There is a thread under the article."
504   :group 'gnus-summary-marks
505   :type 'character)
506
507 (defcustom gnus-view-pseudo-asynchronously nil
508   "*If non-nil, Gnus will view pseudo-articles asynchronously."
509   :group 'gnus-extract-view
510   :type 'boolean)
511
512 (defcustom gnus-auto-expirable-marks
513   (list gnus-killed-mark gnus-del-mark gnus-catchup-mark
514         gnus-low-score-mark gnus-ancient-mark gnus-read-mark
515         gnus-souped-mark gnus-duplicate-mark)
516   "*The list of marks converted into expiration if a group is auto-expirable."
517   :group 'gnus-summary
518   :type '(repeat character))
519
520 (defcustom gnus-inhibit-user-auto-expire t
521   "*If non-nil, user marking commands will not mark an article as expirable, even if the group has auto-expire turned on."
522   :group 'gnus-summary
523   :type 'boolean)
524
525 (defcustom gnus-view-pseudos nil
526   "*If `automatic', pseudo-articles will be viewed automatically.
527 If `not-confirm', pseudos will be viewed automatically, and the user
528 will not be asked to confirm the command."
529   :group 'gnus-extract-view
530   :type '(choice (const :tag "off" nil)
531                  (const automatic)
532                  (const not-confirm)))
533
534 (defcustom gnus-view-pseudos-separately t
535   "*If non-nil, one pseudo-article will be created for each file to be viewed.
536 If nil, all files that use the same viewing command will be given as a
537 list of parameters to that command."
538   :group 'gnus-extract-view
539   :type 'boolean)
540
541 (defcustom gnus-insert-pseudo-articles t
542   "*If non-nil, insert pseudo-articles when decoding articles."
543   :group 'gnus-extract-view
544   :type 'boolean)
545
546 (defcustom gnus-summary-dummy-line-format
547   "  %(:                          :%) %S\n"
548   "*The format specification for the dummy roots in the summary buffer.
549 It works along the same lines as a normal formatting string,
550 with some simple extensions.
551
552 %S  The subject"
553   :group 'gnus-threading
554   :type 'string)
555
556 (defcustom gnus-summary-mode-line-format "Gnus: %g [%A] %Z"
557   "*The format specification for the summary mode line.
558 It works along the same lines as a normal formatting string,
559 with some simple extensions:
560
561 %G  Group name
562 %p  Unprefixed group name
563 %A  Current article number
564 %z  Current article score
565 %V  Gnus version
566 %U  Number of unread articles in the group
567 %e  Number of unselected articles in the group
568 %Z  A string with unread/unselected article counts
569 %g  Shortish group name
570 %S  Subject of the current article
571 %u  User-defined spec
572 %s  Current score file name
573 %d  Number of dormant articles
574 %r  Number of articles that have been marked as read in this session
575 %E  Number of articles expunged by the score files"
576   :group 'gnus-summary-format
577   :type 'string)
578
579 (defcustom gnus-list-identifiers nil
580   "Regexp that matches list identifiers to be removed from subject.
581 This can also be a list of regexps."
582   :group 'gnus-summary-format
583   :group 'gnus-article-hiding
584   :type '(choice (const :tag "none" nil)
585                  (regexp :value ".*")
586                  (repeat :value (".*") regexp)))
587
588 (defcustom gnus-summary-mark-below 0
589   "*Mark all articles with a score below this variable as read.
590 This variable is local to each summary buffer and usually set by the
591 score file."
592   :group 'gnus-score-default
593   :type 'integer)
594
595 (defcustom gnus-article-sort-functions '(gnus-article-sort-by-number)
596   "*List of functions used for sorting articles in the summary buffer.
597 This variable is only used when not using a threaded display."
598   :group 'gnus-summary-sort
599   :type '(repeat (choice (function-item gnus-article-sort-by-number)
600                          (function-item gnus-article-sort-by-author)
601                          (function-item gnus-article-sort-by-subject)
602                          (function-item gnus-article-sort-by-date)
603                          (function-item gnus-article-sort-by-score)
604                          (function :tag "other"))))
605
606 (defcustom gnus-thread-sort-functions '(gnus-thread-sort-by-number)
607   "*List of functions used for sorting threads in the summary buffer.
608 By default, threads are sorted by article number.
609
610 Each function takes two threads and return non-nil if the first thread
611 should be sorted before the other.  If you use more than one function,
612 the primary sort function should be the last.  You should probably
613 always include `gnus-thread-sort-by-number' in the list of sorting
614 functions -- preferably first.
615
616 Ready-made functions include `gnus-thread-sort-by-number',
617 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
618 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
619 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function')."
620   :group 'gnus-summary-sort
621   :type '(repeat (choice (function-item gnus-thread-sort-by-number)
622                          (function-item gnus-thread-sort-by-author)
623                          (function-item gnus-thread-sort-by-subject)
624                          (function-item gnus-thread-sort-by-date)
625                          (function-item gnus-thread-sort-by-score)
626                          (function-item gnus-thread-sort-by-total-score)
627                          (function :tag "other"))))
628
629 (defcustom gnus-thread-score-function '+
630   "*Function used for calculating the total score of a thread.
631
632 The function is called with the scores of the article and each
633 subthread and should then return the score of the thread.
634
635 Some functions you can use are `+', `max', or `min'."
636   :group 'gnus-summary-sort
637   :type 'function)
638
639 (defcustom gnus-summary-expunge-below nil
640   "All articles that have a score less than this variable will be expunged.
641 This variable is local to the summary buffers."
642   :group 'gnus-score-default
643   :type '(choice (const :tag "off" nil)
644                  integer))
645
646 (defcustom gnus-thread-expunge-below nil
647   "All threads that have a total score less than this variable will be expunged.
648 See `gnus-thread-score-function' for en explanation of what a
649 \"thread score\" is.
650
651 This variable is local to the summary buffers."
652   :group 'gnus-threading
653   :group 'gnus-score-default
654   :type '(choice (const :tag "off" nil)
655                  integer))
656
657 (defcustom gnus-summary-mode-hook nil
658   "*A hook for Gnus summary mode.
659 This hook is run before any variables are set in the summary buffer."
660   :group 'gnus-summary-various
661   :type 'hook)
662
663 (defcustom gnus-summary-menu-hook nil
664   "*Hook run after the creation of the summary mode menu."
665   :group 'gnus-summary-visual
666   :type 'hook)
667
668 (defcustom gnus-summary-exit-hook nil
669   "*A hook called on exit from the summary buffer.
670 It will be called with point in the group buffer."
671   :group 'gnus-summary-exit
672   :type 'hook)
673
674 (defcustom gnus-summary-prepare-hook nil
675   "*A hook called after the summary buffer has been generated.
676 If you want to modify the summary buffer, you can use this hook."
677   :group 'gnus-summary-various
678   :type 'hook)
679
680 (defcustom gnus-summary-prepared-hook nil
681   "*A hook called as the last thing after the summary buffer has been generated."
682   :group 'gnus-summary-various
683   :type 'hook)
684
685 (defcustom gnus-summary-generate-hook nil
686   "*A hook run just before generating the summary buffer.
687 This hook is commonly used to customize threading variables and the
688 like."
689   :group 'gnus-summary-various
690   :type 'hook)
691
692 (defcustom gnus-select-group-hook nil
693   "*A hook called when a newsgroup is selected.
694
695 If you'd like to simplify subjects like the
696 `gnus-summary-next-same-subject' command does, you can use the
697 following hook:
698
699  (setq gnus-select-group-hook
700       (list
701         (lambda ()
702           (mapcar (lambda (header)
703                      (mail-header-set-subject
704                       header
705                       (gnus-simplify-subject
706                        (mail-header-subject header) 're-only)))
707                   gnus-newsgroup-headers))))"
708   :group 'gnus-group-select
709   :type 'hook)
710
711 (defcustom gnus-select-article-hook nil
712   "*A hook called when an article is selected."
713   :group 'gnus-summary-choose
714   :type 'hook)
715
716 (defcustom gnus-visual-mark-article-hook
717   (list 'gnus-highlight-selected-summary)
718   "*Hook run after selecting an article in the summary buffer.
719 It is meant to be used for highlighting the article in some way.  It
720 is not run if `gnus-visual' is nil."
721   :group 'gnus-summary-visual
722   :type 'hook)
723
724 (defcustom gnus-parse-headers-hook '(gnus-set-summary-default-charset)
725   "*A hook called before parsing the headers."
726   :group 'gnus-various
727   :type 'hook)
728
729 (defcustom gnus-exit-group-hook nil
730   "*A hook called when exiting summary mode.
731 This hook is not called from the non-updating exit commands like `Q'."
732   :group 'gnus-various
733   :type 'hook)
734
735 (defcustom gnus-summary-update-hook
736   (list 'gnus-summary-highlight-line)
737   "*A hook called when a summary line is changed.
738 The hook will not be called if `gnus-visual' is nil.
739
740 The default function `gnus-summary-highlight-line' will
741 highlight the line according to the `gnus-summary-highlight'
742 variable."
743   :group 'gnus-summary-visual
744   :type 'hook)
745
746 (defcustom gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
747   "*A hook called when an article is selected for the first time.
748 The hook is intended to mark an article as read (or unread)
749 automatically when it is selected."
750   :group 'gnus-summary-choose
751   :type 'hook)
752
753 (defcustom gnus-group-no-more-groups-hook nil
754   "*A hook run when returning to group mode having no more (unread) groups."
755   :group 'gnus-group-select
756   :type 'hook)
757
758 (defcustom gnus-ps-print-hook nil
759   "*A hook run before ps-printing something from Gnus."
760   :group 'gnus-summary
761   :type 'hook)
762
763 (defcustom gnus-summary-selected-face 'gnus-summary-selected-face
764   "Face used for highlighting the current article in the summary buffer."
765   :group 'gnus-summary-visual
766   :type 'face)
767
768 (defcustom gnus-summary-highlight
769   '(((= mark gnus-canceled-mark)
770      . gnus-summary-cancelled-face)
771     ((and (> score default)
772           (or (= mark gnus-dormant-mark)
773               (= mark gnus-ticked-mark)))
774      . gnus-summary-high-ticked-face)
775     ((and (< score default)
776           (or (= mark gnus-dormant-mark)
777               (= mark gnus-ticked-mark)))
778      . gnus-summary-low-ticked-face)
779     ((or (= mark gnus-dormant-mark)
780          (= mark gnus-ticked-mark))
781      . gnus-summary-normal-ticked-face)
782     ((and (> score default) (= mark gnus-ancient-mark))
783      . gnus-summary-high-ancient-face)
784     ((and (< score default) (= mark gnus-ancient-mark))
785      . gnus-summary-low-ancient-face)
786     ((= mark gnus-ancient-mark)
787      . gnus-summary-normal-ancient-face)
788     ((and (> score default) (= mark gnus-unread-mark))
789      . gnus-summary-high-unread-face)
790     ((and (< score default) (= mark gnus-unread-mark))
791      . gnus-summary-low-unread-face)
792     ((and (memq article gnus-newsgroup-incorporated)
793           (= mark gnus-unread-mark))
794      . gnus-summary-incorporated-face)
795     ((= mark gnus-unread-mark)
796      . gnus-summary-normal-unread-face)
797     ((and (> score default) (memq mark (list gnus-downloadable-mark
798                                              gnus-undownloaded-mark)))
799      . gnus-summary-high-unread-face)
800     ((and (< score default) (memq mark (list gnus-downloadable-mark
801                                              gnus-undownloaded-mark)))
802      . gnus-summary-low-unread-face)
803     ((memq mark (list gnus-downloadable-mark gnus-undownloaded-mark))
804      . gnus-summary-normal-unread-face)
805     ((> score default)
806      . gnus-summary-high-read-face)
807     ((< score default)
808      . gnus-summary-low-read-face)
809     (t
810      . gnus-summary-normal-read-face))
811   "*Controls the highlighting of summary buffer lines.
812
813 A list of (FORM . FACE) pairs.  When deciding how a a particular
814 summary line should be displayed, each form is evaluated.  The content
815 of the face field after the first true form is used.  You can change
816 how those summary lines are displayed, by editing the face field.
817
818 You can use the following variables in the FORM field.
819
820 score:   The articles score
821 default: The default article score.
822 below:   The score below which articles are automatically marked as read.
823 mark:    The articles mark."
824   :group 'gnus-summary-visual
825   :type '(repeat (cons (sexp :tag "Form" nil)
826                        face)))
827
828 (defcustom gnus-alter-header-function nil
829   "Function called to allow alteration of article header structures.
830 The function is called with one parameter, the article header vector,
831 which it may alter in any way.")
832
833 (defvar gnus-decode-encoded-word-function
834   (mime-find-field-decoder 'From 'nov)
835   "Variable that says which function should be used to decode a string with encoded words.")
836
837 (defcustom gnus-extra-headers nil
838   "*Extra headers to parse."
839   :group 'gnus-summary
840   :type '(repeat symbol))
841
842 (defcustom gnus-ignored-from-addresses
843   (and user-mail-address (regexp-quote user-mail-address))
844   "*Regexp of From headers that may be suppressed in favor of To headers."
845   :group 'gnus-summary
846   :type 'regexp)
847
848 (defcustom gnus-group-charset-alist
849   '(("^hk\\>\\|^tw\\>\\|\\<big5\\>" cn-big5)
850     ("^cn\\>\\|\\<chinese\\>" cn-gb-2312)
851     ("^fj\\>\\|^japan\\>" iso-2022-jp-2)
852     ("^tnn\\>\\|^pin\\>\\|^sci.lang.japan" iso-2022-7bit)
853     ("^relcom\\>" koi8-r)
854     ("^fido7\\>" koi8-r)
855     ("^\\(cz\\|hun\\|pl\\|sk\\|hr\\)\\>" iso-8859-2)
856     ("^israel\\>" iso-8859-1)
857     ("^han\\>" euc-kr)
858     ("^alt.chinese.text.big5\\>" chinese-big5)
859     ("^soc.culture.vietnamese\\>" vietnamese-viqr)
860     ("^\\(comp\\|rec\\|alt\\|sci\\|soc\\|news\\|gnu\\|bofh\\)\\>" iso-8859-1)
861     (".*" iso-8859-1))
862   "Alist of regexps (to match group names) and default charsets to be used when reading."
863   :type '(repeat (list (regexp :tag "Group")
864                        (symbol :tag "Charset")))
865   :group 'gnus-charset)
866
867 (defcustom gnus-newsgroup-ignored-charsets '(unknown-8bit x-unknown)
868   "List of charsets that should be ignored.
869 When these charsets are used in the \"charset\" parameter, the
870 default charset will be used instead."
871   :type '(repeat symbol)
872   :group 'gnus-charset)
873
874 (defcustom gnus-group-ignored-charsets-alist
875   '(("alt\\.chinese\\.text" iso-8859-1))
876   "Alist of regexps (to match group names) and charsets that should be ignored.
877 When these charsets are used in the \"charset\" parameter, the
878 default charset will be used instead."
879   :type '(repeat (cons (regexp :tag "Group")
880                        (repeat symbol)))
881   :group 'gnus-charset)
882
883 (defcustom gnus-group-highlight-words-alist nil
884   "Alist of group regexps and highlight regexps.
885 This variable uses the same syntax as `gnus-emphasis-alist'."
886   :type '(repeat (cons (regexp :tag "Group")
887                        (repeat (list (regexp :tag "Highlight regexp")
888                                      (number :tag "Group for entire word" 0)
889                                      (number :tag "Group for displayed part" 0)
890                                      (symbol :tag "Face"
891                                              gnus-emphasis-highlight-words)))))
892   :group 'gnus-summary-visual)
893
894 (defcustom gnus-use-wheel nil
895   "Use Intelli-mouse on summary movement"
896   :type 'boolean
897   :group 'gnus-summary-maneuvering)
898
899 (defcustom gnus-wheel-scroll-amount '(5 . 1)
900   "Amount to scroll messages by spinning the mouse wheel.
901 This is actually a cons cell, where the first item is the amount to scroll
902 on a normal wheel event, and the second is the amount to scroll when the
903 wheel is moved with the shift key depressed."
904   :type '(cons (integer :tag "Shift") integer)
905   :group 'gnus-summary-maneuvering)
906
907 (defcustom gnus-wheel-edge-resistance 2
908   "How hard it should be to change the current article
909 by moving the mouse over the edge of the article window."
910   :type 'integer
911   :group 'gnus-summary-maneuvering)
912
913 (defcustom gnus-summary-show-article-charset-alist
914   nil
915   "Alist of number and charset.
916 The article will be shown with the charset corresponding to the
917 numbered argument.
918 For example: ((1 . cn-gb-2312) (2 . big5))."
919   :type '(repeat (cons (number :tag "Argument" 1)
920                        (symbol :tag "Charset")))
921   :group 'gnus-charset)
922
923 (defcustom gnus-preserve-marks t
924   "Whether marks are preserved when moving, copying and respooling messages."
925   :type 'boolean
926   :group 'gnus-summary-marks)
927
928 (defcustom gnus-alter-articles-to-read-function nil
929   "Function to be called to alter the list of articles to be selected."
930   :type 'function
931   :group 'gnus-summary)
932
933 (defcustom gnus-orphan-score nil
934   "*All orphans get this score added.  Set in the score file."
935   :group 'gnus-score-default
936   :type '(choice (const nil)
937                  integer))
938
939 ;;; Internal variables
940
941 (defvar gnus-article-mime-handles nil)
942 (defvar gnus-article-decoded-p nil)
943 (defvar gnus-scores-exclude-files nil)
944 (defvar gnus-page-broken nil)
945 (defvar gnus-inhibit-mime-unbuttonizing nil)
946
947 (defvar gnus-original-article nil)
948 (defvar gnus-article-internal-prepare-hook nil)
949 (defvar gnus-newsgroup-process-stack nil)
950
951 (defvar gnus-thread-indent-array nil)
952 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
953 (defvar gnus-sort-gathered-threads-function 'gnus-thread-sort-by-number
954   "Function called to sort the articles within a thread after it has been gathered together.")
955
956 ;; Avoid highlighting in kill files.
957 (defvar gnus-summary-inhibit-highlight nil)
958 (defvar gnus-newsgroup-selected-overlay nil)
959 (defvar gnus-inhibit-limiting nil)
960 (defvar gnus-newsgroup-adaptive-score-file nil)
961 (defvar gnus-current-score-file nil)
962 (defvar gnus-current-move-group nil)
963 (defvar gnus-current-copy-group nil)
964 (defvar gnus-current-crosspost-group nil)
965
966 (defvar gnus-newsgroup-dependencies nil)
967 (defvar gnus-newsgroup-adaptive nil)
968 (defvar gnus-summary-display-article-function nil)
969 (defvar gnus-summary-highlight-line-function nil
970   "Function called after highlighting a summary line.")
971
972 (defvar gnus-summary-line-format-alist
973   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
974     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
975     (?s gnus-tmp-subject-or-nil ?s)
976     (?n gnus-tmp-name ?s)
977     (?A (std11-address-string
978          (car (mime-entity-read-field gnus-tmp-header 'From))) ?s)
979     (?a (or (std11-full-name-string
980              (car (mime-entity-read-field gnus-tmp-header 'From)))
981             gnus-tmp-from) ?s)
982     (?F gnus-tmp-from ?s)
983     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
984     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
985     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
986     (?o (gnus-date-iso8601 (mail-header-date gnus-tmp-header)) ?s)
987     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
988     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
989     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
990     (?L gnus-tmp-lines ?d)
991     (?I gnus-tmp-indentation ?s)
992     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
993     (?R gnus-tmp-replied ?c)
994     (?\[ gnus-tmp-opening-bracket ?c)
995     (?\] gnus-tmp-closing-bracket ?c)
996     (?\> (make-string gnus-tmp-level ? ) ?s)
997     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
998     (?i gnus-tmp-score ?d)
999     (?z gnus-tmp-score-char ?c)
1000     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1001     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1002     (?U gnus-tmp-unread ?c)
1003     (?f (gnus-summary-from-or-to-or-newsgroups gnus-tmp-header) ?s)
1004     (?t (gnus-summary-number-of-articles-in-thread
1005          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1006         ?d)
1007     (?e (gnus-summary-number-of-articles-in-thread
1008          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1009         ?c)
1010     (?u gnus-tmp-user-defined ?s)
1011     (?P (gnus-pick-line-number) ?d))
1012   "An alist of format specifications that can appear in summary lines.
1013 These are paired with what variables they correspond with, along with
1014 the type of the variable (string, integer, character, etc).")
1015
1016 (defvar gnus-summary-dummy-line-format-alist
1017   `((?S gnus-tmp-subject ?s)
1018     (?N gnus-tmp-number ?d)
1019     (?u gnus-tmp-user-defined ?s)))
1020
1021 (defvar gnus-summary-mode-line-format-alist
1022   `((?G gnus-tmp-group-name ?s)
1023     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1024     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1025     (?A gnus-tmp-article-number ?d)
1026     (?Z gnus-tmp-unread-and-unselected ?s)
1027     (?V gnus-version ?s)
1028     (?U gnus-tmp-unread-and-unticked ?d)
1029     (?S gnus-tmp-subject ?s)
1030     (?e gnus-tmp-unselected ?d)
1031     (?u gnus-tmp-user-defined ?s)
1032     (?d (length gnus-newsgroup-dormant) ?d)
1033     (?t (length gnus-newsgroup-marked) ?d)
1034     (?r (length gnus-newsgroup-reads) ?d)
1035     (?z (gnus-summary-article-score gnus-tmp-article-number) ?d)
1036     (?E gnus-newsgroup-expunged-tally ?d)
1037     (?s (gnus-current-score-file-nondirectory) ?s)))
1038
1039 (defvar gnus-last-search-regexp nil
1040   "Default regexp for article search command.")
1041
1042 (defvar gnus-summary-search-article-matched-data nil
1043   "Last matched data of article search command.  It is the local variable
1044 in `gnus-article-buffer' which consists of the list of start position,
1045 end position and text.")
1046
1047 (defvar gnus-last-shell-command nil
1048   "Default shell command on article.")
1049
1050 (defvar gnus-newsgroup-begin nil)
1051 (defvar gnus-newsgroup-end nil)
1052 (defvar gnus-newsgroup-last-rmail nil)
1053 (defvar gnus-newsgroup-last-mail nil)
1054 (defvar gnus-newsgroup-last-folder nil)
1055 (defvar gnus-newsgroup-last-file nil)
1056 (defvar gnus-newsgroup-auto-expire nil)
1057 (defvar gnus-newsgroup-active nil)
1058
1059 (defvar gnus-newsgroup-data nil)
1060 (defvar gnus-newsgroup-data-reverse nil)
1061 (defvar gnus-newsgroup-limit nil)
1062 (defvar gnus-newsgroup-limits nil)
1063
1064 (defvar gnus-newsgroup-unreads nil
1065   "List of unread articles in the current newsgroup.")
1066
1067 (defvar gnus-newsgroup-unselected nil
1068   "List of unselected unread articles in the current newsgroup.")
1069
1070 (defvar gnus-newsgroup-reads nil
1071   "Alist of read articles and article marks in the current newsgroup.")
1072
1073 (defvar gnus-newsgroup-expunged-tally nil)
1074
1075 (defvar gnus-newsgroup-marked nil
1076   "List of ticked articles in the current newsgroup (a subset of unread art).")
1077
1078 (defvar gnus-newsgroup-killed nil
1079   "List of ranges of articles that have been through the scoring process.")
1080
1081 (defvar gnus-newsgroup-cached nil
1082   "List of articles that come from the article cache.")
1083
1084 (defvar gnus-newsgroup-saved nil
1085   "List of articles that have been saved.")
1086
1087 (defvar gnus-newsgroup-kill-headers nil)
1088
1089 (defvar gnus-newsgroup-replied nil
1090   "List of articles that have been replied to in the current newsgroup.")
1091
1092 (defvar gnus-newsgroup-expirable nil
1093   "List of articles in the current newsgroup that can be expired.")
1094
1095 (defvar gnus-newsgroup-processable nil
1096   "List of articles in the current newsgroup that can be processed.")
1097
1098 (defvar gnus-newsgroup-downloadable nil
1099   "List of articles in the current newsgroup that can be processed.")
1100
1101 (defvar gnus-newsgroup-undownloaded nil
1102   "List of articles in the current newsgroup that haven't been downloaded..")
1103
1104 (defvar gnus-newsgroup-unsendable nil
1105   "List of articles in the current newsgroup that won't be sent.")
1106
1107 (defvar gnus-newsgroup-bookmarks nil
1108   "List of articles in the current newsgroup that have bookmarks.")
1109
1110 (defvar gnus-newsgroup-dormant nil
1111   "List of dormant articles in the current newsgroup.")
1112
1113 (defvar gnus-newsgroup-scored nil
1114   "List of scored articles in the current newsgroup.")
1115
1116 (defvar gnus-newsgroup-incorporated nil
1117   "List of incorporated articles in the current newsgroup.")
1118
1119 (defvar gnus-newsgroup-headers nil
1120   "List of article headers in the current newsgroup.")
1121
1122 (defvar gnus-newsgroup-threads nil)
1123
1124 (defvar gnus-newsgroup-prepared nil
1125   "Whether the current group has been prepared properly.")
1126
1127 (defvar gnus-newsgroup-ancient nil
1128   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1129
1130 (defvar gnus-newsgroup-sparse nil)
1131
1132 (defvar gnus-current-article nil)
1133 (defvar gnus-article-current nil)
1134 (defvar gnus-current-headers nil)
1135 (defvar gnus-have-all-headers nil)
1136 (defvar gnus-last-article nil)
1137 (defvar gnus-newsgroup-history nil)
1138 (defvar gnus-newsgroup-charset nil)
1139 (defvar gnus-newsgroup-ephemeral-charset nil)
1140 (defvar gnus-newsgroup-ephemeral-ignored-charsets nil)
1141
1142 (defconst gnus-summary-local-variables
1143   '(gnus-newsgroup-name
1144     gnus-newsgroup-begin gnus-newsgroup-end
1145     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1146     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1147     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1148     gnus-newsgroup-unselected gnus-newsgroup-marked
1149     gnus-newsgroup-reads gnus-newsgroup-saved
1150     gnus-newsgroup-replied gnus-newsgroup-expirable
1151     gnus-newsgroup-processable gnus-newsgroup-killed
1152     gnus-newsgroup-downloadable gnus-newsgroup-undownloaded
1153     gnus-newsgroup-unsendable
1154     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1155     gnus-newsgroup-headers gnus-newsgroup-threads
1156     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1157     gnus-current-article gnus-current-headers gnus-have-all-headers
1158     gnus-last-article gnus-article-internal-prepare-hook
1159     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1160     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1161     gnus-thread-expunge-below
1162     gnus-score-alist gnus-current-score-file
1163     (gnus-summary-expunge-below . global)
1164     (gnus-summary-mark-below . global)
1165     (gnus-orphan-score . global)
1166     gnus-newsgroup-active gnus-scores-exclude-files
1167     gnus-newsgroup-history gnus-newsgroup-ancient
1168     gnus-newsgroup-sparse gnus-newsgroup-process-stack
1169     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1170     gnus-newsgroup-adaptive-score-file (gnus-reffed-article-number . -1)
1171     (gnus-newsgroup-expunged-tally . 0)
1172     gnus-cache-removable-articles gnus-newsgroup-cached
1173     gnus-newsgroup-data gnus-newsgroup-data-reverse
1174     gnus-newsgroup-limit gnus-newsgroup-limits
1175     gnus-newsgroup-charset
1176     gnus-newsgroup-incorporated)
1177   "Variables that are buffer-local to the summary buffers.")
1178
1179 ;; Byte-compiler warning.
1180 (defvar gnus-article-mode-map)
1181
1182 ;; Subject simplification.
1183
1184 (defun gnus-simplify-whitespace (str)
1185   "Remove excessive whitespace from STR."
1186   (let ((mystr str))
1187     ;; Multiple spaces.
1188     (while (string-match "[ \t][ \t]+" mystr)
1189       (setq mystr (concat (substring mystr 0 (match-beginning 0))
1190                           " "
1191                           (substring mystr (match-end 0)))))
1192     ;; Leading spaces.
1193     (when (string-match "^[ \t]+" mystr)
1194       (setq mystr (substring mystr (match-end 0))))
1195     ;; Trailing spaces.
1196     (when (string-match "[ \t]+$" mystr)
1197       (setq mystr (substring mystr 0 (match-beginning 0))))
1198     mystr))
1199
1200 (defsubst gnus-simplify-subject-re (subject)
1201   "Remove \"Re:\" from subject lines."
1202   (if (string-match "^[Rr][Ee]: *" subject)
1203       (substring subject (match-end 0))
1204     subject))
1205
1206 (defun gnus-simplify-subject (subject &optional re-only)
1207   "Remove `Re:' and words in parentheses.
1208 If RE-ONLY is non-nil, strip leading `Re:'s only."
1209   (let ((case-fold-search t))           ;Ignore case.
1210     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
1211     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
1212       (setq subject (substring subject (match-end 0))))
1213     ;; Remove uninteresting prefixes.
1214     (when (and (not re-only)
1215                gnus-simplify-ignored-prefixes
1216                (string-match gnus-simplify-ignored-prefixes subject))
1217       (setq subject (substring subject (match-end 0))))
1218     ;; Remove words in parentheses from end.
1219     (unless re-only
1220       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
1221         (setq subject (substring subject 0 (match-beginning 0)))))
1222     ;; Return subject string.
1223     subject))
1224
1225 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
1226 ;; all whitespace.
1227 (defsubst gnus-simplify-buffer-fuzzy-step (regexp &optional newtext)
1228   (goto-char (point-min))
1229   (while (re-search-forward regexp nil t)
1230     (replace-match (or newtext ""))))
1231
1232 (defun gnus-simplify-buffer-fuzzy ()
1233   "Simplify string in the buffer fuzzily.
1234 The string in the accessible portion of the current buffer is simplified.
1235 It is assumed to be a single-line subject.
1236 Whitespace is generally cleaned up, and miscellaneous leading/trailing
1237 matter is removed.  Additional things can be deleted by setting
1238 `gnus-simplify-subject-fuzzy-regexp'."
1239   (let ((case-fold-search t)
1240         (modified-tick))
1241     (gnus-simplify-buffer-fuzzy-step "\t" " ")
1242
1243     (while (not (eq modified-tick (buffer-modified-tick)))
1244       (setq modified-tick (buffer-modified-tick))
1245       (cond
1246        ((listp gnus-simplify-subject-fuzzy-regexp)
1247         (mapcar 'gnus-simplify-buffer-fuzzy-step
1248                 gnus-simplify-subject-fuzzy-regexp))
1249        (gnus-simplify-subject-fuzzy-regexp
1250         (gnus-simplify-buffer-fuzzy-step gnus-simplify-subject-fuzzy-regexp)))
1251       (gnus-simplify-buffer-fuzzy-step "^ *\\[[-+?*!][-+?*!]\\] *")
1252       (gnus-simplify-buffer-fuzzy-step
1253        "^ *\\(re\\|fw\\|fwd\\)[[{(^0-9]*[])}]?[:;] *")
1254       (gnus-simplify-buffer-fuzzy-step "^[[].*:\\( .*\\)[]]$" "\\1"))
1255
1256     (gnus-simplify-buffer-fuzzy-step " *[[{(][^()\n]*[]})] *$")
1257     (gnus-simplify-buffer-fuzzy-step "  +" " ")
1258     (gnus-simplify-buffer-fuzzy-step " $")
1259     (gnus-simplify-buffer-fuzzy-step "^ +")))
1260
1261 (defun gnus-simplify-subject-fuzzy (subject)
1262   "Simplify a subject string fuzzily.
1263 See `gnus-simplify-buffer-fuzzy' for details."
1264   (save-excursion
1265     (gnus-set-work-buffer)
1266     (let ((case-fold-search t))
1267       ;; Remove uninteresting prefixes.
1268       (when (and gnus-simplify-ignored-prefixes
1269                  (string-match gnus-simplify-ignored-prefixes subject))
1270         (setq subject (substring subject (match-end 0))))
1271       (insert subject)
1272       (inline (gnus-simplify-buffer-fuzzy))
1273       (buffer-string))))
1274
1275 (defsubst gnus-simplify-subject-fully (subject)
1276   "Simplify a subject string according to gnus-summary-gather-subject-limit."
1277   (cond
1278    (gnus-simplify-subject-functions
1279     (gnus-map-function gnus-simplify-subject-functions subject))
1280    ((null gnus-summary-gather-subject-limit)
1281     (gnus-simplify-subject-re subject))
1282    ((eq gnus-summary-gather-subject-limit 'fuzzy)
1283     (gnus-simplify-subject-fuzzy subject))
1284    ((numberp gnus-summary-gather-subject-limit)
1285     (gnus-limit-string (gnus-simplify-subject-re subject)
1286                        gnus-summary-gather-subject-limit))
1287    (t
1288     subject)))
1289
1290 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
1291   "Check whether two subjects are equal.
1292 If optional argument simple-first is t, first argument is already
1293 simplified."
1294   (cond
1295    ((null simple-first)
1296     (equal (gnus-simplify-subject-fully s1)
1297            (gnus-simplify-subject-fully s2)))
1298    (t
1299     (equal s1
1300            (gnus-simplify-subject-fully s2)))))
1301
1302 (defun gnus-summary-bubble-group ()
1303   "Increase the score of the current group.
1304 This is a handy function to add to `gnus-summary-exit-hook' to
1305 increase the score of each group you read."
1306   (gnus-group-add-score gnus-newsgroup-name))
1307
1308 \f
1309 ;;;
1310 ;;; Gnus summary mode
1311 ;;;
1312
1313 (put 'gnus-summary-mode 'mode-class 'special)
1314
1315 (when t
1316   ;; Non-orthogonal keys
1317
1318   (gnus-define-keys gnus-summary-mode-map
1319     " " gnus-summary-next-page
1320     "\177" gnus-summary-prev-page
1321     [delete] gnus-summary-prev-page
1322     [backspace] gnus-summary-prev-page
1323     "\r" gnus-summary-scroll-up
1324     "\M-\r" gnus-summary-scroll-down
1325     "n" gnus-summary-next-unread-article
1326     "p" gnus-summary-prev-unread-article
1327     "N" gnus-summary-next-article
1328     "P" gnus-summary-prev-article
1329     "\M-\C-n" gnus-summary-next-same-subject
1330     "\M-\C-p" gnus-summary-prev-same-subject
1331     "\M-n" gnus-summary-next-unread-subject
1332     "\M-p" gnus-summary-prev-unread-subject
1333     "." gnus-summary-first-unread-article
1334     "," gnus-summary-best-unread-article
1335     "\M-s" gnus-summary-search-article-forward
1336     "\M-r" gnus-summary-search-article-backward
1337     "<" gnus-summary-beginning-of-article
1338     ">" gnus-summary-end-of-article
1339     "j" gnus-summary-goto-article
1340     "^" gnus-summary-refer-parent-article
1341     "\M-^" gnus-summary-refer-article
1342     "u" gnus-summary-tick-article-forward
1343     "!" gnus-summary-tick-article-forward
1344     "U" gnus-summary-tick-article-backward
1345     "d" gnus-summary-mark-as-read-forward
1346     "D" gnus-summary-mark-as-read-backward
1347     "E" gnus-summary-mark-as-expirable
1348     "\M-u" gnus-summary-clear-mark-forward
1349     "\M-U" gnus-summary-clear-mark-backward
1350     "k" gnus-summary-kill-same-subject-and-select
1351     "\C-k" gnus-summary-kill-same-subject
1352     "\M-\C-k" gnus-summary-kill-thread
1353     "\M-\C-l" gnus-summary-lower-thread
1354     "e" gnus-summary-edit-article
1355     "#" gnus-summary-mark-as-processable
1356     "\M-#" gnus-summary-unmark-as-processable
1357     "\M-\C-t" gnus-summary-toggle-threads
1358     "\M-\C-s" gnus-summary-show-thread
1359     "\M-\C-h" gnus-summary-hide-thread
1360     "\M-\C-f" gnus-summary-next-thread
1361     "\M-\C-b" gnus-summary-prev-thread
1362     [(meta down)] gnus-summary-next-thread
1363     [(meta up)] gnus-summary-prev-thread
1364     "\M-\C-u" gnus-summary-up-thread
1365     "\M-\C-d" gnus-summary-down-thread
1366     "&" gnus-summary-execute-command
1367     "c" gnus-summary-catchup-and-exit
1368     "\C-w" gnus-summary-mark-region-as-read
1369     "\C-t" gnus-summary-toggle-truncation
1370     "?" gnus-summary-mark-as-dormant
1371     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
1372     "\C-c\C-s\C-n" gnus-summary-sort-by-number
1373     "\C-c\C-s\C-l" gnus-summary-sort-by-lines
1374     "\C-c\C-s\C-c" gnus-summary-sort-by-chars
1375     "\C-c\C-s\C-a" gnus-summary-sort-by-author
1376     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
1377     "\C-c\C-s\C-d" gnus-summary-sort-by-date
1378     "\C-c\C-s\C-i" gnus-summary-sort-by-score
1379     "=" gnus-summary-expand-window
1380     "\C-x\C-s" gnus-summary-reselect-current-group
1381     "\M-g" gnus-summary-rescan-group
1382     "w" gnus-summary-stop-page-breaking
1383     "\C-c\C-r" gnus-summary-caesar-message
1384     "\M-t" gnus-summary-toggle-mime
1385     "f" gnus-summary-followup
1386     "F" gnus-summary-followup-with-original
1387     "C" gnus-summary-cancel-article
1388     "r" gnus-summary-reply
1389     "R" gnus-summary-reply-with-original
1390     "\C-c\C-f" gnus-summary-mail-forward
1391     "o" gnus-summary-save-article
1392     "\C-o" gnus-summary-save-article-mail
1393     "|" gnus-summary-pipe-output
1394     "\M-k" gnus-summary-edit-local-kill
1395     "\M-K" gnus-summary-edit-global-kill
1396     ;; "V" gnus-version
1397     "\C-c\C-d" gnus-summary-describe-group
1398     "q" gnus-summary-exit
1399     "Q" gnus-summary-exit-no-update
1400     "\C-c\C-i" gnus-info-find-node
1401     gnus-mouse-2 gnus-mouse-pick-article
1402     "m" gnus-summary-mail-other-window
1403     "a" gnus-summary-post-news
1404     "x" gnus-summary-limit-to-unread
1405     "s" gnus-summary-isearch-article
1406     "t" gnus-article-toggle-headers
1407     "g" gnus-summary-show-article
1408     "l" gnus-summary-goto-last-article
1409     "v" gnus-summary-preview-mime-message
1410     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
1411     "\C-d" gnus-summary-enter-digest-group
1412     "\M-\C-d" gnus-summary-read-document
1413     "\M-\C-e" gnus-summary-edit-parameters
1414     "\M-\C-a" gnus-summary-customize-parameters
1415     "\C-c\C-b" gnus-bug
1416     "*" gnus-cache-enter-article
1417     "\M-*" gnus-cache-remove-article
1418     "\M-&" gnus-summary-universal-argument
1419     "\C-l" gnus-recenter
1420     "I" gnus-summary-increase-score
1421     "L" gnus-summary-lower-score
1422     "\M-i" gnus-symbolic-argument
1423     "h" gnus-summary-select-article-buffer
1424
1425     "V" gnus-summary-score-map
1426     "X" gnus-uu-extract-map
1427     "S" gnus-summary-send-map)
1428
1429   ;; Sort of orthogonal keymap
1430   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
1431     "t" gnus-summary-tick-article-forward
1432     "!" gnus-summary-tick-article-forward
1433     "d" gnus-summary-mark-as-read-forward
1434     "r" gnus-summary-mark-as-read-forward
1435     "c" gnus-summary-clear-mark-forward
1436     " " gnus-summary-clear-mark-forward
1437     "e" gnus-summary-mark-as-expirable
1438     "x" gnus-summary-mark-as-expirable
1439     "?" gnus-summary-mark-as-dormant
1440     "b" gnus-summary-set-bookmark
1441     "B" gnus-summary-remove-bookmark
1442     "#" gnus-summary-mark-as-processable
1443     "\M-#" gnus-summary-unmark-as-processable
1444     "S" gnus-summary-limit-include-expunged
1445     "C" gnus-summary-catchup
1446     "H" gnus-summary-catchup-to-here
1447     "\C-c" gnus-summary-catchup-all
1448     "k" gnus-summary-kill-same-subject-and-select
1449     "K" gnus-summary-kill-same-subject
1450     "P" gnus-uu-mark-map)
1451
1452   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mark-map)
1453     "c" gnus-summary-clear-above
1454     "u" gnus-summary-tick-above
1455     "m" gnus-summary-mark-above
1456     "k" gnus-summary-kill-below)
1457
1458   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
1459     "/" gnus-summary-limit-to-subject
1460     "n" gnus-summary-limit-to-articles
1461     "w" gnus-summary-pop-limit
1462     "s" gnus-summary-limit-to-subject
1463     "a" gnus-summary-limit-to-author
1464     "u" gnus-summary-limit-to-unread
1465     "m" gnus-summary-limit-to-marks
1466     "M" gnus-summary-limit-exclude-marks
1467     "v" gnus-summary-limit-to-score
1468     "*" gnus-summary-limit-include-cached
1469     "D" gnus-summary-limit-include-dormant
1470     "T" gnus-summary-limit-include-thread
1471     "d" gnus-summary-limit-exclude-dormant
1472     "t" gnus-summary-limit-to-age
1473     "x" gnus-summary-limit-to-extra
1474     "E" gnus-summary-limit-include-expunged
1475     "c" gnus-summary-limit-exclude-childless-dormant
1476     "C" gnus-summary-limit-mark-excluded-as-read)
1477
1478   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
1479     "n" gnus-summary-next-unread-article
1480     "p" gnus-summary-prev-unread-article
1481     "N" gnus-summary-next-article
1482     "P" gnus-summary-prev-article
1483     "\C-n" gnus-summary-next-same-subject
1484     "\C-p" gnus-summary-prev-same-subject
1485     "\M-n" gnus-summary-next-unread-subject
1486     "\M-p" gnus-summary-prev-unread-subject
1487     "f" gnus-summary-first-unread-article
1488     "b" gnus-summary-best-unread-article
1489     "j" gnus-summary-goto-article
1490     "g" gnus-summary-goto-subject
1491     "l" gnus-summary-goto-last-article
1492     "o" gnus-summary-pop-article)
1493
1494   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
1495     "k" gnus-summary-kill-thread
1496     "l" gnus-summary-lower-thread
1497     "i" gnus-summary-raise-thread
1498     "T" gnus-summary-toggle-threads
1499     "t" gnus-summary-rethread-current
1500     "^" gnus-summary-reparent-thread
1501     "s" gnus-summary-show-thread
1502     "S" gnus-summary-show-all-threads
1503     "h" gnus-summary-hide-thread
1504     "H" gnus-summary-hide-all-threads
1505     "n" gnus-summary-next-thread
1506     "p" gnus-summary-prev-thread
1507     "u" gnus-summary-up-thread
1508     "o" gnus-summary-top-thread
1509     "d" gnus-summary-down-thread
1510     "#" gnus-uu-mark-thread
1511     "\M-#" gnus-uu-unmark-thread)
1512
1513   (gnus-define-keys (gnus-summary-buffer-map "Y" gnus-summary-mode-map)
1514     "g" gnus-summary-prepare
1515     "c" gnus-summary-insert-cached-articles)
1516
1517   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
1518     "c" gnus-summary-catchup-and-exit
1519     "C" gnus-summary-catchup-all-and-exit
1520     "E" gnus-summary-exit-no-update
1521     "J" gnus-summary-jump-to-other-group
1522     "Q" gnus-summary-exit
1523     "Z" gnus-summary-exit
1524     "n" gnus-summary-catchup-and-goto-next-group
1525     "R" gnus-summary-reselect-current-group
1526     "G" gnus-summary-rescan-group
1527     "N" gnus-summary-next-group
1528     "s" gnus-summary-save-newsrc
1529     "P" gnus-summary-prev-group)
1530
1531   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
1532     " " gnus-summary-next-page
1533     "n" gnus-summary-next-page
1534     "\177" gnus-summary-prev-page
1535     [delete] gnus-summary-prev-page
1536     "p" gnus-summary-prev-page
1537     "\r" gnus-summary-scroll-up
1538     "\M-\r" gnus-summary-scroll-down
1539     "<" gnus-summary-beginning-of-article
1540     ">" gnus-summary-end-of-article
1541     "b" gnus-summary-beginning-of-article
1542     "e" gnus-summary-end-of-article
1543     "^" gnus-summary-refer-parent-article
1544     "r" gnus-summary-refer-parent-article
1545     "D" gnus-summary-enter-digest-group
1546     "R" gnus-summary-refer-references
1547     "T" gnus-summary-refer-thread
1548     "g" gnus-summary-show-article
1549     "s" gnus-summary-isearch-article
1550     "P" gnus-summary-print-article
1551     "t" gnus-article-babel
1552     "d" gnus-summary-decrypt-article
1553     "v" gnus-summary-verify-article)
1554
1555   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
1556     "b" gnus-article-add-buttons
1557     "B" gnus-article-add-buttons-to-head
1558     "o" gnus-article-treat-overstrike
1559     "e" gnus-article-emphasize
1560     "w" gnus-article-fill-cited-article
1561     "Q" gnus-article-fill-long-lines
1562     "C" gnus-article-capitalize-sentences
1563     "c" gnus-article-remove-cr
1564     "Z" gnus-article-decode-HZ
1565     "f" gnus-article-display-x-face
1566     "l" gnus-summary-stop-page-breaking
1567     "r" gnus-summary-caesar-message
1568     "t" gnus-article-toggle-headers
1569     "v" gnus-summary-verbose-headers
1570     "m" gnus-summary-toggle-mime
1571     "H" gnus-article-strip-headers-in-body
1572     "d" gnus-article-treat-dumbquotes
1573     "s" gnus-smiley-display)
1574
1575   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
1576     "a" gnus-article-hide
1577     "h" gnus-article-toggle-headers
1578     "b" gnus-article-hide-boring-headers
1579     "s" gnus-article-hide-signature
1580     "c" gnus-article-hide-citation
1581     "C" gnus-article-hide-citation-in-followups
1582     "l" gnus-article-hide-list-identifiers
1583     "p" gnus-article-hide-pgp
1584     "B" gnus-article-strip-banner
1585     "P" gnus-article-hide-pem
1586     "\C-c" gnus-article-hide-citation-maybe)
1587
1588   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
1589     "a" gnus-article-highlight
1590     "h" gnus-article-highlight-headers
1591     "c" gnus-article-highlight-citation
1592     "s" gnus-article-highlight-signature)
1593
1594   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
1595     "z" gnus-article-date-ut
1596     "u" gnus-article-date-ut
1597     "l" gnus-article-date-local
1598     "e" gnus-article-date-lapsed
1599     "o" gnus-article-date-original
1600     "i" gnus-article-date-iso8601
1601     "s" gnus-article-date-user)
1602
1603   (gnus-define-keys (gnus-summary-wash-empty-map "E" gnus-summary-wash-map)
1604     "t" gnus-article-remove-trailing-blank-lines
1605     "l" gnus-article-strip-leading-blank-lines
1606     "m" gnus-article-strip-multiple-blank-lines
1607     "a" gnus-article-strip-blank-lines
1608     "A" gnus-article-strip-all-blank-lines
1609     "s" gnus-article-strip-leading-space
1610     "e" gnus-article-strip-trailing-space)
1611
1612   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
1613     "v" gnus-version
1614     "f" gnus-summary-fetch-faq
1615     "d" gnus-summary-describe-group
1616     "h" gnus-summary-describe-briefly
1617     "i" gnus-info-find-node)
1618
1619   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
1620     "e" gnus-summary-expire-articles
1621     "\M-\C-e" gnus-summary-expire-articles-now
1622     "\177" gnus-summary-delete-article
1623     [delete] gnus-summary-delete-article
1624     [backspace] gnus-summary-delete-article
1625     "m" gnus-summary-move-article
1626     "r" gnus-summary-respool-article
1627     "w" gnus-summary-edit-article
1628     "c" gnus-summary-copy-article
1629     "B" gnus-summary-crosspost-article
1630     "q" gnus-summary-respool-query
1631     "t" gnus-summary-respool-trace
1632     "i" gnus-summary-import-article
1633     "p" gnus-summary-article-posted-p)
1634
1635   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
1636     "o" gnus-summary-save-article
1637     "m" gnus-summary-save-article-mail
1638     "F" gnus-summary-write-article-file
1639     "r" gnus-summary-save-article-rmail
1640     "f" gnus-summary-save-article-file
1641     "b" gnus-summary-save-article-body-file
1642     "h" gnus-summary-save-article-folder
1643     "v" gnus-summary-save-article-vm
1644     "p" gnus-summary-pipe-output
1645     "s" gnus-soup-add-article)
1646
1647   (gnus-define-keys (gnus-summary-mime-map "K" gnus-summary-mode-map)
1648     "b" gnus-summary-display-buttonized
1649     "m" gnus-summary-repair-multipart
1650     "v" gnus-article-view-part
1651     "o" gnus-article-save-part
1652     "c" gnus-article-copy-part
1653     "e" gnus-article-externalize-part
1654     "i" gnus-article-inline-part
1655     "|" gnus-article-pipe-part))
1656
1657 (defun gnus-summary-make-menu-bar ()
1658   (gnus-turn-off-edit-menu 'summary)
1659
1660   (unless (boundp 'gnus-summary-misc-menu)
1661
1662     (easy-menu-define
1663      gnus-summary-kill-menu gnus-summary-mode-map ""
1664      (cons
1665       "Score"
1666       (nconc
1667        (list
1668         ["Enter score..." gnus-summary-score-entry t]
1669         ["Customize" gnus-score-customize t])
1670        (gnus-make-score-map 'increase)
1671        (gnus-make-score-map 'lower)
1672        '(("Mark"
1673           ["Kill below" gnus-summary-kill-below t]
1674           ["Mark above" gnus-summary-mark-above t]
1675           ["Tick above" gnus-summary-tick-above t]
1676           ["Clear above" gnus-summary-clear-above t])
1677          ["Current score" gnus-summary-current-score t]
1678          ["Set score" gnus-summary-set-score t]
1679          ["Switch current score file..." gnus-score-change-score-file t]
1680          ["Set mark below..." gnus-score-set-mark-below t]
1681          ["Set expunge below..." gnus-score-set-expunge-below t]
1682          ["Edit current score file" gnus-score-edit-current-scores t]
1683          ["Edit score file" gnus-score-edit-file t]
1684          ["Trace score" gnus-score-find-trace t]
1685          ["Find words" gnus-score-find-favourite-words t]
1686          ["Rescore buffer" gnus-summary-rescore t]
1687          ["Increase score..." gnus-summary-increase-score t]
1688          ["Lower score..." gnus-summary-lower-score t]))))
1689
1690     ;; Define both the Article menu in the summary buffer and the equivalent
1691     ;; Commands menu in the article buffer here for consistency.
1692     (let ((innards
1693            '(("Hide"
1694               ["All" gnus-article-hide t]
1695               ["Headers" gnus-article-toggle-headers t]
1696               ["Signature" gnus-article-hide-signature t]
1697               ["Citation" gnus-article-hide-citation t]
1698               ["List identifiers" gnus-article-hide-list-identifiers t]
1699               ["PGP" gnus-article-hide-pgp t]
1700               ["Banner" gnus-article-strip-banner t]
1701               ["Boring headers" gnus-article-hide-boring-headers t])
1702              ("Highlight"
1703               ["All" gnus-article-highlight t]
1704               ["Headers" gnus-article-highlight-headers t]
1705               ["Signature" gnus-article-highlight-signature t]
1706               ["Citation" gnus-article-highlight-citation t])
1707              ("Date"
1708               ["Local" gnus-article-date-local t]
1709               ["ISO8601" gnus-article-date-iso8601 t]
1710               ["UT" gnus-article-date-ut t]
1711               ["Original" gnus-article-date-original t]
1712               ["Lapsed" gnus-article-date-lapsed t]
1713               ["User-defined" gnus-article-date-user t])
1714              ("Washing"
1715               ("Remove Blanks"
1716                ["Leading" gnus-article-strip-leading-blank-lines t]
1717                ["Multiple" gnus-article-strip-multiple-blank-lines t]
1718                ["Trailing" gnus-article-remove-trailing-blank-lines t]
1719                ["All of the above" gnus-article-strip-blank-lines t]
1720                ["All" gnus-article-strip-all-blank-lines t]
1721                ["Leading space" gnus-article-strip-leading-space t]
1722                ["Trailing space" gnus-article-strip-trailing-space t])
1723               ["Overstrike" gnus-article-treat-overstrike t]
1724               ["Dumb quotes" gnus-article-treat-dumbquotes t]
1725               ["Emphasis" gnus-article-emphasize t]
1726               ["Word wrap" gnus-article-fill-cited-article t]
1727               ["Fill long lines" gnus-article-fill-long-lines t]
1728               ["Capitalize sentences" gnus-article-capitalize-sentences t]
1729               ["CR" gnus-article-remove-cr t]
1730               ["Show X-Face" gnus-article-display-x-face t]
1731               ["Rot 13" gnus-summary-caesar-message t]
1732               ["Unix pipe" gnus-summary-pipe-message t]
1733               ["Add buttons" gnus-article-add-buttons t]
1734               ["Add buttons to head" gnus-article-add-buttons-to-head t]
1735               ["Stop page breaking" gnus-summary-stop-page-breaking t]
1736               ["Toggle MIME" gnus-summary-toggle-mime t]
1737               ["Verbose header" gnus-summary-verbose-headers t]
1738               ["Toggle header" gnus-summary-toggle-header t]
1739               ["Toggle smileys" gnus-smiley-display t]
1740               ["HZ" gnus-article-decode-HZ t])
1741              ("Output"
1742               ["Save in default format" gnus-summary-save-article t]
1743               ["Save in file" gnus-summary-save-article-file t]
1744               ["Save in Unix mail format" gnus-summary-save-article-mail t]
1745               ["Save in MH folder" gnus-summary-save-article-folder t]
1746               ["Save in VM folder" gnus-summary-save-article-vm t]
1747               ["Save in RMAIL mbox" gnus-summary-save-article-rmail t]
1748               ["Save body in file" gnus-summary-save-article-body-file t]
1749               ["Pipe through a filter" gnus-summary-pipe-output t]
1750               ["Add to SOUP packet" gnus-soup-add-article t]
1751               ["Print" gnus-summary-print-article t])
1752              ("Backend"
1753               ["Respool article..." gnus-summary-respool-article t]
1754               ["Move article..." gnus-summary-move-article
1755                (gnus-check-backend-function
1756                 'request-move-article gnus-newsgroup-name)]
1757               ["Copy article..." gnus-summary-copy-article t]
1758               ["Crosspost article..." gnus-summary-crosspost-article
1759                (gnus-check-backend-function
1760                 'request-replace-article gnus-newsgroup-name)]
1761               ["Import file..." gnus-summary-import-article t]
1762               ["Check if posted" gnus-summary-article-posted-p t]
1763               ["Edit article" gnus-summary-edit-article
1764                (not (gnus-group-read-only-p))]
1765               ["Delete article" gnus-summary-delete-article
1766                (gnus-check-backend-function
1767                 'request-expire-articles gnus-newsgroup-name)]
1768               ["Query respool" gnus-summary-respool-query t]
1769               ["Trace respool" gnus-summary-respool-trace t]
1770               ["Delete expirable articles" gnus-summary-expire-articles-now
1771                (gnus-check-backend-function
1772                 'request-expire-articles gnus-newsgroup-name)])
1773              ("Extract"
1774               ["Uudecode" gnus-uu-decode-uu t]
1775               ["Uudecode and save" gnus-uu-decode-uu-and-save t]
1776               ["Unshar" gnus-uu-decode-unshar t]
1777               ["Unshar and save" gnus-uu-decode-unshar-and-save t]
1778               ["Save" gnus-uu-decode-save t]
1779               ["Binhex" gnus-uu-decode-binhex t]
1780               ["Postscript" gnus-uu-decode-postscript t])
1781              ("Cache"
1782               ["Enter article" gnus-cache-enter-article t]
1783               ["Remove article" gnus-cache-remove-article t])
1784              ["Translate" gnus-article-babel t]
1785              ["Select article buffer" gnus-summary-select-article-buffer t]
1786              ["Enter digest buffer" gnus-summary-enter-digest-group t]
1787              ["Isearch article..." gnus-summary-isearch-article t]
1788              ["Beginning of the article" gnus-summary-beginning-of-article t]
1789              ["End of the article" gnus-summary-end-of-article t]
1790              ["Fetch parent of article" gnus-summary-refer-parent-article t]
1791              ["Fetch referenced articles" gnus-summary-refer-references t]
1792              ["Fetch current thread" gnus-summary-refer-thread t]
1793              ["Fetch article with id..." gnus-summary-refer-article t]
1794              ["Redisplay" gnus-summary-show-article t])))
1795       (easy-menu-define
1796        gnus-summary-article-menu gnus-summary-mode-map ""
1797        (cons "Article" innards))
1798
1799       (easy-menu-define
1800        gnus-article-commands-menu gnus-article-mode-map ""
1801        (cons "Commands" innards)))
1802
1803     (easy-menu-define
1804      gnus-summary-thread-menu gnus-summary-mode-map ""
1805      '("Threads"
1806        ["Toggle threading" gnus-summary-toggle-threads t]
1807        ["Hide threads" gnus-summary-hide-all-threads t]
1808        ["Show threads" gnus-summary-show-all-threads t]
1809        ["Hide thread" gnus-summary-hide-thread t]
1810        ["Show thread" gnus-summary-show-thread t]
1811        ["Go to next thread" gnus-summary-next-thread t]
1812        ["Go to previous thread" gnus-summary-prev-thread t]
1813        ["Go down thread" gnus-summary-down-thread t]
1814        ["Go up thread" gnus-summary-up-thread t]
1815        ["Top of thread" gnus-summary-top-thread t]
1816        ["Mark thread as read" gnus-summary-kill-thread t]
1817        ["Lower thread score" gnus-summary-lower-thread t]
1818        ["Raise thread score" gnus-summary-raise-thread t]
1819        ["Rethread current" gnus-summary-rethread-current t]))
1820
1821     (easy-menu-define
1822      gnus-summary-post-menu gnus-summary-mode-map ""
1823      '("Post"
1824        ["Post an article" gnus-summary-post-news t]
1825        ["Followup" gnus-summary-followup t]
1826        ["Followup and yank" gnus-summary-followup-with-original t]
1827        ["Supersede article" gnus-summary-supersede-article t]
1828        ["Cancel article" gnus-summary-cancel-article t]
1829        ["Reply" gnus-summary-reply t]
1830        ["Reply and yank" gnus-summary-reply-with-original t]
1831        ["Wide reply" gnus-summary-wide-reply t]
1832        ["Wide reply and yank" gnus-summary-wide-reply-with-original t]
1833        ["Mail forward" gnus-summary-mail-forward t]
1834        ["Post forward" gnus-summary-post-forward t]
1835        ["Digest and mail" gnus-uu-digest-mail-forward t]
1836        ["Digest and post" gnus-uu-digest-post-forward t]
1837        ["Resend message" gnus-summary-resend-message t]
1838        ["Send bounced mail" gnus-summary-resend-bounced-mail t]
1839        ["Send a mail" gnus-summary-mail-other-window t]
1840        ["Uuencode and post" gnus-uu-post-news t]
1841        ["Followup via news" gnus-summary-followup-to-mail t]
1842        ["Followup via news and yank"
1843         gnus-summary-followup-to-mail-with-original t]
1844        ;;("Draft"
1845        ;;["Send" gnus-summary-send-draft t]
1846        ;;["Send bounced" gnus-resend-bounced-mail t])
1847        ))
1848
1849     (easy-menu-define
1850      gnus-summary-misc-menu gnus-summary-mode-map ""
1851      '("Misc"
1852        ("Mark Read"
1853         ["Mark as read" gnus-summary-mark-as-read-forward t]
1854         ["Mark same subject and select"
1855          gnus-summary-kill-same-subject-and-select t]
1856         ["Mark same subject" gnus-summary-kill-same-subject t]
1857         ["Catchup" gnus-summary-catchup t]
1858         ["Catchup all" gnus-summary-catchup-all t]
1859         ["Catchup to here" gnus-summary-catchup-to-here t]
1860         ["Catchup region" gnus-summary-mark-region-as-read t]
1861         ["Mark excluded" gnus-summary-limit-mark-excluded-as-read t])
1862        ("Mark Various"
1863         ["Tick" gnus-summary-tick-article-forward t]
1864         ["Mark as dormant" gnus-summary-mark-as-dormant t]
1865         ["Remove marks" gnus-summary-clear-mark-forward t]
1866         ["Set expirable mark" gnus-summary-mark-as-expirable t]
1867         ["Set bookmark" gnus-summary-set-bookmark t]
1868         ["Remove bookmark" gnus-summary-remove-bookmark t])
1869        ("Mark Limit"
1870         ["Marks..." gnus-summary-limit-to-marks t]
1871         ["Subject..." gnus-summary-limit-to-subject t]
1872         ["Author..." gnus-summary-limit-to-author t]
1873         ["Age..." gnus-summary-limit-to-age t]
1874         ["Extra..." gnus-summary-limit-to-extra t]
1875         ["Score" gnus-summary-limit-to-score t]
1876         ["Unread" gnus-summary-limit-to-unread t]
1877         ["Non-dormant" gnus-summary-limit-exclude-dormant t]
1878         ["Articles" gnus-summary-limit-to-articles t]
1879         ["Pop limit" gnus-summary-pop-limit t]
1880         ["Show dormant" gnus-summary-limit-include-dormant t]
1881         ["Hide childless dormant"
1882          gnus-summary-limit-exclude-childless-dormant t]
1883         ;;["Hide thread" gnus-summary-limit-exclude-thread t]
1884         ["Hide marked" gnus-summary-limit-exclude-marks t]
1885         ["Show expunged" gnus-summary-show-all-expunged t])
1886        ("Process Mark"
1887         ["Set mark" gnus-summary-mark-as-processable t]
1888         ["Remove mark" gnus-summary-unmark-as-processable t]
1889         ["Remove all marks" gnus-summary-unmark-all-processable t]
1890         ["Mark above" gnus-uu-mark-over t]
1891         ["Mark series" gnus-uu-mark-series t]
1892         ["Mark region" gnus-uu-mark-region t]
1893         ["Unmark region" gnus-uu-unmark-region t]
1894         ["Mark by regexp..." gnus-uu-mark-by-regexp t]
1895         ["Unmark by regexp..." gnus-uu-unmark-by-regexp t]
1896         ["Mark all" gnus-uu-mark-all t]
1897         ["Mark buffer" gnus-uu-mark-buffer t]
1898         ["Mark sparse" gnus-uu-mark-sparse t]
1899         ["Mark thread" gnus-uu-mark-thread t]
1900         ["Unmark thread" gnus-uu-unmark-thread t]
1901         ("Process Mark Sets"
1902          ["Kill" gnus-summary-kill-process-mark t]
1903          ["Yank" gnus-summary-yank-process-mark
1904           gnus-newsgroup-process-stack]
1905          ["Save" gnus-summary-save-process-mark t]))
1906        ("Scroll article"
1907         ["Page forward" gnus-summary-next-page t]
1908         ["Page backward" gnus-summary-prev-page t]
1909         ["Line forward" gnus-summary-scroll-up t])
1910        ("Move"
1911         ["Next unread article" gnus-summary-next-unread-article t]
1912         ["Previous unread article" gnus-summary-prev-unread-article t]
1913         ["Next article" gnus-summary-next-article t]
1914         ["Previous article" gnus-summary-prev-article t]
1915         ["Next unread subject" gnus-summary-next-unread-subject t]
1916         ["Previous unread subject" gnus-summary-prev-unread-subject t]
1917         ["Next article same subject" gnus-summary-next-same-subject t]
1918         ["Previous article same subject" gnus-summary-prev-same-subject t]
1919         ["First unread article" gnus-summary-first-unread-article t]
1920         ["Best unread article" gnus-summary-best-unread-article t]
1921         ["Go to subject number..." gnus-summary-goto-subject t]
1922         ["Go to article number..." gnus-summary-goto-article t]
1923         ["Go to the last article" gnus-summary-goto-last-article t]
1924         ["Pop article off history" gnus-summary-pop-article t])
1925        ("Sort"
1926         ["Sort by number" gnus-summary-sort-by-number t]
1927         ["Sort by author" gnus-summary-sort-by-author t]
1928         ["Sort by subject" gnus-summary-sort-by-subject t]
1929         ["Sort by date" gnus-summary-sort-by-date t]
1930         ["Sort by score" gnus-summary-sort-by-score t]
1931         ["Sort by lines" gnus-summary-sort-by-lines t]
1932         ["Sort by characters" gnus-summary-sort-by-chars t])
1933        ("Help"
1934         ["Fetch group FAQ" gnus-summary-fetch-faq t]
1935         ["Describe group" gnus-summary-describe-group t]
1936         ["Read manual" gnus-info-find-node t])
1937        ("Modes"
1938         ["Pick and read" gnus-pick-mode t]
1939         ["Binary" gnus-binary-mode t])
1940        ("Regeneration"
1941         ["Regenerate" gnus-summary-prepare t]
1942         ["Insert cached articles" gnus-summary-insert-cached-articles t]
1943         ["Toggle threading" gnus-summary-toggle-threads t])
1944        ["Filter articles..." gnus-summary-execute-command t]
1945        ["Run command on subjects..." gnus-summary-universal-argument t]
1946        ["Search articles forward..." gnus-summary-search-article-forward t]
1947        ["Search articles backward..." gnus-summary-search-article-backward t]
1948        ["Toggle line truncation" gnus-summary-toggle-truncation t]
1949        ["Expand window" gnus-summary-expand-window t]
1950        ["Expire expirable articles" gnus-summary-expire-articles
1951         (gnus-check-backend-function
1952          'request-expire-articles gnus-newsgroup-name)]
1953        ["Edit local kill file" gnus-summary-edit-local-kill t]
1954        ["Edit main kill file" gnus-summary-edit-global-kill t]
1955        ["Edit group parameters" gnus-summary-edit-parameters t]
1956        ["Customize group parameters" gnus-summary-customize-parameters t]
1957        ["Send a bug report" gnus-bug t]
1958        ("Exit"
1959         ["Catchup and exit" gnus-summary-catchup-and-exit t]
1960         ["Catchup all and exit" gnus-summary-catchup-all-and-exit t]
1961         ["Catchup and goto next" gnus-summary-catchup-and-goto-next-group t]
1962         ["Exit group" gnus-summary-exit t]
1963         ["Exit group without updating" gnus-summary-exit-no-update t]
1964         ["Exit and goto next group" gnus-summary-next-group t]
1965         ["Exit and goto prev group" gnus-summary-prev-group t]
1966         ["Reselect group" gnus-summary-reselect-current-group t]
1967         ["Rescan group" gnus-summary-rescan-group t]
1968         ["Update dribble" gnus-summary-save-newsrc t])))
1969
1970     (gnus-run-hooks 'gnus-summary-menu-hook)))
1971
1972 (defun gnus-score-set-default (var value)
1973   "A version of set that updates the GNU Emacs menu-bar."
1974   (set var value)
1975   ;; It is the message that forces the active status to be updated.
1976   (message ""))
1977
1978 (defun gnus-make-score-map (type)
1979   "Make a summary score map of type TYPE."
1980   (if t
1981       nil
1982     (let ((headers '(("author" "from" string)
1983                      ("subject" "subject" string)
1984                      ("article body" "body" string)
1985                      ("article head" "head" string)
1986                      ("xref" "xref" string)
1987                      ("extra header" "extra" string)
1988                      ("lines" "lines" number)
1989                      ("followups to author" "followup" string)))
1990           (types '((number ("less than" <)
1991                            ("greater than" >)
1992                            ("equal" =))
1993                    (string ("substring" s)
1994                            ("exact string" e)
1995                            ("fuzzy string" f)
1996                            ("regexp" r))))
1997           (perms '(("temporary" (current-time-string))
1998                    ("permanent" nil)
1999                    ("immediate" now)))
2000           header)
2001       (list
2002        (apply
2003         'nconc
2004         (list
2005          (if (eq type 'lower)
2006              "Lower score"
2007            "Increase score"))
2008         (let (outh)
2009           (while headers
2010             (setq header (car headers))
2011             (setq outh
2012                   (cons
2013                    (apply
2014                     'nconc
2015                     (list (car header))
2016                     (let ((ts (cdr (assoc (nth 2 header) types)))
2017                           outt)
2018                       (while ts
2019                         (setq outt
2020                               (cons
2021                                (apply
2022                                 'nconc
2023                                 (list (caar ts))
2024                                 (let ((ps perms)
2025                                       outp)
2026                                   (while ps
2027                                     (setq outp
2028                                           (cons
2029                                            (vector
2030                                             (caar ps)
2031                                             (list
2032                                              'gnus-summary-score-entry
2033                                              (nth 1 header)
2034                                              (if (or (string= (nth 1 header)
2035                                                               "head")
2036                                                      (string= (nth 1 header)
2037                                                               "body"))
2038                                                  ""
2039                                                (list 'gnus-summary-header
2040                                                      (nth 1 header)))
2041                                              (list 'quote (nth 1 (car ts)))
2042                                              (list 'gnus-score-delta-default
2043                                                    nil)
2044                                              (nth 1 (car ps))
2045                                              t)
2046                                             t)
2047                                            outp))
2048                                     (setq ps (cdr ps)))
2049                                   (list (nreverse outp))))
2050                                outt))
2051                         (setq ts (cdr ts)))
2052                       (list (nreverse outt))))
2053                    outh))
2054             (setq headers (cdr headers)))
2055           (list (nreverse outh))))))))
2056
2057 \f
2058
2059 (defun gnus-summary-mode (&optional group)
2060   "Major mode for reading articles.
2061
2062 All normal editing commands are switched off.
2063 \\<gnus-summary-mode-map>
2064 Each line in this buffer represents one article.  To read an
2065 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
2066 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
2067 respectively.
2068
2069 You can also post articles and send mail from this buffer.  To
2070 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
2071 of an article, type `\\[gnus-summary-reply]'.
2072
2073 There are approx. one gazillion commands you can execute in this
2074 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
2075
2076 The following commands are available:
2077
2078 \\{gnus-summary-mode-map}"
2079   (interactive)
2080   (when (gnus-visual-p 'summary-menu 'menu)
2081     (gnus-summary-make-menu-bar))
2082   (kill-all-local-variables)
2083   (gnus-summary-make-local-variables)
2084   (gnus-make-thread-indent-array)
2085   (gnus-simplify-mode-line)
2086   (setq major-mode 'gnus-summary-mode)
2087   (setq mode-name "Summary")
2088   (make-local-variable 'minor-mode-alist)
2089   (use-local-map gnus-summary-mode-map)
2090   (buffer-disable-undo)
2091   (setq buffer-read-only t)             ;Disable modification
2092   (setq truncate-lines t)
2093   (setq selective-display t)
2094   (setq selective-display-ellipses t)   ;Display `...'
2095   (gnus-summary-set-display-table)
2096   (gnus-set-default-directory)
2097   (setq gnus-newsgroup-name group)
2098   (unless (gnus-news-group-p group)
2099     (setq gnus-newsgroup-incorporated
2100           (nnmail-new-mail-numbers (gnus-group-real-name group))))
2101   (make-local-variable 'gnus-summary-line-format)
2102   (make-local-variable 'gnus-summary-line-format-spec)
2103   (make-local-variable 'gnus-summary-dummy-line-format)
2104   (make-local-variable 'gnus-summary-dummy-line-format-spec)
2105   (make-local-variable 'gnus-summary-mark-positions)
2106   (make-local-hook 'pre-command-hook)
2107   (add-hook 'pre-command-hook 'gnus-set-global-variables nil t)
2108   (gnus-run-hooks 'gnus-summary-mode-hook)
2109   (gnus-update-format-specifications nil 'summary 'summary-mode 'summary-dummy)
2110   (gnus-update-summary-mark-positions))
2111
2112 (defun gnus-summary-make-local-variables ()
2113   "Make all the local summary buffer variables."
2114   (let (global)
2115     (dolist (local gnus-summary-local-variables)
2116       (if (consp local)
2117           (progn
2118             (if (eq (cdr local) 'global)
2119                 ;; Copy the global value of the variable.
2120                 (setq global (symbol-value (car local)))
2121               ;; Use the value from the list.
2122               (setq global (eval (cdr local))))
2123             (set (make-local-variable (car local)) global))
2124         ;; Simple nil-valued local variable.
2125         (set (make-local-variable local) nil)))))
2126
2127 (defun gnus-summary-clear-local-variables ()
2128   (let ((locals gnus-summary-local-variables))
2129     (while locals
2130       (if (consp (car locals))
2131           (and (vectorp (caar locals))
2132                (set (caar locals) nil))
2133         (and (vectorp (car locals))
2134              (set (car locals) nil)))
2135       (setq locals (cdr locals)))))
2136
2137 ;; Summary data functions.
2138
2139 (defmacro gnus-data-number (data)
2140   `(car ,data))
2141
2142 (defmacro gnus-data-set-number (data number)
2143   `(setcar ,data ,number))
2144
2145 (defmacro gnus-data-mark (data)
2146   `(nth 1 ,data))
2147
2148 (defmacro gnus-data-set-mark (data mark)
2149   `(setcar (nthcdr 1 ,data) ,mark))
2150
2151 (defmacro gnus-data-pos (data)
2152   `(nth 2 ,data))
2153
2154 (defmacro gnus-data-set-pos (data pos)
2155   `(setcar (nthcdr 2 ,data) ,pos))
2156
2157 (defmacro gnus-data-header (data)
2158   `(nth 3 ,data))
2159
2160 (defmacro gnus-data-set-header (data header)
2161   `(setcar (nthcdr 3 ,data) ,header))
2162
2163 (defmacro gnus-data-level (data)
2164   `(nth 4 ,data))
2165
2166 (defmacro gnus-data-unread-p (data)
2167   `(= (nth 1 ,data) gnus-unread-mark))
2168
2169 (defmacro gnus-data-read-p (data)
2170   `(/= (nth 1 ,data) gnus-unread-mark))
2171
2172 (defmacro gnus-data-pseudo-p (data)
2173   `(consp (nth 3 ,data)))
2174
2175 (defmacro gnus-data-find (number)
2176   `(assq ,number gnus-newsgroup-data))
2177
2178 (defmacro gnus-data-find-list (number &optional data)
2179   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
2180      (memq (assq ,number bdata)
2181            bdata)))
2182
2183 (defmacro gnus-data-make (number mark pos header level)
2184   `(list ,number ,mark ,pos ,header ,level))
2185
2186 (defun gnus-data-enter (after-article number mark pos header level offset)
2187   (let ((data (gnus-data-find-list after-article)))
2188     (unless data
2189       (error "No such article: %d" after-article))
2190     (setcdr data (cons (gnus-data-make number mark pos header level)
2191                        (cdr data)))
2192     (setq gnus-newsgroup-data-reverse nil)
2193     (gnus-data-update-list (cddr data) offset)))
2194
2195 (defun gnus-data-enter-list (after-article list &optional offset)
2196   (when list
2197     (let ((data (and after-article (gnus-data-find-list after-article)))
2198           (ilist list))
2199       (if (not (or data
2200                    after-article))
2201           (let ((odata gnus-newsgroup-data))
2202             (setq gnus-newsgroup-data (nconc list gnus-newsgroup-data))
2203             (when offset
2204               (gnus-data-update-list odata offset)))
2205         ;; Find the last element in the list to be spliced into the main
2206         ;; list.
2207         (while (cdr list)
2208           (setq list (cdr list)))
2209         (if (not data)
2210             (progn
2211               (setcdr list gnus-newsgroup-data)
2212               (setq gnus-newsgroup-data ilist)
2213               (when offset
2214                 (gnus-data-update-list (cdr list) offset)))
2215           (setcdr list (cdr data))
2216           (setcdr data ilist)
2217           (when offset
2218             (gnus-data-update-list (cdr list) offset))))
2219       (setq gnus-newsgroup-data-reverse nil))))
2220
2221 (defun gnus-data-remove (article &optional offset)
2222   (let ((data gnus-newsgroup-data))
2223     (if (= (gnus-data-number (car data)) article)
2224         (progn
2225           (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
2226                 gnus-newsgroup-data-reverse nil)
2227           (when offset
2228             (gnus-data-update-list gnus-newsgroup-data offset)))
2229       (while (cdr data)
2230         (when (= (gnus-data-number (cadr data)) article)
2231           (setcdr data (cddr data))
2232           (when offset
2233             (gnus-data-update-list (cdr data) offset))
2234           (setq data nil
2235                 gnus-newsgroup-data-reverse nil))
2236         (setq data (cdr data))))))
2237
2238 (defmacro gnus-data-list (backward)
2239   `(if ,backward
2240        (or gnus-newsgroup-data-reverse
2241            (setq gnus-newsgroup-data-reverse
2242                  (reverse gnus-newsgroup-data)))
2243      gnus-newsgroup-data))
2244
2245 (defun gnus-data-update-list (data offset)
2246   "Add OFFSET to the POS of all data entries in DATA."
2247   (setq gnus-newsgroup-data-reverse nil)
2248   (while data
2249     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
2250     (setq data (cdr data))))
2251
2252 (defun gnus-summary-article-pseudo-p (article)
2253   "Say whether this article is a pseudo article or not."
2254   (not (vectorp (gnus-data-header (gnus-data-find article)))))
2255
2256 (defmacro gnus-summary-article-sparse-p (article)
2257   "Say whether this article is a sparse article or not."
2258   `(memq ,article gnus-newsgroup-sparse))
2259
2260 (defmacro gnus-summary-article-ancient-p (article)
2261   "Say whether this article is a sparse article or not."
2262   `(memq ,article gnus-newsgroup-ancient))
2263
2264 (defun gnus-article-parent-p (number)
2265   "Say whether this article is a parent or not."
2266   (let ((data (gnus-data-find-list number)))
2267     (and (cdr data)                     ; There has to be an article after...
2268          (< (gnus-data-level (car data)) ; And it has to have a higher level.
2269             (gnus-data-level (nth 1 data))))))
2270
2271 (defun gnus-article-children (number)
2272   "Return a list of all children to NUMBER."
2273   (let* ((data (gnus-data-find-list number))
2274          (level (gnus-data-level (car data)))
2275          children)
2276     (setq data (cdr data))
2277     (while (and data
2278                 (= (gnus-data-level (car data)) (1+ level)))
2279       (push (gnus-data-number (car data)) children)
2280       (setq data (cdr data)))
2281     children))
2282
2283 (defmacro gnus-summary-skip-intangible ()
2284   "If the current article is intangible, then jump to a different article."
2285   '(let ((to (get-text-property (point) 'gnus-intangible)))
2286      (and to (gnus-summary-goto-subject to))))
2287
2288 (defmacro gnus-summary-article-intangible-p ()
2289   "Say whether this article is intangible or not."
2290   '(get-text-property (point) 'gnus-intangible))
2291
2292 (defun gnus-article-read-p (article)
2293   "Say whether ARTICLE is read or not."
2294   (not (or (memq article gnus-newsgroup-marked)
2295            (memq article gnus-newsgroup-unreads)
2296            (memq article gnus-newsgroup-unselected)
2297            (memq article gnus-newsgroup-dormant))))
2298
2299 ;; Some summary mode macros.
2300
2301 (defmacro gnus-summary-article-number ()
2302   "The article number of the article on the current line.
2303 If there isn's an article number here, then we return the current
2304 article number."
2305   '(progn
2306      (gnus-summary-skip-intangible)
2307      (or (get-text-property (point) 'gnus-number)
2308          (gnus-summary-last-subject))))
2309
2310 (defmacro gnus-summary-article-header (&optional number)
2311   "Return the header of article NUMBER."
2312   `(gnus-data-header (gnus-data-find
2313                       ,(or number '(gnus-summary-article-number)))))
2314
2315 (defmacro gnus-summary-thread-level (&optional number)
2316   "Return the level of thread that starts with article NUMBER."
2317   `(if (and (eq gnus-summary-make-false-root 'dummy)
2318             (get-text-property (point) 'gnus-intangible))
2319        0
2320      (gnus-data-level (gnus-data-find
2321                        ,(or number '(gnus-summary-article-number))))))
2322
2323 (defmacro gnus-summary-article-mark (&optional number)
2324   "Return the mark of article NUMBER."
2325   `(gnus-data-mark (gnus-data-find
2326                     ,(or number '(gnus-summary-article-number)))))
2327
2328 (defmacro gnus-summary-article-pos (&optional number)
2329   "Return the position of the line of article NUMBER."
2330   `(gnus-data-pos (gnus-data-find
2331                    ,(or number '(gnus-summary-article-number)))))
2332
2333 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
2334 (defmacro gnus-summary-article-subject (&optional number)
2335   "Return current subject string or nil if nothing."
2336   `(let ((headers
2337           ,(if number
2338                `(gnus-data-header (assq ,number gnus-newsgroup-data))
2339              '(gnus-data-header (assq (gnus-summary-article-number)
2340                                       gnus-newsgroup-data)))))
2341      (and headers
2342           (vectorp headers)
2343           (mail-header-subject headers))))
2344
2345 (defmacro gnus-summary-article-score (&optional number)
2346   "Return current article score."
2347   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
2348                   gnus-newsgroup-scored))
2349        gnus-summary-default-score 0))
2350
2351 (defun gnus-summary-article-children (&optional number)
2352   "Return a list of article numbers that are children of article NUMBER."
2353   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
2354          (level (gnus-data-level (car data)))
2355          l children)
2356     (while (and (setq data (cdr data))
2357                 (> (setq l (gnus-data-level (car data))) level))
2358       (and (= (1+ level) l)
2359            (push (gnus-data-number (car data))
2360                  children)))
2361     (nreverse children)))
2362
2363 (defun gnus-summary-article-parent (&optional number)
2364   "Return the article number of the parent of article NUMBER."
2365   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
2366                                     (gnus-data-list t)))
2367          (level (gnus-data-level (car data))))
2368     (if (zerop level)
2369         ()                              ; This is a root.
2370       ;; We search until we find an article with a level less than
2371       ;; this one.  That function has to be the parent.
2372       (while (and (setq data (cdr data))
2373                   (not (< (gnus-data-level (car data)) level))))
2374       (and data (gnus-data-number (car data))))))
2375
2376 (defun gnus-unread-mark-p (mark)
2377   "Say whether MARK is the unread mark."
2378   (= mark gnus-unread-mark))
2379
2380 (defun gnus-read-mark-p (mark)
2381   "Say whether MARK is one of the marks that mark as read.
2382 This is all marks except unread, ticked, dormant, and expirable."
2383   (not (or (= mark gnus-unread-mark)
2384            (= mark gnus-ticked-mark)
2385            (= mark gnus-dormant-mark)
2386            (= mark gnus-expirable-mark))))
2387
2388 (defmacro gnus-article-mark (number)
2389   "Return the MARK of article NUMBER.
2390 This macro should only be used when computing the mark the \"first\"
2391 time; i.e., when generating the summary lines.  After that,
2392 `gnus-summary-article-mark' should be used to examine the
2393 marks of articles."
2394   `(cond
2395     ((memq ,number gnus-newsgroup-unsendable) gnus-unsendable-mark)
2396     ((memq ,number gnus-newsgroup-undownloaded) gnus-undownloaded-mark)
2397     ((memq ,number gnus-newsgroup-downloadable) gnus-downloadable-mark)
2398     ((memq ,number gnus-newsgroup-unreads) gnus-unread-mark)
2399     ((memq ,number gnus-newsgroup-marked) gnus-ticked-mark)
2400     ((memq ,number gnus-newsgroup-dormant) gnus-dormant-mark)
2401     ((memq ,number gnus-newsgroup-expirable) gnus-expirable-mark)
2402     (t (or (cdr (assq ,number gnus-newsgroup-reads))
2403            gnus-ancient-mark))))
2404
2405 ;; Saving hidden threads.
2406
2407 (put 'gnus-save-hidden-threads 'lisp-indent-function 0)
2408 (put 'gnus-save-hidden-threads 'edebug-form-spec '(body))
2409
2410 (defmacro gnus-save-hidden-threads (&rest forms)
2411   "Save hidden threads, eval FORMS, and restore the hidden threads."
2412   (let ((config (make-symbol "config")))
2413     `(let ((,config (gnus-hidden-threads-configuration)))
2414        (unwind-protect
2415            (save-excursion
2416              ,@forms)
2417          (gnus-restore-hidden-threads-configuration ,config)))))
2418
2419 (defun gnus-data-compute-positions ()
2420   "Compute the positions of all articles."
2421   (setq gnus-newsgroup-data-reverse nil)
2422   (let ((data gnus-newsgroup-data))
2423     (save-excursion
2424       (gnus-save-hidden-threads
2425         (gnus-summary-show-all-threads)
2426         (goto-char (point-min))
2427         (while data
2428           (while (get-text-property (point) 'gnus-intangible)
2429             (forward-line 1))
2430           (gnus-data-set-pos (car data) (+ (point) 3))
2431           (setq data (cdr data))
2432           (forward-line 1))))))
2433
2434 (defun gnus-hidden-threads-configuration ()
2435   "Return the current hidden threads configuration."
2436   (save-excursion
2437     (let (config)
2438       (goto-char (point-min))
2439       (while (search-forward "\r" nil t)
2440         (push (1- (point)) config))
2441       config)))
2442
2443 (defun gnus-restore-hidden-threads-configuration (config)
2444   "Restore hidden threads configuration from CONFIG."
2445   (save-excursion
2446     (let (point buffer-read-only)
2447       (while (setq point (pop config))
2448         (when (and (< point (point-max))
2449                    (goto-char point)
2450                    (eq (char-after) ?\n))
2451           (subst-char-in-region point (1+ point) ?\n ?\r))))))
2452
2453 ;; Various summary mode internalish functions.
2454
2455 (defun gnus-mouse-pick-article (e)
2456   (interactive "e")
2457   (mouse-set-point e)
2458   (gnus-summary-next-page nil t))
2459
2460 (defun gnus-summary-set-display-table ()
2461   "Change the display table.
2462 Odd characters have a tendency to mess
2463 up nicely formatted displays - we make all possible glyphs
2464 display only a single character."
2465
2466   ;; We start from the standard display table, if any.
2467   (let ((table (or (copy-sequence standard-display-table)
2468                    (make-display-table)))
2469         (i 32))
2470     ;; Nix out all the control chars...
2471     (while (>= (setq i (1- i)) 0)
2472       (aset table i [??]))
2473     ;; ... but not newline and cr, of course.  (cr is necessary for the
2474     ;; selective display).
2475     (aset table ?\n nil)
2476     (aset table ?\r nil)
2477     ;; We keep TAB as well.
2478     (aset table ?\t nil)
2479     ;; We nix out any glyphs over 126 that are not set already.
2480     (let ((i 256))
2481       (while (>= (setq i (1- i)) 127)
2482         ;; Only modify if the entry is nil.
2483         (unless (aref table i)
2484           (aset table i [??]))))
2485     (setq buffer-display-table table)))
2486
2487 (defun gnus-summary-setup-buffer (group)
2488   "Initialize summary buffer."
2489   (let ((buffer (concat "*Summary " group "*")))
2490     (if (get-buffer buffer)
2491         (progn
2492           (set-buffer buffer)
2493           (setq gnus-summary-buffer (current-buffer))
2494           (not gnus-newsgroup-prepared))
2495       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
2496       (setq gnus-summary-buffer (set-buffer (gnus-get-buffer-create buffer)))
2497       (gnus-summary-mode group)
2498       (when gnus-carpal
2499         (gnus-carpal-setup-buffer 'summary))
2500       (unless gnus-single-article-buffer
2501         (make-local-variable 'gnus-article-buffer)
2502         (make-local-variable 'gnus-article-current)
2503         (make-local-variable 'gnus-original-article-buffer))
2504       (setq gnus-newsgroup-name group)
2505       t)))
2506
2507 (defun gnus-set-global-variables ()
2508   "Set the global equivalents of the buffer-local variables.
2509 They are set to the latest values they had.  These reflect the summary
2510 buffer that was in action when the last article was fetched."
2511   (when (eq major-mode 'gnus-summary-mode)
2512     (setq gnus-summary-buffer (current-buffer))
2513     (let ((name gnus-newsgroup-name)
2514           (marked gnus-newsgroup-marked)
2515           (unread gnus-newsgroup-unreads)
2516           (headers gnus-current-headers)
2517           (data gnus-newsgroup-data)
2518           (summary gnus-summary-buffer)
2519           (article-buffer gnus-article-buffer)
2520           (original gnus-original-article-buffer)
2521           (gac gnus-article-current)
2522           (reffed gnus-reffed-article-number)
2523           (score-file gnus-current-score-file)
2524           (default-charset gnus-newsgroup-charset))
2525       (save-excursion
2526         (set-buffer gnus-group-buffer)
2527         (setq gnus-newsgroup-name name
2528               gnus-newsgroup-marked marked
2529               gnus-newsgroup-unreads unread
2530               gnus-current-headers headers
2531               gnus-newsgroup-data data
2532               gnus-article-current gac
2533               gnus-summary-buffer summary
2534               gnus-article-buffer article-buffer
2535               gnus-original-article-buffer original
2536               gnus-reffed-article-number reffed
2537               gnus-current-score-file score-file
2538               gnus-newsgroup-charset default-charset)
2539         ;; The article buffer also has local variables.
2540         (when (gnus-buffer-live-p gnus-article-buffer)
2541           (set-buffer gnus-article-buffer)
2542           (setq gnus-summary-buffer summary))))))
2543
2544 (defun gnus-summary-article-unread-p (article)
2545   "Say whether ARTICLE is unread or not."
2546   (memq article gnus-newsgroup-unreads))
2547
2548 (defun gnus-summary-first-article-p (&optional article)
2549   "Return whether ARTICLE is the first article in the buffer."
2550   (if (not (setq article (or article (gnus-summary-article-number))))
2551       nil
2552     (eq article (caar gnus-newsgroup-data))))
2553
2554 (defun gnus-summary-last-article-p (&optional article)
2555   "Return whether ARTICLE is the last article in the buffer."
2556   (if (not (setq article (or article (gnus-summary-article-number))))
2557       ;; All non-existent numbers are the last article.  :-)
2558       t
2559     (not (cdr (gnus-data-find-list article)))))
2560
2561 (defun gnus-make-thread-indent-array ()
2562   (let ((n 200))
2563     (unless (and gnus-thread-indent-array
2564                  (= gnus-thread-indent-level gnus-thread-indent-array-level))
2565       (setq gnus-thread-indent-array (make-vector 201 "")
2566             gnus-thread-indent-array-level gnus-thread-indent-level)
2567       (while (>= n 0)
2568         (aset gnus-thread-indent-array n
2569               (make-string (* n gnus-thread-indent-level) ? ))
2570         (setq n (1- n))))))
2571
2572 (defun gnus-update-summary-mark-positions ()
2573   "Compute where the summary marks are to go."
2574   (save-excursion
2575     (when (gnus-buffer-exists-p gnus-summary-buffer)
2576       (set-buffer gnus-summary-buffer))
2577     (let ((gnus-replied-mark 129)
2578           (gnus-score-below-mark 130)
2579           (gnus-score-over-mark 130)
2580           (gnus-download-mark 131)
2581           (spec gnus-summary-line-format-spec)
2582           gnus-visual pos)
2583       (save-excursion
2584         (gnus-set-work-buffer)
2585         (let ((gnus-summary-line-format-spec spec)
2586               (gnus-newsgroup-downloadable '((0 . t))))
2587           (gnus-summary-insert-line
2588            (make-full-mail-header 0 "" "nobody" "" "" "" 0 0 "" nil)
2589            0 nil 128 t nil "" nil 1)
2590           (goto-char (point-min))
2591           (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2592                                              (- (point) 2)))))
2593           (goto-char (point-min))
2594           (push (cons 'replied (and (search-forward "\201" nil t)
2595                                     (- (point) 2)))
2596                 pos)
2597           (goto-char (point-min))
2598           (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2599                 pos)
2600           (goto-char (point-min))
2601           (push (cons 'download
2602                       (and (search-forward "\203" nil t) (- (point) 2)))
2603                 pos)))
2604       (setq gnus-summary-mark-positions pos))))
2605
2606 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
2607   "Insert a dummy root in the summary buffer."
2608   (beginning-of-line)
2609   (gnus-add-text-properties
2610    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
2611    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
2612
2613 (defun gnus-summary-from-or-to-or-newsgroups (header)
2614   (let ((to (cdr (assq 'To (mail-header-extra header))))
2615         (newsgroups (cdr (assq 'Newsgroups (mail-header-extra header))))
2616         (default-mime-charset (with-current-buffer gnus-summary-buffer
2617                                 default-mime-charset)))
2618     (cond
2619      ((and to
2620            gnus-ignored-from-addresses
2621            (string-match gnus-ignored-from-addresses
2622                          (mail-header-from header)))
2623       (concat "-> "
2624               (or (car (funcall gnus-extract-address-components
2625                                 (funcall
2626                                  gnus-decode-encoded-word-function to)))
2627                   (funcall gnus-decode-encoded-word-function to))))
2628      ((and newsgroups
2629            gnus-ignored-from-addresses
2630            (string-match gnus-ignored-from-addresses
2631                          (mail-header-from header)))
2632       (concat "=> " newsgroups))
2633      (t
2634       (or (car (funcall gnus-extract-address-components
2635                         (mail-header-from header)))
2636           (mail-header-from header))))))
2637
2638 (defun gnus-summary-insert-line (gnus-tmp-header
2639                                  gnus-tmp-level gnus-tmp-current
2640                                  gnus-tmp-unread gnus-tmp-replied
2641                                  gnus-tmp-expirable gnus-tmp-subject-or-nil
2642                                  &optional gnus-tmp-dummy gnus-tmp-score
2643                                  gnus-tmp-process)
2644   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
2645          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
2646          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
2647          (gnus-tmp-score-char
2648           (if (or (null gnus-summary-default-score)
2649                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
2650                       gnus-summary-zcore-fuzz))
2651               ?  ;Whitespace
2652             (if (< gnus-tmp-score gnus-summary-default-score)
2653                 gnus-score-below-mark gnus-score-over-mark)))
2654          (gnus-tmp-replied
2655           (cond (gnus-tmp-process gnus-process-mark)
2656                 ((memq gnus-tmp-current gnus-newsgroup-cached)
2657                  gnus-cached-mark)
2658                 (gnus-tmp-replied gnus-replied-mark)
2659                 ((memq gnus-tmp-current gnus-newsgroup-saved)
2660                  gnus-saved-mark)
2661                 (t gnus-unread-mark)))
2662          (gnus-tmp-from (mail-header-from gnus-tmp-header))
2663          (gnus-tmp-name
2664           (cond
2665            ((string-match "<[^>]+> *$" gnus-tmp-from)
2666             (let ((beg (match-beginning 0)))
2667               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
2668                        (substring gnus-tmp-from (1+ (match-beginning 0))
2669                                   (1- (match-end 0))))
2670                   (substring gnus-tmp-from 0 beg))))
2671            ((string-match "(.+)" gnus-tmp-from)
2672             (substring gnus-tmp-from
2673                        (1+ (match-beginning 0)) (1- (match-end 0))))
2674            (t gnus-tmp-from)))
2675          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
2676          (gnus-tmp-number (mail-header-number gnus-tmp-header))
2677          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
2678          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
2679          (buffer-read-only nil))
2680     (when (string= gnus-tmp-name "")
2681       (setq gnus-tmp-name gnus-tmp-from))
2682     (unless (numberp gnus-tmp-lines)
2683       (setq gnus-tmp-lines 0))
2684     (gnus-put-text-property-excluding-characters-with-faces
2685      (point)
2686      (progn (eval gnus-summary-line-format-spec) (point))
2687      'gnus-number gnus-tmp-number)
2688     (when (gnus-visual-p 'summary-highlight 'highlight)
2689       (forward-line -1)
2690       (gnus-run-hooks 'gnus-summary-update-hook)
2691       (forward-line 1))))
2692
2693 (defun gnus-summary-update-line (&optional dont-update)
2694   "Update summary line after change."
2695   (when (and gnus-summary-default-score
2696              (not gnus-summary-inhibit-highlight))
2697     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
2698            (article (gnus-summary-article-number))
2699            (score (gnus-summary-article-score article)))
2700       (unless dont-update
2701         (if (and gnus-summary-mark-below
2702                  (< (gnus-summary-article-score)
2703                     gnus-summary-mark-below))
2704             ;; This article has a low score, so we mark it as read.
2705             (when (memq article gnus-newsgroup-unreads)
2706               (gnus-summary-mark-article-as-read gnus-low-score-mark))
2707           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
2708             ;; This article was previously marked as read on account
2709             ;; of a low score, but now it has risen, so we mark it as
2710             ;; unread.
2711             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
2712         (gnus-summary-update-mark
2713          (if (or (null gnus-summary-default-score)
2714                  (<= (abs (- score gnus-summary-default-score))
2715                      gnus-summary-zcore-fuzz))
2716              ?  ;Whitespace
2717            (if (< score gnus-summary-default-score)
2718                gnus-score-below-mark gnus-score-over-mark))
2719          'score))
2720       ;; Do visual highlighting.
2721       (when (gnus-visual-p 'summary-highlight 'highlight)
2722         (gnus-run-hooks 'gnus-summary-update-hook)))))
2723
2724 (defvar gnus-tmp-new-adopts nil)
2725
2726 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
2727   "Return the number of articles in THREAD.
2728 This may be 0 in some cases -- if none of the articles in
2729 the thread are to be displayed."
2730   (let* ((number
2731           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
2732           (cond
2733            ((not (listp thread))
2734             1)
2735            ((and (consp thread) (cdr thread))
2736             (apply
2737              '+ 1 (mapcar
2738                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
2739            ((null thread)
2740             1)
2741            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
2742             1)
2743            (t 0))))
2744     (when (and level (zerop level) gnus-tmp-new-adopts)
2745       (incf number
2746             (apply '+ (mapcar
2747                        'gnus-summary-number-of-articles-in-thread
2748                        gnus-tmp-new-adopts))))
2749     (if char
2750         (if (> number 1) gnus-not-empty-thread-mark
2751           gnus-empty-thread-mark)
2752       number)))
2753
2754 (defun gnus-summary-set-local-parameters (group)
2755   "Go through the local params of GROUP and set all variable specs in that list."
2756   (let ((params (gnus-group-find-parameter group))
2757         elem)
2758     (while params
2759       (setq elem (car params)
2760             params (cdr params))
2761       (and (consp elem)                 ; Has to be a cons.
2762            (consp (cdr elem))           ; The cdr has to be a list.
2763            (symbolp (car elem))         ; Has to be a symbol in there.
2764            (not (memq (car elem) '(quit-config))) ; Ignore quit-config.
2765            (ignore-errors               ; So we set it.
2766              (make-local-variable (car elem))
2767              (set (car elem) (eval (nth 1 elem))))))))
2768
2769 (defun gnus-summary-read-group (group &optional show-all no-article
2770                                       kill-buffer no-display backward
2771                                       select-articles)
2772   "Start reading news in newsgroup GROUP.
2773 If SHOW-ALL is non-nil, already read articles are also listed.
2774 If NO-ARTICLE is non-nil, no article is selected initially.
2775 If NO-DISPLAY, don't generate a summary buffer."
2776   (let (result)
2777     (while (and group
2778                 (null (setq result
2779                             (let ((gnus-auto-select-next nil))
2780                               (or (gnus-summary-read-group-1
2781                                    group show-all no-article
2782                                    kill-buffer no-display
2783                                    select-articles)
2784                                   (setq show-all nil
2785                                         select-articles nil)))))
2786                 (eq gnus-auto-select-next 'quietly))
2787       (set-buffer gnus-group-buffer)
2788       ;; The entry function called above goes to the next
2789       ;; group automatically, so we go two groups back
2790       ;; if we are searching for the previous group.
2791       (when backward
2792         (gnus-group-prev-unread-group 2))
2793       (if (not (equal group (gnus-group-group-name)))
2794           (setq group (gnus-group-group-name))
2795         (setq group nil)))
2796     result))
2797
2798 (defun gnus-summary-jump-to-other-group (group &optional show-all)
2799   "Directly jump to the other GROUP from summary buffer.
2800 If SHOW-ALL is non-nil, already read articles are also listed."
2801   (interactive
2802    (if (eq gnus-summary-buffer (current-buffer))
2803        (list (completing-read
2804               "Group: " gnus-active-hashtb nil t
2805               (when (and gnus-newsgroup-name
2806                          (string-match "[.:][^.:]+$" gnus-newsgroup-name))
2807                 (substring gnus-newsgroup-name 0 (1+ (match-beginning 0))))
2808               'gnus-group-history)
2809              current-prefix-arg)
2810      (error "%s must be invoked from a gnus summary buffer." this-command)))
2811   (unless (or (zerop (length group))
2812               (and gnus-newsgroup-name
2813                    (string-equal gnus-newsgroup-name group)))
2814     (gnus-summary-exit)
2815     (gnus-summary-read-group group show-all
2816                              gnus-dont-select-after-jump-to-other-group)))
2817
2818 (defun gnus-summary-read-group-1 (group show-all no-article
2819                                         kill-buffer no-display
2820                                         &optional select-articles)
2821   ;; Killed foreign groups can't be entered.
2822   (when (and (not (gnus-group-native-p group))
2823              (not (gnus-gethash group gnus-newsrc-hashtb)))
2824     (error "Dead non-native groups can't be entered"))
2825   (gnus-message 5 "Retrieving newsgroup: %s..." group)
2826   (let* ((new-group (gnus-summary-setup-buffer group))
2827          (quit-config (gnus-group-quit-config group))
2828          (did-select (and new-group (gnus-select-newsgroup
2829                                      group show-all select-articles))))
2830     (cond
2831      ;; This summary buffer exists already, so we just select it.
2832      ((not new-group)
2833       (gnus-set-global-variables)
2834       (when kill-buffer
2835         (gnus-kill-or-deaden-summary kill-buffer))
2836       (gnus-configure-windows 'summary 'force)
2837       (gnus-set-mode-line 'summary)
2838       (gnus-summary-position-point)
2839       (message "")
2840       t)
2841      ;; We couldn't select this group.
2842      ((null did-select)
2843       (when (and (eq major-mode 'gnus-summary-mode)
2844                  (not (equal (current-buffer) kill-buffer)))
2845         (kill-buffer (current-buffer))
2846         (if (not quit-config)
2847             (progn
2848               ;; Update the info -- marks might need to be removed,
2849               ;; for instance.
2850               (gnus-summary-update-info)
2851               (set-buffer gnus-group-buffer)
2852               (gnus-group-jump-to-group group)
2853               (gnus-group-next-unread-group 1))
2854           (gnus-handle-ephemeral-exit quit-config)))
2855       (gnus-message 3 "Can't select group")
2856       nil)
2857      ;; The user did a `C-g' while prompting for number of articles,
2858      ;; so we exit this group.
2859      ((eq did-select 'quit)
2860       (and (eq major-mode 'gnus-summary-mode)
2861            (not (equal (current-buffer) kill-buffer))
2862            (kill-buffer (current-buffer)))
2863       (when kill-buffer
2864         (gnus-kill-or-deaden-summary kill-buffer))
2865       (if (not quit-config)
2866           (progn
2867             (set-buffer gnus-group-buffer)
2868             (gnus-group-jump-to-group group)
2869             (gnus-group-next-unread-group 1)
2870             (gnus-configure-windows 'group 'force))
2871         (gnus-handle-ephemeral-exit quit-config))
2872       ;; Finally signal the quit.
2873       (signal 'quit nil))
2874      ;; The group was successfully selected.
2875      (t
2876       (gnus-set-global-variables)
2877       ;; Save the active value in effect when the group was entered.
2878       (setq gnus-newsgroup-active
2879             (gnus-copy-sequence
2880              (gnus-active gnus-newsgroup-name)))
2881       ;; You can change the summary buffer in some way with this hook.
2882       (gnus-run-hooks 'gnus-select-group-hook)
2883       ;; Set any local variables in the group parameters.
2884       (gnus-summary-set-local-parameters gnus-newsgroup-name)
2885       (gnus-update-format-specifications
2886        nil 'summary 'summary-mode 'summary-dummy)
2887       (gnus-update-summary-mark-positions)
2888       ;; Do score processing.
2889       (when gnus-use-scoring
2890         (gnus-possibly-score-headers))
2891       ;; Check whether to fill in the gaps in the threads.
2892       (when gnus-build-sparse-threads
2893         (gnus-build-sparse-threads))
2894       ;; Find the initial limit.
2895       (if gnus-show-threads
2896           (if show-all
2897               (let ((gnus-newsgroup-dormant nil))
2898                 (gnus-summary-initial-limit show-all))
2899             (gnus-summary-initial-limit show-all))
2900         ;; When untreaded, all articles are always shown.
2901         (setq gnus-newsgroup-limit
2902               (mapcar
2903                (lambda (header) (mail-header-number header))
2904                gnus-newsgroup-headers)))
2905       ;; Generate the summary buffer.
2906       (unless no-display
2907         (gnus-summary-prepare))
2908       (when gnus-use-trees
2909         (gnus-tree-open group)
2910         (setq gnus-summary-highlight-line-function
2911               'gnus-tree-highlight-article))
2912       ;; If the summary buffer is empty, but there are some low-scored
2913       ;; articles or some excluded dormants, we include these in the
2914       ;; buffer.
2915       (when (and (zerop (buffer-size))
2916                  (not no-display))
2917         (cond (gnus-newsgroup-dormant
2918                (gnus-summary-limit-include-dormant))
2919               ((and gnus-newsgroup-scored show-all)
2920                (gnus-summary-limit-include-expunged t))))
2921       ;; Function `gnus-apply-kill-file' must be called in this hook.
2922       (gnus-run-hooks 'gnus-apply-kill-hook)
2923       (if (and (zerop (buffer-size))
2924                (not no-display))
2925           (progn
2926             ;; This newsgroup is empty.
2927             (gnus-summary-catchup-and-exit nil t)
2928             (gnus-message 6 "No unread news")
2929             (when kill-buffer
2930               (gnus-kill-or-deaden-summary kill-buffer))
2931             ;; Return nil from this function.
2932             nil)
2933         ;; Hide conversation thread subtrees.  We cannot do this in
2934         ;; gnus-summary-prepare-hook since kill processing may not
2935         ;; work with hidden articles.
2936         (and gnus-show-threads
2937              gnus-thread-hide-subtree
2938              (gnus-summary-hide-all-threads))
2939         (when kill-buffer
2940           (gnus-kill-or-deaden-summary kill-buffer))
2941         ;; Show first unread article if requested.
2942         (if (and (not no-article)
2943                  (not no-display)
2944                  gnus-newsgroup-unreads
2945                  gnus-auto-select-first)
2946             (progn
2947               (gnus-configure-windows 'summary)
2948               (cond
2949                ((eq gnus-auto-select-first 'best)
2950                 (gnus-summary-best-unread-article))
2951                ((eq gnus-auto-select-first t)
2952                 (gnus-summary-first-unread-article))
2953                ((gnus-functionp gnus-auto-select-first)
2954                 (funcall gnus-auto-select-first))))
2955           ;; Don't select any articles, just move point to the first
2956           ;; article in the group.
2957           (goto-char (point-min))
2958           (gnus-summary-position-point)
2959           (gnus-configure-windows 'summary 'force)
2960           (gnus-set-mode-line 'summary))
2961         (when (get-buffer-window gnus-group-buffer t)
2962           ;; Gotta use windows, because recenter does weird stuff if
2963           ;; the current buffer ain't the displayed window.
2964           (let ((owin (selected-window)))
2965             (select-window (get-buffer-window gnus-group-buffer t))
2966             (when (gnus-group-goto-group group)
2967               (recenter))
2968             (select-window owin)))
2969         ;; Mark this buffer as "prepared".
2970         (setq gnus-newsgroup-prepared t)
2971         (gnus-run-hooks 'gnus-summary-prepared-hook)
2972         t)))))
2973
2974 (defun gnus-summary-prepare ()
2975   "Generate the summary buffer."
2976   (interactive)
2977   (let ((buffer-read-only nil))
2978     (erase-buffer)
2979     (setq gnus-newsgroup-data nil
2980           gnus-newsgroup-data-reverse nil)
2981     (gnus-run-hooks 'gnus-summary-generate-hook)
2982     ;; Generate the buffer, either with threads or without.
2983     (when gnus-newsgroup-headers
2984       (gnus-summary-prepare-threads
2985        (if gnus-show-threads
2986            (gnus-sort-gathered-threads
2987             (funcall gnus-summary-thread-gathering-function
2988                      (gnus-sort-threads
2989                       (gnus-cut-threads (gnus-make-threads)))))
2990          ;; Unthreaded display.
2991          (gnus-sort-articles gnus-newsgroup-headers))))
2992     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
2993     ;; Call hooks for modifying summary buffer.
2994     (goto-char (point-min))
2995     (gnus-run-hooks 'gnus-summary-prepare-hook)))
2996
2997 (defsubst gnus-general-simplify-subject (subject)
2998   "Simply subject by the same rules as gnus-gather-threads-by-subject."
2999   (setq subject
3000         (cond
3001          ;; Truncate the subject.
3002          (gnus-simplify-subject-functions
3003           (gnus-map-function gnus-simplify-subject-functions subject))
3004          ((numberp gnus-summary-gather-subject-limit)
3005           (setq subject (gnus-simplify-subject-re subject))
3006           (if (> (length subject) gnus-summary-gather-subject-limit)
3007               (substring subject 0 gnus-summary-gather-subject-limit)
3008             subject))
3009          ;; Fuzzily simplify it.
3010          ((eq 'fuzzy gnus-summary-gather-subject-limit)
3011           (gnus-simplify-subject-fuzzy subject))
3012          ;; Just remove the leading "Re:".
3013          (t
3014           (gnus-simplify-subject-re subject))))
3015
3016   (if (and gnus-summary-gather-exclude-subject
3017            (string-match gnus-summary-gather-exclude-subject subject))
3018       nil                               ; This article shouldn't be gathered
3019     subject))
3020
3021 (defun gnus-summary-simplify-subject-query ()
3022   "Query where the respool algorithm would put this article."
3023   (interactive)
3024   (gnus-summary-select-article)
3025   (message "%s"
3026            (gnus-general-simplify-subject (gnus-summary-article-subject))))
3027
3028 (defun gnus-gather-threads-by-subject (threads)
3029   "Gather threads by looking at Subject headers."
3030   (if (not gnus-summary-make-false-root)
3031       threads
3032     (let ((hashtb (gnus-make-hashtable 1024))
3033           (prev threads)
3034           (result threads)
3035           subject hthread whole-subject)
3036       (while threads
3037         (setq subject (gnus-general-simplify-subject
3038                        (setq whole-subject (mail-header-subject
3039                                             (caar threads)))))
3040         (when subject
3041           (if (setq hthread (gnus-gethash subject hashtb))
3042               (progn
3043                 ;; We enter a dummy root into the thread, if we
3044                 ;; haven't done that already.
3045                 (unless (stringp (caar hthread))
3046                   (setcar hthread (list whole-subject (car hthread))))
3047                 ;; We add this new gathered thread to this gathered
3048                 ;; thread.
3049                 (setcdr (car hthread)
3050                         (nconc (cdar hthread) (list (car threads))))
3051                 ;; Remove it from the list of threads.
3052                 (setcdr prev (cdr threads))
3053                 (setq threads prev))
3054             ;; Enter this thread into the hash table.
3055             (gnus-sethash subject threads hashtb)))
3056         (setq prev threads)
3057         (setq threads (cdr threads)))
3058       result)))
3059
3060 (defun gnus-gather-threads-by-references (threads)
3061   "Gather threads by looking at References headers."
3062   (let ((idhashtb (gnus-make-hashtable 1024))
3063         (thhashtb (gnus-make-hashtable 1024))
3064         (prev threads)
3065         (result threads)
3066         ids references id gthread gid entered ref)
3067     (while threads
3068       (when (setq references (mail-header-references (caar threads)))
3069         (setq id (mail-header-id (caar threads))
3070               ids (gnus-split-references references)
3071               entered nil)
3072         (while (setq ref (pop ids))
3073           (setq ids (delete ref ids))
3074           (if (not (setq gid (gnus-gethash ref idhashtb)))
3075               (progn
3076                 (gnus-sethash ref id idhashtb)
3077                 (gnus-sethash id threads thhashtb))
3078             (setq gthread (gnus-gethash gid thhashtb))
3079             (unless entered
3080               ;; We enter a dummy root into the thread, if we
3081               ;; haven't done that already.
3082               (unless (stringp (caar gthread))
3083                 (setcar gthread (list (mail-header-subject (caar gthread))
3084                                       (car gthread))))
3085               ;; We add this new gathered thread to this gathered
3086               ;; thread.
3087               (setcdr (car gthread)
3088                       (nconc (cdar gthread) (list (car threads)))))
3089             ;; Add it into the thread hash table.
3090             (gnus-sethash id gthread thhashtb)
3091             (setq entered t)
3092             ;; Remove it from the list of threads.
3093             (setcdr prev (cdr threads))
3094             (setq threads prev))))
3095       (setq prev threads)
3096       (setq threads (cdr threads)))
3097     result))
3098
3099 (defun gnus-sort-gathered-threads (threads)
3100   "Sort subtreads inside each gathered thread by `gnus-sort-gathered-threads-function'."
3101   (let ((result threads))
3102     (while threads
3103       (when (stringp (caar threads))
3104         (setcdr (car threads)
3105                 (sort (cdar threads) gnus-sort-gathered-threads-function)))
3106       (setq threads (cdr threads)))
3107     result))
3108
3109 (defun gnus-thread-loop-p (root thread)
3110   "Say whether ROOT is in THREAD."
3111   (let ((stack (list thread))
3112         (infloop 0)
3113         th)
3114     (while (setq thread (pop stack))
3115       (setq th (cdr thread))
3116       (while (and th
3117                   (not (eq (caar th) root)))
3118         (pop th))
3119       (if th
3120           ;; We have found a loop.
3121           (let (ref-dep)
3122             (setcdr thread (delq (car th) (cdr thread)))
3123             (if (boundp (setq ref-dep (intern "none"
3124                                               gnus-newsgroup-dependencies)))
3125                 (setcdr (symbol-value ref-dep)
3126                         (nconc (cdr (symbol-value ref-dep))
3127                                (list (car th))))
3128               (set ref-dep (list nil (car th))))
3129             (setq infloop 1
3130                   stack nil))
3131         ;; Push all the subthreads onto the stack.
3132         (push (cdr thread) stack)))
3133     infloop))
3134
3135 (defun gnus-make-threads ()
3136   "Go through the dependency hashtb and find the roots.  Return all threads."
3137   (let (threads)
3138     (while (catch 'infloop
3139              (mapatoms
3140               (lambda (refs)
3141                 ;; Deal with self-referencing References loops.
3142                 (when (and (car (symbol-value refs))
3143                            (not (zerop
3144                                  (apply
3145                                   '+
3146                                   (mapcar
3147                                    (lambda (thread)
3148                                      (gnus-thread-loop-p
3149                                       (car (symbol-value refs)) thread))
3150                                    (cdr (symbol-value refs)))))))
3151                   (setq threads nil)
3152                   (throw 'infloop t))
3153                 (unless (car (symbol-value refs))
3154                   ;; These threads do not refer back to any other articles,
3155                   ;; so they're roots.
3156                   (setq threads (append (cdr (symbol-value refs)) threads))))
3157               gnus-newsgroup-dependencies)))
3158     threads))
3159
3160 ;; Build the thread tree.
3161 (defsubst gnus-dependencies-add-header (header dependencies force-new)
3162   "Enter HEADER into the DEPENDENCIES table if it is not already there.
3163
3164 If FORCE-NEW is not nil, enter HEADER into the DEPENDENCIES table even
3165 if it was already present.
3166
3167 If `gnus-summary-ignore-duplicates' is nil then duplicate Message-IDs
3168 will not be entered in the DEPENDENCIES table.  Otherwise duplicate
3169 Message-IDs will be renamed be renamed to a unique Message-ID before
3170 being entered.
3171
3172 Returns HEADER if it was entered in the DEPENDENCIES.  Returns nil otherwise."
3173   (let* ((id (mail-header-id header))
3174          (id-dep (and id (intern id dependencies)))
3175          ref ref-dep ref-header)
3176     ;; Enter this `header' in the `dependencies' table.
3177     (cond
3178      ((not id-dep)
3179       (setq header nil))
3180      ;; The first two cases do the normal part: enter a new `header'
3181      ;; in the `dependencies' table.
3182      ((not (boundp id-dep))
3183       (set id-dep (list header)))
3184      ((null (car (symbol-value id-dep)))
3185       (setcar (symbol-value id-dep) header))
3186
3187      ;; From here the `header' was already present in the
3188      ;; `dependencies' table.
3189      (force-new
3190       ;; Overrides an existing entry;
3191       ;; just set the header part of the entry.
3192       (setcar (symbol-value id-dep) header))
3193
3194      ;; Renames the existing `header' to a unique Message-ID.
3195      ((not gnus-summary-ignore-duplicates)
3196       ;; An article with this Message-ID has already been seen.
3197       ;; We rename the Message-ID.
3198       (set (setq id-dep (intern (setq id (nnmail-message-id)) dependencies))
3199            (list header))
3200       (mail-header-set-id header id))
3201
3202      ;; The last case ignores an existing entry, except it adds any
3203      ;; additional Xrefs (in case the two articles came from different
3204      ;; servers.
3205      ;; Also sets `header' to `nil' meaning that the `dependencies'
3206      ;; table was *not* modified.
3207      (t
3208       (mail-header-set-xref
3209        (car (symbol-value id-dep))
3210        (concat (or (mail-header-xref (car (symbol-value id-dep)))
3211                    "")
3212                (or (mail-header-xref header) "")))
3213       (setq header nil)))
3214
3215     (when header
3216       ;; First check if that we are not creating a References loop.
3217       (setq ref (gnus-parent-id (mail-header-references header)))
3218       (while (and ref
3219                   (setq ref-dep (intern-soft ref dependencies))
3220                   (boundp ref-dep)
3221                   (setq ref-header (car (symbol-value ref-dep))))
3222         (if (string= id ref)
3223             ;; Yuk!  This is a reference loop.  Make the article be a
3224             ;; root article.
3225             (progn
3226               (mail-header-set-references (car (symbol-value id-dep)) "none")
3227               (setq ref nil))
3228           (setq ref (gnus-parent-id (mail-header-references ref-header)))))
3229       (setq ref (gnus-parent-id (mail-header-references header)))
3230       (setq ref-dep (intern (or ref "none") dependencies))
3231       (if (boundp ref-dep)
3232           (setcdr (symbol-value ref-dep)
3233                   (nconc (cdr (symbol-value ref-dep))
3234                          (list (symbol-value id-dep))))
3235         (set ref-dep (list nil (symbol-value id-dep)))))
3236     header))
3237
3238 (defun gnus-build-sparse-threads ()
3239   (let ((headers gnus-newsgroup-headers)
3240         (mail-parse-charset gnus-newsgroup-charset)
3241         (gnus-summary-ignore-duplicates t)
3242         header references generation relations
3243         subject child end new-child date)
3244     ;; First we create an alist of generations/relations, where
3245     ;; generations is how much we trust the relation, and the relation
3246     ;; is parent/child.
3247     (gnus-message 7 "Making sparse threads...")
3248     (save-excursion
3249       (nnheader-set-temp-buffer " *gnus sparse threads*")
3250       (while (setq header (pop headers))
3251         (when (and (setq references (mail-header-references header))
3252                    (not (string= references "")))
3253           (insert references)
3254           (setq child (mail-header-id header)
3255                 subject (mail-header-subject header)
3256                 date (mail-header-date header)
3257                 generation 0)
3258           (while (search-backward ">" nil t)
3259             (setq end (1+ (point)))
3260             (when (search-backward "<" nil t)
3261               (setq new-child (buffer-substring (point) end))
3262               (push (list (incf generation)
3263                           child (setq child new-child)
3264                           subject date)
3265                     relations)))
3266           (when child
3267             (push (list (1+ generation) child nil subject) relations))
3268           (erase-buffer)))
3269       (kill-buffer (current-buffer)))
3270     ;; Sort over trustworthiness.
3271     (mapcar
3272      (lambda (relation)
3273        (when (gnus-dependencies-add-header
3274               (make-full-mail-header
3275                gnus-reffed-article-number
3276                (nth 3 relation) "" (or (nth 4 relation) "")
3277                (nth 1 relation)
3278                (or (nth 2 relation) "") 0 0 "")
3279               gnus-newsgroup-dependencies nil)
3280          (push gnus-reffed-article-number gnus-newsgroup-limit)
3281          (push gnus-reffed-article-number gnus-newsgroup-sparse)
3282          (push (cons gnus-reffed-article-number gnus-sparse-mark)
3283                gnus-newsgroup-reads)
3284          (decf gnus-reffed-article-number)))
3285      (sort relations 'car-less-than-car))
3286     (gnus-message 7 "Making sparse threads...done")))
3287
3288 (defun gnus-build-old-threads ()
3289   ;; Look at all the articles that refer back to old articles, and
3290   ;; fetch the headers for the articles that aren't there.  This will
3291   ;; build complete threads - if the roots haven't been expired by the
3292   ;; server, that is.
3293   (let ((mail-parse-charset gnus-newsgroup-charset)
3294         id heads)
3295     (mapatoms
3296      (lambda (refs)
3297        (when (not (car (symbol-value refs)))
3298          (setq heads (cdr (symbol-value refs)))
3299          (while heads
3300            (if (memq (mail-header-number (caar heads))
3301                      gnus-newsgroup-dormant)
3302                (setq heads (cdr heads))
3303              (setq id (symbol-name refs))
3304              (while (and (setq id (gnus-build-get-header id))
3305                          (not (car (gnus-id-to-thread id)))))
3306              (setq heads nil)))))
3307      gnus-newsgroup-dependencies)))
3308
3309 ;; This function has to be called with point after the article number
3310 ;; on the beginning of the line.
3311 (defsubst gnus-nov-parse-line (number dependencies &optional force-new)
3312   (let ((eol (gnus-point-at-eol))
3313         (buffer (current-buffer))
3314         header)
3315
3316     ;; overview: [num subject from date id refs chars lines misc]
3317     (unless (eobp)
3318       (forward-char))
3319
3320     (setq header
3321           (make-full-mail-header
3322            number                               ; number
3323            (nnheader-nov-field)                 ; subject
3324            (nnheader-nov-field)                 ; from
3325            (nnheader-nov-field)                 ; date
3326            (nnheader-nov-read-message-id)       ; id
3327            (nnheader-nov-field)                 ; refs
3328            (nnheader-nov-read-integer)          ; chars
3329            (nnheader-nov-read-integer)          ; lines
3330            (unless (eobp)
3331              (nnheader-nov-field))              ; misc
3332            (nnheader-nov-parse-extra)))         ; extra
3333
3334     (when gnus-alter-header-function
3335       (funcall gnus-alter-header-function header))
3336     (gnus-dependencies-add-header header dependencies force-new)))
3337
3338 (defun gnus-build-get-header (id)
3339   "Look through the buffer of NOV lines and find the header to ID.
3340 Enter this line into the dependencies hash table, and return
3341 the id of the parent article (if any)."
3342   (let ((deps gnus-newsgroup-dependencies)
3343         found header)
3344     (prog1
3345         (save-excursion
3346           (set-buffer nntp-server-buffer)
3347           (let ((case-fold-search nil))
3348             (goto-char (point-min))
3349             (while (and (not found)
3350                         (search-forward id nil t))
3351               (beginning-of-line)
3352               (setq found (looking-at
3353                            (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
3354                                    (regexp-quote id))))
3355               (or found (beginning-of-line 2)))
3356             (when found
3357               (beginning-of-line)
3358               (and
3359                (setq header (gnus-nov-parse-line
3360                              (read (current-buffer)) deps))
3361                (gnus-parent-id (mail-header-references header))))))
3362       (when header
3363         (let ((number (mail-header-number header)))
3364           (push number gnus-newsgroup-limit)
3365           (push header gnus-newsgroup-headers)
3366           (if (memq number gnus-newsgroup-unselected)
3367               (progn
3368                 (push number gnus-newsgroup-unreads)
3369                 (setq gnus-newsgroup-unselected
3370                       (delq number gnus-newsgroup-unselected)))
3371             (push number gnus-newsgroup-ancient)))))))
3372
3373 (defun gnus-build-all-threads ()
3374   "Read all the headers."
3375   (let ((gnus-summary-ignore-duplicates t)
3376         (mail-parse-charset gnus-newsgroup-charset)
3377         (dependencies gnus-newsgroup-dependencies)
3378         header article)
3379     (save-excursion
3380       (set-buffer nntp-server-buffer)
3381       (let ((case-fold-search nil))
3382         (goto-char (point-min))
3383         (while (not (eobp))
3384           (ignore-errors
3385             (setq article (read (current-buffer))
3386                   header (gnus-nov-parse-line article dependencies)))
3387           (when header
3388             (save-excursion
3389               (set-buffer gnus-summary-buffer)
3390               (push header gnus-newsgroup-headers)
3391               (if (memq (setq article (mail-header-number header))
3392                         gnus-newsgroup-unselected)
3393                   (progn
3394                     (push article gnus-newsgroup-unreads)
3395                     (setq gnus-newsgroup-unselected
3396                           (delq article gnus-newsgroup-unselected)))
3397                 (push article gnus-newsgroup-ancient)))
3398             (forward-line 1)))))))
3399
3400 (defun gnus-summary-update-article-line (article header)
3401   "Update the line for ARTICLE using HEADERS."
3402   (let* ((id (mail-header-id header))
3403          (thread (gnus-id-to-thread id)))
3404     (unless thread
3405       (error "Article in no thread"))
3406     ;; Update the thread.
3407     (setcar thread header)
3408     (gnus-summary-goto-subject article)
3409     (let* ((datal (gnus-data-find-list article))
3410            (data (car datal))
3411            (length (when (cdr datal)
3412                      (- (gnus-data-pos data)
3413                         (gnus-data-pos (cadr datal)))))
3414            (buffer-read-only nil)
3415            (level (gnus-summary-thread-level)))
3416       (gnus-delete-line)
3417       (gnus-summary-insert-line
3418        header level nil (gnus-article-mark article)
3419        (memq article gnus-newsgroup-replied)
3420        (memq article gnus-newsgroup-expirable)
3421        ;; Only insert the Subject string when it's different
3422        ;; from the previous Subject string.
3423        (if (and
3424             gnus-show-threads
3425             (gnus-subject-equal
3426              (condition-case ()
3427                  (mail-header-subject
3428                   (gnus-data-header
3429                    (cadr
3430                     (gnus-data-find-list
3431                      article
3432                      (gnus-data-list t)))))
3433                ;; Error on the side of excessive subjects.
3434                (error ""))
3435              (mail-header-subject header)))
3436            ""
3437          (mail-header-subject header))
3438        nil (cdr (assq article gnus-newsgroup-scored))
3439        (memq article gnus-newsgroup-processable))
3440       (when length
3441         (gnus-data-update-list
3442          (cdr datal) (- length (- (gnus-data-pos data) (point))))))))
3443
3444 (defun gnus-summary-update-article (article &optional iheader)
3445   "Update ARTICLE in the summary buffer."
3446   (set-buffer gnus-summary-buffer)
3447   (let* ((header (gnus-summary-article-header article))
3448          (id (mail-header-id header))
3449          (data (gnus-data-find article))
3450          (thread (gnus-id-to-thread id))
3451          (references (mail-header-references header))
3452          (parent
3453           (gnus-id-to-thread
3454            (or (gnus-parent-id
3455                 (when (and references
3456                            (not (equal "" references)))
3457                   references))
3458                "none")))
3459          (buffer-read-only nil)
3460          (old (car thread)))
3461     (when thread
3462       (unless iheader
3463         (setcar thread nil)
3464         (when parent
3465           (delq thread parent)))
3466       (if (gnus-summary-insert-subject id header)
3467           ;; Set the (possibly) new article number in the data structure.
3468           (gnus-data-set-number data (gnus-id-to-article id))
3469         (setcar thread old)
3470         nil))))
3471
3472 (defun gnus-rebuild-thread (id &optional line)
3473   "Rebuild the thread containing ID.
3474 If LINE, insert the rebuilt thread starting on line LINE."
3475   (let ((buffer-read-only nil)
3476         old-pos current thread data)
3477     (if (not gnus-show-threads)
3478         (setq thread (list (car (gnus-id-to-thread id))))
3479       ;; Get the thread this article is part of.
3480       (setq thread (gnus-remove-thread id)))
3481     (setq old-pos (gnus-point-at-bol))
3482     (setq current (save-excursion
3483                     (and (zerop (forward-line -1))
3484                          (gnus-summary-article-number))))
3485     ;; If this is a gathered thread, we have to go some re-gathering.
3486     (when (stringp (car thread))
3487       (let ((subject (car thread))
3488             roots thr)
3489         (setq thread (cdr thread))
3490         (while thread
3491           (unless (memq (setq thr (gnus-id-to-thread
3492                                    (gnus-root-id
3493                                     (mail-header-id (caar thread)))))
3494                         roots)
3495             (push thr roots))
3496           (setq thread (cdr thread)))
3497         ;; We now have all (unique) roots.
3498         (if (= (length roots) 1)
3499             ;; All the loose roots are now one solid root.
3500             (setq thread (car roots))
3501           (setq thread (cons subject (gnus-sort-threads roots))))))
3502     (let (threads)
3503       ;; We then insert this thread into the summary buffer.
3504       (when line
3505         (goto-char (point-min))
3506         (forward-line (1- line)))
3507       (let (gnus-newsgroup-data gnus-newsgroup-threads)
3508         (if gnus-show-threads
3509             (gnus-summary-prepare-threads (gnus-cut-threads (list thread)))
3510           (gnus-summary-prepare-unthreaded thread))
3511         (setq data (nreverse gnus-newsgroup-data))
3512         (setq threads gnus-newsgroup-threads))
3513       ;; We splice the new data into the data structure.
3514       ;;!!! This is kinda bogus.  We assume that in LINE is non-nil,
3515       ;;!!! then we want to insert at the beginning of the buffer.
3516       ;;!!! That happens to be true with Gnus now, but that may
3517       ;;!!! change in the future.  Perhaps.
3518       (gnus-data-enter-list
3519        (if line nil current) data (- (point) old-pos))
3520       (setq gnus-newsgroup-threads
3521             (nconc threads gnus-newsgroup-threads))
3522       (gnus-data-compute-positions))))
3523
3524 (defun gnus-number-to-header (number)
3525   "Return the header for article NUMBER."
3526   (let ((headers gnus-newsgroup-headers))
3527     (while (and headers
3528                 (not (= number (mail-header-number (car headers)))))
3529       (pop headers))
3530     (when headers
3531       (car headers))))
3532
3533 (defun gnus-parent-headers (in-headers &optional generation)
3534   "Return the headers of the GENERATIONeth parent of HEADERS."
3535   (unless generation
3536     (setq generation 1))
3537   (let ((parent t)
3538         (headers in-headers)
3539         references)
3540     (while (and parent
3541                 (not (zerop generation))
3542                 (setq references (mail-header-references headers)))
3543       (setq headers (if (and references
3544                              (setq parent (gnus-parent-id references)))
3545                         (car (gnus-id-to-thread parent))
3546                       nil))
3547       (decf generation))
3548     (and (not (eq headers in-headers))
3549          headers)))
3550
3551 (defun gnus-id-to-thread (id)
3552   "Return the (sub-)thread where ID appears."
3553   (gnus-gethash id gnus-newsgroup-dependencies))
3554
3555 (defun gnus-id-to-article (id)
3556   "Return the article number of ID."
3557   (let ((thread (gnus-id-to-thread id)))
3558     (when (and thread
3559                (car thread))
3560       (mail-header-number (car thread)))))
3561
3562 (defun gnus-id-to-header (id)
3563   "Return the article headers of ID."
3564   (car (gnus-id-to-thread id)))
3565
3566 (defun gnus-article-displayed-root-p (article)
3567   "Say whether ARTICLE is a root(ish) article."
3568   (let ((level (gnus-summary-thread-level article))
3569         (refs (mail-header-references  (gnus-summary-article-header article)))
3570         particle)
3571     (cond
3572      ((null level) nil)
3573      ((zerop level) t)
3574      ((null refs) t)
3575      ((null (gnus-parent-id refs)) t)
3576      ((and (= 1 level)
3577            (null (setq particle (gnus-id-to-article
3578                                  (gnus-parent-id refs))))
3579            (null (gnus-summary-thread-level particle)))))))
3580
3581 (defun gnus-root-id (id)
3582   "Return the id of the root of the thread where ID appears."
3583   (let (last-id prev)
3584     (while (and id (setq prev (car (gnus-id-to-thread id))))
3585       (setq last-id id
3586             id (gnus-parent-id (mail-header-references prev))))
3587     last-id))
3588
3589 (defun gnus-articles-in-thread (thread)
3590   "Return the list of articles in THREAD."
3591   (cons (mail-header-number (car thread))
3592         (apply 'nconc (mapcar 'gnus-articles-in-thread (cdr thread)))))
3593
3594 (defun gnus-remove-thread (id &optional dont-remove)
3595   "Remove the thread that has ID in it."
3596   (let (headers thread last-id)
3597     ;; First go up in this thread until we find the root.
3598     (setq last-id (gnus-root-id id)
3599           headers (message-flatten-list (gnus-id-to-thread last-id)))
3600     ;; We have now found the real root of this thread.  It might have
3601     ;; been gathered into some loose thread, so we have to search
3602     ;; through the threads to find the thread we wanted.
3603     (let ((threads gnus-newsgroup-threads)
3604           sub)
3605       (while threads
3606         (setq sub (car threads))
3607         (if (stringp (car sub))
3608             ;; This is a gathered thread, so we look at the roots
3609             ;; below it to find whether this article is in this
3610             ;; gathered root.
3611             (progn
3612               (setq sub (cdr sub))
3613               (while sub
3614                 (when (member (caar sub) headers)
3615                   (setq thread (car threads)
3616                         threads nil
3617                         sub nil))
3618                 (setq sub (cdr sub))))
3619           ;; It's an ordinary thread, so we check it.
3620           (when (eq (car sub) (car headers))
3621             (setq thread sub
3622                   threads nil)))
3623         (setq threads (cdr threads)))
3624       ;; If this article is in no thread, then it's a root.
3625       (if thread
3626           (unless dont-remove
3627             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
3628         (setq thread (gnus-id-to-thread last-id)))
3629       (when thread
3630         (prog1
3631             thread                      ; We return this thread.
3632           (unless dont-remove
3633             (if (stringp (car thread))
3634                 (progn
3635                   ;; If we use dummy roots, then we have to remove the
3636                   ;; dummy root as well.
3637                   (when (eq gnus-summary-make-false-root 'dummy)
3638                     ;; We go to the dummy root by going to
3639                     ;; the first sub-"thread", and then one line up.
3640                     (gnus-summary-goto-article
3641                      (mail-header-number (caadr thread)))
3642                     (forward-line -1)
3643                     (gnus-delete-line)
3644                     (gnus-data-compute-positions))
3645                   (setq thread (cdr thread))
3646                   (while thread
3647                     (gnus-remove-thread-1 (car thread))
3648                     (setq thread (cdr thread))))
3649               (gnus-remove-thread-1 thread))))))))
3650
3651 (defun gnus-remove-thread-1 (thread)
3652   "Remove the thread THREAD recursively."
3653   (let ((number (mail-header-number (pop thread)))
3654         d)
3655     (setq thread (reverse thread))
3656     (while thread
3657       (gnus-remove-thread-1 (pop thread)))
3658     (when (setq d (gnus-data-find number))
3659       (goto-char (gnus-data-pos d))
3660       (gnus-summary-show-thread)
3661       (gnus-data-remove
3662        number
3663        (- (gnus-point-at-bol)
3664           (prog1
3665               (1+ (gnus-point-at-eol))
3666             (gnus-delete-line)))))))
3667
3668 (defun gnus-sort-threads-1 (threads func)
3669   (sort (mapcar (lambda (thread)
3670                   (cons (car thread)
3671                         (and (cdr thread)
3672                              (gnus-sort-threads-1 (cdr thread) func))))
3673                 threads) func))
3674
3675 (defun gnus-sort-threads (threads)
3676   "Sort THREADS."
3677   (if (not gnus-thread-sort-functions)
3678       threads
3679     (gnus-message 8 "Sorting threads...")
3680     (prog1
3681         (gnus-sort-threads-1 
3682          threads 
3683          (gnus-make-sort-function gnus-thread-sort-functions))
3684       (gnus-message 8 "Sorting threads...done"))))
3685
3686 (defun gnus-sort-articles (articles)
3687   "Sort ARTICLES."
3688   (when gnus-article-sort-functions
3689     (gnus-message 7 "Sorting articles...")
3690     (prog1
3691         (setq gnus-newsgroup-headers
3692               (sort articles (gnus-make-sort-function
3693                               gnus-article-sort-functions)))
3694       (gnus-message 7 "Sorting articles...done"))))
3695
3696 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
3697 (defmacro gnus-thread-header (thread)
3698   "Return header of first article in THREAD.
3699 Note that THREAD must never, ever be anything else than a variable -
3700 using some other form will lead to serious barfage."
3701   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
3702   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
3703   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207"
3704         (vector thread) 2))
3705
3706 (defsubst gnus-article-sort-by-number (h1 h2)
3707   "Sort articles by article number."
3708   (< (mail-header-number h1)
3709      (mail-header-number h2)))
3710
3711 (defun gnus-thread-sort-by-number (h1 h2)
3712   "Sort threads by root article number."
3713   (gnus-article-sort-by-number
3714    (gnus-thread-header h1) (gnus-thread-header h2)))
3715
3716 (defsubst gnus-article-sort-by-lines (h1 h2)
3717   "Sort articles by article Lines header."
3718   (< (mail-header-lines h1)
3719      (mail-header-lines h2)))
3720
3721 (defun gnus-thread-sort-by-lines (h1 h2)
3722   "Sort threads by root article Lines header."
3723   (gnus-article-sort-by-lines
3724    (gnus-thread-header h1) (gnus-thread-header h2)))
3725
3726 (defsubst gnus-article-sort-by-chars (h1 h2)
3727   "Sort articles by octet length."
3728   (< (mail-header-chars h1)
3729      (mail-header-chars h2)))
3730
3731 (defun gnus-thread-sort-by-chars (h1 h2)
3732   "Sort threads by root article octet length."
3733   (gnus-article-sort-by-chars
3734    (gnus-thread-header h1) (gnus-thread-header h2)))
3735
3736 (defsubst gnus-article-sort-by-author (h1 h2)
3737   "Sort articles by root author."
3738   (string-lessp
3739    (let ((addr (car (mime-entity-read-field h1 'From))))
3740      (or (std11-full-name-string addr)
3741          (std11-address-string addr)
3742          ""))
3743    (let ((addr (car (mime-entity-read-field h2 'From))))
3744      (or (std11-full-name-string addr)
3745          (std11-address-string addr)
3746          ""))
3747    ))
3748
3749 (defun gnus-thread-sort-by-author (h1 h2)
3750   "Sort threads by root author."
3751   (gnus-article-sort-by-author
3752    (gnus-thread-header h1)  (gnus-thread-header h2)))
3753
3754 (defsubst gnus-article-sort-by-subject (h1 h2)
3755   "Sort articles by root subject."
3756   (string-lessp
3757    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
3758    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
3759
3760 (defun gnus-thread-sort-by-subject (h1 h2)
3761   "Sort threads by root subject."
3762   (gnus-article-sort-by-subject
3763    (gnus-thread-header h1) (gnus-thread-header h2)))
3764
3765 (defsubst gnus-article-sort-by-date (h1 h2)
3766   "Sort articles by root article date."
3767   (time-less-p
3768    (gnus-date-get-time (mail-header-date h1))
3769    (gnus-date-get-time (mail-header-date h2))))
3770
3771 (defun gnus-thread-sort-by-date (h1 h2)
3772   "Sort threads by root article date."
3773   (gnus-article-sort-by-date
3774    (gnus-thread-header h1) (gnus-thread-header h2)))
3775
3776 (defsubst gnus-article-sort-by-score (h1 h2)
3777   "Sort articles by root article score.
3778 Unscored articles will be counted as having a score of zero."
3779   (> (or (cdr (assq (mail-header-number h1)
3780                     gnus-newsgroup-scored))
3781          gnus-summary-default-score 0)
3782      (or (cdr (assq (mail-header-number h2)
3783                     gnus-newsgroup-scored))
3784          gnus-summary-default-score 0)))
3785
3786 (defun gnus-thread-sort-by-score (h1 h2)
3787   "Sort threads by root article score."
3788   (gnus-article-sort-by-score
3789    (gnus-thread-header h1) (gnus-thread-header h2)))
3790
3791 (defun gnus-thread-sort-by-total-score (h1 h2)
3792   "Sort threads by the sum of all scores in the thread.
3793 Unscored articles will be counted as having a score of zero."
3794   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
3795
3796 (defun gnus-thread-total-score (thread)
3797   ;; This function find the total score of THREAD.
3798   (cond ((null thread)
3799          0)
3800         ((consp thread)
3801          (if (stringp (car thread))
3802              (apply gnus-thread-score-function 0
3803                     (mapcar 'gnus-thread-total-score-1 (cdr thread)))
3804            (gnus-thread-total-score-1 thread)))
3805         (t
3806          (gnus-thread-total-score-1 (list thread)))))
3807
3808 (defun gnus-thread-total-score-1 (root)
3809   ;; This function find the total score of the thread below ROOT.
3810   (setq root (car root))
3811   (apply gnus-thread-score-function
3812          (or (append
3813               (mapcar 'gnus-thread-total-score
3814                       (cdr (gnus-id-to-thread (mail-header-id root))))
3815               (when (> (mail-header-number root) 0)
3816                 (list (or (cdr (assq (mail-header-number root)
3817                                      gnus-newsgroup-scored))
3818                           gnus-summary-default-score 0))))
3819              (list gnus-summary-default-score)
3820              '(0))))
3821
3822 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
3823 (defvar gnus-tmp-prev-subject nil)
3824 (defvar gnus-tmp-false-parent nil)
3825 (defvar gnus-tmp-root-expunged nil)
3826 (defvar gnus-tmp-dummy-line nil)
3827
3828 (defvar gnus-tmp-header)
3829 (defun gnus-extra-header (type &optional header)
3830   "Return the extra header of TYPE."
3831   (or (cdr (assq type (mail-header-extra (or header gnus-tmp-header))))
3832       ""))
3833
3834 (defun gnus-summary-prepare-threads (threads)
3835   "Prepare summary buffer from THREADS and indentation LEVEL.
3836 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
3837 or a straight list of headers."
3838   (gnus-message 7 "Generating summary...")
3839
3840   (setq gnus-newsgroup-threads threads)
3841   (beginning-of-line)
3842
3843   (let ((gnus-tmp-level 0)
3844         (default-score (or gnus-summary-default-score 0))
3845         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
3846         thread number subject stack state gnus-tmp-gathered beg-match
3847         new-roots gnus-tmp-new-adopts thread-end
3848         gnus-tmp-header gnus-tmp-unread
3849         gnus-tmp-replied gnus-tmp-subject-or-nil
3850         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
3851         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
3852         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
3853
3854     (setq gnus-tmp-prev-subject nil)
3855
3856     (if (vectorp (car threads))
3857         ;; If this is a straight (sic) list of headers, then a
3858         ;; threaded summary display isn't required, so we just create
3859         ;; an unthreaded one.
3860         (gnus-summary-prepare-unthreaded threads)
3861
3862       ;; Do the threaded display.
3863
3864       (while (or threads stack gnus-tmp-new-adopts new-roots)
3865
3866         (if (and (= gnus-tmp-level 0)
3867                  (or (not stack)
3868                      (= (caar stack) 0))
3869                  (not gnus-tmp-false-parent)
3870                  (or gnus-tmp-new-adopts new-roots))
3871             (if gnus-tmp-new-adopts
3872                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
3873                       thread (list (car gnus-tmp-new-adopts))
3874                       gnus-tmp-header (caar thread)
3875                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
3876               (when new-roots
3877                 (setq thread (list (car new-roots))
3878                       gnus-tmp-header (caar thread)
3879                       new-roots (cdr new-roots))))
3880
3881           (if threads
3882               ;; If there are some threads, we do them before the
3883               ;; threads on the stack.
3884               (setq thread threads
3885                     gnus-tmp-header (caar thread))
3886             ;; There were no current threads, so we pop something off
3887             ;; the stack.
3888             (setq state (car stack)
3889                   gnus-tmp-level (car state)
3890                   thread (cdr state)
3891                   stack (cdr stack)
3892                   gnus-tmp-header (caar thread))))
3893
3894         (setq gnus-tmp-false-parent nil)
3895         (setq gnus-tmp-root-expunged nil)
3896         (setq thread-end nil)
3897
3898         (if (stringp gnus-tmp-header)
3899             ;; The header is a dummy root.
3900             (cond
3901              ((eq gnus-summary-make-false-root 'adopt)
3902               ;; We let the first article adopt the rest.
3903               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
3904                                                (cddar thread)))
3905               (setq gnus-tmp-gathered
3906                     (nconc (mapcar
3907                             (lambda (h) (mail-header-number (car h)))
3908                             (cddar thread))
3909                            gnus-tmp-gathered))
3910               (setq thread (cons (list (caar thread)
3911                                        (cadar thread))
3912                                  (cdr thread)))
3913               (setq gnus-tmp-level -1
3914                     gnus-tmp-false-parent t))
3915              ((eq gnus-summary-make-false-root 'empty)
3916               ;; We print adopted articles with empty subject fields.
3917               (setq gnus-tmp-gathered
3918                     (nconc (mapcar
3919                             (lambda (h) (mail-header-number (car h)))
3920                             (cddar thread))
3921                            gnus-tmp-gathered))
3922               (setq gnus-tmp-level -1))
3923              ((eq gnus-summary-make-false-root 'dummy)
3924               ;; We remember that we probably want to output a dummy
3925               ;; root.
3926               (setq gnus-tmp-dummy-line gnus-tmp-header)
3927               (setq gnus-tmp-prev-subject gnus-tmp-header))
3928              (t
3929               ;; We do not make a root for the gathered
3930               ;; sub-threads at all.
3931               (setq gnus-tmp-level -1)))
3932
3933           (setq number (mail-header-number gnus-tmp-header)
3934                 subject (mail-header-subject gnus-tmp-header))
3935
3936           (cond
3937            ;; If the thread has changed subject, we might want to make
3938            ;; this subthread into a root.
3939            ((and (null gnus-thread-ignore-subject)
3940                  (not (zerop gnus-tmp-level))
3941                  gnus-tmp-prev-subject
3942                  (not (inline
3943                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
3944             (setq new-roots (nconc new-roots (list (car thread)))
3945                   thread-end t
3946                   gnus-tmp-header nil))
3947            ;; If the article lies outside the current limit,
3948            ;; then we do not display it.
3949            ((not (memq number gnus-newsgroup-limit))
3950             (setq gnus-tmp-gathered
3951                   (nconc (mapcar
3952                           (lambda (h) (mail-header-number (car h)))
3953                           (cdar thread))
3954                          gnus-tmp-gathered))
3955             (setq gnus-tmp-new-adopts (if (cdar thread)
3956                                           (append gnus-tmp-new-adopts
3957                                                   (cdar thread))
3958                                         gnus-tmp-new-adopts)
3959                   thread-end t
3960                   gnus-tmp-header nil)
3961             (when (zerop gnus-tmp-level)
3962               (setq gnus-tmp-root-expunged t)))
3963            ;; Perhaps this article is to be marked as read?
3964            ((and gnus-summary-mark-below
3965                  (< (or (cdr (assq number gnus-newsgroup-scored))
3966                         default-score)
3967                     gnus-summary-mark-below)
3968                  ;; Don't touch sparse articles.
3969                  (not (gnus-summary-article-sparse-p number))
3970                  (not (gnus-summary-article-ancient-p number)))
3971             (setq gnus-newsgroup-unreads
3972                   (delq number gnus-newsgroup-unreads))
3973             (if gnus-newsgroup-auto-expire
3974                 (push number gnus-newsgroup-expirable)
3975               (push (cons number gnus-low-score-mark)
3976                     gnus-newsgroup-reads))))
3977
3978           (when gnus-tmp-header
3979             ;; We may have an old dummy line to output before this
3980             ;; article.
3981             (when (and gnus-tmp-dummy-line
3982                        (gnus-subject-equal
3983                         gnus-tmp-dummy-line
3984                         (mail-header-subject gnus-tmp-header)))
3985               (gnus-summary-insert-dummy-line
3986                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
3987               (setq gnus-tmp-dummy-line nil))
3988
3989             ;; Compute the mark.
3990             (setq gnus-tmp-unread (gnus-article-mark number))
3991
3992             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
3993                                   gnus-tmp-header gnus-tmp-level)
3994                   gnus-newsgroup-data)
3995
3996             ;; Actually insert the line.
3997             (setq
3998              gnus-tmp-subject-or-nil
3999              (cond
4000               ((and gnus-thread-ignore-subject
4001                     gnus-tmp-prev-subject
4002                     (not (inline (gnus-subject-equal
4003                                   gnus-tmp-prev-subject subject))))
4004                subject)
4005               ((zerop gnus-tmp-level)
4006                (if (and (eq gnus-summary-make-false-root 'empty)
4007                         (memq number gnus-tmp-gathered)
4008                         gnus-tmp-prev-subject
4009                         (inline (gnus-subject-equal
4010                                  gnus-tmp-prev-subject subject)))
4011                    gnus-summary-same-subject
4012                  subject))
4013               (t gnus-summary-same-subject)))
4014             (if (and (eq gnus-summary-make-false-root 'adopt)
4015                      (= gnus-tmp-level 1)
4016                      (memq number gnus-tmp-gathered))
4017                 (setq gnus-tmp-opening-bracket ?\<
4018                       gnus-tmp-closing-bracket ?\>)
4019               (setq gnus-tmp-opening-bracket ?\[
4020                     gnus-tmp-closing-bracket ?\]))
4021             (setq
4022              gnus-tmp-indentation
4023              (aref gnus-thread-indent-array gnus-tmp-level)
4024              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
4025              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
4026                                 gnus-summary-default-score 0)
4027              gnus-tmp-score-char
4028              (if (or (null gnus-summary-default-score)
4029                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
4030                          gnus-summary-zcore-fuzz))
4031                  ?  ;Whitespace
4032                (if (< gnus-tmp-score gnus-summary-default-score)
4033                    gnus-score-below-mark gnus-score-over-mark))
4034              gnus-tmp-replied
4035              (cond ((memq number gnus-newsgroup-processable)
4036                     gnus-process-mark)
4037                    ((memq number gnus-newsgroup-cached)
4038                     gnus-cached-mark)
4039                    ((memq number gnus-newsgroup-replied)
4040                     gnus-replied-mark)
4041                    ((memq number gnus-newsgroup-saved)
4042                     gnus-saved-mark)
4043                    (t gnus-unread-mark))
4044              gnus-tmp-from (mail-header-from gnus-tmp-header)
4045              gnus-tmp-name
4046              (cond
4047               ((string-match "<[^>]+> *$" gnus-tmp-from)
4048                (setq beg-match (match-beginning 0))
4049                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
4050                         (substring gnus-tmp-from (1+ (match-beginning 0))
4051                                    (1- (match-end 0))))
4052                    (substring gnus-tmp-from 0 beg-match)))
4053               ((string-match "(.+)" gnus-tmp-from)
4054                (substring gnus-tmp-from
4055                           (1+ (match-beginning 0)) (1- (match-end 0))))
4056               (t gnus-tmp-from)))
4057             (when (string= gnus-tmp-name "")
4058               (setq gnus-tmp-name gnus-tmp-from))
4059             (unless (numberp gnus-tmp-lines)
4060               (setq gnus-tmp-lines 0))
4061             (gnus-put-text-property
4062              (point)
4063              (progn (eval gnus-summary-line-format-spec) (point))
4064              'gnus-number number)
4065             (when gnus-visual-p
4066               (forward-line -1)
4067               (gnus-run-hooks 'gnus-summary-update-hook)
4068               (forward-line 1))
4069
4070             (setq gnus-tmp-prev-subject subject)))
4071
4072         (when (nth 1 thread)
4073           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
4074         (incf gnus-tmp-level)
4075         (setq threads (if thread-end nil (cdar thread)))
4076         (unless threads
4077           (setq gnus-tmp-level 0)))))
4078   (gnus-message 7 "Generating summary...done"))
4079
4080 (defun gnus-summary-prepare-unthreaded (headers)
4081   "Generate an unthreaded summary buffer based on HEADERS."
4082   (let (header number mark)
4083
4084     (beginning-of-line)
4085
4086     (while headers
4087       ;; We may have to root out some bad articles...
4088       (when (memq (setq number (mail-header-number
4089                                 (setq header (pop headers))))
4090                   gnus-newsgroup-limit)
4091         ;; Mark article as read when it has a low score.
4092         (when (and gnus-summary-mark-below
4093                    (< (or (cdr (assq number gnus-newsgroup-scored))
4094                           gnus-summary-default-score 0)
4095                       gnus-summary-mark-below)
4096                    (not (gnus-summary-article-ancient-p number)))
4097           (setq gnus-newsgroup-unreads
4098                 (delq number gnus-newsgroup-unreads))
4099           (if gnus-newsgroup-auto-expire
4100               (push number gnus-newsgroup-expirable)
4101             (push (cons number gnus-low-score-mark)
4102                   gnus-newsgroup-reads)))
4103
4104         (setq mark (gnus-article-mark number))
4105         (push (gnus-data-make number mark (1+ (point)) header 0)
4106               gnus-newsgroup-data)
4107         (gnus-summary-insert-line
4108          header 0 number
4109          mark (memq number gnus-newsgroup-replied)
4110          (memq number gnus-newsgroup-expirable)
4111          (mail-header-subject header) nil
4112          (cdr (assq number gnus-newsgroup-scored))
4113          (memq number gnus-newsgroup-processable))))))
4114
4115 (defun gnus-summary-remove-list-identifiers ()
4116   "Remove list identifiers in `gnus-list-identifiers' from articles in the current group."
4117   (let ((regexp (if (stringp gnus-list-identifiers)
4118                     gnus-list-identifiers
4119                   (mapconcat 'identity gnus-list-identifiers " *\\|"))))
4120     (dolist (header gnus-newsgroup-headers)
4121       (when (string-match (concat "\\(\\(\\(Re: +\\)?\\(" regexp
4122                                   " *\\)\\)+\\(Re: +\\)?\\)")
4123                           (mail-header-subject header))
4124         (mail-header-set-subject
4125          header (concat (substring (mail-header-subject header)
4126                                    0 (match-beginning 1))
4127                         (or
4128                          (match-string 3 (mail-header-subject header))
4129                          (match-string 5 (mail-header-subject header)))
4130                         (substring (mail-header-subject header)
4131                                    (match-end 1))))))))
4132
4133 (defun gnus-select-newsgroup (group &optional read-all select-articles)
4134   "Select newsgroup GROUP.
4135 If READ-ALL is non-nil, all articles in the group are selected.
4136 If SELECT-ARTICLES, only select those articles from GROUP."
4137   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4138          ;;!!! Dirty hack; should be removed.
4139          (gnus-summary-ignore-duplicates
4140           (if (eq (car (gnus-find-method-for-group group)) 'nnvirtual)
4141               t
4142             gnus-summary-ignore-duplicates))
4143          (info (nth 2 entry))
4144          articles fetched-articles cached)
4145
4146     (unless (gnus-check-server
4147              (setq gnus-current-select-method
4148                    (gnus-find-method-for-group group)))
4149       (error "Couldn't open server"))
4150
4151     (or (and entry (not (eq (car entry) t))) ; Either it's active...
4152         (gnus-activate-group group)     ; Or we can activate it...
4153         (progn                          ; Or we bug out.
4154           (when (equal major-mode 'gnus-summary-mode)
4155             (kill-buffer (current-buffer)))
4156           (error "Couldn't request group %s: %s"
4157                  group (gnus-status-message group))))
4158
4159     (unless (gnus-request-group group t)
4160       (when (equal major-mode 'gnus-summary-mode)
4161         (kill-buffer (current-buffer)))
4162       (error "Couldn't request group %s: %s"
4163              group (gnus-status-message group)))
4164
4165     (setq gnus-newsgroup-name group)
4166     (setq gnus-newsgroup-unselected nil)
4167     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
4168     (gnus-summary-setup-default-charset)
4169
4170     ;; Adjust and set lists of article marks.
4171     (when info
4172       (gnus-adjust-marked-articles info))
4173
4174     ;; Kludge to avoid having cached articles nixed out in virtual groups.
4175     (setq cached
4176           (if (gnus-virtual-group-p group)
4177               gnus-newsgroup-cached
4178             (gnus-cache-articles-in-group group)))
4179
4180     (setq gnus-newsgroup-unreads
4181           (gnus-set-difference
4182            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
4183            gnus-newsgroup-dormant))
4184
4185     (setq gnus-newsgroup-processable nil)
4186
4187     (gnus-update-read-articles group gnus-newsgroup-unreads)
4188
4189     (if (setq articles select-articles)
4190         (setq gnus-newsgroup-unselected
4191               (gnus-sorted-intersection
4192                gnus-newsgroup-unreads
4193                (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4194       (setq articles (gnus-articles-to-read group read-all)))
4195
4196     (cond
4197      ((null articles)
4198       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
4199       'quit)
4200      ((eq articles 0) nil)
4201      (t
4202       ;; Init the dependencies hash table.
4203       (setq gnus-newsgroup-dependencies
4204             (gnus-make-hashtable (length articles)))
4205       (gnus-set-global-variables)
4206       ;; Retrieve the headers and read them in.
4207       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
4208       (setq gnus-newsgroup-headers
4209             (gnus-retrieve-parsed-headers
4210              articles gnus-newsgroup-name
4211              ;; We might want to fetch old headers, but
4212              ;; not if there is only 1 article.
4213              (and (or (and (not (eq gnus-fetch-old-headers 'some))
4214                            (not (numberp gnus-fetch-old-headers)))
4215                       (> (length articles) 1))
4216                   gnus-fetch-old-headers)))
4217       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name)
4218
4219       ;; Suppress duplicates?
4220       (when gnus-suppress-duplicates
4221         (gnus-dup-suppress-articles))
4222
4223       ;; Set the initial limit.
4224       (setq gnus-newsgroup-limit (copy-sequence articles))
4225       ;; Remove canceled articles from the list of unread articles.
4226       (setq gnus-newsgroup-unreads
4227             (gnus-set-sorted-intersection
4228              gnus-newsgroup-unreads
4229              (setq fetched-articles
4230                    (mapcar (lambda (headers) (mail-header-number headers))
4231                            gnus-newsgroup-headers))))
4232       ;; Removed marked articles that do not exist.
4233       (gnus-update-missing-marks
4234        (gnus-sorted-complement fetched-articles articles))
4235
4236       ;; Kludge to avoid having cached articles nixed out in virtual groups.
4237       (when cached
4238         (setq gnus-newsgroup-cached cached))
4239
4240       ;; We might want to build some more threads first.
4241       (when (and gnus-fetch-old-headers
4242                  (eq gnus-headers-retrieved-by 'nov))
4243         (if (eq gnus-fetch-old-headers 'invisible)
4244             (gnus-build-all-threads)
4245           (gnus-build-old-threads)))
4246       ;; Let the Gnus agent mark articles as read.
4247       (when gnus-agent
4248         (gnus-agent-get-undownloaded-list))
4249       ;; Remove list identifiers from subject
4250       (when gnus-list-identifiers
4251         (gnus-summary-remove-list-identifiers))
4252       ;; Check whether auto-expire is to be done in this group.
4253       (setq gnus-newsgroup-auto-expire
4254             (gnus-group-auto-expirable-p group))
4255       ;; Set up the article buffer now, if necessary.
4256       (unless gnus-single-article-buffer
4257         (gnus-article-setup-buffer))
4258       ;; First and last article in this newsgroup.
4259       (when gnus-newsgroup-headers
4260         (setq gnus-newsgroup-begin
4261               (mail-header-number (car gnus-newsgroup-headers))
4262               gnus-newsgroup-end
4263               (mail-header-number
4264                (gnus-last-element gnus-newsgroup-headers))))
4265       ;; GROUP is successfully selected.
4266       (or gnus-newsgroup-headers t)))))
4267
4268 (defun gnus-articles-to-read (group &optional read-all)
4269   "Find out what articles the user wants to read."
4270   (let* ((articles
4271           ;; Select all articles if `read-all' is non-nil, or if there
4272           ;; are no unread articles.
4273           (if (or read-all
4274                   (and (zerop (length gnus-newsgroup-marked))
4275                        (zerop (length gnus-newsgroup-unreads)))
4276                   (eq (gnus-group-find-parameter group 'display)
4277                       'all))
4278               (or
4279                (gnus-uncompress-range (gnus-active group))
4280                (gnus-cache-articles-in-group group))
4281             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
4282                           (copy-sequence gnus-newsgroup-unreads))
4283                   '<)))
4284          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
4285          (scored (length scored-list))
4286          (number (length articles))
4287          (marked (+ (length gnus-newsgroup-marked)
4288                     (length gnus-newsgroup-dormant)))
4289          (select
4290           (cond
4291            ((numberp read-all)
4292             read-all)
4293            (t
4294             (condition-case ()
4295                 (cond
4296                  ((and (or (<= scored marked) (= scored number))
4297                        (natnump gnus-large-newsgroup)
4298                        (> number gnus-large-newsgroup))
4299                   (let* ((cursor-in-echo-area nil)
4300                          (input (read-from-minibuffer
4301                                  (format
4302                                   "How many articles from %s (max %d): "
4303                                   (gnus-limit-string gnus-newsgroup-name 35)
4304                                   number)
4305                                  (cons (number-to-string gnus-large-newsgroup)
4306                                        0))))
4307                     (if (string-match "^[ \t]*$" input)
4308                         number
4309                       input)))
4310                  ((and (> scored marked) (< scored number)
4311                        (> (- scored number) 20))
4312                   (let ((input
4313                          (read-string
4314                           (format "%s %s (%d scored, %d total): "
4315                                   "How many articles from"
4316                                   group scored number))))
4317                     (if (string-match "^[ \t]*$" input)
4318                         number input)))
4319                  (t number))
4320               (quit nil))))))
4321     (setq select (if (stringp select) (string-to-number select) select))
4322     (if (or (null select) (zerop select))
4323         select
4324       (if (and (not (zerop scored)) (<= (abs select) scored))
4325           (progn
4326             (setq articles (sort scored-list '<))
4327             (setq number (length articles)))
4328         (setq articles (copy-sequence articles)))
4329
4330       (when (< (abs select) number)
4331         (if (< select 0)
4332             ;; Select the N oldest articles.
4333             (setcdr (nthcdr (1- (abs select)) articles) nil)
4334           ;; Select the N most recent articles.
4335           (setq articles (nthcdr (- number select) articles))))
4336       (setq gnus-newsgroup-unselected
4337             (gnus-sorted-intersection
4338              gnus-newsgroup-unreads
4339              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
4340       (when gnus-alter-articles-to-read-function
4341         (setq gnus-newsgroup-unreads
4342               (sort
4343                (funcall gnus-alter-articles-to-read-function
4344                         gnus-newsgroup-name gnus-newsgroup-unreads)
4345                '<)))
4346       articles)))
4347
4348 (defun gnus-killed-articles (killed articles)
4349   (let (out)
4350     (while articles
4351       (when (inline (gnus-member-of-range (car articles) killed))
4352         (push (car articles) out))
4353       (setq articles (cdr articles)))
4354     out))
4355
4356 (defun gnus-uncompress-marks (marks)
4357   "Uncompress the mark ranges in MARKS."
4358   (let ((uncompressed '(score bookmark))
4359         out)
4360     (while marks
4361       (if (memq (caar marks) uncompressed)
4362           (push (car marks) out)
4363         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
4364       (setq marks (cdr marks)))
4365     out))
4366
4367 (defun gnus-adjust-marked-articles (info)
4368   "Set all article lists and remove all marks that are no longer valid."
4369   (let* ((marked-lists (gnus-info-marks info))
4370          (active (gnus-active (gnus-info-group info)))
4371          (min (car active))
4372          (max (cdr active))
4373          (types gnus-article-mark-lists)
4374          (uncompressed '(score bookmark killed))
4375          marks var articles article mark)
4376
4377     (while marked-lists
4378       (setq marks (pop marked-lists))
4379       (set (setq var (intern (format "gnus-newsgroup-%s"
4380                                      (car (rassq (setq mark (car marks))
4381                                                  types)))))
4382            (if (memq (car marks) uncompressed) (cdr marks)
4383              (gnus-uncompress-range (cdr marks))))
4384
4385       (setq articles (symbol-value var))
4386
4387       ;; All articles have to be subsets of the active articles.
4388       (cond
4389        ;; Adjust "simple" lists.
4390        ((memq mark '(tick dormant expire reply save))
4391         (while articles
4392           (when (or (< (setq article (pop articles)) min) (> article max))
4393             (set var (delq article (symbol-value var))))))
4394        ;; Adjust assocs.
4395        ((memq mark uncompressed)
4396         (when (not (listp (cdr (symbol-value var))))
4397           (set var (list (symbol-value var))))
4398         (when (not (listp (cdr articles)))
4399           (setq articles (list articles)))
4400         (while articles
4401           (when (or (not (consp (setq article (pop articles))))
4402                     (< (car article) min)
4403                     (> (car article) max))
4404             (set var (delq article (symbol-value var))))))))))
4405
4406 (defun gnus-update-missing-marks (missing)
4407   "Go through the list of MISSING articles and remove them from the mark lists."
4408   (when missing
4409     (let ((types gnus-article-mark-lists)
4410           var m)
4411       ;; Go through all types.
4412       (while types
4413         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
4414         (when (symbol-value var)
4415           ;; This list has articles.  So we delete all missing articles
4416           ;; from it.
4417           (setq m missing)
4418           (while m
4419             (set var (delq (pop m) (symbol-value var)))))))))
4420
4421 (defun gnus-update-marks ()
4422   "Enter the various lists of marked articles into the newsgroup info list."
4423   (let ((types gnus-article-mark-lists)
4424         (info (gnus-get-info gnus-newsgroup-name))
4425         (uncompressed '(score bookmark killed))
4426         type list newmarked symbol delta-marks)
4427     (when info
4428       ;; Add all marks lists to the list of marks lists.
4429       (while (setq type (pop types))
4430         (setq list (symbol-value
4431                     (setq symbol
4432                           (intern (format "gnus-newsgroup-%s"
4433                                           (car type))))))
4434
4435         (when list
4436           ;; Get rid of the entries of the articles that have the
4437           ;; default score.
4438           (when (and (eq (cdr type) 'score)
4439                      gnus-save-score
4440                      list)
4441             (let* ((arts list)
4442                    (prev (cons nil list))
4443                    (all prev))
4444               (while arts
4445                 (if (or (not (consp (car arts)))
4446                         (= (cdar arts) gnus-summary-default-score))
4447                     (setcdr prev (cdr arts))
4448                   (setq prev arts))
4449                 (setq arts (cdr arts)))
4450               (setq list (cdr all)))))
4451
4452         (unless (memq (cdr type) uncompressed)
4453           (setq list (gnus-compress-sequence (set symbol (sort list '<)) t)))
4454
4455         (when (gnus-check-backend-function
4456                'request-set-mark gnus-newsgroup-name)
4457           ;; propagate flags to server, with the following exceptions:
4458           ;; uncompressed:s are not proper flags (they are cons cells)
4459           ;; cache is a internal gnus flag
4460           ;; download are local to one gnus installation (well)
4461           ;; unsend are for nndraft groups only
4462           ;; xxx: generality of this?  this suits nnimap anyway
4463           (unless (memq (cdr type) (append '(cache download unsend)
4464                                            uncompressed))
4465             (let* ((old (cdr (assq (cdr type) (gnus-info-marks info))))
4466                    (del (gnus-remove-from-range (gnus-copy-sequence old) list))
4467                    (add (gnus-remove-from-range
4468                          (gnus-copy-sequence list) old)))
4469               (when add
4470                 (push (list add 'add (list (cdr type))) delta-marks))
4471               (when del
4472                 (push (list del 'del (list (cdr type))) delta-marks)))))
4473
4474         (when list
4475           (push (cons (cdr type) list) newmarked)))
4476
4477       (when delta-marks
4478         (unless (gnus-check-group gnus-newsgroup-name)
4479           (error "Can't open server for %s" gnus-newsgroup-name))
4480         (gnus-request-set-mark gnus-newsgroup-name delta-marks))
4481
4482       ;; Enter these new marks into the info of the group.
4483       (if (nthcdr 3 info)
4484           (setcar (nthcdr 3 info) newmarked)
4485         ;; Add the marks lists to the end of the info.
4486         (when newmarked
4487           (setcdr (nthcdr 2 info) (list newmarked))))
4488
4489       ;; Cut off the end of the info if there's nothing else there.
4490       (let ((i 5))
4491         (while (and (> i 2)
4492                     (not (nth i info)))
4493           (when (nthcdr (decf i) info)
4494             (setcdr (nthcdr i info) nil)))))))
4495
4496 (defun gnus-set-mode-line (where)
4497   "Set the mode line of the article or summary buffers.
4498 If WHERE is `summary', the summary mode line format will be used."
4499   ;; Is this mode line one we keep updated?
4500   (when (and (memq where gnus-updated-mode-lines)
4501              (symbol-value
4502               (intern (format "gnus-%s-mode-line-format-spec" where))))
4503     (let (mode-string)
4504       (save-excursion
4505         ;; We evaluate this in the summary buffer since these
4506         ;; variables are buffer-local to that buffer.
4507         (set-buffer gnus-summary-buffer)
4508         ;; We bind all these variables that are used in the `eval' form
4509         ;; below.
4510         (let* ((mformat (symbol-value
4511                          (intern
4512                           (format "gnus-%s-mode-line-format-spec" where))))
4513                (gnus-tmp-group-name (gnus-group-name-decode
4514                                      gnus-newsgroup-name
4515                                      (gnus-group-name-charset
4516                                       nil
4517                                       gnus-newsgroup-name)))
4518                (gnus-tmp-article-number (or gnus-current-article 0))
4519                (gnus-tmp-unread gnus-newsgroup-unreads)
4520                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
4521                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
4522                (gnus-tmp-unread-and-unselected
4523                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
4524                             (zerop gnus-tmp-unselected))
4525                        "")
4526                       ((zerop gnus-tmp-unselected)
4527                        (format "{%d more}" gnus-tmp-unread-and-unticked))
4528                       (t (format "{%d(+%d) more}"
4529                                  gnus-tmp-unread-and-unticked
4530                                  gnus-tmp-unselected))))
4531                (gnus-tmp-subject
4532                 (if (and gnus-current-headers
4533                          (vectorp gnus-current-headers))
4534                     (gnus-mode-string-quote
4535                      (mail-header-subject gnus-current-headers))
4536                   ""))
4537                bufname-length max-len
4538                gnus-tmp-header);; passed as argument to any user-format-funcs
4539           (setq mode-string (eval mformat))
4540           (setq bufname-length (if (string-match "%b" mode-string)
4541                                    (- (length
4542                                        (buffer-name
4543                                         (if (eq where 'summary)
4544                                             nil
4545                                           (get-buffer gnus-article-buffer))))
4546                                       2)
4547                                  0))
4548           (setq max-len (max 4 (if gnus-mode-non-string-length
4549                                    (- (window-width)
4550                                       gnus-mode-non-string-length
4551                                       bufname-length)
4552                                  (length mode-string))))
4553           ;; We might have to chop a bit of the string off...
4554           (when (> (length mode-string) max-len)
4555             (setq mode-string
4556                   (concat (gnus-truncate-string mode-string (- max-len 3))
4557                           "...")))
4558           ;; Pad the mode string a bit.
4559           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
4560       ;; Update the mode line.
4561       (setq mode-line-buffer-identification
4562             (gnus-mode-line-buffer-identification (list mode-string)))
4563       (set-buffer-modified-p t))))
4564
4565 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
4566   "Go through the HEADERS list and add all Xrefs to a hash table.
4567 The resulting hash table is returned, or nil if no Xrefs were found."
4568   (let* ((virtual (gnus-virtual-group-p from-newsgroup))
4569          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
4570          (xref-hashtb (gnus-make-hashtable))
4571          start group entry number xrefs header)
4572     (while headers
4573       (setq header (pop headers))
4574       (when (and (setq xrefs (mail-header-xref header))
4575                  (not (memq (setq number (mail-header-number header))
4576                             unreads)))
4577         (setq start 0)
4578         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
4579           (setq start (match-end 0))
4580           (setq group (if prefix
4581                           (concat prefix (substring xrefs (match-beginning 1)
4582                                                     (match-end 1)))
4583                         (substring xrefs (match-beginning 1) (match-end 1))))
4584           (setq number
4585                 (string-to-int (substring xrefs (match-beginning 2)
4586                                           (match-end 2))))
4587           (if (setq entry (gnus-gethash group xref-hashtb))
4588               (setcdr entry (cons number (cdr entry)))
4589             (gnus-sethash group (cons number nil) xref-hashtb)))))
4590     (and start xref-hashtb)))
4591
4592 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
4593   "Look through all the headers and mark the Xrefs as read."
4594   (let ((virtual (gnus-virtual-group-p from-newsgroup))
4595         name entry info xref-hashtb idlist method nth4)
4596     (save-excursion
4597       (set-buffer gnus-group-buffer)
4598       (when (setq xref-hashtb
4599                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
4600         (mapatoms
4601          (lambda (group)
4602            (unless (string= from-newsgroup (setq name (symbol-name group)))
4603              (setq idlist (symbol-value group))
4604              ;; Dead groups are not updated.
4605              (and (prog1
4606                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
4607                             info (nth 2 entry))
4608                     (when (stringp (setq nth4 (gnus-info-method info)))
4609                       (setq nth4 (gnus-server-to-method nth4))))
4610                   ;; Only do the xrefs if the group has the same
4611                   ;; select method as the group we have just read.
4612                   (or (gnus-methods-equal-p
4613                        nth4 (gnus-find-method-for-group from-newsgroup))
4614                       virtual
4615                       (equal nth4 (setq method (gnus-find-method-for-group
4616                                                 from-newsgroup)))
4617                       (and (equal (car nth4) (car method))
4618                            (equal (nth 1 nth4) (nth 1 method))))
4619                   gnus-use-cross-reference
4620                   (or (not (eq gnus-use-cross-reference t))
4621                       virtual
4622                       ;; Only do cross-references on subscribed
4623                       ;; groups, if that is what is wanted.
4624                       (<= (gnus-info-level info) gnus-level-subscribed))
4625                   (gnus-group-make-articles-read name idlist))))
4626          xref-hashtb)))))
4627
4628 (defun gnus-compute-read-articles (group articles)
4629   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
4630          (info (nth 2 entry))
4631          (active (gnus-active group))
4632          ninfo)
4633     (when entry
4634       ;; First peel off all invalid article numbers.
4635       (when active
4636         (let ((ids articles)
4637               id first)
4638           (while (setq id (pop ids))
4639             (when (and first (> id (cdr active)))
4640               ;; We'll end up in this situation in one particular
4641               ;; obscure situation.  If you re-scan a group and get
4642               ;; a new article that is cross-posted to a different
4643               ;; group that has not been re-scanned, you might get
4644               ;; crossposted article that has a higher number than
4645               ;; Gnus believes possible.  So we re-activate this
4646               ;; group as well.  This might mean doing the
4647               ;; crossposting thingy will *increase* the number
4648               ;; of articles in some groups.  Tsk, tsk.
4649               (setq active (or (gnus-activate-group group) active)))
4650             (when (or (> id (cdr active))
4651                       (< id (car active)))
4652               (setq articles (delq id articles))))))
4653       ;; If the read list is nil, we init it.
4654       (if (and active
4655                (null (gnus-info-read info))
4656                (> (car active) 1))
4657           (setq ninfo (cons 1 (1- (car active))))
4658         (setq ninfo (gnus-info-read info)))
4659       ;; Then we add the read articles to the range.
4660       (gnus-add-to-range
4661        ninfo (setq articles (sort articles '<))))))
4662
4663 (defun gnus-group-make-articles-read (group articles)
4664   "Update the info of GROUP to say that ARTICLES are read."
4665   (let* ((num 0)
4666          (entry (gnus-gethash group gnus-newsrc-hashtb))
4667          (info (nth 2 entry))
4668          (active (gnus-active group))
4669          range)
4670     (when entry
4671       (setq range (gnus-compute-read-articles group articles))
4672       (save-excursion
4673         (set-buffer gnus-group-buffer)
4674         (gnus-undo-register
4675           `(progn
4676              (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
4677              (gnus-info-set-read ',info ',(gnus-info-read info))
4678              (gnus-get-unread-articles-in-group ',info (gnus-active ,group))
4679              (gnus-group-update-group ,group t))))
4680       ;; Add the read articles to the range.
4681       (gnus-info-set-read info range)
4682       ;; Then we have to re-compute how many unread
4683       ;; articles there are in this group.
4684       (when active
4685         (cond
4686          ((not range)
4687           (setq num (- (1+ (cdr active)) (car active))))
4688          ((not (listp (cdr range)))
4689           (setq num (- (cdr active) (- (1+ (cdr range))
4690                                        (car range)))))
4691          (t
4692           (while range
4693             (if (numberp (car range))
4694                 (setq num (1+ num))
4695               (setq num (+ num (- (1+ (cdar range)) (caar range)))))
4696             (setq range (cdr range)))
4697           (setq num (- (cdr active) num))))
4698         ;; Update the number of unread articles.
4699         (setcar entry num)
4700         ;; Update the group buffer.
4701         (gnus-group-update-group group t)))))
4702
4703 (defvar gnus-newsgroup-none-id 0)
4704
4705 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
4706   (let ((cur nntp-server-buffer)
4707         (dependencies
4708          (or dependencies
4709              (save-excursion (set-buffer gnus-summary-buffer)
4710                              gnus-newsgroup-dependencies)))
4711         headers id end ref
4712         (mail-parse-charset gnus-newsgroup-charset)
4713         (mail-parse-ignored-charsets
4714          (save-excursion (condition-case nil
4715                              (set-buffer gnus-summary-buffer)
4716                            (error))
4717                          gnus-newsgroup-ignored-charsets)))
4718     (save-excursion
4719       (set-buffer nntp-server-buffer)
4720       ;; Translate all TAB characters into SPACE characters.
4721       (subst-char-in-region (point-min) (point-max) ?\t ?  t)
4722       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4723       (gnus-run-hooks 'gnus-parse-headers-hook)
4724       (let ((case-fold-search t)
4725             in-reply-to header p lines chars ctype)
4726         (goto-char (point-min))
4727         ;; Search to the beginning of the next header.  Error messages
4728         ;; do not begin with 2 or 3.
4729         (while (re-search-forward "^[23][0-9]+ " nil t)
4730           (setq id nil
4731                 ref nil)
4732           ;; This implementation of this function, with nine
4733           ;; search-forwards instead of the one re-search-forward and
4734           ;; a case (which basically was the old function) is actually
4735           ;; about twice as fast, even though it looks messier.  You
4736           ;; can't have everything, I guess.  Speed and elegance
4737           ;; doesn't always go hand in hand.
4738           (setq
4739            header
4740            (make-full-mail-header
4741             ;; Number.
4742             (prog1
4743                 (read cur)
4744               (end-of-line)
4745               (setq p (point))
4746               (narrow-to-region (point)
4747                                 (or (and (search-forward "\n.\n" nil t)
4748                                          (- (point) 2))
4749                                     (point))))
4750             ;; Subject.
4751             (progn
4752               (goto-char p)
4753               (if (search-forward "\nsubject: " nil t)
4754                   (buffer-substring (match-end 0) (std11-field-end))
4755                 "(none)"))
4756             ;; From.
4757             (progn
4758               (goto-char p)
4759               (if (search-forward "\nfrom: " nil t)
4760                   (buffer-substring (match-end 0) (std11-field-end))
4761                 "(nobody)"))
4762             ;; Date.
4763             (progn
4764               (goto-char p)
4765               (if (search-forward "\ndate: " nil t)
4766                   (buffer-substring (match-end 0) (std11-field-end))
4767                 ""))
4768             ;; Message-ID.
4769             (progn
4770               (goto-char p)
4771               (setq id (if (re-search-forward
4772                             "^message-id: *\\(<[^\n\t> ]+>\\)" nil t)
4773                            ;; We do it this way to make sure the Message-ID
4774                            ;; is (somewhat) syntactically valid.
4775                            (buffer-substring (match-beginning 1)
4776                                              (match-end 1))
4777                          ;; If there was no message-id, we just fake one
4778                          ;; to make subsequent routines simpler.
4779                          (nnheader-generate-fake-message-id))))
4780             ;; References.
4781             (progn
4782               (goto-char p)
4783               (if (search-forward "\nreferences: " nil t)
4784                   (progn
4785                     (setq end (point))
4786                     (prog1
4787                         (buffer-substring (match-end 0) (std11-field-end))
4788                       (setq ref
4789                             (buffer-substring
4790                              (progn
4791                                ;; (end-of-line)
4792                                (search-backward ">" end t)
4793                                (1+ (point)))
4794                              (progn
4795                                (search-backward "<" end t)
4796                                (point))))))
4797                 ;; Get the references from the in-reply-to header if there
4798                 ;; were no references and the in-reply-to header looks
4799                 ;; promising.
4800                 (if (and (search-forward "\nin-reply-to: " nil t)
4801                          (setq in-reply-to
4802                                (buffer-substring (match-end 0)
4803                                                  (std11-field-end)))
4804                          (string-match "<[^>]+>" in-reply-to))
4805                     (let (ref2)
4806                       (setq ref (substring in-reply-to (match-beginning 0)
4807                                            (match-end 0)))
4808                       (while (string-match "<[^>]+>" in-reply-to (match-end 0))
4809                         (setq ref2 (substring in-reply-to (match-beginning 0)
4810                                               (match-end 0)))
4811                         (when (> (length ref2) (length ref))
4812                           (setq ref ref2)))
4813                       ref)
4814                   (setq ref nil))))
4815             ;; Chars.
4816             (progn
4817               (goto-char p)
4818               (if (search-forward "\nchars: " nil t)
4819                   (if (numberp (setq chars (ignore-errors (read cur))))
4820                       chars 0)
4821                 0))
4822             ;; Lines.
4823             (progn
4824               (goto-char p)
4825               (if (search-forward "\nlines: " nil t)
4826                   (if (numberp (setq lines (ignore-errors (read cur))))
4827                       lines 0)
4828                 0))
4829             ;; Xref.
4830             (progn
4831               (goto-char p)
4832               (and (search-forward "\nxref: " nil t)
4833                    (buffer-substring (match-end 0) (std11-field-end))))
4834             ;; Extra.
4835             (when gnus-extra-headers
4836               (let ((extra gnus-extra-headers)
4837                     out)
4838                 (while extra
4839                   (goto-char p)
4840                   (when (search-forward
4841                          (concat "\n" (symbol-name (car extra)) ": ") nil t)
4842                     (push (cons (car extra)
4843                                 (buffer-substring (match-end 0)
4844                                                   (std11-field-end)))
4845                           out))
4846                   (pop extra))
4847                 out))))
4848           (goto-char p)
4849           (if (and (search-forward "\ncontent-type: " nil t)
4850                    (setq ctype
4851                          (buffer-substring (match-end 0) (std11-field-end))))
4852               (mime-entity-set-content-type-internal
4853                header (mime-parse-Content-Type ctype)))
4854           (when (equal id ref)
4855             (setq ref nil))
4856
4857           (when gnus-alter-header-function
4858             (funcall gnus-alter-header-function header)
4859             (setq id (mail-header-id header)
4860                   ref (gnus-parent-id (mail-header-references header))))
4861
4862           (when (setq header
4863                       (gnus-dependencies-add-header
4864                        header dependencies force-new))
4865             (push header headers))
4866           (goto-char (point-max))
4867           (widen))
4868         (nreverse headers)))))
4869
4870 ;; Goes through the xover lines and returns a list of vectors
4871 (defun gnus-get-newsgroup-headers-xover (sequence &optional
4872                                                   force-new dependencies
4873                                                   group also-fetch-heads)
4874   "Parse the news overview data in the server buffer.
4875 Return a list of headers that match SEQUENCE (see
4876 `nntp-retrieve-headers')."
4877   ;; Get the Xref when the users reads the articles since most/some
4878   ;; NNTP servers do not include Xrefs when using XOVER.
4879   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
4880   (let ((mail-parse-charset gnus-newsgroup-charset)
4881         (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
4882         (cur nntp-server-buffer)
4883         (dependencies (or dependencies gnus-newsgroup-dependencies))
4884         number headers header)
4885     (save-excursion
4886       (set-buffer nntp-server-buffer)
4887       (subst-char-in-region (point-min) (point-max) ?\r ?  t)
4888       ;; Allow the user to mangle the headers before parsing them.
4889       (gnus-run-hooks 'gnus-parse-headers-hook)
4890       (goto-char (point-min))
4891       (while (not (eobp))
4892         (condition-case ()
4893             (while (and sequence (not (eobp)))
4894               (setq number (read cur))
4895               (while (and sequence
4896                           (< (car sequence) number))
4897                 (setq sequence (cdr sequence)))
4898               (and sequence
4899                    (eq number (car sequence))
4900                    (progn
4901                      (setq sequence (cdr sequence))
4902                      (setq header (inline
4903                                     (gnus-nov-parse-line
4904                                      number dependencies force-new))))
4905                    (push header headers))
4906               (forward-line 1))
4907           (error
4908            (gnus-error 4 "Strange nov line (%d)"
4909                        (count-lines (point-min) (point)))))
4910         (forward-line 1))
4911       ;; A common bug in inn is that if you have posted an article and
4912       ;; then retrieves the active file, it will answer correctly --
4913       ;; the new article is included.  However, a NOV entry for the
4914       ;; article may not have been generated yet, so this may fail.
4915       ;; We work around this problem by retrieving the last few
4916       ;; headers using HEAD.
4917       (if (or (not also-fetch-heads)
4918               (not sequence))
4919           ;; We (probably) got all the headers.
4920           (nreverse headers)
4921         (let ((gnus-nov-is-evil t))
4922           (nconc
4923            (nreverse headers)
4924            (gnus-retrieve-parsed-headers sequence group)
4925            ))))))
4926
4927 (defun gnus-article-get-xrefs ()
4928   "Fill in the Xref value in `gnus-current-headers', if necessary.
4929 This is meant to be called in `gnus-article-internal-prepare-hook'."
4930   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
4931                                  gnus-current-headers)))
4932     (or (not gnus-use-cross-reference)
4933         (not headers)
4934         (and (mail-header-xref headers)
4935              (not (string= (mail-header-xref headers) "")))
4936         (let ((case-fold-search t)
4937               xref)
4938           (save-restriction
4939             (nnheader-narrow-to-headers)
4940             (goto-char (point-min))
4941             (when (or (and (not (eobp))
4942                            (eq (downcase (char-after)) ?x)
4943                            (looking-at "Xref:"))
4944                       (search-forward "\nXref:" nil t))
4945               (goto-char (1+ (match-end 0)))
4946               (setq xref (buffer-substring (point)
4947                                            (progn (end-of-line) (point))))
4948               (mail-header-set-xref headers xref)))))))
4949
4950 (defun gnus-summary-insert-subject (id &optional old-header use-old-header)
4951   "Find article ID and insert the summary line for that article.
4952 OLD-HEADER can either be a header or a line number to insert
4953 the subject line on."
4954   (let* ((line (and (numberp old-header) old-header))
4955          (old-header (and (vectorp old-header) old-header))
4956          (header (cond ((and old-header use-old-header)
4957                         old-header)
4958                        ((and (numberp id)
4959                              (gnus-number-to-header id))
4960                         (gnus-number-to-header id))
4961                        (t
4962                         (gnus-read-header id))))
4963          (number (and (numberp id) id))
4964          d)
4965     (when header
4966       ;; Rebuild the thread that this article is part of and go to the
4967       ;; article we have fetched.
4968       (when (and (not gnus-show-threads)
4969                  old-header)
4970         (when (and number
4971                    (setq d (gnus-data-find (mail-header-number old-header))))
4972           (goto-char (gnus-data-pos d))
4973           (gnus-data-remove
4974            number
4975            (- (gnus-point-at-bol)
4976               (prog1
4977                   (1+ (gnus-point-at-eol))
4978                 (gnus-delete-line))))))
4979       (when old-header
4980         (mail-header-set-number header (mail-header-number old-header)))
4981       (setq gnus-newsgroup-sparse
4982             (delq (setq number (mail-header-number header))
4983                   gnus-newsgroup-sparse))
4984       (setq gnus-newsgroup-ancient (delq number gnus-newsgroup-ancient))
4985       (push number gnus-newsgroup-limit)
4986       (gnus-rebuild-thread (mail-header-id header) line)
4987       (gnus-summary-goto-subject number nil t))
4988     (when (and (numberp number)
4989                (> number 0))
4990       ;; We have to update the boundaries even if we can't fetch the
4991       ;; article if ID is a number -- so that the next `P' or `N'
4992       ;; command will fetch the previous (or next) article even
4993       ;; if the one we tried to fetch this time has been canceled.
4994       (when (> number gnus-newsgroup-end)
4995         (setq gnus-newsgroup-end number))
4996       (when (< number gnus-newsgroup-begin)
4997         (setq gnus-newsgroup-begin number))
4998       (setq gnus-newsgroup-unselected
4999             (delq number gnus-newsgroup-unselected)))
5000     ;; Report back a success?
5001     (and header (mail-header-number header))))
5002
5003 ;;; Process/prefix in the summary buffer
5004
5005 (defun gnus-summary-work-articles (n)
5006   "Return a list of articles to be worked upon.
5007 The prefix argument, the list of process marked articles, and the
5008 current article will be taken into consideration."
5009   (save-excursion
5010     (set-buffer gnus-summary-buffer)
5011     (cond
5012      (n
5013       ;; A numerical prefix has been given.
5014       (setq n (prefix-numeric-value n))
5015       (let ((backward (< n 0))
5016             (n (abs (prefix-numeric-value n)))
5017             articles article)
5018         (save-excursion
5019           (while
5020               (and (> n 0)
5021                    (push (setq article (gnus-summary-article-number))
5022                          articles)
5023                    (if backward
5024                        (gnus-summary-find-prev nil article)
5025                      (gnus-summary-find-next nil article)))
5026             (decf n)))
5027         (nreverse articles)))
5028      ((and (gnus-region-active-p) (mark))
5029       (message "region active")
5030       ;; Work on the region between point and mark.
5031       (let ((max (max (point) (mark)))
5032             articles article)
5033         (save-excursion
5034           (goto-char (min (point) (mark)))
5035           (while
5036               (and
5037                (push (setq article (gnus-summary-article-number)) articles)
5038                (gnus-summary-find-next nil article)
5039                (< (point) max)))
5040           (nreverse articles))))
5041      (gnus-newsgroup-processable
5042       ;; There are process-marked articles present.
5043       ;; Save current state.
5044       (gnus-summary-save-process-mark)
5045       ;; Return the list.
5046       (reverse gnus-newsgroup-processable))
5047      (t
5048       ;; Just return the current article.
5049       (list (gnus-summary-article-number))))))
5050
5051 (defmacro gnus-summary-iterate (arg &rest forms)
5052   "Iterate over the process/prefixed articles and do FORMS.
5053 ARG is the interactive prefix given to the command.  FORMS will be
5054 executed with point over the summary line of the articles."
5055   (let ((articles (make-symbol "gnus-summary-iterate-articles")))
5056     `(let ((,articles (gnus-summary-work-articles ,arg)))
5057        (while ,articles
5058          (gnus-summary-goto-subject (car ,articles))
5059          ,@forms
5060          (pop ,articles)))))
5061
5062 (put 'gnus-summary-iterate 'lisp-indent-function 1)
5063 (put 'gnus-summary-iterate 'edebug-form-spec '(form body))
5064
5065 (defun gnus-summary-save-process-mark ()
5066   "Push the current set of process marked articles on the stack."
5067   (interactive)
5068   (push (copy-sequence gnus-newsgroup-processable)
5069         gnus-newsgroup-process-stack))
5070
5071 (defun gnus-summary-kill-process-mark ()
5072   "Push the current set of process marked articles on the stack and unmark."
5073   (interactive)
5074   (gnus-summary-save-process-mark)
5075   (gnus-summary-unmark-all-processable))
5076
5077 (defun gnus-summary-yank-process-mark ()
5078   "Pop the last process mark state off the stack and restore it."
5079   (interactive)
5080   (unless gnus-newsgroup-process-stack
5081     (error "Empty mark stack"))
5082   (gnus-summary-process-mark-set (pop gnus-newsgroup-process-stack)))
5083
5084 (defun gnus-summary-process-mark-set (set)
5085   "Make SET into the current process marked articles."
5086   (gnus-summary-unmark-all-processable)
5087   (while set
5088     (gnus-summary-set-process-mark (pop set))))
5089
5090 ;;; Searching and stuff
5091
5092 (defun gnus-summary-search-group (&optional backward use-level)
5093   "Search for next unread newsgroup.
5094 If optional argument BACKWARD is non-nil, search backward instead."
5095   (save-excursion
5096     (set-buffer gnus-group-buffer)
5097     (when (gnus-group-search-forward
5098            backward nil (if use-level (gnus-group-group-level) nil))
5099       (gnus-group-group-name))))
5100
5101 (defun gnus-summary-best-group (&optional exclude-group)
5102   "Find the name of the best unread group.
5103 If EXCLUDE-GROUP, do not go to this group."
5104   (save-excursion
5105     (set-buffer gnus-group-buffer)
5106     (save-excursion
5107       (gnus-group-best-unread-group exclude-group))))
5108
5109 (defun gnus-summary-find-next (&optional unread article backward undownloaded)
5110   (if backward (gnus-summary-find-prev)
5111     (let* ((dummy (gnus-summary-article-intangible-p))
5112            (article (or article (gnus-summary-article-number)))
5113            (arts (gnus-data-find-list article))
5114            result)
5115       (when (and (not dummy)
5116                  (or (not gnus-summary-check-current)
5117                      (not unread)
5118                      (not (gnus-data-unread-p (car arts)))))
5119         (setq arts (cdr arts)))
5120       (when (setq result
5121                   (if unread
5122                       (progn
5123                         (while arts
5124                           (when (or (and undownloaded
5125                                          (eq gnus-undownloaded-mark
5126                                              (gnus-data-mark (car arts))))
5127                                     (gnus-data-unread-p (car arts)))
5128                             (setq result (car arts)
5129                                   arts nil))
5130                           (setq arts (cdr arts)))
5131                         result)
5132                     (car arts)))
5133         (goto-char (gnus-data-pos result))
5134         (gnus-data-number result)))))
5135
5136 (defun gnus-summary-find-prev (&optional unread article)
5137   (let* ((eobp (eobp))
5138          (article (or article (gnus-summary-article-number)))
5139          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
5140          result)
5141     (when (and (not eobp)
5142                (or (not gnus-summary-check-current)
5143                    (not unread)
5144                    (not (gnus-data-unread-p (car arts)))))
5145       (setq arts (cdr arts)))
5146     (when (setq result
5147                 (if unread
5148                     (progn
5149                       (while arts
5150                         (when (gnus-data-unread-p (car arts))
5151                           (setq result (car arts)
5152                                 arts nil))
5153                         (setq arts (cdr arts)))
5154                       result)
5155                   (car arts)))
5156       (goto-char (gnus-data-pos result))
5157       (gnus-data-number result))))
5158
5159 (defun gnus-summary-find-subject (subject &optional unread backward article)
5160   (let* ((simp-subject (gnus-simplify-subject-fully subject))
5161          (article (or article (gnus-summary-article-number)))
5162          (articles (gnus-data-list backward))
5163          (arts (gnus-data-find-list article articles))
5164          result)
5165     (when (or (not gnus-summary-check-current)
5166               (not unread)
5167               (not (gnus-data-unread-p (car arts))))
5168       (setq arts (cdr arts)))
5169     (while arts
5170       (and (or (not unread)
5171                (gnus-data-unread-p (car arts)))
5172            (vectorp (gnus-data-header (car arts)))
5173            (gnus-subject-equal
5174             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
5175            (setq result (car arts)
5176                  arts nil))
5177       (setq arts (cdr arts)))
5178     (and result
5179          (goto-char (gnus-data-pos result))
5180          (gnus-data-number result))))
5181
5182 (defun gnus-summary-search-forward (&optional unread subject backward)
5183   "Search forward for an article.
5184 If UNREAD, look for unread articles.  If SUBJECT, look for
5185 articles with that subject.  If BACKWARD, search backward instead."
5186   (cond (subject (gnus-summary-find-subject subject unread backward))
5187         (backward (gnus-summary-find-prev unread))
5188         (t (gnus-summary-find-next unread))))
5189
5190 (defun gnus-recenter (&optional n)
5191   "Center point in window and redisplay frame.
5192 Also do horizontal recentering."
5193   (interactive "P")
5194   (when (and gnus-auto-center-summary
5195              (not (eq gnus-auto-center-summary 'vertical)))
5196     (gnus-horizontal-recenter))
5197   (recenter n))
5198
5199 (defun gnus-summary-recenter ()
5200   "Center point in the summary window.
5201 If `gnus-auto-center-summary' is nil, or the article buffer isn't
5202 displayed, no centering will be performed."
5203   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
5204   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
5205   (interactive)
5206   (let* ((top (cond ((< (window-height) 4) 0)
5207                     ((< (window-height) 7) 1)
5208                     (t (if (numberp gnus-auto-center-summary)
5209                            gnus-auto-center-summary
5210                          2))))
5211          (height (1- (window-height)))
5212          (bottom (save-excursion (goto-char (point-max))
5213                                  (forward-line (- height))
5214                                  (point)))
5215          (window (get-buffer-window (current-buffer))))
5216     ;; The user has to want it.
5217     (when gnus-auto-center-summary
5218       (when (get-buffer-window gnus-article-buffer)
5219         ;; Only do recentering when the article buffer is displayed,
5220         ;; Set the window start to either `bottom', which is the biggest
5221         ;; possible valid number, or the second line from the top,
5222         ;; whichever is the least.
5223         (set-window-start
5224          window (min bottom (save-excursion
5225                               (forward-line (- top)) (point)))
5226          t))
5227       ;; Do horizontal recentering while we're at it.
5228       (when (and (get-buffer-window (current-buffer) t)
5229                  (not (eq gnus-auto-center-summary 'vertical)))
5230         (let ((selected (selected-window)))
5231           (select-window (get-buffer-window (current-buffer) t))
5232           (gnus-summary-position-point)
5233           (gnus-horizontal-recenter)
5234           (select-window selected))))))
5235
5236 (defun gnus-summary-jump-to-group (newsgroup)
5237   "Move point to NEWSGROUP in group mode buffer."
5238   ;; Keep update point of group mode buffer if visible.
5239   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
5240       (save-window-excursion
5241         ;; Take care of tree window mode.
5242         (when (get-buffer-window gnus-group-buffer)
5243           (pop-to-buffer gnus-group-buffer))
5244         (gnus-group-jump-to-group newsgroup))
5245     (save-excursion
5246       ;; Take care of tree window mode.
5247       (if (get-buffer-window gnus-group-buffer)
5248           (pop-to-buffer gnus-group-buffer)
5249         (set-buffer gnus-group-buffer))
5250       (gnus-group-jump-to-group newsgroup))))
5251
5252 ;; This function returns a list of article numbers based on the
5253 ;; difference between the ranges of read articles in this group and
5254 ;; the range of active articles.
5255 (defun gnus-list-of-unread-articles (group)
5256   (let* ((read (gnus-info-read (gnus-get-info group)))
5257          (active (or (gnus-active group) (gnus-activate-group group)))
5258          (last (cdr active))
5259          first nlast unread)
5260     ;; If none are read, then all are unread.
5261     (if (not read)
5262         (setq first (car active))
5263       ;; If the range of read articles is a single range, then the
5264       ;; first unread article is the article after the last read
5265       ;; article.  Sounds logical, doesn't it?
5266       (if (and (not (listp (cdr read)))
5267                (or (< (car read) (car active))
5268                    (progn (setq read (list read))
5269                           nil)))
5270           (setq first (max (car active) (1+ (cdr read))))
5271         ;; `read' is a list of ranges.
5272         (when (/= (setq nlast (or (and (numberp (car read)) (car read))
5273                                   (caar read)))
5274                   1)
5275           (setq first (car active)))
5276         (while read
5277           (when first
5278             (while (< first nlast)
5279               (push first unread)
5280               (setq first (1+ first))))
5281           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
5282           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
5283           (setq read (cdr read)))))
5284     ;; And add the last unread articles.
5285     (while (<= first last)
5286       (push first unread)
5287       (setq first (1+ first)))
5288     ;; Return the list of unread articles.
5289     (delq 0 (nreverse unread))))
5290
5291 (defun gnus-list-of-read-articles (group)
5292   "Return a list of unread, unticked and non-dormant articles."
5293   (let* ((info (gnus-get-info group))
5294          (marked (gnus-info-marks info))
5295          (active (gnus-active group)))
5296     (and info active
5297          (gnus-set-difference
5298           (gnus-sorted-complement
5299            (gnus-uncompress-range active)
5300            (gnus-list-of-unread-articles group))
5301           (append
5302            (gnus-uncompress-range (cdr (assq 'dormant marked)))
5303            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
5304
5305 ;; Various summary commands
5306
5307 (defun gnus-summary-select-article-buffer ()
5308   "Reconfigure windows to show article buffer."
5309   (interactive)
5310   (if (not (gnus-buffer-live-p gnus-article-buffer))
5311       (error "There is no article buffer for this summary buffer")
5312     (gnus-configure-windows 'article)
5313     (select-window (get-buffer-window gnus-article-buffer))))
5314
5315 (defun gnus-summary-universal-argument (arg)
5316   "Perform any operation on all articles that are process/prefixed."
5317   (interactive "P")
5318   (let ((articles (gnus-summary-work-articles arg))
5319         func article)
5320     (if (eq
5321          (setq
5322           func
5323           (key-binding
5324            (read-key-sequence
5325             (substitute-command-keys
5326              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"))))
5327          'undefined)
5328         (gnus-error 1 "Undefined key")
5329       (save-excursion
5330         (while articles
5331           (gnus-summary-goto-subject (setq article (pop articles)))
5332           (let (gnus-newsgroup-processable)
5333             (command-execute func))
5334           (gnus-summary-remove-process-mark article)))))
5335   (gnus-summary-position-point))
5336
5337 (defun gnus-summary-toggle-truncation (&optional arg)
5338   "Toggle truncation of summary lines.
5339 With arg, turn line truncation on iff arg is positive."
5340   (interactive "P")
5341   (setq truncate-lines
5342         (if (null arg) (not truncate-lines)
5343           (> (prefix-numeric-value arg) 0)))
5344   (redraw-display))
5345
5346 (defun gnus-summary-reselect-current-group (&optional all rescan)
5347   "Exit and then reselect the current newsgroup.
5348 The prefix argument ALL means to select all articles."
5349   (interactive "P")
5350   (when (gnus-ephemeral-group-p gnus-newsgroup-name)
5351     (error "Ephemeral groups can't be reselected"))
5352   (let ((current-subject (gnus-summary-article-number))
5353         (group gnus-newsgroup-name))
5354     (setq gnus-newsgroup-begin nil)
5355     (gnus-summary-exit)
5356     ;; We have to adjust the point of group mode buffer because
5357     ;; point was moved to the next unread newsgroup by exiting.
5358     (gnus-summary-jump-to-group group)
5359     (when rescan
5360       (save-excursion
5361         (save-window-excursion
5362           ;; Don't show group contents.
5363           (set-window-start (selected-window) (point-max))
5364           (gnus-group-get-new-news-this-group 1))))
5365     (gnus-group-read-group all t)
5366     (gnus-summary-goto-subject current-subject nil t)))
5367
5368 (defun gnus-summary-rescan-group (&optional all)
5369   "Exit the newsgroup, ask for new articles, and select the newsgroup."
5370   (interactive "P")
5371   (gnus-summary-reselect-current-group all t))
5372
5373 (defun gnus-summary-update-info (&optional non-destructive)
5374   (save-excursion
5375     (let ((group gnus-newsgroup-name))
5376       (when group
5377         (when gnus-newsgroup-kill-headers
5378           (setq gnus-newsgroup-killed
5379                 (gnus-compress-sequence
5380                  (nconc
5381                   (gnus-set-sorted-intersection
5382                    (gnus-uncompress-range gnus-newsgroup-killed)
5383                    (setq gnus-newsgroup-unselected
5384                          (sort gnus-newsgroup-unselected '<)))
5385                   (setq gnus-newsgroup-unreads
5386                         (sort gnus-newsgroup-unreads '<)))
5387                  t)))
5388         (unless (listp (cdr gnus-newsgroup-killed))
5389           (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
5390         (let ((headers gnus-newsgroup-headers))
5391           ;; Set the new ranges of read articles.
5392           (save-excursion
5393             (set-buffer gnus-group-buffer)
5394             (gnus-undo-force-boundary))
5395           (gnus-update-read-articles
5396            group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
5397           ;; Set the current article marks.
5398           (let ((gnus-newsgroup-scored
5399                  (if (and (not gnus-save-score)
5400                           (not non-destructive))
5401                      nil
5402                    gnus-newsgroup-scored)))
5403             (save-excursion
5404               (gnus-update-marks)))
5405           ;; Do the cross-ref thing.
5406           (when gnus-use-cross-reference
5407             (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
5408           ;; Do not switch windows but change the buffer to work.
5409           (set-buffer gnus-group-buffer)
5410           (unless (gnus-ephemeral-group-p group)
5411             (gnus-group-update-group group)))))))
5412
5413 (defun gnus-summary-save-newsrc (&optional force)
5414   "Save the current number of read/marked articles in the dribble buffer.
5415 The dribble buffer will then be saved.
5416 If FORCE (the prefix), also save the .newsrc file(s)."
5417   (interactive "P")
5418   (gnus-summary-update-info t)
5419   (if force
5420       (gnus-save-newsrc-file)
5421     (gnus-dribble-save)))
5422
5423 (defun gnus-summary-exit (&optional temporary)
5424   "Exit reading current newsgroup, and then return to group selection mode.
5425 `gnus-exit-group-hook' is called with no arguments if that value is non-nil."
5426   (interactive)
5427   (gnus-set-global-variables)
5428   (gnus-kill-save-kill-buffer)
5429   (gnus-async-halt-prefetch)
5430   (let* ((group gnus-newsgroup-name)
5431          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
5432          (mode major-mode)
5433          (group-point nil)
5434          (buf (current-buffer)))
5435     (unless quit-config
5436       ;; Do adaptive scoring, and possibly save score files.
5437       (when gnus-newsgroup-adaptive
5438         (gnus-score-adaptive))
5439       (when gnus-use-scoring
5440         (gnus-score-save)))
5441     (gnus-run-hooks 'gnus-summary-prepare-exit-hook)
5442     ;; If we have several article buffers, we kill them at exit.
5443     (unless gnus-single-article-buffer
5444       (gnus-kill-buffer gnus-original-article-buffer)
5445       (setq gnus-article-current nil))
5446     (when gnus-use-cache
5447       (gnus-cache-possibly-remove-articles)
5448       (gnus-cache-save-buffers))
5449     (gnus-async-prefetch-remove-group group)
5450     (when gnus-suppress-duplicates
5451       (gnus-dup-enter-articles))
5452     (when gnus-use-trees
5453       (gnus-tree-close group))
5454     (when gnus-use-cache
5455       (gnus-cache-write-active))
5456     ;; Remove entries for this group.
5457     (nnmail-purge-split-history (gnus-group-real-name group))
5458     ;; Make all changes in this group permanent.
5459     (unless quit-config
5460       (gnus-run-hooks 'gnus-exit-group-hook)
5461       (gnus-summary-update-info))
5462     (gnus-close-group group)
5463     ;; Make sure where we were, and go to next newsgroup.
5464     (set-buffer gnus-group-buffer)
5465     (unless quit-config
5466       (gnus-group-jump-to-group group))
5467     (gnus-run-hooks 'gnus-summary-exit-hook)
5468     (unless (or quit-config
5469                 ;; If this group has disappeared from the summary
5470                 ;; buffer, don't skip forwards.
5471                 (not (string= group (gnus-group-group-name))))
5472       (gnus-group-next-unread-group 1))
5473     (setq group-point (point))
5474     (if temporary
5475         nil                             ;Nothing to do.
5476       ;; If we have several article buffers, we kill them at exit.
5477       (unless gnus-single-article-buffer
5478         (gnus-kill-buffer gnus-article-buffer)
5479         (gnus-kill-buffer gnus-original-article-buffer)
5480         (setq gnus-article-current nil))
5481       (set-buffer buf)
5482       (if (not gnus-kill-summary-on-exit)
5483           (gnus-deaden-summary)
5484         ;; We set all buffer-local variables to nil.  It is unclear why
5485         ;; this is needed, but if we don't, buffer-local variables are
5486         ;; not garbage-collected, it seems.  This would the lead to en
5487         ;; ever-growing Emacs.
5488         (gnus-summary-clear-local-variables)
5489         (when (get-buffer gnus-article-buffer)
5490           (bury-buffer gnus-article-buffer))
5491         ;; We clear the global counterparts of the buffer-local
5492         ;; variables as well, just to be on the safe side.
5493         (set-buffer gnus-group-buffer)
5494         (gnus-summary-clear-local-variables)
5495         ;; Return to group mode buffer.
5496         (when (eq mode 'gnus-summary-mode)
5497           (gnus-kill-buffer buf)))
5498       (setq gnus-current-select-method gnus-select-method)
5499       (pop-to-buffer gnus-group-buffer)
5500       (if (not quit-config)
5501           (progn
5502             (goto-char group-point)
5503             (gnus-configure-windows 'group 'force)
5504             (unless (pos-visible-in-window-p)
5505               (forward-line (/ (static-if (featurep 'xemacs)
5506                                    (window-displayed-height)
5507                                  (1- (window-height)))
5508                                -2))
5509               (set-window-start (selected-window) (point))
5510               (goto-char group-point)))
5511         (gnus-handle-ephemeral-exit quit-config))
5512       ;; Clear the current group name.
5513       (unless quit-config
5514         (setq gnus-newsgroup-name nil)))))
5515
5516 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
5517 (defun gnus-summary-exit-no-update (&optional no-questions)
5518   "Quit reading current newsgroup without updating read article info."
5519   (interactive)
5520   (let* ((group gnus-newsgroup-name)
5521          (quit-config (gnus-group-quit-config group)))
5522     (when (or no-questions
5523               gnus-expert-user
5524               (gnus-y-or-n-p "Discard changes to this group and exit? "))
5525       (gnus-async-halt-prefetch)
5526       (mapcar 'funcall
5527               (delq 'gnus-summary-expire-articles
5528                     (copy-sequence gnus-summary-prepare-exit-hook)))
5529       ;; If we have several article buffers, we kill them at exit.
5530       (unless gnus-single-article-buffer
5531         (gnus-kill-buffer gnus-article-buffer)
5532         (gnus-kill-buffer gnus-original-article-buffer)
5533         (setq gnus-article-current nil))
5534       (if (not gnus-kill-summary-on-exit)
5535           (gnus-deaden-summary)
5536         (gnus-close-group group)
5537         (gnus-summary-clear-local-variables)
5538         (set-buffer gnus-group-buffer)
5539         (gnus-summary-clear-local-variables)
5540         (when (get-buffer gnus-summary-buffer)
5541           (kill-buffer gnus-summary-buffer)))
5542       (unless gnus-single-article-buffer
5543         (setq gnus-article-current nil))
5544       (when gnus-use-trees
5545         (gnus-tree-close group))
5546       (gnus-async-prefetch-remove-group group)
5547       (when (get-buffer gnus-article-buffer)
5548         (bury-buffer gnus-article-buffer))
5549       ;; Return to the group buffer.
5550       (gnus-configure-windows 'group 'force)
5551       ;; Clear the current group name.
5552       (setq gnus-newsgroup-name nil)
5553       (when (equal (gnus-group-group-name) group)
5554         (gnus-group-next-unread-group 1))
5555       (when quit-config
5556         (gnus-handle-ephemeral-exit quit-config)))))
5557
5558 (defun gnus-handle-ephemeral-exit (quit-config)
5559   "Handle movement when leaving an ephemeral group.
5560 The state which existed when entering the ephemeral is reset."
5561   (if (not (buffer-name (car quit-config)))
5562       (gnus-configure-windows 'group 'force)
5563     (set-buffer (car quit-config))
5564     (cond ((eq major-mode 'gnus-summary-mode)
5565            (gnus-set-global-variables))
5566           ((eq major-mode 'gnus-article-mode)
5567            (save-excursion
5568              ;; The `gnus-summary-buffer' variable may point
5569              ;; to the old summary buffer when using a single
5570              ;; article buffer.
5571              (unless (gnus-buffer-live-p gnus-summary-buffer)
5572                (set-buffer gnus-group-buffer))
5573              (set-buffer gnus-summary-buffer)
5574              (gnus-set-global-variables))))
5575     (if (or (eq (cdr quit-config) 'article)
5576             (eq (cdr quit-config) 'pick))
5577         (progn
5578           ;; The current article may be from the ephemeral group
5579           ;; thus it is best that we reload this article
5580           (gnus-summary-show-article)
5581           (if (and (boundp 'gnus-pick-mode) (symbol-value 'gnus-pick-mode))
5582               (gnus-configure-windows 'pick 'force)
5583             (gnus-configure-windows (cdr quit-config) 'force)))
5584       (gnus-configure-windows (cdr quit-config) 'force))
5585     (when (eq major-mode 'gnus-summary-mode)
5586       (gnus-summary-next-subject 1 nil t)
5587       (gnus-summary-recenter)
5588       (gnus-summary-position-point))))
5589
5590 (defun gnus-summary-preview-mime-message ()
5591   "MIME decode and play this message."
5592   (interactive)
5593   (let ((gnus-break-pages nil)
5594         (gnus-show-mime t))
5595     (gnus-summary-select-article gnus-show-all-headers t))
5596   (select-window (get-buffer-window gnus-article-buffer)))
5597
5598 ;;; Dead summaries.
5599
5600 (defvar gnus-dead-summary-mode-map nil)
5601
5602 (unless gnus-dead-summary-mode-map
5603   (setq gnus-dead-summary-mode-map (make-keymap))
5604   (suppress-keymap gnus-dead-summary-mode-map)
5605   (substitute-key-definition
5606    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
5607   (let ((keys '("\C-d" "\r" "\177" [delete])))
5608     (while keys
5609       (define-key gnus-dead-summary-mode-map
5610         (pop keys) 'gnus-summary-wake-up-the-dead))))
5611
5612 (defvar gnus-dead-summary-mode nil
5613   "Minor mode for Gnus summary buffers.")
5614
5615 (defun gnus-dead-summary-mode (&optional arg)
5616   "Minor mode for Gnus summary buffers."
5617   (interactive "P")
5618   (when (eq major-mode 'gnus-summary-mode)
5619     (make-local-variable 'gnus-dead-summary-mode)
5620     (setq gnus-dead-summary-mode
5621           (if (null arg) (not gnus-dead-summary-mode)
5622             (> (prefix-numeric-value arg) 0)))
5623     (when gnus-dead-summary-mode
5624       (gnus-add-minor-mode
5625        'gnus-dead-summary-mode " Dead" gnus-dead-summary-mode-map))))
5626
5627 (defun gnus-deaden-summary ()
5628   "Make the current summary buffer into a dead summary buffer."
5629   ;; Kill any previous dead summary buffer.
5630   (when (and gnus-dead-summary
5631              (buffer-name gnus-dead-summary))
5632     (save-excursion
5633       (set-buffer gnus-dead-summary)
5634       (when gnus-dead-summary-mode
5635         (kill-buffer (current-buffer)))))
5636   ;; Make this the current dead summary.
5637   (setq gnus-dead-summary (current-buffer))
5638   (gnus-dead-summary-mode 1)
5639   (let ((name (buffer-name)))
5640     (when (string-match "Summary" name)
5641       (rename-buffer
5642        (concat (substring name 0 (match-beginning 0)) "Dead "
5643                (substring name (match-beginning 0)))
5644        t)
5645       (bury-buffer))))
5646
5647 (defun gnus-kill-or-deaden-summary (buffer)
5648   "Kill or deaden the summary BUFFER."
5649   (save-excursion
5650     (when (and (buffer-name buffer)
5651                (not gnus-single-article-buffer))
5652       (save-excursion
5653         (set-buffer buffer)
5654         (gnus-kill-buffer gnus-article-buffer)
5655         (gnus-kill-buffer gnus-original-article-buffer)))
5656     (cond (gnus-kill-summary-on-exit
5657            (when (and gnus-use-trees
5658                       (gnus-buffer-exists-p buffer))
5659              (save-excursion
5660                (set-buffer buffer)
5661                (gnus-tree-close gnus-newsgroup-name)))
5662            (gnus-kill-buffer buffer))
5663           ((gnus-buffer-exists-p buffer)
5664            (save-excursion
5665              (set-buffer buffer)
5666              (gnus-deaden-summary))))))
5667
5668 (defun gnus-summary-wake-up-the-dead (&rest args)
5669   "Wake up the dead summary buffer."
5670   (interactive)
5671   (gnus-dead-summary-mode -1)
5672   (let ((name (buffer-name)))
5673     (when (string-match "Dead " name)
5674       (rename-buffer
5675        (concat (substring name 0 (match-beginning 0))
5676                (substring name (match-end 0)))
5677        t)))
5678   (gnus-message 3 "This dead summary is now alive again"))
5679
5680 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
5681 (defun gnus-summary-fetch-faq (&optional faq-dir)
5682   "Fetch the FAQ for the current group.
5683 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
5684 in."
5685   (interactive
5686    (list
5687     (when current-prefix-arg
5688       (completing-read
5689        "Faq dir: " (and (listp gnus-group-faq-directory)
5690                         (mapcar (lambda (file) (list file))
5691                                 gnus-group-faq-directory))))))
5692   (let (gnus-faq-buffer)
5693     (when (setq gnus-faq-buffer
5694                 (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
5695       (gnus-configure-windows 'summary-faq))))
5696
5697 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
5698 (defun gnus-summary-describe-group (&optional force)
5699   "Describe the current newsgroup."
5700   (interactive "P")
5701   (gnus-group-describe-group force gnus-newsgroup-name))
5702
5703 (defun gnus-summary-describe-briefly ()
5704   "Describe summary mode commands briefly."
5705   (interactive)
5706   (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")))
5707
5708 ;; Walking around group mode buffer from summary mode.
5709
5710 (defun gnus-summary-next-group (&optional no-article target-group backward)
5711   "Exit current newsgroup and then select next unread newsgroup.
5712 If prefix argument NO-ARTICLE is non-nil, no article is selected
5713 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
5714 previous group instead."
5715   (interactive "P")
5716   ;; Stop pre-fetching.
5717   (gnus-async-halt-prefetch)
5718   (let ((current-group gnus-newsgroup-name)
5719         (current-buffer (current-buffer))
5720         entered)
5721     ;; First we semi-exit this group to update Xrefs and all variables.
5722     ;; We can't do a real exit, because the window conf must remain
5723     ;; the same in case the user is prompted for info, and we don't
5724     ;; want the window conf to change before that...
5725     (gnus-summary-exit t)
5726     (while (not entered)
5727       ;; Then we find what group we are supposed to enter.
5728       (set-buffer gnus-group-buffer)
5729       (gnus-group-jump-to-group current-group)
5730       (setq target-group
5731             (or target-group
5732                 (if (eq gnus-keep-same-level 'best)
5733                     (gnus-summary-best-group gnus-newsgroup-name)
5734                   (gnus-summary-search-group backward gnus-keep-same-level))))
5735       (if (not target-group)
5736           ;; There are no further groups, so we return to the group
5737           ;; buffer.
5738           (progn
5739             (gnus-message 5 "Returning to the group buffer")
5740             (setq entered t)
5741             (when (gnus-buffer-live-p current-buffer)
5742               (set-buffer current-buffer)
5743               (gnus-summary-exit))
5744             (gnus-run-hooks 'gnus-group-no-more-groups-hook))
5745         ;; We try to enter the target group.
5746         (gnus-group-jump-to-group target-group)
5747         (let ((unreads (gnus-group-group-unread)))
5748           (if (and (or (eq t unreads)
5749                        (and unreads (not (zerop unreads))))
5750                    (gnus-summary-read-group
5751                     target-group nil no-article
5752                     (and (buffer-name current-buffer) current-buffer)
5753                     nil backward))
5754               (setq entered t)
5755             (setq current-group target-group
5756                   target-group nil)))))))
5757
5758 (defun gnus-summary-prev-group (&optional no-article)
5759   "Exit current newsgroup and then select previous unread newsgroup.
5760 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
5761   (interactive "P")
5762   (gnus-summary-next-group no-article nil t))
5763
5764 ;; Walking around summary lines.
5765
5766 (defun gnus-summary-first-subject (&optional unread undownloaded)
5767   "Go to the first unread subject.
5768 If UNREAD is non-nil, go to the first unread article.
5769 Returns the article selected or nil if there are no unread articles."
5770   (interactive "P")
5771   (prog1
5772       (cond
5773        ;; Empty summary.
5774        ((null gnus-newsgroup-data)
5775         (gnus-message 3 "No articles in the group")
5776         nil)
5777        ;; Pick the first article.
5778        ((not unread)
5779         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
5780         (gnus-data-number (car gnus-newsgroup-data)))
5781        ;; No unread articles.
5782        ((null gnus-newsgroup-unreads)
5783         (gnus-message 3 "No more unread articles")
5784         nil)
5785        ;; Find the first unread article.
5786        (t
5787         (let ((data gnus-newsgroup-data))
5788           (while (and data
5789                       (and (not (and undownloaded
5790                                      (eq gnus-undownloaded-mark
5791                                          (gnus-data-mark (car data)))))
5792                            (not (gnus-data-unread-p (car data)))))
5793             (setq data (cdr data)))
5794           (when data
5795             (goto-char (gnus-data-pos (car data)))
5796             (gnus-data-number (car data))))))
5797     (gnus-summary-position-point)))
5798
5799 (defun gnus-summary-next-subject (n &optional unread dont-display)
5800   "Go to next N'th summary line.
5801 If N is negative, go to the previous N'th subject line.
5802 If UNREAD is non-nil, only unread articles are selected.
5803 The difference between N and the actual number of steps taken is
5804 returned."
5805   (interactive "p")
5806   (let ((backward (< n 0))
5807         (n (abs n)))
5808     (while (and (> n 0)
5809                 (if backward
5810                     (gnus-summary-find-prev unread)
5811                   (gnus-summary-find-next unread)))
5812       (unless (zerop (setq n (1- n)))
5813         (gnus-summary-show-thread)))
5814     (when (/= 0 n)
5815       (gnus-message 7 "No more%s articles"
5816                     (if unread " unread" "")))
5817     (unless dont-display
5818       (gnus-summary-recenter)
5819       (gnus-summary-position-point))
5820     n))
5821
5822 (defun gnus-summary-next-unread-subject (n)
5823   "Go to next N'th unread summary line."
5824   (interactive "p")
5825   (gnus-summary-next-subject n t))
5826
5827 (defun gnus-summary-prev-subject (n &optional unread)
5828   "Go to previous N'th summary line.
5829 If optional argument UNREAD is non-nil, only unread article is selected."
5830   (interactive "p")
5831   (gnus-summary-next-subject (- n) unread))
5832
5833 (defun gnus-summary-prev-unread-subject (n)
5834   "Go to previous N'th unread summary line."
5835   (interactive "p")
5836   (gnus-summary-next-subject (- n) t))
5837
5838 (defun gnus-summary-goto-subject (article &optional force silent)
5839   "Go the subject line of ARTICLE.
5840 If FORCE, also allow jumping to articles not currently shown."
5841   (interactive "nArticle number: ")
5842   (let ((b (point))
5843         (data (gnus-data-find article)))
5844     ;; We read in the article if we have to.
5845     (and (not data)
5846          force
5847          (gnus-summary-insert-subject
5848           article
5849           (if (or (numberp force) (vectorp force)) force)
5850           t)
5851          (setq data (gnus-data-find article)))
5852     (goto-char b)
5853     (if (not data)
5854         (progn
5855           (unless silent
5856             (gnus-message 3 "Can't find article %d" article))
5857           nil)
5858       (goto-char (gnus-data-pos data))
5859       (gnus-summary-position-point)
5860       article)))
5861
5862 ;; Walking around summary lines with displaying articles.
5863
5864 (defun gnus-summary-expand-window (&optional arg)
5865   "Make the summary buffer take up the entire Emacs frame.
5866 Given a prefix, will force an `article' buffer configuration."
5867   (interactive "P")
5868   (if arg
5869       (gnus-configure-windows 'article 'force)
5870     (gnus-configure-windows 'summary 'force)))
5871
5872 (defun gnus-summary-display-article (article &optional all-header)
5873   "Display ARTICLE in article buffer."
5874   (gnus-set-global-variables)
5875   (if (null article)
5876       nil
5877     (prog1
5878         (if gnus-summary-display-article-function
5879             (funcall gnus-summary-display-article-function article all-header)
5880           (gnus-article-prepare article all-header))
5881       (with-current-buffer gnus-article-buffer
5882         (set (make-local-variable 'gnus-summary-search-article-matched-data)
5883              nil))
5884       (gnus-run-hooks 'gnus-select-article-hook)
5885       (when (and gnus-current-article
5886                  (not (zerop gnus-current-article)))
5887         (gnus-summary-goto-subject gnus-current-article))
5888       (gnus-summary-recenter)
5889       (when (and gnus-use-trees gnus-show-threads)
5890         (gnus-possibly-generate-tree article)
5891         (gnus-highlight-selected-tree article))
5892       ;; Successfully display article.
5893       (gnus-article-set-window-start
5894        (cdr (assq article gnus-newsgroup-bookmarks))))))
5895
5896 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
5897   "Select the current article.
5898 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
5899 non-nil, the article will be re-fetched even if it already present in
5900 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
5901 be displayed."
5902   ;; Make sure we are in the summary buffer to work around bbdb bug.
5903   (unless (eq major-mode 'gnus-summary-mode)
5904     (set-buffer gnus-summary-buffer))
5905   (let ((article (or article (gnus-summary-article-number)))
5906         (all-headers (not (not all-headers))) ;Must be T or NIL.
5907         gnus-summary-display-article-function)
5908     (and (not pseudo)
5909          (gnus-summary-article-pseudo-p article)
5910          (error "This is a pseudo-article"))
5911     (save-excursion
5912       (set-buffer gnus-summary-buffer)
5913       (if (or (and gnus-single-article-buffer
5914                    (or (null gnus-current-article)
5915                        (null gnus-article-current)
5916                        (null (get-buffer gnus-article-buffer))
5917                        (not (eq article (cdr gnus-article-current)))
5918                        (not (equal (car gnus-article-current)
5919                                    gnus-newsgroup-name))))
5920               (and (not gnus-single-article-buffer)
5921                    (or (null gnus-current-article)
5922                        (not (eq gnus-current-article article))))
5923               force)
5924           ;; The requested article is different from the current article.
5925           (progn
5926             (gnus-summary-display-article article all-headers)
5927             (when (or all-headers gnus-show-all-headers)
5928               (gnus-article-show-all-headers))
5929             (gnus-article-set-window-start
5930              (cdr (assq article gnus-newsgroup-bookmarks)))
5931             article)
5932         (when (or all-headers gnus-show-all-headers)
5933           (gnus-article-show-all-headers))
5934         'old))))
5935
5936 (defun gnus-summary-set-current-mark (&optional current-mark)
5937   "Obsolete function."
5938   nil)
5939
5940 (defun gnus-summary-next-article (&optional unread subject backward push)
5941   "Select the next article.
5942 If UNREAD, only unread articles are selected.
5943 If SUBJECT, only articles with SUBJECT are selected.
5944 If BACKWARD, the previous article is selected instead of the next."
5945   (interactive "P")
5946   (cond
5947    ;; Is there such an article?
5948    ((and (gnus-summary-search-forward unread subject backward)
5949          (or (gnus-summary-display-article (gnus-summary-article-number))
5950              (eq (gnus-summary-article-mark) gnus-canceled-mark)))
5951     (gnus-summary-position-point))
5952    ;; If not, we try the first unread, if that is wanted.
5953    ((and subject
5954          gnus-auto-select-same
5955          (gnus-summary-first-unread-article))
5956     (gnus-summary-position-point)
5957     (gnus-message 6 "Wrapped"))
5958    ;; Try to get next/previous article not displayed in this group.
5959    ((and gnus-auto-extend-newsgroup
5960          (not unread) (not subject))
5961     (gnus-summary-goto-article
5962      (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
5963      nil (count-lines (point-min) (point))))
5964    ;; Go to next/previous group.
5965    (t
5966     (unless (gnus-ephemeral-group-p gnus-newsgroup-name)
5967       (gnus-summary-jump-to-group gnus-newsgroup-name))
5968     (let ((cmd last-command-char)
5969           (point
5970            (save-excursion
5971              (set-buffer gnus-group-buffer)
5972              (point)))
5973           (group
5974            (if (eq gnus-keep-same-level 'best)
5975                (gnus-summary-best-group gnus-newsgroup-name)
5976              (gnus-summary-search-group backward gnus-keep-same-level))))
5977       ;; For some reason, the group window gets selected.  We change
5978       ;; it back.
5979       (select-window (get-buffer-window (current-buffer)))
5980       ;; Select next unread newsgroup automagically.
5981       (cond
5982        ((or (not gnus-auto-select-next)
5983             (not cmd))
5984         (gnus-message 7 "No more%s articles" (if unread " unread" "")))
5985        ((or (eq gnus-auto-select-next 'quietly)
5986             (and (eq gnus-auto-select-next 'slightly-quietly)
5987                  push)
5988             (and (eq gnus-auto-select-next 'almost-quietly)
5989                  (gnus-summary-last-article-p)))
5990         ;; Select quietly.
5991         (if (gnus-ephemeral-group-p gnus-newsgroup-name)
5992             (gnus-summary-exit)
5993           (gnus-message 7 "No more%s articles (%s)..."
5994                         (if unread " unread" "")
5995                         (if group (concat "selecting " group)
5996                           "exiting"))
5997           (gnus-summary-next-group nil group backward)))
5998        (t
5999         (when (gnus-key-press-event-p last-input-event)
6000           (gnus-summary-walk-group-buffer
6001            gnus-newsgroup-name cmd unread backward point))))))))
6002
6003 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward start)
6004   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
6005                       (?\C-p (gnus-group-prev-unread-group 1))))
6006         (cursor-in-echo-area t)
6007         keve key group ended)
6008     (save-excursion
6009       (set-buffer gnus-group-buffer)
6010       (goto-char start)
6011       (setq group
6012             (if (eq gnus-keep-same-level 'best)
6013                 (gnus-summary-best-group gnus-newsgroup-name)
6014               (gnus-summary-search-group backward gnus-keep-same-level))))
6015     (while (not ended)
6016       (gnus-message
6017        5 "No more%s articles%s" (if unread " unread" "")
6018        (if (and group
6019                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
6020            (format " (Type %s for %s [%s])"
6021                    (single-key-description cmd) group
6022                    (car (gnus-gethash group gnus-newsrc-hashtb)))
6023          (format " (Type %s to exit %s)"
6024                  (single-key-description cmd)
6025                  gnus-newsgroup-name)))
6026       ;; Confirm auto selection.
6027       (setq key (car (setq keve (gnus-read-event-char))))
6028       (setq ended t)
6029       (cond
6030        ((assq key keystrokes)
6031         (let ((obuf (current-buffer)))
6032           (switch-to-buffer gnus-group-buffer)
6033           (when group
6034             (gnus-group-jump-to-group group))
6035           (eval (cadr (assq key keystrokes)))
6036           (setq group (gnus-group-group-name))
6037           (switch-to-buffer obuf))
6038         (setq ended nil))
6039        ((equal key cmd)
6040         (if (or (not group)
6041                 (gnus-ephemeral-group-p gnus-newsgroup-name))
6042             (gnus-summary-exit)
6043           (gnus-summary-next-group nil group backward)))
6044        (t
6045         (push (cdr keve) unread-command-events))))))
6046
6047 (defun gnus-summary-next-unread-article ()
6048   "Select unread article after current one."
6049   (interactive)
6050   (gnus-summary-next-article
6051    (or (not (eq gnus-summary-goto-unread 'never))
6052        (gnus-summary-last-article-p (gnus-summary-article-number)))
6053    (and gnus-auto-select-same
6054         (gnus-summary-article-subject))))
6055
6056 (defun gnus-summary-prev-article (&optional unread subject)
6057   "Select the article after the current one.
6058 If UNREAD is non-nil, only unread articles are selected."
6059   (interactive "P")
6060   (gnus-summary-next-article unread subject t))
6061
6062 (defun gnus-summary-prev-unread-article ()
6063   "Select unread article before current one."
6064   (interactive)
6065   (gnus-summary-prev-article
6066    (or (not (eq gnus-summary-goto-unread 'never))
6067        (gnus-summary-first-article-p (gnus-summary-article-number)))
6068    (and gnus-auto-select-same
6069         (gnus-summary-article-subject))))
6070
6071 (defun gnus-summary-next-page (&optional lines circular)
6072   "Show next page of the selected article.
6073 If at the end of the current article, select the next article.
6074 LINES says how many lines should be scrolled up.
6075
6076 If CIRCULAR is non-nil, go to the start of the article instead of
6077 selecting the next article when reaching the end of the current
6078 article."
6079   (interactive "P")
6080   (setq gnus-summary-buffer (current-buffer))
6081   (gnus-set-global-variables)
6082   (let ((article (gnus-summary-article-number))
6083         (article-window (get-buffer-window gnus-article-buffer t))
6084         endp)
6085     ;; If the buffer is empty, we have no article.
6086     (unless article
6087       (error "No article to select"))
6088     (gnus-configure-windows 'article)
6089     (if (eq (cdr (assq article gnus-newsgroup-reads)) gnus-canceled-mark)
6090         (if (and (eq gnus-summary-goto-unread 'never)
6091                  (not (gnus-summary-last-article-p article)))
6092             (gnus-summary-next-article)
6093           (gnus-summary-next-unread-article))
6094       (if (or (null gnus-current-article)
6095               (null gnus-article-current)
6096               (/= article (cdr gnus-article-current))
6097               (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6098           ;; Selected subject is different from current article's.
6099           (gnus-summary-display-article article)
6100         (when article-window
6101           (gnus-eval-in-buffer-window gnus-article-buffer
6102             (setq endp (gnus-article-next-page lines)))
6103           (when endp
6104             (cond (circular
6105                    (gnus-summary-beginning-of-article))
6106                   (lines
6107                    (gnus-message 3 "End of message"))
6108                   ((null lines)
6109                    (if (and (eq gnus-summary-goto-unread 'never)
6110                             (not (gnus-summary-last-article-p article)))
6111                        (gnus-summary-next-article)
6112                      (gnus-summary-next-unread-article))))))))
6113     (gnus-summary-recenter)
6114     (gnus-summary-position-point)))
6115
6116 (defun gnus-summary-prev-page (&optional lines move)
6117   "Show previous page of selected article.
6118 Argument LINES specifies lines to be scrolled down.
6119 If MOVE, move to the previous unread article if point is at
6120 the beginning of the buffer."
6121   (interactive "P")
6122   (let ((article (gnus-summary-article-number))
6123         (article-window (get-buffer-window gnus-article-buffer t))
6124         endp)
6125     (gnus-configure-windows 'article)
6126     (if (or (null gnus-current-article)
6127             (null gnus-article-current)
6128             (/= article (cdr gnus-article-current))
6129             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
6130         ;; Selected subject is different from current article's.
6131         (gnus-summary-display-article article)
6132       (gnus-summary-recenter)
6133       (when article-window
6134         (gnus-eval-in-buffer-window gnus-article-buffer
6135           (setq endp (gnus-article-prev-page lines)))
6136         (when (and move endp)
6137           (cond (lines
6138                  (gnus-message 3 "Beginning of message"))
6139                 ((null lines)
6140                  (if (and (eq gnus-summary-goto-unread 'never)
6141                           (not (gnus-summary-first-article-p article)))
6142                      (gnus-summary-prev-article)
6143                    (gnus-summary-prev-unread-article))))))))
6144   (gnus-summary-position-point))
6145
6146 (defun gnus-summary-prev-page-or-article (&optional lines)
6147   "Show previous page of selected article.
6148 Argument LINES specifies lines to be scrolled down.
6149 If at the beginning of the article, go to the next article."
6150   (interactive "P")
6151   (gnus-summary-prev-page lines t))
6152
6153 (defun gnus-summary-scroll-up (lines)
6154   "Scroll up (or down) one line current article.
6155 Argument LINES specifies lines to be scrolled up (or down if negative)."
6156   (interactive "p")
6157   (gnus-configure-windows 'article)
6158   (gnus-summary-show-thread)
6159   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
6160     (gnus-eval-in-buffer-window gnus-article-buffer
6161       (cond ((> lines 0)
6162              (when (gnus-article-next-page lines)
6163                (gnus-message 3 "End of message")))
6164             ((< lines 0)
6165              (gnus-article-prev-page (- lines))))))
6166   (gnus-summary-recenter)
6167   (gnus-summary-position-point))
6168
6169 (defun gnus-summary-scroll-down (lines)
6170   "Scroll down (or up) one line current article.
6171 Argument LINES specifies lines to be scrolled down (or up if negative)."
6172   (interactive "p")
6173   (gnus-summary-scroll-up (- lines)))
6174
6175 (defun gnus-summary-next-same-subject ()
6176   "Select next article which has the same subject as current one."
6177   (interactive)
6178   (gnus-summary-next-article nil (gnus-summary-article-subject)))
6179
6180 (defun gnus-summary-prev-same-subject ()
6181   "Select previous article which has the same subject as current one."
6182   (interactive)
6183   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
6184
6185 (defun gnus-summary-next-unread-same-subject ()
6186   "Select next unread article which has the same subject as current one."
6187   (interactive)
6188   (gnus-summary-next-article t (gnus-summary-article-subject)))
6189
6190 (defun gnus-summary-prev-unread-same-subject ()
6191   "Select previous unread article which has the same subject as current one."
6192   (interactive)
6193   (gnus-summary-prev-article t (gnus-summary-article-subject)))
6194
6195 (defun gnus-summary-first-unread-article ()
6196   "Select the first unread article.
6197 Return nil if there are no unread articles."
6198   (interactive)
6199   (prog1
6200       (when (gnus-summary-first-subject t)
6201         (gnus-summary-show-thread)
6202         (gnus-summary-first-subject t)
6203         (gnus-summary-display-article (gnus-summary-article-number)))
6204     (gnus-summary-position-point)))
6205
6206 (defun gnus-summary-first-unread-subject ()
6207   "Place the point on the subject line of the first unread article.
6208 Return nil if there are no unread articles."
6209   (interactive)
6210   (prog1
6211       (when (gnus-summary-first-subject t)
6212         (gnus-summary-show-thread)
6213         (gnus-summary-first-subject t))
6214     (gnus-summary-position-point)))
6215
6216 (defun gnus-summary-first-article ()
6217   "Select the first article.
6218 Return nil if there are no articles."
6219   (interactive)
6220   (prog1
6221       (when (gnus-summary-first-subject)
6222         (gnus-summary-show-thread)
6223         (gnus-summary-first-subject)
6224         (gnus-summary-display-article (gnus-summary-article-number)))
6225     (gnus-summary-position-point)))
6226
6227 (defun gnus-summary-best-unread-article ()
6228   "Select the unread article with the highest score."
6229   (interactive)
6230   (let ((best -1000000)
6231         (data gnus-newsgroup-data)
6232         article score)
6233     (while data
6234       (and (gnus-data-unread-p (car data))
6235            (> (setq score
6236                     (gnus-summary-article-score (gnus-data-number (car data))))
6237               best)
6238            (setq best score
6239                  article (gnus-data-number (car data))))
6240       (setq data (cdr data)))
6241     (prog1
6242         (if article
6243             (gnus-summary-goto-article article)
6244           (error "No unread articles"))
6245       (gnus-summary-position-point))))
6246
6247 (defun gnus-summary-last-subject ()
6248   "Go to the last displayed subject line in the group."
6249   (let ((article (gnus-data-number (car (gnus-data-list t)))))
6250     (when article
6251       (gnus-summary-goto-subject article))))
6252
6253 (defun gnus-summary-goto-article (article &optional all-headers force)
6254   "Fetch ARTICLE (article number or Message-ID) and display it if it exists.
6255 If ALL-HEADERS is non-nil, no header lines are hidden.
6256 If FORCE, go to the article even if it isn't displayed.  If FORCE
6257 is a number, it is the line the article is to be displayed on."
6258   (interactive
6259    (list
6260     (completing-read
6261      "Article number or Message-ID: "
6262      (mapcar (lambda (number) (list (int-to-string number)))
6263              gnus-newsgroup-limit))
6264     current-prefix-arg
6265     t))
6266   (prog1
6267       (if (and (stringp article)
6268                (string-match "@" article))
6269           (gnus-summary-refer-article article)
6270         (when (stringp article)
6271           (setq article (string-to-number article)))
6272         (if (gnus-summary-goto-subject article force)
6273             (gnus-summary-display-article article all-headers)
6274           (gnus-message 4 "Couldn't go to article %s" article) nil))
6275     (gnus-summary-position-point)))
6276
6277 (defun gnus-summary-goto-last-article ()
6278   "Go to the previously read article."
6279   (interactive)
6280   (prog1
6281       (when gnus-last-article
6282         (gnus-summary-goto-article gnus-last-article nil t))
6283     (gnus-summary-position-point)))
6284
6285 (defun gnus-summary-pop-article (number)
6286   "Pop one article off the history and go to the previous.
6287 NUMBER articles will be popped off."
6288   (interactive "p")
6289   (let (to)
6290     (setq gnus-newsgroup-history
6291           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
6292     (if to
6293         (gnus-summary-goto-article (car to) nil t)
6294       (error "Article history empty")))
6295   (gnus-summary-position-point))
6296
6297 ;; Summary commands and functions for limiting the summary buffer.
6298
6299 (defun gnus-summary-limit-to-articles (n)
6300   "Limit the summary buffer to the next N articles.
6301 If not given a prefix, use the process marked articles instead."
6302   (interactive "P")
6303   (prog1
6304       (let ((articles (gnus-summary-work-articles n)))
6305         (setq gnus-newsgroup-processable nil)
6306         (gnus-summary-limit articles))
6307     (gnus-summary-position-point)))
6308
6309 (defun gnus-summary-pop-limit (&optional total)
6310   "Restore the previous limit.
6311 If given a prefix, remove all limits."
6312   (interactive "P")
6313   (when total
6314     (setq gnus-newsgroup-limits
6315           (list (mapcar (lambda (h) (mail-header-number h))
6316                         gnus-newsgroup-headers))))
6317   (unless gnus-newsgroup-limits
6318     (error "No limit to pop"))
6319   (prog1
6320       (gnus-summary-limit nil 'pop)
6321     (gnus-summary-position-point)))
6322
6323 (defun gnus-summary-limit-to-subject (subject &optional header)
6324   "Limit the summary buffer to articles that have subjects that match a regexp."
6325   (interactive "sLimit to subject (regexp): ")
6326   (unless header
6327     (setq header "subject"))
6328   (when (not (equal "" subject))
6329     (prog1
6330         (let ((articles (gnus-summary-find-matching
6331                          (or header "subject") subject 'all)))
6332           (unless articles
6333             (error "Found no matches for \"%s\"" subject))
6334           (gnus-summary-limit articles))
6335       (gnus-summary-position-point))))
6336
6337 (defun gnus-summary-limit-to-author (from)
6338   "Limit the summary buffer to articles that have authors that match a regexp."
6339   (interactive "sLimit to author (regexp): ")
6340   (gnus-summary-limit-to-subject from "from"))
6341
6342 (defun gnus-summary-limit-to-age (age &optional younger-p)
6343   "Limit the summary buffer to articles that are older than (or equal) AGE days.
6344 If YOUNGER-P (the prefix) is non-nil, limit the summary buffer to
6345 articles that are younger than AGE days."
6346   (interactive
6347    (let ((younger current-prefix-arg)
6348          (days-got nil)
6349          days)
6350      (while (not days-got)
6351        (setq days (if younger
6352                       (read-string "Limit to articles within (in days): ")
6353                     (read-string "Limit to articles old than (in days): ")))
6354        (when (> (length days) 0)
6355          (setq days (read days)))
6356        (if (numberp days)
6357            (setq days-got t)
6358          (message "Please enter a number.")
6359          (sleep-for 1)))
6360      (list days younger)))
6361   (prog1
6362       (let ((data gnus-newsgroup-data)
6363             (cutoff (days-to-time age))
6364             articles d date is-younger)
6365         (while (setq d (pop data))
6366           (when (and (vectorp (gnus-data-header d))
6367                      (setq date (mail-header-date (gnus-data-header d))))
6368             (setq is-younger (time-less-p
6369                               (time-since (condition-case ()
6370                                               (date-to-time date)
6371                                             (error '(0 0))))
6372                               cutoff))
6373             (when (if younger-p
6374                       is-younger
6375                     (not is-younger))
6376               (push (gnus-data-number d) articles))))
6377         (gnus-summary-limit (nreverse articles)))
6378     (gnus-summary-position-point)))
6379
6380 (defun gnus-summary-limit-to-extra (header regexp)
6381   "Limit the summary buffer to articles that match an 'extra' header."
6382   (interactive
6383    (let ((header
6384           (intern
6385            (gnus-completing-read
6386             (symbol-name (car gnus-extra-headers))
6387             "Limit extra header:"
6388             (mapcar (lambda (x)
6389                       (cons (symbol-name x) x))
6390                     gnus-extra-headers)
6391             nil
6392             t))))
6393      (list header
6394            (read-string (format "Limit to header %s (regexp): " header)))))
6395   (when (not (equal "" regexp))
6396     (prog1
6397         (let ((articles (gnus-summary-find-matching
6398                          (cons 'extra header) regexp 'all)))
6399           (unless articles
6400             (error "Found no matches for \"%s\"" regexp))
6401           (gnus-summary-limit articles))
6402       (gnus-summary-position-point))))
6403
6404 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6405 (make-obsolete
6406  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
6407
6408 (defun gnus-summary-limit-to-unread (&optional all)
6409   "Limit the summary buffer to articles that are not marked as read.
6410 If ALL is non-nil, limit strictly to unread articles."
6411   (interactive "P")
6412   (if all
6413       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
6414     (gnus-summary-limit-to-marks
6415      ;; Concat all the marks that say that an article is read and have
6416      ;; those removed.
6417      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
6418            gnus-killed-mark gnus-kill-file-mark
6419            gnus-low-score-mark gnus-expirable-mark
6420            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark
6421            gnus-duplicate-mark gnus-souped-mark)
6422      'reverse)))
6423
6424 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-exclude-marks)
6425 (make-obsolete 'gnus-summary-delete-marked-with
6426                'gnus-summary-limit-exlude-marks)
6427
6428 (defun gnus-summary-limit-exclude-marks (marks &optional reverse)
6429   "Exclude articles that are marked with MARKS (e.g. \"DK\").
6430 If REVERSE, limit the summary buffer to articles that are marked
6431 with MARKS.  MARKS can either be a string of marks or a list of marks.
6432 Returns how many articles were removed."
6433   (interactive "sMarks: ")
6434   (gnus-summary-limit-to-marks marks t))
6435
6436 (defun gnus-summary-limit-to-marks (marks &optional reverse)
6437   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
6438 If REVERSE (the prefix), limit the summary buffer to articles that are
6439 not marked with MARKS.  MARKS can either be a string of marks or a
6440 list of marks.
6441 Returns how many articles were removed."
6442   (interactive "sMarks: \nP")
6443   (prog1
6444       (let ((data gnus-newsgroup-data)
6445             (marks (if (listp marks) marks
6446                      (append marks nil))) ; Transform to list.
6447             articles)
6448         (while data
6449           (when (if reverse (not (memq (gnus-data-mark (car data)) marks))
6450                   (memq (gnus-data-mark (car data)) marks))
6451             (push (gnus-data-number (car data)) articles))
6452           (setq data (cdr data)))
6453         (gnus-summary-limit articles))
6454     (gnus-summary-position-point)))
6455
6456 (defun gnus-summary-limit-to-score (&optional score)
6457   "Limit to articles with score at or above SCORE."
6458   (interactive "P")
6459   (setq score (if score
6460                   (prefix-numeric-value score)
6461                 (or gnus-summary-default-score 0)))
6462   (let ((data gnus-newsgroup-data)
6463         articles)
6464     (while data
6465       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
6466                 score)
6467         (push (gnus-data-number (car data)) articles))
6468       (setq data (cdr data)))
6469     (prog1
6470         (gnus-summary-limit articles)
6471       (gnus-summary-position-point))))
6472
6473 (defun gnus-summary-limit-include-thread (id)
6474   "Display all the hidden articles that in the current thread."
6475   (interactive (list (mail-header-id (gnus-summary-article-header))))
6476   (let ((articles (gnus-articles-in-thread
6477                    (gnus-id-to-thread (gnus-root-id id)))))
6478     (prog1
6479         (gnus-summary-limit (nconc articles gnus-newsgroup-limit))
6480       (gnus-summary-position-point))))
6481
6482 (defun gnus-summary-limit-include-dormant ()
6483   "Display all the hidden articles that are marked as dormant.
6484 Note that this command only works on a subset of the articles currently
6485 fetched for this group."
6486   (interactive)
6487   (unless gnus-newsgroup-dormant
6488     (error "There are no dormant articles in this group"))
6489   (prog1
6490       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
6491     (gnus-summary-position-point)))
6492
6493 (defun gnus-summary-limit-exclude-dormant ()
6494   "Hide all dormant articles."
6495   (interactive)
6496   (prog1
6497       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
6498     (gnus-summary-position-point)))
6499
6500 (defun gnus-summary-limit-exclude-childless-dormant ()
6501   "Hide all dormant articles that have no children."
6502   (interactive)
6503   (let ((data (gnus-data-list t))
6504         articles d children)
6505     ;; Find all articles that are either not dormant or have
6506     ;; children.
6507     (while (setq d (pop data))
6508       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
6509                 (and (setq children
6510                            (gnus-article-children (gnus-data-number d)))
6511                      (let (found)
6512                        (while children
6513                          (when (memq (car children) articles)
6514                            (setq children nil
6515                                  found t))
6516                          (pop children))
6517                        found)))
6518         (push (gnus-data-number d) articles)))
6519     ;; Do the limiting.
6520     (prog1
6521         (gnus-summary-limit articles)
6522       (gnus-summary-position-point))))
6523
6524 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
6525   "Mark all unread excluded articles as read.
6526 If ALL, mark even excluded ticked and dormants as read."
6527   (interactive "P")
6528   (let ((articles (gnus-sorted-complement
6529                    (sort
6530                     (mapcar (lambda (h) (mail-header-number h))
6531                             gnus-newsgroup-headers)
6532                     '<)
6533                    (sort gnus-newsgroup-limit '<)))
6534         article)
6535     (setq gnus-newsgroup-unreads
6536           (gnus-intersection gnus-newsgroup-unreads gnus-newsgroup-limit))
6537     (if all
6538         (setq gnus-newsgroup-dormant nil
6539               gnus-newsgroup-marked nil
6540               gnus-newsgroup-reads
6541               (nconc
6542                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
6543                gnus-newsgroup-reads))
6544       (while (setq article (pop articles))
6545         (unless (or (memq article gnus-newsgroup-dormant)
6546                     (memq article gnus-newsgroup-marked))
6547           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
6548
6549 (defun gnus-summary-limit (articles &optional pop)
6550   (if pop
6551       ;; We pop the previous limit off the stack and use that.
6552       (setq articles (car gnus-newsgroup-limits)
6553             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
6554     ;; We use the new limit, so we push the old limit on the stack.
6555     (push gnus-newsgroup-limit gnus-newsgroup-limits))
6556   ;; Set the limit.
6557   (setq gnus-newsgroup-limit articles)
6558   (let ((total (length gnus-newsgroup-data))
6559         (data (gnus-data-find-list (gnus-summary-article-number)))
6560         (gnus-summary-mark-below nil)   ; Inhibit this.
6561         found)
6562     ;; This will do all the work of generating the new summary buffer
6563     ;; according to the new limit.
6564     (gnus-summary-prepare)
6565     ;; Hide any threads, possibly.
6566     (and gnus-show-threads
6567          gnus-thread-hide-subtree
6568          (gnus-summary-hide-all-threads))
6569     ;; Try to return to the article you were at, or one in the
6570     ;; neighborhood.
6571     (when data
6572       ;; We try to find some article after the current one.
6573       (while data
6574         (when (gnus-summary-goto-subject (gnus-data-number (car data)) nil t)
6575           (setq data nil
6576                 found t))
6577         (setq data (cdr data))))
6578     (unless found
6579       ;; If there is no data, that means that we were after the last
6580       ;; article.  The same goes when we can't find any articles
6581       ;; after the current one.
6582       (goto-char (point-max))
6583       (gnus-summary-find-prev))
6584     (gnus-set-mode-line 'summary)
6585     ;; We return how many articles were removed from the summary
6586     ;; buffer as a result of the new limit.
6587     (- total (length gnus-newsgroup-data))))
6588
6589 (defsubst gnus-invisible-cut-children (threads)
6590   (let ((num 0))
6591     (while threads
6592       (when (memq (mail-header-number (caar threads)) gnus-newsgroup-limit)
6593         (incf num))
6594       (pop threads))
6595     (< num 2)))
6596
6597 (defsubst gnus-cut-thread (thread)
6598   "Go forwards in the thread until we find an article that we want to display."
6599   (when (or (eq gnus-fetch-old-headers 'some)
6600             (eq gnus-fetch-old-headers 'invisible)
6601             (numberp gnus-fetch-old-headers)
6602             (eq gnus-build-sparse-threads 'some)
6603             (eq gnus-build-sparse-threads 'more))
6604     ;; Deal with old-fetched headers and sparse threads.
6605     (while (and
6606             thread
6607             (or
6608              (gnus-summary-article-sparse-p (mail-header-number (car thread)))
6609              (gnus-summary-article-ancient-p
6610               (mail-header-number (car thread))))
6611             (if (or (<= (length (cdr thread)) 1)
6612                     (eq gnus-fetch-old-headers 'invisible))
6613                 (setq gnus-newsgroup-limit
6614                       (delq (mail-header-number (car thread))
6615                             gnus-newsgroup-limit)
6616                       thread (cadr thread))
6617               (when (gnus-invisible-cut-children (cdr thread))
6618                 (let ((th (cdr thread)))
6619                   (while th
6620                     (if (memq (mail-header-number (caar th))
6621                               gnus-newsgroup-limit)
6622                         (setq thread (car th)
6623                               th nil)
6624                       (setq th (cdr th))))))))))
6625   thread)
6626
6627 (defun gnus-cut-threads (threads)
6628   "Cut off all uninteresting articles from the beginning of threads."
6629   (when (or (eq gnus-fetch-old-headers 'some)
6630             (eq gnus-fetch-old-headers 'invisible)
6631             (numberp gnus-fetch-old-headers)
6632             (eq gnus-build-sparse-threads 'some)
6633             (eq gnus-build-sparse-threads 'more))
6634     (let ((th threads))
6635       (while th
6636         (setcar th (gnus-cut-thread (car th)))
6637         (setq th (cdr th)))))
6638   ;; Remove nixed out threads.
6639   (delq nil threads))
6640
6641 (defun gnus-summary-initial-limit (&optional show-if-empty)
6642   "Figure out what the initial limit is supposed to be on group entry.
6643 This entails weeding out unwanted dormants, low-scored articles,
6644 fetch-old-headers verbiage, and so on."
6645   ;; Most groups have nothing to remove.
6646   (if (or gnus-inhibit-limiting
6647           (and (null gnus-newsgroup-dormant)
6648                (not (eq gnus-fetch-old-headers 'some))
6649                (not (numberp gnus-fetch-old-headers))
6650                (not (eq gnus-fetch-old-headers 'invisible))
6651                (null gnus-summary-expunge-below)
6652                (not (eq gnus-build-sparse-threads 'some))
6653                (not (eq gnus-build-sparse-threads 'more))
6654                (null gnus-thread-expunge-below)
6655                (not gnus-use-nocem)))
6656       ()                                ; Do nothing.
6657     (push gnus-newsgroup-limit gnus-newsgroup-limits)
6658     (setq gnus-newsgroup-limit nil)
6659     (mapatoms
6660      (lambda (node)
6661        (unless (car (symbol-value node))
6662          ;; These threads have no parents -- they are roots.
6663          (let ((nodes (cdr (symbol-value node)))
6664                thread)
6665            (while nodes
6666              (if (and gnus-thread-expunge-below
6667                       (< (gnus-thread-total-score (car nodes))
6668                          gnus-thread-expunge-below))
6669                  (gnus-expunge-thread (pop nodes))
6670                (setq thread (pop nodes))
6671                (gnus-summary-limit-children thread))))))
6672      gnus-newsgroup-dependencies)
6673     ;; If this limitation resulted in an empty group, we might
6674     ;; pop the previous limit and use it instead.
6675     (when (and (not gnus-newsgroup-limit)
6676                show-if-empty)
6677       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
6678     gnus-newsgroup-limit))
6679
6680 (defun gnus-summary-limit-children (thread)
6681   "Return 1 if this subthread is visible and 0 if it is not."
6682   ;; First we get the number of visible children to this thread.  This
6683   ;; is done by recursing down the thread using this function, so this
6684   ;; will really go down to a leaf article first, before slowly
6685   ;; working its way up towards the root.
6686   (when thread
6687     (let ((children
6688            (if (cdr thread)
6689                (apply '+ (mapcar 'gnus-summary-limit-children
6690                                  (cdr thread)))
6691              0))
6692           (number (mail-header-number (car thread)))
6693           score)
6694       (if (and
6695            (not (memq number gnus-newsgroup-marked))
6696            (or
6697             ;; If this article is dormant and has absolutely no visible
6698             ;; children, then this article isn't visible.
6699             (and (memq number gnus-newsgroup-dormant)
6700                  (zerop children))
6701             ;; If this is "fetch-old-headered" and there is no
6702             ;; visible children, then we don't want this article.
6703             (and (or (eq gnus-fetch-old-headers 'some)
6704                      (numberp gnus-fetch-old-headers))
6705                  (gnus-summary-article-ancient-p number)
6706                  (zerop children))
6707             ;; If this is "fetch-old-headered" and `invisible', then
6708             ;; we don't want this article.
6709             (and (eq gnus-fetch-old-headers 'invisible)
6710                  (gnus-summary-article-ancient-p number))
6711             ;; If this is a sparsely inserted article with no children,
6712             ;; we don't want it.
6713             (and (eq gnus-build-sparse-threads 'some)
6714                  (gnus-summary-article-sparse-p number)
6715                  (zerop children))
6716             ;; If we use expunging, and this article is really
6717             ;; low-scored, then we don't want this article.
6718             (when (and gnus-summary-expunge-below
6719                        (< (setq score
6720                                 (or (cdr (assq number gnus-newsgroup-scored))
6721                                     gnus-summary-default-score))
6722                           gnus-summary-expunge-below))
6723               ;; We increase the expunge-tally here, but that has
6724               ;; nothing to do with the limits, really.
6725               (incf gnus-newsgroup-expunged-tally)
6726               ;; We also mark as read here, if that's wanted.
6727               (when (and gnus-summary-mark-below
6728                          (< score gnus-summary-mark-below))
6729                 (setq gnus-newsgroup-unreads
6730                       (delq number gnus-newsgroup-unreads))
6731                 (if gnus-newsgroup-auto-expire
6732                     (push number gnus-newsgroup-expirable)
6733                   (push (cons number gnus-low-score-mark)
6734                         gnus-newsgroup-reads)))
6735               t)
6736             ;; Check NoCeM things.
6737             (if (and gnus-use-nocem
6738                      (gnus-nocem-unwanted-article-p
6739                       (mail-header-id (car thread))))
6740                 (progn
6741                   (setq gnus-newsgroup-unreads
6742                         (delq number gnus-newsgroup-unreads))
6743                   t))))
6744           ;; Nope, invisible article.
6745           0
6746         ;; Ok, this article is to be visible, so we add it to the limit
6747         ;; and return 1.
6748         (push number gnus-newsgroup-limit)
6749         1))))
6750
6751 (defun gnus-expunge-thread (thread)
6752   "Mark all articles in THREAD as read."
6753   (let* ((number (mail-header-number (car thread))))
6754     (incf gnus-newsgroup-expunged-tally)
6755     ;; We also mark as read here, if that's wanted.
6756     (setq gnus-newsgroup-unreads
6757           (delq number gnus-newsgroup-unreads))
6758     (if gnus-newsgroup-auto-expire
6759         (push number gnus-newsgroup-expirable)
6760       (push (cons number gnus-low-score-mark)
6761             gnus-newsgroup-reads)))
6762   ;; Go recursively through all subthreads.
6763   (mapcar 'gnus-expunge-thread (cdr thread)))
6764
6765 ;; Summary article oriented commands
6766
6767 (defun gnus-summary-refer-parent-article (n)
6768   "Refer parent article N times.
6769 If N is negative, go to ancestor -N instead.
6770 The difference between N and the number of articles fetched is returned."
6771   (interactive "p")
6772   (let ((skip 1)
6773         error header ref)
6774     (when (not (natnump n))
6775       (setq skip (abs n)
6776             n 1))
6777     (while (and (> n 0)
6778                 (not error))
6779       (setq header (gnus-summary-article-header))
6780       (if (and (eq (mail-header-number header)
6781                    (cdr gnus-article-current))
6782                (equal gnus-newsgroup-name
6783                       (car gnus-article-current)))
6784           ;; If we try to find the parent of the currently
6785           ;; displayed article, then we take a look at the actual
6786           ;; References header, since this is slightly more
6787           ;; reliable than the References field we got from the
6788           ;; server.
6789           (save-excursion
6790             (set-buffer gnus-original-article-buffer)
6791             (nnheader-narrow-to-headers)
6792             (unless (setq ref (message-fetch-field "references"))
6793               (setq ref (message-fetch-field "in-reply-to")))
6794             (widen))
6795         (setq ref
6796               ;; It's not the current article, so we take a bet on
6797               ;; the value we got from the server.
6798               (mail-header-references header)))
6799       (if (and ref
6800                (not (equal ref "")))
6801           (unless (gnus-summary-refer-article (gnus-parent-id ref skip))
6802             (gnus-message 1 "Couldn't find parent"))
6803         (gnus-message 1 "No references in article %d"
6804                       (gnus-summary-article-number))
6805         (setq error t))
6806       (decf n))
6807     (gnus-summary-position-point)
6808     n))
6809
6810 (defun gnus-summary-refer-references ()
6811   "Fetch all articles mentioned in the References header.
6812 Return the number of articles fetched."
6813   (interactive)
6814   (let ((ref (mail-header-references (gnus-summary-article-header)))
6815         (current (gnus-summary-article-number))
6816         (n 0))
6817     (if (or (not ref)
6818             (equal ref ""))
6819         (error "No References in the current article")
6820       ;; For each Message-ID in the References header...
6821       (while (string-match "<[^>]*>" ref)
6822         (incf n)
6823         ;; ... fetch that article.
6824         (gnus-summary-refer-article
6825          (prog1 (match-string 0 ref)
6826            (setq ref (substring ref (match-end 0))))))
6827       (gnus-summary-goto-subject current)
6828       (gnus-summary-position-point)
6829       n)))
6830
6831 (defun gnus-summary-refer-thread (&optional limit)
6832   "Fetch all articles in the current thread.
6833 If LIMIT (the numerical prefix), fetch that many old headers instead
6834 of what's specified by the `gnus-refer-thread-limit' variable."
6835   (interactive "P")
6836   (let ((id (mail-header-id (gnus-summary-article-header)))
6837         (limit (if limit (prefix-numeric-value limit)
6838                  gnus-refer-thread-limit)))
6839     ;; We want to fetch LIMIT *old* headers, but we also have to
6840     ;; re-fetch all the headers in the current buffer, because many of
6841     ;; them may be undisplayed.  So we adjust LIMIT.
6842     (when (numberp limit)
6843       (incf limit (- gnus-newsgroup-end gnus-newsgroup-begin)))
6844     (unless (eq gnus-fetch-old-headers 'invisible)
6845       (gnus-message 5 "Fetching headers for %s..." gnus-newsgroup-name)
6846       ;; Retrieve the headers and read them in.
6847       (if (eq (gnus-retrieve-headers
6848                (list gnus-newsgroup-end) gnus-newsgroup-name limit)
6849               'nov)
6850           (gnus-build-all-threads)
6851         (error "Can't fetch thread from backends that don't support NOV"))
6852       (gnus-message 5 "Fetching headers for %s...done" gnus-newsgroup-name))
6853     (gnus-summary-limit-include-thread id)))
6854
6855 (defun gnus-summary-refer-article (message-id)
6856   "Fetch an article specified by MESSAGE-ID."
6857   (interactive "sMessage-ID: ")
6858   (when (and (stringp message-id)
6859              (not (zerop (length message-id))))
6860     ;; Construct the correct Message-ID if necessary.
6861     ;; Suggested by tale@pawl.rpi.edu.
6862     (unless (string-match "^<" message-id)
6863       (setq message-id (concat "<" message-id)))
6864     (unless (string-match ">$" message-id)
6865       (setq message-id (concat message-id ">")))
6866     (let* ((header (gnus-id-to-header message-id))
6867            (sparse (and header
6868                         (gnus-summary-article-sparse-p
6869                          (mail-header-number header))
6870                         (memq (mail-header-number header)
6871                               gnus-newsgroup-limit)))
6872            number)
6873       (cond
6874        ;; If the article is present in the buffer we just go to it.
6875        ((and header
6876              (or (not (gnus-summary-article-sparse-p
6877                        (mail-header-number header)))
6878                  sparse))
6879         (prog1
6880             (gnus-summary-goto-article
6881              (mail-header-number header) nil t)
6882           (when sparse
6883             (gnus-summary-update-article (mail-header-number header)))))
6884        (t
6885         ;; We fetch the article.
6886         (catch 'found
6887           (dolist (gnus-override-method (gnus-refer-article-methods))
6888             (gnus-check-server gnus-override-method)
6889             ;; Fetch the header, and display the article.
6890             (when (setq number (gnus-summary-insert-subject message-id))
6891               (gnus-summary-select-article nil nil nil number)
6892               (throw 'found t)))
6893           (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
6894
6895 (defun gnus-refer-article-methods ()
6896   "Return a list of referrable methods."
6897   (cond
6898    ;; No method, so we default to current and native.
6899    ((null gnus-refer-article-method)
6900     (list gnus-current-select-method gnus-select-method))
6901    ;; Current.
6902    ((eq 'current gnus-refer-article-method)
6903     (list gnus-current-select-method))
6904    ;; List of select methods.
6905    ((not (stringp (cadr gnus-refer-article-method)))
6906     (let (out)
6907       (dolist (method gnus-refer-article-method)
6908         (push (if (eq 'current method)
6909                   gnus-current-select-method
6910                 method)
6911               out))
6912       (nreverse out)))
6913    ;; One single select method.
6914    (t
6915     (list gnus-refer-article-method))))
6916
6917 (defun gnus-summary-edit-parameters ()
6918   "Edit the group parameters of the current group."
6919   (interactive)
6920   (gnus-group-edit-group gnus-newsgroup-name 'params))
6921
6922 (defun gnus-summary-customize-parameters ()
6923   "Customize the group parameters of the current group."
6924   (interactive)
6925   (gnus-group-customize gnus-newsgroup-name))
6926
6927 (defun gnus-summary-enter-digest-group (&optional force)
6928   "Enter an nndoc group based on the current article.
6929 If FORCE, force a digest interpretation.  If not, try
6930 to guess what the document format is."
6931   (interactive "P")
6932   (let ((conf gnus-current-window-configuration))
6933     (save-excursion
6934       (gnus-summary-select-article))
6935     (setq gnus-current-window-configuration conf)
6936     (let* ((name (format "%s-%d"
6937                          (gnus-group-prefixed-name
6938                           gnus-newsgroup-name (list 'nndoc ""))
6939                          (save-excursion
6940                            (set-buffer gnus-summary-buffer)
6941                            gnus-current-article)))
6942            (ogroup gnus-newsgroup-name)
6943            (params (append (gnus-info-params (gnus-get-info ogroup))
6944                            (list (cons 'to-group ogroup))
6945                            (list (cons 'save-article-group ogroup))))
6946            (case-fold-search t)
6947            (buf (current-buffer))
6948            dig to-address)
6949       (save-excursion
6950         (set-buffer gnus-original-article-buffer)
6951         ;; Have the digest group inherit the main mail address of
6952         ;; the parent article.
6953         (when (setq to-address (or (message-fetch-field "reply-to")
6954                                    (message-fetch-field "from")))
6955           (setq params (append (list (cons 'to-address to-address)))))
6956         (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
6957         (insert-buffer-substring gnus-original-article-buffer)
6958         ;; Remove lines that may lead nndoc to misinterpret the
6959         ;; document type.
6960         (narrow-to-region
6961          (goto-char (point-min))
6962          (or (search-forward "\n\n" nil t) (point)))
6963         (goto-char (point-min))
6964         (delete-matching-lines "^Path:\\|^From ")
6965         (widen))
6966       (unwind-protect
6967           (if (let ((gnus-newsgroup-ephemeral-charset gnus-newsgroup-charset)
6968                     (gnus-newsgroup-ephemeral-ignored-charsets
6969                      gnus-newsgroup-ignored-charsets))
6970                 (gnus-group-read-ephemeral-group
6971                  name `(nndoc ,name (nndoc-address ,(get-buffer dig))
6972                               (nndoc-article-type
6973                                ,(if force 'mbox 'guess))) t))
6974               ;; Make all postings to this group go to the parent group.
6975               (nconc (gnus-info-params (gnus-get-info name))
6976                      params)
6977             ;; Couldn't select this doc group.
6978             (switch-to-buffer buf)
6979             (gnus-set-global-variables)
6980             (gnus-configure-windows 'summary)
6981             (gnus-message 3 "Article couldn't be entered?"))
6982         (kill-buffer dig)))))
6983
6984 (defun gnus-summary-read-document (n)
6985   "Open a new group based on the current article(s).
6986 This will allow you to read digests and other similar
6987 documents as newsgroups.
6988 Obeys the standard process/prefix convention."
6989   (interactive "P")
6990   (let* ((articles (gnus-summary-work-articles n))
6991          (ogroup gnus-newsgroup-name)
6992          (params (append (gnus-info-params (gnus-get-info ogroup))
6993                          (list (cons 'to-group ogroup))))
6994          article group egroup groups vgroup)
6995     (while (setq article (pop articles))
6996       (setq group (format "%s-%d" gnus-newsgroup-name article))
6997       (gnus-summary-remove-process-mark article)
6998       (when (gnus-summary-display-article article)
6999         (save-excursion
7000           (with-temp-buffer
7001             (insert-buffer-substring gnus-original-article-buffer)
7002             ;; Remove some headers that may lead nndoc to make
7003             ;; the wrong guess.
7004             (message-narrow-to-head)
7005             (goto-char (point-min))
7006             (delete-matching-lines "^\\(Path\\):\\|^From ")
7007             (widen)
7008             (if (setq egroup
7009                       (gnus-group-read-ephemeral-group
7010                        group `(nndoc ,group (nndoc-address ,(current-buffer))
7011                                      (nndoc-article-type guess))
7012                        t nil t))
7013                 (progn
7014                   ;; Make all postings to this group go to the parent group.
7015                   (nconc (gnus-info-params (gnus-get-info egroup))
7016                          params)
7017                   (push egroup groups))
7018               ;; Couldn't select this doc group.
7019               (gnus-error 3 "Article couldn't be entered"))))))
7020     ;; Now we have selected all the documents.
7021     (cond
7022      ((not groups)
7023       (error "None of the articles could be interpreted as documents"))
7024      ((gnus-group-read-ephemeral-group
7025        (setq vgroup (format
7026                      "nnvirtual:%s-%s" gnus-newsgroup-name
7027                      (format-time-string "%Y%m%dT%H%M%S" (current-time))))
7028        `(nnvirtual ,vgroup (nnvirtual-component-groups ,groups))
7029        t
7030        (cons (current-buffer) 'summary)))
7031      (t
7032       (error "Couldn't select virtual nndoc group")))))
7033
7034 (defun gnus-summary-isearch-article (&optional regexp-p)
7035   "Do incremental search forward on the current article.
7036 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
7037   (interactive "P")
7038   (let* ((gnus-inhibit-treatment t)
7039          (old (gnus-summary-select-article)))
7040     (gnus-configure-windows 'article)
7041     (gnus-eval-in-buffer-window gnus-article-buffer
7042       (save-restriction
7043         (widen)
7044         (when (eq 'old old)
7045           (gnus-article-show-all-headers))
7046         (goto-char (point-min))
7047         (isearch-forward regexp-p)))))
7048
7049 (defun gnus-summary-search-article-forward (regexp &optional backward)
7050   "Search for an article containing REGEXP forward.
7051 If BACKWARD, search backward instead."
7052   (interactive
7053    (list (read-string
7054           (format "Search article %s (regexp%s): "
7055                   (if current-prefix-arg "backward" "forward")
7056                   (if gnus-last-search-regexp
7057                       (concat ", default " gnus-last-search-regexp)
7058                     "")))
7059          current-prefix-arg))
7060   (if (string-equal regexp "")
7061       (setq regexp (or gnus-last-search-regexp ""))
7062     (setq gnus-last-search-regexp regexp))
7063   (if (gnus-summary-search-article regexp backward)
7064       (gnus-summary-show-thread)
7065     (error "Search failed: \"%s\"" regexp)))
7066
7067 (defun gnus-summary-search-article-backward (regexp)
7068   "Search for an article containing REGEXP backward."
7069   (interactive
7070    (list (read-string
7071           (format "Search article backward (regexp%s): "
7072                   (if gnus-last-search-regexp
7073                       (concat ", default " gnus-last-search-regexp)
7074                     "")))))
7075   (gnus-summary-search-article-forward regexp 'backward))
7076
7077 (eval-when-compile
7078   (defmacro gnus-summary-search-article-position-point (regexp backward)
7079     "Dehighlight the last matched text and goto the beginning position."
7080     (` (if (and gnus-summary-search-article-matched-data
7081                 (let ((text (caddr gnus-summary-search-article-matched-data))
7082                       (inhibit-read-only t)
7083                       buffer-read-only)
7084                   (delete-region
7085                    (goto-char (car gnus-summary-search-article-matched-data))
7086                    (cadr gnus-summary-search-article-matched-data))
7087                   (insert text)
7088                   (string-match (, regexp) text)))
7089            (if (, backward) (beginning-of-line) (end-of-line))
7090          (goto-char (if (, backward) (point-max) (point-min))))))
7091
7092   (defmacro gnus-summary-search-article-highlight-goto-x-face (opoint)
7093     "Place point where X-Face image is displayed."
7094     (if (featurep 'xemacs)
7095         (` (let ((end (if (search-forward "\n\n" nil t)
7096                           (goto-char (1- (point)))
7097                         (point-min)))
7098                  extent)
7099              (or (search-backward "\n\n" nil t) (goto-char (point-min)))
7100              (unless (and (re-search-forward "^From:" end t)
7101                           (setq extent (extent-at (point)))
7102                           (extent-begin-glyph extent))
7103                (goto-char (, opoint)))))
7104       (` (let ((end (if (search-forward "\n\n" nil t)
7105                         (goto-char (1- (point)))
7106                       (point-min))))
7107            (goto-char
7108             (or (text-property-any (or (search-backward "\n\n" nil t)
7109                                        (point-min))
7110                                    end 'x-face-mule-bitmap-image t)
7111                 (, opoint)))))))
7112
7113   (defmacro gnus-summary-search-article-highlight-matched-text
7114     (backward treated x-face)
7115     "Highlight matched text in the function `gnus-summary-search-article'."
7116     (` (let ((start (set-marker (make-marker) (match-beginning 0)))
7117              (end (set-marker (make-marker) (match-end 0)))
7118              (inhibit-read-only t)
7119              buffer-read-only)
7120          (unless treated
7121            (let ((,@
7122                   (let ((items (mapcar 'car gnus-treatment-function-alist)))
7123                     (mapcar
7124                      (lambda (item) (setq items (delq item items)))
7125                      '(gnus-treat-buttonize
7126                        gnus-treat-fill-article
7127                        gnus-treat-fill-long-lines
7128                        gnus-treat-emphasize
7129                        gnus-treat-highlight-headers
7130                        gnus-treat-highlight-citation
7131                        gnus-treat-highlight-signature
7132                        gnus-treat-overstrike
7133                        gnus-treat-display-xface
7134                        gnus-treat-buttonize-head
7135                        gnus-treat-decode-article-as-default-mime-charset))
7136                     (static-if (featurep 'xemacs)
7137                         items
7138                       (cons '(x-face-mule-delete-x-face-field
7139                               (quote never))
7140                             items))))
7141                  (gnus-treat-display-xface
7142                   (when (, x-face) gnus-treat-display-xface)))
7143              (gnus-article-prepare-mime-display)))
7144          (goto-char (if (, backward) start end))
7145          (when (, x-face)
7146            (gnus-summary-search-article-highlight-goto-x-face (point)))
7147          (setq gnus-summary-search-article-matched-data
7148                (list start end (buffer-substring start end)))
7149          (unless (eq start end);; matched text has been deleted. :-<
7150            (put-text-property start end 'face
7151                               (or (find-face 'isearch)
7152                                   'secondary-selection))))))
7153   )
7154
7155 (defun gnus-summary-search-article (regexp &optional backward)
7156   "Search for an article containing REGEXP.
7157 Optional argument BACKWARD means do search for backward.
7158 `gnus-select-article-hook' is not called during the search."
7159   ;; We have to require this here to make sure that the following
7160   ;; dynamic binding isn't shadowed by autoloading.
7161   (require 'gnus-async)
7162   (require 'gnus-art)
7163   (let ((gnus-select-article-hook nil)  ;Disable hook.
7164         (gnus-article-display-hook nil)
7165         (gnus-article-prepare-hook nil)
7166         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
7167         (gnus-use-article-prefetch nil)
7168         (gnus-xmas-force-redisplay nil) ;Inhibit XEmacs redisplay.
7169         (gnus-use-trees nil)            ;Inhibit updating tree buffer.
7170         (sum (current-buffer))
7171         (found nil)
7172         point treated)
7173     (gnus-save-hidden-threads
7174       (static-if (featurep 'xemacs)
7175           (let ((gnus-inhibit-treatment t))
7176             (setq treated (eq 'old (gnus-summary-select-article)))
7177             (when (and treated
7178                        (not (and (gnus-buffer-live-p gnus-article-buffer)
7179                                  (window-live-p (get-buffer-window
7180                                                  gnus-article-buffer t)))))
7181               (gnus-summary-select-article nil t)
7182               (setq treated nil)))
7183         (let ((gnus-inhibit-treatment t)
7184               (x-face-mule-delete-x-face-field 'never))
7185           (setq treated (eq 'old (gnus-summary-select-article)))
7186           (when (and treated
7187                      (not
7188                       (and (gnus-buffer-live-p gnus-article-buffer)
7189                            (window-live-p (get-buffer-window
7190                                            gnus-article-buffer t))
7191                            (or (not (string-match "^\\^X-Face:" regexp))
7192                                (with-current-buffer gnus-article-buffer
7193                                  gnus-summary-search-article-matched-data)))))
7194             (gnus-summary-select-article nil t)
7195             (setq treated nil))))
7196       (set-buffer gnus-article-buffer)
7197       (widen)
7198       (if treated
7199           (progn
7200             (gnus-article-show-all-headers)
7201             (gnus-summary-search-article-position-point regexp backward))
7202         (goto-char (if backward (point-max) (point-min))))
7203       (while (not found)
7204         (gnus-message 7 "Searching article: %d..." (cdr gnus-article-current))
7205         (if (if backward
7206                 (re-search-backward regexp nil t)
7207               (re-search-forward regexp nil t))
7208             ;; We found the regexp.
7209             (progn
7210               (gnus-summary-search-article-highlight-matched-text
7211                backward treated (string-match "^\\^X-Face:" regexp))
7212               (setq found 'found)
7213               (forward-line
7214                (/ (- 2 (window-height
7215                         (get-buffer-window gnus-article-buffer t)))
7216                   2))
7217               (set-window-start
7218                (get-buffer-window (current-buffer))
7219                (point))
7220               (set-buffer sum)
7221               (setq point (point)))
7222           ;; We didn't find it, so we go to the next article.
7223           (set-buffer sum)
7224           (setq found 'not)
7225           (while (eq found 'not)
7226             (if (not (if backward (gnus-summary-find-prev)
7227                        (gnus-summary-find-next)))
7228                 ;; No more articles.
7229                 (setq found t)
7230               ;; Select the next article and adjust point.
7231               (unless (gnus-summary-article-sparse-p
7232                        (gnus-summary-article-number))
7233                 (setq found nil)
7234                 (let ((gnus-inhibit-treatment t))
7235                   (gnus-summary-select-article))
7236                 (setq treated nil)
7237                 (set-buffer gnus-article-buffer)
7238                 (widen)
7239                 (goto-char (if backward (point-max) (point-min))))))))
7240       (gnus-message 7 ""))
7241     ;; Return whether we found the regexp.
7242     (when (eq found 'found)
7243       (goto-char point)
7244       (gnus-summary-show-thread)
7245       (gnus-summary-goto-subject gnus-current-article)
7246       (gnus-summary-position-point)
7247       t)))
7248
7249 (defun gnus-summary-find-matching (header regexp &optional backward unread
7250                                           not-case-fold)
7251   "Return a list of all articles that match REGEXP on HEADER.
7252 The search stars on the current article and goes forwards unless
7253 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
7254 If UNREAD is non-nil, only unread articles will
7255 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
7256 in the comparisons."
7257   (let ((data (if (eq backward 'all) gnus-newsgroup-data
7258                 (gnus-data-find-list
7259                  (gnus-summary-article-number) (gnus-data-list backward))))
7260         (case-fold-search (not not-case-fold))
7261         articles d func)
7262     (if (consp header)
7263         (if (eq (car header) 'extra)
7264             (setq func
7265                   `(lambda (h)
7266                      (or (cdr (assq ',(cdr header) (mail-header-extra h)))
7267                          "")))
7268           (error "%s is an invalid header" header))
7269       (unless (fboundp (intern (concat "mail-header-" header)))
7270         (error "%s is not a valid header" header))
7271       (setq func `(lambda (h) (,(intern (concat "mail-header-" header)) h))))
7272     (while data
7273       (setq d (car data))
7274       (and (or (not unread)             ; We want all articles...
7275                (gnus-data-unread-p d))  ; Or just unreads.
7276            (vectorp (gnus-data-header d)) ; It's not a pseudo.
7277            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
7278            (push (gnus-data-number d) articles)) ; Success!
7279       (setq data (cdr data)))
7280     (nreverse articles)))
7281
7282 (defun gnus-summary-execute-command (header regexp command &optional backward)
7283   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
7284 If HEADER is an empty string (or nil), the match is done on the entire
7285 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
7286   (interactive
7287    (list (let ((completion-ignore-case t))
7288            (completing-read
7289             "Header name: "
7290             (mapcar (lambda (string) (list string))
7291                     '("Number" "Subject" "From" "Lines" "Date"
7292                       "Message-ID" "Xref" "References" "Body"))
7293             nil 'require-match))
7294          (read-string "Regexp: ")
7295          (read-key-sequence "Command: ")
7296          current-prefix-arg))
7297   (when (equal header "Body")
7298     (setq header ""))
7299   ;; Hidden thread subtrees must be searched as well.
7300   (gnus-summary-show-all-threads)
7301   ;; We don't want to change current point nor window configuration.
7302   (save-excursion
7303     (save-window-excursion
7304       (gnus-message 6 "Executing %s..." (key-description command))
7305       ;; We'd like to execute COMMAND interactively so as to give arguments.
7306       (gnus-execute header regexp
7307                     `(call-interactively ',(key-binding command))
7308                     backward)
7309       (gnus-message 6 "Executing %s...done" (key-description command)))))
7310
7311 (defun gnus-summary-beginning-of-article ()
7312   "Scroll the article back to the beginning."
7313   (interactive)
7314   (gnus-summary-select-article)
7315   (gnus-configure-windows 'article)
7316   (gnus-eval-in-buffer-window gnus-article-buffer
7317     (widen)
7318     (goto-char (point-min))
7319     (when gnus-page-broken
7320       (gnus-narrow-to-page))))
7321
7322 (defun gnus-summary-end-of-article ()
7323   "Scroll to the end of the article."
7324   (interactive)
7325   (gnus-summary-select-article)
7326   (gnus-configure-windows 'article)
7327   (gnus-eval-in-buffer-window gnus-article-buffer
7328     (widen)
7329     (goto-char (point-max))
7330     (recenter -3)
7331     (when gnus-page-broken
7332       (gnus-narrow-to-page))))
7333
7334 (defun gnus-summary-print-article (&optional filename n)
7335   "Generate and print a PostScript image of the N next (mail) articles.
7336
7337 If N is negative, print the N previous articles.  If N is nil and articles
7338 have been marked with the process mark, print these instead.
7339
7340 If the optional first argument FILENAME is nil, send the image to the
7341 printer.  If FILENAME is a string, save the PostScript image in a file with
7342 that name.  If FILENAME is a number, prompt the user for the name of the file
7343 to save in."
7344   (interactive (list (ps-print-preprint current-prefix-arg)
7345                      current-prefix-arg))
7346   (dolist (article (gnus-summary-work-articles n))
7347     (gnus-summary-select-article nil nil 'pseudo article)
7348     (gnus-eval-in-buffer-window gnus-article-buffer
7349       (let ((buffer (generate-new-buffer " *print*")))
7350         (unwind-protect
7351             (progn
7352               (copy-to-buffer buffer (point-min) (point-max))
7353               (set-buffer buffer)
7354               (gnus-article-delete-invisible-text)
7355               (let ((ps-left-header
7356                      (list
7357                       (concat "("
7358                               (mail-header-subject gnus-current-headers) ")")
7359                       (concat "("
7360                               (mail-header-from gnus-current-headers) ")")))
7361                     (ps-right-header
7362                      (list
7363                       "/pagenumberstring load"
7364                       (concat "("
7365                               (mail-header-date gnus-current-headers) ")"))))
7366                 (gnus-run-hooks 'gnus-ps-print-hook)
7367                 (save-excursion
7368                   (ps-print-buffer-with-faces filename))))
7369           (kill-buffer buffer))))))
7370
7371 (defun gnus-summary-show-article (&optional arg)
7372   "Force re-fetching of the current article.
7373 If ARG (the prefix) is a number, show the article with the charset
7374 defined in `gnus-summary-show-article-charset-alist', or the charset
7375 inputed.
7376 If ARG (the prefix) is non-nil and not a number, show the raw article
7377 without any article massaging functions being run."
7378   (interactive "P")
7379   (cond
7380    ((numberp arg)
7381     (let ((gnus-newsgroup-charset
7382            (or (cdr (assq arg gnus-summary-show-article-charset-alist))
7383                (read-coding-system "Charset: ")))
7384           (gnus-newsgroup-ignored-charsets 'gnus-all))
7385       (gnus-summary-select-article nil 'force)))
7386    ((not arg)
7387     ;; Select the article the normal way.
7388     (gnus-summary-select-article nil 'force))
7389    (t
7390     ;; We have to require this here to make sure that the following
7391     ;; dynamic binding isn't shadowed by autoloading.
7392     (require 'gnus-async)
7393     (require 'gnus-art)
7394     ;; Bind the article treatment functions to nil.
7395     (let ((gnus-have-all-headers t)
7396           gnus-article-display-hook
7397           gnus-article-prepare-hook
7398           gnus-article-decode-hook
7399           gnus-break-pages
7400           gnus-show-mime)
7401       (gnus-summary-select-article nil 'force))))
7402   (gnus-summary-goto-subject gnus-current-article)
7403   (gnus-summary-position-point))
7404
7405 (defun gnus-summary-verbose-headers (&optional arg)
7406   "Toggle permanent full header display.
7407 If ARG is a positive number, turn header display on.
7408 If ARG is a negative number, turn header display off."
7409   (interactive "P")
7410   (setq gnus-show-all-headers
7411         (cond ((or (not (numberp arg))
7412                    (zerop arg))
7413                (not gnus-show-all-headers))
7414               ((natnump arg)
7415                t)))
7416   (gnus-summary-show-article))
7417
7418 (defun gnus-summary-toggle-header (&optional arg)
7419   "Show the headers if they are hidden, or hide them if they are shown.
7420 If ARG is a positive number, show the entire header.
7421 If ARG is a negative number, hide the unwanted header lines."
7422   (interactive "P")
7423   (save-excursion
7424     (set-buffer gnus-article-buffer)
7425     (save-restriction
7426       (let* ((buffer-read-only nil)
7427              (inhibit-point-motion-hooks t)
7428              hidden e)
7429         (setq hidden
7430               (if (numberp arg)
7431                   (>= arg 0)
7432                 (save-restriction
7433                   (article-narrow-to-head)
7434                   (gnus-article-hidden-text-p 'headers))))
7435         (goto-char (point-min))
7436         (when (search-forward "\n\n" nil t)
7437           (delete-region (point-min) (1- (point))))
7438         (goto-char (point-min))
7439         (save-excursion
7440           (set-buffer gnus-original-article-buffer)
7441           (goto-char (point-min))
7442           (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
7443         (insert-buffer-substring gnus-original-article-buffer 1 e)
7444         (save-restriction
7445           (narrow-to-region (point-min) (point))
7446           (article-decode-encoded-words)
7447           (if  hidden
7448               (let ((gnus-treat-hide-headers nil)
7449                     (gnus-treat-hide-boring-headers nil))
7450                 (setq gnus-article-wash-types
7451                       (delq 'headers gnus-article-wash-types))
7452                 (gnus-treat-article 'head))
7453             (gnus-treat-article 'head)))
7454         (gnus-set-mode-line 'article)))))
7455
7456 (defun gnus-summary-show-all-headers ()
7457   "Make all header lines visible."
7458   (interactive)
7459   (gnus-article-show-all-headers))
7460
7461 (defun gnus-summary-toggle-mime (&optional arg)
7462   "Toggle MIME processing.
7463 If ARG is a positive number, turn MIME processing on."
7464   (interactive "P")
7465   (setq gnus-show-mime
7466         (if (null arg)
7467             (not gnus-show-mime)
7468           (> (prefix-numeric-value arg) 0)))
7469   (gnus-summary-select-article t 'force))
7470
7471 (defun gnus-summary-caesar-message (&optional arg)
7472   "Caesar rotate the current article by 13.
7473 The numerical prefix specifies how many places to rotate each letter
7474 forward."
7475   (interactive "P")
7476   (gnus-summary-select-article)
7477   (let ((mail-header-separator ""))
7478     (gnus-eval-in-buffer-window gnus-article-buffer
7479       (save-restriction
7480         (widen)
7481         (let ((start (window-start))
7482               buffer-read-only)
7483           (message-caesar-buffer-body arg)
7484           (set-window-start (get-buffer-window (current-buffer)) start))))))
7485
7486 (defun gnus-summary-stop-page-breaking ()
7487   "Stop page breaking in the current article."
7488   (interactive)
7489   (gnus-summary-select-article)
7490   (gnus-eval-in-buffer-window gnus-article-buffer
7491     (widen)
7492     (when (gnus-visual-p 'page-marker)
7493       (let ((buffer-read-only nil))
7494         (gnus-remove-text-with-property 'gnus-prev)
7495         (gnus-remove-text-with-property 'gnus-next))
7496       (setq gnus-page-broken nil))))
7497
7498 (defun gnus-summary-move-article (&optional n to-newsgroup
7499                                             select-method action)
7500   "Move the current article to a different newsgroup.
7501 If N is a positive number, move the N next articles.
7502 If N is a negative number, move the N previous articles.
7503 If N is nil and any articles have been marked with the process mark,
7504 move those articles instead.
7505 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7506 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7507 re-spool using this method.
7508
7509 For this function to work, both the current newsgroup and the
7510 newsgroup that you want to move to have to support the `request-move'
7511 and `request-accept' functions.
7512
7513 ACTION can be either `move' (the default), `crosspost' or `copy'."
7514   (interactive "P")
7515   (unless action
7516     (setq action 'move))
7517   ;; Disable marking as read.
7518   (let (gnus-mark-article-hook)
7519     (save-window-excursion
7520       (gnus-summary-select-article)))
7521   ;; Check whether the source group supports the required functions.
7522   (cond ((and (eq action 'move)
7523               (not (gnus-check-backend-function
7524                     'request-move-article gnus-newsgroup-name)))
7525          (error "The current group does not support article moving"))
7526         ((and (eq action 'crosspost)
7527               (not (gnus-check-backend-function
7528                     'request-replace-article gnus-newsgroup-name)))
7529          (error "The current group does not support article editing")))
7530   (let ((articles (gnus-summary-work-articles n))
7531         (prefix (if (gnus-check-backend-function
7532                     'request-move-article gnus-newsgroup-name)
7533                     (gnus-group-real-prefix gnus-newsgroup-name)
7534                   ""))
7535         (names '((move "Move" "Moving")
7536                  (copy "Copy" "Copying")
7537                  (crosspost "Crosspost" "Crossposting")))
7538         (copy-buf (save-excursion
7539                     (nnheader-set-temp-buffer " *copy article*")))
7540         (default-marks gnus-article-mark-lists)
7541         (no-expire-marks (delete '(expirable . expire)
7542                                  (copy-sequence gnus-article-mark-lists)))
7543         art-group to-method new-xref article to-groups)
7544     (unless (assq action names)
7545       (error "Unknown action %s" action))
7546     ;; Read the newsgroup name.
7547     (when (and (not to-newsgroup)
7548                (not select-method))
7549       (setq to-newsgroup
7550             (gnus-read-move-group-name
7551              (cadr (assq action names))
7552              (symbol-value (intern (format "gnus-current-%s-group" action)))
7553              articles prefix))
7554       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
7555     (setq to-method (or select-method
7556                         (gnus-server-to-method
7557                          (gnus-group-method to-newsgroup))))
7558     ;; Check the method we are to move this article to...
7559     (unless (gnus-check-backend-function
7560              'request-accept-article (car to-method))
7561       (error "%s does not support article copying" (car to-method)))
7562     (unless (gnus-check-server to-method)
7563       (error "Can't open server %s" (car to-method)))
7564     (gnus-message 6 "%s to %s: %s..."
7565                   (caddr (assq action names))
7566                   (or (car select-method) to-newsgroup) articles)
7567     (while articles
7568       (setq article (pop articles))
7569       (setq
7570        art-group
7571        (cond
7572         ;; Move the article.
7573         ((eq action 'move)
7574          ;; Remove this article from future suppression.
7575          (gnus-dup-unsuppress-article article)
7576          (gnus-request-move-article
7577           article                       ; Article to move
7578           gnus-newsgroup-name           ; From newsgroup
7579           (nth 1 (gnus-find-method-for-group
7580                   gnus-newsgroup-name)) ; Server
7581           (list 'gnus-request-accept-article
7582                 to-newsgroup (list 'quote select-method)
7583                 (not articles) t)       ; Accept form
7584           (not articles)))              ; Only save nov last time
7585         ;; Copy the article.
7586         ((eq action 'copy)
7587          (save-excursion
7588            (set-buffer copy-buf)
7589            (when (gnus-request-article-this-buffer article gnus-newsgroup-name)
7590              (gnus-request-accept-article
7591               to-newsgroup select-method (not articles) t))))
7592         ;; Crosspost the article.
7593         ((eq action 'crosspost)
7594          (let ((xref (message-tokenize-header
7595                       (mail-header-xref (gnus-summary-article-header article))
7596                       " ")))
7597            (setq new-xref (concat (gnus-group-real-name gnus-newsgroup-name)
7598                                   ":" article))
7599            (unless xref
7600              (setq xref (list (system-name))))
7601            (setq new-xref
7602                  (concat
7603                   (mapconcat 'identity
7604                              (delete "Xref:" (delete new-xref xref))
7605                              " ")
7606                   " " new-xref))
7607            (save-excursion
7608              (set-buffer copy-buf)
7609              ;; First put the article in the destination group.
7610              (gnus-request-article-this-buffer article gnus-newsgroup-name)
7611              (when (consp (setq art-group
7612                                 (gnus-request-accept-article
7613                                  to-newsgroup select-method (not articles))))
7614                (setq new-xref (concat new-xref " " (car art-group)
7615                                       ":" (cdr art-group)))
7616                ;; Now we have the new Xrefs header, so we insert
7617                ;; it and replace the new article.
7618                (nnheader-replace-header "Xref" new-xref)
7619                (gnus-request-replace-article
7620                 (cdr art-group) to-newsgroup (current-buffer))
7621                art-group))))))
7622       (cond
7623        ((not art-group)
7624         (gnus-message 1 "Couldn't %s article %s: %s"
7625                       (cadr (assq action names)) article
7626                       (nnheader-get-report (car to-method))))
7627        ((eq art-group 'junk)
7628         (when (eq action 'move)
7629           (gnus-summary-mark-article article gnus-canceled-mark)
7630           (gnus-message 4 "Deleted article %s" article)))
7631        (t
7632         (let* ((pto-group (gnus-group-prefixed-name
7633                            (car art-group) to-method))
7634                (entry
7635                 (gnus-gethash pto-group gnus-newsrc-hashtb))
7636                (info (nth 2 entry))
7637                (to-group (gnus-info-group info))
7638                to-marks)
7639           ;; Update the group that has been moved to.
7640           (when (and info
7641                      (memq action '(move copy)))
7642             (unless (member to-group to-groups)
7643               (push to-group to-groups))
7644
7645             (unless (memq article gnus-newsgroup-unreads)
7646               (push 'read to-marks)
7647               (gnus-info-set-read
7648                info (gnus-add-to-range (gnus-info-read info)
7649                                        (list (cdr art-group)))))
7650
7651             ;; See whether the article is to be put in the cache.
7652             (let ((marks (if (gnus-group-auto-expirable-p to-group)
7653                              default-marks
7654                            no-expire-marks))
7655                   (to-article (cdr art-group)))
7656
7657               ;; Enter the article into the cache in the new group,
7658               ;; if that is required.
7659               (when gnus-use-cache
7660                 (gnus-cache-possibly-enter-article
7661                  to-group to-article
7662                  (let ((header (copy-sequence
7663                                 (gnus-summary-article-header article))))
7664                    (mail-header-set-number header to-article)
7665                    header)
7666                  (memq article gnus-newsgroup-marked)
7667                  (memq article gnus-newsgroup-dormant)
7668                  (memq article gnus-newsgroup-unreads)))
7669
7670               (when gnus-preserve-marks
7671                 ;; Copy any marks over to the new group.
7672                 (when (and (equal to-group gnus-newsgroup-name)
7673                            (not (memq article gnus-newsgroup-unreads)))
7674                   ;; Mark this article as read in this group.
7675                   (push (cons to-article gnus-read-mark) gnus-newsgroup-reads)
7676                   (setcdr (gnus-active to-group) to-article)
7677                   (setcdr gnus-newsgroup-active to-article))
7678
7679                 (while marks
7680                   (when (memq article (symbol-value
7681                                        (intern (format "gnus-newsgroup-%s"
7682                                                        (caar marks)))))
7683                     (push (cdar marks) to-marks)
7684                     ;; If the other group is the same as this group,
7685                     ;; then we have to add the mark to the list.
7686                     (when (equal to-group gnus-newsgroup-name)
7687                       (set (intern (format "gnus-newsgroup-%s" (caar marks)))
7688                            (cons to-article
7689                                  (symbol-value
7690                                   (intern (format "gnus-newsgroup-%s"
7691                                                   (caar marks)))))))
7692                     ;; Copy the marks to other group.
7693                     (gnus-add-marked-articles
7694                      to-group (cdar marks) (list to-article) info))
7695                   (setq marks (cdr marks)))
7696
7697                 (gnus-request-set-mark to-group (list (list (list to-article)
7698                                                             'set
7699                                                             to-marks))))
7700
7701               (gnus-dribble-enter
7702                (concat "(gnus-group-set-info '"
7703                        (gnus-prin1-to-string (gnus-get-info to-group))
7704                        ")"))))
7705
7706           ;; Update the Xref header in this article to point to
7707           ;; the new crossposted article we have just created.
7708           (when (eq action 'crosspost)
7709             (save-excursion
7710               (set-buffer copy-buf)
7711               (gnus-request-article-this-buffer article gnus-newsgroup-name)
7712               (nnheader-replace-header "Xref" new-xref)
7713               (gnus-request-replace-article
7714                article gnus-newsgroup-name (current-buffer)))))
7715
7716         ;;;!!!Why is this necessary?
7717         (set-buffer gnus-summary-buffer)
7718
7719         (gnus-summary-goto-subject article)
7720         (when (eq action 'move)
7721           (gnus-summary-mark-article article gnus-canceled-mark))))
7722       (gnus-summary-remove-process-mark article))
7723     ;; Re-activate all groups that have been moved to.
7724     (while to-groups
7725       (save-excursion
7726         (set-buffer gnus-group-buffer)
7727         (when (gnus-group-goto-group (car to-groups) t)
7728           (gnus-group-get-new-news-this-group 1 t))
7729         (pop to-groups)))
7730
7731     (gnus-kill-buffer copy-buf)
7732     (gnus-summary-position-point)
7733     (gnus-set-mode-line 'summary)))
7734
7735 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
7736   "Move the current article to a different newsgroup.
7737 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
7738 If SELECT-METHOD is non-nil, do not move to a specific newsgroup, but
7739 re-spool using this method."
7740   (interactive "P")
7741   (gnus-summary-move-article n to-newsgroup select-method 'copy))
7742
7743 (defun gnus-summary-crosspost-article (&optional n)
7744   "Crosspost the current article to some other group."
7745   (interactive "P")
7746   (gnus-summary-move-article n nil nil 'crosspost))
7747
7748 (defcustom gnus-summary-respool-default-method nil
7749   "Default method for respooling an article.
7750 If nil, use to the current newsgroup method."
7751   :type '(choice (gnus-select-method :value (nnml ""))
7752                  (const nil))
7753   :group 'gnus-summary-mail)
7754
7755 (defun gnus-summary-respool-article (&optional n method)
7756   "Respool the current article.
7757 The article will be squeezed through the mail spooling process again,
7758 which means that it will be put in some mail newsgroup or other
7759 depending on `nnmail-split-methods'.
7760 If N is a positive number, respool the N next articles.
7761 If N is a negative number, respool the N previous articles.
7762 If N is nil and any articles have been marked with the process mark,
7763 respool those articles instead.
7764
7765 Respooling can be done both from mail groups and \"real\" newsgroups.
7766 In the former case, the articles in question will be moved from the
7767 current group into whatever groups they are destined to.  In the
7768 latter case, they will be copied into the relevant groups."
7769   (interactive
7770    (list current-prefix-arg
7771          (let* ((methods (gnus-methods-using 'respool))
7772                 (methname
7773                  (symbol-name (or gnus-summary-respool-default-method
7774                                   (car (gnus-find-method-for-group
7775                                         gnus-newsgroup-name)))))
7776                 (method
7777                  (gnus-completing-read
7778                   methname "What backend do you want to use when respooling?"
7779                   methods nil t nil 'gnus-mail-method-history))
7780                 ms)
7781            (cond
7782             ((zerop (length (setq ms (gnus-servers-using-backend
7783                                       (intern method)))))
7784              (list (intern method) ""))
7785             ((= 1 (length ms))
7786              (car ms))
7787             (t
7788              (let ((ms-alist (mapcar (lambda (m) (cons (cadr m) m)) ms)))
7789                (cdr (assoc (completing-read "Server name: " ms-alist nil t)
7790                            ms-alist))))))))
7791   (unless method
7792     (error "No method given for respooling"))
7793   (if (assoc (symbol-name
7794               (car (gnus-find-method-for-group gnus-newsgroup-name)))
7795              (gnus-methods-using 'respool))
7796       (gnus-summary-move-article n nil method)
7797     (gnus-summary-copy-article n nil method)))
7798
7799 (defun gnus-summary-import-article (file)
7800   "Import an arbitrary file into a mail newsgroup."
7801   (interactive "fImport file: ")
7802   (let ((group gnus-newsgroup-name)
7803         (now (current-time))
7804         atts lines)
7805     (unless (gnus-check-backend-function 'request-accept-article group)
7806       (error "%s does not support article importing" group))
7807     (or (file-readable-p file)
7808         (not (file-regular-p file))
7809         (error "Can't read %s" file))
7810     (save-excursion
7811       (set-buffer (gnus-get-buffer-create " *import file*"))
7812       (erase-buffer)
7813       (nnheader-insert-file-contents file)
7814       (goto-char (point-min))
7815       (unless (nnheader-article-p)
7816         ;; This doesn't look like an article, so we fudge some headers.
7817         (setq atts (file-attributes file)
7818               lines (count-lines (point-min) (point-max)))
7819         (insert "From: " (read-string "From: ") "\n"
7820                 "Subject: " (read-string "Subject: ") "\n"
7821                 "Date: " (message-make-date (nth 5 atts))
7822                 "\n"
7823                 "Message-ID: " (message-make-message-id) "\n"
7824                 "Lines: " (int-to-string lines) "\n"
7825                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
7826       (gnus-request-accept-article group nil t)
7827       (kill-buffer (current-buffer)))))
7828
7829 (defun gnus-summary-article-posted-p ()
7830   "Say whether the current (mail) article is available from news as well.
7831 This will be the case if the article has both been mailed and posted."
7832   (interactive)
7833   (let ((id (mail-header-references (gnus-summary-article-header)))
7834         (gnus-override-method (car (gnus-refer-article-methods))))
7835     (if (gnus-request-head id "")
7836         (gnus-message 2 "The current message was found on %s"
7837                       gnus-override-method)
7838       (gnus-message 2 "The current message couldn't be found on %s"
7839                     gnus-override-method)
7840       nil)))
7841
7842 (defun gnus-summary-expire-articles (&optional now)
7843   "Expire all articles that are marked as expirable in the current group."
7844   (interactive)
7845   (when (gnus-check-backend-function
7846          'request-expire-articles gnus-newsgroup-name)
7847     ;; This backend supports expiry.
7848     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
7849            (expirable (if total
7850                           (progn
7851                             ;; We need to update the info for
7852                             ;; this group for `gnus-list-of-read-articles'
7853                             ;; to give us the right answer.
7854                             (gnus-run-hooks 'gnus-exit-group-hook)
7855                             (gnus-summary-update-info)
7856                             (gnus-list-of-read-articles gnus-newsgroup-name))
7857                         (setq gnus-newsgroup-expirable
7858                               (sort gnus-newsgroup-expirable '<))))
7859            (expiry-wait (if now 'immediate
7860                           (gnus-group-find-parameter
7861                            gnus-newsgroup-name 'expiry-wait)))
7862            (nnmail-expiry-target
7863             (or (gnus-group-find-parameter gnus-newsgroup-name 'expiry-target)
7864                 nnmail-expiry-target))
7865            es)
7866       (when expirable
7867         ;; There are expirable articles in this group, so we run them
7868         ;; through the expiry process.
7869         (gnus-message 6 "Expiring articles...")
7870         (unless (gnus-check-group gnus-newsgroup-name)
7871           (error "Can't open server for %s" gnus-newsgroup-name))
7872         ;; The list of articles that weren't expired is returned.
7873         (save-excursion
7874           (if expiry-wait
7875               (let ((nnmail-expiry-wait-function nil)
7876                     (nnmail-expiry-wait expiry-wait))
7877                 (setq es (gnus-request-expire-articles
7878                           expirable gnus-newsgroup-name)))
7879             (setq es (gnus-request-expire-articles
7880                       expirable gnus-newsgroup-name))))
7881         (unless total
7882           (setq gnus-newsgroup-expirable es))
7883         ;; We go through the old list of expirable, and mark all
7884         ;; really expired articles as nonexistent.
7885         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
7886           (let ((gnus-use-cache nil))
7887             (while expirable
7888               (unless (memq (car expirable) es)
7889                 (when (gnus-data-find (car expirable))
7890                   (gnus-summary-mark-article
7891                    (car expirable) gnus-canceled-mark)))
7892               (setq expirable (cdr expirable)))))
7893         (gnus-message 6 "Expiring articles...done")))))
7894
7895 (defun gnus-summary-expire-articles-now ()
7896   "Expunge all expirable articles in the current group.
7897 This means that *all* articles that are marked as expirable will be
7898 deleted forever, right now."
7899   (interactive)
7900   (or gnus-expert-user
7901       (gnus-yes-or-no-p
7902        "Are you really, really, really sure you want to delete all these messages? ")
7903       (error "Phew!"))
7904   (gnus-summary-expire-articles t))
7905
7906 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
7907 (defun gnus-summary-delete-article (&optional n)
7908   "Delete the N next (mail) articles.
7909 This command actually deletes articles.  This is not a marking
7910 command.  The article will disappear forever from your life, never to
7911 return.
7912 If N is negative, delete backwards.
7913 If N is nil and articles have been marked with the process mark,
7914 delete these instead."
7915   (interactive "P")
7916   (unless (gnus-check-backend-function 'request-expire-articles
7917                                        gnus-newsgroup-name)
7918     (error "The current newsgroup does not support article deletion"))
7919   (unless (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
7920     (error "Couldn't open server"))
7921   ;; Compute the list of articles to delete.
7922   (let ((articles (sort (copy-sequence (gnus-summary-work-articles n)) '<))
7923         not-deleted)
7924     (if (and gnus-novice-user
7925              (not (gnus-yes-or-no-p
7926                    (format "Do you really want to delete %s forever? "
7927                            (if (> (length articles) 1)
7928                                (format "these %s articles" (length articles))
7929                              "this article")))))
7930         ()
7931       ;; Delete the articles.
7932       (setq not-deleted (gnus-request-expire-articles
7933                          articles gnus-newsgroup-name 'force))
7934       (while articles
7935         (gnus-summary-remove-process-mark (car articles))
7936         ;; The backend might not have been able to delete the article
7937         ;; after all.
7938         (unless (memq (car articles) not-deleted)
7939           (gnus-summary-mark-article (car articles) gnus-canceled-mark))
7940         (setq articles (cdr articles)))
7941       (when not-deleted
7942         (gnus-message 4 "Couldn't delete articles %s" not-deleted)))
7943     (gnus-summary-position-point)
7944     (gnus-set-mode-line 'summary)
7945     not-deleted))
7946
7947 (defun gnus-summary-edit-article (&optional force)
7948   "Edit the current article.
7949 This will have permanent effect only in mail groups.
7950 If FORCE is non-nil, allow editing of articles even in read-only
7951 groups."
7952   (interactive "P")
7953   (save-excursion
7954     (set-buffer gnus-summary-buffer)
7955     (let ((mail-parse-charset gnus-newsgroup-charset)
7956           (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets))
7957       (gnus-set-global-variables)
7958       (when (and (not force)
7959                  (gnus-group-read-only-p))
7960         (error "The current newsgroup does not support article editing"))
7961       (gnus-summary-show-article t)
7962       (gnus-article-edit-article
7963        'ignore
7964        `(lambda (no-highlight)
7965           (let ((mail-parse-charset ',gnus-newsgroup-charset)
7966                 (mail-parse-ignored-charsets
7967                  ',gnus-newsgroup-ignored-charsets))
7968             (gnus-summary-edit-article-done
7969              ,(or (mail-header-references gnus-current-headers) "")
7970              ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight)))))))
7971
7972 (defalias 'gnus-summary-edit-article-postpone 'gnus-article-edit-exit)
7973
7974 (defun gnus-summary-edit-article-done (&optional references read-only buffer
7975                                                  no-highlight)
7976   "Make edits to the current article permanent."
7977   (interactive)
7978   ;; Replace the article.
7979   (let ((buf (current-buffer)))
7980     (with-temp-buffer
7981       (insert-buffer-substring buf)
7982       (if (and (not read-only)
7983                (not (gnus-request-replace-article
7984                      (cdr gnus-article-current) (car gnus-article-current)
7985                      (current-buffer) t)))
7986           (error "Couldn't replace article")
7987         ;; Update the summary buffer.
7988         (if (and references
7989                  (equal (message-tokenize-header references " ")
7990                         (message-tokenize-header
7991                          (or (message-fetch-field "references") "") " ")))
7992             ;; We only have to update this line.
7993             (save-excursion
7994               (save-restriction
7995                 (message-narrow-to-head)
7996                 (let ((head (buffer-string))
7997                       header)
7998                   (with-temp-buffer
7999                     (insert (format "211 %d Article retrieved.\n"
8000                                     (cdr gnus-article-current)))
8001                     (insert head)
8002                     (insert ".\n")
8003                     (let ((nntp-server-buffer (current-buffer)))
8004                       (setq header (car (gnus-get-newsgroup-headers
8005                                          (save-excursion
8006                                            (set-buffer gnus-summary-buffer)
8007                                            gnus-newsgroup-dependencies)
8008                                          t))))
8009                     (save-excursion
8010                       (set-buffer gnus-summary-buffer)
8011                       (gnus-data-set-header
8012                        (gnus-data-find (cdr gnus-article-current))
8013                        header)
8014                       (gnus-summary-update-article-line
8015                        (cdr gnus-article-current) header))))))
8016           ;; Update threads.
8017           (set-buffer (or buffer gnus-summary-buffer))
8018           (gnus-summary-update-article (cdr gnus-article-current)))
8019         ;; Prettify the article buffer again.
8020         (unless no-highlight
8021           (save-excursion
8022             (set-buffer gnus-article-buffer)
8023             ;;;!!! Fix this -- article should be rehighlighted.
8024             ;;;(gnus-run-hooks 'gnus-article-display-hook)
8025             (set-buffer gnus-original-article-buffer)
8026             (gnus-request-article
8027              (cdr gnus-article-current)
8028              (car gnus-article-current) (current-buffer))))
8029         ;; Prettify the summary buffer line.
8030         (when (gnus-visual-p 'summary-highlight 'highlight)
8031           (gnus-run-hooks 'gnus-visual-mark-article-hook))))))
8032
8033 (defun gnus-summary-edit-wash (key)
8034   "Perform editing command KEY in the article buffer."
8035   (interactive
8036    (list
8037     (progn
8038       (message "%s" (concat (this-command-keys) "- "))
8039       (read-char))))
8040   (message "")
8041   (gnus-summary-edit-article)
8042   (execute-kbd-macro (concat (this-command-keys) key))
8043   (gnus-article-edit-done))
8044
8045 ;;; Respooling
8046
8047 (defun gnus-summary-respool-query (&optional silent trace)
8048   "Query where the respool algorithm would put this article."
8049   (interactive)
8050   (let (gnus-mark-article-hook)
8051     (gnus-summary-select-article)
8052     (save-excursion
8053       (set-buffer gnus-original-article-buffer)
8054       (save-restriction
8055         (message-narrow-to-head)
8056         (let ((groups (nnmail-article-group 'identity trace)))
8057           (unless silent
8058             (if groups
8059                 (message "This message would go to %s"
8060                          (mapconcat 'car groups ", "))
8061               (message "This message would go to no groups"))
8062             groups))))))
8063
8064 (defun gnus-summary-respool-trace ()
8065   "Trace where the respool algorithm would put this article.
8066 Display a buffer showing all fancy splitting patterns which matched."
8067   (interactive)
8068   (gnus-summary-respool-query nil t))
8069
8070 ;; Summary marking commands.
8071
8072 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
8073   "Mark articles which has the same subject as read, and then select the next.
8074 If UNMARK is positive, remove any kind of mark.
8075 If UNMARK is negative, tick articles."
8076   (interactive "P")
8077   (when unmark
8078     (setq unmark (prefix-numeric-value unmark)))
8079   (let ((count
8080          (gnus-summary-mark-same-subject
8081           (gnus-summary-article-subject) unmark)))
8082     ;; Select next unread article.  If auto-select-same mode, should
8083     ;; select the first unread article.
8084     (gnus-summary-next-article t (and gnus-auto-select-same
8085                                       (gnus-summary-article-subject)))
8086     (gnus-message 7 "%d article%s marked as %s"
8087                   count (if (= count 1) " is" "s are")
8088                   (if unmark "unread" "read"))))
8089
8090 (defun gnus-summary-kill-same-subject (&optional unmark)
8091   "Mark articles which has the same subject as read.
8092 If UNMARK is positive, remove any kind of mark.
8093 If UNMARK is negative, tick articles."
8094   (interactive "P")
8095   (when unmark
8096     (setq unmark (prefix-numeric-value unmark)))
8097   (let ((count
8098          (gnus-summary-mark-same-subject
8099           (gnus-summary-article-subject) unmark)))
8100     ;; If marked as read, go to next unread subject.
8101     (when (null unmark)
8102       ;; Go to next unread subject.
8103       (gnus-summary-next-subject 1 t))
8104     (gnus-message 7 "%d articles are marked as %s"
8105                   count (if unmark "unread" "read"))))
8106
8107 (defun gnus-summary-mark-same-subject (subject &optional unmark)
8108   "Mark articles with same SUBJECT as read, and return marked number.
8109 If optional argument UNMARK is positive, remove any kinds of marks.
8110 If optional argument UNMARK is negative, mark articles as unread instead."
8111   (let ((count 1))
8112     (save-excursion
8113       (cond
8114        ((null unmark)                   ; Mark as read.
8115         (while (and
8116                 (progn
8117                   (gnus-summary-mark-article-as-read gnus-killed-mark)
8118                   (gnus-summary-show-thread) t)
8119                 (gnus-summary-find-subject subject))
8120           (setq count (1+ count))))
8121        ((> unmark 0)                    ; Tick.
8122         (while (and
8123                 (progn
8124                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
8125                   (gnus-summary-show-thread) t)
8126                 (gnus-summary-find-subject subject))
8127           (setq count (1+ count))))
8128        (t                               ; Mark as unread.
8129         (while (and
8130                 (progn
8131                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
8132                   (gnus-summary-show-thread) t)
8133                 (gnus-summary-find-subject subject))
8134           (setq count (1+ count)))))
8135       (gnus-set-mode-line 'summary)
8136       ;; Return the number of marked articles.
8137       count)))
8138
8139 (defun gnus-summary-mark-as-processable (n &optional unmark)
8140   "Set the process mark on the next N articles.
8141 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
8142 the process mark instead.  The difference between N and the actual
8143 number of articles marked is returned."
8144   (interactive "p")
8145   (let ((backward (< n 0))
8146         (n (abs n)))
8147     (while (and
8148             (> n 0)
8149             (if unmark
8150                 (gnus-summary-remove-process-mark
8151                  (gnus-summary-article-number))
8152               (gnus-summary-set-process-mark (gnus-summary-article-number)))
8153             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
8154       (setq n (1- n)))
8155     (when (/= 0 n)
8156       (gnus-message 7 "No more articles"))
8157     (gnus-summary-recenter)
8158     (gnus-summary-position-point)
8159     n))
8160
8161 (defun gnus-summary-unmark-as-processable (n)
8162   "Remove the process mark from the next N articles.
8163 If N is negative, unmark backward instead.  The difference between N and
8164 the actual number of articles unmarked is returned."
8165   (interactive "p")
8166   (gnus-summary-mark-as-processable n t))
8167
8168 (defun gnus-summary-unmark-all-processable ()
8169   "Remove the process mark from all articles."
8170   (interactive)
8171   (save-excursion
8172     (while gnus-newsgroup-processable
8173       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
8174   (gnus-summary-position-point))
8175
8176 (defun gnus-summary-mark-as-expirable (n)
8177   "Mark N articles forward as expirable.
8178 If N is negative, mark backward instead.  The difference between N and
8179 the actual number of articles marked is returned."
8180   (interactive "p")
8181   (gnus-summary-mark-forward n gnus-expirable-mark))
8182
8183 (defun gnus-summary-mark-article-as-replied (article)
8184   "Mark ARTICLE replied and update the summary line."
8185   (push article gnus-newsgroup-replied)
8186   (let ((buffer-read-only nil))
8187     (when (gnus-summary-goto-subject article nil t)
8188       (gnus-summary-update-secondary-mark article))))
8189
8190 (defun gnus-summary-set-bookmark (article)
8191   "Set a bookmark in current article."
8192   (interactive (list (gnus-summary-article-number)))
8193   (when (or (not (get-buffer gnus-article-buffer))
8194             (not gnus-current-article)
8195             (not gnus-article-current)
8196             (not (equal gnus-newsgroup-name (car gnus-article-current))))
8197     (error "No current article selected"))
8198   ;; Remove old bookmark, if one exists.
8199   (let ((old (assq article gnus-newsgroup-bookmarks)))
8200     (when old
8201       (setq gnus-newsgroup-bookmarks
8202             (delq old gnus-newsgroup-bookmarks))))
8203   ;; Set the new bookmark, which is on the form
8204   ;; (article-number . line-number-in-body).
8205   (push
8206    (cons article
8207          (save-excursion
8208            (set-buffer gnus-article-buffer)
8209            (count-lines
8210             (min (point)
8211                  (save-excursion
8212                    (goto-char (point-min))
8213                    (search-forward "\n\n" nil t)
8214                    (point)))
8215             (point))))
8216    gnus-newsgroup-bookmarks)
8217   (gnus-message 6 "A bookmark has been added to the current article."))
8218
8219 (defun gnus-summary-remove-bookmark (article)
8220   "Remove the bookmark from the current article."
8221   (interactive (list (gnus-summary-article-number)))
8222   ;; Remove old bookmark, if one exists.
8223   (let ((old (assq article gnus-newsgroup-bookmarks)))
8224     (if old
8225         (progn
8226           (setq gnus-newsgroup-bookmarks
8227                 (delq old gnus-newsgroup-bookmarks))
8228           (gnus-message 6 "Removed bookmark."))
8229       (gnus-message 6 "No bookmark in current article."))))
8230
8231 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8232 (defun gnus-summary-mark-as-dormant (n)
8233   "Mark N articles forward as dormant.
8234 If N is negative, mark backward instead.  The difference between N and
8235 the actual number of articles marked is returned."
8236   (interactive "p")
8237   (gnus-summary-mark-forward n gnus-dormant-mark))
8238
8239 (defun gnus-summary-set-process-mark (article)
8240   "Set the process mark on ARTICLE and update the summary line."
8241   (setq gnus-newsgroup-processable
8242         (cons article
8243               (delq article gnus-newsgroup-processable)))
8244   (when (gnus-summary-goto-subject article)
8245     (gnus-summary-show-thread)
8246     (gnus-summary-goto-subject article)
8247     (gnus-summary-update-secondary-mark article)))
8248
8249 (defun gnus-summary-remove-process-mark (article)
8250   "Remove the process mark from ARTICLE and update the summary line."
8251   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
8252   (when (gnus-summary-goto-subject article)
8253     (gnus-summary-show-thread)
8254     (gnus-summary-goto-subject article)
8255     (gnus-summary-update-secondary-mark article)))
8256
8257 (defun gnus-summary-set-saved-mark (article)
8258   "Set the process mark on ARTICLE and update the summary line."
8259   (push article gnus-newsgroup-saved)
8260   (when (gnus-summary-goto-subject article)
8261     (gnus-summary-update-secondary-mark article)))
8262
8263 (defun gnus-summary-mark-forward (n &optional mark no-expire)
8264   "Mark N articles as read forwards.
8265 If N is negative, mark backwards instead.  Mark with MARK, ?r by default.
8266 The difference between N and the actual number of articles marked is
8267 returned.
8268 Iff NO-EXPIRE, auto-expiry will be inhibited."
8269   (interactive "p")
8270   (gnus-summary-show-thread)
8271   (let ((backward (< n 0))
8272         (gnus-summary-goto-unread
8273          (and gnus-summary-goto-unread
8274               (not (eq gnus-summary-goto-unread 'never))
8275               (not (memq mark (list gnus-unread-mark
8276                                     gnus-ticked-mark gnus-dormant-mark)))))
8277         (n (abs n))
8278         (mark (or mark gnus-del-mark)))
8279     (while (and (> n 0)
8280                 (gnus-summary-mark-article nil mark no-expire)
8281                 (zerop (gnus-summary-next-subject
8282                         (if backward -1 1)
8283                         (and gnus-summary-goto-unread
8284                              (not (eq gnus-summary-goto-unread 'never)))
8285                         t)))
8286       (setq n (1- n)))
8287     (when (/= 0 n)
8288       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
8289     (gnus-summary-recenter)
8290     (gnus-summary-position-point)
8291     (gnus-set-mode-line 'summary)
8292     n))
8293
8294 (defun gnus-summary-mark-article-as-read (mark)
8295   "Mark the current article quickly as read with MARK."
8296   (let ((article (gnus-summary-article-number)))
8297     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8298     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8299     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8300     (push (cons article mark) gnus-newsgroup-reads)
8301     ;; Possibly remove from cache, if that is used.
8302     (when gnus-use-cache
8303       (gnus-cache-enter-remove-article article))
8304     ;; Allow the backend to change the mark.
8305     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8306     ;; Check for auto-expiry.
8307     (when (and gnus-newsgroup-auto-expire
8308                (memq mark gnus-auto-expirable-marks))
8309       (setq mark gnus-expirable-mark)
8310       ;; Let the backend know about the mark change.
8311       (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8312       (push article gnus-newsgroup-expirable))
8313     ;; Set the mark in the buffer.
8314     (gnus-summary-update-mark mark 'unread)
8315     t))
8316
8317 (defun gnus-summary-mark-article-as-unread (mark)
8318   "Mark the current article quickly as unread with MARK."
8319   (let* ((article (gnus-summary-article-number))
8320          (old-mark (gnus-summary-article-mark article)))
8321     ;; Allow the backend to change the mark.
8322     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8323     (if (eq mark old-mark)
8324         t
8325       (if (<= article 0)
8326           (progn
8327             (gnus-error 1 "Can't mark negative article numbers")
8328             nil)
8329         (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8330         (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8331         (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
8332         (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
8333         (cond ((= mark gnus-ticked-mark)
8334                (push article gnus-newsgroup-marked))
8335               ((= mark gnus-dormant-mark)
8336                (push article gnus-newsgroup-dormant))
8337               (t
8338                (push article gnus-newsgroup-unreads)))
8339         (gnus-pull article gnus-newsgroup-reads)
8340
8341         ;; See whether the article is to be put in the cache.
8342         (and gnus-use-cache
8343              (vectorp (gnus-summary-article-header article))
8344              (save-excursion
8345                (gnus-cache-possibly-enter-article
8346                 gnus-newsgroup-name article
8347                 (gnus-summary-article-header article)
8348                 (= mark gnus-ticked-mark)
8349                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8350
8351         ;; Fix the mark.
8352         (gnus-summary-update-mark mark 'unread)
8353         t))))
8354
8355 (defun gnus-summary-mark-article (&optional article mark no-expire)
8356   "Mark ARTICLE with MARK.  MARK can be any character.
8357 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
8358 `??' (dormant) and `?E' (expirable).
8359 If MARK is nil, then the default character `?r' is used.
8360 If ARTICLE is nil, then the article on the current line will be
8361 marked.
8362 Iff NO-EXPIRE, auto-expiry will be inhibited."
8363   ;; The mark might be a string.
8364   (when (stringp mark)
8365     (setq mark (aref mark 0)))
8366   ;; If no mark is given, then we check auto-expiring.
8367   (when (null mark)
8368     (setq mark gnus-del-mark))
8369   (when (and (not no-expire)
8370              gnus-newsgroup-auto-expire
8371              (memq mark gnus-auto-expirable-marks))
8372     (setq mark gnus-expirable-mark))
8373   (let ((article (or article (gnus-summary-article-number)))
8374         (old-mark (gnus-summary-article-mark article)))
8375     ;; Allow the backend to change the mark.
8376     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
8377     (if (eq mark old-mark)
8378         t
8379       (unless article
8380         (error "No article on current line"))
8381       (if (not (if (or (= mark gnus-unread-mark)
8382                        (= mark gnus-ticked-mark)
8383                        (= mark gnus-dormant-mark))
8384                    (gnus-mark-article-as-unread article mark)
8385                  (gnus-mark-article-as-read article mark)))
8386           t
8387         ;; See whether the article is to be put in the cache.
8388         (and gnus-use-cache
8389              (not (= mark gnus-canceled-mark))
8390              (vectorp (gnus-summary-article-header article))
8391              (save-excursion
8392                (gnus-cache-possibly-enter-article
8393                 gnus-newsgroup-name article
8394                 (gnus-summary-article-header article)
8395                 (= mark gnus-ticked-mark)
8396                 (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
8397
8398         (when (gnus-summary-goto-subject article nil t)
8399           (let ((buffer-read-only nil))
8400             (gnus-summary-show-thread)
8401             ;; Fix the mark.
8402             (gnus-summary-update-mark mark 'unread)
8403             t))))))
8404
8405 (defun gnus-summary-update-secondary-mark (article)
8406   "Update the secondary (read, process, cache) mark."
8407   (gnus-summary-update-mark
8408    (cond ((memq article gnus-newsgroup-processable)
8409           gnus-process-mark)
8410          ((memq article gnus-newsgroup-cached)
8411           gnus-cached-mark)
8412          ((memq article gnus-newsgroup-replied)
8413           gnus-replied-mark)
8414          ((memq article gnus-newsgroup-saved)
8415           gnus-saved-mark)
8416          (t gnus-unread-mark))
8417    'replied)
8418   (when (gnus-visual-p 'summary-highlight 'highlight)
8419     (gnus-run-hooks 'gnus-summary-update-hook))
8420   t)
8421
8422 (defun gnus-summary-update-mark (mark type)
8423   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
8424         (buffer-read-only nil))
8425     (re-search-backward "[\n\r]" (gnus-point-at-bol) 'move-to-limit)
8426     (when forward
8427       (when (looking-at "\r")
8428         (incf forward))
8429       (when (<= (+ forward (point)) (point-max))
8430         ;; Go to the right position on the line.
8431         (goto-char (+ forward (point)))
8432         ;; Replace the old mark with the new mark.
8433         (subst-char-in-region (point) (1+ (point)) (char-after) mark)
8434         ;; Optionally update the marks by some user rule.
8435         (when (eq type 'unread)
8436           (gnus-data-set-mark
8437            (gnus-data-find (gnus-summary-article-number)) mark)
8438           (gnus-summary-update-line (eq mark gnus-unread-mark)))))))
8439
8440 (defun gnus-mark-article-as-read (article &optional mark)
8441   "Enter ARTICLE in the pertinent lists and remove it from others."
8442   ;; Make the article expirable.
8443   (let ((mark (or mark gnus-del-mark)))
8444     (if (= mark gnus-expirable-mark)
8445         (push article gnus-newsgroup-expirable)
8446       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
8447     ;; Remove from unread and marked lists.
8448     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8449     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
8450     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
8451     (push (cons article mark) gnus-newsgroup-reads)
8452     ;; Possibly remove from cache, if that is used.
8453     (when gnus-use-cache
8454       (gnus-cache-enter-remove-article article))
8455     t))
8456
8457 (defun gnus-mark-article-as-unread (article &optional mark)
8458   "Enter ARTICLE in the pertinent lists and remove it from others."
8459   (let ((mark (or mark gnus-ticked-mark)))
8460     (if (<= article 0)
8461         (progn
8462           (gnus-error 1 "Can't mark negative article numbers")
8463           nil)
8464       (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked)
8465             gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant)
8466             gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)
8467             gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
8468
8469       ;; Unsuppress duplicates?
8470       (when gnus-suppress-duplicates
8471         (gnus-dup-unsuppress-article article))
8472
8473       (cond ((= mark gnus-ticked-mark)
8474              (push article gnus-newsgroup-marked))
8475             ((= mark gnus-dormant-mark)
8476              (push article gnus-newsgroup-dormant))
8477             (t
8478              (push article gnus-newsgroup-unreads)))
8479       (gnus-pull article gnus-newsgroup-reads)
8480       t)))
8481
8482 (defalias 'gnus-summary-mark-as-unread-forward
8483   'gnus-summary-tick-article-forward)
8484 (make-obsolete 'gnus-summary-mark-as-unread-forward
8485                'gnus-summary-tick-article-forward)
8486 (defun gnus-summary-tick-article-forward (n)
8487   "Tick N articles forwards.
8488 If N is negative, tick backwards instead.
8489 The difference between N and the number of articles ticked is returned."
8490   (interactive "p")
8491   (gnus-summary-mark-forward n gnus-ticked-mark))
8492
8493 (defalias 'gnus-summary-mark-as-unread-backward
8494   'gnus-summary-tick-article-backward)
8495 (make-obsolete 'gnus-summary-mark-as-unread-backward
8496                'gnus-summary-tick-article-backward)
8497 (defun gnus-summary-tick-article-backward (n)
8498   "Tick N articles backwards.
8499 The difference between N and the number of articles ticked is returned."
8500   (interactive "p")
8501   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
8502
8503 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8504 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
8505 (defun gnus-summary-tick-article (&optional article clear-mark)
8506   "Mark current article as unread.
8507 Optional 1st argument ARTICLE specifies article number to be marked as unread.
8508 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
8509   (interactive)
8510   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
8511                                        gnus-ticked-mark)))
8512
8513 (defun gnus-summary-mark-as-read-forward (n)
8514   "Mark N articles as read forwards.
8515 If N is negative, mark backwards instead.
8516 The difference between N and the actual number of articles marked is
8517 returned."
8518   (interactive "p")
8519   (gnus-summary-mark-forward n gnus-del-mark gnus-inhibit-user-auto-expire))
8520
8521 (defun gnus-summary-mark-as-read-backward (n)
8522   "Mark the N articles as read backwards.
8523 The difference between N and the actual number of articles marked is
8524 returned."
8525   (interactive "p")
8526   (gnus-summary-mark-forward
8527    (- n) gnus-del-mark gnus-inhibit-user-auto-expire))
8528
8529 (defun gnus-summary-mark-as-read (&optional article mark)
8530   "Mark current article as read.
8531 ARTICLE specifies the article to be marked as read.
8532 MARK specifies a string to be inserted at the beginning of the line."
8533   (gnus-summary-mark-article article mark))
8534
8535 (defun gnus-summary-clear-mark-forward (n)
8536   "Clear marks from N articles forward.
8537 If N is negative, clear backward instead.
8538 The difference between N and the number of marks cleared is returned."
8539   (interactive "p")
8540   (gnus-summary-mark-forward n gnus-unread-mark))
8541
8542 (defun gnus-summary-clear-mark-backward (n)
8543   "Clear marks from N articles backward.
8544 The difference between N and the number of marks cleared is returned."
8545   (interactive "p")
8546   (gnus-summary-mark-forward (- n) gnus-unread-mark))
8547
8548 (defun gnus-summary-mark-unread-as-read ()
8549   "Intended to be used by `gnus-summary-mark-article-hook'."
8550   (when (memq gnus-current-article gnus-newsgroup-unreads)
8551     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
8552
8553 (defun gnus-summary-mark-read-and-unread-as-read ()
8554   "Intended to be used by `gnus-summary-mark-article-hook'."
8555   (let ((mark (gnus-summary-article-mark)))
8556     (when (or (gnus-unread-mark-p mark)
8557               (gnus-read-mark-p mark))
8558       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
8559
8560 (defun gnus-summary-mark-region-as-read (point mark all)
8561   "Mark all unread articles between point and mark as read.
8562 If given a prefix, mark all articles between point and mark as read,
8563 even ticked and dormant ones."
8564   (interactive "r\nP")
8565   (save-excursion
8566     (let (article)
8567       (goto-char point)
8568       (beginning-of-line)
8569       (while (and
8570               (< (point) mark)
8571               (progn
8572                 (when (or all
8573                           (memq (setq article (gnus-summary-article-number))
8574                                 gnus-newsgroup-unreads))
8575                   (gnus-summary-mark-article article gnus-del-mark))
8576                 t)
8577               (gnus-summary-find-next))))))
8578
8579 (defun gnus-summary-mark-below (score mark)
8580   "Mark articles with score less than SCORE with MARK."
8581   (interactive "P\ncMark: ")
8582   (setq score (if score
8583                   (prefix-numeric-value score)
8584                 (or gnus-summary-default-score 0)))
8585   (save-excursion
8586     (set-buffer gnus-summary-buffer)
8587     (goto-char (point-min))
8588     (while
8589         (progn
8590           (and (< (gnus-summary-article-score) score)
8591                (gnus-summary-mark-article nil mark))
8592           (gnus-summary-find-next)))))
8593
8594 (defun gnus-summary-kill-below (&optional score)
8595   "Mark articles with score below SCORE as read."
8596   (interactive "P")
8597   (gnus-summary-mark-below score gnus-killed-mark))
8598
8599 (defun gnus-summary-clear-above (&optional score)
8600   "Clear all marks from articles with score above SCORE."
8601   (interactive "P")
8602   (gnus-summary-mark-above score gnus-unread-mark))
8603
8604 (defun gnus-summary-tick-above (&optional score)
8605   "Tick all articles with score above SCORE."
8606   (interactive "P")
8607   (gnus-summary-mark-above score gnus-ticked-mark))
8608
8609 (defun gnus-summary-mark-above (score mark)
8610   "Mark articles with score over SCORE with MARK."
8611   (interactive "P\ncMark: ")
8612   (setq score (if score
8613                   (prefix-numeric-value score)
8614                 (or gnus-summary-default-score 0)))
8615   (save-excursion
8616     (set-buffer gnus-summary-buffer)
8617     (goto-char (point-min))
8618     (while (and (progn
8619                   (when (> (gnus-summary-article-score) score)
8620                     (gnus-summary-mark-article nil mark))
8621                   t)
8622                 (gnus-summary-find-next)))))
8623
8624 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
8625 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
8626 (defun gnus-summary-limit-include-expunged (&optional no-error)
8627   "Display all the hidden articles that were expunged for low scores."
8628   (interactive)
8629   (let ((buffer-read-only nil))
8630     (let ((scored gnus-newsgroup-scored)
8631           headers h)
8632       (while scored
8633         (unless (gnus-summary-goto-subject (caar scored))
8634           (and (setq h (gnus-summary-article-header (caar scored)))
8635                (< (cdar scored) gnus-summary-expunge-below)
8636                (push h headers)))
8637         (setq scored (cdr scored)))
8638       (if (not headers)
8639           (when (not no-error)
8640             (error "No expunged articles hidden"))
8641         (goto-char (point-min))
8642         (gnus-summary-prepare-unthreaded (nreverse headers))
8643         (goto-char (point-min))
8644         (gnus-summary-position-point)
8645         t))))
8646
8647 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
8648   "Mark all unread articles in this newsgroup as read.
8649 If prefix argument ALL is non-nil, ticked and dormant articles will
8650 also be marked as read.
8651 If QUIETLY is non-nil, no questions will be asked.
8652 If TO-HERE is non-nil, it should be a point in the buffer.  All
8653 articles before this point will be marked as read.
8654 Note that this function will only catch up the unread article
8655 in the current summary buffer limitation.
8656 The number of articles marked as read is returned."
8657   (interactive "P")
8658   (prog1
8659       (save-excursion
8660         (when (or quietly
8661                   (not gnus-interactive-catchup) ;Without confirmation?
8662                   gnus-expert-user
8663                   (gnus-y-or-n-p
8664                    (if all
8665                        "Mark absolutely all articles as read? "
8666                      "Mark all unread articles as read? ")))
8667           (if (and not-mark
8668                    (not gnus-newsgroup-adaptive)
8669                    (not gnus-newsgroup-auto-expire)
8670                    (not gnus-suppress-duplicates)
8671                    (or (not gnus-use-cache)
8672                        (eq gnus-use-cache 'passive)))
8673               (progn
8674                 (when all
8675                   (setq gnus-newsgroup-marked nil
8676                         gnus-newsgroup-dormant nil))
8677                 (setq gnus-newsgroup-unreads gnus-newsgroup-downloadable))
8678             ;; We actually mark all articles as canceled, which we
8679             ;; have to do when using auto-expiry or adaptive scoring.
8680             (gnus-summary-show-all-threads)
8681             (when (gnus-summary-first-subject (not all) t)
8682               (while (and
8683                       (if to-here (< (point) to-here) t)
8684                       (gnus-summary-mark-article-as-read gnus-catchup-mark)
8685                       (gnus-summary-find-next (not all) nil nil t))))
8686             (gnus-set-mode-line 'summary))
8687           t))
8688     (gnus-summary-position-point)))
8689
8690 (defun gnus-summary-catchup-to-here (&optional all)
8691   "Mark all unticked articles before the current one as read.
8692 If ALL is non-nil, also mark ticked and dormant articles as read."
8693   (interactive "P")
8694   (save-excursion
8695     (gnus-save-hidden-threads
8696       (let ((beg (point)))
8697         ;; We check that there are unread articles.
8698         (when (or all (gnus-summary-find-prev))
8699           (gnus-summary-catchup all t beg)))))
8700   (gnus-summary-position-point))
8701
8702 (defun gnus-summary-catchup-all (&optional quietly)
8703   "Mark all articles in this newsgroup as read."
8704   (interactive "P")
8705   (gnus-summary-catchup t quietly))
8706
8707 (defun gnus-summary-catchup-and-exit (&optional all quietly)
8708   "Mark all unread articles in this group as read, then exit.
8709 If prefix argument ALL is non-nil, all articles are marked as read."
8710   (interactive "P")
8711   (when (gnus-summary-catchup all quietly nil 'fast)
8712     ;; Select next newsgroup or exit.
8713     (if (and (not (gnus-group-quit-config gnus-newsgroup-name))
8714              (eq gnus-auto-select-next 'quietly))
8715         (gnus-summary-next-group nil)
8716       (gnus-summary-exit))))
8717
8718 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
8719   "Mark all articles in this newsgroup as read, and then exit."
8720   (interactive "P")
8721   (gnus-summary-catchup-and-exit t quietly))
8722
8723 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
8724   "Mark all articles in this group as read and select the next group.
8725 If given a prefix, mark all articles, unread as well as ticked, as
8726 read."
8727   (interactive "P")
8728   (save-excursion
8729     (gnus-summary-catchup all))
8730   (gnus-summary-next-group))
8731
8732 ;;;
8733 ;;; with article
8734 ;;;
8735
8736 (defmacro gnus-with-article (article &rest forms)
8737   "Select ARTICLE and perform FORMS in the original article buffer.
8738 Then replace the article with the result."
8739   `(progn
8740      ;; We don't want the article to be marked as read.
8741      (let (gnus-mark-article-hook)
8742        (gnus-summary-select-article t t nil ,article))
8743      (set-buffer gnus-original-article-buffer)
8744      ,@forms
8745      (if (not (gnus-check-backend-function
8746                'request-replace-article (car gnus-article-current)))
8747          (gnus-message 5 "Read-only group; not replacing")
8748        (unless (gnus-request-replace-article
8749                 ,article (car gnus-article-current)
8750                 (current-buffer) t)
8751          (error "Couldn't replace article")))
8752      ;; The cache and backlog have to be flushed somewhat.
8753      (when gnus-keep-backlog
8754        (gnus-backlog-remove-article
8755         (car gnus-article-current) (cdr gnus-article-current)))
8756      (when gnus-use-cache
8757        (gnus-cache-update-article
8758         (car gnus-article-current) (cdr gnus-article-current)))))
8759
8760 (put 'gnus-with-article 'lisp-indent-function 1)
8761 (put 'gnus-with-article 'edebug-form-spec '(form body))
8762
8763 ;; Thread-based commands.
8764
8765 (defun gnus-summary-articles-in-thread (&optional article)
8766   "Return a list of all articles in the current thread.
8767 If ARTICLE is non-nil, return all articles in the thread that starts
8768 with that article."
8769   (let* ((article (or article (gnus-summary-article-number)))
8770          (data (gnus-data-find-list article))
8771          (top-level (gnus-data-level (car data)))
8772          (top-subject
8773           (cond ((null gnus-thread-operation-ignore-subject)
8774                  (gnus-simplify-subject-re
8775                   (mail-header-subject (gnus-data-header (car data)))))
8776                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
8777                  (gnus-simplify-subject-fuzzy
8778                   (mail-header-subject (gnus-data-header (car data)))))
8779                 (t nil)))
8780          (end-point (save-excursion
8781                       (if (gnus-summary-go-to-next-thread)
8782                           (point) (point-max))))
8783          articles)
8784     (while (and data
8785                 (< (gnus-data-pos (car data)) end-point))
8786       (when (or (not top-subject)
8787                 (string= top-subject
8788                          (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
8789                              (gnus-simplify-subject-fuzzy
8790                               (mail-header-subject
8791                                (gnus-data-header (car data))))
8792                            (gnus-simplify-subject-re
8793                             (mail-header-subject
8794                              (gnus-data-header (car data)))))))
8795         (push (gnus-data-number (car data)) articles))
8796       (unless (and (setq data (cdr data))
8797                    (> (gnus-data-level (car data)) top-level))
8798         (setq data nil)))
8799     ;; Return the list of articles.
8800     (nreverse articles)))
8801
8802 (defun gnus-summary-rethread-current ()
8803   "Rethread the thread the current article is part of."
8804   (interactive)
8805   (let* ((gnus-show-threads t)
8806          (article (gnus-summary-article-number))
8807          (id (mail-header-id (gnus-summary-article-header)))
8808          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
8809     (unless id
8810       (error "No article on the current line"))
8811     (gnus-rebuild-thread id)
8812     (gnus-summary-goto-subject article)))
8813
8814 (defun gnus-summary-reparent-thread ()
8815   "Make the current article child of the marked (or previous) article.
8816
8817 Note that the re-threading will only work if `gnus-thread-ignore-subject'
8818 is non-nil or the Subject: of both articles are the same."
8819   (interactive)
8820   (unless (not (gnus-group-read-only-p))
8821     (error "The current newsgroup does not support article editing"))
8822   (unless (<= (length gnus-newsgroup-processable) 1)
8823     (error "No more than one article may be marked"))
8824   (save-window-excursion
8825     (let ((gnus-article-buffer " *reparent*")
8826           (current-article (gnus-summary-article-number))
8827           ;; First grab the marked article, otherwise one line up.
8828           (parent-article (if (not (null gnus-newsgroup-processable))
8829                               (car gnus-newsgroup-processable)
8830                             (save-excursion
8831                               (if (eq (forward-line -1) 0)
8832                                   (gnus-summary-article-number)
8833                                 (error "Beginning of summary buffer"))))))
8834       (unless (not (eq current-article parent-article))
8835         (error "An article may not be self-referential"))
8836       (let ((message-id (mail-header-id
8837                          (gnus-summary-article-header parent-article))))
8838         (unless (and message-id (not (equal message-id "")))
8839           (error "No message-id in desired parent"))
8840         (gnus-with-article current-article
8841           (save-restriction
8842             (goto-char (point-min))
8843             (message-narrow-to-head)
8844             (if (re-search-forward "^References: " nil t)
8845                 (progn
8846                   (re-search-forward "^[^ \t]" nil t)
8847                   (forward-line -1)
8848                   (end-of-line)
8849                   (insert " " message-id))
8850               (insert "References: " message-id "\n"))))
8851         (set-buffer gnus-summary-buffer)
8852         (gnus-summary-unmark-all-processable)
8853         (gnus-summary-update-article current-article)
8854         (gnus-summary-rethread-current)
8855         (gnus-message 3 "Article %d is now the child of article %d"
8856                       current-article parent-article)))))
8857
8858 (defun gnus-summary-toggle-threads (&optional arg)
8859   "Toggle showing conversation threads.
8860 If ARG is positive number, turn showing conversation threads on."
8861   (interactive "P")
8862   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
8863     (setq gnus-show-threads
8864           (if (null arg) (not gnus-show-threads)
8865             (> (prefix-numeric-value arg) 0)))
8866     (gnus-summary-prepare)
8867     (gnus-summary-goto-subject current)
8868     (gnus-message 6 "Threading is now %s" (if gnus-show-threads "on" "off"))
8869     (gnus-summary-position-point)))
8870
8871 (defun gnus-summary-show-all-threads ()
8872   "Show all threads."
8873   (interactive)
8874   (save-excursion
8875     (let ((buffer-read-only nil))
8876       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
8877   (gnus-summary-position-point))
8878
8879 (defun gnus-summary-show-thread ()
8880   "Show thread subtrees.
8881 Returns nil if no thread was there to be shown."
8882   (interactive)
8883   (let ((buffer-read-only nil)
8884         (orig (point))
8885         ;; first goto end then to beg, to have point at beg after let
8886         (end (progn (end-of-line) (point)))
8887         (beg (progn (beginning-of-line) (point))))
8888     (prog1
8889         ;; Any hidden lines here?
8890         (search-forward "\r" end t)
8891       (subst-char-in-region beg end ?\^M ?\n t)
8892       (goto-char orig)
8893       (gnus-summary-position-point))))
8894
8895 (defun gnus-summary-hide-all-threads ()
8896   "Hide all thread subtrees."
8897   (interactive)
8898   (save-excursion
8899     (goto-char (point-min))
8900     (gnus-summary-hide-thread)
8901     (while (zerop (gnus-summary-next-thread 1 t))
8902       (gnus-summary-hide-thread)))
8903   (gnus-summary-position-point))
8904
8905 (defun gnus-summary-hide-thread ()
8906   "Hide thread subtrees.
8907 Returns nil if no threads were there to be hidden."
8908   (interactive)
8909   (let ((buffer-read-only nil)
8910         (start (point))
8911         (article (gnus-summary-article-number)))
8912     (goto-char start)
8913     ;; Go forward until either the buffer ends or the subthread
8914     ;; ends.
8915     (when (and (not (eobp))
8916                (or (zerop (gnus-summary-next-thread 1 t))
8917                    (goto-char (point-max))))
8918       (prog1
8919           (if (and (> (point) start)
8920                    (search-backward "\n" start t))
8921               (progn
8922                 (subst-char-in-region start (point) ?\n ?\^M)
8923                 (gnus-summary-goto-subject article))
8924             (goto-char start)
8925             nil)))))
8926
8927 (defun gnus-summary-go-to-next-thread (&optional previous)
8928   "Go to the same level (or less) next thread.
8929 If PREVIOUS is non-nil, go to previous thread instead.
8930 Return the article number moved to, or nil if moving was impossible."
8931   (let ((level (gnus-summary-thread-level))
8932         (way (if previous -1 1))
8933         (beg (point)))
8934     (forward-line way)
8935     (while (and (not (eobp))
8936                 (< level (gnus-summary-thread-level)))
8937       (forward-line way))
8938     (if (eobp)
8939         (progn
8940           (goto-char beg)
8941           nil)
8942       (setq beg (point))
8943       (prog1
8944           (gnus-summary-article-number)
8945         (goto-char beg)))))
8946
8947 (defun gnus-summary-next-thread (n &optional silent)
8948   "Go to the same level next N'th thread.
8949 If N is negative, search backward instead.
8950 Returns the difference between N and the number of skips actually
8951 done.
8952
8953 If SILENT, don't output messages."
8954   (interactive "p")
8955   (let ((backward (< n 0))
8956         (n (abs n)))
8957     (while (and (> n 0)
8958                 (gnus-summary-go-to-next-thread backward))
8959       (decf n))
8960     (unless silent
8961       (gnus-summary-position-point))
8962     (when (and (not silent) (/= 0 n))
8963       (gnus-message 7 "No more threads"))
8964     n))
8965
8966 (defun gnus-summary-prev-thread (n)
8967   "Go to the same level previous N'th thread.
8968 Returns the difference between N and the number of skips actually
8969 done."
8970   (interactive "p")
8971   (gnus-summary-next-thread (- n)))
8972
8973 (defun gnus-summary-go-down-thread ()
8974   "Go down one level in the current thread."
8975   (let ((children (gnus-summary-article-children)))
8976     (when children
8977       (gnus-summary-goto-subject (car children)))))
8978
8979 (defun gnus-summary-go-up-thread ()
8980   "Go up one level in the current thread."
8981   (let ((parent (gnus-summary-article-parent)))
8982     (when parent
8983       (gnus-summary-goto-subject parent))))
8984
8985 (defun gnus-summary-down-thread (n)
8986   "Go down thread N steps.
8987 If N is negative, go up instead.
8988 Returns the difference between N and how many steps down that were
8989 taken."
8990   (interactive "p")
8991   (let ((up (< n 0))
8992         (n (abs n)))
8993     (while (and (> n 0)
8994                 (if up (gnus-summary-go-up-thread)
8995                   (gnus-summary-go-down-thread)))
8996       (setq n (1- n)))
8997     (gnus-summary-position-point)
8998     (when (/= 0 n)
8999       (gnus-message 7 "Can't go further"))
9000     n))
9001
9002 (defun gnus-summary-up-thread (n)
9003   "Go up thread N steps.
9004 If N is negative, go up instead.
9005 Returns the difference between N and how many steps down that were
9006 taken."
9007   (interactive "p")
9008   (gnus-summary-down-thread (- n)))
9009
9010 (defun gnus-summary-top-thread ()
9011   "Go to the top of the thread."
9012   (interactive)
9013   (while (gnus-summary-go-up-thread))
9014   (gnus-summary-article-number))
9015
9016 (defun gnus-summary-kill-thread (&optional unmark)
9017   "Mark articles under current thread as read.
9018 If the prefix argument is positive, remove any kinds of marks.
9019 If the prefix argument is negative, tick articles instead."
9020   (interactive "P")
9021   (when unmark
9022     (setq unmark (prefix-numeric-value unmark)))
9023   (let ((articles (gnus-summary-articles-in-thread)))
9024     (save-excursion
9025       ;; Expand the thread.
9026       (gnus-summary-show-thread)
9027       ;; Mark all the articles.
9028       (while articles
9029         (gnus-summary-goto-subject (car articles))
9030         (cond ((null unmark)
9031                (gnus-summary-mark-article-as-read gnus-killed-mark))
9032               ((> unmark 0)
9033                (gnus-summary-mark-article-as-unread gnus-unread-mark))
9034               (t
9035                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
9036         (setq articles (cdr articles))))
9037     ;; Hide killed subtrees.
9038     (and (null unmark)
9039          gnus-thread-hide-killed
9040          (gnus-summary-hide-thread))
9041     ;; If marked as read, go to next unread subject.
9042     (when (null unmark)
9043       ;; Go to next unread subject.
9044       (gnus-summary-next-subject 1 t)))
9045   (gnus-set-mode-line 'summary))
9046
9047 ;; Summary sorting commands
9048
9049 (defun gnus-summary-sort-by-number (&optional reverse)
9050   "Sort the summary buffer by article number.
9051 Argument REVERSE means reverse order."
9052   (interactive "P")
9053   (gnus-summary-sort 'number reverse))
9054
9055 (defun gnus-summary-sort-by-author (&optional reverse)
9056   "Sort the summary buffer by author name alphabetically.
9057 If `case-fold-search' is non-nil, case of letters is ignored.
9058 Argument REVERSE means reverse order."
9059   (interactive "P")
9060   (gnus-summary-sort 'author reverse))
9061
9062 (defun gnus-summary-sort-by-subject (&optional reverse)
9063   "Sort the summary buffer by subject alphabetically.  `Re:'s are ignored.
9064 If `case-fold-search' is non-nil, case of letters is ignored.
9065 Argument REVERSE means reverse order."
9066   (interactive "P")
9067   (gnus-summary-sort 'subject reverse))
9068
9069 (defun gnus-summary-sort-by-date (&optional reverse)
9070   "Sort the summary buffer by date.
9071 Argument REVERSE means reverse order."
9072   (interactive "P")
9073   (gnus-summary-sort 'date reverse))
9074
9075 (defun gnus-summary-sort-by-score (&optional reverse)
9076   "Sort the summary buffer by score.
9077 Argument REVERSE means reverse order."
9078   (interactive "P")
9079   (gnus-summary-sort 'score reverse))
9080
9081 (defun gnus-summary-sort-by-lines (&optional reverse)
9082   "Sort the summary buffer by the number of lines.
9083 Argument REVERSE means reverse order."
9084   (interactive "P")
9085   (gnus-summary-sort 'lines reverse))
9086
9087 (defun gnus-summary-sort-by-chars (&optional reverse)
9088   "Sort the summary buffer by article length.
9089 Argument REVERSE means reverse order."
9090   (interactive "P")
9091   (gnus-summary-sort 'chars reverse))
9092
9093 (defun gnus-summary-sort (predicate reverse)
9094   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
9095   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
9096          (article (intern (format "gnus-article-sort-by-%s" predicate)))
9097          (gnus-thread-sort-functions
9098           (if (not reverse)
9099               thread
9100             `(lambda (t1 t2)
9101                (,thread t2 t1))))
9102          (gnus-sort-gathered-threads-function
9103           gnus-thread-sort-functions)
9104          (gnus-article-sort-functions
9105           (if (not reverse)
9106               article
9107             `(lambda (t1 t2)
9108                (,article t2 t1))))
9109          (buffer-read-only)
9110          (gnus-summary-prepare-hook nil))
9111     ;; We do the sorting by regenerating the threads.
9112     (gnus-summary-prepare)
9113     ;; Hide subthreads if needed.
9114     (when (and gnus-show-threads gnus-thread-hide-subtree)
9115       (gnus-summary-hide-all-threads))))
9116
9117 ;; Summary saving commands.
9118
9119 (defun gnus-summary-save-article (&optional n not-saved)
9120   "Save the current article using the default saver function.
9121 If N is a positive number, save the N next articles.
9122 If N is a negative number, save the N previous articles.
9123 If N is nil and any articles have been marked with the process mark,
9124 save those articles instead.
9125 The variable `gnus-default-article-saver' specifies the saver function."
9126   (interactive "P")
9127   (let* ((articles (gnus-summary-work-articles n))
9128          (save-buffer (save-excursion
9129                         (nnheader-set-temp-buffer " *Gnus Save*")))
9130          (num (length articles))
9131          header file)
9132     (dolist (article articles)
9133       (setq header (gnus-summary-article-header article))
9134       (if (not (vectorp header))
9135           ;; This is a pseudo-article.
9136           (if (assq 'name header)
9137               (gnus-copy-file (cdr (assq 'name header)))
9138             (gnus-message 1 "Article %d is unsaveable" article))
9139         ;; This is a real article.
9140         (save-window-excursion
9141           (gnus-summary-select-article t nil nil article))
9142         (save-excursion
9143           (set-buffer save-buffer)
9144           (erase-buffer)
9145           (insert-buffer-substring gnus-original-article-buffer))
9146         (setq file (gnus-article-save save-buffer file num))
9147         (gnus-summary-remove-process-mark article)
9148         (unless not-saved
9149           (gnus-summary-set-saved-mark article))))
9150     (gnus-kill-buffer save-buffer)
9151     (gnus-summary-position-point)
9152     (gnus-set-mode-line 'summary)
9153     n))
9154
9155 (defun gnus-summary-pipe-output (&optional arg)
9156   "Pipe the current article to a subprocess.
9157 If N is a positive number, pipe the N next articles.
9158 If N is a negative number, pipe the N previous articles.
9159 If N is nil and any articles have been marked with the process mark,
9160 pipe those articles instead."
9161   (interactive "P")
9162   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
9163     (gnus-summary-save-article arg t))
9164   (gnus-configure-windows 'pipe))
9165
9166 (defun gnus-summary-save-article-mail (&optional arg)
9167   "Append the current article to an mail file.
9168 If N is a positive number, save the N next articles.
9169 If N is a negative number, save the N previous articles.
9170 If N is nil and any articles have been marked with the process mark,
9171 save those articles instead."
9172   (interactive "P")
9173   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
9174     (gnus-summary-save-article arg)))
9175
9176 (defun gnus-summary-save-article-rmail (&optional arg)
9177   "Append the current article to an rmail file.
9178 If N is a positive number, save the N next articles.
9179 If N is a negative number, save the N previous articles.
9180 If N is nil and any articles have been marked with the process mark,
9181 save those articles instead."
9182   (interactive "P")
9183   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
9184     (gnus-summary-save-article arg)))
9185
9186 (defun gnus-summary-save-article-file (&optional arg)
9187   "Append the current article to a file.
9188 If N is a positive number, save the N next articles.
9189 If N is a negative number, save the N previous articles.
9190 If N is nil and any articles have been marked with the process mark,
9191 save those articles instead."
9192   (interactive "P")
9193   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
9194     (gnus-summary-save-article arg)))
9195
9196 (defun gnus-summary-write-article-file (&optional arg)
9197   "Write the current article to a file, deleting the previous file.
9198 If N is a positive number, save the N next articles.
9199 If N is a negative number, save the N previous articles.
9200 If N is nil and any articles have been marked with the process mark,
9201 save those articles instead."
9202   (interactive "P")
9203   (let ((gnus-default-article-saver 'gnus-summary-write-to-file))
9204     (gnus-summary-save-article arg)))
9205
9206 (defun gnus-summary-save-article-body-file (&optional arg)
9207   "Append the current article body to a file.
9208 If N is a positive number, save the N next articles.
9209 If N is a negative number, save the N previous articles.
9210 If N is nil and any articles have been marked with the process mark,
9211 save those articles instead."
9212   (interactive "P")
9213   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
9214     (gnus-summary-save-article arg)))
9215
9216 (defun gnus-summary-pipe-message (program)
9217   "Pipe the current article through PROGRAM."
9218   (interactive "sProgram: ")
9219   (gnus-summary-select-article)
9220   (let ((mail-header-separator ""))
9221     (gnus-eval-in-buffer-window gnus-article-buffer
9222       (save-restriction
9223         (widen)
9224         (let ((start (window-start))
9225               buffer-read-only)
9226           (message-pipe-buffer-body program)
9227           (set-window-start (get-buffer-window (current-buffer)) start))))))
9228
9229 (defun gnus-get-split-value (methods)
9230   "Return a value based on the split METHODS."
9231   (let (split-name method result match)
9232     (when methods
9233       (save-excursion
9234         (set-buffer gnus-original-article-buffer)
9235         (save-restriction
9236           (nnheader-narrow-to-headers)
9237           (while methods
9238             (goto-char (point-min))
9239             (setq method (pop methods))
9240             (setq match (car method))
9241             (when (cond
9242                    ((stringp match)
9243                     ;; Regular expression.
9244                     (ignore-errors
9245                       (re-search-forward match nil t)))
9246                    ((gnus-functionp match)
9247                     ;; Function.
9248                     (save-restriction
9249                       (widen)
9250                       (setq result (funcall match gnus-newsgroup-name))))
9251                    ((consp match)
9252                     ;; Form.
9253                     (save-restriction
9254                       (widen)
9255                       (setq result (eval match)))))
9256               (setq split-name (append (cdr method) split-name))
9257               (cond ((stringp result)
9258                      (push (expand-file-name
9259                             result gnus-article-save-directory)
9260                            split-name))
9261                     ((consp result)
9262                      (setq split-name (append result split-name)))))))))
9263     (nreverse split-name)))
9264
9265 (defun gnus-valid-move-group-p (group)
9266   (and (boundp group)
9267        (symbol-name group)
9268        (symbol-value group)
9269        (gnus-get-function (gnus-find-method-for-group
9270                            (symbol-name group)) 'request-accept-article t)))
9271
9272 (defun gnus-read-move-group-name (prompt default articles prefix)
9273   "Read a group name."
9274   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
9275          (minibuffer-confirm-incomplete nil) ; XEmacs
9276          (prom
9277           (format "%s %s to:"
9278                   prompt
9279                   (if (> (length articles) 1)
9280                       (format "these %d articles" (length articles))
9281                     "this article")))
9282          (to-newsgroup
9283           (cond
9284            ((null split-name)
9285             (gnus-completing-read default prom
9286                                   gnus-active-hashtb
9287                                   'gnus-valid-move-group-p
9288                                   nil prefix
9289                                   'gnus-group-history))
9290            ((= 1 (length split-name))
9291             (gnus-completing-read (car split-name) prom
9292                                   gnus-active-hashtb
9293                                   'gnus-valid-move-group-p
9294                                   nil nil
9295                                   'gnus-group-history))
9296            (t
9297             (gnus-completing-read nil prom
9298                                   (mapcar (lambda (el) (list el))
9299                                           (nreverse split-name))
9300                                   nil nil nil
9301                                   'gnus-group-history))))
9302          (to-method (gnus-server-to-method (gnus-group-method to-newsgroup))))
9303     (when to-newsgroup
9304       (if (or (string= to-newsgroup "")
9305               (string= to-newsgroup prefix))
9306           (setq to-newsgroup default))
9307       (unless to-newsgroup
9308         (error "No group name entered"))
9309       (or (gnus-active to-newsgroup)
9310           (gnus-activate-group to-newsgroup nil nil to-method)
9311           (if (gnus-y-or-n-p (format "No such group: %s.  Create it? "
9312                                      to-newsgroup))
9313               (or (and (gnus-request-create-group to-newsgroup to-method)
9314                        (gnus-activate-group
9315                         to-newsgroup nil nil to-method)
9316                        (gnus-subscribe-group to-newsgroup))
9317                   (error "Couldn't create group %s" to-newsgroup)))
9318           (error "No such group: %s" to-newsgroup)))
9319     to-newsgroup))
9320
9321 (defun gnus-summary-save-parts (type dir n &optional reverse)
9322   "Save parts matching TYPE to DIR.
9323 If REVERSE, save parts that do not match TYPE."
9324   (interactive
9325    (list (read-string "Save parts of type: " "image/.*")
9326          (read-file-name "Save to directory: " nil nil t)
9327          current-prefix-arg))
9328   (gnus-summary-iterate n
9329     (let ((gnus-display-mime-function nil)
9330           (gnus-inhibit-treatment t))
9331       (gnus-summary-select-article))
9332     (save-excursion
9333       (set-buffer gnus-article-buffer)
9334       (let ((handles (or gnus-article-mime-handles
9335                          (mm-dissect-buffer) (mm-uu-dissect))))
9336         (when handles
9337           (gnus-summary-save-parts-1 type dir handles reverse)
9338           (unless gnus-article-mime-handles ;; Don't destroy this case.
9339             (mm-destroy-parts handles)))))))
9340
9341 (defun gnus-summary-save-parts-1 (type dir handle reverse)
9342   (if (stringp (car handle))
9343       (mapcar (lambda (h) (gnus-summary-save-parts-1 type dir h reverse))
9344               (cdr handle))
9345     (when (if reverse
9346               (not (string-match type (mm-handle-media-type handle)))
9347             (string-match type (mm-handle-media-type handle)))
9348       (let ((file (expand-file-name
9349                    (file-name-nondirectory
9350                     (or
9351                      (mail-content-type-get
9352                       (mm-handle-disposition handle) 'filename)
9353                      (concat gnus-newsgroup-name
9354                              "." (number-to-string
9355                                   (cdr gnus-article-current)))))
9356                    dir)))
9357         (unless (file-exists-p file)
9358           (mm-save-part-to-file handle file))))))
9359
9360 ;; Summary extract commands
9361
9362 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
9363   (let ((buffer-read-only nil)
9364         (article (gnus-summary-article-number))
9365         after-article b e)
9366     (unless (gnus-summary-goto-subject article)
9367       (error "No such article: %d" article))
9368     (gnus-summary-position-point)
9369     ;; If all commands are to be bunched up on one line, we collect
9370     ;; them here.
9371     (unless gnus-view-pseudos-separately
9372       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
9373             files action)
9374         (while ps
9375           (setq action (cdr (assq 'action (car ps))))
9376           (setq files (list (cdr (assq 'name (car ps)))))
9377           (while (and ps (cdr ps)
9378                       (string= (or action "1")
9379                                (or (cdr (assq 'action (cadr ps))) "2")))
9380             (push (cdr (assq 'name (cadr ps))) files)
9381             (setcdr ps (cddr ps)))
9382           (when files
9383             (when (not (string-match "%s" action))
9384               (push " " files))
9385             (push " " files)
9386             (when (assq 'execute (car ps))
9387               (setcdr (assq 'execute (car ps))
9388                       (funcall (if (string-match "%s" action)
9389                                    'format 'concat)
9390                                action
9391                                (mapconcat
9392                                 (lambda (f)
9393                                   (if (equal f " ")
9394                                       f
9395                                     (gnus-quote-arg-for-sh-or-csh f)))
9396                                 files " ")))))
9397           (setq ps (cdr ps)))))
9398     (if (and gnus-view-pseudos (not not-view))
9399         (while pslist
9400           (when (assq 'execute (car pslist))
9401             (gnus-execute-command (cdr (assq 'execute (car pslist)))
9402                                   (eq gnus-view-pseudos 'not-confirm)))
9403           (setq pslist (cdr pslist)))
9404       (save-excursion
9405         (while pslist
9406           (setq after-article (or (cdr (assq 'article (car pslist)))
9407                                   (gnus-summary-article-number)))
9408           (gnus-summary-goto-subject after-article)
9409           (forward-line 1)
9410           (setq b (point))
9411           (insert "    " (file-name-nondirectory
9412                           (cdr (assq 'name (car pslist))))
9413                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
9414           (setq e (point))
9415           (forward-line -1)             ; back to `b'
9416           (gnus-add-text-properties
9417            b (1- e) (list 'gnus-number gnus-reffed-article-number
9418                           gnus-mouse-face-prop gnus-mouse-face))
9419           (gnus-data-enter
9420            after-article gnus-reffed-article-number
9421            gnus-unread-mark b (car pslist) 0 (- e b))
9422           (push gnus-reffed-article-number gnus-newsgroup-unreads)
9423           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
9424           (setq pslist (cdr pslist)))))))
9425
9426 (defun gnus-pseudos< (p1 p2)
9427   (let ((c1 (cdr (assq 'action p1)))
9428         (c2 (cdr (assq 'action p2))))
9429     (and c1 c2 (string< c1 c2))))
9430
9431 (defun gnus-request-pseudo-article (props)
9432   (cond ((assq 'execute props)
9433          (gnus-execute-command (cdr (assq 'execute props)))))
9434   (let ((gnus-current-article (gnus-summary-article-number)))
9435     (gnus-run-hooks 'gnus-mark-article-hook)))
9436
9437 (defun gnus-execute-command (command &optional automatic)
9438   (save-excursion
9439     (gnus-article-setup-buffer)
9440     (set-buffer gnus-article-buffer)
9441     (setq buffer-read-only nil)
9442     (let ((command (if automatic command
9443                      (read-string "Command: " (cons command 0)))))
9444       (erase-buffer)
9445       (insert "$ " command "\n\n")
9446       (if gnus-view-pseudo-asynchronously
9447           (start-process "gnus-execute" (current-buffer) shell-file-name
9448                          shell-command-switch command)
9449         (call-process shell-file-name nil t nil
9450                       shell-command-switch command)))))
9451
9452 ;; Summary kill commands.
9453
9454 (defun gnus-summary-edit-global-kill (article)
9455   "Edit the \"global\" kill file."
9456   (interactive (list (gnus-summary-article-number)))
9457   (gnus-group-edit-global-kill article))
9458
9459 (defun gnus-summary-edit-local-kill ()
9460   "Edit a local kill file applied to the current newsgroup."
9461   (interactive)
9462   (setq gnus-current-headers (gnus-summary-article-header))
9463   (gnus-group-edit-local-kill
9464    (gnus-summary-article-number) gnus-newsgroup-name))
9465
9466 ;;; Header reading.
9467
9468 (defun gnus-read-header (id &optional header)
9469   "Read the headers of article ID and enter them into the Gnus system."
9470   (let ((group gnus-newsgroup-name)
9471         (gnus-override-method
9472          (or
9473           gnus-override-method
9474           (and (gnus-news-group-p gnus-newsgroup-name)
9475                (car (gnus-refer-article-methods)))))
9476         where)
9477     ;; First we check to see whether the header in question is already
9478     ;; fetched.
9479     (if (stringp id)
9480         ;; This is a Message-ID.
9481         (setq header (or header (gnus-id-to-header id)))
9482       ;; This is an article number.
9483       (setq header (or header (gnus-summary-article-header id))))
9484     (if (and header
9485              (not (gnus-summary-article-sparse-p (mail-header-number header))))
9486         ;; We have found the header.
9487         header
9488       ;; If this is a sparse article, we have to nix out its
9489       ;; previous entry in the thread hashtb.
9490       (when (and header
9491                  (gnus-summary-article-sparse-p (mail-header-number header)))
9492         (let* ((parent (gnus-parent-id (mail-header-references header)))
9493                (thread (and parent (gnus-id-to-thread parent))))
9494           (when thread
9495             (delq (assq header thread) thread))))
9496       ;; We have to really fetch the header to this article.
9497       (save-excursion
9498         (set-buffer nntp-server-buffer)
9499         (when (setq where (gnus-request-head id group))
9500           (nnheader-fold-continuation-lines)
9501           (goto-char (point-max))
9502           (insert ".\n")
9503           (goto-char (point-min))
9504           (insert "211 ")
9505           (princ (cond
9506                   ((numberp id) id)
9507                   ((cdr where) (cdr where))
9508                   (header (mail-header-number header))
9509                   (t gnus-reffed-article-number))
9510                  (current-buffer))
9511           (insert " Article retrieved.\n"))
9512         (if (or (not where)
9513                 (not (setq header (car (gnus-get-newsgroup-headers nil t)))))
9514             ()                          ; Malformed head.
9515           (unless (gnus-summary-article-sparse-p (mail-header-number header))
9516             (when (and (stringp id)
9517                        (not (string= (gnus-group-real-name group)
9518                                      (car where))))
9519               ;; If we fetched by Message-ID and the article came
9520               ;; from a different group, we fudge some bogus article
9521               ;; numbers for this article.
9522               (mail-header-set-number header gnus-reffed-article-number))
9523             (save-excursion
9524               (set-buffer gnus-summary-buffer)
9525               (decf gnus-reffed-article-number)
9526               (gnus-remove-header (mail-header-number header))
9527               (push header gnus-newsgroup-headers)
9528               (setq gnus-current-headers header)
9529               (push (mail-header-number header) gnus-newsgroup-limit)))
9530           header)))))
9531
9532 (defun gnus-remove-header (number)
9533   "Remove header NUMBER from `gnus-newsgroup-headers'."
9534   (if (and gnus-newsgroup-headers
9535            (= number (mail-header-number (car gnus-newsgroup-headers))))
9536       (pop gnus-newsgroup-headers)
9537     (let ((headers gnus-newsgroup-headers))
9538       (while (and (cdr headers)
9539                   (not (= number (mail-header-number (cadr headers)))))
9540         (pop headers))
9541       (when (cdr headers)
9542         (setcdr headers (cddr headers))))))
9543
9544 ;;;
9545 ;;; summary highlights
9546 ;;;
9547
9548 (defun gnus-highlight-selected-summary ()
9549   "Highlight selected article in summary buffer."
9550   ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
9551   (when gnus-summary-selected-face
9552     (save-excursion
9553       (let* ((beg (progn (beginning-of-line) (point)))
9554              (end (progn (end-of-line) (point)))
9555              ;; Fix by Mike Dugan <dugan@bucrf16.bu.edu>.
9556              (from (if (get-text-property beg gnus-mouse-face-prop)
9557                        beg
9558                      (or (next-single-property-change
9559                           beg gnus-mouse-face-prop nil end)
9560                          beg)))
9561              (to
9562               (if (= from end)
9563                   (- from 2)
9564                 (or (next-single-property-change
9565                      from gnus-mouse-face-prop nil end)
9566                     end))))
9567         ;; If no mouse-face prop on line we will have to = from = end,
9568         ;; so we highlight the entire line instead.
9569         (when (= (+ to 2) from)
9570           (setq from beg)
9571           (setq to end))
9572         (if gnus-newsgroup-selected-overlay
9573             ;; Move old overlay.
9574             (gnus-move-overlay
9575              gnus-newsgroup-selected-overlay from to (current-buffer))
9576           ;; Create new overlay.
9577           (gnus-overlay-put
9578            (setq gnus-newsgroup-selected-overlay (gnus-make-overlay from to))
9579            'face gnus-summary-selected-face))))))
9580
9581 ;; New implementation by Christian Limpach <Christian.Limpach@nice.ch>.
9582 (defun gnus-summary-highlight-line ()
9583   "Highlight current line according to `gnus-summary-highlight'."
9584   (let* ((list gnus-summary-highlight)
9585          (p (point))
9586          (end (progn (end-of-line) (point)))
9587          ;; now find out where the line starts and leave point there.
9588          (beg (progn (beginning-of-line) (point)))
9589          (article (gnus-summary-article-number))
9590          (score (or (cdr (assq (or article gnus-current-article)
9591                                gnus-newsgroup-scored))
9592                     gnus-summary-default-score 0))
9593          (mark (or (gnus-summary-article-mark) gnus-unread-mark))
9594          (inhibit-read-only t))
9595     ;; Eval the cars of the lists until we find a match.
9596     (let ((default gnus-summary-default-score))
9597       (while (and list
9598                   (not (eval (caar list))))
9599         (setq list (cdr list))))
9600     (let ((face (cdar list)))
9601       (unless (eq face (get-text-property beg 'face))
9602         (gnus-put-text-property-excluding-characters-with-faces
9603          beg end 'face
9604          (setq face (if (boundp face) (symbol-value face) face)))
9605         (when gnus-summary-highlight-line-function
9606           (funcall gnus-summary-highlight-line-function article face))))
9607     (goto-char p)))
9608
9609 (defun gnus-update-read-articles (group unread &optional compute)
9610   "Update the list of read articles in GROUP."
9611   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
9612          (entry (gnus-gethash group gnus-newsrc-hashtb))
9613          (info (nth 2 entry))
9614          (prev 1)
9615          (unread (sort (copy-sequence unread) '<))
9616          read)
9617     (if (or (not info) (not active))
9618         ;; There is no info on this group if it was, in fact,
9619         ;; killed.  Gnus stores no information on killed groups, so
9620         ;; there's nothing to be done.
9621         ;; One could store the information somewhere temporarily,
9622         ;; perhaps...  Hmmm...
9623         ()
9624       ;; Remove any negative articles numbers.
9625       (while (and unread (< (car unread) 0))
9626         (setq unread (cdr unread)))
9627       ;; Remove any expired article numbers
9628       (while (and unread (< (car unread) (car active)))
9629         (setq unread (cdr unread)))
9630       ;; Compute the ranges of read articles by looking at the list of
9631       ;; unread articles.
9632       (while unread
9633         (when (/= (car unread) prev)
9634           (push (if (= prev (1- (car unread))) prev
9635                   (cons prev (1- (car unread))))
9636                 read))
9637         (setq prev (1+ (car unread)))
9638         (setq unread (cdr unread)))
9639       (when (<= prev (cdr active))
9640         (push (cons prev (cdr active)) read))
9641       (setq read (if (> (length read) 1) (nreverse read) read))
9642       (if compute
9643           read
9644         (save-excursion
9645           (let (setmarkundo)
9646             ;; Propagate the read marks to the backend.
9647             (when (gnus-check-backend-function 'request-set-mark group)
9648               (let ((del (gnus-remove-from-range (gnus-info-read info) read))
9649                     (add (gnus-remove-from-range read (gnus-info-read info))))
9650                 (when (or add del)
9651                   (unless (gnus-check-group group)
9652                     (error "Can't open server for %s" group))
9653                   (gnus-request-set-mark
9654                    group (delq nil (list (if add (list add 'add '(read)))
9655                                          (if del (list del 'del '(read))))))
9656                   (setq setmarkundo
9657                         `(gnus-request-set-mark
9658                           ,group
9659                           ',(delq nil (list
9660                                        (if del (list del 'add '(read)))
9661                                        (if add (list add 'del '(read))))))))))
9662             (set-buffer gnus-group-buffer)
9663             (gnus-undo-register
9664               `(progn
9665                  (gnus-info-set-marks ',info ',(gnus-info-marks info) t)
9666                  (gnus-info-set-read ',info ',(gnus-info-read info))
9667                  (gnus-get-unread-articles-in-group ',info 
9668                                                     (gnus-active ,group))
9669                  (gnus-group-update-group ,group t)
9670                  ,setmarkundo))))
9671         ;; Enter this list into the group info.
9672         (gnus-info-set-read info read)
9673         ;; Set the number of unread articles in gnus-newsrc-hashtb.
9674         (gnus-get-unread-articles-in-group info (gnus-active group))
9675         t))))
9676
9677 (defun gnus-offer-save-summaries ()
9678   "Offer to save all active summary buffers."
9679   (save-excursion
9680     (let ((buflist (buffer-list))
9681           buffers bufname)
9682       ;; Go through all buffers and find all summaries.
9683       (while buflist
9684         (and (setq bufname (buffer-name (car buflist)))
9685              (string-match "Summary" bufname)
9686              (save-excursion
9687                (set-buffer bufname)
9688                ;; We check that this is, indeed, a summary buffer.
9689                (and (eq major-mode 'gnus-summary-mode)
9690                     ;; Also make sure this isn't bogus.
9691                     gnus-newsgroup-prepared
9692                     ;; Also make sure that this isn't a dead summary buffer.
9693                     (not gnus-dead-summary-mode)))
9694              (push bufname buffers))
9695         (setq buflist (cdr buflist)))
9696       ;; Go through all these summary buffers and offer to save them.
9697       (when buffers
9698         (map-y-or-n-p
9699          "Update summary buffer %s? "
9700          (lambda (buf)
9701            (switch-to-buffer buf)
9702            (gnus-summary-exit))
9703          buffers)))))
9704
9705
9706 ;;; @ for mime-partial
9707 ;;;
9708
9709 (defun gnus-request-partial-message ()
9710   (save-excursion
9711     (let ((number (gnus-summary-article-number))
9712           (group gnus-newsgroup-name)
9713           (mother gnus-article-buffer))
9714       (set-buffer (get-buffer-create " *Partial Article*"))
9715       (erase-buffer)
9716       (setq mime-preview-buffer mother)
9717       (gnus-request-article-this-buffer number group)
9718       (mime-parse-buffer)
9719       )))
9720
9721 (autoload 'mime-combine-message/partial-pieces-automatically
9722   "mime-partial"
9723   "Internal method to combine message/partial messages automatically.")
9724
9725 (mime-add-condition
9726  'action '((type . message)(subtype . partial)
9727            (major-mode . gnus-original-article-mode)
9728            (method . mime-combine-message/partial-pieces-automatically)
9729            (summary-buffer-exp . gnus-summary-buffer)
9730            (request-partial-message-method . gnus-request-partial-message)
9731            ))
9732
9733
9734 ;;; @ for message/rfc822
9735 ;;;
9736
9737 (defun gnus-mime-extract-message/rfc822 (entity situation)
9738   (let (group article num cwin swin cur)
9739     (with-temp-buffer
9740       (mime-insert-entity-content entity)
9741       (setq group (or (cdr (assq 'group situation))
9742                       (completing-read "Group: "
9743                                        gnus-active-hashtb
9744                                        nil
9745                                        (gnus-read-active-file-p)
9746                                        gnus-newsgroup-name))
9747             article (gnus-request-accept-article group)))
9748     (when (and (consp article)
9749                (numberp (setq article (cdr article))))
9750       (setq num (1+ (or (cdr (assq 'number situation)) 0))
9751             cwin (get-buffer-window (current-buffer) t))
9752       (save-window-excursion
9753         (if (setq swin (get-buffer-window gnus-summary-buffer t))
9754             (select-window swin)
9755           (set-buffer gnus-summary-buffer))
9756         (setq cur gnus-current-article)
9757         (forward-line num)
9758         (let (gnus-show-threads)
9759           (gnus-summary-goto-subject article t))
9760         (gnus-summary-clear-mark-forward 1)
9761         (gnus-summary-goto-subject cur))
9762       (when (and cwin (window-frame cwin))
9763         (select-frame (window-frame cwin)))
9764       (when (boundp 'mime-acting-situation-to-override)
9765         (set-alist 'mime-acting-situation-to-override
9766                    'group
9767                    group)
9768         (set-alist 'mime-acting-situation-to-override
9769                    'after-method
9770                    `(progn
9771                       (save-current-buffer
9772                         (set-buffer gnus-group-buffer)
9773                         (gnus-activate-group ,group))
9774                       (gnus-summary-goto-article ,cur
9775                                                  gnus-show-all-headers)))
9776         (set-alist 'mime-acting-situation-to-override
9777                    'number num)))))
9778
9779 (mime-add-condition
9780  'action '((type . message)(subtype . rfc822)
9781            (major-mode . gnus-original-article-mode)
9782            (method . gnus-mime-extract-message/rfc822)
9783            (mode . "extract")
9784            ))
9785
9786 (mime-add-condition
9787  'action '((type . message)(subtype . news)
9788            (major-mode . gnus-original-article-mode)
9789            (method . gnus-mime-extract-message/rfc822)
9790            (mode . "extract")
9791            ))
9792
9793 (defun gnus-mime-extract-multipart (entity situation)
9794   (let ((children (mime-entity-children entity))
9795         mime-acting-situation-to-override
9796         f)
9797     (while children
9798       (mime-play-entity (car children)
9799                         (cons (assq 'mode situation)
9800                               mime-acting-situation-to-override))
9801       (setq children (cdr children)))
9802     (if (setq f (cdr (assq 'after-method
9803                            mime-acting-situation-to-override)))
9804         (eval f)
9805       )))
9806
9807 (mime-add-condition
9808  'action '((type . multipart)
9809            (method . gnus-mime-extract-multipart)
9810            (mode . "extract")
9811            )
9812  'with-default)
9813
9814
9815 ;;; @ end
9816 ;;;
9817
9818 (defun gnus-summary-setup-default-charset ()
9819   "Setup newsgroup default charset."
9820   (if (equal gnus-newsgroup-name "nndraft:drafts")
9821       (setq gnus-newsgroup-charset nil)
9822     (let* ((name (and gnus-newsgroup-name
9823                       (gnus-group-real-name gnus-newsgroup-name)))
9824            (ignored-charsets
9825             (or gnus-newsgroup-ephemeral-ignored-charsets
9826                 (append
9827                  (and gnus-newsgroup-name
9828                       (or (gnus-group-find-parameter gnus-newsgroup-name
9829                                                      'ignored-charsets t)
9830                           (let ((alist gnus-group-ignored-charsets-alist)
9831                                 elem (charsets nil))
9832                             (while (setq elem (pop alist))
9833                               (when (and name
9834                                          (string-match (car elem) name))
9835                                 (setq alist nil
9836                                       charsets (cdr elem))))
9837                             charsets)))
9838                  gnus-newsgroup-ignored-charsets))))
9839       (setq gnus-newsgroup-charset
9840             (or gnus-newsgroup-ephemeral-charset
9841                 (and gnus-newsgroup-name
9842                      (or (gnus-group-find-parameter gnus-newsgroup-name 'charset)
9843                          (let ((alist gnus-group-charset-alist)
9844                                elem charset)
9845                            (while (setq elem (pop alist))
9846                              (when (and name
9847                                         (string-match (car elem) name))
9848                                (setq alist nil
9849                                      charset (cadr elem))))
9850                            charset)))
9851                 gnus-default-charset))
9852       (set (make-local-variable 'gnus-newsgroup-ignored-charsets)
9853            ignored-charsets))))
9854
9855 ;;;
9856 ;;; Mime Commands
9857 ;;;
9858
9859 (defun gnus-summary-display-buttonized (&optional show-all-parts)
9860   "Display the current article buffer fully MIME-buttonized.
9861 If SHOW-ALL-PARTS (the prefix) is non-nil, all multipart/* parts are
9862 treated as multipart/mixed."
9863   (interactive "P")
9864   (require 'gnus-art)
9865   (let ((gnus-unbuttonized-mime-types nil)
9866         (gnus-mime-display-multipart-as-mixed show-all-parts))
9867     (gnus-summary-show-article)))
9868
9869 (defun gnus-summary-repair-multipart (article)
9870   "Add a Content-Type header to a multipart article without one."
9871   (interactive (list (gnus-summary-article-number)))
9872   (gnus-with-article article
9873     (message-narrow-to-head)
9874     (goto-char (point-max))
9875     (widen)
9876     (when (search-forward "\n--" nil t)
9877       (let ((separator (buffer-substring (point) (gnus-point-at-eol))))
9878         (message-narrow-to-head)
9879         (message-remove-header "Mime-Version")
9880         (message-remove-header "Content-Type")
9881         (goto-char (point-max))
9882         (insert (format "Content-Type: multipart/mixed; boundary=\"%s\"\n"
9883                         separator))
9884         (insert "Mime-Version: 1.0\n")
9885         (widen))))
9886   (let (gnus-mark-article-hook)
9887     (gnus-summary-select-article t t nil article)))
9888
9889 (defun gnus-summary-toggle-display-buttonized ()
9890   "Toggle the buttonizing of the article buffer."
9891   (interactive)
9892   (require 'gnus-art)
9893   (if (setq gnus-inhibit-mime-unbuttonizing
9894             (not gnus-inhibit-mime-unbuttonizing))
9895       (let ((gnus-unbuttonized-mime-types nil))
9896         (gnus-summary-show-article))
9897     (gnus-summary-show-article)))
9898
9899 ;;;
9900 ;;; Intelli-mouse commmands
9901 ;;;
9902
9903 (defun gnus-wheel-summary-scroll (event)
9904   (interactive "e")
9905   (let ((amount (if (memq 'shift (event-modifiers event))
9906                     (car gnus-wheel-scroll-amount)
9907                   (cdr gnus-wheel-scroll-amount)))
9908         (direction (- (* (static-if (featurep 'xemacs)
9909                              (event-button event)
9910                            (cond ((eq 'mouse-4 (event-basic-type event))
9911                                   4)
9912                                  ((eq 'mouse-5 (event-basic-type event))
9913                                   5)))
9914                          2) 9))
9915         edge)
9916     (gnus-summary-scroll-up (* amount direction))
9917     (when (gnus-eval-in-buffer-window gnus-article-buffer
9918             (save-restriction
9919               (widen)
9920               (and (if (< 0 direction)
9921                        (gnus-article-next-page 0)
9922                      (gnus-article-prev-page 0)
9923                      (bobp))
9924                    (if (setq edge (get-text-property
9925                                    (point-min) 'gnus-wheel-edge))
9926                        (setq edge (* edge direction))
9927                      (setq edge -1))
9928                    (or (plusp edge)
9929                        (let ((buffer-read-only nil)
9930                              (inhibit-read-only t))
9931                          (put-text-property (point-min) (point-max)
9932                                             'gnus-wheel-edge direction)
9933                          nil))
9934                    (or (> edge gnus-wheel-edge-resistance)
9935                        (let ((buffer-read-only nil)
9936                              (inhibit-read-only t))
9937                          (put-text-property (point-min) (point-max)
9938                                             'gnus-wheel-edge
9939                                             (* (1+ edge) direction))
9940                          nil))
9941                    (eq last-command 'gnus-wheel-summary-scroll))))
9942       (gnus-summary-next-article nil nil (minusp direction)))))
9943
9944 (defun gnus-wheel-install ()
9945   "Enable mouse wheel support on summary window."
9946   (when gnus-use-wheel
9947     (let ((keys
9948            '([(mouse-4)] [(shift mouse-4)] [(mouse-5)] [(shift mouse-5)])))
9949       (dolist (key keys)
9950         (define-key gnus-summary-mode-map key
9951           'gnus-wheel-summary-scroll)))))
9952
9953 (add-hook 'gnus-summary-mode-hook 'gnus-wheel-install)
9954
9955 ;;;
9956 ;;; Traditional PGP commmands
9957 ;;;
9958
9959 (defun gnus-summary-decrypt-article (&optional force)
9960   "Decrypt the current article in traditional PGP way.
9961 This will have permanent effect only in mail groups.
9962 If FORCE is non-nil, allow editing of articles even in read-only
9963 groups."
9964   (interactive "P")
9965   (gnus-summary-select-article t)
9966   (gnus-eval-in-buffer-window gnus-article-buffer
9967     (save-excursion
9968       (save-restriction
9969         (widen)
9970         (goto-char (point-min))
9971         (unless (re-search-forward (car pgg-armor-header-lines) nil t)
9972           (error "Not a traditional PGP message!"))
9973         (let ((armor-start (match-beginning 0)))
9974           (if (and (pgg-decrypt-region armor-start (point-max))
9975                    (or force (not (gnus-group-read-only-p))))
9976               (let ((inhibit-read-only t)
9977                     buffer-read-only)
9978                 (delete-region armor-start
9979                                (progn
9980                                  (re-search-forward "^-+END PGP" nil t)
9981                                  (beginning-of-line 2)
9982                                  (point)))
9983                 (insert-buffer-substring pgg-output-buffer))))))))
9984
9985 (defun gnus-summary-verify-article ()
9986   "Verify the current article in traditional PGP way."
9987   (interactive)
9988   (save-excursion
9989     (set-buffer gnus-original-article-buffer)
9990     (goto-char (point-min))
9991     (unless (re-search-forward "^-+BEGIN PGP SIGNED MESSAGE" nil t)
9992       (error "Not a traditional PGP message!"))
9993     (re-search-forward "^-+END PGP" nil t)
9994     (beginning-of-line 2)
9995     (call-interactively (function pgg-verify-region))))
9996
9997 ;;;
9998 ;;; Generic summary marking commands
9999 ;;;
10000
10001 (defvar gnus-summary-marking-alist
10002   '((read gnus-del-mark "d")
10003     (unread gnus-unread-mark "u")
10004     (ticked gnus-ticked-mark "!")
10005     (dormant gnus-dormant-mark "?")
10006     (expirable gnus-expirable-mark "e"))
10007   "An alist of names/marks/keystrokes.")
10008
10009 (defvar gnus-summary-generic-mark-map (make-sparse-keymap))
10010 (defvar gnus-summary-mark-map)
10011
10012 (defun gnus-summary-make-all-marking-commands ()
10013   (define-key gnus-summary-mark-map "M" gnus-summary-generic-mark-map)
10014   (dolist (elem gnus-summary-marking-alist)
10015     (apply 'gnus-summary-make-marking-command elem)))
10016
10017 (defun gnus-summary-make-marking-command (name mark keystroke)
10018   (let ((map (make-sparse-keymap)))
10019     (define-key gnus-summary-generic-mark-map keystroke map)
10020     (dolist (lway `((next "next" next nil "n")
10021                     (next-unread "next unread" next t "N")
10022                     (prev "previous" prev nil "p")
10023                     (prev-unread "previous unread" prev t "P")
10024                     (nomove "" nil nil ,keystroke)))
10025       (let ((func (gnus-summary-make-marking-command-1
10026                    mark (car lway) lway name)))
10027         (setq func (eval func))
10028         (define-key map (nth 4 lway) func)))))
10029
10030 (defun gnus-summary-make-marking-command-1 (mark way lway name)
10031   `(defun ,(intern
10032             (format "gnus-summary-put-mark-as-%s%s"
10033                     name (if (eq way 'nomove)
10034                              ""
10035                            (concat "-" (symbol-name way)))))
10036      (n)
10037      ,(format
10038        "Mark the current article as %s%s.
10039 If N, the prefix, then repeat N times.
10040 If N is negative, move in reverse order.
10041 The difference between N and the actual number of articles marked is
10042 returned."
10043        name (car (cdr lway)))
10044      (interactive "p")
10045      (gnus-summary-generic-mark n ,mark ',(nth 2 lway) ,(nth 3 lway))))
10046
10047 (defun gnus-summary-generic-mark (n mark move unread)
10048   "Mark N articles with MARK."
10049   (unless (eq major-mode 'gnus-summary-mode)
10050     (error "This command can only be used in the summary buffer"))
10051   (gnus-summary-show-thread)
10052   (let ((nummove
10053          (cond
10054           ((eq move 'next) 1)
10055           ((eq move 'prev) -1)
10056           (t 0))))
10057     (if (zerop nummove)
10058         (setq n 1)
10059       (when (< n 0)
10060         (setq n (abs n)
10061               nummove (* -1 nummove))))
10062     (while (and (> n 0)
10063                 (gnus-summary-mark-article nil mark)
10064                 (zerop (gnus-summary-next-subject nummove unread t)))
10065       (setq n (1- n)))
10066     (when (/= 0 n)
10067       (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
10068     (gnus-summary-recenter)
10069     (gnus-summary-position-point)
10070     (gnus-set-mode-line 'summary)
10071     n))
10072
10073 (gnus-summary-make-all-marking-commands)
10074
10075 (gnus-ems-redefine)
10076
10077 (provide 'gnus-sum)
10078
10079 (run-hooks 'gnus-sum-load-hook)
10080
10081 ;;; gnus-sum.el ends here